在本章会介绍uboot命令的相关结构体并添加自定义命令。
______ _______ _______ _________ _______ _______ ______
|\ /|( ___ \ ( ___ )( ___ )\__ __/ ( ____ \( )( __ \
| ) ( || ( ) )| ( ) || ( ) | ) ( | ( \/| () () || ( \ )
| | | || (__/ / | | | || | | | | | | | | || || || | ) |
| | | || __ ( | | | || | | | | | | | | |(_)| || | | |
| | | || ( \ \ | | | || | | | | | | | | | | || | ) |
| (___) || )___) )| (___) || (___) | | | | (____/\| ) ( || (__/ )
(_______)|/ \___/ (_______)(_______) )_( (_______/|/ \|(______/
1.相关结构体分析
在uboot中与命令相关的一个结构体如下,该结构体的内容会被保存在二进制u_boot_cmd
段,每当输入一个命令,就会依次在该段内寻找命令,然后执行相应的函数。
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 * const []);
char *usage; /* Usage message (short) */
#ifdef CONFIG_SYS_LONGHELP
char *help; /* Help message (long) */
#endif
#ifdef CONFIG_AUTO_COMPLETE
/* do auto completion on the arguments */
int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
#endif
};
如下:就定义了一个imls命令,参数最多为1,可重复执行,实现函数为do_imls
,之后是帮助文档。
U_BOOT_CMD(
imls, 1, 1, do_imls,
"list all images found in flash",
"\n"
" - Prints information about all images found at sector/block\n"
" boundaries in nor/nand flash."
);
#endif
2.增加自定义命令
在common目录下新建hello_cmd.c
文件,输入一下内容:
#include <common.h>
#include <bootretry.h>
#include <cli.h>
#include <console.h>
#include <linux/ctype.h>
static int do_hello(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
printf("Hello World\n");
return 0;
}
U_BOOT_CMD(
hello, CONFIG_SYS_MAXARGS, 1, do_hello,
"print Hello",
"print hello"
" passing 'arg' as arguments"
);
在Makefile中输入:
obj-y += hello_cmd.o
重新使用命令:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
重新编译后,进入uboot
原文地址 : http://coderdock.com