gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
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
40namespace gem5
41{
42
43namespace loader
44{
45
46DtbFile::DtbFile(const std::string &filename) :
48{
49 panic_if(fdt_magic((const void *)imageData->data()) != FDT_MAGIC,
50 "File %s doesn't seem to be a DTB.\n", filename);
51 fileDataMmapped = true;
52 fileData = const_cast<uint8_t *>(imageData->data());
53 length = imageData->len();
54}
55
57{
58 // Make sure to clean up memory properly depending
59 // on how buffer was allocated.
60 if (!fileDataMmapped)
61 delete [] fileData;
62}
63
64bool
65DtbFile::addBootData(const char *_cmdline, size_t cmdline_len,
66 off_t initrd_start, size_t initrd_len)
67{
68 const char *root_path = "/";
69 const char *node_name = "chosen";
70 const char *full_path_node_name = "/chosen";
71 const char *bootargs_property_name = "bootargs";
72 const char *linux_initrd_start_property_name = "linux,initrd-start";
73 const char *linux_initrd_end_property_name = "linux,initrd-end";
74
75 // Make a new buffer that has extra space to add nodes/properties
76 int newLen = 2 * length;
77 uint8_t *fdt_buf_w_space = new uint8_t[newLen];
78 // Copy and unpack flattened device tree into new buffer
79 int ret = fdt_open_into((void *)fileData, (void *)fdt_buf_w_space, newLen);
80 if (ret < 0) {
81 warn("Error resizing buffer of flattened device tree, "
82 "errno: %d\n", ret);
83 delete [] fdt_buf_w_space;
84 return false;
85 }
86
87 // First try finding the /chosen node in the dtb
88 int offset = fdt_path_offset((void *)fdt_buf_w_space, full_path_node_name);
89 if (offset < 0) {
90 // try adding the node by walking dtb tree to proper insertion point
91 offset = fdt_path_offset((void *)fdt_buf_w_space, root_path);
92 offset = fdt_add_subnode((void *)fdt_buf_w_space, offset, node_name);
93 // if we successfully add the subnode, get the offset
94 if (offset >= 0)
95 offset = fdt_path_offset((void *)fdt_buf_w_space,
96 full_path_node_name);
97
98 if (offset < 0) {
99 warn("Error finding or adding \"chosen\" subnode to flattened "
100 "device tree, errno: %d\n", offset);
101 delete [] fdt_buf_w_space;
102 return false;
103 }
104 }
105
106 // Set the bootargs property in the /chosen node
107 ret = fdt_setprop((void *)fdt_buf_w_space, offset, bootargs_property_name,
108 (const void *)_cmdline, cmdline_len+1);
109 if (ret < 0) {
110 warn("Error setting \"bootargs\" property to flattened device tree, "
111 "errno: %d\n", ret);
112 delete [] fdt_buf_w_space;
113 return false;
114 }
115
116 if (initrd_len != 0) {
117 // Set the linux,initrd-start property in the /chosen node
118 ret = fdt_setprop_u64((void *)fdt_buf_w_space, offset,
119 linux_initrd_start_property_name,
120 (uint64_t)initrd_start);
121 if (ret < 0) {
122 warn("Error setting \"linux,initrd-start\" property to flattened "
123 "device tree, errno: %d\n", ret);
124 delete [] fdt_buf_w_space;
125 return false;
126 }
127
128 // Set the computed linux,initrd-end property in the /chosen node
129 ret = fdt_setprop_u64((void *)fdt_buf_w_space, offset,
130 linux_initrd_end_property_name,
131 (uint64_t)initrd_start + initrd_len);
132 if (ret < 0) {
133 warn("Error setting \"linux,initrd-len\" property to flattened "
134 "device tree, errno: %d\n", ret);
135 delete [] fdt_buf_w_space;
136 return false;
137 }
138 }
139
140 // Repack the dtb for kernel use
141 ret = fdt_pack((void *)fdt_buf_w_space);
142 if (ret < 0) {
143 warn("Error re-packing flattened device tree structure, "
144 "errno: %d\n", ret);
145 delete [] fdt_buf_w_space;
146 return false;
147 }
148
149 // clean up old buffer and set to new fdt blob
150 if (!fileDataMmapped)
151 delete [] fileData;
152 fileData = fdt_buf_w_space;
153 fileDataMmapped = false;
154 length = newLen;
155
156 return true;
157}
158
159Addr
161{
162 void *fd = (void *)fileData;
163
164 int offset = fdt_path_offset(fd, "/cpus/cpu@0");
165 int len;
166
167 const void *temp = fdt_getprop(fd, offset, "cpu-release-addr", &len);
168 Addr rel_addr = 0;
169
170 if (len > 3)
171 rel_addr = betoh(*static_cast<const uint32_t *>(temp));
172 if (len == 8) {
173 rel_addr = (rel_addr << 32) |
174 betoh(*(static_cast<const uint32_t *>(temp) + 1));
175 }
176
177 return rel_addr;
178}
179
182{
183 if (fileDataMmapped)
184 return {{ "data", imageData }};
185 else
186 return {{ "data", 0, fileData, length }};
187}
188
189} // namespace loader
190} // 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:65
Addr findReleaseAddr()
Parse the DTB file enough to find the provided release address and return it.
Definition dtb_file.cc:160
DtbFile(const std::string &name)
Definition dtb_file.cc:46
MemoryImage buildImage() const override
Definition dtb_file.cc:181
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:52
ImageFileDataPtr imageData
Definition image_file.hh:48
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:214
#define warn(...)
Definition logging.hh:256
Bitfield< 14, 12 > fd
Definition types.hh:150
Bitfield< 18, 16 > len
Bitfield< 23, 0 > offset
Definition types.hh:144
std::shared_ptr< ImageFileData > ImageFileDataPtr
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
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

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0