uboot源码分析(1)uboot 命令解析流程简析

                                                                    uboot 命令解析流程简析

      uboot正常启动后,会调用main_loop(void)函数,进入main_loop()之后,如果在规定的时间(CONFIG_BOOTDELAY)内,没有检查到任何按键事件的发生,就会去加载OS,并启动系统,比如把linux内核压缩镜像从nand flash中读到sdram ,然后执行它。

     如果在CONFIG_BOOTDELAY时间内,用户按下键盘上的任意一个按键,uboot就会进入与用户交互的状态。如果用户在配置文件中定义了CONFIG_SYS_HUSH_PARSER,就会通过parse_file_outer(),去接收并解析用户命令,否则进入一个for(;;)循环中,通过

readline (CONFIG_SYS_PROMPT)接收用户命令,然后调用run_command(cmd,flag)去解析并执行命令。代码如下:  

void main_loop(void)

{ 

 

   ..............................

  #ifdef CONFIG_SYS_HUSH_PARSER
       parse_file_outer();
      /* This point is never reached */
     for (;;);
 #else
    for (;;)
   {
         #ifdef CONFIG_BOOT_RETRY_TIME
               if (rc >= 0) {
               /* Saw enough of a valid command to
              * restart the timeout.
              */
             reset_cmd_timeout();
           }
     #endif
     len = readline (CONFIG_SYS_PROMPT);
     printf("after readline:%d\n",len);
     flag = 0; /* assume no special flags for now */
    if (len > 0)
       strcpy (lastcommand, console_buffer);
    else if (len == 0)
      flag |= CMD_FLAG_REPEAT;
  #ifdef CONFIG_BOOT_RETRY_TIME
    else if (len == -2) {
   /* -2 means timed out, retry autoboot
    */
   puts ("\nTimed out waiting for command\n");
# ifdef CONFIG_RESET_TO_RETRY
   /* Reinit board to run initialization code again */
   do_reset (NULL, 0, 0, NULL);
# else
   return;  /* retry autoboot */
# endif
  }
#endif

  if (len == -1)
       puts ("<INTERRUPT>\n");
  else
      rc = run_command (lastcommand, flag);

 

.....................................

}

     如果定义了CONFIG_SYS_HUSH_PARSER,命令接收和解析讲采用busybox 中的hush(对应hush.c)工具来实现,与uboot原始的命令解析方法相比,该工具更加智能。这里主要讲uboot中基于hush的命令解析流程。不过hush的实现太过复杂 ,鉴于自己水平太次,只是简单追踪下流程。

      当在配置文件中定义了CONFIG_SYS_HUSH_PARSER,main_loop会调用parse_file_outer(),进入hush,然后里面是一大堆和hush相关的机制,暂时不做分析,最终会调用到hush中的run_pipe_real(struct pipe *pi),在该函数中经过一些列解析 ,最终会调用到对应的命令执行函数,代码如下:

static run_pipe_real(struct pipe *pi)
{

   ........................................

    rcode = (cmdtp->cmd)(cmdtp, flag,child->argc-i,&child->argv[i]);
    if ( !cmdtp->repeatable )
       flag_repeat = 0;

  ...................................
}

cmdtp 是对应命令的结构指针,cmd就是该命令对应的执行函数指针。在uboot中,对所有的命令,有一个cmd_tbl_t的结构,定义如下:

struct cmd_tbl_s

 {
     char  *name;  /* Command Name   */
     int  maxargs; /* maximum number of arguments */
    int  repeatable; /* autorepeat allowed?  */
    int  (*cmd)(struct cmd_tbl_s *, int, int, char *[]);   /* Implementation function */
   char  *usage;  /* Usage message (short) */
   #ifdef CONFIG_SYS_LONGHELP
     char  *help;  /* Help  message (long) */
   # endif
   #ifdef CONFIG_AUTO_COMPLETE
      /* do auto completion on the arguments */
       int  (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);
   #endif
};

typedef struct cmd_tbl_s cmd_tbl_t;

#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}

   对于uboot支持的每一个命令,是通过U_BOOT_CMD宏定义的,他定义了该命令对应的名称name,支持的最大参数rep,重复次数,实现函数cmd,以及输入help命令时,显示的帮助信息usage。例如,在cmd_nand中,定义nand命令的方法如下:

   U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,
 "NAND sub-system",
 "info - show available NAND devices\n"
 "nand device [dev] - show or set current device\n"
 "nand read - addr off|partition size\n"
 "nand write - addr off|partition size\n"
 "    read/write 'size' bytes starting at offset 'off'\n"
 "    to/from memory address 'addr', skipping bad blocks.\n"
 "nand erase [clean] [off size] - erase 'size' bytes from\n"
 "    offset 'off' (entire device if not specified)\n"
#if defined(ENABLE_CMD_NAND_YAFFS)
 "nand read[.yaffs[1]] is not provide temporarily!\n"
 "nand write[.yaffs[1]]    addr off size - write the `size' byte yaffs image starting\n"
 "     at offset `off' from memory address `addr' (.yaffs1 for 512+16 NAND)\n"
#endif
 "nand bad - show bad blocks\n"
 "nand dump[.oob] off - dump page\n"
 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
 "nand biterr off - make a bit error at offset (UNSAFE)"
#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
 "\n"
 "nand lock [tight] [status]\n"
 "    bring nand to lock state or display locked pages\n"
 "nand unlock [offset] [size] - unlock section"
#endif
);

    命令名称为nand,支持的最大参数个数为CONFIG_SYS_MAXARGS,命令重复次数为1,对应的实现函数为do_nand,后面的是用户输入nand -help时,将显示的帮助信息。

    在执行函数cmd中,第一个参数对应该命令结构本身的指针,第二个参数对应flag标记,第三个参数对应参数数目,第四个参数是指针数组,里面存储的是对应参数的指针。比如我们输入命令“nand read 30008000 0x80000 300000”,在run_pipe_real中,解析到该命令为nand ,有五个参数,这些参数对应的指针存储在argv数组中,nand命令对应的实现函数cmd为do_nand,在实现函数do_nand中,会从argv中取得对应的参数并解析,然后执行相应的命令。这些实现在do_nand函数中都可以看到。

