如何将设备树 blob 添加到 Linux x86 内核引导?

How to add device tree blob to Linux x86 kernel boot?

我的自定义开发板是基于 x86 的,如果不使用供应商内核驱动程序(如果我不这样做,供应商也不会提供帮助),无法轻松控制连接到它的电子组件之一(主要通过 SPI) '使用它)。该模块需要一些从设备树中获取的配置参数。我相信这个模块主要用于设备树很常见的ARM平台。

在 x86 上,通常不需要设备树,因此在 Linux 内核编译期间默认禁用它。我更改了配置以启用它,但我找不到将设备树 BLOB 放入引导映像的方法。内核源代码中只有 x86 架构的 one DTS file 但它似乎根本没有被使用,所以它没有帮助。

直接从 kernel documentation, I understand I need to put it in the setup_data field of the x86 real-mode kernel header, but I don't understand how to do that and when (at kernel build time? when building the bootloader?). Am I supposed to hack the arch/x86/boot/header.S 文件?

现在,我已经用硬编码值替换了模块配置,但使用设备树会更好。

在 x86 上,引导加载程序在调用内核入口点之前将设备树二进制数据 (DTB) 添加到 setup_data 结构的链表中。 DTB 可以从存储设备加载或嵌入到引导加载程序映像中。

下面的代码展示了它是如何在 U-Boot 中实现的。

http://git.denx.de/?p=u-boot.git;a=blob;f=arch/x86/lib/zimage.c:

static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
{
        int bootproto = get_boot_protocol(hdr);
        struct setup_data *sd;
        int size;

        if (bootproto < 0x0209)
                return -ENOTSUPP;

        if (!fdt_blob)
                return 0;

        size = fdt_totalsize(fdt_blob);
        if (size < 0)
                return -EINVAL;

        size += sizeof(struct setup_data);
        sd = (struct setup_data *)malloc(size);
        if (!sd) {
                printf("Not enough memory for DTB setup data\n");
                return -ENOMEM;
        }

        sd->next = hdr->setup_data;
        sd->type = SETUP_DTB;
        sd->len = fdt_totalsize(fdt_blob);
        memcpy(sd->data, fdt_blob, sd->len);
        hdr->setup_data = (unsigned long)sd;

        return 0;
}