【 Intel/Altera FPGA技术实战 】Stratix 10 SOC U-Boot & ATF (Arm Trusted Firmware) Build(六)
一、前言
上节我们已经大致介绍了Stratix 10 SOC的Boot机制,在此基础上提出了Boot方式的选择方法,Boot系统架构的设计建议,软件开发、调试的所需文件/依赖等。
而我们GHRD(Golden Hardware Reference Design)工程自定义设计的开发调试是通过JTAG加载实现的,就需要软件生成JTAG加载所需的文件/依赖,那么我们就先来准备软件开发、调试的所需文件/依赖。
我们之前通过Quartus工具综合 + 实现已生成sof(1)文件,这里还需要HPS FSBL(2)文件,然后再把sof(1)文件和HPS FSBL(2)文件打包成HPS SOF(3)文件,HPS SOF(3)文件才能用于JTAG加载启动。

由上图可知,需要通过HPS Bootloader源码编译生成HPS FSBL(2)文件,这里与之前的FPGA器件通过Quartus工具的BSP Generator生成FSBL的方式不同,优势在于:之前的生成FSBL的方式需要依托于EDA工具高度定制,每板需要一个镜像;而Stratix 10和Agilex器件使用开源的U-boot源码,一个镜像可以适配多个板。
接下来向大家step by step的展示HPS FSBL的编译过程,编译过程中会遇到一些问题,让我们一起来解决,希望对大家后续的开发有帮助。
二、准备
1. EDA工具:Quartus Prime Pro 25.1(或其他版本,建议25.1以上)
2. Linux 操作系统:建议 ubuntu 22.04
3. 或者 Windows 操作系统:建议 Windows 11 安装 WSL,具体搭建方式请参考:【 Intel/Altera FPGA技术实战 】Stratix 10 SOC GHRD工程搭建(一)_altera fpga windows开发 平台搭建-ZEEKLOG博客
三、U-Boot编译
1. U-Boot Build流程具体请参考:Building Bootloader for Stratix10 | Documentation | RocketBoards.org,大致如下图所示;

2. 在GitHub下载U-Boot源码;
3. 使用下面命令下载 & Setup 交叉编译工具链,注意此时aarch64-none-linux-gnu-安装在linux根目录下:
wget https://developer.arm.com/-/media/Files/downloads/gnu/11.2-2022.02/binrel\ /gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu.tar.xz tar xf gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu.tar.xz rm -f gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu.tar.xz export PATH=`pwd`/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin:$PATH
4. 解压后切换到U-Boot源码路径,并设置编译工具链环境变量:
cd /mnt/g/Stratix10_uboot/u-boot-socfpga-socfpga_v2025.07/ export PATH=/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin:$PATH export CROSS_COMPILE=aarch64-none-linux-gnu-
5. 配置Stratix 10通用defconfig;
make socfpga_stratix10_defconfig
其实就是把./configs/socfpga_stratix10_defconfig文件内的配置信息刷到U-Boot根目录下的generated_defconfig文件,请查看下图中,执行make socfpga_stratix10_defconfig命令后,两个config文件完全相同;

6. 使用下面命令编译U-Boot,可以自动生成SPL和U-Boot;
make -j7. 编译过程可能会提示没有gcc或者python等依赖,使用下面命令安装;
sudo apt update sudo apt install build-essential sudo apt install python38. 编译过程出现make[1]: warning: Clock skew detected. Your build may be incomplete,是因为Makefile的修改时间大于系统的当前时间,这里是WSL系统时间同步问题,如果是Vmware虚拟机运行Linux应该不会出现该问题;

9. 查询WSL系统的当前时间,比Windows系统时间慢了2分钟;

10. 安装后台同步时间的服务chrony,并查询系统控制状态确认chrony已激活 & 运行;
sudo apt update sudo apt install chrony sudo systemctl status chrony11. 再次查询WSL系统的当前时间,与Windows系统时间相同;

