1、添加命令
1.u-boot的命令格式:
U_BOOT_CMD(name,maxargs,repeatable,command,”usage”,"help")
name:命令的名字,不是一個字符串;
maxargs:最大的參數個數;
repeatable:命令是可重復的;
command:對應的函數指針
2.在uboot/common目錄下,隨便找一個cmd_xxx.c文件,將cmd_xxx.c文件拷貝一下,並重新命名為cmd_hello.c
cp cmd_xxx.c cmd_hello.c
3.進入到cmd_hello.c中,修改
a:修改宏U_BOOT_CMD
U_BOOT_CMD宏參數有6個:
第一個參數:添加的命令的名字
第二個參數:添加的命令最多有幾個參數(注意,假如你設置的參數個數是3,而實際的參數個數是4,那么執行命令會輸出幫助信息的)
第三個參數:是否重復(1重復,0不重復)(即按下Enter鍵的時候,自動執行上次的命令)
第四個參數:執行函數,即運行了命令具體做啥會在這個函數中體現出來
第五個參數:幫助信息(short)
第六個參數:幫助信息(long)
b:定義執行函數
執行函數格式:int do_hello(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
然后在該函數中添加你想要做的事,比如打印一行信息
int do_hello(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
printf("hello world\n");
return 0;
}
c:uboot中添加命令基本操作已經做完,但是還差一步,就是將該命令添加進uboot中,
之前的操作雖然添加了一個cmd_hello.c文件,但是還沒有將該文件放進Uboot的代碼中,
所以,我們需要在uboot下common文件的Makfile中添加一行:
COBJS-y += cmd_hello.o
#include<command.h> #include<common.h> #ifdef CONFIG_CMD_HELLO int do_hello(cmd_tbl_t *cmdtp,int flag,int argc,char *argv) { printf("my test \n"); return 0; } U_BOOT_CMD( hello,1,0,do_hello,"usage:test\n","help:test\n" ); #endif
2、uboot命令解析
uboot的命令基本都是基於宏U_BOOT_CMD實現的,所以解析下該宏:
通過搜索,我們找到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}
1.#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
由定義可知該命令在鏈接的時候鏈接的地址或者說該命令存放的位置是u_boot_cmd section
2.typedef struct cmd_tbl_s cmd_tbl_t 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) */
char *help
}
1、u-boot添加命令
1) u-boot正常運行后等待用戶輸入命令,想要為u-boot添加新命令,操作步驟很簡單,原因是u-boot有現成的命令添加框架。
2)第一步,在common文件夾下新增c文件,cmd_xx.c,xx就是新增命令。
3)第二步,cmd_xx.c文件中添加頭文件,及命令聲明和命令實現函數。
#include <common.h>
#include <command.h>
static int do_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
const int len = ll_entry_count(cmd_tbl_t, cmd);
return _do_help(start, len, cmdtp, flag, argc, argv);
}
U_BOOT_CMD(
help, CONFIG_SYS_MAXARGS, 1, do_help,
"print command description/usage",
"\n"
" - print brief description of all commands\n"
"help command ...\n"
" - print detailed usage of 'command'"
);
4)第三步,common/Makefile文件添加cmd_xx.o,實現命令編譯,完成命令添加。
5)如上,只需要三步,同把大象裝進冰箱是一個道理。