您的位置:首页 > 其它

uboot命令实现举例

2018-03-03 16:13 369 查看
之前一直在想,为什么没有人出一个完全从零写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,    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"
#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")))
[code]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.
[/code]
该属性附加到变量上,意味着该变量可能未被使用。 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-0;i<argc;i++)
{
printf("argv[%d],%s\n",i,argv[i]);
}

return 0;
}

U_BOOT_CMD(
hello,    CFG_MAXARGS,    1,    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语言,把时间花在更需要的地方。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: