【u-boot添加命令】
在u-boot/common/main.c中
void main_loop(void)
{
……
run_command(xxx,xxx);
……
}
int run_command(const char *cmd, int flag)
{
cmd_tbl_t*cmdtp;
……
/*Extract arguments */
argc= parse_line (finaltoken, argv);
/*Look up command in command table */
if((cmdtp = find_cmd(argv[0])) == NULL) {
printf("Unknown command '%s' - try 'help'\n", argv[0]);
rc= -1; /* give up after bad command */
continue;
}
……
}
cmd_tbl_t *find_cmd(const char *cmd)
{
……
len= ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
for(cmdtp = &__u_boot_cmd_start; //在u-boot.lds中定义了
cmdtp != &__u_boot_cmd_end; //在u-boot.lds中定义了
cmdtp++) {
if(strncmp (cmd, cmdtp->name, len) == 0) {
if(len == strlen (cmdtp->name))
return cmdtp; /* full match */
cmdtp_temp= cmdtp; /* abbreviated command ? */
n_found++;
}
}
……
}
u-boot.lds:
__u_boot_cmd_start = .;
.u_boot_cmd : { *(.u_boot_cmd) } // .u_boot_cmd段
__u_boot_cmd_end =.;
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,help}
如:
U_BOOT_CMD(bootm,CFG_MAXARGS,1,do_bootm,”xxx” ,”xxx xxx”);
展开该宏定义:
cmd_tbl_t __u_boot_cmd_bootm __attribute__((unused,section (".u_boot_cmd"))) = {bootm, CFG_MAXARGS, 1, do_bootm,”xxx” ,”xxx xxx”}
在u-boot/include/command.h中定义了命令结构体:
struct cmd_tbl_s{
char *name; /*Command Name */
int maxargs; /* maximum number ofarguments */
int repeatable; /* autorepeat allowed? */
/* Implementation function */
int (*cmd)(struct cmd_tbl_s *, int, int,char *[]);
char *usage; /*Usage message (short) */
#ifdef CFG_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
};
typedef struct cmd_tbl_s cmd_tbl_t;
添加一个test命令:
1、 在common目录下新建一个cmd_test.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>
int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char*argv[])
{
int i;
printf(“test argc:%d\n”,argc);
for(i=0;i< argc;i++)
{
printf(“argv[%d]: %s\n”,i,argv[i]);
}
return 0;
}
U_BOOT_CMD(
test, CFG_MAXARGS, 1, do_test,
"test - u-boot command test\n",
"\n"
" - xxx xxx xxx test\n"
" xxx test.\n"
);
编译:
在当前目录下的Makefile中加入编译信息:
COBJS = main.o
……
cmd_test.o
在烧写启动 u-boot 后,将在 help 中显示 test 命令,并且可以执行 test 命令。