设为首页 加入收藏

TOP

u-boot-1.1.6实现自定义命令(一)
2019-08-27 07:21:32 】 浏览:50
Tags:u-boot-1.1.6 实现 定义 命令

学习目标:

1、了解u-boot-1.1.6中命令的实现机制

2、掌握如何在u-boot-1.1.6中添加自定义命令


1、命令的实现机制

uboot运行在命令行解析模式时,在串口终端输入uboot命令,按下回车后,系统将执行命令的相应操作。以help命令为例,当输入help命令,并按下回车时,串口终端打印当前uboot支持的所有命令的帮助信息,如下图所示(图片仅截取部分):

到这里我们应该很好奇uboot的命令是如何实现的呢?想要知道命令如何实现,最简单的办法就是在uboot工程中搜索“help”关键词。通过查找在common/command.c源码文件中找到了uboot命令的定义:

U_BOOT_CMD(
    help,    CFG_MAXARGS,    1,    do_help,
     "help    - print online help\n",
     "[command ...]\n"
     "    - show help information (for 'command')\n"
     "'help' prints online help for the monitor commands.\n\n"
     "Without arguments, it prints a short usage message for all commands.\n\n"
     "To get detailed help information for specific commands you can type\n"
  "'help' with one or more command names as arguments.\n"
);

这里我们并不知道U_BOOT_CMD是什么,还需再进行查找。通过进一步查找,可以发现U_BOOT_CMD是一个宏,这个宏在include/command.h头文件中定义,U_BOOT_CMD宏的原型如下:

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

#ifdef CFG_LONGHELP
#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} #else /* no long help info */ #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \ cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage} #endif /* CFG_LONGHELP */

这里采用条件编译的方式,如果宏CFG_LONGHELP被定义,#ifdef 到#else之间的语句被编译,否者#else到#endif之间的语句被编译。这里我们假设宏CFG_LONGHELP(表示是否支持长的帮助信息)在其他处被定义,按照#ifdef和#else之间的宏定义格式将上述的help命令实现U_BOOT_CMD(help,....)展开,展开后的形式如下:

cmd_tbl_t __u_boot_cmd_help __attribute__ ((unused,section (".u_boot_cmd"))) = {help, CFG_MAXARGS, 1, do_help, 
    "help    - print online help\n", 
  "[command ...]\n"
     "    - show help information (for 'command')\n"
     "'help' prints online help for the monitor commands.\n\n"
     "Without arguments, it prints a short usage message for all commands.\n\n"
     "To get detailed help information for specific commands you can type\n"
     "'help' with one or more command names as arguments.\n"
}

将help命令实现U_BOOT_CMD(help,....)展开,可以看出其实U_BOOT_CMD(help,....)就是定义了一个cmd_tbl_t类型的结构体变量,变量名为__u_boot_cmd_help,比较特别的是这个变量被强加了__attribute__属性,编译器在进行链接时,将该变量放在了名为".u_boot_cmd"自定义段的地址中。下面来看cmd_tbl_t结构体的声明形式:

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

typedef struct cmd_tbl_s    cmd_tbl_t;

name:命令的名称(很重要)

maxargs :命令所支持的最大参数

repeatable :命令是否可重复

cmd:回调函数,执行命令便是调用该回调函数

usage:对应短的帮助信息

help :对应长的帮助信息

那么这些定义的命令是如何被调用呢?通过再次查找我们找到了最底层的命令查找函数find_cmd,其源码如下:

cmd_tbl_t *find_cmd (const char *cmd)
{
    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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇深入理解内存映射mmap 下一篇u-boot-1.1.6第2阶段入口函数star..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目