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