如何基于硬件版本通过barebox从FIT镜像选择对应设备树配置
基于硬件版本在barebox中选择FitImage对应配置的实现方法
问题概述
当前使用barebox bootloader,现有FitImage包含1个内核与2个设备树(comm-a.dtb、comm-b.dtb),已知硬件版本信息,需根据硬件版本自动选择FitImage中的对应配置,加载匹配的设备树。
实现步骤
1. 优化FitImage的ITS配置(可选但推荐)
为每个配置节点添加硬件版本标识元数据,方便后续barebox识别匹配。修改你提供的fitimage.its文件中configurations部分:
{ description = "fitImage for kernel and two device trees"; #address-cells = <1>; images { kernel-1 { description = "Linux kernel"; data = /incbin("/build/tmp/deploy/images/zImage"); type = "kernel"; arch = "arm"; os = "linux"; compression = "none"; hash-1 { algo = "sha256"; }; }; fdt-comm-a.dtb { description = "Flattened Device Tree blob for HW Version A"; data = /incbin("/build/tmp/deploy/images/comm-a.dtb"); type = "flat_dt"; arch = "arm"; compression = "none"; hash-1 { algo = "sha256"; }; }; fdt-comm-b.dtb { description = "Flattened Device Tree blob for HW Version B"; data = /incbin("/build/tmp/deploy/images/comm-b.dtb"); type = "flat_dt"; arch = "arm"; compression = "none"; hash-1 { algo = "sha256"; }; }; }; configurations { default = "comm-a.dtb"; comm-a.dtb { description = "Linux kernel with comm-a DTB (HW Version A)"; kernel = "kernel-1"; fdt = "fdt-comm-a.dtb"; /* 添加硬件版本标识,可自定义数值 */ hw-version = <0x1>; signature-1 { algo = "sha256,rsa4096"; key-name-hint = "FIT-4096"; sign-images = "kernel", "fdt"; signer-name = "customer"; signer-version = "vPD20.0.0"; }; }; comm-b.dtb { description = "Linux kernel with comm-b DTB (HW Version B)"; kernel = "kernel-1"; fdt = "fdt-comm-b.dtb"; /* 添加硬件版本标识 */ hw-version = <0x2>; signature-1 { algo = "sha256,rsa4096"; key-name-hint = "FIT-4096"; sign-images = "kernel", "fdt"; signer-name = "customer"; signer-version = "vPD20.0.0"; }; }; }; };
修改完成后,重新编译生成FitImage:
mkimage -f fitimage.its fitImage
2. Barebox侧实现硬件版本读取与配置选择
步骤1:读取硬件版本
根据你的硬件设计,读取版本信息(示例为常见的EEPROM读取,需根据实际硬件修改):
# 从I2C EEPROM(地址0x50)的0x00地址读取1字节硬件版本 hw_ver=$(i2c_read 0 0x50 0x00 1)
如果是通过GPIO电平判断版本:
# 假设GPIO3_0为高电平对应版本B,低电平对应版本A if gpio_get_value 3 0; then hw_ver=0x2 else hw_ver=0x1 fi
步骤2:选择对应FitImage配置
有两种实现方式:
方式一:直接匹配配置名(简单直接)
如果配置名与硬件版本一一对应,直接指定配置名启动:
# 根据硬件版本设置配置名 if [ $hw_ver -eq 0x1 ]; then fit_config="comm-a.dtb" else fit_config="comm-b.dtb" fi # 启动FitImage并指定配置 bootm fitImage#$fit_config
方式二:遍历匹配hw-version属性(扩展性强)
适合后续新增硬件版本的场景,自动遍历FitImage中的配置寻找匹配项:
# 查找匹配当前硬件版本的FitImage配置 fit_config=$(fit_find_config fitImage "hw-version=$hw_ver") # 启动逻辑:找到匹配配置则启动,否则使用默认配置 if [ -n "$fit_config" ]; then bootm fitImage#$fit_config else bootm fitImage fi
3. 固化启动逻辑到barebox环境
将上述逻辑写入barebox的启动脚本(如/env/boot),实现开机自动选择:
#!/bin/sh # 读取硬件版本(根据实际硬件修改) hw_ver=$(i2c_read 0 0x50 0x00 1) # 选择对应配置 if [ $hw_ver -eq 0x1 ]; then fit_config="comm-a.dtb" elif [ $hw_ver -eq 0x2 ]; then fit_config="comm-b.dtb" else fit_config="comm-a.dtb" # 版本未知时使用默认配置 fi # 启动内核 bootm fitImage#$fit_config
内容的提问来源于stack exchange,提问作者cherry411
相关产品推荐
相关产品推荐

