在uboot中加入cmd_run命令,运行环境变量
在学习uboot的过程中会经常烧录程序,每次都要敲一些下载指令。这样是不是很麻烦,有什么办法能快速的烧写呢。很简单,将需要敲击的指令编译到uboot中,以环境变量的形式存在。但是环境变量很好加,如何运行环境变量呢。这就需要自己加入run指令了。本文旨在简化uboot、Linux内核及文件系统的烧录过程。
本文基于海思的Hi3531的uboot修改,首先在uboot目录下的include/configs/godnet.h中修改加入如下代码,增加烧写的环境变量。
- #define CONFIG_BOOTDELAY 1
- #define CONFIG_BOOTARGS "mem=64M console=ttyAMA0,115200 root=/dev/mtdblock2 rootfstype=jffs2 mtdparts=hi_sfc:1M(boot),4M(kernel),11M(rootfs)"
- #define CONFIG_NETMASK 255.255.255.0 /* talk on MY local net */
- #define CONFIG_IPADDR 192.168.0.251 /* static IP I currently own */
- #define CONFIG_SERVERIP 192.168.0.27 /* current IP of tftp server ip */
- #define CONFIG_ETHADDR 00:00:23:34:45:66
- #define CONFIG_BOOTFILE "uImage" /* file to load */
- #define CONFIG_BAUDRATE 115200
- #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"
- #define CONFIG_KERNEL_BURN "mw.b 82000000 ff 400000;tftp 82000000 uImage;sf probe 0;sf erase 100000 400000;sf write 82000000 100000 400000"
- #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数组中加入环境变量的值。具体代码如下:
- uchar default_environment[] = {
- #ifdef CONFIG_BOOTARGS
- "bootargs=" CONFIG_BOOTARGS "\0"
- #endif
- #ifdef CONFIG_BOOTCOMMAND
- "bootcmd=" CONFIG_BOOTCOMMAND "\0"
- #endif
- #ifdef CONFIG_RAMBOOTCOMMAND
- "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
- #endif
- #ifdef CONFIG_NFSBOOTCOMMAND
- "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
- #endif
- #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
- "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
- #endif
- #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
- "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
- #endif
- #ifdef CONFIG_LOADS_ECHO
- "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
- #endif
- #ifdef CONFIG_MDIO_INTF
- "mdio_intf=" CONFIG_MDIO_INTF "\0"
- #endif
- #ifdef CONFIG_ETHADDR
- "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
- #endif
- #ifdef CONFIG_ETH1ADDR
- "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
- #endif
- #ifdef CONFIG_ETH2ADDR
- "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
- #endif
- #ifdef CONFIG_ETH3ADDR
- "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
- #endif
- #ifdef CONFIG_ETH4ADDR
- "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
- #endif
- #ifdef CONFIG_ETH5ADDR
- "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
- #endif
- #ifdef CONFIG_IPADDR
- "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
- #endif
- #ifdef CONFIG_SERVERIP
- "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
- #endif
- #ifdef CONFIG_SYS_AUTOLOAD
- "autoload=" CONFIG_SYS_AUTOLOAD "\0"
- #endif
- #ifdef CONFIG_PREBOOT
- "preboot=" CONFIG_PREBOOT "\0"
- #endif
- #ifdef CONFIG_ROOTPATH
- "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
- #endif
- #ifdef CONFIG_GATEWAYIP
- "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
- #endif
- #ifdef CONFIG_NETMASK
- "netmask=" MK_STR(CONFIG_NETMASK) "\0"
- #endif
- #ifdef CONFIG_HOSTNAME
- "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
- #endif
- #ifdef CONFIG_BOOTFILE
- "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
- #endif
- #ifdef CONFIG_LOADADDR
- "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
- #endif
- #ifdef CONFIG_CLOCKS_IN_MHZ
- "clocks_in_mhz=1\0"
- #endif
- #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
- "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
- #endif
- #ifdef CONFIG_UBOOT_BURN
- "uboot_burn=" CONFIG_UBOOT_BURN "\0"
- #endif
- #ifdef CONFIG_KERNEL_BURN
- "kernel_burn=" CONFIG_KERNEL_BURN "\0"
- #endif
- #ifdef CONFIG_ROOT_BURN
- "root_burn=" CONFIG_ROOT_BURN "\0"
- #endif
- #ifdef CONFIG_EXTRA_ENV_SETTINGS
- CONFIG_EXTRA_ENV_SETTINGS
- #endif
- "\0"
- };
上述红色部分为加入的环境变量,其中uboot_burn,kernel_burn,root_burn为环境变量的名称。此后在uboot指令输入界面输入printenv可以看到变量值。
至此,环境变量修改完毕。下面如何在uboot中运行环境变量呢,其实只要能让uboot_burn变量中的内容执行即可。因此需要有一条指令可以运行这个环境变量。此时就需要cmd_run.c参与了。
在学习uboot时,我们知道在/common路径下面有很多以cmd_开头的文件,这些文件即使在uboot中可以运行的指令。但是,需要在配置文件和Makefile中添加支持。
首先我们把cmd_run.c文件创建了,代码如下:
- /*
- * (C) Copyright 2000-2003
- * 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
- */
- #include <common.h>
- #include <command.h>
- int do_run (cmd_tbl_t * cmdtp, int flag, int argc, charchar *argv[])
- {
- int i;
- if (argc < 2) {
- cmd_usage(cmdtp);
- return 1;
- }
- for (i=1; i<argc; ++i) {
- charchar *arg;
- if ((arg = getenv (argv[i])) == NULL) {
- printf ("## Error: \"%s\" not defined\n", argv[i]);
- return 1;
- }
- #ifndef CONFIG_SYS_HUSH_PARSER
- if (run_command (arg, flag) == -1)
- return 1;
- #else
- if (parse_string_outer(arg,
- FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
- return 1;
- #endif
- }
- return 0;
- }
- U_BOOT_CMD(
- run, CONFIG_SYS_MAXARGS, 1, do_run,
- "run commands in an environment variable",
- "var [...]\n"
- " - run the commands in the environment variable(s) 'var'"
- );
该C文件创建完,放入到common路径下,下面需要修改common路径下的Makefile,在文件中加入COBJS-y += cmd_run.o即可。这是将cmd_run.c加入编译选项中。在如下位置添加红色部分。
- COBJS-$(CONFIG_CMD_I2C) += cmd_i2c.o
- COBJS-$(CONFIG_CMD_IDE) += cmd_ide.o
- COBJS-$(CONFIG_CMD_IMMAP) += cmd_immap.o
- COBJS-$(CONFIG_CMD_IRQ) += cmd_irq.o
- COBJS-$(CONFIG_CMD_ITEST) += cmd_itest.o
- COBJS-$(CONFIG_CMD_JFFS2) += cmd_jffs2.o
- COBJS-$(CONFIG_CMD_CRAMFS) += cmd_cramfs.o
- COBJS-$(CONFIG_CMD_LICENSE) += cmd_license.o
- COBJS-y += cmd_load.o
- COBJS-y += cmd_run.o
至此run运行环境变量的方法已经添加完毕,按照正常的流程编译uboot,然后按照正常流程烧写。烧录成功后,在uboot运行指令界面输入run会出现如下界面
- U-Boot 2010.06 (Oct 26 2015 - 10:15:14)
- DRAM: 256 MiB
- NAND: Special Nand id table Version 1.35
- Nand ID: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
- No NAND device found!!!
- 0 MiB
- Check spi flash controller v300. found
- Spi(cs1) ID: 0xEF 0x40 0x18 0x00 0x00 0x00
- Spi(cs1): Block:64KB Chip:16MB Name:"W25Q128BV"
- In: serial
- Out: serial
- Err: serial
- judge ddr init
- user init finish.
- Hit any key to stop autoboot: 0
- hisilicon # run
- run - run commands in an environment variable
- hisilicon #
现在可以去运行刚刚添加的环境变量了,输入:“run uboot_burn”试试吧