您的位置:首页 > 大数据 > 人工智能

uboot main_loop函数分析

2014-07-14 16:29 549 查看

uboot main_loop函数分析

一、概述

main_loop()函数做的都是与具体平台无关的工作。主要包括的工作如下:
(1)初始化启动次数限制机制
(2)Modem功能
(3)设置软件版本号
(4)启动延迟
(5)读取命令,解析命令
二、具体分析

void main_loop (void)

{

#ifndef CFG_HUSH_PARSER

static char lastcommand[CFG_CBSIZE] = { 0, };

int len;

int rc = 1;

int flag;

#endif
#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)

char *s;

int bootdelay;

#endif

#ifdef CONFIG_PREBOOT

char *p;

#endif
(1)启动次数限制功能,启动次数限制可以被用户设置一个启动次数,然后保存在Flash存储器的特定位置,当到达启动次数后,U-Boot无法启动。该功能适合一些商业产品,通过配置不同的License限制用户重新启动系统。

#ifdef CONFIG_BOOTCOUNT_LIMIT //使能启动次数限制功能,主要用于产品的限次使用

unsigned long bootcount = 0;

unsigned long bootlimit = 0;

char *bcs;

char bcs_set[16];

#endif /* CONFIG_BOOTCOUNT_LIMIT */
#if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)

ulong bmp = 0; /* default bitmap */

extern int trab_vfd (ulong bitmap);
#ifdef CONFIG_MODEM_SUPPORT

if (do_mdm_init)

bmp = 1; /* alternate bitmap */

#endif

trab_vfd (bmp);

#endif /* CONFIG_VFD && VFD_TEST_LOGO */
#ifdef CONFIG_BOOTCOUNT_LIMIT

bootcount = bootcount_load(); //加载启动次数

bootcount++;//启动次数加一

bootcount_store (bootcount); //存储修改后的启动次数

sprintf (bcs_set, "%lu", bootcount);

setenv ("bootcount", bcs_set);//设置环境变量bootcount

bcs = getenv ("bootlimit");//获得启动次数的上限

bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;

#endif /* CONFIG_BOOTCOUNT_LIMIT */

(2)Modem功能。如果系统中有Modem,打开该功能可以接受其他用户通过电话网络的拨号请求。Modem功能通常供一些远程控制的系统使用。
#ifdef CONFIG_MODEM_SUPPORT

debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);

if (do_mdm_init) {

char *str = strdup(getenv("mdm_cmd"));

setenv ("preboot", str); /* set or delete definition */

if (str != NULL)

free (str);

mdm_init(); /* wait for modem connection */

}

#endif /* CONFIG_MODEM_SUPPORT */
(3)设置U-Boot的版本号,初始化命令自动完成功能等。动态版本号功能支持代码,version_string变量是在其他文件定义的一个字符串变量,当用户改变U-Boot版本的时候会更新该变量。打开动态版本支持功能后,U-Boot在启动的时候会显示最新的版本号。

#ifdef CONFIG_VERSION_VARIABLE

{

extern char version_string[]; //在lib_arm/board.c中定义有此字符串
setenv ("ver", version_string); /* set version variable */ //将版本号写入flash

}

#endif /* CONFIG_VERSION_VARIABLE */
#ifdef CFG_HUSH_PARSER

u_boot_hush_start ();

#endif
#ifdef CONFIG_AUTO_COMPLETE /// 初始化命令自动完成功能

install_auto_complete();

#endif
//设置命令行自动完成功能,该功能与Linux的shell类似,当用户输入一部分命令后,可以通过按下键盘上的Tab键 //补全命令的剩余部分。main_loop()函数不同的功能使用宏开关控制不仅能提高代码模块化,更主要的是针对嵌入 //式系统Flash存储器大小设计的。在嵌入式系统上,不同的系统Flash存储空间不同。对于一些Flash空间比较紧张 //的设备来说,通过宏开关关闭一些不是特别必要的功能如命令行自动完成,可以减小U-Boot编译后的文件大小。
#ifdef CONFIG_PREBOOT

