制作FIT格式镜像并运行

1. 安装依赖

# QEMU(ARM 系统模拟器 + ARM64 UEFI 固件 + 辅助工具)
sudo apt install -y qemu-system-arm qemu-efi-aarch64 qemu-utils

# 64 位 ARM 交叉编译工具链(注意:不是 arm-none-linux-gnueabihf-,那是 32 位的)
sudo apt install -y gcc-aarch64-linux-gnu

# mkimage 解析 .its 时需要 dtc
sudo apt install -y device-tree-compiler

# 直接读写 FAT 镜像,绕开 loop 设备和 mount
sudo apt install -y mtools dosfstools

# 编译 U-Boot 的 mkimage / mkeficapsule 所需
sudo apt install -y libssl-dev libgnutls28-dev

2. 编译 U-Boot

下载 U-Boot v2025.07

配置与编译

cd u-boot-v2025.07/
make distclean
export CROSS_COMPILE=aarch64-linux-gnu-
make qemu_arm64_defconfig
make -j$(nproc)

# 验证产物
$ file u-boot
u-boot: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), statically linked, with debug_info, not stripped

$ ls -lh u-boot u-boot.bin
-rwxr-xr-x 1 user01 user01  11M Nov 24 19:25 u-boot
-rw-r--r-- 1 user01 user01 1.4M Nov 24 19:25 u-boot.bin

3. 编写裸机ARM64固件

/* hello.c - minimal bare-metal ARM64 program for QEMU virt */
#define UART_BASE   0x09000000UL
#define UART_DR     (*(volatile unsigned int *)(UART_BASE + 0x00))
#define UART_FR     (*(volatile unsigned int *)(UART_BASE + 0x18))
#define UART_FR_TXFF (1u << 5)

static void uart_putc(char c)
{
    while (UART_FR & UART_FR_TXFF)
        ;
    UART_DR = (unsigned int)c;
}

static void uart_puts(const char *s)
{
    while (*s)
        uart_putc(*s++);
}

void _start(void) __attribute__((section(".text._start"), noreturn));

void _start(void)
{
    /* 在访问任何内存之前,关闭 MMU、D-Cache 和 I-Cache */
    __asm__ volatile(
        "mrs x0, sctlr_el1\n"
        "bic x0, x0, #(1 << 0)\n"   /* 关闭 MMU */
        "bic x0, x0, #(1 << 2)\n"   /* 关闭 D-Cache */
        "bic x0, x0, #(1 << 12)\n"  /* 关闭 I-Cache */
        "msr sctlr_el1, x0\n"
        "isb\n"
        ::: "x0", "memory"
    );

    uart_puts("\r\n");
    uart_puts("[MyFirmware] Hello from my own firmware!\r\n");
    uart_puts("[MyFirmware] I am running on QEMU virt ARM64.\r\n");
    uart_puts("[MyFirmware] Hanging here forever.\r\n");

    for (;;)
        ;
}

编写连接文件:

ENTRY(_start)
SECTIONS
{
    . = 0x40400000;
    .text : {
        *(.text._start)
        *(.text*)
    }
    .rodata : { *(.rodata*) }
    .data   : { *(.data*)   }
    .bss    : { *(.bss*)    }
}

执行编译、链接、生成raw binary

aarch64-linux-gnu-gcc -c -ffreestanding -nostdlib -mgeneral-regs-only -o hello.o hello.c
aarch64-linux-gnu-ld -T link.ld -o hello.elf hello.o
aarch64-linux-gnu-objcopy -O binary hello.elf hello.bin

# 验证入口地址必须是 0x40400000
$ aarch64-linux-gnu-nm hello.elf | grep _start
0000000040400000 T _start

$ file hello.bin
hello.bin: data
$ ls -lh hello.bin
-rwxr-xr-x 1 user01 user01 460 Nov 24 17:24 hello.bin

指令参数说明 : aarch64-linux-gnu-gcc -c -ffreestanding -nostdlib -mgeneral-regs-only -o hello.o hello.c

  • -c : compile only, 只编译, 不链接
  • -ffreestanding : 告诉编译器, 是自由独立环境freestanding, 不是托管环境hosted
    freestanding自由独立环境默认printf, malloc, exit等标准库函数不存在, 允许程序中没有main函数
    hosted托管环境默认有完整的C标准库, 必须要有main入口函数, 有信号及环境变量
  • -nostdlib: 不链接标准标准库启动文件, 并且不链接libc, -ffreestanding -nostdlib 这两个配置项经常搭配使用
  • -mgeneral-regs-only : AArch64 专属选项, 只使用通用寄存器(X0~X31),禁止使用浮点寄存器、NEON/SIMD 寄存器(V 寄存器)。 编译内核裸机代码非常常用, 因为ARM64上电默认浮点单元是关闭的, 直接用V寄存器会触发未定义指令异常, 代码中使用float、double浮点运算会报错

指令参数说明 : aarch64-linux-gnu-ld -T link.ld -o hello.elf hello.o
使用链接器, 按照链接脚本的规则, 将.o目标文件、段、符号、地址拼在一起, 解决符号引用, 输出最终的二进制程序

  • -T 指定链接脚本

