How does kernel know which device tree to load

device-treekernel

Device tree is used at runtime on ARM devices to identify and load correct drivers with its configuration. But when I look into loaded DT at /proc/device-tree on my phone for example, there are many configurations unrelated to actuall platform. For example about 15 different display configurations that this specific device never used and never will. So far I thought only relevant DT gets included into the kernel and loaded but it seems all DTS from related /arch source folder are loaded. How does the kernel select the right one for the platform? IsnĀ“t this a bit redundant?

Best Answer

Determining which device tree blob (DTB) file, which is compiled from the device tree source (DTS), to load is typically handled by the bootloader.

Many device manufacturers will customize the bootloader to add manufacturer-specific code for automating the task of determining which DTB file to load for that hardware.

An example for U-Boot, a commonly used bootloader, that loads a zImage kernel image and the DTB fdt_file from a FAT partition on a SATA device. These are loaded to memory addresses specified by loadaddr and fdt_addr respectively, and then handed off to U-Boot's bootz command.

fatload sata 0:1 ${loadaddr} ${image};
fatload sata 0:1 ${fdt_addr} ${fdt_file};
bootz ${loadaddr} - ${fdt_addr};

U-Boot's bootz usage:

bootz [addr [initrd[:size]] [fdt]]
    - boot Linux zImage stored in memory
        The argument 'initrd' is optional and specifies the address
        of the initrd in memory. The optional argument ':size' allows
        specifying the size of RAW initrd.
        When booting a Linux kernel which requires a flat device-tree
        a third argument is required which is the address of the
        device-tree blob. To boot that kernel without an initrd image,
        use a '-' for the second argument. If you do not pass a third
        a bd_info struct will be passed instead

For specifics regarding how U-Boot handles the fdt_file, you can reference the source code for bootm_find_images, as well as boot_get_fdt.

You can also try to glean some information from the U-Boot manual: https://www.denx.de/wiki/view/DULG/UBootCmdFDT

Related Question