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