xref: /freebsd/usr.bin/dtc/fdt.hh (revision 009e81b16465ea457c0e63fd49fe77f47cc27a5a)
1 /*-
2  * Copyright (c) 2013 David Chisnall
3  * All rights reserved.
4  *
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.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #ifndef _FDT_HH_
34 #define _FDT_HH_
35 #include <unordered_map>
36 #include <unordered_set>
37 #include <memory>
38 #include <string>
39 #include <functional>
40 
41 #include "util.hh"
42 #include "string.hh"
43 
44 namespace dtc
45 {
46 
47 namespace dtb
48 {
49 struct output_writer;
50 class string_table;
51 }
52 
53 namespace fdt
54 {
55 class property;
56 class node;
57 /**
58  * Type for (owned) pointers to properties.
59  */
60 typedef std::shared_ptr<property> property_ptr;
61 /**
62  * Owning pointer to a node.
63  */
64 typedef std::unique_ptr<node> node_ptr;
65 /**
66  * Map from macros to property pointers.
67  */
68 typedef std::unordered_map<string, property_ptr> define_map;
69 /**
70  * Properties may contain a number of different value, each with a different
71  * label.  This class encapsulates a single value.
72  */
73 struct property_value
74 {
75 	/**
76 	 * The label for this data.  This is usually empty.
77 	 */
78 	string label;
79 	/**
80 	 * If this value is a string, or something resolved from a string (a
81 	 * reference) then this contains the source string.
82 	 */
83 	string string_data;
84 	/**
85 	 * The data that should be written to the final output.
86 	 */
87 	byte_buffer byte_data;
88 	/**
89 	 * Enumeration describing the possible types of a value.  Note that
90 	 * property-coded arrays will appear simply as binary (or possibly
91 	 * string, if they happen to be nul-terminated and printable), and must
92 	 * be checked separately.
93 	 */
94 	enum value_type
95 	{
96 		/**
97 		 * This is a list of strings.  When read from source, string
98 		 * lists become one property value for each string, however
99 		 * when read from binary we have a single property value
100 		 * incorporating the entire text, with nul bytes separating the
101 		 * strings.
102 		 */
103 		STRING_LIST,
104 		/**
105 		 * This property contains a single string.
106 		 */
107 		STRING,
108 		/**
109 		 * This is a binary value.  Check the size of byte_data to
110 		 * determine how many bytes this contains.
111 		 */
112 		BINARY,
113 		/** This contains a short-form address that should be replaced
114 		 * by a fully-qualified version.  This will only appear when
115 		 * the input is a device tree source.  When parsed from a
116 		 * device tree blob, the cross reference will have already been
117 		 * resolved and the property value will be a string containing
118 		 * the full path of the target node.  */
119 		CROSS_REFERENCE,
120 		/**
121 		 * This is a phandle reference.  When parsed from source, the
122 		 * string_data will contain the node label for the target and,
123 		 * after cross references have been resolved, the binary data
124 		 * will contain a 32-bit integer that should match the phandle
125 		 * property of the target node.
126 		 */
127 		PHANDLE,
128 		/**
129 		 * An empty property value.  This will never appear on a real
130 		 * property value, it is used by checkers to indicate that no
131 		 * property values should exist for a property.
132 		 */
133 		EMPTY,
134 		/**
135 		 * The type of this property has not yet been determined.
136 		 */
137 		UNKNOWN
138 	};
139 	/**
140 	 * The type of this property.
141 	 */
142 	value_type type;
143 	/**
144 	 * Returns true if this value is a cross reference, false otherwise.
145 	 */
146 	inline bool is_cross_reference()
147 	{
148 		return is_type(CROSS_REFERENCE);
149 	}
150 	/**
151 	 * Returns true if this value is a phandle reference, false otherwise.
152 	 */
153 	inline bool is_phandle()
154 	{
155 		return is_type(PHANDLE);
156 	}
157 	/**
158 	 * Returns true if this value is a string, false otherwise.
159 	 */
160 	inline bool is_string()
161 	{
162 		return is_type(STRING);
163 	}
164 	/**
165 	 * Returns true if this value is a string list (a nul-separated
166 	 * sequence of strings), false otherwise.
167 	 */
168 	inline bool is_string_list()
169 	{
170 		return is_type(STRING_LIST);
171 	}
172 	/**
173 	 * Returns true if this value is binary, false otherwise.
174 	 */
175 	inline bool is_binary()
176 	{
177 		return is_type(BINARY);
178 	}
179 	/**
180 	 * Returns this property value as a 32-bit integer.  Returns 0 if this
181 	 * property value is not 32 bits long.  The bytes in the property value
182 	 * are assumed to be in big-endian format, but the return value is in
183 	 * the host native endian.
184 	 */
185 	uint32_t get_as_uint32();
186 	/**
187 	 * Default constructor, specifying the label of the value.
188 	 */
189 	property_value(string l=string()) : label(l), type(UNKNOWN) {}
190 	/**
191 	 * Writes the data for this value into an output buffer.
192 	 */
193 	void push_to_buffer(byte_buffer &buffer);
194 
195 	/**
196 	 * Writes the property value to the standard output.  This uses the
197 	 * following heuristics for deciding how to print the output:
198 	 *
199 	 * - If the value is nul-terminated and only contains printable
200 	 *   characters, it is written as a string.
201 	 * - If it is a multiple of 4 bytes long, then it is printed as cells.
202 	 * - Otherwise, it is printed as a byte buffer.
203 	 */
204 	void write_dts(FILE *file);
205 	/**
206 	 * Tries to merge adjacent property values, returns true if it succeeds and
207 	 * false otherwise.
208 	 */
209 	bool try_to_merge(property_value &other);
210 	private:
211 	/**
212 	 * Returns whether the value is of the specified type.  If the type of
213 	 * the value has not yet been determined, then this calculates it.
214 	 */
215 	inline bool is_type(value_type v)
216 	{
217 		if (type == UNKNOWN)
218 		{
219 			resolve_type();
220 		}
221 		return type == v;
222 	}
223 	/**
224 	 * Determines the type of the value based on its contents.
225 	 */
226 	void resolve_type();
227 	/**
228 	 * Writes the property value to the specified file as a quoted string.
229 	 * This is used when generating DTS.
230 	 */
231 	void write_as_string(FILE *file);
232 	/**
233 	 * Writes the property value to the specified file as a sequence of
234 	 * 32-bit big-endian cells.  This is used when generating DTS.
235 	 */
236 	void write_as_cells(FILE *file);
237 	/**
238 	 * Writes the property value to the specified file as a sequence of
239 	 * bytes.  This is used when generating DTS.
240 	 */
241 	void write_as_bytes(FILE *file);
242 };
243 
244 /**
245  * A value encapsulating a single property.  This contains a key, optionally a
246  * label, and optionally one or more values.
247  */
248 class property
249 {
250 	/**
251 	 * The name of this property.
252 	 */
253 	string key;
254 	/**
255 	 * An optional label.
256 	 */
257 	string label;
258 	/**
259 	 * The values in this property.
260 	 */
261 	std::vector<property_value> values;
262 	/**
263 	 * Value indicating that this is a valid property.  If a parse error
264 	 * occurs, then this value is false.
265 	 */
266 	bool valid;
267 	/**
268 	 * Parses a string property value, i.e. a value enclosed in double quotes.
269 	 */
270 	void parse_string(input_buffer &input);
271 	/**
272 	 * Parses one or more 32-bit values enclosed in angle brackets.
273 	 */
274 	void parse_cells(input_buffer &input, int cell_size);
275 	/**
276 	 * Parses an array of bytes, contained within square brackets.
277 	 */
278 	void parse_bytes(input_buffer &input);
279 	/**
280 	 * Parses a reference.  This is a node label preceded by an ampersand
281 	 * symbol, which should expand to the full path to that node.
282 	 *
283 	 * Note: The specification says that the target of such a reference is
284 	 * a node name, however dtc assumes that it is a label, and so we
285 	 * follow their interpretation for compatibility.
286 	 */
287 	void parse_reference(input_buffer &input);
288 	/**
289 	 * Parse a predefined macro definition for a property.
290 	 */
291 	void parse_define(input_buffer &input, define_map *defines);
292 	/**
293 	 * Constructs a new property from two input buffers, pointing to the
294 	 * struct and strings tables in the device tree blob, respectively.
295 	 * The structs input buffer is assumed to have just consumed the
296 	 * FDT_PROP token.
297 	 */
298 	property(input_buffer &structs, input_buffer &strings);
299 	/**
300 	 * Parses a new property from the input buffer.
301 	 */
302 	property(input_buffer &input,
303 	         string k,
304 	         string l,
305 	         bool terminated,
306 	         define_map *defines);
307 	public:
308 	/**
309 	 * Creates an empty property.
310 	 */
311 	property(string k, string l=string()) : key(k), label(l), valid(true)
312 	{}
313 	/**
314 	 * Copy constructor.
315 	 */
316 	property(property &p) : key(p.key), label(p.label), values(p.values),
317 		valid(p.valid) {}
318 	/**
319 	 * Factory method for constructing a new property.  Attempts to parse a
320 	 * property from the input, and returns it on success.  On any parse
321 	 * error, this will return 0.
322 	 */
323 	static property_ptr parse_dtb(input_buffer &structs,
324 	                           input_buffer &strings);
325 	/**
326 	 * Factory method for constructing a new property.  Attempts to parse a
327 	 * property from the input, and returns it on success.  On any parse
328 	 * error, this will return 0.
329 	 */
330 	static property_ptr parse(input_buffer &input,
331 	                          string key,
332 	                          string label=string(),
333 	                          bool semicolonTerminated=true,
334 	                          define_map *defines=0);
335 	/**
336 	 * Iterator type used for accessing the values of a property.
337 	 */
338 	typedef std::vector<property_value>::iterator value_iterator;
339 	/**
340 	 * Returns an iterator referring to the first value in this property.
341 	 */
342 	inline value_iterator begin()
343 	{
344 		return values.begin();
345 	}
346 	/**
347 	 * Returns an iterator referring to the last value in this property.
348 	 */
349 	inline value_iterator end()
350 	{
351 		return values.end();
352 	}
353 	/**
354 	 * Adds a new value to an existing property.
355 	 */
356 	inline void add_value(property_value v)
357 	{
358 		values.push_back(v);
359 	}
360 	/**
361 	 * Returns the key for this property.
362 	 */
363 	inline string get_key()
364 	{
365 		return key;
366 	}
367 	/**
368 	 * Writes the property to the specified writer.  The property name is a
369 	 * reference into the strings table.
370 	 */
371 	void write(dtb::output_writer &writer, dtb::string_table &strings);
372 	/**
373 	 * Writes in DTS format to the specified file, at the given indent
374 	 * level.  This will begin the line with the number of tabs specified
375 	 * as the indent level and then write the property in the most
376 	 * applicable way that it can determine.
377 	 */
378 	void write_dts(FILE *file, int indent);
379 };
380 
381 /**
382  * Class encapsulating a device tree node.  Nodes may contain properties and
383  * other nodes.
384  */
385 class node
386 {
387 	public:
388 	/**
389 	 * The label for this node, if any.  Node labels are used as the
390 	 * targets for cross references.
391 	 */
392 	string label;
393 	/**
394 	 * The name of the node.
395 	 */
396 	string name;
397 	/**
398 	 * The unit address of the node, which is optionally written after the
399 	 * name followed by an at symbol.
400 	 */
401 	string unit_address;
402 	/**
403 	 * The type for the property vector.
404 	 */
405 	typedef std::vector<property_ptr> property_vector;
406 	/**
407 	 * Iterator type for child nodes.
408 	 */
409 	typedef std::vector<node_ptr>::iterator child_iterator;
410 	private:
411 	/**
412 	 * Adaptor to use children in range-based for loops.
413 	 */
414 	struct child_range
415 	{
416 		child_range(node &nd) : n(nd) {}
417 		child_iterator begin() { return n.child_begin(); }
418 		child_iterator end() { return n.child_end(); }
419 		private:
420 		node &n;
421 	};
422 	/**
423 	 * Adaptor to use properties in range-based for loops.
424 	 */
425 	struct property_range
426 	{
427 		property_range(node &nd) : n(nd) {}
428 		property_vector::iterator begin() { return n.property_begin(); }
429 		property_vector::iterator end() { return n.property_end(); }
430 		private:
431 		node &n;
432 	};
433 	/**
434 	 * The properties contained within this node.
435 	 */
436 	property_vector props;
437 	/**
438 	 * The children of this node.
439 	 */
440 	std::vector<node_ptr> children;
441 	/**
442 	 * A flag indicating whether this node is valid.  This is set to false
443 	 * if an error occurs during parsing.
444 	 */
445 	bool valid;
446 	/**
447 	 * Parses a name inside a node, writing the string passed as the last
448 	 * argument as an error if it fails.
449 	 */
450 	string parse_name(input_buffer &input,
451 	                  bool &is_property,
452 	                  const char *error);
453 	/**
454 	 * Constructs a new node from two input buffers, pointing to the struct
455 	 * and strings tables in the device tree blob, respectively.
456 	 */
457 	node(input_buffer &structs, input_buffer &strings);
458 	/**
459 	 * Parses a new node from the specified input buffer.  This is called
460 	 * when the input cursor is on the open brace for the start of the
461 	 * node.  The name, and optionally label and unit address, should have
462 	 * already been parsed.
463 	 */
464 	node(input_buffer &input, string n, string l, string a, define_map*);
465 	/**
466 	 * Comparison function for properties, used when sorting the properties
467 	 * vector.  Orders the properties based on their names.
468 	 */
469 	static inline bool cmp_properties(property_ptr &p1, property_ptr &p2);
470 		/*
471 	{
472 		return p1->get_key() < p2->get_key();
473 	}
474 	*/
475 	/**
476 	 * Comparison function for nodes, used when sorting the children
477 	 * vector.  Orders the nodes based on their names or, if the names are
478 	 * the same, by the unit addresses.
479 	 */
480 	static inline bool cmp_children(node_ptr &c1, node_ptr &c2);
481 	public:
482 	/**
483 	 * Sorts the node's properties and children into alphabetical order and
484 	 * recursively sorts the children.
485 	 */
486 	void sort();
487 	/**
488 	 * Returns an iterator for the first child of this node.
489 	 */
490 	inline child_iterator child_begin()
491 	{
492 		return children.begin();
493 	}
494 	/**
495 	 * Returns an iterator after the last child of this node.
496 	 */
497 	inline child_iterator child_end()
498 	{
499 		return children.end();
500 	}
501 	inline child_range child_nodes()
502 	{
503 		return child_range(*this);
504 	}
505 	inline property_range properties()
506 	{
507 		return property_range(*this);
508 	}
509 	/**
510 	 * Returns an iterator after the last property of this node.
511 	 */
512 	inline property_vector::iterator property_begin()
513 	{
514 		return props.begin();
515 	}
516 	/**
517 	 * Returns an iterator for the first property of this node.
518 	 */
519 	inline property_vector::iterator property_end()
520 	{
521 		return props.end();
522 	}
523 	/**
524 	 * Factory method for constructing a new node.  Attempts to parse a
525 	 * node in DTS format from the input, and returns it on success.  On
526 	 * any parse error, this will return 0.  This should be called with the
527 	 * cursor on the open brace of the property, after the name and so on
528 	 * have been parsed.
529 	 */
530 	static node_ptr parse(input_buffer &input,
531 	                      string name,
532 	                      string label=string(),
533 	                      string address=string(),
534 	                      define_map *defines=0);
535 	/**
536 	 * Factory method for constructing a new node.  Attempts to parse a
537 	 * node in DTB format from the input, and returns it on success.  On
538 	 * any parse error, this will return 0.  This should be called with the
539 	 * cursor on the open brace of the property, after the name and so on
540 	 * have been parsed.
541 	 */
542 	static node_ptr parse_dtb(input_buffer &structs, input_buffer &strings);
543 	/**
544 	 * Returns a property corresponding to the specified key, or 0 if this
545 	 * node does not contain a property of that name.
546 	 */
547 	property_ptr get_property(string key);
548 	/**
549 	 * Adds a new property to this node.
550 	 */
551 	inline void add_property(property_ptr &p)
552 	{
553 		props.push_back(p);
554 	}
555 	/**
556 	 * Merges a node into this one.  Any properties present in both are
557 	 * overridden, any properties present in only one are preserved.
558 	 */
559 	void merge_node(node_ptr other);
560 	/**
561 	 * Write this node to the specified output.  Although nodes do not
562 	 * refer to a string table directly, their properties do.  The string
563 	 * table passed as the second argument is used for the names of
564 	 * properties within this node and its children.
565 	 */
566 	void write(dtb::output_writer &writer, dtb::string_table &strings);
567 	/**
568 	 * Writes the current node as DTS to the specified file.  The second
569 	 * parameter is the indent level.  This function will start every line
570 	 * with this number of tabs.
571 	 */
572 	void write_dts(FILE *file, int indent);
573 	/**
574 	 * Recursively visit this node and then its children.
575 	 */
576 	void visit(std::function<void(node&)>);
577 };
578 
579 /**
580  * Class encapsulating the entire parsed FDT.  This is the top-level class,
581  * which parses the entire DTS representation and write out the finished
582  * version.
583  */
584 class device_tree
585 {
586 	public:
587 	/**
588 	 * Type used for node paths.  A node path is sequence of names and unit
589 	 * addresses.
590 	 */
591 	typedef std::vector<std::pair<string,string> > node_path;
592 	/**
593 	 * Name that we should use for phandle nodes.
594 	 */
595 	enum phandle_format
596 	{
597 		/** linux,phandle */
598 		LINUX,
599 		/** phandle */
600 		EPAPR,
601 		/** Create both nodes. */
602 		BOTH
603 	};
604 	private:
605 	/**
606 	 * The format that we should use for writing phandles.
607 	 */
608 	phandle_format phandle_node_name;
609 	/**
610 	 * Flag indicating that this tree is valid.  This will be set to false
611 	 * on parse errors.
612 	 */
613 	bool valid;
614 	/**
615 	 * Type used for memory reservations.  A reservation is two 64-bit
616 	 * values indicating a base address and length in memory that the
617 	 * kernel should not use.  The high 32 bits are ignored on 32-bit
618 	 * platforms.
619 	 */
620 	typedef std::pair<uint64_t, uint64_t> reservation;
621 	/**
622 	 * The memory reserves table.
623 	 */
624 	std::vector<reservation> reservations;
625 	/**
626 	 * Root node.  All other nodes are children of this node.
627 	 */
628 	node_ptr root;
629 	/**
630 	 * Mapping from names to nodes.  Only unambiguous names are recorded,
631 	 * duplicate names are stored as (node*)-1.
632 	 */
633 	std::unordered_map<string, node*> node_names;
634 	/**
635 	 * A map from labels to node paths.  When resolving cross references,
636 	 * we look up referenced nodes in this and replace the cross reference
637 	 * with the full path to its target.
638 	 */
639 	std::unordered_map<string, node_path> node_paths;
640 	/**
641 	 * A collection of property values that are references to other nodes.
642 	 * These should be expanded to the full path of their targets.
643 	 */
644 	std::vector<property_value*> cross_references;
645 	/**
646 	 * A collection of property values that refer to phandles.  These will
647 	 * be replaced by the value of the phandle property in their
648 	 * destination.
649 	 */
650 	std::vector<property_value*> phandles;
651 	/**
652 	 * The names of nodes that target phandles.
653 	 */
654 	std::unordered_set<string> phandle_targets;
655 	/**
656 	 * A collection of input buffers that we are using.  These input
657 	 * buffers are the ones that own their memory, and so we must preserve
658 	 * them for the lifetime of the device tree.
659 	 */
660 	std::vector<std::unique_ptr<input_buffer>> buffers;
661 	/**
662 	 * A map of used phandle values to nodes.  All phandles must be unique,
663 	 * so we keep a set of ones that the user explicitly provides in the
664 	 * input to ensure that we don't reuse them.
665 	 *
666 	 * This is a map, rather than a set, because we also want to be able to
667 	 * find phandles that were provided by the user explicitly when we are
668 	 * doing checking.
669 	 */
670 	std::unordered_map<uint32_t, node*> used_phandles;
671 	/**
672 	 * Paths to search for include files.  This contains a set of
673 	 * nul-terminated strings, which are not owned by this class and so
674 	 * must be freed separately.
675 	 */
676 	std::vector<std::string> include_paths;
677 	/**
678 	 * Dictionary of predefined macros provided on the command line.
679 	 */
680 	define_map               defines;
681 	/**
682 	 * The default boot CPU, specified in the device tree header.
683 	 */
684 	uint32_t boot_cpu;
685 	/**
686 	 * The number of empty reserve map entries to generate in the blob.
687 	 */
688 	uint32_t spare_reserve_map_entries;
689 	/**
690 	 * The minimum size in bytes of the blob.
691 	 */
692 	uint32_t minimum_blob_size;
693 	/**
694 	 * The number of bytes of padding to add to the end of the blob.
695 	 */
696 	uint32_t blob_padding;
697 	/**
698 	 * Visit all of the nodes recursively, and if they have labels then add
699 	 * them to the node_paths and node_names vectors so that they can be
700 	 * used in resolving cross references.  Also collects phandle
701 	 * properties that have been explicitly added.
702 	 */
703 	void collect_names_recursive(node_ptr &n, node_path &path);
704 	/**
705 	 * Assign phandle properties to all nodes that have been referenced and
706 	 * require one.  This method will recursively visit the tree starting at
707 	 * the node that it is passed.
708 	 */
709 	void assign_phandles(node_ptr &n, uint32_t &next);
710 	/**
711 	 * Calls the recursive version of this method on every root node.
712 	 */
713 	void collect_names();
714 	/**
715 	 * Resolves all cross references.  Any properties that refer to another
716 	 * node must have their values replaced by either the node path or
717 	 * phandle value.
718 	 */
719 	void resolve_cross_references();
720 	/**
721 	 * Parse a top-level include directive.
722 	 */
723 	bool parse_include(input_buffer &input,
724 	                   const std::string &dir,
725 	                   std::vector<node_ptr> &roots,
726 	                   FILE *depfile,
727 	                   bool &read_header);
728 	/**
729 	 * Parses a dts file in the given buffer and adds the roots to the parsed
730 	 * set.  The `read_header` argument indicates whether the header has
731 	 * already been read.  Some dts files place the header in an include,
732 	 * rather than in the top-level file.
733 	 */
734 	void parse_file(input_buffer &input,
735 	                const std::string &dir,
736 	                std::vector<node_ptr> &roots,
737 	                FILE *depfile,
738 	                bool &read_header);
739 	/**
740 	 * Allocates a new mmap()'d input buffer for use in parsing.  This
741 	 * object then keeps a reference to it, ensuring that it is not
742 	 * deallocated until the device tree is destroyed.
743 	 */
744 	input_buffer *buffer_for_file(const char *path, bool warn=true);
745 	/**
746 	 * Template function that writes a dtb blob using the specified writer.
747 	 * The writer defines the output format (assembly, blob).
748 	 */
749 	template<class writer>
750 	void write(int fd);
751 	public:
752 	/**
753 	 * Returns the node referenced by the property.  If this is a tree that
754 	 * is in source form, then we have a string that we can use to index
755 	 * the cross_references array and so we can just look that up.
756 	 */
757 	node *referenced_node(property_value &v);
758 	/**
759 	 * Writes this FDT as a DTB to the specified output.
760 	 */
761 	void write_binary(int fd);
762 	/**
763 	 * Writes this FDT as an assembly representation of the DTB to the
764 	 * specified output.  The result can then be assembled and linked into
765 	 * a program.
766 	 */
767 	void write_asm(int fd);
768 	/**
769 	 * Writes the tree in DTS (source) format.
770 	 */
771 	void write_dts(int fd);
772 	/**
773 	 * Default constructor.  Creates a valid, but empty FDT.
774 	 */
775 	device_tree() : phandle_node_name(EPAPR), valid(true),
776 		boot_cpu(0), spare_reserve_map_entries(0),
777 		minimum_blob_size(0), blob_padding(0) {}
778 	/**
779 	 * Constructs a device tree from the specified file name, referring to
780 	 * a file that contains a device tree blob.
781 	 */
782 	void parse_dtb(const char *fn, FILE *depfile);
783 	/**
784 	 * Constructs a device tree from the specified file name, referring to
785 	 * a file that contains device tree source.
786 	 */
787 	void parse_dts(const char *fn, FILE *depfile);
788 	/**
789 	 * Returns whether this tree is valid.
790 	 */
791 	inline bool is_valid()
792 	{
793 		return valid;
794 	}
795 	/**
796 	 * Sets the format for writing phandle properties.
797 	 */
798 	inline void set_phandle_format(phandle_format f)
799 	{
800 		phandle_node_name = f;
801 	}
802 	/**
803 	 * Returns a pointer to the root node of this tree.  No ownership
804 	 * transfer.
805 	 */
806 	inline const node_ptr &get_root() const
807 	{
808 		return root;
809 	}
810 	/**
811 	 * Sets the physical boot CPU.
812 	 */
813 	void set_boot_cpu(uint32_t cpu)
814 	{
815 		boot_cpu = cpu;
816 	}
817 	/**
818 	 * Sorts the tree.  Useful for debugging device trees.
819 	 */
820 	void sort()
821 	{
822 		root->sort();
823 	}
824 	/**
825 	 * Adds a path to search for include files.  The argument must be a
826 	 * nul-terminated string representing the path.  The device tree keeps
827 	 * a pointer to this string, but does not own it: the caller is
828 	 * responsible for freeing it if required.
829 	 */
830 	void add_include_path(const char *path)
831 	{
832 		std::string p(path);
833 		include_paths.push_back(std::move(p));
834 	}
835 	/**
836 	 * Sets the number of empty reserve map entries to add.
837 	 */
838 	void set_empty_reserve_map_entries(uint32_t e)
839 	{
840 		spare_reserve_map_entries = e;
841 	}
842 	/**
843 	 * Sets the minimum size, in bytes, of the blob.
844 	 */
845 	void set_blob_minimum_size(uint32_t s)
846 	{
847 		minimum_blob_size = s;
848 	}
849 	/**
850 	 * Sets the amount of padding to add to the blob.
851 	 */
852 	void set_blob_padding(uint32_t p)
853 	{
854 		blob_padding = p;
855 	}
856 	/**
857 	 * Parses a predefined macro value.
858 	 */
859 	bool parse_define(const char *def);
860 };
861 
862 } // namespace fdt
863 
864 } // namespace dtc
865 
866 #endif // !_FDT_HH_
867