2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 David Chisnall
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9 * ("CTSRD"), as part of the DARPA CRASH research programme.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #include "input_buffer.hh"
48 * The dtb namespace contains code related to the generation of device tree
49 * blobs, the binary representation of flattened device trees. The abstract
50 * tree representation calls into this code to generate the output.
54 /** The token types in the DTB, as defined by ยง7.4.1 of the ePAPR
55 * specification. All of these values are written in big-endian format in the
61 * Marker indicating the start of a node in the tree. This is followed
62 * by the nul-terminated name. If a unit address is specified, then
63 * the name also contains the address, with an @ symbol between the end
64 * of the name and the start of the address.
66 * The name is then padded such that the next token begins on a 4-byte
67 * boundary. The node may contain properties, other nodes, both, or be
70 FDT_BEGIN_NODE = 0x00000001,
72 * Marker indicating the end of a node.
74 FDT_END_NODE = 0x00000002,
76 * The start of a property. This is followed by two 32-bit big-endian
77 * values. The first indicates the length of the property value, the
78 * second its index in the strings table. It is then followed by the
79 * property value, if the value is of non-zero length.
81 FDT_PROP = 0x00000003,
83 * Ignored token. May be used for padding inside DTB nodes.
87 * Marker indicating the end of the tree.
93 * Returns the token as a string. This is used for debugging and for printing
94 * human-friendly error messages about malformed DTB input.
96 inline const char *token_type_name(token_type t)
101 return "FDT_BEGIN_NODE";
103 return "FDT_END_NODE";
117 * Abstract class for writing a section of the output. We create one
118 * of these for each section that needs to be written. It is intended to build
119 * a temporary buffer of the output in memory and then write it to a file
120 * stream. The size can be returned after all of the data has been written
121 * into the internal buffer, so the sizes of the three tables can be calculated
122 * before storing them in the buffer.
127 * Writes a label into the output stream. This is only applicable for
128 * assembly output, where the labels become symbols that can be
129 * resolved at link time.
131 virtual void write_label(const std::string &name) = 0;
133 * Writes a comment into the output stream. Useful only when debugging
136 virtual void write_comment(const std::string &name) = 0;
138 * Writes a string. A nul terminator is implicitly added.
140 virtual void write_string(const std::string &name) = 0;
142 * Writes a single 8-bit value.
144 virtual void write_data(uint8_t) = 0;
146 * Writes a single 32-bit value. The value is written in big-endian
147 * format, but should be passed in the host's native endian.
149 virtual void write_data(uint32_t) = 0;
151 * Writes a single 64-bit value. The value is written in big-endian
152 * format, but should be passed in the host's native endian.
154 virtual void write_data(uint64_t) = 0;
156 * Writes the collected output to the specified file descriptor.
158 virtual void write_to_file(int fd) = 0;
160 * Returns the number of bytes.
162 virtual uint32_t size() = 0;
164 * Helper for writing tokens to the output stream. This writes a
165 * comment above the token describing its value, for easier debugging
168 inline void write_token(token_type t)
170 write_comment(token_type_name(t));
171 write_data((uint32_t)t);
174 * Helper function that writes a byte buffer to the output, one byte at
177 void write_data(byte_buffer b);
181 * Binary file writer. This class is responsible for writing the DTB output
182 * directly in blob format.
184 class binary_writer : public output_writer
187 * The internal buffer used to store the blob while it is being
193 * The binary format does not support labels, so this method
196 void write_label(const std::string &) override {}
198 * Comments are ignored by the binary writer.
200 void write_comment(const std::string&) override {}
201 void write_string(const std::string &name) override;
202 void write_data(uint8_t v) override;
203 void write_data(uint32_t v) override;
204 void write_data(uint64_t v) override;
205 void write_to_file(int fd) override;
206 uint32_t size() override;
209 * Assembly writer. This class is responsible for writing the output in an
210 * assembly format that is suitable for linking into a kernel, loader, and so
213 class asm_writer : public output_writer
216 * The internal buffer for temporary values. Note that this actually
217 * contains ASCII text, but it is a byte buffer so that we can just
218 * copy strings across as-is.
222 * The number of bytes written to the current line. This is used to
223 * allow line wrapping, where we aim to write four .byte directives to
224 * make the alignment clearer.
228 * The current number of bytes written. This is the number in binary
229 * format, not the number of bytes in the buffer.
231 uint32_t bytes_written;
234 * Writes a string directly to the output as-is. This is the function that
235 * performs the real output.
237 void write_string(const char *c);
239 * Write a string to the output.
241 void write_string(const std::string &c) override;
243 * Writes the string, starting on a new line.
245 void write_line(const char *c);
247 * Writes a byte in binary format. This will emit a single .byte
248 * directive, with up to four per line.
250 void write_byte(uint8_t b);
252 asm_writer() : byte_count(0), bytes_written(0) {}
253 void write_label(const std::string &name) override;
254 void write_comment(const std::string &name) override;
255 void write_data(uint8_t v) override;
256 void write_data(uint32_t v) override;
257 void write_data(uint64_t v) override;
258 void write_to_file(int fd) override;
259 uint32_t size() override;
263 * Class encapsulating the device tree blob header. This class stores all of
264 * the values found in the header and is responsible for writing them to the
270 * Magic value, used to validate that this really is a device tree
271 * blob. Should always be set to 0xd00dfeed.
275 * The total size of the blob, including header, reservations, strings
276 * table, and padding.
280 * The offset from the start of the blob of the struct table (i.e. the
281 * part of the blob containing the entire device tree).
283 uint32_t off_dt_struct;
285 * The offset from the start of the blob of the strings table.
287 uint32_t off_dt_strings;
289 * The offset of the reservation map from the start of the blob.
291 uint32_t off_mem_rsvmap;
293 * The version of the blob. This should always be 17.
297 * The earliest version of the DTB specification with which this blob
298 * is backwards compatible. This should always be 16.
300 uint32_t last_comp_version;
302 * The ID of the CPU where this boots.
304 uint32_t boot_cpuid_phys;
306 * The size of the strings table.
308 uint32_t size_dt_strings;
310 * The size of the struct table.
312 uint32_t size_dt_struct;
314 * Writes the entire header to the specified output buffer.
316 void write(output_writer &out);
318 * Reads the header from bits binary representation in a blob.
320 bool read_dtb(input_buffer &input);
322 * Default constructor. Initialises the values that have sensible
323 * defaults, leaves the others blank.
325 header() : magic(0xd00dfeed), version(17), last_comp_version(16),
326 boot_cpuid_phys(0) {}
330 * Class encapsulating the string table. FDT strings are stored in a string
331 * section. This maintains a map from strings to their offsets in the strings
334 * Note: We don't currently do suffix matching, which may save a small amount
339 * Map from strings to their offset.
341 std::map<std::string, uint32_t> string_offsets;
343 * The strings, in the order in which they should be written to the
344 * output. The order must be stable - adding another string must not
345 * change the offset of any that we have already referenced - and so we
346 * simply write the strings in the order that they are passed.
348 std::vector<std::string> strings;
350 * The current size of the strings section.
355 * Default constructor, creates an empty strings table.
357 string_table() : size(0) {}
359 * Adds a string to the table, returning the offset from the start
360 * where it will be written. If the string is already present, this
361 * will return its existing offset, otherwise it will return a new
364 uint32_t add_string(const std::string &str);
366 * Writes the strings table to the specified output.
368 void write(dtb::output_writer &writer);