U_BOOT_CMD命令注册宏分析

其中U_BOOT_CMD在u-boot/include/command.h文件中命令格式如下:

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,从而完成了命令的执行。

OpenJTAG> print; md.w 0
bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0
bootdelay=2
baudrate=115200
ethaddr=08:00:3e:26:0a:5b
netmask=255.255.255.0
mtdids=nand0=nandflash0
mtdparts=mtdparts=nandflash0:256k@0(bootloader),128k(params),2m(kernel),-(root)
ipaddr=2.2.2.117
filesize=1C35D0
serverip=2.2.2.102
bootargs=noinitrd root=/dev/mtdblock3 rw init=/linuxrc console=ttySAC0,115200 nfsroot=2.2.2.102:/work/nfs_root ip=2.2.2.112:2.2.2.102:2.2.2.1022:255.255.255.0::eth0:off
stdin=serial
stdout=serial
stderr=serial
partition=nand0,0
mtddevnum=0
mtddevname=bootloader

Environment size: 554/131068 bytes
00000000: 00ff ea00 f014 e59f f014 e59f f014 e59f    ................
00000010: f014 e59f f014 e59f f014 e59f f014 e59f    ................
00000020: 0200 33f8 0260 33f8 02c0 33f8 0320 33f8    ...3`..3...3 ..3
00000030: 0380 33f8 04a0 33f8 04c0 33f8 beef dead    ...3...3...3....
00000040: 0000 33f8 0000 33f8 dbec 33fa 4df8 33fb    ...3...3...3.M.3
00000050: c0de 0bad c0de 0bad 0000 0000 c0de 0bad    ................
00000060: c0de 0bad 0000 e10f 001f e3c0 00d3 e380    ................
00000070: f000 e129 0453 e3a0 1000 e3a0 1000 e580    ..).S...........

六、自制u-boot命令
这里写图片描述

首先模仿cmd_bootm.c文件,创建一个cmd_hello.c文件,将cmd_bootm.c中包含的头文件全部都包含进来,然后是do_hello函数,然后创建一个U_BOOT_CMD结构体。

还需要修改common目录的Makefile,在末尾加上cmd_hello.o,这样编译时才会编译cmd_hello.c。

然后编译u-boot并下载,进入u-boot命令页面,输入help指令,可以看到出现了hello命令。


输入hello,终端输出Hello world!, 1,符合预期,重复执行没问题,输出长信息也没问题,这样,一个自定义的hello命令就完成了。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值