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 ...
随机推荐
- Jetty使用内存过大的解决方案
之前用Jetty做过一个消息通知服务器,主要功能就是其他各个子系统如果有需要push给客户端消息的就把这个消息发给我的Server,我用WebSocket来推送给客户端~ 程序上线一段时间之后运维工程 ...
- MVC摘记
2013年9月23日18:04:06 直接cshtml页面的js中使用 var customerId = @(ViewBag.Oppotunity as OppotunityDto==null ...
- 【转】dubbo各种协议
原文地址:http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-协议参考手册 协议参考手册 (+) (#) 推荐使用Dubbo协议 性能测试报告各协议的性能情况 ...
- webservice复杂类型实例
1.准备工作: 概念:SOAP(简单对象访问协议).WSDL(web服务描述语言).XML(可扩展标记语言).axis(阿帕奇可扩展交互系统) (1) 下载axis1.4,将axis1.4中的 ...
- Python endswith() 函数
函数:endswith() 作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型 相关函数:判断字符串开头 startswith() 一.函数说明语法:string.endswith(st ...
- ipod classic 检查硬盘方法
长按(中间键+MENU)重启接着按(中键+左键)一直进入工程模式进入后顺序是:menu-IO-HARDDRIVE-HDSMARTDATA 就可以看到了. retracts--硬盘磁头非正常退回,比如硬 ...
- fdatool的滤波器设计
作者:桂. 时间:2017-08-15 20:28:11 链接:http://www.cnblogs.com/xingshansi/p/7367738.html 前言 本文主要记录滤波器设计的基本流 ...
- MongoDB联合查询 -摘自网络
1.简单手工关联 首先将结果查询出来放到一个变量里面,然后再查询 u = db.user.findOne({author:"wangwenlong"}); for(var p = ...
- php分享十九:网络带宽预估
网络带宽是指在一个固定的时间内(1秒),能通过的最大位数据.就好象高速公路的车道一样,带宽越大,好比车道越多. 数字信息流的基本单位是bit(比特),时间的基本单位是s(秒),因此bit/s(比特/秒 ...
- JDK1.5新特性,基础类库篇,System类
一. 背景 System.getenv(String)方法继续有效:增加了新的System.getenv()方法,返回保存环境变量的Map<String,String>. 同时增加了以纳秒 ...