1.命令的注册
2.命令的调用
3.命令的执行
4.命令的返回
- 1 命令的注册(以help为例)
cmd/help.c
10 static int do_help(struct cmd_tbl *cmdtp, int flag, int argc,
11 char *const argv[])
12 {
13 #ifdef CONFIG_CMDLINE
14 struct cmd_tbl *start = ll_entry_start(struct cmd_tbl, cmd);
15 const int len = ll_entry_count(struct cmd_tbl, cmd);
16 return _do_help(start, len, cmdtp, flag, argc, argv);
17 #else
18 return 0;
19 #endif
20 }
21
22 U_BOOT_CMD(
23 help, CONFIG_SYS_MAXARGS, 1, do_help,
24 "print command description/usage",
25 "\n"
26 " - print brief description of all commands\n"
27 "help command ...\n"
28 " - print detailed usage of 'command'"
29 );
----------------------- 以上的代码展开为以下代码
----------------------- 具体怎么注册以及怎么查找的请参考 __attribute__
----------------------- __attribute__ demo : https://github.com/lisider/attribute_sample
static int do_help(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
struct cmd_tbl *start = ({
static char start[0] __attribute__((__aligned__(4))) __attribute__((unused, section(".u_boot_list_2_""cmd""_1"))); (struct cmd_tbl *)&start; });
const int len = ({
struct cmd_tbl *start = ({
static char start[0] __attribute__((__aligned__(4))) __attribute__((unused, section(".u_boot_list_2_""cmd""_1"))); (struct cmd_tbl *)&start; }); struct cmd_tbl *end = ({
static char end[0] __attribute__((__aligned__(4))) __attribute__((unused, section(".u_boot_list_2_""cmd""_3"))); (struct cmd_tbl *)&end; }); unsigned int _ll_result = end - start; _ll_result; });
return _do_help(start, len, cmdtp, flag, argc, argv);
}
struct cmd_tbl _u_boot_list_2_cmd_2_help __attribute__((__aligned__(4))) __attribute__((unused, section(".u_boot_list_2_""cmd""_2_""help"))) = {
"help", 16, 1 ? cmd_always_repeatable : cmd_never_repeatable, do_help, "print command description/usage", "\n" " - print brief description of all commands\n" "help command ...\n" " - print detailed usage of 'command'",
# 22 "../cmd/help.c" 3 4
((void *)0)
# 22 "../cmd/help.c"
, };
- 2 命令的调用
cmd_call 被调用的时机
1. bootcmd
2. cmdline
所有的命令的调用都是 cmd_call 调用的 // cmd_call 不直接调用 do_xxx
以help 为例 , do_help的调用堆栈为
cmd_call
cmd_always_repeatable // 以该 字符串为关键字 在 全文搜索
do_help
common/command.c 564-585
564 /**
565 * Call a command function. This should be the only route in U-Boot to call
566 * a command, so that we can track whether we are waiting for input or
567 * executing a command.
568 *
569 * @param cmdtp Pointer to the command to execute
570 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
571 * @param argc Number of arguments (arg 0 must be the command text)
572 * @param argv Arguments
573 * @param repeatable Can the command be repeated
574 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
575 */
576 static int cmd_call(struct cmd_tbl *cmdtp, int flag, int argc,
577 char *const argv[], int *repeatable)
578 {
579 int result;
580
581 result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable);
582 if (result)
583 debug("Command failed, result=%d\n", result);
584 return result;
585 }
- 3 命令的执行(以help为例)
cmd/help.c
// 想怎么写,就怎么写
10 static int do_help(struct cmd_tbl *cmdtp, int flag, int argc,
11 char *const argv[])
12 {
13 #ifdef CONFIG_CMDLINE
14 struct cmd_tbl *start = ll_entry_start(struct cmd_tbl, cmd);
15 const int len = ll_entry_count(struct cmd_tbl, cmd);
16 return _do_help(start, len, cmdtp, flag, argc, argv);
17 #else
18 return 0;
19 #endif
20 }
- 4 命令的返回
返回时也是 cmd_call 检查的
命令的返回值
1. 正确为0
2. 错误为非0