免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 802 | 回复: 0
打印 上一主题 下一主题

U-Boot启动分析C语言部分 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-10 23:23 |只看该作者 |倒序浏览
最近由于在做u-boot-1.1.4到深圳优龙的ST2410开发板的移植,想通过修改U-Boot的源代码来让它支持从NAND Flash的启动。现在移植已经做得有点眉目了,还存在一些问题。在这个过程中,最近遇到的问题是启动ARM Linux的必备条件和U-Boot的go命令和bootm命令的一些区别,有所体会,在此记录一下。
先来引用一下这篇介绍“ARM Linux内核启动要求”的文章

ARM Linux Kernel Boot Requirements
,是ARM Linux内核的维护者Russell King写的。

  • CPU register settings

    • r0 = 0.
    • r1 = machine type number.
    • r2 = physical address of tagged list in system RAM.

  • CPU mode

    • All forms of interrupts must be disabled (IRQs and FIQs.)
    • The CPU must be in SVC mode. (A special exception exists for Angel.)

  • Caches, MMUs

    • The MMU must be off.
    • Instruction cache may be on or off.
    • Data cache must be off and must not contain any stale data.

  • Devices

    • 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行开始有:
#ifdef CONFIG_PPC
static boot_os_Fcn do_bootm_linux;
#else
extern boot_os_Fcn do_bootm_linux;
#endif
这里的预编译宏说明了,非PPC体系结构的CPU的do_bootm_linux()函数都不是在这个文件内实现的(extern)。可想而知,这个函数的实现应该是和体系结构相关的,具体到arm体系结构的实现就是在lib_arm/armlinux.c这个文件当中。可以看到从lib_arm/armlinux.c中的第77行开始就是do_bootm_linux()函数的实现。

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

接下来第93行就是给这个函数指针赋值:
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行:
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的美丽新世界。
本文还是以u-boot-1.1.4为例,以make smdk2410_config命令配置源代码之后进行分析。
对于C语言部分代码的调用出现在cpu/arm920t/start.S的第223行:
  1.         ldr pc, _start_armboot
  2. _start_armboot: .word start_armboot
复制代码
这里的start_armboot就是lib_arm/board.c中第207行的start_armboot()函数,由此U-Boot开始执行C语言部分的代码。
start_armboot()函数一开始首先是一个宏调用:
  1. DECLARE_GLOBAL_DATA_PTR;
复制代码
这个宏的定义在include/asm-arm/global_data.h文件的第64行:
  1. #define DECLARE_GLOBAL_DATA_PTR     register volatile gd_t *gd asm ("r8")
复制代码
大概的意思是声明一个指向gd_t结构体变量的指针gd,并固定使用寄存器r8来存放该指针。而对gd_t结构体的定义从上面的第36行开始:
  1. typedef struct global_data {
  2.     bd_t  *bd;
  3.     unsigned long flags;
  4.     unsigned long baudrate;
  5.     unsigned long have_console; /* serial_init() was called */
  6.     unsigned long reloc_off; /* Relocation Offset */
  7.     unsigned long env_addr; /* Address  of Environment struct */
  8.     unsigned long env_valid; /* Checksum of Environment valid? */
  9.     unsigned long fb_base; /* base address of frame buffer */
  10. #ifdef CONFIG_VFD
  11.     unsigned char vfd_type; /* display type */
  12. #endif
  13. #if 0
  14.     unsigned long cpu_clk; /* CPU clock in Hz!  */
  15.     unsigned long bus_clk;
  16.     unsigned long ram_size; /* RAM size */
  17.     unsigned long reset_status; /* reset status register at boot */
  18. #endif
  19.     void  **jt;  /* jump table */
  20. } gd_t;
复制代码
在lib_arm/board.c文件中之所以能够直接使用这个宏和gd_t结构体是因为包含了include/common.h头文件,在common.h的第115行包含了include/asm-arm/global_data.h头文件。
继续来看start_armboot()函数的执行。lib_arm/board.c第229行开始的一个for循环:
  1. for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
  2.     if ((*init_fnc_ptr)() != 0) {
  3.         hang ();
  4.     }
  5. }
