How to modify kernel DTB file
Asked Answered
B

3

12

Summary

I am currently compiling the Linux kernel (kernel, modules and DTB) with some custom drivers for a custom board. Occasionally I'll compile the kernel and realize that the compatibility string in the DTB file is not what the custom driver is looking for. Right now the only way i can remedy this is modify the DTS or kernel driver so the strings match and then recompile the kernel again. Is there are way I can just edit the DTB file to update the compatibility string?

Failed Attempts

I have been able to decompile the DTB file back to a DTS file using the command:

dtc -I dtb -o <filename>.dts -<filename>.dtb

However if I modify the DTS file and recompile using the command:

dtc -I dts -o <filename>.dtb -<filename>.dts

The kernel will not load the recompiled DTB file

Burch answered 28/9, 2017 at 13:4 Comment(2)
Can you show any errors or kernel logs. ?Assize
You should use -O flag to compile, -I is to get the device tree in text from the device tree blob.Therewithal
B
11

Just want to update this with 2 years more experience on the subject.

The DTS files in the Linux repository are a mixture of DTS and C preprocessor directives (#include, #define, etc.). So when the original DTB is compiled, the preprocessor links to the referenced files to create a pure DTS file. dtc converts the single DTS file into a DTB file.

So if you want to modify a kernel DTS file and compile it, then you have two options:

  1. Just run make dtbs which automatically handles all of this
  2. Manually run the preprocessor (cpp -nostdinc -I <include dir> -undef -x assembler-with-cpp ...) and then compile the output with dtc.
Burch answered 24/2, 2020 at 13:28 Comment(0)
T
8

Why don't you generate new dtb?

DTB(Device tree blob/binary), is hardware database which represents the hardware components of the board.

U-boot pass the board information struct to the kernel, that is derived from the header file in U-Boot.

DTB is compiled by the special compiler that produces the binary in the proper form for U-Boot and Linux to understand.


DTC (Device Tree Compiler) it translates device tree file to the machine-readable binary that U-Boot and Linux kernel can understand.

The straightforward way to use DTC.

$ dtc -O dtb -o arm_board.dtb -b 0 arm_board.dts

to get the device tree in text from the dtb.

dtc -I dtb -O dts arm_board.dtb

board.dts is binary created by the above command. -O specifies the output format. -o flag is output file. -b 0 specifies physical boot CPU.

Then do

$ make ARCH=arm arm_board.dtb

Another approach might be just use make dtbs this will call dtc. arch/arm/boot/dts/Makefile lists which DTBs should be generated at build time This another way to compile it. make will put that in this location of kernel tree /arch/arm/boot/dts

Have a look at this Device Tree for Dummies

Therewithal answered 29/9, 2017 at 11:16 Comment(1)
The make call will definitely work. Unfortunately I am using the yocto build system so i would need to make an additional recipe to expose that call. I think this is the best option because the dtc (even with the -O) still does not work for me.Burch
L
0

With yocto, you can use

bitbake <KERNEL-VERSION> -f -c compile

e.g.

bitbake linux-stable -f -c compile

You will find the results here:

build/tmp/work/<YOUR_MACHINE>-<YOUR-DISTRO>-linux-gnueabi/<KERNEL-RECIPE>/<KERNEL-VERSION>/linux-<YOUR-MACHINE>-standard-build/arch/<ARCH>/boot/dts/<YOUR-DTS-FILENAME>.dtb

You cam find there also the result of the preprocessor with name .<YOUR-DTS-FILENAME>.dtb.dts.tmp. This file contains all includes and is useful for error analysis.

Lavallee answered 17/2, 2023 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.