Chinaunix首页 | 论坛 | 博客
  • 博客访问: 363017
  • 博文数量: 135
  • 博客积分: 425
  • 博客等级: 下士
  • 技术积分: 599
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-19 21:10
文章分类
文章存档

2014年(3)

2013年(79)

2012年(53)

分类:

2013-08-04 13:30:31

原文地址:start_armboot分析 作者:siasca

start_armboot()函数的主要作用,其实该函数就是进行一系列的硬件初始化,然后进入main_loop,等待用户的命令,
 
typedef int (init_fnc_t) (void);   //define init_fnc_t  type
init_fnc_t *init_sequence[] = { //define point array init_sequence type's pionter
 cpu_init,  /* basic cpu dependent setup */
 board_init,  /* basic board dependent setup */
 interrupt_init,  /* set up exceptions */
 env_init,  /* initialize environment */
 init_baudrate,  /* initialze baudrate settings */
 serial_init,  /* serial communications setup *///串口初始化后我们就可以打印信息了
 console_init_f,  /* stage 1 init of console */
 display_banner,  /* say that we are here */
 dram_init,  /* configure available RAM banks */
 display_dram_config,
#if defined(CONFIG_VCMA9)
 checkboard,
#endif
 NULL,
};
 
void start_armboot (void)
{
 DECLARE_GLOBAL_DATA_PTR;
 ulong size;
 init_fnc_t **init_fnc_ptr;  ///point initial function's pointer
 char *s;
#if defined(CONFIG_VFD)
 unsigned long addr;
#endif
 
#if CFG_LED_FLASH
LED();    ///all leds power on flash once
#endif
 
 /* Pointer is writable since we allocated a register for it */

 gd = (gd_t*)(_armboot_start - CFG_MALLOC_LEN - sizeof(gd_t));
 memset ((void*)gd, 0, sizeof (gd_t));
 gd->bd = (bd_t*)((char*)gd - sizeof(bd_t));
 memset (gd->bd, 0, sizeof (bd_t));
 monitor_flash_len = _bss_start - _armboot_start;
 
///This for{} init arry init_sequence[] paraments one by one
 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
  if ((*init_fnc_ptr)() != 0) {
   hang ();///f init fail output "error" and go to dead cycle,if that we must reset cpu
  }
 }
 
 /* configure available FLASH banks */
 size = flash_init ();                     ///init flash  source code don't understand???
 display_flash_config (size);      /// print flash's config information
#ifdef CONFIG_VFD     ///#define CONFIG_VFD  1     in Trab.h
# ifndef PAGE_SIZE
#   define PAGE_SIZE 4096  //若没定义则用4096
# endif

 /*
  * reserve memory for VFD display (always full pages)
  */
 /* armboot_end is defined in the board-specific linker script */
 addr = (_bss_start + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
 size = vfd_setmem (addr);
 gd->fb_base = addr;
#endif /* CONFIG_VFD */
 /* armboot_start is defined in the board-specific linker script */
 mem_malloc_init (_armboot_start - CFG_MALLOC_LEN);
#if (CONFIG_COMMANDS & CFG_CMD_NAND)
 puts ("NAND:");
 nand_init();  /* go init the NAND */
#endif
#ifdef CONFIG_HAS_DATAFLASH
 AT91F_DataflashInit();
 dataflash_print_info();
#endif
 
 /* initialize environment */
 env_relocate ();
 
#ifdef CONFIG_VFD
 /* must do this after the framebuffer is allocated */
 drv_vfd_init();
#endif /* CONFIG_VFD */
 
 /* IP Address */
 gd->bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
 /* MAC Address */
 {
  int i;
  ulong reg;
  char *s, *e;
  uchar tmp[64];
  i = getenv_r ("ethaddr", tmp, sizeof (tmp));
  s = (i > 0) ? tmp : NULL;
  for (reg = 0; reg < 6; ++reg) {
   gd->bd->bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;
   if (s)
    s = (*e) ? e + 1 : e;
  }
 }
 
 devices_init (); /* get the devices list going. */
 jumptable_init ();
 console_init_r (); /* fully init console as a device */
 
#if defined(CONFIG_MISC_INIT_R)
 /* miscellaneous platform dependent initialisations */
 misc_init_r ();
#endif
 
 /* enable exceptions */
 enable_interrupts ();
 /* Perform network card initialisation if necessary */
#ifdef CONFIG_DRIVER_CS8900
 cs8900_get_enetaddr (gd->bd->bi_enetaddr);
#endif
#ifdef CONFIG_DRIVER_LAN91C96
 if (getenv ("ethaddr")) {
  smc_set_mac_addr(gd->bd->bi_enetaddr);
 }
 /* eth_hw_init(); */
#endif /* CONFIG_DRIVER_LAN91C96 */
 /* Initialize from environment */
 if ((s = getenv ("loadaddr")) != NULL) {
  load_addr = simple_strtoul (s, NULL, 16);
 }
#if (CONFIG_COMMANDS & CFG_CMD_NET)
 if ((s = getenv ("bootfile")) != NULL) {
  copy_filename (BootFile, s, sizeof (BootFile));
 }
#endif /* CFG_CMD_NET */
#ifdef BOARD_LATE_INIT
 board_late_init ();
#endif
 /* main_loop() can return to retry autoboot, if so just run it again. */
 for (;;) {
  main_loop ();
 }
 /* NOTREACHED - no way out of command loop except booting */
}
 
