uboot的命令分析
在控制台可以看出,在每一行都会有一个前缀,这个是在readline函数的参数传入宏
CFG_PROMPT,查看这个宏可以知道,100ask的uboot会用“OpenJTAG>”来做前缀
读取一行之后,就运行run_command
run_command里会分析输入的命令,然后利用find_cmd来查找命令,最后运行
这个命令的回调函数。
看代码时首先看数据结构,以下是命令列表的结构
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
#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;
结构体很简单,就不做介绍了
不过,可以依据这个结构体,来构造一个自己的命令看看
在定义CMD时,我们需要把命令存在u_boot_cmd段里,在运行find_cmd的时候,实际上就是
遍历u_boot_cmd这个段
搜索bootm时,可以看出命令的代码一般存在common文件夹里,随便看一个例如bootm
可以看出,利用U_BOOT_CMD这个宏来实现把bootm放在u_boot_cmd的段里
U_BOOT_CMD(
bootm, CFG_MAXARGS, 1, do_bootm,
"bootm - boot application image from memory\n",
"[addr [arg ...]]\n - boot application image stored in memory\n"
"\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
"\t'arg' can be the address of an initrd image\n"
#ifdef CONFIG_OF_FLAT_TREE
"\tWhen booting a Linux kernel which requires a flat device-tree\n"
"\ta third argument is required which is the address of the of the\n"
"\tdevice-tree blob. To boot that kernel without an initrd image,\n"
"\tuse a '-' for the second argument. If you do not pass a third\n"
"\ta bd_info struct will be passed instead\n"
#endif
);
#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}
这个宏就是为了实现把这个命令放在段里
那么,我们需要实现命令,只需要按照这个结构体来实现,最后用U_BOOT_CMD装入即可