08 uboot上增加自己的命令

在实现前需了解lds和里面的段



在uboot源码目录里include/command.h:

struct cmd_tbl_s {
    char        *name;    
    int     maxargs;   //命令功能函数支持的最大参数的个数
    int     repeatable; //此命令执行完后,再按"enter"键时是否重复执行.                 
    int     (*cmd)(struct cmd_tbl_s *, int, int, char * const []); //命令的功能函数
    char        *usage;    // 打"help"时,此命令相关的内容
    char        *help;     // help 命令
    ...
};

typedef struct cmd_tbl_s    cmd_tbl_t;

#define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help)      \
    U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)

191 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
192     ll_entry_declare(cmd_tbl_t, _name, cmd) =           \
193         U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd,  \
194                         _usage, _help, _comp);

// 这个宏用于声变结构体变量并指定存入在段".uboot_list_2_#_name"
150 #define ll_entry_declare(_type, _name, _list)               \
151     _type _u_boot_list_2_##_list##_2_##_name __aligned(4)       \
152             __attribute__((unused,              \
153             section(".u_boot_list_2_"#_list"_2_"#_name)))

// 这个宏用于结构全变量的初始化
186 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd,      \
187                 _usage, _help, _comp)           \
188         { #_name, _maxargs, _rep, _cmd, _usage,         \
189             _CMD_HELP(_help) _CMD_COMPLETE(_comp) }

//////////////////////////////////////////////////////

在uboot里增加自定义的命令, 只要实现一个功能函数,再调用U_BOOT_CMD宏即可.
uboot里命令统一放在源码目录里的cmd目录.每个文件表示一类或一个命令.

如实现一个自己的命令,在cmd目录里:
cmd_mytest.c

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

    int do_mytest(struct cmd_tbl_s *tbl, int flag, int argc, char * const argv[])
    {
        int i;

        printf("in mytest : \n");
        for (i = 0; i < argc ; i++)
        {   
        printf("argv[%d] : %s\n", i, argv[i]);
        }   

        return 0;
    }

    U_BOOT_CMD(mytest, 5, 1, do_mytest, "mytest - usage of mytest", "help of mytest");

修改cmd目录里的Makefile, 在第16行:
    obj-y += cmd_mytest.o  

回到orangepi_sdk目录下:
    make uboot   
    make install_uboot sdcard=/dev/sdb

/////////////////////////////////
重新启动后,在uboot上输入 "help"命令:
   可以查看到:
    mytest  - mytest - usage of mytest

   执行命令:
    mytest 11 22 34 55

    in mytest : 
    argv[0] : mytest
    argv[1] : 11
    argv[2] : 22
    argv[3] : 34
    argv[4] : 55


//////////////////////////
 在uboot里每个命令都有一个功能函数,我们可以在自己实现的命令函数里调用其它命令的功能函数来实现相应的功能.
如启动系统的命令:
extern int do_fat_fsload (cmd_tbl_t *, int, int, char * const []);
extern int do_bootm (cmd_tbl_t *, int, int, char * const []);
int do_mybootcmd(struct cmd_tbl_s *tbl, int flag, int argc, char * const argv[])
{
    // fatoad mmc 0:1 0x43000000 /script.bin
    char *argvs_script[] = {"fatload", "mmc", "0:1", "0x43000000", "/script.bin"};

    // fatload mmc 0:1 0x42000000 /uImage
    char *argvs_uimage[] = {"fatload", "mmc", "0:1", "0x42000000", "/uImage"};

    // bootm 0x42000000
    char *argvs_bootm[] = {"bootm", "0x42000000"};


    if (0 != do_fat_fsload(NULL, 0, 5, argvs_script))
        return;

    if (0 != do_fat_fsload(NULL, 0, 5, argvs_uimage))
        return;

    return do_bootm(NULL, 0, 2, argvs_bootm);
}

U_BOOT_CMD(mybootcmd, 5, 1, do_mybootcmd, "boot system...", "help of mybootcmd");
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值