(五) u-boot 命令执行过程解析与添加自定义命令
来源:互联网 发布:win10系统安装mac os 编辑:程序博客网 时间:2024/06/07 07:05
当我们在控制台的时候,输入boot
可以启动Linux内核,那么我们以boot为例子来解析一下uboot命令的执行过程,为下一步分析uboot怎样启动Linux来做准备。
一、我们搜索boot命令
grep -wnR "boot" common
得到:common/cmd_bootm.c:1162: boot, 1, 1, do_bootd,
打开
common/cmd_bootm.c
文件
int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){ int rcode = 0;#ifndef CFG_HUSH_PARSER if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;#else if (parse_string_outer(getenv("bootcmd"), FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;#endif return rcode;}U_BOOT_CMD( boot, 1, 1, do_bootd, "boot - boot default, i.e., run 'bootcmd'\n", NULL);
其中U_BOOT_CMD命令格式如下:
U_BOOT_CMD(name,maxargs,repeatable,command,"usage","help")
各个参数的意义如下:
name:命令名,非字符串,但在U_BOOT_CMD中用“#”符号转化为字符串 maxargs:命令的最大参数个数 repeatable:是否自动重复(按Enter键是否会重复执行) command:该命令对应的响应函数指针 usage:简短的使用说明(字符串) help:较详细的使用说明(字符串)
二、U_BOOT_CMD宏分析
① U_BOOT_CMD宏在include/command.h中定义:
#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}
“##”
与“#”
都是预编译操作符,“##”
有字符串连接的功能,“#”
表示后面紧接着的是一个字符串。
② 其中的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 /* 自动补全参数 */ 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;
一个cmd_tbl_t结构体变量包含了调用一条命令的所需要的信息。
③ 其中Struct_Section在include/command.h中定义如下:
#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
凡是带有attribute ((unused,section (“.u_boot_cmd”))属性声明的变量都将被存放在”.u_boot_cmd”段中,并且即使该变量没有在代码中显式的使用编译器也不产生警告信息。
在u-Boot连接脚本board\smdk2410\u-boot.lds中定义了”.u_boot_cmd”段:
. = .;__u_boot_cmd_start = .; /*将 __u_boot_cmd_start指定为当前地址 */.u_boot_cmd : { *(.u_boot_cmd) }__u_boot_cmd_end = .; /* 将__u_boot_cmd_end指定为当前地址 */
这表明带有“.u_boot_cmd”声明的函数或变量将存储在“u_boot_cmd”段。
这样只要将u-boot所有命令对应的cmd_tbl_t变量加上“.u_boot_cmd”声明,编译器就会自动将其放在“u_boot_cmd”段,查找cmd_tbl_t变量时只要在__u_boot_cmd_start与__u_boot_cmd_end之间查找就可以了。
三、boot命令宏展开
因此“boot”命令的定义经过宏展开后如下:
cmd_tbl_t __u_boot_cmd_boot __attribute__ ((unused,section (".u_boot_cmd"))) = {boot, 1, 1, do_bootd, "boot - boot default, i.e., run 'bootcmd'\n", " NULL"}
int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){ int rcode = 0;#ifndef CFG_HUSH_PARSER if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;#else if (parse_string_outer(getenv("bootcmd"), FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;#endif return rcode;}
四、run_command分析
u-boot启动第二阶段最后跳到main_loop函数中循环,
s = getenv ("bootcmd"); if (bootdelay >= 0 && s && !abortboot (bootdelay)) { ...... run_command (s, 0); ...... }
从main_loop中我们知道,如果bootdelay时间内未按下按键则启动Linux内核,按下按键则进入uboot命令行等待用户输入命令。
用户输入命令则调取run_command函数,在该函数中有下面几个比较重要的点:
1. 从注释我们很容易知道这段代码是在对命令进行分离,并且u-boot支持’;’分离命令。
/* * 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; }
2. 分离参数
/* Extract arguments */ if ((argc = parse_line (finaltoken, argv)) == 0) { rc = -1; /* no command at all */ continue; }
3. 用第一个参数argv[0]在命令列表中寻找对应的命令,并返回一个cmd_tbl_t类型的实例。我们可以猜到这个结构体应该保函了有关命令的一系列内容。
/* 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; }
在common/command.c 中查看find_cmd函数
/*__u_boot_cmd_start与__u_boot_cmd_end间查找命令,并返回cmdtp->name命令的cmd_tbl_t结构。*/cmd_tbl_t *cmdtp; cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */ const char *p; int len; int n_found = 0; /* * Some commands allow length modifiers (like "cp.b"); * compare command name only until first dot. */ len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd); for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) { if (strncmp (cmd, cmdtp->name, len) == 0) { if (len == strlen (cmdtp->name)) return cmdtp; /* full match */ cmdtp_temp = cmdtp; /* abbreviated command ? */ n_found++; } }
五、总结命令执行过程
① 在u-boot控制台中输入“boot”命令执行时,u-boot控制台接收输入的字符串“boot”,传递给run_command函数。
② run_command函数调用common/command.c中实现的find_cmd函数在__u_boot_cmd_start与__u_boot_cmd_end间查找命令,并返回boot命令的cmd_tbl_t结构。
③ 然后run_command函数使用返回的cmd_tbl_t结构中的函数指针调用boot命令的响应函数do_bootd,从而完成了命令的执行。
六、自制u-boot命令
① 首先我们在u-boot的common目录下增加一个cmd_czg.c文件 。
复制cmd_bootm.c中的头文件到cmd_czg.c
#include <common.h>#include <watchdog.h>#include <command.h>#include <image.h>#include <malloc.h>#include <zlib.h>#include <bzlib.h>#include <environment.h>#include <asm/byteorder.h>
②、③定义命令和操作函数
#include <command.h>#include <image.h>#include <malloc.h>#include <zlib.h>#include <bzlib.h>#include <environment.h>#include <asm/byteorder.h>int do_czg (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){ int i; for(i=0;i<argc;i++) { printf ("i love you ~ argc = %d\n",argc); printf ("argv[%d] = %s\n",i,argv[i]); } return 0;}U_BOOT_CMD( czg, CFG_MAXARGS, 1, do_czg, "czg - just for test\n",/*短帮助信息*/ "czg, long help·····················\n"//长帮帮助信息 );
④ 修改common下面的makefile文件,
参考Makefile中其他文件的定义,加入一句
COBJS += cmd_czg.o
⑤ 清除配置编译u-boot并烧录
make distcleanmake 100ask24x0_configmake
阅读全文
1 0
- (五) u-boot 命令执行过程解析与添加自定义命令
- U-boot命令执行过程和添加命令的方法
- U-boot 添加自定义命令
- U-Boot添加命令的方法及U-Boot命令执行过程
- U-Boot添加命令的方法及U-Boot命令执行过程
- U-Boot添加命令的方法及U-Boot命令执行过程
- U-Boot添加menu命令的方法及U-Boot命令执行过程
- U-Boot添加命令的方法及U-Boot命令执行过程
- u-boot中添加自定义命令
- 在u-boot下添加自定义命令
- u-boot中添加自定义命令
- 添加u-boot命令
- 添加U-Boot命令
- u-boot添加命令
- U-BOOT添加命令
- u-boot添加命令
- u-boot添加命令
- 添加u-boot命令
- Marriage Match IV HDU
- python_面向对象三
- struct device
- Lintcode 背包问题
- (C++)继承、菱形继承和虚继承的那些事儿
- (五) u-boot 命令执行过程解析与添加自定义命令
- note_cloud--编辑区域中的“保存笔记”--更新笔记信息
- 摘抄
- 图---存储方式二
- 汉诺塔问题
- Android判断软键盘弹出并隐藏的简单完美解决方法(推荐)
- Linux中用户与口令的管理
- 流:简单的压缩和解压流程
- 循环结构