Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 2 | /* |
Detlev Zundel | 2111d3e | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 3 | * (C) Copyright 2000-2010 |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 6 | * (C) Copyright 2008 |
| 7 | * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de. |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Jörg Krause | 983487f | 2015-04-22 21:36:22 +0200 | [diff] [blame] | 10 | #define _GNU_SOURCE |
| 11 | |
Peter Robinson | 656ee04 | 2015-12-09 07:15:33 +0000 | [diff] [blame] | 12 | #include <compiler.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 13 | #include <env.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> |
Alex Kiernan | 7150aaa | 2018-03-09 12:13:02 +0000 | [diff] [blame] | 17 | #include <libgen.h> |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 18 | #include <linux/fs.h> |
Joe Hershberger | e79a91f | 2012-10-12 10:23:37 +0000 | [diff] [blame] | 19 | #include <linux/stringify.h> |
Andreas Fenkart | d0e12cb | 2016-03-11 09:39:35 +0100 | [diff] [blame] | 20 | #include <ctype.h> |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stddef.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/stat.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 28 | #include <u-boot/crc.h> |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 29 | #include <unistd.h> |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 30 | #include <dirent.h> |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 31 | |
| 32 | #ifdef MTD_OLD |
Wolfgang Denk | 4cd94a7 | 2008-09-02 21:17:36 +0200 | [diff] [blame] | 33 | # include <stdint.h> |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 34 | # include <linux/mtd/mtd.h> |
| 35 | #else |
Wolfgang Denk | c7d16fd | 2008-01-08 22:58:27 +0100 | [diff] [blame] | 36 | # define __user /* nothing */ |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 37 | # include <mtd/mtd-user.h> |
| 38 | #endif |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 39 | |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 40 | #include <mtd/ubi-user.h> |
| 41 | |
Stefano Babic | 831c4d5 | 2017-04-05 18:08:01 +0200 | [diff] [blame] | 42 | #include "fw_env_private.h" |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 43 | #include "fw_env.h" |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 44 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 45 | struct env_opts default_opts = { |
| 46 | #ifdef CONFIG_FILE |
| 47 | .config_file = CONFIG_FILE |
| 48 | #endif |
| 49 | }; |
| 50 | |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 51 | #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
| 52 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 53 | #define min(x, y) ({ \ |
| 54 | typeof(x) _min1 = (x); \ |
| 55 | typeof(y) _min2 = (y); \ |
| 56 | (void) (&_min1 == &_min2); \ |
| 57 | _min1 < _min2 ? _min1 : _min2; }) |
| 58 | |
| 59 | struct envdev_s { |
Tom Rini | 313e912 | 2014-03-28 12:03:33 -0400 | [diff] [blame] | 60 | const char *devname; /* Device name */ |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 61 | long long devoff; /* Device offset */ |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 62 | ulong env_size; /* environment size */ |
| 63 | ulong erase_size; /* device erase size */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 64 | ulong env_sectors; /* number of environment sectors */ |
| 65 | uint8_t mtd_type; /* type of the MTD device */ |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 66 | int is_ubi; /* set if we use UBI volume */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 67 | }; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 68 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 69 | static struct envdev_s envdevices[2] = { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 70 | { |
| 71 | .mtd_type = MTD_ABSENT, |
| 72 | }, { |
| 73 | .mtd_type = MTD_ABSENT, |
| 74 | }, |
| 75 | }; |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 76 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 77 | static int dev_current; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 78 | |
| 79 | #define DEVNAME(i) envdevices[(i)].devname |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 80 | #define DEVOFFSET(i) envdevices[(i)].devoff |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 81 | #define ENVSIZE(i) envdevices[(i)].env_size |
| 82 | #define DEVESIZE(i) envdevices[(i)].erase_size |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 83 | #define ENVSECTORS(i) envdevices[(i)].env_sectors |
| 84 | #define DEVTYPE(i) envdevices[(i)].mtd_type |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 85 | #define IS_UBI(i) envdevices[(i)].is_ubi |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 86 | |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 87 | #define CUR_ENVSIZE ENVSIZE(dev_current) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 88 | |
Andreas Fenkart | b8bf404 | 2016-04-19 22:43:42 +0200 | [diff] [blame] | 89 | static unsigned long usable_envsize; |
| 90 | #define ENV_SIZE usable_envsize |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 91 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 92 | struct env_image_single { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 93 | uint32_t crc; /* CRC32 over data bytes */ |
| 94 | char data[]; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 95 | }; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 96 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 97 | struct env_image_redundant { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 98 | uint32_t crc; /* CRC32 over data bytes */ |
| 99 | unsigned char flags; /* active or obsolete */ |
| 100 | char data[]; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | enum flag_scheme { |
| 104 | FLAG_NONE, |
| 105 | FLAG_BOOLEAN, |
| 106 | FLAG_INCREMENTAL, |
| 107 | }; |
| 108 | |
| 109 | struct environment { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 110 | void *image; |
| 111 | uint32_t *crc; |
| 112 | unsigned char *flags; |
| 113 | char *data; |
| 114 | enum flag_scheme flag_scheme; |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 115 | int dirty; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | static struct environment environment = { |
| 119 | .flag_scheme = FLAG_NONE, |
| 120 | }; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 121 | |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 122 | static int have_redund_env; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 123 | |
Joe Hershberger | 8132114 | 2012-10-12 08:48:51 +0000 | [diff] [blame] | 124 | #define DEFAULT_ENV_INSTANCE_STATIC |
| 125 | #include <env_default.h> |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 126 | |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 127 | #define UBI_DEV_START "/dev/ubi" |
| 128 | #define UBI_SYSFS "/sys/class/ubi" |
| 129 | #define UBI_VOL_NAME_PATT "ubi%d_%d" |
| 130 | |
| 131 | static int is_ubi_devname(const char *devname) |
| 132 | { |
| 133 | return !strncmp(devname, UBI_DEV_START, sizeof(UBI_DEV_START) - 1); |
| 134 | } |
| 135 | |
| 136 | static int ubi_check_volume_sysfs_name(const char *volume_sysfs_name, |
| 137 | const char *volname) |
| 138 | { |
| 139 | char path[256]; |
| 140 | FILE *file; |
| 141 | char *name; |
| 142 | int ret; |
| 143 | |
| 144 | strcpy(path, UBI_SYSFS "/"); |
| 145 | strcat(path, volume_sysfs_name); |
| 146 | strcat(path, "/name"); |
| 147 | |
| 148 | file = fopen(path, "r"); |
| 149 | if (!file) |
| 150 | return -1; |
| 151 | |
| 152 | ret = fscanf(file, "%ms", &name); |
| 153 | fclose(file); |
| 154 | if (ret <= 0 || !name) { |
| 155 | fprintf(stderr, |
| 156 | "Failed to read from file %s, ret = %d, name = %s\n", |
| 157 | path, ret, name); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | if (!strcmp(name, volname)) { |
| 162 | free(name); |
| 163 | return 0; |
| 164 | } |
| 165 | free(name); |
| 166 | |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | static int ubi_get_volnum_by_name(int devnum, const char *volname) |
| 171 | { |
| 172 | DIR *sysfs_ubi; |
| 173 | struct dirent *dirent; |
| 174 | int ret; |
| 175 | int tmp_devnum; |
| 176 | int volnum; |
| 177 | |
| 178 | sysfs_ubi = opendir(UBI_SYSFS); |
| 179 | if (!sysfs_ubi) |
| 180 | return -1; |
| 181 | |
| 182 | #ifdef DEBUG |
| 183 | fprintf(stderr, "Looking for volume name \"%s\"\n", volname); |
| 184 | #endif |
| 185 | |
| 186 | while (1) { |
| 187 | dirent = readdir(sysfs_ubi); |
| 188 | if (!dirent) |
| 189 | return -1; |
| 190 | |
| 191 | ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT, |
| 192 | &tmp_devnum, &volnum); |
| 193 | if (ret == 2 && devnum == tmp_devnum) { |
| 194 | if (ubi_check_volume_sysfs_name(dirent->d_name, |
| 195 | volname) == 0) |
| 196 | return volnum; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | static int ubi_get_devnum_by_devname(const char *devname) |
| 204 | { |
| 205 | int devnum; |
| 206 | int ret; |
| 207 | |
| 208 | ret = sscanf(devname + sizeof(UBI_DEV_START) - 1, "%d", &devnum); |
| 209 | if (ret != 1) |
| 210 | return -1; |
| 211 | |
| 212 | return devnum; |
| 213 | } |
| 214 | |
| 215 | static const char *ubi_get_volume_devname(const char *devname, |
| 216 | const char *volname) |
| 217 | { |
| 218 | char *volume_devname; |
| 219 | int volnum; |
| 220 | int devnum; |
| 221 | int ret; |
| 222 | |
| 223 | devnum = ubi_get_devnum_by_devname(devname); |
| 224 | if (devnum < 0) |
| 225 | return NULL; |
| 226 | |
| 227 | volnum = ubi_get_volnum_by_name(devnum, volname); |
| 228 | if (volnum < 0) |
| 229 | return NULL; |
| 230 | |
| 231 | ret = asprintf(&volume_devname, "%s_%d", devname, volnum); |
| 232 | if (ret < 0) |
| 233 | return NULL; |
| 234 | |
| 235 | #ifdef DEBUG |
| 236 | fprintf(stderr, "Found ubi volume \"%s:%s\" -> %s\n", |
| 237 | devname, volname, volume_devname); |
| 238 | #endif |
| 239 | |
| 240 | return volume_devname; |
| 241 | } |
| 242 | |
| 243 | static void ubi_check_dev(unsigned int dev_id) |
| 244 | { |
| 245 | char *devname = (char *)DEVNAME(dev_id); |
| 246 | char *pname; |
| 247 | const char *volname = NULL; |
| 248 | const char *volume_devname; |
| 249 | |
| 250 | if (!is_ubi_devname(DEVNAME(dev_id))) |
| 251 | return; |
| 252 | |
| 253 | IS_UBI(dev_id) = 1; |
| 254 | |
| 255 | for (pname = devname; *pname != '\0'; pname++) { |
| 256 | if (*pname == ':') { |
| 257 | *pname = '\0'; |
| 258 | volname = pname + 1; |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (volname) { |
| 264 | /* Let's find real volume device name */ |
| 265 | volume_devname = ubi_get_volume_devname(devname, volname); |
| 266 | if (!volume_devname) { |
| 267 | fprintf(stderr, "Didn't found ubi volume \"%s\"\n", |
| 268 | volname); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | free(devname); |
| 273 | DEVNAME(dev_id) = volume_devname; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | static int ubi_update_start(int fd, int64_t bytes) |
| 278 | { |
| 279 | if (ioctl(fd, UBI_IOCVOLUP, &bytes)) |
| 280 | return -1; |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static int ubi_read(int fd, void *buf, size_t count) |
| 285 | { |
| 286 | ssize_t ret; |
| 287 | |
| 288 | while (count > 0) { |
| 289 | ret = read(fd, buf, count); |
| 290 | if (ret > 0) { |
| 291 | count -= ret; |
| 292 | buf += ret; |
| 293 | |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | if (ret == 0) { |
| 298 | /* |
| 299 | * Happens in case of too short volume data size. If we |
| 300 | * return error status we will fail it will be treated |
| 301 | * as UBI device error. |
| 302 | * |
| 303 | * Leave catching this error to CRC check. |
| 304 | */ |
| 305 | fprintf(stderr, "Warning: end of data on ubi volume\n"); |
| 306 | return 0; |
| 307 | } else if (errno == EBADF) { |
| 308 | /* |
| 309 | * Happens in case of corrupted volume. The same as |
| 310 | * above, we cannot return error now, as we will still |
| 311 | * be able to successfully write environment later. |
| 312 | */ |
| 313 | fprintf(stderr, "Warning: corrupted volume?\n"); |
| 314 | return 0; |
| 315 | } else if (errno == EINTR) { |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | fprintf(stderr, "Cannot read %u bytes from ubi volume, %s\n", |
| 320 | (unsigned int)count, strerror(errno)); |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static int ubi_write(int fd, const void *buf, size_t count) |
| 328 | { |
| 329 | ssize_t ret; |
| 330 | |
| 331 | while (count > 0) { |
| 332 | ret = write(fd, buf, count); |
| 333 | if (ret <= 0) { |
| 334 | if (ret < 0 && errno == EINTR) |
| 335 | continue; |
| 336 | |
| 337 | fprintf(stderr, "Cannot write %u bytes to ubi volume\n", |
| 338 | (unsigned int)count); |
| 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | count -= ret; |
| 343 | buf += ret; |
| 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 349 | static int flash_io(int mode); |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 350 | static int parse_config(struct env_opts *opts); |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 351 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 352 | #if defined(CONFIG_FILE) |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 353 | static int get_config(char *); |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 354 | #endif |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 355 | |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 356 | static char *skip_chars(char *s) |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 357 | { |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 358 | for (; *s != '\0'; s++) { |
Stefan Agner | 8783324 | 2018-03-01 14:06:32 +0100 | [diff] [blame] | 359 | if (isblank(*s) || *s == '=') |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 360 | return s; |
| 361 | } |
| 362 | return NULL; |
| 363 | } |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 364 | |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 365 | static char *skip_blanks(char *s) |
| 366 | { |
| 367 | for (; *s != '\0'; s++) { |
| 368 | if (!isblank(*s)) |
Andreas Fenkart | e42c68e | 2016-03-11 09:39:36 +0100 | [diff] [blame] | 369 | return s; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 370 | } |
Andreas Fenkart | e42c68e | 2016-03-11 09:39:36 +0100 | [diff] [blame] | 371 | return NULL; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 372 | } |
| 373 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 374 | /* |
Andreas Fenkart | 3ff1074 | 2016-07-16 17:06:14 +0200 | [diff] [blame] | 375 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 376 | * s2 is a 'name=value' pair. |
| 377 | * If the names match, return the value of s2, else NULL. |
| 378 | */ |
| 379 | static char *envmatch(char *s1, char *s2) |
| 380 | { |
| 381 | if (s1 == NULL || s2 == NULL) |
| 382 | return NULL; |
| 383 | |
| 384 | while (*s1 == *s2++) |
| 385 | if (*s1++ == '=') |
| 386 | return s2; |
| 387 | if (*s1 == '\0' && *(s2 - 1) == '=') |
| 388 | return s2; |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | /** |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 393 | * Search the environment for a variable. |
| 394 | * Return the value, if found, or NULL, if not found. |
| 395 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 396 | char *fw_getenv(char *name) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 397 | { |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 398 | char *env, *nxt; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 399 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 400 | for (env = environment.data; *env; env = nxt + 1) { |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 401 | char *val; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 402 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 403 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 404 | if (nxt >= &environment.data[ENV_SIZE]) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 405 | fprintf(stderr, "## Error: " |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 406 | "environment not terminated\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 407 | return NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 410 | val = envmatch(name, env); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 411 | if (!val) |
| 412 | continue; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 413 | return val; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 414 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 415 | return NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 419 | * Search the default environment for a variable. |
| 420 | * Return the value, if found, or NULL, if not found. |
| 421 | */ |
| 422 | char *fw_getdefenv(char *name) |
| 423 | { |
| 424 | char *env, *nxt; |
| 425 | |
| 426 | for (env = default_environment; *env; env = nxt + 1) { |
| 427 | char *val; |
| 428 | |
| 429 | for (nxt = env; *nxt; ++nxt) { |
| 430 | if (nxt >= &default_environment[ENV_SIZE]) { |
| 431 | fprintf(stderr, "## Error: " |
| 432 | "default environment not terminated\n"); |
| 433 | return NULL; |
| 434 | } |
| 435 | } |
| 436 | val = envmatch(name, env); |
| 437 | if (!val) |
| 438 | continue; |
| 439 | return val; |
| 440 | } |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | /* |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 445 | * Print the current definition of one, or more, or all |
| 446 | * environment variables |
| 447 | */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 448 | 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] | 449 | { |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 450 | int i, rc = 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 451 | |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 452 | if (value_only && argc != 1) { |
| 453 | fprintf(stderr, |
Alex Kiernan | 688cee9 | 2018-02-11 17:16:46 +0000 | [diff] [blame] | 454 | "## Error: `-n'/`--noheader' option requires exactly one argument\n"); |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 455 | return -1; |
| 456 | } |
| 457 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 458 | if (!opts) |
| 459 | opts = &default_opts; |
| 460 | |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 461 | if (fw_env_open(opts)) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 462 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 463 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 464 | if (argc == 0) { /* Print all env variables */ |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 465 | char *env, *nxt; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 466 | for (env = environment.data; *env; env = nxt + 1) { |
| 467 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 468 | if (nxt >= &environment.data[ENV_SIZE]) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 469 | fprintf(stderr, "## Error: " |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 470 | "environment not terminated\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 471 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 475 | printf("%s\n", env); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 476 | } |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 477 | fw_env_close(opts); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 478 | return 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 481 | 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] | 482 | char *name = argv[i]; |
| 483 | char *val = NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 484 | |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 485 | val = fw_getenv(name); |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 486 | if (!val) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 487 | fprintf(stderr, "## Error: \"%s\" not defined\n", name); |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 488 | rc = -1; |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 489 | continue; |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 490 | } |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 491 | |
| 492 | if (value_only) { |
| 493 | puts(val); |
| 494 | break; |
| 495 | } |
| 496 | |
| 497 | printf("%s=%s\n", name, val); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 498 | } |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 499 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 500 | fw_env_close(opts); |
| 501 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 502 | return rc; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 505 | int fw_env_flush(struct env_opts *opts) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 506 | { |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 507 | if (!opts) |
| 508 | opts = &default_opts; |
| 509 | |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 510 | if (!environment.dirty) |
| 511 | return 0; |
| 512 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 513 | /* |
| 514 | * Update CRC |
| 515 | */ |
| 516 | *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 517 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 518 | /* write environment back to flash */ |
| 519 | if (flash_io(O_RDWR)) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 520 | fprintf(stderr, "Error: can't write fw_env to flash\n"); |
| 521 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 524 | return 0; |
| 525 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 526 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 527 | /* |
| 528 | * Set/Clear a single variable in the environment. |
| 529 | * This is called in sequence to update the environment |
| 530 | * in RAM without updating the copy in flash after each set |
| 531 | */ |
| 532 | int fw_env_write(char *name, char *value) |
| 533 | { |
| 534 | int len; |
| 535 | char *env, *nxt; |
| 536 | char *oldval = NULL; |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 537 | int deleting, creating, overwriting; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 538 | |
| 539 | /* |
| 540 | * search if variable with this name already exists |
| 541 | */ |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 542 | for (nxt = env = environment.data; *env; env = nxt + 1) { |
| 543 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 544 | if (nxt >= &environment.data[ENV_SIZE]) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 545 | fprintf(stderr, "## Error: " |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 546 | "environment not terminated\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 547 | errno = EINVAL; |
| 548 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 551 | oldval = envmatch(name, env); |
| 552 | if (oldval) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 553 | break; |
| 554 | } |
| 555 | |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 556 | deleting = (oldval && !(value && strlen(value))); |
| 557 | creating = (!oldval && (value && strlen(value))); |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 558 | overwriting = (oldval && (value && strlen(value) && |
| 559 | strcmp(oldval, value))); |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 560 | |
| 561 | /* check for permission */ |
| 562 | if (deleting) { |
| 563 | if (env_flags_validate_varaccess(name, |
| 564 | ENV_FLAGS_VARACCESS_PREVENT_DELETE)) { |
| 565 | printf("Can't delete \"%s\"\n", name); |
| 566 | errno = EROFS; |
| 567 | return -1; |
| 568 | } |
| 569 | } else if (overwriting) { |
| 570 | if (env_flags_validate_varaccess(name, |
| 571 | ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) { |
| 572 | printf("Can't overwrite \"%s\"\n", name); |
| 573 | errno = EROFS; |
| 574 | return -1; |
| 575 | } else if (env_flags_validate_varaccess(name, |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 576 | ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) { |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 577 | const char *defval = fw_getdefenv(name); |
| 578 | |
| 579 | if (defval == NULL) |
| 580 | defval = ""; |
| 581 | if (strcmp(oldval, defval) |
| 582 | != 0) { |
| 583 | printf("Can't overwrite \"%s\"\n", name); |
| 584 | errno = EROFS; |
| 585 | return -1; |
| 586 | } |
| 587 | } |
| 588 | } else if (creating) { |
| 589 | if (env_flags_validate_varaccess(name, |
| 590 | ENV_FLAGS_VARACCESS_PREVENT_CREATE)) { |
| 591 | printf("Can't create \"%s\"\n", name); |
| 592 | errno = EROFS; |
| 593 | return -1; |
| 594 | } |
| 595 | } else |
| 596 | /* Nothing to do */ |
| 597 | return 0; |
| 598 | |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 599 | environment.dirty = 1; |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 600 | if (deleting || overwriting) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 601 | if (*++nxt == '\0') { |
| 602 | *env = '\0'; |
| 603 | } else { |
| 604 | for (;;) { |
| 605 | *env = *nxt++; |
| 606 | if ((*env == '\0') && (*nxt == '\0')) |
| 607 | break; |
| 608 | ++env; |
| 609 | } |
| 610 | } |
| 611 | *++env = '\0'; |
| 612 | } |
| 613 | |
| 614 | /* Delete only ? */ |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 615 | if (!value || !strlen(value)) |
| 616 | return 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 617 | |
| 618 | /* |
| 619 | * Append new definition at the end |
| 620 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 621 | for (env = environment.data; *env || *(env + 1); ++env) |
| 622 | ; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 623 | if (env > environment.data) |
| 624 | ++env; |
| 625 | /* |
| 626 | * Overflow when: |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 627 | * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 628 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 629 | len = strlen(name) + 2; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 630 | /* add '=' for first arg, ' ' for all others */ |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 631 | len += strlen(value) + 1; |
| 632 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 633 | if (len > (&environment.data[ENV_SIZE] - env)) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 634 | fprintf(stderr, |
| 635 | "Error: environment overflow, \"%s\" deleted\n", name); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 636 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 637 | } |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 638 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 639 | while ((*env = *name++) != '\0') |
| 640 | env++; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 641 | *env = '='; |
| 642 | while ((*++env = *value++) != '\0') |
| 643 | ; |
| 644 | |
| 645 | /* end is marked with double '\0' */ |
| 646 | *++env = '\0'; |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Deletes or sets environment variables. Returns -1 and sets errno error codes: |
| 653 | * 0 - OK |
| 654 | * EINVAL - need at least 1 argument |
| 655 | * EROFS - certain variables ("ethaddr", "serial#") cannot be |
| 656 | * modified or deleted |
| 657 | * |
| 658 | */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 659 | int fw_env_set(int argc, char *argv[], struct env_opts *opts) |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 660 | { |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 661 | int i; |
Mike Frysinger | 178ec77 | 2012-11-10 19:47:45 +0000 | [diff] [blame] | 662 | size_t len; |
Andreas Fenkart | 4e336ee | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 663 | char *name, **valv; |
xypron.glpk@gmx.de | 2a3325c | 2017-04-15 13:05:40 +0200 | [diff] [blame] | 664 | char *oldval; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 665 | char *value = NULL; |
Andreas Fenkart | 4e336ee | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 666 | int valc; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 667 | int ret; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 668 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 669 | if (!opts) |
| 670 | opts = &default_opts; |
| 671 | |
Andreas Fenkart | 14908ef | 2015-12-09 13:13:25 +0100 | [diff] [blame] | 672 | if (argc < 1) { |
| 673 | fprintf(stderr, "## Error: variable name missing\n"); |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 674 | errno = EINVAL; |
| 675 | return -1; |
| 676 | } |
| 677 | |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 678 | if (fw_env_open(opts)) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 679 | fprintf(stderr, "Error: environment not initialized\n"); |
| 680 | return -1; |
| 681 | } |
| 682 | |
Andreas Fenkart | 14908ef | 2015-12-09 13:13:25 +0100 | [diff] [blame] | 683 | name = argv[0]; |
| 684 | valv = argv + 1; |
| 685 | valc = argc - 1; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 686 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 687 | if (env_flags_validate_env_set_params(name, valv, valc) < 0) { |
| 688 | fw_env_close(opts); |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 689 | return -1; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 690 | } |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 691 | |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 692 | len = 0; |
Andreas Fenkart | 4e336ee | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 693 | for (i = 0; i < valc; ++i) { |
| 694 | char *val = valv[i]; |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 695 | size_t val_len = strlen(val); |
| 696 | |
Joe Hershberger | 03bf21d | 2012-10-15 15:29:24 +0000 | [diff] [blame] | 697 | if (value) |
| 698 | value[len - 1] = ' '; |
xypron.glpk@gmx.de | 2a3325c | 2017-04-15 13:05:40 +0200 | [diff] [blame] | 699 | oldval = value; |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 700 | value = realloc(value, len + val_len + 1); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 701 | if (!value) { |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 702 | fprintf(stderr, |
Luka Perkov | d598271 | 2011-09-05 23:40:13 +0200 | [diff] [blame] | 703 | "Cannot malloc %zu bytes: %s\n", |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 704 | len, strerror(errno)); |
xypron.glpk@gmx.de | 2a3325c | 2017-04-15 13:05:40 +0200 | [diff] [blame] | 705 | free(oldval); |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 706 | return -1; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 707 | } |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 708 | |
| 709 | memcpy(value + len, val, val_len); |
| 710 | len += val_len; |
Joe Hershberger | 03bf21d | 2012-10-15 15:29:24 +0000 | [diff] [blame] | 711 | value[len++] = '\0'; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 714 | fw_env_write(name, value); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 715 | |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 716 | free(value); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 717 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 718 | ret = fw_env_flush(opts); |
| 719 | fw_env_close(opts); |
| 720 | |
| 721 | return ret; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 722 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 723 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 724 | /* |
| 725 | * Parse a file and configure the u-boot variables. |
| 726 | * The script file has a very simple format, as follows: |
| 727 | * |
| 728 | * Each line has a couple with name, value: |
| 729 | * <white spaces>variable_name<white spaces>variable_value |
| 730 | * |
| 731 | * Both variable_name and variable_value are interpreted as strings. |
| 732 | * Any character after <white spaces> and before ending \r\n is interpreted |
| 733 | * as variable's value (no comment allowed on these lines !) |
| 734 | * |
| 735 | * Comments are allowed if the first character in the line is # |
| 736 | * |
| 737 | * Returns -1 and sets errno error codes: |
| 738 | * 0 - OK |
| 739 | * -1 - Error |
| 740 | */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 741 | int fw_parse_script(char *fname, struct env_opts *opts) |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 742 | { |
| 743 | FILE *fp; |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 744 | char *line = NULL; |
| 745 | size_t linesize = 0; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 746 | char *name; |
| 747 | char *val; |
| 748 | int lineno = 0; |
| 749 | int len; |
| 750 | int ret = 0; |
| 751 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 752 | if (!opts) |
| 753 | opts = &default_opts; |
| 754 | |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 755 | if (fw_env_open(opts)) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 756 | fprintf(stderr, "Error: environment not initialized\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 757 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 760 | if (strcmp(fname, "-") == 0) |
| 761 | fp = stdin; |
| 762 | else { |
| 763 | fp = fopen(fname, "r"); |
| 764 | if (fp == NULL) { |
| 765 | fprintf(stderr, "I cannot open %s for reading\n", |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 766 | fname); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 767 | return -1; |
| 768 | } |
| 769 | } |
| 770 | |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 771 | while ((len = getline(&line, &linesize, fp)) != -1) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 772 | lineno++; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 773 | |
| 774 | /* |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 775 | * Read a whole line from the file. If the line is not |
| 776 | * terminated, reports an error and exit. |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 777 | */ |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 778 | if (line[len - 1] != '\n') { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 779 | fprintf(stderr, |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 780 | "Line %d not correctly terminated\n", |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 781 | lineno); |
| 782 | ret = -1; |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | /* Drop ending line feed / carriage return */ |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 787 | line[--len] = '\0'; |
| 788 | if (len && line[len - 1] == '\r') |
| 789 | line[--len] = '\0'; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 790 | |
| 791 | /* Skip comment or empty lines */ |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 792 | if (len == 0 || line[0] == '#') |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 793 | continue; |
| 794 | |
| 795 | /* |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 796 | * Search for variable's name remove leading whitespaces |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 797 | */ |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 798 | name = skip_blanks(line); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 799 | if (!name) |
| 800 | continue; |
| 801 | |
| 802 | /* The first white space is the end of variable name */ |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 803 | val = skip_chars(name); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 804 | len = strlen(name); |
| 805 | if (val) { |
| 806 | *val++ = '\0'; |
| 807 | if ((val - name) < len) |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 808 | val = skip_blanks(val); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 809 | else |
| 810 | val = NULL; |
| 811 | } |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 812 | #ifdef DEBUG |
| 813 | fprintf(stderr, "Setting %s : %s\n", |
| 814 | name, val ? val : " removed"); |
| 815 | #endif |
| 816 | |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 817 | if (env_flags_validate_type(name, val) < 0) { |
| 818 | ret = -1; |
| 819 | break; |
| 820 | } |
| 821 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 822 | /* |
| 823 | * If there is an error setting a variable, |
| 824 | * try to save the environment and returns an error |
| 825 | */ |
| 826 | if (fw_env_write(name, val)) { |
| 827 | fprintf(stderr, |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 828 | "fw_env_write returns with error : %s\n", |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 829 | strerror(errno)); |
| 830 | ret = -1; |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | } |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 835 | free(line); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 836 | |
| 837 | /* Close file if not stdin */ |
| 838 | if (strcmp(fname, "-") != 0) |
| 839 | fclose(fp); |
| 840 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 841 | ret |= fw_env_flush(opts); |
| 842 | |
| 843 | fw_env_close(opts); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 844 | |
| 845 | return ret; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 848 | /** |
Shyam Saini | efe757c | 2018-06-07 19:47:19 +0530 | [diff] [blame] | 849 | * environment_end() - compute offset of first byte right after environment |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 850 | * @dev - index of enviroment buffer |
| 851 | * Return: |
Shyam Saini | efe757c | 2018-06-07 19:47:19 +0530 | [diff] [blame] | 852 | * device offset of first byte right after environment |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 853 | */ |
| 854 | off_t environment_end(int dev) |
| 855 | { |
| 856 | /* environment is block aligned */ |
| 857 | return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev); |
| 858 | } |
| 859 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 860 | /* |
| 861 | * Test for bad block on NAND, just returns 0 on NOR, on NAND: |
| 862 | * 0 - block is good |
| 863 | * > 0 - block is bad |
| 864 | * < 0 - failed to test |
| 865 | */ |
Andreas Fenkart | ae3b320 | 2016-08-29 23:16:58 +0200 | [diff] [blame] | 866 | 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] | 867 | { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 868 | if (mtd_type == MTD_NANDFLASH) { |
Andreas Fenkart | ae3b320 | 2016-08-29 23:16:58 +0200 | [diff] [blame] | 869 | int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 870 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 871 | if (badblock < 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 872 | perror("Cannot read bad block mark"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 873 | return badblock; |
| 874 | } |
| 875 | |
| 876 | if (badblock) { |
| 877 | #ifdef DEBUG |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 878 | fprintf(stderr, "Bad block at 0x%llx, skipping\n", |
Andreas Fenkart | ae3b320 | 2016-08-29 23:16:58 +0200 | [diff] [blame] | 879 | (unsigned long long)blockstart); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 880 | #endif |
| 881 | return badblock; |
| 882 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Read data from flash at an offset into a provided buffer. On NAND it skips |
| 890 | * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from |
| 891 | * the DEVOFFSET (dev) block. On NOR the loop is only run once. |
| 892 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 893 | static int flash_read_buf(int dev, int fd, void *buf, size_t count, |
| 894 | off_t offset) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 895 | { |
| 896 | size_t blocklen; /* erase / write length - one block on NAND, |
| 897 | 0 on NOR */ |
| 898 | size_t processed = 0; /* progress counter */ |
| 899 | size_t readlen = count; /* current read length */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 900 | off_t block_seek; /* offset inside the current block to the start |
| 901 | of the data */ |
| 902 | loff_t blockstart; /* running start of the current block - |
| 903 | MEMGETBADBLOCK needs 64 bits */ |
| 904 | int rc; |
| 905 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 906 | blockstart = (offset / DEVESIZE(dev)) * DEVESIZE(dev); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 907 | |
| 908 | /* Offset inside a block */ |
| 909 | block_seek = offset - blockstart; |
| 910 | |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 911 | if (DEVTYPE(dev) == MTD_NANDFLASH) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 912 | /* |
| 913 | * NAND: calculate which blocks we are reading. We have |
| 914 | * to read one block at a time to skip bad blocks. |
| 915 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 916 | blocklen = DEVESIZE(dev); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 917 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 918 | /* Limit to one block for the first read */ |
| 919 | if (readlen > blocklen - block_seek) |
| 920 | readlen = blocklen - block_seek; |
| 921 | } else { |
| 922 | blocklen = 0; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 923 | } |
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 | /* This only runs once on NOR flash */ |
| 926 | while (processed < count) { |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 927 | rc = flash_bad_block(fd, DEVTYPE(dev), blockstart); |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 928 | if (rc < 0) /* block test failed */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 929 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 930 | |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 931 | if (blockstart + block_seek + readlen > environment_end(dev)) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 932 | /* End of range is reached */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 933 | fprintf(stderr, "Too few good blocks within range\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 934 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 937 | if (rc) { /* block is bad */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 938 | blockstart += blocklen; |
| 939 | continue; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 942 | /* |
| 943 | * If a block is bad, we retry in the next block at the same |
Tom Rini | 282f270 | 2017-09-05 14:01:29 -0400 | [diff] [blame] | 944 | * offset - see env/nand.c::writeenv() |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 945 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 946 | lseek(fd, blockstart + block_seek, SEEK_SET); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 947 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 948 | rc = read(fd, buf + processed, readlen); |
Harald Seiler | 79685b2 | 2020-05-28 17:54:45 +0200 | [diff] [blame] | 949 | if (rc == -1) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 950 | fprintf(stderr, "Read error on %s: %s\n", |
| 951 | DEVNAME(dev), strerror(errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 952 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 953 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 954 | #ifdef DEBUG |
Joe Hershberger | a8dd2c6 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 955 | fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n", |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 956 | rc, (unsigned long long)blockstart + block_seek, |
Marcin Niestroj | 2089073 | 2016-05-06 14:58:29 +0200 | [diff] [blame] | 957 | DEVNAME(dev)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 958 | #endif |
Thibault Ferrante | 8586cda | 2021-08-24 17:29:50 +0200 | [diff] [blame] | 959 | processed += rc; |
| 960 | if (rc != readlen) { |
| 961 | fprintf(stderr, |
| 962 | "Warning on %s: Attempted to read %zd bytes but got %d\n", |
| 963 | DEVNAME(dev), readlen, rc); |
| 964 | readlen -= rc; |
| 965 | block_seek += rc; |
| 966 | } else { |
| 967 | blockstart += blocklen; |
| 968 | readlen = min(blocklen, count - processed); |
| 969 | block_seek = 0; |
| 970 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | return processed; |
| 974 | } |
| 975 | |
| 976 | /* |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 977 | * Write count bytes from begin of environment, but stay within |
| 978 | * ENVSECTORS(dev) sectors of |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 979 | * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we |
| 980 | * erase and write the whole data at once. |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 981 | */ |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 982 | static int flash_write_buf(int dev, int fd, void *buf, size_t count) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 983 | { |
| 984 | void *data; |
| 985 | struct erase_info_user erase; |
| 986 | size_t blocklen; /* length of NAND block / NOR erase sector */ |
| 987 | size_t erase_len; /* whole area that can be erased - may include |
| 988 | bad blocks */ |
| 989 | size_t erasesize; /* erase / write length - one block on NAND, |
| 990 | whole area on NOR */ |
| 991 | size_t processed = 0; /* progress counter */ |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 992 | size_t write_total; /* total size to actually write - excluding |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 993 | bad blocks */ |
| 994 | off_t erase_offset; /* offset to the first erase block (aligned) |
| 995 | below offset */ |
| 996 | off_t block_seek; /* offset inside the erase block to the start |
| 997 | of the data */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 998 | loff_t blockstart; /* running start of the current block - |
| 999 | MEMGETBADBLOCK needs 64 bits */ |
Simon Glass | f820563 | 2020-08-01 10:30:39 -0600 | [diff] [blame] | 1000 | int was_locked = 0; /* flash lock flag */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1001 | int rc; |
| 1002 | |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1003 | /* |
| 1004 | * For mtd devices only offset and size of the environment do matter |
| 1005 | */ |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1006 | if (DEVTYPE(dev) == MTD_ABSENT) { |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1007 | blocklen = count; |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1008 | erase_len = blocklen; |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 1009 | blockstart = DEVOFFSET(dev); |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1010 | block_seek = 0; |
| 1011 | write_total = blocklen; |
| 1012 | } else { |
| 1013 | blocklen = DEVESIZE(dev); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1014 | |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 1015 | erase_offset = DEVOFFSET(dev); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1016 | |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1017 | /* Maximum area we may use */ |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 1018 | erase_len = environment_end(dev) - erase_offset; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1019 | |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1020 | blockstart = erase_offset; |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 1021 | |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1022 | /* Offset inside a block */ |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 1023 | block_seek = DEVOFFSET(dev) - erase_offset; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1024 | |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1025 | /* |
| 1026 | * Data size we actually write: from the start of the block |
| 1027 | * to the start of the data, then count bytes of data, and |
| 1028 | * to the end of the block |
| 1029 | */ |
| 1030 | write_total = ((block_seek + count + blocklen - 1) / |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1031 | blocklen) * blocklen; |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1032 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1033 | |
| 1034 | /* |
| 1035 | * Support data anywhere within erase sectors: read out the complete |
| 1036 | * area to be erased, replace the environment image, write the whole |
| 1037 | * block back again. |
| 1038 | */ |
| 1039 | if (write_total > count) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1040 | data = malloc(erase_len); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1041 | if (!data) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1042 | fprintf(stderr, |
| 1043 | "Cannot malloc %zu bytes: %s\n", |
| 1044 | erase_len, strerror(errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1045 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1046 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1047 | |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1048 | rc = flash_read_buf(dev, fd, data, write_total, erase_offset); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1049 | if (write_total != rc) |
| 1050 | return -1; |
| 1051 | |
Joe Hershberger | a8dd2c6 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 1052 | #ifdef DEBUG |
| 1053 | fprintf(stderr, "Preserving data "); |
| 1054 | if (block_seek != 0) |
| 1055 | fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1); |
| 1056 | if (block_seek + count != write_total) { |
| 1057 | if (block_seek != 0) |
| 1058 | fprintf(stderr, " and "); |
Marcin Niestroj | 2089073 | 2016-05-06 14:58:29 +0200 | [diff] [blame] | 1059 | fprintf(stderr, "0x%lx - 0x%lx", |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1060 | (unsigned long)block_seek + count, |
| 1061 | (unsigned long)write_total - 1); |
Joe Hershberger | a8dd2c6 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 1062 | } |
| 1063 | fprintf(stderr, "\n"); |
| 1064 | #endif |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1065 | /* Overwrite the old environment */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1066 | memcpy(data + block_seek, buf, count); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1067 | } else { |
| 1068 | /* |
| 1069 | * We get here, iff offset is block-aligned and count is a |
| 1070 | * multiple of blocklen - see write_total calculation above |
| 1071 | */ |
| 1072 | data = buf; |
| 1073 | } |
| 1074 | |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1075 | if (DEVTYPE(dev) == MTD_NANDFLASH) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1076 | /* |
| 1077 | * NAND: calculate which blocks we are writing. We have |
| 1078 | * to write one block at a time to skip bad blocks. |
| 1079 | */ |
| 1080 | erasesize = blocklen; |
| 1081 | } else { |
| 1082 | erasesize = erase_len; |
| 1083 | } |
| 1084 | |
| 1085 | erase.length = erasesize; |
Ivan Mikhaylov | 20c21bf | 2020-07-10 19:54:18 +0300 | [diff] [blame] | 1086 | if (DEVTYPE(dev) != MTD_ABSENT) { |
| 1087 | was_locked = ioctl(fd, MEMISLOCKED, &erase); |
| 1088 | /* treat any errors as unlocked flash */ |
| 1089 | if (was_locked < 0) |
| 1090 | was_locked = 0; |
| 1091 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1092 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1093 | /* This only runs once on NOR flash and SPI-dataflash */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1094 | while (processed < write_total) { |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1095 | rc = flash_bad_block(fd, DEVTYPE(dev), blockstart); |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1096 | if (rc < 0) /* block test failed */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1097 | return rc; |
| 1098 | |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 1099 | if (blockstart + erasesize > environment_end(dev)) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1100 | fprintf(stderr, "End of range reached, aborting\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1101 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1102 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1103 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1104 | if (rc) { /* block is bad */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1105 | blockstart += blocklen; |
| 1106 | continue; |
| 1107 | } |
| 1108 | |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1109 | if (DEVTYPE(dev) != MTD_ABSENT) { |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1110 | erase.start = blockstart; |
Ivan Mikhaylov | 20c21bf | 2020-07-10 19:54:18 +0300 | [diff] [blame] | 1111 | if (was_locked) |
| 1112 | ioctl(fd, MEMUNLOCK, &erase); |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1113 | /* These do not need an explicit erase cycle */ |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1114 | if (DEVTYPE(dev) != MTD_DATAFLASH) |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1115 | if (ioctl(fd, MEMERASE, &erase) != 0) { |
| 1116 | fprintf(stderr, |
| 1117 | "MTD erase error on %s: %s\n", |
| 1118 | DEVNAME(dev), strerror(errno)); |
| 1119 | return -1; |
| 1120 | } |
| 1121 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1122 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1123 | if (lseek(fd, blockstart, SEEK_SET) == -1) { |
| 1124 | fprintf(stderr, |
| 1125 | "Seek error on %s: %s\n", |
| 1126 | DEVNAME(dev), strerror(errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1127 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1128 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1129 | #ifdef DEBUG |
Marcin Niestroj | 2089073 | 2016-05-06 14:58:29 +0200 | [diff] [blame] | 1130 | fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n", |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1131 | (unsigned long long)erasesize, |
| 1132 | (unsigned long long)blockstart); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1133 | #endif |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1134 | if (write(fd, data + processed, erasesize) != erasesize) { |
| 1135 | fprintf(stderr, "Write error on %s: %s\n", |
| 1136 | DEVNAME(dev), strerror(errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1137 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1138 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1139 | |
Ivan Mikhaylov | 20c21bf | 2020-07-10 19:54:18 +0300 | [diff] [blame] | 1140 | if (DEVTYPE(dev) != MTD_ABSENT) { |
| 1141 | if (was_locked) |
| 1142 | ioctl(fd, MEMLOCK, &erase); |
| 1143 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1144 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1145 | processed += erasesize; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1146 | block_seek = 0; |
Dustin Byford | 0983cad | 2014-03-06 20:48:23 -0800 | [diff] [blame] | 1147 | blockstart += erasesize; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | if (write_total > count) |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1151 | free(data); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1152 | |
| 1153 | return processed; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * Set obsolete flag at offset - NOR flash only |
| 1158 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1159 | static int flash_flag_obsolete(int dev, int fd, off_t offset) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1160 | { |
| 1161 | int rc; |
Detlev Zundel | 2111d3e | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 1162 | struct erase_info_user erase; |
Pierre-Jean Texier | d7f9545 | 2019-08-26 13:06:16 +0200 | [diff] [blame] | 1163 | char tmp = ENV_REDUND_OBSOLETE; |
Ivan Mikhaylov | 20c21bf | 2020-07-10 19:54:18 +0300 | [diff] [blame] | 1164 | int was_locked; /* flash lock flag */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1165 | |
Ivan Mikhaylov | 20c21bf | 2020-07-10 19:54:18 +0300 | [diff] [blame] | 1166 | was_locked = ioctl(fd, MEMISLOCKED, &erase); |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1167 | erase.start = DEVOFFSET(dev); |
| 1168 | erase.length = DEVESIZE(dev); |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 1169 | /* This relies on the fact, that ENV_REDUND_OBSOLETE == 0 */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1170 | rc = lseek(fd, offset, SEEK_SET); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1171 | if (rc < 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1172 | fprintf(stderr, "Cannot seek to set the flag on %s\n", |
| 1173 | DEVNAME(dev)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1174 | return rc; |
| 1175 | } |
Ivan Mikhaylov | 20c21bf | 2020-07-10 19:54:18 +0300 | [diff] [blame] | 1176 | if (was_locked) |
| 1177 | ioctl(fd, MEMUNLOCK, &erase); |
Pierre-Jean Texier | d7f9545 | 2019-08-26 13:06:16 +0200 | [diff] [blame] | 1178 | rc = write(fd, &tmp, sizeof(tmp)); |
Ivan Mikhaylov | 20c21bf | 2020-07-10 19:54:18 +0300 | [diff] [blame] | 1179 | if (was_locked) |
| 1180 | ioctl(fd, MEMLOCK, &erase); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1181 | if (rc < 0) |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1182 | perror("Could not set obsolete flag"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1183 | |
| 1184 | return rc; |
| 1185 | } |
| 1186 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1187 | static int flash_write(int fd_current, int fd_target, int dev_target) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1188 | { |
| 1189 | int rc; |
| 1190 | |
| 1191 | switch (environment.flag_scheme) { |
| 1192 | case FLAG_NONE: |
| 1193 | break; |
| 1194 | case FLAG_INCREMENTAL: |
| 1195 | (*environment.flags)++; |
| 1196 | break; |
| 1197 | case FLAG_BOOLEAN: |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 1198 | *environment.flags = ENV_REDUND_ACTIVE; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1199 | break; |
| 1200 | default: |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1201 | fprintf(stderr, "Unimplemented flash scheme %u\n", |
| 1202 | environment.flag_scheme); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1203 | return -1; |
| 1204 | } |
| 1205 | |
| 1206 | #ifdef DEBUG |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1207 | fprintf(stderr, "Writing new environment at 0x%llx on %s\n", |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1208 | DEVOFFSET(dev_target), DEVNAME(dev_target)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1209 | #endif |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 1210 | |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1211 | if (IS_UBI(dev_target)) { |
| 1212 | if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0) |
Martin Hundebøll | 710e9c1 | 2020-11-30 12:11:32 +0100 | [diff] [blame] | 1213 | return -1; |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1214 | return ubi_write(fd_target, environment.image, CUR_ENVSIZE); |
| 1215 | } |
| 1216 | |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1217 | rc = flash_write_buf(dev_target, fd_target, environment.image, |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 1218 | CUR_ENVSIZE); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1219 | if (rc < 0) |
| 1220 | return rc; |
| 1221 | |
| 1222 | if (environment.flag_scheme == FLAG_BOOLEAN) { |
| 1223 | /* Have to set obsolete flag */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1224 | off_t offset = DEVOFFSET(dev_current) + |
| 1225 | offsetof(struct env_image_redundant, flags); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1226 | #ifdef DEBUG |
Joe Hershberger | a8dd2c6 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 1227 | fprintf(stderr, |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1228 | "Setting obsolete flag in environment at 0x%llx on %s\n", |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1229 | DEVOFFSET(dev_current), DEVNAME(dev_current)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1230 | #endif |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1231 | flash_flag_obsolete(dev_current, fd_current, offset); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1237 | static int flash_read(int fd) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1238 | { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1239 | int rc; |
| 1240 | |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1241 | if (IS_UBI(dev_current)) { |
| 1242 | DEVTYPE(dev_current) = MTD_ABSENT; |
| 1243 | |
| 1244 | return ubi_read(fd, environment.image, CUR_ENVSIZE); |
| 1245 | } |
| 1246 | |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1247 | rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE, |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1248 | DEVOFFSET(dev_current)); |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 1249 | if (rc != CUR_ENVSIZE) |
| 1250 | return -1; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1251 | |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 1252 | return 0; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1253 | } |
| 1254 | |
Alex Kiernan | 7150aaa | 2018-03-09 12:13:02 +0000 | [diff] [blame] | 1255 | static int flash_open_tempfile(const char **dname, const char **target_temp) |
| 1256 | { |
| 1257 | char *dup_name = strdup(DEVNAME(dev_current)); |
| 1258 | char *temp_name = NULL; |
| 1259 | int rc = -1; |
| 1260 | |
| 1261 | if (!dup_name) |
| 1262 | return -1; |
| 1263 | |
| 1264 | *dname = dirname(dup_name); |
| 1265 | if (!*dname) |
| 1266 | goto err; |
| 1267 | |
| 1268 | rc = asprintf(&temp_name, "%s/XXXXXX", *dname); |
| 1269 | if (rc == -1) |
| 1270 | goto err; |
| 1271 | |
| 1272 | rc = mkstemp(temp_name); |
| 1273 | if (rc == -1) { |
| 1274 | /* fall back to in place write */ |
| 1275 | fprintf(stderr, |
| 1276 | "Can't create %s: %s\n", temp_name, strerror(errno)); |
| 1277 | free(temp_name); |
| 1278 | } else { |
| 1279 | *target_temp = temp_name; |
| 1280 | /* deliberately leak dup_name as dname /might/ point into |
| 1281 | * it and we need it for our caller |
| 1282 | */ |
| 1283 | dup_name = NULL; |
| 1284 | } |
| 1285 | |
| 1286 | err: |
| 1287 | if (dup_name) |
| 1288 | free(dup_name); |
| 1289 | |
| 1290 | return rc; |
| 1291 | } |
| 1292 | |
Alex Kiernan | be1e788 | 2018-03-09 12:13:01 +0000 | [diff] [blame] | 1293 | static int flash_io_write(int fd_current) |
| 1294 | { |
Alex Kiernan | 7150aaa | 2018-03-09 12:13:02 +0000 | [diff] [blame] | 1295 | int fd_target = -1, rc, dev_target; |
| 1296 | const char *dname, *target_temp = NULL; |
Alex Kiernan | be1e788 | 2018-03-09 12:13:01 +0000 | [diff] [blame] | 1297 | |
| 1298 | if (have_redund_env) { |
| 1299 | /* switch to next partition for writing */ |
| 1300 | dev_target = !dev_current; |
| 1301 | /* dev_target: fd_target, erase_target */ |
| 1302 | fd_target = open(DEVNAME(dev_target), O_RDWR); |
| 1303 | if (fd_target < 0) { |
| 1304 | fprintf(stderr, |
| 1305 | "Can't open %s: %s\n", |
| 1306 | DEVNAME(dev_target), strerror(errno)); |
| 1307 | rc = -1; |
| 1308 | goto exit; |
| 1309 | } |
| 1310 | } else { |
Alex Kiernan | 7150aaa | 2018-03-09 12:13:02 +0000 | [diff] [blame] | 1311 | struct stat sb; |
| 1312 | |
| 1313 | if (fstat(fd_current, &sb) == 0 && S_ISREG(sb.st_mode)) { |
| 1314 | /* if any part of flash_open_tempfile() fails we fall |
| 1315 | * back to in-place writes |
| 1316 | */ |
| 1317 | fd_target = flash_open_tempfile(&dname, &target_temp); |
| 1318 | } |
Alex Kiernan | be1e788 | 2018-03-09 12:13:01 +0000 | [diff] [blame] | 1319 | dev_target = dev_current; |
Alex Kiernan | 7150aaa | 2018-03-09 12:13:02 +0000 | [diff] [blame] | 1320 | if (fd_target == -1) |
| 1321 | fd_target = fd_current; |
Alex Kiernan | be1e788 | 2018-03-09 12:13:01 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | rc = flash_write(fd_current, fd_target, dev_target); |
| 1325 | |
| 1326 | if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) { |
| 1327 | fprintf(stderr, |
| 1328 | "fsync failed on %s: %s\n", |
| 1329 | DEVNAME(dev_current), strerror(errno)); |
| 1330 | } |
| 1331 | |
Alex Kiernan | 7150aaa | 2018-03-09 12:13:02 +0000 | [diff] [blame] | 1332 | if (fd_current != fd_target) { |
Alex Kiernan | be1e788 | 2018-03-09 12:13:01 +0000 | [diff] [blame] | 1333 | if (fsync(fd_target) && |
| 1334 | !(errno == EINVAL || errno == EROFS)) { |
| 1335 | fprintf(stderr, |
| 1336 | "fsync failed on %s: %s\n", |
| 1337 | DEVNAME(dev_current), strerror(errno)); |
| 1338 | } |
| 1339 | |
| 1340 | if (close(fd_target)) { |
| 1341 | fprintf(stderr, |
| 1342 | "I/O error on %s: %s\n", |
| 1343 | DEVNAME(dev_target), strerror(errno)); |
| 1344 | rc = -1; |
| 1345 | } |
Alex Kiernan | 7150aaa | 2018-03-09 12:13:02 +0000 | [diff] [blame] | 1346 | |
Alex Kiernan | ad4f3d0 | 2019-06-12 15:27:55 +0100 | [diff] [blame] | 1347 | if (rc >= 0 && target_temp) { |
Alex Kiernan | 7150aaa | 2018-03-09 12:13:02 +0000 | [diff] [blame] | 1348 | int dir_fd; |
| 1349 | |
| 1350 | dir_fd = open(dname, O_DIRECTORY | O_RDONLY); |
| 1351 | if (dir_fd == -1) |
| 1352 | fprintf(stderr, |
| 1353 | "Can't open %s: %s\n", |
| 1354 | dname, strerror(errno)); |
| 1355 | |
| 1356 | if (rename(target_temp, DEVNAME(dev_target))) { |
| 1357 | fprintf(stderr, |
| 1358 | "rename failed %s => %s: %s\n", |
| 1359 | target_temp, DEVNAME(dev_target), |
| 1360 | strerror(errno)); |
| 1361 | rc = -1; |
| 1362 | } |
| 1363 | |
| 1364 | if (dir_fd != -1 && fsync(dir_fd)) |
| 1365 | fprintf(stderr, |
| 1366 | "fsync failed on %s: %s\n", |
| 1367 | dname, strerror(errno)); |
| 1368 | |
| 1369 | if (dir_fd != -1 && close(dir_fd)) |
| 1370 | fprintf(stderr, |
| 1371 | "I/O error on %s: %s\n", |
| 1372 | dname, strerror(errno)); |
| 1373 | } |
Alex Kiernan | be1e788 | 2018-03-09 12:13:01 +0000 | [diff] [blame] | 1374 | } |
| 1375 | exit: |
| 1376 | return rc; |
| 1377 | } |
| 1378 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1379 | static int flash_io(int mode) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1380 | { |
Alex Kiernan | be1e788 | 2018-03-09 12:13:01 +0000 | [diff] [blame] | 1381 | int fd_current, rc; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1382 | |
| 1383 | /* dev_current: fd_current, erase_current */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1384 | fd_current = open(DEVNAME(dev_current), mode); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1385 | if (fd_current < 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1386 | fprintf(stderr, |
| 1387 | "Can't open %s: %s\n", |
| 1388 | DEVNAME(dev_current), strerror(errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1389 | return -1; |
| 1390 | } |
| 1391 | |
| 1392 | if (mode == O_RDWR) { |
Alex Kiernan | be1e788 | 2018-03-09 12:13:01 +0000 | [diff] [blame] | 1393 | rc = flash_io_write(fd_current); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1394 | } else { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1395 | rc = flash_read(fd_current); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1398 | if (close(fd_current)) { |
| 1399 | fprintf(stderr, |
| 1400 | "I/O error on %s: %s\n", |
| 1401 | DEVNAME(dev_current), strerror(errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1402 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1405 | return rc; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | /* |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1409 | * Prevent confusion if running from erased flash memory |
| 1410 | */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1411 | int fw_env_open(struct env_opts *opts) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1412 | { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1413 | int crc0, crc0_ok; |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1414 | unsigned char flag0; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1415 | void *addr0 = NULL; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1416 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1417 | int crc1, crc1_ok; |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1418 | unsigned char flag1; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1419 | void *addr1 = NULL; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1420 | |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 1421 | int ret; |
| 1422 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1423 | struct env_image_single *single; |
| 1424 | struct env_image_redundant *redundant; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1425 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 1426 | if (!opts) |
| 1427 | opts = &default_opts; |
| 1428 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1429 | if (parse_config(opts)) /* should fill envdevices */ |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1430 | return -EINVAL; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1431 | |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1432 | addr0 = calloc(1, CUR_ENVSIZE); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1433 | if (addr0 == NULL) { |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1434 | fprintf(stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1435 | "Not enough memory for environment (%ld bytes)\n", |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1436 | CUR_ENVSIZE); |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1437 | ret = -ENOMEM; |
| 1438 | goto open_cleanup; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1439 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1440 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1441 | /* read environment from FLASH to local buffer */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1442 | environment.image = addr0; |
| 1443 | |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1444 | if (have_redund_env) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1445 | redundant = addr0; |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1446 | environment.crc = &redundant->crc; |
| 1447 | environment.flags = &redundant->flags; |
| 1448 | environment.data = redundant->data; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1449 | } else { |
| 1450 | single = addr0; |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1451 | environment.crc = &single->crc; |
| 1452 | environment.flags = NULL; |
| 1453 | environment.data = single->data; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1454 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1455 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1456 | dev_current = 0; |
Tom Rini | 4c1192c | 2018-07-01 22:10:33 -0400 | [diff] [blame] | 1457 | if (flash_io(O_RDONLY)) { |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1458 | ret = -EIO; |
| 1459 | goto open_cleanup; |
| 1460 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1461 | |
Tom Rini | 4c1192c | 2018-07-01 22:10:33 -0400 | [diff] [blame] | 1462 | crc0 = crc32(0, (uint8_t *)environment.data, ENV_SIZE); |
| 1463 | |
| 1464 | crc0_ok = (crc0 == *environment.crc); |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1465 | if (!have_redund_env) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1466 | if (!crc0_ok) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1467 | fprintf(stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1468 | "Warning: Bad CRC, using default environment\n"); |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1469 | memcpy(environment.data, default_environment, |
| 1470 | sizeof(default_environment)); |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 1471 | environment.dirty = 1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1472 | } |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1473 | } else { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1474 | flag0 = *environment.flags; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1475 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1476 | dev_current = 1; |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1477 | addr1 = calloc(1, CUR_ENVSIZE); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1478 | if (addr1 == NULL) { |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1479 | fprintf(stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1480 | "Not enough memory for environment (%ld bytes)\n", |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1481 | CUR_ENVSIZE); |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1482 | ret = -ENOMEM; |
| 1483 | goto open_cleanup; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1484 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1485 | redundant = addr1; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1486 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1487 | /* |
| 1488 | * have to set environment.image for flash_read(), careful - |
| 1489 | * other pointers in environment still point inside addr0 |
| 1490 | */ |
| 1491 | environment.image = addr1; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1492 | if (flash_io(O_RDONLY)) { |
Tom Rini | 4c1192c | 2018-07-01 22:10:33 -0400 | [diff] [blame] | 1493 | ret = -EIO; |
| 1494 | goto open_cleanup; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1495 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1496 | |
| 1497 | /* Check flag scheme compatibility */ |
| 1498 | if (DEVTYPE(dev_current) == MTD_NORFLASH && |
| 1499 | DEVTYPE(!dev_current) == MTD_NORFLASH) { |
| 1500 | environment.flag_scheme = FLAG_BOOLEAN; |
| 1501 | } else if (DEVTYPE(dev_current) == MTD_NANDFLASH && |
| 1502 | DEVTYPE(!dev_current) == MTD_NANDFLASH) { |
| 1503 | environment.flag_scheme = FLAG_INCREMENTAL; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1504 | } else if (DEVTYPE(dev_current) == MTD_DATAFLASH && |
| 1505 | DEVTYPE(!dev_current) == MTD_DATAFLASH) { |
| 1506 | environment.flag_scheme = FLAG_BOOLEAN; |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 1507 | } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME && |
| 1508 | DEVTYPE(!dev_current) == MTD_UBIVOLUME) { |
| 1509 | environment.flag_scheme = FLAG_INCREMENTAL; |
Oliver Metz | e745d4c | 2013-08-30 00:56:01 +0200 | [diff] [blame] | 1510 | } else if (DEVTYPE(dev_current) == MTD_ABSENT && |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1511 | DEVTYPE(!dev_current) == MTD_ABSENT && |
| 1512 | IS_UBI(dev_current) == IS_UBI(!dev_current)) { |
Oliver Metz | e745d4c | 2013-08-30 00:56:01 +0200 | [diff] [blame] | 1513 | environment.flag_scheme = FLAG_INCREMENTAL; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1514 | } else { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1515 | fprintf(stderr, "Incompatible flash types!\n"); |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1516 | ret = -EINVAL; |
| 1517 | goto open_cleanup; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1518 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1519 | |
Tom Rini | 4c1192c | 2018-07-01 22:10:33 -0400 | [diff] [blame] | 1520 | crc1 = crc32(0, (uint8_t *)redundant->data, ENV_SIZE); |
| 1521 | |
| 1522 | crc1_ok = (crc1 == redundant->crc); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1523 | flag1 = redundant->flags; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1524 | |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 1525 | /* |
| 1526 | * environment.data still points to ((struct |
| 1527 | * env_image_redundant *)addr0)->data. If the two |
| 1528 | * environments differ, or one has bad crc, force a |
| 1529 | * write-out by marking the environment dirty. |
| 1530 | */ |
| 1531 | if (memcmp(environment.data, redundant->data, ENV_SIZE) || |
| 1532 | !crc0_ok || !crc1_ok) |
| 1533 | environment.dirty = 1; |
| 1534 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1535 | if (crc0_ok && !crc1_ok) { |
| 1536 | dev_current = 0; |
| 1537 | } else if (!crc0_ok && crc1_ok) { |
| 1538 | dev_current = 1; |
| 1539 | } else if (!crc0_ok && !crc1_ok) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1540 | fprintf(stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1541 | "Warning: Bad CRC, using default environment\n"); |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1542 | memcpy(environment.data, default_environment, |
| 1543 | sizeof(default_environment)); |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 1544 | environment.dirty = 1; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1545 | dev_current = 0; |
| 1546 | } else { |
| 1547 | switch (environment.flag_scheme) { |
| 1548 | case FLAG_BOOLEAN: |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 1549 | if (flag0 == ENV_REDUND_ACTIVE && |
| 1550 | flag1 == ENV_REDUND_OBSOLETE) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1551 | dev_current = 0; |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 1552 | } else if (flag0 == ENV_REDUND_OBSOLETE && |
| 1553 | flag1 == ENV_REDUND_ACTIVE) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1554 | dev_current = 1; |
| 1555 | } else if (flag0 == flag1) { |
| 1556 | dev_current = 0; |
| 1557 | } else if (flag0 == 0xFF) { |
| 1558 | dev_current = 0; |
| 1559 | } else if (flag1 == 0xFF) { |
| 1560 | dev_current = 1; |
| 1561 | } else { |
| 1562 | dev_current = 0; |
| 1563 | } |
| 1564 | break; |
| 1565 | case FLAG_INCREMENTAL: |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1566 | if (flag0 == 255 && flag1 == 0) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1567 | dev_current = 1; |
| 1568 | else if ((flag1 == 255 && flag0 == 0) || |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1569 | flag0 >= flag1) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1570 | dev_current = 0; |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1571 | else /* flag1 > flag0 */ |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1572 | dev_current = 1; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1573 | break; |
| 1574 | default: |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1575 | fprintf(stderr, "Unknown flag scheme %u\n", |
| 1576 | environment.flag_scheme); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1577 | return -1; |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | * If we are reading, we don't need the flag and the CRC any |
| 1583 | * more, if we are writing, we will re-calculate CRC and update |
| 1584 | * flags before writing out |
| 1585 | */ |
| 1586 | if (dev_current) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1587 | environment.image = addr1; |
| 1588 | environment.crc = &redundant->crc; |
| 1589 | environment.flags = &redundant->flags; |
| 1590 | environment.data = redundant->data; |
| 1591 | free(addr0); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1592 | } else { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1593 | environment.image = addr0; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1594 | /* Other pointers are already set */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1595 | free(addr1); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1596 | } |
Joe Hershberger | a8dd2c6 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 1597 | #ifdef DEBUG |
| 1598 | fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current)); |
| 1599 | #endif |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1600 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1601 | return 0; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1602 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1603 | open_cleanup: |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1604 | if (addr0) |
| 1605 | free(addr0); |
| 1606 | |
| 1607 | if (addr1) |
Björn Stenberg | 4d1734d | 2019-04-17 16:56:54 +0200 | [diff] [blame] | 1608 | free(addr1); |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1609 | |
| 1610 | return ret; |
| 1611 | } |
| 1612 | |
| 1613 | /* |
| 1614 | * Simply free allocated buffer with environment |
| 1615 | */ |
| 1616 | int fw_env_close(struct env_opts *opts) |
| 1617 | { |
| 1618 | if (environment.image) |
| 1619 | free(environment.image); |
| 1620 | |
| 1621 | environment.image = NULL; |
| 1622 | |
| 1623 | return 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1626 | static int check_device_config(int dev) |
| 1627 | { |
| 1628 | struct stat st; |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1629 | int32_t lnum = 0; |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1630 | int fd, rc = 0; |
| 1631 | |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1632 | /* Fills in IS_UBI(), converts DEVNAME() with ubi volume name */ |
| 1633 | ubi_check_dev(dev); |
| 1634 | |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1635 | fd = open(DEVNAME(dev), O_RDONLY); |
| 1636 | if (fd < 0) { |
| 1637 | fprintf(stderr, |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1638 | "Cannot open %s: %s\n", DEVNAME(dev), strerror(errno)); |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1639 | return -1; |
| 1640 | } |
| 1641 | |
| 1642 | rc = fstat(fd, &st); |
| 1643 | if (rc < 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1644 | fprintf(stderr, "Cannot stat the file %s\n", DEVNAME(dev)); |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1645 | goto err; |
| 1646 | } |
| 1647 | |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1648 | if (IS_UBI(dev)) { |
| 1649 | rc = ioctl(fd, UBI_IOCEBISMAP, &lnum); |
| 1650 | if (rc < 0) { |
| 1651 | fprintf(stderr, "Cannot get UBI information for %s\n", |
| 1652 | DEVNAME(dev)); |
| 1653 | goto err; |
| 1654 | } |
| 1655 | } else if (S_ISCHR(st.st_mode)) { |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1656 | struct mtd_info_user mtdinfo; |
| 1657 | rc = ioctl(fd, MEMGETINFO, &mtdinfo); |
| 1658 | if (rc < 0) { |
| 1659 | fprintf(stderr, "Cannot get MTD information for %s\n", |
| 1660 | DEVNAME(dev)); |
| 1661 | goto err; |
| 1662 | } |
| 1663 | if (mtdinfo.type != MTD_NORFLASH && |
| 1664 | mtdinfo.type != MTD_NANDFLASH && |
| 1665 | mtdinfo.type != MTD_DATAFLASH && |
| 1666 | mtdinfo.type != MTD_UBIVOLUME) { |
| 1667 | fprintf(stderr, "Unsupported flash type %u on %s\n", |
| 1668 | mtdinfo.type, DEVNAME(dev)); |
| 1669 | goto err; |
| 1670 | } |
| 1671 | DEVTYPE(dev) = mtdinfo.type; |
Rasmus Villemoes | faf2998 | 2020-03-24 13:57:40 +0100 | [diff] [blame] | 1672 | if (DEVESIZE(dev) == 0 && ENVSECTORS(dev) == 0 && |
| 1673 | mtdinfo.type == MTD_NORFLASH) |
| 1674 | DEVESIZE(dev) = mtdinfo.erasesize; |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1675 | if (DEVESIZE(dev) == 0) |
| 1676 | /* Assume the erase size is the same as the env-size */ |
| 1677 | DEVESIZE(dev) = ENVSIZE(dev); |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1678 | } else { |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1679 | uint64_t size; |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1680 | DEVTYPE(dev) = MTD_ABSENT; |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1681 | if (DEVESIZE(dev) == 0) |
| 1682 | /* Assume the erase size to be 512 bytes */ |
| 1683 | DEVESIZE(dev) = 0x200; |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1684 | |
| 1685 | /* |
| 1686 | * Check for negative offsets, treat it as backwards offset |
| 1687 | * from the end of the block device |
| 1688 | */ |
| 1689 | if (DEVOFFSET(dev) < 0) { |
| 1690 | rc = ioctl(fd, BLKGETSIZE64, &size); |
| 1691 | if (rc < 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1692 | fprintf(stderr, |
| 1693 | "Could not get block device size on %s\n", |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1694 | DEVNAME(dev)); |
| 1695 | goto err; |
| 1696 | } |
| 1697 | |
| 1698 | DEVOFFSET(dev) = DEVOFFSET(dev) + size; |
| 1699 | #ifdef DEBUG |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1700 | fprintf(stderr, |
| 1701 | "Calculated device offset 0x%llx on %s\n", |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1702 | DEVOFFSET(dev), DEVNAME(dev)); |
| 1703 | #endif |
| 1704 | } |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1707 | if (ENVSECTORS(dev) == 0) |
| 1708 | /* Assume enough sectors to cover the environment */ |
| 1709 | ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev)); |
| 1710 | |
| 1711 | if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1712 | fprintf(stderr, |
| 1713 | "Environment does not start on (erase) block boundary\n"); |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1714 | errno = EINVAL; |
| 1715 | return -1; |
| 1716 | } |
| 1717 | |
| 1718 | if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1719 | fprintf(stderr, |
| 1720 | "Environment does not fit into available sectors\n"); |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1721 | errno = EINVAL; |
| 1722 | return -1; |
| 1723 | } |
| 1724 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1725 | err: |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1726 | close(fd); |
| 1727 | return rc; |
| 1728 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1729 | |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1730 | static int parse_config(struct env_opts *opts) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1731 | { |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1732 | int rc; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1733 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 1734 | if (!opts) |
| 1735 | opts = &default_opts; |
Anatolij Gustschin | e89ead3 | 2016-04-29 22:00:11 +0200 | [diff] [blame] | 1736 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 1737 | #if defined(CONFIG_FILE) |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1738 | /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1739 | if (get_config(opts->config_file)) { |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 1740 | fprintf(stderr, "Cannot parse config file '%s': %m\n", |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1741 | opts->config_file); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1742 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1743 | } |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1744 | #else |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1745 | DEVNAME(0) = DEVICE1_NAME; |
| 1746 | DEVOFFSET(0) = DEVICE1_OFFSET; |
| 1747 | ENVSIZE(0) = ENV1_SIZE; |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1748 | |
| 1749 | /* Set defaults for DEVESIZE, ENVSECTORS later once we |
| 1750 | * know DEVTYPE |
| 1751 | */ |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1752 | #ifdef DEVICE1_ESIZE |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1753 | DEVESIZE(0) = DEVICE1_ESIZE; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1754 | #endif |
| 1755 | #ifdef DEVICE1_ENVSECTORS |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1756 | ENVSECTORS(0) = DEVICE1_ENVSECTORS; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1757 | #endif |
| 1758 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1759 | #ifdef HAVE_REDUND |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1760 | DEVNAME(1) = DEVICE2_NAME; |
| 1761 | DEVOFFSET(1) = DEVICE2_OFFSET; |
| 1762 | ENVSIZE(1) = ENV2_SIZE; |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1763 | |
| 1764 | /* Set defaults for DEVESIZE, ENVSECTORS later once we |
| 1765 | * know DEVTYPE |
| 1766 | */ |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1767 | #ifdef DEVICE2_ESIZE |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1768 | DEVESIZE(1) = DEVICE2_ESIZE; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1769 | #endif |
| 1770 | #ifdef DEVICE2_ENVSECTORS |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1771 | ENVSECTORS(1) = DEVICE2_ENVSECTORS; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1772 | #endif |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1773 | have_redund_env = 1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1774 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1775 | #endif |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1776 | rc = check_device_config(0); |
| 1777 | if (rc < 0) |
| 1778 | return rc; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1779 | |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1780 | if (have_redund_env) { |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1781 | rc = check_device_config(1); |
| 1782 | if (rc < 0) |
| 1783 | return rc; |
Andreas Fenkart | b8bf404 | 2016-04-19 22:43:42 +0200 | [diff] [blame] | 1784 | |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1785 | if (ENVSIZE(0) != ENVSIZE(1)) { |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1786 | fprintf(stderr, |
Philip Molloy | f60f751 | 2019-03-31 03:44:57 +0000 | [diff] [blame] | 1787 | "Redundant environments have unequal size\n"); |
Andreas Fenkart | 31f2e05 | 2016-08-17 23:41:53 +0200 | [diff] [blame] | 1788 | return -1; |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1789 | } |
Andreas Fenkart | b8bf404 | 2016-04-19 22:43:42 +0200 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | usable_envsize = CUR_ENVSIZE - sizeof(uint32_t); |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1793 | if (have_redund_env) |
Andreas Fenkart | b8bf404 | 2016-04-19 22:43:42 +0200 | [diff] [blame] | 1794 | usable_envsize -= sizeof(char); |
| 1795 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1798 | |
| 1799 | #if defined(CONFIG_FILE) |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1800 | static int get_config(char *fname) |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1801 | { |
| 1802 | FILE *fp; |
| 1803 | int i = 0; |
| 1804 | int rc; |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 1805 | char *line = NULL; |
| 1806 | size_t linesize = 0; |
Tom Rini | 313e912 | 2014-03-28 12:03:33 -0400 | [diff] [blame] | 1807 | char *devname; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1808 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1809 | fp = fopen(fname, "r"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1810 | if (fp == NULL) |
| 1811 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1812 | |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 1813 | while (i < 2 && getline(&line, &linesize, fp) != -1) { |
| 1814 | /* Skip comment strings */ |
| 1815 | if (line[0] == '#') |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1816 | continue; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1817 | |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 1818 | rc = sscanf(line, "%ms %lli %lx %lx %lx", |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1819 | &devname, |
| 1820 | &DEVOFFSET(i), |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1821 | &ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1822 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1823 | if (rc < 3) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1824 | continue; |
| 1825 | |
Tom Rini | 313e912 | 2014-03-28 12:03:33 -0400 | [diff] [blame] | 1826 | DEVNAME(i) = devname; |
| 1827 | |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1828 | /* Set defaults for DEVESIZE, ENVSECTORS later once we |
| 1829 | * know DEVTYPE |
| 1830 | */ |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1831 | |
| 1832 | i++; |
| 1833 | } |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 1834 | free(line); |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1835 | fclose(fp); |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1836 | |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1837 | have_redund_env = i - 1; |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1838 | if (!i) { /* No valid entries found */ |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1839 | errno = EINVAL; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1840 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1841 | } else |
| 1842 | return 0; |
| 1843 | } |
| 1844 | #endif |