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