之前一直在想,为什么没有人出一个完全从零写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命令实现举例的更多相关文章

  1. U-Boot命令大全(功能参数及用法)

    U-Boot上电启动后,按任意键可以退出自动启动状态,进入命令行. U-Boot 2010.03 (Sep 25 2011 - 16:18:50)     DRAM: 64 MB     Flash: ...

  2. Uboot mmc命令解析&NAND flash uboot命令详解

    转载:http://blog.csdn.net/simonjay2007/article/details/43198353 一:mmc的命令如下: 1:对mmc读操作 mmc read addr bl ...

  3. uboot命令(1):mmc命令

    版权声明 更新:2017-06-07博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 今天在进行Android分区修改的时候发 ...

  4. 1.ok6410移植bootloader,移植u-boot,学习u-boot命令

    ok6410移植u-boot 既然是移植u-boot当然首先需要u-boot源码,这里的u-boot代码是由国嵌提供的. 一.配置编译u-boot A. 解压 u-boot 压缩文件 B. 进入解压生 ...

  5. uboot命令分析+实现【转】

    转自:http://xouou.iteye.com/blog/2150061 先贴一个重要结构,位于uboot/include/command.h,这个结构代表每个uboot命令 struct cmd ...

  6. 编译u-boot命令和u-boot常用命令

    一.编译u-boot命令 1.配置开发板 #make TQ2440_config 2.编译 #make all 3.交叉编译器是crosstools_3.4.5_softfloat” 使用4.3.3版 ...

  7. bootargs中的环境变量说明和一些常用的uboot命令

    bootargs中的环境变量说明和一些常用的uboot命令 一些常见的uboot命令:Help [command]在屏幕上打印命令的说明Boom [addr]启动在内存储器的内核Tftpboot通过t ...

  8. securecrt中进入uboot命令行时,出现无法键入任何指令的问题解决方法

    securecrt中进入uboot命令行时,出现无法键入任何指令的问题解决方法 可能出现以下几种情况 1.securecrt在创建连接时,忘记取消勾选流控: 2.usb转串口线坏了3.uboot有问题 ...

  9. Linux Yum 命令使用举例

    转自:https://blog.csdn.net/u012359618/article/details/51199309/ 本文给大家讲解Yum的使用15个范例: Yum软件包管理方式,在Red Ha ...

随机推荐

  1. Java 时间相比较

    private String getRunTime() { String start = new Date.getTime(); String end= new Date.getTime(); lon ...

  2. 树莓派进阶之路 (030) -Picustom.h(原创)

    写代码的时候敢接每次查wiringPi库函数挺麻烦的,自己wiringPi库封装了一下: #ifndef __PICUSTOM_H__ #define __PICUSTOM_H__ /******** ...

  3. 蓝牙进阶之路 (003) - AT指令(转)

    一 . 一 般 命 令 1.AT+CGMI      给出模块厂商的标识. 2.AT+CGMM    获得模块标识.这个命令用来得到支持的频带(GSM 900,DCS 1800    或PCS 190 ...

  4. 把PHP的数组变成带单引号的字符串

    上次做项目的时候,遇到 查询结果为 数组.因为条件原因,需要用$where['_string'] 去组合查询.进而用到把数组变成单引号的字符串.举例:查询返回的数组为: $projectcode_ar ...

  5. 兼容各大浏览器的event获取

    event: //得到事件 function getEvent(evt){ if (evt && typeof(evt) != "undefined") { var ...

  6. Using Repository Pattern in Entity Framework

    One of the most common pattern is followed in the world of Entity Framework is “Repository Pattern”. ...

  7. Postgresql: UUID的使用

    默认安装的 Postgresql 是不带 UUID 函数的,为了生成一个 UUID,我们必须装载它到数据库中. CREATE EXTENSION "uuid-ossp"; 然后就可 ...

  8. Linux下通过关键字模糊查找搜索文件

    [背景] 想要在Linux下面,找之前不知道放到哪里的一个tomcat的文件. [折腾过程] 1.最后是参考: linux查找文件命令find – 发芽的石头 – 博客频道 – CSDN.NET 去搜 ...

  9. Hadoop相关项目Hive-Pig-Spark-Storm-HBase-Sqoop

    Hadoop相关项目Hive-Pig-Spark-Storm-HBase-Sqoop的相关介绍. Hive Pig和Hive的对比 摘要: Pig Pig是一种编程语言,它简化了Hadoop常见的工作 ...

  10. 【转】Java利用反射机制访问私有化构造器

    Java利用反射机制访问私有化构造器 博客分类: java   我们都知道,当一个类的构造方法被设为私有的时候(private),在其他类中是无法用new来实例化一个对象的. 但是有一种方法可以把带有 ...