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