Skip to content
systemcoding.dev
Embedded Linux

Compile and Decompile Device Trees With dtc

Use dtc to compile a .dts into a .dtb, turn a .dtb back into readable source and read the warnings it prints. The flags that matter, with exact commands.

6 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the Linux, NXP and Debian logos.

Two situations send everyone to dtc sooner or later. A board does not boot and you need to know what device tree it is really using, which means turning a binary blob back into text. Or you have written a node and want to know if it is valid before you spend ten minutes copying files to an SD card. The device tree compiler does both, in both directions, and it also checks your source for mistakes that the kernel would only report much later. This post shows the exact commands for dtc 1.7, the version most distributions ship with Linux 6.12 tooling, reads the warnings it prints and covers the flags you will use every week.

Install and check the version

dtc ships in the device-tree-compiler package on Debian and Ubuntu and as dtc in many other distributions. The kernel also builds its own copy in scripts/dtc/dtc, which is the one make dtbs uses, so a kernel tree has a working compiler without any installation:

Shell
dtc --version
Text
Version: DTC 1.7.0

Your number may differ. The two programs behave the same for everything in this post.

Compile source to a blob

A device tree source file, .dts, becomes a binary blob, .dtb. Start from a minimal board:

demo.dtsdts
/dts-v1/;

/ {
	model = "Demo Board";
	compatible = "acme,demo";
	#address-cells = <1>;
	#size-cells = <1>;

	chosen {
		bootargs = "console=ttyS0,115200";
	};

	memory@40000000 {
		device_type = "memory";
		reg = <0x40000000 0x10000000>;
	};
};

Compile it with the input and output formats spelled out. -I names the input format, -O the output, and -o the file:

Shell
dtc -I dts -O dtb -o demo.dtb demo.dts

If everything is valid, dtc prints nothing and writes demo.dtb. Silence is success. Check the file with file:

Shell
file demo.dtb
Text
demo.dtb: Device Tree Blob version 17, size=205, boot CPU=0, string block size=105, DT structure block size=68

The size, string block size and DT structure block size values depend on the source, so yours will differ. The version 17 is the format revision dtc writes by default.

The blob begins with the magic number 0xd00dfeed, which you can check with xxd demo.dtb | head -1 and read as d00d feed. That is a fast way to tell if a file in a firmware image is a device tree.

Decompile a blob back to source

Reverse the formats. This works on any blob, including the one a board is using right now, copied out of /sys/firmware/fdt or extracted from a firmware package:

Shell
dtc -I dtb -O dts -o decoded.dts demo.dtb
cat decoded.dts
Text
/dts-v1/;

/ {
	model = "Demo Board";
	compatible = "acme,demo";
	#address-cells = <0x01>;
	#size-cells = <0x01>;

	chosen {
		bootargs = "console=ttyS0,115200";
	};

	memory@40000000 {
		device_type = "memory";
		reg = <0x40000000 0x10000000>;
	};
};

Three differences from the original are normal. Numbers print in hexadecimal with a 0x prefix and a fixed width, comments are gone because they were never in the blob, and the preprocessor #include and #define lines are gone because they ran before dtc. Labels, such as uart0:, also disappear unless the blob was built with -@, in which case a __symbols__ node keeps them.

To decompile the tree of the running system without finding a file, use the fs input format, which reads the directory under /sys/firmware/devicetree/base:

Shell
dtc -I fs -O dts /sys/firmware/devicetree/base > running.dts

That produces the tree after the bootloader edited it, so it includes bootargs and the memory size U-Boot filled in. Compare it with the source tree to see what the bootloader changed.

Read the warnings

dtc runs a set of checks and reports them as warnings, which do not stop the output. Here is a mistake that is easy to make, a child whose reg has the wrong number of cells for its parent:

bad.dtsdts
/dts-v1/;

/ {
	#address-cells = <1>;
	#size-cells = <1>;

	serial@10000000 {
		reg = <0x10000000>;
	};
};
Shell
dtc -I dts -O dtb -o bad.dtb bad.dts
Text
bad.dts:7.19-9.3: Warning (reg_format): /serial@10000000:reg: property has invalid length (4 bytes) (#address-cells == 1, #size-cells == 1)

The message names the file, the line and column range (the exact numbers depend on your layout), the check (reg_format) and the node and property. The check name is useful because you can silence or promote it with -W and -Wno-: the warning says the property is 4 bytes while the parent promises 8, one cell for the address plus one for the size. Add the missing size cell and it goes away.

Other checks you will meet:

CheckWhat it flags
unit_address_vs_regA node's @ address does not match the first reg address, or there is a reg but no unit address
avoid_unnecessary_addr_size#address-cells or #size-cells on a node with no children that need them
unique_unit_addressTwo sibling nodes use the same unit address
simple_bus_regA simple-bus child without a reg, or with the wrong unit address

Run with -W to enable every check, and with -Werror to turn warnings into failures in a build script. Use -Wno-unit_address_vs_reg when a vendor tree cannot be fixed, and leave it on otherwise.

The flags you will use

FlagMeaning
-I, -Oinput and output format: dts, dtb or fs
-o fileoutput file, with - for standard output
-@emit __symbols__ so overlays can refer to labels
-p bytespad the blob with extra space, for a bootloader that adds nodes
-b idset the physical CPU ID of the boot CPU in the header
-W name / -Wno-nameenable or disable a check
-qsuppress warnings (-qq also suppresses errors)
-i diradd an include search directory for /include/

-p solves a practical problem. A blob exactly as large as its content has no room, and fdt commands in U-Boot that add properties fail with FDT_ERR_NOSPACE unless you resize. Building with -p 0x1000 gives every blob 4 KiB of slack from the start.

Compile a kernel board file

Device trees in the Linux tree use the C preprocessor for headers like #include <dt-bindings/gpio/gpio.h>, which plain dtc cannot read. Do not run dtc on them directly. Let the kernel build do both steps:

Shell
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- dtbs
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- vendor/board.dtb

The second form builds one file. The output is arch/arm64/boot/dts/vendor/board.dtb. To change dtc flags for the whole build, pass DTC_FLAGS, for example DTC_FLAGS=-@. The preprocessing step has its own post, because it explains an error that stumps most newcomers.

When this does not apply: if your vendor ships a prebuilt .dtb and no source, decompiling it gives you a readable tree, but it is not the original. Edit the decompiled version only to learn; maintain the real source in your build.

A routine for debugging a board that will not boot

Put the two directions together and you have a reliable first step for any boot problem that smells like a device tree. Copy the blob the bootloader actually loaded, not the one you think it loaded. From U-Boot, fdt addr ${fdt_addr_r} followed by fdt print / dumps the tree in memory. From a running system, copy /sys/firmware/fdt. Decompile it, then diff it against the decompiled version of the blob you built:

Shell
dtc -I dtb -O dts -o built.dts arch/arm64/boot/dts/acme/acme-demo.dtb
dtc -I dtb -O dts -o running.dts /sys/firmware/fdt
diff -u built.dts running.dts

Expect a few differences, all introduced by the bootloader: bootargs in /chosen, a memory node resized to the real RAM, MAC addresses and the linux,initrd-start pair. Any other difference means the board loaded a different file, applied an overlay or booted a stale copy from another partition. In our experience, "I changed the device tree and nothing happened" is a stale copy more often than it is a wrong edit, and the diff settles it in ten seconds.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.
Kernels

Write a Pin Controller Driver With pinctrl

Write a Linux 6.12 pin controller driver: pinctrl_desc, pins, groups and functions, the set_mux callback, and the device tree states that consumers select.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.