Home » Linux Kernel » Core Kernel » How to compile DTS to DTB from Linux kernel and manually ?

How to compile DTS to DTB from Linux kernel and manually ?

Lets say, you have been trying to compile Linux kernel source code for Raspberry Pi following steps from “Cross Compilation and Booting of Linux kernel for Raspberry Pi3 – Manual Compilation” although most of the steps remains same in general for other ARM architectures as well , so just replace Raspberry Pi name with your own platform if any.

As we know all recent kernels use device tree formats for defining the devices information in Linux kernel which has been written in “device tree syntax” DTS files, generally kept at arch/arm/boot/dts directory and the file which normally gets flashed on device is “device tree blog” DTB which is compiled using “device tree compiler” DTC.

Compile the dtb from Linux kernel source code as,

$ make ARCH=arm CROSS_COMPILE=arm-bcm2708-linux-gnueabi- dtbs 

Install the dtb using following command,

 $ export INSTALL_PATH=$(PWD)/out 

NOTE: you can create out folder in current directory if not present, just to save output binary files.

 $ make ARCH=arm CROSS_COMPILE=arm-bcm2708-linux-gnueabi- dtbs_install 

and you will see, dtbs directory created in out folder.

 $ file out/dtbs/4.9.45-v7+/bcm2708-rpi-b.dtb 

out/dtbs/4.9.45-v7+/bcm2708-rpi-b.dtb: Device Tree Blob version 17, size=15197, boot CPU=0, string block size=1393, DT structure block size=13732

If you check the kernel source code, the make file arch/arm/boot/dts/Makefile defines which target selects which dts file and also mentions about the compilation targets.

You can also create a single dtb using below command,

$ make ARCH=arm CROSS_COMPILE=arm-bcm2708-linux-gnueabi- bcm2708-rpi-b.dtb 

where you can replace bcm2708-rpi-b with any name of dts you need from arch/arm/boot/dts/

While you can create a dtb from dtc command manually using below command,

 $ dtc -O dtb -o filename.dtb -I dts filename.dts 

If you try to create dts already written in Linux kernel, chances are that you will get an error as,

syntax error
FATAL ERROR: Unable to parse input tree

This is because, dts files in kernel use #include whereas standard dts uses include in between two / like “/include/”
so if you replace line like below,
#include “bcm2708.dtsi”

TO

/include/ “bcm2708.dtsi”

The above error may go, which is because the newer kernels run a C preprocessor on dts files like below,

$ cpp -nostdinc -I include -undef -x assembler-with-cpp bcm2708-rpi-b.dts > bcm2708-rpi-b.dts.tmp 

where you will find that bcm2708-rpi-b.dts.tmp has /include/ instead of #include.

But this also might fail to add #include of standard C headers which some dts files use, so best approach is to use “make dtbs” from kernel if you are writing a complicated dts file.

Leave a Comment