Device Tree (dtb)

The Device Tree Blob (or binary) is a file containing information about the board and its hardware.

Why is it needed?
Most embedded platforms do not provide a way of discovering the hardware they have. You either have to resort to guesswork (which is not viable), or provide the Linux kernel with information about the board and the peripherals.

Before the dtb was introduced, each board needed a kernel specifically compiled for them, which was hardcoded to use the peripherals. With the dtb, a single kernel can run on multiple devices, if it is given the right dtb at boot time.

How is it created?
The dtb is created from device tree source (dts) files. Those contain the hardware description in a readable format. The dtb format merely exists to make loading them simpler for the kernel. As such, dts can be converted to and from dtb with a device tree compiler, dtc, which is available on most linux distributions.

How is it used?
The dtb is loaded in memory by the bootloader (often uboot) before the kernel is started.

Some older versions of uboot do not support loading a separate dtb file, which means that the dtb has to be embedded in the kernel zImage.

QCDT
Some Qualcomm devices also use the QCDT format, where the dtb is appended to the end of the filesystem (boot.img) instead of the zImage.

Where can I find a dts for my device?
Those are generally present in the kernel fork your device uses. Look in the  directory, or in. The compiled device tree can also be extracted from a boot image with  and   (both packaged in postmarketOS), and converted back to a source file with the dtc compiler (though the comments will be lost, as well as the includes). When a board is mainlined, the corresponding dts file is mainlined as well (though those may be put in a separate repository in the future).

Writing a device tree
When mainlining a board, it is often needed to write one's own device tree (see also Mainlining_Guide). Indeed, while the physical description part won't change, the name of the drivers will probably. Thus, you need to find kernel drivers that are compatible with your own device, and put them in the device tree (assuming there is one). You should at least read carefully the description on elinux.org. Another good tutorial to writing your device trees is present on xillybus.com.

Links

 * Device Tree Blob (Flat Device Tree)
 * Linux documentation on the ARM boot process
 * DTC (device tree compiler) manual
 * Linux kernel docs Documentation/devicetree/usage-model.rst
 * Device Tree on ARM
 * YouTube: Device Tree for Dummies! - Thomas Petazzoni, Free Electrons
 * YouTube: Device Tree: Past, Present, and Future