uclinux很久前笔记6

【u-boot添加命令】

在u-boot/common/main.c中

void main_loop(void)
{
       ……
       run_command(xxx,xxx);
       ……
}
int run_command(const char *cmd, int flag)
{
       cmd_tbl_t*cmdtp;
       ……
       /*Extract arguments */
       argc= parse_line (finaltoken, argv);
       /*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;
       }
       ……
}
cmd_tbl_t *find_cmd(const char *cmd)
{
       ……
       len= ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
 
       for(cmdtp = &__u_boot_cmd_start;           //在u-boot.lds中定义了
            cmdtp != &__u_boot_cmd_end;              //在u-boot.lds中定义了
            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.lds:

  __u_boot_cmd_start = .;

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

  __u_boot_cmd_end =.;

 

 

command.h中定义了.u_boot_cmd

#define Struct_Section  __attribute__ ((unused,section(".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}

如:

U_BOOT_CMD(bootm,CFG_MAXARGS,1,do_bootm,”xxx” ,”xxx xxx”);

展开该宏定义:

cmd_tbl_t __u_boot_cmd_bootm __attribute__((unused,section (".u_boot_cmd"))) = {bootm, CFG_MAXARGS, 1, do_bootm,”xxx” ,”xxx xxx”}

 

在u-boot/include/command.h中定义了命令结构体:

struct cmd_tbl_s{
       char        *name;           /*Command Name               */
       int          maxargs;        /* maximum number ofarguments       */
       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;

添加一个test命令:

1、 在common目录下新建一个cmd_test.c

内容为:

#include<common.h>
#include<watchdog.h>
#include<command.h>
#include<image.h>
#include<malloc.h>
#include<zlib.h>
#include<bzlib.h>
#include<environment.h>
#include<asm/byteorder.h>
 
int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char*argv[])
{
        int i;
        printf(“test argc:%d\n”,argc);
        for(i=0;i< argc;i++)
        {
               printf(“argv[%d]: %s\n”,i,argv[i]);
        }
        return 0;
}
 
U_BOOT_CMD(
        test, CFG_MAXARGS, 1,    do_test,
        "test    - u-boot command test\n",
        "\n"
        "   - xxx xxx xxx test\n"
        "      xxx test.\n"
);

       编译:

       在当前目录下的Makefile中加入编译信息:

       COBJS    = main.o

……

cmd_test.o

 

在烧写启动 u-boot 后,将在 help 中显示 test 命令,并且可以执行 test 命令。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值