命令结构体的定义在command.h中
struct cmd_tbl_s {
char *name; /* Command Name */
int maxargs; /* maximum number of arguments */
int repeatable; /* autorepeat allowed? */
/*Implementation function */
int (*cmd)(structcmd_tbl_s *, int, int, char *[]);
char *usage; /* Usage message (short) */
#ifdef CFG_LONGHELP
char *help; /* Help message (long) */
#endif
#ifdefCONFIG_AUTO_COMPLETE
/* do auto completion on the arguments */
int (*complete)(intargc, char *argv[], char last_char, int maxv, char *cmdv[]);
#endif
};
typedef structcmd_tbl_s cmd_tbl_t;
定义定义命令的宏,使用此宏定义的命令结构体变量将会放在.u_boot_cmd的段里面
#defineStruct_Section __attribute__ ((unused,section(".u_boot_cmd")))
在u-boot.lds中定义了.u_boot_cmd的连接方式: . = .; __u_boot_cmd_start = .; .u_boot_cmd : { *(.u_boot_cmd) } __u_boot_cmd_end = .; 这意味着,所有的.u_boot_cmd段都会在__u_boot_cmd_start到__u_boot_cmd_end之间。 |
#ifdef CFG_LONGHELP
#defineU_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t__u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}
#else /* no long help info */
#defineU_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t__u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}
#endif /* CFG_LONGHELP */
示例: U_BOOT_CMD( date, 2, 1, do_date, "date - get/set/reset date & time\n", "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n" " - without arguments: print date & time\n" " - with numeric argument: set the system date & time\n" " - with 'reset' argument: reset the RTC\n" ); |
命令的源代码一般使用一个单独的C文件,名字为cmd_xxx.c,在开头会定义命令选项开关:
#if (CONFIG_COMMANDS& CFG_CMD_XXX)
命令源代码
#endif
其中CFG_CMD_XXX是命令的掩码,每个命令不一样,它在cmd_confdefs.h中被定义。
在进入u-boot命令模式时,u-boot通过find_cmd查找命令结构体,然后调用命令结构体中的函数实现命令。