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