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
37 #include <unordered_map>
38 #include <unordered_set>
44 #include "input_buffer.hh"
60 * Type for (owned) pointers to properties.
62 typedef std::shared_ptr<property> property_ptr;
64 * Owning pointer to a node.
66 typedef std::unique_ptr<node> node_ptr;
68 * Map from macros to property pointers.
70 typedef std::unordered_map<std::string, property_ptr> define_map;
72 * Set of strings used for label names.
74 typedef std::unordered_set<std::string> string_set;
76 * Properties may contain a number of different value, each with a different
77 * label. This class encapsulates a single value.
82 * The label for this data. This is usually empty.
86 * If this value is a string, or something resolved from a string (a
87 * reference) then this contains the source string.
89 std::string string_data;
91 * The data that should be written to the final output.
93 byte_buffer byte_data;
95 * Enumeration describing the possible types of a value. Note that
96 * property-coded arrays will appear simply as binary (or possibly
97 * string, if they happen to be nul-terminated and printable), and must
98 * be checked separately.
103 * This is a list of strings. When read from source, string
104 * lists become one property value for each string, however
105 * when read from binary we have a single property value
106 * incorporating the entire text, with nul bytes separating the
111 * This property contains a single string.
115 * This is a binary value. Check the size of byte_data to
116 * determine how many bytes this contains.
119 /** This contains a short-form address that should be replaced
120 * by a fully-qualified version. This will only appear when
121 * the input is a device tree source. When parsed from a
122 * device tree blob, the cross reference will have already been
123 * resolved and the property value will be a string containing
124 * the full path of the target node. */
127 * This is a phandle reference. When parsed from source, the
128 * string_data will contain the node label for the target and,
129 * after cross references have been resolved, the binary data
130 * will contain a 32-bit integer that should match the phandle
131 * property of the target node.
135 * An empty property value. This will never appear on a real
136 * property value, it is used by checkers to indicate that no
137 * property values should exist for a property.
141 * The type of this property has not yet been determined.
146 * The type of this property.
150 * Returns true if this value is a cross reference, false otherwise.
152 inline bool is_cross_reference()
154 return is_type(CROSS_REFERENCE);
157 * Returns true if this value is a phandle reference, false otherwise.
159 inline bool is_phandle()
161 return is_type(PHANDLE);
164 * Returns true if this value is a string, false otherwise.
166 inline bool is_string()
168 return is_type(STRING);
171 * Returns true if this value is a string list (a nul-separated
172 * sequence of strings), false otherwise.
174 inline bool is_string_list()
176 return is_type(STRING_LIST);
179 * Returns true if this value is binary, false otherwise.
181 inline bool is_binary()
183 return is_type(BINARY);
186 * Returns this property value as a 32-bit integer. Returns 0 if this
187 * property value is not 32 bits long. The bytes in the property value
188 * are assumed to be in big-endian format, but the return value is in
189 * the host native endian.
191 uint32_t get_as_uint32();
193 * Default constructor, specifying the label of the value.
195 property_value(std::string l=std::string()) : label(l), type(UNKNOWN) {}
197 * Writes the data for this value into an output buffer.
199 void push_to_buffer(byte_buffer &buffer);
202 * Writes the property value to the standard output. This uses the
203 * following heuristics for deciding how to print the output:
205 * - If the value is nul-terminated and only contains printable
206 * characters, it is written as a string.
207 * - If it is a multiple of 4 bytes long, then it is printed as cells.
208 * - Otherwise, it is printed as a byte buffer.
210 void write_dts(FILE *file);
212 * Tries to merge adjacent property values, returns true if it succeeds and
215 bool try_to_merge(property_value &other);
217 * Returns the size (in bytes) of this property value.
222 * Returns whether the value is of the specified type. If the type of
223 * the value has not yet been determined, then this calculates it.
225 inline bool is_type(value_type v)
234 * Determines the type of the value based on its contents.
238 * Writes the property value to the specified file as a quoted string.
239 * This is used when generating DTS.
241 void write_as_string(FILE *file);
243 * Writes the property value to the specified file as a sequence of
244 * 32-bit big-endian cells. This is used when generating DTS.
246 void write_as_cells(FILE *file);
248 * Writes the property value to the specified file as a sequence of
249 * bytes. This is used when generating DTS.
251 void write_as_bytes(FILE *file);
255 * A value encapsulating a single property. This contains a key, optionally a
256 * label, and optionally one or more values.
261 * The name of this property.
265 * Zero or more labels.
269 * The values in this property.
271 std::vector<property_value> values;
273 * Value indicating that this is a valid property. If a parse error
274 * occurs, then this value is false.
278 * Parses a string property value, i.e. a value enclosed in double quotes.
280 void parse_string(text_input_buffer &input);
282 * Parses one or more 32-bit values enclosed in angle brackets.
284 void parse_cells(text_input_buffer &input, int cell_size);
286 * Parses an array of bytes, contained within square brackets.
288 void parse_bytes(text_input_buffer &input);
290 * Parses a reference. This is a node label preceded by an ampersand
291 * symbol, which should expand to the full path to that node.
293 * Note: The specification says that the target of such a reference is
294 * a node name, however dtc assumes that it is a label, and so we
295 * follow their interpretation for compatibility.
297 void parse_reference(text_input_buffer &input);
299 * Parse a predefined macro definition for a property.
301 void parse_define(text_input_buffer &input, define_map *defines);
303 * Constructs a new property from two input buffers, pointing to the
304 * struct and strings tables in the device tree blob, respectively.
305 * The structs input buffer is assumed to have just consumed the
308 property(input_buffer &structs, input_buffer &strings);
310 * Parses a new property from the input buffer.
312 property(text_input_buffer &input,
316 define_map *defines);
319 * Creates an empty property.
321 property(std::string &&k, string_set &&l=string_set())
322 : key(k), labels(l), valid(true) {}
326 property(property &p) : key(p.key), labels(p.labels), values(p.values),
329 * Factory method for constructing a new property. Attempts to parse a
330 * property from the input, and returns it on success. On any parse
331 * error, this will return 0.
333 static property_ptr parse_dtb(input_buffer &structs,
334 input_buffer &strings);
336 * Factory method for constructing a new property. Attempts to parse a
337 * property from the input, and returns it on success. On any parse
338 * error, this will return 0.
340 static property_ptr parse(text_input_buffer &input,
342 string_set &&labels=string_set(),
343 bool semicolonTerminated=true,
344 define_map *defines=0);
346 * Iterator type used for accessing the values of a property.
348 typedef std::vector<property_value>::iterator value_iterator;
350 * Returns an iterator referring to the first value in this property.
352 inline value_iterator begin()
354 return values.begin();
357 * Returns an iterator referring to the last value in this property.
359 inline value_iterator end()
364 * Adds a new value to an existing property.
366 inline void add_value(property_value v)
371 * Returns the key for this property.
373 inline const std::string &get_key()
378 * Writes the property to the specified writer. The property name is a
379 * reference into the strings table.
381 void write(dtb::output_writer &writer, dtb::string_table &strings);
383 * Writes in DTS format to the specified file, at the given indent
384 * level. This will begin the line with the number of tabs specified
385 * as the indent level and then write the property in the most
386 * applicable way that it can determine.
388 void write_dts(FILE *file, int indent);
390 * Returns the byte offset of the specified property value.
392 size_t offset_of_value(property_value &val);
396 * Class encapsulating a device tree node. Nodes may contain properties and
403 * The labels for this node, if any. Node labels are used as the
404 * targets for cross references.
406 std::unordered_set<std::string> labels;
408 * The name of the node.
412 * The name of the node is a path reference.
414 bool name_is_path_reference = false;
416 * The unit address of the node, which is optionally written after the
417 * name followed by an at symbol.
419 std::string unit_address;
421 * The type for the property vector.
423 typedef std::vector<property_ptr> property_vector;
425 * Iterator type for child nodes.
427 typedef std::vector<node_ptr>::iterator child_iterator;
429 * Recursion behavior to be observed for visiting
434 * Recurse as normal through the rest of the tree.
438 * Continue recursing through the device tree, but do not
439 * recurse through this branch of the tree any further.
443 * Immediately halt the visit. No further nodes will be visited.
449 * Adaptor to use children in range-based for loops.
453 child_range(node &nd) : n(nd) {}
454 child_iterator begin() { return n.child_begin(); }
455 child_iterator end() { return n.child_end(); }
460 * Adaptor to use properties in range-based for loops.
462 struct property_range
464 property_range(node &nd) : n(nd) {}
465 property_vector::iterator begin() { return n.property_begin(); }
466 property_vector::iterator end() { return n.property_end(); }
471 * The properties contained within this node.
473 property_vector props;
475 * The children of this node.
477 std::vector<node_ptr> children;
479 * Children that should be deleted from this node when merging.
481 std::unordered_set<std::string> deleted_children;
483 * Properties that should be deleted from this node when merging.
485 std::unordered_set<std::string> deleted_props;
487 * A flag indicating whether this node is valid. This is set to false
488 * if an error occurs during parsing.
492 * Parses a name inside a node, writing the string passed as the last
493 * argument as an error if it fails.
495 std::string parse_name(text_input_buffer &input,
499 * Constructs a new node from two input buffers, pointing to the struct
500 * and strings tables in the device tree blob, respectively.
502 node(input_buffer &structs, input_buffer &strings);
504 * Parses a new node from the specified input buffer. This is called
505 * when the input cursor is on the open brace for the start of the
506 * node. The name, and optionally label and unit address, should have
507 * already been parsed.
509 node(text_input_buffer &input,
511 std::unordered_set<std::string> &&l,
515 * Creates a special node with the specified name and properties.
517 node(const std::string &n, const std::vector<property_ptr> &p);
519 * Comparison function for properties, used when sorting the properties
520 * vector. Orders the properties based on their names.
522 static inline bool cmp_properties(property_ptr &p1, property_ptr &p2);
525 return p1->get_key() < p2->get_key();
529 * Comparison function for nodes, used when sorting the children
530 * vector. Orders the nodes based on their names or, if the names are
531 * the same, by the unit addresses.
533 static inline bool cmp_children(node_ptr &c1, node_ptr &c2);
536 * Sorts the node's properties and children into alphabetical order and
537 * recursively sorts the children.
541 * Returns an iterator for the first child of this node.
543 inline child_iterator child_begin()
545 return children.begin();
548 * Returns an iterator after the last child of this node.
550 inline child_iterator child_end()
552 return children.end();
555 * Returns a range suitable for use in a range-based for loop describing
556 * the children of this node.
558 inline child_range child_nodes()
560 return child_range(*this);
563 * Accessor for the deleted children.
565 inline const std::unordered_set<std::string> &deleted_child_nodes()
567 return deleted_children;
570 * Accessor for the deleted properties
572 inline const std::unordered_set<std::string> &deleted_properties()
574 return deleted_props;
577 * Returns a range suitable for use in a range-based for loop describing
578 * the properties of this node.
580 inline property_range properties()
582 return property_range(*this);
585 * Returns an iterator after the last property of this node.
587 inline property_vector::iterator property_begin()
589 return props.begin();
592 * Returns an iterator for the first property of this node.
594 inline property_vector::iterator property_end()
599 * Factory method for constructing a new node. Attempts to parse a
600 * node in DTS format from the input, and returns it on success. On
601 * any parse error, this will return 0. This should be called with the
602 * cursor on the open brace of the property, after the name and so on
605 static node_ptr parse(text_input_buffer &input,
607 std::unordered_set<std::string> &&label=std::unordered_set<std::string>(),
608 std::string &&address=std::string(),
609 define_map *defines=0);
611 * Factory method for constructing a new node. Attempts to parse a
612 * node in DTB format from the input, and returns it on success. On
613 * any parse error, this will return 0. This should be called with the
614 * cursor on the open brace of the property, after the name and so on
617 static node_ptr parse_dtb(input_buffer &structs, input_buffer &strings);
619 * Construct a new special node from a name and set of properties.
621 static node_ptr create_special_node(const std::string &name,
622 const std::vector<property_ptr> &props);
624 * Returns a property corresponding to the specified key, or 0 if this
625 * node does not contain a property of that name.
627 property_ptr get_property(const std::string &key);
629 * Adds a new property to this node.
631 inline void add_property(property_ptr &p)
636 * Adds a new child to this node.
638 inline void add_child(node_ptr &&n)
640 children.push_back(std::move(n));
643 * Merges a node into this one. Any properties present in both are
644 * overridden, any properties present in only one are preserved.
646 void merge_node(node_ptr &other);
648 * Write this node to the specified output. Although nodes do not
649 * refer to a string table directly, their properties do. The string
650 * table passed as the second argument is used for the names of
651 * properties within this node and its children.
653 void write(dtb::output_writer &writer, dtb::string_table &strings);
655 * Writes the current node as DTS to the specified file. The second
656 * parameter is the indent level. This function will start every line
657 * with this number of tabs.
659 void write_dts(FILE *file, int indent);
661 * Recursively visit this node and then its children based on the
662 * callable's return value. The callable may return VISIT_BREAK
663 * immediately halt all recursion and end the visit, VISIT_CONTINUE to
664 * not recurse into the current node's children, or VISIT_RECURSE to recurse
665 * through children as expected. parent will be passed to the callable.
667 visit_behavior visit(std::function<visit_behavior(node&, node*)>, node *parent);
671 * Class encapsulating the entire parsed FDT. This is the top-level class,
672 * which parses the entire DTS representation and write out the finished
679 * Type used for node paths. A node path is sequence of names and unit
682 class node_path : public std::vector<std::pair<std::string,std::string>>
686 * Converts this to a string representation.
688 std::string to_string() const;
691 * Name that we should use for phandle nodes.
699 /** Create both nodes. */
704 * The format that we should use for writing phandles.
706 phandle_format phandle_node_name = EPAPR;
708 * Flag indicating that this tree is valid. This will be set to false
713 * Type used for memory reservations. A reservation is two 64-bit
714 * values indicating a base address and length in memory that the
715 * kernel should not use. The high 32 bits are ignored on 32-bit
718 typedef std::pair<uint64_t, uint64_t> reservation;
720 * The memory reserves table.
722 std::vector<reservation> reservations;
724 * Root node. All other nodes are children of this node.
728 * Mapping from names to nodes. Only unambiguous names are recorded,
729 * duplicate names are stored as (node*)-1.
731 std::unordered_map<std::string, node*> node_names;
733 * A map from labels to node paths. When resolving cross references,
734 * we look up referenced nodes in this and replace the cross reference
735 * with the full path to its target.
737 std::unordered_map<std::string, node_path> node_paths;
739 * A collection of property values that are references to other nodes.
740 * These should be expanded to the full path of their targets.
742 std::vector<property_value*> cross_references;
744 * The location of something requiring a fixup entry.
749 * The path to the node.
753 * The property containing the reference.
757 * The property value that contains the reference.
762 * A collection of property values that refer to phandles. These will
763 * be replaced by the value of the phandle property in their
766 std::vector<fixup> fixups;
768 * The locations of all of the values that are supposed to become phandle
769 * references, but refer to things outside of this file.
771 std::vector<std::reference_wrapper<fixup>> unresolved_fixups;
773 * The names of nodes that target phandles.
775 std::unordered_set<std::string> phandle_targets;
777 * A collection of input buffers that we are using. These input
778 * buffers are the ones that own their memory, and so we must preserve
779 * them for the lifetime of the device tree.
781 std::vector<std::unique_ptr<input_buffer>> buffers;
783 * A map of used phandle values to nodes. All phandles must be unique,
784 * so we keep a set of ones that the user explicitly provides in the
785 * input to ensure that we don't reuse them.
787 * This is a map, rather than a set, because we also want to be able to
788 * find phandles that were provided by the user explicitly when we are
791 std::unordered_map<uint32_t, node*> used_phandles;
793 * Paths to search for include files. This contains a set of
794 * nul-terminated strings, which are not owned by this class and so
795 * must be freed separately.
797 std::vector<std::string> include_paths;
799 * Dictionary of predefined macros provided on the command line.
803 * The default boot CPU, specified in the device tree header.
805 uint32_t boot_cpu = 0;
807 * The number of empty reserve map entries to generate in the blob.
809 uint32_t spare_reserve_map_entries = 0;
811 * The minimum size in bytes of the blob.
813 uint32_t minimum_blob_size = 0;
815 * The number of bytes of padding to add to the end of the blob.
817 uint32_t blob_padding = 0;
819 * Is this tree a plugin?
821 bool is_plugin = false;
823 * Visit all of the nodes recursively, and if they have labels then add
824 * them to the node_paths and node_names vectors so that they can be
825 * used in resolving cross references. Also collects phandle
826 * properties that have been explicitly added.
828 void collect_names_recursive(node_ptr &n, node_path &path);
830 * Assign a phandle property to a single node. The next parameter
831 * holds the phandle to be assigned, and will be incremented upon
834 property_ptr assign_phandle(node *n, uint32_t &next);
836 * Assign phandle properties to all nodes that have been referenced and
837 * require one. This method will recursively visit the tree starting at
838 * the node that it is passed.
840 void assign_phandles(node_ptr &n, uint32_t &next);
842 * Calls the recursive version of this method on every root node.
844 void collect_names();
846 * Resolves all cross references. Any properties that refer to another
847 * node must have their values replaced by either the node path or
848 * phandle value. The phandle parameter holds the next phandle to be
849 * assigned, should the need arise. It will be incremented upon each
850 * assignment of a phandle.
852 void resolve_cross_references(uint32_t &phandle);
854 * Parses a dts file in the given buffer and adds the roots to the parsed
855 * set. The `read_header` argument indicates whether the header has
856 * already been read. Some dts files place the header in an include,
857 * rather than in the top-level file.
859 void parse_file(text_input_buffer &input,
860 std::vector<node_ptr> &roots,
863 * Template function that writes a dtb blob using the specified writer.
864 * The writer defines the output format (assembly, blob).
866 template<class writer>
870 * Should we write the __symbols__ node (to allow overlays to be linked
871 * against this blob)?
873 bool write_symbols = false;
875 * Returns the node referenced by the property. If this is a tree that
876 * is in source form, then we have a string that we can use to index
877 * the cross_references array and so we can just look that up.
879 node *referenced_node(property_value &v);
881 * Writes this FDT as a DTB to the specified output.
883 void write_binary(int fd);
885 * Writes this FDT as an assembly representation of the DTB to the
886 * specified output. The result can then be assembled and linked into
889 void write_asm(int fd);
891 * Writes the tree in DTS (source) format.
893 void write_dts(int fd);
895 * Default constructor. Creates a valid, but empty FDT.
899 * Constructs a device tree from the specified file name, referring to
900 * a file that contains a device tree blob.
902 void parse_dtb(const std::string &fn, FILE *depfile);
904 * Construct a fragment wrapper around node. This will assume that node's
905 * name may be used as the target of the fragment, and the contents are to
906 * be wrapped in an __overlay__ node. The fragment wrapper will be assigned
907 * fragnumas its fragment number, and fragment number will be incremented.
909 node_ptr create_fragment_wrapper(node_ptr &node, int &fragnum);
911 * Generate a root node from the node passed in. This is sensitive to
912 * whether we're in a plugin context or not, so that if we're in a plugin we
913 * can circumvent any errors that might normally arise from a non-/ root.
914 * fragnum will be assigned to any fragment wrapper generated as a result
915 * of the call, and fragnum will be incremented.
917 node_ptr generate_root(node_ptr &node, int &fragnum);
919 * Reassign any fragment numbers from this new node, based on the given
922 void reassign_fragment_numbers(node_ptr &node, int &delta);
924 * Constructs a device tree from the specified file name, referring to
925 * a file that contains device tree source.
927 void parse_dts(const std::string &fn, FILE *depfile);
929 * Returns whether this tree is valid.
931 inline bool is_valid()
936 * Sets the format for writing phandle properties.
938 inline void set_phandle_format(phandle_format f)
940 phandle_node_name = f;
943 * Returns a pointer to the root node of this tree. No ownership
946 inline const node_ptr &get_root() const
951 * Sets the physical boot CPU.
953 void set_boot_cpu(uint32_t cpu)
958 * Sorts the tree. Useful for debugging device trees.
968 * Adds a path to search for include files. The argument must be a
969 * nul-terminated string representing the path. The device tree keeps
970 * a pointer to this string, but does not own it: the caller is
971 * responsible for freeing it if required.
973 void add_include_path(const char *path)
976 include_paths.push_back(std::move(p));
979 * Sets the number of empty reserve map entries to add.
981 void set_empty_reserve_map_entries(uint32_t e)
983 spare_reserve_map_entries = e;
986 * Sets the minimum size, in bytes, of the blob.
988 void set_blob_minimum_size(uint32_t s)
990 minimum_blob_size = s;
993 * Sets the amount of padding to add to the blob.
995 void set_blob_padding(uint32_t p)
1000 * Parses a predefined macro value.
1002 bool parse_define(const char *def);