S&T的笔记 https://passport2.21ic.com/?600909 [收藏] [复制] [RSS] science and technology!

日志

U-Boot启动过程完全分析(7)

已有 1129 次阅读2011-12-16 03:21 |个人分类:ARM|系统分类:ARM| U-Boot

1.1.3             U-Boot启动Linux过程


       U-Boot使用标记列表(tagged list)的方式向Linux传递参数。标记的数据结构式是tag,在U-Boot源代码目录include/asm-arm/setup.h中定义如下:


struct tag_header {


       u32 size;       /* 表示tag数据结构的联合u实质存放的数据的大小*/


       u32 tag;        /* 表示标记的类型 */


};


 


struct tag {


       struct tag_header hdr;


       union {


              struct tag_core           core;


              struct tag_mem32      mem;


              struct tag_videotext   videotext;


              struct tag_ramdisk     ramdisk;


              struct tag_initrd  initrd;


              struct tag_serialnr       serialnr;


              struct tag_revision      revision;


              struct tag_videolfb     videolfb;


              struct tag_cmdline     cmdline;


 


              /*


               * Acorn specific


               */


              struct tag_acorn  acorn;


              /*


               * DC21285 specific


               */


              struct tag_memclk      memclk;


       } u;


};


       U-Boot使用命令bootm来启动已经加载到内存中的内核。而bootm命令实际上调用的是do_bootm函数。对于Linux内核,do_bootm函数会调用do_bootm_linux函数来设置标记列表和启动内核。do_bootm_linux函数在lib_arm/bootm.c 中定义如下:


59   int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)


60   {


61       bd_t       *bd = gd->bd;


62       char       *s;


63       int   machid = bd->bi_arch_number;


64       void       (*theKernel)(int zero, int arch, uint params);


65  


66   #ifdef CONFIG_CMDLINE_TAG


67       char *commandline = getenv ("bootargs");   /* U-Boot环境变量bootargs */


68   #endif


       … …


73       theKernel = (void (*)(int, int, uint))images->ep; /* 获取内核入口地址 */


       … …


86   #if defined (CONFIG_SETUP_MEMORY_TAGS) || \


87       defined (CONFIG_CMDLINE_TAG) || \


88       defined (CONFIG_INITRD_TAG) || \


89       defined (CONFIG_SERIAL_TAG) || \


90       defined (CONFIG_REVISION_TAG) || \


91       defined (CONFIG_LCD) || \


92       defined (CONFIG_VFD)


93       setup_start_tag (bd);                                     /* 设置ATAG_CORE标志 */


       … …


100  #ifdef CONFIG_SETUP_MEMORY_TAGS


101      setup_memory_tags (bd);                             /* 设置内存标记 */


102  #endif


103  #ifdef CONFIG_CMDLINE_TAG


104      setup_commandline_tag (bd, commandline);      /* 设置命令行标记 */


105  #endif


       … …


113      setup_end_tag (bd);                               /* 设置ATAG_NONE标志 */          


114  #endif


115 


116      /* we assume that the kernel is in place */


117      printf ("\nStarting kernel ...\n\n");


       … …


126      cleanup_before_linux ();          /* 启动内核前对CPU作最后的设置 */


127 


128      theKernel (0, machid, bd->bi_boot_params);      /* 调用内核 */


129      /* does not return */


130 


131      return 1;


132  }


       其中的setup_start_tagsetup_memory_tagssetup_end_tag函数在lib_arm/bootm.c中定义如下:


       1setup_start_tag函数


static void setup_start_tag (bd_t *bd)


{


       params = (struct tag *) bd->bi_boot_params;  /* 内核的参数的开始地址 */


 


       params->hdr.tag = ATAG_CORE;


       params->hdr.size = tag_size (tag_core);


 


       params->u.core.flags = 0;


       params->u.core.pagesize = 0;


       params->u.core.rootdev = 0;


 


       params = tag_next (params);


}


       标记列表必须以ATAG_CORE开始,setup_start_tag函数在内核的参数的开始地址设置了一个ATAG_CORE标记。


       2setup_memory_tags函数


