前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >第1阶段——uboot分析之仿照bootm制作hello命令(7)

第1阶段——uboot分析之仿照bootm制作hello命令(7)

作者头像
诺谦
发布2018-01-03 16:31:00
5280
发布2018-01-03 16:31:00
举报
文章被收录于专栏:Linux驱动Linux驱动

仿照bootm命令生成来制作一个hello命令,功能:打印出hello,world!和参数值

1.点击New File ,创建cmd_hello.c 将./common/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>

2.点击保存: 保存在./common文件下,(命令文件都存在common文件里)

3.写执行命令需要调用的函数: 复制./common/cmd_bootm.c里 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { } 改成: int do_hello (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) //执行命令需要调用的函数 { int i; printf ("hello,world!,arg_numble=%d\n",argc); //打印"hello,world!"和参数个数arg_numble for(i=0;i<argc;i++) printf("argv[i]=%s",i,arg[i]); //打印命令名字和参数值 return 0; }

4. 添加U_BOOT_CMD宏(实现:通过U_BOOT_CMD宏来将命令保存在.u_boot_cmd段里): U_BOOT_CMD( hello, //命令名 CFG_MAXARGS, //参数最大值 1, //支持重复使用命令 do_hello, //函数指针,用于命令执行时需要调用什么函数,就是第2节的do_hello函数 "hello - just for help...", //短的使用说明 "hello - long help... ..." //长的使用说明,敲打"help hello"命令,就会出现这段字符串 #endif ); 5.将cmd_hello.c复制到虚拟机中u-boot-1.1.6/common目录下。 6.进入common目录,输入"vi mkfine" 修改conmon目录下mkfine,在mkefine第54行,COBJS里添加cmd_hello.o文件 7.输入"make",生成u-boot.bin文件重新下载就可以使用hello命令了.

cmd_hello.C源码:

代码语言:javascript
复制
#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>

int do_hello (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) //执行命令需要调用的函数
{
int i;
printf ("hello,world!,arg_numble=%d\n",argc); //打印"hello,world!"和参数个数arg_numble 
for(i=0;i<argc;i++)
printf("argv[i]=%s",i,arg[i]); //打印参数

return 0;
}
U_BOOT_CMD(
hello,    //命令名
CFG_MAXARGS, //参数最大值
1,    //支持重复使用命令
do_hello, //函数指针,用于命令执行时需要调用什么函数,就是第2节的do_hello函数
"hello - just for help...\n", //短的使用说明
"hello - long help... ...\n" //长的使用说明,敲打"help hello"命令,就会出现这段字符串 
#endif
);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档