void hang (void)
{
 puts ("### ERROR ### Please RESET the board ###\n");
 for (;;);
}
 
 
#ifdef CONFIG_MODEM_SUPPORT
/* called from main loop (common/main.c) */
extern void  dbg(const char *fmt, ...);

int mdm_init (void)
{
 char env_str[16];
 char *init_str;
 int i;
 extern char console_buffer[];
 static inline void mdm_readline(char *buf, int bufsiz);
 extern void enable_putc(void);
 extern int hwflow_onoff(int);
 enable_putc(); /* enable serial_putc() */
#ifdef CONFIG_HWFLOW
 init_str = getenv("mdm_flow_control");
 if (init_str && (strcmp(init_str, "rts/cts") == 0))
  hwflow_onoff (1);
 else
  hwflow_onoff(-1);
#endif
 for (i = 1;;i++) {
  sprintf(env_str, "mdm_init%d", i);
  if ((init_str = getenv(env_str)) != NULL) {
   serial_puts(init_str);
   serial_puts("\n");
   for(;;) {
    mdm_readline(console_buffer, CFG_CBSIZE);
    dbg("ini%d: [%s]", i, console_buffer);
    if ((strcmp(console_buffer, "OK") == 0) ||
     (strcmp(console_buffer, "ERROR") == 0)) {
     dbg("ini%d: cmd done", i);
     break;
    } else /* in case we are originating call ... */
     if (strncmp(console_buffer, "CONNECT", 7) == 0) {
      dbg("ini%d: connect", i);
      return 0;
     }
   }
  } else
   break; /* no init string - stop modem init */
  udelay(100000);
 }
 udelay(100000);
 /* final stage - wait for connect */
 for(;i > 1;) { /* if 'i' > 1 - wait for connection
      message from modem */
  mdm_readline(console_buffer, CFG_CBSIZE);
  dbg("ini_f: [%s]", console_buffer);
  if (strncmp(console_buffer, "CONNECT", 7) == 0) {
   dbg("ini_f: connected");
   return 0;
  }
 }
 return 0;
}
/* 'inline' - We have to do it fast */
static inline void mdm_readline(char *buf, int bufsiz)
{
 char c;
 char *p;
 int n;
 n = 0;
 p = buf;
 for(;;) {
  c = serial_getc();
  /*  dbg("(%c)", c); */
  switch(c) {
  case '\r':
   break;
  case '\n':
   *p = '\0';
   return;
  default:
   if(n++ > bufsiz) {
    *p = '\0';
    return; /* sanity check */
   }
   *p = c;
   p++;
   break;
  }
 }
}
#endif /* CONFIG_MODEM_SUPPORT */
 
 
 
在介绍该函数之前,我们需要看一看几个数据结构,这些是u-boot中几个重要的数据结构:

