U-Boot启动内核分析

来源:互联网 发布:豆瓣知乎都是什么人 编辑:程序博客网 时间:2024/05/13 17:23

先来引用一下这篇介绍“ARM Linux内核启动要求”的文章ARM Linux Kernel Boot Requirements,是ARM Linux内核的维护者Russell King写的。
    * CPU register settings
          o r0 = 0.
          o r1 = machine type number.
          o r2 = physical address of tagged list in system RAM. 
    * CPU mode
          o All forms of interrupts must be disabled (IRQs and FIQs.)
          o The CPU must be in SVC mode. (A special exception exists for Angel.) 
    * Caches, MMUs
          o The MMU must be off.
          o Instruction cache may be on or off.
          o Data cache must be off and must not contain any stale data. 
    * Devices
          o DMA to/from devices should be quiesced. 
    * The boot loader is expected to call the kernel image by jumping directly to the first instruction of the kernel image.


大致就是以上条件了,请特别关注一下第一条,这个基本上就是U-Boot的go命令和bootm命令之间的本质区别所在了。先来看看bootm命令的实现,在common/cmd_bootm.c的第119行开始有:
  1. #ifdef CONFIG_PPC
  2. static boot_os_Fcn do_bootm_linux;
  3. #else
  4. extern boot_os_Fcn do_bootm_linux;
  5. #endif
复制代码

这里的预编译宏说明了,非 PPC体系结构的CPU的do_bootm_linux()函数都不是在这个文件内实现的(extern)。可想而知,这个函数的实现应该是和体系结构相关的,具体到arm体系结构的实现就是在lib_arm/armlinux.c这个文件当中。可以看到从lib_arm/armlinux.c中的第77 行开始就是do_bootm_linux()函数的实现。

其中第85行声明了这样一个函数指针theKernel:

  1. void (*theKernel)(int zero, int arch, uint params);
复制代码

看看它的名字和参数的命名我们也可以猜到这个其实就是内核的入口函数的指针了。几个参数的命名也说明了上文提到的ARM Linux内核启动要求的第一条,因为根据ACPS(ARM/Thumb Procedure Call Standard)的规定,这三个参数就是依次使用r0,r1和r2来传递的。

接下来第93行就是给这个函数指针赋值:

  1. theKernel = (void (*)(int, int, uint))ntohl(hdr->ih_ep);
复制代码

可以看到theKernel被赋值为hdr->ih_ep,这个hdr是指使用tools/mkimage工具程序制作uImage时加在linux.bin.gz前面的一个头部,而ih_ep结构体成员保存的就是使用mkimage时指定的-e参数的值,即内核的入口点(Entry Point)。知道了hdr->ih_ep的意义之后,给theKernel赋这个值也就是理所当然的了。

最后是对内核入口函数的调用,发生在第270行:

  1. theKernel (0, bd->bi_arch_number, bd->bi_boot_params);
复制代码

调用的时候对参数进行赋值,r0=0,r1=bd->bi_arch_number,r2=bd->bi_boot_params,一个都不少。至此U-Boot的使命完成,开始进入ARM Linux的美丽新世界。

====================================================================

要知道哪个地址是启动内核,哪个地址启动文件系统,要分析common/cmd_bootm.c中的函数do_bootm,因为引导kernel就是bootm这条命令的工作,do_bootm是命令bootm的执行函数。

