foreword
-
Manually compiling the Linux device tree dts source file and its device tree relying on dtsi and .h header files through Makefile, how to write a shell script and compile it directly?
-
In fact, just rewrite the Makefile as a shell script
Compile the device tree shell script
- The script content is as follows: gen_dtbs.sh
#! /bin/bash
DIR_ROOT=$( cd "$(dirname "${
BASH_SOURCE[0]}")" && pwd)
HOST_DTC=${
DIR_ROOT}/dtc
HOSTCC=gcc
# ${
HOSTCC} -v
# ${
HOST_DTC} --version
suffix_dtb=".dtb"
suffix_dts=".dts"
dtb_d_pre_tmp=".dtb.d.pre.tmp"
dtb_dts_tmp=".dtb.dts.tmp"
dtb_d_dtc_tmp=".dtb.d.dtc.tmp"
dtc_cflags="-E -Wp,-MMD,"
dtc_flags="0 -Wno-interrupt_provider -Wno-unit_address_vs_reg -Wno-avoid_unnecessary_addr_size "
dtc_flags+="-Wno-alias_paths -Wno-graph_child_address -Wno-simple_bus_reg -Wno-unique_unit_address"
dtc_def="-undef -D__DTS__ -x assembler-with-cpp"
dts_src_path=${
DIR_ROOT}
dts_file_string=`find ${
dts_src_path} -maxdepth 2 -type f -name *.dts`
for dts in ${
dts_file_string}
do
# echo ${
dts}
echo ${
dts%.*}
${
HOSTCC} -E ${
dtc_cflags}${
dts%.*}${
dtb_d_pre_tmp} -nostdinc -I${
DIR_ROOT}/include ${
dtc_def} -o ${
dts%.*}${
dtb_dts_tmp} ${
dts%.*}${
suffix_dts}
${
HOST_DTC} -o ${
dts%.*}${
suffix_dtb} -b ${
dtc_flags} -d ${
dts%.*}${
dtb_d_dtc_tmp} ${
dts%.*}${
dtb_dts_tmp}
done
dts_folders=`find ${
dts_src_path} -maxdepth 1 -type d`
for dts in ${
dts_folders}
do
`rm -f ${
dts}/*.d.dtc.tmp`
`rm -f ${dts}/*.d.pre.tmp`
`rm -f ${dts}/*.dtb.dts.tmp`
done
echo " --- build dtbs end --- "
- The download address of the sample project
https://gitee.com/zhangsz0516/make_dtbs.git
shell script instructions
-
Operating environment: Linux, need to install gcc, do not need to install make
-
Need to prepare: device tree dts original file, and dependent dtsi and device tree header files
-
The dtc compilation tool, under the source code Linux kernel scripts/dtc directory, can be generated after compiling the Linux kernel. By default, Linux is compiled from the source code every time, and it can be used directly by copying it here. It does not need to compile the source code to generate the dtc tool.
Get the dtc tool
-
The dtc tool is compiled with gcc in the Linux environment, that is, host gcc, no cross-compilation is required, the dtc tool cannot be directly compiled under Linux, and a board can be compiled, such as qemu
vexpress_defconfig
-
Compile method:
$ make ARCH=arm vexpress_defconfig
$ make ARCH=arm dtbs
- At this point, scripts/dtc/dtc will be generated, and you can view the version number of the dtc tool:
$ chmod +x scripts/dtc/dtc
zhangsz@zhangsz:~/linux/kernel/linux-6.0.10$ scripts/dtc/dtc --version
Version: DTC 1.6.1-g0a3a9d34
Get dts source file
- The device tree source file for this example, from the Linux kernel
Compile the device tree
-
script execution permission
chmod +x gen_dtbs.sh
-
Execute
source gen_dtbs.sh
or run./gen_dtbs.sh
directly
- The test found that the device tree binary file is normally generated: dtb
summary
-
You can use make or shell scripts to manually compile device tree source files
-
Note that gcc is used for preprocessing here, and all the header files that the device tree depends on are placed in a temporary file in the form of source code, just like the processing of C language source files, so that the dtb file is generated through the dtc tool