u-boot命令

本文详细介绍了u-boot中命令结构体的定义、宏使用、源代码组织方式及命令执行流程,包括命令的参数、功能实现、帮助信息等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

命令结构体的定义在command.h中

struct cmd_tbl_s {

       char        *name;          /* Command Name                   */

       int           maxargs; /* maximum number of arguments     */

       int           repeatable;    /* autorepeat allowed?        */

                                   /*Implementation function  */

       int           (*cmd)(structcmd_tbl_s *, int, int, char *[]);

       char        *usage;          /* Usage message (short)    */

#ifdef      CFG_LONGHELP

       char        *help;            /* Help  message       (long)     */

#endif

#ifdefCONFIG_AUTO_COMPLETE

       /* do auto completion on the arguments */

       int           (*complete)(intargc, char *argv[], char last_char, int maxv, char *cmdv[]);

#endif

};

typedef structcmd_tbl_s     cmd_tbl_t;

 

定义定义命令的宏,使用此宏定义的命令结构体变量将会放在.u_boot_cmd的段里面

#defineStruct_Section  __attribute__ ((unused,section(".u_boot_cmd")))

在u-boot.lds中定义了.u_boot_cmd的连接方式:

    . = .;

    __u_boot_cmd_start = .;

    .u_boot_cmd : { *(.u_boot_cmd) }

    __u_boot_cmd_end = .;

这意味着,所有的.u_boot_cmd段都会在__u_boot_cmd_start到__u_boot_cmd_end之间。

#ifdef  CFG_LONGHELP

#defineU_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \

cmd_tbl_t__u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}

#else      /* no long help info */

#defineU_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \

cmd_tbl_t__u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}

#endif     /* CFG_LONGHELP */

示例:

U_BOOT_CMD(

       date,       2,    1,    do_date,

       "date    - get/set/reset date & time\n",

       "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"

       "  - without arguments: print date & time\n"

       "  - with numeric argument: set the system date & time\n"

       "  - with 'reset' argument: reset the RTC\n"

);

 

命令的源代码一般使用一个单独的C文件,名字为cmd_xxx.c,在开头会定义命令选项开关:

#if (CONFIG_COMMANDS& CFG_CMD_XXX)

       命令源代码

#endif

其中CFG_CMD_XXX是命令的掩码,每个命令不一样,它在cmd_confdefs.h中被定义。

 

在进入u-boot命令模式时,u-boot通过find_cmd查找命令结构体,然后调用命令结构体中的函数实现命令。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值