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