现在我们来分析一下common/cmd_bootm.c中的函数do_bootm,这是bootm命令的处理函数。
  1. ……

  2. image_header_t header;

  3. ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */

  4. int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  5. {
  6. ulong iflag;
  7. ulong addr;
  8. ulong data, len, checksum;
  9. ulong *len_ptr;
  10. uint unc_len = 0x400000;
  11. int i, verify;
  12. char *name, *s;
  13. int (*appl)(int, char *[]);
  14. image_header_t *hdr = &header;
复制代码


读取uboot的环境变量verify,如果环境变量verify等于’n’,则局部变量verify赋值成为0;如果环境变量verify为空(即没有定义环境变量verify)或者环境变量verify不等于’n’,则局部变量verify赋值成为1。
  1. s = getenv ("verify");
  2. verify = (s && (*s == 'n')) ? 0 : 1;
复制代码

如果参数个数小于2(即只是输入了bootm),使用缺省加载地址CFG_LOAD_ADDR;否则使用第二个参数作为加载地址。
  1. if (argc < 2) {
  2. addr = load_addr;
  3. } else {
  4. addr = simple_strtoul(argv[1], NULL, 16);
  5. }

  6. SHOW_BOOT_PROGRESS (1);
  7. printf ("## Booting image at %08lx ...\n", addr);
复制代码

将mkimage添加到映象文件头部的64字节提取到image_header_t 结构变量header中。
/* Copy header so we can blank CRC field for re-calculation */
定义了CONFIG_HAS_DATAFLASH,表示系统中存在ATMEL的数据Flash。
  1. #ifdef CONFIG_HAS_DATAFLASH
  2. if (addr_dataflash(addr)){
  3. read_dataflash(addr, sizeof(image_header_t), (char *)&header);
  4. } else
  5. #endif
  6. memmove (&header, (char *)addr, sizeof(image_header_t));
复制代码


判断image header的magic是否匹配,如果不匹配,说明下载过程中发生了错误。
  1. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  2. #ifdef __I386__ /* correct image format not implemented yet - fake it */
  3. if (fake_header(hdr, (void*)addr, -1) != NULL) {
  4. /* to compensate for the addition below */
  5. addr -= sizeof(image_header_t);
  6. /* turnof verify,
  7. * fake_header() does not fake the data crc
  8. */
  9. verify = 0;
  10. } else
  11. #endif /* __I386__ */
  12. {
  13. puts ("Bad Magic Number\n");
  14. SHOW_BOOT_PROGRESS (-1);
  15. return 1;
  16. }
  17. }
  18. SHOW_BOOT_PROGRESS (2);
复制代码


校验image header的CRC以及image data的CRC,如果校验不匹配,说明下载过程中发生了错误。
  1. data = (ulong)&header;
  2. len = sizeof(image_header_t);

  3. checksum = ntohl(hdr->ih_hcrc);
  4. hdr->ih_hcrc = 0;

  5. if (crc32 (0, (char *)data, len) != checksum) {
  6. puts ("Bad Header Checksum\n");
  7. SHOW_BOOT_PROGRESS (-2);
  8. return 1;
  9. }
  10. SHOW_BOOT_PROGRESS (3);

  11. /* for multi-file images we need the data part, too */
  12. print_image_hdr ((image_header_t *)addr);

  13. data = addr + sizeof(image_header_t);
  14. len = ntohl(hdr->ih_size);

  15. #ifdef CONFIG_HAS_DATAFLASH
  16. if (addr_dataflash(addr)){
  17. read_dataflash(data, len, (char *)CFG_LOAD_ADDR);
  18. data = CFG_LOAD_ADDR;
  19. }
  20. #endif

  21. if (verify) {
  22. puts (" Verifying Checksum ... ");
  23. if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
  24. printf ("Bad Data CRC\n");
  25. SHOW_BOOT_PROGRESS (-3);
  26. return 1;
  27. }
  28. puts ("OK\n");
  29. }
  30. SHOW_BOOT_PROGRESS (4);
复制代码


判断体系结构。
  1. len_ptr = (ulong *)data;

  2. #if defined(__PPC__)
  3. if (hdr->ih_arch != IH_CPU_PPC)
  4. #elif defined(__ARM__)
  5. if (hdr->ih_arch != IH_CPU_ARM)
  6. #elif defined(__I386__)
  7. if (hdr->ih_arch != IH_CPU_I386)
  8. #elif defined(__mips__)
  9. if (hdr->ih_arch != IH_CPU_MIPS)
  10. #elif defined(__nios__)
  11. if (hdr->ih_arch != IH_CPU_NIOS)
  12. #elif defined(__M68K__)
  13. if (hdr->ih_arch != IH_CPU_M68K)
  14. #elif defined(__microblaze__)
  15. if (hdr->ih_arch != IH_CPU_MICROBLAZE)
  16. #else
  17. # error Unknown CPU type
  18. #endif
  19. {
  20. printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
  21. SHOW_BOOT_PROGRESS (-4);
  22. return 1;
  23. }
  24. SHOW_BOOT_PROGRESS (5);
复制代码


