Chinaunix首页 | 论坛 | 博客
  • 博客访问: 388914
  • 博文数量: 120
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 741
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-27 18:15
文章分类

全部博文(120)

文章存档

2016年(13)

2015年(41)

2014年(66)

我的朋友

分类: Android平台

2014-08-12 00:22:20

一. u-boot的编译与链接
1. lichee下编译命令
cong@dell:/work/ct/lichee$  ./build.sh -p sun7i_android -m uboot
u-boot的编译是用脚本lichee/u-boot/build.sh
  1. function build_uboot()
  2. {
  3.      make distclean CROSS_COMPILE=arm-linux-gnueabi-
  4.      make -j8 ${LICHEE_CHIP} CROSS_COMPILE=arm-linux-gnueabi-
  5.      [ $? -ne 0 ] && exit 1
  6.      cp -f u-boot.bin ../out/${LICHEE_PLATFORM}/common/     
  7. }
2. 链接
  1. cd /work/ct/lichee/u-boot && arm-linux-gnueabi-ld -pie -T u-boot.lds -Bstatic -Ttext 0x4A000000 *.o -o u-boot
说明u-boot的text段是从 0x4A000000开始的,链接脚本是u-boot.lds
  1. OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
  2. OUTPUT_ARCH(arm)
  3. ENTRY(_start)
  4. SECTIONS
  5. {
  6.  . = 0x00000000;      //在链接时通过-Ttext改为0x4A000000
  7.  . = ALIGN(4);
  8.  .text :
  9.  {
  10.   arch/arm/cpu/armv7/start.o (.text)    //u-boot的开始
  11.   *(.text)
  12.  }
  13. }
因为boot.axf把u-boot.bin读到了0x4A000000,所以运行地址与加载地址相同.

二. u-boot分析

1. stars.S
在arch/arm/cpu/armv7/start.S中,这儿的start.S就非常简单了,因为运行地址与加载地址相同,所以代码不需要再拷贝到另外的地方去,
并且在此之前ram与cpu的频率都配好了,所以这儿在中断向量之后,稍做配置就进入了c代码的board_init_f
2. 启动linux
在arch/arm/lib/board.c中
  好像这儿挺复杂的先进入board_init_f --> relocate_code --> start.S--> board_init_r
