Device Tree Basics: .dts, .dtb and How Linux Finds Hardware

Learn what device tree source (.dts) and blob (.dtb) files are, how the kernel uses them, and how to compile and decompile one with dtc on Debian.

Embedded Published 3 min read

Contents

A device tree is a description of a machine’s hardware that the bootloader hands to the Linux kernel, so the kernel does not need that hardware hard-coded. You write it as a text .dts file, compile it into a binary .dtb blob, and inspect or reverse it with dtc. This guide walks through that cycle on Debian or Ubuntu.

What a device tree is

According to the kernel documentation, a devicetree is a tree of named nodes, and each node can have named properties. Shared conventions for how common hardware appears in the tree are called bindings. The Devicetree Specification is maintained at devicetree.org, where v0.4 (June 2023) is listed as the latest release.

Two file types matter in practice:

  • .dts: the human-readable source.
  • .dtb: the compiled binary blob, also called a flattened device tree (FDT), which the bootloader passes to the kernel.

How the kernel uses it

The kernel documentation describes three uses:

  1. Platform identification. On ARM, the kernel matches the root compatible property against what each supported machine declares, and the most specific match wins.
  2. Runtime configuration. Early boot code reads data such as the kernel command line and the initrd location from the tree.
  3. Device population. The blob is unflattened into a runtime form, and devices are created from it. Root-level nodes with a compatible property become platform devices. Nodes for buses such as I2C or SPI have their children registered by the bus driver.

The compatible property is a list of strings ordered from most to least specific. The documentation gives this example: the board first, then the SoC, then the SoC family.

compatible = "ti,omap3-beagleboard-xm", "ti,omap3450", "ti,omap3";

The /chosen node carries data from firmware to the kernel, such as bootargs (the command line) and initrd-start and initrd-end.

Install dtc

dtc is the device tree compiler. On Debian and Ubuntu it ships in the device-tree-compiler package.

✓ tested · debian,ubuntu
apt-get update
apt-get install -y device-tree-compiler

Write a small .dts

This toy tree does not describe real hardware. It only shows the syntax: a root node, a compatible list, a /chosen node and a made-up UART node. The addresses and the example, vendor prefix are placeholders.

✓ tested · debian,ubuntu
mkdir -p /tmp/dt
cat > /tmp/dt/demo.dts <<'DTS'
/dts-v1/;

/ {
	compatible = "example,demo-board";
	#address-cells = <1>;
	#size-cells = <1>;

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

	serial@10000000 {
		compatible = "example,demo-uart";
		reg = <0x10000000 0x100>;
		status = "okay";
	};
};
DTS

Compile to a .dtb

Use -I dts for the input format, -O dtb for the output format and -o for the output file, all documented in the dtc man page.

✓ tested · debian,ubuntu
dtc -I dts -O dtb -o /tmp/dt/demo.dtb /tmp/dt/demo.dts
ls -l /tmp/dt/demo.dtb

Decompile a .dtb

Reverse the formats to turn a blob back into source. This is how you read a .dtb shipped with a board image.

✓ tested · debian,ubuntu
dtc -I dtb -O dts -o /tmp/dt/roundtrip.dts /tmp/dt/demo.dtb
cat /tmp/dt/roundtrip.dts

The output should show the same nodes and properties you wrote, in dts syntax.

Compare two trees

The -s option sorts nodes and properties before output, which the man page says is mainly useful for comparing trees. Sorting both sides first makes diff show real differences only.

✓ tested · debian,ubuntu
dtc -I dts -O dts -s -o /tmp/dt/a.dts /tmp/dt/demo.dts
dtc -I dtb -O dts -s -o /tmp/dt/b.dts /tmp/dt/demo.dtb
diff /tmp/dt/a.dts /tmp/dt/b.dts && echo "trees match"

Where to go next

dtc also accepts -I fs, which reads a /proc/device-tree-style directory. To see a build that boots end to end, read Buildroot: Build a Minimal Linux Image and Boot It in QEMU. For toolchain basics, see Cross-Compile a Hello World for ARM and Run It with QEMU User-Mode.

Sources

  1. Linux and the Devicetree (kernel documentation)
  2. Devicetree Specification releases
  3. dtc(1) man page, Debian trixie

#Device Tree #Dts #Dtb #Dtc #Embedded

This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.