指令参数说明 : aarch64-linux-gnu-objcopy -O binary hello.elf hello.bin

  • 将ELF文件中的ELF头, 段表, 符号表, 调试信息等附加数据全部剥离, 只保留指令和数据字节

4.制作 FIT 镜像(.its)

/dts-v1/;

/ {
    description = "Test FIT with A/B";
    #address-cells = <1>;

    images {
        firmwareA {
            description = "My Firmware A";
            data = /incbin/("hello.bin");
            type = "firmware";
            arch = "arm64";
            os = "linux";
            compression = "none";
            load = <0x40400000>;
            entry = <0x40400000>;
        };
        firmwareB {
            description = "My Firmware B";
            data = /incbin/("hello.bin");
            type = "firmware";
            arch = "arm64";
            os = "linux";
            compression = "none";
            load = <0x40400000>;
            entry = <0x40400000>;
        };
        fdt {
            description = "QEMU virt DTB";
            data = /incbin/("/home/user01/uboot/u-boot-v2025.07/arch/arm/dts/qemu-arm64.dtb");
            type = "flat_dt";
            arch = "arm64";
            compression = "none";
        };
    };

    configurations {
        default = "config_A";
        config_A {
            description = "Firmware A";
            firmware = "firmwareA";
            fdt = "fdt";
        };
        config_B {
            description = "Firmware B";
            firmware = "firmwareB";
            fdt = "fdt";
        };
    };
};

这里的FIT景象描述文件, 定义了镜像包含哪些部分,FirmwareA, FirmwareB, 固件的加载地址, 固件运行的入口地址等。这个描述文件会被mkimage打包成FIT容器镜像, 它将被U-Boot的bootm解析

5. 用mkimage生成.itb

$ ~/uboot/u-boot-v2025.07/tools/mkimage -f test.its test.itb
FIT description: Test FIT with A/B
Created:         Tue Nov 24 19:45:32 2026
 Image 0 (firmwareA)
  Description:  My Firmware A
  Created:      Tue Nov 24 19:45:32 2026
  Type:         Firmware
  Compression:  uncompressed
  Data Size:    460 Bytes = 0.45 KiB = 0.00 MiB
  Architecture: AArch64
  OS:           Linux
  Load Address: 0x40400000
 Image 1 (firmwareB)
  Description:  My Firmware B
  Created:      Tue Nov 24 19:45:32 2026
  Type:         Firmware
  Compression:  uncompressed
  Data Size:    460 Bytes = 0.45 KiB = 0.00 MiB
  Architecture: AArch64
  OS:           Linux
  Load Address: 0x40400000
 Image 2 (fdt)
  Description:  QEMU virt DTB
  Created:      Tue Nov 24 19:45:32 2026
  Type:         Flat Device Tree
  Compression:  uncompressed
  Data Size:    1288 Bytes = 1.26 KiB = 0.00 MiB
  Architecture: AArch64
 Default Configuration: 'config_A'
 Configuration 0 (config_A)
  Description:  Firmware A
  Kernel:       unavailable
  Firmware:     firmwareA
  FDT:          fdt
 Configuration 1 (config_B)
  Description:  Firmware B
  Kernel:       unavailable
  Firmware:     firmwareB
  FDT:          fdt

$ ls -lh test.itb
-rw-r--r-- 1 user01 user01 4.1K Nov 24 19:45 test.itb

$ file test.itb
test.itb: Device Tree Blob version 17, size=4137, boot CPU=0, string block size=99, DT structure block size=2984

制作虚拟磁盘并放入文件

dd if=/dev/zero of=disk.img bs=1M count=64
mkfs.vfat disk.img

# -o 表示覆盖已存在的同名文件
mcopy -o -i disk.img hello.bin ::
mcopy -o -i disk.img test.itb ::

# 列出虚拟磁盘中的文件
$ mdir -i disk.img ::
 Volume in drive : has no label
 Volume Serial Number is E151-2F36
Directory for ::/

hello    bin       460 2026-11-24  21:12
test     itb      4137 2026-11-24  21:12
        2 files               4 597 bytes
                         66 951 168 bytes free


在QEMU中启动U-Boot并引导FIT

$ qemu-system-aarch64 -machine virt -cpu cortex-a57 -nographic   -bios u-boot.bin   -drive if=none,file=disk.img,format=raw,id=hd0   -device virtio-blk-device,drive=hd0

=> fatload virtio 0 0x42000000 test.itb
4137 bytes read in 1 ms (3.9 MiB/s)
=> imxtract 0x42000000 firmwareA 0x40400000
## Copying 'firmwareA' subimage from FIT image at 42000000 ...
   Loading part 15 ... OK
=> go 0x40400000
## Starting application at 0x40400000 ...

[MyFirmware] Hello from my own firmware!
[MyFirmware] I am running on QEMU virt ARM64.
[MyFirmware] Hanging here forever.

这里模拟的就是一个FIT格式镜像从加载到运行的过程

posted @ 2026-11-26 13:30  --万波--  阅读(10)  评论(0)    收藏  举报