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