uboot cmdline解读来源:http://blog.csdn.net/shengzhadon/article/details/527662631、最关键的数据结构定义2、用宏定义的方式,定义变量3、概括4、举例5、简化版写法A、定义结构体B、直接用宏定义变量,将中间步骤直接省略C、应用D、系统组成E、系统动作
http://blog.csdn.net/shengzhadon/article/details/52766263
xtypedef struct cmd_tbl_s cmd_tbl_t; 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) */ char *help; /* Help message (long) */ /* do auto completion on the arguments */ int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);}; xxxxxxxxxx/* uboot/include/linker_lists.h */ _type _u_boot_list_2_##_list##_2_##_name __aligned(4) \ __attribute__((unused, \ section(".u_boot_list_2_"#_list"_2_"#_name))) /* uboot/include/command.h */ { #_name, _maxargs, _rep, _cmd, _usage, \ _CMD_HELP(_help) _CMD_COMPLETE(_comp) } ll_entry_declare(cmd_tbl_t, _name, cmd) = \ U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \ _usage, _help, _comp); U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL) /* uboot/common/cmd_net.c */ static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { return netboot_common(NFS, cmdtp, argc, argv); } U_BOOT_CMD( nfs, 3, 0, do_nfs, "boot image via network using NFS protocol", "[loadAddress] [[hostIPaddr:]bootfilename]" ); 如前所述,U_BOOT_CMD的作用就是定义一个结构体变量(struct cmd_tbl_s),并将其存放再uboot的没有被占用的section中。展开就是:
xxxxxxxxxxstruct cmd_tbl_s _u_boot_list_2_cmd_2_##_name = {};注:该变量属性是4字节对齐(aligned(4) ),存放在未被使用的section中,并将该section命名为【".u_boot_list_2_"#name】(attribute_((unused, section(".u_boot_list_2"#name))))。
命令nfs
xxxxxxxxxxU_BOOT_CMD( nfs, 3, 0, do_nfs, "boot image via network using NFS protocol", "loadAddressbootfilename]" ); 即为:
xxxxxxxxxxstruct cmd_tbl_s _u_boot_list_2_cmd_2_nfs = { "nfs", 3, 0, do_nfs, "boot image via network using NFS protocol", "[loadAddress] [[hostIPaddr:]bootfilename]", NULL }; 将_u_boot_list_2_cmd_2_nfs变量4字节对齐存放在未被使用的uboot的section中,该section被命名为.u_boot_list_2_nfs。
xxxxxxxxxxtypedef struct { char *name; char *help; int argc; int (*func)(int argc, char **argv);} cmd_tbl_s;xxxxxxxxxx //将普通字符转化为字符串变量 .help = _help, \ //_help 本身就是字符串传进来,无需转换 .argc = _argc, \ .func = _func }}xxxxxxxxxxPARSE_CMD_LINE(clean, "clean screen", 0, clean_screen);/* 实际上定义了一个变量 cmd_list_clean */即:cmd_tbl_s cmd_list_clean = {"clean", "clean screen", 0, clean_screen};通过多条
PARSE_CMD_LINE宏,注册(生成)多条命令行子功能。只需要将其追加到一个列表中即可遍历访问:
cmd_tbl_s *cmd_tbl[] =
{
&cmd_list_clean,
&cmd_list_help,
};
主函数
main运行一个while(1)列循环,然后不断监测用户输入。如检测到用户输入了
clean则清空屏幕。 或者其它命令,解析成命令和相应参数