歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> uboot中main_loop分析

uboot中main_loop分析

日期:2017/3/1 10:49:56   编辑:Linux編程

終於分析完了main_loop,發文紀念一下。

1。main_loop

common/main.c

main_loop又臭又長,去掉宏注釋掉的部分就只剩下一點點了。如下:

void main_loop (void)
{
#ifndef CONFIG_SYS_HUSH_PARSER
static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
int len;
int rc = 1;
int flag;
#endif

#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
char *s;
int bootdelay;
#endif

#ifdef CONFIG_AUTO_COMPLETE
install_auto_complete(); //安裝自動補全的函數,分析如下 。
#endif

#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
s = getenv ("bootdelay");
bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;

debug ("### main_loop entered: bootdelay=%d/n/n", bootdelay);


s = getenv ("bootcmd"); //獲取引導命令。分析見下面。

debug ("### main_loop: bootcmd=/"%s/"/n", s ? s : "<UNDEFINED>");

if (bootdelay >= 0 && s && !abortboot (bootdelay)) {//如果延時大於等於零,並且沒有在延時過程中接收到按鍵,則引導內核。abortboot函數的分析見下面。
run_command (s, 0); //運行引導內核的命令。這個命令是在配置頭文件中定義的。run_command的分析在下面。
}

#endif /* CONFIG_BOOTDELAY */

for (;;) {
len = readline (CONFIG_SYS_PROMPT); //CONFIG_SYS_PROMPT的意思是回顯字符,一般是“>”。這是由配置頭文件定義的

flag = 0; /* assume no special flags for now */
if (len > 0)
strcpy (lastcommand, console_buffer); //保存輸入的數據。
else if (len == 0)
flag |= CMD_FLAG_REPEAT;//如果輸入數據為零,則重復執行上次的命令,如果上次輸入的是一個命令的話

if (len == -1)
puts ("<INTERRUPT>/n");
else
rc = run_command (lastcommand, flag); //執行命令

if (rc <= 0) {//執行失敗,則清空記錄
/* invalid command or not repeatable, forget it */
lastcommand[0] = 0;
}
}

}


2。自動補全
common/common.c
int var_complete(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
{
static char tmp_buf[512];
int space;

space = last_char == '/0' || last_char == ' ' || last_char == '/t';

if (space && argc == 1)
return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);

if (!space && argc == 2)
return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);

return 0;
}

static void install_auto_complete_handler(const char *cmd,
int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]))
{
cmd_tbl_t *cmdtp;

cmdtp = find_cmd(cmd);
if (cmdtp == NULL)
return;

cmdtp->complete = complete; //命令結構體的complete指針指向傳入的函數。
}

void install_auto_complete(void)
{
#if defined(CONFIG_CMD_EDITENV)
install_auto_complete_handler("editenv", var_complete);
#endif
install_auto_complete_handler("printenv", var_complete);
install_auto_complete_handler("setenv", var_complete);
#if defined(CONFIG_CMD_RUN)
install_auto_complete_handler("run", var_complete);
#endif
}

可以看到將editenv、printenv、setenv和run的自動補全函數安裝為 var_complete。
var_complete的功能是根據給出的前綴字符串,找出所有前綴相同的命令。

