在uboot上创建菜单

时间:2022-08-21 15:26:13

一、原理

菜单其实就是一个uboot中的命令,和其他的命令没有什么差别。

  uboot启动时,如果进入uboot命令模式,先运行这个命令,就会打印出一个菜单界面。

在uboot的命令模式,通过键入“menu”命令,同样可以调出这个界面。

在uboot上创建菜单

二、操作步骤

1、在common目录下创建cmd_menu.c

内容如下:

#include <common.h>
#include <command.h> #ifdef CONFIG_CMD_MENU #define USE_TFTP_DOWN 1
#define USE_USB_DOWN 2 extern char console_buffer[]; /*
* Reads and returns a character from the serial port
* - Times out after delay iterations checking for presence of character
* - Sets *error_p to UART error bits or - on timeout
* - On timeout, sets *error_p to -1 and returns 0
*/
static char awaitkey(unsigned long delay, int* error_p)
{
int i;
char c; if (delay == -) {
while () {
if (tstc()) /* we got a key press */
return getc();
}
}
else {
for (i = ; i < delay; i++) {
if (tstc()) /* we got a key press */
return getc();
udelay (*);
}
} if (error_p)
*error_p = -;
return ;
} static int bBootFrmNORFlash(void)
{
volatile unsigned int *pdw = (volatile unsigned int *);
unsigned int dwVal; dwVal = *pdw;
*pdw = 0x12345678;
if (*pdw != 0x12345678)
{
return ;
}
else
{
*pdw = dwVal;
return ;
}
} void main_menu_usage(char menu_type)
{ if (bBootFrmNORFlash())
printf("\r\n##### Boot for Nor Flash Main Menu #####\r\n");
else
printf("\r\n##### Boot for Nand Flash Main Menu #####\r\n"); if( menu_type == USE_USB_DOWN)
{
printf("##### EmbedSky USB download mode #####\r\n\n");
}
else if( menu_type == USE_TFTP_DOWN)
{
printf("##### EmbedSky TFTP download mode #####\r\n\n");
} if( menu_type == USE_USB_DOWN)
{
printf("[1] Download u-boot or STEPLDR.nb1 or other bootloader to Nand Flash\r\n");
}
else if( menu_type == USE_TFTP_DOWN)
{
printf("[1] Download u-boot.bin to Nand Flash\r\n");
}
printf("[2] Download Eboot (eboot.nb0) to Nand Flash\r\n");
printf("[3] Download Linux Kernel (zImage.bin) to Nand Flash\r\n");
if( menu_type == USE_USB_DOWN)
{
#ifdef CONFIG_SURPORT_WINCE
printf("[4] Download WinCE NK.bin to Nand Flash\r\n");
#endif
printf("[5] Download CRAMFS image to Nand Flash\r\n");
}
else if( menu_type == USE_TFTP_DOWN)
{
printf("[4] Download stepldr.nb1 to Nand Flash\r\n");
printf("[5] Set TFTP parameters(PC IP,TQ2440 IP,Mask IP...)\r\n");
}
printf("[6] Download YAFFS image (root.bin) to Nand Flash\r\n");
printf("[7] Download Program (uCOS-II or TQ2440_Test) to SDRAM and Run it\r\n"); printf("[8] Boot the system\r\n");
printf("[9] Format the Nand Flash\r\n");
printf("[0] Set the boot parameters\r\n");
printf("[a] Download User Program (eg: uCOS-II or TQ2440_Test)\r\n");
printf("[b] Download LOGO Picture (.bin) to Nand Flash \r\n");
printf("[l] Set LCD Parameters \r\n");
if( menu_type == USE_USB_DOWN)
{
printf("[n] Enter TFTP download mode menu \r\n");
} if (bBootFrmNORFlash())
printf("[o] Download u-boot to Nor Flash\r\n");
if( menu_type == USE_TFTP_DOWN)
printf("[p] Test network (TQ2440 Ping PC's IP) \r\n"); printf("[r] Reboot u-boot\r\n");
printf("[t] Test Linux Image (zImage)\r\n");
if( menu_type == USE_USB_DOWN)
{
printf("[q] quit from menu\r\n");
}
else if( menu_type == USE_TFTP_DOWN)
{
printf("[q] Return main Menu \r\n");
} printf("Enter your selection: ");
} void tftp_menu_shell(void)
{
char c;
char cmd_buf[]; while ()
{
main_menu_usage(USE_TFTP_DOWN);
c = awaitkey(-, NULL);
printf("%c\n", c);
switch (c)
{
case '':
{
strcpy(cmd_buf, "tftp 0x30000000 u-boot.bin; nand erase bios; nand write.jffs2 0x30000000 bios $(filesize)");
run_command(cmd_buf, );
break;
} case '':
{
sprintf(cmd_buf, "tftp 0x30000000 eboot.nb0; nand erase eboot; nand write.jffs2 0x30000000 eboot $(filesize)");
run_command(cmd_buf, );
break;
} case '':
{
strcpy(cmd_buf, "tftp 0x30000000 zImage.bin; nand erase kernel; nand write.jffs2 0x30000000 kernel $(filesize)");
run_command(cmd_buf, );
break;
} case '':
{
strcpy(cmd_buf, "tftp 0x30000000 stepldr.nb1; nand erase kernel; nand write.jffs2 0x30000000 kernel $(filesize)");
run_command(cmd_buf, );
break;
} case '':
{
char param_buf1[];
char param_buf2[];
char param_buf3[]; printf("Enter the TFTP Server(PC) IP address:(xxx.xxx.xxx.xxx)\n");
readline(NULL);
strcpy(param_buf1,console_buffer);
sprintf(cmd_buf, "setenv serverip %s",param_buf1);
run_command(cmd_buf, ); printf("Enter the SKY2440/TQ2440 IP address:(xxx.xxx.xxx.xxx)\n");
readline(NULL);
strcpy(param_buf2,console_buffer);
sprintf(cmd_buf, "setenv ipaddr %s",param_buf2);
run_command(cmd_buf, ); printf("Enter the Mask IP address:(xxx.xxx.xxx.xxx)\n");
readline(NULL);
strcpy(param_buf3,console_buffer);
sprintf(cmd_buf, "setenv netmask %s",param_buf3);
run_command(cmd_buf, ); printf("Save TFTP IP parameters?(y/n)\n");
if (getc() == 'y' )
{
printf("y");
getc() == '\r';
printf("\n");
sprintf(cmd_buf, "saveenv");
run_command(cmd_buf, );
}
else
{
printf("Not Save it!!!\n");
}
break;
} case '':
{
strcpy(cmd_buf, "tftp 0x30000000 root.bin; nand erase root; nand write.yaffs 0x30000000 root $(filesize)");
run_command(cmd_buf, );
break;
} case '':
{
char tftpaddress[];
char filename[]; printf("Enter downloads to SDRAM address:\n");
readline(NULL);
strcpy(tftpaddress, console_buffer); printf("Enter program name:\n");
readline(NULL);
strcpy(filename, console_buffer); sprintf(cmd_buf, "tftp %s %s", tftpaddress, filename);
printf("tftp %s %s\n", tftpaddress, filename);
run_command(cmd_buf, ); sprintf(cmd_buf, "go %s", tftpaddress);
run_command(cmd_buf, );
break;
} case '':
{
printf("Start Linux ...\n");
strcpy(cmd_buf, "boot_zImage");
run_command(cmd_buf, );
break;
} case '':
{
strcpy(cmd_buf, "nand scrub ");
run_command(cmd_buf, );
// erase_menu_shell();
break;
} case 'A':
case 'a':
{
char filename[]; printf("Enter program name:\n");
readline(NULL);
strcpy(filename, console_buffer); sprintf(cmd_buf, "tftp 0x30000000 %s; nand erase 0x0 $(filesize+1); nand write.jffs2 0x30000000 0x0 $(filesize+1)", filename);
run_command(cmd_buf, );
break;
} case 'B':
case 'b':
{
strcpy(cmd_buf, "tftp 0x30000000 logo.bin; nand erase logo; nand write.jffs2 0x30000000 logo $(filesize)");
run_command(cmd_buf, );
break;
} case 'O':
case 'o':
{
if (bBootFrmNORFlash())
{
strcpy(cmd_buf, "tftp 0x30000000 uboot.bin; protect off all; erase 0 +$(filesize); cp.b 0x30000000 0 $(filesize)");
run_command(cmd_buf, );
}
break;
} case 'P':
case 'p':
{
char *serverip;
serverip=getenv("serverip");
printf("TQ2440 ping PC IP:ping %s\n",serverip);
sprintf(cmd_buf, "ping %s",serverip);
run_command(cmd_buf, );
break;
} case 'R':
case 'r':
{
strcpy(cmd_buf, "reset");
run_command(cmd_buf, );
break;
} case 'T':
case 't':
{
strcpy(cmd_buf, "tftp 0x30008000 zImage.bin; test_zImage");
run_command(cmd_buf, );
break;
} case 'Q':
case 'q':
{
return;
break;
}
default: ;
}
} } int do_menu (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
tftp_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"
); #endif /* #ifdef CONFIG_CMD_MENU */

