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, |
Miaoqian Lin | 64f2dda | 2022-09-19 08:28:09 +0400 | [diff] [blame] | 195 | volname) == 0) { |
| 196 | closedir(sysfs_ubi); |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 197 | return volnum; |
Miaoqian Lin | 64f2dda | 2022-09-19 08:28:09 +0400 | [diff] [blame] | 198 | } |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
Miaoqian Lin | 64f2dda | 2022-09-19 08:28:09 +0400 | [diff] [blame] | 201 | closedir(sysfs_ubi); |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 202 | |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | static int ubi_get_devnum_by_devname(const char *devname) |
| 207 | { |
| 208 | int devnum; |
| 209 | int ret; |
| 210 | |
| 211 | ret = sscanf(devname + sizeof(UBI_DEV_START) - 1, "%d", &devnum); |
| 212 | if (ret != 1) |
| 213 | return -1; |
| 214 | |
| 215 | return devnum; |
| 216 | } |
| 217 | |
| 218 | static const char *ubi_get_volume_devname(const char *devname, |
| 219 | const char *volname) |
| 220 | { |
| 221 | char *volume_devname; |
| 222 | int volnum; |
| 223 | int devnum; |
| 224 | int ret; |
| 225 | |
| 226 | devnum = ubi_get_devnum_by_devname(devname); |
| 227 | if (devnum < 0) |
| 228 | return NULL; |
| 229 | |
| 230 | volnum = ubi_get_volnum_by_name(devnum, volname); |
| 231 | if (volnum < 0) |
| 232 | return NULL; |
| 233 | |
| 234 | ret = asprintf(&volume_devname, "%s_%d", devname, volnum); |
| 235 | if (ret < 0) |
| 236 | return NULL; |
| 237 | |
| 238 | #ifdef DEBUG |
| 239 | fprintf(stderr, "Found ubi volume \"%s:%s\" -> %s\n", |
| 240 | devname, volname, volume_devname); |
| 241 | #endif |
| 242 | |
| 243 | return volume_devname; |
| 244 | } |
| 245 | |
| 246 | static void ubi_check_dev(unsigned int dev_id) |
| 247 | { |
| 248 | char *devname = (char *)DEVNAME(dev_id); |
| 249 | char *pname; |
| 250 | const char *volname = NULL; |
| 251 | const char *volume_devname; |
| 252 | |
| 253 | if (!is_ubi_devname(DEVNAME(dev_id))) |
| 254 | return; |
| 255 | |
| 256 | IS_UBI(dev_id) = 1; |
| 257 | |
| 258 | for (pname = devname; *pname != '\0'; pname++) { |
| 259 | if (*pname == ':') { |
| 260 | *pname = '\0'; |
| 261 | volname = pname + 1; |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (volname) { |
| 267 | /* Let's find real volume device name */ |
| 268 | volume_devname = ubi_get_volume_devname(devname, volname); |
| 269 | if (!volume_devname) { |
| 270 | fprintf(stderr, "Didn't found ubi volume \"%s\"\n", |
| 271 | volname); |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | free(devname); |
| 276 | DEVNAME(dev_id) = volume_devname; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static int ubi_update_start(int fd, int64_t bytes) |
| 281 | { |
| 282 | if (ioctl(fd, UBI_IOCVOLUP, &bytes)) |
| 283 | return -1; |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int ubi_read(int fd, void *buf, size_t count) |
| 288 | { |
| 289 | ssize_t ret; |
| 290 | |
| 291 | while (count > 0) { |
| 292 | ret = read(fd, buf, count); |
| 293 | if (ret > 0) { |
| 294 | count -= ret; |
| 295 | buf += ret; |
| 296 | |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | if (ret == 0) { |
| 301 | /* |
| 302 | * Happens in case of too short volume data size. If we |
| 303 | * return error status we will fail it will be treated |
| 304 | * as UBI device error. |
| 305 | * |
| 306 | * Leave catching this error to CRC check. |
| 307 | */ |
| 308 | fprintf(stderr, "Warning: end of data on ubi volume\n"); |
| 309 | return 0; |
| 310 | } else if (errno == EBADF) { |
| 311 | /* |
| 312 | * Happens in case of corrupted volume. The same as |
| 313 | * above, we cannot return error now, as we will still |
| 314 | * be able to successfully write environment later. |
| 315 | */ |
| 316 | fprintf(stderr, "Warning: corrupted volume?\n"); |
| 317 | return 0; |
| 318 | } else if (errno == EINTR) { |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | fprintf(stderr, "Cannot read %u bytes from ubi volume, %s\n", |
| 323 | (unsigned int)count, strerror(errno)); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int ubi_write(int fd, const void *buf, size_t count) |
| 331 | { |
| 332 | ssize_t ret; |
| 333 | |
| 334 | while (count > 0) { |
| 335 | ret = write(fd, buf, count); |
| 336 | if (ret <= 0) { |
| 337 | if (ret < 0 && errno == EINTR) |
| 338 | continue; |
| 339 | |
| 340 | fprintf(stderr, "Cannot write %u bytes to ubi volume\n", |
| 341 | (unsigned int)count); |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | count -= ret; |
| 346 | buf += ret; |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 352 | static int flash_io(int mode, void *buf, size_t count); |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 353 | static int parse_config(struct env_opts *opts); |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 354 | |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 355 | #if defined(CONFIG_FILE) |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 356 | static int get_config(char *); |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 357 | #endif |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 358 | |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 359 | static char *skip_chars(char *s) |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 360 | { |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 361 | for (; *s != '\0'; s++) { |
Stefan Agner | 8783324 | 2018-03-01 14:06:32 +0100 | [diff] [blame] | 362 | if (isblank(*s) || *s == '=') |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 363 | return s; |
| 364 | } |
| 365 | return NULL; |
| 366 | } |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 367 | |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 368 | static char *skip_blanks(char *s) |
| 369 | { |
| 370 | for (; *s != '\0'; s++) { |
| 371 | if (!isblank(*s)) |
Andreas Fenkart | e42c68e | 2016-03-11 09:39:36 +0100 | [diff] [blame] | 372 | return s; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 373 | } |
Andreas Fenkart | e42c68e | 2016-03-11 09:39:36 +0100 | [diff] [blame] | 374 | return NULL; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 375 | } |
| 376 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 377 | /* |
Andreas Fenkart | 3ff1074 | 2016-07-16 17:06:14 +0200 | [diff] [blame] | 378 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 379 | * s2 is a 'name=value' pair. |
| 380 | * If the names match, return the value of s2, else NULL. |
| 381 | */ |
| 382 | static char *envmatch(char *s1, char *s2) |
| 383 | { |
| 384 | if (s1 == NULL || s2 == NULL) |
| 385 | return NULL; |
| 386 | |
| 387 | while (*s1 == *s2++) |
| 388 | if (*s1++ == '=') |
| 389 | return s2; |
| 390 | if (*s1 == '\0' && *(s2 - 1) == '=') |
| 391 | return s2; |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | /** |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 396 | * Search the environment for a variable. |
| 397 | * Return the value, if found, or NULL, if not found. |
| 398 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 399 | char *fw_getenv(char *name) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 400 | { |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 401 | char *env, *nxt; |
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 (env = environment.data; *env; env = nxt + 1) { |
Markus Klotzbücher | 94db557 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 404 | char *val; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 405 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 406 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 407 | if (nxt >= &environment.data[ENV_SIZE]) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 408 | fprintf(stderr, "## Error: " |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 409 | "environment not terminated\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 410 | return NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 413 | val = envmatch(name, env); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 414 | if (!val) |
| 415 | continue; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 416 | return val; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 417 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 418 | return NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 422 | * Search the default environment for a variable. |
| 423 | * Return the value, if found, or NULL, if not found. |
| 424 | */ |
| 425 | char *fw_getdefenv(char *name) |
| 426 | { |
| 427 | char *env, *nxt; |
| 428 | |
| 429 | for (env = default_environment; *env; env = nxt + 1) { |
| 430 | char *val; |
| 431 | |
| 432 | for (nxt = env; *nxt; ++nxt) { |
| 433 | if (nxt >= &default_environment[ENV_SIZE]) { |
| 434 | fprintf(stderr, "## Error: " |
| 435 | "default environment not terminated\n"); |
| 436 | return NULL; |
| 437 | } |
| 438 | } |
| 439 | val = envmatch(name, env); |
| 440 | if (!val) |
| 441 | continue; |
| 442 | return val; |
| 443 | } |
| 444 | return NULL; |
| 445 | } |
| 446 | |
| 447 | /* |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 448 | * Print the current definition of one, or more, or all |
| 449 | * environment variables |
| 450 | */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 451 | 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] | 452 | { |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 453 | int i, rc = 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 454 | |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 455 | if (value_only && argc != 1) { |
| 456 | fprintf(stderr, |
Alex Kiernan | 688cee9 | 2018-02-11 17:16:46 +0000 | [diff] [blame] | 457 | "## Error: `-n'/`--noheader' option requires exactly one argument\n"); |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 458 | return -1; |
| 459 | } |
| 460 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 461 | if (!opts) |
| 462 | opts = &default_opts; |
| 463 | |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 464 | if (fw_env_open(opts)) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 465 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 466 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 467 | if (argc == 0) { /* Print all env variables */ |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 468 | char *env, *nxt; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 469 | for (env = environment.data; *env; env = nxt + 1) { |
| 470 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 471 | if (nxt >= &environment.data[ENV_SIZE]) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 472 | fprintf(stderr, "## Error: " |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 473 | "environment not terminated\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 474 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 478 | printf("%s\n", env); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 479 | } |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 480 | fw_env_close(opts); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 481 | return 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 484 | 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] | 485 | char *name = argv[i]; |
| 486 | char *val = NULL; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 487 | |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 488 | val = fw_getenv(name); |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 489 | if (!val) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 490 | fprintf(stderr, "## Error: \"%s\" not defined\n", name); |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 491 | rc = -1; |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 492 | continue; |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 493 | } |
Andreas Fenkart | 33aaef9 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 494 | |
| 495 | if (value_only) { |
| 496 | puts(val); |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | printf("%s=%s\n", name, val); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 501 | } |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 502 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 503 | fw_env_close(opts); |
| 504 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 505 | return rc; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 508 | int fw_env_flush(struct env_opts *opts) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 509 | { |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 510 | if (!opts) |
| 511 | opts = &default_opts; |
| 512 | |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 513 | if (!environment.dirty) |
| 514 | return 0; |
| 515 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 516 | /* |
| 517 | * Update CRC |
| 518 | */ |
| 519 | *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 520 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 521 | /* write environment back to flash */ |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 522 | if (flash_io(O_RDWR, environment.image, CUR_ENVSIZE)) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 523 | fprintf(stderr, "Error: can't write fw_env to flash\n"); |
| 524 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 527 | return 0; |
| 528 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 529 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 530 | /* |
| 531 | * Set/Clear a single variable in the environment. |
| 532 | * This is called in sequence to update the environment |
| 533 | * in RAM without updating the copy in flash after each set |
| 534 | */ |
| 535 | int fw_env_write(char *name, char *value) |
| 536 | { |
| 537 | int len; |
| 538 | char *env, *nxt; |
| 539 | char *oldval = NULL; |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 540 | int deleting, creating, overwriting; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 541 | |
| 542 | /* |
| 543 | * search if variable with this name already exists |
| 544 | */ |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 545 | for (nxt = env = environment.data; *env; env = nxt + 1) { |
| 546 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 547 | if (nxt >= &environment.data[ENV_SIZE]) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 548 | fprintf(stderr, "## Error: " |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 549 | "environment not terminated\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 550 | errno = EINVAL; |
| 551 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 554 | oldval = envmatch(name, env); |
| 555 | if (oldval) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 556 | break; |
| 557 | } |
| 558 | |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 559 | deleting = (oldval && !(value && strlen(value))); |
| 560 | creating = (!oldval && (value && strlen(value))); |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 561 | overwriting = (oldval && (value && strlen(value) && |
| 562 | strcmp(oldval, value))); |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 563 | |
| 564 | /* check for permission */ |
| 565 | if (deleting) { |
| 566 | if (env_flags_validate_varaccess(name, |
| 567 | ENV_FLAGS_VARACCESS_PREVENT_DELETE)) { |
| 568 | printf("Can't delete \"%s\"\n", name); |
| 569 | errno = EROFS; |
| 570 | return -1; |
| 571 | } |
| 572 | } else if (overwriting) { |
| 573 | if (env_flags_validate_varaccess(name, |
| 574 | ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) { |
| 575 | printf("Can't overwrite \"%s\"\n", name); |
| 576 | errno = EROFS; |
| 577 | return -1; |
| 578 | } else if (env_flags_validate_varaccess(name, |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 579 | ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) { |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 580 | const char *defval = fw_getdefenv(name); |
| 581 | |
| 582 | if (defval == NULL) |
| 583 | defval = ""; |
| 584 | if (strcmp(oldval, defval) |
| 585 | != 0) { |
| 586 | printf("Can't overwrite \"%s\"\n", name); |
| 587 | errno = EROFS; |
| 588 | return -1; |
| 589 | } |
| 590 | } |
| 591 | } else if (creating) { |
| 592 | if (env_flags_validate_varaccess(name, |
| 593 | ENV_FLAGS_VARACCESS_PREVENT_CREATE)) { |
| 594 | printf("Can't create \"%s\"\n", name); |
| 595 | errno = EROFS; |
| 596 | return -1; |
| 597 | } |
| 598 | } else |
| 599 | /* Nothing to do */ |
| 600 | return 0; |
| 601 | |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 602 | environment.dirty = 1; |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 603 | if (deleting || overwriting) { |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 604 | if (*++nxt == '\0') { |
| 605 | *env = '\0'; |
| 606 | } else { |
| 607 | for (;;) { |
| 608 | *env = *nxt++; |
| 609 | if ((*env == '\0') && (*nxt == '\0')) |
| 610 | break; |
| 611 | ++env; |
| 612 | } |
| 613 | } |
| 614 | *++env = '\0'; |
| 615 | } |
| 616 | |
| 617 | /* Delete only ? */ |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 618 | if (!value || !strlen(value)) |
| 619 | return 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 620 | |
| 621 | /* |
| 622 | * Append new definition at the end |
| 623 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 624 | for (env = environment.data; *env || *(env + 1); ++env) |
| 625 | ; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 626 | if (env > environment.data) |
| 627 | ++env; |
| 628 | /* |
| 629 | * Overflow when: |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 630 | * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 631 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 632 | len = strlen(name) + 2; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 633 | /* add '=' for first arg, ' ' for all others */ |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 634 | len += strlen(value) + 1; |
| 635 | |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 636 | if (len > (&environment.data[ENV_SIZE] - env)) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 637 | fprintf(stderr, |
| 638 | "Error: environment overflow, \"%s\" deleted\n", name); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 639 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 640 | } |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 641 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 642 | while ((*env = *name++) != '\0') |
| 643 | env++; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 644 | *env = '='; |
| 645 | while ((*++env = *value++) != '\0') |
| 646 | ; |
| 647 | |
| 648 | /* end is marked with double '\0' */ |
| 649 | *++env = '\0'; |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | /* |
| 655 | * Deletes or sets environment variables. Returns -1 and sets errno error codes: |
| 656 | * 0 - OK |
| 657 | * EINVAL - need at least 1 argument |
| 658 | * EROFS - certain variables ("ethaddr", "serial#") cannot be |
| 659 | * modified or deleted |
| 660 | * |
| 661 | */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 662 | int fw_env_set(int argc, char *argv[], struct env_opts *opts) |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 663 | { |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 664 | int i; |
Mike Frysinger | 178ec77 | 2012-11-10 19:47:45 +0000 | [diff] [blame] | 665 | size_t len; |
Andreas Fenkart | 4e336ee | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 666 | char *name, **valv; |
xypron.glpk@gmx.de | 2a3325c | 2017-04-15 13:05:40 +0200 | [diff] [blame] | 667 | char *oldval; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 668 | char *value = NULL; |
Andreas Fenkart | 4e336ee | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 669 | int valc; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 670 | int ret; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 671 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 672 | if (!opts) |
| 673 | opts = &default_opts; |
| 674 | |
Andreas Fenkart | 14908ef | 2015-12-09 13:13:25 +0100 | [diff] [blame] | 675 | if (argc < 1) { |
| 676 | fprintf(stderr, "## Error: variable name missing\n"); |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 677 | errno = EINVAL; |
| 678 | return -1; |
| 679 | } |
| 680 | |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 681 | if (fw_env_open(opts)) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 682 | fprintf(stderr, "Error: environment not initialized\n"); |
| 683 | return -1; |
| 684 | } |
| 685 | |
Andreas Fenkart | 14908ef | 2015-12-09 13:13:25 +0100 | [diff] [blame] | 686 | name = argv[0]; |
| 687 | valv = argv + 1; |
| 688 | valc = argc - 1; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 689 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 690 | if (env_flags_validate_env_set_params(name, valv, valc) < 0) { |
| 691 | fw_env_close(opts); |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 692 | return -1; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 693 | } |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 694 | |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 695 | len = 0; |
Andreas Fenkart | 4e336ee | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 696 | for (i = 0; i < valc; ++i) { |
| 697 | char *val = valv[i]; |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 698 | size_t val_len = strlen(val); |
| 699 | |
Joe Hershberger | 03bf21d | 2012-10-15 15:29:24 +0000 | [diff] [blame] | 700 | if (value) |
| 701 | value[len - 1] = ' '; |
xypron.glpk@gmx.de | 2a3325c | 2017-04-15 13:05:40 +0200 | [diff] [blame] | 702 | oldval = value; |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 703 | value = realloc(value, len + val_len + 1); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 704 | if (!value) { |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 705 | fprintf(stderr, |
Luka Perkov | d598271 | 2011-09-05 23:40:13 +0200 | [diff] [blame] | 706 | "Cannot malloc %zu bytes: %s\n", |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 707 | len, strerror(errno)); |
xypron.glpk@gmx.de | 2a3325c | 2017-04-15 13:05:40 +0200 | [diff] [blame] | 708 | free(oldval); |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 709 | return -1; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 710 | } |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 711 | |
| 712 | memcpy(value + len, val, val_len); |
| 713 | len += val_len; |
Joe Hershberger | 03bf21d | 2012-10-15 15:29:24 +0000 | [diff] [blame] | 714 | value[len++] = '\0'; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 717 | fw_env_write(name, value); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 718 | |
Joe Hershberger | ed97f4c | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 719 | free(value); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 720 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 721 | ret = fw_env_flush(opts); |
| 722 | fw_env_close(opts); |
| 723 | |
| 724 | return ret; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 725 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 726 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 727 | /* |
| 728 | * Parse a file and configure the u-boot variables. |
| 729 | * The script file has a very simple format, as follows: |
| 730 | * |
| 731 | * Each line has a couple with name, value: |
| 732 | * <white spaces>variable_name<white spaces>variable_value |
| 733 | * |
| 734 | * Both variable_name and variable_value are interpreted as strings. |
| 735 | * Any character after <white spaces> and before ending \r\n is interpreted |
| 736 | * as variable's value (no comment allowed on these lines !) |
| 737 | * |
| 738 | * Comments are allowed if the first character in the line is # |
| 739 | * |
| 740 | * Returns -1 and sets errno error codes: |
| 741 | * 0 - OK |
| 742 | * -1 - Error |
| 743 | */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 744 | int fw_parse_script(char *fname, struct env_opts *opts) |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 745 | { |
| 746 | FILE *fp; |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 747 | char *line = NULL; |
| 748 | size_t linesize = 0; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 749 | char *name; |
| 750 | char *val; |
| 751 | int lineno = 0; |
| 752 | int len; |
| 753 | int ret = 0; |
| 754 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 755 | if (!opts) |
| 756 | opts = &default_opts; |
| 757 | |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 758 | if (fw_env_open(opts)) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 759 | fprintf(stderr, "Error: environment not initialized\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 760 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 763 | if (strcmp(fname, "-") == 0) |
| 764 | fp = stdin; |
| 765 | else { |
| 766 | fp = fopen(fname, "r"); |
| 767 | if (fp == NULL) { |
| 768 | fprintf(stderr, "I cannot open %s for reading\n", |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 769 | fname); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 770 | return -1; |
| 771 | } |
| 772 | } |
| 773 | |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 774 | while ((len = getline(&line, &linesize, fp)) != -1) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 775 | lineno++; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 776 | |
| 777 | /* |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 778 | * Read a whole line from the file. If the line is not |
| 779 | * terminated, reports an error and exit. |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 780 | */ |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 781 | if (line[len - 1] != '\n') { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 782 | fprintf(stderr, |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 783 | "Line %d not correctly terminated\n", |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 784 | lineno); |
| 785 | ret = -1; |
| 786 | break; |
| 787 | } |
| 788 | |
| 789 | /* Drop ending line feed / carriage return */ |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 790 | line[--len] = '\0'; |
| 791 | if (len && line[len - 1] == '\r') |
| 792 | line[--len] = '\0'; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 793 | |
| 794 | /* Skip comment or empty lines */ |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 795 | if (len == 0 || line[0] == '#') |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 796 | continue; |
| 797 | |
| 798 | /* |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 799 | * Search for variable's name remove leading whitespaces |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 800 | */ |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 801 | name = skip_blanks(line); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 802 | if (!name) |
| 803 | continue; |
| 804 | |
| 805 | /* The first white space is the end of variable name */ |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 806 | val = skip_chars(name); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 807 | len = strlen(name); |
| 808 | if (val) { |
| 809 | *val++ = '\0'; |
| 810 | if ((val - name) < len) |
Andreas Fenkart | 191d26f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 811 | val = skip_blanks(val); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 812 | else |
| 813 | val = NULL; |
| 814 | } |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 815 | #ifdef DEBUG |
| 816 | fprintf(stderr, "Setting %s : %s\n", |
| 817 | name, val ? val : " removed"); |
| 818 | #endif |
| 819 | |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 820 | if (env_flags_validate_type(name, val) < 0) { |
| 821 | ret = -1; |
| 822 | break; |
| 823 | } |
| 824 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 825 | /* |
| 826 | * If there is an error setting a variable, |
| 827 | * try to save the environment and returns an error |
| 828 | */ |
| 829 | if (fw_env_write(name, val)) { |
| 830 | fprintf(stderr, |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 831 | "fw_env_write returns with error : %s\n", |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 832 | strerror(errno)); |
| 833 | ret = -1; |
| 834 | break; |
| 835 | } |
| 836 | |
| 837 | } |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 838 | free(line); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 839 | |
| 840 | /* Close file if not stdin */ |
| 841 | if (strcmp(fname, "-") != 0) |
| 842 | fclose(fp); |
| 843 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 844 | ret |= fw_env_flush(opts); |
| 845 | |
| 846 | fw_env_close(opts); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 847 | |
| 848 | return ret; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 851 | /** |
Shyam Saini | efe757c | 2018-06-07 19:47:19 +0530 | [diff] [blame] | 852 | * environment_end() - compute offset of first byte right after environment |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 853 | * @dev - index of enviroment buffer |
| 854 | * Return: |
Shyam Saini | efe757c | 2018-06-07 19:47:19 +0530 | [diff] [blame] | 855 | * device offset of first byte right after environment |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 856 | */ |
| 857 | off_t environment_end(int dev) |
| 858 | { |
| 859 | /* environment is block aligned */ |
| 860 | return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev); |
| 861 | } |
| 862 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 863 | /* |
| 864 | * Test for bad block on NAND, just returns 0 on NOR, on NAND: |
| 865 | * 0 - block is good |
| 866 | * > 0 - block is bad |
| 867 | * < 0 - failed to test |
| 868 | */ |
Andreas Fenkart | ae3b320 | 2016-08-29 23:16:58 +0200 | [diff] [blame] | 869 | 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] | 870 | { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 871 | if (mtd_type == MTD_NANDFLASH) { |
Andreas Fenkart | ae3b320 | 2016-08-29 23:16:58 +0200 | [diff] [blame] | 872 | int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 873 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 874 | if (badblock < 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 875 | perror("Cannot read bad block mark"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 876 | return badblock; |
| 877 | } |
| 878 | |
| 879 | if (badblock) { |
| 880 | #ifdef DEBUG |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 881 | fprintf(stderr, "Bad block at 0x%llx, skipping\n", |
Andreas Fenkart | ae3b320 | 2016-08-29 23:16:58 +0200 | [diff] [blame] | 882 | (unsigned long long)blockstart); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 883 | #endif |
| 884 | return badblock; |
| 885 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | /* |
| 892 | * Read data from flash at an offset into a provided buffer. On NAND it skips |
| 893 | * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from |
| 894 | * the DEVOFFSET (dev) block. On NOR the loop is only run once. |
| 895 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 896 | static int flash_read_buf(int dev, int fd, void *buf, size_t count, |
| 897 | off_t offset) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 898 | { |
| 899 | size_t blocklen; /* erase / write length - one block on NAND, |
| 900 | 0 on NOR */ |
| 901 | size_t processed = 0; /* progress counter */ |
| 902 | size_t readlen = count; /* current read length */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 903 | off_t block_seek; /* offset inside the current block to the start |
| 904 | of the data */ |
| 905 | loff_t blockstart; /* running start of the current block - |
| 906 | MEMGETBADBLOCK needs 64 bits */ |
| 907 | int rc; |
| 908 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 909 | blockstart = (offset / DEVESIZE(dev)) * DEVESIZE(dev); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 910 | |
| 911 | /* Offset inside a block */ |
| 912 | block_seek = offset - blockstart; |
| 913 | |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 914 | if (DEVTYPE(dev) == MTD_NANDFLASH) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 915 | /* |
| 916 | * NAND: calculate which blocks we are reading. We have |
| 917 | * to read one block at a time to skip bad blocks. |
| 918 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 919 | blocklen = DEVESIZE(dev); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 920 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 921 | /* Limit to one block for the first read */ |
| 922 | if (readlen > blocklen - block_seek) |
| 923 | readlen = blocklen - block_seek; |
| 924 | } else { |
| 925 | blocklen = 0; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 926 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 927 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 928 | /* This only runs once on NOR flash */ |
| 929 | while (processed < count) { |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 930 | rc = flash_bad_block(fd, DEVTYPE(dev), blockstart); |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 931 | if (rc < 0) /* block test failed */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 932 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 933 | |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 934 | if (blockstart + block_seek + readlen > environment_end(dev)) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 935 | /* End of range is reached */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 936 | fprintf(stderr, "Too few good blocks within range\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 937 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 940 | if (rc) { /* block is bad */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 941 | blockstart += blocklen; |
| 942 | continue; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 945 | /* |
| 946 | * 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] | 947 | * offset - see env/nand.c::writeenv() |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 948 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 949 | lseek(fd, blockstart + block_seek, SEEK_SET); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 950 | |
Rafał Miłecki | b509c7d | 2023-12-13 13:13:54 +0100 | [diff] [blame^] | 951 | while (readlen) { |
| 952 | rc = read(fd, buf + processed, readlen); |
| 953 | if (rc == -1) { |
| 954 | fprintf(stderr, "Read error on %s: %s\n", |
| 955 | DEVNAME(dev), strerror(errno)); |
| 956 | return -1; |
| 957 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 958 | #ifdef DEBUG |
Rafał Miłecki | b509c7d | 2023-12-13 13:13:54 +0100 | [diff] [blame^] | 959 | fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n", |
| 960 | rc, (unsigned long long)blockstart + block_seek, |
| 961 | DEVNAME(dev)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 962 | #endif |
Rafał Miłecki | b509c7d | 2023-12-13 13:13:54 +0100 | [diff] [blame^] | 963 | processed += rc; |
Thibault Ferrante | 8586cda | 2021-08-24 17:29:50 +0200 | [diff] [blame] | 964 | readlen -= rc; |
Thibault Ferrante | 8586cda | 2021-08-24 17:29:50 +0200 | [diff] [blame] | 965 | } |
Rafał Miłecki | b509c7d | 2023-12-13 13:13:54 +0100 | [diff] [blame^] | 966 | |
| 967 | blockstart += blocklen; |
| 968 | readlen = min(blocklen, count - processed); |
| 969 | block_seek = 0; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | return processed; |
| 973 | } |
| 974 | |
| 975 | /* |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 976 | * Write count bytes from begin of environment, but stay within |
| 977 | * ENVSECTORS(dev) sectors of |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 978 | * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we |
| 979 | * erase and write the whole data at once. |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 980 | */ |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 981 | 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] | 982 | { |
| 983 | void *data; |
| 984 | struct erase_info_user erase; |
| 985 | size_t blocklen; /* length of NAND block / NOR erase sector */ |
| 986 | size_t erase_len; /* whole area that can be erased - may include |
| 987 | bad blocks */ |
| 988 | size_t erasesize; /* erase / write length - one block on NAND, |
| 989 | whole area on NOR */ |
| 990 | size_t processed = 0; /* progress counter */ |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 991 | size_t write_total; /* total size to actually write - excluding |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 992 | bad blocks */ |
| 993 | off_t erase_offset; /* offset to the first erase block (aligned) |
| 994 | below offset */ |
| 995 | off_t block_seek; /* offset inside the erase block to the start |
| 996 | of the data */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 997 | loff_t blockstart; /* running start of the current block - |
| 998 | MEMGETBADBLOCK needs 64 bits */ |
Simon Glass | f820563 | 2020-08-01 10:30:39 -0600 | [diff] [blame] | 999 | int was_locked = 0; /* flash lock flag */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1000 | int rc; |
| 1001 | |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1002 | /* |
| 1003 | * For mtd devices only offset and size of the environment do matter |
| 1004 | */ |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1005 | if (DEVTYPE(dev) == MTD_ABSENT) { |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1006 | blocklen = count; |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1007 | erase_len = blocklen; |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 1008 | blockstart = DEVOFFSET(dev); |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1009 | block_seek = 0; |
| 1010 | write_total = blocklen; |
| 1011 | } else { |
| 1012 | blocklen = DEVESIZE(dev); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1013 | |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 1014 | erase_offset = DEVOFFSET(dev); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1015 | |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1016 | /* Maximum area we may use */ |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 1017 | erase_len = environment_end(dev) - erase_offset; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1018 | |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1019 | blockstart = erase_offset; |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 1020 | |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1021 | /* Offset inside a block */ |
Andreas Fenkart | 0b49b84 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 1022 | block_seek = DEVOFFSET(dev) - erase_offset; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1023 | |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1024 | /* |
| 1025 | * Data size we actually write: from the start of the block |
| 1026 | * to the start of the data, then count bytes of data, and |
| 1027 | * to the end of the block |
| 1028 | */ |
| 1029 | write_total = ((block_seek + count + blocklen - 1) / |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1030 | blocklen) * blocklen; |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1031 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1032 | |
| 1033 | /* |
| 1034 | * Support data anywhere within erase sectors: read out the complete |
| 1035 | * area to be erased, replace the environment image, write the whole |
| 1036 | * block back again. |
| 1037 | */ |
| 1038 | if (write_total > count) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1039 | data = malloc(erase_len); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1040 | if (!data) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1041 | fprintf(stderr, |
| 1042 | "Cannot malloc %zu bytes: %s\n", |
| 1043 | erase_len, strerror(errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1044 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1045 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1046 | |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1047 | rc = flash_read_buf(dev, fd, data, write_total, erase_offset); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1048 | if (write_total != rc) |
| 1049 | return -1; |
| 1050 | |
Joe Hershberger | a8dd2c6 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 1051 | #ifdef DEBUG |
| 1052 | fprintf(stderr, "Preserving data "); |
| 1053 | if (block_seek != 0) |
| 1054 | fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1); |
| 1055 | if (block_seek + count != write_total) { |
| 1056 | if (block_seek != 0) |
| 1057 | fprintf(stderr, " and "); |
Marcin Niestroj | 2089073 | 2016-05-06 14:58:29 +0200 | [diff] [blame] | 1058 | fprintf(stderr, "0x%lx - 0x%lx", |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1059 | (unsigned long)block_seek + count, |
| 1060 | (unsigned long)write_total - 1); |
Joe Hershberger | a8dd2c6 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 1061 | } |
| 1062 | fprintf(stderr, "\n"); |
| 1063 | #endif |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1064 | /* Overwrite the old environment */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1065 | memcpy(data + block_seek, buf, count); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1066 | } else { |
| 1067 | /* |
| 1068 | * We get here, iff offset is block-aligned and count is a |
| 1069 | * multiple of blocklen - see write_total calculation above |
| 1070 | */ |
| 1071 | data = buf; |
| 1072 | } |
| 1073 | |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1074 | if (DEVTYPE(dev) == MTD_NANDFLASH) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1075 | /* |
| 1076 | * NAND: calculate which blocks we are writing. We have |
| 1077 | * to write one block at a time to skip bad blocks. |
| 1078 | */ |
| 1079 | erasesize = blocklen; |
| 1080 | } else { |
| 1081 | erasesize = erase_len; |
| 1082 | } |
| 1083 | |
| 1084 | erase.length = erasesize; |
| 1085 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1086 | /* This only runs once on NOR flash and SPI-dataflash */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1087 | while (processed < write_total) { |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1088 | rc = flash_bad_block(fd, DEVTYPE(dev), blockstart); |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1089 | if (rc < 0) /* block test failed */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1090 | return rc; |
| 1091 | |
Andreas Fenkart | 2228158 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 1092 | if (blockstart + erasesize > environment_end(dev)) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1093 | fprintf(stderr, "End of range reached, aborting\n"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1094 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1095 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1096 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1097 | if (rc) { /* block is bad */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1098 | blockstart += blocklen; |
| 1099 | continue; |
| 1100 | } |
| 1101 | |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1102 | if (DEVTYPE(dev) != MTD_ABSENT) { |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1103 | erase.start = blockstart; |
Joakim Tjernlund | 85206c0 | 2021-12-08 15:33:11 +0100 | [diff] [blame] | 1104 | was_locked = ioctl(fd, MEMISLOCKED, &erase); |
| 1105 | /* treat any errors as unlocked flash */ |
| 1106 | if (was_locked < 0) |
| 1107 | was_locked = 0; |
Ivan Mikhaylov | 20c21bf | 2020-07-10 19:54:18 +0300 | [diff] [blame] | 1108 | if (was_locked) |
| 1109 | ioctl(fd, MEMUNLOCK, &erase); |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1110 | /* These do not need an explicit erase cycle */ |
Andreas Fenkart | 5e755c7 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 1111 | if (DEVTYPE(dev) != MTD_DATAFLASH) |
Oliver Metz | e9c66ab | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 1112 | if (ioctl(fd, MEMERASE, &erase) != 0) { |
| 1113 | fprintf(stderr, |
| 1114 | "MTD erase error on %s: %s\n", |
| 1115 | DEVNAME(dev), strerror(errno)); |
| 1116 | return -1; |
| 1117 | } |
| 1118 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1119 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1120 | if (lseek(fd, blockstart, SEEK_SET) == -1) { |
| 1121 | fprintf(stderr, |
| 1122 | "Seek error on %s: %s\n", |
| 1123 | DEVNAME(dev), strerror(errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1124 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1125 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1126 | #ifdef DEBUG |
Marcin Niestroj | 2089073 | 2016-05-06 14:58:29 +0200 | [diff] [blame] | 1127 | fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n", |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1128 | (unsigned long long)erasesize, |
| 1129 | (unsigned long long)blockstart); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1130 | #endif |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1131 | if (write(fd, data + processed, erasesize) != erasesize) { |
| 1132 | fprintf(stderr, "Write error on %s: %s\n", |
| 1133 | DEVNAME(dev), strerror(errno)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1134 | return -1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1135 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1136 | |
Ivan Mikhaylov | 20c21bf | 2020-07-10 19:54:18 +0300 | [diff] [blame] | 1137 | if (DEVTYPE(dev) != MTD_ABSENT) { |
| 1138 | if (was_locked) |
| 1139 | ioctl(fd, MEMLOCK, &erase); |
| 1140 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1141 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1142 | processed += erasesize; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1143 | block_seek = 0; |
Dustin Byford | 0983cad | 2014-03-06 20:48:23 -0800 | [diff] [blame] | 1144 | blockstart += erasesize; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | if (write_total > count) |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1148 | free(data); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1149 | |
| 1150 | return processed; |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * Set obsolete flag at offset - NOR flash only |
| 1155 | */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1156 | static int flash_flag_obsolete(int dev, int fd, off_t offset) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1157 | { |
| 1158 | int rc; |
Detlev Zundel | 2111d3e | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 1159 | struct erase_info_user erase; |
Pierre-Jean Texier | d7f9545 | 2019-08-26 13:06:16 +0200 | [diff] [blame] | 1160 | char tmp = ENV_REDUND_OBSOLETE; |
Ivan Mikhaylov | 20c21bf | 2020-07-10 19:54:18 +0300 | [diff] [blame] | 1161 | int was_locked; /* flash lock flag */ |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1162 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1163 | erase.start = DEVOFFSET(dev); |
| 1164 | erase.length = DEVESIZE(dev); |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 1165 | /* This relies on the fact, that ENV_REDUND_OBSOLETE == 0 */ |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1166 | rc = lseek(fd, offset, SEEK_SET); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1167 | if (rc < 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1168 | fprintf(stderr, "Cannot seek to set the flag on %s\n", |
| 1169 | DEVNAME(dev)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1170 | return rc; |
| 1171 | } |
Joakim Tjernlund | 85206c0 | 2021-12-08 15:33:11 +0100 | [diff] [blame] | 1172 | was_locked = ioctl(fd, MEMISLOCKED, &erase); |
| 1173 | /* treat any errors as unlocked flash */ |
| 1174 | if (was_locked < 0) |
| 1175 | was_locked = 0; |
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 | |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1187 | static int flash_write(int fd_current, int fd_target, int dev_target, void *buf, |
| 1188 | size_t count) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1189 | { |
| 1190 | int rc; |
| 1191 | |
| 1192 | switch (environment.flag_scheme) { |
| 1193 | case FLAG_NONE: |
| 1194 | break; |
| 1195 | case FLAG_INCREMENTAL: |
| 1196 | (*environment.flags)++; |
| 1197 | break; |
| 1198 | case FLAG_BOOLEAN: |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 1199 | *environment.flags = ENV_REDUND_ACTIVE; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1200 | break; |
| 1201 | default: |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1202 | fprintf(stderr, "Unimplemented flash scheme %u\n", |
| 1203 | environment.flag_scheme); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1204 | return -1; |
| 1205 | } |
| 1206 | |
| 1207 | #ifdef DEBUG |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1208 | fprintf(stderr, "Writing new environment at 0x%llx on %s\n", |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1209 | DEVOFFSET(dev_target), DEVNAME(dev_target)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1210 | #endif |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 1211 | |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1212 | if (IS_UBI(dev_target)) { |
| 1213 | if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0) |
Martin Hundebøll | 710e9c1 | 2020-11-30 12:11:32 +0100 | [diff] [blame] | 1214 | return -1; |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1215 | return ubi_write(fd_target, buf, count); |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1218 | rc = flash_write_buf(dev_target, fd_target, buf, count); |
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 | |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1237 | static int flash_read(int fd, void *buf, size_t count) |
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 | |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1244 | return ubi_read(fd, buf, count); |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1247 | rc = flash_read_buf(dev_current, fd, buf, count, |
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 | |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1293 | static int flash_io_write(int fd_current, void *buf, size_t count) |
Alex Kiernan | be1e788 | 2018-03-09 12:13:01 +0000 | [diff] [blame] | 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 | |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1324 | rc = flash_write(fd_current, fd_target, dev_target, buf, count); |
Alex Kiernan | be1e788 | 2018-03-09 12:13:01 +0000 | [diff] [blame] | 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 | |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1379 | static int flash_io(int mode, void *buf, size_t count) |
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) { |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1393 | rc = flash_io_write(fd_current, buf, count); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1394 | } else { |
Rafał Miłecki | 840ec19 | 2022-01-12 12:47:05 +0100 | [diff] [blame] | 1395 | rc = flash_read(fd_current, buf, count); |
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; |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1415 | void *buf0 = 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; |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1419 | void *buf1 = 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 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 1423 | if (!opts) |
| 1424 | opts = &default_opts; |
| 1425 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1426 | if (parse_config(opts)) /* should fill envdevices */ |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1427 | return -EINVAL; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1428 | |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1429 | buf0 = calloc(1, CUR_ENVSIZE); |
| 1430 | if (buf0 == NULL) { |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1431 | fprintf(stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1432 | "Not enough memory for environment (%ld bytes)\n", |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1433 | CUR_ENVSIZE); |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1434 | ret = -ENOMEM; |
| 1435 | goto open_cleanup; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1436 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1437 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1438 | dev_current = 0; |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1439 | if (flash_io(O_RDONLY, buf0, CUR_ENVSIZE)) { |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1440 | ret = -EIO; |
| 1441 | goto open_cleanup; |
| 1442 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1443 | |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1444 | if (!have_redund_env) { |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1445 | struct env_image_single *single = buf0; |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1446 | |
| 1447 | crc0 = crc32(0, (uint8_t *)single->data, ENV_SIZE); |
| 1448 | crc0_ok = (crc0 == single->crc); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1449 | if (!crc0_ok) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1450 | fprintf(stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1451 | "Warning: Bad CRC, using default environment\n"); |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1452 | memcpy(single->data, default_environment, |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1453 | sizeof(default_environment)); |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 1454 | environment.dirty = 1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1455 | } |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1456 | |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1457 | environment.image = buf0; |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1458 | environment.crc = &single->crc; |
| 1459 | environment.flags = NULL; |
| 1460 | environment.data = single->data; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1461 | } else { |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1462 | struct env_image_redundant *redundant0 = buf0; |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1463 | struct env_image_redundant *redundant1; |
| 1464 | |
| 1465 | crc0 = crc32(0, (uint8_t *)redundant0->data, ENV_SIZE); |
| 1466 | crc0_ok = (crc0 == redundant0->crc); |
| 1467 | |
| 1468 | flag0 = redundant0->flags; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1469 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1470 | dev_current = 1; |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1471 | buf1 = calloc(1, CUR_ENVSIZE); |
| 1472 | if (buf1 == NULL) { |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1473 | fprintf(stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1474 | "Not enough memory for environment (%ld bytes)\n", |
Joe Hershberger | 671adc4 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1475 | CUR_ENVSIZE); |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1476 | ret = -ENOMEM; |
| 1477 | goto open_cleanup; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1478 | } |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1479 | redundant1 = buf1; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1480 | |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1481 | if (flash_io(O_RDONLY, buf1, CUR_ENVSIZE)) { |
Tom Rini | 4c1192c | 2018-07-01 22:10:33 -0400 | [diff] [blame] | 1482 | ret = -EIO; |
| 1483 | goto open_cleanup; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1484 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1485 | |
| 1486 | /* Check flag scheme compatibility */ |
| 1487 | if (DEVTYPE(dev_current) == MTD_NORFLASH && |
| 1488 | DEVTYPE(!dev_current) == MTD_NORFLASH) { |
| 1489 | environment.flag_scheme = FLAG_BOOLEAN; |
| 1490 | } else if (DEVTYPE(dev_current) == MTD_NANDFLASH && |
| 1491 | DEVTYPE(!dev_current) == MTD_NANDFLASH) { |
| 1492 | environment.flag_scheme = FLAG_INCREMENTAL; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1493 | } else if (DEVTYPE(dev_current) == MTD_DATAFLASH && |
| 1494 | DEVTYPE(!dev_current) == MTD_DATAFLASH) { |
| 1495 | environment.flag_scheme = FLAG_BOOLEAN; |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 1496 | } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME && |
| 1497 | DEVTYPE(!dev_current) == MTD_UBIVOLUME) { |
| 1498 | environment.flag_scheme = FLAG_INCREMENTAL; |
Oliver Metz | e745d4c | 2013-08-30 00:56:01 +0200 | [diff] [blame] | 1499 | } else if (DEVTYPE(dev_current) == MTD_ABSENT && |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1500 | DEVTYPE(!dev_current) == MTD_ABSENT && |
| 1501 | IS_UBI(dev_current) == IS_UBI(!dev_current)) { |
Oliver Metz | e745d4c | 2013-08-30 00:56:01 +0200 | [diff] [blame] | 1502 | environment.flag_scheme = FLAG_INCREMENTAL; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1503 | } else { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1504 | fprintf(stderr, "Incompatible flash types!\n"); |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1505 | ret = -EINVAL; |
| 1506 | goto open_cleanup; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1507 | } |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1508 | |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1509 | crc1 = crc32(0, (uint8_t *)redundant1->data, ENV_SIZE); |
Tom Rini | 4c1192c | 2018-07-01 22:10:33 -0400 | [diff] [blame] | 1510 | |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1511 | crc1_ok = (crc1 == redundant1->crc); |
| 1512 | flag1 = redundant1->flags; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1513 | |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1514 | if (memcmp(redundant0->data, redundant1->data, ENV_SIZE) || |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 1515 | !crc0_ok || !crc1_ok) |
| 1516 | environment.dirty = 1; |
| 1517 | |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1518 | if (crc0_ok && !crc1_ok) { |
| 1519 | dev_current = 0; |
| 1520 | } else if (!crc0_ok && crc1_ok) { |
| 1521 | dev_current = 1; |
| 1522 | } else if (!crc0_ok && !crc1_ok) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1523 | fprintf(stderr, |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1524 | "Warning: Bad CRC, using default environment\n"); |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1525 | memcpy(redundant0->data, default_environment, |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1526 | sizeof(default_environment)); |
Rasmus Villemoes | 06a7964 | 2018-09-27 22:45:26 +0200 | [diff] [blame] | 1527 | environment.dirty = 1; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1528 | dev_current = 0; |
| 1529 | } else { |
| 1530 | switch (environment.flag_scheme) { |
| 1531 | case FLAG_BOOLEAN: |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 1532 | if (flag0 == ENV_REDUND_ACTIVE && |
| 1533 | flag1 == ENV_REDUND_OBSOLETE) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1534 | dev_current = 0; |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 1535 | } else if (flag0 == ENV_REDUND_OBSOLETE && |
| 1536 | flag1 == ENV_REDUND_ACTIVE) { |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1537 | dev_current = 1; |
| 1538 | } else if (flag0 == flag1) { |
| 1539 | dev_current = 0; |
| 1540 | } else if (flag0 == 0xFF) { |
| 1541 | dev_current = 0; |
| 1542 | } else if (flag1 == 0xFF) { |
| 1543 | dev_current = 1; |
| 1544 | } else { |
| 1545 | dev_current = 0; |
| 1546 | } |
| 1547 | break; |
| 1548 | case FLAG_INCREMENTAL: |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1549 | if (flag0 == 255 && flag1 == 0) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1550 | dev_current = 1; |
| 1551 | else if ((flag1 == 255 && flag0 == 0) || |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1552 | flag0 >= flag1) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1553 | dev_current = 0; |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1554 | else /* flag1 > flag0 */ |
Jon Povey | 93ccd47 | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1555 | dev_current = 1; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1556 | break; |
| 1557 | default: |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1558 | fprintf(stderr, "Unknown flag scheme %u\n", |
| 1559 | environment.flag_scheme); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1560 | return -1; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | * If we are reading, we don't need the flag and the CRC any |
| 1566 | * more, if we are writing, we will re-calculate CRC and update |
| 1567 | * flags before writing out |
| 1568 | */ |
| 1569 | if (dev_current) { |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1570 | environment.image = buf1; |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1571 | environment.crc = &redundant1->crc; |
| 1572 | environment.flags = &redundant1->flags; |
| 1573 | environment.data = redundant1->data; |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1574 | free(buf0); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1575 | } else { |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1576 | environment.image = buf0; |
Rafał Miłecki | 02acbf1 | 2022-01-12 12:47:06 +0100 | [diff] [blame] | 1577 | environment.crc = &redundant0->crc; |
| 1578 | environment.flags = &redundant0->flags; |
| 1579 | environment.data = redundant0->data; |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1580 | free(buf1); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1581 | } |
Joe Hershberger | a8dd2c6 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 1582 | #ifdef DEBUG |
| 1583 | fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current)); |
| 1584 | #endif |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1585 | } |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1586 | return 0; |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1587 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1588 | open_cleanup: |
Rafał Miłecki | 260de10 | 2023-12-13 10:23:44 +0100 | [diff] [blame] | 1589 | free(buf0); |
| 1590 | free(buf1); |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1591 | |
| 1592 | return ret; |
| 1593 | } |
| 1594 | |
| 1595 | /* |
| 1596 | * Simply free allocated buffer with environment |
| 1597 | */ |
| 1598 | int fw_env_close(struct env_opts *opts) |
| 1599 | { |
| 1600 | if (environment.image) |
| 1601 | free(environment.image); |
| 1602 | |
| 1603 | environment.image = NULL; |
| 1604 | |
| 1605 | return 0; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1608 | static int check_device_config(int dev) |
| 1609 | { |
| 1610 | struct stat st; |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1611 | int32_t lnum = 0; |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1612 | int fd, rc = 0; |
| 1613 | |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1614 | /* Fills in IS_UBI(), converts DEVNAME() with ubi volume name */ |
| 1615 | ubi_check_dev(dev); |
| 1616 | |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1617 | fd = open(DEVNAME(dev), O_RDONLY); |
| 1618 | if (fd < 0) { |
| 1619 | fprintf(stderr, |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1620 | "Cannot open %s: %s\n", DEVNAME(dev), strerror(errno)); |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1621 | return -1; |
| 1622 | } |
| 1623 | |
| 1624 | rc = fstat(fd, &st); |
| 1625 | if (rc < 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1626 | fprintf(stderr, "Cannot stat the file %s\n", DEVNAME(dev)); |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1627 | goto err; |
| 1628 | } |
| 1629 | |
S. Lockwood-Childs | a1015c2 | 2017-11-14 23:01:26 -0800 | [diff] [blame] | 1630 | if (IS_UBI(dev)) { |
| 1631 | rc = ioctl(fd, UBI_IOCEBISMAP, &lnum); |
| 1632 | if (rc < 0) { |
| 1633 | fprintf(stderr, "Cannot get UBI information for %s\n", |
| 1634 | DEVNAME(dev)); |
| 1635 | goto err; |
| 1636 | } |
| 1637 | } else if (S_ISCHR(st.st_mode)) { |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1638 | struct mtd_info_user mtdinfo; |
| 1639 | rc = ioctl(fd, MEMGETINFO, &mtdinfo); |
| 1640 | if (rc < 0) { |
| 1641 | fprintf(stderr, "Cannot get MTD information for %s\n", |
| 1642 | DEVNAME(dev)); |
| 1643 | goto err; |
| 1644 | } |
| 1645 | if (mtdinfo.type != MTD_NORFLASH && |
| 1646 | mtdinfo.type != MTD_NANDFLASH && |
| 1647 | mtdinfo.type != MTD_DATAFLASH && |
| 1648 | mtdinfo.type != MTD_UBIVOLUME) { |
| 1649 | fprintf(stderr, "Unsupported flash type %u on %s\n", |
| 1650 | mtdinfo.type, DEVNAME(dev)); |
| 1651 | goto err; |
| 1652 | } |
| 1653 | DEVTYPE(dev) = mtdinfo.type; |
Rasmus Villemoes | faf2998 | 2020-03-24 13:57:40 +0100 | [diff] [blame] | 1654 | if (DEVESIZE(dev) == 0 && ENVSECTORS(dev) == 0 && |
| 1655 | mtdinfo.type == MTD_NORFLASH) |
| 1656 | DEVESIZE(dev) = mtdinfo.erasesize; |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1657 | if (DEVESIZE(dev) == 0) |
| 1658 | /* Assume the erase size is the same as the env-size */ |
| 1659 | DEVESIZE(dev) = ENVSIZE(dev); |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1660 | } else { |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1661 | uint64_t size; |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1662 | DEVTYPE(dev) = MTD_ABSENT; |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1663 | if (DEVESIZE(dev) == 0) |
| 1664 | /* Assume the erase size to be 512 bytes */ |
| 1665 | DEVESIZE(dev) = 0x200; |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1666 | |
| 1667 | /* |
| 1668 | * Check for negative offsets, treat it as backwards offset |
| 1669 | * from the end of the block device |
| 1670 | */ |
| 1671 | if (DEVOFFSET(dev) < 0) { |
| 1672 | rc = ioctl(fd, BLKGETSIZE64, &size); |
| 1673 | if (rc < 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1674 | fprintf(stderr, |
| 1675 | "Could not get block device size on %s\n", |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1676 | DEVNAME(dev)); |
| 1677 | goto err; |
| 1678 | } |
| 1679 | |
| 1680 | DEVOFFSET(dev) = DEVOFFSET(dev) + size; |
| 1681 | #ifdef DEBUG |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1682 | fprintf(stderr, |
| 1683 | "Calculated device offset 0x%llx on %s\n", |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1684 | DEVOFFSET(dev), DEVNAME(dev)); |
| 1685 | #endif |
| 1686 | } |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1689 | if (ENVSECTORS(dev) == 0) |
| 1690 | /* Assume enough sectors to cover the environment */ |
| 1691 | ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev)); |
| 1692 | |
| 1693 | if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1694 | fprintf(stderr, |
| 1695 | "Environment does not start on (erase) block boundary\n"); |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1696 | errno = EINVAL; |
| 1697 | return -1; |
| 1698 | } |
| 1699 | |
| 1700 | if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) { |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1701 | fprintf(stderr, |
| 1702 | "Environment does not fit into available sectors\n"); |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1703 | errno = EINVAL; |
| 1704 | return -1; |
| 1705 | } |
| 1706 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1707 | err: |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1708 | close(fd); |
| 1709 | return rc; |
| 1710 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1711 | |
Rafał Miłecki | 326d590 | 2022-06-16 20:59:03 +0200 | [diff] [blame] | 1712 | static int find_nvmem_device(void) |
| 1713 | { |
| 1714 | const char *path = "/sys/bus/nvmem/devices"; |
| 1715 | struct dirent *dent; |
| 1716 | char *nvmem = NULL; |
| 1717 | char comp[256]; |
| 1718 | char buf[32]; |
| 1719 | int bytes; |
| 1720 | DIR *dir; |
| 1721 | |
| 1722 | dir = opendir(path); |
| 1723 | if (!dir) { |
| 1724 | return -EIO; |
| 1725 | } |
| 1726 | |
| 1727 | while (!nvmem && (dent = readdir(dir))) { |
Rafał Miłecki | a2c5291 | 2023-12-12 18:33:27 +0100 | [diff] [blame] | 1728 | struct stat s; |
Rafał Miłecki | 326d590 | 2022-06-16 20:59:03 +0200 | [diff] [blame] | 1729 | FILE *fp; |
Jaehoon Chung | 18ca449 | 2022-10-13 17:41:21 +0900 | [diff] [blame] | 1730 | size_t size; |
Rafał Miłecki | 326d590 | 2022-06-16 20:59:03 +0200 | [diff] [blame] | 1731 | |
| 1732 | if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) { |
| 1733 | continue; |
| 1734 | } |
| 1735 | |
| 1736 | bytes = snprintf(comp, sizeof(comp), "%s/%s/of_node/compatible", path, dent->d_name); |
| 1737 | if (bytes < 0 || bytes == sizeof(comp)) { |
| 1738 | continue; |
| 1739 | } |
| 1740 | |
| 1741 | fp = fopen(comp, "r"); |
| 1742 | if (!fp) { |
| 1743 | continue; |
| 1744 | } |
| 1745 | |
Rafał Miłecki | a2c5291 | 2023-12-12 18:33:27 +0100 | [diff] [blame] | 1746 | if (fstat(fileno(fp), &s)) { |
| 1747 | fprintf(stderr, "Failed to fstat %s\n", comp); |
| 1748 | goto next; |
| 1749 | } |
| 1750 | |
| 1751 | if (s.st_size >= sizeof(buf)) { |
| 1752 | goto next; |
| 1753 | } |
| 1754 | |
| 1755 | size = fread(buf, s.st_size, 1, fp); |
Jaehoon Chung | 18ca449 | 2022-10-13 17:41:21 +0900 | [diff] [blame] | 1756 | if (size != 1) { |
| 1757 | fprintf(stderr, |
| 1758 | "read failed about %s\n", comp); |
Rafał Miłecki | a2c5291 | 2023-12-12 18:33:27 +0100 | [diff] [blame] | 1759 | goto next; |
Jaehoon Chung | 18ca449 | 2022-10-13 17:41:21 +0900 | [diff] [blame] | 1760 | } |
Rafał Miłecki | a2c5291 | 2023-12-12 18:33:27 +0100 | [diff] [blame] | 1761 | buf[s.st_size] = '\0'; |
Rafał Miłecki | 326d590 | 2022-06-16 20:59:03 +0200 | [diff] [blame] | 1762 | |
| 1763 | if (!strcmp(buf, "u-boot,env")) { |
| 1764 | bytes = asprintf(&nvmem, "%s/%s/nvmem", path, dent->d_name); |
| 1765 | if (bytes < 0) { |
| 1766 | nvmem = NULL; |
| 1767 | } |
| 1768 | } |
| 1769 | |
Rafał Miłecki | a2c5291 | 2023-12-12 18:33:27 +0100 | [diff] [blame] | 1770 | next: |
Rafał Miłecki | 326d590 | 2022-06-16 20:59:03 +0200 | [diff] [blame] | 1771 | fclose(fp); |
| 1772 | } |
| 1773 | |
| 1774 | closedir(dir); |
| 1775 | |
| 1776 | if (nvmem) { |
| 1777 | struct stat s; |
| 1778 | |
| 1779 | stat(nvmem, &s); |
| 1780 | |
| 1781 | DEVNAME(0) = nvmem; |
| 1782 | DEVOFFSET(0) = 0; |
| 1783 | ENVSIZE(0) = s.st_size; |
| 1784 | |
| 1785 | return 0; |
| 1786 | } |
| 1787 | |
| 1788 | return -ENOENT; |
| 1789 | } |
| 1790 | |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1791 | static int parse_config(struct env_opts *opts) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1792 | { |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1793 | int rc; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1794 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 1795 | if (!opts) |
| 1796 | opts = &default_opts; |
Anatolij Gustschin | e89ead3 | 2016-04-29 22:00:11 +0200 | [diff] [blame] | 1797 | |
Andreas Fenkart | e651130 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 1798 | #if defined(CONFIG_FILE) |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1799 | /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1800 | if (get_config(opts->config_file)) { |
Rafał Miłecki | 326d590 | 2022-06-16 20:59:03 +0200 | [diff] [blame] | 1801 | if (find_nvmem_device()) { |
| 1802 | fprintf(stderr, "Cannot parse config file '%s': %m\n", |
| 1803 | opts->config_file); |
| 1804 | fprintf(stderr, "Failed to find NVMEM device\n"); |
| 1805 | return -1; |
| 1806 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1807 | } |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1808 | #else |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1809 | DEVNAME(0) = DEVICE1_NAME; |
| 1810 | DEVOFFSET(0) = DEVICE1_OFFSET; |
| 1811 | ENVSIZE(0) = ENV1_SIZE; |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1812 | |
| 1813 | /* Set defaults for DEVESIZE, ENVSECTORS later once we |
| 1814 | * know DEVTYPE |
| 1815 | */ |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1816 | #ifdef DEVICE1_ESIZE |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1817 | DEVESIZE(0) = DEVICE1_ESIZE; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1818 | #endif |
| 1819 | #ifdef DEVICE1_ENVSECTORS |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1820 | ENVSECTORS(0) = DEVICE1_ENVSECTORS; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1821 | #endif |
| 1822 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1823 | #ifdef HAVE_REDUND |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1824 | DEVNAME(1) = DEVICE2_NAME; |
| 1825 | DEVOFFSET(1) = DEVICE2_OFFSET; |
| 1826 | ENVSIZE(1) = ENV2_SIZE; |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1827 | |
| 1828 | /* Set defaults for DEVESIZE, ENVSECTORS later once we |
| 1829 | * know DEVTYPE |
| 1830 | */ |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1831 | #ifdef DEVICE2_ESIZE |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1832 | DEVESIZE(1) = DEVICE2_ESIZE; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1833 | #endif |
| 1834 | #ifdef DEVICE2_ENVSECTORS |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1835 | ENVSECTORS(1) = DEVICE2_ENVSECTORS; |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1836 | #endif |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1837 | have_redund_env = 1; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1838 | #endif |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1839 | #endif |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1840 | rc = check_device_config(0); |
| 1841 | if (rc < 0) |
| 1842 | return rc; |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1843 | |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1844 | if (have_redund_env) { |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1845 | rc = check_device_config(1); |
| 1846 | if (rc < 0) |
| 1847 | return rc; |
Andreas Fenkart | b8bf404 | 2016-04-19 22:43:42 +0200 | [diff] [blame] | 1848 | |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1849 | if (ENVSIZE(0) != ENVSIZE(1)) { |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1850 | fprintf(stderr, |
Philip Molloy | f60f751 | 2019-03-31 03:44:57 +0000 | [diff] [blame] | 1851 | "Redundant environments have unequal size\n"); |
Andreas Fenkart | 31f2e05 | 2016-08-17 23:41:53 +0200 | [diff] [blame] | 1852 | return -1; |
Stefan Agner | 21dbd6a | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1853 | } |
Andreas Fenkart | b8bf404 | 2016-04-19 22:43:42 +0200 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | usable_envsize = CUR_ENVSIZE - sizeof(uint32_t); |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1857 | if (have_redund_env) |
Andreas Fenkart | b8bf404 | 2016-04-19 22:43:42 +0200 | [diff] [blame] | 1858 | usable_envsize -= sizeof(char); |
| 1859 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1860 | return 0; |
| 1861 | } |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1862 | |
| 1863 | #if defined(CONFIG_FILE) |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1864 | static int get_config(char *fname) |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1865 | { |
| 1866 | FILE *fp; |
| 1867 | int i = 0; |
| 1868 | int rc; |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 1869 | char *line = NULL; |
| 1870 | size_t linesize = 0; |
Tom Rini | 313e912 | 2014-03-28 12:03:33 -0400 | [diff] [blame] | 1871 | char *devname; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1872 | |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1873 | fp = fopen(fname, "r"); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1874 | if (fp == NULL) |
| 1875 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1876 | |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 1877 | while (i < 2 && getline(&line, &linesize, fp) != -1) { |
| 1878 | /* Skip comment strings */ |
| 1879 | if (line[0] == '#') |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1880 | continue; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1881 | |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 1882 | rc = sscanf(line, "%ms %lli %lx %lx %lx", |
Stefan Agner | b21e602 | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1883 | &devname, |
| 1884 | &DEVOFFSET(i), |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1885 | &ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i)); |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1886 | |
Remy Bohmer | cb969eb | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1887 | if (rc < 3) |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1888 | continue; |
| 1889 | |
Tom Rini | 313e912 | 2014-03-28 12:03:33 -0400 | [diff] [blame] | 1890 | DEVNAME(i) = devname; |
| 1891 | |
Max Krummenacher | 6c44f54 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1892 | /* Set defaults for DEVESIZE, ENVSECTORS later once we |
| 1893 | * know DEVTYPE |
| 1894 | */ |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1895 | |
| 1896 | i++; |
| 1897 | } |
Alex Kiernan | 66df4e2 | 2018-06-07 12:20:05 +0000 | [diff] [blame] | 1898 | free(line); |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1899 | fclose(fp); |
wdenk | 3f9ab98 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1900 | |
Alex Kiernan | f46d0ca | 2018-03-09 12:13:00 +0000 | [diff] [blame] | 1901 | have_redund_env = i - 1; |
Alex Kiernan | e9c6220 | 2018-03-09 12:12:59 +0000 | [diff] [blame] | 1902 | if (!i) { /* No valid entries found */ |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1903 | errno = EINVAL; |
Guennadi Liakhovetski | ffb379a | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1904 | return -1; |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1905 | } else |
| 1906 | return 0; |
| 1907 | } |
| 1908 | #endif |