static void setup_memory_tags (bd_t *bd)


{


       int i;


/*设置一个内存标记 */


       for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {   


              params->hdr.tag = ATAG_MEM;


              params->hdr.size = tag_size (tag_mem32);


 


              params->u.mem.start = bd->bi_dram.start;


              params->u.mem.size = bd->bi_dram.size;


 


              params = tag_next (params);


       }


}


       setup_memory_tags函数设置了一个ATAG_MEM标记,该标记包含内存起始地址,内存大小这两个参数。


       3setup_end_tag函数


static void setup_end_tag (bd_t *bd)


{


       params->hdr.tag = ATAG_NONE;


       params->hdr.size = 0;


}


       标记列表必须以标记ATAG_NONE结束,setup_end_tag函数设置了一个ATAG_NONE标记,表示标记列表的结束。


       U-Boot设置好标记列表后就要调用内核了。但调用内核前,CPU必须满足下面的条件:


(1)    CPU寄存器的设置


Ø  r0=0


Ø  r1=机器码


Ø  r2=内核参数标记列表在RAM中的起始地址


(2)    CPU工作模式


Ø  禁止IRQFIQ中断


Ø  CPUSVC模式


(3)    使数据Cache与指令Cache失效


       do_bootm_linux中调用的cleanup_before_linux函数完成了禁止中断和使Cache失效的功能。cleanup_before_linux函数在cpu/arm920t/cpu.中定义:


int cleanup_before_linux (void)


{


       /*


        * this  is called just before we call linux


        * it prepares the processor for linux


        *


        * we turn off caches etc ...


        */


 


       disable_interrupts ();         /* 禁止FIQ/IRQ中断 */


 


       /* turn off I/D-cache */


       icache_disable();               /* 使指令Cache失效 */


       dcache_disable();              /* 使数据Cache失效 */


       /* flush I/D-cache */


       cache_flush();                    /* 刷新Cache */


 


       return 0;


}


       由于U-Boot启动以来就一直工作在SVC模式,因此CPU的工作模式就无需设置了。


do_bootm_linux中:


64       void       (*theKernel)(int zero, int arch, uint params);


… …


73       theKernel = (void (*)(int, int, uint))images->ep;


… …


128      theKernel (0, machid, bd->bi_boot_params);


       73行代码将内核的入口地址“images->ep”强制类型转换为函数指针。根据ATPCS规则,函数的参数个数不超过4个时,使用r0~r34个寄存器来传递参数。因此第128行的函数调用则会将0放入r0,机器码machid放入r1,内核参数地址bd->bi_boot_params放入r2,从而完成了寄存器的设置,最后转到内核的入口地址。


       到这里,U-Boot的工作就结束了,系统跳转到Linux内核代码执行。


1.1.4             U-Boot添加命令的方法及U-Boot命令执行过程


       下面以添加menu命令(启动菜单)为例讲解U-Boot添加命令的方法。


(1)    建立common/cmd_menu.c


       习惯上通用命令源代码放在common目录下,与开发板专有命令源代码则放在board/<board_dir>目录下,并且习惯以“cmd_<命令名>.c”为文件名。


(2)    定义“menu”命令


       cmd_menu.c中使用如下的代码定义“menu”命令:


_BOOT_CMD(


       menu,    3,    0,    do_menu,


       "menu - display a menu, to select the items to do something\n",


       " - display a menu, to select the items to do something"


);


       其中U_BOOT_CMD命令格式如下:


U_BOOT_CMD(name,maxargs,rep,cmd,usage,help)


       各个参数的意义如下:


name:命令名,非字符串,但在U_BOOT_CMD中用“#”符号转化为字符串


maxargs:命令的最大参数个数


rep:是否自动重复(按Enter键是否会重复执行)


cmd:该命令对应的响应函数


usage:简短的使用说明(字符串)


