gem5  v22.1.0.0
dtb_file.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 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 
29 #include "base/loader/dtb_file.hh"
30 
31 #include <sys/mman.h>
32 #include <unistd.h>
33 
34 #include <cassert>
35 
36 #include "fdt.h"
37 #include "libfdt.h"
38 #include "sim/byteswap.hh"
39 
40 namespace gem5
41 {
42 
43 GEM5_DEPRECATED_NAMESPACE(Loader, loader);
44 namespace loader
45 {
46 
47 DtbFile::DtbFile(const std::string &filename) :
49 {
50  panic_if(fdt_magic((const void *)imageData->data()) != FDT_MAGIC,
51  "File %s doesn't seem to be a DTB.\n", filename);
52  fileDataMmapped = true;
53  fileData = const_cast<uint8_t *>(imageData->data());
54  length = imageData->len();
55 }
56 
58 {
59  // Make sure to clean up memory properly depending
60  // on how buffer was allocated.
61  if (!fileDataMmapped)
62  delete [] fileData;
63 }
64 
65 bool
66 DtbFile::addBootData(const char *_cmdline, size_t cmdline_len,
67  off_t initrd_start, size_t initrd_len)
68 {
69  const char *root_path = "/";
70  const char *node_name = "chosen";
71  const char *full_path_node_name = "/chosen";
72  const char *bootargs_property_name = "bootargs";
73  const char *linux_initrd_start_property_name = "linux,initrd-start";
74  const char *linux_initrd_end_property_name = "linux,initrd-end";
75 
76  // Make a new buffer that has extra space to add nodes/properties
77  int newLen = 2 * length;
78  uint8_t *fdt_buf_w_space = new uint8_t[newLen];
79  // Copy and unpack flattened device tree into new buffer
80  int ret = fdt_open_into((void *)fileData, (void *)fdt_buf_w_space, newLen);
81  if (ret < 0) {
82  warn("Error resizing buffer of flattened device tree, "
83  "errno: %d\n", ret);
84  delete [] fdt_buf_w_space;
85  return false;
86  }
87 
88  // First try finding the /chosen node in the dtb
89  int offset = fdt_path_offset((void *)fdt_buf_w_space, full_path_node_name);
90  if (offset < 0) {
91  // try adding the node by walking dtb tree to proper insertion point
92  offset = fdt_path_offset((void *)fdt_buf_w_space, root_path);
93  offset = fdt_add_subnode((void *)fdt_buf_w_space, offset, node_name);
94  // if we successfully add the subnode, get the offset
95  if (offset >= 0)
96  offset = fdt_path_offset((void *)fdt_buf_w_space,
97  full_path_node_name);
98 
99  if (offset < 0) {
100  warn("Error finding or adding \"chosen\" subnode to flattened "
101  "device tree, errno: %d\n", offset);
102  delete [] fdt_buf_w_space;
103  return false;
104  }
105  }
106 
107  // Set the bootargs property in the /chosen node
108  ret = fdt_setprop((void *)fdt_buf_w_space, offset, bootargs_property_name,
109  (const void *)_cmdline, cmdline_len+1);
110  if (ret < 0) {
111  warn("Error setting \"bootargs\" property to flattened device tree, "
112  "errno: %d\n", ret);
113  delete [] fdt_buf_w_space;
114  return false;
115  }
116 
117  if (initrd_len != 0) {
118  // Set the linux,initrd-start property in the /chosen node
119  ret = fdt_setprop_u64((void *)fdt_buf_w_space, offset,
120  linux_initrd_start_property_name,
121  (uint64_t)initrd_start);
122  if (ret < 0) {
123  warn("Error setting \"linux,initrd-start\" property to flattened "
124  "device tree, errno: %d\n", ret);
125  delete [] fdt_buf_w_space;
126  return false;
127  }
128 
129  // Set the computed linux,initrd-end property in the /chosen node
130  ret = fdt_setprop_u64((void *)fdt_buf_w_space, offset,
131  linux_initrd_end_property_name,
132  (uint64_t)initrd_start + initrd_len);
133  if (ret < 0) {
134  warn("Error setting \"linux,initrd-len\" property to flattened "
135  "device tree, errno: %d\n", ret);
136  delete [] fdt_buf_w_space;
137  return false;
138  }
139  }
140 
141  // Repack the dtb for kernel use
142  ret = fdt_pack((void *)fdt_buf_w_space);
143  if (ret < 0) {
144  warn("Error re-packing flattened device tree structure, "
145  "errno: %d\n", ret);
146  delete [] fdt_buf_w_space;
147  return false;
148  }
149 
150  // clean up old buffer and set to new fdt blob
151  if (!fileDataMmapped)
152  delete [] fileData;
153  fileData = fdt_buf_w_space;
154  fileDataMmapped = false;
155  length = newLen;
156 
157  return true;
158 }
159 
160 Addr
162 {
163  void *fd = (void *)fileData;
164 
165  int offset = fdt_path_offset(fd, "/cpus/cpu@0");
166  int len;
167 
168  const void *temp = fdt_getprop(fd, offset, "cpu-release-addr", &len);
169  Addr rel_addr = 0;
170 
171  if (len > 3)
172  rel_addr = betoh(*static_cast<const uint32_t *>(temp));
173  if (len == 8) {
174  rel_addr = (rel_addr << 32) |
175  betoh(*(static_cast<const uint32_t *>(temp) + 1));
176  }
177 
178  return rel_addr;
179 }
180 
183 {
184  if (fileDataMmapped)
185  return {{ "data", imageData }};
186  else
187  return {{ "data", 0, fileData, length }};
188 }
189 
190 } // namespace loader
191 } // namespace gem5
bool addBootData(const char *_cmdline, size_t cmdline_len, off_t initrd_addr, size_t initrd_len)
Adds the passed in Command Line options and initrd for the kernel to the proper location in the devic...
Definition: dtb_file.cc:66
uint8_t * fileData
Definition: dtb_file.hh:54
Addr findReleaseAddr()
Parse the DTB file enough to find the provided release address and return it.
Definition: dtb_file.cc:161
DtbFile(const std::string &name)
Definition: dtb_file.cc:47
MemoryImage buildImage() const override
Definition: dtb_file.cc:182
bool fileDataMmapped
Bool marking if this dtb file has replaced the original read in DTB file with a new modified buffer.
Definition: dtb_file.hh:53
ImageFileDataPtr imageData
Definition: image_file.hh:49
This implements an image file format to support loading and modifying flattened device tree blobs for...
#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
uint16_t len
Definition: helpers.cc:62
#define warn(...)
Definition: logging.hh:246
Bitfield< 14, 12 > fd
Definition: types.hh:150
Bitfield< 23, 0 > offset
Definition: types.hh:144
std::shared_ptr< ImageFileData > ImageFileDataPtr
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
T betoh(T value)
Definition: byteswap.hh:175
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1