12. 重新编译U-Boot,warning已经消除,但是又报错提示:Image 'u-boot' is missing external blobs and is non-functional: blob-ext,Image 'u-boot' has faked external blobs and is non-functional: bl31.bin;
出现该错误的原因是U-Boot编译需要ATF支持,但缺少关键的二进制blob文件(bl31.bin),bl31.bin是ATF的Stage 3-1镜像,负责提供安全监控调用(SMC)、PSCI电源管理等服务,U-Boot 的FIT(Flattened Image Tree)镜像在build时需要把 bl31.bin打包进去,但找不到该文件,故Binman(U-Boot 的镜像打包工具)报错并中止编译;

四、ATF编译
1. 由上面log可知需要编译ATF生成bl31.bin,在GitHub下载ATF源码;
2. 解压后切换到ATF源码路径,设置编译工具链环境变量,并检查交叉编译工具链是否ok;
cd /mnt/g/Stratix10_uboot/arm-trusted-firmware-socfpga_v2.13.0/ export PATH=/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin:$PATH export CROSS_COMPILE=aarch64-none-linux-gnu- aarch64-none-linux-gnu-gcc --version
3. 使用下面命令编译ATF;
make PLAT=stratix10 clean make PLAT=stratix10 all4. 在./build/stratix10/release路径下成功编译bl2.bin,bl31.bin;


五、U-Boot & bl31.bin编译
1. 切换到U-Boot源码路径,把刚才编译好的bl31.bin复制到U-Boot根目录;
cp /mnt/g/Stratix10_uboot/arm-trusted-firmware-socfpga_v2.13.0/build/stratix10/release/bl31.bin /2. 使用下面命令重新编译U-Boot;
PATH=/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin:$PATH export CROSS_COMPILE=aarch64-none-linux-gnu- aarch64-none-linux-gnu-gcc --version make distclean make -j3. 下图中可以看到在spl/路径下生成u-boot-spl.bin,u-boot-spl-dtb.bin和u-boot-spl-dtb.hex文件,最后OFCHK .config检查配置并正常退出,说明U-Boot编译成功;

4. 使用下面命令查看,U-Boot根目录下FIT镜像u-boot.itb是否包含bl31.bin和u-boot,Image 1 (atf)子镜像就是bl31.bin,说明bl31.bin已打包ok;
./tools/dumpimage -l u-boot.itb
5. 查看生成的u-boot文件;
- U-Boot根目录下:u-boot文件是ELF(Executable and Linkable Format)格式的u-boot,是一种可执行文件格式,主要用于开发调试;而u-boot.itb文件是FIT格式的u-boot,是把内核、设备树等封装在一起的镜像,主要用于固化部署,u-boot.itb就是上节中我们提到的HPS SSBL文件;
- spl/路径下:u-boot-spl.bin就是上节中我们提到的HPS FSBL文件,该BIN格式文件用于烧录到flash;

