1Device Tree Compiler Manual 2=========================== 3 4I - "dtc", the device tree compiler 5 1) Obtaining Sources 6 1.1) Submitting Patches 7 2) Description 8 3) Command Line 9 4) Source File 10 4.1) Overview 11 4.2) Properties 12 4.3) Labels and References 13 14II - The DT block format 15 1) Header 16 2) Device tree generalities 17 3) Device tree "structure" block 18 4) Device tree "strings" block 19 20 21III - libfdt 22 23IV - Utility Tools 24 1) convert-dtsv0 -- Conversion to Version 1 25 1) fdtdump 26 27 28I - "dtc", the device tree compiler 29=================================== 30 311) Sources 32 33Source code for the Device Tree Compiler can be found at git.kernel.org. 34 35The upstream repository is here: 36 37 git://git.kernel.org/pub/scm/utils/dtc/dtc.git 38 https://git.kernel.org/pub/scm/utils/dtc/dtc.git 39 40The gitweb interface for the upstream respository is: 41 42 https://git.kernel.org/cgit/utils/dtc/dtc.git/ 43 441.1) Submitting Patches 45 46Patches should be sent to the maintainers: 47 David Gibson <david@gibson.dropbear.id.au> 48 Jon Loeliger <jdl@jdl.com> 49and CCed to <devicetree-compiler@vger.kernel.org>. 50 512) Description 52 53The Device Tree Compiler, dtc, takes as input a device-tree in 54a given format and outputs a device-tree in another format. 55Typically, the input format is "dts", a human readable source 56format, and creates a "dtb", or binary format as output. 57 58The currently supported Input Formats are: 59 60 - "dtb": "blob" format. A flattened device-tree block with 61 header in one binary blob. 62 63 - "dts": "source" format. A text file containing a "source" 64 for a device-tree. 65 66 - "fs" format. A representation equivalent to the output of 67 /proc/device-tree where nodes are directories and 68 properties are files. 69 70The currently supported Output Formats are: 71 72 - "dtb": "blob" format 73 74 - "dts": "source" format 75 76 - "asm": assembly language file. A file that can be sourced 77 by gas to generate a device-tree "blob". That file can 78 then simply be added to your Makefile. Additionally, the 79 assembly file exports some symbols that can be used. 80 81 823) Command Line 83 84The syntax of the dtc command line is: 85 86 dtc [options] [<input_filename>] 87 88Options: 89 90 <input_filename> 91 The name of the input source file. If no <input_filename> 92 or "-" is given, stdin is used. 93 94 -b <number> 95 Set the physical boot cpu. 96 97 -f 98 Force. Try to produce output even if the input tree has errors. 99 100 -h 101 Emit a brief usage and help message. 102 103 -I <input_format> 104 The source input format, as listed above. 105 106 -o <output_filename> 107 The name of the generated output file. Use "-" for stdout. 108 109 -O <output_format> 110 The generated output format, as listed above. 111 112 -d <dependency_filename> 113 Generate a dependency file during compilation. 114 115 -q 116 Quiet: -q suppress warnings, -qq errors, -qqq all 117 118 -R <number> 119 Make space for <number> reserve map entries 120 Relevant for dtb and asm output only. 121 122 -@ 123 Generates a __symbols__ node at the root node of the resulting blob 124 for any node labels used, and for any local references using phandles 125 it also generates a __local_fixups__ node that tracks them. 126 127 When using the /plugin/ tag all unresolved label references to 128 be tracked in the __fixups__ node, making dynamic resolution possible. 129 130 -A 131 Generate automatically aliases for all node labels. This is similar to 132 the -@ option (the __symbols__ node contain identical information) but 133 the semantics are slightly different since no phandles are automatically 134 generated for labeled nodes. 135 136 -S <bytes> 137 Ensure the blob at least <bytes> long, adding additional 138 space if needed. 139 140 -v 141 Print DTC version and exit. 142 143 -V <output_version> 144 Generate output conforming to the given <output_version>. 145 By default the most recent version is generated. 146 Relevant for dtb and asm output only. 147 148 149The <output_version> defines what version of the "blob" format will be 150generated. Supported versions are 1, 2, 3, 16 and 17. The default is 151always the most recent version and is likely the highest number. 152 153Additionally, dtc performs various sanity checks on the tree. 154 155 1564) Device Tree Source file 157 1584.1) Overview 159 160Here is a very rough overview of the layout of a DTS source file: 161 162 163 sourcefile: versioninfo plugindecl list_of_memreserve devicetree 164 165 memreserve: label 'memreserve' ADDR ADDR ';' 166 | label 'memreserve' ADDR '-' ADDR ';' 167 168 devicetree: '/' nodedef 169 170 versioninfo: '/' 'dts-v1' '/' ';' 171 172 plugindecl: '/' 'plugin' '/' ';' 173 | /* empty */ 174 175 nodedef: '{' list_of_property list_of_subnode '}' ';' 176 177 property: label PROPNAME '=' propdata ';' 178 179 propdata: STRING 180 | '<' list_of_cells '>' 181 | '[' list_of_bytes ']' 182 183 subnode: label nodename nodedef 184 185That structure forms a hierarchical layout of nodes and properties 186rooted at an initial node as: 187 188 / { 189 } 190 191Both classic C style and C++ style comments are supported. 192 193Source files may be directly included using the syntax: 194 195 /include/ "filename" 196 197 1984.2) Properties 199 200Properties are named, possibly labeled, values. Each value 201is one of: 202 203 - A null-teminated C-like string, 204 - A numeric value fitting in 32 bits, 205 - A list of 32-bit values 206 - A byte sequence 207 208Here are some example property definitions: 209 210 - A property containing a 0 terminated string 211 212 property1 = "string_value"; 213 214 - A property containing a numerical 32-bit hexadecimal value 215 216 property2 = <1234abcd>; 217 218 - A property containing 3 numerical 32-bit hexadecimal values 219 220 property3 = <12345678 12345678 deadbeef>; 221 222 - A property whose content is an arbitrary array of bytes 223 224 property4 = [0a 0b 0c 0d de ea ad be ef]; 225 226 227Node may contain sub-nodes to obtain a hierarchical structure. 228For example: 229 230 - A child node named "childnode" whose unit name is 231 "childnode at address". It in turn has a string property 232 called "childprop". 233 234 childnode@addresss { 235 childprop = "hello\n"; 236 }; 237 238 239By default, all numeric values are hexadecimal. Alternate bases 240may be specified using a prefix "d#" for decimal, "b#" for binary, 241and "o#" for octal. 242 243Strings support common escape sequences from C: "\n", "\t", "\r", 244"\(octal value)", "\x(hex value)". 245 246 2474.3) Labels and References 248 249Labels may be applied to nodes or properties. Labels appear 250before a node name, and are referenced using an ampersand: &label. 251Absolute node path names are also allowed in node references. 252 253In this exmaple, a node is labled "mpic" and then referenced: 254 255 mpic: interrupt-controller@40000 { 256 ... 257 }; 258 259 ethernet-phy@3 { 260 interrupt-parent = <&mpic>; 261 ... 262 }; 263 264And used in properties, lables may appear before or after any value: 265 266 randomnode { 267 prop: string = data: "mystring\n" data_end: ; 268 ... 269 }; 270 271 272 273II - The DT block format 274======================== 275 276This chapter defines the format of the flattened device-tree 277passed to the kernel. The actual content of the device tree 278are described in the kernel documentation in the file 279 280 linux-2.6/Documentation/powerpc/booting-without-of.txt 281 282You can find example of code manipulating that format within 283the kernel. For example, the file: 284 285 including arch/powerpc/kernel/prom_init.c 286 287will generate a flattened device-tree from the Open Firmware 288representation. Other utilities such as fs2dt, which is part of 289the kexec tools, will generate one from a filesystem representation. 290Some bootloaders such as U-Boot provide a bit more support by 291using the libfdt code. 292 293For booting the kernel, the device tree block has to be in main memory. 294It has to be accessible in both real mode and virtual mode with no 295mapping other than main memory. If you are writing a simple flash 296bootloader, it should copy the block to RAM before passing it to 297the kernel. 298 299 3001) Header 301--------- 302 303The kernel is entered with r3 pointing to an area of memory that is 304roughly described in include/asm-powerpc/prom.h by the structure 305boot_param_header: 306 307 struct boot_param_header { 308 u32 magic; /* magic word OF_DT_HEADER */ 309 u32 totalsize; /* total size of DT block */ 310 u32 off_dt_struct; /* offset to structure */ 311 u32 off_dt_strings; /* offset to strings */ 312 u32 off_mem_rsvmap; /* offset to memory reserve map */ 313 u32 version; /* format version */ 314 u32 last_comp_version; /* last compatible version */ 315 316 /* version 2 fields below */ 317 u32 boot_cpuid_phys; /* Which physical CPU id we're 318 booting on */ 319 /* version 3 fields below */ 320 u32 size_dt_strings; /* size of the strings block */ 321 322 /* version 17 fields below */ 323 u32 size_dt_struct; /* size of the DT structure block */ 324 }; 325 326Along with the constants: 327 328 /* Definitions used by the flattened device tree */ 329 #define OF_DT_HEADER 0xd00dfeed /* 4: version, 330 4: total size */ 331 #define OF_DT_BEGIN_NODE 0x1 /* Start node: full name 332 */ 333 #define OF_DT_END_NODE 0x2 /* End node */ 334 #define OF_DT_PROP 0x3 /* Property: name off, 335 size, content */ 336 #define OF_DT_END 0x9 337 338All values in this header are in big endian format, the various 339fields in this header are defined more precisely below. All "offset" 340values are in bytes from the start of the header; that is from the 341value of r3. 342 343 - magic 344 345 This is a magic value that "marks" the beginning of the 346 device-tree block header. It contains the value 0xd00dfeed and is 347 defined by the constant OF_DT_HEADER 348 349 - totalsize 350 351 This is the total size of the DT block including the header. The 352 "DT" block should enclose all data structures defined in this 353 chapter (who are pointed to by offsets in this header). That is, 354 the device-tree structure, strings, and the memory reserve map. 355 356 - off_dt_struct 357 358 This is an offset from the beginning of the header to the start 359 of the "structure" part the device tree. (see 2) device tree) 360 361 - off_dt_strings 362 363 This is an offset from the beginning of the header to the start 364 of the "strings" part of the device-tree 365 366 - off_mem_rsvmap 367 368 This is an offset from the beginning of the header to the start 369 of the reserved memory map. This map is a list of pairs of 64- 370 bit integers. Each pair is a physical address and a size. The 371 list is terminated by an entry of size 0. This map provides the 372 kernel with a list of physical memory areas that are "reserved" 373 and thus not to be used for memory allocations, especially during 374 early initialization. The kernel needs to allocate memory during 375 boot for things like un-flattening the device-tree, allocating an 376 MMU hash table, etc... Those allocations must be done in such a 377 way to avoid overriding critical things like, on Open Firmware 378 capable machines, the RTAS instance, or on some pSeries, the TCE 379 tables used for the iommu. Typically, the reserve map should 380 contain _at least_ this DT block itself (header,total_size). If 381 you are passing an initrd to the kernel, you should reserve it as 382 well. You do not need to reserve the kernel image itself. The map 383 should be 64-bit aligned. 384 385 - version 386 387 This is the version of this structure. Version 1 stops 388 here. Version 2 adds an additional field boot_cpuid_phys. 389 Version 3 adds the size of the strings block, allowing the kernel 390 to reallocate it easily at boot and free up the unused flattened 391 structure after expansion. Version 16 introduces a new more 392 "compact" format for the tree itself that is however not backward 393 compatible. Version 17 adds an additional field, size_dt_struct, 394 allowing it to be reallocated or moved more easily (this is 395 particularly useful for bootloaders which need to make 396 adjustments to a device tree based on probed information). You 397 should always generate a structure of the highest version defined 398 at the time of your implementation. Currently that is version 17, 399 unless you explicitly aim at being backward compatible. 400 401 - last_comp_version 402 403 Last compatible version. This indicates down to what version of 404 the DT block you are backward compatible. For example, version 2 405 is backward compatible with version 1 (that is, a kernel build 406 for version 1 will be able to boot with a version 2 format). You 407 should put a 1 in this field if you generate a device tree of 408 version 1 to 3, or 16 if you generate a tree of version 16 or 17 409 using the new unit name format. 410 411 - boot_cpuid_phys 412 413 This field only exist on version 2 headers. It indicate which 414 physical CPU ID is calling the kernel entry point. This is used, 415 among others, by kexec. If you are on an SMP system, this value 416 should match the content of the "reg" property of the CPU node in 417 the device-tree corresponding to the CPU calling the kernel entry 418 point (see further chapters for more informations on the required 419 device-tree contents) 420 421 - size_dt_strings 422 423 This field only exists on version 3 and later headers. It 424 gives the size of the "strings" section of the device tree (which 425 starts at the offset given by off_dt_strings). 426 427 - size_dt_struct 428 429 This field only exists on version 17 and later headers. It gives 430 the size of the "structure" section of the device tree (which 431 starts at the offset given by off_dt_struct). 432 433So the typical layout of a DT block (though the various parts don't 434need to be in that order) looks like this (addresses go from top to 435bottom): 436 437 ------------------------------ 438 r3 -> | struct boot_param_header | 439 ------------------------------ 440 | (alignment gap) (*) | 441 ------------------------------ 442 | memory reserve map | 443 ------------------------------ 444 | (alignment gap) | 445 ------------------------------ 446 | | 447 | device-tree structure | 448 | | 449 ------------------------------ 450 | (alignment gap) | 451 ------------------------------ 452 | | 453 | device-tree strings | 454 | | 455 -----> ------------------------------ 456 | 457 | 458 --- (r3 + totalsize) 459 460 (*) The alignment gaps are not necessarily present; their presence 461 and size are dependent on the various alignment requirements of 462 the individual data blocks. 463 464 4652) Device tree generalities 466--------------------------- 467 468This device-tree itself is separated in two different blocks, a 469structure block and a strings block. Both need to be aligned to a 4 470byte boundary. 471 472First, let's quickly describe the device-tree concept before detailing 473the storage format. This chapter does _not_ describe the detail of the 474required types of nodes & properties for the kernel, this is done 475later in chapter III. 476 477The device-tree layout is strongly inherited from the definition of 478the Open Firmware IEEE 1275 device-tree. It's basically a tree of 479nodes, each node having two or more named properties. A property can 480have a value or not. 481 482It is a tree, so each node has one and only one parent except for the 483root node who has no parent. 484 485A node has 2 names. The actual node name is generally contained in a 486property of type "name" in the node property list whose value is a 487zero terminated string and is mandatory for version 1 to 3 of the 488format definition (as it is in Open Firmware). Version 16 makes it 489optional as it can generate it from the unit name defined below. 490 491There is also a "unit name" that is used to differentiate nodes with 492the same name at the same level, it is usually made of the node 493names, the "@" sign, and a "unit address", which definition is 494specific to the bus type the node sits on. 495 496The unit name doesn't exist as a property per-se but is included in 497the device-tree structure. It is typically used to represent "path" in 498the device-tree. More details about the actual format of these will be 499below. 500 501The kernel powerpc generic code does not make any formal use of the 502unit address (though some board support code may do) so the only real 503requirement here for the unit address is to ensure uniqueness of 504the node unit name at a given level of the tree. Nodes with no notion 505of address and no possible sibling of the same name (like /memory or 506/cpus) may omit the unit address in the context of this specification, 507or use the "@0" default unit address. The unit name is used to define 508a node "full path", which is the concatenation of all parent node 509unit names separated with "/". 510 511The root node doesn't have a defined name, and isn't required to have 512a name property either if you are using version 3 or earlier of the 513format. It also has no unit address (no @ symbol followed by a unit 514address). The root node unit name is thus an empty string. The full 515path to the root node is "/". 516 517Every node which actually represents an actual device (that is, a node 518which isn't only a virtual "container" for more nodes, like "/cpus" 519is) is also required to have a "device_type" property indicating the 520type of node . 521 522Finally, every node that can be referenced from a property in another 523node is required to have a "linux,phandle" property. Real open 524firmware implementations provide a unique "phandle" value for every 525node that the "prom_init()" trampoline code turns into 526"linux,phandle" properties. However, this is made optional if the 527flattened device tree is used directly. An example of a node 528referencing another node via "phandle" is when laying out the 529interrupt tree which will be described in a further version of this 530document. 531 532This "linux, phandle" property is a 32-bit value that uniquely 533identifies a node. You are free to use whatever values or system of 534values, internal pointers, or whatever to generate these, the only 535requirement is that every node for which you provide that property has 536a unique value for it. 537 538Here is an example of a simple device-tree. In this example, an "o" 539designates a node followed by the node unit name. Properties are 540presented with their name followed by their content. "content" 541represents an ASCII string (zero terminated) value, while <content> 542represents a 32-bit hexadecimal value. The various nodes in this 543example will be discussed in a later chapter. At this point, it is 544only meant to give you a idea of what a device-tree looks like. I have 545purposefully kept the "name" and "linux,phandle" properties which 546aren't necessary in order to give you a better idea of what the tree 547looks like in practice. 548 549 / o device-tree 550 |- name = "device-tree" 551 |- model = "MyBoardName" 552 |- compatible = "MyBoardFamilyName" 553 |- #address-cells = <2> 554 |- #size-cells = <2> 555 |- linux,phandle = <0> 556 | 557 o cpus 558 | | - name = "cpus" 559 | | - linux,phandle = <1> 560 | | - #address-cells = <1> 561 | | - #size-cells = <0> 562 | | 563 | o PowerPC,970@0 564 | |- name = "PowerPC,970" 565 | |- device_type = "cpu" 566 | |- reg = <0> 567 | |- clock-frequency = <5f5e1000> 568 | |- 64-bit 569 | |- linux,phandle = <2> 570 | 571 o memory@0 572 | |- name = "memory" 573 | |- device_type = "memory" 574 | |- reg = <00000000 00000000 00000000 20000000> 575 | |- linux,phandle = <3> 576 | 577 o chosen 578 |- name = "chosen" 579 |- bootargs = "root=/dev/sda2" 580 |- linux,phandle = <4> 581 582This tree is almost a minimal tree. It pretty much contains the 583minimal set of required nodes and properties to boot a linux kernel; 584that is, some basic model informations at the root, the CPUs, and the 585physical memory layout. It also includes misc information passed 586through /chosen, like in this example, the platform type (mandatory) 587and the kernel command line arguments (optional). 588 589The /cpus/PowerPC,970@0/64-bit property is an example of a 590property without a value. All other properties have a value. The 591significance of the #address-cells and #size-cells properties will be 592explained in chapter IV which defines precisely the required nodes and 593properties and their content. 594 595 5963) Device tree "structure" block 597 598The structure of the device tree is a linearized tree structure. The 599"OF_DT_BEGIN_NODE" token starts a new node, and the "OF_DT_END_NODE" 600ends that node definition. Child nodes are simply defined before 601"OF_DT_END_NODE" (that is nodes within the node). A 'token' is a 32 602bit value. The tree has to be "finished" with a OF_DT_END token 603 604Here's the basic structure of a single node: 605 606 * token OF_DT_BEGIN_NODE (that is 0x00000001) 607 * for version 1 to 3, this is the node full path as a zero 608 terminated string, starting with "/". For version 16 and later, 609 this is the node unit name only (or an empty string for the 610 root node) 611 * [align gap to next 4 bytes boundary] 612 * for each property: 613 * token OF_DT_PROP (that is 0x00000003) 614 * 32-bit value of property value size in bytes (or 0 if no 615 value) 616 * 32-bit value of offset in string block of property name 617 * property value data if any 618 * [align gap to next 4 bytes boundary] 619 * [child nodes if any] 620 * token OF_DT_END_NODE (that is 0x00000002) 621 622So the node content can be summarized as a start token, a full path, 623a list of properties, a list of child nodes, and an end token. Every 624child node is a full node structure itself as defined above. 625 626NOTE: The above definition requires that all property definitions for 627a particular node MUST precede any subnode definitions for that node. 628Although the structure would not be ambiguous if properties and 629subnodes were intermingled, the kernel parser requires that the 630properties come first (up until at least 2.6.22). Any tools 631manipulating a flattened tree must take care to preserve this 632constraint. 633 6344) Device tree "strings" block 635 636In order to save space, property names, which are generally redundant, 637are stored separately in the "strings" block. This block is simply the 638whole bunch of zero terminated strings for all property names 639concatenated together. The device-tree property definitions in the 640structure block will contain offset values from the beginning of the 641strings block. 642 643 644III - libfdt 645============ 646 647This library should be merged into dtc proper. 648This library should likely be worked into U-Boot and the kernel. 649 650 651IV - Utility Tools 652================== 653 6541) convert-dtsv0 -- Conversion to Version 1 655 656convert-dtsv0 is a small utility program which converts (DTS) 657Device Tree Source from the obsolete version 0 to version 1. 658 659Version 1 DTS files are marked by line "/dts-v1/;" at the top of the file. 660 661The syntax of the convert-dtsv0 command line is: 662 663 convert-dtsv0 [<input_filename ... >] 664 665Each file passed will be converted to the new /dts-v1/ version by creating 666a new file with a "v1" appended the filename. 667 668Comments, empty lines, etc. are preserved. 669 670 6712) fdtdump -- Flat Device Tree dumping utility 672 673The fdtdump program prints a readable version of a flat device tree file. 674 675The syntax of the fdtdump command line is: 676 677 fdtdump <DTB-file-name> 678