2 * Copyright (c) 2013 The Regents of The University of Michigan
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.
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.
29 #include "base/loader/dtb_file.hh"
38 #include "sim/byteswap.hh"
43 DtbFile::DtbFile(const std::string
&filename
) :
44 ImageFile(ImageFileDataPtr(new ImageFileData(filename
)))
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();
55 // Make sure to clean up memory properly depending
56 // on how buffer was allocated.
62 DtbFile::addBootCmdLine(const char *_args
, size_t len
)
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";
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
);
75 warn("Error resizing buffer of flattened device tree, "
77 delete [] fdt_buf_w_space
;
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
);
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
89 offset
= fdt_path_offset((void *)fdt_buf_w_space
,
93 warn("Error finding or adding \"chosen\" subnode to flattened "
94 "device tree, errno: %d\n", offset
);
95 delete [] fdt_buf_w_space
;
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);
104 warn("Error setting \"bootargs\" property to flattened device tree, "
106 delete [] fdt_buf_w_space
;
110 // Repack the dtb for kernel use
111 ret
= fdt_pack((void *)fdt_buf_w_space
);
113 warn("Error re-packing flattened device tree structure, "
115 delete [] fdt_buf_w_space
;
119 // clean up old buffer and set to new fdt blob
120 if (!fileDataMmapped
)
122 fileData
= fdt_buf_w_space
;
123 fileDataMmapped
= false;
130 DtbFile::findReleaseAddr()
132 void *fd
= (void *)fileData
;
134 int offset
= fdt_path_offset(fd
, "/cpus/cpu@0");
137 const void *temp
= fdt_getprop(fd
, offset
, "cpu-release-addr", &len
);
141 rel_addr
= betoh(*static_cast<const uint32_t *>(temp
));
143 rel_addr
= (rel_addr
<< 32) |
144 betoh(*(static_cast<const uint32_t *>(temp
) + 1));
151 DtbFile::buildImage() const
154 return {{ "data", imageData
}};
156 return {{ "data", 0, fileData
, length
}};
159 } // namespace Loader