1、前言
在前面的文章《Uboot启动流程分析(一)》中,链接如下:
https://www.cnblogs.com/Cqlismy/p/12000889.html
已经简单地分析了low_level_init函数,其调用流程如下:
save_boot_params_ret | cpu_init_crit | | | lowlevel_init | | | s_init | _main
接下来,则继续往下分析_main函数。
2、_main函数
在save_boot_params_ret的最后,会运行bl _main这句代码,Uboot则将会跳转到_main函数中去运行,该函数的定义在arch/arm/lib/crt0.S文件中,_main函数的功能已经在文件中注释得很清楚了,先来看看_main函数实现的功能是什么,注释如下:
/* * This file handles the target-independent stages of the U-Boot * start-up where a C runtime environment is needed. Its entry point * is _main and is branched into from the target's start.S file. * * _main execution sequence is: * * 1. Set up initial environment for calling board_init_f(). * This environment only provides a stack and a place to store * the GD ('global data') structure, both located in some readily * available RAM (SRAM, locked cache...). In this context, VARIABLE * global data, initialized or not (BSS), are UNAVAILABLE; only * CONSTANT initialized data are available. GD should be zeroed * before board_init_f() is called. * * 2. Call board_init_f(). This function prepares the hardware for * execution from system RAM (DRAM, DDR...) As system RAM may not * be available yet, , board_init_f() must use the current GD to * store any data which must be passed on to later stages. These * data include the relocation destination, the future stack, and * the future GD location. * * 3. Set up intermediate environment where the stack and GD are the * ones allocated by board_init_f() in system RAM, but BSS and * initialized non-const data are still not available. * * 4a.For U-Boot proper (not SPL), call relocate_code(). This function * relocates U-Boot from its current location into the relocation * destination computed by board_init_f(). * * 4b.For SPL, board_init_f() just returns (to crt0). There is no * code relocation in SPL. * * 5. Set up final environment for calling board_init_r(). This * environment has BSS (initialized to 0), initialized non-const * data (initialized to their intended value), and stack in system * RAM (for SPL moving the stack and GD into RAM is optional - see * CONFIG_SPL_STACK_R). GD has retained values set by board_init_f(). * * 6. For U-Boot proper (not SPL), some CPUs have some work left to do * at this point regarding memory, so call c_runtime_cpu_setup. * * 7. Branch to board_init_r(). * * For more information see 'Board Initialisation Flow in README. */
从注释中,我们可以大概知道_main函数的执行顺序为:
- 先设置用于调用board_init_f()函数的初始环境,该环境仅仅是提供了堆栈和存储位置GD('global data')结构,两者都是位于可以使用的RAM(SRAM,locked cache...)中,在调用board_init_f()函数前,GD应该被清0;
- 调用board_init_f()函数,该函数的功能为从system RAM(DRAM,DDR...)中执行准备硬件,当system RAM还不能够使用的时,必须要使用目前的GD存储传递到后续阶段的所有数据,这些数据包括重定位的目标,将来的堆栈和GD的位置;
- 设置中间环境,其中堆栈和GD是由board_init_f()函数在system RAM中进行分配的,但此时的bss和初始化的非常量仍然不能使用;
- 对于正常的uboot引导(非SPL),调用relocate_code()函数,该函数的功能将uboot从当前的位置重新转移到由board_init_f()函数计算的目标位置;
- 对于SPL,board_init_f()函数仅仅是返回(crt0),没有代码的重定位;
- 设置用于调用board_init_r()函数的最终环境,该环境将有bss段(初始化为0),初始化非常量数据(初始化为预期值),并入栈到system RAM中,GD保留了board_init_f()函数设置的值;
- 为了使uboot正常运行(非SPL),某些CPU还有一些关于内存的工作要做,调用c_runtime_cpu_setup()函数;
- 调用board_init_r()函数。
_main函数的定义如下所示:
ENTRY(_main) /* * Set up initial C runtime environment and call board_init_f(0). */ #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK) ldr sp, =(CONFIG_SPL_STACK) #else ldr sp, =(CONFIG_SYS_INIT_SP_ADDR) //sp指针指向CONFIG_SYS_INIT_SP_ADDR(0x0091FF00) #endif #if defined(CONFIG_CPU_V7M) /* v7M forbids using SP as BIC destination */ mov r3, sp bic r3, r3, #7 mov sp, r3 #else bic sp, sp, #7 /* 8-byte alignment for ABI compliance */ #endif mov r0, sp //将sp指针保存到r0寄存器,此时r0的值为0x0091FF00 bl board_init_f_alloc_reserve //调用函数board_init_f_alloc_reserve,参数为r0的值 mov sp, r0 //将函数返回值写到sp指针,r0的值为0x0091FA00 /* set up gd here, outside any C code */ mov r9, r0 //将r0寄存器的值保存到r9寄存器,也就是gd结构体的地址,r9 = 0x0091FA00 bl board_init_f_init_reserve //调用board_init_f_init_reserve,参数为r0的值0x0091FA00 mov r0, #0 //将r0寄存器的值设置为0 bl board_init_f //调用board_init_f函数 #if ! defined(CONFIG_SPL_BUILD) /* * Set up intermediate environment (new sp and gd) and call * relocate_code(addr_moni). Trick here is that we'll return * 'here' but relocated. */ ldr sp, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp */ //获取新的sp指针值 #if defined(CONFIG_CPU_V7M) /* v7M forbids using SP as BIC destination */ mov r3, sp bic r3, r3, #7 mov sp, r3 #else bic sp, sp, #7 /* 8-byte alignment for ABI compliance */ //sp指针8字节对齐 #endif ldr r9, [r9, #GD_BD] /* r9 = gd->bd */ //获取gd->bd的值 sub r9, r9, #GD_SIZE /* new GD is below bd */ //r9 = r9 - GD_SIZE,r9 = gd->new_gd adr lr, here /* 获取here的链接地址 */ ldr r0, [r9, #GD_RELOC_OFF] /* r0 = gd->reloc_off */ //r0保存重定位的偏移量 add lr, lr, r0 //lr = lr + r0,here的链接地址需要加上偏移,因为uboot将被重定位 #if defined(CONFIG_CPU_V7M) orr lr, #1 /* As required by Thumb-only */ #endif ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */ //r0保存重定位的地址 b relocate_code //跳转到relocate_code函数 here: /* * now relocate vectors */ bl relocate_vectors /* Set up final (full) environment */ bl c_runtime_cpu_setup /* we still call old routine here */ #endif #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FRAMEWORK) # ifdef CONFIG_SPL_BUILD /* Use a DRAM stack for the rest of SPL, if requested */ bl spl_relocate_stack_gd cmp r0, #0 movne sp, r0 movne r9, r0 # endif ldr r0, =__bss_start /* this is auto-relocated! */ #ifdef CONFIG_USE_ARCH_MEMSET ldr r3, =__bss_end /* this is auto-relocated! */ mov r1, #0x00000000 /* prepare zero to clear BSS */ subs r2, r3, r0 /* r2 = memset len */ bl memset #else ldr r1, =__bss_end /* this is auto-relocated! */ mov r2, #0x00000000 /* prepare zero to clear BSS */ clbss_l:cmp r0, r1 /* while not at end of BSS */ #if defined(CONFIG_CPU_V7M) itt lo #endif strlo r2, [r0] /* clear 32-bit BSS word */ addlo r0, r0, #4 /* move to next */ blo clbss_l #endif #if ! defined(CONFIG_SPL_BUILD) bl coloured_LED_init bl red_led_on #endif /* call board_init_r(gd_t *id, ulong dest_addr) */ mov r0, r9 /* gd_t */ //设置第一个参数值gd指针 ldr r1, [r9, #GD_RELOCADDR] /* dest_addr */ //设置第二个参数值gd->relocaddr /* call board_init_r */ #if defined(CONFIG_SYS_THUMB_BUILD) ldr lr, =board_init_r /* this is auto-relocated! */ bx lr #else ldr pc, =board_init_r /* this is auto-relocated! */ //调用board_init_r函数 #endif /* we should not return here. */ #endif ENDPROC(_main)
_main函数的实现比较复杂,需要对其分部分进行分析。
第一部分,首先是设置初始化C运行环境,然后调用board_init_f(0)函数,功能实现的代码如下:
/* * Set up initial C runtime environment and call board_init_f(0). */ #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK) ldr sp, =(CONFIG_SPL_STACK) #else ldr sp, =(CONFIG_SYS_INIT_SP_ADDR) //sp指针指向CONFIG_SYS_INIT_SP_ADDR(0x0091FF00) #endif #if defined(CONFIG_CPU_V7M) /* v7M forbids using SP as BIC destination */ mov r3, sp bic r3, r3, #7 mov sp, r3 #else bic sp, sp, #7 /* 8-byte alignment for ABI compliance */ #endif mov r0, sp //将sp指针保存到r0寄存器,此时r0的值为0x0091FF00 bl board_init_f_alloc_reserve //调用函数board_init_f_alloc_reserve,参数为r0的值 mov sp, r0 //将函数返回值写到sp指针,r0的值为0x0091FA00 /* set up gd here, outside any C code */ mov r9, r0 //将r0寄存器的值保存到r9寄存器,也就是gd结构体的地址,r9 = 0x0091FA00 bl board_init_f_init_reserve //调用board_init_f_init_reserve,参数为r0的值0x0091FA00 mov r0, #0 //将r0寄存器的值设置为0 bl board_init_f //调用board_init_f函数
从上面的代码中可以知道,_main函数进来后,设置sp指针的值为CONFIG_SYS_INIT_SP_ADDR,并对其进行8字节对齐,然后将sp指针的值保存到r0寄存器中,接下来开始调用board_init_f_alloc_reserve函数,传入的参数为r0寄存器的值,也就是sp指针,board_init_f_alloc_reserve函数的定义在common/init/board_init.c文件中,定义如下所示:
ulong board_init_f_alloc_reserve(ulong top) //top = 0x0091FF00 { /* Reserve early malloc arena */ #if defined(CONFIG_SYS_MALLOC_F) top -= CONFIG_SYS_MALLOC_F_LEN; //top = top - 0x400,此时top = 0x0091FF00 - 0x400 = 0x0091FB00 #endif /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */ //sizeof(struct global_data) = 248 top = rounddown(top-sizeof(struct global_data), 16);
//top = top - sizeof(struct global_data),16字节对齐 //top = 0x0091FB00 - 248 - 8 = 0x0091FA00 return top; }
在上面的代码中,可以知道,board_init_f_alloc_reserve()函数主要是留出早期的malloc内存区域和global_data结构体内存区域,对于CONFIG_SYS_MALLOC_F_LEN宏定义在文件include/generated/autoconf.h文件中,为0x400,而sizeof(struct global_data)为GD_SIZE宏的值,为248,并且top的值需要16字节对齐,该函数返回top指针的值,函数调用后,OCRAM的内存分配如下所示:
board_init_f_alloc_reserve()函数是具有返回值,将top=0x0091FA00返回,继续往下分析,因此r0寄存器的值为0x0091FA00,并将r0寄存器的值回写sp指针,此时,sp=0x0091FA00,另外,r0寄存器的值也写到了r9寄存器,因此r9=0x0091FA00,为啥要将分配好的global_data结构体首地址写到r9寄存器呢?
在uboot源码文件arch/arm/include/asm/global_data.h中,具有下面的两个宏定义:
#ifdef CONFIG_ARM64 #define DECLARE_GLOBAL_DATA_PTR register volatile gd_t *gd asm ("x18") #else #define DECLARE_GLOBAL_DATA_PTR register volatile gd_t *gd asm ("r9") #endif #endif
对于ARM32的CPU,uboot定义了一个指向gd_t类型的gd指针,gd是一个全局变量,存放在r9寄存器,gd_t的类型定义在文件include/asm-generic/global_data.h中,其实就是struct global_data,该结构体的成员比较多,定义如下:
typedef struct global_data { bd_t *bd; //指向bd_t结构体内存空间 unsigned long flags; unsigned int baudrate; unsigned long cpu_clk; /* CPU clock in Hz! */ unsigned long bus_clk; /* We cannot bracket this with CONFIG_PCI due to mpc5xxx */ unsigned long pci_clk; unsigned long mem_clk; #if defined(CONFIG_LCD) || defined(CONFIG_VIDEO) unsigned long fb_base; /* Base address of framebuffer mem */ #endif #if defined(CONFIG_POST) || defined(CONFIG_LOGBUFFER) unsigned long post_log_word; /* Record POST activities */ unsigned long post_log_res; /* success of POST test */ unsigned long post_init_f_time; /* When post_init_f started */ #endif #ifdef CONFIG_BOARD_TYPES unsigned long board_type; #endif unsigned long have_console; /* serial_init() was called */ //标志位:是否具有console #ifdef CONFIG_PRE_CONSOLE_BUFFER unsigned long precon_buf_idx; /* Pre-Console buffer index */ #endif unsigned long env_addr; /* Address of Environment struct */ unsigned long env_valid; /* Checksum of Environment valid? */ unsigned long ram_top; /* Top address of RAM used by U-Boot */ //DRAM的最终地址 unsigned long relocaddr; /* Start address of U-Boot in RAM */ //uboot重定位的地址 phys_size_t ram_size; /* RAM size */ //DRAM的大小 #ifdef CONFIG_SYS_MEM_RESERVE_SECURE #define MEM_RESERVE_SECURE_SECURED 0x1 #define MEM_RESERVE_SECURE_MAINTAINED 0x2 #define MEM_RESERVE_SECURE_ADDR_MASK (~0x3) /* * Secure memory addr * This variable needs maintenance if the RAM base is not zero, * or if RAM splits into non-consecutive banks. It also has a * flag indicating the secure memory is marked as secure by MMU. * Flags used: 0x1 secured * 0x2 maintained */ phys_addr_t secure_ram; #endif unsigned long mon_len; /* monitor len */ //整个uboot的长度 unsigned long irq_sp; /* irq stack pointer */ unsigned long start_addr_sp; /* start_addr_stackpointer */ //栈指针 unsigned long reloc_off; //uboot重定位的偏移 struct global_data *new_gd; /* relocated global data */ //uboot重定位后新的gd_t结构体首地址 #ifdef CONFIG_DM struct udevice *dm_root; /* Root instance for Driver Model */ struct udevice *dm_root_f; /* Pre-relocation root instance */ struct list_head uclass_root; /* Head of core tree */ #endif #ifdef CONFIG_TIMER struct udevice *timer; /* Timer instance for Driver Model */ #endif const void *fdt_blob; /* Our device tree, NULL if none */ void *new_fdt; /* Relocated FDT */ unsigned long fdt_size; /* Space reserved for relocated FDT */ struct jt_funcs *jt; /* jump table */ char env_buf[32]; /* buffer for getenv() before reloc. */ #ifdef CONFIG_TRACE void *trace_buff; /* The trace buffer */ #endif #if defined(CONFIG_SYS_I2C) int cur_i2c_bus; /* current used i2c bus */ #endif #ifdef CONFIG_SYS_I2C_MXC void *srdata[10]; #endif unsigned long timebase_h; unsigned long timebase_l; #ifdef CONFIG_SYS_MALLOC_F_LEN unsigned long malloc_base; /* base address of early malloc() */ //早期malloc相关成员 unsigned long malloc_limit; /* limit address */ unsigned long malloc_ptr; /* current address */ #endif #ifdef CONFIG_PCI struct pci_controller *hose; /* PCI hose for early use */ phys_addr_t pci_ram_top; /* top of region accessible to PCI */ #endif #ifdef CONFIG_PCI_BOOTDELAY int pcidelay_done; #endif struct udevice *cur_serial_dev; /* current serial device */ struct arch_global_data arch; /* architecture-specific data */ #ifdef CONFIG_CONSOLE_RECORD struct membuff console_out; /* console output */ struct membuff console_in; /* console input */ #endif #ifdef CONFIG_DM_VIDEO ulong video_top; /* Top of video frame buffer area */ ulong video_bottom; /* Bottom of video frame buffer area */ #endif } gd_t;
所以,将r0寄存器的值写到r9寄存器,其实就是设置gd指针的值为0x0x0091FA00,也就是指向在OCRAM中分配的struct global_data结构体的首地址。
接下来,带参调用board_init_f_init_reserve()函数,参数为r0寄存器的值,也就是struct global_data结构体首地址0x0091FA00,board_init_f_init_reserve()函数的定义如下:
void board_init_f_init_reserve(ulong base) { struct global_data *gd_ptr; #ifndef _USE_MEMCPY int *ptr; #endif /* * clear GD entirely and set it up. * Use gd_ptr, as gd may not be properly set yet. */ gd_ptr = (struct global_data *)base; //获取global_data结构体首地址 /* zero the area */ #ifdef _USE_MEMCPY memset(gd_ptr, '