《单片机小白转嵌入式Linux学习记录,基于S3C2440----目录》
------------------------------
u-boot 命令是怎么实现的?
1. 输入字符串
2. 调用对应的函数 
    根据字符串调用对应的函数。
------------------------------
自定义一个 u-boot 下的 hello 命令
在 common 目录下添加 cmd_hello.c ,并在common/Makefile 中添加 cmd_hello.o 
--------------------
#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[])
{
	printf ("hello word ! argc = %d\n", argc);
	for(i=0;i<argc;i++)
	{
		printf("argv[%d]: %s\n",i,argv[i]);
	}
	return 0;
}
U_BOOT_CMD(
 	hello,	CFG_MAXARGS,	1,	do_hello,
 	"hello   - just for test \n",
 	"hello,long help ........................\n"
);测试:
在u-boot 下
输入 help 查看是否有 hello 命令及简单帮助信息
输入 help hello 查看长帮助信息。
执行hello aaa bbb 查看执行结果。
链接脚本中
.u_boot_cmd 段理解,
命令结构存放段
搜索 u_boot_cmd
uboot/include/command.h
#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
print 命令打印含有 bootm ,以bootm为线索
搜索 bootm -> U_BOOT_CMD
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"
#ifdef CONFIG_OF_FLAT_TREE
    "\tWhen booting a Linux kernel which requires a flat device-tree\n"
    "\ta third argument is required which is the address of the of the\n"
    "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
    "\tuse a '-' for the second argument. If you do not pass a third\n"
    "\ta bd_info struct will be passed instead\n"
#endif
);
搜索 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 展开
# 把参数字符串化
## 参数字符串化后粘贴在当前位置
cmd_tbl_t __u_boot_cmd_bootm __attribute__ ((unused,section (".u_boot_cmd"))) = {"bootm", CFG_MAXARGS, 1, do_bootm, "短帮助", "长帮助"}
由此可见 U_BOOT_CMD 将 cmd_tbl_t 结构保存在 .u_boot_cmd 段
cmd_tbl_t 结构
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)    短的帮助信息,输入help   */
#ifdef    CFG_LONGHELP
    char        *help;        /* Help  message    (long) 长的帮助信息,输入help 命令    */
#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
};
离线