这两天看了一下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:
breset//复位中断向量
ldrpc, _undefined_instruction//未使用
ldrpc, _software_interrupt//软中断
ldrpc, _prefetch_abort//处理器预取指令的地址不存在,或该地址不允许当前指令访问,产生指令预取中止异常
ldrpc, _data_abort//处理器数据访问指令的地址不存在,或该地址不允许当前指令访问时,产生数据中止异常
ldrpc, _not_used//
ldrpc, _irq//外部中断
ldrpc, _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:
.wordTEXT_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://设置中断栈的地址
.word0x0badc0de
/* 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
*/
mrsr0,cpsr
bicr0,r0,#0x1f
orrr0,r0,#0xd3
msrcpsr,r0
/*
* we do sys-critical inits only at reboot,
* not when booting from ram!
*/
#ifndef CONFIG_SKIP_LOWLEVEL_INIT
blcpu_init_crit //执行cpu_init_crit
#endif
#ifndef CONFIG_SKIP_RELOCATE_UBOOT
relocate:/* relocate U-Boot to RAM */ 将uboot复制到ram。
adrr0, _start/* r0 <- current position of code */
ldrr1, _TEXT_BASE/* test if we run from flash or RAM */
cmp r0, r1 /* don't reloc during debug */
beq stack_setup
ldrr2, _armboot_start
ldrr3, _bss_start
subr2, r3, r2/* r2 <- size of armboot */
addr2, r0, r2/* r2 <- source end address */
copy_loop:
ldmiar0!, {r3-r10}/* copy from source address [r0] */
stmiar1!, {r3-r10}/* copy to target address [r1] */
cmpr0, r2/* until source end addreee [r2] */
blecopy_loop
#endif/* CONFIG_SKIP_RELOCATE_UBOOT */
/* Set up the stack */
stack_setup://设置程序栈
ldrr0, _TEXT_BASE/* upper 128 KiB: relocated uboot */
subsp, r0, #128/* leave 32 words for abort-stack */
#ifndef CONFIG_PRELOADER
subr0, r0, #CONFIG_SYS_MALLOC_LEN/* malloc area */
subr0, r0, #CONFIG_SYS_GBL_DATA_SIZE /* bdinfo */
#ifdef CONFIG_USE_IRQ
subr0, r0, #(CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ)
#endif
#endif /* CONFIG_PRELOADER */
subsp, r0, #12/* leave 3 words for abort-stack */
bicsp, sp, #7/* 8-byte alignment for ABI compliance */
clear_bss://清空bss段
ldrr0, _bss_start/* find start of bss segment */
ldrr1, _bss_end/* stop here */
movr2, #0x00000000/* clear */
#ifndef CONFIG_PRELOADER//循环中清空bss
clbss_l:strr2, [r0]/* clear loop... */
addr0, r0, #4
cmpr0, r1
bleclbss_l
bl coloured_LED_init
bl red_LED_on
#endif /* CONFIG_PRELOADER */
ldrpc, _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函数。