判断image类型。
  1. switch (hdr->ih_type) {
  2. case IH_TYPE_STANDALONE:
  3. name = "Standalone Application";
  4. /* A second argument overwrites the load address */
  5. if (argc > 2) {
  6. hdr->ih_load = simple_strtoul(argv[2], NULL, 16);
  7. }
  8. break;
  9. case IH_TYPE_KERNEL:
  10. name = "Kernel Image";
  11. break;
  12. case IH_TYPE_MULTI:
  13. name = "Multi-File Image";
  14. len = ntohl(len_ptr[0]);
  15. /* OS kernel is always the first image */
  16. data += 8; /* kernel_len + terminator */
  17. for (i=1; len_ptr[i]; ++i)
  18. data += 4;
  19. break;
  20. default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
  21. SHOW_BOOT_PROGRESS (-5);
  22. return 1;
  23. }
  24. SHOW_BOOT_PROGRESS (6);

  25. /*
  26. * We have reached the point of no return: we are going to
  27. * overwrite all exception vector code, so we cannot easily
  28. * recover from any failures any more...
  29. */

  30. iflag = disable_interrupts();

  31. #ifdef CONFIG_AMIGAONEG3SE
  32. /*
  33. * We've possible left the caches enabled during
  34. * bios emulation, so turn them off again
  35. */
  36. icache_disable();
  37. invalidate_l1_instruction_cache();
  38. flush_data_cache();
  39. dcache_disable();
  40. #endif
复制代码


判断image压缩类型
  1. switch (hdr->ih_comp) {
  2. case IH_COMP_NONE: 没有压缩
  3. if(ntohl(hdr->ih_load) == addr) { 如果image header中指示的加载地址和bootm命令中参数2指定的地址相同,则表示不需要copy,可以就地执行。
  4. printf (" XIP %s ... ", name);
  5. } else {
  6. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  7. size_t l = len;
  8. void *to = (void *)ntohl(hdr->ih_load);
  9. void *from = (void *)data;

  10. printf (" Loading %s ... ", name);

  11. while (l > 0) {
  12. size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
  13. WATCHDOG_RESET();
  14. memmove (to, from, tail);
  15. to += tail;
  16. from += tail;
  17. l -= tail;
  18. }
  19. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
复制代码

如果image header中指示的加载地址和bootm命令中参数2指定的地址不相同,则表示要从image header中指示的加载地址处把image data copy到bootm命令中参数2指定的地址处,然后再执行。
  1. memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
  2. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  3. }
  4. break;
  5. case IH_COMP_GZIP:
  6. printf (" Uncompressing %s ... ", name);
  7. if (gunzip ((void *)ntohl(hdr->ih_load), unc_len,
  8. (uchar *)data, (int *)&len) != 0) {
  9. puts ("GUNZIP ERROR - must RESET board to recover\n");
  10. SHOW_BOOT_PROGRESS (-6);
  11. do_reset (cmdtp, flag, argc, argv);
  12. }
  13. break;
  14. #ifdef CONFIG_BZIP2
  15. case IH_COMP_BZIP2:
  16. printf (" Uncompressing %s ... ", name);
  17. /*
  18. * If we've got less than 4 MB of malloc() space,
  19. * use slower decompression algorithm which requires
  20. * at most 2300 KB of memory.
  21. */
  22. i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load),
  23. &unc_len, (char *)data, len,
  24. CFG_MALLOC_LEN < (4096 * 1024), 0);
  25. if (i != BZ_OK) {
  26. printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
  27. SHOW_BOOT_PROGRESS (-6);
  28. udelay(100000);
  29. do_reset (cmdtp, flag, argc, argv);
  30. }
  31. break;
  32. #endif /* CONFIG_BZIP2 */
  33. default:
  34. if (iflag)
  35. enable_interrupts();
  36. printf ("Unimplemented compression type %d\n", hdr->ih_comp);
  37. SHOW_BOOT_PROGRESS (-7);
  38. return 1;
  39. }
  40. puts ("OK\n");
  41. SHOW_BOOT_PROGRESS (7);
复制代码


根据image 执行type来决定如何引导。

  1. switch (hdr->ih_type) {
  2. case IH_TYPE_STANDALONE:
  3. if (iflag)
  4. enable_interrupts();

  5. /* load (and uncompress), but don't start if "autostart"
  6. * is set to "no"
  7. */
  8. if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
  9. char buf[32];
  10. sprintf(buf, "%lX", len);
  11. setenv("filesize", buf);
  12. return 0;
  13. }
  14. appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep);
  15. (*appl)(argc-1, &argv[1]);
  16. return 0;
  17. case IH_TYPE_KERNEL:
  18. case IH_TYPE_MULTI:
  19. /* handled below */
  20. break; 下面将有代码专门处理这两种image类型
  21. default:
  22. if (iflag)
  23. enable_interrupts();
  24. printf ("Can't boot image type %d\n", hdr->ih_type);
  25. SHOW_BOOT_PROGRESS (-8);
  26. return 1;
  27. }
  28. SHOW_BOOT_PROGRESS (8);
复制代码


