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