u-boot(四)命令实现
title: u-boot(四)命令实现
tags: linux
date: 2018-09-25 23:13:05
u-boot(四)命令实现
命令是如何实现的?
- 输入命令
- 执行函数,根据命令去寻找函数
所以会有一个命令的结构体[name,fun]
分析run_command
函数原型如下 int run_command (const char *cmd, int flag)
处理
, 空格,;等解析参数
parse_line (finaltoken, argv)example: md.w 0 ------>argv[0]= "md.w", argv[1]=" 0" ```
/* Extract arguments */
if ((argc = parse_line (finaltoken, argv)) == 0) {
rc = -1; /* no command at all */
continue;
}
```
命令搜索
if ((cmdtp = find_cmd(argv[0])) == NULL),可以发现结构体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) */
#ifdef CFG_LONGHELP
char *help; /* Help message (long) */
#endif
#ifdef CONFIG_AUTO_COMPLETE
/* do auto completion on the arguments */
int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);
#endif
};
- repeatable 可重复,指的是直接按回车是否继续执行上次命令
- usage,短的help,指的是直接输入help查看的所有命令显示的帮助
- help,具体的help,指的是help cmd 查看的具体的信息
查看函数,可以发现是在
__u_boot_cmd_start和__u_boot_cmd_end中遍历,这个地址是在链接脚本中定义的,也就是命令这个东西,有一个特殊的属性,定位到某个地址.. = .;
__u_boot_cmd_start = .;
.u_boot_cmd : { *(.u_boot_cmd) }
__u_boot_cmd_end = .;
搜索这个段属性
.u_boot_cmd,在include\command.h有这么一个宏#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}
再搜索一下这个
U_BOOT_CMD,可以发现其实就是命令了,搜索下命令bootm,在common\cmd_bootm.c中U_BOOT_CMD(
bootm, CFG_MAXARGS, 1, do_bootm,
"bootm - boot application image from memory\n",//注意,下面的几个是没有逗号,是整体
"[addr [arg ...]]\n - boot application image stored in memory\n"
"\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
"\t'arg' can be the address of an initrd image\n"
);
尝试着展开这个宏,可以发现就是定义了一个段属性特殊的结构体,也就是命令结构体
cmd_tbl_t __u_boot_cmd_bootm Struct_Section=
{
"bootm",
CFG_MAXARGS,
1,
do_bootm,
"bootm - boot application image from memory\n",
//下面的字符串是一个整体
"[addr [arg ...]]\n - boot application image stored in memory\n"
"\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
"\t'arg' can be the address of an initrd image\n"
}
小结
U-boot 的命令是用结构体存储的,这些结构体是用特殊的段属性集合到一块区域里面去,分散在各个文件中
命令解析的时候是去这个段去搜索的,这个段属性的地址是从
__u_boot_cmd_start到__u_boot_cmd_end,在链接脚本中定义的.命令结构体
struct cmd_tbl_s ;
自定义一个命令
参考common/cmd_bootm.c的头文件,编写源代码cmd_hello.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>
int do_hello (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
int i ;
printf ("hello world %d \n,", argc);
//打印下参数
for(i=0;i<argc;i++)
{
printf ("argv[%d] is %s \n",i,argv[i]);
}
return 0;
}
U_BOOT_CMD(
hello, CFG_MAXARGS, 1, do_hello,
"this is short help for hello just test\n",
"this is long help for hello just test\n"
);
makefile
修改common 的makefile ,只需要在COBJS上加上cmd_hello.o
u-boot(四)命令实现的更多相关文章
- Spring Boot(四) Mybatis-MySql
Spring Boot(四) Mybatis-MySql 0.准备数据库表 -- ---------------------------- -- Table structure for person ...
- Spring Boot启动命令参数详解及源码分析
使用过Spring Boot,我们都知道通过java -jar可以快速启动Spring Boot项目.同时,也可以通过在执行jar -jar时传递参数来进行配置.本文带大家系统的了解一下Spring ...
- spring boot(四):thymeleaf使用详解
在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...
- Spring Boot通过命令行启动发生FileNotFoundException
Spring Boot + Jersey 通过命令行启动会发生错误FileNotFoundException异常 异常信息如下: ERROR o.a.c.c.C.[Tomcat].[localhost ...
- Spring Boot(四):Thymeleaf 使用详解
在上篇文章Spring Boot (二):Web 综合开发中简单介绍了一下 Thymeleaf,这篇文章将更加全面详细的介绍 Thymeleaf 的使用.Thymeleaf 是新一代的模板引擎,在 S ...
- C#设计模式之十四命令模式(Command Pattern)【行为型】
一.引言 今天我们开始讲“行为型”设计模式的第二个模式,该模式是[命令模式],又称为行动(Action)模式或交易(Transaction)模式,英文名称是:Command Pattern.还是老套路 ...
- (转)Spring Boot(四):Thymeleaf 使用详解
http://www.ityouknow.com/springboot/2016/05/01/spring-boot-thymeleaf.html 在上篇文章Spring Boot (二):Web 综 ...
- spring boot(四) 多数据源
前言 前一篇中我们使用spring boot+mybatis创建了单一数据源,其中单一数据源不需要我们自己手动创建,spring boot自动配置在程序启动时会替我们创建好数据源. 准备工作 appl ...
- Spring boot(四)thymeleaf使用介绍
在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...
随机推荐
- Nginx lingering_close延迟关闭
L:130
- H5 history.pushState 在微信内修改url后点击用safari打开/复制链接是修改之前的页面
解决方案:url参数增加随机参数 function wxRefresh() { var replaceQueryParam = (param, newval, search) => { var ...
- JSON 解析 (三)—— FastJSON与Jackson比较
一.方便性与性能 调用方便性而言: FastJSON提供了大量静态方法,调用简洁方便 Jackson须实例化类,调用相对繁琐,可通过封装成JSON工具类简化调用 性能而言: FastJSON反序列化的 ...
- 在一般类中通过XmlWebApplicationContext对象获取应用部署上下文Context
XmlWebApplicationContext xwac = (XmlWebApplicationContext) ContextLoader.getCurrentWebApplicationCon ...
- BZOJ3291Alice与能源计划——匈牙利算法+模拟费用流
题目描述 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验.为 了方便,我们可以将火星抽象成平面,并建立平面直角坐标系.火星上一共有N个居民点 ...
- Redis——redis使用redis-dump,redis-load导出导入数据——【三】
来源 https://www.cnblogs.com/dadonggg/p/8662455.html https://blog.csdn.net/chenxinchongcn/article/deta ...
- 数据结构与算法(Python)
一.数据结构与算法概述 数据结构与算法的定义 我们把现实中大量而且非常复杂的问题以特定的数据类型(个体)和特定的存储结构(个体的关系)保存到相应的主存储器(内存)中,以及在此基础上为实现某个功能而执行 ...
- 【BZOJ4325】【NOIP2015】斗地主 搜索
题目描述 就是给你一副牌,问你最少几次能出完. 详细规则见规则 \(n\leq 23\) 题解 NOIP的数据非常水,错误一大堆的程序都能AC. 因为顺子对答案的影响最大,所以先枚举顺子进行搜索. 接 ...
- 【CF633D】Fibonacci-ish
题目描述 小y最近迷上了fibonacci数列,他定义了一种数列叫类fibonacci数列: 1.这个数列包含至少\(2\)个元素 2.\(f_0\)和\(f_1\)是任意选取的 3.\(f_{n+2 ...
- SQL循环语句 详解
SQL循环语句 declare @i int set @i=1 while @i<30 begin insert into test (userid) values(@i) set @i=@i+ ...