U_BOOT_CMD 命令数据结构

来源:互联网 发布:软件开发的前景 编辑:程序博客网 时间:2024/06/03 05:07

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.

原创粉丝点击