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