uboot 命令实现分析
1. 命令实现
命令由宏 U_BOOT_CMD 定义,该宏具体为
#ifdef CFG_LONGHELP
#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}
#else /* no long help info */
#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}
#endif
其中
1. 数据类型为 cmd_tbl_t,该结构体为
struct cmd_tbl_s {
char *name; /* Command Name */
int maxargs; /* maximum number of arguments */
int repeatable; /* autorepeat allowed? */
/* Implementation function */
int (*cmd)(struct cmd_tbl_s *, int, int, char *[]);
char *usage; /* Usage message (short) */
#ifdef CFG_LONGHELP
char *help; /* Help message (long) */
#endif
};
2. 宏Struct_Section的定义为
#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
即申明该结构体放置在段 .u_boot_cmd 中
使用宏 U_BOOT_CMD 定义命令,则生成一个 _u_boot_cmd命令名称 的结构体,该结构体代码放置的 .u_boot_cmd 代码段,其中
参数 | 意义 |
---|---|
name | 命令名称 |
maxargs | 最多参数 |
repeatable | 是否可重复 |
cmd | 命令实现函数指针 |
usage | 使用方法 |
help | 帮助信息 |
2. 命令执行过程
- 调用函数 run_command 解析输入命令
- 调用函数 parse_line 分解输入命令
- 调用 find_cmd 寻找命令结构体
- 执行 cmd 命令