uboot中添加自定义命令

uboot中可以通过修改源程序来添加自定义命令,进一步扩展uboot的功能。

我想在uboot下添加一条新的命令(名为varcpy),用来拷贝uboot中的环境变量。

修改方式如下:

创建新文件common/cmd_varcpy.c,并在该文件中添加如下内容:

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

#ifdef CONFIG_CMD_VARCPY
int do_varcpy(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    int i;
    if(argc != 3)
        cmd_usage(cmdtp);
    setenv(argv[2],getenv(argv[1]));    

    return 0;
}

U_BOOT_CMD(
    varcpy,    3,        1,    do_varcpy,
    "copy oldvar to newvar",
    "oldvar newvar\n"
    "    - copy enviroment value of oldvar to a new variable named as newvar\n"
);
#endif

在common/Makefile中添加一行:

COBJS-$(CONFIG_CMD_VARCPY) += cmd_varcpy.o

在include/configs/hwgw6410_dev.h下添加一行:

#define CONFIG_CMD_VARCPY

然后重新编译,将生成的uboot烧写到开发板,然后就可以使用该命令了。使用方式如下:

hwgw # printenv
baudrate=115200
bootargs=console=ttySAC,115200
bootcmd=if mmc rescan 0 ; then fatload mmc 0 0x50000000 reload.img; source 0x50000000; fi 
bootdelay=3
ethact=dm9000
ethaddr=00:12:39:8f:ad:b3
ipaddr=192.168.1.111
oldipaddr=192.168.1.111
serverip=192.168.1.149
stderr=serial
stdin=serial
stdout=serial

Environment size: 324/16380 bytes
hwgw # varcpy serverip oldserverip
hwgw # printenv
baudrate=115200
bootargs=console=ttySAC,115200
bootcmd=if mmc rescan 0 ; then fatload mmc 0 0x50000000 reload.img; source 0x50000000; fi
bootdelay=3
ethact=dm9000
ethaddr=00:12:39:8f:ad:b3
ipaddr=192.168.1.111
oldipaddr=192.168.1.111
oldserverip=192.168.1.149
serverip=192.168.1.149
stderr=serial
stdin=serial
stdout=serial

Environment size: 352/16380 bytes

从上面的输出信息可以看到,使用varcpy将serverip拷贝到oldserverip后,得到的环境变量多了一行关于oldserverip的信息。

posted on 2014-05-29 12:31  qiaoqiao2003  阅读(627)  评论(0编辑  收藏  举报

导航