Uboot如何添加命令


Uboot如何添加属于自己的一条命令呢?其实doc/README.commands文档中已有详细的说明如下:

Commands are added to U-Boot by creating a new command structure.
This is done by first including command.h

## 通过创建一个新的命令结构体,向U-boot中添加命令。在此之前需要包含头文件

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

## 使用宏函数U_BOOT_CMD()填充一个cmd_tbl_t结构体(查看command.h中的结构体定义)

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

## 该宏函数定义在command.h头文件中

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

# 该命令详细的使用说明。


**** Behinde the scene ******

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

## 创建该结构体的变量名称是以(__u_boot_cmd_)为前缀,并添加了

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

## 目的是在链接的时候,放到指定的.u_boot_cmd段中。

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.

## 定义该结构体变量添加__attribute__属性;使的在链接阶段,

## 把不同的.o文件中定义的命令抽取出来并构造成一个静态数组。

## 而且所有的命令都可以以__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:

## 如果一个新版子被定义,

## 在$(TOPDIR)/board/boardname/u-boot.lds链接文件中,请不要忘记添加如下三行内容。

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

    __u_boot_cmd_end = .;

###########################

#    command.h 头文件内容如下    #

###########################

/*
 * (C) Copyright 2000-2009
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/*
 *  Definitions for Command Processor
 */
#ifndef __COMMAND_H
#define __COMMAND_H

#include <config.h>

#ifndef NULL
#define NULL	0
#endif

/* Default to a width of 8 characters for help message command width */
#ifndef CONFIG_SYS_HELP_CMD_WIDTH
#define CONFIG_SYS_HELP_CMD_WIDTH	8
#endif

#ifndef	__ASSEMBLY__
/*
 * Monitor Command Table
 */

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 * const []);
	char		*usage;		/* Usage message	(short)	*/
#ifdef	CONFIG_SYS_LONGHELP
	char		*help;		/* Help  message	(long)	*/
#endif
#ifdef CONFIG_AUTO_COMPLETE
	/* do auto completion on the arguments */
	int		(*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
#endif
};

typedef struct cmd_tbl_s	cmd_tbl_t;

/* @FeiRan
 * __u_boot_cmd_start 和 __u_boot_cmd_end符号在 u-boot.lds文件中有定义 
 */
extern cmd_tbl_t  __u_boot_cmd_start;
extern cmd_tbl_t  __u_boot_cmd_end;


/* common/command.c */
int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
	      flag, int argc, char * const argv[]);
cmd_tbl_t *find_cmd(const char *cmd);
cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);

extern int cmd_usage(cmd_tbl_t *cmdtp);

#ifdef CONFIG_AUTO_COMPLETE
extern void install_auto_complete(void);
extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
#endif

/*
 * Monitor Command
 *
 * All commands use a common argument format:
 *
 * void function (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 */

typedef	void	command_t (cmd_tbl_t *, int, int, char *[]);

#if defined(CONFIG_CMD_MEMORY)		\
    || defined(CONFIG_CMD_I2C)		\
    || defined(CONFIG_CMD_ITEST)	\
    || defined(CONFIG_CMD_PCI)		\
    || defined(CONFIG_CMD_PORTIO)
#define CMD_DATA_SIZE
extern int cmd_get_data_size(char* arg, int default_size);
#endif

#endif	/* __ASSEMBLY__ */

/*
 * Command Flags:
 */
#define CMD_FLAG_REPEAT		0x0001	/* repeat last command		*/
#define CMD_FLAG_BOOTD		0x0002	/* command is from bootd	*/

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

#ifdef  CONFIG_SYS_LONGHELP

#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}

#define U_BOOT_CMD_MKENT(name,maxargs,rep,cmd,usage,help) \
{#name, maxargs, rep, cmd, usage, help}

#else	/* no long help info */

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

#define U_BOOT_CMD_MKENT(name,maxargs,rep,cmd,usage,help) \
{#name, maxargs, rep, cmd, usage}

#endif	/* CONFIG_SYS_LONGHELP */

#if defined(CONFIG_NEEDS_MANUAL_RELOC)
void fixup_cmdtable(cmd_tbl_t *cmdtp, int size);
#endif
#endif	/* __COMMAND_H */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值