下面以添加menu命令为例分析U-Boot添加命令的方法。
(1)在common目录下新建cmd_menu.c文件
习惯上把通用命令源代码放在common目录下,与开发板专有命令有关的源代码则放在board/目录下,命名方式只是习惯而已。为了方便阅读和查询习惯以“cmd_<命 令名>.c”为文件名。
(2)定义“menu”命令
在cmd_menu.c中使用如下的代码定义“menu”命令:
U_BOOT_CMD(
menu, 3, 0, do_menu,
"menu - display a menu, to select the items to do something\n",
" - display a menu, to select the items to do something"
);
其中U_BOOT_CMD命令格式如下:
1 U_BOOT_CMD(name,maxargs,rep,cmd,usage,help)
各个参数的意义如下:
name:命令名,非字符串,但在U_BOOT_CMD中用“#”符号转化为字符串
maxargs:命令的最大参数个数
rep:是否自动重复(按Enter键是否会重复执行)
cmd:该命令对应的响应函数
usage:简短的使用说明(字符串)
help:较详细的使用说明(字符串)
在内存中保存命令的help字段会占用一定的内存,通过配置U-Boot可以选择是否保存help字段。若在include/configs/mx51_bbg.h中定义了CONFIG_SYS_LONGHELP宏,则在U-Boot中使用help命令查看某个命令的帮助信息时将显示usage和help字段的内容,否则就只显示usage字段的内容。
U_BOOT_CMD宏在include/command.h中定义:
1 #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \ 2 cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}
“##”与“#”都是预编译操作符,“##”有字符串连接的功能,“#”表示后面紧接着的是一个字符串。
-
struct cmd_tbl_s {
-
char *name; /* 命令名 */
-
int maxargs; /* 最大参数个数 */
-
int repeatable; /* 是否自动重复 */
-
int (*cmd)(struct cmd_tbl_s *, int, int, char *[]); /* 响应函数 */
-
char *usage; /* 简短的帮助信息 */
-
#ifdef CONFIG_SYS_LONGHELP
-
char *help; /* 较详细的帮助信息 */
-
#endif
-
#ifdef CONFIG_AUTO_COMPLETE
-
/* 自动补全参数 */
-
int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);
-
#endif
-
};
-
typedef struct cmd_tbl_s cmd_tbl_t;
一个cmd_tbl_t结构体变量包含了调用一条命令的所需要的信息。
其中Struct_Section在include/command.h中定义如下:
1 #define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))
凡是带有__attribute__ ((unused,section (".u_boot_cmd"))属性声明的变量都将被存放在".u_boot_cmd"段中,并且即使该变量没有在代码中显式的使用编译器也不产生警告信息。
在U-Boot连接脚本u-boot.lds中定义了".u_boot_cmd"段:
1 . = .; 2 __u_boot_cmd_start = .; /*将 __u_boot_cmd_start指定为当前地址 */ 3 .u_boot_cmd : { *(.u_boot_cmd) } 4 __u_boot_cmd_end = .; /* 将__u_boot_cmd_end指定为当前地址 */
这表明带有“.u_boot_cmd”声明的函数或变量将存储在“u_boot_cmd”段。这样只要将U-Boot所有命令对应的cmd_tbl_t变量加上“.u_boot_cmd”声明,编译器就会自动将其放在“u_boot_cmd”段,查找cmd_tbl_t变量时只要在__u_boot_cmd_start与__u_boot_cmd_end之间查找就可以了。
因此“menu”命令的定义经过宏展开后如下:
cmd_tbl_t __u_boot_cmd_menu __attribute__
((unused,section (".u_boot_cmd"))) =
{menu, 3, 0, do_menu,
"menu - display a menu, to select the items to do something\n",
" - display a menu, to select the items to do something"}
实质上就是用U_BOOT_CMD宏定义的信息构造了一个cmd_tbl_t类型的结构体。编译器将该结构体放在“u_boot_cmd”段,执行命令时就可以在“u_boot_cmd”段查找到对应的cmd_tbl_t类型结构体。
(3)实现命令的函数
即U_BOOT_CMD(name,maxargs,rep,cmd,usage,help)中的cmd参数
在cmd_menu.c中添加“menu”命令的响应函数的实现。具体的实现代码略:
1 int do_menu (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) 2 { 3 /* 实现代码略 */ 4 }
(4) 将common/cmd_menu.c编译进u-boot.bin
在common/Makefile中把目标代码cmd_menu.o也加入一起编译:
在include/configs/mx51_bbg.h加入如代码:
#define CONFIG_BOOT_MENU 1
重新编译下载U-Boot就可以使用menu命令了
(5)menu命令执行的过程
在U-Boot中输入“menu”命令执行时,U-Boot接收输入的字符串“menu”,传递给run_command函数。run_command函数调用common/command.c中实现的find_cmd函数在__u_boot_cmd_start与__u_boot_cmd_end间查找命令,并返回menu命令的cmd_tbl_t结构。然后run_command函数使用返回的cmd_tbl_t结构中的函数指针调用menu命令的响应函数do_menu,从而完成了命令的执行。
(6)例子:USB下载,命令很简单。
-
#include <common.h>
-
#include <command.h>
-
extern char console_buffer[];
-
extern int readline (const char *const prompt);
-
extern char awaitkey(unsigned long delay, int* error_p);
-
extern void download_nkbin_to_flash(void);
-
/**
-
* Parses a string into a number. The number stored at ptr is
-
* potentially suffixed with K (for kilobytes, or 1024 bytes),
-
* M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
-
* 1073741824). If the number is suffixed with K, M, or G, then
-
* the return value is the number multiplied by one kilobyte, one
-
* megabyte, or one gigabyte, respectively.
-
*
-
* @param ptr where parse begins
-
* @param retptr output pointer to next char after parse completes (output)
-
* @return resulting unsigned int
-
*/
-
static unsigned long memsize_parse2 (const char *const ptr, const char **retptr)
-
{
-
unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
-
int sixteen = 1;
-
switch (**retptr) {
-
case 'G':
-
case 'g':
-
ret <<= 10;
-
case 'M':
-
case 'm':
-
ret <<= 10;
-
case 'K':
-
case 'k':
-
ret <<= 10;
-
(*retptr)++;
-
sixteen = 0;
-
default:
-
break;
-
}
-
if (sixteen)
-
return simple_strtoul(ptr, NULL, 16);
-
-
return ret;
-
}
-
-
void param_menu_usage()
-
{
-
printf("\r\n##### Parameter Menu #####\r\n");
-
printf("[v] View the parameters\r\n");
-
printf("[s] Set parameter \r\n");
-
printf("[d] Delete parameter \r\n");
-
printf("[w] Write the parameters to flash memeory \r\n");
-
printf("[q] Quit \r\n");
-
printf("Enter your selection: ");
-
}
-
-
void param_menu_shell(void)
-
{
-
char c;
-
char cmd_buf[256];
-
char name_buf[20];
-
char val_buf[256];
-
-
while (1)
-
{
-
param_menu_usage();
-
c = awaitkey(-1, NULL);
-
printf("%c\n", c);
-
switch (c)
-
{
-
case 'v':
-
{
-
strcpy(cmd_buf, "printenv ");
-
printf("Name(enter to view all paramters): ");
-
readline(NULL);
-
strcat(cmd_buf, console_buffer);
-
run_command(cmd_buf, 0);
-
break;
-
}
-
-
case 's':
-
{
-
sprintf(cmd_buf, "setenv ");
-
printf("Name: ");
-
readline(NULL);
-
strcat(cmd_buf, console_buffer);
-
printf("Value: ");
-
readline(NULL);
-
strcat(cmd_buf, " ");
-
strcat(cmd_buf, console_buffer);
-
run_command(cmd_buf, 0);
-
break;
-
}
-
-
case 'd':
-
{
-
sprintf(cmd_buf, "setenv ");
-
printf("Name: ");
-
readline(NULL);
-
strcat(cmd_buf, console_buffer);
-
run_command(cmd_buf, 0);
-
break;
-
}
-
-
case 'w':
-
{
-
sprintf(cmd_buf, "saveenv");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
-
case 'q':
-
{
-
return;
-
break;
-
}
-
}
-
}
-
}
-
-
void main_menu_usage(void)
-
{
-
printf("\r\n##### 100ask Bootloader for OpenJTAG #####\r\n");
-
printf("[n] Download u-boot to Nand Flash\r\n");
-
if (bBootFrmNORFlash())
-
printf("[o] Download u-boot to Nor Flash\r\n");
-
printf("[k] Download Linux kernel uImage\r\n");
-
printf("[j] Download root_jffs2 image\r\n");
-
// printf("[c] Download root_cramfs image\r\n");
-
printf("[y] Download root_yaffs image\r\n");
-
printf("[d] Download to SDRAM & Run\r\n");
-
printf("[z] Download zImage into RAM\r\n");
-
printf("[g] Boot linux from RAM\r\n");
-
printf("[f] Format the Nand Flash\r\n");
-
printf("[s] Set the boot parameters\r\n");
-
printf("[b] Boot the system\r\n");
-
printf("[r] Reboot u-boot\r\n");
-
printf("[q] Quit from menu\r\n");
-
printf("Enter your selection: ");
-
}
-
-
void menu_shell(void)
-
{
-
char c;
-
char cmd_buf[200];
-
char *p = NULL;
-
unsigned long size;
-
unsigned long offset;
-
struct mtd_info *mtd = &nand_info[nand_curr_device];
-
while (1)
-
{
-
main_menu_usage();
-
c = awaitkey(-1, NULL);
-
printf("%c\n", c);
-
switch (c)
-
{
-
case 'n':
-
{
-
strcpy(cmd_buf, "usbslave 1 0x30000000; nand erase bootloader; nand write.jffs2 0x30000000 bootloader $(filesize)");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case 'o':
-
{
-
if (bBootFrmNORFlash())
-
{
-
strcpy(cmd_buf, "usbslave 1 0x30000000; protect off all; erase 0 +$(filesize); cp.b 0x30000000 0 $(filesize)");
-
run_command(cmd_buf, 0);
-
}
-
break;
-
}
-
-
case 'k':
-
{
-
strcpy(cmd_buf, "usbslave 1 0x30000000; nand erase kernel; nand write.jffs2 0x30000000 kernel $(filesize)");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case 'j':
-
{
-
strcpy(cmd_buf, "usbslave 1 0x30000000; nand erase root; nand write.jffs2 0x30000000 root $(filesize)");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
#if 0
-
case 'c':
-
{
-
strcpy(cmd_buf, "usbslave 1 0x30000000; nand erase root; nand write.jffs2 0x30000000 root $(filesize)");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
#endif
-
case 'y':
-
{
-
strcpy(cmd_buf, "usbslave 1 0x30000000; nand erase root; nand write.yaffs 0x30000000 root $(filesize)");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case 'd':
-
{
-
extern volatile U32 downloadAddress;
-
extern int download_run;
-
-
download_run = 1;
-
strcpy(cmd_buf, "usbslave 1");
-
run_command(cmd_buf, 0);
-
download_run = 0;
-
sprintf(cmd_buf, "go %x", downloadAddress);
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case 'z':
-
{
-
strcpy(cmd_buf, "usbslave 1 0x30008000");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case 'g':
-
{
-
extern void do_bootm_rawLinux (ulong addr);
-
do_bootm_rawLinux(0x30008000);
-
}
-
case 'b':
-
{
-
printf("Booting Linux ...\n");
-
strcpy(cmd_buf, "nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case 'f':
-
{
-
strcpy(cmd_buf, "nand erase ");
-
printf("Start address: ");
-
readline(NULL);
-
strcat(cmd_buf, console_buffer);
-
printf("Size(eg. 4000000, 0x4000000, 64m and so on): ");
-
readline(NULL);
-
p = console_buffer;
-
size = memsize_parse2(p, &p);
-
sprintf(console_buffer, " %x", size);
-
strcat(cmd_buf, console_buffer);
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case 's':
-
{
-
param_menu_shell();
-
break;
-
}
-
case 'r':
-
{
-
strcpy(cmd_buf, "reset");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
-
case 'q':
-
{
-
return;
-
break;
-
}
-
}
-
-
}
-
}
-
int do_menu (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
-
{
-
menu_shell();
-
return 0;
-
}
-
U_BOOT_CMD(
-
menu, 3, 0, do_menu,
-
"menu - display a menu, to select the items to do something\n",
-
" - display a menu, to select the items to do something"
-
);
TFTP下载:
-
#include <common.h>
-
#include <command.h>
-
/**功能:等待键盘输入***/
-
static char awaitkey(unsigned long delay, int* error_p)
-
{
-
int i;
-
char c;
-
if (delay == -1) {
-
while (1) {
-
if (tstc()) /* we got a key press */
-
return getc();
-
}
-
}
-
else {
-
for (i = 0; i < delay; i++) {
-
if (tstc()) /* we got a key press */
-
return getc();
-
udelay (10*1000);
-
}
-
}
-
if (error_p)
-
*error_p = -1;
-
return 0;
-
}
-
/*****提示符,功能说明****/
-
void main_menu_usage(void)
-
{
-
printf("\r\n######## Hotips TFTP DownLoad for SMDK2440 ########\r\n");
-
printf("\r\n");
-
printf("[1] 下载 u-boot.bin 写入 Nand Flash\r\n");
-
printf("[2] 下载 Linux(uImage) 内核镜像写入 Nand Flash\r\n");
-
printf("[3] 下载 yaffs2(fs.yaffs) 文件系统镜像写入 Nand Flash\r\n");
-
printf("[4] 下载 Linux(uImage) 内核镜像到内存并运行\r\n");
-
printf("[5] 重启设备\r\n");
-
printf("[q] 退出菜单\r\n");
-
printf("\r\n");
-
printf("输入选择: ");
-
}
-
/***do_menu()的调用函数,命令的具体实现***/
-
-
void menu_shell(void)
-
{
-
char c;
-
char cmd_buf[200];
-
while (1)
-
{
-
main_menu_usage();
-
c = awaitkey(-1, NULL);
-
printf("%c\n", c);
-
switch (c)
-
{
-
case '1':
-
{
-
strcpy(cmd_buf, "tftp 0x32000000 u-boot.bin; nand erase 0x0 0x60000; nand write 0x32000000 0x0 0x60000");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case '2':
-
{
-
strcpy(cmd_buf, "tftp 0x32000000 uImage; nand erase 0x80000 0x200000; nand write 0x32000000 0x80000 0x200000");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case '3':
-
{
-
strcpy(cmd_buf, "tftp 0x32000000 fs.yaffs; nand erase 0x280000; nand write.yaffs2 0x32000000 0x280000 $(filesize)");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case '4':
-
{
-
strcpy(cmd_buf, "tftp 0x32000000 uImage; bootm 0x32000000");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case '5':
-
{
-
strcpy(cmd_buf, "reset");
-
run_command(cmd_buf, 0);
-
break;
-
}
-
case 'q':
-
{
-
return;
-
break;
-
}
-
}
-
}
-
}
-
int do_menu (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
-
{
-
menu_shell();
-
return 0;
-
}
-
U_BOOT_CMD(
-
menu, 1, 0, do_menu,
-
"Download Menu",
-
"U-boot Download Menu by Hotips\n"
-
);
对比两种下载方式我们清楚命令的添加和执行方式了。
转自:http://www.cnblogs.com/sdphome/archive/2011/08/19/2146327.html
阅读(1918) | 评论(0) | 转发(0) |