buildroot构建项目(八)--- u-boot 2017.11 适配开发板修改 5 ---- 系统启动初始化之五

时间:2021-08-24 02:40:03

  执行完 board_init_f 后,跳回到 crt0.S中继续执行汇编语言

     ldr    r0, [r9, #GD_START_ADDR_SP]    /* sp = gd->start_addr_sp,gd->start_addr_sp在fdt的初始位置 */
bic r0, r0, # /* 8-byte alignment for ABI compliance */
mov sp, r0 /* sp 指向 fdt 初始位置 */
ldr r9, [r9, #GD_BD] /* r9 = gd->bd */
sub r9, r9, #GD_SIZE /* new GD is below bd,r9 为 gd-t 的基地址 */ adr lr, here /* 跳转到 here 处执行,gd->relocaddr = 0x3400 0000 */

  最后一句,跳转到here处去执行

 here:
/*
* now relocate vectors
*/
bl relocate_vectors /* 重定向向量表 */

一、relocate_vectors 重定向向量表

  relocate.S (arch\arm\lib)

  这一段主要是对前面所说的向量表进行重定向

     /*
* Copy the relocated exception vectors to the
* correct address
* CP15 c1 V bit gives us the location of the vectors:
* 0x00000000 or 0xFFFF0000.
*/
ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr。0x3400 0000 */
/* 将 c1 的值读到 r2 寄存器中 */
/* 主要是控制 bit[13]: V */
/* 对于支持高端异常向量表的系统,本控制位控制向量表的位置 */
/* 0 :选择低端异常中断向量 0x0~0x1c */
/* 1 :选择高端异常中断向量0xffff0000~ 0xffff001c */
/* 对于不支持高端异常向量表的系统,读取时该位返回0,写入时忽略 */
mrc p15, , r2, c1, c0, /* V bit (bit[13]) in CP15 c1 */
/* ands 后面的 s 会影响CPSR状态的寄存器的标志位 */
/* 若 相与的 结果为0,则CPSR的状态标志位 Z = 1;反之,Z = 0 */
ands r2, r2, #( << ) /* r2 寄存器和 0010 0000 0000 0000 按位与后存入r2中 */
ldreq r1, =0x00000000 /* If V=0,则Z=1,可执行 ldr指令 */
ldrne r1, =0xFFFF0000 /* If V=1,则Z=0,可执行 ldr */
/* 将 r2 -- r8 以及 r10 传入相应的地址,每次传输之后递增R0指向的存储地址,因为是32位,每次递增的地址应该是4bytes */
ldmia r0!, {r2-r8,r10} /* ldmia 多寄存器寻址指令, */
stmia r1!, {r2-r8,r10} /* 将R2-R8 和 r10的数据存储到R1指向的地址上,R1值更新。 */
ldmia r0!, {r2-r8,r10}
stmia r1!, {r2-r8,r10}
#endif

二、relocate_code

  here执行完后,跳回,执行下面的代码

     ldr    r0, [r9, #GD_RELOC_OFF]        /* r0 = gd->reloc_off,gd->reloc_off = gd->relocaddr */
add lr, lr, r0 /* lr += r0,异常向量表的地址确定 */
/* 重定向地址确定 */
ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr 为 0x3400 0000 - u-boot 大小,即在u-boot的起始地址处 */
b relocate_code /* 重定向代码 */

  进入 relocate_code, relocate.S (arch\arm\lib)

 ENTRY(relocate_code)
/* __image_copy_start 在u-boot.lds 文件中定义,为起始地址 0 */
ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */
/* subs 影响借位C标志, */
subs r4, r0, r1 /* r4 <- relocation offset ,r4 为重定向偏移量 */
beq relocate_done /* skip relocation,C变为位为1则执行代码 */
/* r2为需要重定位代码的结束地址,r2 - r1就是需要重定位代码长度了 */
ldr r2, =__image_copy_end /* r2 <- SRC &__image_copy_end */ /* 到现在为止,比较重要的几个寄存器的值为
* r0 = gd->reloc_off,r0为重定位偏移量,本处也就是目标地址
* r1 = __image_copy_start,r1为需要重定位代码当前的起始地址,也就是代码段的开始0
* r4 = r0 - r1,r4为重定位的偏移值,偏移值减去0还是0
* r2 =__image_copy_end,r2为需要重定位代码的结束地址,r2 - r1就是需要重定位代码长度了
*/
copy_loop:
/* 从源地址 [r1] 开始拷贝,pop到r10与r11里面,一次8个字节 */
ldmia r1!, {r10-r11} /* copy from source address [r1] */
/* 拷贝到目标地址 [r0] */
stmia r0!, {r10-r11} /* copy to target address [r0] */
/* 一直到 [r1] 等于 [r2], 说明代码拷贝结束 */
cmp r1, r2 /* until source end address [r2] */
blo copy_loop /*
* fix .rel.dyn relocations
* 重定位修正
*/
ldr r2, =__rel_dyn_start /* r2 <- SRC &__rel_dyn_start */
ldr r3, =__rel_dyn_end /* r3 <- SRC &__rel_dyn_end */
fixloop:
ldmia r2!, {r0-r1} /* (r0,r1) <- (SRC location,fixup) */
and r1, r1, #0xff
cmp r1, #R_ARM_RELATIVE
bne fixnext /* relative fix: increase location by offset */
add r0, r0, r4
ldr r1, [r0]
add r1, r1, r4
str r1, [r0]
fixnext:
cmp r2, r3
blo fixloop relocate_done: #ifdef __XSCALE__
/*
* On xscale, icache must be invalidated and write buffers drained,
* even with cache disabled - 4.2.7 of xscale core developer's manual
*/
mcr p15, , r0, c7, c7, /* invalidate icache */
mcr p15, , r0, c7, c10, /* drain write buffer */
#endif /* ARMv4- don't know bx lr but the assembler fails to see that */ #ifdef __ARM_ARCH_4__
mov pc, lr
#else
bx lr
#endif ENDPROC(relocate_code)

  执行完后,跳回 crt0.S中继续执行

 /*
* now relocate vectors
*/
bl relocate_vectors /* 重定向向量表 */ /* Set up final (full) environment */
bl c_runtime_cpu_setup /* we still call old routine here */ #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FRAMEWORK) ldr r0, =__bss_start /* this is auto-relocated! */
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 */
strlo r2, [r0] /* clear 32-bit BSS word */
addlo r0, r0, # /* move to next */
blo clbss_l #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 */
ldr r1, [r9, #GD_RELOCADDR] /* dest_addr */
/* call board_init_r */
ldr pc, =board_init_r /* this is auto-relocated! */
/* we should not return here. */
#endif

  执行进入 board_init_r 中

三、board_init_r

 void board_init_r(gd_t *new_gd, ulong dest_addr)
{
if (initcall_run_list(init_sequence_r))
hang();
/* NOTREACHED - run_main_loop() does not return */
hang();
}

  功能函数得实现都在 init_sequeuece_r 中

 static init_fnc_t init_sequence_r[] = {
initr_trace, /* 宏未定义,直接返回 0 */
initr_reloc, /* gd->flags = 1 */
/* TODO: could x86/PPC have this also perhaps? */
#ifdef CONFIG_ARM
initr_caches, /* 打印 :Caches not enabled ,暂时未实现*/
#endif
initr_reloc_global_data, /* 宏未定义,直接返回 0 */
initr_barrier, /* 宏未定义,直接返回 0 */
initr_malloc, /* malloc 区分配 */
initr_bootstage, /* Needs malloc() but has its own timer */
initr_console_record, /* 终端记录 */
bootstage_relocate, /* 重定向启动阶段 */
#if defined(CONFIG_ARM) || defined(CONFIG_NDS32)
board_init, /* Setup chipselects */
#endif
stdio_init_tables, /* 设备表格初始化 */
initr_serial, /* 初始化串口,执行得是 Serial.c (drivers\serial) 得serial_initialize()*/
initr_announce, /* 打印 u-boot 运行信息 */
INIT_FUNC_WATCHDOG_RESET
power_init_board, /* 执行得 board.c(common) 中得 */
#ifdef CONFIG_MTD_NOR_FLASH
initr_flash, /* norflash 初始化 */
#endif
INIT_FUNC_WATCHDOG_RESET
#ifdef CONFIG_CMD_NAND
initr_nand, /* nand flash 初始化 */
#endif
initr_env, /* 环境变量初始化 */
INIT_FUNC_WATCHDOG_RESET
initr_secondary_cpu, /* CPU第二阶段初始化,里面为空 */
INIT_FUNC_WATCHDOG_RESET
stdio_add_devices, /* 宏未定义,里面执行为空 */
initr_jumptable, /* jumptable 初始化 */
console_init_r, /* fully init console as a device */
INIT_FUNC_WATCHDOG_RESET
interrupt_init, /* 中断初始化 */
#ifdef CONFIG_ARM
initr_enable_interrupts, /* 中断使能初始化 */
#endif
/* PPC has a udelay(20) here dating from 2002. Why? */
#ifdef CONFIG_CMD_NET
initr_ethaddr, /* 网络地址初始化 */
#endif
#ifdef CONFIG_CMD_NET
INIT_FUNC_WATCHDOG_RESET
initr_net, /* 网络初始化 */
#endif
run_main_loop, /* 启动 内核 */
};

  重新编译一下,编译OK了,制作补丁