if ((p = getenv ("preboot")) != NULL) {

# ifdef CONFIG_AUTOBOOT_KEYED

int prev = disable_ctrlc(1); /* disable Control C checking */ // 关闭Crtl+C组合键

# endif
# ifndef CFG_HUSH_PARSER

run_command (p, 0);

# else

parse_string_outer(p, FLAG_PARSE_SEMICOLON |

FLAG_EXIT_FROM_LOOP);

# endif
# ifdef CONFIG_AUTOBOOT_KEYED

disable_ctrlc(prev); /* restore Control C checking */ // 恢复Ctrl+C组合键

# endif

}

#endif /* CONFIG_PREBOOT */
(4)启动延迟功能,需要等待用户从串口或者网络接口输入。如果用户按下任意键打断,启动流程,会向终端打印出一个启动菜单。

#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);
# ifdef CONFIG_BOOT_RETRY_TIME

init_cmd_timeout ();// 初始化命令行超时机制

# endif /* CONFIG_BOOT_RETRY_TIME */
#ifdef CONFIG_BOOTCOUNT_LIMIT //启动次数限制检查

if (bootlimit && (bootcount > bootlimit)) {

printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",

(unsigned)bootlimit);

s = getenv ("altbootcmd");

}

else

#endif /* CONFIG_BOOTCOUNT_LIMIT */

s = getenv ("bootcmd"); // 获取启动命令参数
debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
//在启动命令非0(有启动命令)的情况下,如果在启动延时时间到之前按下键终止了,abortboot返回1,否则如果一 //直没有键按下,直到启动延时时间到,那么函数返回0。所以,如果按下键了,就可以跳过启动操作系统;如果没 //按就直接启动操作系统了。

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

# ifdef CONFIG_AUTOBOOT_KEYED

int prev = disable_ctrlc(1); /* disable Control C checking *///关闭control+c组合键

# endif
# ifndef CFG_HUSH_PARSER

run_command (s, 0); //运行启动命令,启动操作系统

# else

parse_string_outer(s, FLAG_PARSE_SEMICOLON |

FLAG_EXIT_FROM_LOOP);

# endif
# ifdef CONFIG_AUTOBOOT_KEYED

disable_ctrlc(prev); /* restore Control C checking *///恢复control+c组合键功能

# endif

}
# ifdef CONFIG_MENUKEY

if (menukey == CONFIG_MENUKEY) {//检查是否支持菜单键

s = getenv("menucmd");

if (s) {

# ifndef CFG_HUSH_PARSER

run_command (s, 0);

# else

parse_string_outer(s, FLAG_PARSE_SEMICOLON |

FLAG_EXIT_FROM_LOOP);

# endif

}

}

#endif /* CONFIG_MENUKEY */

#endif /* CONFIG_BOOTDELAY */
#ifdef CONFIG_AMIGAONEG3SE

{

extern void video_banner(void);

video_banner(); // 打印启动图标

}

#endif
(5)读取命令,解析命令。这是一个for死循环,该循环不断使用readline()函数(第463行)从控制台(一般是串口)读取用户的输入,然后解析。有关如何解析命令请参考U-Boot代码中run_command()函数的定义。

/*

* Main Loop for Monitor Command Processing

*/

#ifdef CFG_HUSH_PARSER

parse_file_outer();

/* This point is never reached */

for (;;);

#else

for (;;) {

#ifdef CONFIG_BOOT_RETRY_TIME

if (rc >= 0) {

/* Saw enough of a valid command to

* restart the timeout.

*/

reset_cmd_timeout();// 设置命令行超时

}

#endif

len = readline (CFG_PROMPT);// 读取命令,读取到的命令存储在全局变量console_buffer 中
flag = 0; /* assume no special flags for now */

if (len > 0) //命令长度大于0

strcpy (lastcommand, console_buffer);//将读取到的命令copy到lastcommand中

else if (len == 0)

flag |= CMD_FLAG_REPEAT;

#ifdef CONFIG_BOOT_RETRY_TIME

else if (len == -2) {

/* -2 means timed out, retry autoboot

*/

puts ("\nTimed out waiting for command\n");

# ifdef CONFIG_RESET_TO_RETRY

/* Reinit board to run initialization code again */

do_reset (NULL, 0, 0, NULL);

# else

return; /* retry autoboot */

# endif

}

#endif
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;

}

}

#endif /*CFG_HUSH_PARSER*/
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: