gem5  v20.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 Loader
41 {
42 
43 DtbFile::DtbFile(const std::string &filename) :
45 {
46  panic_if(fdt_magic((const void *)imageData->data()) != FDT_MAGIC,
47  "File %s doesn't seem to be a DTB.\n", filename);
48  fileDataMmapped = true;
49  fileData = const_cast<uint8_t *>(imageData->data());
50  length = imageData->len();
51 }
52 
54 {
55  // Make sure to clean up memory properly depending
56  // on how buffer was allocated.
57  if (!fileDataMmapped)
58  delete [] fileData;
59 }
60 
61 bool
62 DtbFile::addBootCmdLine(const char *_args, size_t len)
63 {
64  const char *root_path = "/";
65  const char *node_name = "chosen";
66  const char *full_path_node_name = "/chosen";
67  const char *property_name = "bootargs";
68 
69  // Make a new buffer that has extra space to add nodes/properties
70  int newLen = 2 * length;
71  uint8_t *fdt_buf_w_space = new uint8_t[newLen];
72  // Copy and unpack flattened device tree into new buffer
73  int ret = fdt_open_into((void *)fileData, (void *)fdt_buf_w_space, newLen);
74  if (ret < 0) {
75  warn("Error resizing buffer of flattened device tree, "
76  "errno: %d\n", ret);
77  delete [] fdt_buf_w_space;
78  return false;
79  }
80 
81  // First try finding the /chosen node in the dtb
82  int offset = fdt_path_offset((void *)fdt_buf_w_space, full_path_node_name);
83  if (offset < 0) {
84  // try adding the node by walking dtb tree to proper insertion point
85  offset = fdt_path_offset((void *)fdt_buf_w_space, root_path);
86  offset = fdt_add_subnode((void *)fdt_buf_w_space, offset, node_name);
87  // if we successfully add the subnode, get the offset
88  if (offset >= 0)
89  offset = fdt_path_offset((void *)fdt_buf_w_space,
90  full_path_node_name);
91 
92  if (offset < 0) {
93  warn("Error finding or adding \"chosen\" subnode to flattened "
94  "device tree, errno: %d\n", offset);
95  delete [] fdt_buf_w_space;
96  return false;
97  }
98  }
99 
100  // Set the bootargs property in the /chosen node
101  ret = fdt_setprop((void *)fdt_buf_w_space, offset, property_name,
102  (const void *)_args, len+1);
103  if (ret < 0) {
104  warn("Error setting \"bootargs\" property to flattened device tree, "
105  "errno: %d\n", ret);
106  delete [] fdt_buf_w_space;
107  return false;
108  }
109 
110  // Repack the dtb for kernel use
111  ret = fdt_pack((void *)fdt_buf_w_space);
112  if (ret < 0) {
113  warn("Error re-packing flattened device tree structure, "
114  "errno: %d\n", ret);
115  delete [] fdt_buf_w_space;
116  return false;
117  }
118 
119  // clean up old buffer and set to new fdt blob
120  if (!fileDataMmapped)
121  delete [] fileData;
122  fileData = fdt_buf_w_space;
123  fileDataMmapped = false;
124  length = newLen;
125 
126  return true;
127 }
128 
129 Addr
131 {
132  void *fd = (void *)fileData;
133 
134  int offset = fdt_path_offset(fd, "/cpus/cpu@0");
135  int len;
136 
137  const void *temp = fdt_getprop(fd, offset, "cpu-release-addr", &len);
138  Addr rel_addr = 0;
139 
140  if (len > 3)
141  rel_addr = betoh(*static_cast<const uint32_t *>(temp));
142  if (len == 8) {
143  rel_addr = (rel_addr << 32) |
144  betoh(*(static_cast<const uint32_t *>(temp) + 1));
145  }
146 
147  return rel_addr;
148 }
149 
152 {
153  if (fileDataMmapped)
154  return {{ "data", imageData }};
155  else
156  return {{ "data", 0, fileData, length }};
157 }
158 
159 } // namespace Loader
warn
#define warn(...)
Definition: logging.hh:239
Loader::DtbFile::addBootCmdLine
bool addBootCmdLine(const char *_args, size_t len)
Adds the passed in Command Line options for the kernel to the proper location in the device tree.
Definition: dtb_file.cc:62
ArmISA::fd
Bitfield< 14, 12 > fd
Definition: types.hh:159
Loader::DtbFile::length
size_t length
Definition: dtb_file.hh:50
Loader::DtbFile::fileData
uint8_t * fileData
Definition: dtb_file.hh:49
dtb_file.hh
Loader::ImageFile
Definition: image_file.hh:41
Loader::ImageFileDataPtr
std::shared_ptr< ImageFileData > ImageFileDataPtr
Definition: image_file_data.hh:55
Loader::MemoryImage
Definition: memory_image.hh:48
Loader
Definition: process.hh:39
Loader::DtbFile::DtbFile
DtbFile(const std::string &name)
Definition: dtb_file.cc:43
Loader::DtbFile::fileDataMmapped
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:48
Loader::DtbFile::buildImage
MemoryImage buildImage() const override
Definition: dtb_file.cc:151
Loader::ImageFile::imageData
ImageFileDataPtr imageData
Definition: image_file.hh:44
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
betoh
T betoh(T value)
Definition: byteswap.hh:143
Loader::ImageFileData
Definition: image_file_data.hh:39
Loader::DtbFile::findReleaseAddr
Addr findReleaseAddr()
Parse the DTB file enough to find the provided release address and return it.
Definition: dtb_file.cc:130
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:197
ArmISA::len
Bitfield< 18, 16 > len
Definition: miscregs_types.hh:439
Loader::DtbFile::~DtbFile
~DtbFile()
Definition: dtb_file.cc:53
byteswap.hh
ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:153

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17