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