一、命令的具体定义
每个命令都是通过U_BOOT_CMD宏来定义的,这个宏定义了一个相关的结构体cmd_tbl_s,是在uboot/include/command.h 文件中。
如下所示:
struct cmd_tbl_s {
char *name; /* Command Name */
int maxargs; /* maximum number of arguments */
int repeatable; /* autorepeat allowed? */
/* Implementation function */
int (*cmd)(struct cmd_tbl_s *, int, int, char *[]);
char *usage; /* Usage message (short) */
#ifdef CONFIG_SYS_LONGHELP
char *help; /* Help message (long) */
#endif
#ifdef CONFIG_AUTO_COMPLETE
/* do auto completion on the arguments */
int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);
#endif
};
从这个宏的定义信息,我们可以知道,每一个 U-Boot 命令都有一个结构体来描述。而这个结构体包含的成员变量有命令名称、最大参数个数、是否可重复、命令执行函数、用法提示、帮助提示。
具体的命令信息定义也在 uboot/include/comman.c 文件中,如下所示:
#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
#ifdef CONFIG_SYS_LONGHELP
#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}
#define U_BOOT_CMD_MKENT(name,maxargs,rep,cmd,usage,help) \
{#name, maxargs, rep, cmd, usage, help}这是定义一个结构的属性,将命令信息放在.u_boot_cmd 这个段当中。
凡通过U_BOOT_CMD定义的cmd_tbl_t 变量会全部被放在.u_boot_cmd 段当中(可以看UBOOT的链接脚本xxx.lds),具体怎么存放是链接器的工作。
二、实现增加一个命令
uboot 本身就有很多命令,这些命令的实现都是类似的。我们在实现增加新的命令的时候,可以参考已有的命令。如参考cmd_bootm.c、cmd_help.c 等文件。
1、在对应的开发板配置文件中,添加相应命令的宏定义。
如:在 uboot/include/configs/mini2440.h 文件中,添加 #define CONFIG_CMD_HELLO 一行。
#define CONFIG_CMD_HELLO /* 增加hello命令 */2、在 uboot/common/ 目录下,建立相应的命令执行文件,如cmd_hello.c 文件。
a、注意命名的规范,文件名必须是cmd_xxx.c 才行。
b、文件的内容也是有格式要求的,如函数的格式。
c、定义U_BOOT_CMD具体部分。
d、如果命令不需要参数,则把maxargs设置为1即可。
cmd_hello.c 文件的具体内容为:
#include <common.h>
#include <command.h>
/* 增加新的命令 - hello */
/* 执行hello命令,打印hello world! */
int do_hello(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
{
printf("hello world!\n");
return 0;
}
/* name,maxargs,repeatable,command,"usage","help" */
U_BOOT_CMD(
hello, 1, 1, do_hello,
"Usage - print hello world. Short description.",
"\n"
"hello command help. Long description.\n"
"hello world ... "
);
3、修改common/Makefile文件,在#command下添加相应命令。
如:COBJS-$(CONFIG_CMD_HELLO) += cmd_hello.o
# add new command #
COBJS-$(CONFIG_CMD_HELLO) += cmd_hello.o4、重新编译uboot。
make distclean
make mini2440_config
make5、将生成的uboot.bin 文件下载到开发板运行。
a、在uboot界面下输入help查看所有命令
[u-boot@MINI2440]# ?
go - start application at address 'addr'
hello - Usage - print hello world. Short description.
help - print command description/usagehello 命令存在!这里只显示部分命令内容。b、查看hello 命令帮助
[u-boot@MINI2440]# ? hello
hello - Usage - print hello world. Short description.
Usage:
hello
hello command help. Long description.
hello world ... c、执行hello 命令[u-boot@MINI2440]# hello
hello world!
837




被折叠的 条评论
为什么被折叠?



