]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/dtc/fdt.hh
Fix getfsstat compatibility system call panic.
[FreeBSD/FreeBSD.git] / usr.bin / dtc / fdt.hh
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 David Chisnall
5  * All rights reserved.
6  *
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.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34
35 #ifndef _FDT_HH_
36 #define _FDT_HH_
37 #include <unordered_map>
38 #include <unordered_set>
39 #include <memory>
40 #include <string>
41 #include <functional>
42
43 #include "util.hh"
44 #include "input_buffer.hh"
45
46 namespace dtc
47 {
48
49 namespace dtb 
50 {
51 struct output_writer;
52 class string_table;
53 }
54
55 namespace fdt
56 {
57 class property;
58 class node;
59 class device_tree;
60 /**
61  * Type for (owned) pointers to properties.
62  */
63 typedef std::shared_ptr<property> property_ptr;
64 /**
65  * Owning pointer to a node.
66  */
67 typedef std::unique_ptr<node> node_ptr;
68 /**
69  * Map from macros to property pointers.
70  */
71 typedef std::unordered_map<std::string, property_ptr> define_map;
72 /**
73  * Set of strings used for label names.
74  */
75 typedef std::unordered_set<std::string> string_set;
76 /**
77  * Properties may contain a number of different value, each with a different
78  * label.  This class encapsulates a single value.
79  */
80 struct property_value
81 {
82         /**
83          * The label for this data.  This is usually empty.
84          */
85         std::string label;
86         /**
87          * If this value is a string, or something resolved from a string (a
88          * reference) then this contains the source string.
89          */
90         std::string string_data;
91         /**
92          * The data that should be written to the final output.
93          */
94         byte_buffer byte_data;
95         /**
96          * Enumeration describing the possible types of a value.  Note that
97          * property-coded arrays will appear simply as binary (or possibly
98          * string, if they happen to be nul-terminated and printable), and must
99          * be checked separately.
100          */
101         enum value_type
102         {
103                 /**
104                  * This is a list of strings.  When read from source, string
105                  * lists become one property value for each string, however
106                  * when read from binary we have a single property value
107                  * incorporating the entire text, with nul bytes separating the
108                  * strings.
109                  */
110                 STRING_LIST,
111                 /**
112                  * This property contains a single string.
113                  */
114                 STRING,
115                 /**
116                  * This is a binary value.  Check the size of byte_data to
117                  * determine how many bytes this contains.
118                  */
119                 BINARY,
120                 /** This contains a short-form address that should be replaced
121                  * by a fully-qualified version.  This will only appear when
122                  * the input is a device tree source.  When parsed from a
123                  * device tree blob, the cross reference will have already been
124                  * resolved and the property value will be a string containing
125                  * the full path of the target node.  */
126                 CROSS_REFERENCE,
127                 /**
128                  * This is a phandle reference.  When parsed from source, the
129                  * string_data will contain the node label for the target and,
130                  * after cross references have been resolved, the binary data
131                  * will contain a 32-bit integer that should match the phandle
132                  * property of the target node.
133                  */
134                 PHANDLE,
135                 /**
136                  * An empty property value.  This will never appear on a real
137                  * property value, it is used by checkers to indicate that no
138                  * property values should exist for a property.
139                  */
140                 EMPTY,
141                 /**
142                  * The type of this property has not yet been determined.
143                  */
144                 UNKNOWN
145         };
146         /**
147          * The type of this property.
148          */
149         value_type type;
150         /**
151          * Returns true if this value is a cross reference, false otherwise.
152          */
153         inline bool is_cross_reference()
154         {
155                 return is_type(CROSS_REFERENCE);
156         }
157         /**
158          * Returns true if this value is a phandle reference, false otherwise.
159          */
160         inline bool is_phandle()
161         {
162                 return is_type(PHANDLE);
163         }
164         /**
165          * Returns true if this value is a string, false otherwise.
166          */
167         inline bool is_string()
168         {
169                 return is_type(STRING);
170         }
171         /**
172          * Returns true if this value is a string list (a nul-separated
173          * sequence of strings), false otherwise.
174          */
175         inline bool is_string_list()
176         {
177                 return is_type(STRING_LIST);
178         }
179         /**
180          * Returns true if this value is binary, false otherwise.
181          */
182         inline bool is_binary()
183         {
184                 return is_type(BINARY);
185         }
186         /**
187          * Returns this property value as a 32-bit integer.  Returns 0 if this
188          * property value is not 32 bits long.  The bytes in the property value
189          * are assumed to be in big-endian format, but the return value is in
190          * the host native endian.
191          */
192         uint32_t get_as_uint32();
193         /**
194          * Default constructor, specifying the label of the value.
195          */
196         property_value(std::string l=std::string()) : label(l), type(UNKNOWN) {}
197         /**
198          * Writes the data for this value into an output buffer.
199          */
200         void push_to_buffer(byte_buffer &buffer);
201
202         /**
203          * Writes the property value to the standard output.  This uses the
204          * following heuristics for deciding how to print the output:
205          *
206          * - If the value is nul-terminated and only contains printable
207          *   characters, it is written as a string.
208          * - If it is a multiple of 4 bytes long, then it is printed as cells.
209          * - Otherwise, it is printed as a byte buffer.
210          */
211         void write_dts(FILE *file);
212         /**
213          * Tries to merge adjacent property values, returns true if it succeeds and
214          * false otherwise.
215          */
216         bool try_to_merge(property_value &other);
217         /**
218          * Returns the size (in bytes) of this property value.
219          */
220         size_t size();
221         private:
222         /**
223          * Returns whether the value is of the specified type.  If the type of
224          * the value has not yet been determined, then this calculates it.
225          */
226         inline bool is_type(value_type v)
227         {
228                 if (type == UNKNOWN)
229                 {
230                         resolve_type();
231                 }
232                 return type == v;
233         }
234         /**
235          * Determines the type of the value based on its contents.
236          */
237         void resolve_type();
238         /**
239          * Writes the property value to the specified file as a quoted string.
240          * This is used when generating DTS.
241          */
242         void write_as_string(FILE *file);
243         /**
244          * Writes the property value to the specified file as a sequence of
245          * 32-bit big-endian cells.  This is used when generating DTS.
246          */
247         void write_as_cells(FILE *file);
248         /**
249          * Writes the property value to the specified file as a sequence of
250          * bytes.  This is used when generating DTS.
251          */
252         void write_as_bytes(FILE *file);
253 };
254
255 /**
256  * A value encapsulating a single property.  This contains a key, optionally a
257  * label, and optionally one or more values.
258  */
259 class property
260 {
261         /**
262          * The name of this property.
263          */
264         std::string key;
265         /**
266          * Zero or more labels.
267          */
268         string_set labels;
269         /**
270          * The values in this property.
271          */
272         std::vector<property_value> values;
273         /**
274          * Value indicating that this is a valid property.  If a parse error
275          * occurs, then this value is false.
276          */
277         bool valid;
278         /**
279          * Parses a string property value, i.e. a value enclosed in double quotes.
280          */
281         void parse_string(text_input_buffer &input);
282         /**
283          * Parses one or more 32-bit values enclosed in angle brackets.
284          */
285         void parse_cells(text_input_buffer &input, int cell_size);
286         /**
287          * Parses an array of bytes, contained within square brackets.
288          */
289         void parse_bytes(text_input_buffer &input);
290         /**
291          * Parses a reference.  This is a node label preceded by an ampersand
292          * symbol, which should expand to the full path to that node.
293          *
294          * Note: The specification says that the target of such a reference is
295          * a node name, however dtc assumes that it is a label, and so we
296          * follow their interpretation for compatibility.
297          */
298         void parse_reference(text_input_buffer &input);
299         /**
300          * Parse a predefined macro definition for a property.
301          */
302         void parse_define(text_input_buffer &input, define_map *defines);
303         /**
304          * Constructs a new property from two input buffers, pointing to the
305          * struct and strings tables in the device tree blob, respectively.
306          * The structs input buffer is assumed to have just consumed the
307          * FDT_PROP token.
308          */
309         property(input_buffer &structs, input_buffer &strings);
310         /**
311          * Parses a new property from the input buffer.  
312          */
313         property(text_input_buffer &input,
314                  std::string &&k,
315                  string_set &&l,
316                  bool terminated,
317                  define_map *defines);
318         public:
319         /**
320          * Creates an empty property.
321          */
322         property(std::string &&k, string_set &&l=string_set())
323                 : key(k), labels(l), valid(true) {}
324         /**
325          * Copy constructor.
326          */
327         property(property &p) : key(p.key), labels(p.labels), values(p.values),
328                 valid(p.valid) {}
329         /**
330          * Factory method for constructing a new property.  Attempts to parse a
331          * property from the input, and returns it on success.  On any parse
332          * error, this will return 0.
333          */
334         static property_ptr parse_dtb(input_buffer &structs,
335                                       input_buffer &strings);
336         /**
337          * Factory method for constructing a new property.  Attempts to parse a
338          * property from the input, and returns it on success.  On any parse
339          * error, this will return 0.
340          */
341         static property_ptr parse(text_input_buffer &input,
342                                   std::string &&key,
343                                   string_set &&labels=string_set(),
344                                   bool semicolonTerminated=true,
345                                   define_map *defines=0);
346         /**
347          * Iterator type used for accessing the values of a property.
348          */
349         typedef std::vector<property_value>::iterator value_iterator;
350         /**
351          * Returns an iterator referring to the first value in this property.
352          */
353         inline value_iterator begin()
354         {
355                 return values.begin();
356         }
357         /**
358          * Returns an iterator referring to the last value in this property.
359          */
360         inline value_iterator end()
361         {
362                 return values.end();
363         }
364         /**
365          * Adds a new value to an existing property.
366          */
367         inline void add_value(property_value v)
368         {
369                 values.push_back(v);
370         }
371         /**
372          * Returns the key for this property.
373          */
374         inline const std::string &get_key()
375         {
376                 return key;
377         }
378         /**
379          * Writes the property to the specified writer.  The property name is a
380          * reference into the strings table.
381          */
382         void write(dtb::output_writer &writer, dtb::string_table &strings);
383         /**
384          * Writes in DTS format to the specified file, at the given indent
385          * level.  This will begin the line with the number of tabs specified
386          * as the indent level and then write the property in the most
387          * applicable way that it can determine.
388          */
389         void write_dts(FILE *file, int indent);
390         /**
391          * Returns the byte offset of the specified property value.
392          */
393         size_t offset_of_value(property_value &val);
394 };
395
396 /**
397  * Class encapsulating a device tree node.  Nodes may contain properties and
398  * other nodes.
399  */
400 class node
401 {
402         public:
403         /**
404          * The labels for this node, if any.  Node labels are used as the
405          * targets for cross references.
406          */
407         std::unordered_set<std::string> labels;
408         /**
409          * The name of the node.
410          */
411         std::string name;
412         /**
413          * The name of the node is a path reference.
414          */
415         bool name_is_path_reference = false;
416         /**
417          * The unit address of the node, which is optionally written after the
418          * name followed by an at symbol.
419          */
420         std::string unit_address;
421         /**
422          * A flag indicating that this node has been marked /omit-if-no-ref/ and
423          * will be omitted if it is not referenced, either directly or indirectly,
424          * by a node that is not similarly denoted.
425          */
426         bool omit_if_no_ref = false;
427         /**
428          * A flag indicating that this node has been referenced, either directly
429          * or indirectly, by a node that is not marked /omit-if-no-ref/.
430          */
431         bool used = false;
432         /**
433          * The type for the property vector.
434          */
435         typedef std::vector<property_ptr> property_vector;
436         /**
437          * Iterator type for child nodes.
438          */
439         typedef std::vector<node_ptr>::iterator child_iterator;
440         /**
441          * Recursion behavior to be observed for visiting
442          */
443         enum visit_behavior
444         {
445                 /**
446                  * Recurse as normal through the rest of the tree.
447                  */
448                 VISIT_RECURSE,
449                 /**
450                  * Continue recursing through the device tree, but do not
451                  * recurse through this branch of the tree any further.
452                  */
453                 VISIT_CONTINUE,
454                 /**
455                  * Immediately halt the visit.  No further nodes will be visited.
456                  */
457                 VISIT_BREAK
458         };
459         private:
460         /**
461          * Adaptor to use children in range-based for loops.
462          */
463         struct child_range
464         {
465                 child_range(node &nd) : n(nd) {}
466                 child_iterator begin() { return n.child_begin(); }
467                 child_iterator end() { return n.child_end(); }
468                 private:
469                 node &n;
470         };
471         /**
472          * Adaptor to use properties in range-based for loops.
473          */
474         struct property_range
475         {
476                 property_range(node &nd) : n(nd) {}
477                 property_vector::iterator begin() { return n.property_begin(); }
478                 property_vector::iterator end() { return n.property_end(); }
479                 private:
480                 node &n;
481         };
482         /**
483          * The properties contained within this node.
484          */
485         property_vector props;
486         /**
487          * The children of this node.
488          */
489         std::vector<node_ptr> children;
490         /**
491          * Children that should be deleted from this node when merging.
492          */
493         std::unordered_set<std::string> deleted_children;
494         /**
495          * Properties that should be deleted from this node when merging.
496          */
497         std::unordered_set<std::string> deleted_props;
498         /**
499          * A flag indicating whether this node is valid.  This is set to false
500          * if an error occurs during parsing.
501          */
502         bool valid;
503         /**
504          * Parses a name inside a node, writing the string passed as the last
505          * argument as an error if it fails.  
506          */
507         std::string parse_name(text_input_buffer &input,
508                                bool &is_property,
509                                const char *error);
510         /**
511          * Constructs a new node from two input buffers, pointing to the struct
512          * and strings tables in the device tree blob, respectively.
513          */
514         node(input_buffer &structs, input_buffer &strings);
515         /**
516          * Parses a new node from the specified input buffer.  This is called
517          * when the input cursor is on the open brace for the start of the
518          * node.  The name, and optionally label and unit address, should have
519          * already been parsed.
520          */
521         node(text_input_buffer &input,
522              device_tree &tree,
523              std::string &&n,
524              std::unordered_set<std::string> &&l,
525              std::string &&a,
526              define_map*);
527         /**
528          * Creates a special node with the specified name and properties.
529          */
530         node(const std::string &n, const std::vector<property_ptr> &p);
531         /**
532          * Comparison function for properties, used when sorting the properties
533          * vector.  Orders the properties based on their names.
534          */
535         static inline bool cmp_properties(property_ptr &p1, property_ptr &p2);
536                 /*
537         {
538                 return p1->get_key() < p2->get_key();
539         }
540         */
541         /**
542          * Comparison function for nodes, used when sorting the children
543          * vector.  Orders the nodes based on their names or, if the names are
544          * the same, by the unit addresses.
545          */
546         static inline bool cmp_children(node_ptr &c1, node_ptr &c2);
547         public:
548         /**
549          * Sorts the node's properties and children into alphabetical order and
550          * recursively sorts the children.
551          */
552         void sort();
553         /**
554          * Returns an iterator for the first child of this node.
555          */
556         inline child_iterator child_begin()
557         {
558                 return children.begin();
559         }
560         /**
561          * Returns an iterator after the last child of this node.
562          */
563         inline child_iterator child_end()
564         {
565                 return children.end();
566         }
567         /**
568          * Returns a range suitable for use in a range-based for loop describing
569          * the children of this node.
570          */
571         inline child_range child_nodes()
572         {
573                 return child_range(*this);
574         }
575         /**
576          * Accessor for the deleted children.
577          */
578         inline const std::unordered_set<std::string> &deleted_child_nodes()
579         {
580                 return deleted_children;
581         }
582         /**
583          * Accessor for the deleted properties
584          */
585         inline const std::unordered_set<std::string> &deleted_properties()
586         {
587                 return deleted_props;
588         }
589         /**
590          * Returns a range suitable for use in a range-based for loop describing
591          * the properties of this node.
592          */
593         inline property_range properties()
594         {
595                 return property_range(*this);
596         }
597         /**
598          * Returns an iterator after the last property of this node.
599          */
600         inline property_vector::iterator property_begin()
601         {
602                 return props.begin();
603         }
604         /**
605          * Returns an iterator for the first property of this node.
606          */
607         inline property_vector::iterator property_end()
608         {
609                 return props.end();
610         }
611         /**
612          * Factory method for constructing a new node.  Attempts to parse a
613          * node in DTS format from the input, and returns it on success.  On
614          * any parse error, this will return 0.  This should be called with the
615          * cursor on the open brace of the property, after the name and so on
616          * have been parsed.
617          */
618         static node_ptr parse(text_input_buffer &input,
619                               device_tree &tree,
620                               std::string &&name,
621                               std::unordered_set<std::string> &&label=std::unordered_set<std::string>(),
622                               std::string &&address=std::string(),
623                               define_map *defines=0);
624         /**
625          * Factory method for constructing a new node.  Attempts to parse a
626          * node in DTB format from the input, and returns it on success.  On
627          * any parse error, this will return 0.  This should be called with the
628          * cursor on the open brace of the property, after the name and so on
629          * have been parsed.
630          */
631         static node_ptr parse_dtb(input_buffer &structs, input_buffer &strings);
632         /**
633          * Construct a new special node from a name and set of properties.
634          */
635         static node_ptr create_special_node(const std::string &name,
636                         const std::vector<property_ptr> &props);
637         /**
638          * Returns a property corresponding to the specified key, or 0 if this
639          * node does not contain a property of that name.
640          */
641         property_ptr get_property(const std::string &key);
642         /**
643          * Adds a new property to this node.
644          */
645         inline void add_property(property_ptr &p)
646         {
647                 props.push_back(p);
648         }
649         /**
650          * Adds a new child to this node.
651          */
652         inline void add_child(node_ptr &&n)
653         {
654                 children.push_back(std::move(n));
655         }
656         /**
657          * Deletes any children from this node.
658          */
659         inline void delete_children_if(bool (*predicate)(node_ptr &))
660         {
661                 children.erase(std::remove_if(children.begin(), children.end(), predicate), children.end());
662         }
663         /**
664          * Merges a node into this one.  Any properties present in both are
665          * overridden, any properties present in only one are preserved.
666          */
667         void merge_node(node_ptr &other);
668         /**
669          * Write this node to the specified output.  Although nodes do not
670          * refer to a string table directly, their properties do.  The string
671          * table passed as the second argument is used for the names of
672          * properties within this node and its children.
673          */
674         void write(dtb::output_writer &writer, dtb::string_table &strings);
675         /**
676          * Writes the current node as DTS to the specified file.  The second
677          * parameter is the indent level.  This function will start every line
678          * with this number of tabs.  
679          */
680         void write_dts(FILE *file, int indent);
681         /**
682          * Recursively visit this node and then its children based on the
683          * callable's return value.  The callable may return VISIT_BREAK
684          * immediately halt all recursion and end the visit, VISIT_CONTINUE to
685          * not recurse into the current node's children, or VISIT_RECURSE to recurse
686          * through children as expected.  parent will be passed to the callable.
687          */
688         visit_behavior visit(std::function<visit_behavior(node&, node*)>, node *parent);
689 };
690
691 /**
692  * Class encapsulating the entire parsed FDT.  This is the top-level class,
693  * which parses the entire DTS representation and write out the finished
694  * version.
695  */
696 class device_tree
697 {
698         public:
699         /**
700          * Type used for node paths.  A node path is sequence of names and unit
701          * addresses.
702          */
703         class node_path : public std::vector<std::pair<std::string,std::string>>
704         {
705                 public:
706                 /**
707                  * Converts this to a string representation.
708                  */
709                 std::string to_string() const;
710         };
711         /**
712          * Name that we should use for phandle nodes.
713          */
714         enum phandle_format
715         {
716                 /** linux,phandle */
717                 LINUX,
718                 /** phandle */
719                 EPAPR,
720                 /** Create both nodes. */
721                 BOTH
722         };
723         private:
724         /**
725          * The format that we should use for writing phandles.
726          */
727         phandle_format phandle_node_name = EPAPR;
728         /**
729          * Flag indicating that this tree is valid.  This will be set to false
730          * on parse errors. 
731          */
732         bool valid = true;
733         /**
734          * Flag indicating that this tree requires garbage collection.  This will be
735          * set to true if a node marked /omit-if-no-ref/ is encountered.
736          */
737         bool garbage_collect = false;
738         /**
739          * Type used for memory reservations.  A reservation is two 64-bit
740          * values indicating a base address and length in memory that the
741          * kernel should not use.  The high 32 bits are ignored on 32-bit
742          * platforms.
743          */
744         typedef std::pair<uint64_t, uint64_t> reservation;
745         /**
746          * The memory reserves table.
747          */
748         std::vector<reservation> reservations;
749         /**
750          * Root node.  All other nodes are children of this node.
751          */
752         node_ptr root;
753         /**
754          * Mapping from names to nodes.  Only unambiguous names are recorded,
755          * duplicate names are stored as (node*)-1.
756          */
757         std::unordered_map<std::string, node*> node_names;
758         /**
759          * A map from labels to node paths.  When resolving cross references,
760          * we look up referenced nodes in this and replace the cross reference
761          * with the full path to its target.
762          */
763         std::unordered_map<std::string, node_path> node_paths;
764         /**
765          * All of the elements in `node_paths` in the order that they were
766          * created.  This is used for emitting the `__symbols__` section, where
767          * we want to guarantee stable ordering.
768          */
769         std::vector<std::pair<std::string, node_path>> ordered_node_paths;
770         /**
771          * A collection of property values that are references to other nodes.
772          * These should be expanded to the full path of their targets.
773          */
774         std::vector<property_value*> cross_references;
775         /**
776          * The location of something requiring a fixup entry.
777          */
778         struct fixup
779         {
780                 /**
781                  * The path to the node.
782                  */
783                 node_path path;
784                 /**
785                  * The property containing the reference.
786                  */
787                 property_ptr prop;
788                 /**
789                  * The property value that contains the reference.
790                  */
791                 property_value &val;
792         };
793         /**
794          * A collection of property values that refer to phandles.  These will
795          * be replaced by the value of the phandle property in their
796          * destination.
797          */
798         std::vector<fixup> fixups;
799         /**
800          * The locations of all of the values that are supposed to become phandle
801          * references, but refer to things outside of this file.  
802          */
803         std::vector<std::reference_wrapper<fixup>> unresolved_fixups;
804         /**
805          * The names of nodes that target phandles.
806          */
807         std::unordered_set<std::string> phandle_targets;
808         /**
809          * A collection of input buffers that we are using.  These input
810          * buffers are the ones that own their memory, and so we must preserve
811          * them for the lifetime of the device tree.  
812          */
813         std::vector<std::unique_ptr<input_buffer>> buffers;
814         /**
815          * A map of used phandle values to nodes.  All phandles must be unique,
816          * so we keep a set of ones that the user explicitly provides in the
817          * input to ensure that we don't reuse them.
818          *
819          * This is a map, rather than a set, because we also want to be able to
820          * find phandles that were provided by the user explicitly when we are
821          * doing checking.
822          */
823         std::unordered_map<uint32_t, node*> used_phandles;
824         /**
825          * Paths to search for include files.  This contains a set of
826          * nul-terminated strings, which are not owned by this class and so
827          * must be freed separately.
828          */
829         std::vector<std::string> include_paths;
830         /**
831          * Dictionary of predefined macros provided on the command line.
832          */
833         define_map               defines;
834         /**
835          * The default boot CPU, specified in the device tree header.
836          */
837         uint32_t boot_cpu = 0;
838         /**
839          * The number of empty reserve map entries to generate in the blob.
840          */
841         uint32_t spare_reserve_map_entries = 0;
842         /**
843          * The minimum size in bytes of the blob.
844          */
845         uint32_t minimum_blob_size = 0;
846         /**
847          * The number of bytes of padding to add to the end of the blob.
848          */
849         uint32_t blob_padding = 0;
850         /**
851          * Is this tree a plugin?
852          */
853         bool is_plugin = false;
854         /**
855          * Visit all of the nodes recursively, and if they have labels then add
856          * them to the node_paths and node_names vectors so that they can be
857          * used in resolving cross references.  Also collects phandle
858          * properties that have been explicitly added.  
859          */
860         void collect_names_recursive(node_ptr &n, node_path &path);
861         /**
862          * Assign a phandle property to a single node.  The next parameter
863          * holds the phandle to be assigned, and will be incremented upon
864          * assignment.
865          */
866         property_ptr assign_phandle(node *n, uint32_t &next);
867         /**
868          * Assign phandle properties to all nodes that have been referenced and
869          * require one.  This method will recursively visit the tree starting at
870          * the node that it is passed.
871          */
872         void assign_phandles(node_ptr &n, uint32_t &next);
873         /**
874          * Calls the recursive version of this method on every root node.
875          */
876         void collect_names();
877         /**
878          * Resolves all cross references.  Any properties that refer to another
879          * node must have their values replaced by either the node path or
880          * phandle value.  The phandle parameter holds the next phandle to be
881          * assigned, should the need arise.  It will be incremented upon each
882          * assignment of a phandle.  Garbage collection of unreferenced nodes
883          * marked for "delete if unreferenced" will also occur here.
884          */
885         void resolve_cross_references(uint32_t &phandle);
886         /**
887          * Garbage collects nodes that have been marked /omit-if-no-ref/ and do not
888          * have any references to them from nodes that are similarly marked.  This
889          * is a fairly expensive operation.  The return value indicates whether the
890          * tree has been dirtied as a result of this operation, so that the caller
891          * may take appropriate measures to bring the device tree into a consistent
892          * state as needed.
893          */
894         bool garbage_collect_marked_nodes();
895         /**
896          * Parses a dts file in the given buffer and adds the roots to the parsed
897          * set.  The `read_header` argument indicates whether the header has
898          * already been read.  Some dts files place the header in an include,
899          * rather than in the top-level file.
900          */
901         void parse_file(text_input_buffer &input,
902                         std::vector<node_ptr> &roots,
903                         bool &read_header);
904         /**
905          * Template function that writes a dtb blob using the specified writer.
906          * The writer defines the output format (assembly, blob).
907          */
908         template<class writer>
909         void write(int fd);
910         public:
911         /**
912          * Should we write the __symbols__ node (to allow overlays to be linked
913          * against this blob)?
914          */
915         bool write_symbols = false;
916         /**
917          * Returns the node referenced by the property.  If this is a tree that
918          * is in source form, then we have a string that we can use to index
919          * the cross_references array and so we can just look that up.  
920          */
921         node *referenced_node(property_value &v);
922         /**
923          * Writes this FDT as a DTB to the specified output.
924          */
925         void write_binary(int fd);
926         /**
927          * Writes this FDT as an assembly representation of the DTB to the
928          * specified output.  The result can then be assembled and linked into
929          * a program.
930          */
931         void write_asm(int fd);
932         /**
933          * Writes the tree in DTS (source) format.
934          */
935         void write_dts(int fd);
936         /**
937          * Default constructor.  Creates a valid, but empty FDT.
938          */
939         device_tree() {}
940         /**
941          * Constructs a device tree from the specified file name, referring to
942          * a file that contains a device tree blob.
943          */
944         void parse_dtb(const std::string &fn, FILE *depfile);
945         /**
946          * Construct a fragment wrapper around node.  This will assume that node's
947          * name may be used as the target of the fragment, and the contents are to
948          * be wrapped in an __overlay__ node.  The fragment wrapper will be assigned
949          * fragnumas its fragment number, and fragment number will be incremented.
950          */
951         node_ptr create_fragment_wrapper(node_ptr &node, int &fragnum);
952         /**
953          * Generate a root node from the node passed in.  This is sensitive to
954          * whether we're in a plugin context or not, so that if we're in a plugin we
955          * can circumvent any errors that might normally arise from a non-/ root.
956          * fragnum will be assigned to any fragment wrapper generated as a result
957          * of the call, and fragnum will be incremented.
958          */
959         node_ptr generate_root(node_ptr &node, int &fragnum);
960         /**
961          * Reassign any fragment numbers from this new node, based on the given
962          * delta.
963          */
964         void reassign_fragment_numbers(node_ptr &node, int &delta);
965         /*
966          * Constructs a device tree from the specified file name, referring to
967          * a file that contains device tree source.
968          */
969         void parse_dts(const std::string &fn, FILE *depfile);
970         /**
971          * Returns whether this tree is valid.
972          */
973         inline bool is_valid()
974         {
975                 return valid;
976         }
977         /**
978          * Mark this tree as needing garbage collection, because an /omit-if-no-ref/
979          * node has been encountered.
980          */
981         void set_needs_garbage_collection()
982         {
983                 garbage_collect = true;
984         }
985         /**
986          * Sets the format for writing phandle properties.
987          */
988         inline void set_phandle_format(phandle_format f)
989         {
990                 phandle_node_name = f;
991         }
992         /**
993          * Returns a pointer to the root node of this tree.  No ownership
994          * transfer.
995          */
996         inline const node_ptr &get_root() const
997         {
998                 return root;
999         }
1000         /**
1001          * Sets the physical boot CPU.
1002          */
1003         void set_boot_cpu(uint32_t cpu)
1004         {
1005                 boot_cpu = cpu;
1006         }
1007         /**
1008          * Sorts the tree.  Useful for debugging device trees.
1009          */
1010         void sort()
1011         {
1012                 if (root)
1013                 {
1014                         root->sort();
1015                 }
1016         }
1017         /**
1018          * Adds a path to search for include files.  The argument must be a
1019          * nul-terminated string representing the path.  The device tree keeps
1020          * a pointer to this string, but does not own it: the caller is
1021          * responsible for freeing it if required.
1022          */
1023         void add_include_path(const char *path)
1024         {
1025                 std::string p(path);
1026                 include_paths.push_back(std::move(p));
1027         }
1028         /**
1029          * Sets the number of empty reserve map entries to add.
1030          */
1031         void set_empty_reserve_map_entries(uint32_t e)
1032         {
1033                 spare_reserve_map_entries = e;
1034         }
1035         /**
1036          * Sets the minimum size, in bytes, of the blob.
1037          */
1038         void set_blob_minimum_size(uint32_t s)
1039         {
1040                 minimum_blob_size = s;
1041         }
1042         /**
1043          * Sets the amount of padding to add to the blob.
1044          */
1045         void set_blob_padding(uint32_t p)
1046         {
1047                 blob_padding = p;
1048         }
1049         /**
1050          * Parses a predefined macro value.
1051          */
1052         bool parse_define(const char *def);
1053 };
1054
1055 } // namespace fdt
1056
1057 } // namespace dtc
1058
1059 #endif // !_FDT_HH_