在uboot中加入cmd_run命令,运行环境变量

在学习uboot的过程中会经常烧录程序,每次都要敲一些下载指令。这样是不是很麻烦,有什么办法能快速的烧写呢。很简单,将需要敲击的指令编译到uboot中,以环境变量的形式存在。但是环境变量很好加,如何运行环境变量呢。这就需要自己加入run指令了。本文旨在简化uboot、Linux内核及文件系统的烧录过程。

本文基于海思的Hi3531的uboot修改,首先在uboot目录下的include/configs/godnet.h中修改加入如下代码,增加烧写的环境变量。

[objc] view plain copy
 
  1. #define CONFIG_BOOTDELAY    1  
  2. #define CONFIG_BOOTARGS "mem=64M console=ttyAMA0,115200 root=/dev/mtdblock2 rootfstype=jffs2 mtdparts=hi_sfc:1M(boot),4M(kernel),11M(rootfs)"  
  3. #define CONFIG_NETMASK  255.255.255.0       /* talk on MY local net */  
  4. #define CONFIG_IPADDR   192.168.0.251        /* static IP I currently own */  
  5. #define CONFIG_SERVERIP 192.168.0.27     /* current IP of tftp server ip */  
  6. #define CONFIG_ETHADDR  00:00:23:34:45:66  
  7. #define CONFIG_BOOTFILE "uImage"        /* file to load */  
  8. #define CONFIG_BAUDRATE         115200  
  9. #define CONFIG_UBOOT_BURN "mw.b 82000000 ff 100000;tftp 0x82000000 u-boot-ok.bin;sf probe 0;sf erase 0 100000;sf write 82000000 0 100000"  
  10. #define CONFIG_KERNEL_BURN "mw.b 82000000 ff 400000;tftp 82000000 uImage;sf probe 0;sf erase 100000 400000;sf write 82000000 100000 400000"  
  11. #define CONFIG_ROOT_BURN "mw.b 82000000 ff b00000; tftp 0x82000000 rootfs.jffs2;sf probe 0;sf erase 500000 b00000;sf write 82000000 500000 b00000"  

 

其中,红色部分为加入的指令

CONFIG_UBOOT_BURN为烧写uboot的指令,CONFIG_KERNEL_BURN为烧写内核的指令,CONFIG_ROOT_BURN为烧写根文件系统的指令,具体指令内容不在详述;

 

还需要在环境变量的文件/common/env_common.c的default_environment数组中加入环境变量的值。具体代码如下:

 

