深入解析U_BOOT_CMD宏

来源:互联网 发布:王侯将相宁有种乎全文 编辑:程序博客网 时间:2024/06/06 00:13

UBOOT当中,各个命令是通过U_BOOT_CMD这个宏来定义出来的。

在include/command.h中定义如下:

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


另外Struct_Section 也是个宏定义,同样在include/command.h中有定义:

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

__attribute__((unused,section (".u_boot_cmd")))又是实现什么功能呢?基于什么考虑呢?

它是用来定义用户的命令, 每当初始化这样的一条命令, 就将在.u_boot_cmd段中增加一段数据。以便于find_cmd函数查找命令。

这样一来,凡通过U_BOOT_CMD定义的cmd_tbl_t变量会全部被放在.u_boot_cmd段当中(可以看UBOOT的链接脚本xxx.lds),具体怎么放是链接器的工作。

u-boot中的readme文件对这个功能是这样描述的: construct a static array so the commandcan be found in an array starting at __u_boot_cmd_start.。

__attribute__解 释如下:

GNUC 允许声明函数、变量和类型的特殊属性,以便手工的代码优化和更仔细的代码检查。要指定一个声明的属性,在声明后写__attribute__ (( ATTRIBUTE ))其中 ATTRIBUTE 是属性说明,多个属性以逗号分隔.

简而言之:声明 +属性   

##name#name这两个操作.

##name:会将字符直接跟在后面;

#name:会将name这个字符中以“..."的形式放置。


好了,现在我们来举个例子:

定义一个命令boot

U_BOOT_CMD(boot,0, 0, fun, "boot xxx");

展开以后会变成:

cmd_tbl_t __u_boot_cmd_boot __attribute___((unused,section(".u_boot_cmd"))) = {"boot", 0, 0, fun, "bootxxx"}

大部分基本不变,将Struct_Section展开了,还将##name换成了boot,#name换成了"boot"。应该不难看出##/#的作用吧。

其实这些在Ubootdoc目录下的README.commands文件有说明:
Commands are added to U-Boot by creating a new command structure.
This is done by first including command.h

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

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

name: is thename 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 structurecreated is named with a special prefix (__u_boot_cmd_)
and placed by the linker in a special section.

This makes itpossible 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 newboard 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 = .;




0 0