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,

SHUFFLED :   Allocate an interrupt line using request_irq and Implementation of IRQ / Interrupt Handler in Linux kernel device driver

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.

Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded
Tags:

Leave a Reply