在u-boot中制作升级菜单

来源:互联网 发布:理性主义 知乎 编辑:程序博客网 时间:2024/06/06 13:13

利用tftp,在u-boot中制作系统代码升级菜单,可在框架上通过加载SD卡中的文件来取代tftp从而快速烧写或升级系统程序(kernel/rootfs/others)。

#include <common.h>#include <command.h>#include <s_record.h>#include <net.h>#include <exports.h>#include <xyzModem.h>#include <stdio_dev.h>#define MACR_OPERATE_CODE0#define MACR_UPGRADE_XLOADER(MACR_OPERATE_CODE+1)#define MACR_UPGRADE_UBOOT(MACR_OPERATE_CODE+2)#define MACR_UPGRADE_KERNAL(MACR_OPERATE_CODE+3)#define MACR_UPGRADE_ROOTFS(MACR_OPERATE_CODE+4)#define MACR_UPGRADE_EXTFS(MACR_OPERATE_CODE+5)#define IS_TRUE1#define IS_FALSE0#define MACR_MAC_ADDR0#define MACR_SERVER_ADDR1#define MACR_LOCAL_ADDR2static char uboot_version[]={CONFIG_UBOOT_VERSION};static char ip_set_default_flag[3] = {IS_FALSE,IS_FALSE,IS_FALSE};static void menu_usage(void){    printf("\r\n");    printf("---------XXX System Operate Menu--------\r\n");    printf("[r] Reboot system\r\n");      printf("[b] Boot system\r\n");      printf("[x] Upgrade [x-loader]\r\n");    printf("[u] Upgrade [u-boot]\r\n");    printf("[k] Upgrade [linux kernel]\r\n");    printf("[f] Upgrade [root filesystem]\r\n");#if 0    printf("[e] Upgrade [extended filesystem]\r\n");#endif    printf("[v] View the u-boot version\r\n");     printf("[q] Quit from menu\r\n");    printf("--------------------------------------------\r\n");    printf("Select operation by inputting r/b/x,etc..\r\n");    printf("\r\n");}static void ip_display(void){char *s=NULL;s=getenv ("serverip");if(s)    printf("server ip:%s\n",s);else    printf("server ip is not set !!!\n");s=NULL;s=getenv ("ipaddr");if(s)    printf("client ip:%s\n",s);else    printf("client ip is not set !!!\n");}static void confirm_display(void){    printf("input 'y' to continue or 'n' to cancel,default is 'n'\r\n");}/*set a group addr info  (MAC addr,server ip,local ip)   if the ethaddr/server ip/ipaddr env are not set */static void ip_set_default(void){#if defined(CONFIG_PRE_SET_IP)if (!getenv ("ethaddr")) {uchar enetaddr[]={CONFIG_DEFAULT_MAC};setenv("ethaddr",enetaddr);ip_set_default_flag[MACR_MAC_ADDR] = IS_TRUE;}if (!getenv ("serverip")) {uchar serverip[]={CONFIG_DEFAULT_SERVER};setenv("serverip",serverip);ip_set_default_flag[MACR_SERVER_ADDR] = IS_TRUE;}if (!getenv ("ipaddr")) {uchar clientip[]={CONFIG_DEFAULT_LOCAL};setenv("ipaddr",clientip);ip_set_default_flag[MACR_LOCAL_ADDR] = IS_TRUE;}#endif}/* restore ethaddr/server ip/ipaddr info which are set by ip_set_default func */static void ip_restore_org(void){#if defined(CONFIG_PRE_SET_IP)if(ip_set_default_flag[MACR_MAC_ADDR] == IS_TRUE){uchar enetaddr[]={""};setenv("ethaddr",enetaddr);ip_set_default_flag[MACR_MAC_ADDR] = IS_FALSE;}if(ip_set_default_flag[MACR_SERVER_ADDR] == IS_TRUE){uchar serverip[]={""};setenv("serverip",serverip);ip_set_default_flag[MACR_SERVER_ADDR] = IS_FALSE;}if(ip_set_default_flag[MACR_LOCAL_ADDR] == IS_TRUE){uchar clientip[]={""};setenv("ipaddr",clientip);ip_set_default_flag[MACR_LOCAL_ADDR] = IS_FALSE;}#endif}extern ulong  NetBootFileXferSize;static void excute_operation(unsigned char op_code){signed char result=0;   signed char fs_cmd[64]={"nand write.i 0x82000000 "};        signed char temp_buffer[16];switch(op_code){case MACR_UPGRADE_XLOADER:run_command("mw.b 0x82000000 0xFF 0x20000",0);result=run_command("tftp MLO",0);if(result==1){run_command("nand erase 0x00000 0x20000",0);run_command("nandecc hw 2",0);run_command("nand write.i 0x82000000 0x00000 0x20000",0);run_command("nandecc hw 1",0);printf("\n** Upgrade x-loader Successfully **\n");}else{printf("\n** Upgrade x-loader Error **\n");}break;case MACR_UPGRADE_UBOOT:run_command("mw.b 0x82000000 0xFF 0x80000",0);result=run_command("tftp u-boot.bin",0);if(result==1){run_command("nand erase 0x80000 0x80000",0);run_command("nandecc hw 2",0);run_command("nand write.i 0x82000000 0x80000 0x80000",0);run_command("nandecc hw 1",0);printf("\n** Upgrade U-boot Successfully **\n");}else{printf("\n** Upgrade u-boot Error **\n");}break;case MACR_UPGRADE_KERNAL:       //run_command("mw.b 0x82000000 0xFF 0x400000",0);result=run_command("tftp uImage",0);if(result==1){run_command("nand erase 0x280000 0x400000",0);run_command("nandecc hw 1",0);run_command("nand write.i 0x82000000 0x280000 0x400000",0);printf("\n** Upgrade Linux kernel Successfully **\n");}else{printf("\n** Upgrade Linux kernel Error **\n");}break;#if 0case MACR_UPGRADE_EXTFS:printf("\nUpgrade Extended filesystem may take some time, waiting ......\n\n");        //run_command("mw.b 0x82000000 0xFF 0x10000000",0);result=run_command("tftp RFODNCC_ext_ubi.img",0);sprintf(temp_buffer, "0x%X ",(unsigned int)NetBootFileXferSize);strcat(fs_cmd,"0x8880000 ");strcat(fs_cmd,temp_buffer);if(result==1){run_command("nand erase 0x8880000 0x8000000",0);run_command("nandecc hw 1",0);printf("\nFlash flash now, it may take some time,waiting ......\n");run_command(fs_cmd,0);printf("\n** Upgrade Extended filesystem Successfully **\n");}else{printf("\n** Upgrade Extended filesystem Error **\n");}break;#endifcase MACR_UPGRADE_ROOTFS:printf("\nUpgrade Root filesystem may take some time, waiting ......\n\n");        //run_command("mw.b 0x82000000 0xFF 0x10000000",0);result=run_command("tftp RFODNCC_root_ubi.img",0);sprintf(temp_buffer, "0x%X ",(unsigned int)NetBootFileXferSize);strcat(fs_cmd,"0x880000 ");strcat(fs_cmd,temp_buffer);if(result==1){run_command("nand erase 0x880000 0x8000000",0);run_command("nandecc hw 1",0);printf("\nFlash flash now, it may take some time,waiting ......\n");run_command(fs_cmd,0);printf("\n** Upgrade Root filesystem Successfully **\n");}else{printf("\n** Upgrade Root filesystem Error **\n");}break;}   }int main_menu(void){unsigned char c;ip_set_default();while(1){menu_usage();//if (tstc()) {c = getc();switch(c){case 'x':case 'X':     printf("[%c]\n",c);     printf("Selected Upgrade x-loader\n");     ip_display();     confirm_display();     c = getc();      printf("%c\n",c);     if(c=='y' || c=='Y')     {excute_operation(MACR_UPGRADE_XLOADER);     }     else     {  printf("Cancel Operation\r\n");     }    break;case 'u':case 'U':     printf("[%c]\n",c);     printf("Selected Upgrade u-boot\n");     ip_display();     confirm_display();     c = getc();      printf("%c\n",c);     if(c=='y' || c=='Y')     {excute_operation(MACR_UPGRADE_UBOOT);     }     else     {  printf("Cancel Operation\r\n");     }    break;case 'k':case 'K':     printf("[%c]\n",c);     printf("Selected Upgrade linux kernel\n");     ip_display();     confirm_display();     c = getc();      printf("%c\n",c);     if(c=='y' || c=='Y')     {excute_operation(MACR_UPGRADE_KERNAL);     }     else     {  printf("Cancel Operation\r\n");     }    break;#if 0case 'e':case 'E':     printf("[%c]\n",c);     printf("Selected Upgrade extended filesystem\n");     ip_display();     confirm_display();     c = getc();      printf("%c\n",c);     if(c=='y' || c=='Y')     {excute_operation(MACR_UPGRADE_EXTFS);     }     else     {  printf("Cancel Operation\r\n");     }    break;#endifcase 'f':case 'F':     printf("[%c]\n",c);     printf("Selected Upgrade root filesystem\n");     ip_display();     confirm_display();     c = getc();      printf("%c\n",c);     if(c=='y' || c=='Y')     {excute_operation(MACR_UPGRADE_ROOTFS);     }     else     {  printf("Cancel Operation\r\n");     }    break;case 'v':case 'V':  printf("[%c]\n",c); printf("Selected View the u-boot version\n"); printf("\nu-boot version is:%s\n",uboot_version);    break;case 'q':case 'Q':  printf("[%c]\n",c); printf("Selected Quit from menu\n");/* restore the ip env info */ip_restore_org();    return 0;case 'b':case 'B': run_command("boot",0);/* restore the ip env info */ip_restore_org();        case 'r':case 'R': run_command("reset",0);    return 0;    break;}}}return 0;}U_BOOT_CMD(menu,3, 0,main_menu,"menu list the operation code and operation content","input operation code,eg.a,b.etc");


原创粉丝点击