1)、gd_t该数据结构保存了u-boot需要的配置信息,注释简单明了
typedef    struct    global_data {
    bd_t        *bd; //与板子相关的结构,见下面
    unsigned long    flags;
    unsigned long    baudrate;
    unsigned long    have_console;    /* serial_init() was called */
    unsigned long    reloc_off;    /* Relocation Offset */
    unsigned long    env_addr;    /* Address  of Environment struct */
    unsigned long    env_valid;    /* Checksum of Environment valid? */
#ifdef CONFIG_VFD  //我们一般没有配置这个,这个是frame buffer的首地址
    unsigned long    fb_base;    /* base address of frame buffer */
#endif
} gd_t;

2)、bd_t 保存与板子相关的配置参数
typedef struct bd_info {
    int            bi_baudrate;    /* serial console baudrate */
    unsigned long    bi_ip_addr;    /* IP Address */
    unsigned char    bi_enetaddr[6]; /* Ethernet adress */
    struct environment_s           *bi_env;
    ulong            bi_arch_number;    /* unique id for this board */
    ulong            bi_boot_params;    /* where this board expects params */
    struct                /* RAM configuration */
    {
    ulong start;
    ulong size;
    }             bi_dram[CONFIG_NR_DRAM_BANKS];//在我的板子上是1个
} bd_t;

现在我贴出start_armboot ()的源代码,然后具体的在其中解释一些代码的作用:


void start_armboot (void)
{
    DECLARE_GLOBAL_DATA_PTR;

    ulong size;
    gd_t gd_data;
    bd_t bd_data;
    init_fnc_t **init_fnc_ptr; //这个是函数的指针,指向一些硬件初始化的函数,见下面
   
                    //init_fnc_t *init_sequence[] = {
                        //    cpu_init,        /* basic cpu dependent setup */
                        //    board_init,        /* basic board dependent setup */
                        //    interrupt_init,        /* set up exceptions */
                        //    env_init,        /* initialize environment */
                        //    init_baudrate,        /* initialze baudrate settings */
                       //     serial_init,        /* serial communications setup */
                       //     display_banner,
                       //     dram_init,        /* configure available RAM banks */
                       //     display_dram_config,
                       //     NULL,
                       //     };

    //printf("**********Start *************\n");
    /* Pointer is writable since we allocated a register for it */
    gd = &gd_data;
    memset (gd, 0, sizeof (gd_t));//初始化为0
    gd->bd = &bd_data;
    memset (gd->bd, 0, sizeof (bd_t));//初始化为0

//注意,下面的循环是依次调用各个硬件初始化函数,顺序见上面的的数组,我们在下一篇中依次解释每种硬//件的初始化,第六个就是串口的初始化,这时候我们就可以通过串口输出信息了
    for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
        if ((*init_fnc_ptr)() != 0) {
            hang ();      //如果不成功的话,就“输出信息,然后就进入死循环”
        }
    }

    /* configure available FLASH banks */
    size = flash_init ();  //flash的初始化
    display_flash_config (size);//打印flash的配置信息

    /* initialize environment */
    env_relocate ();//环境的初始化,代码在common\env_common.c中

    /* IP Address */
    bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");//读取IP地址,保存到bd_t数据结构中

    /* MAC Address */ //MAC地址的初始化
    {
        int i;
        ulong reg;
        char *s, *e;
        uchar tmp[64];

        i = getenv_r ("ethaddr", tmp, sizeof (tmp));
        s = (i > 0) ? tmp : NULL;

        for (reg = 0; reg < 6; ++reg) {
            bd_data.bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;
            if (s)
                s = (*e) ? e + 1 : e;
        }
    }


    /* enable exceptions */  //允许中断
    enable_interrupts ();

#ifdef CONFIG_DRIVER_CS8900  //配置网卡
    if (!getenv ("ethaddr")) {
        cs8900_get_enetaddr (gd->bd->bi_enetaddr);
    } 
#endif

//直接进入main_loop 该函数在common\main.c中,至此,硬件初始化完成
    /* main_loop() can return to retry autoboot, if so just run it again. */
    for (;;) {
        main_loop ()
    }

    /* NOTREACHED - no way out of command loop except booting */
}

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