2 * Copyright (c) 2013 David Chisnall
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * Properties may contain a number of different value, each with a different
53 * label. This class encapsulates a single value.
58 * The label for this data. This is usually empty.
62 * If this value is a string, or something resolved from a string (a
63 * reference) then this contains the source string.
67 * The data that should be written to the final output.
69 byte_buffer byte_data;
71 * Enumeration describing the possible types of a value. Note that
72 * property-coded arrays will appear simply as binary (or possibly
73 * string, if they happen to be nul-terminated and printable), and must
74 * be checked separately.
79 * This is a list of strings. When read from source, string
80 * lists become one property value for each string, however
81 * when read from binary we have a single property value
82 * incorporating the entire text, with nul bytes separating the
87 * This property contains a single string.
91 * This is a binary value. Check the size of byte_data to
92 * determine how many bytes this contains.
95 /** This contains a short-form address that should be replaced
96 * by a fully-qualified version. This will only appear when
97 * the input is a device tree source. When parsed from a
98 * device tree blob, the cross reference will have already been
99 * resolved and the property value will be a string containing
100 * the full path of the target node. */
103 * This is a phandle reference. When parsed from source, the
104 * string_data will contain the node label for the target and,
105 * after cross references have been resolved, the binary data
106 * will contain a 32-bit integer that should match the phandle
107 * property of the target node.
111 * An empty property value. This will never appear on a real
112 * property value, it is used by checkers to indicate that no
113 * property values should exist for a property.
117 * The type of this property has not yet been determined.
122 * The type of this property.
126 * Returns true if this value is a cross reference, false otherwise.
128 inline bool is_cross_reference()
130 return is_type(CROSS_REFERENCE);
133 * Returns true if this value is a phandle reference, false otherwise.
135 inline bool is_phandle()
137 return is_type(PHANDLE);
140 * Returns true if this value is a string, false otherwise.
142 inline bool is_string()
144 return is_type(STRING);
147 * Returns true if this value is a string list (a nul-separated
148 * sequence of strings), false otherwise.
150 inline bool is_string_list()
152 return is_type(STRING_LIST);
155 * Returns true if this value is binary, false otherwise.
157 inline bool is_binary()
159 return is_type(BINARY);
162 * Returns this property value as a 32-bit integer. Returns 0 if this
163 * property value is not 32 bits long. The bytes in the property value
164 * are assumed to be in big-endian format, but the return value is in
165 * the host native endian.
167 uint32_t get_as_uint32();
169 * Default constructor, specifying the label of the value.
171 property_value(string l=string()) : label(l), type(UNKNOWN) {}
173 * Writes the data for this value into an output buffer.
175 void push_to_buffer(byte_buffer &buffer);
178 * Writes the property value to the standard output. This uses the
179 * following heuristics for deciding how to print the output:
181 * - If the value is nul-terminated and only contains printable
182 * characters, it is written as a string.
183 * - If it is a multiple of 4 bytes long, then it is printed as cells.
184 * - Otherwise, it is printed as a byte buffer.
186 void write_dts(FILE *file);
189 * Returns whether the value is of the specified type. If the type of
190 * the value has not yet been determined, then this calculates it.
192 inline bool is_type(value_type v)
201 * Determines the type of the value based on its contents.
205 * Writes the property value to the specified file as a quoted string.
206 * This is used when generating DTS.
208 void write_as_string(FILE *file);
210 * Writes the property value to the specified file as a sequence of
211 * 32-bit big-endian cells. This is used when generating DTS.
213 void write_as_cells(FILE *file);
215 * Writes the property value to the specified file as a sequence of
216 * bytes. This is used when generating DTS.
218 void write_as_bytes(FILE *file);
222 * A value encapsulating a single property. This contains a key, optionally a
223 * label, and optionally one or more values.
228 * The name of this property.
236 * The values in this property.
238 std::vector<property_value> values;
240 * Value indicating that this is a valid property. If a parse error
241 * occurs, then this value is false.
245 * Parses a string property value, i.e. a value enclosed in double quotes.
247 void parse_string(input_buffer &input);
249 * Parses one or more 32-bit values enclosed in angle brackets.
251 void parse_cells(input_buffer &input);
253 * Parses an array of bytes, contained within square brackets.
255 void parse_bytes(input_buffer &input);
257 * Parses a reference. This is a node label preceded by an ampersand
258 * symbol, which should expand to the full path to that node.
260 * Note: The specification says that the target of such a reference is
261 * a node name, however dtc assumes that it is a label, and so we
262 * follow their interpretation for compatibility.
264 void parse_reference(input_buffer &input);
266 * Constructs a new property from two input buffers, pointing to the
267 * struct and strings tables in the device tree blob, respectively.
268 * The structs input buffer is assumed to have just consumed the
271 property(input_buffer &structs, input_buffer &strings);
273 * Parses a new property from the input buffer.
275 property(input_buffer &input, string k, string l);
278 * Creates an empty property.
280 property(string k, string l=string()) : key(k), label(l), valid(true)
285 property(property &p) : key(p.key), label(p.label), values(p.values),
288 * Factory method for constructing a new property. Attempts to parse a
289 * property from the input, and returns it on success. On any parse
290 * error, this will return 0.
292 static property* parse_dtb(input_buffer &structs,
293 input_buffer &strings);
295 * Factory method for constructing a new property. Attempts to parse a
296 * property from the input, and returns it on success. On any parse
297 * error, this will return 0.
299 static property* parse(input_buffer &input,
301 string label=string());
303 * Iterator type used for accessing the values of a property.
305 typedef std::vector<property_value>::iterator value_iterator;
307 * Returns an iterator referring to the first value in this property.
309 inline value_iterator begin()
311 return values.begin();
314 * Returns an iterator referring to the last value in this property.
316 inline value_iterator end()
321 * Adds a new value to an existing property.
323 inline void add_value(property_value v)
328 * Returns the key for this property.
330 inline string get_key()
335 * Writes the property to the specified writer. The property name is a
336 * reference into the strings table.
338 void write(dtb::output_writer &writer, dtb::string_table &strings);
340 * Writes in DTS format to the specified file, at the given indent
341 * level. This will begin the line with the number of tabs specified
342 * as the indent level and then write the property in the most
343 * applicable way that it can determine.
345 void write_dts(FILE *file, int indent);
349 * Class encapsulating a device tree node. Nodes may contain properties and
356 * The label for this node, if any. Node labels are used as the
357 * targets for cross references.
361 * The name of the node.
365 * The unit address of the node, which is optionally written after the
366 * name followed by an at symbol.
371 * The properties contained within this node.
373 std::vector<property*> properties;
375 * The children of this node.
377 std::vector<node*> children;
379 * A flag indicating whether this node is valid. This is set to false
380 * if an error occurs during parsing.
384 * Parses a name inside a node, writing the string passed as the last
385 * argument as an error if it fails.
387 string parse_name(input_buffer &input,
391 * Constructs a new node from two input buffers, pointing to the struct
392 * and strings tables in the device tree blob, respectively.
394 node(input_buffer &structs, input_buffer &strings);
396 * Parses a new node from the specified input buffer. This is called
397 * when the input cursor is on the open brace for the start of the
398 * node. The name, and optionally label and unit address, should have
399 * already been parsed.
401 node(input_buffer &input, string n, string l, string a);
403 * Comparison function for properties, used when sorting the properties
404 * vector. Orders the properties based on their names.
406 static inline bool cmp_properties(property *p1, property *p2);
409 return p1->get_key() < p2->get_key();
413 * Comparison function for nodes, used when sorting the children
414 * vector. Orders the nodes based on their names or, if the names are
415 * the same, by the unit addresses.
417 static inline bool cmp_children(node *c1, node *c2);
420 if (c1->name == c2->name)
422 return c1->unit_address < c2->unit_address;
424 return c1->name < c2->name;
429 * Sorts the node's properties and children into alphabetical order and
430 * recursively sorts the children.
434 * Iterator type for child nodes.
436 typedef std::vector<node*>::iterator child_iterator;
438 * Returns an iterator for the first child of this node.
440 inline child_iterator child_begin()
442 return children.begin();
445 * Returns an iterator after the last child of this node.
447 inline child_iterator child_end()
449 return children.end();
452 * Iterator type for properties of a node.
454 typedef std::vector<property*>::iterator property_iterator;
456 * Returns an iterator after the last property of this node.
458 inline property_iterator property_begin()
460 return properties.begin();
463 * Returns an iterator for the first property of this node.
465 inline property_iterator property_end()
467 return properties.end();
470 * Factory method for constructing a new node. Attempts to parse a
471 * node in DTS format from the input, and returns it on success. On
472 * any parse error, this will return 0. This should be called with the
473 * cursor on the open brace of the property, after the name and so on
476 static node* parse(input_buffer &input,
478 string label=string(),
479 string address=string());
481 * Factory method for constructing a new node. Attempts to parse a
482 * node in DTB format from the input, and returns it on success. On
483 * any parse error, this will return 0. This should be called with the
484 * cursor on the open brace of the property, after the name and so on
487 static node* parse_dtb(input_buffer &structs, input_buffer &strings);
489 * Destroys the node, recursively deleting all of its properties and
494 * Returns a property corresponding to the specified key, or 0 if this
495 * node does not contain a property of that name.
497 property *get_property(string key);
499 * Adds a new property to this node.
501 inline void add_property(property *p)
503 properties.push_back(p);
506 * Merges a node into this one. Any properties present in both are
507 * overridden, any properties present in only one are preserved.
509 void merge_node(node *other);
511 * Write this node to the specified output. Although nodes do not
512 * refer to a string table directly, their properties do. The string
513 * table passed as the second argument is used for the names of
514 * properties within this node and its children.
516 void write(dtb::output_writer &writer, dtb::string_table &strings);
518 * Writes the current node as DTS to the specified file. The second
519 * parameter is the indent level. This function will start every line
520 * with this number of tabs.
522 void write_dts(FILE *file, int indent);
526 * Class encapsulating the entire parsed FDT. This is the top-level class,
527 * which parses the entire DTS representation and write out the finished
534 * Type used for node paths. A node path is sequence of names and unit
537 typedef std::vector<std::pair<string,string> > node_path;
539 * Name that we should use for phandle nodes.
547 /** Create both nodes. */
552 * The format that we should use for writing phandles.
554 phandle_format phandle_node_name;
556 * Flag indicating that this tree is valid. This will be set to false
561 * Type used for memory reservations. A reservation is two 64-bit
562 * values indicating a base address and length in memory that the
563 * kernel should not use. The high 32 bits are ignored on 32-bit
566 typedef std::pair<uint64_t, uint64_t> reservation;
568 * The memory reserves table.
570 std::vector<reservation> reservations;
572 * Root node. All other nodes are children of this node.
576 * Mapping from names to nodes. Only unambiguous names are recorded,
577 * duplicate names are stored as (node*)-1.
579 std::map<string, node*> node_names;
581 * A map from labels to node paths. When resolving cross references,
582 * we look up referenced nodes in this and replace the cross reference
583 * with the full path to its target.
585 std::map<string, node_path> node_paths;
587 * A collection of property values that are references to other nodes.
588 * These should be expanded to the full path of their targets.
590 std::vector<property_value*> cross_references;
592 * A collection of property values that refer to phandles. These will
593 * be replaced by the value of the phandle property in their
596 std::vector<property_value*> phandles;
598 * A collection of input buffers that we are using. These input
599 * buffers are the ones that own their memory, and so we must preserve
600 * them for the lifetime of the device tree.
602 std::vector<input_buffer*> buffers;
604 * A map of used phandle values to nodes. All phandles must be unique,
605 * so we keep a set of ones that the user explicitly provides in the
606 * input to ensure that we don't reuse them.
608 * This is a map, rather than a set, because we also want to be able to
609 * find phandles that were provided by the user explicitly when we are
612 std::map<uint32_t, node*> used_phandles;
614 * Paths to search for include files. This contains a set of
615 * nul-terminated strings, which are not owned by this class and so
616 * must be freed separately.
618 std::vector<const char*> include_paths;
620 * The default boot CPU, specified in the device tree header.
624 * The number of empty reserve map entries to generate in the blob.
626 uint32_t spare_reserve_map_entries;
628 * The minimum size in bytes of the blob.
630 uint32_t minimum_blob_size;
632 * The number of bytes of padding to add to the end of the blob.
634 uint32_t blob_padding;
636 * Visit all of the nodes recursively, and if they have labels then add
637 * them to the node_paths and node_names vectors so that they can be
638 * used in resolving cross references. Also collects phandle
639 * properties that have been explicitly added.
641 void collect_names_recursive(node* n, node_path &path);
643 * Calls the recursive version of this method on every root node.
645 void collect_names();
647 * Resolves all cross references. Any properties that refer to another
648 * node must have their values replaced by either the node path or
651 void resolve_cross_references();
653 * Parses root nodes from the top level of a dts file.
655 void parse_roots(input_buffer &input, std::vector<node*> &roots);
657 * Allocates a new mmap()'d input buffer for use in parsing. This
658 * object then keeps a reference to it, ensuring that it is not
659 * deallocated until the device tree is destroyed.
661 input_buffer *buffer_for_file(const char *path);
663 * Template function that writes a dtb blob using the specified writer.
664 * The writer defines the output format (assembly, blob).
666 template<class writer>
670 * Returns the node referenced by the property. If this is a tree that
671 * is in source form, then we have a string that we can use to index
672 * the cross_references array and so we can just look that up.
674 node *referenced_node(property_value &v);
676 * Writes this FDT as a DTB to the specified output.
678 void write_binary(int fd);
680 * Writes this FDT as an assembly representation of the DTB to the
681 * specified output. The result can then be assembled and linked into
684 void write_asm(int fd);
686 * Writes the tree in DTS (source) format.
688 void write_dts(int fd);
690 * Default constructor. Creates a valid, but empty FDT.
692 device_tree() : phandle_node_name(EPAPR), valid(true), root(0),
693 boot_cpu(0), spare_reserve_map_entries(0),
694 minimum_blob_size(0), blob_padding(0) {}
696 * Constructs a device tree from the specified file name, referring to
697 * a file that contains a device tree blob.
699 void parse_dtb(const char *fn, FILE *depfile);
701 * Constructs a device tree from the specified file name, referring to
702 * a file that contains device tree source.
704 void parse_dts(const char *fn, FILE *depfile);
706 * Destroy the tree and any input buffers that it holds.
710 * Returns whether this tree is valid.
712 inline bool is_valid()
717 * Sets the format for writing phandle properties.
719 inline void set_phandle_format(phandle_format f)
721 phandle_node_name = f;
724 * Returns a pointer to the root node of this tree. No ownership
727 inline node *get_root() const
732 * Sets the physical boot CPU.
734 void set_boot_cpu(uint32_t cpu)
739 * Sorts the tree. Useful for debugging device trees.
746 * Adds a path to search for include files. The argument must be a
747 * nul-terminated string representing the path. The device tree keeps
748 * a pointer to this string, but does not own it: the caller is
749 * responsible for freeing it if required.
751 void add_include_path(const char *path)
753 include_paths.push_back(path);
756 * Sets the number of empty reserve map entries to add.
758 void set_empty_reserve_map_entries(uint32_t e)
760 spare_reserve_map_entries = e;
763 * Sets the minimum size, in bytes, of the blob.
765 void set_blob_minimum_size(uint32_t s)
767 minimum_blob_size = s;
770 * Sets the amount of padding to add to the blob.
772 void set_blob_padding(uint32_t p)