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