执行完 board_init_f 后,重新跳转回 _main 中执行。
10.1 relloc_code 前
10.1.1 gd 设置
在调用board_init_f()完成板卡与全局结构体变量 gd 的初始化后将其拷贝到在代码段下重新分配的全局结构体中。接下来进行sp的重新设置,将r9指向重新分配的全局变量gd,然后进行代码的重定位。
/* 将 sp 指针指向全局gd的gd->start_addr_sp,即栈底 */
ldr r0, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp */
bic r0, r0, # /* 8-byte alignment for ABI compliance */
mov sp, r0
ldr r9, [r9, #GD_BD] /* r9 = gd->bd */
sub r9, r9, #GD_SIZE /* new GD is below bd */
10.1.2 relocate_vectors
当执行未 GD 的设置后,跳转到 relocate_vectors 中去执行向量初始化
adr lr, here
here:
/*
* now relocate vectors
*/ bl relocate_vectors
relocate_vectors 主要是进行异常向量表的重定位,将异常向量表拷贝到正确的地址中去,relocate.S (arch\arm\lib)
ENTRY(relocate_vectors)
/*
* 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 */
mrc p15, , r2, c1, c0, /* V bit (bit[13]) in CP15 c1 */
ands r2, r2, #( << )
ldreq r1, =0x00000000 /* If V=0 */
ldrne r1, =0xFFFF0000 /* If V=1 */
ldmia r0!, {r2-r8,r10}
stmia r1!, {r2-r8,r10}
ldmia r0!, {r2-r8,r10}
stmia r1!, {r2-r8,r10}
bx lr ENDPROC(relocate_vectors)
10.1.3 relocate_code
紧接着设置重定位的开始和结束地址。设置完成后跳转到 relocate_code(relocate.S) 中执行
ldr r0, [r9, #GD_RELOC_OFF] /* r0 = gd->reloc_off */
add lr, lr, r0
ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
b relocate_code
拷贝代码段和动态连接段
ENTRY(relocate_code)
ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */
subs r4, r0, r1 /* r4 <- relocation offset */
beq relocate_done /* skip relocation */
ldr r2, =__image_copy_end /* r2 <- SRC &__image_copy_end */ /* 拷贝代码到 SDRAM 中去,.__image_copy_start = 0; */
copy_loop:
ldmia r1!, {r10-r11} /* copy from source address [r1] */
stmia r0!, {r10-r11} /* copy to target address [r0] */
cmp r1, r2 /* until source end address [r2] */
blo copy_loop /*
* fix .rel.dyn relocations
* 重定位 .rel.dyn 段
*/
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: /* 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)
10.1.4 relocate_code 后
运行C环境,清理 BSS,执行点灯等操作,然后跳转到 board_init_r 中执行第二阶段初始化
/* 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)
ldr r0, =__bss_start /* this is auto-relocated! */ /* 清理 BSS */
#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
#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 */
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