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 #
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值