本文共 6060 字,大约阅读时间需要 20 分钟。
1.命令的注册2.命令的调用3.命令的执行4.命令的返回
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_samplestatic 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", };
cmd_call 被调用的时机 1. bootcmd 2. cmdline所有的命令的调用都是 cmd_call 调用的 // cmd_call 不直接调用 do_xxx以help 为例 , do_help的调用堆栈为 cmd_call cmd_always_repeatable // 以该 字符串为关键字 在 全文搜索 do_helpcommon/command.c 564-585564 /** 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 }
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 }
返回时也是 cmd_call 检查的命令的返回值 1. 正确为0 2. 错误为非0
转载地址:http://nbigi.baihongyu.com/