gem5  v21.2.1.1
image_file_data.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2004 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
30 
31 #include <fcntl.h>
32 #include <sys/mman.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 
37 #include <cstdio>
38 #include <vector>
39 
40 #include "base/logging.hh"
41 
42 namespace gem5
43 {
44 
45 GEM5_DEPRECATED_NAMESPACE(Loader, loader);
46 namespace loader
47 {
48 
49 static bool
51 {
52  uint8_t buf[2] = {0};
53  size_t sz = pread(fd, buf, 2, 0);
54  panic_if(sz != 2, "Couldn't read magic bytes from object file");
55  return ((buf[0] == 0x1f) && (buf[1] == 0x8b));
56 }
57 
58 static int
60 {
61  const size_t blk_sz = 4096;
62 
63  gzFile fdz = gzdopen(fd, "rb");
64  if (!fdz) {
65  return -1;
66  }
67 
68  size_t tmp_len = strlen(P_tmpdir);
69  char *tmpnam = (char*) malloc(tmp_len + 20);
70  strcpy(tmpnam, P_tmpdir);
71  strcpy(tmpnam+tmp_len, "/gem5-gz-obj-XXXXXX"); // 19 chars
72  fd = mkstemp(tmpnam); // repurposing fd variable for output
73  if (fd < 0) {
74  free(tmpnam);
75  gzclose(fdz);
76  return fd;
77  }
78 
79  if (unlink(tmpnam) != 0)
80  warn("couldn't remove temporary file %s\n", tmpnam);
81 
82  free(tmpnam);
83 
84  auto buf = new uint8_t[blk_sz];
85  int r; // size of (r)emaining uncopied data in (buf)fer
86  while ((r = gzread(fdz, buf, blk_sz)) > 0) {
87  auto p = buf; // pointer into buffer
88  while (r > 0) {
89  auto sz = write(fd, p, r);
90  assert(sz <= r);
91  r -= sz;
92  p += sz;
93  }
94  }
95  delete[] buf;
96  gzclose(fdz);
97  if (r < 0) { // error
98  close(fd);
99  return -1;
100  }
101  assert(r == 0); // finished successfully
102  return fd; // return fd to decompressed temporary file for mmap()'ing
103 }
104 
105 ImageFileData::ImageFileData(const std::string &fname)
106 {
107  _filename = fname;
108 
109  // Open the file.
110  int fd = open(fname.c_str(), O_RDONLY);
111  panic_if(fd < 0, "Failed to open file %s.\n", fname);
112 
113  // Decompress GZ files.
114  if (hasGzipMagic(fd)) {
115  fd = doGzipLoad(fd);
116  panic_if(fd < 0, "Failed to unzip file %s.\n", fname);
117  }
118 
119  // Find the length of the file by seeking to the end.
120  off_t off = lseek(fd, 0, SEEK_END);
121  fatal_if(off < 0, "Failed to determine size of file %s.\n", fname);
122  _len = static_cast<size_t>(off);
123 
124  // Mmap the whole shebang.
125  _data = (uint8_t *)mmap(NULL, _len, PROT_READ, MAP_SHARED, fd, 0);
126  close(fd);
127 
128  panic_if(_data == MAP_FAILED, "Failed to mmap file %s.\n", fname);
129 }
130 
132 {
133  munmap((void *)_data, _len);
134 }
135 
136 } // namespace loader
137 } // namespace gem5
warn
#define warn(...)
Definition: logging.hh:246
gem5::loader::ImageFileData::_data
uint8_t * _data
Definition: image_file_data.hh:49
gem5::loader::ImageFileData::_filename
std::string _filename
Definition: image_file_data.hh:48
gem5::ArmISA::fd
Bitfield< 14, 12 > fd
Definition: types.hh:150
gem5::loader::doGzipLoad
static int doGzipLoad(int fd)
Definition: image_file_data.cc:59
gem5::loader::hasGzipMagic
static bool hasGzipMagic(int fd)
Definition: image_file_data.cc:50
image_file_data.hh
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::GEM5_DEPRECATED_NAMESPACE
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:204
gem5::loader::ImageFileData::_len
size_t _len
Definition: image_file_data.hh:50
gem5::loader::ImageFileData::~ImageFileData
virtual ~ImageFileData()
Definition: image_file_data.cc:131
logging.hh
gem5::MipsISA::r
r
Definition: pra_constants.hh:98
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:226
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: tlb.cc:60
gem5::loader::ImageFileData::ImageFileData
ImageFileData(const std::string &f_name)
Definition: image_file_data.cc:105

Generated on Wed May 4 2022 12:13:51 for gem5 by doxygen 1.8.17