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