uboot等同於其他所有的bootloader程序,從根本上講是壹個稍復雜的裸機程序,是最底層的東西,要分析裸機程序我們要從它的連接文件開始。連 接文件(.lds文件)定義了程序編譯之後整個連接過程,這樣我們就可以找到這個程序的第壹句匯編代碼,進而來下壹步分析。uboot的鏈接文件代碼在 android\bootable\bootloader\uboot-imx\u-boot.lds
[cpp] view plaincopy
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm") //文件輸出格式
OUTPUT_ARCH(arm)
ENTRY(_start) //首地址標示符
SECTIONS
{
. = 0x00000000; //其實地址0
. = ALIGN(4); //4字節對齊
.text : //代碼段
{
board/freescale/mx6q_sabresd/flash_header.o (.text.flasheader) //第壹個文件是board/freescale/mx6q_sabresd/flash_header.o
cpu/arm_cortexa8/start.o //第二個cpu/arm_cortexa8/start.o
board/freescale/mx6q_sabresd/libmx6q_sabresd.a (.text)
lib_arm/libarm.a (.text)
net/libnet.a (.text)
drivers/mtd/libmtd.a (.text)
drivers/mmc/libmmc.a (.text)
. = DEFINED(env_offset) ? env_offset : .;
common/env_embedded.o(.text)
*(.text) //剩余的所有代碼
}
. = ALIGN(4);
.rodata : { *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*))) } //readonly data 段
. = ALIGN(4);
.data : { *(.data) } //所有的readonly data
. = ALIGN(4);
.got : { *(.got) }
. = .;
__u_boot_cmd_start = .; //u_boot_cmd段,裏面是所有uboot命令的壹個列表
.u_boot_cmd : { *(.u_boot_cmd) }
__u_boot_cmd_end = .;
. = ALIGN(4);
_end_of_copy = .;
__bss_start = .; //bss段 就是內存數據段
.bss : { *(.bss) }
_end = .;
}
從上面的代碼可以看出我們編譯生成的二進制應用程序組成是:代碼段->rodata段->uboot命令列表->bss段。我們啟動這個應用程序時候是從,0地址開始的,因此我們來看
board/freescale/mx6q_sabresd/flash_header.s這個文件。
這個文件中除了分配內存和宏定義的偽匯編指令以外,真正執行的命令有壹條
[cpp] view plaincopy
.section ".text.flasheader", "x"
b _start
.org CONFIG_FLASH_HEADER_OFFSET
也就是說,這個文件壹執行就直接跳到_start 位置處。_start 在android\bootable\bootloader\uboot-imx\cpu\arm_cortexa8\ start.S中,因此我們來看這個文件代碼
[cpp] view plaincopy
.globl _start
_start: b reset
這裏直接跳轉的reset中接下來看
[csharp] view plaincopy
reset:
/*
* set the cpu to SVC32 mode cpu設置成32位管理模式
*/
mrs r0, cpsr
bic r0, r0, #0x1f
orr r0, r0, #0xd3
msr cpsr,r0
#if (CONFIG_OMAP34XX) //因為我們的cpu不是ompa的 所以這段不會編譯
.............................
#endif
/* the mask ROM code should have PLL and others stable */
#ifndef CONFIG_SKIP_LOWLEVEL_INIT
bl cpu_init_crit
#endif
這裏接下來執行cpu_init_crit
[csharp] view plaincopy
/*************************************************************************
*
* CPU_init_critical registers
*
* setup important registers
* setup memory timing
*
*************************************************************************/
cpu_init_crit:
/*
* Invalidate L1 I/D
*/
mov r0, #0 @ set up for MCR
mcr p15, 0, r0, c8, c7, 0 @ invalidate TLBs
mcr p15, 0, r0, c7, c5, 0 @ invalidate icache
/*
* disable MMU stuff and caches //關閉mmu
*/
mrc p15, 0, r0, c1, c0, 0
bic r0, r0, #0x00002000 @ clear bits 13 (--V-)
bic r0, r0, #0x00000007 @ clear bits 2:0 (-CAM)
orr r0, r0, #0x00000002 @ set bit 1 (--A-) Align
orr r0, r0, #0x00000800 @ set bit 12 (Z---) BTB
mcr p15, 0, r0, c1, c0, 0
/*
* Jump to board specific initialization...
* The Mask ROM will have already initialized
* basic memory. Go here to bump up clock rate and handle
* wake up conditions.
*/
mov ip, lr @ persevere link reg across call
bl lowlevel_init @ go setup pll,mux,memory//執行lowlevel_init這個函數代碼在
@\bootloader\uboot-imx\board\freescale\mx6q_sabresd\lowlevel_init.S中
@主要對時鐘,外部ram,rom等進行了初始化代碼不貼了。
mov lr, ip @ restore link
mov pc, lr @ back to my caller
初始化完成後,接下來執行
[csharp] view plaincopy
#ifndef CONFIG_SKIP_RELOCATE_UBOOT
relocate: @ relocate U-Boot to RAM 將uboot重新定位到內存中
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 @如果在的話就直接跳轉到stack_setup
ldr r2, _armboot_start @如果不在的話,加載_armboot_start地址到r2中。_armboot_start是uboot執行的主體c函數。
ldr r3, _bss_start
sub r2, r3, r2 @ r2 <- size of armboot計算bss_start-armboot_start 保存到R2中,也就是uboot的總大小
add r2, r0, r2 @ r2 <- source end address 計算出uboot代碼和rodata地址
copy_loop: @ copy 32 bytes at a time //開始拷貝
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 */