如果在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函数中都可以看到。