uboot cmd简要分析

在本章会介绍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
hello_cmd

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值