Chinaunix首页 | 论坛 | 博客
  • 博客访问: 235526
  • 博文数量: 27
  • 博客积分: 358
  • 博客等级: 一等列兵
  • 技术积分: 291
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 17:35
文章分类

全部博文(27)

文章存档

2015年(1)

2014年(4)

2013年(6)

2012年(4)

2011年(12)

分类: LINUX

2013-03-12 14:16:53

原文地址:U_BOOT_CMD 分析 作者:wangcong02345

start_armboot
{
}
main_loop
{
1. 环境变量mtdparts, 调用mtdparts_init
2. 如果在启动过程中 无空格键按下则boot_zImage
有空格键按下则 run_command("menu",0)
3. shell过程,读取用户端输入并执行相应的命令
{
从输入端获得命令,保存在全局变量comsole_buffer中
执行 run_command();
}
}

run_command
{
1. 对\;进行解析,划分出一个个完整的命令
2. 然后对每一个完整的命令执行:
{
parse_line
{
line 是指整个的命令行字符串;
假设line = nboot 0x32000000 kernel; bootm 0x32000000
先去掉开头的空格,
然后对命令进行解析,找到空格之后将空格替换为\0,这样解析出命令和参数
}
find_cmd(argv[0])
{
从 __u_boot_cmd_start 到 __u_boot_cmd_end 的array进行遍历,
从找到的cmd_tbl_t中,字符串寻找cmdtp->name与argv[0]相同的命令
}
找到命令后,调用cmd_tbl_t->cmd调用函数
}
}

1.
U_BOOT_CMD(
    mtdparts,   6,  0,  do_jffs2_mtdparts,
    "mtdparts- define flash/nand partitions\n",
    "\n"
);

cmd_tbl_t __u_boot_cmd_mtdparts Struct_Section = {mtdparts, 6, 0, do_jffs2_mtdparts, usage, help};

以下从doc/README.command 翻译
要想在u-boot中添加命令,必须新建一个command structure。要想创建一个command structure,则首先包含 "command.h" 头文件,然后用U_BOOT_CMD宏填充 cmd_tbl_t struct。
经过宏展开后新创建的这个结构体的名字会__u_boot_cmd开头,然后连接器会把这个结构体连接到指定的section上。
这样link才能从代码中提取所有的命令,生成一个静态的array。这样就可以通过遍历一个以__u_boot_cmd_starty开头的数组找到所要的命令。

1.
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 *[]);
    char        *usage;     /* Usage message    (short) */
    char        *help;      /* Help  message    (long)  */
#ifdef CONFIG_AUTO_COMPLETE
    /* do auto completion on the arguments */
    int     (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);
#endif
};

#define Struct_Section  __attribute__ ((unused,section (".u_boot_cmd")))

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

typedef struct cmd_tbl_s    cmd_tbl_t;

extern cmd_tbl_t  __u_boot_cmd_start;
extern cmd_tbl_t  __u_boot_cmd_end;

这里要看的是##name和#name这两个操作.##name将字符直接跟在后面, #name会将name这个字符中以"..."的形式放置。
1. 
U_BOOT_CMD(
    tftpboot,   3,  1,  do_tftpb,
    "tftpboot- boot image via network using TFTP protocol\n",
    "[loadAddress] [bootfilename]\n"
);

usage=    "tftpboot- boot image via network using TFTP protocol\n";
help=    "[loadAddress] [bootfilename]\n";

cmd_tbl_t __u_boot_cmd_tftpboot __attribute__ ((unused,section (".u_boot_cmd"))) = {"tftpboot", 3, 1, do_tftpb,     "tftpboot- boot image via network using TFTP protocol\n",
    "[loadAddress] [bootfilename]\n"};
    
int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    return netboot_common (TFTP, cmdtp, argc, argv);
}


阅读(1595) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~