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