[objc] view plain copy
 
  1. uchar default_environment[] = {  
  2. #ifdef  CONFIG_BOOTARGS  
  3.     "bootargs=" CONFIG_BOOTARGS         "\0"  
  4. #endif  
  5. #ifdef  CONFIG_BOOTCOMMAND  
  6.     "bootcmd="  CONFIG_BOOTCOMMAND      "\0"  
  7. #endif  
  8. #ifdef  CONFIG_RAMBOOTCOMMAND  
  9.     "ramboot="  CONFIG_RAMBOOTCOMMAND       "\0"  
  10. #endif  
  11. #ifdef  CONFIG_NFSBOOTCOMMAND  
  12.     "nfsboot="  CONFIG_NFSBOOTCOMMAND       "\0"  
  13. #endif  
  14. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)  
  15.     "bootdelay="    MK_STR(CONFIG_BOOTDELAY)    "\0"  
  16. #endif  
  17. #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)  
  18.     "baudrate=" MK_STR(CONFIG_BAUDRATE)     "\0"  
  19. #endif  
  20. #ifdef  CONFIG_LOADS_ECHO  
  21.     "loads_echo="   MK_STR(CONFIG_LOADS_ECHO)   "\0"  
  22. #endif  
  23. #ifdef  CONFIG_MDIO_INTF  
  24.     "mdio_intf="    CONFIG_MDIO_INTF            "\0"  
  25. #endif  
  26. #ifdef  CONFIG_ETHADDR  
  27.     "ethaddr="  MK_STR(CONFIG_ETHADDR)      "\0"  
  28. #endif  
  29. #ifdef  CONFIG_ETH1ADDR  
  30.     "eth1addr=" MK_STR(CONFIG_ETH1ADDR)     "\0"  
  31. #endif  
  32. #ifdef  CONFIG_ETH2ADDR  
  33.     "eth2addr=" MK_STR(CONFIG_ETH2ADDR)     "\0"  
  34. #endif  
  35. #ifdef  CONFIG_ETH3ADDR  
  36.     "eth3addr=" MK_STR(CONFIG_ETH3ADDR)     "\0"  
  37. #endif  
  38. #ifdef  CONFIG_ETH4ADDR  
  39.     "eth4addr=" MK_STR(CONFIG_ETH4ADDR)     "\0"  
  40. #endif  
  41. #ifdef  CONFIG_ETH5ADDR  
  42.     "eth5addr=" MK_STR(CONFIG_ETH5ADDR)     "\0"  
  43. #endif  
  44. #ifdef  CONFIG_IPADDR  
  45.     "ipaddr="   MK_STR(CONFIG_IPADDR)       "\0"  
  46. #endif  
  47. #ifdef  CONFIG_SERVERIP  
  48.     "serverip=" MK_STR(CONFIG_SERVERIP)     "\0"  
  49. #endif  
  50. #ifdef  CONFIG_SYS_AUTOLOAD  
  51.     "autoload=" CONFIG_SYS_AUTOLOAD         "\0"  
  52. #endif  
  53. #ifdef  CONFIG_PREBOOT  
  54.     "preboot="  CONFIG_PREBOOT          "\0"  
  55. #endif  
  56. #ifdef  CONFIG_ROOTPATH  
  57.     "rootpath=" MK_STR(CONFIG_ROOTPATH)     "\0"  
  58. #endif  
  59. #ifdef  CONFIG_GATEWAYIP  
  60.     "gatewayip="    MK_STR(CONFIG_GATEWAYIP)    "\0"  
  61. #endif  
  62. #ifdef  CONFIG_NETMASK  
  63.     "netmask="  MK_STR(CONFIG_NETMASK)      "\0"  
  64. #endif  
  65. #ifdef  CONFIG_HOSTNAME  
  66.     "hostname=" MK_STR(CONFIG_HOSTNAME)     "\0"  
  67. #endif  
  68. #ifdef  CONFIG_BOOTFILE  
  69.     "bootfile=" MK_STR(CONFIG_BOOTFILE)     "\0"  
  70. #endif  
  71. #ifdef  CONFIG_LOADADDR  
  72.     "loadaddr=" MK_STR(CONFIG_LOADADDR)     "\0"  
  73. #endif  
  74. #ifdef  CONFIG_CLOCKS_IN_MHZ  
  75.     "clocks_in_mhz=1\0"  
  76. #endif  
  77. #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)  
  78.     "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY)    "\0"  
  79. #endif  
  80. #ifdef CONFIG_UBOOT_BURN  
  81.     "uboot_burn="   CONFIG_UBOOT_BURN           "\0"  
  82. #endif  
  83. #ifdef CONFIG_KERNEL_BURN  
  84.     "kernel_burn="  CONFIG_KERNEL_BURN          "\0"  
  85. #endif  
  86. #ifdef CONFIG_ROOT_BURN  
  87.     "root_burn="    CONFIG_ROOT_BURN            "\0"  
  88. #endif  
  89. #ifdef  CONFIG_EXTRA_ENV_SETTINGS  
  90.     CONFIG_EXTRA_ENV_SETTINGS  
  91. #endif  
  92.     "\0"  
  93. };  

上述红色部分为加入的环境变量,其中uboot_burn,kernel_burn,root_burn为环境变量的名称。此后在uboot指令输入界面输入printenv可以看到变量值。

 

至此,环境变量修改完毕。下面如何在uboot中运行环境变量呢,其实只要能让uboot_burn变量中的内容执行即可。因此需要有一条指令可以运行这个环境变量。此时就需要cmd_run.c参与了。

在学习uboot时,我们知道在/common路径下面有很多以cmd_开头的文件,这些文件即使在uboot中可以运行的指令。但是,需要在配置文件和Makefile中添加支持。

首先我们把cmd_run.c文件创建了,代码如下:

 

