Uboot命令U_BOOT_CMD分析

其中U_BOOT_CMD命令格式如下:

U_BOOT_CMD(name,maxargs,repeatable,command,“usage”,“help”)

 各个参数的意义如下:

   name:命令名,非字符串,但在U_BOOT_CMD中用“#”符号转化为字符串
   maxargs:命令的最大参数个数
   repeatable:是否自动重复(按Enter键是否会重复执行)
   command:该命令对应的响应函数指针
   usage:简短的使用说明(字符串)
   help:较详细的使用说明(字符串)

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

#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}

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

其中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”段:

. = .;
__u_boot_cmd_start = .;          /*将 __u_boot_cmd_start指定为当前地址 */
.u_boot_cmd : { *(.u_boot_cmd) }
__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中定义如下:

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 * const []);
    char        *usage;        /* Usage message    (short)    */
#ifdef    CONFIG_SYS_LONGHELP
    char        *help;        /* Help  message    (long)    */
#endif
#ifdef CONFIG_AUTO_COMPLETE
    /* do auto completion on the arguments */
    int        (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
#endif
};
 
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"}

int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int rcode = 0;
 
	if (run_command(getenv("bootcmd"), flag) < 0)
		rcode = 1;
	return rcode;
}

run_command 命令分析:
u-boot启动第二阶段最后跳到main_loop函数中循环

        s = getenv ("bootcmd");
    if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
        ......
        run_command (s, 0);
        ......
        }

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

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

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

         /*
          * Find separator, or string end
          * Allow simple escape of ';' by writing "\;"
          */
         for (inquotes = 0, sep = str; *sep; sep++) {
             if ((*sep=='\'') &&
                 (*(sep-1) != '\\'))
                 inquotes=!inquotes;
    
             if (!inquotes &&
                 (*sep == ';') &&    /* separator        */
                 ( sep != str) &&    /* past string start    */
                 (*(sep-1) != '\\')) /* and NOT escaped  */
                 break;
         }
    
  2. 分离参数

     /* Extract arguments */
     if ((argc = parse_line (finaltoken, argv)) == 0) {
         rc = -1;    /* no command at all */
         continue;
     }
    
  3. 用第一个参数argv[0]在命令列表中寻找对应的命令,并返回一个cmd_tbl_t类型的实例。我们可以猜到这个结构体应该保函了有关命令的一系列内容。

         /* Look up command in command table */
         if ((cmdtp = find_cmd(argv[0])) == NULL) {
             printf ("Unknown command '%s' - try 'help'\n", argv[0]);
             rc = -1;    /* give up after bad command */
             continue;
         }
    

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

/*__u_boot_cmd_start与__u_boot_cmd_end间查找命令,并返回cmdtp->name命令的cmd_tbl_t结构。*/
cmd_tbl_t *cmdtp;
    cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start;    /*Init value */
    const char *p;
    int len;
    int n_found = 0;
 
    /*
     * Some commands allow length modifiers (like "cp.b");
     * compare command name only until first dot.
     */
    len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
 
    for (cmdtp = &__u_boot_cmd_start;
         cmdtp != &__u_boot_cmd_end;
         cmdtp++) {
        if (strncmp (cmd, cmdtp->name, len) == 0) {
            if (len == strlen (cmdtp->name))
                return cmdtp;   /* full match */
 
            cmdtp_temp = cmdtp; /* abbreviated command ? */
            n_found++;
        }
    }

五、总结命令执行过程

① 在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,从而完成了命令的执行。
六、自制u-boot命令

猜你喜欢

转载自blog.csdn.net/qq_33894122/article/details/86129765