uboot U_BOOT_CMD

其中U_BOOT_CMD命令格式以下:函数

U_BOOT_CMD(name,maxargs,repeatable,command,"usage","help")oop

 各个参数的意义以下:spa

 
  1. name:命令名,非字符串,但在U_BOOT_CMD中用“#”符号转化为字符串.net

  2. maxargs:命令的最大参数个数命令行

  3. repeatable:是否自动重复(按Enter键是否会重复执行)指针

  4. command:该命令对应的响应函数指针code

  5. usage:简短的使用说明(字符串)blog

  6. help:较详细的使用说明(字符串)token

U_BOOT_CMD宏在include/command.h中定义:图片

 
  1. #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \

  2.     cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}

“##”“#”都是预编译操做符,“##”有字符串链接的功能,“#”表示后面紧接着的是一个字符串。

其中Struct_Section在include/command.h中定义以下:

#define Struct_Section  __attribute__ ((unused,section (".u_boot_cmd")))

凡是带有attribute ((unused,section (“.u_boot_cmd”))属性声明的变量都将被存放在”.u_boot_cmd”段中,而且即便该变量没有在代码中显式的使用编译器也不产生警告信息。

 

在u-Boot链接脚本 u-boot.lds中定义了”.u_boot_cmd”段:

 
  1. . = .;

  2. __u_boot_cmd_start = .; /*将 __u_boot_cmd_start指定为当前地址 */

  3. .u_boot_cmd : { *(.u_boot_cmd) }

  4. __u_boot_cmd_end = .; /* 将__u_boot_cmd_end指定为当前地址 */

这代表带有“.u_boot_cmd”声明的函数或变量将存储在“u_boot_cmd”段。

这样只要将u-boot全部命令对应的cmd_tbl_t变量加上“.u_boot_cmd”声明,编译器就会自动将其放在“u_boot_cmd”段,查找cmd_tbl_t变量时只要在 __u_boot_cmd_start 与 __u_boot_cmd_end 之间查找就能够了。

 

cmd_tbl_t在include/command.h中定义以下: 

 
  1. struct cmd_tbl_s {

  2.     char        *name;        /* Command Name            */

  3.     int          maxargs;    /* maximum number of arguments    */

  4.     int          repeatable;    /* autorepeat allowed?        */

  5.                     /* Implementation function    */

  6.     int        (*cmd)(struct cmd_tbl_s *, int, int, char * const []);

  7.     char        *usage;        /* Usage message    (short)    */

  8. #ifdef    CONFIG_SYS_LONGHELP

  9.     char        *help;        /* Help  message    (long)    */

  10. #endif

  11. #ifdef CONFIG_AUTO_COMPLETE

  12.     /* do auto completion on the arguments */

  13.     int        (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);

  14. #endif

  15. };

  16.  
  17. typedef struct cmd_tbl_s    cmd_tbl_t;

一个cmd_tbl_t结构体变量包含了调用一条命令的所须要的信息。

 

以“boot”命令的定义通过宏展开后以下:

这里写图片描述

    cmd_tbl_t __u_boot_cmd_boot __attribute__ ((unused,section (".u_boot_cmd"))) = {boot, 1, 1, do_bootd, "boot    - boot default, i.e., run 'bootcmd'\n", " NULL"}

 

 
  1. int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])

  2. {

  3. int rcode = 0;

  4.  
  5. if (run_command(getenv("bootcmd"), flag) < 0)

  6. rcode = 1;

  7. return rcode;

  8. }

run_command 命令分析:

u-boot启动第二阶段最后跳到main_loop函数中循环

 
  1. s = getenv ("bootcmd");

  2. if (bootdelay >= 0 && s && !abortboot (bootdelay)) {

  3. ......

  4. run_command (s, 0);

  5. ......

  6. }

从main_loop中咱们知道,若是bootdelay时间内未按下按键则启动Linux内核,按下按键则进入uboot命令行等待用户输入命令。

用户输入命令则调取run_command函数,在该函数中有下面几个比较重要的点:

1. 从注释咱们很容易知道这段代码是在对命令进行分离,而且u-boot支持’;’分离命令。

 
  1. /*

  2. * Find separator, or string end

  3. * Allow simple escape of ';' by writing "\;"

  4. */

  5. for (inquotes = 0, sep = str; *sep; sep++) {

  6. if ((*sep=='\'') &&

  7. (*(sep-1) != '\\'))

  8. inquotes=!inquotes;

  9.  
  10. if (!inquotes &&

  11. (*sep == ';') && /* separator */

  12. ( sep != str) && /* past string start */

  13. (*(sep-1) != '\\')) /* and NOT escaped */

  14. break;

  15. }

2. 分离参数

 
  1. /* Extract arguments */

  2. if ((argc = parse_line (finaltoken, argv)) == 0) {

  3. rc = -1; /* no command at all */

  4. continue;

  5. }

3. 用第一个参数argv[0]在命令列表中寻找对应的命令,并返回一个cmd_tbl_t类型的实例。咱们能够猜到这个结构体应该保函了有关命令的一系列内容。

 
  1. /* Look up command in command table */

  2. if ((cmdtp = find_cmd(argv[0])) == NULL) {

  3. printf ("Unknown command '%s' - try 'help'\n", argv[0]);

  4. rc = -1; /* give up after bad command */

  5. continue;

  6. }

在common/command.c 中查看find_cmd函数

 
  1. /*__u_boot_cmd_start与__u_boot_cmd_end间查找命令,并返回cmdtp->name命令的cmd_tbl_t结构。*/

  2. cmd_tbl_t *cmdtp;

  3. cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */

  4. const char *p;

  5. int len;

  6. int n_found = 0;

  7.  
  8. /*

  9. * Some commands allow length modifiers (like "cp.b");

  10. * compare command name only until first dot.

  11. */

  12. len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);

  13.  
  14. for (cmdtp = &__u_boot_cmd_start;

  15. cmdtp != &__u_boot_cmd_end;

  16. cmdtp++) {

  17. if (strncmp (cmd, cmdtp->name, len) == 0) {

  18. if (len == strlen (cmdtp->name))

  19. return cmdtp; /* full match */

  20.  
  21. cmdtp_temp = cmdtp; /* abbreviated command ? */

  22. n_found++;

  23. }

  24. }

5、总结命令执行过程

① 在u-boot控制台中输入“boot”命令执行时,u-boot控制台接收输入的字符串“boot”,传递给run_command函数。

② run_command函数调用common/command.c中实现的find_cmd函数在__u_boot_cmd_start与__u_boot_cmd_end间查找命令,并返回boot命令的cmd_tbl_t结构。

③ 而后run_command函数使用返回的cmd_tbl_t结构中的函数指针调用boot命令的响应函数do_bootd,从而完成了命令的执行。

6、自制u-boot命令

这里写图片描述

https://blog.csdn.net/yexiangCSDN/article/details/81773093

相关文章
相关标签/搜索