U_BOOT_CMD 命令数据结构

The user interface to U-Boot consists of a command line interpreter (CLI), much like a Linux shell prompt. When connected via a serial line you can interactively enter commands and see the results.

在Uboot的doc目录下的README.commands文件说明如下:
Commands are added to U-Boot by creating a new command structure.
This is done by first including command.h

Then using the U_BOOT_CMD() macro to fill in a cmd_tbl_t struct.

U_BOOT_CMD(name,maxargs,repeatable,command,"usage","help")

name: is the name of the commad. THIS IS NOT a string.
maxargs: the maximumn numbers of arguments this function takes
command: Function pointer (*cmd)(struct cmd_tbl_s *, int, int, char *[ ]);
usage: Short description. This is a string
help: long description. This is a string


**** Behind the scene ******

The structure created is named with a special prefix (__u_boot_cmd_)
and placed by the linker in a special section.

This makes it possible for the final link to extract all commands
compiled into any object code and construct a static array so the
command can be found in an array starting at __u_boot_cmd_start.

If a new board is defined do not forget to define the command section
by writing in u-boot.lds ($(TOPDIR)/board/boardname/u-boot.lds) these
3 lines:

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

U-boot的命令用struct cmd_tbl_t来实现。cmd_tbl_t的主要数据成分是命令名称(name)和命令处理函数(cmd),此外还包括最大参数个数(maxargs),是否可重复执行(repeatable),使用方法和帮助信息(usage,help)等。这个数据结构在文件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}

举例如下:
bootm命令定义如下:
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   __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"
}
这样就为bootm命令定义了一个cmd_tbl_t 结构。

但__attribute__ ((unused,section (".u_boot_cmd")))又是实现什么功能呢?基于什么考虑呢?
它是用来定义用户的命令, 每当初始化这样的一条命令, 就将在.u_boot_cmd段中增加一段数据。以便于find_cmd函数查找命令。u-boot中的readme文件对这个功能是这样描述的: construct a static array so the command can be found in an array starting at __u_boot_cmd_start.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值