复制代码
这里的init_sequence是定义在上面第190行处的一个指针数组:
  1. init_fnc_t *init_sequence[] = {
  2.     cpu_init,  /* basic cpu dependent setup */
  3.     board_init,  /* basic board dependent setup */
  4.     interrupt_init,  /* set up exceptions */
  5.     env_init,  /* initialize environment */
  6.     init_baudrate,  /* initialze baudrate settings */
  7.     serial_init,  /* serial communications setup */
  8.     console_init_f,  /* stage 1 init of console */
  9.     display_banner,  /* say that we are here */
  10.     dram_init,  /* configure available RAM banks */
  11.     display_dram_config,
  12. #if defined(CONFIG_VCMA9) || defined (CONFIG_CMC_PU2)
  13.     checkboard,
  14. #endif
  15.     NULL,
  16. };
复制代码
数组类型init_fnc_t则是一个定义在第188行的函数指针类型:
  1. typedef int (init_fnc_t) (void);
复制代码
由此可知,init_sequence[]数组当中的所有元素都是函数指针了,而这个for循环的作用就是遍历这个数组的所有元素,然后用“(*init_fnc_ptr)()”就依次调用了这些函数来进行初始化的工作。
书接上回,我们依次来看看init_sequence[]数组当中的各个元素。

首先是cpu_init()函数,定义于lib_arm/arm920t/cpu.c第88行。

接下来的board_init()函数定义于board/smdk2410/smdk2410.c第68行:
int board_init (void)
{
    DECLARE_GLOBAL_DATA_PTR;
    S3C24X0_CLOCK_POWER * const clk_power = S3C24X0_GetBase_CLOCK_POWER();
    S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
    /* to reduce PLL lock time, adjust the LOCKTIME register */
    clk_power->LOCKTIME = 0xFFFFFF;
    /* configure MPLL */
    clk_power->MPLLCON = ((M_MDIV  12) + (M_PDIV  4) + M_SDIV);
    /* some delay between MPLL and UPLL */
    delay (4000);
    /* configure UPLL */
    clk_power->UPLLCON = ((U_M_MDIV  12) + (U_M_PDIV  4) + U_M_SDIV);
    /* some delay between MPLL and UPLL */
    delay (8000);
    /* set up the I/O ports */
    gpio->GPACON = 0x007FFFFF;
    gpio->GPBCON = 0x00044555;
    gpio->GPBUP = 0x000007FF;
    gpio->GPCCON = 0xAAAAAAAA;
    gpio->GPCUP = 0x0000FFFF;
    gpio->GPDCON = 0xAAAAAAAA;
    gpio->GPDUP = 0x0000FFFF;
    gpio->GPECON = 0xAAAAAAAA;
    gpio->GPEUP = 0x0000FFFF;
    gpio->GPFCON = 0x000055AA;
    gpio->GPFUP = 0x000000FF;
    gpio->GPGCON = 0xFF95FFBA;
    gpio->GPGUP = 0x0000FFFF;
    gpio->GPHCON = 0x002AFAAA;
    gpio->GPHUP = 0x000007FF;
    /* arch number of SMDK2410-Board */
    gd->bd->bi_arch_number = MACH_TYPE_SMDK2410;
    /* adress of boot parameters */
    gd->bd->bi_boot_params = 0x30000100;
    icache_enable();
    dcache_enable();
    return 0;
}
interrupt_init()函数定义于cpu/arm920t/s3c24x0/interrupts.c第55行:
int interrupt_init (void)
{
    S3C24X0_TIMERS * const timers = S3C24X0_GetBase_TIMERS();
    /* use PWM Timer 4 because it has no output */
    /* prescaler for Timer 4 is 16 */
    timers->TCFG0 = 0x0f00;
    if (timer_load_val == 0)
    {
        /*
         * for 10 ms clock period @ PCLK with 4 bit divider = 1/2
         * (default) and prescaler = 16. Should be 10390
         * @33.25MHz and 15625 @ 50 MHz
         */
        timer_load_val = get_PCLK()/(2 * 16 * 100);
    }
    /* load value for 10 ms timeout */
    lastdec = timers->TCNTB4 = timer_load_val;
    /* auto load, manual update of Timer 4 */
    timers->TCON = (timers->TCON & ~0x0700000) | 0x600000;
    /* auto load, start Timer 4 */
    timers->TCON = (timers->TCON & ~0x0700000) | 0x500000;
    timestamp = 0;
    return (0);
}



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45593/showart_357806.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP