u-boot中添加自定义命令

1.u-boot命令机制
u-boot中,每个命令都使用一个struct cmd_tbl_s结构体定义,该定义在include/command.h中实现:
struct cmd_tbl_s{
  char *name,//u-boot中执行的命令
  int maxargs,//命令所能带的参数个数,最少为1
  int repeatable,//该命令是否可重复
  int (*cmd)(struct cmd_tbl_s *,int,int,char*[]),//指向该命令对应的源函数
  char *usage,//命令的使用提示
  char *help//在线帮助信息
};

u-boot中定义的命令能与具体的函数程序相对应,通过指针  int (*cmd)(struct cmd_tbl_s *,int,int,char*[]) 实现。

在include/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}

“##”与"#"都是预编译操作符,“##”有字符串连接功能,"#"表示后面紧跟的是一个字符串。
宏 U_BOOT_CMD(name, maxargs, rep, cmd, usage, help) 就是将
struct cmd_tbl_s {
  char *name,
  int maxargs,
  int repeatable,
  int (*cmd)(struct cmd_tbl_s *,int,int,char*[]),
  char *usage,
  char *help
};
这样的一个命令结构体放到U-BOOT连接脚本 board/xxx/u-boot.lds中定义的".u-boot_cmd"段所在的内存区域.当用户在u-boot的shell中输入命令时,就会在".u_boot_cmd"这个内存区域中查找,当该区域中某一个cmd_tbl_s命令结构体的
cmd_tbl_s.name和输入的命令字符串相符时,就调用该命令结构体的cmd_tbl_s.cmd()函数.

2.添加自定义命令
自定义命令设为"myubootcmd",不可与u-boot命令重名,

<1>添加命令行配置信息,在u-boot-1.3.2/include/configs/smdk2410.h中添加 #define CONFIG_CMD_MYUBOOT,如下:
#define CONFIG_CMD_CACHE
#define CONFIG_CMD_DATE
#define CONFIG_CMD_ELF
#define CONFIG_CMD_PING
#define CONFIG_CMD_NET
#define CONFIG_CMD_MYUBOOT

<2>编写命令行对应的源程序,u-boot-1.3.2/board/smdk2410/中添加文件myuboot.c,内容如下所示

#include
#include
#include
#ifdef CONFIG_CMD_MYUBOOT
void myubootcmd(void)
{
    printf("Hello,my u-boot!\n");
}

U_BOOT_CMD( myuboot,                             //uboot命令
        1,                                       //不带参数
        2,                                       //可重复
        myubootcmd,                              //命令对应函数
        "hello-my uboot command",                //用法提示
        "my uboot test command in u-boot 1.3.2\n"//在线帮助信息
        );
#endif

<3>添加编译 u-boot-1.3.2/board/smdk2410/Makefile 中添加myuboot.o
include $(TOPDIR)/config.mk
LIB = $(obj)lib$(BOARD).a
COBJS := smdk2410.o flash.o myuboot.o

<4>编译u-boot
# make smdk2410_config CROSS_COMPILE=arm-linux-
Configuring for smdk2410 board...
# make ARCH=arm  CROSS_COMPILE=arm-linux- all

<5>运行
SMDK2410
# help myuboot
myuboot my uboot test command in u-boot 1.3.2

SMDK2410
# myuboot
Hello,my u-boot!
SMDK2410

http://blog.sina.com.cn/s/blog_4c02ba150101cp97.html

posted on 2013-09-11 15:24  屁屁侠  阅读(643)  评论(0编辑  收藏  举报

导航