Linux Device Tree Compiler (DTC)

It has been a common practice to keep a device tree of a platform scattered across multiple DTS files. Seeing all of these DTS files as a single device tree is a nightmare.

Currently everyone is using DTC compiler to convert DTS file(s) into DTB file and then use it again to convert DTB into DTS file. The problem with this approach is that DTC compiler will convert alias/device name (string) into integer (phandle). This transformation makes final DTS file less readable.

To solve this problem, I have added a new option is added in forked DTC compiler. This new option (-m or –merge) allows to generate DTS very similar to DTB but keeps device
names/aliases in their original format.

There is an another option (-g or –ignore-dead) which will be useful alongwith –merge option. This option allows DTC compiler to ignore dead aliases otherwise it will generate an error on finding dead alias.

Here is the comparison of the DTB converted into DTS using mainline and forked DTC.

dtc1

dtc2

 

 

Handy commands to convert DTB into DTS using mainline and forked DTC.

# To convert dts file into dtb
<kernel-source>/scripts/dtc/dtc -I dts -O dtb <dts_file> -o <dtb_file>
# To convert dtb to dts
<kernel-source>/scripts/dtc/dtc -I dtb -O dts <dtb_file> -o <dts_file>
# Both of above commands are using dtc built with linux kernel source.
# However you can also use dtc binary comes with ubuntu packages. For this you
# need to install following package:
sudo apt-get install -y device-tree-compiler
# Ubuntu device-tree-compiler package comes with another utility known as fdtdump.
# fdtdump can also be used to convert dtb file into dts.
fdtdump <dtb_file> > <dts_dump>

view raw
dts <> dtb
hosted with ❤ by GitHub

# !/bin/bash
#
# This script uses forked dtc to generate flat device tree.
#
# Change these path as per your machine
COMPILER='arm-linux-gnueabihf-gcc'
KERNEL_FOLDER='/your/linux/source/code'
DTS_FILE_NAME='dra7-evm.dts'
FORKED_DTC='/forked/dtc/executable'
# Setup useful env
DTS_FOLDER=$KERNEL_FOLDER/arch/arm/boot/dts
DTS_INCLUDE=$DTS_FOLDER/include
KERNEL_INCLUDE=$KERNEL_FOLDER/include
DTS_FILE=$KERNEL_FOLDER/arch/arm/boot/dts/$DTS_FILE_NAME
echo "Generate flat dts file…(an intermediate file)"
$COMPILER -E -nostdinc -undef -D__DTS__ -x assembler-with-cpp -I$DTS_FOLDER -I$DTS_INCLUDE -o $DTS_FILE_NAME.flat $DTS_FILE
echo "Generate merge dts file…($DTS_FILE_NAME.merge)"
$FORKED_DTC/dtc -i$DTS_FOLDER -i$DTS_INCLUDE -I dts -O dts -m -g $DTS_FILE_NAME.flat -o $DTS_FILE_NAME.merge

view raw
dts <> dtb (forked)
hosted with ❤ by GitHub