虚拟开发板-uboot添加实现命令

在本章会介绍uboot命令的相关结构体并添加自定义命令。

1
2
3
4
5
6
7
8
______   _______  _______ _________   _______  _______  ______
|\ /|( ___ \ ( ___ )( ___ )\__ __/ ( ____ \( )( __ \
| ) ( || ( ) )| ( ) || ( ) | ) ( | ( \/| () () || ( \ )
| | | || (__/ / | | | || | | | | | | | | || || || | ) |
| | | || __ ( | | | || | | | | | | | | |(_)| || | | |
| | | || ( \ \ | | | || | | | | | | | | | | || | ) |
| (___) || )___) )| (___) || (___) | | | | (____/\| ) ( || (__/ )
(_______)|/ \___/ (_______)(_______) )_( (_______/|/ \|(______/

1.相关结构体分析

在uboot中与命令相关的一个结构体如下,该结构体的内容会被保存在二进制u_boot_cmd段,每当输入一个命令,就会依次在该段内寻找命令,然后执行相应的函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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,之后是帮助文档。

1
2
3
4
5
6
7
8
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文件,输入一下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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中输入:

1
obj-y += hello_cmd.o

重新使用命令:

1
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-

重新编译后,进入uboot:

hello_cmd

版权声明:本文为博主原创文章,转载需声明为转载内容并添加原文地址。

原文地址:https://coderdock.com

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2017-2020 Dock
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信