[objc] view plain copy
 
  1. /*  
  2. * (C) Copyright 2000-2003  
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.  
  4. *  
  5. * See file CREDITS for list of people who contributed to this  
  6. * project.  
  7. *  
  8. * This program is free software; you can redistribute it and/or  
  9. * modify it under the terms of the GNU General Public License as  
  10. * published by the Free Software Foundation; either version 2 of  
  11. * the License, or (at your option) any later version.  
  12. *  
  13. * This program is distributed in the hope that it will be useful,  
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of  
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
  16. * GNU General Public License for more details.  
  17. *  
  18. * You should have received a copy of the GNU General Public License  
  19. * along with this program; if not, write to the Free Software  
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,  
  21. * MA 02111-1307 USA  
  22. */   
  23.   
  24. #include <common.h>  
  25. #include <command.h>  
  26.   
  27. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, charchar *argv[])  
  28. {  
  29.     int i;  
  30.   
  31.     if (argc < 2) {  
  32.         cmd_usage(cmdtp);  
  33.         return 1;  
  34.     }  
  35.   
  36.     for (i=1; i<argc; ++i) {  
  37.         charchar *arg;  
  38.   
  39.         if ((arg = getenv (argv[i])) == NULL) {  
  40.             printf ("## Error: \"%s\" not defined\n", argv[i]);  
  41.             return 1;  
  42.         }  
  43. #ifndef CONFIG_SYS_HUSH_PARSER  
  44.         if (run_command (arg, flag) == -1)  
  45.             return 1;  
  46. #else  
  47.         if (parse_string_outer(arg,  
  48.             FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)  
  49.             return 1;  
  50. #endif  
  51.     }  
  52.     return 0;  
  53. }  
  54.   
  55. U_BOOT_CMD(  
  56.     run,    CONFIG_SYS_MAXARGS, 1,  do_run,  
  57.     "run commands in an environment variable",  
  58.     "var [...]\n"  
  59.     "    - run the commands in the environment variable(s) 'var'"  
  60. );  

该C文件创建完,放入到common路径下,下面需要修改common路径下的Makefile,在文件中加入COBJS-y += cmd_run.o即可。这是将cmd_run.c加入编译选项中。在如下位置添加红色部分。

 

 

[objc] view plain copy
 
  1. COBJS-$(CONFIG_CMD_I2C) += cmd_i2c.o  
  2. COBJS-$(CONFIG_CMD_IDE) += cmd_ide.o  
  3. COBJS-$(CONFIG_CMD_IMMAP) += cmd_immap.o  
  4. COBJS-$(CONFIG_CMD_IRQ) += cmd_irq.o  
  5. COBJS-$(CONFIG_CMD_ITEST) += cmd_itest.o  
  6. COBJS-$(CONFIG_CMD_JFFS2) += cmd_jffs2.o  
  7. COBJS-$(CONFIG_CMD_CRAMFS) += cmd_cramfs.o  
  8. COBJS-$(CONFIG_CMD_LICENSE) += cmd_license.o  
  9. COBJS-y += cmd_load.o  
[objc] view plain copy
 
  1. COBJS-y += cmd_run.o  


至此run运行环境变量的方法已经添加完毕,按照正常的流程编译uboot,然后按照正常流程烧写。烧录成功后,在uboot运行指令界面输入run会出现如下界面

 

 

[objc] view plain copy
 
  1. U-Boot 2010.06 (Oct 22015 - 10:15:14)  
  2.   
  3. DRAM:  256 MiB  
  4. NAND:  Special Nand id table Version 1.35  
  5. Nand ID: 0x00x00x00x00x00x00x00x00  
  6. No NAND device found!!!  
  7. 0 MiB  
  8. Check spi flash controller v300. found  
  9. Spi(cs1) ID: 0xEF 0x40x10x00x00x00  
  10. Spi(cs1): Block:64KB Chip:16MB Name:"W25Q128BV"  
  11. In:    serial  
  12. Out:   serial  
  13. Err:   serial  
  14. judge ddr init  
  15. user init finish.  
  16. Hit any key to stop autoboot:  0   
  17. hisilicon # run  
  18. run - run commands in an environment variable  
  19.   
  20. hisilicon #   

 

现在可以去运行刚刚添加的环境变量了,输入:“run uboot_burn”试试吧

posted @ 2016-12-25 12:02  苏博  阅读(2545)  评论(0编辑  收藏  举报