wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1 | /* |
Detlev Zundel | 2111d3e | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 5 | * (C) Copyright 2008 |
| 6 | * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de. |
| 7 | * |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
Joe Hershberger | e79a91f | 2012-10-12 10:23:37 +0000 | [diff] [blame^] | 29 | #include <linux/stringify.h> |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <stddef.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/ioctl.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <unistd.h> |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 38 | |
| 39 | #ifdef MTD_OLD |
Wolfgang Denk | 4cd94a7 | 2008-09-02 21:17:36 +0200 | [diff] [blame] | 40 | # include <stdint.h> |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 41 | # include <linux/mtd/mtd.h> |
| 42 | #else |
Wolfgang Denk | c7d16fd | 2008-01-08 22:58:27 +0100 | [diff] [blame] | 43 | # define __user /* nothing */ |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 44 | # include <mtd/mtd-user.h> |
| 45 | #endif |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 46 | |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 47 | #include "fw_env.h" |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 48 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 49 | #define WHITESPACE(c) ((c == '\t') || (c == ' ')) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 50 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 51 | #define min(x, y) ({ \ |
| 52 | typeof(x) _min1 = (x); \ |
| 53 | typeof(y) _min2 = (y); \ |
| 54 | (void) (&_min1 == &_min2); \ |
| 55 | _min1 < _min2 ? _min1 : _min2; }) |
| 56 | |
| 57 | struct envdev_s { |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 58 | char devname[16]; /* Device name */ |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 59 | ulong devoff; /* Device offset */ |
| 60 | ulong env_size; /* environment size */ |
| 61 | ulong erase_size; /* device erase size */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 62 | ulong env_sectors; /* number of environment sectors */ |
| 63 | uint8_t mtd_type; /* type of the MTD device */ |
| 64 | }; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 65 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 66 | static struct envdev_s envdevices[2] = |
| 67 | { |
| 68 | { |
| 69 | .mtd_type = MTD_ABSENT, |
| 70 | }, { |
| 71 | .mtd_type = MTD_ABSENT, |
| 72 | }, |
| 73 | }; |
| 74 | static int dev_current; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 75 | |
| 76 | #define DEVNAME(i) envdevices[(i)].devname |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 77 | #define DEVOFFSET(i) envdevices[(i)].devoff |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 78 | #define ENVSIZE(i) envdevices[(i)].env_size |
| 79 | #define DEVESIZE(i) envdevices[(i)].erase_size |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 80 | #define ENVSECTORS(i) envdevices[(i)].env_sectors |
| 81 | #define DEVTYPE(i) envdevices[(i)].mtd_type |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 82 | |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 83 | #define CUR_ENVSIZE ENVSIZE(dev_current) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 84 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 85 | #define ENV_SIZE getenvsize() |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 86 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 87 | struct env_image_single { |
| 88 | uint32_t crc; /* CRC32 over data bytes */ |
| 89 | char data[]; |
| 90 | }; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 91 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 92 | struct env_image_redundant { |
| 93 | uint32_t crc; /* CRC32 over data bytes */ |
| 94 | unsigned char flags; /* active or obsolete */ |
| 95 | char data[]; |
| 96 | }; |
| 97 | |
| 98 | enum flag_scheme { |
| 99 | FLAG_NONE, |
| 100 | FLAG_BOOLEAN, |
| 101 | FLAG_INCREMENTAL, |
| 102 | }; |
| 103 | |
| 104 | struct environment { |
| 105 | void *image; |
| 106 | uint32_t *crc; |
| 107 | unsigned char *flags; |
| 108 | char *data; |
| 109 | enum flag_scheme flag_scheme; |
| 110 | }; |
| 111 | |
| 112 | static struct environment environment = { |
| 113 | .flag_scheme = FLAG_NONE, |
| 114 | }; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 115 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 116 | static int HaveRedundEnv = 0; |
| 117 | |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 118 | static unsigned char active_flag = 1; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 119 | /* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */ |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 120 | static unsigned char obsolete_flag = 0; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 121 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 122 | |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 123 | static char default_environment[] = { |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 124 | #if defined(CONFIG_BOOTARGS) |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 125 | "bootargs=" CONFIG_BOOTARGS "\0" |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 126 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 127 | #if defined(CONFIG_BOOTCOMMAND) |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 128 | "bootcmd=" CONFIG_BOOTCOMMAND "\0" |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 129 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 130 | #if defined(CONFIG_RAMBOOTCOMMAND) |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 131 | "ramboot=" CONFIG_RAMBOOTCOMMAND "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 132 | #endif |
| 133 | #if defined(CONFIG_NFSBOOTCOMMAND) |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 134 | "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 135 | #endif |
| 136 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 137 | "bootdelay=" __stringify(CONFIG_BOOTDELAY) "\0" |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 138 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 139 | #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0) |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 140 | "baudrate=" __stringify(CONFIG_BAUDRATE) "\0" |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 141 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 142 | #ifdef CONFIG_LOADS_ECHO |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 143 | "loads_echo=" __stringify(CONFIG_LOADS_ECHO) "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 144 | #endif |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 145 | #ifdef CONFIG_ETHADDR |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 146 | "ethaddr=" __stringify(CONFIG_ETHADDR) "\0" |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 147 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 148 | #ifdef CONFIG_ETH1ADDR |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 149 | "eth1addr=" __stringify(CONFIG_ETH1ADDR) "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 150 | #endif |
| 151 | #ifdef CONFIG_ETH2ADDR |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 152 | "eth2addr=" __stringify(CONFIG_ETH2ADDR) "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 153 | #endif |
wdenk | 54070ab | 2004-12-31 09:32:47 +0000 | [diff] [blame] | 154 | #ifdef CONFIG_ETH3ADDR |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 155 | "eth3addr=" __stringify(CONFIG_ETH3ADDR) "\0" |
wdenk | 54070ab | 2004-12-31 09:32:47 +0000 | [diff] [blame] | 156 | #endif |
richardretanubun | e5167f1 | 2008-09-29 18:28:23 -0400 | [diff] [blame] | 157 | #ifdef CONFIG_ETH4ADDR |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 158 | "eth4addr=" __stringify(CONFIG_ETH4ADDR) "\0" |
richardretanubun | e5167f1 | 2008-09-29 18:28:23 -0400 | [diff] [blame] | 159 | #endif |
| 160 | #ifdef CONFIG_ETH5ADDR |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 161 | "eth5addr=" __stringify(CONFIG_ETH5ADDR) "\0" |
richardretanubun | e5167f1 | 2008-09-29 18:28:23 -0400 | [diff] [blame] | 162 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 163 | #ifdef CONFIG_ETHPRIME |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 164 | "ethprime=" CONFIG_ETHPRIME "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 165 | #endif |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 166 | #ifdef CONFIG_IPADDR |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 167 | "ipaddr=" __stringify(CONFIG_IPADDR) "\0" |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 168 | #endif |
| 169 | #ifdef CONFIG_SERVERIP |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 170 | "serverip=" __stringify(CONFIG_SERVERIP) "\0" |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 171 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 172 | #ifdef CONFIG_SYS_AUTOLOAD |
| 173 | "autoload=" CONFIG_SYS_AUTOLOAD "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 174 | #endif |
| 175 | #ifdef CONFIG_ROOTPATH |
Joe Hershberger | 257ff78 | 2011-10-13 13:03:47 +0000 | [diff] [blame] | 176 | "rootpath=" CONFIG_ROOTPATH "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 177 | #endif |
| 178 | #ifdef CONFIG_GATEWAYIP |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 179 | "gatewayip=" __stringify(CONFIG_GATEWAYIP) "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 180 | #endif |
| 181 | #ifdef CONFIG_NETMASK |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 182 | "netmask=" __stringify(CONFIG_NETMASK) "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 183 | #endif |
| 184 | #ifdef CONFIG_HOSTNAME |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 185 | "hostname=" __stringify(CONFIG_HOSTNAME) "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 186 | #endif |
| 187 | #ifdef CONFIG_BOOTFILE |
Joe Hershberger | e4da248 | 2011-10-13 13:03:48 +0000 | [diff] [blame] | 188 | "bootfile=" CONFIG_BOOTFILE "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 189 | #endif |
| 190 | #ifdef CONFIG_LOADADDR |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 191 | "loadaddr=" __stringify(CONFIG_LOADADDR) "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 192 | #endif |
| 193 | #ifdef CONFIG_PREBOOT |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 194 | "preboot=" CONFIG_PREBOOT "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 195 | #endif |
| 196 | #ifdef CONFIG_CLOCKS_IN_MHZ |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 197 | "clocks_in_mhz=" "1" "\0" |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 198 | #endif |
stroese | f5dd410 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 199 | #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0) |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 200 | "pcidelay=" __stringify(CONFIG_PCI_BOOTDELAY) "\0" |
stroese | f5dd410 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 201 | #endif |
Benoît Thébaudeau | eb6aaeb | 2012-08-10 07:45:15 +0000 | [diff] [blame] | 202 | #ifdef CONFIG_ENV_VARS_UBOOT_CONFIG |
| 203 | "arch=" CONFIG_SYS_ARCH "\0" |
| 204 | "cpu=" CONFIG_SYS_CPU "\0" |
| 205 | "board=" CONFIG_SYS_BOARD "\0" |
| 206 | #ifdef CONFIG_SYS_VENDOR |
| 207 | "vendor=" CONFIG_SYS_VENDOR "\0" |
| 208 | #endif |
| 209 | #ifdef CONFIG_SYS_SOC |
| 210 | "soc=" CONFIG_SYS_SOC "\0" |
| 211 | #endif |
| 212 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 213 | #ifdef CONFIG_EXTRA_ENV_SETTINGS |
| 214 | CONFIG_EXTRA_ENV_SETTINGS |
| 215 | #endif |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 216 | "\0" /* Termimate struct environment data with 2 NULs */ |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 219 | static int flash_io (int mode); |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 220 | static char *envmatch (char * s1, char * s2); |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 221 | static int parse_config (void); |
| 222 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 223 | #if defined(CONFIG_FILE) |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 224 | static int get_config (char *); |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 225 | #endif |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 226 | static inline ulong getenvsize (void) |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 227 | { |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 228 | ulong rc = CUR_ENVSIZE - sizeof(long); |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 229 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 230 | if (HaveRedundEnv) |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 231 | rc -= sizeof (char); |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 232 | return rc; |
| 233 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 234 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 235 | static char *fw_string_blank(char *s, int noblank) |
| 236 | { |
| 237 | int i; |
| 238 | int len = strlen(s); |
| 239 | |
| 240 | for (i = 0; i < len; i++, s++) { |
| 241 | if ((noblank && !WHITESPACE(*s)) || |
| 242 | (!noblank && WHITESPACE(*s))) |
| 243 | break; |
| 244 | } |
| 245 | if (i == len) |
| 246 | return NULL; |
| 247 | |
| 248 | return s; |
| 249 | } |
| 250 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 251 | /* |
| 252 | * Search the environment for a variable. |
| 253 | * Return the value, if found, or NULL, if not found. |
| 254 | */ |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 255 | char *fw_getenv (char *name) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 256 | { |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 257 | char *env, *nxt; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 258 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 259 | for (env = environment.data; *env; env = nxt + 1) { |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 260 | char *val; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 261 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 262 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 263 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 264 | fprintf (stderr, "## Error: " |
| 265 | "environment not terminated\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 266 | return NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 269 | val = envmatch (name, env); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 270 | if (!val) |
| 271 | continue; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 272 | return val; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 273 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 274 | return NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Print the current definition of one, or more, or all |
| 279 | * environment variables |
| 280 | */ |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 281 | int fw_printenv (int argc, char *argv[]) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 282 | { |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 283 | char *env, *nxt; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 284 | int i, n_flag; |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 285 | int rc = 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 286 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 287 | if (fw_env_open()) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 288 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 289 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 290 | if (argc == 1) { /* Print all env variables */ |
| 291 | for (env = environment.data; *env; env = nxt + 1) { |
| 292 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 293 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 294 | fprintf (stderr, "## Error: " |
| 295 | "environment not terminated\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 296 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 300 | printf ("%s\n", env); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 301 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 302 | return 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 303 | } |
| 304 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 305 | if (strcmp (argv[1], "-n") == 0) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 306 | n_flag = 1; |
| 307 | ++argv; |
| 308 | --argc; |
| 309 | if (argc != 2) { |
| 310 | fprintf (stderr, "## Error: " |
| 311 | "`-n' option requires exactly one argument\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 312 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 313 | } |
| 314 | } else { |
| 315 | n_flag = 0; |
| 316 | } |
| 317 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 318 | for (i = 1; i < argc; ++i) { /* print single env variables */ |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 319 | char *name = argv[i]; |
| 320 | char *val = NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 321 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 322 | for (env = environment.data; *env; env = nxt + 1) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 323 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 324 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 325 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 326 | fprintf (stderr, "## Error: " |
| 327 | "environment not terminated\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 328 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 331 | val = envmatch (name, env); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 332 | if (val) { |
| 333 | if (!n_flag) { |
| 334 | fputs (name, stdout); |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 335 | putc ('=', stdout); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 336 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 337 | puts (val); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 338 | break; |
| 339 | } |
| 340 | } |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 341 | if (!val) { |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 342 | fprintf (stderr, "## Error: \"%s\" not defined\n", name); |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 343 | rc = -1; |
| 344 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 345 | } |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 346 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 347 | return rc; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 350 | int fw_env_close(void) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 351 | { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 352 | /* |
| 353 | * Update CRC |
| 354 | */ |
| 355 | *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 356 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 357 | /* write environment back to flash */ |
| 358 | if (flash_io(O_RDWR)) { |
| 359 | fprintf(stderr, |
| 360 | "Error: can't write fw_env to flash\n"); |
| 361 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 364 | return 0; |
| 365 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 366 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 367 | |
| 368 | /* |
| 369 | * Set/Clear a single variable in the environment. |
| 370 | * This is called in sequence to update the environment |
| 371 | * in RAM without updating the copy in flash after each set |
| 372 | */ |
| 373 | int fw_env_write(char *name, char *value) |
| 374 | { |
| 375 | int len; |
| 376 | char *env, *nxt; |
| 377 | char *oldval = NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * search if variable with this name already exists |
| 381 | */ |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 382 | for (nxt = env = environment.data; *env; env = nxt + 1) { |
| 383 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 384 | if (nxt >= &environment.data[ENV_SIZE]) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 385 | fprintf(stderr, "## Error: " |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 386 | "environment not terminated\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 387 | errno = EINVAL; |
| 388 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 391 | if ((oldval = envmatch (name, env)) != NULL) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Delete any existing definition |
| 397 | */ |
| 398 | if (oldval) { |
Grant Erickson | 528178a | 2011-12-22 08:59:55 +0000 | [diff] [blame] | 399 | #ifndef CONFIG_ENV_OVERWRITE |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 400 | /* |
| 401 | * Ethernet Address and serial# can be set only once |
| 402 | */ |
Grant Erickson | 528178a | 2011-12-22 08:59:55 +0000 | [diff] [blame] | 403 | if ( |
| 404 | (strcmp(name, "serial#") == 0) || |
| 405 | ((strcmp(name, "ethaddr") == 0) |
| 406 | #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR) |
Marek Vasut | 0b3176c | 2012-09-23 17:41:24 +0200 | [diff] [blame] | 407 | && (strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0) |
Grant Erickson | 528178a | 2011-12-22 08:59:55 +0000 | [diff] [blame] | 408 | #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */ |
| 409 | ) ) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 410 | fprintf (stderr, "Can't overwrite \"%s\"\n", name); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 411 | errno = EROFS; |
| 412 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 413 | } |
Grant Erickson | 528178a | 2011-12-22 08:59:55 +0000 | [diff] [blame] | 414 | #endif /* CONFIG_ENV_OVERWRITE */ |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 415 | |
| 416 | if (*++nxt == '\0') { |
| 417 | *env = '\0'; |
| 418 | } else { |
| 419 | for (;;) { |
| 420 | *env = *nxt++; |
| 421 | if ((*env == '\0') && (*nxt == '\0')) |
| 422 | break; |
| 423 | ++env; |
| 424 | } |
| 425 | } |
| 426 | *++env = '\0'; |
| 427 | } |
| 428 | |
| 429 | /* Delete only ? */ |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 430 | if (!value || !strlen(value)) |
| 431 | return 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * Append new definition at the end |
| 435 | */ |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 436 | for (env = environment.data; *env || *(env + 1); ++env); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 437 | if (env > environment.data) |
| 438 | ++env; |
| 439 | /* |
| 440 | * Overflow when: |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 441 | * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 442 | */ |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 443 | len = strlen (name) + 2; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 444 | /* add '=' for first arg, ' ' for all others */ |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 445 | len += strlen(value) + 1; |
| 446 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 447 | if (len > (&environment.data[ENV_SIZE] - env)) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 448 | fprintf (stderr, |
| 449 | "Error: environment overflow, \"%s\" deleted\n", |
| 450 | name); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 451 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 452 | } |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 453 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 454 | while ((*env = *name++) != '\0') |
| 455 | env++; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 456 | *env = '='; |
| 457 | while ((*++env = *value++) != '\0') |
| 458 | ; |
| 459 | |
| 460 | /* end is marked with double '\0' */ |
| 461 | *++env = '\0'; |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * Deletes or sets environment variables. Returns -1 and sets errno error codes: |
| 468 | * 0 - OK |
| 469 | * EINVAL - need at least 1 argument |
| 470 | * EROFS - certain variables ("ethaddr", "serial#") cannot be |
| 471 | * modified or deleted |
| 472 | * |
| 473 | */ |
| 474 | int fw_setenv(int argc, char *argv[]) |
| 475 | { |
| 476 | int i, len; |
| 477 | char *name; |
| 478 | char *value = NULL; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 479 | |
| 480 | if (argc < 2) { |
| 481 | errno = EINVAL; |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | if (fw_env_open()) { |
| 486 | fprintf(stderr, "Error: environment not initialized\n"); |
| 487 | return -1; |
| 488 | } |
| 489 | |
| 490 | name = argv[1]; |
| 491 | |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 492 | len = 0; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 493 | for (i = 2; i < argc; ++i) { |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 494 | char *val = argv[i]; |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 495 | size_t val_len = strlen(val); |
| 496 | |
| 497 | value = realloc(value, len + val_len + 1); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 498 | if (!value) { |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 499 | fprintf(stderr, |
Luka Perkov | d598271 | 2011-09-05 23:40:13 +0200 | [diff] [blame] | 500 | "Cannot malloc %zu bytes: %s\n", |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 501 | len, strerror(errno)); |
| 502 | return -1; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 503 | } |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 504 | |
| 505 | memcpy(value + len, val, val_len); |
| 506 | len += val_len; |
| 507 | value[len++] = ' '; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 508 | } |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 509 | value[len - 1] = '\0'; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 510 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 511 | fw_env_write(name, value); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 512 | |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 513 | free(value); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 514 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 515 | return fw_env_close(); |
| 516 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 517 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 518 | /* |
| 519 | * Parse a file and configure the u-boot variables. |
| 520 | * The script file has a very simple format, as follows: |
| 521 | * |
| 522 | * Each line has a couple with name, value: |
| 523 | * <white spaces>variable_name<white spaces>variable_value |
| 524 | * |
| 525 | * Both variable_name and variable_value are interpreted as strings. |
| 526 | * Any character after <white spaces> and before ending \r\n is interpreted |
| 527 | * as variable's value (no comment allowed on these lines !) |
| 528 | * |
| 529 | * Comments are allowed if the first character in the line is # |
| 530 | * |
| 531 | * Returns -1 and sets errno error codes: |
| 532 | * 0 - OK |
| 533 | * -1 - Error |
| 534 | */ |
| 535 | int fw_parse_script(char *fname) |
| 536 | { |
| 537 | FILE *fp; |
| 538 | char dump[1024]; /* Maximum line length in the file */ |
| 539 | char *name; |
| 540 | char *val; |
| 541 | int lineno = 0; |
| 542 | int len; |
| 543 | int ret = 0; |
| 544 | |
| 545 | if (fw_env_open()) { |
| 546 | fprintf(stderr, "Error: environment not initialized\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 547 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 550 | if (strcmp(fname, "-") == 0) |
| 551 | fp = stdin; |
| 552 | else { |
| 553 | fp = fopen(fname, "r"); |
| 554 | if (fp == NULL) { |
| 555 | fprintf(stderr, "I cannot open %s for reading\n", |
| 556 | fname); |
| 557 | return -1; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | while (fgets(dump, sizeof(dump), fp)) { |
| 562 | lineno++; |
| 563 | len = strlen(dump); |
| 564 | |
| 565 | /* |
| 566 | * Read a whole line from the file. If the line is too long |
| 567 | * or is not terminated, reports an error and exit. |
| 568 | */ |
| 569 | if (dump[len - 1] != '\n') { |
| 570 | fprintf(stderr, |
| 571 | "Line %d not corrected terminated or too long\n", |
| 572 | lineno); |
| 573 | ret = -1; |
| 574 | break; |
| 575 | } |
| 576 | |
| 577 | /* Drop ending line feed / carriage return */ |
| 578 | while (len > 0 && (dump[len - 1] == '\n' || |
| 579 | dump[len - 1] == '\r')) { |
| 580 | dump[len - 1] = '\0'; |
| 581 | len--; |
| 582 | } |
| 583 | |
| 584 | /* Skip comment or empty lines */ |
| 585 | if ((len == 0) || dump[0] == '#') |
| 586 | continue; |
| 587 | |
| 588 | /* |
| 589 | * Search for variable's name, |
| 590 | * remove leading whitespaces |
| 591 | */ |
| 592 | name = fw_string_blank(dump, 1); |
| 593 | if (!name) |
| 594 | continue; |
| 595 | |
| 596 | /* The first white space is the end of variable name */ |
| 597 | val = fw_string_blank(name, 0); |
| 598 | len = strlen(name); |
| 599 | if (val) { |
| 600 | *val++ = '\0'; |
| 601 | if ((val - name) < len) |
| 602 | val = fw_string_blank(val, 1); |
| 603 | else |
| 604 | val = NULL; |
| 605 | } |
| 606 | |
| 607 | #ifdef DEBUG |
| 608 | fprintf(stderr, "Setting %s : %s\n", |
| 609 | name, val ? val : " removed"); |
| 610 | #endif |
| 611 | |
| 612 | /* |
| 613 | * If there is an error setting a variable, |
| 614 | * try to save the environment and returns an error |
| 615 | */ |
| 616 | if (fw_env_write(name, val)) { |
| 617 | fprintf(stderr, |
| 618 | "fw_env_write returns with error : %s\n", |
| 619 | strerror(errno)); |
| 620 | ret = -1; |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | } |
| 625 | |
| 626 | /* Close file if not stdin */ |
| 627 | if (strcmp(fname, "-") != 0) |
| 628 | fclose(fp); |
| 629 | |
| 630 | ret |= fw_env_close(); |
| 631 | |
| 632 | return ret; |
| 633 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 636 | /* |
| 637 | * Test for bad block on NAND, just returns 0 on NOR, on NAND: |
| 638 | * 0 - block is good |
| 639 | * > 0 - block is bad |
| 640 | * < 0 - failed to test |
| 641 | */ |
| 642 | static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 643 | { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 644 | if (mtd_type == MTD_NANDFLASH) { |
| 645 | int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 646 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 647 | if (badblock < 0) { |
| 648 | perror ("Cannot read bad block mark"); |
| 649 | return badblock; |
| 650 | } |
| 651 | |
| 652 | if (badblock) { |
| 653 | #ifdef DEBUG |
| 654 | fprintf (stderr, "Bad block at 0x%llx, " |
| 655 | "skipping\n", *blockstart); |
| 656 | #endif |
| 657 | return badblock; |
| 658 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Read data from flash at an offset into a provided buffer. On NAND it skips |
| 666 | * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from |
| 667 | * the DEVOFFSET (dev) block. On NOR the loop is only run once. |
| 668 | */ |
| 669 | static int flash_read_buf (int dev, int fd, void *buf, size_t count, |
| 670 | off_t offset, uint8_t mtd_type) |
| 671 | { |
| 672 | size_t blocklen; /* erase / write length - one block on NAND, |
| 673 | 0 on NOR */ |
| 674 | size_t processed = 0; /* progress counter */ |
| 675 | size_t readlen = count; /* current read length */ |
| 676 | off_t top_of_range; /* end of the last block we may use */ |
| 677 | off_t block_seek; /* offset inside the current block to the start |
| 678 | of the data */ |
| 679 | loff_t blockstart; /* running start of the current block - |
| 680 | MEMGETBADBLOCK needs 64 bits */ |
| 681 | int rc; |
| 682 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 683 | blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 684 | |
| 685 | /* Offset inside a block */ |
| 686 | block_seek = offset - blockstart; |
| 687 | |
| 688 | if (mtd_type == MTD_NANDFLASH) { |
| 689 | /* |
| 690 | * NAND: calculate which blocks we are reading. We have |
| 691 | * to read one block at a time to skip bad blocks. |
| 692 | */ |
| 693 | blocklen = DEVESIZE (dev); |
| 694 | |
| 695 | /* |
| 696 | * To calculate the top of the range, we have to use the |
| 697 | * global DEVOFFSET (dev), which can be different from offset |
| 698 | */ |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 699 | top_of_range = ((DEVOFFSET(dev) / blocklen) + |
| 700 | ENVSECTORS (dev)) * blocklen; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 701 | |
| 702 | /* Limit to one block for the first read */ |
| 703 | if (readlen > blocklen - block_seek) |
| 704 | readlen = blocklen - block_seek; |
| 705 | } else { |
| 706 | blocklen = 0; |
| 707 | top_of_range = offset + count; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 708 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 709 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 710 | /* This only runs once on NOR flash */ |
| 711 | while (processed < count) { |
| 712 | rc = flash_bad_block (fd, mtd_type, &blockstart); |
| 713 | if (rc < 0) /* block test failed */ |
| 714 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 715 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 716 | if (blockstart + block_seek + readlen > top_of_range) { |
| 717 | /* End of range is reached */ |
| 718 | fprintf (stderr, |
| 719 | "Too few good blocks within range\n"); |
| 720 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 723 | if (rc) { /* block is bad */ |
| 724 | blockstart += blocklen; |
| 725 | continue; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 728 | /* |
| 729 | * If a block is bad, we retry in the next block at the same |
| 730 | * offset - see common/env_nand.c::writeenv() |
| 731 | */ |
| 732 | lseek (fd, blockstart + block_seek, SEEK_SET); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 733 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 734 | rc = read (fd, buf + processed, readlen); |
| 735 | if (rc != readlen) { |
| 736 | fprintf (stderr, "Read error on %s: %s\n", |
| 737 | DEVNAME (dev), strerror (errno)); |
| 738 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 739 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 740 | #ifdef DEBUG |
| 741 | fprintf (stderr, "Read 0x%x bytes at 0x%llx\n", |
| 742 | rc, blockstart + block_seek); |
| 743 | #endif |
| 744 | processed += readlen; |
| 745 | readlen = min (blocklen, count - processed); |
| 746 | block_seek = 0; |
| 747 | blockstart += blocklen; |
| 748 | } |
| 749 | |
| 750 | return processed; |
| 751 | } |
| 752 | |
| 753 | /* |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 754 | * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of |
| 755 | * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we |
| 756 | * erase and write the whole data at once. |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 757 | */ |
| 758 | static int flash_write_buf (int dev, int fd, void *buf, size_t count, |
| 759 | off_t offset, uint8_t mtd_type) |
| 760 | { |
| 761 | void *data; |
| 762 | struct erase_info_user erase; |
| 763 | size_t blocklen; /* length of NAND block / NOR erase sector */ |
| 764 | size_t erase_len; /* whole area that can be erased - may include |
| 765 | bad blocks */ |
| 766 | size_t erasesize; /* erase / write length - one block on NAND, |
| 767 | whole area on NOR */ |
| 768 | size_t processed = 0; /* progress counter */ |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 769 | size_t write_total; /* total size to actually write - excluding |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 770 | bad blocks */ |
| 771 | off_t erase_offset; /* offset to the first erase block (aligned) |
| 772 | below offset */ |
| 773 | off_t block_seek; /* offset inside the erase block to the start |
| 774 | of the data */ |
| 775 | off_t top_of_range; /* end of the last block we may use */ |
| 776 | loff_t blockstart; /* running start of the current block - |
| 777 | MEMGETBADBLOCK needs 64 bits */ |
| 778 | int rc; |
| 779 | |
| 780 | blocklen = DEVESIZE (dev); |
| 781 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 782 | top_of_range = ((DEVOFFSET(dev) / blocklen) + |
| 783 | ENVSECTORS (dev)) * blocklen; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 784 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 785 | erase_offset = (offset / blocklen) * blocklen; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 786 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 787 | /* Maximum area we may use */ |
| 788 | erase_len = top_of_range - erase_offset; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 789 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 790 | blockstart = erase_offset; |
| 791 | /* Offset inside a block */ |
| 792 | block_seek = offset - erase_offset; |
| 793 | |
| 794 | /* |
| 795 | * Data size we actually have to write: from the start of the block |
| 796 | * to the start of the data, then count bytes of data, and to the |
| 797 | * end of the block |
| 798 | */ |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 799 | write_total = ((block_seek + count + blocklen - 1) / |
| 800 | blocklen) * blocklen; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 801 | |
| 802 | /* |
| 803 | * Support data anywhere within erase sectors: read out the complete |
| 804 | * area to be erased, replace the environment image, write the whole |
| 805 | * block back again. |
| 806 | */ |
| 807 | if (write_total > count) { |
| 808 | data = malloc (erase_len); |
| 809 | if (!data) { |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 810 | fprintf (stderr, |
Luka Perkov | d598271 | 2011-09-05 23:40:13 +0200 | [diff] [blame] | 811 | "Cannot malloc %zu bytes: %s\n", |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 812 | erase_len, strerror (errno)); |
| 813 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 814 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 815 | |
| 816 | rc = flash_read_buf (dev, fd, data, write_total, erase_offset, |
| 817 | mtd_type); |
| 818 | if (write_total != rc) |
| 819 | return -1; |
| 820 | |
| 821 | /* Overwrite the old environment */ |
| 822 | memcpy (data + block_seek, buf, count); |
| 823 | } else { |
| 824 | /* |
| 825 | * We get here, iff offset is block-aligned and count is a |
| 826 | * multiple of blocklen - see write_total calculation above |
| 827 | */ |
| 828 | data = buf; |
| 829 | } |
| 830 | |
| 831 | if (mtd_type == MTD_NANDFLASH) { |
| 832 | /* |
| 833 | * NAND: calculate which blocks we are writing. We have |
| 834 | * to write one block at a time to skip bad blocks. |
| 835 | */ |
| 836 | erasesize = blocklen; |
| 837 | } else { |
| 838 | erasesize = erase_len; |
| 839 | } |
| 840 | |
| 841 | erase.length = erasesize; |
| 842 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 843 | /* This only runs once on NOR flash and SPI-dataflash */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 844 | while (processed < write_total) { |
| 845 | rc = flash_bad_block (fd, mtd_type, &blockstart); |
| 846 | if (rc < 0) /* block test failed */ |
| 847 | return rc; |
| 848 | |
| 849 | if (blockstart + erasesize > top_of_range) { |
| 850 | fprintf (stderr, "End of range reached, aborting\n"); |
| 851 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 852 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 853 | |
| 854 | if (rc) { /* block is bad */ |
| 855 | blockstart += blocklen; |
| 856 | continue; |
| 857 | } |
| 858 | |
| 859 | erase.start = blockstart; |
| 860 | ioctl (fd, MEMUNLOCK, &erase); |
| 861 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 862 | /* Dataflash does not need an explicit erase cycle */ |
| 863 | if (mtd_type != MTD_DATAFLASH) |
| 864 | if (ioctl (fd, MEMERASE, &erase) != 0) { |
| 865 | fprintf (stderr, "MTD erase error on %s: %s\n", |
| 866 | DEVNAME (dev), |
| 867 | strerror (errno)); |
| 868 | return -1; |
| 869 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 870 | |
| 871 | if (lseek (fd, blockstart, SEEK_SET) == -1) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 872 | fprintf (stderr, |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 873 | "Seek error on %s: %s\n", |
| 874 | DEVNAME (dev), strerror (errno)); |
| 875 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 876 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 877 | |
| 878 | #ifdef DEBUG |
| 879 | printf ("Write 0x%x bytes at 0x%llx\n", erasesize, blockstart); |
| 880 | #endif |
| 881 | if (write (fd, data + processed, erasesize) != erasesize) { |
| 882 | fprintf (stderr, "Write error on %s: %s\n", |
| 883 | DEVNAME (dev), strerror (errno)); |
| 884 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 885 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 886 | |
| 887 | ioctl (fd, MEMLOCK, &erase); |
| 888 | |
| 889 | processed += blocklen; |
| 890 | block_seek = 0; |
| 891 | blockstart += blocklen; |
| 892 | } |
| 893 | |
| 894 | if (write_total > count) |
| 895 | free (data); |
| 896 | |
| 897 | return processed; |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Set obsolete flag at offset - NOR flash only |
| 902 | */ |
| 903 | static int flash_flag_obsolete (int dev, int fd, off_t offset) |
| 904 | { |
| 905 | int rc; |
Detlev Zundel | 2111d3e | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 906 | struct erase_info_user erase; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 907 | |
Detlev Zundel | 2111d3e | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 908 | erase.start = DEVOFFSET (dev); |
| 909 | erase.length = DEVESIZE (dev); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 910 | /* This relies on the fact, that obsolete_flag == 0 */ |
| 911 | rc = lseek (fd, offset, SEEK_SET); |
| 912 | if (rc < 0) { |
| 913 | fprintf (stderr, "Cannot seek to set the flag on %s \n", |
| 914 | DEVNAME (dev)); |
| 915 | return rc; |
| 916 | } |
Detlev Zundel | 2111d3e | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 917 | ioctl (fd, MEMUNLOCK, &erase); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 918 | rc = write (fd, &obsolete_flag, sizeof (obsolete_flag)); |
Detlev Zundel | 2111d3e | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 919 | ioctl (fd, MEMLOCK, &erase); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 920 | if (rc < 0) |
| 921 | perror ("Could not set obsolete flag"); |
| 922 | |
| 923 | return rc; |
| 924 | } |
| 925 | |
| 926 | static int flash_write (int fd_current, int fd_target, int dev_target) |
| 927 | { |
| 928 | int rc; |
| 929 | |
| 930 | switch (environment.flag_scheme) { |
| 931 | case FLAG_NONE: |
| 932 | break; |
| 933 | case FLAG_INCREMENTAL: |
| 934 | (*environment.flags)++; |
| 935 | break; |
| 936 | case FLAG_BOOLEAN: |
| 937 | *environment.flags = active_flag; |
| 938 | break; |
| 939 | default: |
| 940 | fprintf (stderr, "Unimplemented flash scheme %u \n", |
| 941 | environment.flag_scheme); |
| 942 | return -1; |
| 943 | } |
| 944 | |
| 945 | #ifdef DEBUG |
| 946 | printf ("Writing new environment at 0x%lx on %s\n", |
| 947 | DEVOFFSET (dev_target), DEVNAME (dev_target)); |
| 948 | #endif |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 949 | rc = flash_write_buf(dev_target, fd_target, environment.image, |
| 950 | CUR_ENVSIZE, DEVOFFSET(dev_target), |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 951 | DEVTYPE(dev_target)); |
| 952 | if (rc < 0) |
| 953 | return rc; |
| 954 | |
| 955 | if (environment.flag_scheme == FLAG_BOOLEAN) { |
| 956 | /* Have to set obsolete flag */ |
| 957 | off_t offset = DEVOFFSET (dev_current) + |
| 958 | offsetof (struct env_image_redundant, flags); |
| 959 | #ifdef DEBUG |
| 960 | printf ("Setting obsolete flag in environment at 0x%lx on %s\n", |
| 961 | DEVOFFSET (dev_current), DEVNAME (dev_current)); |
| 962 | #endif |
| 963 | flash_flag_obsolete (dev_current, fd_current, offset); |
| 964 | } |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | static int flash_read (int fd) |
| 970 | { |
| 971 | struct mtd_info_user mtdinfo; |
| 972 | int rc; |
| 973 | |
| 974 | rc = ioctl (fd, MEMGETINFO, &mtdinfo); |
| 975 | if (rc < 0) { |
| 976 | perror ("Cannot get MTD information"); |
| 977 | return -1; |
| 978 | } |
| 979 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 980 | if (mtdinfo.type != MTD_NORFLASH && |
| 981 | mtdinfo.type != MTD_NANDFLASH && |
| 982 | mtdinfo.type != MTD_DATAFLASH) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 983 | fprintf (stderr, "Unsupported flash type %u\n", mtdinfo.type); |
| 984 | return -1; |
| 985 | } |
| 986 | |
| 987 | DEVTYPE(dev_current) = mtdinfo.type; |
| 988 | |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 989 | rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE, |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 990 | DEVOFFSET (dev_current), mtdinfo.type); |
| 991 | |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 992 | return (rc != CUR_ENVSIZE) ? -1 : 0; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | static int flash_io (int mode) |
| 996 | { |
| 997 | int fd_current, fd_target, rc, dev_target; |
| 998 | |
| 999 | /* dev_current: fd_current, erase_current */ |
| 1000 | fd_current = open (DEVNAME (dev_current), mode); |
| 1001 | if (fd_current < 0) { |
| 1002 | fprintf (stderr, |
| 1003 | "Can't open %s: %s\n", |
| 1004 | DEVNAME (dev_current), strerror (errno)); |
| 1005 | return -1; |
| 1006 | } |
| 1007 | |
| 1008 | if (mode == O_RDWR) { |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1009 | if (HaveRedundEnv) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1010 | /* switch to next partition for writing */ |
| 1011 | dev_target = !dev_current; |
| 1012 | /* dev_target: fd_target, erase_target */ |
| 1013 | fd_target = open (DEVNAME (dev_target), mode); |
| 1014 | if (fd_target < 0) { |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1015 | fprintf (stderr, |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1016 | "Can't open %s: %s\n", |
| 1017 | DEVNAME (dev_target), |
| 1018 | strerror (errno)); |
| 1019 | rc = -1; |
| 1020 | goto exit; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1021 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1022 | } else { |
| 1023 | dev_target = dev_current; |
| 1024 | fd_target = fd_current; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1025 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1026 | |
| 1027 | rc = flash_write (fd_current, fd_target, dev_target); |
| 1028 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1029 | if (HaveRedundEnv) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1030 | if (close (fd_target)) { |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1031 | fprintf (stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1032 | "I/O error on %s: %s\n", |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1033 | DEVNAME (dev_target), |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1034 | strerror (errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1035 | rc = -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1036 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1037 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1038 | } else { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1039 | rc = flash_read (fd_current); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1042 | exit: |
| 1043 | if (close (fd_current)) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1044 | fprintf (stderr, |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1045 | "I/O error on %s: %s\n", |
| 1046 | DEVNAME (dev_current), strerror (errno)); |
| 1047 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1050 | return rc; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 1055 | * s2 is a 'name=value' pair. |
| 1056 | * If the names match, return the value of s2, else NULL. |
| 1057 | */ |
| 1058 | |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 1059 | static char *envmatch (char * s1, char * s2) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1060 | { |
Joe Hershberger | dd000dd | 2012-10-03 09:38:50 +0000 | [diff] [blame] | 1061 | if (s1 == NULL || s2 == NULL) |
| 1062 | return NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1063 | |
| 1064 | while (*s1 == *s2++) |
| 1065 | if (*s1++ == '=') |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1066 | return s2; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1067 | if (*s1 == '\0' && *(s2 - 1) == '=') |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1068 | return s2; |
| 1069 | return NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * Prevent confusion if running from erased flash memory |
| 1074 | */ |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 1075 | int fw_env_open(void) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1076 | { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1077 | int crc0, crc0_ok; |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1078 | unsigned char flag0; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1079 | void *addr0; |
| 1080 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1081 | int crc1, crc1_ok; |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1082 | unsigned char flag1; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1083 | void *addr1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1084 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1085 | struct env_image_single *single; |
| 1086 | struct env_image_redundant *redundant; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1087 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1088 | if (parse_config ()) /* should fill envdevices */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1089 | return -1; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1090 | |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1091 | addr0 = calloc(1, CUR_ENVSIZE); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1092 | if (addr0 == NULL) { |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1093 | fprintf(stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1094 | "Not enough memory for environment (%ld bytes)\n", |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1095 | CUR_ENVSIZE); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1096 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1097 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1098 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1099 | /* read environment from FLASH to local buffer */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1100 | environment.image = addr0; |
| 1101 | |
| 1102 | if (HaveRedundEnv) { |
| 1103 | redundant = addr0; |
| 1104 | environment.crc = &redundant->crc; |
| 1105 | environment.flags = &redundant->flags; |
| 1106 | environment.data = redundant->data; |
| 1107 | } else { |
| 1108 | single = addr0; |
| 1109 | environment.crc = &single->crc; |
| 1110 | environment.flags = NULL; |
| 1111 | environment.data = single->data; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1112 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1113 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1114 | dev_current = 0; |
| 1115 | if (flash_io (O_RDONLY)) |
| 1116 | return -1; |
| 1117 | |
| 1118 | crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE); |
| 1119 | crc0_ok = (crc0 == *environment.crc); |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1120 | if (!HaveRedundEnv) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1121 | if (!crc0_ok) { |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1122 | fprintf (stderr, |
| 1123 | "Warning: Bad CRC, using default environment\n"); |
Wolfgang Denk | c5203f9 | 2006-03-12 23:27:46 +0100 | [diff] [blame] | 1124 | memcpy(environment.data, default_environment, sizeof default_environment); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1125 | } |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1126 | } else { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1127 | flag0 = *environment.flags; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1128 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1129 | dev_current = 1; |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1130 | addr1 = calloc(1, CUR_ENVSIZE); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1131 | if (addr1 == NULL) { |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1132 | fprintf(stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1133 | "Not enough memory for environment (%ld bytes)\n", |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1134 | CUR_ENVSIZE); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1135 | return -1; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1136 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1137 | redundant = addr1; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1138 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1139 | /* |
| 1140 | * have to set environment.image for flash_read(), careful - |
| 1141 | * other pointers in environment still point inside addr0 |
| 1142 | */ |
| 1143 | environment.image = addr1; |
| 1144 | if (flash_io (O_RDONLY)) |
| 1145 | return -1; |
| 1146 | |
| 1147 | /* Check flag scheme compatibility */ |
| 1148 | if (DEVTYPE(dev_current) == MTD_NORFLASH && |
| 1149 | DEVTYPE(!dev_current) == MTD_NORFLASH) { |
| 1150 | environment.flag_scheme = FLAG_BOOLEAN; |
| 1151 | } else if (DEVTYPE(dev_current) == MTD_NANDFLASH && |
| 1152 | DEVTYPE(!dev_current) == MTD_NANDFLASH) { |
| 1153 | environment.flag_scheme = FLAG_INCREMENTAL; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1154 | } else if (DEVTYPE(dev_current) == MTD_DATAFLASH && |
| 1155 | DEVTYPE(!dev_current) == MTD_DATAFLASH) { |
| 1156 | environment.flag_scheme = FLAG_BOOLEAN; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1157 | } else { |
| 1158 | fprintf (stderr, "Incompatible flash types!\n"); |
| 1159 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1160 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1161 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1162 | crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE); |
| 1163 | crc1_ok = (crc1 == redundant->crc); |
| 1164 | flag1 = redundant->flags; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1165 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1166 | if (crc0_ok && !crc1_ok) { |
| 1167 | dev_current = 0; |
| 1168 | } else if (!crc0_ok && crc1_ok) { |
| 1169 | dev_current = 1; |
| 1170 | } else if (!crc0_ok && !crc1_ok) { |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1171 | fprintf (stderr, |
| 1172 | "Warning: Bad CRC, using default environment\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1173 | memcpy (environment.data, default_environment, |
| 1174 | sizeof default_environment); |
| 1175 | dev_current = 0; |
| 1176 | } else { |
| 1177 | switch (environment.flag_scheme) { |
| 1178 | case FLAG_BOOLEAN: |
| 1179 | if (flag0 == active_flag && |
| 1180 | flag1 == obsolete_flag) { |
| 1181 | dev_current = 0; |
| 1182 | } else if (flag0 == obsolete_flag && |
| 1183 | flag1 == active_flag) { |
| 1184 | dev_current = 1; |
| 1185 | } else if (flag0 == flag1) { |
| 1186 | dev_current = 0; |
| 1187 | } else if (flag0 == 0xFF) { |
| 1188 | dev_current = 0; |
| 1189 | } else if (flag1 == 0xFF) { |
| 1190 | dev_current = 1; |
| 1191 | } else { |
| 1192 | dev_current = 0; |
| 1193 | } |
| 1194 | break; |
| 1195 | case FLAG_INCREMENTAL: |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1196 | if (flag0 == 255 && flag1 == 0) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1197 | dev_current = 1; |
| 1198 | else if ((flag1 == 255 && flag0 == 0) || |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1199 | flag0 >= flag1) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1200 | dev_current = 0; |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1201 | else /* flag1 > flag0 */ |
| 1202 | dev_current = 1; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1203 | break; |
| 1204 | default: |
| 1205 | fprintf (stderr, "Unknown flag scheme %u \n", |
| 1206 | environment.flag_scheme); |
| 1207 | return -1; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | * If we are reading, we don't need the flag and the CRC any |
| 1213 | * more, if we are writing, we will re-calculate CRC and update |
| 1214 | * flags before writing out |
| 1215 | */ |
| 1216 | if (dev_current) { |
| 1217 | environment.image = addr1; |
| 1218 | environment.crc = &redundant->crc; |
| 1219 | environment.flags = &redundant->flags; |
| 1220 | environment.data = redundant->data; |
| 1221 | free (addr0); |
| 1222 | } else { |
| 1223 | environment.image = addr0; |
| 1224 | /* Other pointers are already set */ |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1225 | free (addr1); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1226 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1227 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1228 | return 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1232 | static int parse_config () |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1233 | { |
| 1234 | struct stat st; |
| 1235 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1236 | #if defined(CONFIG_FILE) |
| 1237 | /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1238 | if (get_config (CONFIG_FILE)) { |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1239 | fprintf (stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1240 | "Cannot parse config file: %s\n", strerror (errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1241 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1242 | } |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1243 | #else |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1244 | strcpy (DEVNAME (0), DEVICE1_NAME); |
| 1245 | DEVOFFSET (0) = DEVICE1_OFFSET; |
| 1246 | ENVSIZE (0) = ENV1_SIZE; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1247 | /* Default values are: erase-size=env-size, #sectors=1 */ |
| 1248 | DEVESIZE (0) = ENVSIZE (0); |
| 1249 | ENVSECTORS (0) = 1; |
| 1250 | #ifdef DEVICE1_ESIZE |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1251 | DEVESIZE (0) = DEVICE1_ESIZE; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1252 | #endif |
| 1253 | #ifdef DEVICE1_ENVSECTORS |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1254 | ENVSECTORS (0) = DEVICE1_ENVSECTORS; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1255 | #endif |
| 1256 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1257 | #ifdef HAVE_REDUND |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1258 | strcpy (DEVNAME (1), DEVICE2_NAME); |
| 1259 | DEVOFFSET (1) = DEVICE2_OFFSET; |
| 1260 | ENVSIZE (1) = ENV2_SIZE; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1261 | /* Default values are: erase-size=env-size, #sectors=1 */ |
| 1262 | DEVESIZE (1) = ENVSIZE (1); |
| 1263 | ENVSECTORS (1) = 1; |
| 1264 | #ifdef DEVICE2_ESIZE |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1265 | DEVESIZE (1) = DEVICE2_ESIZE; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1266 | #endif |
| 1267 | #ifdef DEVICE2_ENVSECTORS |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1268 | ENVSECTORS (1) = DEVICE2_ENVSECTORS; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1269 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1270 | HaveRedundEnv = 1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1271 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1272 | #endif |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1273 | if (stat (DEVNAME (0), &st)) { |
| 1274 | fprintf (stderr, |
| 1275 | "Cannot access MTD device %s: %s\n", |
| 1276 | DEVNAME (0), strerror (errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1277 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1278 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1279 | |
| 1280 | if (HaveRedundEnv && stat (DEVNAME (1), &st)) { |
| 1281 | fprintf (stderr, |
| 1282 | "Cannot access MTD device %s: %s\n", |
Wolfgang Denk | 8e6213c | 2005-09-25 01:20:38 +0200 | [diff] [blame] | 1283 | DEVNAME (1), strerror (errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1284 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1285 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1286 | return 0; |
| 1287 | } |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1288 | |
| 1289 | #if defined(CONFIG_FILE) |
| 1290 | static int get_config (char *fname) |
| 1291 | { |
| 1292 | FILE *fp; |
| 1293 | int i = 0; |
| 1294 | int rc; |
| 1295 | char dump[128]; |
| 1296 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1297 | fp = fopen (fname, "r"); |
| 1298 | if (fp == NULL) |
| 1299 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1300 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1301 | while (i < 2 && fgets (dump, sizeof (dump), fp)) { |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1302 | /* Skip incomplete conversions and comment strings */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1303 | if (dump[0] == '#') |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1304 | continue; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1305 | |
| 1306 | rc = sscanf (dump, "%s %lx %lx %lx %lx", |
| 1307 | DEVNAME (i), |
| 1308 | &DEVOFFSET (i), |
| 1309 | &ENVSIZE (i), |
| 1310 | &DEVESIZE (i), |
| 1311 | &ENVSECTORS (i)); |
| 1312 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1313 | if (rc < 3) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1314 | continue; |
| 1315 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1316 | if (rc < 4) |
| 1317 | /* Assume the erase size is the same as the env-size */ |
| 1318 | DEVESIZE(i) = ENVSIZE(i); |
| 1319 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1320 | if (rc < 5) |
| 1321 | /* Default - 1 sector */ |
| 1322 | ENVSECTORS (i) = 1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1323 | |
| 1324 | i++; |
| 1325 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1326 | fclose (fp); |
| 1327 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1328 | HaveRedundEnv = i - 1; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1329 | if (!i) { /* No valid entries found */ |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1330 | errno = EINVAL; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1331 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1332 | } else |
| 1333 | return 0; |
| 1334 | } |
| 1335 | #endif |