U-Boot初始化及工作流程分析
时间:2023-09-24 08:37:02
0. 概述
U-Boot第一个执行指令通常从与结构相关的汇编文件(尾缀为大写S的汇编文件意为可链接)中获得,例如:
- arch/arm/cpu/armv7/start.S - arch/powerpc/cpu/mpc83xx/start.S - arch/mips/cpu/start.S
在上述汇编文件中,主要执行以下三个函数:
lowlevel_init() board_init_f() board_init_r()
如下图所示:
1. lowlevel_init()
不同架构类型的处理器将单独定义lowlevel_init.S
例如:
./arch/arm/cpu/armv7/lowlevel_init.S ./arch/arm/cpu/armv8/lowlevel_init.S ./arch/mips/mach-ath79/ar933x/lowlevel_init.S
函数的基本功能是使CPU可获得,可执行board_init_f()
函数。函数中没有栈信息,无法设置SDRAM和控制台。
.pushsection .text.lowlevel_init, "ax" WEAK(lowlevel_init) #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK) ldr sp, =CONFIG_SPL_STACK #else ldr sp, =CONFIG_SYS_INIT_SP_ADDR #endif bic sp, sp, #7 /* 8-byte alignment for ABI compliance */ #ifdef CONFIG_SPL_DM mov r9, #0 #else #ifdef CONFIG_SPL_BUILD ldr r9, =gdata #else sub sp, sp, #GD_SIZE bic sp, sp, #7 mov r9, sp #endif #endif push {ip, lr} bl s_init pop {ip, pc} ENDPROC(lowlevel_init) .popsection
2. board_init_f()
为执行board_init_r
两个关键功能需要初始化:SDRAM和串口。
在此阶段,global_data
堆栈信息已经可栈信息位于SRAM中。由于BSS段仍不能使用,因此不能使用全局/静态变量。
若U-Boot中开启了SPL功能,则在common/spl.c
该函数可以在代码中实现,否则通常是common/board_f.c
以实现为准。
board_init_f()
数组中调用的函数init_sequence_f[]
中定义:
static const init_fnc_t init_sequence_f[] = {
setup_mon_len, ...... env_init, /* initialize environment */ init_baud_rate, /* initialze baudrate settings */ serial_init, /* serial communications setup */ console_init_f, /* stage 1 init of console */ display_options, /* say that we are here */ display_text_info, /* show debugging info if required */ checkcpu, #if defined(CONFIG_SYSRESET) print_resetinfo, #endif #if defined(CONFIG_DISPLAY_CPUINFO) print_cpuinfo, /* display cpu info (and speed) */ #endif #if defined(CONFIG_DTB_RESELECT)
embedded_dtb_select,
#endif
#if defined(CONFIG_DISPLAY_BOARDINFO)
show_board_info,
#endif
INIT_FUNC_WATCHDOG_INIT
#if defined(CONFIG_MISC_INIT_F)
misc_init_f,
#endif
INIT_FUNC_WATCHDOG_RESET
#if defined(CONFIG_SYS_I2C)
init_func_i2c,
#endif
#if defined(CONFIG_VID) && !defined(CONFIG_SPL)
init_func_vid,
#endif
announce_dram_init,
dram_init, /* configure available RAM banks */
#ifdef CONFIG_POST
post_init_f,
#endif
......
#if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \ !CONFIG_IS_ENABLED(X86_64)
jump_to_copy,
#endif
NULL,
};
3. board_init_r()
U-Boot执行到此处,已经进入到正常的功能代码调用流程,例如设备驱动、命令行、镜像搬移加载等功能。
若U-Boot中开启了SPL功能,则在common/spl.c
代码中可以实现该函数,否则,通常以common/board_r.c
中实现为准。
board_init_r()
中调用的函数在数组init_sequence_r[]
中定义:
static init_fnc_t init_sequence_r[] = {
initr_trace,
initr_reloc,
......
#ifdef CONFIG_MMC
initr_mmc,
#endif
......
run_main_loop,
};
以上所有的功能相关的初始化中,CPU相关的初始化代码通常位于如下路径:
linux@u-boot$ ls arch/arm/
config.mk lib mach-davinci mach-keystone mach-orion5x mach-snapdragon mach-tegra mach-zynqmp-r5
cpu mach-aspeed mach-exynos mach-kirkwood mach-owl mach-socfpga mach-uniphier Makefile
dts mach-at91 mach-highbank mach-mediatek ......
linux@u-boot$
板级相关的初始化代码通常位于如下路径:
linux@u-boot$ ls board/
abilis bluewater corscience ge lg qca sifive toradex
advantech bosch creative geekbuying l+g qemu-mips silica tplink
alliedtelesis boundary cssi google liebherr qualcomm sks-kinkel tqc
altera broadcom CZ.NIC grinn logicpd quipos socrates ucRobotics
......