blob: c251e2e6ba7d1b3513a9b162c44c59953957a6a2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00002/*
Detlev Zundel2111d3e2010-07-30 11:22:15 +02003 * (C) Copyright 2000-2010
wdenk34b613e2002-12-17 01:51:00 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02006 * (C) Copyright 2008
7 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
wdenk34b613e2002-12-17 01:51:00 +00008 */
9
Jörg Krause983487f2015-04-22 21:36:22 +020010#define _GNU_SOURCE
11
Peter Robinson656ee042015-12-09 07:15:33 +000012#include <compiler.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060013#include <env.h>
wdenk34b613e2002-12-17 01:51:00 +000014#include <errno.h>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060015#include <env_flags.h>
wdenk34b613e2002-12-17 01:51:00 +000016#include <fcntl.h>
Alex Kiernan7150aaa2018-03-09 12:13:02 +000017#include <libgen.h>
Stefan Agnerb21e6022016-07-13 17:14:38 -070018#include <linux/fs.h>
Joe Hershbergere79a91f2012-10-12 10:23:37 +000019#include <linux/stringify.h>
Andreas Fenkartd0e12cb2016-03-11 09:39:35 +010020#include <ctype.h>
wdenk34b613e2002-12-17 01:51:00 +000021#include <stdio.h>
22#include <stdlib.h>
23#include <stddef.h>
24#include <string.h>
25#include <sys/types.h>
26#include <sys/ioctl.h>
27#include <sys/stat.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070028#include <u-boot/crc.h>
wdenk34b613e2002-12-17 01:51:00 +000029#include <unistd.h>
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080030#include <dirent.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010031
32#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020033# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010034# include <linux/mtd/mtd.h>
35#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010036# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010037# include <mtd/mtd-user.h>
38#endif
wdenk34b613e2002-12-17 01:51:00 +000039
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080040#include <mtd/ubi-user.h>
41
Stefano Babic831c4d52017-04-05 18:08:01 +020042#include "fw_env_private.h"
Markus Klotzbücher94db5572007-11-27 10:23:20 +010043#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000044
Andreas Fenkarte6511302016-05-31 09:21:56 +020045struct env_opts default_opts = {
46#ifdef CONFIG_FILE
47 .config_file = CONFIG_FILE
48#endif
49};
50
Marek Vasut05ed2a52014-03-05 19:59:52 +010051#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
52
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020053#define min(x, y) ({ \
54 typeof(x) _min1 = (x); \
55 typeof(y) _min2 = (y); \
56 (void) (&_min1 == &_min2); \
57 _min1 < _min2 ? _min1 : _min2; })
58
59struct envdev_s {
Tom Rini313e9122014-03-28 12:03:33 -040060 const char *devname; /* Device name */
Stefan Agnerb21e6022016-07-13 17:14:38 -070061 long long devoff; /* Device offset */
wdenk3f9ab982003-04-12 23:38:12 +000062 ulong env_size; /* environment size */
63 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020064 ulong env_sectors; /* number of environment sectors */
65 uint8_t mtd_type; /* type of the MTD device */
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080066 int is_ubi; /* set if we use UBI volume */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020067};
wdenk34b613e2002-12-17 01:51:00 +000068
Alex Kiernane9c62202018-03-09 12:12:59 +000069static struct envdev_s envdevices[2] = {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020070 {
71 .mtd_type = MTD_ABSENT,
72 }, {
73 .mtd_type = MTD_ABSENT,
74 },
75};
Alex Kiernane9c62202018-03-09 12:12:59 +000076
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020077static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000078
79#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000080#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000081#define ENVSIZE(i) envdevices[(i)].env_size
82#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020083#define ENVSECTORS(i) envdevices[(i)].env_sectors
84#define DEVTYPE(i) envdevices[(i)].mtd_type
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080085#define IS_UBI(i) envdevices[(i)].is_ubi
wdenk34b613e2002-12-17 01:51:00 +000086
Joe Hershberger671adc42012-10-03 09:38:46 +000087#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000088
Andreas Fenkartb8bf4042016-04-19 22:43:42 +020089static unsigned long usable_envsize;
90#define ENV_SIZE usable_envsize
wdenk34b613e2002-12-17 01:51:00 +000091
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020092struct env_image_single {
Alex Kiernane9c62202018-03-09 12:12:59 +000093 uint32_t crc; /* CRC32 over data bytes */
94 char data[];
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020095};
wdenk34b613e2002-12-17 01:51:00 +000096
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020097struct env_image_redundant {
Alex Kiernane9c62202018-03-09 12:12:59 +000098 uint32_t crc; /* CRC32 over data bytes */
99 unsigned char flags; /* active or obsolete */
100 char data[];
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200101};
102
103enum flag_scheme {
104 FLAG_NONE,
105 FLAG_BOOLEAN,
106 FLAG_INCREMENTAL,
107};
108
109struct environment {
Alex Kiernane9c62202018-03-09 12:12:59 +0000110 void *image;
111 uint32_t *crc;
112 unsigned char *flags;
113 char *data;
114 enum flag_scheme flag_scheme;
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200115 int dirty;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200116};
117
118static struct environment environment = {
119 .flag_scheme = FLAG_NONE,
120};
wdenk34b613e2002-12-17 01:51:00 +0000121
Alex Kiernanf46d0ca2018-03-09 12:13:00 +0000122static int have_redund_env;
wdenke7f34c62003-01-11 09:48:40 +0000123
Joe Hershberger81321142012-10-12 08:48:51 +0000124#define DEFAULT_ENV_INSTANCE_STATIC
125#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000126
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -0800127#define UBI_DEV_START "/dev/ubi"
128#define UBI_SYSFS "/sys/class/ubi"
129#define UBI_VOL_NAME_PATT "ubi%d_%d"
130
131static int is_ubi_devname(const char *devname)
132{
133 return !strncmp(devname, UBI_DEV_START, sizeof(UBI_DEV_START) - 1);
134}
135
136static int ubi_check_volume_sysfs_name(const char *volume_sysfs_name,
137 const char *volname)
138{
139 char path[256];
140 FILE *file;
141 char *name;
142 int ret;
143
144 strcpy(path, UBI_SYSFS "/");
145 strcat(path, volume_sysfs_name);
146 strcat(path, "/name");
147
148 file = fopen(path, "r");
149 if (!file)
150 return -1;
151
152 ret = fscanf(file, "%ms", &name);
153 fclose(file);
154 if (ret <= 0 || !name) {
155 fprintf(stderr,
156 "Failed to read from file %s, ret = %d, name = %s\n",
157 path, ret, name);
158 return -1;
159 }
160
161 if (!strcmp(name, volname)) {
162 free(name);
163 return 0;
164 }
165 free(name);
166
167 return -1;
168}
169
170static int ubi_get_volnum_by_name(int devnum, const char *volname)
171{
172 DIR *sysfs_ubi;
173 struct dirent *dirent;
174 int ret;
175 int tmp_devnum;
176 int volnum;
177
178 sysfs_ubi = opendir(UBI_SYSFS);
179 if (!sysfs_ubi)
180 return -1;
181
182#ifdef DEBUG
183 fprintf(stderr, "Looking for volume name \"%s\"\n", volname);
184#endif
185
186 while (1) {
187 dirent = readdir(sysfs_ubi);
188 if (!dirent)
189 return -1;
190
191 ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT,
192 &tmp_devnum, &volnum);
193 if (ret == 2 && devnum == tmp_devnum) {
194 if (ubi_check_volume_sysfs_name(dirent->d_name,
Miaoqian Lin64f2dda2022-09-19 08:28:09 +0400195 volname) == 0) {
196 closedir(sysfs_ubi);
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -0800197 return volnum;
Miaoqian Lin64f2dda2022-09-19 08:28:09 +0400198 }
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -0800199 }
200 }
Miaoqian Lin64f2dda2022-09-19 08:28:09 +0400201 closedir(sysfs_ubi);
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -0800202
203 return -1;
204}
205
206static int ubi_get_devnum_by_devname(const char *devname)
207{
208 int devnum;
209 int ret;
210
211 ret = sscanf(devname + sizeof(UBI_DEV_START) - 1, "%d", &devnum);
212 if (ret != 1)
213 return -1;
214
215 return devnum;
216}
217
218static const char *ubi_get_volume_devname(const char *devname,
219 const char *volname)
220{
221 char *volume_devname;
222 int volnum;
223 int devnum;
224 int ret;
225
226 devnum = ubi_get_devnum_by_devname(devname);
227 if (devnum < 0)
228 return NULL;
229
230 volnum = ubi_get_volnum_by_name(devnum, volname);
231 if (volnum < 0)
232 return NULL;
233
234 ret = asprintf(&volume_devname, "%s_%d", devname, volnum);
235 if (ret < 0)
236 return NULL;
237
238#ifdef DEBUG
239 fprintf(stderr, "Found ubi volume \"%s:%s\" -> %s\n",
240 devname, volname, volume_devname);
241#endif
242
243 return volume_devname;
244}
245
246static void ubi_check_dev(unsigned int dev_id)
247{
248 char *devname = (char *)DEVNAME(dev_id);
249 char *pname;
250 const char *volname = NULL;
251 const char *volume_devname;
252
253 if (!is_ubi_devname(DEVNAME(dev_id)))
254 return;
255
256 IS_UBI(dev_id) = 1;
257
258 for (pname = devname; *pname != '\0'; pname++) {
259 if (*pname == ':') {
260 *pname = '\0';
261 volname = pname + 1;
262 break;
263 }
264 }
265
266 if (volname) {
267 /* Let's find real volume device name */
268 volume_devname = ubi_get_volume_devname(devname, volname);
269 if (!volume_devname) {
270 fprintf(stderr, "Didn't found ubi volume \"%s\"\n",
271 volname);
272 return;
273 }
274
275 free(devname);
276 DEVNAME(dev_id) = volume_devname;
277 }
278}
279
280static int ubi_update_start(int fd, int64_t bytes)
281{
282 if (ioctl(fd, UBI_IOCVOLUP, &bytes))
283 return -1;
284 return 0;
285}
286
287static int ubi_read(int fd, void *buf, size_t count)
288{
289 ssize_t ret;
290
291 while (count > 0) {
292 ret = read(fd, buf, count);
293 if (ret > 0) {
294 count -= ret;
295 buf += ret;
296
297 continue;
298 }
299
300 if (ret == 0) {
301 /*
302 * Happens in case of too short volume data size. If we
303 * return error status we will fail it will be treated
304 * as UBI device error.
305 *
306 * Leave catching this error to CRC check.
307 */
308 fprintf(stderr, "Warning: end of data on ubi volume\n");
309 return 0;
310 } else if (errno == EBADF) {
311 /*
312 * Happens in case of corrupted volume. The same as
313 * above, we cannot return error now, as we will still
314 * be able to successfully write environment later.
315 */
316 fprintf(stderr, "Warning: corrupted volume?\n");
317 return 0;
318 } else if (errno == EINTR) {
319 continue;
320 }
321
322 fprintf(stderr, "Cannot read %u bytes from ubi volume, %s\n",
323 (unsigned int)count, strerror(errno));
324 return -1;
325 }
326
327 return 0;
328}
329
330static int ubi_write(int fd, const void *buf, size_t count)
331{
332 ssize_t ret;
333
334 while (count > 0) {
335 ret = write(fd, buf, count);
336 if (ret <= 0) {
337 if (ret < 0 && errno == EINTR)
338 continue;
339
340 fprintf(stderr, "Cannot write %u bytes to ubi volume\n",
341 (unsigned int)count);
342 return -1;
343 }
344
345 count -= ret;
346 buf += ret;
347 }
348
349 return 0;
350}
351
Rafał Miłecki840ec192022-01-12 12:47:05 +0100352static int flash_io(int mode, void *buf, size_t count);
Andreas Fenkart24371902016-04-05 23:13:42 +0200353static int parse_config(struct env_opts *opts);
wdenk3f9ab982003-04-12 23:38:12 +0000354
wdenke7f34c62003-01-11 09:48:40 +0000355#if defined(CONFIG_FILE)
Alex Kiernane9c62202018-03-09 12:12:59 +0000356static int get_config(char *);
wdenke7f34c62003-01-11 09:48:40 +0000357#endif
wdenk34b613e2002-12-17 01:51:00 +0000358
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100359static char *skip_chars(char *s)
Stefano Babicada628b2010-05-24 12:08:16 +0200360{
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100361 for (; *s != '\0'; s++) {
Stefan Agner87833242018-03-01 14:06:32 +0100362 if (isblank(*s) || *s == '=')
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100363 return s;
364 }
365 return NULL;
366}
Stefano Babicada628b2010-05-24 12:08:16 +0200367
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100368static char *skip_blanks(char *s)
369{
370 for (; *s != '\0'; s++) {
371 if (!isblank(*s))
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100372 return s;
Stefano Babicada628b2010-05-24 12:08:16 +0200373 }
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100374 return NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200375}
376
wdenk34b613e2002-12-17 01:51:00 +0000377/*
Andreas Fenkart3ff10742016-07-16 17:06:14 +0200378 * s1 is either a simple 'name', or a 'name=value' pair.
379 * s2 is a 'name=value' pair.
380 * If the names match, return the value of s2, else NULL.
381 */
382static char *envmatch(char *s1, char *s2)
383{
384 if (s1 == NULL || s2 == NULL)
385 return NULL;
386
387 while (*s1 == *s2++)
388 if (*s1++ == '=')
389 return s2;
390 if (*s1 == '\0' && *(s2 - 1) == '=')
391 return s2;
392 return NULL;
393}
394
395/**
wdenk34b613e2002-12-17 01:51:00 +0000396 * Search the environment for a variable.
397 * Return the value, if found, or NULL, if not found.
398 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000399char *fw_getenv(char *name)
wdenk34b613e2002-12-17 01:51:00 +0000400{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100401 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000402
wdenk3f9ab982003-04-12 23:38:12 +0000403 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100404 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000405
wdenk3f9ab982003-04-12 23:38:12 +0000406 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000407 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000408 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000409 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200410 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000411 }
412 }
Alex Kiernane9c62202018-03-09 12:12:59 +0000413 val = envmatch(name, env);
wdenk34b613e2002-12-17 01:51:00 +0000414 if (!val)
415 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200416 return val;
wdenk34b613e2002-12-17 01:51:00 +0000417 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200418 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000419}
420
421/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600422 * Search the default environment for a variable.
423 * Return the value, if found, or NULL, if not found.
424 */
425char *fw_getdefenv(char *name)
426{
427 char *env, *nxt;
428
429 for (env = default_environment; *env; env = nxt + 1) {
430 char *val;
431
432 for (nxt = env; *nxt; ++nxt) {
433 if (nxt >= &default_environment[ENV_SIZE]) {
434 fprintf(stderr, "## Error: "
435 "default environment not terminated\n");
436 return NULL;
437 }
438 }
439 val = envmatch(name, env);
440 if (!val)
441 continue;
442 return val;
443 }
444 return NULL;
445}
446
447/*
wdenk34b613e2002-12-17 01:51:00 +0000448 * Print the current definition of one, or more, or all
449 * environment variables
450 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200451int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000452{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100453 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000454
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200455 if (value_only && argc != 1) {
456 fprintf(stderr,
Alex Kiernan688cee92018-02-11 17:16:46 +0000457 "## Error: `-n'/`--noheader' option requires exactly one argument\n");
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200458 return -1;
459 }
460
Andreas Fenkarte6511302016-05-31 09:21:56 +0200461 if (!opts)
462 opts = &default_opts;
463
Andreas Fenkart24371902016-04-05 23:13:42 +0200464 if (fw_env_open(opts))
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200465 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000466
Alex Kiernane9c62202018-03-09 12:12:59 +0000467 if (argc == 0) { /* Print all env variables */
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200468 char *env, *nxt;
wdenk3f9ab982003-04-12 23:38:12 +0000469 for (env = environment.data; *env; env = nxt + 1) {
470 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000471 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000472 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000473 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200474 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000475 }
476 }
477
Alex Kiernane9c62202018-03-09 12:12:59 +0000478 printf("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000479 }
Stefano Babic4b0a2932017-04-05 18:08:03 +0200480 fw_env_close(opts);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200481 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000482 }
483
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200484 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100485 char *name = argv[i];
486 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000487
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200488 val = fw_getenv(name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700489 if (!val) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000490 fprintf(stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700491 rc = -1;
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200492 continue;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700493 }
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200494
495 if (value_only) {
496 puts(val);
497 break;
498 }
499
500 printf("%s=%s\n", name, val);
wdenk34b613e2002-12-17 01:51:00 +0000501 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700502
Stefano Babic4b0a2932017-04-05 18:08:03 +0200503 fw_env_close(opts);
504
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200505 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000506}
507
Stefano Babic4b0a2932017-04-05 18:08:03 +0200508int fw_env_flush(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000509{
Andreas Fenkarte6511302016-05-31 09:21:56 +0200510 if (!opts)
511 opts = &default_opts;
512
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200513 if (!environment.dirty)
514 return 0;
515
Stefano Babicada628b2010-05-24 12:08:16 +0200516 /*
517 * Update CRC
518 */
519 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000520
Stefano Babicada628b2010-05-24 12:08:16 +0200521 /* write environment back to flash */
Rafał Miłecki840ec192022-01-12 12:47:05 +0100522 if (flash_io(O_RDWR, environment.image, CUR_ENVSIZE)) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000523 fprintf(stderr, "Error: can't write fw_env to flash\n");
524 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000525 }
526
Stefano Babicada628b2010-05-24 12:08:16 +0200527 return 0;
528}
wdenk34b613e2002-12-17 01:51:00 +0000529
Stefano Babicada628b2010-05-24 12:08:16 +0200530/*
531 * Set/Clear a single variable in the environment.
532 * This is called in sequence to update the environment
533 * in RAM without updating the copy in flash after each set
534 */
535int fw_env_write(char *name, char *value)
536{
537 int len;
538 char *env, *nxt;
539 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600540 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000541
542 /*
543 * search if variable with this name already exists
544 */
wdenk3f9ab982003-04-12 23:38:12 +0000545 for (nxt = env = environment.data; *env; env = nxt + 1) {
546 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000547 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200548 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000549 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200550 errno = EINVAL;
551 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000552 }
553 }
Alex Kiernane9c62202018-03-09 12:12:59 +0000554 oldval = envmatch(name, env);
555 if (oldval)
wdenk34b613e2002-12-17 01:51:00 +0000556 break;
557 }
558
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600559 deleting = (oldval && !(value && strlen(value)));
560 creating = (!oldval && (value && strlen(value)));
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200561 overwriting = (oldval && (value && strlen(value) &&
562 strcmp(oldval, value)));
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600563
564 /* check for permission */
565 if (deleting) {
566 if (env_flags_validate_varaccess(name,
567 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
568 printf("Can't delete \"%s\"\n", name);
569 errno = EROFS;
570 return -1;
571 }
572 } else if (overwriting) {
573 if (env_flags_validate_varaccess(name,
574 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
575 printf("Can't overwrite \"%s\"\n", name);
576 errno = EROFS;
577 return -1;
578 } else if (env_flags_validate_varaccess(name,
Alex Kiernane9c62202018-03-09 12:12:59 +0000579 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600580 const char *defval = fw_getdefenv(name);
581
582 if (defval == NULL)
583 defval = "";
584 if (strcmp(oldval, defval)
585 != 0) {
586 printf("Can't overwrite \"%s\"\n", name);
587 errno = EROFS;
588 return -1;
589 }
590 }
591 } else if (creating) {
592 if (env_flags_validate_varaccess(name,
593 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
594 printf("Can't create \"%s\"\n", name);
595 errno = EROFS;
596 return -1;
597 }
598 } else
599 /* Nothing to do */
600 return 0;
601
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200602 environment.dirty = 1;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600603 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000604 if (*++nxt == '\0') {
605 *env = '\0';
606 } else {
607 for (;;) {
608 *env = *nxt++;
609 if ((*env == '\0') && (*nxt == '\0'))
610 break;
611 ++env;
612 }
613 }
614 *++env = '\0';
615 }
616
617 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200618 if (!value || !strlen(value))
619 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000620
621 /*
622 * Append new definition at the end
623 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000624 for (env = environment.data; *env || *(env + 1); ++env)
625 ;
wdenk34b613e2002-12-17 01:51:00 +0000626 if (env > environment.data)
627 ++env;
628 /*
629 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000630 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000631 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000632 len = strlen(name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000633 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200634 len += strlen(value) + 1;
635
wdenk3f9ab982003-04-12 23:38:12 +0000636 if (len > (&environment.data[ENV_SIZE] - env)) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000637 fprintf(stderr,
638 "Error: environment overflow, \"%s\" deleted\n", name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200639 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000640 }
Stefano Babicada628b2010-05-24 12:08:16 +0200641
wdenk34b613e2002-12-17 01:51:00 +0000642 while ((*env = *name++) != '\0')
643 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200644 *env = '=';
645 while ((*++env = *value++) != '\0')
646 ;
647
648 /* end is marked with double '\0' */
649 *++env = '\0';
650
651 return 0;
652}
653
654/*
655 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
656 * 0 - OK
657 * EINVAL - need at least 1 argument
658 * EROFS - certain variables ("ethaddr", "serial#") cannot be
659 * modified or deleted
660 *
661 */
Simon Glass6a38e412017-08-03 12:22:09 -0600662int fw_env_set(int argc, char *argv[], struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200663{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100664 int i;
Mike Frysinger178ec772012-11-10 19:47:45 +0000665 size_t len;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100666 char *name, **valv;
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200667 char *oldval;
Stefano Babicada628b2010-05-24 12:08:16 +0200668 char *value = NULL;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100669 int valc;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200670 int ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200671
Andreas Fenkarte6511302016-05-31 09:21:56 +0200672 if (!opts)
673 opts = &default_opts;
674
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100675 if (argc < 1) {
676 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasut05ed2a52014-03-05 19:59:52 +0100677 errno = EINVAL;
678 return -1;
679 }
680
Andreas Fenkart24371902016-04-05 23:13:42 +0200681 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200682 fprintf(stderr, "Error: environment not initialized\n");
683 return -1;
684 }
685
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100686 name = argv[0];
687 valv = argv + 1;
688 valc = argc - 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200689
Stefano Babic4b0a2932017-04-05 18:08:03 +0200690 if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
691 fw_env_close(opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200692 return -1;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200693 }
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600694
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000695 len = 0;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100696 for (i = 0; i < valc; ++i) {
697 char *val = valv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000698 size_t val_len = strlen(val);
699
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000700 if (value)
701 value[len - 1] = ' ';
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200702 oldval = value;
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000703 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200704 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000705 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200706 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000707 len, strerror(errno));
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200708 free(oldval);
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000709 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200710 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000711
712 memcpy(value + len, val, val_len);
713 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000714 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000715 }
716
Stefano Babicada628b2010-05-24 12:08:16 +0200717 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000718
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000719 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000720
Stefano Babic4b0a2932017-04-05 18:08:03 +0200721 ret = fw_env_flush(opts);
722 fw_env_close(opts);
723
724 return ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200725}
wdenk34b613e2002-12-17 01:51:00 +0000726
Stefano Babicada628b2010-05-24 12:08:16 +0200727/*
728 * Parse a file and configure the u-boot variables.
729 * The script file has a very simple format, as follows:
730 *
731 * Each line has a couple with name, value:
732 * <white spaces>variable_name<white spaces>variable_value
733 *
734 * Both variable_name and variable_value are interpreted as strings.
735 * Any character after <white spaces> and before ending \r\n is interpreted
736 * as variable's value (no comment allowed on these lines !)
737 *
738 * Comments are allowed if the first character in the line is #
739 *
740 * Returns -1 and sets errno error codes:
741 * 0 - OK
742 * -1 - Error
743 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200744int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200745{
746 FILE *fp;
Alex Kiernan66df4e22018-06-07 12:20:05 +0000747 char *line = NULL;
748 size_t linesize = 0;
Stefano Babicada628b2010-05-24 12:08:16 +0200749 char *name;
750 char *val;
751 int lineno = 0;
752 int len;
753 int ret = 0;
754
Andreas Fenkarte6511302016-05-31 09:21:56 +0200755 if (!opts)
756 opts = &default_opts;
757
Andreas Fenkart24371902016-04-05 23:13:42 +0200758 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200759 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200760 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000761 }
762
Stefano Babicada628b2010-05-24 12:08:16 +0200763 if (strcmp(fname, "-") == 0)
764 fp = stdin;
765 else {
766 fp = fopen(fname, "r");
767 if (fp == NULL) {
768 fprintf(stderr, "I cannot open %s for reading\n",
Alex Kiernane9c62202018-03-09 12:12:59 +0000769 fname);
Stefano Babicada628b2010-05-24 12:08:16 +0200770 return -1;
771 }
772 }
773
Alex Kiernan66df4e22018-06-07 12:20:05 +0000774 while ((len = getline(&line, &linesize, fp)) != -1) {
Stefano Babicada628b2010-05-24 12:08:16 +0200775 lineno++;
Stefano Babicada628b2010-05-24 12:08:16 +0200776
777 /*
Alex Kiernan66df4e22018-06-07 12:20:05 +0000778 * Read a whole line from the file. If the line is not
779 * terminated, reports an error and exit.
Stefano Babicada628b2010-05-24 12:08:16 +0200780 */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000781 if (line[len - 1] != '\n') {
Stefano Babicada628b2010-05-24 12:08:16 +0200782 fprintf(stderr,
Alex Kiernan66df4e22018-06-07 12:20:05 +0000783 "Line %d not correctly terminated\n",
Stefano Babicada628b2010-05-24 12:08:16 +0200784 lineno);
785 ret = -1;
786 break;
787 }
788
789 /* Drop ending line feed / carriage return */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000790 line[--len] = '\0';
791 if (len && line[len - 1] == '\r')
792 line[--len] = '\0';
Stefano Babicada628b2010-05-24 12:08:16 +0200793
794 /* Skip comment or empty lines */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000795 if (len == 0 || line[0] == '#')
Stefano Babicada628b2010-05-24 12:08:16 +0200796 continue;
797
798 /*
Alex Kiernan66df4e22018-06-07 12:20:05 +0000799 * Search for variable's name remove leading whitespaces
Stefano Babicada628b2010-05-24 12:08:16 +0200800 */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000801 name = skip_blanks(line);
Stefano Babicada628b2010-05-24 12:08:16 +0200802 if (!name)
803 continue;
804
805 /* The first white space is the end of variable name */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100806 val = skip_chars(name);
Stefano Babicada628b2010-05-24 12:08:16 +0200807 len = strlen(name);
808 if (val) {
809 *val++ = '\0';
810 if ((val - name) < len)
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100811 val = skip_blanks(val);
Stefano Babicada628b2010-05-24 12:08:16 +0200812 else
813 val = NULL;
814 }
Stefano Babicada628b2010-05-24 12:08:16 +0200815#ifdef DEBUG
816 fprintf(stderr, "Setting %s : %s\n",
817 name, val ? val : " removed");
818#endif
819
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600820 if (env_flags_validate_type(name, val) < 0) {
821 ret = -1;
822 break;
823 }
824
Stefano Babicada628b2010-05-24 12:08:16 +0200825 /*
826 * If there is an error setting a variable,
827 * try to save the environment and returns an error
828 */
829 if (fw_env_write(name, val)) {
830 fprintf(stderr,
Alex Kiernane9c62202018-03-09 12:12:59 +0000831 "fw_env_write returns with error : %s\n",
Stefano Babicada628b2010-05-24 12:08:16 +0200832 strerror(errno));
833 ret = -1;
834 break;
835 }
836
837 }
Alex Kiernan66df4e22018-06-07 12:20:05 +0000838 free(line);
Stefano Babicada628b2010-05-24 12:08:16 +0200839
840 /* Close file if not stdin */
841 if (strcmp(fname, "-") != 0)
842 fclose(fp);
843
Stefano Babic4b0a2932017-04-05 18:08:03 +0200844 ret |= fw_env_flush(opts);
845
846 fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200847
848 return ret;
wdenk34b613e2002-12-17 01:51:00 +0000849}
850
Andreas Fenkart22281582016-08-29 23:16:57 +0200851/**
Shyam Sainiefe757c2018-06-07 19:47:19 +0530852 * environment_end() - compute offset of first byte right after environment
Andreas Fenkart22281582016-08-29 23:16:57 +0200853 * @dev - index of enviroment buffer
854 * Return:
Shyam Sainiefe757c2018-06-07 19:47:19 +0530855 * device offset of first byte right after environment
Andreas Fenkart22281582016-08-29 23:16:57 +0200856 */
857off_t environment_end(int dev)
858{
859 /* environment is block aligned */
860 return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
861}
862
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200863/*
864 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
865 * 0 - block is good
866 * > 0 - block is bad
867 * < 0 - failed to test
868 */
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200869static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000870{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200871 if (mtd_type == MTD_NANDFLASH) {
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200872 int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000873
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200874 if (badblock < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000875 perror("Cannot read bad block mark");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200876 return badblock;
877 }
878
879 if (badblock) {
880#ifdef DEBUG
Alex Kiernane9c62202018-03-09 12:12:59 +0000881 fprintf(stderr, "Bad block at 0x%llx, skipping\n",
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200882 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200883#endif
884 return badblock;
885 }
wdenk34b613e2002-12-17 01:51:00 +0000886 }
887
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200888 return 0;
889}
890
891/*
892 * Read data from flash at an offset into a provided buffer. On NAND it skips
893 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
894 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
895 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000896static int flash_read_buf(int dev, int fd, void *buf, size_t count,
897 off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200898{
899 size_t blocklen; /* erase / write length - one block on NAND,
900 0 on NOR */
901 size_t processed = 0; /* progress counter */
902 size_t readlen = count; /* current read length */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200903 off_t block_seek; /* offset inside the current block to the start
904 of the data */
905 loff_t blockstart; /* running start of the current block -
906 MEMGETBADBLOCK needs 64 bits */
907 int rc;
908
Alex Kiernane9c62202018-03-09 12:12:59 +0000909 blockstart = (offset / DEVESIZE(dev)) * DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200910
911 /* Offset inside a block */
912 block_seek = offset - blockstart;
913
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200914 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200915 /*
916 * NAND: calculate which blocks we are reading. We have
917 * to read one block at a time to skip bad blocks.
918 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000919 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200920
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200921 /* Limit to one block for the first read */
922 if (readlen > blocklen - block_seek)
923 readlen = blocklen - block_seek;
924 } else {
925 blocklen = 0;
wdenke7f34c62003-01-11 09:48:40 +0000926 }
wdenk34b613e2002-12-17 01:51:00 +0000927
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200928 /* This only runs once on NOR flash */
929 while (processed < count) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200930 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernane9c62202018-03-09 12:12:59 +0000931 if (rc < 0) /* block test failed */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200932 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000933
Andreas Fenkart22281582016-08-29 23:16:57 +0200934 if (blockstart + block_seek + readlen > environment_end(dev)) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200935 /* End of range is reached */
Alex Kiernane9c62202018-03-09 12:12:59 +0000936 fprintf(stderr, "Too few good blocks within range\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200937 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000938 }
939
Alex Kiernane9c62202018-03-09 12:12:59 +0000940 if (rc) { /* block is bad */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200941 blockstart += blocklen;
942 continue;
wdenk34b613e2002-12-17 01:51:00 +0000943 }
944
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200945 /*
946 * If a block is bad, we retry in the next block at the same
Tom Rini282f2702017-09-05 14:01:29 -0400947 * offset - see env/nand.c::writeenv()
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200948 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000949 lseek(fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000950
Alex Kiernane9c62202018-03-09 12:12:59 +0000951 rc = read(fd, buf + processed, readlen);
Harald Seiler79685b22020-05-28 17:54:45 +0200952 if (rc == -1) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000953 fprintf(stderr, "Read error on %s: %s\n",
954 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200955 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000956 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200957#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000958 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +0000959 rc, (unsigned long long)blockstart + block_seek,
Marcin Niestroj20890732016-05-06 14:58:29 +0200960 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200961#endif
Thibault Ferrante8586cda2021-08-24 17:29:50 +0200962 processed += rc;
963 if (rc != readlen) {
964 fprintf(stderr,
965 "Warning on %s: Attempted to read %zd bytes but got %d\n",
966 DEVNAME(dev), readlen, rc);
967 readlen -= rc;
968 block_seek += rc;
969 } else {
970 blockstart += blocklen;
971 readlen = min(blocklen, count - processed);
972 block_seek = 0;
973 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200974 }
975
976 return processed;
977}
978
979/*
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200980 * Write count bytes from begin of environment, but stay within
981 * ENVSECTORS(dev) sectors of
Remy Bohmercb969eb2011-02-12 19:06:26 +0100982 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
983 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200984 */
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200985static int flash_write_buf(int dev, int fd, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200986{
987 void *data;
988 struct erase_info_user erase;
989 size_t blocklen; /* length of NAND block / NOR erase sector */
990 size_t erase_len; /* whole area that can be erased - may include
991 bad blocks */
992 size_t erasesize; /* erase / write length - one block on NAND,
993 whole area on NOR */
994 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100995 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200996 bad blocks */
997 off_t erase_offset; /* offset to the first erase block (aligned)
998 below offset */
999 off_t block_seek; /* offset inside the erase block to the start
1000 of the data */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001001 loff_t blockstart; /* running start of the current block -
1002 MEMGETBADBLOCK needs 64 bits */
Simon Glassf8205632020-08-01 10:30:39 -06001003 int was_locked = 0; /* flash lock flag */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001004 int rc;
1005
Oliver Metze9c66ab2013-08-30 00:56:02 +02001006 /*
1007 * For mtd devices only offset and size of the environment do matter
1008 */
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001009 if (DEVTYPE(dev) == MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +02001010 blocklen = count;
Oliver Metze9c66ab2013-08-30 00:56:02 +02001011 erase_len = blocklen;
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001012 blockstart = DEVOFFSET(dev);
Oliver Metze9c66ab2013-08-30 00:56:02 +02001013 block_seek = 0;
1014 write_total = blocklen;
1015 } else {
1016 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001017
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001018 erase_offset = DEVOFFSET(dev);
wdenk34b613e2002-12-17 01:51:00 +00001019
Oliver Metze9c66ab2013-08-30 00:56:02 +02001020 /* Maximum area we may use */
Andreas Fenkart22281582016-08-29 23:16:57 +02001021 erase_len = environment_end(dev) - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +00001022
Oliver Metze9c66ab2013-08-30 00:56:02 +02001023 blockstart = erase_offset;
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001024
Oliver Metze9c66ab2013-08-30 00:56:02 +02001025 /* Offset inside a block */
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001026 block_seek = DEVOFFSET(dev) - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001027
Oliver Metze9c66ab2013-08-30 00:56:02 +02001028 /*
1029 * Data size we actually write: from the start of the block
1030 * to the start of the data, then count bytes of data, and
1031 * to the end of the block
1032 */
1033 write_total = ((block_seek + count + blocklen - 1) /
Alex Kiernane9c62202018-03-09 12:12:59 +00001034 blocklen) * blocklen;
Oliver Metze9c66ab2013-08-30 00:56:02 +02001035 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001036
1037 /*
1038 * Support data anywhere within erase sectors: read out the complete
1039 * area to be erased, replace the environment image, write the whole
1040 * block back again.
1041 */
1042 if (write_total > count) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001043 data = malloc(erase_len);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001044 if (!data) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001045 fprintf(stderr,
1046 "Cannot malloc %zu bytes: %s\n",
1047 erase_len, strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001048 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001049 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001050
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001051 rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001052 if (write_total != rc)
1053 return -1;
1054
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001055#ifdef DEBUG
1056 fprintf(stderr, "Preserving data ");
1057 if (block_seek != 0)
1058 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
1059 if (block_seek + count != write_total) {
1060 if (block_seek != 0)
1061 fprintf(stderr, " and ");
Marcin Niestroj20890732016-05-06 14:58:29 +02001062 fprintf(stderr, "0x%lx - 0x%lx",
Alex Kiernane9c62202018-03-09 12:12:59 +00001063 (unsigned long)block_seek + count,
1064 (unsigned long)write_total - 1);
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001065 }
1066 fprintf(stderr, "\n");
1067#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001068 /* Overwrite the old environment */
Alex Kiernane9c62202018-03-09 12:12:59 +00001069 memcpy(data + block_seek, buf, count);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001070 } else {
1071 /*
1072 * We get here, iff offset is block-aligned and count is a
1073 * multiple of blocklen - see write_total calculation above
1074 */
1075 data = buf;
1076 }
1077
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001078 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001079 /*
1080 * NAND: calculate which blocks we are writing. We have
1081 * to write one block at a time to skip bad blocks.
1082 */
1083 erasesize = blocklen;
1084 } else {
1085 erasesize = erase_len;
1086 }
1087
1088 erase.length = erasesize;
1089
Remy Bohmercb969eb2011-02-12 19:06:26 +01001090 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001091 while (processed < write_total) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001092 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernane9c62202018-03-09 12:12:59 +00001093 if (rc < 0) /* block test failed */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001094 return rc;
1095
Andreas Fenkart22281582016-08-29 23:16:57 +02001096 if (blockstart + erasesize > environment_end(dev)) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001097 fprintf(stderr, "End of range reached, aborting\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001098 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001099 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001100
Alex Kiernane9c62202018-03-09 12:12:59 +00001101 if (rc) { /* block is bad */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001102 blockstart += blocklen;
1103 continue;
1104 }
1105
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001106 if (DEVTYPE(dev) != MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +02001107 erase.start = blockstart;
Joakim Tjernlund85206c02021-12-08 15:33:11 +01001108 was_locked = ioctl(fd, MEMISLOCKED, &erase);
1109 /* treat any errors as unlocked flash */
1110 if (was_locked < 0)
1111 was_locked = 0;
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001112 if (was_locked)
1113 ioctl(fd, MEMUNLOCK, &erase);
Oliver Metze9c66ab2013-08-30 00:56:02 +02001114 /* These do not need an explicit erase cycle */
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001115 if (DEVTYPE(dev) != MTD_DATAFLASH)
Oliver Metze9c66ab2013-08-30 00:56:02 +02001116 if (ioctl(fd, MEMERASE, &erase) != 0) {
1117 fprintf(stderr,
1118 "MTD erase error on %s: %s\n",
1119 DEVNAME(dev), strerror(errno));
1120 return -1;
1121 }
1122 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001123
Alex Kiernane9c62202018-03-09 12:12:59 +00001124 if (lseek(fd, blockstart, SEEK_SET) == -1) {
1125 fprintf(stderr,
1126 "Seek error on %s: %s\n",
1127 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001128 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001129 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001130#ifdef DEBUG
Marcin Niestroj20890732016-05-06 14:58:29 +02001131 fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001132 (unsigned long long)erasesize,
1133 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001134#endif
Alex Kiernane9c62202018-03-09 12:12:59 +00001135 if (write(fd, data + processed, erasesize) != erasesize) {
1136 fprintf(stderr, "Write error on %s: %s\n",
1137 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001138 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001139 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001140
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001141 if (DEVTYPE(dev) != MTD_ABSENT) {
1142 if (was_locked)
1143 ioctl(fd, MEMLOCK, &erase);
1144 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001145
Alex Kiernane9c62202018-03-09 12:12:59 +00001146 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001147 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -08001148 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001149 }
1150
1151 if (write_total > count)
Alex Kiernane9c62202018-03-09 12:12:59 +00001152 free(data);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001153
1154 return processed;
1155}
1156
1157/*
1158 * Set obsolete flag at offset - NOR flash only
1159 */
Alex Kiernane9c62202018-03-09 12:12:59 +00001160static int flash_flag_obsolete(int dev, int fd, off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001161{
1162 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +02001163 struct erase_info_user erase;
Pierre-Jean Texierd7f95452019-08-26 13:06:16 +02001164 char tmp = ENV_REDUND_OBSOLETE;
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001165 int was_locked; /* flash lock flag */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001166
Alex Kiernane9c62202018-03-09 12:12:59 +00001167 erase.start = DEVOFFSET(dev);
1168 erase.length = DEVESIZE(dev);
Simon Glass4686d8f2019-08-01 09:47:08 -06001169 /* This relies on the fact, that ENV_REDUND_OBSOLETE == 0 */
Alex Kiernane9c62202018-03-09 12:12:59 +00001170 rc = lseek(fd, offset, SEEK_SET);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001171 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001172 fprintf(stderr, "Cannot seek to set the flag on %s\n",
1173 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001174 return rc;
1175 }
Joakim Tjernlund85206c02021-12-08 15:33:11 +01001176 was_locked = ioctl(fd, MEMISLOCKED, &erase);
1177 /* treat any errors as unlocked flash */
1178 if (was_locked < 0)
1179 was_locked = 0;
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001180 if (was_locked)
1181 ioctl(fd, MEMUNLOCK, &erase);
Pierre-Jean Texierd7f95452019-08-26 13:06:16 +02001182 rc = write(fd, &tmp, sizeof(tmp));
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001183 if (was_locked)
1184 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001185 if (rc < 0)
Alex Kiernane9c62202018-03-09 12:12:59 +00001186 perror("Could not set obsolete flag");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001187
1188 return rc;
1189}
1190
Rafał Miłecki840ec192022-01-12 12:47:05 +01001191static int flash_write(int fd_current, int fd_target, int dev_target, void *buf,
1192 size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001193{
1194 int rc;
1195
1196 switch (environment.flag_scheme) {
1197 case FLAG_NONE:
1198 break;
1199 case FLAG_INCREMENTAL:
1200 (*environment.flags)++;
1201 break;
1202 case FLAG_BOOLEAN:
Simon Glass4686d8f2019-08-01 09:47:08 -06001203 *environment.flags = ENV_REDUND_ACTIVE;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001204 break;
1205 default:
Alex Kiernane9c62202018-03-09 12:12:59 +00001206 fprintf(stderr, "Unimplemented flash scheme %u\n",
1207 environment.flag_scheme);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001208 return -1;
1209 }
1210
1211#ifdef DEBUG
Stefan Agnerb21e6022016-07-13 17:14:38 -07001212 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001213 DEVOFFSET(dev_target), DEVNAME(dev_target));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001214#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +01001215
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001216 if (IS_UBI(dev_target)) {
1217 if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0)
Martin Hundebøll710e9c12020-11-30 12:11:32 +01001218 return -1;
Rafał Miłecki840ec192022-01-12 12:47:05 +01001219 return ubi_write(fd_target, buf, count);
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001220 }
1221
Rafał Miłecki840ec192022-01-12 12:47:05 +01001222 rc = flash_write_buf(dev_target, fd_target, buf, count);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001223 if (rc < 0)
1224 return rc;
1225
1226 if (environment.flag_scheme == FLAG_BOOLEAN) {
1227 /* Have to set obsolete flag */
Alex Kiernane9c62202018-03-09 12:12:59 +00001228 off_t offset = DEVOFFSET(dev_current) +
1229 offsetof(struct env_image_redundant, flags);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001230#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001231 fprintf(stderr,
Stefan Agnerb21e6022016-07-13 17:14:38 -07001232 "Setting obsolete flag in environment at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001233 DEVOFFSET(dev_current), DEVNAME(dev_current));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001234#endif
Alex Kiernane9c62202018-03-09 12:12:59 +00001235 flash_flag_obsolete(dev_current, fd_current, offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001236 }
1237
1238 return 0;
1239}
1240
Rafał Miłecki840ec192022-01-12 12:47:05 +01001241static int flash_read(int fd, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001242{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001243 int rc;
1244
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001245 if (IS_UBI(dev_current)) {
1246 DEVTYPE(dev_current) = MTD_ABSENT;
1247
Rafał Miłecki840ec192022-01-12 12:47:05 +01001248 return ubi_read(fd, buf, count);
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001249 }
1250
Rafał Miłecki840ec192022-01-12 12:47:05 +01001251 rc = flash_read_buf(dev_current, fd, buf, count,
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001252 DEVOFFSET(dev_current));
Marek Vasut05ed2a52014-03-05 19:59:52 +01001253 if (rc != CUR_ENVSIZE)
1254 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001255
Marek Vasut05ed2a52014-03-05 19:59:52 +01001256 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001257}
1258
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001259static int flash_open_tempfile(const char **dname, const char **target_temp)
1260{
1261 char *dup_name = strdup(DEVNAME(dev_current));
1262 char *temp_name = NULL;
1263 int rc = -1;
1264
1265 if (!dup_name)
1266 return -1;
1267
1268 *dname = dirname(dup_name);
1269 if (!*dname)
1270 goto err;
1271
1272 rc = asprintf(&temp_name, "%s/XXXXXX", *dname);
1273 if (rc == -1)
1274 goto err;
1275
1276 rc = mkstemp(temp_name);
1277 if (rc == -1) {
1278 /* fall back to in place write */
1279 fprintf(stderr,
1280 "Can't create %s: %s\n", temp_name, strerror(errno));
1281 free(temp_name);
1282 } else {
1283 *target_temp = temp_name;
1284 /* deliberately leak dup_name as dname /might/ point into
1285 * it and we need it for our caller
1286 */
1287 dup_name = NULL;
1288 }
1289
1290err:
1291 if (dup_name)
1292 free(dup_name);
1293
1294 return rc;
1295}
1296
Rafał Miłecki840ec192022-01-12 12:47:05 +01001297static int flash_io_write(int fd_current, void *buf, size_t count)
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001298{
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001299 int fd_target = -1, rc, dev_target;
1300 const char *dname, *target_temp = NULL;
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001301
1302 if (have_redund_env) {
1303 /* switch to next partition for writing */
1304 dev_target = !dev_current;
1305 /* dev_target: fd_target, erase_target */
1306 fd_target = open(DEVNAME(dev_target), O_RDWR);
1307 if (fd_target < 0) {
1308 fprintf(stderr,
1309 "Can't open %s: %s\n",
1310 DEVNAME(dev_target), strerror(errno));
1311 rc = -1;
1312 goto exit;
1313 }
1314 } else {
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001315 struct stat sb;
1316
1317 if (fstat(fd_current, &sb) == 0 && S_ISREG(sb.st_mode)) {
1318 /* if any part of flash_open_tempfile() fails we fall
1319 * back to in-place writes
1320 */
1321 fd_target = flash_open_tempfile(&dname, &target_temp);
1322 }
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001323 dev_target = dev_current;
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001324 if (fd_target == -1)
1325 fd_target = fd_current;
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001326 }
1327
Rafał Miłecki840ec192022-01-12 12:47:05 +01001328 rc = flash_write(fd_current, fd_target, dev_target, buf, count);
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001329
1330 if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) {
1331 fprintf(stderr,
1332 "fsync failed on %s: %s\n",
1333 DEVNAME(dev_current), strerror(errno));
1334 }
1335
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001336 if (fd_current != fd_target) {
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001337 if (fsync(fd_target) &&
1338 !(errno == EINVAL || errno == EROFS)) {
1339 fprintf(stderr,
1340 "fsync failed on %s: %s\n",
1341 DEVNAME(dev_current), strerror(errno));
1342 }
1343
1344 if (close(fd_target)) {
1345 fprintf(stderr,
1346 "I/O error on %s: %s\n",
1347 DEVNAME(dev_target), strerror(errno));
1348 rc = -1;
1349 }
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001350
Alex Kiernanad4f3d02019-06-12 15:27:55 +01001351 if (rc >= 0 && target_temp) {
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001352 int dir_fd;
1353
1354 dir_fd = open(dname, O_DIRECTORY | O_RDONLY);
1355 if (dir_fd == -1)
1356 fprintf(stderr,
1357 "Can't open %s: %s\n",
1358 dname, strerror(errno));
1359
1360 if (rename(target_temp, DEVNAME(dev_target))) {
1361 fprintf(stderr,
1362 "rename failed %s => %s: %s\n",
1363 target_temp, DEVNAME(dev_target),
1364 strerror(errno));
1365 rc = -1;
1366 }
1367
1368 if (dir_fd != -1 && fsync(dir_fd))
1369 fprintf(stderr,
1370 "fsync failed on %s: %s\n",
1371 dname, strerror(errno));
1372
1373 if (dir_fd != -1 && close(dir_fd))
1374 fprintf(stderr,
1375 "I/O error on %s: %s\n",
1376 dname, strerror(errno));
1377 }
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001378 }
1379 exit:
1380 return rc;
1381}
1382
Rafał Miłecki840ec192022-01-12 12:47:05 +01001383static int flash_io(int mode, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001384{
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001385 int fd_current, rc;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001386
1387 /* dev_current: fd_current, erase_current */
Alex Kiernane9c62202018-03-09 12:12:59 +00001388 fd_current = open(DEVNAME(dev_current), mode);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001389 if (fd_current < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001390 fprintf(stderr,
1391 "Can't open %s: %s\n",
1392 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001393 return -1;
1394 }
1395
1396 if (mode == O_RDWR) {
Rafał Miłecki840ec192022-01-12 12:47:05 +01001397 rc = flash_io_write(fd_current, buf, count);
wdenk34b613e2002-12-17 01:51:00 +00001398 } else {
Rafał Miłecki840ec192022-01-12 12:47:05 +01001399 rc = flash_read(fd_current, buf, count);
wdenk34b613e2002-12-17 01:51:00 +00001400 }
1401
Alex Kiernane9c62202018-03-09 12:12:59 +00001402 if (close(fd_current)) {
1403 fprintf(stderr,
1404 "I/O error on %s: %s\n",
1405 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001406 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001407 }
1408
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001409 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001410}
1411
1412/*
wdenk34b613e2002-12-17 01:51:00 +00001413 * Prevent confusion if running from erased flash memory
1414 */
Andreas Fenkart24371902016-04-05 23:13:42 +02001415int fw_env_open(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001416{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001417 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001418 unsigned char flag0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001419 void *addr0 = NULL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001420
wdenk34b613e2002-12-17 01:51:00 +00001421 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001422 unsigned char flag1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001423 void *addr1 = NULL;
wdenke7f34c62003-01-11 09:48:40 +00001424
Marek Vasut05ed2a52014-03-05 19:59:52 +01001425 int ret;
1426
Andreas Fenkarte6511302016-05-31 09:21:56 +02001427 if (!opts)
1428 opts = &default_opts;
1429
Alex Kiernane9c62202018-03-09 12:12:59 +00001430 if (parse_config(opts)) /* should fill envdevices */
Stefano Babic4b0a2932017-04-05 18:08:03 +02001431 return -EINVAL;
wdenk3f9ab982003-04-12 23:38:12 +00001432
Joe Hershberger671adc42012-10-03 09:38:46 +00001433 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001434 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001435 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001436 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001437 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001438 ret = -ENOMEM;
1439 goto open_cleanup;
wdenke7f34c62003-01-11 09:48:40 +00001440 }
wdenk3f9ab982003-04-12 23:38:12 +00001441
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001442 dev_current = 0;
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001443 if (flash_io(O_RDONLY, addr0, CUR_ENVSIZE)) {
Stefano Babic4b0a2932017-04-05 18:08:03 +02001444 ret = -EIO;
1445 goto open_cleanup;
1446 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001447
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001448 if (!have_redund_env) {
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001449 struct env_image_single *single = addr0;
1450
1451 crc0 = crc32(0, (uint8_t *)single->data, ENV_SIZE);
1452 crc0_ok = (crc0 == single->crc);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001453 if (!crc0_ok) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001454 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001455 "Warning: Bad CRC, using default environment\n");
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001456 memcpy(single->data, default_environment,
Alex Kiernane9c62202018-03-09 12:12:59 +00001457 sizeof(default_environment));
Rasmus Villemoes06a79642018-09-27 22:45:26 +02001458 environment.dirty = 1;
wdenk34b613e2002-12-17 01:51:00 +00001459 }
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001460
1461 environment.image = addr0;
1462 environment.crc = &single->crc;
1463 environment.flags = NULL;
1464 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001465 } else {
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001466 struct env_image_redundant *redundant0 = addr0;
1467 struct env_image_redundant *redundant1;
1468
1469 crc0 = crc32(0, (uint8_t *)redundant0->data, ENV_SIZE);
1470 crc0_ok = (crc0 == redundant0->crc);
1471
1472 flag0 = redundant0->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001473
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001474 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001475 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001476 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001477 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001478 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001479 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001480 ret = -ENOMEM;
1481 goto open_cleanup;
wdenk3f9ab982003-04-12 23:38:12 +00001482 }
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001483 redundant1 = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001484
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001485 if (flash_io(O_RDONLY, addr1, CUR_ENVSIZE)) {
Tom Rini4c1192c2018-07-01 22:10:33 -04001486 ret = -EIO;
1487 goto open_cleanup;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001488 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001489
1490 /* Check flag scheme compatibility */
1491 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1492 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1493 environment.flag_scheme = FLAG_BOOLEAN;
1494 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1495 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1496 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001497 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1498 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1499 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001500 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1501 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1502 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001503 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001504 DEVTYPE(!dev_current) == MTD_ABSENT &&
1505 IS_UBI(dev_current) == IS_UBI(!dev_current)) {
Oliver Metze745d4c2013-08-30 00:56:01 +02001506 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001507 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001508 fprintf(stderr, "Incompatible flash types!\n");
Stefano Babic4b0a2932017-04-05 18:08:03 +02001509 ret = -EINVAL;
1510 goto open_cleanup;
wdenk34b613e2002-12-17 01:51:00 +00001511 }
wdenk3f9ab982003-04-12 23:38:12 +00001512
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001513 crc1 = crc32(0, (uint8_t *)redundant1->data, ENV_SIZE);
Tom Rini4c1192c2018-07-01 22:10:33 -04001514
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001515 crc1_ok = (crc1 == redundant1->crc);
1516 flag1 = redundant1->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001517
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001518 if (memcmp(redundant0->data, redundant1->data, ENV_SIZE) ||
Rasmus Villemoes06a79642018-09-27 22:45:26 +02001519 !crc0_ok || !crc1_ok)
1520 environment.dirty = 1;
1521
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001522 if (crc0_ok && !crc1_ok) {
1523 dev_current = 0;
1524 } else if (!crc0_ok && crc1_ok) {
1525 dev_current = 1;
1526 } else if (!crc0_ok && !crc1_ok) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001527 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001528 "Warning: Bad CRC, using default environment\n");
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001529 memcpy(redundant0->data, default_environment,
Alex Kiernane9c62202018-03-09 12:12:59 +00001530 sizeof(default_environment));
Rasmus Villemoes06a79642018-09-27 22:45:26 +02001531 environment.dirty = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001532 dev_current = 0;
1533 } else {
1534 switch (environment.flag_scheme) {
1535 case FLAG_BOOLEAN:
Simon Glass4686d8f2019-08-01 09:47:08 -06001536 if (flag0 == ENV_REDUND_ACTIVE &&
1537 flag1 == ENV_REDUND_OBSOLETE) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001538 dev_current = 0;
Simon Glass4686d8f2019-08-01 09:47:08 -06001539 } else if (flag0 == ENV_REDUND_OBSOLETE &&
1540 flag1 == ENV_REDUND_ACTIVE) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001541 dev_current = 1;
1542 } else if (flag0 == flag1) {
1543 dev_current = 0;
1544 } else if (flag0 == 0xFF) {
1545 dev_current = 0;
1546 } else if (flag1 == 0xFF) {
1547 dev_current = 1;
1548 } else {
1549 dev_current = 0;
1550 }
1551 break;
1552 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001553 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001554 dev_current = 1;
1555 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001556 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001557 dev_current = 0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001558 else /* flag1 > flag0 */
Jon Povey93ccd472011-03-11 14:10:56 +09001559 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001560 break;
1561 default:
Alex Kiernane9c62202018-03-09 12:12:59 +00001562 fprintf(stderr, "Unknown flag scheme %u\n",
1563 environment.flag_scheme);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001564 return -1;
1565 }
1566 }
1567
1568 /*
1569 * If we are reading, we don't need the flag and the CRC any
1570 * more, if we are writing, we will re-calculate CRC and update
1571 * flags before writing out
1572 */
1573 if (dev_current) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001574 environment.image = addr1;
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001575 environment.crc = &redundant1->crc;
1576 environment.flags = &redundant1->flags;
1577 environment.data = redundant1->data;
Alex Kiernane9c62202018-03-09 12:12:59 +00001578 free(addr0);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001579 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001580 environment.image = addr0;
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001581 environment.crc = &redundant0->crc;
1582 environment.flags = &redundant0->flags;
1583 environment.data = redundant0->data;
Alex Kiernane9c62202018-03-09 12:12:59 +00001584 free(addr1);
wdenk34b613e2002-12-17 01:51:00 +00001585 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001586#ifdef DEBUG
1587 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1588#endif
wdenk34b613e2002-12-17 01:51:00 +00001589 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001590 return 0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001591
Alex Kiernane9c62202018-03-09 12:12:59 +00001592 open_cleanup:
Stefano Babic4b0a2932017-04-05 18:08:03 +02001593 if (addr0)
1594 free(addr0);
1595
1596 if (addr1)
Björn Stenberg4d1734d2019-04-17 16:56:54 +02001597 free(addr1);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001598
1599 return ret;
1600}
1601
1602/*
1603 * Simply free allocated buffer with environment
1604 */
1605int fw_env_close(struct env_opts *opts)
1606{
1607 if (environment.image)
1608 free(environment.image);
1609
1610 environment.image = NULL;
1611
1612 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001613}
1614
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001615static int check_device_config(int dev)
1616{
1617 struct stat st;
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001618 int32_t lnum = 0;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001619 int fd, rc = 0;
1620
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001621 /* Fills in IS_UBI(), converts DEVNAME() with ubi volume name */
1622 ubi_check_dev(dev);
1623
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001624 fd = open(DEVNAME(dev), O_RDONLY);
1625 if (fd < 0) {
1626 fprintf(stderr,
Alex Kiernane9c62202018-03-09 12:12:59 +00001627 "Cannot open %s: %s\n", DEVNAME(dev), strerror(errno));
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001628 return -1;
1629 }
1630
1631 rc = fstat(fd, &st);
1632 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001633 fprintf(stderr, "Cannot stat the file %s\n", DEVNAME(dev));
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001634 goto err;
1635 }
1636
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001637 if (IS_UBI(dev)) {
1638 rc = ioctl(fd, UBI_IOCEBISMAP, &lnum);
1639 if (rc < 0) {
1640 fprintf(stderr, "Cannot get UBI information for %s\n",
1641 DEVNAME(dev));
1642 goto err;
1643 }
1644 } else if (S_ISCHR(st.st_mode)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001645 struct mtd_info_user mtdinfo;
1646 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1647 if (rc < 0) {
1648 fprintf(stderr, "Cannot get MTD information for %s\n",
1649 DEVNAME(dev));
1650 goto err;
1651 }
1652 if (mtdinfo.type != MTD_NORFLASH &&
1653 mtdinfo.type != MTD_NANDFLASH &&
1654 mtdinfo.type != MTD_DATAFLASH &&
1655 mtdinfo.type != MTD_UBIVOLUME) {
1656 fprintf(stderr, "Unsupported flash type %u on %s\n",
1657 mtdinfo.type, DEVNAME(dev));
1658 goto err;
1659 }
1660 DEVTYPE(dev) = mtdinfo.type;
Rasmus Villemoesfaf29982020-03-24 13:57:40 +01001661 if (DEVESIZE(dev) == 0 && ENVSECTORS(dev) == 0 &&
1662 mtdinfo.type == MTD_NORFLASH)
1663 DEVESIZE(dev) = mtdinfo.erasesize;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001664 if (DEVESIZE(dev) == 0)
1665 /* Assume the erase size is the same as the env-size */
1666 DEVESIZE(dev) = ENVSIZE(dev);
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001667 } else {
Stefan Agnerb21e6022016-07-13 17:14:38 -07001668 uint64_t size;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001669 DEVTYPE(dev) = MTD_ABSENT;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001670 if (DEVESIZE(dev) == 0)
1671 /* Assume the erase size to be 512 bytes */
1672 DEVESIZE(dev) = 0x200;
Stefan Agnerb21e6022016-07-13 17:14:38 -07001673
1674 /*
1675 * Check for negative offsets, treat it as backwards offset
1676 * from the end of the block device
1677 */
1678 if (DEVOFFSET(dev) < 0) {
1679 rc = ioctl(fd, BLKGETSIZE64, &size);
1680 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001681 fprintf(stderr,
1682 "Could not get block device size on %s\n",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001683 DEVNAME(dev));
1684 goto err;
1685 }
1686
1687 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1688#ifdef DEBUG
Alex Kiernane9c62202018-03-09 12:12:59 +00001689 fprintf(stderr,
1690 "Calculated device offset 0x%llx on %s\n",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001691 DEVOFFSET(dev), DEVNAME(dev));
1692#endif
1693 }
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001694 }
1695
Max Krummenacher6c44f542016-11-19 13:58:56 +01001696 if (ENVSECTORS(dev) == 0)
1697 /* Assume enough sectors to cover the environment */
1698 ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev));
1699
1700 if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001701 fprintf(stderr,
1702 "Environment does not start on (erase) block boundary\n");
Max Krummenacher6c44f542016-11-19 13:58:56 +01001703 errno = EINVAL;
1704 return -1;
1705 }
1706
1707 if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001708 fprintf(stderr,
1709 "Environment does not fit into available sectors\n");
Max Krummenacher6c44f542016-11-19 13:58:56 +01001710 errno = EINVAL;
1711 return -1;
1712 }
1713
Alex Kiernane9c62202018-03-09 12:12:59 +00001714 err:
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001715 close(fd);
1716 return rc;
1717}
wdenk34b613e2002-12-17 01:51:00 +00001718
Rafał Miłecki326d5902022-06-16 20:59:03 +02001719static int find_nvmem_device(void)
1720{
1721 const char *path = "/sys/bus/nvmem/devices";
1722 struct dirent *dent;
1723 char *nvmem = NULL;
1724 char comp[256];
1725 char buf[32];
1726 int bytes;
1727 DIR *dir;
1728
1729 dir = opendir(path);
1730 if (!dir) {
1731 return -EIO;
1732 }
1733
1734 while (!nvmem && (dent = readdir(dir))) {
1735 FILE *fp;
1736
1737 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
1738 continue;
1739 }
1740
1741 bytes = snprintf(comp, sizeof(comp), "%s/%s/of_node/compatible", path, dent->d_name);
1742 if (bytes < 0 || bytes == sizeof(comp)) {
1743 continue;
1744 }
1745
1746 fp = fopen(comp, "r");
1747 if (!fp) {
1748 continue;
1749 }
1750
1751 fread(buf, sizeof(buf), 1, fp);
1752
1753 if (!strcmp(buf, "u-boot,env")) {
1754 bytes = asprintf(&nvmem, "%s/%s/nvmem", path, dent->d_name);
1755 if (bytes < 0) {
1756 nvmem = NULL;
1757 }
1758 }
1759
1760 fclose(fp);
1761 }
1762
1763 closedir(dir);
1764
1765 if (nvmem) {
1766 struct stat s;
1767
1768 stat(nvmem, &s);
1769
1770 DEVNAME(0) = nvmem;
1771 DEVOFFSET(0) = 0;
1772 ENVSIZE(0) = s.st_size;
1773
1774 return 0;
1775 }
1776
1777 return -ENOENT;
1778}
1779
Andreas Fenkart24371902016-04-05 23:13:42 +02001780static int parse_config(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001781{
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001782 int rc;
wdenk34b613e2002-12-17 01:51:00 +00001783
Andreas Fenkarte6511302016-05-31 09:21:56 +02001784 if (!opts)
1785 opts = &default_opts;
Anatolij Gustschine89ead32016-04-29 22:00:11 +02001786
Andreas Fenkarte6511302016-05-31 09:21:56 +02001787#if defined(CONFIG_FILE)
wdenke7f34c62003-01-11 09:48:40 +00001788 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart24371902016-04-05 23:13:42 +02001789 if (get_config(opts->config_file)) {
Rafał Miłecki326d5902022-06-16 20:59:03 +02001790 if (find_nvmem_device()) {
1791 fprintf(stderr, "Cannot parse config file '%s': %m\n",
1792 opts->config_file);
1793 fprintf(stderr, "Failed to find NVMEM device\n");
1794 return -1;
1795 }
wdenk34b613e2002-12-17 01:51:00 +00001796 }
wdenke7f34c62003-01-11 09:48:40 +00001797#else
Alex Kiernane9c62202018-03-09 12:12:59 +00001798 DEVNAME(0) = DEVICE1_NAME;
1799 DEVOFFSET(0) = DEVICE1_OFFSET;
1800 ENVSIZE(0) = ENV1_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001801
1802 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1803 * know DEVTYPE
1804 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001805#ifdef DEVICE1_ESIZE
Alex Kiernane9c62202018-03-09 12:12:59 +00001806 DEVESIZE(0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001807#endif
1808#ifdef DEVICE1_ENVSECTORS
Alex Kiernane9c62202018-03-09 12:12:59 +00001809 ENVSECTORS(0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001810#endif
1811
wdenk34b613e2002-12-17 01:51:00 +00001812#ifdef HAVE_REDUND
Alex Kiernane9c62202018-03-09 12:12:59 +00001813 DEVNAME(1) = DEVICE2_NAME;
1814 DEVOFFSET(1) = DEVICE2_OFFSET;
1815 ENVSIZE(1) = ENV2_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001816
1817 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1818 * know DEVTYPE
1819 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001820#ifdef DEVICE2_ESIZE
Alex Kiernane9c62202018-03-09 12:12:59 +00001821 DEVESIZE(1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001822#endif
1823#ifdef DEVICE2_ENVSECTORS
Alex Kiernane9c62202018-03-09 12:12:59 +00001824 ENVSECTORS(1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001825#endif
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001826 have_redund_env = 1;
wdenk34b613e2002-12-17 01:51:00 +00001827#endif
wdenke7f34c62003-01-11 09:48:40 +00001828#endif
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001829 rc = check_device_config(0);
1830 if (rc < 0)
1831 return rc;
wdenk3f9ab982003-04-12 23:38:12 +00001832
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001833 if (have_redund_env) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001834 rc = check_device_config(1);
1835 if (rc < 0)
1836 return rc;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001837
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001838 if (ENVSIZE(0) != ENVSIZE(1)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001839 fprintf(stderr,
Philip Molloyf60f7512019-03-31 03:44:57 +00001840 "Redundant environments have unequal size\n");
Andreas Fenkart31f2e052016-08-17 23:41:53 +02001841 return -1;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001842 }
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001843 }
1844
1845 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001846 if (have_redund_env)
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001847 usable_envsize -= sizeof(char);
1848
wdenk34b613e2002-12-17 01:51:00 +00001849 return 0;
1850}
wdenke7f34c62003-01-11 09:48:40 +00001851
1852#if defined(CONFIG_FILE)
Alex Kiernane9c62202018-03-09 12:12:59 +00001853static int get_config(char *fname)
wdenke7f34c62003-01-11 09:48:40 +00001854{
1855 FILE *fp;
1856 int i = 0;
1857 int rc;
Alex Kiernan66df4e22018-06-07 12:20:05 +00001858 char *line = NULL;
1859 size_t linesize = 0;
Tom Rini313e9122014-03-28 12:03:33 -04001860 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001861
Alex Kiernane9c62202018-03-09 12:12:59 +00001862 fp = fopen(fname, "r");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001863 if (fp == NULL)
1864 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001865
Alex Kiernan66df4e22018-06-07 12:20:05 +00001866 while (i < 2 && getline(&line, &linesize, fp) != -1) {
1867 /* Skip comment strings */
1868 if (line[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001869 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001870
Alex Kiernan66df4e22018-06-07 12:20:05 +00001871 rc = sscanf(line, "%ms %lli %lx %lx %lx",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001872 &devname,
1873 &DEVOFFSET(i),
Alex Kiernane9c62202018-03-09 12:12:59 +00001874 &ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001875
Remy Bohmercb969eb2011-02-12 19:06:26 +01001876 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001877 continue;
1878
Tom Rini313e9122014-03-28 12:03:33 -04001879 DEVNAME(i) = devname;
1880
Max Krummenacher6c44f542016-11-19 13:58:56 +01001881 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1882 * know DEVTYPE
1883 */
wdenke7f34c62003-01-11 09:48:40 +00001884
1885 i++;
1886 }
Alex Kiernan66df4e22018-06-07 12:20:05 +00001887 free(line);
Alex Kiernane9c62202018-03-09 12:12:59 +00001888 fclose(fp);
wdenk3f9ab982003-04-12 23:38:12 +00001889
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001890 have_redund_env = i - 1;
Alex Kiernane9c62202018-03-09 12:12:59 +00001891 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001892 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001893 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001894 } else
1895 return 0;
1896}
1897#endif