本文转载自:http://blog.csdn.net/dkleikesa/article/details/9792747
本人用的android平台用的bootloader用的是uboot,貌似大多数手持设备平台都不用这个,因为功能过于强大用不上,反而显得太复杂了。不知道这个平台开发者是怎么想的。既然用了那就来分析一下,顺便修改一下其中的几个小问题,以符合我们的要求。
uboot等同于其他所有的bootloader程序,从根本上讲是一个稍复杂的裸机程序,是最底层的东西,要分析裸机程序我们要从它的连接文件开始。连接文件(.lds文件)定义了程序编译之后整个连接过程,这样我们就可以找到这个程序的第一句汇编代码,进而来下一步分析。uboot的链接文件代码在android\bootable\bootloader\uboot-imx\u-boot.lds
- 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这个文件。
这个文件中除了分配内存和宏定义的伪汇编指令以外,真正执行的命令有一条
- .section ".text.flasheader", "x"
- b _start
- .org CONFIG_FLASH_HEADER_OFFSET
也就是说,这个文件一执行就直接跳到_start 位置处。_start 在android\bootable\bootloader\uboot-imx\cpu\arm_cortexa8\ start.S中,因此我们来看这个文件代码
- .globl _start
- _start: b reset
这里直接跳转的reset中接下来看
- 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
- /*************************************************************************
- *
- * 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
初始化完成后,接下来执行
- #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测试当前代码是否已经在内存中