============================================
作者:yuanlulu
http://blog.csdn.net/yuanlulu
版权没有,但是转载请保留此段声明
============================================
这两天看了一下uboot。记录一下自己的总结成果。
参考http://www.cnblogs.com/heaad/archive/2010/07/17/1779829.html
1。参考arch/arm/arch/arm/cpu/arm926ejs/u-boot.lds文件的内容:
(参考连接http://blog.chinaunix.net/space.php?uid=20632682&do=blog&id=82147)
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
OUTPUT_ARCH(arm)
ENTRY(_start)
SECTIONS
{
. = 0x00000000; //镜像开始的地方在物理地址的0x0地址开始的地方
. = ALIGN(4); //四字节对齐
.text :
{
arch/arm/cpu/arm926ejs/start.o (.text) //代码的最前面的部分数本目录下的start.S
*(.text)
}
. = ALIGN(4); //四字节对齐
.rodata : { *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*))) } //只读数据段
. = ALIGN(4); //四字节对齐
.data : { *(.data) } //其他数据段
. = ALIGN(4); //四字节对齐
.got : { *(.got) }
. = .;
__u_boot_cmd_start = .;
.u_boot_cmd : { *(.u_boot_cmd) }
__u_boot_cmd_end = .;
. = ALIGN(4);
__bss_start = .;
.bss (NOLOAD) : { *(.bss) . = ALIGN(4); }
_end = .;
}
可以看到首先执行的代码在start.s中。
2。打开arch/arm/arch/arm/cpu/arm926ejs/start.S。
.globl _start
_start:
b reset //复位中断向量
ldr pc, _undefined_instruction //未使用
ldr pc, _software_interrupt //软中断
ldr pc, _prefetch_abort //处理器预取指令的地址不存在,或该地址不允许当前指令访问,产生指令预取中止异常
ldr pc, _data_abort //处理器数据访问指令的地址不存在,或该地址不允许当前指令访问时,产生数据中止异常
ldr pc, _not_used //
ldr pc, _irq //外部中断
ldr pc, _fiq //外部快速中断
_undefined_instruction: //定义一些变量存储处理函数的地址
.word undefined_instruction
_software_interrupt:
.word software_interrupt
_prefetch_abort:
.word prefetch_abort
_data_abort:
.word data_abort
_not_used:
.word not_used
_irq:
.word irq
_fiq:
.word fiq
#endif /* CONFIG_PRELOADER */
.balignl 16,0xdeadbeef
/*
*************************************************************************
*
* Startup Code (reset vector)
*
* do important init only if we don't start from memory!
* setup Memory and board specific bits prior to relocation.
* relocate armboot to ram
* setup stack
*
*************************************************************************
*/
_TEXT_BASE:
.word TEXT_BASE //定义一个变量存储
//TEXT_BASE是Uboot载入内存后物理的基地址。定义在board/xxx/config.mk里面。这个地址一定要和uboot在内存的首地址相同
.globl _armboot_start
_armboot_start:
.word _start
/*
* These are defined in the board-specific linker script.
*/
.globl _bss_start
_bss_start:
.word __bss_start
.globl _bss_end
_bss_end:
.word _end
#ifdef CONFIG_USE_IRQ
/* IRQ stack memory (calculated at run-time) */
.globl IRQ_STACK_START
IRQ_STACK_START: //设置中断栈的地址
.word 0x0badc0de
/* IRQ stack memory (calculated at run-time) */
.globl FIQ_STACK_START //快速中断的地址。
FIQ_STACK_START:
.word 0x0badc0de
#endif
/*
* the actual reset code
*/
reset:
/*
* set the cpu to SVC32 mode
*/
mrs r0,cpsr
bic r0,r0,#0x1f
orr r0,r0,#0xd3
msr cpsr,r0
/*
* we do sys-critical inits only at reboot,
* not when booting from ram!
*/
#ifndef CONFIG_SKIP_LOWLEVEL_INIT
bl cpu_init_crit //执行cpu_init_crit
#endif
#ifndef CONFIG_SKIP_RELOCATE_UBOOT
relocate: /* relocate U-Boot to RAM */ 将uboot复制到ram。
adr r0, _start /* r0 <- current position of code */
ldr r1, _TEXT_BASE /* test if we run from flash or RAM */
cmp r0, r1 /* don't reloc during debug */
beq stack_setup
ldr r2, _armboot_start
ldr r3, _bss_start
sub r2, r3, r2 /* r2 <- size of armboot */
add r2, r0, r2 /* r2 <- source end address */
copy_loop:
ldmia r0!, {r3-r10} /* copy from source address [r0] */
stmia r1!, {r3-r10} /* copy to target address [r1] */
cmp r0, r2 /* until source end addreee [r2] */
ble copy_loop
#endif /* CONFIG_SKIP_RELOCATE_UBOOT */
/* Set up the stack */
stack_setup: //设置程序栈
ldr r0, _TEXT_BASE /* upper 128 KiB: relocated uboot */
sub sp, r0, #128 /* leave 32 words for abort-stack */
#ifndef CONFIG_PRELOADER
sub r0, r0, #CONFIG_SYS_MALLOC_LEN /* malloc area */
sub r0, r0, #CONFIG_SYS_GBL_DATA_SIZE /* bdinfo */
#ifdef CONFIG_USE_IRQ
sub r0, r0, #(CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ)
#endif
#endif /* CONFIG_PRELOADER */
sub sp, r0, #12 /* leave 3 words for abort-stack */
bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
clear_bss: //清空bss段
ldr r0, _bss_start /* find start of bss segment */
ldr r1, _bss_end /* stop here */
mov r2, #0x00000000 /* clear */
#ifndef CONFIG_PRELOADER //循环中清空bss
clbss_l:str r2, [r0] /* clear loop... */
add r0, r0, #4
cmp r0, r1
ble clbss_l
bl coloured_LED_init
bl red_LED_on
#endif /* CONFIG_PRELOADER */
ldr pc, _start_armboot //执行_start_armboot,第一个C函数
_start_armboot:
#ifdef CONFIG_NAND_SPL //如果是从spi_nand中启动
.word nand_boot
#else
.word start_armboot //正常启动,进入C函数。
#endif /* CONFIG_NAND_SPL */
总结:
start.S中主要禁用cache、MMU,设置了中断向量,栈并清空bss段。为程序建立好C语言运行的环境之后,就将uboot复制到第二部分,去执行第一个C函数。