6. 我们再来回忆一下Stratix 10 SoC中HPS的Boot流程(为简化流程,这里省掉了FPGA配置),其中HPS FSBL文件主要用于初始化,具体请参考:【 Intel/Altera FPGA技术实战 】Stratix 10 SOC 系统 Boot 概述(五)-ZEEKLOG博客
- Power On;
- SDM先加载HPS EMIF IO,再加载HPS FSBL(u-boot-spl.bin)到HPS的片上RAM的0x00000000地址,最后释放HPS的复位;
- HPS执行FSBL(初始化DRAM、时钟、外设等),再从启动介质加载SSBL(u-boot.itb)到HPS侧DRAM;
- HPS跳转执行SSBL;
- SSBL加载Linux Kernel/Application;
7. SPL源码中有详细的FSBL初始化流程(SPL源码绝对路径:./arch/arm/mach-socfpga/spl_s10.c),请参考已注释的SPL源码;
// SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2016-2023 Intel Corporation <www.intel.com> * */ #include <hang.h> #include <init.h> #include <log.h> #include <asm/global_data.h> #include <asm/io.h> #include <asm/utils.h> #include <debug_uart.h> #include <image.h> #include <spl.h> #include <asm/arch/clock_manager.h> #include <asm/arch/firewall.h> #include <asm/arch/mailbox_s10.h> #include <asm/arch/misc.h> #include <asm/arch/reset_manager.h> #include <asm/arch/smmu_s10.h> #include <asm/arch/system_manager.h> #include <wdt.h> #include <dm/uclass.h> DECLARE_GLOBAL_DATA_PTR; void board_init_f(ulong dummy) { // 初始化运行环境 const struct cm_config *cm_default_cfg = cm_get_default_config(); int ret; struct udevice *dev; ret = spl_early_init(); // 初始化 SPL 内存池、malloc if (ret) hang(); // 获取Stratix 10外设基地址,包括时钟、复位、系统管理等寄存器 socfpga_get_managers_addr(); /* Ensure watchdog is paused when debugging is happening */ writel(SYSMGR_WDDBG_PAUSE_ALL_CPU, socfpga_get_sysmgr_addr() + SYSMGR_SOC64_WDDBG); // 配置看门狗 /* * Enable watchdog as early as possible before initializing other * component. */ if (CONFIG_IS_ENABLED(WDT)) // 配置看门狗 initr_watchdog(); /* ensure all processors are not released prior Linux boot */ writeq(0, CPU_RELEASE_ADDR); // 清除 CPU 释放寄存器,确保所有核在reset // 初始化定时器 socfpga_per_reset(SOCFPGA_RESET(OSC1TIMER0), 0); timer_init(); // 初始化FSBL和SDM之间通信的Mailbox,FSBL获取SDM侧QSPI flash访问控制权的请求通道 mbox_init(); mbox_hps_stage_notify(HPS_EXECUTION_STATE_FSBL); // 引脚复用初始化,根据Platform Designer中HPS外设IO配置进行初始化 sysmgr_pinmux_init(); /* configuring the HPS clocks */ cm_basic_init(cm_default_cfg); // HPS时钟系统初始化,包括HPS,DRAM和HPS外设等时钟 // 调试UART初始化,建议开发调试阶段开启 #ifdef CONFIG_DEBUG_UART socfpga_per_reset(SOCFPGA_RESET(UART0), 0); debug_uart_init(); #endif preloader_console_init(); // 控制台注册,使用printf/puts函数可以输出log print_reset_info(); // 显示上次复位原因(POR, WDOG, 软件复位等),用于调试 cm_print_clock_quick_summary(); // 打印关键时钟频率(CPU, DDR, OSC1 等),用于调试 // 初始化Security Register & Firewall ret = uclass_get_device_by_name(UCLASS_NOP, "socfpga-secreg", &dev); if (ret) { printf("Firewall & secure settings init failed: %d\n", ret); hang(); } // 初始化DRAM,包括DRAM PHY/controller initial/calibration + tarining #if CONFIG_IS_ENABLED(ALTERA_SDRAM) ret = uclass_get_device(UCLASS_RAM, 0, &dev); if (ret) { debug("DRAM init failed: %d\n", ret); hang(); } #endif // 初始化QSPI #ifdef CONFIG_CADENCE_QSPI mbox_qspi_open(); #endif }8. 前言中用于JTAG加载启动的HPS SOF文件(3),是由sof(1)文件和HPS FSBL Hex(2)文件打包生成的,而U-Boot编译生成的HPS FSBL(u-boot-spl.bin)文件不能直接用于打包,需要用ELF格式的u-boot-spl文件转换出HEX格式的u-boot-spl.hex;

9. 使用下面命令把HPS FSBL(u-boot-spl.bin)文件转换为HEX文件;
aarch64-none-linux-gnu-objcopy -O ihex spl/u-boot-spl spl/u-boot-spl.hex10. 查看已生成HPS FSBL HEX文件,接下来我们就可以打包HPS SOF文件(3),再用JTAG愉快的加载启动了。
