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