u-boot原理分析第四课-------实现hello命令

    上节课,我们彻底分析了U-Boot命令的原理。本节课,我们就来实现一下一个自己定义的命令------hello命令,调用这个命令后,就会打印hello world。我们仿照cmd_bootm.c的来写,我们先新建一个cmd_hello.c的文件,并包含cmd_bootm.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>

#ifdef CONFIG_OF_FLAT_TREE
#include <ft_build.h>
#endif

DECLARE_GLOBAL_DATA_PTR;

上节我们已经将bootm的命令结构体展开过一次了,这里我们再展开一次:

cmd_tbl_t __u_boot_cmd_bootm __attribute__ ((unused,section (".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"
}

我们看到,在第四个参数里,它是一个函数指针do_bootm,也就是我们执行命令后所调用的函数。所以这里我们也仿照它,做一个do_hello的函数。在此之前,我们先看看do_bootm的函数是长什么样的:

所以,我们仿照它做一个我们的do_hello的函数:

int do_hello((cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])

{

    printf("Hello World");

}

这样,我们就构建成了我们的命令函数,我们也可以利用它传入的参数打印一下参数,但这里我们就不进行打印了。接着,我们就开始构建一个命令结构体了,利用上我们上节课讲得U_BOOT_CMD宏,这里我们再次列出来U_BOOT_CMD宏的格式:

#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help)
然后,我们就开始构建了:
U_BOOT_CMD(
 	hello,	CFG_MAXARGS,	1,	do_hello,
 	"hello   - say hello to you",
        "just type hello is over"
);

所有的代码如下:

#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>

#ifdef CONFIG_OF_FLAT_TREE
#include <ft_build.h>
#endif

DECLARE_GLOBAL_DATA_PTR;
int do_hello(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    printf("Hello World");
}

U_BOOT_CMD(
 	hello,	CFG_MAXARGS,	1,	do_hello,
 	"hello   - say hello to you",
    "just type hello is over"
);

我们把我们写的cmd_hello.c放在common目录,同时修改Makefile,在COBJS中加入cmd_hello.o命令即可。接着将源码进行上传,并make生成,得到的文件进行烧写测试,测试图如下:


猜你喜欢

转载自blog.csdn.net/xiaokangdream/article/details/79545161