uboot 添加 自定义命令

--- title: uboot-uboot 添加 自定义命令 date: 2020-05-13 16:51:38 categories: tags: - uboot - cmd - config ---

章节描述:

定制化uboot 的时偶尔需要添加自定义命令,本问介绍如何添加命令。

步骤

添加命令文件

在common目录下,新建一个cmd_xx.c,需要添加的命令格式为:

int do_hello(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])

并在文件中使用 U_BOOT_CMD 宏进行有关说明 :

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

宏参数有6个:

  • 第一个参数:添加的命令的名字
  • 第二个参数:添加的命令最多有几个参数(注意,假如你设置的参数个数是3,而实际的参数个数是4,那么执行命令会输出帮助信息的)
  • 第三个参数:是否重复(1重复,0不重复)(即按下Enter键的时候,自动执行上次的命令)
  • 第四个参数:执行函数,即运行了命令具体做啥会在这个函数中体现出来
  • 第五个参数:帮助信息(short)
  • 第六个参数:帮助信息(long)

最简单的命令文件的范例

#include <command.h>
#include <common.h>

int do_hello(cmd_tbl_t *cmdtp,int flag,int argc,char *argv)
{
    printf("my test \n");
    return 0;
}

U_BOOT_CMD(
hello,1,0,do_hello,"usage:test\n","help:test\n"
);

修改 Makefile

修改 common/Makefile 在Makfile中添加一行:

COBJS-y += cmd_xx.o 

附录 : 宏U_BOOT_CMD 的解析

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

我们分段来看:

命令所在的段:

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

由定义可知该命令在链接的时候链接的地址或者说该命令存放的位置是u_boot_cmd section

宏展开的结构体:

typedef struct cmd_tbl_s        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) */
  char  *help
}
posted @ 2019-08-26 02:09  schips  阅读(866)  评论(0编辑  收藏  举报