help:较详细的使用说明(字符串)


       在内存中保存命令的help字段会占用一定的内存,通过配置U-Boot可以选择是否保存help字段。若在include/configs/mini2440.h中定义了CONFIG_SYS_LONGHELP宏,则在U-Boot中使用help命令查看某个命令的帮助信息时将显示usagehelp字段的内容,否则就只显示usage字段的内容。


       U_BOOT_CMD宏在include/command.h中定义:


#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \


cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}


       ##”与“#”都是预编译操作符,“##”有字符串连接的功能,“#”表示后面紧接着的是一个字符串。


       其中的cmd_tbl_tinclude/command.h中定义如下:


struct cmd_tbl_s {


       char              *name;          /* 命令名 */


       int          maxargs;       /* 最大参数个数 */


       int          repeatable;    /* 是否自动重复 */


       int          (*cmd)(struct cmd_tbl_s *, int, int, char *[]);  /*  响应函数 */


       char              *usage;         /* 简短的帮助信息 */


#ifdef    CONFIG_SYS_LONGHELP


       char              *help;           /*  较详细的帮助信息 */


#endif


#ifdef CONFIG_AUTO_COMPLETE


       /* 自动补全参数 */


       int          (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);


#endif


};


typedef struct cmd_tbl_s  cmd_tbl_t;


       一个cmd_tbl_t结构体变量包含了调用一条命令的所需要的信息。


       其中Struct_Sectioninclude/command.h中定义如下:


#define Struct_Section  __attribute__ ((unused,section (".u_boot_cmd")))


       凡是带有__attribute__ ((unused,section (".u_boot_cmd"))属性声明的变量都将被存放在".u_boot_cmd"段中,并且即使该变量没有在代码中显式的使用编译器也不产生警告信息。


       U-Boot连接脚本u-boot.lds中定义了".u_boot_cmd"段:


       . = .;


       __u_boot_cmd_start = .;          /* __u_boot_cmd_start指定为当前地址 */


       .u_boot_cmd : { *(.u_boot_cmd) }


       __u_boot_cmd_end = .;           /*  __u_boot_cmd_end指定为当前地址  */


       这表明带有“.u_boot_cmd”声明的函数或变量将存储在“u_boot_cmd”段。这样只要将U-Boot所有命令对应的cmd_tbl_t变量加上“.u_boot_cmd”声明,编译器就会自动将其放在“u_boot_cmd”段,查找cmd_tbl_t变量时只要在__u_boot_cmd_start__u_boot_cmd_end之间查找就可以了。


       因此“menu”命令的定义经过宏展开后如下:


cmd_tbl_t __u_boot_cmd_menu __attribute__ ((unused,section (".u_boot_cmd"))) = {menu, 3, 0, do_menu, "menu - display a menu, to select the items to do something\n", " - display a menu, to select the items to do something"}


       实质上就是用U_BOOT_CMD宏定义的信息构造了一个cmd_tbl_t类型的结构体。编译器将该结构体放在“u_boot_cmd”段,执行命令时就可以在“u_boot_cmd”段查找到对应的cmd_tbl_t类型结构体。


(3)    实现命令的函数


       cmd_menu.c中添加“menu”命令的响应函数的实现。具体的实现代码略:


int do_menu (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])


{


       /* 实现代码略 */


}


(4)    common/cmd_menu.c编译进u-boot.bin


       common/Makefile中加入如下代码:


COBJS-$(CONFIG_BOOT_MENU) += cmd_menu.o


       include/configs/mini2440.h加入如代码:


#define CONFIG_BOOT_MENU 1


       重新编译下载U-Boot就可以使用menu命令了


5menu命令执行的过程


       U-Boot中输入“menu”命令执行时,U-Boot接收输入的字符串“menu”,传递给run_command函数。run_command函数调用common/command.c中实现的find_cmd函数在__u_boot_cmd_start__u_boot_cmd_end间查找命令,并返回menu命令的cmd_tbl_t结构。然后run_command函数使用返回的cmd_tbl_t结构中的函数指针调用menu命令的响应函数do_menu,从而完成了命令的执行。


 


   作者:heaad


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)