根据image 的OS type来决定如何引导
  1. switch (hdr->ih_os) {
  2. default: /* handled by (original) Linux case */
  3. case IH_OS_LINUX:
  4. #ifdef CONFIG_SILENT_CONSOLE
  5. fixup_silent_linux();
  6. #endif
  7. do_bootm_linux (cmdtp, flag, argc, argv,
  8. addr, len_ptr, verify);
  9. break;
  10. case IH_OS_NETBSD:
  11. do_bootm_netbsd (cmdtp, flag, argc, argv,
  12. addr, len_ptr, verify);
  13. break;

  14. #ifdef CONFIG_LYNXKDI
  15. case IH_OS_LYNXOS:
  16. do_bootm_lynxkdi (cmdtp, flag, argc, argv,
  17. addr, len_ptr, verify);
  18. break;
  19. #endif

  20. case IH_OS_RTEMS:
  21. do_bootm_rtems (cmdtp, flag, argc, argv,
  22. addr, len_ptr, verify);
  23. break;

  24. #if (CONFIG_COMMANDS & CFG_CMD_ELF)
  25. case IH_OS_VXWORKS:
  26. do_bootm_vxworks (cmdtp, flag, argc, argv,
  27. addr, len_ptr, verify);
  28. break;
  29. case IH_OS_QNX:
  30. do_bootm_qnxelf (cmdtp, flag, argc, argv,
  31. addr, len_ptr, verify);
  32. break;
  33. #endif /* CFG_CMD_ELF */
  34. #ifdef CONFIG_ARTOS
  35. case IH_OS_ARTOS:
  36. do_bootm_artos (cmdtp, flag, argc, argv,
  37. addr, len_ptr, verify);
  38. break;
  39. #endif
  40. }

  41. SHOW_BOOT_PROGRESS (-9);
  42. #ifdef DEBUG
  43. puts ("\n## Control returned to monitor - resetting...\n");
  44. do_reset (cmdtp, flag, argc, argv);
  45. #endif
  46. return 1;
  47. }
复制代码

bootm命令是用来引导经过u-boot的工具mkimage打包后的kernel image的,什么叫做经过u-boot的工具mkimage打包后的kernel image,这个就要看mkimage的代码,看看它做了些什么,虽然我很希望大家不要偷懒,认真地去看看,但是我知道还是有很多人懒得去做这件,那么我就j将分析mkimage代码后得到的总结告诉大家,mkimage做了些什么,怎么用这个工具。

mkimage的用法
uboot源代码的tools/目录下有mkimage工具,这个工具可以用来制作不压缩或者压缩的多种可启动映象文件。

mkimage在制作映象文件的时候,是在原来的可执行映象文件的前面加上一个0x40字节的头,记录参数所指定的信息,这样uboot才能识别这个映象是针对哪个CPU体系结构的,哪个OS的,哪种类型,加载内存中的哪个位置, 入口点在内存的那个位置以及映象名是什么
root@Glym:/tftpboot# ./mkimage
Usage: ./mkimage -l image
-l ==> list image header information
./mkimage -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image
-A ==> set architecture to 'arch'
-O ==> set operating system to 'os'
-T ==> set image type to 'type'
-C ==> set compression type 'comp'
-a ==> set load address to 'addr' (hex)
-e ==> set entry point to 'ep' (hex)
-n ==> set image name to 'name'
-d ==> use image data from 'datafile'
-x ==> set XIP (execute in place)

参数说明:

-A 指定CPU的体系结构:

取值 表示的体系结构
alpha Alpha
arm A RM
x86 Intel x86
ia64 IA64
mips MIPS
mips64 MIPS 64 Bit
ppc PowerPC
s390 IBM S390
sh SuperH
sparc SPARC
sparc64 SPARC 64 Bit
m68k MC68000

-O 指定操作系统类型,可以取以下值:
openbsd、netbsd、freebsd、4_4bsd、linux、svr4、esix、solaris、irix、sco、dell、ncr、lynxos、vxworks、psos、qnx、u-boot、rtems、artos

-T 指定映象类型,可以取以下值:
standalone、kernel、ramdisk、multi、firmware、script、filesystem

-C 指定映象压缩方式,可以取以下值:
none 不压缩
gzip 用gzip的压缩方式
bzip2 用bzip2的压缩方式

-a 指定映象在内存中的加载地址,映象下载到内存中时,要按照用mkimage制作映象时,这个参数所指定的地址值来下载

-e 指定映象运行的入口点地址,这个地址就是-a参数指定的值加上0x40(因为前面有个mkimage添加的0x40个字节的头)

-n 指定映象名

-d 指定制作映象的源文件







本文轉自: http://blog.chinaunix.net/u/17660/showart_279896.html
原创粉丝点击