U_BOOT_CMD分析与定制

U_BOOT_CMD(name,maxargs,repeatable,command,“usage”,“help”)
各个参数的意义如下:
name:命令名,非字符串,但在U_BOOT_CMD中用“#”符号转化为字符串
maxargs:命令的最大参数个数
repeatable:是否自动重复(按Enter键是否会重复执行)
command:该命令对应的响应函数指针
usage:简短的使用说明(字符串)
help:较详细的使用说明(字符串)
U_BOOT_CMD宏在include/command.h中定义:
在u-Boot连接脚本 u-boot.lds中定义了”.u_boot_cmd”段:这表明带有“.u_boot_cmd”声明的函数或变量将存储在“u_boot_cmd”段。这样只要将u-boot所有命令对应的cmd_tbl_t变量加上“.u_boot_cmd”声明,编译器就会自动将其放在“u_boot_cmd”段,查找cmd_tbl_t变量时只要在 __u_boot_cmd_start 与 __u_boot_cmd_end 之间查找就可以了。
cmd_tbl_t在include/command.h中定义
一个cmd_tbl_t结构体变量包含了调用一条命令的所需要的信息。

自制u-boot命令
(1)首先我们在u-boot的common目录下增加一个cmd_mycmd.c文件 。复制cmd_bootm.c中的头文件到cmd_mycmd.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>
(2)定义命令和操作函数
int do_mycmd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
int i;
for(i=0;i<argc;i++)
{
printf (“i love you ~ argc = %d\n”,argc);
printf (“argv[%d] = %s\n”,i,argv[i]);
}
return 0;
}
(3)注册命令和执行函数
U_BOOT_CMD(
mycmd, CFG_MAXARGS, 1, do_mycmd,
“mycmd - just for test\n”, /短帮助信息/
“mycmd, long help•••••••••••••••••••••\n” //长帮帮助信息
);

(4)修改common下面的makefile文件,
参考Makefile中其他文件的定义,加入一句
COBJS += cmd_mycmd.o

猜你喜欢

转载自blog.csdn.net/weixin_42418557/article/details/89003616