Uboot命令U BOOT CMD分析

2021-09-08 05:13:04 字數 3626 閱讀 5032

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中定義:

#define u_boot_cmd(name,maxargs,rep,cmd,usage,help) \

cmd_tbl_t __u_boot_cmd_##name struct_section =

「##」與「#」都是預編譯操作符,「##」有字串連線的功能,「#」表示後面緊接著的是乙個字串。

其中struct_section在include/command.h中定義如下:

#define struct_section  __attribute__ ((unused,section (".u_boot_cmd")))
凡是帶有attribute ((unused,section (「.u_boot_cmd」))屬性宣告的變數都將被存放在」.u_boot_cmd」段中,並且即使該變數沒有在**中顯式的使用編譯器也不產生警告資訊。
在u-boot連線指令碼 u-boot.lds中定義了.u_boot_cmd段:

. = .;

__u_boot_cmd_start = .; /*將 __u_boot_cmd_start指定為當前位址 */

.u_boot_cmd :

__u_boot_cmd_end = .; /* 將__u_boot_cmd_end指定為當前位址 */

這表明帶有「.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中定義如下:

struct cmd_tbl_s ;

typedef struct cmd_tbl_s cmd_tbl_t;

乙個cmd_tbl_t結構體變數包含了呼叫一條命令的所需要的資訊。

在記憶體中所有cmd_tbl_t型別記憶體分布如下

以「boot」命令的定義經過巨集展開後如下:

u_boot_cmd(boot,1,1,do_bootd,"boot    - boot default, i.e., run 'bootcmd'\n","null");

cmd_tbl_t __u_boot_cmd_boot __attribute__ ((unused,section (".u_boot_cmd"))) = ;

int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *ar**)

u-boot啟動第二階段最後跳到main_loop函式中迴圈

s = getenv ("bootcmd");

if (bootdelay >= 0 && s && !abortboot (bootdelay))

從main_loop中我們知道,如果bootdelay時間內未按下按鍵則啟動linux核心,按下按鍵則進入uboot命令列等待使用者輸入命令。

使用者輸入命令則調取run_command函式,在該函式中有下面幾個比較重要的點:

從注釋我們很容易知道這段**是在對命令進行分離,並且u-boot支援』;』分離命令。

/*

* find separator, or string end

* allow ****** escape of ';' by writing "\;"

*/for (inquotes = 0, sep = str; *sep; sep++)

分離引數

/* extract arguments */

if ((argc = parse_line (finaltoken, ar**)) == 0)

用第乙個引數ar**[0]在命令列表中尋找對應的命令,並返回乙個cmd_tbl_t型別的例項。我們可以猜到這個結構體應該保函了有關命令的一系列內容。

/* look up command in command table */

if ((cmdtp = find_cmd(ar**[0])) == null)

在common/command.c 中檢視find_cmd函式

/*__u_boot_cmd_start與__u_boot_cmd_end間查詢命令,並返回cmdtp->name命令的cmd_tbl_t結構。*/

cmd_tbl_t *cmdtp;

cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*init value */

const char *p;

int len;

int n_found = 0;

/** some commands allow length modifiers (like "cp.b");

* compare command name only until first dot.

*/len = ((p = strchr(cmd, '.')) == null) ? strlen (cmd) : (p - cmd);

for (cmdtp = &__u_boot_cmd_start;

cmdtp != &__u_boot_cmd_end;

cmdtp++)

}

① 在u-boot控制台中輸入「boot」命令執行時,u-boot控制台接收輸入的字串「boot」,傳遞給run_command函式。

② run_command函式呼叫common/command.c中實現的find_cmd函式在__u_boot_cmd_start與__u_boot_cmd_end間查詢命令,並返回boot命令的cmd_tbl_t結構。

③ 然後run_command函式使用返回的cmd_tbl_t結構中的函式指標呼叫boot命令的響應函式do_bootd,從而完成了命令的執行。

U boot 新增命令 U BOOT CMD

u boot的每乙個命令都是通過u boot cmd巨集定義的。這個巨集在include command.h標頭檔案中定義,每乙個命令定義乙個cmd tbl t結構體。命令巨集u boot cmd define u boot cmd name,maxargs,rep,cmd,usage,help c...

U boot 新增命令 U BOOT CMD

u boot的每乙個命令都是通過u boot cmd巨集定義的。這個巨集在include command.h標頭檔案中定義,每乙個命令定義乙個cmd tbl t結構體。命令巨集u boot cmd define u boot cmd name,maxargs,rep,cmd,usage,help c...

U boot 新增命令 U BOOT CMD

2013 02 01 16 51 49 分類 linux u boot 新增命令 u boot cmd u boot的每乙個命令都是通過u boot cmd巨集定義的。這個巨集在include command.h標頭檔案中定義,每乙個命令定義乙個cmd tbl t結構體。命令巨集u boot cmd...