blob: 95c99847a3af2647aef191deea2361293625a6fd [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>
28#include <unistd.h>
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080029#include <dirent.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010030
31#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020032# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010033# include <linux/mtd/mtd.h>
34#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010035# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010036# include <mtd/mtd-user.h>
37#endif
wdenk34b613e2002-12-17 01:51:00 +000038
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080039#include <mtd/ubi-user.h>
40
Stefano Babic831c4d52017-04-05 18:08:01 +020041#include "fw_env_private.h"
Markus Klotzbücher94db5572007-11-27 10:23:20 +010042#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000043
Andreas Fenkarte6511302016-05-31 09:21:56 +020044struct env_opts default_opts = {
45#ifdef CONFIG_FILE
46 .config_file = CONFIG_FILE
47#endif
48};
49
Marek Vasut05ed2a52014-03-05 19:59:52 +010050#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
51
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020052#define min(x, y) ({ \
53 typeof(x) _min1 = (x); \
54 typeof(y) _min2 = (y); \
55 (void) (&_min1 == &_min2); \
56 _min1 < _min2 ? _min1 : _min2; })
57
58struct envdev_s {
Tom Rini313e9122014-03-28 12:03:33 -040059 const char *devname; /* Device name */
Stefan Agnerb21e6022016-07-13 17:14:38 -070060 long long devoff; /* Device offset */
wdenk3f9ab982003-04-12 23:38:12 +000061 ulong env_size; /* environment size */
62 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020063 ulong env_sectors; /* number of environment sectors */
64 uint8_t mtd_type; /* type of the MTD device */
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080065 int is_ubi; /* set if we use UBI volume */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020066};
wdenk34b613e2002-12-17 01:51:00 +000067
Alex Kiernane9c62202018-03-09 12:12:59 +000068static struct envdev_s envdevices[2] = {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020069 {
70 .mtd_type = MTD_ABSENT,
71 }, {
72 .mtd_type = MTD_ABSENT,
73 },
74};
Alex Kiernane9c62202018-03-09 12:12:59 +000075
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020076static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000077
78#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000079#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000080#define ENVSIZE(i) envdevices[(i)].env_size
81#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020082#define ENVSECTORS(i) envdevices[(i)].env_sectors
83#define DEVTYPE(i) envdevices[(i)].mtd_type
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080084#define IS_UBI(i) envdevices[(i)].is_ubi
wdenk34b613e2002-12-17 01:51:00 +000085
Joe Hershberger671adc42012-10-03 09:38:46 +000086#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000087
Andreas Fenkartb8bf4042016-04-19 22:43:42 +020088static unsigned long usable_envsize;
89#define ENV_SIZE usable_envsize
wdenk34b613e2002-12-17 01:51:00 +000090
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020091struct env_image_single {
Alex Kiernane9c62202018-03-09 12:12:59 +000092 uint32_t crc; /* CRC32 over data bytes */
93 char data[];
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020094};
wdenk34b613e2002-12-17 01:51:00 +000095
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020096struct env_image_redundant {
Alex Kiernane9c62202018-03-09 12:12:59 +000097 uint32_t crc; /* CRC32 over data bytes */
98 unsigned char flags; /* active or obsolete */
99 char data[];
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200100};
101
102enum flag_scheme {
103 FLAG_NONE,
104 FLAG_BOOLEAN,
105 FLAG_INCREMENTAL,
106};
107
108struct environment {
Alex Kiernane9c62202018-03-09 12:12:59 +0000109 void *image;
110 uint32_t *crc;
111 unsigned char *flags;
112 char *data;
113 enum flag_scheme flag_scheme;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200114};
115
116static struct environment environment = {
117 .flag_scheme = FLAG_NONE,
118};
wdenk34b613e2002-12-17 01:51:00 +0000119
Alex Kiernanf46d0ca2018-03-09 12:13:00 +0000120static int have_redund_env;
wdenke7f34c62003-01-11 09:48:40 +0000121
Simon Glass4686d8f2019-08-01 09:47:08 -0600122static unsigned char ENV_REDUND_ACTIVE = 1;
123/*
124 * ENV_REDUND_OBSOLETE must be 0 to efficiently set it on NOR flash without
125 * erasing
126 */
127static unsigned char ENV_REDUND_OBSOLETE;
wdenke7f34c62003-01-11 09:48:40 +0000128
Joe Hershberger81321142012-10-12 08:48:51 +0000129#define DEFAULT_ENV_INSTANCE_STATIC
130#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000131
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -0800132#define UBI_DEV_START "/dev/ubi"
133#define UBI_SYSFS "/sys/class/ubi"
134#define UBI_VOL_NAME_PATT "ubi%d_%d"
135
136static int is_ubi_devname(const char *devname)
137{
138 return !strncmp(devname, UBI_DEV_START, sizeof(UBI_DEV_START) - 1);
139}
140
141static int ubi_check_volume_sysfs_name(const char *volume_sysfs_name,
142 const char *volname)
143{
144 char path[256];
145 FILE *file;
146 char *name;
147 int ret;
148
149 strcpy(path, UBI_SYSFS "/");
150 strcat(path, volume_sysfs_name);
151 strcat(path, "/name");
152
153 file = fopen(path, "r");
154 if (!file)
155 return -1;
156
157 ret = fscanf(file, "%ms", &name);
158 fclose(file);
159 if (ret <= 0 || !name) {
160 fprintf(stderr,
161 "Failed to read from file %s, ret = %d, name = %s\n",
162 path, ret, name);
163 return -1;
164 }
165
166 if (!strcmp(name, volname)) {
167 free(name);
168 return 0;
169 }
170 free(name);
171
172 return -1;
173}
174
175static int ubi_get_volnum_by_name(int devnum, const char *volname)
176{
177 DIR *sysfs_ubi;
178 struct dirent *dirent;
179 int ret;
180 int tmp_devnum;
181 int volnum;
182
183 sysfs_ubi = opendir(UBI_SYSFS);
184 if (!sysfs_ubi)
185 return -1;
186
187#ifdef DEBUG
188 fprintf(stderr, "Looking for volume name \"%s\"\n", volname);
189#endif
190
191 while (1) {
192 dirent = readdir(sysfs_ubi);
193 if (!dirent)
194 return -1;
195
196 ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT,
197 &tmp_devnum, &volnum);
198 if (ret == 2 && devnum == tmp_devnum) {
199 if (ubi_check_volume_sysfs_name(dirent->d_name,
200 volname) == 0)
201 return volnum;
202 }
203 }
204
205 return -1;
206}
207
208static int ubi_get_devnum_by_devname(const char *devname)
209{
210 int devnum;
211 int ret;
212
213 ret = sscanf(devname + sizeof(UBI_DEV_START) - 1, "%d", &devnum);
214 if (ret != 1)
215 return -1;
216
217 return devnum;
218}
219
220static const char *ubi_get_volume_devname(const char *devname,
221 const char *volname)
222{
223 char *volume_devname;
224 int volnum;
225 int devnum;
226 int ret;
227
228 devnum = ubi_get_devnum_by_devname(devname);
229 if (devnum < 0)
230 return NULL;
231
232 volnum = ubi_get_volnum_by_name(devnum, volname);
233 if (volnum < 0)
234 return NULL;
235
236 ret = asprintf(&volume_devname, "%s_%d", devname, volnum);
237 if (ret < 0)
238 return NULL;
239
240#ifdef DEBUG
241 fprintf(stderr, "Found ubi volume \"%s:%s\" -> %s\n",
242 devname, volname, volume_devname);
243#endif
244
245 return volume_devname;
246}
247
248static void ubi_check_dev(unsigned int dev_id)
249{
250 char *devname = (char *)DEVNAME(dev_id);
251 char *pname;
252 const char *volname = NULL;
253 const char *volume_devname;
254
255 if (!is_ubi_devname(DEVNAME(dev_id)))
256 return;
257
258 IS_UBI(dev_id) = 1;
259
260 for (pname = devname; *pname != '\0'; pname++) {
261 if (*pname == ':') {
262 *pname = '\0';
263 volname = pname + 1;
264 break;
265 }
266 }
267
268 if (volname) {
269 /* Let's find real volume device name */
270 volume_devname = ubi_get_volume_devname(devname, volname);
271 if (!volume_devname) {
272 fprintf(stderr, "Didn't found ubi volume \"%s\"\n",
273 volname);
274 return;
275 }
276
277 free(devname);
278 DEVNAME(dev_id) = volume_devname;
279 }
280}
281
282static int ubi_update_start(int fd, int64_t bytes)
283{
284 if (ioctl(fd, UBI_IOCVOLUP, &bytes))
285 return -1;
286 return 0;
287}
288
289static int ubi_read(int fd, void *buf, size_t count)
290{
291 ssize_t ret;
292
293 while (count > 0) {
294 ret = read(fd, buf, count);
295 if (ret > 0) {
296 count -= ret;
297 buf += ret;
298
299 continue;
300 }
301
302 if (ret == 0) {
303 /*
304 * Happens in case of too short volume data size. If we
305 * return error status we will fail it will be treated
306 * as UBI device error.
307 *
308 * Leave catching this error to CRC check.
309 */
310 fprintf(stderr, "Warning: end of data on ubi volume\n");
311 return 0;
312 } else if (errno == EBADF) {
313 /*
314 * Happens in case of corrupted volume. The same as
315 * above, we cannot return error now, as we will still
316 * be able to successfully write environment later.
317 */
318 fprintf(stderr, "Warning: corrupted volume?\n");
319 return 0;
320 } else if (errno == EINTR) {
321 continue;
322 }
323
324 fprintf(stderr, "Cannot read %u bytes from ubi volume, %s\n",
325 (unsigned int)count, strerror(errno));
326 return -1;
327 }
328
329 return 0;
330}
331
332static int ubi_write(int fd, const void *buf, size_t count)
333{
334 ssize_t ret;
335
336 while (count > 0) {
337 ret = write(fd, buf, count);
338 if (ret <= 0) {
339 if (ret < 0 && errno == EINTR)
340 continue;
341
342 fprintf(stderr, "Cannot write %u bytes to ubi volume\n",
343 (unsigned int)count);
344 return -1;
345 }
346
347 count -= ret;
348 buf += ret;
349 }
350
351 return 0;
352}
353
Alex Kiernane9c62202018-03-09 12:12:59 +0000354static int flash_io(int mode);
Andreas Fenkart24371902016-04-05 23:13:42 +0200355static int parse_config(struct env_opts *opts);
wdenk3f9ab982003-04-12 23:38:12 +0000356
wdenke7f34c62003-01-11 09:48:40 +0000357#if defined(CONFIG_FILE)
Alex Kiernane9c62202018-03-09 12:12:59 +0000358static int get_config(char *);
wdenke7f34c62003-01-11 09:48:40 +0000359#endif
wdenk34b613e2002-12-17 01:51:00 +0000360
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100361static char *skip_chars(char *s)
Stefano Babicada628b2010-05-24 12:08:16 +0200362{
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100363 for (; *s != '\0'; s++) {
Stefan Agner87833242018-03-01 14:06:32 +0100364 if (isblank(*s) || *s == '=')
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100365 return s;
366 }
367 return NULL;
368}
Stefano Babicada628b2010-05-24 12:08:16 +0200369
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100370static char *skip_blanks(char *s)
371{
372 for (; *s != '\0'; s++) {
373 if (!isblank(*s))
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100374 return s;
Stefano Babicada628b2010-05-24 12:08:16 +0200375 }
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100376 return NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200377}
378
wdenk34b613e2002-12-17 01:51:00 +0000379/*
Andreas Fenkart3ff10742016-07-16 17:06:14 +0200380 * s1 is either a simple 'name', or a 'name=value' pair.
381 * s2 is a 'name=value' pair.
382 * If the names match, return the value of s2, else NULL.
383 */
384static char *envmatch(char *s1, char *s2)
385{
386 if (s1 == NULL || s2 == NULL)
387 return NULL;
388
389 while (*s1 == *s2++)
390 if (*s1++ == '=')
391 return s2;
392 if (*s1 == '\0' && *(s2 - 1) == '=')
393 return s2;
394 return NULL;
395}
396
397/**
wdenk34b613e2002-12-17 01:51:00 +0000398 * Search the environment for a variable.
399 * Return the value, if found, or NULL, if not found.
400 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000401char *fw_getenv(char *name)
wdenk34b613e2002-12-17 01:51:00 +0000402{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100403 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000404
wdenk3f9ab982003-04-12 23:38:12 +0000405 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100406 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000407
wdenk3f9ab982003-04-12 23:38:12 +0000408 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000409 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000410 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000411 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200412 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000413 }
414 }
Alex Kiernane9c62202018-03-09 12:12:59 +0000415 val = envmatch(name, env);
wdenk34b613e2002-12-17 01:51:00 +0000416 if (!val)
417 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200418 return val;
wdenk34b613e2002-12-17 01:51:00 +0000419 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200420 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000421}
422
423/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600424 * Search the default environment for a variable.
425 * Return the value, if found, or NULL, if not found.
426 */
427char *fw_getdefenv(char *name)
428{
429 char *env, *nxt;
430
431 for (env = default_environment; *env; env = nxt + 1) {
432 char *val;
433
434 for (nxt = env; *nxt; ++nxt) {
435 if (nxt >= &default_environment[ENV_SIZE]) {
436 fprintf(stderr, "## Error: "
437 "default environment not terminated\n");
438 return NULL;
439 }
440 }
441 val = envmatch(name, env);
442 if (!val)
443 continue;
444 return val;
445 }
446 return NULL;
447}
448
449/*
wdenk34b613e2002-12-17 01:51:00 +0000450 * Print the current definition of one, or more, or all
451 * environment variables
452 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200453int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000454{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100455 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000456
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200457 if (value_only && argc != 1) {
458 fprintf(stderr,
Alex Kiernan688cee92018-02-11 17:16:46 +0000459 "## Error: `-n'/`--noheader' option requires exactly one argument\n");
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200460 return -1;
461 }
462
Andreas Fenkarte6511302016-05-31 09:21:56 +0200463 if (!opts)
464 opts = &default_opts;
465
Andreas Fenkart24371902016-04-05 23:13:42 +0200466 if (fw_env_open(opts))
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200467 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000468
Alex Kiernane9c62202018-03-09 12:12:59 +0000469 if (argc == 0) { /* Print all env variables */
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200470 char *env, *nxt;
wdenk3f9ab982003-04-12 23:38:12 +0000471 for (env = environment.data; *env; env = nxt + 1) {
472 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000473 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000474 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000475 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200476 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000477 }
478 }
479
Alex Kiernane9c62202018-03-09 12:12:59 +0000480 printf("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000481 }
Stefano Babic4b0a2932017-04-05 18:08:03 +0200482 fw_env_close(opts);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200483 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000484 }
485
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200486 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100487 char *name = argv[i];
488 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000489
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200490 val = fw_getenv(name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700491 if (!val) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000492 fprintf(stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700493 rc = -1;
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200494 continue;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700495 }
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200496
497 if (value_only) {
498 puts(val);
499 break;
500 }
501
502 printf("%s=%s\n", name, val);
wdenk34b613e2002-12-17 01:51:00 +0000503 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700504
Stefano Babic4b0a2932017-04-05 18:08:03 +0200505 fw_env_close(opts);
506
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200507 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000508}
509
Stefano Babic4b0a2932017-04-05 18:08:03 +0200510int fw_env_flush(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000511{
Andreas Fenkarte6511302016-05-31 09:21:56 +0200512 if (!opts)
513 opts = &default_opts;
514
Stefano Babicada628b2010-05-24 12:08:16 +0200515 /*
516 * Update CRC
517 */
518 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000519
Stefano Babicada628b2010-05-24 12:08:16 +0200520 /* write environment back to flash */
521 if (flash_io(O_RDWR)) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000522 fprintf(stderr, "Error: can't write fw_env to flash\n");
523 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000524 }
525
Stefano Babicada628b2010-05-24 12:08:16 +0200526 return 0;
527}
wdenk34b613e2002-12-17 01:51:00 +0000528
Stefano Babicada628b2010-05-24 12:08:16 +0200529/*
530 * Set/Clear a single variable in the environment.
531 * This is called in sequence to update the environment
532 * in RAM without updating the copy in flash after each set
533 */
534int fw_env_write(char *name, char *value)
535{
536 int len;
537 char *env, *nxt;
538 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600539 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000540
541 /*
542 * search if variable with this name already exists
543 */
wdenk3f9ab982003-04-12 23:38:12 +0000544 for (nxt = env = environment.data; *env; env = nxt + 1) {
545 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000546 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200547 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000548 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200549 errno = EINVAL;
550 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000551 }
552 }
Alex Kiernane9c62202018-03-09 12:12:59 +0000553 oldval = envmatch(name, env);
554 if (oldval)
wdenk34b613e2002-12-17 01:51:00 +0000555 break;
556 }
557
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600558 deleting = (oldval && !(value && strlen(value)));
559 creating = (!oldval && (value && strlen(value)));
560 overwriting = (oldval && (value && strlen(value)));
561
562 /* check for permission */
563 if (deleting) {
564 if (env_flags_validate_varaccess(name,
565 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
566 printf("Can't delete \"%s\"\n", name);
567 errno = EROFS;
568 return -1;
569 }
570 } else if (overwriting) {
571 if (env_flags_validate_varaccess(name,
572 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
573 printf("Can't overwrite \"%s\"\n", name);
574 errno = EROFS;
575 return -1;
576 } else if (env_flags_validate_varaccess(name,
Alex Kiernane9c62202018-03-09 12:12:59 +0000577 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600578 const char *defval = fw_getdefenv(name);
579
580 if (defval == NULL)
581 defval = "";
582 if (strcmp(oldval, defval)
583 != 0) {
584 printf("Can't overwrite \"%s\"\n", name);
585 errno = EROFS;
586 return -1;
587 }
588 }
589 } else if (creating) {
590 if (env_flags_validate_varaccess(name,
591 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
592 printf("Can't create \"%s\"\n", name);
593 errno = EROFS;
594 return -1;
595 }
596 } else
597 /* Nothing to do */
598 return 0;
599
600 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);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200949 if (rc != readlen) {
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 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200954#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000955 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +0000956 rc, (unsigned long long)blockstart + block_seek,
Marcin Niestroj20890732016-05-06 14:58:29 +0200957 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200958#endif
959 processed += readlen;
Alex Kiernane9c62202018-03-09 12:12:59 +0000960 readlen = min(blocklen, count - processed);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200961 block_seek = 0;
962 blockstart += blocklen;
963 }
964
965 return processed;
966}
967
968/*
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200969 * Write count bytes from begin of environment, but stay within
970 * ENVSECTORS(dev) sectors of
Remy Bohmercb969eb2011-02-12 19:06:26 +0100971 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
972 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200973 */
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200974static int flash_write_buf(int dev, int fd, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200975{
976 void *data;
977 struct erase_info_user erase;
978 size_t blocklen; /* length of NAND block / NOR erase sector */
979 size_t erase_len; /* whole area that can be erased - may include
980 bad blocks */
981 size_t erasesize; /* erase / write length - one block on NAND,
982 whole area on NOR */
983 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100984 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200985 bad blocks */
986 off_t erase_offset; /* offset to the first erase block (aligned)
987 below offset */
988 off_t block_seek; /* offset inside the erase block to the start
989 of the data */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200990 loff_t blockstart; /* running start of the current block -
991 MEMGETBADBLOCK needs 64 bits */
992 int rc;
993
Oliver Metze9c66ab2013-08-30 00:56:02 +0200994 /*
995 * For mtd devices only offset and size of the environment do matter
996 */
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200997 if (DEVTYPE(dev) == MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +0200998 blocklen = count;
Oliver Metze9c66ab2013-08-30 00:56:02 +0200999 erase_len = blocklen;
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001000 blockstart = DEVOFFSET(dev);
Oliver Metze9c66ab2013-08-30 00:56:02 +02001001 block_seek = 0;
1002 write_total = blocklen;
1003 } else {
1004 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001005
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001006 erase_offset = DEVOFFSET(dev);
wdenk34b613e2002-12-17 01:51:00 +00001007
Oliver Metze9c66ab2013-08-30 00:56:02 +02001008 /* Maximum area we may use */
Andreas Fenkart22281582016-08-29 23:16:57 +02001009 erase_len = environment_end(dev) - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +00001010
Oliver Metze9c66ab2013-08-30 00:56:02 +02001011 blockstart = erase_offset;
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001012
Oliver Metze9c66ab2013-08-30 00:56:02 +02001013 /* Offset inside a block */
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001014 block_seek = DEVOFFSET(dev) - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001015
Oliver Metze9c66ab2013-08-30 00:56:02 +02001016 /*
1017 * Data size we actually write: from the start of the block
1018 * to the start of the data, then count bytes of data, and
1019 * to the end of the block
1020 */
1021 write_total = ((block_seek + count + blocklen - 1) /
Alex Kiernane9c62202018-03-09 12:12:59 +00001022 blocklen) * blocklen;
Oliver Metze9c66ab2013-08-30 00:56:02 +02001023 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001024
1025 /*
1026 * Support data anywhere within erase sectors: read out the complete
1027 * area to be erased, replace the environment image, write the whole
1028 * block back again.
1029 */
1030 if (write_total > count) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001031 data = malloc(erase_len);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001032 if (!data) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001033 fprintf(stderr,
1034 "Cannot malloc %zu bytes: %s\n",
1035 erase_len, strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001036 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001037 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001038
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001039 rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001040 if (write_total != rc)
1041 return -1;
1042
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001043#ifdef DEBUG
1044 fprintf(stderr, "Preserving data ");
1045 if (block_seek != 0)
1046 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
1047 if (block_seek + count != write_total) {
1048 if (block_seek != 0)
1049 fprintf(stderr, " and ");
Marcin Niestroj20890732016-05-06 14:58:29 +02001050 fprintf(stderr, "0x%lx - 0x%lx",
Alex Kiernane9c62202018-03-09 12:12:59 +00001051 (unsigned long)block_seek + count,
1052 (unsigned long)write_total - 1);
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001053 }
1054 fprintf(stderr, "\n");
1055#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001056 /* Overwrite the old environment */
Alex Kiernane9c62202018-03-09 12:12:59 +00001057 memcpy(data + block_seek, buf, count);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001058 } else {
1059 /*
1060 * We get here, iff offset is block-aligned and count is a
1061 * multiple of blocklen - see write_total calculation above
1062 */
1063 data = buf;
1064 }
1065
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001066 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001067 /*
1068 * NAND: calculate which blocks we are writing. We have
1069 * to write one block at a time to skip bad blocks.
1070 */
1071 erasesize = blocklen;
1072 } else {
1073 erasesize = erase_len;
1074 }
1075
1076 erase.length = erasesize;
1077
Remy Bohmercb969eb2011-02-12 19:06:26 +01001078 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001079 while (processed < write_total) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001080 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernane9c62202018-03-09 12:12:59 +00001081 if (rc < 0) /* block test failed */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001082 return rc;
1083
Andreas Fenkart22281582016-08-29 23:16:57 +02001084 if (blockstart + erasesize > environment_end(dev)) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001085 fprintf(stderr, "End of range reached, aborting\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001086 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001087 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001088
Alex Kiernane9c62202018-03-09 12:12:59 +00001089 if (rc) { /* block is bad */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001090 blockstart += blocklen;
1091 continue;
1092 }
1093
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001094 if (DEVTYPE(dev) != MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +02001095 erase.start = blockstart;
1096 ioctl(fd, MEMUNLOCK, &erase);
1097 /* These do not need an explicit erase cycle */
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001098 if (DEVTYPE(dev) != MTD_DATAFLASH)
Oliver Metze9c66ab2013-08-30 00:56:02 +02001099 if (ioctl(fd, MEMERASE, &erase) != 0) {
1100 fprintf(stderr,
1101 "MTD erase error on %s: %s\n",
1102 DEVNAME(dev), strerror(errno));
1103 return -1;
1104 }
1105 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001106
Alex Kiernane9c62202018-03-09 12:12:59 +00001107 if (lseek(fd, blockstart, SEEK_SET) == -1) {
1108 fprintf(stderr,
1109 "Seek error on %s: %s\n",
1110 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001111 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001112 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001113#ifdef DEBUG
Marcin Niestroj20890732016-05-06 14:58:29 +02001114 fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001115 (unsigned long long)erasesize,
1116 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001117#endif
Alex Kiernane9c62202018-03-09 12:12:59 +00001118 if (write(fd, data + processed, erasesize) != erasesize) {
1119 fprintf(stderr, "Write error on %s: %s\n",
1120 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001121 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001122 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001123
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001124 if (DEVTYPE(dev) != MTD_ABSENT)
Oliver Metze9c66ab2013-08-30 00:56:02 +02001125 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001126
Alex Kiernane9c62202018-03-09 12:12:59 +00001127 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001128 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -08001129 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001130 }
1131
1132 if (write_total > count)
Alex Kiernane9c62202018-03-09 12:12:59 +00001133 free(data);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001134
1135 return processed;
1136}
1137
1138/*
1139 * Set obsolete flag at offset - NOR flash only
1140 */
Alex Kiernane9c62202018-03-09 12:12:59 +00001141static int flash_flag_obsolete(int dev, int fd, off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001142{
1143 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +02001144 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001145
Alex Kiernane9c62202018-03-09 12:12:59 +00001146 erase.start = DEVOFFSET(dev);
1147 erase.length = DEVESIZE(dev);
Simon Glass4686d8f2019-08-01 09:47:08 -06001148 /* This relies on the fact, that ENV_REDUND_OBSOLETE == 0 */
Alex Kiernane9c62202018-03-09 12:12:59 +00001149 rc = lseek(fd, offset, SEEK_SET);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001150 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001151 fprintf(stderr, "Cannot seek to set the flag on %s\n",
1152 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001153 return rc;
1154 }
Alex Kiernane9c62202018-03-09 12:12:59 +00001155 ioctl(fd, MEMUNLOCK, &erase);
Simon Glass4686d8f2019-08-01 09:47:08 -06001156 rc = write(fd, &ENV_REDUND_OBSOLETE, sizeof(ENV_REDUND_OBSOLETE));
Alex Kiernane9c62202018-03-09 12:12:59 +00001157 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001158 if (rc < 0)
Alex Kiernane9c62202018-03-09 12:12:59 +00001159 perror("Could not set obsolete flag");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001160
1161 return rc;
1162}
1163
Alex Kiernane9c62202018-03-09 12:12:59 +00001164static int flash_write(int fd_current, int fd_target, int dev_target)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001165{
1166 int rc;
1167
1168 switch (environment.flag_scheme) {
1169 case FLAG_NONE:
1170 break;
1171 case FLAG_INCREMENTAL:
1172 (*environment.flags)++;
1173 break;
1174 case FLAG_BOOLEAN:
Simon Glass4686d8f2019-08-01 09:47:08 -06001175 *environment.flags = ENV_REDUND_ACTIVE;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001176 break;
1177 default:
Alex Kiernane9c62202018-03-09 12:12:59 +00001178 fprintf(stderr, "Unimplemented flash scheme %u\n",
1179 environment.flag_scheme);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001180 return -1;
1181 }
1182
1183#ifdef DEBUG
Stefan Agnerb21e6022016-07-13 17:14:38 -07001184 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001185 DEVOFFSET(dev_target), DEVNAME(dev_target));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001186#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +01001187
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001188 if (IS_UBI(dev_target)) {
1189 if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0)
1190 return 0;
1191 return ubi_write(fd_target, environment.image, CUR_ENVSIZE);
1192 }
1193
Joe Hershberger671adc42012-10-03 09:38:46 +00001194 rc = flash_write_buf(dev_target, fd_target, environment.image,
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001195 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001196 if (rc < 0)
1197 return rc;
1198
1199 if (environment.flag_scheme == FLAG_BOOLEAN) {
1200 /* Have to set obsolete flag */
Alex Kiernane9c62202018-03-09 12:12:59 +00001201 off_t offset = DEVOFFSET(dev_current) +
1202 offsetof(struct env_image_redundant, flags);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001203#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001204 fprintf(stderr,
Stefan Agnerb21e6022016-07-13 17:14:38 -07001205 "Setting obsolete flag in environment at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001206 DEVOFFSET(dev_current), DEVNAME(dev_current));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001207#endif
Alex Kiernane9c62202018-03-09 12:12:59 +00001208 flash_flag_obsolete(dev_current, fd_current, offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001209 }
1210
1211 return 0;
1212}
1213
Alex Kiernane9c62202018-03-09 12:12:59 +00001214static int flash_read(int fd)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001215{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001216 int rc;
1217
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001218 if (IS_UBI(dev_current)) {
1219 DEVTYPE(dev_current) = MTD_ABSENT;
1220
1221 return ubi_read(fd, environment.image, CUR_ENVSIZE);
1222 }
1223
Joe Hershberger671adc42012-10-03 09:38:46 +00001224 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001225 DEVOFFSET(dev_current));
Marek Vasut05ed2a52014-03-05 19:59:52 +01001226 if (rc != CUR_ENVSIZE)
1227 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001228
Marek Vasut05ed2a52014-03-05 19:59:52 +01001229 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001230}
1231
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001232static int flash_open_tempfile(const char **dname, const char **target_temp)
1233{
1234 char *dup_name = strdup(DEVNAME(dev_current));
1235 char *temp_name = NULL;
1236 int rc = -1;
1237
1238 if (!dup_name)
1239 return -1;
1240
1241 *dname = dirname(dup_name);
1242 if (!*dname)
1243 goto err;
1244
1245 rc = asprintf(&temp_name, "%s/XXXXXX", *dname);
1246 if (rc == -1)
1247 goto err;
1248
1249 rc = mkstemp(temp_name);
1250 if (rc == -1) {
1251 /* fall back to in place write */
1252 fprintf(stderr,
1253 "Can't create %s: %s\n", temp_name, strerror(errno));
1254 free(temp_name);
1255 } else {
1256 *target_temp = temp_name;
1257 /* deliberately leak dup_name as dname /might/ point into
1258 * it and we need it for our caller
1259 */
1260 dup_name = NULL;
1261 }
1262
1263err:
1264 if (dup_name)
1265 free(dup_name);
1266
1267 return rc;
1268}
1269
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001270static int flash_io_write(int fd_current)
1271{
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001272 int fd_target = -1, rc, dev_target;
1273 const char *dname, *target_temp = NULL;
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001274
1275 if (have_redund_env) {
1276 /* switch to next partition for writing */
1277 dev_target = !dev_current;
1278 /* dev_target: fd_target, erase_target */
1279 fd_target = open(DEVNAME(dev_target), O_RDWR);
1280 if (fd_target < 0) {
1281 fprintf(stderr,
1282 "Can't open %s: %s\n",
1283 DEVNAME(dev_target), strerror(errno));
1284 rc = -1;
1285 goto exit;
1286 }
1287 } else {
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001288 struct stat sb;
1289
1290 if (fstat(fd_current, &sb) == 0 && S_ISREG(sb.st_mode)) {
1291 /* if any part of flash_open_tempfile() fails we fall
1292 * back to in-place writes
1293 */
1294 fd_target = flash_open_tempfile(&dname, &target_temp);
1295 }
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001296 dev_target = dev_current;
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001297 if (fd_target == -1)
1298 fd_target = fd_current;
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001299 }
1300
1301 rc = flash_write(fd_current, fd_target, dev_target);
1302
1303 if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) {
1304 fprintf(stderr,
1305 "fsync failed on %s: %s\n",
1306 DEVNAME(dev_current), strerror(errno));
1307 }
1308
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001309 if (fd_current != fd_target) {
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001310 if (fsync(fd_target) &&
1311 !(errno == EINVAL || errno == EROFS)) {
1312 fprintf(stderr,
1313 "fsync failed on %s: %s\n",
1314 DEVNAME(dev_current), strerror(errno));
1315 }
1316
1317 if (close(fd_target)) {
1318 fprintf(stderr,
1319 "I/O error on %s: %s\n",
1320 DEVNAME(dev_target), strerror(errno));
1321 rc = -1;
1322 }
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001323
Alex Kiernanad4f3d02019-06-12 15:27:55 +01001324 if (rc >= 0 && target_temp) {
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001325 int dir_fd;
1326
1327 dir_fd = open(dname, O_DIRECTORY | O_RDONLY);
1328 if (dir_fd == -1)
1329 fprintf(stderr,
1330 "Can't open %s: %s\n",
1331 dname, strerror(errno));
1332
1333 if (rename(target_temp, DEVNAME(dev_target))) {
1334 fprintf(stderr,
1335 "rename failed %s => %s: %s\n",
1336 target_temp, DEVNAME(dev_target),
1337 strerror(errno));
1338 rc = -1;
1339 }
1340
1341 if (dir_fd != -1 && fsync(dir_fd))
1342 fprintf(stderr,
1343 "fsync failed on %s: %s\n",
1344 dname, strerror(errno));
1345
1346 if (dir_fd != -1 && close(dir_fd))
1347 fprintf(stderr,
1348 "I/O error on %s: %s\n",
1349 dname, strerror(errno));
1350 }
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001351 }
1352 exit:
1353 return rc;
1354}
1355
Alex Kiernane9c62202018-03-09 12:12:59 +00001356static int flash_io(int mode)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001357{
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001358 int fd_current, rc;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001359
1360 /* dev_current: fd_current, erase_current */
Alex Kiernane9c62202018-03-09 12:12:59 +00001361 fd_current = open(DEVNAME(dev_current), mode);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001362 if (fd_current < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001363 fprintf(stderr,
1364 "Can't open %s: %s\n",
1365 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001366 return -1;
1367 }
1368
1369 if (mode == O_RDWR) {
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001370 rc = flash_io_write(fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001371 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001372 rc = flash_read(fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001373 }
1374
Alex Kiernane9c62202018-03-09 12:12:59 +00001375 if (close(fd_current)) {
1376 fprintf(stderr,
1377 "I/O error on %s: %s\n",
1378 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001379 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001380 }
1381
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001382 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001383}
1384
1385/*
wdenk34b613e2002-12-17 01:51:00 +00001386 * Prevent confusion if running from erased flash memory
1387 */
Andreas Fenkart24371902016-04-05 23:13:42 +02001388int fw_env_open(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001389{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001390 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001391 unsigned char flag0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001392 void *addr0 = NULL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001393
wdenk34b613e2002-12-17 01:51:00 +00001394 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001395 unsigned char flag1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001396 void *addr1 = NULL;
wdenke7f34c62003-01-11 09:48:40 +00001397
Marek Vasut05ed2a52014-03-05 19:59:52 +01001398 int ret;
1399
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001400 struct env_image_single *single;
1401 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001402
Andreas Fenkarte6511302016-05-31 09:21:56 +02001403 if (!opts)
1404 opts = &default_opts;
1405
Alex Kiernane9c62202018-03-09 12:12:59 +00001406 if (parse_config(opts)) /* should fill envdevices */
Stefano Babic4b0a2932017-04-05 18:08:03 +02001407 return -EINVAL;
wdenk3f9ab982003-04-12 23:38:12 +00001408
Joe Hershberger671adc42012-10-03 09:38:46 +00001409 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001410 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001411 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001412 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001413 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001414 ret = -ENOMEM;
1415 goto open_cleanup;
wdenke7f34c62003-01-11 09:48:40 +00001416 }
wdenk3f9ab982003-04-12 23:38:12 +00001417
wdenke7f34c62003-01-11 09:48:40 +00001418 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001419 environment.image = addr0;
1420
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001421 if (have_redund_env) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001422 redundant = addr0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001423 environment.crc = &redundant->crc;
1424 environment.flags = &redundant->flags;
1425 environment.data = redundant->data;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001426 } else {
1427 single = addr0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001428 environment.crc = &single->crc;
1429 environment.flags = NULL;
1430 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001431 }
wdenk3f9ab982003-04-12 23:38:12 +00001432
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001433 dev_current = 0;
Tom Rini4c1192c2018-07-01 22:10:33 -04001434 if (flash_io(O_RDONLY)) {
Stefano Babic4b0a2932017-04-05 18:08:03 +02001435 ret = -EIO;
1436 goto open_cleanup;
1437 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001438
Tom Rini4c1192c2018-07-01 22:10:33 -04001439 crc0 = crc32(0, (uint8_t *)environment.data, ENV_SIZE);
1440
1441 crc0_ok = (crc0 == *environment.crc);
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001442 if (!have_redund_env) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001443 if (!crc0_ok) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001444 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001445 "Warning: Bad CRC, using default environment\n");
Alex Kiernane9c62202018-03-09 12:12:59 +00001446 memcpy(environment.data, default_environment,
1447 sizeof(default_environment));
wdenk34b613e2002-12-17 01:51:00 +00001448 }
wdenke7f34c62003-01-11 09:48:40 +00001449 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001450 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001451
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001452 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001453 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001454 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001455 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001456 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001457 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001458 ret = -ENOMEM;
1459 goto open_cleanup;
wdenk3f9ab982003-04-12 23:38:12 +00001460 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001461 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001462
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001463 /*
1464 * have to set environment.image for flash_read(), careful -
1465 * other pointers in environment still point inside addr0
1466 */
1467 environment.image = addr1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001468 if (flash_io(O_RDONLY)) {
Tom Rini4c1192c2018-07-01 22:10:33 -04001469 ret = -EIO;
1470 goto open_cleanup;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001471 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001472
1473 /* Check flag scheme compatibility */
1474 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1475 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1476 environment.flag_scheme = FLAG_BOOLEAN;
1477 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1478 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1479 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001480 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1481 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1482 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001483 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1484 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1485 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001486 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001487 DEVTYPE(!dev_current) == MTD_ABSENT &&
1488 IS_UBI(dev_current) == IS_UBI(!dev_current)) {
Oliver Metze745d4c2013-08-30 00:56:01 +02001489 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001490 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001491 fprintf(stderr, "Incompatible flash types!\n");
Stefano Babic4b0a2932017-04-05 18:08:03 +02001492 ret = -EINVAL;
1493 goto open_cleanup;
wdenk34b613e2002-12-17 01:51:00 +00001494 }
wdenk3f9ab982003-04-12 23:38:12 +00001495
Tom Rini4c1192c2018-07-01 22:10:33 -04001496 crc1 = crc32(0, (uint8_t *)redundant->data, ENV_SIZE);
1497
1498 crc1_ok = (crc1 == redundant->crc);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001499 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001500
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001501 if (crc0_ok && !crc1_ok) {
1502 dev_current = 0;
1503 } else if (!crc0_ok && crc1_ok) {
1504 dev_current = 1;
1505 } else if (!crc0_ok && !crc1_ok) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001506 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001507 "Warning: Bad CRC, using default environment\n");
Alex Kiernane9c62202018-03-09 12:12:59 +00001508 memcpy(environment.data, default_environment,
1509 sizeof(default_environment));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001510 dev_current = 0;
1511 } else {
1512 switch (environment.flag_scheme) {
1513 case FLAG_BOOLEAN:
Simon Glass4686d8f2019-08-01 09:47:08 -06001514 if (flag0 == ENV_REDUND_ACTIVE &&
1515 flag1 == ENV_REDUND_OBSOLETE) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001516 dev_current = 0;
Simon Glass4686d8f2019-08-01 09:47:08 -06001517 } else if (flag0 == ENV_REDUND_OBSOLETE &&
1518 flag1 == ENV_REDUND_ACTIVE) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001519 dev_current = 1;
1520 } else if (flag0 == flag1) {
1521 dev_current = 0;
1522 } else if (flag0 == 0xFF) {
1523 dev_current = 0;
1524 } else if (flag1 == 0xFF) {
1525 dev_current = 1;
1526 } else {
1527 dev_current = 0;
1528 }
1529 break;
1530 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001531 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001532 dev_current = 1;
1533 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001534 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001535 dev_current = 0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001536 else /* flag1 > flag0 */
Jon Povey93ccd472011-03-11 14:10:56 +09001537 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001538 break;
1539 default:
Alex Kiernane9c62202018-03-09 12:12:59 +00001540 fprintf(stderr, "Unknown flag scheme %u\n",
1541 environment.flag_scheme);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001542 return -1;
1543 }
1544 }
1545
1546 /*
1547 * If we are reading, we don't need the flag and the CRC any
1548 * more, if we are writing, we will re-calculate CRC and update
1549 * flags before writing out
1550 */
1551 if (dev_current) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001552 environment.image = addr1;
1553 environment.crc = &redundant->crc;
1554 environment.flags = &redundant->flags;
1555 environment.data = redundant->data;
1556 free(addr0);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001557 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001558 environment.image = addr0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001559 /* Other pointers are already set */
Alex Kiernane9c62202018-03-09 12:12:59 +00001560 free(addr1);
wdenk34b613e2002-12-17 01:51:00 +00001561 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001562#ifdef DEBUG
1563 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1564#endif
wdenk34b613e2002-12-17 01:51:00 +00001565 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001566 return 0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001567
Alex Kiernane9c62202018-03-09 12:12:59 +00001568 open_cleanup:
Stefano Babic4b0a2932017-04-05 18:08:03 +02001569 if (addr0)
1570 free(addr0);
1571
1572 if (addr1)
Björn Stenberg4d1734d2019-04-17 16:56:54 +02001573 free(addr1);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001574
1575 return ret;
1576}
1577
1578/*
1579 * Simply free allocated buffer with environment
1580 */
1581int fw_env_close(struct env_opts *opts)
1582{
1583 if (environment.image)
1584 free(environment.image);
1585
1586 environment.image = NULL;
1587
1588 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001589}
1590
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001591static int check_device_config(int dev)
1592{
1593 struct stat st;
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001594 int32_t lnum = 0;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001595 int fd, rc = 0;
1596
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001597 /* Fills in IS_UBI(), converts DEVNAME() with ubi volume name */
1598 ubi_check_dev(dev);
1599
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001600 fd = open(DEVNAME(dev), O_RDONLY);
1601 if (fd < 0) {
1602 fprintf(stderr,
Alex Kiernane9c62202018-03-09 12:12:59 +00001603 "Cannot open %s: %s\n", DEVNAME(dev), strerror(errno));
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001604 return -1;
1605 }
1606
1607 rc = fstat(fd, &st);
1608 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001609 fprintf(stderr, "Cannot stat the file %s\n", DEVNAME(dev));
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001610 goto err;
1611 }
1612
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001613 if (IS_UBI(dev)) {
1614 rc = ioctl(fd, UBI_IOCEBISMAP, &lnum);
1615 if (rc < 0) {
1616 fprintf(stderr, "Cannot get UBI information for %s\n",
1617 DEVNAME(dev));
1618 goto err;
1619 }
1620 } else if (S_ISCHR(st.st_mode)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001621 struct mtd_info_user mtdinfo;
1622 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1623 if (rc < 0) {
1624 fprintf(stderr, "Cannot get MTD information for %s\n",
1625 DEVNAME(dev));
1626 goto err;
1627 }
1628 if (mtdinfo.type != MTD_NORFLASH &&
1629 mtdinfo.type != MTD_NANDFLASH &&
1630 mtdinfo.type != MTD_DATAFLASH &&
1631 mtdinfo.type != MTD_UBIVOLUME) {
1632 fprintf(stderr, "Unsupported flash type %u on %s\n",
1633 mtdinfo.type, DEVNAME(dev));
1634 goto err;
1635 }
1636 DEVTYPE(dev) = mtdinfo.type;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001637 if (DEVESIZE(dev) == 0)
1638 /* Assume the erase size is the same as the env-size */
1639 DEVESIZE(dev) = ENVSIZE(dev);
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001640 } else {
Stefan Agnerb21e6022016-07-13 17:14:38 -07001641 uint64_t size;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001642 DEVTYPE(dev) = MTD_ABSENT;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001643 if (DEVESIZE(dev) == 0)
1644 /* Assume the erase size to be 512 bytes */
1645 DEVESIZE(dev) = 0x200;
Stefan Agnerb21e6022016-07-13 17:14:38 -07001646
1647 /*
1648 * Check for negative offsets, treat it as backwards offset
1649 * from the end of the block device
1650 */
1651 if (DEVOFFSET(dev) < 0) {
1652 rc = ioctl(fd, BLKGETSIZE64, &size);
1653 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001654 fprintf(stderr,
1655 "Could not get block device size on %s\n",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001656 DEVNAME(dev));
1657 goto err;
1658 }
1659
1660 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1661#ifdef DEBUG
Alex Kiernane9c62202018-03-09 12:12:59 +00001662 fprintf(stderr,
1663 "Calculated device offset 0x%llx on %s\n",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001664 DEVOFFSET(dev), DEVNAME(dev));
1665#endif
1666 }
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001667 }
1668
Max Krummenacher6c44f542016-11-19 13:58:56 +01001669 if (ENVSECTORS(dev) == 0)
1670 /* Assume enough sectors to cover the environment */
1671 ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev));
1672
1673 if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001674 fprintf(stderr,
1675 "Environment does not start on (erase) block boundary\n");
Max Krummenacher6c44f542016-11-19 13:58:56 +01001676 errno = EINVAL;
1677 return -1;
1678 }
1679
1680 if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001681 fprintf(stderr,
1682 "Environment does not fit into available sectors\n");
Max Krummenacher6c44f542016-11-19 13:58:56 +01001683 errno = EINVAL;
1684 return -1;
1685 }
1686
Alex Kiernane9c62202018-03-09 12:12:59 +00001687 err:
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001688 close(fd);
1689 return rc;
1690}
wdenk34b613e2002-12-17 01:51:00 +00001691
Andreas Fenkart24371902016-04-05 23:13:42 +02001692static int parse_config(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001693{
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001694 int rc;
wdenk34b613e2002-12-17 01:51:00 +00001695
Andreas Fenkarte6511302016-05-31 09:21:56 +02001696 if (!opts)
1697 opts = &default_opts;
Anatolij Gustschine89ead32016-04-29 22:00:11 +02001698
Andreas Fenkarte6511302016-05-31 09:21:56 +02001699#if defined(CONFIG_FILE)
wdenke7f34c62003-01-11 09:48:40 +00001700 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart24371902016-04-05 23:13:42 +02001701 if (get_config(opts->config_file)) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001702 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart24371902016-04-05 23:13:42 +02001703 opts->config_file);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001704 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001705 }
wdenke7f34c62003-01-11 09:48:40 +00001706#else
Alex Kiernane9c62202018-03-09 12:12:59 +00001707 DEVNAME(0) = DEVICE1_NAME;
1708 DEVOFFSET(0) = DEVICE1_OFFSET;
1709 ENVSIZE(0) = ENV1_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001710
1711 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1712 * know DEVTYPE
1713 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001714#ifdef DEVICE1_ESIZE
Alex Kiernane9c62202018-03-09 12:12:59 +00001715 DEVESIZE(0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001716#endif
1717#ifdef DEVICE1_ENVSECTORS
Alex Kiernane9c62202018-03-09 12:12:59 +00001718 ENVSECTORS(0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001719#endif
1720
wdenk34b613e2002-12-17 01:51:00 +00001721#ifdef HAVE_REDUND
Alex Kiernane9c62202018-03-09 12:12:59 +00001722 DEVNAME(1) = DEVICE2_NAME;
1723 DEVOFFSET(1) = DEVICE2_OFFSET;
1724 ENVSIZE(1) = ENV2_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001725
1726 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1727 * know DEVTYPE
1728 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001729#ifdef DEVICE2_ESIZE
Alex Kiernane9c62202018-03-09 12:12:59 +00001730 DEVESIZE(1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001731#endif
1732#ifdef DEVICE2_ENVSECTORS
Alex Kiernane9c62202018-03-09 12:12:59 +00001733 ENVSECTORS(1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001734#endif
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001735 have_redund_env = 1;
wdenk34b613e2002-12-17 01:51:00 +00001736#endif
wdenke7f34c62003-01-11 09:48:40 +00001737#endif
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001738 rc = check_device_config(0);
1739 if (rc < 0)
1740 return rc;
wdenk3f9ab982003-04-12 23:38:12 +00001741
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001742 if (have_redund_env) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001743 rc = check_device_config(1);
1744 if (rc < 0)
1745 return rc;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001746
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001747 if (ENVSIZE(0) != ENVSIZE(1)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001748 fprintf(stderr,
Philip Molloyf60f7512019-03-31 03:44:57 +00001749 "Redundant environments have unequal size\n");
Andreas Fenkart31f2e052016-08-17 23:41:53 +02001750 return -1;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001751 }
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001752 }
1753
1754 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001755 if (have_redund_env)
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001756 usable_envsize -= sizeof(char);
1757
wdenk34b613e2002-12-17 01:51:00 +00001758 return 0;
1759}
wdenke7f34c62003-01-11 09:48:40 +00001760
1761#if defined(CONFIG_FILE)
Alex Kiernane9c62202018-03-09 12:12:59 +00001762static int get_config(char *fname)
wdenke7f34c62003-01-11 09:48:40 +00001763{
1764 FILE *fp;
1765 int i = 0;
1766 int rc;
Alex Kiernan66df4e22018-06-07 12:20:05 +00001767 char *line = NULL;
1768 size_t linesize = 0;
Tom Rini313e9122014-03-28 12:03:33 -04001769 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001770
Alex Kiernane9c62202018-03-09 12:12:59 +00001771 fp = fopen(fname, "r");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001772 if (fp == NULL)
1773 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001774
Alex Kiernan66df4e22018-06-07 12:20:05 +00001775 while (i < 2 && getline(&line, &linesize, fp) != -1) {
1776 /* Skip comment strings */
1777 if (line[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001778 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001779
Alex Kiernan66df4e22018-06-07 12:20:05 +00001780 rc = sscanf(line, "%ms %lli %lx %lx %lx",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001781 &devname,
1782 &DEVOFFSET(i),
Alex Kiernane9c62202018-03-09 12:12:59 +00001783 &ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001784
Remy Bohmercb969eb2011-02-12 19:06:26 +01001785 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001786 continue;
1787
Tom Rini313e9122014-03-28 12:03:33 -04001788 DEVNAME(i) = devname;
1789
Max Krummenacher6c44f542016-11-19 13:58:56 +01001790 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1791 * know DEVTYPE
1792 */
wdenke7f34c62003-01-11 09:48:40 +00001793
1794 i++;
1795 }
Alex Kiernan66df4e22018-06-07 12:20:05 +00001796 free(line);
Alex Kiernane9c62202018-03-09 12:12:59 +00001797 fclose(fp);
wdenk3f9ab982003-04-12 23:38:12 +00001798
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001799 have_redund_env = i - 1;
Alex Kiernane9c62202018-03-09 12:12:59 +00001800 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001801 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001802 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001803 } else
1804 return 0;
1805}
1806#endif