uboot流程分析--修改android启动模式按键【转】

本文详细分析了Android平台使用UBoot作为bootloader的情况,探讨了UBoot的启动流程,从链接文件开始解析,直到最终的C代码执行。文章着重介绍了如何修改按键模式,以实现开机时Vol Up进入恢复模式,Vol Down长按进入Fastboot模式。通过修改`check_key()`函数和调整主函数中的按键判定逻辑,实现了按键启动模式的定制。
摘要由CSDN通过智能技术生成

本文转载自:http://blog.csdn.net/dkleikesa/article/details/9792747

  本人用的android平台用的bootloader用的是uboot,貌似大多数手持设备平台都不用这个,因为功能过于强大用不上,反而显得太复杂了。不知道这个平台开发者是怎么想的。既然用了那就来分析一下,顺便修改一下其中的几个小问题,以符合我们的要求。

  uboot等同于其他所有的bootloader程序,从根本上讲是一个稍复杂的裸机程序,是最底层的东西,要分析裸机程序我们要从它的连接文件开始。连接文件(.lds文件)定义了程序编译之后整个连接过程,这样我们就可以找到这个程序的第一句汇编代码,进而来下一步分析。uboot的链接文件代码在android\bootable\bootloader\uboot-imx\u-boot.lds

[cpp]  view plain  copy
  1. OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")  //文件输出格式  
  2. OUTPUT_ARCH(arm)  
  3. ENTRY(_start)       //首地址标示符  
  4. SECTIONS  
  5. {  
  6.  . = 0x00000000;    //其实地址0  
  7.  . = ALIGN(4);      //4字节对齐  
  8.  .text :        //代码段  
  9.  {  
  10.    board/freescale/mx6q_sabresd/flash_header.o (.text.flasheader)   //第一个文件是board/freescale/mx6q_sabresd/flash_header.o  
  11.    cpu/arm_cortexa8/start.o         //第二个cpu/arm_cortexa8/start.o   
  12.    board/freescale/mx6q_sabresd/libmx6q_sabresd.a (.text)  
  13.    lib_arm/libarm.a (.text)  
  14.    net/libnet.a (.text)  
  15.    drivers/mtd/libmtd.a (.text)  
  16.    drivers/mmc/libmmc.a (.text)  
  17.    . = DEFINED(env_offset) ? env_offset : .;  
  18.    common/env_embedded.o(.text)  
  19.    *(.text)             //剩余的所有代码  
  20.  }  
  21.  . = ALIGN(4);  
  22.  .rodata : { *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*))) } //readonly data 段  
  23.  . = ALIGN(4);  
  24.  .data : { *(.data) }       //所有的readonly data  
  25.  . = ALIGN(4);  
  26.  .got : { *(.got) }  
  27.  . = .;  
  28.  __u_boot_cmd_start = .;        //u_boot_cmd段,里面是所有uboot命令的一个列表  
  29.  .u_boot_cmd : { *(.u_boot_cmd) }  
  30.  __u_boot_cmd_end = .;  
  31.  . = ALIGN(4);  
  32.  _end_of_copy = .;  
  33.  __bss_start = .;           //bss段 就是内存数据段  
  34.  .bss : { *(.bss) }  
  35.  _end = .;  
  36. }  


从上面的代码可以看出我们编译生成的二进制应用程序组成是:代码段->rodata段->uboot命令列表->bss段。我们启动这个应用程序时候是从,0地址开始的,因此我们来看
board/freescale/mx6q_sabresd/flash_header.s这个文件。
  这个文件中除了分配内存和宏定义的伪汇编指令以外,真正执行的命令有一条

[cpp]  view plain  copy
  1. .section ".text.flasheader", "x"  
  2.     b   _start  
  3.     .org    CONFIG_FLASH_HEADER_OFFSET  

也就是说,这个文件一执行就直接跳到_start 位置处。_start 在android\bootable\bootloader\uboot-imx\cpu\arm_cortexa8\ start.S中,因此我们来看这个文件代码

[cpp]  view plain  copy
  1. .globl _start  
  2. _start: b   reset         

这里直接跳转的reset中接下来看

[csharp]  view plain  copy
  1. reset:  
  2.     /* 
  3.      * set the cpu to SVC32 mode    cpu设置成32位管理模式 
  4.      */  
  5.     mrs r0, cpsr  
  6.     bic r0, r0, #0x1f  
  7.     orr r0, r0, #0xd3  
  8.     msr cpsr,r0  
  9.  
  10. #if (CONFIG_OMAP34XX)   //因为我们的cpu不是ompa的 所以这段不会编译  
  11. .............................  
  12. #endif  
  13.     /* the mask ROM code should have PLL and others stable */  
  14. #ifndef CONFIG_SKIP_LOWLEVEL_INIT  
  15.     bl  cpu_init_crit         
  16. #endif  

这里接下来执行cpu_init_crit

[csharp]  view plain  copy
  1. /************************************************************************* 
  2.  * 
  3.  * CPU_init_critical registers 
  4.  * 
  5.  * setup important registers 
  6.  * setup memory timing 
  7.  * 
  8.  *************************************************************************/  
  9. cpu_init_crit:  
  10.     /* 
  11.      * Invalidate L1 I/D 
  12.      */  
  13.     mov r0, #0          @ set up for MCR  
  14.     mcr p15, 0, r0, c8, c7, 0   @ invalidate TLBs  
  15.     mcr p15, 0, r0, c7, c5, 0   @ invalidate icache  
  16.   
  17.     /* 
  18.      * disable MMU stuff and caches     //关闭mmu 
  19.      */  
  20.     mrc p15, 0, r0, c1, c0, 0  
  21.     bic r0, r0, #0x00002000 @ clear bits 13 (--V-)  
  22.     bic r0, r0, #0x00000007 @ clear bits 2:0 (-CAM)  
  23.     orr r0, r0, #0x00000002 @ set bit 1 (--A-) Align  
  24.     orr r0, r0, #0x00000800 @ set bit 12 (Z---) BTB  
  25.     mcr p15, 0, r0, c1, c0, 0  
  26.   
  27.     /* 
  28.      * Jump to board specific initialization... 
  29.      * The Mask ROM will have already initialized 
  30.      * basic memory. Go here to bump up clock rate and handle 
  31.      * wake up conditions. 
  32.      */  
  33.     mov ip, lr      @ persevere link reg across call  
  34.     bl  lowlevel_init   @ go setup pll,mux,memory//执行lowlevel_init这个函数代码在  
  35.                             @\bootloader\uboot-imx\board\freescale\mx6q_sabresd\lowlevel_init.S中  
  36.                             @主要对时钟,外部ram,rom等进行了初始化代码不贴了。  
  37.     mov lr, ip      @ restore link  
  38.     mov pc, lr      @ back to my caller  

初始化完成后,接下来执行

[csharp]  view plain  copy
  1. #ifndef CONFIG_SKIP_RELOCATE_UBOOT  
  2. relocate:               @ relocate U-Boot to RAM    将uboot重新定位到内存中  
  3.     adr r0, _start      @ r0 <- current position of code  
  4.     ldr r1, _TEXT_BASE      @ test if we run from flash or RAM   
  5.     cmp r0, r1          @ don't reloc during debug测试当前代码是否已经在内存中  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式小庄老师

要是觉得不错,就给我点支持吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值