board_init_r中有main_loop--> run_command
启动内核的命令是:
bootcmd=run setargs_nand boot_normal
展开后就是:
run setenv bootargs console=ttyS0,115200 root=/dev/nandd
run sunxi_flash read 40007800 boot;boota 40007800
为什么这儿是40007800呢, 因为内核要运行在0x40008000处,而boot.img中前0x800(2048)字节是header(大小为0x800),
header之后是内核,所以将boot.img读到0x40007800,前0x800是header,那么内核就正好放在了0x40008000处.
boota 40007800
  1. int do_boota (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  2. {
  3.     addr = simple_strtoul(argv[1], NULL, 16);    //将字符串0x40007800存在addr中

  4.     //这个fastboot_boot_img_hdr结构体与 pc上的mkbootimg的结构体是一个,只不过名字变了
  5.     struct fastboot_boot_img_hdr *fb_hdr = (struct fastboot_boot_img_hdr *)addr;
  6.     //hdr指向内核在内存的起始地址  
  7.     image_header_t *hdr =(image_header_t *)(addr + CFG_FASTBOOT_MKBOOTIMAGE_PAGE_SIZE);
  8.     //将boot.img的header备份一下
  9.     memcpy(boot_hdr, (void*) addr, sizeof(*hdr));
  10.     if (memcmp(fb_hdr->magic, FASTBOOT_BOOT_MAGIC, 8)) {   //检验
  11.         puts("boota: bad boot image magic, maybe not a boot.img?\n");
  12.         return 1;
  13.     }

  14.     kaddr = addr + fb_hdr->page_size;
  15.     raddr = kaddr + ALIGN(fb_hdr->kernel_size, fb_hdr->page_size);

  16.     if((fb_hdr->unused[0] == 0x55) &&(fb_hdr->unused[1] == 0xaa))
  17.     {
  18.         dbmsg("bootimg load in boot1!\n");
  19.         do_boota_linux(fb_hdr);                 //1.1内核的启动
  20.         tick_printf(__FILE__, __LINE__);
  21.     }
  22.     puts("Boot linux failed, control return to monitor\n");
  23.     return 0;
  24. }

在arch/arm/lib/bootm.c中,传递initram的起始地址
  1. int do_boota_linux (struct fastboot_boot_img_hdr *hdr)
  2. {
  3.     ulong initrd_start, initrd_end;
  4.     void (*kernel_entry)(int zero, int arch, uint params);
  5.     bd_t *bd = gd->bd;

  6.     kernel_entry = (void (*)(int, int, uint))(hdr->kernel_addr);   //将地址0x40008000转为函数

  7.     initrd_start = hdr->ramdisk_addr;
  8.     initrd_end = initrd_start + hdr->ramdisk_size;

  9.     setup_start_tag (bd);                            //配置tag结构体开始
  10.     //设置tag
  11.     ....
  12.     setup_initrd_tag (bd, initrd_start, initrd_end)//配置initram,将起始地址与大小传给tag

  13.     setup_end_tag (bd);                              //配置tag结构体结束

  14.     sunxi_flash_exit();

  15.     announce_and_cleanup();    //引导内核前的初始,清tlb类似

  16.    *(volatile unsigned int *)(0x01c20C00 + 0x84 ) = 0;
  17.    *(volatile unsigned int *)(0x01c20C00 + 0x8C ) = 0x05DB05DB;
  18.    *(volatile unsigned int *)(0x01c20C00 + 0x80 ) = 0;
  19.    *(volatile unsigned int *)(0x01c20000 + 0x144) &= ~(1U << 31);

  20.     kernel_entry(0, bd->bi_arch_number, bd->bi_boot_params);    //跳到0x40008000处运行

  21.     return 1;
  22. }
上述启动linux过程,实际上是对boot.img的解析过程,下面就来看一下boot.img有什么.
三.boot.img分析
3.1 boot.im生成过程
./android42/out/host/linux-x86/bin/mkbootimg --kernel ./linux-3.3/bImage --ramdisk ./android42/out/target/product/sugar-cubietruck/ramdisk.img --base 0x40000000 --output ./android42/out/target/product/sugar-cubietruck/boot.img
mkbootimg有四个参数:
    kernel        指定bImage的路径
    ramdisk      指定ramdisk的路径
    base          指定kerenel与ramdisk的基地址,为0x40000000
    output       指定输出文件的路径
在android42/system/core/mkbootimg/mkbootimag.c中
  1. int main(int argc, char **argv)
  2. {
  3.     boot_img_hdr hdr;

  4.     unsigned base = 0x10000000; //由参数base确定0x40000000
  5.     unsigned kernel_offset = 0x00008000;
  6.     unsigned ramdisk_offset = 0x01000000;
  7.     unsigned second_offset = 0x00f00000;
  8.     unsigned tags_offset = 0x00000100;

  9.     hdr.page_size = pagesize;
  10.     hdr.kernel_addr = base + kernel_offset;     //0x40008000
  11.     hdr.ramdisk_addr = base + ramdisk_offset;   //0x41000000
  12.     hdr.second_addr = base + second_offset;     //0x40f00000
  13.     hdr.tags_addr = base + tags_offset;         //0x40000100

  14.     kernel_data = load_file(kernel_fn, &hdr.kernel_size);        //将内核读取到buffer中(函数中有malloc)
  15.     ramdisk_data = load_file(ramdisk_fn, &hdr.ramdisk_size);     //将ramdisk读取到buffer中(函数中有malloc)  
  16.     memcpy(hdr.id, sha, SHA_DIGEST_SIZE > sizeof(hdr.id) ? sizeof(hdr.id) : SHA_DIGEST_SIZE);
  17.     fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);      //打开输出文件boot.img

  18.     write(fd, &hdr, sizeof(hdr));                                //将head写入到boot.img中 
  19.     write_padding(fd, pagesize, sizeof(hdr));                    //2048(0x800)字节对齐   

  20.     write(fd, kernel_data, hdr.kernel_size);                     //将kernel写入到boot.img中
  21.     write_padding(fd, pagesize, hdr.kernel_size);                //2048(0x800)字节对齐   

  22.     write(fd, ramdisk_data, hdr.ramdisk_size);                   //将ramdisk写入到boot.img中
  23.     write_padding(fd, pagesize, hdr.ramdisk_size);                //2048(0x800)字节对齐   
  24.     return 0;
  25. }
3.2 boot.img的组成
所以这个boot.img是由3部分组成
  header  (kernel_size代表了内核的大小,kernel_addr) 
  kernel
  ramdisk

  1. struct fastboot_boot_img_hdr {
  2.     unsigned char magic[FASTBOOT_BOOT_MAGIC_SIZE];

  3.     unsigned kernel_size;    //内核的大小
  4.     unsigned kernel_addr;    //内核的加载地址     

  5.     unsigned ramdisk_size;   //ramdisk的大小
  6.     unsigned ramdisk_addr;   //ramdisk的起始地址

  7.     unsigned second_size;    //其它
  8.     unsigned second_addr;    //其它

  9.     unsigned tags_addr;      //其它
  10.     unsigned page_size;      //对齐到多少字节
  11.     unsigned unused[2];      //

  12.     unsigned char name[FASTBOOT_BOOT_NAME_SIZE]; /* asciiz product name */

  13.     unsigned char cmdline[FASTBOOT_BOOT_ARGS_SIZE];

  14.     unsigned id[8]; /* timestamp / checksum / sha1 / etc */
  15. };




阅读(867) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~