archarmlib crt0.S
1.设置sp为CONFIG_SYS_INIT_SP_ADDR
include/configs/xxx.h
#define CONFIG_SYS_INIT_SP_ADDR (CONFIG_SYS_INIT_RAM_ADDR +
CONFIG_SYS_INIT_RAM_SIZE - GENERATED_GBL_DATA_SIZE)
#define CONFIG_SYS_INIT_RAM_ADDR 0x20000
#define CONFIG_SYS_INIT_RAM_SIZE 0x1000
include/generated/generic-asm-offsets.h
#define GENERATED_GBL_DATA_SIZE 192 /* (sizeof(struct global_data) + 15) & ~15 @ */
CONFIG_SYS_INIT_SP_ADDR=0x20F40
2.调用board_init_f_alloc_reserve
没有定义SYS_MALLOC_F_LEN,不执行。这里不对
make distclean后,CONFIG_SYS_MALLOC_F_LEN默认值0x400
top初始值0x20F40
#if CONFIG_VAL(SYS_MALLOC_F_LEN)
top -= CONFIG_VAL(SYS_MALLOC_F_LEN); #top=0x20B40
#endif
top = rounddown(top-sizeof(struct global_data), 16); #top=0x20A80
3. r9(gd) = sp = top;
gd地址定了。CONFIG_SYS_INIT_SP_ADDR=0x20A80
4.调用board_init_f
注意:串口初始化之后才能打印输出。
setup_dest_addr
#ifdef CONFIG_SYS_SDRAM_BASE
gd->ram_top = CONFIG_SYS_SDRAM_BASE;
#endif
gd->ram_top += get_effective_memsize();
gd->ram_top = board_get_usable_ram_top(gd->mon_len);
gd->relocaddr = gd->ram_top;
debug("Ram top: %08lX
", (ulong)gd->ram_top);
gd->ram_top=0x20000+0x40000=0x60000
gd->relocaddr=gd->ram_top=0x60000
reserve_uboot --> Reserving 192k for U-Boot at: 0002f000 gd->relocaddr到此为止,之后gd->start_addr_sp出场
reserve_malloc --> Reserving 4k for malloc() at: 0002de70
reserve_board --> Reserving 80 Bytes for Board Info at: 0002de20
reserve_global_data --> Reserving 192 Bytes for Global Data at: 0002dd60 gd->new_gd
reloc_fdt --> 因为CONFIG_OF_EMBED,所以不执行什么
reloc_bootstage --> 因为没有定义CONFIG_BOOTSTAGE,所以不执行什么
reserve_arch --> 空
reserve_stacks --> gd->start_addr_sp -= 16; gd->start_addr_sp &= ~0xf; 调用arch_reserve_stacks(定义在archarmlibstack.c)gd->start_addr_sp -= 16; 一共减32byte
display_new_sp --> New Stack Pointer is: 0002dd40
setup_reloc --> gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
gd->relocaddr=
0002f000
__image_copy_start
就是CONFIG_SYS_TEXT_BASE=0x2f000
Relocation Offset is: 00000000
Relocating to 0002f000, new gd at 0002dd60, sp at 0002dd40
ldr r0, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp */
bic r0, r0, #7 /* 8-byte alignment for ABI compliance */
mov sp, r0 //定义新sp 之前为0x21000
ldr r9, [r9, #GD_BD] /* r9 = gd->bd */
sub r9, r9, #GD_SIZE /* new GD is below bd */
adr lr, here
ldr r0, [r9, #GD_RELOC_OFF] /* r0 = gd->reloc_off */
add lr, lr, r0
#if defined(CONFIG_CPU_V7M)
orr lr, #1 /* As required by Thumb-only */
#endif
ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
b relocate_code
如果gd->relocaddr和__image_copy_start相等
ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */
subs r4, r0, r1 /* r4 <- relocation offset */
beq relocate_done /* skip relocation */
就跳过搬移。
所以整个过程中,搬移的就只有gd_t。
如何计算defconfig中的CONFIG_SYS_TEXT_BASE
vi u-boot.map
setup_mon_len中
gd->mon_len = (ulong)&__bss_end - (ulong)_start;
reserve_uboot
gd->relocaddr -= gd->mon_len;
__bss_end
0x000000000005f3a0
.__image_copy_start
0x0000000000033000
gd->mon_len=0x2c3a0
gd->relocaddr=gd->ram_top-gd->mon_len(u-boot)=0x60000-0x2c3a0=0x33c60
后三位清零,4K对齐 gd->relocaddr &= ~(4096 - 1);
gd->relocaddr=0x33000
即CONFIG_SYS_TEXT_BASE设置为gd->relocaddr=0x33000__image_copy_start
即可跳过搬移。