- 论坛徽章:
- 0
|
关于U-boot中命令相关的编程:
1、命令相关的函数和定义
@main_loop:这个函数里有太多编译选项,对于smdk2410,去掉所有选项后等效下面的程序
void main_loop()
{
static char lastcommand[CFG_CBSIZE] = { 0, };
int len;
int rc = 1;
int flag;
char *s;
int bootdelay;
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 : "");
if (bootdelay >= 0 && s && !abortboot (bootdelay))
{
run_command (s, 0);//执行启动命令行,smdk2410.h中没有定义CONFIG_BOOTCOMMAND,所以没有命令执行。
}
for (;;) {
len = readline(CFG_PROMPT);//读取键入的命令行到console_buffer
flag = 0; /* assume no special flags for now */
if (len > 0)
strcpy (lastcommand, console_buffer);//拷贝命令行到lastcommand.
else if (len == 0)
flag |= CMD_FLAG_REPEAT;
if (len == -1)
puts ("\n");
else
rc = run_command (lastcommand, flag); //执行这个命令行。
if (rc ta cache\n",
"[on, off]\n"
" - enable or disable data (writethrough) cache\n"
);
其实就是定义了一个cmd_tbl_t类型的结构体变量,这个结构体变量名为__u_boot_cmd_dcache。
其中变量的五个域初始化为括号的内容。分别指明了命令名,参数个数,重复数,执行命令的函数,命令提示。
每个命令都对应这样一个变量,同时这个结构体变量的section属性为.u_boot_cmd.也就是说每个变量编译结束
在目标文件中都会有一个.u_boot_cmd的section.一个section是连接时的一个输入段,如.text,.bss,.data等都是section名。
最后由链接程序把所有的.u_boot_cmd段连接在一起,这样就组成了一个命令结构体数组。
u-boot.lds中相应脚本如下:
. = .;
__u_boot_cmd_start = .;
.u_boot_cmd : { *(.u_boot_cmd) }
__u_boot_cmd_end = .;
可以看到所有的命令结构体变量集中在__u_boot_cmd_start开始到__u_boot_cmd_end结束的连续地址范围内,
这样形成一个cmd_tbl_t类型的数组,run_command函数就是在这个数组中查找命令的。
3)实现命令处理函数。命令处理函数的格式:
void function (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
总体来说,如果要实现自己的命令,应该在include/com_confdefs.h中定义一个命令选项标志位。
在板子的配置文件中添加命令自己的选项。按照u-boot的风格,可以在common/下面添加自己的cmd_*.c,并且定义自己的命令结构体变量,如U_BOOT_CMD(
mycommand, 2, 1, do_mycommand,
"my command!\n",
"...\n"
" ..\n"
);
然后实现自己的命令处理函数do_mycommand(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/104319/showart_2080236.html |
|