每個命令在內存中用一個cmd_tbl_t 表示。
include/command.h
struct cmd_tbl_s {
char *name; /* 命令名,輸入的就是它 */
int maxargs; /* 最大參數個數 */
int repeatable; /* 允許自動重發,也就是在按下空格鍵之後執行最後一條命令。 */

int (*cmd)(struct cmd_tbl_s *, int, int, char *[]); /* 實現命令的參數 */
char *usage; /* 短的提示信息 */
#ifdef CONFIG_SYS_LONGHELP
char *help; /* 詳細的幫助信息。 */
#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;

extern cmd_tbl_t __u_boot_cmd_start;
extern cmd_tbl_t __u_boot_cmd_end;

#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}

#define U_BOOT_CMD_MKENT(name,maxargs,rep,cmd,usage,help) /
{#name, maxargs, rep, cmd, usage, help}

uboot中的命令使用U_BOOT_CMD這個宏聲明來注冊進系統,鏈接腳本會把所有的cmd_tbl_t結構體放在相鄰的地方。
鏈接腳本中的一些內容如下:
. = .;
__u_boot_cmd_start = .;
.u_boot_cmd : { *(.u_boot_cmd) }
__u_boot_cmd_end = .;
可見,__u_boot_cmd_start 和__u_boot_cmd_end 分別對應命令結構體在內存中開始和結束的地址。

3。abortboot函數的分析
abortboot是uboot在引導期間的延時函數。期間可以按鍵進入uboot的命令行。
common/main.c
static __inline__ int abortboot(int bootdelay)
{
int abort = 0;
printf("Hit any key to stop autoboot: %2d ", bootdelay);

#if defined CONFIG_ZERO_BOOTDELAY_CHECK //如果定義了這個宏,即使定義延時為0,也會檢查一次是否有按鍵按下。只要在這裡執行之前按鍵,還是能進入uboot的命令行。
if (bootdelay >= 0) {
if (tstc()) { /* we got a key press */ 測試是否有按鍵按下
(void) getc(); /* consume input */接收按鍵值
puts ("/b/b/b 0");
abort = 1; /* don't auto boot */修改標記,停止自動引導
}
}
#endif

while ((bootdelay > 0) && (!abort)) { //如果延時大於零並且停止標記沒有賦值則進入延時循環,直到延時完或者接收到了按 鍵
int i;

--bootdelay;
/* delay 100 * 10ms */ 每秒中測試按鍵100次,之後延時10ms。
for (i=0; !abort && i<100; ++i) {
if (tstc()) { /* we got a key press */
abort = 1; /* don't auto boot */*/修改標記,停止自動引導
bootdelay = 0; /* no more delay */延時歸零
(void) getc(); /* consume input */獲取按鍵
break;
}
udelay(10000);//延時10000us,也就是10ms
}

printf("/b/b/b%2d ", bootdelay);//打印當前剩余時間
}

putc('/n');
return abort;//返回結果:1-停止引導,進入命令行; 0-引導內核。
}

可以看到uboot延時的單位是秒,如果想提高延時的精度,比如想進行10ms級的延時,將udelay(10000)改為udelay(100)就可以了 。

4。引導命令
include/configs/smartarm.h

#define CONFIG_BOOTCOMMAND "run yboot "
#define CONFIG_EXTRA_ENV_SETTINGS /
"serverip=192.168.1.110/0" /
"gatewayip=192.168.1.254/0" /
"ipaddr=192.168.1.164/0" /
"bootfile=uImage/0"/
/
"upkernel=" "tftp 80008000 uImage;"/
"nand erase clean 0x00200000 $(filesize);"/
"nand write.jffs2 0x80008000 0x00200000 $(filesize);"/
"setenv kernelsize $(filesize); saveenv/0"/
/
"upsafefs=" "tftp 80008000 safefs.cramfs;"/
"nand erase clean 0x00600000 $(filesize);"/
"nand write.jffs2 0x80008000 0x00600000 $(filesize)/0"/
/
"yboot=" "nand read.jffs2 0x81000000 0x00200000 $(filesize);"/
"bootm 81000000/0"/
/
"safemode=" "setenv bootargs root=/dev/mtdblock3 console=ttyS0,115200, mem=64M rootfstype=cramfs; run yboot/0"/
/
"zhiyuan=" "run upsafefs; run upkernel/0"

可見,引導內核其實就是將內核讀取到內存中然後再跳到那個地方引導。

5。run_command
common/main.c
int run_command (const char *cmd, int flag)
{
cmd_tbl_t *cmdtp;
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;

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); //將命令拷貝到臨時命令緩沖cmdbuf

/* Process separators and check for invalid
* repeatable commands
*/
while (*str) { //str指向cmdbuf

/*
* Find separator, or string end
* Allow simple escape of ';' by writing "/;"
*/
for (inquotes = 0, sep = str; *sep; sep++) { //尋找分割符或者命令尾部。相鄰的句子之間是用;隔開的。每次處理一句命令
if ((*sep=='/'') &&
(*(sep-1) != '//'))
inquotes=!inquotes;

if (!inquotes &&
(*sep == ';') && /* separator */
( sep != str) && /* past string start */
(*(sep-1) != '//')) /* and NOT escaped */
break;
}

/*
* Limit the token to data between separators
*/
token = str; //token指向命令的開頭
if (*sep) { //如果是分隔符的話,將分隔符替換為空字符
str = sep + 1; /* start of command for next pass */str指向下一句的開頭
*sep = '/0';
}
else
str = sep; /* no more commands for next pass */如果沒有其它命令了,str指向命令尾部

/* find macros in this token and replace them */
process_macros (token, finaltoken); //將token指向的命令中的宏替換掉,如把$(kernelsize)替換成內核的大小

/* Extract arguments */
if ((argc = parse_line (finaltoken, argv)) == 0) {//將每一個參數用‘/0’隔開,argv中的每一個指針指向一個參數的起始地址。 返回值為參數的個數
rc = -1; /* no command at all */
continue;
}

/* 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;
}

/* found - check max args */
if (argc > cmdtp->maxargs) { //檢查參數個數是否過多
cmd_usage(cmdtp);
rc = -1;
continue;
}

/* OK - call function to do the command */
if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {//調用命令執行函數。這是最重要的一步。
rc = -1;
}

repeatable &= cmdtp->repeatable;//設置命令自動重復執行的標志。也就是只按下enter鍵是否可以執行最近執行的命令 .

/* Did the user stop this? */
if (had_ctrlc ()) //檢查是否有ctrl+c按鍵按下,如果有,結束當前命令。本函數並沒有從中斷接收字符,接收ctrl+c的是一些執行命令的函數。
return -1; /* if stopped then not repeatable */
}

return rc ? rc : repeatable;
}

Copyright © Linux教程網 All Rights Reserved