gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Anthony Gutierrez
29  */
30 
31 #include "base/loader/dtb_file.hh"
32 
33 #include <sys/mman.h>
34 #include <unistd.h>
35 
36 #include <cassert>
37 
38 #include "fdt.h"
39 #include "libfdt.h"
40 #include "sim/byteswap.hh"
41 
42 DtbFile::DtbFile(const std::string &filename) :
44 {
45  panic_if(fdt_magic((const void *)imageData->data()) != FDT_MAGIC,
46  "File %s doesn't seem to be a DTB.\n", filename);
47  fileDataMmapped = true;
48  fileData = const_cast<uint8_t *>(imageData->data());
49  length = imageData->len();
50 }
51 
53 {
54  // Make sure to clean up memory properly depending
55  // on how buffer was allocated.
56  if (!fileDataMmapped)
57  delete [] fileData;
58 }
59 
60 bool
61 DtbFile::addBootCmdLine(const char *_args, size_t len)
62 {
63  const char *root_path = "/";
64  const char *node_name = "chosen";
65  const char *full_path_node_name = "/chosen";
66  const char *property_name = "bootargs";
67 
68  // Make a new buffer that has extra space to add nodes/properties
69  int newLen = 2 * length;
70  uint8_t *fdt_buf_w_space = new uint8_t[newLen];
71  // Copy and unpack flattened device tree into new buffer
72  int ret = fdt_open_into((void *)fileData, (void *)fdt_buf_w_space, newLen);
73  if (ret < 0) {
74  warn("Error resizing buffer of flattened device tree, "
75  "errno: %d\n", ret);
76  delete [] fdt_buf_w_space;
77  return false;
78  }
79 
80  // First try finding the /chosen node in the dtb
81  int offset = fdt_path_offset((void *)fdt_buf_w_space, full_path_node_name);
82  if (offset < 0) {
83  // try adding the node by walking dtb tree to proper insertion point
84  offset = fdt_path_offset((void *)fdt_buf_w_space, root_path);
85  offset = fdt_add_subnode((void *)fdt_buf_w_space, offset, node_name);
86  // if we successfully add the subnode, get the offset
87  if (offset >= 0)
88  offset = fdt_path_offset((void *)fdt_buf_w_space,
89  full_path_node_name);
90 
91  if (offset < 0) {
92  warn("Error finding or adding \"chosen\" subnode to flattened "
93  "device tree, errno: %d\n", offset);
94  delete [] fdt_buf_w_space;
95  return false;
96  }
97  }
98 
99  // Set the bootargs property in the /chosen node
100  ret = fdt_setprop((void *)fdt_buf_w_space, offset, property_name,
101  (const void *)_args, len+1);
102  if (ret < 0) {
103  warn("Error setting \"bootargs\" property to flattened device tree, "
104  "errno: %d\n", ret);
105  delete [] fdt_buf_w_space;
106  return false;
107  }
108 
109  // Repack the dtb for kernel use
110  ret = fdt_pack((void *)fdt_buf_w_space);
111  if (ret < 0) {
112  warn("Error re-packing flattened device tree structure, "
113  "errno: %d\n", ret);
114  delete [] fdt_buf_w_space;
115  return false;
116  }
117 
118  // clean up old buffer and set to new fdt blob
119  if (!fileDataMmapped)
120  delete [] fileData;
121  fileData = fdt_buf_w_space;
122  fileDataMmapped = false;
123  length = newLen;
124 
125  return true;
126 }
127 
128 Addr
130 {
131  void *fd = (void *)fileData;
132 
133  int offset = fdt_path_offset(fd, "/cpus/cpu@0");
134  int len;
135 
136  const void *temp = fdt_getprop(fd, offset, "cpu-release-addr", &len);
137  Addr rel_addr = 0;
138 
139  if (len > 3)
140  rel_addr = betoh(*static_cast<const uint32_t *>(temp));
141  if (len == 8) {
142  rel_addr = (rel_addr << 32) |
143  betoh(*(static_cast<const uint32_t *>(temp) + 1));
144  }
145 
146  return rel_addr;
147 }
148 
151 {
152  if (fileDataMmapped)
153  return {{ "data", imageData }};
154  else
155  return {{ "data", 0, fileData, length }};
156 }
~DtbFile()
Definition: dtb_file.cc:52
uint8_t * fileData
Definition: dtb_file.hh:48
ImageFileDataPtr imageData
Definition: image_file.hh:44
Bitfield< 23, 0 > offset
Definition: types.hh:154
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:47
Bitfield< 18, 16 > len
Addr findReleaseAddr()
Parse the DTB file enough to find the provided release address and return it.
Definition: dtb_file.cc:129
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
DtbFile(const std::string &name)
Definition: dtb_file.cc:42
This implements an image file format to support loading and modifying flattened device tree blobs for...
MemoryImage buildImage() const override
Definition: dtb_file.cc:150
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:61
T betoh(T value)
Definition: byteswap.hh:147
std::shared_ptr< ImageFileData > ImageFileDataPtr
#define warn(...)
Definition: logging.hh:212
size_t length
Definition: dtb_file.hh:49
Bitfield< 14, 12 > fd
Definition: types.hh:160
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:185

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13