標籤:uboot repeat 基於 目錄 ifd maximum 基本 UNC mes
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_HELLOint 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
}
uboot自訂添加命令