uboot命令实现举例
之前一直在想,为什么没有人出一个完全从零写uboot和移植剪裁内核的教程,后来发现,确实这样的教程十分不容易,还有就是我们也没有必要花那么多时间去做别人已经做好的事情,所以,一般而言我们只用管怎么实现功能。(把更多的时间给音频、视频、图像类等的算法去更加符合经济效益)。
我们知道uboot的菜单中是支持很多指令的,我们今天就来增加一条hello指令。分析uboot知道,我们指令依托于一个 run_command函数,而且支持分号,比如 print;ls就会先执行print然后接着执行ls指令。其实uboot的命令和linux内核学习的时候一样,每个指令就是一个可执行程序。
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
};
U_BOOT_CMD第一个参数是命令的名字,第二个参数是参数最长数字,第三个参数代表是否可重复,为1表示可重复,可重复就是在输入执行
了一次之后,不用再输入,直接回车就可以达到之前输入指令的效果。第四个参数是一个函数指针,指向要执行命令的函数体。第五个参数是
用法信息的简短说明。第六个参数是命令的长帮助信息。
U_BOOT_CMD(
bootm, CFG_MAXARGS, , 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"
#ifdef CONFIG_OF_FLAT_TREE
"\tWhen booting a Linux kernel which requires a flat device-tree\n"
"\ta third argument is required which is the address of the of the\n"
"\tdevice-tree blob. To boot that kernel without an initrd image,\n"
"\tuse a '-' for the second argument. If you do not pass a third\n"
"\ta bd_info struct will be passed instead\n"
#endif
);
#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
#ifdef CFG_LONGHELP
#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}
#else /* no long help info */
#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}
#endif /* CFG_LONGHELP */
CFG_LONGHELP 代表是否配置长的帮助消息,在我们的uboot中是支持的。关C语言的#和##就不再赘述了,字符串和连接符。
这里说一下:#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))unused:This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable.
该属性附加到变量上,意味着该变量可能未被使用。 GCC不会为这个变量产生警告。

参考连接:点击此处
在100ask目录下的lds文件中有:
. = .;
__u_boot_cmd_start = .;
.u_boot_cmd : { *(.u_boot_cmd) }
__u_boot_cmd_end = .;
其中的u_boot_cmd段就是由上面的__attribute__指定的属性。
如果想实现一个简单的hello命令,只需要新建一个文件比如cmd_hello.c,common目录下的Makefile增加一个cmd_hello.o,

源文件放在common目录下。
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-;i<argc;i++)
{
printf("argv[%d],%s\n",i,argv[i]);
} return ;
} U_BOOT_CMD(
hello, CFG_MAXARGS, , do_hello,
"hello-just a test!\n",
"hello long help\n"
"do you know how to add a command?\n" );

就这样,就添加了一个命令了。
一般而言,uboot已经给我们写好了的东西,我们就不再多花时间去了解每一条语句了,先知道怎么用,试想,当初学C语言的时候,我们不也不知道printf怎么实现的吗,还不是能够学到今天这一步,要是当初一来学习就去研究printf怎么实现的输出到屏幕,那么一定坚持不了学习C语言,把时间花在更需要的地方。
uboot命令实现举例的更多相关文章
- U-Boot命令大全(功能参数及用法)
U-Boot上电启动后,按任意键可以退出自动启动状态,进入命令行. U-Boot 2010.03 (Sep 25 2011 - 16:18:50) DRAM: 64 MB Flash: ...
- Uboot mmc命令解析&NAND flash uboot命令详解
转载:http://blog.csdn.net/simonjay2007/article/details/43198353 一:mmc的命令如下: 1:对mmc读操作 mmc read addr bl ...
- uboot命令(1):mmc命令
版权声明 更新:2017-06-07博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 今天在进行Android分区修改的时候发 ...
- 1.ok6410移植bootloader,移植u-boot,学习u-boot命令
ok6410移植u-boot 既然是移植u-boot当然首先需要u-boot源码,这里的u-boot代码是由国嵌提供的. 一.配置编译u-boot A. 解压 u-boot 压缩文件 B. 进入解压生 ...
- uboot命令分析+实现【转】
转自:http://xouou.iteye.com/blog/2150061 先贴一个重要结构,位于uboot/include/command.h,这个结构代表每个uboot命令 struct cmd ...
- 编译u-boot命令和u-boot常用命令
一.编译u-boot命令 1.配置开发板 #make TQ2440_config 2.编译 #make all 3.交叉编译器是crosstools_3.4.5_softfloat” 使用4.3.3版 ...
- bootargs中的环境变量说明和一些常用的uboot命令
bootargs中的环境变量说明和一些常用的uboot命令 一些常见的uboot命令:Help [command]在屏幕上打印命令的说明Boom [addr]启动在内存储器的内核Tftpboot通过t ...
- securecrt中进入uboot命令行时,出现无法键入任何指令的问题解决方法
securecrt中进入uboot命令行时,出现无法键入任何指令的问题解决方法 可能出现以下几种情况 1.securecrt在创建连接时,忘记取消勾选流控: 2.usb转串口线坏了3.uboot有问题 ...
- Linux Yum 命令使用举例
转自:https://blog.csdn.net/u012359618/article/details/51199309/ 本文给大家讲解Yum的使用15个范例: Yum软件包管理方式,在Red Ha ...
随机推荐
- HDU 4585 Shaolin (STL)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- 【java】详解java中的注解(Annotation)
目录结构: contents structure [+] 什么是注解 为什么要使用注解 基本语法 4种基本元注解 重复注解 使用注解 运行时处理的注解 编译时处理的注解 1.什么是注解 用一个词就可以 ...
- SQL2000的系统表sysproperties在SQL2005中 无效的 问题
有两种解决办法 方法一.是我在网上找的:将原来的sysproperties改成sys.extended_properties并且对应关系如下 sys.extended_properties left ...
- Could not connect to Redis at xxx.xxx.xxx.xxx:6379: Connection refused
开发发来消息说测试环境的redis无法登录: # redis-cli -p 6379 -h xxx.xxx.xxx.xxx Could not connect to Redis at xxx.xxx. ...
- 构建基于阿里云OSS文件上传服务
转载请注明来源:http://blog.csdn.net/loongshawn/article/details/50710132 <构建基于阿里云OSS文件上传服务> <构建基于OS ...
- SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script
In this post I will introduce a way how to run a script for backing up SharePoint data which could b ...
- python标准库介绍——34 commands 模块详解
==commands 模块== (只用于 Unix) ``commands`` 模块包含一些用于执行外部命令的函数. [Example 3-7 #eg-3-7] 展示了这个模块. ====Exampl ...
- mac 下python使用venv 虚拟环境
1.安装virtualenv :pip3 install virtualenv 2.创建虚拟环境命令:virtualenv --no-site-packages venv 在当前目录创建一个虚拟环境v ...
- 使用layui 和 jquery 问题小结
问题 1 在使用 layui 2.2.5 之前,可以引入最新版的 jquery ,使用更好的性能.也可以使用layui 的jquery内部版本.如果引入要在引入layui.js 之前引入 2 使用 s ...
- 分享十:php中并发读写文件冲突的解决方案
对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些!用一般的文件操作方法完全没有问题.但如果并发高,在我们对文件进行读写操作时,很有可能多个进程对进一文件进行操作,如果这时不对文件的访问进行相 ...