title: u-boot(四)命令实现

tags: linux

date: 2018-09-25 23:13:05

u-boot(四)命令实现

命令是如何实现的?

  1. 输入命令
  2. 执行函数,根据命令去寻找函数

所以会有一个命令的结构体[name,fun]

分析run_command

函数原型如下 int run_command (const char *cmd, int flag)

  1. 处理, 空格,;

  2. 解析参数parse_line (finaltoken, argv)

    example: md.w 0 ------>argv[0]= "md.w", argv[1]=" 0"
    
    ​```
    /* Extract arguments */
    if ((argc = parse_line (finaltoken, argv)) == 0) {
    rc = -1; /* no command at all */
    continue;
    }
    ​```
  3. 命令搜索if ((cmdtp = find_cmd(argv[0])) == NULL),可以发现结构体

    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 *[]);
    char *usage; /* Usage message (short) */
    #ifdef CFG_LONGHELP
    char *help; /* Help message (long) */
    #endif
    #ifdef CONFIG_AUTO_COMPLETE
    /* do auto completion on the arguments */
    int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);
    #endif
    };
    • repeatable 可重复,指的是直接按回车是否继续执行上次命令
    • usage,短的help,指的是直接输入help查看的所有命令显示的帮助
    • help,具体的help,指的是help cmd 查看的具体的信息

    查看函数,可以发现是在__u_boot_cmd_start__u_boot_cmd_end中遍历,这个地址是在链接脚本中定义的,也就是命令这个东西,有一个特殊的属性,定位到某个地址.

    	. = .;
    __u_boot_cmd_start = .;
    .u_boot_cmd : { *(.u_boot_cmd) }
    __u_boot_cmd_end = .;

    搜索这个段属性.u_boot_cmd,在include\command.h有这么一个宏

    #define Struct_Section  __attribute__ ((unused,section (".u_boot_cmd")))

    再搜索下这个宏

    #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
    cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}

    再搜索一下这个U_BOOT_CMD,可以发现其实就是命令了,搜索下命令bootm,在common\cmd_bootm.c

    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"
    );

    尝试着展开这个宏,可以发现就是定义了一个段属性特殊的结构体,也就是命令结构体

    cmd_tbl_t  __u_boot_cmd_bootm  Struct_Section=
    {
    "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"
    }

小结

  1. U-boot 的命令是用结构体存储的,这些结构体是用特殊的段属性集合到一块区域里面去,分散在各个文件中

  2. 命令解析的时候是去这个段去搜索的,这个段属性的地址是从__u_boot_cmd_start__u_boot_cmd_end,在链接脚本中定义的.

  3. 命令结构体

    struct cmd_tbl_s ;

自定义一个命令

参考common/cmd_bootm.c的头文件,编写源代码cmd_hello.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>
int do_hello (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
int i ;
printf ("hello world %d \n,", argc);
//打印下参数
for(i=0;i<argc;i++)
{
printf ("argv[%d] is %s \n",i,argv[i]);
}
return 0;
} U_BOOT_CMD(
hello, CFG_MAXARGS, 1, do_hello,
"this is short help for hello just test\n",
"this is long help for hello just test\n"
);

makefile

修改common makefile ,只需要在COBJS上加上cmd_hello.o

u-boot(四)命令实现的更多相关文章

  1. Spring Boot(四) Mybatis-MySql

    Spring Boot(四) Mybatis-MySql 0.准备数据库表 -- ---------------------------- -- Table structure for person ...

  2. Spring Boot启动命令参数详解及源码分析

    使用过Spring Boot,我们都知道通过java -jar可以快速启动Spring Boot项目.同时,也可以通过在执行jar -jar时传递参数来进行配置.本文带大家系统的了解一下Spring ...

  3. spring boot(四):thymeleaf使用详解

    在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...

  4. Spring Boot通过命令行启动发生FileNotFoundException

    Spring Boot + Jersey 通过命令行启动会发生错误FileNotFoundException异常 异常信息如下: ERROR o.a.c.c.C.[Tomcat].[localhost ...

  5. Spring Boot(四):Thymeleaf 使用详解

    在上篇文章Spring Boot (二):Web 综合开发中简单介绍了一下 Thymeleaf,这篇文章将更加全面详细的介绍 Thymeleaf 的使用.Thymeleaf 是新一代的模板引擎,在 S ...

  6. C#设计模式之十四命令模式(Command Pattern)【行为型】

    一.引言 今天我们开始讲“行为型”设计模式的第二个模式,该模式是[命令模式],又称为行动(Action)模式或交易(Transaction)模式,英文名称是:Command Pattern.还是老套路 ...

  7. (转)Spring Boot(四):Thymeleaf 使用详解

    http://www.ityouknow.com/springboot/2016/05/01/spring-boot-thymeleaf.html 在上篇文章Spring Boot (二):Web 综 ...

  8. spring boot(四) 多数据源

    前言 前一篇中我们使用spring boot+mybatis创建了单一数据源,其中单一数据源不需要我们自己手动创建,spring boot自动配置在程序启动时会替我们创建好数据源. 准备工作 appl ...

  9. Spring boot(四)thymeleaf使用介绍

    在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...

随机推荐

  1. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  2. python+selenium+unnitest写一个完整的登陆的验证

    import unittest from selenium import webdriver from time import sleep class lonInTest (unittest.Test ...

  3. Iroha and a Grid AtCoder - 1974(思维水题)

    就是一个组合数水题 偷个图 去掉阴影部分  把整个图看成上下两个矩形 对于上面的矩形求出起点到每个绿点的方案 对于下面的矩形 求出每个绿点到终点的方案 上下两个绿点的方案相乘后相加 就是了 想想为什么 ...

  4. 【XSY2787】Mythological VII 贪心

    题目描述 有两个指针\(l,r\),初始时\(l=r=k\) 给你\(a_1,\ldots,a_n\),你要移动\(l,r\),\(l\)只能每次向左移一个数,\(r\)只能向右移一个数,要求任意时刻 ...

  5. 【XSY2730】Ball 多项式exp 多项式ln 多项式开根 常系数线性递推 DP

    题目大意 一行有\(n\)个球,现在将这些球分成\(k\) 组,每组可以有一个球或相邻两个球.一个球只能在至多一个组中(可以不在任何组中).求对于\(1\leq k\leq m\)的所有\(k\)分别 ...

  6. 树莓派播放网络磁盘MP3文件

    树莓派映射网络磁盘 sudo mount -t cifs -o username=jerry.huang,password=Hello2014 //10.10.0.1/Public/ESLPod.co ...

  7. android设置透明度代码片段

    我们可以在代码中完成对窗体透明度(这是窗体本身的透明度,非背景)的设置,方法如下: WindowManager.LayoutParams windowLP = getWindow().getAttr ...

  8. kafka清理数据日志

    背景问题: 使用kafka的路上踩过不少坑,其中一个就是在测试环境使用kafka一阵子以后,发现其日志目录变的很大,占了磁盘很大空间,定位到指定目录下发现其中一个系统自动创建的 topic,__con ...

  9. Yii2 如何输出 sql 语句?

    可以用 $model->find()->createCommand()->getRawSql(); 不能带 all(). find() 它的 返回值类型是 yii\db\Active ...

  10. ELK日志系统安装、配置

    1.关闭SELINUX: [root@ELK /]# vim /etc/selinux/config 将SELINUX=enforcing修改为SELINUX=disabled 2.关闭防火墙: [r ...