1. 程式人生 > >uboot中命令列模式以及命令處理

uboot中命令列模式以及命令處理

AM335X中的巨集定義

這裡寫圖片描述
這裡寫圖片描述
1、需要開啟哪些巨集
CONFIG_CMDLINE
表示是否支援命令列模式,定義如下:
configs/am335x_evm_defconfig :CONFIG_CMDLINE=y
CONFIG_SYS_GENERIC_BOARD
用於定義板子為通用型別的板子。開啟這個巨集之後,common/board_f.c和common/board_r.c才會被編譯進去,否則,需要自己實現。
configs/am335x_evm_defconfig :CONFIG_SYS_GENERIC_BOARD=y
CONFIG_SYS_PROMPT
命令列模式下的提示符
configs/am335x_evm_defconfig :CONFIG_SYS_PROMPT=”=> ”
CONFIG_SYS_HUSH_PARSER
表示使用使用hush來對命令列進行解析
configs/am335x_evm_defconfig :CONFIG_SYS_HUSH_PARSER=y
對應命令需要開啟對應命令的巨集
以bootm命令為例,如果要支援bootm,則需要開啟CONFIG_CMD_BOOTM巨集,具體可以參考cmd/Makefile
這裡寫圖片描述

API
● U_BOOT_CMD
#define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help)
定義一個命令。
● cmd_process
enum command_ret_t cmd_process(int flag, int argc, char * const argv[], int *repeatable, ulong *ticks)
命令的處理函式,命令是作為argv[0]傳入。

這個是命令的描述結構體 每個這樣的結構體代表一個命令
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 * const []);
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 const argv[], char last_char, int maxv, char *cmdv[]);
#endif
};

引數說明如下:
● name:定義一個命令的名字。 其實就是執行的命令的字串。這個要注意。
● maxargs:這個命令支援的最大引數
● repeatable:是否需要重複
● cmd:命令處理函式的地址
● usage:字串,使用說明

● help:字串,幫助

在uboot 2016.5版本里的bootm命令 是這樣表示的
U_BOOT_CMD(
bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
“boot application image from memory”, bootm_help_text
);
U_BOOT_CMD是一個巨集指令 替換就是

#define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)

#define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
ll_entry_declare(cmd_tbl_t, _name, cmd) = \
U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
_usage, _help, _comp);

#define ll_entry_declare(_type, _name, _list) \
type _u_boot_list_2##list##_2##_name __aligned(4) \
attribute((unused, \
section(“.u_boot_list_2_”#list”_2“#_name)))

ll_entry_declare(cmd_tbl_t, _name, cmd)

_type = cmd_tbl_t
_name = bootm
_cmd = do_bootm

cmd_tbl_t _u_boot_list_2_cmd_2_bootm=
{
_name=bootm,
_maxargs=CONFIG_SYS_MAXARGS,
_rep=1,
_cmd=do_bootm,
_usage=”boot application image from memory”,
_help=bootm_help_text,
_comp=NULL,
}
並且這個資料結構給存放到了 .u_boot_list_2_cmd_2_bootm段中!!!和我們上述的完全一致。

命令的處理
1、簡單流程說明:
假設傳進來的命令是cmd。
* 獲取命令表
* 從命令表中搜索和cmd匹配的項
* 執行對應項中的命令

cmd_tbl_t *find_cmd(const char *cmd)
{
//獲得指向cmd_tbl_t 的指標
cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
//獲得命令表的長度
const int len = ll_entry_count(cmd_tbl_t, cmd);
//以命令表的指標和命令表的長度為引數,查詢和cmd匹配的表項,也就是cmd_tbl_t結構,並返回給呼叫者
return find_cmd_tbl(cmd, start, len);
}

cmd_tbl_t *find_cmd_tbl(const char *cmd, cmd_tbl_t *table, int table_len)
{
#ifdef CONFIG_CMDLINE
cmd_tbl_t *cmdtp;
cmd_tbl_t cmdtp_temp = table; / Init value */
const char *p;
int len;
int n_found = 0;

if (!cmd)
    return NULL;
/*
 * Some commands allow length modifiers (like "cp.b");
 * compare command name only until first dot.
 */
len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);

// 通過指標遞增的方式,查詢table中的每一個cmd_tbl_t
for (cmdtp = table; cmdtp != table + table_len; cmdtp++) {
if (strncmp(cmd, cmdtp->name, len) == 0) {
if (len == strlen(cmdtp->name))
return cmdtp; /* full match */
// 如果是命令字串和表項中的name完全匹配,包括長度一致的,則直接返回
cmdtp_temp = cmdtp; /* abbreviated command ? */
n_found++;
// 如果命令字串和表項中的name的前面部分匹配的話(我們稱為部分匹配),暫時儲存下來,並且記錄有幾個這樣的表項,主要是為了支援自動補全的功能
}
}
if (n_found == 1) { /* exactly one match */
return cmdtp_temp;
// 如果部分匹配的表項是唯一的話,則可以將這個表項返回,主要是為了支援自動補全的功能
}
#endif /* CONFIG_CMDLINE */

include/linker_lists.h
#define ll_entry_start(_type, _list) \
({ \
static char start[0] aligned(4) __attribute((unused, \
section(“.u_boot_list_2_”#_list”_1”))); \
(_type *)&start; \
})
// 因為傳進來的_list是cmd,所以這裡就是獲取命令表的起始地址,也就是.u_boot_list_2_cmd_1的地址,

#define ll_entry_end(_type, _list) \
({ \
static char end[0] aligned(4) __attribute((unused, \
section(“.u_boot_list_2_”#_list”_3”))); \
(_type *)&end; \
})
// 因為傳進來的_list是cmd,所以這裡就是獲取命令表的結束地址,也就是.u_boot_list_2_cmd_3的地址,

#define ll_entry_count(_type, _list) \
({ \
_type *start = ll_entry_start(_type, _list); \
_type *end = ll_entry_end(_type, _list); \
unsigned int _ll_result = end - start; \
_ll_result; \
})
// 因為傳進來的_list是cmd,所以這裡就是計算命令表的長度

執行對應表項中的命令——cmd_call
通過呼叫cmd_call可以執行命令表項cmd_tbl_t 中的命令
common/command.c
static int cmd_call(cmd_tbl_t cmdtp, int flag, int argc, char const argv[])
{
int result;

result = (cmdtp->cmd)(cmdtp, flag, argc, argv);

// 直接執行命令表項cmd_tbl_t 中的cmd命令處理函式
if (result)
debug(“Command failed, result=%d\n”, result);
// 命令返回非0值時,報錯
return result;
}
命令處理函式——cmd_process
程式碼如下:
common/command.c
enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
int *repeatable, ulong *ticks)
{
enum command_ret_t rc = CMD_RET_SUCCESS;
cmd_tbl_t *cmdtp;

/* Look up command in command table */
cmdtp = find_cmd(argv[0]);
    // 第一個引數argv[0]表示命令,呼叫find_cmd獲取命令對應的表項cmd_tbl_t。
if (cmdtp == NULL) {
    printf("Unknown command '%s' - try 'help'\n", argv[0]);
    return 1;
}

/* found - check max args */
if (argc > cmdtp->maxargs)
    rc = CMD_RET_USAGE;
    // 檢測引數是否正常

/* If OK so far, then do the command */
if (!rc) {
    if (ticks)
        *ticks = get_timer(0);
    rc = cmd_call(cmdtp, flag, argc, argv);
            // 呼叫cmd_call執行命令表項中的命令,成功的話需要返回0值
    if (ticks)
        *ticks = get_timer(*ticks);
            // 判斷命令執行的時間
    *repeatable &= cmdtp->repeatable;
            // 這個命令執行的重複次數存放在repeatable中的
}
if (rc == CMD_RET_USAGE)
    rc = cmd_usage(cmdtp);
    // 命令格式有問題,列印幫助資訊
return rc;

}
返回0表示執行成功,返回非0值表示執行失敗。
後續需要執行一個命令的時候,直接呼叫cmd_process即可

命令列模式的流程
命令列模式有兩種簡單的方式。正常模式是簡單地獲取串列埠資料、解析和處理命令。
hush模式則是指命令的接收和解析使用busybox的hush工具,對應程式碼是hush.c。
關於hush模式的作用和使用自己還不是很清楚,還要再研究一下。這裡簡單的寫一點流程。
1、入口
通過《[uboot] (第五章)uboot流程——uboot啟動流程》,我們知道了uboot在執行完所有初始化程式之後,呼叫run_main_loop進入主迴圈。
通過主迴圈進入了命令列模式。
common/board_r.c
static int run_main_loop(void)
{
/* main_loop() can return to retry autoboot, if so just run it again */
for (;;)
main_loop();
// 這裡進入了主迴圈,而autoboot也是在主迴圈裡面實現
return 0;
}
/* We come here after U-Boot is initialised and ready to process commands */
void main_loop(void)
{
const char *s;

bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");

#ifndef CONFIG_SYS_GENERIC_BOARD
puts(“Warning: Your board does not use generic board. Please read\n”);
puts(“doc/README.generic-board and take action. Boards not\n”);
puts(“upgraded by the late 2014 may break or be removed.\n”);
#endif

#ifdef CONFIG_VERSION_VARIABLE
setenv(“ver”, version_string); /* set version variable */
#endif /* CONFIG_VERSION_VARIABLE */

cli_init();

run_preboot_environment_command();

#if defined(CONFIG_UPDATE_TFTP)
update_tftp(0UL, NULL, NULL);
#endif /* CONFIG_UPDATE_TFTP */

s = bootdelay_process();
if (cli_process_fdt(&s))
    cli_secure_boot_cmd(s);

autoboot_command(s);

cli_loop();
panic("No CLI available");

}

通過呼叫cli_loop進入了命令列模式,並且不允許返回。
common/cli.c
void cli_loop(void)
{
#ifdef CONFIG_SYS_HUSH_PARSER
parse_file_outer();
// 這裡進入hush命令模式
/* This point is never reached */
for (;;);
#elif defined(CONFIG_CMDLINE)
// 這裡進入通用命令列模式
cli_simple_loop();
#else
// 說明沒有開啟CONFIG_CMDLINE巨集,無法進入命令列模式,直接列印一個提示
printf(“## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n”);
#endif /CONFIG_SYS_HUSH_PARSER/
}

2、通用命令列模式
程式碼如下:
void cli_simple_loop(void)
{
static char lastcommand[CONFIG_SYS_CBSIZE + 1] = { 0, };

int len;
int flag;
int rc = 1;

for (;;) {

    len = cli_readline(CONFIG_SYS_PROMPT);

// 列印CONFIG_SYS_PROMPT,然後從串列埠讀取一行作為命令,儲存在console_buffer中
// 在tiny210中定義如下:./configs/tiny210_defconfig:203:CONFIG_SYS_PROMPT=”TINY210 => “

    flag = 0;   /* assume no special flags for now */
    if (len > 0)
        strlcpy(lastcommand, console_buffer,
            CONFIG_SYS_CBSIZE + 1);

// 如果獲得了一個新行時,命令會儲存在console_buffer,將命令複製到lastcommand中
else if (len == 0)
flag |= CMD_FLAG_REPEAT;
// 只是得到一個單純的換行符時,設定重複標識,後續重複執行上一次命令

    if (len == -1)
        puts("<INTERRUPT>\n");

// 獲得非資料值時,直接列印中斷
else
rc = run_command_repeatable(lastcommand, flag);
// 否則,執行lastcommand中的命令

    if (rc <= 0) {
        /* invalid command or not repeatable, forget it */
        lastcommand[0] = 0;

// 命令執行出錯時,清空lastcommand,防止下一次重複執行這個命令
}
}
}

/* run_command_repeatable實現如下 */
int run_command_repeatable(const char *cmd, int flag)
{
return cli_simple_run_command(cmd, flag);
}

/* cli_simple_run_command實現如下 */
int cli_simple_run_command(const char *cmd, int flag)
{
char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
char token; / start of token in cmdbuf */
char sep; / end of token (separator) in cmdbuf */
char finaltoken[CONFIG_SYS_CBSIZE];
char *str = cmdbuf;
char argv[CONFIG_SYS_MAXARGS + 1]; / NULL terminated */
int argc, inquotes;
int repeatable = 1;
int rc = 0;

debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
if (DEBUG_PARSER) {
    /* use puts - string may be loooong */
    puts(cmd ? cmd : "NULL");
    puts("\"\n");
}
clear_ctrlc();      /* forget any previous Control C */

if (!cmd || !*cmd)
    return -1;  /* empty command */

if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
    puts("## Command too long!\n");
    return -1;
}

strcpy(cmdbuf, cmd);

/* Process separators and check for invalid
 * repeatable commands
 */

debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
while (*str) {

// 這裡過濾掉一些對命令進行處理的部分程式碼
/* find macros in this token and replace them */
cli_simple_process_macros(token, finaltoken);

    /* Extract arguments */
    argc = cli_simple_parse_line(finaltoken, argv);

// 對命令進行加工處理,轉化成argv和argc格式。
if (argc == 0) {
rc = -1; /* no command at all */
continue;
}

    if (cmd_process(flag, argc, argv, &repeatable, NULL))
        rc = -1;

// 呼叫cmd_process對命令進行處理
// 關於這個的實現我們在上述三、4中說明過了

    /* Did the user stop this? */
    if (had_ctrlc())
        return -1;  /* if stopped then not repeatable */
}

return rc ? rc : repeatable;

}