目录 u-boot-1.1.6 之 cpu/arm920t/start.s分析 ........................................................................................... 2 u-boot 中.lds连接脚本文件的分析 ...................................................................................................12 分享一篇我总结的 uboot 学习笔记(转) .....................................................................................15 U-BOOT内存布局及启动过程浅析 ...................................................................................................22 u-boot 中的命令实现 .......................................................................................................................... 25 U-BOOT环境变量实现 ........................................................................................................................28 1.相关文件 ....................................................................................................................................28 2.数据结构 ....................................................................................................................................28 3.ENV 的初始化...........................................................................................................................30 3.1env_init ............................................................................................................................30 3.2 env_relocate ...................................................................................................................30 3.3*env_relocate_spec ........................................................................................................31 4. ENV 的保存 ..............................................................................................................................31 U-Boot 环境变量 ..........................................................................................................................32 u-boot 代码链接的问题 ......................................................................................................................35 ldr 和 adr 在使用标号表达式作为操作数的区别 ............................................................................40 start_armboot 浅析 ..............................................................................................................................42 1.全局数据结构的初始化 ..........................................................................................................42 2.调用通用初始化函数...............................................................................................................43 3.初始化具体设备 .......................................................................................................................44 4.初始化环境变量 .......................................................................................................................44 5.进入主循环 ...............................................................................................................................44 u-boot 编译过程 ...................................................................................................................................44 mkconfig文件的分析 .......................................................................................................................... 47 从 NAND闪存中启动 U-BOOT的设计 ..............................................................................................50 引言 ...............................................................................................................................................50 NAND闪存工作原理 ................................................................................................................... 51 从 NAND闪存启动 U-BOOT的设计思路.................................................................................. 51 具体设计 ....................................................................................................................................... 51 支持 NAND闪存的启动程序设计 ..................................................................................... 51 支持 U-BOOT命令设计 ...................................................................................................... 52 结语 ............................................................................................................................................... 53 参考文献 ....................................................................................................................................... 53 U-boot 给 kernel 传参数和 kernel 读取参数—struct tag (以及补充) ............................................ 53 1 、u-boot 给 kernel 传 RAM 参数 ........................................................................................54 2 、Kernel 读取 U-boot 传递的相关参数 .............................................................................56 3 、关于 U-boot 中的 bd 和 gd...............................................................................................59 U-BOOT源码分析及移植 ....................................................................................................................60 一、 u-boot 工程的总体结构: ..................................................................................................61 1、源代码组织 ....................................................................................................................61 2.makefile简要分析 ............................................................................................................61 3、u-boot 的通用目录是怎么做到与平台无关的?......................................................63 4、smkd2410 其余重要的文件 : ...................................................................................63 二、u-boot流程、主要的数据结构、内存分配 ................................................................64 1、u-boot 的启动流程: ...................................................................................................64 2、u-boot 主要的数据结构 ...............................................................................................66 3、u-boot 重定位后的内存分布: ...................................................................................68 三、u-boot 的重要细节 。 ........................................................................................................68 关于 U-boot命令相关的编程 : ................................................................................. 73 四、U-boot 在 ST2410 的移植,基于 NOR FLASH和 NAND FLASH启动。......................... 76 1、从 smdk2410 到 ST2410: .............................................................................................. 76 2、移植过程: .................................................................................................................... 76 3、移植要考虑的问题: ...................................................................................................77 4、SST39VF1601: .................................................................................................................77 5、我实现的 flash.c主要部分: ...................................................................................... 78 6、增加从 Nand 启动的代码 : ..................................................................................... 82 7、添加网络命令。 ............................................................................................................ 87
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值