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
35 #include <unordered_map>
36 #include <unordered_set>
42 #include "input_buffer.hh"
58 * Type for (owned) pointers to properties.
60 typedef std::shared_ptr<property> property_ptr;
62 * Owning pointer to a node.
64 typedef std::unique_ptr<node> node_ptr;
66 * Map from macros to property pointers.
68 typedef std::unordered_map<std::string, property_ptr> define_map;
70 * Set of strings used for label names.
72 typedef std::unordered_set<std::string> string_set;
74 * Properties may contain a number of different value, each with a different
75 * label. This class encapsulates a single value.
80 * The label for this data. This is usually empty.
84 * If this value is a string, or something resolved from a string (a
85 * reference) then this contains the source string.
87 std::string string_data;
89 * The data that should be written to the final output.
91 byte_buffer byte_data;
93 * Enumeration describing the possible types of a value. Note that
94 * property-coded arrays will appear simply as binary (or possibly
95 * string, if they happen to be nul-terminated and printable), and must
96 * be checked separately.
101 * This is a list of strings. When read from source, string
102 * lists become one property value for each string, however
103 * when read from binary we have a single property value
104 * incorporating the entire text, with nul bytes separating the
109 * This property contains a single string.
113 * This is a binary value. Check the size of byte_data to
114 * determine how many bytes this contains.
117 /** This contains a short-form address that should be replaced
118 * by a fully-qualified version. This will only appear when
119 * the input is a device tree source. When parsed from a
120 * device tree blob, the cross reference will have already been
121 * resolved and the property value will be a string containing
122 * the full path of the target node. */
125 * This is a phandle reference. When parsed from source, the
126 * string_data will contain the node label for the target and,
127 * after cross references have been resolved, the binary data
128 * will contain a 32-bit integer that should match the phandle
129 * property of the target node.
133 * An empty property value. This will never appear on a real
134 * property value, it is used by checkers to indicate that no
135 * property values should exist for a property.
139 * The type of this property has not yet been determined.
144 * The type of this property.
148 * Returns true if this value is a cross reference, false otherwise.
150 inline bool is_cross_reference()
152 return is_type(CROSS_REFERENCE);
155 * Returns true if this value is a phandle reference, false otherwise.
157 inline bool is_phandle()
159 return is_type(PHANDLE);
162 * Returns true if this value is a string, false otherwise.
164 inline bool is_string()
166 return is_type(STRING);
169 * Returns true if this value is a string list (a nul-separated
170 * sequence of strings), false otherwise.
172 inline bool is_string_list()
174 return is_type(STRING_LIST);
177 * Returns true if this value is binary, false otherwise.
179 inline bool is_binary()
181 return is_type(BINARY);
184 * Returns this property value as a 32-bit integer. Returns 0 if this
185 * property value is not 32 bits long. The bytes in the property value
186 * are assumed to be in big-endian format, but the return value is in
187 * the host native endian.
189 uint32_t get_as_uint32();
191 * Default constructor, specifying the label of the value.
193 property_value(std::string l=std::string()) : label(l), type(UNKNOWN) {}
195 * Writes the data for this value into an output buffer.
197 void push_to_buffer(byte_buffer &buffer);
200 * Writes the property value to the standard output. This uses the
201 * following heuristics for deciding how to print the output:
203 * - If the value is nul-terminated and only contains printable
204 * characters, it is written as a string.
205 * - If it is a multiple of 4 bytes long, then it is printed as cells.
206 * - Otherwise, it is printed as a byte buffer.
208 void write_dts(FILE *file);
210 * Tries to merge adjacent property values, returns true if it succeeds and
213 bool try_to_merge(property_value &other);
215 * Returns the size (in bytes) of this property value.
220 * Returns whether the value is of the specified type. If the type of
221 * the value has not yet been determined, then this calculates it.
223 inline bool is_type(value_type v)
232 * Determines the type of the value based on its contents.
236 * Writes the property value to the specified file as a quoted string.
237 * This is used when generating DTS.
239 void write_as_string(FILE *file);
241 * Writes the property value to the specified file as a sequence of
242 * 32-bit big-endian cells. This is used when generating DTS.
244 void write_as_cells(FILE *file);
246 * Writes the property value to the specified file as a sequence of
247 * bytes. This is used when generating DTS.
249 void write_as_bytes(FILE *file);
253 * A value encapsulating a single property. This contains a key, optionally a
254 * label, and optionally one or more values.
259 * The name of this property.
263 * Zero or more labels.
267 * The values in this property.
269 std::vector<property_value> values;
271 * Value indicating that this is a valid property. If a parse error
272 * occurs, then this value is false.
276 * Parses a string property value, i.e. a value enclosed in double quotes.
278 void parse_string(text_input_buffer &input);
280 * Parses one or more 32-bit values enclosed in angle brackets.
282 void parse_cells(text_input_buffer &input, int cell_size);
284 * Parses an array of bytes, contained within square brackets.
286 void parse_bytes(text_input_buffer &input);
288 * Parses a reference. This is a node label preceded by an ampersand
289 * symbol, which should expand to the full path to that node.
291 * Note: The specification says that the target of such a reference is
292 * a node name, however dtc assumes that it is a label, and so we
293 * follow their interpretation for compatibility.
295 void parse_reference(text_input_buffer &input);
297 * Parse a predefined macro definition for a property.
299 void parse_define(text_input_buffer &input, define_map *defines);
301 * Constructs a new property from two input buffers, pointing to the
302 * struct and strings tables in the device tree blob, respectively.
303 * The structs input buffer is assumed to have just consumed the
306 property(input_buffer &structs, input_buffer &strings);
308 * Parses a new property from the input buffer.
310 property(text_input_buffer &input,
314 define_map *defines);
317 * Creates an empty property.
319 property(std::string &&k, string_set &&l=string_set())
320 : key(k), labels(l), valid(true) {}
324 property(property &p) : key(p.key), labels(p.labels), values(p.values),
327 * Factory method for constructing a new property. Attempts to parse a
328 * property from the input, and returns it on success. On any parse
329 * error, this will return 0.
331 static property_ptr parse_dtb(input_buffer &structs,
332 input_buffer &strings);
334 * Factory method for constructing a new property. Attempts to parse a
335 * property from the input, and returns it on success. On any parse
336 * error, this will return 0.
338 static property_ptr parse(text_input_buffer &input,
340 string_set &&labels=string_set(),
341 bool semicolonTerminated=true,
342 define_map *defines=0);
344 * Iterator type used for accessing the values of a property.
346 typedef std::vector<property_value>::iterator value_iterator;
348 * Returns an iterator referring to the first value in this property.
350 inline value_iterator begin()
352 return values.begin();
355 * Returns an iterator referring to the last value in this property.
357 inline value_iterator end()
362 * Adds a new value to an existing property.
364 inline void add_value(property_value v)
369 * Returns the key for this property.
371 inline std::string get_key()
376 * Writes the property to the specified writer. The property name is a
377 * reference into the strings table.
379 void write(dtb::output_writer &writer, dtb::string_table &strings);
381 * Writes in DTS format to the specified file, at the given indent
382 * level. This will begin the line with the number of tabs specified
383 * as the indent level and then write the property in the most
384 * applicable way that it can determine.
386 void write_dts(FILE *file, int indent);
388 * Returns the byte offset of the specified property value.
390 size_t offset_of_value(property_value &val);
394 * Class encapsulating a device tree node. Nodes may contain properties and
401 * The labels for this node, if any. Node labels are used as the
402 * targets for cross references.
404 std::unordered_set<std::string> labels;
406 * The name of the node.
410 * The unit address of the node, which is optionally written after the
411 * name followed by an at symbol.
413 std::string unit_address;
415 * The type for the property vector.
417 typedef std::vector<property_ptr> property_vector;
419 * Iterator type for child nodes.
421 typedef std::vector<node_ptr>::iterator child_iterator;
424 * Adaptor to use children in range-based for loops.
428 child_range(node &nd) : n(nd) {}
429 child_iterator begin() { return n.child_begin(); }
430 child_iterator end() { return n.child_end(); }
435 * Adaptor to use properties in range-based for loops.
437 struct property_range
439 property_range(node &nd) : n(nd) {}
440 property_vector::iterator begin() { return n.property_begin(); }
441 property_vector::iterator end() { return n.property_end(); }
446 * The properties contained within this node.
448 property_vector props;
450 * The children of this node.
452 std::vector<node_ptr> children;
454 * Children that should be deleted from this node when merging.
456 std::unordered_set<std::string> deleted_children;
458 * Properties that should be deleted from this node when merging.
460 std::unordered_set<std::string> deleted_props;
462 * A flag indicating whether this node is valid. This is set to false
463 * if an error occurs during parsing.
467 * Parses a name inside a node, writing the string passed as the last
468 * argument as an error if it fails.
470 std::string parse_name(text_input_buffer &input,
474 * Constructs a new node from two input buffers, pointing to the struct
475 * and strings tables in the device tree blob, respectively.
477 node(input_buffer &structs, input_buffer &strings);
479 * Parses a new node from the specified input buffer. This is called
480 * when the input cursor is on the open brace for the start of the
481 * node. The name, and optionally label and unit address, should have
482 * already been parsed.
484 node(text_input_buffer &input,
486 std::unordered_set<std::string> &&l,
490 * Creates a special node with the specified name and properties.
492 node(const std::string &n, const std::vector<property_ptr> &p);
494 * Comparison function for properties, used when sorting the properties
495 * vector. Orders the properties based on their names.
497 static inline bool cmp_properties(property_ptr &p1, property_ptr &p2);
500 return p1->get_key() < p2->get_key();
504 * Comparison function for nodes, used when sorting the children
505 * vector. Orders the nodes based on their names or, if the names are
506 * the same, by the unit addresses.
508 static inline bool cmp_children(node_ptr &c1, node_ptr &c2);
511 * Sorts the node's properties and children into alphabetical order and
512 * recursively sorts the children.
516 * Returns an iterator for the first child of this node.
518 inline child_iterator child_begin()
520 return children.begin();
523 * Returns an iterator after the last child of this node.
525 inline child_iterator child_end()
527 return children.end();
530 * Returns a range suitable for use in a range-based for loop describing
531 * the children of this node.
533 inline child_range child_nodes()
535 return child_range(*this);
538 * Accessor for the deleted children.
540 inline const std::unordered_set<std::string> &deleted_child_nodes()
542 return deleted_children;
545 * Accessor for the deleted properties
547 inline const std::unordered_set<std::string> &deleted_properties()
549 return deleted_props;
552 * Returns a range suitable for use in a range-based for loop describing
553 * the properties of this node.
555 inline property_range properties()
557 return property_range(*this);
560 * Returns an iterator after the last property of this node.
562 inline property_vector::iterator property_begin()
564 return props.begin();
567 * Returns an iterator for the first property of this node.
569 inline property_vector::iterator property_end()
574 * Factory method for constructing a new node. Attempts to parse a
575 * node in DTS format from the input, and returns it on success. On
576 * any parse error, this will return 0. This should be called with the
577 * cursor on the open brace of the property, after the name and so on
580 static node_ptr parse(text_input_buffer &input,
582 std::unordered_set<std::string> &&label=std::unordered_set<std::string>(),
583 std::string &&address=std::string(),
584 define_map *defines=0);
586 * Factory method for constructing a new node. Attempts to parse a
587 * node in DTB format from the input, and returns it on success. On
588 * any parse error, this will return 0. This should be called with the
589 * cursor on the open brace of the property, after the name and so on
592 static node_ptr parse_dtb(input_buffer &structs, input_buffer &strings);
594 * Construct a new special node from a name and set of properties.
596 static node_ptr create_special_node(const std::string &name,
597 const std::vector<property_ptr> &props);
599 * Returns a property corresponding to the specified key, or 0 if this
600 * node does not contain a property of that name.
602 property_ptr get_property(const std::string &key);
604 * Adds a new property to this node.
606 inline void add_property(property_ptr &p)
611 * Adds a new child to this node.
613 inline void add_child(node_ptr &&n)
615 children.push_back(std::move(n));
618 * Merges a node into this one. Any properties present in both are
619 * overridden, any properties present in only one are preserved.
621 void merge_node(node_ptr other);
623 * Write this node to the specified output. Although nodes do not
624 * refer to a string table directly, their properties do. The string
625 * table passed as the second argument is used for the names of
626 * properties within this node and its children.
628 void write(dtb::output_writer &writer, dtb::string_table &strings);
630 * Writes the current node as DTS to the specified file. The second
631 * parameter is the indent level. This function will start every line
632 * with this number of tabs.
634 void write_dts(FILE *file, int indent);
636 * Recursively visit this node and then its children.
638 void visit(std::function<void(node&)>);
642 * Class encapsulating the entire parsed FDT. This is the top-level class,
643 * which parses the entire DTS representation and write out the finished
650 * Type used for node paths. A node path is sequence of names and unit
653 class node_path : public std::vector<std::pair<std::string,std::string>>
657 * Converts this to a string representation.
659 std::string to_string() const;
662 * Name that we should use for phandle nodes.
670 /** Create both nodes. */
675 * The format that we should use for writing phandles.
677 phandle_format phandle_node_name;
679 * Flag indicating that this tree is valid. This will be set to false
684 * Type used for memory reservations. A reservation is two 64-bit
685 * values indicating a base address and length in memory that the
686 * kernel should not use. The high 32 bits are ignored on 32-bit
689 typedef std::pair<uint64_t, uint64_t> reservation;
691 * The memory reserves table.
693 std::vector<reservation> reservations;
695 * Root node. All other nodes are children of this node.
699 * Mapping from names to nodes. Only unambiguous names are recorded,
700 * duplicate names are stored as (node*)-1.
702 std::unordered_map<std::string, node*> node_names;
704 * A map from labels to node paths. When resolving cross references,
705 * we look up referenced nodes in this and replace the cross reference
706 * with the full path to its target.
708 std::unordered_map<std::string, node_path> node_paths;
710 * A collection of property values that are references to other nodes.
711 * These should be expanded to the full path of their targets.
713 std::vector<property_value*> cross_references;
715 * The location of something requiring a fixup entry.
720 * The path to the node.
724 * The property containing the reference.
728 * The property value that contains the reference.
733 * A collection of property values that refer to phandles. These will
734 * be replaced by the value of the phandle property in their
737 std::vector<fixup> fixups;
739 * The locations of all of the values that are supposed to become phandle
740 * references, but refer to things outside of this file.
742 std::vector<std::reference_wrapper<fixup>> unresolved_fixups;
744 * The names of nodes that target phandles.
746 std::unordered_set<std::string> phandle_targets;
748 * A collection of input buffers that we are using. These input
749 * buffers are the ones that own their memory, and so we must preserve
750 * them for the lifetime of the device tree.
752 std::vector<std::unique_ptr<input_buffer>> buffers;
754 * A map of used phandle values to nodes. All phandles must be unique,
755 * so we keep a set of ones that the user explicitly provides in the
756 * input to ensure that we don't reuse them.
758 * This is a map, rather than a set, because we also want to be able to
759 * find phandles that were provided by the user explicitly when we are
762 std::unordered_map<uint32_t, node*> used_phandles;
764 * Paths to search for include files. This contains a set of
765 * nul-terminated strings, which are not owned by this class and so
766 * must be freed separately.
768 std::vector<std::string> include_paths;
770 * Dictionary of predefined macros provided on the command line.
774 * The default boot CPU, specified in the device tree header.
778 * The number of empty reserve map entries to generate in the blob.
780 uint32_t spare_reserve_map_entries;
782 * The minimum size in bytes of the blob.
784 uint32_t minimum_blob_size;
786 * The number of bytes of padding to add to the end of the blob.
788 uint32_t blob_padding;
790 * Is this tree a plugin?
794 * Visit all of the nodes recursively, and if they have labels then add
795 * them to the node_paths and node_names vectors so that they can be
796 * used in resolving cross references. Also collects phandle
797 * properties that have been explicitly added.
799 void collect_names_recursive(node_ptr &n, node_path &path);
801 * Assign phandle properties to all nodes that have been referenced and
802 * require one. This method will recursively visit the tree starting at
803 * the node that it is passed.
805 void assign_phandles(node_ptr &n, uint32_t &next);
807 * Calls the recursive version of this method on every root node.
809 void collect_names();
811 * Resolves all cross references. Any properties that refer to another
812 * node must have their values replaced by either the node path or
815 void resolve_cross_references();
817 * Parses a dts file in the given buffer and adds the roots to the parsed
818 * set. The `read_header` argument indicates whether the header has
819 * already been read. Some dts files place the header in an include,
820 * rather than in the top-level file.
822 void parse_file(text_input_buffer &input,
823 std::vector<node_ptr> &roots,
826 * Template function that writes a dtb blob using the specified writer.
827 * The writer defines the output format (assembly, blob).
829 template<class writer>
833 * Should we write the __symbols__ node (to allow overlays to be linked
834 * against this blob)?
836 bool write_symbols = false;
838 * Returns the node referenced by the property. If this is a tree that
839 * is in source form, then we have a string that we can use to index
840 * the cross_references array and so we can just look that up.
842 node *referenced_node(property_value &v);
844 * Writes this FDT as a DTB to the specified output.
846 void write_binary(int fd);
848 * Writes this FDT as an assembly representation of the DTB to the
849 * specified output. The result can then be assembled and linked into
852 void write_asm(int fd);
854 * Writes the tree in DTS (source) format.
856 void write_dts(int fd);
858 * Default constructor. Creates a valid, but empty FDT.
860 device_tree() : phandle_node_name(EPAPR), valid(true),
861 boot_cpu(0), spare_reserve_map_entries(0),
862 minimum_blob_size(0), blob_padding(0) {}
864 * Constructs a device tree from the specified file name, referring to
865 * a file that contains a device tree blob.
867 void parse_dtb(const std::string &fn, FILE *depfile);
869 * Constructs a device tree from the specified file name, referring to
870 * a file that contains device tree source.
872 void parse_dts(const std::string &fn, FILE *depfile);
874 * Returns whether this tree is valid.
876 inline bool is_valid()
881 * Sets the format for writing phandle properties.
883 inline void set_phandle_format(phandle_format f)
885 phandle_node_name = f;
888 * Returns a pointer to the root node of this tree. No ownership
891 inline const node_ptr &get_root() const
896 * Sets the physical boot CPU.
898 void set_boot_cpu(uint32_t cpu)
903 * Sorts the tree. Useful for debugging device trees.
910 * Adds a path to search for include files. The argument must be a
911 * nul-terminated string representing the path. The device tree keeps
912 * a pointer to this string, but does not own it: the caller is
913 * responsible for freeing it if required.
915 void add_include_path(const char *path)
918 include_paths.push_back(std::move(p));
921 * Sets the number of empty reserve map entries to add.
923 void set_empty_reserve_map_entries(uint32_t e)
925 spare_reserve_map_entries = e;
928 * Sets the minimum size, in bytes, of the blob.
930 void set_blob_minimum_size(uint32_t s)
932 minimum_blob_size = s;
935 * Sets the amount of padding to add to the blob.
937 void set_blob_padding(uint32_t p)
942 * Parses a predefined macro value.
944 bool parse_define(const char *def);