通常将设备树源码(dts/dtsi)编译成设备树二进制文件(dtb)可以使用DTC(Device Tree Compiler)工具编译。
- 单文件编译
对于单文件的dts,可以采用下面的命令:
# dtc命令使用方法见文末
dtc -O dtb -b 0 -o [dest_dtb_file] [src_dts_file]将src_dts_file编译成dest_dtb_file设备树二进制文件。
- 多文件编译
对于有#include包含关系、宏定义的dts文件,直接采用以上的方法将会出现<u>#include相关的语法错误</u>。
DTC本身不支持#include语法,其正确语法为/include/。
如将以下dts(没有宏定义)
#include "child_file.dtsi"
#include "child_file_common.dtsi"改为
/include/ "child_file.dtsi"
/include/ "child_file_common.dtsi"即可通过编译。
对于以下稍微复杂一点(包含#include,宏,*.h等)的设备树,以上的方法不免有些笨拙。

由于“#include”“宏”等都是C的特征,因此可以使用CPP(C Preprocessor)命令对dts源文件进行处理,完成文件包含与宏置换的工作。
# cpp的使用方法较长就不列出来了,可以自己man一下。
cpp -nostdinc -I. -undef -x assembler-with-cpp [src_dts_file] > [tmp_dts_file]
# -nostdinc 不搜索标准目录
# -I. 搜索当前目录
# -undef 不预定义系统和gcc特定的宏
# -x assembler-with-cpp 指定语言c c++ objective-c assembler-with-cpp使用以上命令将所有的*.dts、*.dtsi、*.h转换至临时*.dts中,然后再使用单文件编译的方法编译临时*.dts,生成最终的dtb。
将上面的操作写成脚本(dts2dtb.sh)如下:
#/bin/bash
#set -vx
device="your_device_name"
src_dts=$device.dts
tmp_dts=$device.tmp.dts
dst_dtb=$device.dtb
cpp -nostdinc -I. -undef -x assembler-with-cpp $src_dts > $tmp_dts
dtc -O dtb -b 0 -o $dst_dtb $tmp_dts
rm $tmp_dts使用:
修改your_device_name为你要编译的设备树名称,拷贝所有相关的设备树源文件至脚本所在目录,运行。
dtc使用方法:
NAME
       dtc - Device Tree Compiler
SYNOPSIS
       /usr/bin/dtc [options] <input file>
DESCRIPTION
       Device  Tree  Compiler,  dtc,  takes  as input a device-tree in a given format and outputs a
       device-tree in another format for booting kernels on embedded systems.  Typically, the input
       format  is  "dts",  a human readable source format, and creates a "dtb", or binary format as
       output.
OPTIONS
       -h     Display help text.
       -q     Quiet:
              -q - Suppress warnings.
              -qq - Suppress errors.
              -qqq - Suppress all.
       -I <input format>
              Input formats are:
              dts - device tree source text
              dtb - device tree blob
              fs - /proc/device-tree style directory
       -o <output file>
              Dump the result into a file, instead of stdout.
       -O <output format>
              Output formats are:
              dts - device tree source text
              dtb - device tree blob
              asm - assembler source
       -V <output version>
              Blob version to produce. The default is 17 (only relevant for dtb and asm output).
       -d <output dependency file>
       -R <number>
              Make space for <number> reserve map entries (only relevant for dtb and asm output).
       -S <bytes>
              Make the blob at least <bytes> long (extra space).
       -p <bytes>
              Add padding to the blob of <bytes> long (extra space)
       -b <number>
              Set the physical boot CPU.
       -f
              Force - try to produce output even if the input tree has errors.
       -s
              Sort nodes and properties before outputting (only useful for comparing trees)
       -v     Print DTC version and exit.
       -H <phandle format>
              phandle formats are:
              legacy - "linux,phandle" properties only
              epapr - "phandle" properties only
              both - Both "linux,phandle" and "phandle" properties 
                    
写的很棒,感谢!