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

原博客:https://blog.csdn.net/xiaokangdream/article/details/79545161

版权声明: https://blog.csdn.net/xiaokangdream/article/details/79545161

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


   
   
  1. #include <common.h>
  2. #include <watchdog.h>
  3. #include <command.h>
  4. #include <image.h>
  5. #include <malloc.h>
  6. #include <zlib.h>
  7. #include <bzlib.h>
  8. #include <environment.h>
  9. #include <asm/byteorder.h>
  10. #ifdef CONFIG_OF_FLAT_TREE
  11. #include <ft_build.h>
  12. #endif
  13. DECLARE_GLOBAL_DATA_PTR;

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


   
   
  1. cmd_tbl_t __u_boot_cmd_bootm __attribute__ ((unused,section ( ".u_boot_cmd")))= {
  2. “bootm”,
  3. CFG_MAXARGS,
  4. 1, do_bootm,
  5. "bootm - boot application image from memory\n",
  6. "[addr [arg ...]]\n - boot application image stored in memory\n"
  7. "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
  8. "\t'arg' can be the address of an initrd image\n"
  9. #ifdef CONFIG_OF_FLAT_TREE
  10. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  11. "\ta third argument is required which is the address of the of the\n"
  12. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  13. "\tuse a '-' for the second argument. If you do not pass a third\n"
  14. "\ta bd_info struct will be passed instead\n"
  15. }

我们看到,在第四个参数里,它是一个函数指针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)
   
   
然后,我们就开始构建了:

   
   
  1. U_BOOT_CMD(
  2. hello, CFG_MAXARGS, 1, do_hello,
  3. "hello - say hello to you",
  4.         "just type hello is over"
  5. );

所有的代码如下:


   
   
  1. #include <common.h>
  2. #include <watchdog.h>
  3. #include <command.h>
  4. #include <image.h>
  5. #include <malloc.h>
  6. #include <zlib.h>
  7. #include <bzlib.h>
  8. #include <environment.h>
  9. #include <asm/byteorder.h>
  10. #ifdef CONFIG_OF_FLAT_TREE
  11. #include <ft_build.h>
  12. #endif
  13. DECLARE_GLOBAL_DATA_PTR;
  14. int do_hello(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  15. {
  16. printf( "Hello World");
  17. }
  18. U_BOOT_CMD(
  19. hello, CFG_MAXARGS, 1, do_hello,
  20. "hello - say hello to you",
  21. "just type hello is over"
  22. );

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值