blob: 2a61a5d6f04982708e9c814a10ee9d2774645c10 [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,
195 volname) == 0)
196 return volnum;
197 }
198 }
199
200 return -1;
201}
202
203static int ubi_get_devnum_by_devname(const char *devname)
204{
205 int devnum;
206 int ret;
207
208 ret = sscanf(devname + sizeof(UBI_DEV_START) - 1, "%d", &devnum);
209 if (ret != 1)
210 return -1;
211
212 return devnum;
213}
214
215static const char *ubi_get_volume_devname(const char *devname,
216 const char *volname)
217{
218 char *volume_devname;
219 int volnum;
220 int devnum;
221 int ret;
222
223 devnum = ubi_get_devnum_by_devname(devname);
224 if (devnum < 0)
225 return NULL;
226
227 volnum = ubi_get_volnum_by_name(devnum, volname);
228 if (volnum < 0)
229 return NULL;
230
231 ret = asprintf(&volume_devname, "%s_%d", devname, volnum);
232 if (ret < 0)
233 return NULL;
234
235#ifdef DEBUG
236 fprintf(stderr, "Found ubi volume \"%s:%s\" -> %s\n",
237 devname, volname, volume_devname);
238#endif
239
240 return volume_devname;
241}
242
243static void ubi_check_dev(unsigned int dev_id)
244{
245 char *devname = (char *)DEVNAME(dev_id);
246 char *pname;
247 const char *volname = NULL;
248 const char *volume_devname;
249
250 if (!is_ubi_devname(DEVNAME(dev_id)))
251 return;
252
253 IS_UBI(dev_id) = 1;
254
255 for (pname = devname; *pname != '\0'; pname++) {
256 if (*pname == ':') {
257 *pname = '\0';
258 volname = pname + 1;
259 break;
260 }
261 }
262
263 if (volname) {
264 /* Let's find real volume device name */
265 volume_devname = ubi_get_volume_devname(devname, volname);
266 if (!volume_devname) {
267 fprintf(stderr, "Didn't found ubi volume \"%s\"\n",
268 volname);
269 return;
270 }
271
272 free(devname);
273 DEVNAME(dev_id) = volume_devname;
274 }
275}
276
277static int ubi_update_start(int fd, int64_t bytes)
278{
279 if (ioctl(fd, UBI_IOCVOLUP, &bytes))
280 return -1;
281 return 0;
282}
283
284static int ubi_read(int fd, void *buf, size_t count)
285{
286 ssize_t ret;
287
288 while (count > 0) {
289 ret = read(fd, buf, count);
290 if (ret > 0) {
291 count -= ret;
292 buf += ret;
293
294 continue;
295 }
296
297 if (ret == 0) {
298 /*
299 * Happens in case of too short volume data size. If we
300 * return error status we will fail it will be treated
301 * as UBI device error.
302 *
303 * Leave catching this error to CRC check.
304 */
305 fprintf(stderr, "Warning: end of data on ubi volume\n");
306 return 0;
307 } else if (errno == EBADF) {
308 /*
309 * Happens in case of corrupted volume. The same as
310 * above, we cannot return error now, as we will still
311 * be able to successfully write environment later.
312 */
313 fprintf(stderr, "Warning: corrupted volume?\n");
314 return 0;
315 } else if (errno == EINTR) {
316 continue;
317 }
318
319 fprintf(stderr, "Cannot read %u bytes from ubi volume, %s\n",
320 (unsigned int)count, strerror(errno));
321 return -1;
322 }
323
324 return 0;
325}
326
327static int ubi_write(int fd, const void *buf, size_t count)
328{
329 ssize_t ret;
330
331 while (count > 0) {
332 ret = write(fd, buf, count);
333 if (ret <= 0) {
334 if (ret < 0 && errno == EINTR)
335 continue;
336
337 fprintf(stderr, "Cannot write %u bytes to ubi volume\n",
338 (unsigned int)count);
339 return -1;
340 }
341
342 count -= ret;
343 buf += ret;
344 }
345
346 return 0;
347}
348
Alex Kiernane9c62202018-03-09 12:12:59 +0000349static int flash_io(int mode);
Andreas Fenkart24371902016-04-05 23:13:42 +0200350static int parse_config(struct env_opts *opts);
wdenk3f9ab982003-04-12 23:38:12 +0000351
wdenke7f34c62003-01-11 09:48:40 +0000352#if defined(CONFIG_FILE)
Alex Kiernane9c62202018-03-09 12:12:59 +0000353static int get_config(char *);
wdenke7f34c62003-01-11 09:48:40 +0000354#endif
wdenk34b613e2002-12-17 01:51:00 +0000355
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100356static char *skip_chars(char *s)
Stefano Babicada628b2010-05-24 12:08:16 +0200357{
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100358 for (; *s != '\0'; s++) {
Stefan Agner87833242018-03-01 14:06:32 +0100359 if (isblank(*s) || *s == '=')
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100360 return s;
361 }
362 return NULL;
363}
Stefano Babicada628b2010-05-24 12:08:16 +0200364
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100365static char *skip_blanks(char *s)
366{
367 for (; *s != '\0'; s++) {
368 if (!isblank(*s))
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100369 return s;
Stefano Babicada628b2010-05-24 12:08:16 +0200370 }
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100371 return NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200372}
373
wdenk34b613e2002-12-17 01:51:00 +0000374/*
Andreas Fenkart3ff10742016-07-16 17:06:14 +0200375 * s1 is either a simple 'name', or a 'name=value' pair.
376 * s2 is a 'name=value' pair.
377 * If the names match, return the value of s2, else NULL.
378 */
379static char *envmatch(char *s1, char *s2)
380{
381 if (s1 == NULL || s2 == NULL)
382 return NULL;
383
384 while (*s1 == *s2++)
385 if (*s1++ == '=')
386 return s2;
387 if (*s1 == '\0' && *(s2 - 1) == '=')
388 return s2;
389 return NULL;
390}
391
392/**
wdenk34b613e2002-12-17 01:51:00 +0000393 * Search the environment for a variable.
394 * Return the value, if found, or NULL, if not found.
395 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000396char *fw_getenv(char *name)
wdenk34b613e2002-12-17 01:51:00 +0000397{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100398 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000399
wdenk3f9ab982003-04-12 23:38:12 +0000400 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100401 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000402
wdenk3f9ab982003-04-12 23:38:12 +0000403 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000404 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000405 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000406 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200407 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000408 }
409 }
Alex Kiernane9c62202018-03-09 12:12:59 +0000410 val = envmatch(name, env);
wdenk34b613e2002-12-17 01:51:00 +0000411 if (!val)
412 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200413 return val;
wdenk34b613e2002-12-17 01:51:00 +0000414 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200415 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000416}
417
418/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600419 * Search the default environment for a variable.
420 * Return the value, if found, or NULL, if not found.
421 */
422char *fw_getdefenv(char *name)
423{
424 char *env, *nxt;
425
426 for (env = default_environment; *env; env = nxt + 1) {
427 char *val;
428
429 for (nxt = env; *nxt; ++nxt) {
430 if (nxt >= &default_environment[ENV_SIZE]) {
431 fprintf(stderr, "## Error: "
432 "default environment not terminated\n");
433 return NULL;
434 }
435 }
436 val = envmatch(name, env);
437 if (!val)
438 continue;
439 return val;
440 }
441 return NULL;
442}
443
444/*
wdenk34b613e2002-12-17 01:51:00 +0000445 * Print the current definition of one, or more, or all
446 * environment variables
447 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200448int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000449{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100450 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000451
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200452 if (value_only && argc != 1) {
453 fprintf(stderr,
Alex Kiernan688cee92018-02-11 17:16:46 +0000454 "## Error: `-n'/`--noheader' option requires exactly one argument\n");
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200455 return -1;
456 }
457
Andreas Fenkarte6511302016-05-31 09:21:56 +0200458 if (!opts)
459 opts = &default_opts;
460
Andreas Fenkart24371902016-04-05 23:13:42 +0200461 if (fw_env_open(opts))
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200462 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000463
Alex Kiernane9c62202018-03-09 12:12:59 +0000464 if (argc == 0) { /* Print all env variables */
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200465 char *env, *nxt;
wdenk3f9ab982003-04-12 23:38:12 +0000466 for (env = environment.data; *env; env = nxt + 1) {
467 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000468 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000469 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000470 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200471 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000472 }
473 }
474
Alex Kiernane9c62202018-03-09 12:12:59 +0000475 printf("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000476 }
Stefano Babic4b0a2932017-04-05 18:08:03 +0200477 fw_env_close(opts);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200478 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000479 }
480
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200481 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100482 char *name = argv[i];
483 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000484
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200485 val = fw_getenv(name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700486 if (!val) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000487 fprintf(stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700488 rc = -1;
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200489 continue;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700490 }
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200491
492 if (value_only) {
493 puts(val);
494 break;
495 }
496
497 printf("%s=%s\n", name, val);
wdenk34b613e2002-12-17 01:51:00 +0000498 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700499
Stefano Babic4b0a2932017-04-05 18:08:03 +0200500 fw_env_close(opts);
501
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200502 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000503}
504
Stefano Babic4b0a2932017-04-05 18:08:03 +0200505int fw_env_flush(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000506{
Andreas Fenkarte6511302016-05-31 09:21:56 +0200507 if (!opts)
508 opts = &default_opts;
509
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200510 if (!environment.dirty)
511 return 0;
512
Stefano Babicada628b2010-05-24 12:08:16 +0200513 /*
514 * Update CRC
515 */
516 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000517
Stefano Babicada628b2010-05-24 12:08:16 +0200518 /* write environment back to flash */
519 if (flash_io(O_RDWR)) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000520 fprintf(stderr, "Error: can't write fw_env to flash\n");
521 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000522 }
523
Stefano Babicada628b2010-05-24 12:08:16 +0200524 return 0;
525}
wdenk34b613e2002-12-17 01:51:00 +0000526
Stefano Babicada628b2010-05-24 12:08:16 +0200527/*
528 * Set/Clear a single variable in the environment.
529 * This is called in sequence to update the environment
530 * in RAM without updating the copy in flash after each set
531 */
532int fw_env_write(char *name, char *value)
533{
534 int len;
535 char *env, *nxt;
536 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600537 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000538
539 /*
540 * search if variable with this name already exists
541 */
wdenk3f9ab982003-04-12 23:38:12 +0000542 for (nxt = env = environment.data; *env; env = nxt + 1) {
543 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000544 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200545 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000546 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200547 errno = EINVAL;
548 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000549 }
550 }
Alex Kiernane9c62202018-03-09 12:12:59 +0000551 oldval = envmatch(name, env);
552 if (oldval)
wdenk34b613e2002-12-17 01:51:00 +0000553 break;
554 }
555
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600556 deleting = (oldval && !(value && strlen(value)));
557 creating = (!oldval && (value && strlen(value)));
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200558 overwriting = (oldval && (value && strlen(value) &&
559 strcmp(oldval, value)));
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600560
561 /* check for permission */
562 if (deleting) {
563 if (env_flags_validate_varaccess(name,
564 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
565 printf("Can't delete \"%s\"\n", name);
566 errno = EROFS;
567 return -1;
568 }
569 } else if (overwriting) {
570 if (env_flags_validate_varaccess(name,
571 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
572 printf("Can't overwrite \"%s\"\n", name);
573 errno = EROFS;
574 return -1;
575 } else if (env_flags_validate_varaccess(name,
Alex Kiernane9c62202018-03-09 12:12:59 +0000576 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600577 const char *defval = fw_getdefenv(name);
578
579 if (defval == NULL)
580 defval = "";
581 if (strcmp(oldval, defval)
582 != 0) {
583 printf("Can't overwrite \"%s\"\n", name);
584 errno = EROFS;
585 return -1;
586 }
587 }
588 } else if (creating) {
589 if (env_flags_validate_varaccess(name,
590 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
591 printf("Can't create \"%s\"\n", name);
592 errno = EROFS;
593 return -1;
594 }
595 } else
596 /* Nothing to do */
597 return 0;
598
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200599 environment.dirty = 1;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600600 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000601 if (*++nxt == '\0') {
602 *env = '\0';
603 } else {
604 for (;;) {
605 *env = *nxt++;
606 if ((*env == '\0') && (*nxt == '\0'))
607 break;
608 ++env;
609 }
610 }
611 *++env = '\0';
612 }
613
614 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200615 if (!value || !strlen(value))
616 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000617
618 /*
619 * Append new definition at the end
620 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000621 for (env = environment.data; *env || *(env + 1); ++env)
622 ;
wdenk34b613e2002-12-17 01:51:00 +0000623 if (env > environment.data)
624 ++env;
625 /*
626 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000627 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000628 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000629 len = strlen(name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000630 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200631 len += strlen(value) + 1;
632
wdenk3f9ab982003-04-12 23:38:12 +0000633 if (len > (&environment.data[ENV_SIZE] - env)) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000634 fprintf(stderr,
635 "Error: environment overflow, \"%s\" deleted\n", name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200636 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000637 }
Stefano Babicada628b2010-05-24 12:08:16 +0200638
wdenk34b613e2002-12-17 01:51:00 +0000639 while ((*env = *name++) != '\0')
640 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200641 *env = '=';
642 while ((*++env = *value++) != '\0')
643 ;
644
645 /* end is marked with double '\0' */
646 *++env = '\0';
647
648 return 0;
649}
650
651/*
652 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
653 * 0 - OK
654 * EINVAL - need at least 1 argument
655 * EROFS - certain variables ("ethaddr", "serial#") cannot be
656 * modified or deleted
657 *
658 */
Simon Glass6a38e412017-08-03 12:22:09 -0600659int fw_env_set(int argc, char *argv[], struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200660{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100661 int i;
Mike Frysinger178ec772012-11-10 19:47:45 +0000662 size_t len;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100663 char *name, **valv;
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200664 char *oldval;
Stefano Babicada628b2010-05-24 12:08:16 +0200665 char *value = NULL;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100666 int valc;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200667 int ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200668
Andreas Fenkarte6511302016-05-31 09:21:56 +0200669 if (!opts)
670 opts = &default_opts;
671
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100672 if (argc < 1) {
673 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasut05ed2a52014-03-05 19:59:52 +0100674 errno = EINVAL;
675 return -1;
676 }
677
Andreas Fenkart24371902016-04-05 23:13:42 +0200678 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200679 fprintf(stderr, "Error: environment not initialized\n");
680 return -1;
681 }
682
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100683 name = argv[0];
684 valv = argv + 1;
685 valc = argc - 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200686
Stefano Babic4b0a2932017-04-05 18:08:03 +0200687 if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
688 fw_env_close(opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200689 return -1;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200690 }
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600691
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000692 len = 0;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100693 for (i = 0; i < valc; ++i) {
694 char *val = valv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000695 size_t val_len = strlen(val);
696
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000697 if (value)
698 value[len - 1] = ' ';
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200699 oldval = value;
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000700 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200701 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000702 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200703 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000704 len, strerror(errno));
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200705 free(oldval);
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000706 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200707 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000708
709 memcpy(value + len, val, val_len);
710 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000711 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000712 }
713
Stefano Babicada628b2010-05-24 12:08:16 +0200714 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000715
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000716 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000717
Stefano Babic4b0a2932017-04-05 18:08:03 +0200718 ret = fw_env_flush(opts);
719 fw_env_close(opts);
720
721 return ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200722}
wdenk34b613e2002-12-17 01:51:00 +0000723
Stefano Babicada628b2010-05-24 12:08:16 +0200724/*
725 * Parse a file and configure the u-boot variables.
726 * The script file has a very simple format, as follows:
727 *
728 * Each line has a couple with name, value:
729 * <white spaces>variable_name<white spaces>variable_value
730 *
731 * Both variable_name and variable_value are interpreted as strings.
732 * Any character after <white spaces> and before ending \r\n is interpreted
733 * as variable's value (no comment allowed on these lines !)
734 *
735 * Comments are allowed if the first character in the line is #
736 *
737 * Returns -1 and sets errno error codes:
738 * 0 - OK
739 * -1 - Error
740 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200741int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200742{
743 FILE *fp;
Alex Kiernan66df4e22018-06-07 12:20:05 +0000744 char *line = NULL;
745 size_t linesize = 0;
Stefano Babicada628b2010-05-24 12:08:16 +0200746 char *name;
747 char *val;
748 int lineno = 0;
749 int len;
750 int ret = 0;
751
Andreas Fenkarte6511302016-05-31 09:21:56 +0200752 if (!opts)
753 opts = &default_opts;
754
Andreas Fenkart24371902016-04-05 23:13:42 +0200755 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200756 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200757 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000758 }
759
Stefano Babicada628b2010-05-24 12:08:16 +0200760 if (strcmp(fname, "-") == 0)
761 fp = stdin;
762 else {
763 fp = fopen(fname, "r");
764 if (fp == NULL) {
765 fprintf(stderr, "I cannot open %s for reading\n",
Alex Kiernane9c62202018-03-09 12:12:59 +0000766 fname);
Stefano Babicada628b2010-05-24 12:08:16 +0200767 return -1;
768 }
769 }
770
Alex Kiernan66df4e22018-06-07 12:20:05 +0000771 while ((len = getline(&line, &linesize, fp)) != -1) {
Stefano Babicada628b2010-05-24 12:08:16 +0200772 lineno++;
Stefano Babicada628b2010-05-24 12:08:16 +0200773
774 /*
Alex Kiernan66df4e22018-06-07 12:20:05 +0000775 * Read a whole line from the file. If the line is not
776 * terminated, reports an error and exit.
Stefano Babicada628b2010-05-24 12:08:16 +0200777 */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000778 if (line[len - 1] != '\n') {
Stefano Babicada628b2010-05-24 12:08:16 +0200779 fprintf(stderr,
Alex Kiernan66df4e22018-06-07 12:20:05 +0000780 "Line %d not correctly terminated\n",
Stefano Babicada628b2010-05-24 12:08:16 +0200781 lineno);
782 ret = -1;
783 break;
784 }
785
786 /* Drop ending line feed / carriage return */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000787 line[--len] = '\0';
788 if (len && line[len - 1] == '\r')
789 line[--len] = '\0';
Stefano Babicada628b2010-05-24 12:08:16 +0200790
791 /* Skip comment or empty lines */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000792 if (len == 0 || line[0] == '#')
Stefano Babicada628b2010-05-24 12:08:16 +0200793 continue;
794
795 /*
Alex Kiernan66df4e22018-06-07 12:20:05 +0000796 * Search for variable's name remove leading whitespaces
Stefano Babicada628b2010-05-24 12:08:16 +0200797 */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000798 name = skip_blanks(line);
Stefano Babicada628b2010-05-24 12:08:16 +0200799 if (!name)
800 continue;
801
802 /* The first white space is the end of variable name */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100803 val = skip_chars(name);
Stefano Babicada628b2010-05-24 12:08:16 +0200804 len = strlen(name);
805 if (val) {
806 *val++ = '\0';
807 if ((val - name) < len)
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100808 val = skip_blanks(val);
Stefano Babicada628b2010-05-24 12:08:16 +0200809 else
810 val = NULL;
811 }
Stefano Babicada628b2010-05-24 12:08:16 +0200812#ifdef DEBUG
813 fprintf(stderr, "Setting %s : %s\n",
814 name, val ? val : " removed");
815#endif
816
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600817 if (env_flags_validate_type(name, val) < 0) {
818 ret = -1;
819 break;
820 }
821
Stefano Babicada628b2010-05-24 12:08:16 +0200822 /*
823 * If there is an error setting a variable,
824 * try to save the environment and returns an error
825 */
826 if (fw_env_write(name, val)) {
827 fprintf(stderr,
Alex Kiernane9c62202018-03-09 12:12:59 +0000828 "fw_env_write returns with error : %s\n",
Stefano Babicada628b2010-05-24 12:08:16 +0200829 strerror(errno));
830 ret = -1;
831 break;
832 }
833
834 }
Alex Kiernan66df4e22018-06-07 12:20:05 +0000835 free(line);
Stefano Babicada628b2010-05-24 12:08:16 +0200836
837 /* Close file if not stdin */
838 if (strcmp(fname, "-") != 0)
839 fclose(fp);
840
Stefano Babic4b0a2932017-04-05 18:08:03 +0200841 ret |= fw_env_flush(opts);
842
843 fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200844
845 return ret;
wdenk34b613e2002-12-17 01:51:00 +0000846}
847
Andreas Fenkart22281582016-08-29 23:16:57 +0200848/**
Shyam Sainiefe757c2018-06-07 19:47:19 +0530849 * environment_end() - compute offset of first byte right after environment
Andreas Fenkart22281582016-08-29 23:16:57 +0200850 * @dev - index of enviroment buffer
851 * Return:
Shyam Sainiefe757c2018-06-07 19:47:19 +0530852 * device offset of first byte right after environment
Andreas Fenkart22281582016-08-29 23:16:57 +0200853 */
854off_t environment_end(int dev)
855{
856 /* environment is block aligned */
857 return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
858}
859
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200860/*
861 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
862 * 0 - block is good
863 * > 0 - block is bad
864 * < 0 - failed to test
865 */
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200866static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000867{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200868 if (mtd_type == MTD_NANDFLASH) {
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200869 int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000870
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200871 if (badblock < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000872 perror("Cannot read bad block mark");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200873 return badblock;
874 }
875
876 if (badblock) {
877#ifdef DEBUG
Alex Kiernane9c62202018-03-09 12:12:59 +0000878 fprintf(stderr, "Bad block at 0x%llx, skipping\n",
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200879 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200880#endif
881 return badblock;
882 }
wdenk34b613e2002-12-17 01:51:00 +0000883 }
884
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200885 return 0;
886}
887
888/*
889 * Read data from flash at an offset into a provided buffer. On NAND it skips
890 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
891 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
892 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000893static int flash_read_buf(int dev, int fd, void *buf, size_t count,
894 off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200895{
896 size_t blocklen; /* erase / write length - one block on NAND,
897 0 on NOR */
898 size_t processed = 0; /* progress counter */
899 size_t readlen = count; /* current read length */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200900 off_t block_seek; /* offset inside the current block to the start
901 of the data */
902 loff_t blockstart; /* running start of the current block -
903 MEMGETBADBLOCK needs 64 bits */
904 int rc;
905
Alex Kiernane9c62202018-03-09 12:12:59 +0000906 blockstart = (offset / DEVESIZE(dev)) * DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200907
908 /* Offset inside a block */
909 block_seek = offset - blockstart;
910
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200911 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200912 /*
913 * NAND: calculate which blocks we are reading. We have
914 * to read one block at a time to skip bad blocks.
915 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000916 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200917
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200918 /* Limit to one block for the first read */
919 if (readlen > blocklen - block_seek)
920 readlen = blocklen - block_seek;
921 } else {
922 blocklen = 0;
wdenke7f34c62003-01-11 09:48:40 +0000923 }
wdenk34b613e2002-12-17 01:51:00 +0000924
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200925 /* This only runs once on NOR flash */
926 while (processed < count) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200927 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernane9c62202018-03-09 12:12:59 +0000928 if (rc < 0) /* block test failed */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200929 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000930
Andreas Fenkart22281582016-08-29 23:16:57 +0200931 if (blockstart + block_seek + readlen > environment_end(dev)) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200932 /* End of range is reached */
Alex Kiernane9c62202018-03-09 12:12:59 +0000933 fprintf(stderr, "Too few good blocks within range\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200934 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000935 }
936
Alex Kiernane9c62202018-03-09 12:12:59 +0000937 if (rc) { /* block is bad */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200938 blockstart += blocklen;
939 continue;
wdenk34b613e2002-12-17 01:51:00 +0000940 }
941
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200942 /*
943 * If a block is bad, we retry in the next block at the same
Tom Rini282f2702017-09-05 14:01:29 -0400944 * offset - see env/nand.c::writeenv()
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200945 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000946 lseek(fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000947
Alex Kiernane9c62202018-03-09 12:12:59 +0000948 rc = read(fd, buf + processed, readlen);
Harald Seiler79685b22020-05-28 17:54:45 +0200949 if (rc == -1) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000950 fprintf(stderr, "Read error on %s: %s\n",
951 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200952 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000953 }
Harald Seiler79685b22020-05-28 17:54:45 +0200954 if (rc != readlen) {
Simon Glasse0de0f22020-08-01 10:30:38 -0600955 fprintf(stderr,
956 "Read error on %s: Attempted to read %zd bytes but got %d\n",
Harald Seiler79685b22020-05-28 17:54:45 +0200957 DEVNAME(dev), readlen, rc);
958 return -1;
959 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200960#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000961 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +0000962 rc, (unsigned long long)blockstart + block_seek,
Marcin Niestroj20890732016-05-06 14:58:29 +0200963 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200964#endif
965 processed += readlen;
Alex Kiernane9c62202018-03-09 12:12:59 +0000966 readlen = min(blocklen, count - processed);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200967 block_seek = 0;
968 blockstart += blocklen;
969 }
970
971 return processed;
972}
973
974/*
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200975 * Write count bytes from begin of environment, but stay within
976 * ENVSECTORS(dev) sectors of
Remy Bohmercb969eb2011-02-12 19:06:26 +0100977 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
978 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200979 */
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200980static int flash_write_buf(int dev, int fd, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200981{
982 void *data;
983 struct erase_info_user erase;
984 size_t blocklen; /* length of NAND block / NOR erase sector */
985 size_t erase_len; /* whole area that can be erased - may include
986 bad blocks */
987 size_t erasesize; /* erase / write length - one block on NAND,
988 whole area on NOR */
989 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100990 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200991 bad blocks */
992 off_t erase_offset; /* offset to the first erase block (aligned)
993 below offset */
994 off_t block_seek; /* offset inside the erase block to the start
995 of the data */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200996 loff_t blockstart; /* running start of the current block -
997 MEMGETBADBLOCK needs 64 bits */
Simon Glassf8205632020-08-01 10:30:39 -0600998 int was_locked = 0; /* flash lock flag */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200999 int rc;
1000
Oliver Metze9c66ab2013-08-30 00:56:02 +02001001 /*
1002 * For mtd devices only offset and size of the environment do matter
1003 */
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001004 if (DEVTYPE(dev) == MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +02001005 blocklen = count;
Oliver Metze9c66ab2013-08-30 00:56:02 +02001006 erase_len = blocklen;
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001007 blockstart = DEVOFFSET(dev);
Oliver Metze9c66ab2013-08-30 00:56:02 +02001008 block_seek = 0;
1009 write_total = blocklen;
1010 } else {
1011 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001012
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001013 erase_offset = DEVOFFSET(dev);
wdenk34b613e2002-12-17 01:51:00 +00001014
Oliver Metze9c66ab2013-08-30 00:56:02 +02001015 /* Maximum area we may use */
Andreas Fenkart22281582016-08-29 23:16:57 +02001016 erase_len = environment_end(dev) - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +00001017
Oliver Metze9c66ab2013-08-30 00:56:02 +02001018 blockstart = erase_offset;
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001019
Oliver Metze9c66ab2013-08-30 00:56:02 +02001020 /* Offset inside a block */
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001021 block_seek = DEVOFFSET(dev) - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001022
Oliver Metze9c66ab2013-08-30 00:56:02 +02001023 /*
1024 * Data size we actually write: from the start of the block
1025 * to the start of the data, then count bytes of data, and
1026 * to the end of the block
1027 */
1028 write_total = ((block_seek + count + blocklen - 1) /
Alex Kiernane9c62202018-03-09 12:12:59 +00001029 blocklen) * blocklen;
Oliver Metze9c66ab2013-08-30 00:56:02 +02001030 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001031
1032 /*
1033 * Support data anywhere within erase sectors: read out the complete
1034 * area to be erased, replace the environment image, write the whole
1035 * block back again.
1036 */
1037 if (write_total > count) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001038 data = malloc(erase_len);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001039 if (!data) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001040 fprintf(stderr,
1041 "Cannot malloc %zu bytes: %s\n",
1042 erase_len, strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001043 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001044 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001045
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001046 rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001047 if (write_total != rc)
1048 return -1;
1049
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001050#ifdef DEBUG
1051 fprintf(stderr, "Preserving data ");
1052 if (block_seek != 0)
1053 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
1054 if (block_seek + count != write_total) {
1055 if (block_seek != 0)
1056 fprintf(stderr, " and ");
Marcin Niestroj20890732016-05-06 14:58:29 +02001057 fprintf(stderr, "0x%lx - 0x%lx",
Alex Kiernane9c62202018-03-09 12:12:59 +00001058 (unsigned long)block_seek + count,
1059 (unsigned long)write_total - 1);
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001060 }
1061 fprintf(stderr, "\n");
1062#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001063 /* Overwrite the old environment */
Alex Kiernane9c62202018-03-09 12:12:59 +00001064 memcpy(data + block_seek, buf, count);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001065 } else {
1066 /*
1067 * We get here, iff offset is block-aligned and count is a
1068 * multiple of blocklen - see write_total calculation above
1069 */
1070 data = buf;
1071 }
1072
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001073 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001074 /*
1075 * NAND: calculate which blocks we are writing. We have
1076 * to write one block at a time to skip bad blocks.
1077 */
1078 erasesize = blocklen;
1079 } else {
1080 erasesize = erase_len;
1081 }
1082
1083 erase.length = erasesize;
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001084 if (DEVTYPE(dev) != MTD_ABSENT) {
1085 was_locked = ioctl(fd, MEMISLOCKED, &erase);
1086 /* treat any errors as unlocked flash */
1087 if (was_locked < 0)
1088 was_locked = 0;
1089 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001090
Remy Bohmercb969eb2011-02-12 19:06:26 +01001091 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001092 while (processed < write_total) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001093 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernane9c62202018-03-09 12:12:59 +00001094 if (rc < 0) /* block test failed */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001095 return rc;
1096
Andreas Fenkart22281582016-08-29 23:16:57 +02001097 if (blockstart + erasesize > environment_end(dev)) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001098 fprintf(stderr, "End of range reached, aborting\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001099 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001100 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001101
Alex Kiernane9c62202018-03-09 12:12:59 +00001102 if (rc) { /* block is bad */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001103 blockstart += blocklen;
1104 continue;
1105 }
1106
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001107 if (DEVTYPE(dev) != MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +02001108 erase.start = blockstart;
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001109 if (was_locked)
1110 ioctl(fd, MEMUNLOCK, &erase);
Oliver Metze9c66ab2013-08-30 00:56:02 +02001111 /* These do not need an explicit erase cycle */
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001112 if (DEVTYPE(dev) != MTD_DATAFLASH)
Oliver Metze9c66ab2013-08-30 00:56:02 +02001113 if (ioctl(fd, MEMERASE, &erase) != 0) {
1114 fprintf(stderr,
1115 "MTD erase error on %s: %s\n",
1116 DEVNAME(dev), strerror(errno));
1117 return -1;
1118 }
1119 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001120
Alex Kiernane9c62202018-03-09 12:12:59 +00001121 if (lseek(fd, blockstart, SEEK_SET) == -1) {
1122 fprintf(stderr,
1123 "Seek error on %s: %s\n",
1124 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001125 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001126 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001127#ifdef DEBUG
Marcin Niestroj20890732016-05-06 14:58:29 +02001128 fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001129 (unsigned long long)erasesize,
1130 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001131#endif
Alex Kiernane9c62202018-03-09 12:12:59 +00001132 if (write(fd, data + processed, erasesize) != erasesize) {
1133 fprintf(stderr, "Write error on %s: %s\n",
1134 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001135 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001136 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001137
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001138 if (DEVTYPE(dev) != MTD_ABSENT) {
1139 if (was_locked)
1140 ioctl(fd, MEMLOCK, &erase);
1141 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001142
Alex Kiernane9c62202018-03-09 12:12:59 +00001143 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001144 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -08001145 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001146 }
1147
1148 if (write_total > count)
Alex Kiernane9c62202018-03-09 12:12:59 +00001149 free(data);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001150
1151 return processed;
1152}
1153
1154/*
1155 * Set obsolete flag at offset - NOR flash only
1156 */
Alex Kiernane9c62202018-03-09 12:12:59 +00001157static int flash_flag_obsolete(int dev, int fd, off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001158{
1159 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +02001160 struct erase_info_user erase;
Pierre-Jean Texierd7f95452019-08-26 13:06:16 +02001161 char tmp = ENV_REDUND_OBSOLETE;
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001162 int was_locked; /* flash lock flag */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001163
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001164 was_locked = ioctl(fd, MEMISLOCKED, &erase);
Alex Kiernane9c62202018-03-09 12:12:59 +00001165 erase.start = DEVOFFSET(dev);
1166 erase.length = DEVESIZE(dev);
Simon Glass4686d8f2019-08-01 09:47:08 -06001167 /* This relies on the fact, that ENV_REDUND_OBSOLETE == 0 */
Alex Kiernane9c62202018-03-09 12:12:59 +00001168 rc = lseek(fd, offset, SEEK_SET);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001169 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001170 fprintf(stderr, "Cannot seek to set the flag on %s\n",
1171 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001172 return rc;
1173 }
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001174 if (was_locked)
1175 ioctl(fd, MEMUNLOCK, &erase);
Pierre-Jean Texierd7f95452019-08-26 13:06:16 +02001176 rc = write(fd, &tmp, sizeof(tmp));
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001177 if (was_locked)
1178 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001179 if (rc < 0)
Alex Kiernane9c62202018-03-09 12:12:59 +00001180 perror("Could not set obsolete flag");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001181
1182 return rc;
1183}
1184
Alex Kiernane9c62202018-03-09 12:12:59 +00001185static int flash_write(int fd_current, int fd_target, int dev_target)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001186{
1187 int rc;
1188
1189 switch (environment.flag_scheme) {
1190 case FLAG_NONE:
1191 break;
1192 case FLAG_INCREMENTAL:
1193 (*environment.flags)++;
1194 break;
1195 case FLAG_BOOLEAN:
Simon Glass4686d8f2019-08-01 09:47:08 -06001196 *environment.flags = ENV_REDUND_ACTIVE;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001197 break;
1198 default:
Alex Kiernane9c62202018-03-09 12:12:59 +00001199 fprintf(stderr, "Unimplemented flash scheme %u\n",
1200 environment.flag_scheme);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001201 return -1;
1202 }
1203
1204#ifdef DEBUG
Stefan Agnerb21e6022016-07-13 17:14:38 -07001205 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001206 DEVOFFSET(dev_target), DEVNAME(dev_target));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001207#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +01001208
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001209 if (IS_UBI(dev_target)) {
1210 if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0)
Martin Hundebøll710e9c12020-11-30 12:11:32 +01001211 return -1;
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001212 return ubi_write(fd_target, environment.image, CUR_ENVSIZE);
1213 }
1214
Joe Hershberger671adc42012-10-03 09:38:46 +00001215 rc = flash_write_buf(dev_target, fd_target, environment.image,
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001216 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001217 if (rc < 0)
1218 return rc;
1219
1220 if (environment.flag_scheme == FLAG_BOOLEAN) {
1221 /* Have to set obsolete flag */
Alex Kiernane9c62202018-03-09 12:12:59 +00001222 off_t offset = DEVOFFSET(dev_current) +
1223 offsetof(struct env_image_redundant, flags);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001224#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001225 fprintf(stderr,
Stefan Agnerb21e6022016-07-13 17:14:38 -07001226 "Setting obsolete flag in environment at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001227 DEVOFFSET(dev_current), DEVNAME(dev_current));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001228#endif
Alex Kiernane9c62202018-03-09 12:12:59 +00001229 flash_flag_obsolete(dev_current, fd_current, offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001230 }
1231
1232 return 0;
1233}
1234
Alex Kiernane9c62202018-03-09 12:12:59 +00001235static int flash_read(int fd)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001236{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001237 int rc;
1238
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001239 if (IS_UBI(dev_current)) {
1240 DEVTYPE(dev_current) = MTD_ABSENT;
1241
1242 return ubi_read(fd, environment.image, CUR_ENVSIZE);
1243 }
1244
Joe Hershberger671adc42012-10-03 09:38:46 +00001245 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001246 DEVOFFSET(dev_current));
Marek Vasut05ed2a52014-03-05 19:59:52 +01001247 if (rc != CUR_ENVSIZE)
1248 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001249
Marek Vasut05ed2a52014-03-05 19:59:52 +01001250 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001251}
1252
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001253static int flash_open_tempfile(const char **dname, const char **target_temp)
1254{
1255 char *dup_name = strdup(DEVNAME(dev_current));
1256 char *temp_name = NULL;
1257 int rc = -1;
1258
1259 if (!dup_name)
1260 return -1;
1261
1262 *dname = dirname(dup_name);
1263 if (!*dname)
1264 goto err;
1265
1266 rc = asprintf(&temp_name, "%s/XXXXXX", *dname);
1267 if (rc == -1)
1268 goto err;
1269
1270 rc = mkstemp(temp_name);
1271 if (rc == -1) {
1272 /* fall back to in place write */
1273 fprintf(stderr,
1274 "Can't create %s: %s\n", temp_name, strerror(errno));
1275 free(temp_name);
1276 } else {
1277 *target_temp = temp_name;
1278 /* deliberately leak dup_name as dname /might/ point into
1279 * it and we need it for our caller
1280 */
1281 dup_name = NULL;
1282 }
1283
1284err:
1285 if (dup_name)
1286 free(dup_name);
1287
1288 return rc;
1289}
1290
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001291static int flash_io_write(int fd_current)
1292{
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001293 int fd_target = -1, rc, dev_target;
1294 const char *dname, *target_temp = NULL;
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001295
1296 if (have_redund_env) {
1297 /* switch to next partition for writing */
1298 dev_target = !dev_current;
1299 /* dev_target: fd_target, erase_target */
1300 fd_target = open(DEVNAME(dev_target), O_RDWR);
1301 if (fd_target < 0) {
1302 fprintf(stderr,
1303 "Can't open %s: %s\n",
1304 DEVNAME(dev_target), strerror(errno));
1305 rc = -1;
1306 goto exit;
1307 }
1308 } else {
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001309 struct stat sb;
1310
1311 if (fstat(fd_current, &sb) == 0 && S_ISREG(sb.st_mode)) {
1312 /* if any part of flash_open_tempfile() fails we fall
1313 * back to in-place writes
1314 */
1315 fd_target = flash_open_tempfile(&dname, &target_temp);
1316 }
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001317 dev_target = dev_current;
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001318 if (fd_target == -1)
1319 fd_target = fd_current;
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001320 }
1321
1322 rc = flash_write(fd_current, fd_target, dev_target);
1323
1324 if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) {
1325 fprintf(stderr,
1326 "fsync failed on %s: %s\n",
1327 DEVNAME(dev_current), strerror(errno));
1328 }
1329
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001330 if (fd_current != fd_target) {
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001331 if (fsync(fd_target) &&
1332 !(errno == EINVAL || errno == EROFS)) {
1333 fprintf(stderr,
1334 "fsync failed on %s: %s\n",
1335 DEVNAME(dev_current), strerror(errno));
1336 }
1337
1338 if (close(fd_target)) {
1339 fprintf(stderr,
1340 "I/O error on %s: %s\n",
1341 DEVNAME(dev_target), strerror(errno));
1342 rc = -1;
1343 }
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001344
Alex Kiernanad4f3d02019-06-12 15:27:55 +01001345 if (rc >= 0 && target_temp) {
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001346 int dir_fd;
1347
1348 dir_fd = open(dname, O_DIRECTORY | O_RDONLY);
1349 if (dir_fd == -1)
1350 fprintf(stderr,
1351 "Can't open %s: %s\n",
1352 dname, strerror(errno));
1353
1354 if (rename(target_temp, DEVNAME(dev_target))) {
1355 fprintf(stderr,
1356 "rename failed %s => %s: %s\n",
1357 target_temp, DEVNAME(dev_target),
1358 strerror(errno));
1359 rc = -1;
1360 }
1361
1362 if (dir_fd != -1 && fsync(dir_fd))
1363 fprintf(stderr,
1364 "fsync failed on %s: %s\n",
1365 dname, strerror(errno));
1366
1367 if (dir_fd != -1 && close(dir_fd))
1368 fprintf(stderr,
1369 "I/O error on %s: %s\n",
1370 dname, strerror(errno));
1371 }
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001372 }
1373 exit:
1374 return rc;
1375}
1376
Alex Kiernane9c62202018-03-09 12:12:59 +00001377static int flash_io(int mode)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001378{
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001379 int fd_current, rc;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001380
1381 /* dev_current: fd_current, erase_current */
Alex Kiernane9c62202018-03-09 12:12:59 +00001382 fd_current = open(DEVNAME(dev_current), mode);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001383 if (fd_current < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001384 fprintf(stderr,
1385 "Can't open %s: %s\n",
1386 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001387 return -1;
1388 }
1389
1390 if (mode == O_RDWR) {
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001391 rc = flash_io_write(fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001392 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001393 rc = flash_read(fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001394 }
1395
Alex Kiernane9c62202018-03-09 12:12:59 +00001396 if (close(fd_current)) {
1397 fprintf(stderr,
1398 "I/O error on %s: %s\n",
1399 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001400 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001401 }
1402
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001403 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001404}
1405
1406/*
wdenk34b613e2002-12-17 01:51:00 +00001407 * Prevent confusion if running from erased flash memory
1408 */
Andreas Fenkart24371902016-04-05 23:13:42 +02001409int fw_env_open(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001410{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001411 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001412 unsigned char flag0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001413 void *addr0 = NULL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001414
wdenk34b613e2002-12-17 01:51:00 +00001415 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001416 unsigned char flag1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001417 void *addr1 = NULL;
wdenke7f34c62003-01-11 09:48:40 +00001418
Marek Vasut05ed2a52014-03-05 19:59:52 +01001419 int ret;
1420
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001421 struct env_image_single *single;
1422 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001423
Andreas Fenkarte6511302016-05-31 09:21:56 +02001424 if (!opts)
1425 opts = &default_opts;
1426
Alex Kiernane9c62202018-03-09 12:12:59 +00001427 if (parse_config(opts)) /* should fill envdevices */
Stefano Babic4b0a2932017-04-05 18:08:03 +02001428 return -EINVAL;
wdenk3f9ab982003-04-12 23:38:12 +00001429
Joe Hershberger671adc42012-10-03 09:38:46 +00001430 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001431 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001432 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001433 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001434 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001435 ret = -ENOMEM;
1436 goto open_cleanup;
wdenke7f34c62003-01-11 09:48:40 +00001437 }
wdenk3f9ab982003-04-12 23:38:12 +00001438
wdenke7f34c62003-01-11 09:48:40 +00001439 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001440 environment.image = addr0;
1441
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001442 if (have_redund_env) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001443 redundant = addr0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001444 environment.crc = &redundant->crc;
1445 environment.flags = &redundant->flags;
1446 environment.data = redundant->data;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001447 } else {
1448 single = addr0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001449 environment.crc = &single->crc;
1450 environment.flags = NULL;
1451 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001452 }
wdenk3f9ab982003-04-12 23:38:12 +00001453
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001454 dev_current = 0;
Tom Rini4c1192c2018-07-01 22:10:33 -04001455 if (flash_io(O_RDONLY)) {
Stefano Babic4b0a2932017-04-05 18:08:03 +02001456 ret = -EIO;
1457 goto open_cleanup;
1458 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001459
Tom Rini4c1192c2018-07-01 22:10:33 -04001460 crc0 = crc32(0, (uint8_t *)environment.data, ENV_SIZE);
1461
1462 crc0_ok = (crc0 == *environment.crc);
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001463 if (!have_redund_env) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001464 if (!crc0_ok) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001465 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001466 "Warning: Bad CRC, using default environment\n");
Alex Kiernane9c62202018-03-09 12:12:59 +00001467 memcpy(environment.data, default_environment,
1468 sizeof(default_environment));
Rasmus Villemoes06a79642018-09-27 22:45:26 +02001469 environment.dirty = 1;
wdenk34b613e2002-12-17 01:51:00 +00001470 }
wdenke7f34c62003-01-11 09:48:40 +00001471 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001472 flag0 = *environment.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 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001483 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001484
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001485 /*
1486 * have to set environment.image for flash_read(), careful -
1487 * other pointers in environment still point inside addr0
1488 */
1489 environment.image = addr1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001490 if (flash_io(O_RDONLY)) {
Tom Rini4c1192c2018-07-01 22:10:33 -04001491 ret = -EIO;
1492 goto open_cleanup;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001493 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001494
1495 /* Check flag scheme compatibility */
1496 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1497 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1498 environment.flag_scheme = FLAG_BOOLEAN;
1499 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1500 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1501 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001502 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1503 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1504 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001505 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1506 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1507 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001508 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001509 DEVTYPE(!dev_current) == MTD_ABSENT &&
1510 IS_UBI(dev_current) == IS_UBI(!dev_current)) {
Oliver Metze745d4c2013-08-30 00:56:01 +02001511 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001512 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001513 fprintf(stderr, "Incompatible flash types!\n");
Stefano Babic4b0a2932017-04-05 18:08:03 +02001514 ret = -EINVAL;
1515 goto open_cleanup;
wdenk34b613e2002-12-17 01:51:00 +00001516 }
wdenk3f9ab982003-04-12 23:38:12 +00001517
Tom Rini4c1192c2018-07-01 22:10:33 -04001518 crc1 = crc32(0, (uint8_t *)redundant->data, ENV_SIZE);
1519
1520 crc1_ok = (crc1 == redundant->crc);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001521 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001522
Rasmus Villemoes06a79642018-09-27 22:45:26 +02001523 /*
1524 * environment.data still points to ((struct
1525 * env_image_redundant *)addr0)->data. If the two
1526 * environments differ, or one has bad crc, force a
1527 * write-out by marking the environment dirty.
1528 */
1529 if (memcmp(environment.data, redundant->data, ENV_SIZE) ||
1530 !crc0_ok || !crc1_ok)
1531 environment.dirty = 1;
1532
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001533 if (crc0_ok && !crc1_ok) {
1534 dev_current = 0;
1535 } else if (!crc0_ok && crc1_ok) {
1536 dev_current = 1;
1537 } else if (!crc0_ok && !crc1_ok) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001538 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001539 "Warning: Bad CRC, using default environment\n");
Alex Kiernane9c62202018-03-09 12:12:59 +00001540 memcpy(environment.data, default_environment,
1541 sizeof(default_environment));
Rasmus Villemoes06a79642018-09-27 22:45:26 +02001542 environment.dirty = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001543 dev_current = 0;
1544 } else {
1545 switch (environment.flag_scheme) {
1546 case FLAG_BOOLEAN:
Simon Glass4686d8f2019-08-01 09:47:08 -06001547 if (flag0 == ENV_REDUND_ACTIVE &&
1548 flag1 == ENV_REDUND_OBSOLETE) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001549 dev_current = 0;
Simon Glass4686d8f2019-08-01 09:47:08 -06001550 } else if (flag0 == ENV_REDUND_OBSOLETE &&
1551 flag1 == ENV_REDUND_ACTIVE) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001552 dev_current = 1;
1553 } else if (flag0 == flag1) {
1554 dev_current = 0;
1555 } else if (flag0 == 0xFF) {
1556 dev_current = 0;
1557 } else if (flag1 == 0xFF) {
1558 dev_current = 1;
1559 } else {
1560 dev_current = 0;
1561 }
1562 break;
1563 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001564 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001565 dev_current = 1;
1566 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001567 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001568 dev_current = 0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001569 else /* flag1 > flag0 */
Jon Povey93ccd472011-03-11 14:10:56 +09001570 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001571 break;
1572 default:
Alex Kiernane9c62202018-03-09 12:12:59 +00001573 fprintf(stderr, "Unknown flag scheme %u\n",
1574 environment.flag_scheme);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001575 return -1;
1576 }
1577 }
1578
1579 /*
1580 * If we are reading, we don't need the flag and the CRC any
1581 * more, if we are writing, we will re-calculate CRC and update
1582 * flags before writing out
1583 */
1584 if (dev_current) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001585 environment.image = addr1;
1586 environment.crc = &redundant->crc;
1587 environment.flags = &redundant->flags;
1588 environment.data = redundant->data;
1589 free(addr0);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001590 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001591 environment.image = addr0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001592 /* Other pointers are already set */
Alex Kiernane9c62202018-03-09 12:12:59 +00001593 free(addr1);
wdenk34b613e2002-12-17 01:51:00 +00001594 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001595#ifdef DEBUG
1596 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1597#endif
wdenk34b613e2002-12-17 01:51:00 +00001598 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001599 return 0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001600
Alex Kiernane9c62202018-03-09 12:12:59 +00001601 open_cleanup:
Stefano Babic4b0a2932017-04-05 18:08:03 +02001602 if (addr0)
1603 free(addr0);
1604
1605 if (addr1)
Björn Stenberg4d1734d2019-04-17 16:56:54 +02001606 free(addr1);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001607
1608 return ret;
1609}
1610
1611/*
1612 * Simply free allocated buffer with environment
1613 */
1614int fw_env_close(struct env_opts *opts)
1615{
1616 if (environment.image)
1617 free(environment.image);
1618
1619 environment.image = NULL;
1620
1621 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001622}
1623
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001624static int check_device_config(int dev)
1625{
1626 struct stat st;
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001627 int32_t lnum = 0;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001628 int fd, rc = 0;
1629
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001630 /* Fills in IS_UBI(), converts DEVNAME() with ubi volume name */
1631 ubi_check_dev(dev);
1632
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001633 fd = open(DEVNAME(dev), O_RDONLY);
1634 if (fd < 0) {
1635 fprintf(stderr,
Alex Kiernane9c62202018-03-09 12:12:59 +00001636 "Cannot open %s: %s\n", DEVNAME(dev), strerror(errno));
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001637 return -1;
1638 }
1639
1640 rc = fstat(fd, &st);
1641 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001642 fprintf(stderr, "Cannot stat the file %s\n", DEVNAME(dev));
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001643 goto err;
1644 }
1645
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001646 if (IS_UBI(dev)) {
1647 rc = ioctl(fd, UBI_IOCEBISMAP, &lnum);
1648 if (rc < 0) {
1649 fprintf(stderr, "Cannot get UBI information for %s\n",
1650 DEVNAME(dev));
1651 goto err;
1652 }
1653 } else if (S_ISCHR(st.st_mode)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001654 struct mtd_info_user mtdinfo;
1655 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1656 if (rc < 0) {
1657 fprintf(stderr, "Cannot get MTD information for %s\n",
1658 DEVNAME(dev));
1659 goto err;
1660 }
1661 if (mtdinfo.type != MTD_NORFLASH &&
1662 mtdinfo.type != MTD_NANDFLASH &&
1663 mtdinfo.type != MTD_DATAFLASH &&
1664 mtdinfo.type != MTD_UBIVOLUME) {
1665 fprintf(stderr, "Unsupported flash type %u on %s\n",
1666 mtdinfo.type, DEVNAME(dev));
1667 goto err;
1668 }
1669 DEVTYPE(dev) = mtdinfo.type;
Rasmus Villemoesfaf29982020-03-24 13:57:40 +01001670 if (DEVESIZE(dev) == 0 && ENVSECTORS(dev) == 0 &&
1671 mtdinfo.type == MTD_NORFLASH)
1672 DEVESIZE(dev) = mtdinfo.erasesize;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001673 if (DEVESIZE(dev) == 0)
1674 /* Assume the erase size is the same as the env-size */
1675 DEVESIZE(dev) = ENVSIZE(dev);
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001676 } else {
Stefan Agnerb21e6022016-07-13 17:14:38 -07001677 uint64_t size;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001678 DEVTYPE(dev) = MTD_ABSENT;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001679 if (DEVESIZE(dev) == 0)
1680 /* Assume the erase size to be 512 bytes */
1681 DEVESIZE(dev) = 0x200;
Stefan Agnerb21e6022016-07-13 17:14:38 -07001682
1683 /*
1684 * Check for negative offsets, treat it as backwards offset
1685 * from the end of the block device
1686 */
1687 if (DEVOFFSET(dev) < 0) {
1688 rc = ioctl(fd, BLKGETSIZE64, &size);
1689 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001690 fprintf(stderr,
1691 "Could not get block device size on %s\n",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001692 DEVNAME(dev));
1693 goto err;
1694 }
1695
1696 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1697#ifdef DEBUG
Alex Kiernane9c62202018-03-09 12:12:59 +00001698 fprintf(stderr,
1699 "Calculated device offset 0x%llx on %s\n",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001700 DEVOFFSET(dev), DEVNAME(dev));
1701#endif
1702 }
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001703 }
1704
Max Krummenacher6c44f542016-11-19 13:58:56 +01001705 if (ENVSECTORS(dev) == 0)
1706 /* Assume enough sectors to cover the environment */
1707 ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev));
1708
1709 if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001710 fprintf(stderr,
1711 "Environment does not start on (erase) block boundary\n");
Max Krummenacher6c44f542016-11-19 13:58:56 +01001712 errno = EINVAL;
1713 return -1;
1714 }
1715
1716 if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001717 fprintf(stderr,
1718 "Environment does not fit into available sectors\n");
Max Krummenacher6c44f542016-11-19 13:58:56 +01001719 errno = EINVAL;
1720 return -1;
1721 }
1722
Alex Kiernane9c62202018-03-09 12:12:59 +00001723 err:
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001724 close(fd);
1725 return rc;
1726}
wdenk34b613e2002-12-17 01:51:00 +00001727
Andreas Fenkart24371902016-04-05 23:13:42 +02001728static int parse_config(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001729{
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001730 int rc;
wdenk34b613e2002-12-17 01:51:00 +00001731
Andreas Fenkarte6511302016-05-31 09:21:56 +02001732 if (!opts)
1733 opts = &default_opts;
Anatolij Gustschine89ead32016-04-29 22:00:11 +02001734
Andreas Fenkarte6511302016-05-31 09:21:56 +02001735#if defined(CONFIG_FILE)
wdenke7f34c62003-01-11 09:48:40 +00001736 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart24371902016-04-05 23:13:42 +02001737 if (get_config(opts->config_file)) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001738 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart24371902016-04-05 23:13:42 +02001739 opts->config_file);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001740 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001741 }
wdenke7f34c62003-01-11 09:48:40 +00001742#else
Alex Kiernane9c62202018-03-09 12:12:59 +00001743 DEVNAME(0) = DEVICE1_NAME;
1744 DEVOFFSET(0) = DEVICE1_OFFSET;
1745 ENVSIZE(0) = ENV1_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001746
1747 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1748 * know DEVTYPE
1749 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001750#ifdef DEVICE1_ESIZE
Alex Kiernane9c62202018-03-09 12:12:59 +00001751 DEVESIZE(0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001752#endif
1753#ifdef DEVICE1_ENVSECTORS
Alex Kiernane9c62202018-03-09 12:12:59 +00001754 ENVSECTORS(0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001755#endif
1756
wdenk34b613e2002-12-17 01:51:00 +00001757#ifdef HAVE_REDUND
Alex Kiernane9c62202018-03-09 12:12:59 +00001758 DEVNAME(1) = DEVICE2_NAME;
1759 DEVOFFSET(1) = DEVICE2_OFFSET;
1760 ENVSIZE(1) = ENV2_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001761
1762 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1763 * know DEVTYPE
1764 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001765#ifdef DEVICE2_ESIZE
Alex Kiernane9c62202018-03-09 12:12:59 +00001766 DEVESIZE(1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001767#endif
1768#ifdef DEVICE2_ENVSECTORS
Alex Kiernane9c62202018-03-09 12:12:59 +00001769 ENVSECTORS(1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001770#endif
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001771 have_redund_env = 1;
wdenk34b613e2002-12-17 01:51:00 +00001772#endif
wdenke7f34c62003-01-11 09:48:40 +00001773#endif
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001774 rc = check_device_config(0);
1775 if (rc < 0)
1776 return rc;
wdenk3f9ab982003-04-12 23:38:12 +00001777
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001778 if (have_redund_env) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001779 rc = check_device_config(1);
1780 if (rc < 0)
1781 return rc;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001782
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001783 if (ENVSIZE(0) != ENVSIZE(1)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001784 fprintf(stderr,
Philip Molloyf60f7512019-03-31 03:44:57 +00001785 "Redundant environments have unequal size\n");
Andreas Fenkart31f2e052016-08-17 23:41:53 +02001786 return -1;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001787 }
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001788 }
1789
1790 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001791 if (have_redund_env)
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001792 usable_envsize -= sizeof(char);
1793
wdenk34b613e2002-12-17 01:51:00 +00001794 return 0;
1795}
wdenke7f34c62003-01-11 09:48:40 +00001796
1797#if defined(CONFIG_FILE)
Alex Kiernane9c62202018-03-09 12:12:59 +00001798static int get_config(char *fname)
wdenke7f34c62003-01-11 09:48:40 +00001799{
1800 FILE *fp;
1801 int i = 0;
1802 int rc;
Alex Kiernan66df4e22018-06-07 12:20:05 +00001803 char *line = NULL;
1804 size_t linesize = 0;
Tom Rini313e9122014-03-28 12:03:33 -04001805 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001806
Alex Kiernane9c62202018-03-09 12:12:59 +00001807 fp = fopen(fname, "r");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001808 if (fp == NULL)
1809 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001810
Alex Kiernan66df4e22018-06-07 12:20:05 +00001811 while (i < 2 && getline(&line, &linesize, fp) != -1) {
1812 /* Skip comment strings */
1813 if (line[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001814 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001815
Alex Kiernan66df4e22018-06-07 12:20:05 +00001816 rc = sscanf(line, "%ms %lli %lx %lx %lx",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001817 &devname,
1818 &DEVOFFSET(i),
Alex Kiernane9c62202018-03-09 12:12:59 +00001819 &ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001820
Remy Bohmercb969eb2011-02-12 19:06:26 +01001821 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001822 continue;
1823
Tom Rini313e9122014-03-28 12:03:33 -04001824 DEVNAME(i) = devname;
1825
Max Krummenacher6c44f542016-11-19 13:58:56 +01001826 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1827 * know DEVTYPE
1828 */
wdenke7f34c62003-01-11 09:48:40 +00001829
1830 i++;
1831 }
Alex Kiernan66df4e22018-06-07 12:20:05 +00001832 free(line);
Alex Kiernane9c62202018-03-09 12:12:59 +00001833 fclose(fp);
wdenk3f9ab982003-04-12 23:38:12 +00001834
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001835 have_redund_env = i - 1;
Alex Kiernane9c62202018-03-09 12:12:59 +00001836 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001837 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001838 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001839 } else
1840 return 0;
1841}
1842#endif