u-boot分析之命令实现(四)
目录
u-boot(四)命令实现
- 分析run_command
- 小结
- 自定义一个命令
- 代码
- makefile
u-boot(四)命令实现
命令是如何实现的?
- 输入命令
- 执行函数,根据命令去寻找函数
所以会有一个命令的结构体[name,fun],该结构体含有命令的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_LONGHELPchar *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
};
查看函数,可以发现是在__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"
}
- repeatable 可重复,指的是直接按回车是否继续执行上次命令
- usage,短的help,指的是直接输入help查看的所有命令显示的帮助
- help,具体的help,指的是help cmd 查看的具体的信息
小结
- 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
转载:https://www.cnblogs.com/zongzi10010/p/10023679.html
u-boot分析之命令实现(四)相关推荐
- linux 查看握手时间,实战:tcpdump抓包分析三次握手四次挥手
本文档以实战的形式介绍tcpdump抓包分析三次握手四次挥手的过程. 执行tcpdump命令 tcpdump -n -i ens32 host 192.168.10.10 and 42.186.113 ...
- Linux入门基础命令(四)
Linux入门基础命令(四) 文件系统与目录结构: 1.文件和被组织成一个单根倒置树结构 2.文件系统从根目录下开始,用"/表示" 3.以.开头的文件为隐藏文件 4.路径分割的 / ...
- 20220215-CTF-MISC-BUUCTF-ningen--binwalk分析---dd命令分离--ARCHPR暴力破解
MISC-BUUCTF-ningen 人类的科学日益发展,对自然的研究依然无法满足,传闻日本科学家秋明重组了基因序列,造出了名为ningen的超自然生物.某天特工小明偶然截获了日本与俄罗斯的秘密通信, ...
- Spring Boot 2.0系列文章(四):Spring Boot 2.0 源码阅读环境搭建
前提 前几天面试的时候,被问过 Spring Boot 的自动配置源码怎么实现的,没看过源码的我只能投降��了. 这不,赶紧来补补了,所以才有了这篇文章的出现,Spring Boot 2. 0 源码阅 ...
- Linux 基础命令(四)—— 文件的权限和访问控制列表(ACL)
Linux 基础命令(四)-- 文件的权限和访问控制列表(ACL) Published On July 24, 2017 前言 查看全文 http://www.taodudu.cc/news/sho ...
- celery源码分析:multi命令分析
celery源码分析 本文环境python3.5.2,celery4.0.2,django1.10.x系列 celery简介 celery是一款异步任务框架,基于AMQP协议的任务调度框架.使用的场景 ...
- 新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t
新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...
- ffmpeg.c学习-1-框架分析及命令行解析
ffmpeg.c学习-1-框架分析及命令行解析 目录 掌握ffmpeg.c的意义 ffmpeg框架分析 ffmpeg对应的⽂件 ffmpeg程序框架流程 命令行解析 1. 掌握ffmpeg.c的意义 ...
- 自学Linux命令的四种方法
本文由 极客范 - 小道空空 翻译自 Danny Stieben.欢迎加入极客翻译小组,同我们一道翻译与分享.转载请参见文章末尾处的要求. 如果你想成为Linux高手,那么掌握一些Linux命令是必不 ...
最新文章
- 解密TDE加密数据库
- Explaining the Genesis Block in Ethereum
- 安装Putty远程终端连接工具
- Linux 安装loadrunner 碰到的问题
- java爬虫工具xpath提取,2020-07-16--爬虫数据提取--xpath
- dubbo源码深度解析_scrapy框架通用爬虫、深度爬虫、分布式爬虫、分布式深度爬虫,源码解析及应用
- 【Spring】模块
- SQL Server 2016的新功能–临时数据表
- 用html5画瀑布图,一起制作瀑布图
- [IOS][已越狱]配合网易云音乐,使用Bridge快速免iTunes导入音乐到“音乐”
- 组策略设置屏保的方法
- 纯js 编canvas处理图片, 涂鸦笔、画布图片内容旋转 放大缩小 裁剪框 。兼容ie9及以上 谷歌、360、火狐浏览器
- img标签src引入svg如何修改颜色
- vi 从第几行到第几行 替换_vi全局替换方法:
- The Pursuit of Happyness 当幸福来敲门(励志电影推荐)
- Android studio使用中问题集锦
- vs为什么打了断点不断_2019年亚洲羽毛球锦标赛决赛焦点对阵:桃田贤斗VS石宇奇 松本麻佑/永原和可那VS陈清晨/贾一凡 山口茜VS何冰娇...
- 小白也能动手搭建属于自己的博客网站
- Python练习-爬虫(附加爬取中国大学排名)
- android 之TCP客户端编程
热门文章
- 【hdoj_1398】SquareCoins(母函数)
- Centos 6.8安装配置KVM
- Collection View Programming Guide for iOS---(四)---Using the Flow Layout
- [zz]淘宝子团关于kvm 调优的分享
- awk 数组用法【精华贴】
- 一次遍历等概率选取字符串中的某个字符
- 量子力学 一 基础2 作用量、普朗克常量与物质波
- UA MATH566 统计理论 概念与定理总结
- VC++ 使用BitBlt函数显示位图
- 初步学习用超图处理三维场景