2、修改common/Makefile

COBJS    = main.o ACEX1K.o altera.o bedbug.o circbuf.o \
cmd_ace.o cmd_autoscript.o \
......
virtex2.o xilinx.o crc16.o xyzModem.o cmd_mac.o cmd_menu.o

3、修改common/main.c,调用菜单命令(menu)

    if (bootdelay >=  && s && !abortboot (bootdelay)) {
# ifdef CONFIG_AUTOBOOT_KEYED
int prev = disable_ctrlc(); /* disable Control C checking */
# endif # ifndef CFG_HUSH_PARSER
run_command (s, );
# else
parse_string_outer(s, FLAG_PARSE_SEMICOLON |
FLAG_EXIT_FROM_LOOP);
# endif # ifdef CONFIG_AUTOBOOT_KEYED
disable_ctrlc(prev); /* restore Control C checking */
# endif
} #ifdef CONFIG_CMD_MENU
run_command("menu", 0);
#endif

4、修改include/configs/TQ2440.h

/***********************************************************
* Command definition
***********************************************************/
#define CONFIG_COMMANDS \
(CONFIG_CMD_DFL | \
CFG_CMD_CACHE | \
CFG_CMD_NAND | \
/*CFG_CMD_EEPROM |*/ \
/*CFG_CMD_I2C |*/ \
/*CFG_CMD_USB |*/ \
CFG_CMD_REGINFO | \
CFG_CMD_DATE | \
CFG_CMD_REGINFO | \
CFG_CMD_PING | \
CFG_CMD_ELF) #define CONFIG_CMD_MENU 1

参考网页:uboot中的快捷菜单的制作说明