blob: 30b5a190ab9b380dc0482bd3930e2946055fe4b2 [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;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200115};
116
117static struct environment environment = {
118 .flag_scheme = FLAG_NONE,
119};
wdenk34b613e2002-12-17 01:51:00 +0000120
Alex Kiernanf46d0ca2018-03-09 12:13:00 +0000121static int have_redund_env;
wdenke7f34c62003-01-11 09:48:40 +0000122
Joe Hershberger81321142012-10-12 08:48:51 +0000123#define DEFAULT_ENV_INSTANCE_STATIC
124#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000125
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -0800126#define UBI_DEV_START "/dev/ubi"
127#define UBI_SYSFS "/sys/class/ubi"
128#define UBI_VOL_NAME_PATT "ubi%d_%d"
129
130static int is_ubi_devname(const char *devname)
131{
132 return !strncmp(devname, UBI_DEV_START, sizeof(UBI_DEV_START) - 1);
133}
134
135static int ubi_check_volume_sysfs_name(const char *volume_sysfs_name,
136 const char *volname)
137{
138 char path[256];
139 FILE *file;
140 char *name;
141 int ret;
142
143 strcpy(path, UBI_SYSFS "/");
144 strcat(path, volume_sysfs_name);
145 strcat(path, "/name");
146
147 file = fopen(path, "r");
148 if (!file)
149 return -1;
150
151 ret = fscanf(file, "%ms", &name);
152 fclose(file);
153 if (ret <= 0 || !name) {
154 fprintf(stderr,
155 "Failed to read from file %s, ret = %d, name = %s\n",
156 path, ret, name);
157 return -1;
158 }
159
160 if (!strcmp(name, volname)) {
161 free(name);
162 return 0;
163 }
164 free(name);
165
166 return -1;
167}
168
169static int ubi_get_volnum_by_name(int devnum, const char *volname)
170{
171 DIR *sysfs_ubi;
172 struct dirent *dirent;
173 int ret;
174 int tmp_devnum;
175 int volnum;
176
177 sysfs_ubi = opendir(UBI_SYSFS);
178 if (!sysfs_ubi)
179 return -1;
180
181#ifdef DEBUG
182 fprintf(stderr, "Looking for volume name \"%s\"\n", volname);
183#endif
184
185 while (1) {
186 dirent = readdir(sysfs_ubi);
187 if (!dirent)
188 return -1;
189
190 ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT,
191 &tmp_devnum, &volnum);
192 if (ret == 2 && devnum == tmp_devnum) {
193 if (ubi_check_volume_sysfs_name(dirent->d_name,
194 volname) == 0)
195 return volnum;
196 }
197 }
198
199 return -1;
200}
201
202static int ubi_get_devnum_by_devname(const char *devname)
203{
204 int devnum;
205 int ret;
206
207 ret = sscanf(devname + sizeof(UBI_DEV_START) - 1, "%d", &devnum);
208 if (ret != 1)
209 return -1;
210
211 return devnum;
212}
213
214static const char *ubi_get_volume_devname(const char *devname,
215 const char *volname)
216{
217 char *volume_devname;
218 int volnum;
219 int devnum;
220 int ret;
221
222 devnum = ubi_get_devnum_by_devname(devname);
223 if (devnum < 0)
224 return NULL;
225
226 volnum = ubi_get_volnum_by_name(devnum, volname);
227 if (volnum < 0)
228 return NULL;
229
230 ret = asprintf(&volume_devname, "%s_%d", devname, volnum);
231 if (ret < 0)
232 return NULL;
233
234#ifdef DEBUG
235 fprintf(stderr, "Found ubi volume \"%s:%s\" -> %s\n",
236 devname, volname, volume_devname);
237#endif
238
239 return volume_devname;
240}
241
242static void ubi_check_dev(unsigned int dev_id)
243{
244 char *devname = (char *)DEVNAME(dev_id);
245 char *pname;
246 const char *volname = NULL;
247 const char *volume_devname;
248
249 if (!is_ubi_devname(DEVNAME(dev_id)))
250 return;
251
252 IS_UBI(dev_id) = 1;
253
254 for (pname = devname; *pname != '\0'; pname++) {
255 if (*pname == ':') {
256 *pname = '\0';
257 volname = pname + 1;
258 break;
259 }
260 }
261
262 if (volname) {
263 /* Let's find real volume device name */
264 volume_devname = ubi_get_volume_devname(devname, volname);
265 if (!volume_devname) {
266 fprintf(stderr, "Didn't found ubi volume \"%s\"\n",
267 volname);
268 return;
269 }
270
271 free(devname);
272 DEVNAME(dev_id) = volume_devname;
273 }
274}
275
276static int ubi_update_start(int fd, int64_t bytes)
277{
278 if (ioctl(fd, UBI_IOCVOLUP, &bytes))
279 return -1;
280 return 0;
281}
282
283static int ubi_read(int fd, void *buf, size_t count)
284{
285 ssize_t ret;
286
287 while (count > 0) {
288 ret = read(fd, buf, count);
289 if (ret > 0) {
290 count -= ret;
291 buf += ret;
292
293 continue;
294 }
295
296 if (ret == 0) {
297 /*
298 * Happens in case of too short volume data size. If we
299 * return error status we will fail it will be treated
300 * as UBI device error.
301 *
302 * Leave catching this error to CRC check.
303 */
304 fprintf(stderr, "Warning: end of data on ubi volume\n");
305 return 0;
306 } else if (errno == EBADF) {
307 /*
308 * Happens in case of corrupted volume. The same as
309 * above, we cannot return error now, as we will still
310 * be able to successfully write environment later.
311 */
312 fprintf(stderr, "Warning: corrupted volume?\n");
313 return 0;
314 } else if (errno == EINTR) {
315 continue;
316 }
317
318 fprintf(stderr, "Cannot read %u bytes from ubi volume, %s\n",
319 (unsigned int)count, strerror(errno));
320 return -1;
321 }
322
323 return 0;
324}
325
326static int ubi_write(int fd, const void *buf, size_t count)
327{
328 ssize_t ret;
329
330 while (count > 0) {
331 ret = write(fd, buf, count);
332 if (ret <= 0) {
333 if (ret < 0 && errno == EINTR)
334 continue;
335
336 fprintf(stderr, "Cannot write %u bytes to ubi volume\n",
337 (unsigned int)count);
338 return -1;
339 }
340
341 count -= ret;
342 buf += ret;
343 }
344
345 return 0;
346}
347
Alex Kiernane9c62202018-03-09 12:12:59 +0000348static int flash_io(int mode);
Andreas Fenkart24371902016-04-05 23:13:42 +0200349static int parse_config(struct env_opts *opts);
wdenk3f9ab982003-04-12 23:38:12 +0000350
wdenke7f34c62003-01-11 09:48:40 +0000351#if defined(CONFIG_FILE)
Alex Kiernane9c62202018-03-09 12:12:59 +0000352static int get_config(char *);
wdenke7f34c62003-01-11 09:48:40 +0000353#endif
wdenk34b613e2002-12-17 01:51:00 +0000354
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100355static char *skip_chars(char *s)
Stefano Babicada628b2010-05-24 12:08:16 +0200356{
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100357 for (; *s != '\0'; s++) {
Stefan Agner87833242018-03-01 14:06:32 +0100358 if (isblank(*s) || *s == '=')
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100359 return s;
360 }
361 return NULL;
362}
Stefano Babicada628b2010-05-24 12:08:16 +0200363
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100364static char *skip_blanks(char *s)
365{
366 for (; *s != '\0'; s++) {
367 if (!isblank(*s))
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100368 return s;
Stefano Babicada628b2010-05-24 12:08:16 +0200369 }
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100370 return NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200371}
372
wdenk34b613e2002-12-17 01:51:00 +0000373/*
Andreas Fenkart3ff10742016-07-16 17:06:14 +0200374 * s1 is either a simple 'name', or a 'name=value' pair.
375 * s2 is a 'name=value' pair.
376 * If the names match, return the value of s2, else NULL.
377 */
378static char *envmatch(char *s1, char *s2)
379{
380 if (s1 == NULL || s2 == NULL)
381 return NULL;
382
383 while (*s1 == *s2++)
384 if (*s1++ == '=')
385 return s2;
386 if (*s1 == '\0' && *(s2 - 1) == '=')
387 return s2;
388 return NULL;
389}
390
391/**
wdenk34b613e2002-12-17 01:51:00 +0000392 * Search the environment for a variable.
393 * Return the value, if found, or NULL, if not found.
394 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000395char *fw_getenv(char *name)
wdenk34b613e2002-12-17 01:51:00 +0000396{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100397 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000398
wdenk3f9ab982003-04-12 23:38:12 +0000399 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100400 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000401
wdenk3f9ab982003-04-12 23:38:12 +0000402 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000403 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000404 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000405 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200406 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000407 }
408 }
Alex Kiernane9c62202018-03-09 12:12:59 +0000409 val = envmatch(name, env);
wdenk34b613e2002-12-17 01:51:00 +0000410 if (!val)
411 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200412 return val;
wdenk34b613e2002-12-17 01:51:00 +0000413 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200414 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000415}
416
417/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600418 * Search the default environment for a variable.
419 * Return the value, if found, or NULL, if not found.
420 */
421char *fw_getdefenv(char *name)
422{
423 char *env, *nxt;
424
425 for (env = default_environment; *env; env = nxt + 1) {
426 char *val;
427
428 for (nxt = env; *nxt; ++nxt) {
429 if (nxt >= &default_environment[ENV_SIZE]) {
430 fprintf(stderr, "## Error: "
431 "default environment not terminated\n");
432 return NULL;
433 }
434 }
435 val = envmatch(name, env);
436 if (!val)
437 continue;
438 return val;
439 }
440 return NULL;
441}
442
443/*
wdenk34b613e2002-12-17 01:51:00 +0000444 * Print the current definition of one, or more, or all
445 * environment variables
446 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200447int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000448{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100449 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000450
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200451 if (value_only && argc != 1) {
452 fprintf(stderr,
Alex Kiernan688cee92018-02-11 17:16:46 +0000453 "## Error: `-n'/`--noheader' option requires exactly one argument\n");
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200454 return -1;
455 }
456
Andreas Fenkarte6511302016-05-31 09:21:56 +0200457 if (!opts)
458 opts = &default_opts;
459
Andreas Fenkart24371902016-04-05 23:13:42 +0200460 if (fw_env_open(opts))
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200461 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000462
Alex Kiernane9c62202018-03-09 12:12:59 +0000463 if (argc == 0) { /* Print all env variables */
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200464 char *env, *nxt;
wdenk3f9ab982003-04-12 23:38:12 +0000465 for (env = environment.data; *env; env = nxt + 1) {
466 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000467 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000468 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000469 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200470 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000471 }
472 }
473
Alex Kiernane9c62202018-03-09 12:12:59 +0000474 printf("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000475 }
Stefano Babic4b0a2932017-04-05 18:08:03 +0200476 fw_env_close(opts);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200477 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000478 }
479
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200480 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100481 char *name = argv[i];
482 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000483
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200484 val = fw_getenv(name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700485 if (!val) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000486 fprintf(stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700487 rc = -1;
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200488 continue;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700489 }
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200490
491 if (value_only) {
492 puts(val);
493 break;
494 }
495
496 printf("%s=%s\n", name, val);
wdenk34b613e2002-12-17 01:51:00 +0000497 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700498
Stefano Babic4b0a2932017-04-05 18:08:03 +0200499 fw_env_close(opts);
500
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200501 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000502}
503
Stefano Babic4b0a2932017-04-05 18:08:03 +0200504int fw_env_flush(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000505{
Andreas Fenkarte6511302016-05-31 09:21:56 +0200506 if (!opts)
507 opts = &default_opts;
508
Stefano Babicada628b2010-05-24 12:08:16 +0200509 /*
510 * Update CRC
511 */
512 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000513
Stefano Babicada628b2010-05-24 12:08:16 +0200514 /* write environment back to flash */
515 if (flash_io(O_RDWR)) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000516 fprintf(stderr, "Error: can't write fw_env to flash\n");
517 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000518 }
519
Stefano Babicada628b2010-05-24 12:08:16 +0200520 return 0;
521}
wdenk34b613e2002-12-17 01:51:00 +0000522
Stefano Babicada628b2010-05-24 12:08:16 +0200523/*
524 * Set/Clear a single variable in the environment.
525 * This is called in sequence to update the environment
526 * in RAM without updating the copy in flash after each set
527 */
528int fw_env_write(char *name, char *value)
529{
530 int len;
531 char *env, *nxt;
532 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600533 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000534
535 /*
536 * search if variable with this name already exists
537 */
wdenk3f9ab982003-04-12 23:38:12 +0000538 for (nxt = env = environment.data; *env; env = nxt + 1) {
539 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000540 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200541 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000542 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200543 errno = EINVAL;
544 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000545 }
546 }
Alex Kiernane9c62202018-03-09 12:12:59 +0000547 oldval = envmatch(name, env);
548 if (oldval)
wdenk34b613e2002-12-17 01:51:00 +0000549 break;
550 }
551
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600552 deleting = (oldval && !(value && strlen(value)));
553 creating = (!oldval && (value && strlen(value)));
554 overwriting = (oldval && (value && strlen(value)));
555
556 /* check for permission */
557 if (deleting) {
558 if (env_flags_validate_varaccess(name,
559 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
560 printf("Can't delete \"%s\"\n", name);
561 errno = EROFS;
562 return -1;
563 }
564 } else if (overwriting) {
565 if (env_flags_validate_varaccess(name,
566 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
567 printf("Can't overwrite \"%s\"\n", name);
568 errno = EROFS;
569 return -1;
570 } else if (env_flags_validate_varaccess(name,
Alex Kiernane9c62202018-03-09 12:12:59 +0000571 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600572 const char *defval = fw_getdefenv(name);
573
574 if (defval == NULL)
575 defval = "";
576 if (strcmp(oldval, defval)
577 != 0) {
578 printf("Can't overwrite \"%s\"\n", name);
579 errno = EROFS;
580 return -1;
581 }
582 }
583 } else if (creating) {
584 if (env_flags_validate_varaccess(name,
585 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
586 printf("Can't create \"%s\"\n", name);
587 errno = EROFS;
588 return -1;
589 }
590 } else
591 /* Nothing to do */
592 return 0;
593
594 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000595 if (*++nxt == '\0') {
596 *env = '\0';
597 } else {
598 for (;;) {
599 *env = *nxt++;
600 if ((*env == '\0') && (*nxt == '\0'))
601 break;
602 ++env;
603 }
604 }
605 *++env = '\0';
606 }
607
608 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200609 if (!value || !strlen(value))
610 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000611
612 /*
613 * Append new definition at the end
614 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000615 for (env = environment.data; *env || *(env + 1); ++env)
616 ;
wdenk34b613e2002-12-17 01:51:00 +0000617 if (env > environment.data)
618 ++env;
619 /*
620 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000621 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000622 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000623 len = strlen(name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000624 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200625 len += strlen(value) + 1;
626
wdenk3f9ab982003-04-12 23:38:12 +0000627 if (len > (&environment.data[ENV_SIZE] - env)) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000628 fprintf(stderr,
629 "Error: environment overflow, \"%s\" deleted\n", name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200630 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000631 }
Stefano Babicada628b2010-05-24 12:08:16 +0200632
wdenk34b613e2002-12-17 01:51:00 +0000633 while ((*env = *name++) != '\0')
634 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200635 *env = '=';
636 while ((*++env = *value++) != '\0')
637 ;
638
639 /* end is marked with double '\0' */
640 *++env = '\0';
641
642 return 0;
643}
644
645/*
646 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
647 * 0 - OK
648 * EINVAL - need at least 1 argument
649 * EROFS - certain variables ("ethaddr", "serial#") cannot be
650 * modified or deleted
651 *
652 */
Simon Glass6a38e412017-08-03 12:22:09 -0600653int fw_env_set(int argc, char *argv[], struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200654{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100655 int i;
Mike Frysinger178ec772012-11-10 19:47:45 +0000656 size_t len;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100657 char *name, **valv;
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200658 char *oldval;
Stefano Babicada628b2010-05-24 12:08:16 +0200659 char *value = NULL;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100660 int valc;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200661 int ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200662
Andreas Fenkarte6511302016-05-31 09:21:56 +0200663 if (!opts)
664 opts = &default_opts;
665
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100666 if (argc < 1) {
667 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasut05ed2a52014-03-05 19:59:52 +0100668 errno = EINVAL;
669 return -1;
670 }
671
Andreas Fenkart24371902016-04-05 23:13:42 +0200672 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200673 fprintf(stderr, "Error: environment not initialized\n");
674 return -1;
675 }
676
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100677 name = argv[0];
678 valv = argv + 1;
679 valc = argc - 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200680
Stefano Babic4b0a2932017-04-05 18:08:03 +0200681 if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
682 fw_env_close(opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200683 return -1;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200684 }
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600685
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000686 len = 0;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100687 for (i = 0; i < valc; ++i) {
688 char *val = valv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000689 size_t val_len = strlen(val);
690
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000691 if (value)
692 value[len - 1] = ' ';
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200693 oldval = value;
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000694 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200695 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000696 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200697 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000698 len, strerror(errno));
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200699 free(oldval);
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000700 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200701 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000702
703 memcpy(value + len, val, val_len);
704 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000705 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000706 }
707
Stefano Babicada628b2010-05-24 12:08:16 +0200708 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000709
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000710 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000711
Stefano Babic4b0a2932017-04-05 18:08:03 +0200712 ret = fw_env_flush(opts);
713 fw_env_close(opts);
714
715 return ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200716}
wdenk34b613e2002-12-17 01:51:00 +0000717
Stefano Babicada628b2010-05-24 12:08:16 +0200718/*
719 * Parse a file and configure the u-boot variables.
720 * The script file has a very simple format, as follows:
721 *
722 * Each line has a couple with name, value:
723 * <white spaces>variable_name<white spaces>variable_value
724 *
725 * Both variable_name and variable_value are interpreted as strings.
726 * Any character after <white spaces> and before ending \r\n is interpreted
727 * as variable's value (no comment allowed on these lines !)
728 *
729 * Comments are allowed if the first character in the line is #
730 *
731 * Returns -1 and sets errno error codes:
732 * 0 - OK
733 * -1 - Error
734 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200735int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200736{
737 FILE *fp;
Alex Kiernan66df4e22018-06-07 12:20:05 +0000738 char *line = NULL;
739 size_t linesize = 0;
Stefano Babicada628b2010-05-24 12:08:16 +0200740 char *name;
741 char *val;
742 int lineno = 0;
743 int len;
744 int ret = 0;
745
Andreas Fenkarte6511302016-05-31 09:21:56 +0200746 if (!opts)
747 opts = &default_opts;
748
Andreas Fenkart24371902016-04-05 23:13:42 +0200749 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200750 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200751 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000752 }
753
Stefano Babicada628b2010-05-24 12:08:16 +0200754 if (strcmp(fname, "-") == 0)
755 fp = stdin;
756 else {
757 fp = fopen(fname, "r");
758 if (fp == NULL) {
759 fprintf(stderr, "I cannot open %s for reading\n",
Alex Kiernane9c62202018-03-09 12:12:59 +0000760 fname);
Stefano Babicada628b2010-05-24 12:08:16 +0200761 return -1;
762 }
763 }
764
Alex Kiernan66df4e22018-06-07 12:20:05 +0000765 while ((len = getline(&line, &linesize, fp)) != -1) {
Stefano Babicada628b2010-05-24 12:08:16 +0200766 lineno++;
Stefano Babicada628b2010-05-24 12:08:16 +0200767
768 /*
Alex Kiernan66df4e22018-06-07 12:20:05 +0000769 * Read a whole line from the file. If the line is not
770 * terminated, reports an error and exit.
Stefano Babicada628b2010-05-24 12:08:16 +0200771 */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000772 if (line[len - 1] != '\n') {
Stefano Babicada628b2010-05-24 12:08:16 +0200773 fprintf(stderr,
Alex Kiernan66df4e22018-06-07 12:20:05 +0000774 "Line %d not correctly terminated\n",
Stefano Babicada628b2010-05-24 12:08:16 +0200775 lineno);
776 ret = -1;
777 break;
778 }
779
780 /* Drop ending line feed / carriage return */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000781 line[--len] = '\0';
782 if (len && line[len - 1] == '\r')
783 line[--len] = '\0';
Stefano Babicada628b2010-05-24 12:08:16 +0200784
785 /* Skip comment or empty lines */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000786 if (len == 0 || line[0] == '#')
Stefano Babicada628b2010-05-24 12:08:16 +0200787 continue;
788
789 /*
Alex Kiernan66df4e22018-06-07 12:20:05 +0000790 * Search for variable's name remove leading whitespaces
Stefano Babicada628b2010-05-24 12:08:16 +0200791 */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000792 name = skip_blanks(line);
Stefano Babicada628b2010-05-24 12:08:16 +0200793 if (!name)
794 continue;
795
796 /* The first white space is the end of variable name */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100797 val = skip_chars(name);
Stefano Babicada628b2010-05-24 12:08:16 +0200798 len = strlen(name);
799 if (val) {
800 *val++ = '\0';
801 if ((val - name) < len)
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100802 val = skip_blanks(val);
Stefano Babicada628b2010-05-24 12:08:16 +0200803 else
804 val = NULL;
805 }
Stefano Babicada628b2010-05-24 12:08:16 +0200806#ifdef DEBUG
807 fprintf(stderr, "Setting %s : %s\n",
808 name, val ? val : " removed");
809#endif
810
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600811 if (env_flags_validate_type(name, val) < 0) {
812 ret = -1;
813 break;
814 }
815
Stefano Babicada628b2010-05-24 12:08:16 +0200816 /*
817 * If there is an error setting a variable,
818 * try to save the environment and returns an error
819 */
820 if (fw_env_write(name, val)) {
821 fprintf(stderr,
Alex Kiernane9c62202018-03-09 12:12:59 +0000822 "fw_env_write returns with error : %s\n",
Stefano Babicada628b2010-05-24 12:08:16 +0200823 strerror(errno));
824 ret = -1;
825 break;
826 }
827
828 }
Alex Kiernan66df4e22018-06-07 12:20:05 +0000829 free(line);
Stefano Babicada628b2010-05-24 12:08:16 +0200830
831 /* Close file if not stdin */
832 if (strcmp(fname, "-") != 0)
833 fclose(fp);
834
Stefano Babic4b0a2932017-04-05 18:08:03 +0200835 ret |= fw_env_flush(opts);
836
837 fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200838
839 return ret;
wdenk34b613e2002-12-17 01:51:00 +0000840}
841
Andreas Fenkart22281582016-08-29 23:16:57 +0200842/**
Shyam Sainiefe757c2018-06-07 19:47:19 +0530843 * environment_end() - compute offset of first byte right after environment
Andreas Fenkart22281582016-08-29 23:16:57 +0200844 * @dev - index of enviroment buffer
845 * Return:
Shyam Sainiefe757c2018-06-07 19:47:19 +0530846 * device offset of first byte right after environment
Andreas Fenkart22281582016-08-29 23:16:57 +0200847 */
848off_t environment_end(int dev)
849{
850 /* environment is block aligned */
851 return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
852}
853
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200854/*
855 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
856 * 0 - block is good
857 * > 0 - block is bad
858 * < 0 - failed to test
859 */
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200860static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000861{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200862 if (mtd_type == MTD_NANDFLASH) {
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200863 int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000864
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200865 if (badblock < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000866 perror("Cannot read bad block mark");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200867 return badblock;
868 }
869
870 if (badblock) {
871#ifdef DEBUG
Alex Kiernane9c62202018-03-09 12:12:59 +0000872 fprintf(stderr, "Bad block at 0x%llx, skipping\n",
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200873 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200874#endif
875 return badblock;
876 }
wdenk34b613e2002-12-17 01:51:00 +0000877 }
878
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200879 return 0;
880}
881
882/*
883 * Read data from flash at an offset into a provided buffer. On NAND it skips
884 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
885 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
886 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000887static int flash_read_buf(int dev, int fd, void *buf, size_t count,
888 off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200889{
890 size_t blocklen; /* erase / write length - one block on NAND,
891 0 on NOR */
892 size_t processed = 0; /* progress counter */
893 size_t readlen = count; /* current read length */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200894 off_t block_seek; /* offset inside the current block to the start
895 of the data */
896 loff_t blockstart; /* running start of the current block -
897 MEMGETBADBLOCK needs 64 bits */
898 int rc;
899
Alex Kiernane9c62202018-03-09 12:12:59 +0000900 blockstart = (offset / DEVESIZE(dev)) * DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200901
902 /* Offset inside a block */
903 block_seek = offset - blockstart;
904
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200905 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200906 /*
907 * NAND: calculate which blocks we are reading. We have
908 * to read one block at a time to skip bad blocks.
909 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000910 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200911
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200912 /* Limit to one block for the first read */
913 if (readlen > blocklen - block_seek)
914 readlen = blocklen - block_seek;
915 } else {
916 blocklen = 0;
wdenke7f34c62003-01-11 09:48:40 +0000917 }
wdenk34b613e2002-12-17 01:51:00 +0000918
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200919 /* This only runs once on NOR flash */
920 while (processed < count) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200921 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernane9c62202018-03-09 12:12:59 +0000922 if (rc < 0) /* block test failed */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200923 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000924
Andreas Fenkart22281582016-08-29 23:16:57 +0200925 if (blockstart + block_seek + readlen > environment_end(dev)) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200926 /* End of range is reached */
Alex Kiernane9c62202018-03-09 12:12:59 +0000927 fprintf(stderr, "Too few good blocks within range\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200928 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000929 }
930
Alex Kiernane9c62202018-03-09 12:12:59 +0000931 if (rc) { /* block is bad */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200932 blockstart += blocklen;
933 continue;
wdenk34b613e2002-12-17 01:51:00 +0000934 }
935
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200936 /*
937 * If a block is bad, we retry in the next block at the same
Tom Rini282f2702017-09-05 14:01:29 -0400938 * offset - see env/nand.c::writeenv()
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200939 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000940 lseek(fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000941
Alex Kiernane9c62202018-03-09 12:12:59 +0000942 rc = read(fd, buf + processed, readlen);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200943 if (rc != readlen) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000944 fprintf(stderr, "Read error on %s: %s\n",
945 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200946 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000947 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200948#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000949 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +0000950 rc, (unsigned long long)blockstart + block_seek,
Marcin Niestroj20890732016-05-06 14:58:29 +0200951 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200952#endif
953 processed += readlen;
Alex Kiernane9c62202018-03-09 12:12:59 +0000954 readlen = min(blocklen, count - processed);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200955 block_seek = 0;
956 blockstart += blocklen;
957 }
958
959 return processed;
960}
961
962/*
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200963 * Write count bytes from begin of environment, but stay within
964 * ENVSECTORS(dev) sectors of
Remy Bohmercb969eb2011-02-12 19:06:26 +0100965 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
966 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200967 */
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200968static int flash_write_buf(int dev, int fd, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200969{
970 void *data;
971 struct erase_info_user erase;
972 size_t blocklen; /* length of NAND block / NOR erase sector */
973 size_t erase_len; /* whole area that can be erased - may include
974 bad blocks */
975 size_t erasesize; /* erase / write length - one block on NAND,
976 whole area on NOR */
977 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100978 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200979 bad blocks */
980 off_t erase_offset; /* offset to the first erase block (aligned)
981 below offset */
982 off_t block_seek; /* offset inside the erase block to the start
983 of the data */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200984 loff_t blockstart; /* running start of the current block -
985 MEMGETBADBLOCK needs 64 bits */
986 int rc;
987
Oliver Metze9c66ab2013-08-30 00:56:02 +0200988 /*
989 * For mtd devices only offset and size of the environment do matter
990 */
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200991 if (DEVTYPE(dev) == MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +0200992 blocklen = count;
Oliver Metze9c66ab2013-08-30 00:56:02 +0200993 erase_len = blocklen;
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200994 blockstart = DEVOFFSET(dev);
Oliver Metze9c66ab2013-08-30 00:56:02 +0200995 block_seek = 0;
996 write_total = blocklen;
997 } else {
998 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200999
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001000 erase_offset = DEVOFFSET(dev);
wdenk34b613e2002-12-17 01:51:00 +00001001
Oliver Metze9c66ab2013-08-30 00:56:02 +02001002 /* Maximum area we may use */
Andreas Fenkart22281582016-08-29 23:16:57 +02001003 erase_len = environment_end(dev) - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +00001004
Oliver Metze9c66ab2013-08-30 00:56:02 +02001005 blockstart = erase_offset;
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001006
Oliver Metze9c66ab2013-08-30 00:56:02 +02001007 /* Offset inside a block */
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001008 block_seek = DEVOFFSET(dev) - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001009
Oliver Metze9c66ab2013-08-30 00:56:02 +02001010 /*
1011 * Data size we actually write: from the start of the block
1012 * to the start of the data, then count bytes of data, and
1013 * to the end of the block
1014 */
1015 write_total = ((block_seek + count + blocklen - 1) /
Alex Kiernane9c62202018-03-09 12:12:59 +00001016 blocklen) * blocklen;
Oliver Metze9c66ab2013-08-30 00:56:02 +02001017 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001018
1019 /*
1020 * Support data anywhere within erase sectors: read out the complete
1021 * area to be erased, replace the environment image, write the whole
1022 * block back again.
1023 */
1024 if (write_total > count) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001025 data = malloc(erase_len);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001026 if (!data) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001027 fprintf(stderr,
1028 "Cannot malloc %zu bytes: %s\n",
1029 erase_len, strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001030 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001031 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001032
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001033 rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001034 if (write_total != rc)
1035 return -1;
1036
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001037#ifdef DEBUG
1038 fprintf(stderr, "Preserving data ");
1039 if (block_seek != 0)
1040 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
1041 if (block_seek + count != write_total) {
1042 if (block_seek != 0)
1043 fprintf(stderr, " and ");
Marcin Niestroj20890732016-05-06 14:58:29 +02001044 fprintf(stderr, "0x%lx - 0x%lx",
Alex Kiernane9c62202018-03-09 12:12:59 +00001045 (unsigned long)block_seek + count,
1046 (unsigned long)write_total - 1);
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001047 }
1048 fprintf(stderr, "\n");
1049#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001050 /* Overwrite the old environment */
Alex Kiernane9c62202018-03-09 12:12:59 +00001051 memcpy(data + block_seek, buf, count);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001052 } else {
1053 /*
1054 * We get here, iff offset is block-aligned and count is a
1055 * multiple of blocklen - see write_total calculation above
1056 */
1057 data = buf;
1058 }
1059
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001060 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001061 /*
1062 * NAND: calculate which blocks we are writing. We have
1063 * to write one block at a time to skip bad blocks.
1064 */
1065 erasesize = blocklen;
1066 } else {
1067 erasesize = erase_len;
1068 }
1069
1070 erase.length = erasesize;
1071
Remy Bohmercb969eb2011-02-12 19:06:26 +01001072 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001073 while (processed < write_total) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001074 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernane9c62202018-03-09 12:12:59 +00001075 if (rc < 0) /* block test failed */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001076 return rc;
1077
Andreas Fenkart22281582016-08-29 23:16:57 +02001078 if (blockstart + erasesize > environment_end(dev)) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001079 fprintf(stderr, "End of range reached, aborting\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001080 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001081 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001082
Alex Kiernane9c62202018-03-09 12:12:59 +00001083 if (rc) { /* block is bad */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001084 blockstart += blocklen;
1085 continue;
1086 }
1087
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001088 if (DEVTYPE(dev) != MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +02001089 erase.start = blockstart;
1090 ioctl(fd, MEMUNLOCK, &erase);
1091 /* These do not need an explicit erase cycle */
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001092 if (DEVTYPE(dev) != MTD_DATAFLASH)
Oliver Metze9c66ab2013-08-30 00:56:02 +02001093 if (ioctl(fd, MEMERASE, &erase) != 0) {
1094 fprintf(stderr,
1095 "MTD erase error on %s: %s\n",
1096 DEVNAME(dev), strerror(errno));
1097 return -1;
1098 }
1099 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001100
Alex Kiernane9c62202018-03-09 12:12:59 +00001101 if (lseek(fd, blockstart, SEEK_SET) == -1) {
1102 fprintf(stderr,
1103 "Seek error on %s: %s\n",
1104 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001105 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001106 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001107#ifdef DEBUG
Marcin Niestroj20890732016-05-06 14:58:29 +02001108 fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001109 (unsigned long long)erasesize,
1110 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001111#endif
Alex Kiernane9c62202018-03-09 12:12:59 +00001112 if (write(fd, data + processed, erasesize) != erasesize) {
1113 fprintf(stderr, "Write error on %s: %s\n",
1114 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001115 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001116 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001117
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001118 if (DEVTYPE(dev) != MTD_ABSENT)
Oliver Metze9c66ab2013-08-30 00:56:02 +02001119 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001120
Alex Kiernane9c62202018-03-09 12:12:59 +00001121 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001122 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -08001123 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001124 }
1125
1126 if (write_total > count)
Alex Kiernane9c62202018-03-09 12:12:59 +00001127 free(data);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001128
1129 return processed;
1130}
1131
1132/*
1133 * Set obsolete flag at offset - NOR flash only
1134 */
Alex Kiernane9c62202018-03-09 12:12:59 +00001135static int flash_flag_obsolete(int dev, int fd, off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001136{
1137 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +02001138 struct erase_info_user erase;
Pierre-Jean Texierd7f95452019-08-26 13:06:16 +02001139 char tmp = ENV_REDUND_OBSOLETE;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001140
Alex Kiernane9c62202018-03-09 12:12:59 +00001141 erase.start = DEVOFFSET(dev);
1142 erase.length = DEVESIZE(dev);
Simon Glass4686d8f2019-08-01 09:47:08 -06001143 /* This relies on the fact, that ENV_REDUND_OBSOLETE == 0 */
Alex Kiernane9c62202018-03-09 12:12:59 +00001144 rc = lseek(fd, offset, SEEK_SET);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001145 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001146 fprintf(stderr, "Cannot seek to set the flag on %s\n",
1147 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001148 return rc;
1149 }
Alex Kiernane9c62202018-03-09 12:12:59 +00001150 ioctl(fd, MEMUNLOCK, &erase);
Pierre-Jean Texierd7f95452019-08-26 13:06:16 +02001151 rc = write(fd, &tmp, sizeof(tmp));
Alex Kiernane9c62202018-03-09 12:12:59 +00001152 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001153 if (rc < 0)
Alex Kiernane9c62202018-03-09 12:12:59 +00001154 perror("Could not set obsolete flag");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001155
1156 return rc;
1157}
1158
Alex Kiernane9c62202018-03-09 12:12:59 +00001159static int flash_write(int fd_current, int fd_target, int dev_target)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001160{
1161 int rc;
1162
1163 switch (environment.flag_scheme) {
1164 case FLAG_NONE:
1165 break;
1166 case FLAG_INCREMENTAL:
1167 (*environment.flags)++;
1168 break;
1169 case FLAG_BOOLEAN:
Simon Glass4686d8f2019-08-01 09:47:08 -06001170 *environment.flags = ENV_REDUND_ACTIVE;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001171 break;
1172 default:
Alex Kiernane9c62202018-03-09 12:12:59 +00001173 fprintf(stderr, "Unimplemented flash scheme %u\n",
1174 environment.flag_scheme);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001175 return -1;
1176 }
1177
1178#ifdef DEBUG
Stefan Agnerb21e6022016-07-13 17:14:38 -07001179 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001180 DEVOFFSET(dev_target), DEVNAME(dev_target));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001181#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +01001182
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001183 if (IS_UBI(dev_target)) {
1184 if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0)
1185 return 0;
1186 return ubi_write(fd_target, environment.image, CUR_ENVSIZE);
1187 }
1188
Joe Hershberger671adc42012-10-03 09:38:46 +00001189 rc = flash_write_buf(dev_target, fd_target, environment.image,
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001190 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001191 if (rc < 0)
1192 return rc;
1193
1194 if (environment.flag_scheme == FLAG_BOOLEAN) {
1195 /* Have to set obsolete flag */
Alex Kiernane9c62202018-03-09 12:12:59 +00001196 off_t offset = DEVOFFSET(dev_current) +
1197 offsetof(struct env_image_redundant, flags);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001198#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001199 fprintf(stderr,
Stefan Agnerb21e6022016-07-13 17:14:38 -07001200 "Setting obsolete flag in environment at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001201 DEVOFFSET(dev_current), DEVNAME(dev_current));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001202#endif
Alex Kiernane9c62202018-03-09 12:12:59 +00001203 flash_flag_obsolete(dev_current, fd_current, offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001204 }
1205
1206 return 0;
1207}
1208
Alex Kiernane9c62202018-03-09 12:12:59 +00001209static int flash_read(int fd)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001210{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001211 int rc;
1212
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001213 if (IS_UBI(dev_current)) {
1214 DEVTYPE(dev_current) = MTD_ABSENT;
1215
1216 return ubi_read(fd, environment.image, CUR_ENVSIZE);
1217 }
1218
Joe Hershberger671adc42012-10-03 09:38:46 +00001219 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001220 DEVOFFSET(dev_current));
Marek Vasut05ed2a52014-03-05 19:59:52 +01001221 if (rc != CUR_ENVSIZE)
1222 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001223
Marek Vasut05ed2a52014-03-05 19:59:52 +01001224 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001225}
1226
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001227static int flash_open_tempfile(const char **dname, const char **target_temp)
1228{
1229 char *dup_name = strdup(DEVNAME(dev_current));
1230 char *temp_name = NULL;
1231 int rc = -1;
1232
1233 if (!dup_name)
1234 return -1;
1235
1236 *dname = dirname(dup_name);
1237 if (!*dname)
1238 goto err;
1239
1240 rc = asprintf(&temp_name, "%s/XXXXXX", *dname);
1241 if (rc == -1)
1242 goto err;
1243
1244 rc = mkstemp(temp_name);
1245 if (rc == -1) {
1246 /* fall back to in place write */
1247 fprintf(stderr,
1248 "Can't create %s: %s\n", temp_name, strerror(errno));
1249 free(temp_name);
1250 } else {
1251 *target_temp = temp_name;
1252 /* deliberately leak dup_name as dname /might/ point into
1253 * it and we need it for our caller
1254 */
1255 dup_name = NULL;
1256 }
1257
1258err:
1259 if (dup_name)
1260 free(dup_name);
1261
1262 return rc;
1263}
1264
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001265static int flash_io_write(int fd_current)
1266{
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001267 int fd_target = -1, rc, dev_target;
1268 const char *dname, *target_temp = NULL;
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001269
1270 if (have_redund_env) {
1271 /* switch to next partition for writing */
1272 dev_target = !dev_current;
1273 /* dev_target: fd_target, erase_target */
1274 fd_target = open(DEVNAME(dev_target), O_RDWR);
1275 if (fd_target < 0) {
1276 fprintf(stderr,
1277 "Can't open %s: %s\n",
1278 DEVNAME(dev_target), strerror(errno));
1279 rc = -1;
1280 goto exit;
1281 }
1282 } else {
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001283 struct stat sb;
1284
1285 if (fstat(fd_current, &sb) == 0 && S_ISREG(sb.st_mode)) {
1286 /* if any part of flash_open_tempfile() fails we fall
1287 * back to in-place writes
1288 */
1289 fd_target = flash_open_tempfile(&dname, &target_temp);
1290 }
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001291 dev_target = dev_current;
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001292 if (fd_target == -1)
1293 fd_target = fd_current;
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001294 }
1295
1296 rc = flash_write(fd_current, fd_target, dev_target);
1297
1298 if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) {
1299 fprintf(stderr,
1300 "fsync failed on %s: %s\n",
1301 DEVNAME(dev_current), strerror(errno));
1302 }
1303
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001304 if (fd_current != fd_target) {
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001305 if (fsync(fd_target) &&
1306 !(errno == EINVAL || errno == EROFS)) {
1307 fprintf(stderr,
1308 "fsync failed on %s: %s\n",
1309 DEVNAME(dev_current), strerror(errno));
1310 }
1311
1312 if (close(fd_target)) {
1313 fprintf(stderr,
1314 "I/O error on %s: %s\n",
1315 DEVNAME(dev_target), strerror(errno));
1316 rc = -1;
1317 }
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001318
Alex Kiernanad4f3d02019-06-12 15:27:55 +01001319 if (rc >= 0 && target_temp) {
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001320 int dir_fd;
1321
1322 dir_fd = open(dname, O_DIRECTORY | O_RDONLY);
1323 if (dir_fd == -1)
1324 fprintf(stderr,
1325 "Can't open %s: %s\n",
1326 dname, strerror(errno));
1327
1328 if (rename(target_temp, DEVNAME(dev_target))) {
1329 fprintf(stderr,
1330 "rename failed %s => %s: %s\n",
1331 target_temp, DEVNAME(dev_target),
1332 strerror(errno));
1333 rc = -1;
1334 }
1335
1336 if (dir_fd != -1 && fsync(dir_fd))
1337 fprintf(stderr,
1338 "fsync failed on %s: %s\n",
1339 dname, strerror(errno));
1340
1341 if (dir_fd != -1 && close(dir_fd))
1342 fprintf(stderr,
1343 "I/O error on %s: %s\n",
1344 dname, strerror(errno));
1345 }
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001346 }
1347 exit:
1348 return rc;
1349}
1350
Alex Kiernane9c62202018-03-09 12:12:59 +00001351static int flash_io(int mode)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001352{
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001353 int fd_current, rc;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001354
1355 /* dev_current: fd_current, erase_current */
Alex Kiernane9c62202018-03-09 12:12:59 +00001356 fd_current = open(DEVNAME(dev_current), mode);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001357 if (fd_current < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001358 fprintf(stderr,
1359 "Can't open %s: %s\n",
1360 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001361 return -1;
1362 }
1363
1364 if (mode == O_RDWR) {
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001365 rc = flash_io_write(fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001366 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001367 rc = flash_read(fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001368 }
1369
Alex Kiernane9c62202018-03-09 12:12:59 +00001370 if (close(fd_current)) {
1371 fprintf(stderr,
1372 "I/O error on %s: %s\n",
1373 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001374 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001375 }
1376
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001377 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001378}
1379
1380/*
wdenk34b613e2002-12-17 01:51:00 +00001381 * Prevent confusion if running from erased flash memory
1382 */
Andreas Fenkart24371902016-04-05 23:13:42 +02001383int fw_env_open(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001384{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001385 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001386 unsigned char flag0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001387 void *addr0 = NULL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001388
wdenk34b613e2002-12-17 01:51:00 +00001389 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001390 unsigned char flag1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001391 void *addr1 = NULL;
wdenke7f34c62003-01-11 09:48:40 +00001392
Marek Vasut05ed2a52014-03-05 19:59:52 +01001393 int ret;
1394
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001395 struct env_image_single *single;
1396 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001397
Andreas Fenkarte6511302016-05-31 09:21:56 +02001398 if (!opts)
1399 opts = &default_opts;
1400
Alex Kiernane9c62202018-03-09 12:12:59 +00001401 if (parse_config(opts)) /* should fill envdevices */
Stefano Babic4b0a2932017-04-05 18:08:03 +02001402 return -EINVAL;
wdenk3f9ab982003-04-12 23:38:12 +00001403
Joe Hershberger671adc42012-10-03 09:38:46 +00001404 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001405 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001406 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001407 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001408 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001409 ret = -ENOMEM;
1410 goto open_cleanup;
wdenke7f34c62003-01-11 09:48:40 +00001411 }
wdenk3f9ab982003-04-12 23:38:12 +00001412
wdenke7f34c62003-01-11 09:48:40 +00001413 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001414 environment.image = addr0;
1415
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001416 if (have_redund_env) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001417 redundant = addr0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001418 environment.crc = &redundant->crc;
1419 environment.flags = &redundant->flags;
1420 environment.data = redundant->data;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001421 } else {
1422 single = addr0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001423 environment.crc = &single->crc;
1424 environment.flags = NULL;
1425 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001426 }
wdenk3f9ab982003-04-12 23:38:12 +00001427
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001428 dev_current = 0;
Tom Rini4c1192c2018-07-01 22:10:33 -04001429 if (flash_io(O_RDONLY)) {
Stefano Babic4b0a2932017-04-05 18:08:03 +02001430 ret = -EIO;
1431 goto open_cleanup;
1432 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001433
Tom Rini4c1192c2018-07-01 22:10:33 -04001434 crc0 = crc32(0, (uint8_t *)environment.data, ENV_SIZE);
1435
1436 crc0_ok = (crc0 == *environment.crc);
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001437 if (!have_redund_env) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001438 if (!crc0_ok) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001439 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001440 "Warning: Bad CRC, using default environment\n");
Alex Kiernane9c62202018-03-09 12:12:59 +00001441 memcpy(environment.data, default_environment,
1442 sizeof(default_environment));
wdenk34b613e2002-12-17 01:51:00 +00001443 }
wdenke7f34c62003-01-11 09:48:40 +00001444 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001445 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001446
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001447 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001448 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001449 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001450 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001451 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001452 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001453 ret = -ENOMEM;
1454 goto open_cleanup;
wdenk3f9ab982003-04-12 23:38:12 +00001455 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001456 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001457
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001458 /*
1459 * have to set environment.image for flash_read(), careful -
1460 * other pointers in environment still point inside addr0
1461 */
1462 environment.image = addr1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001463 if (flash_io(O_RDONLY)) {
Tom Rini4c1192c2018-07-01 22:10:33 -04001464 ret = -EIO;
1465 goto open_cleanup;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001466 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001467
1468 /* Check flag scheme compatibility */
1469 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1470 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1471 environment.flag_scheme = FLAG_BOOLEAN;
1472 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1473 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1474 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001475 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1476 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1477 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001478 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1479 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1480 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001481 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001482 DEVTYPE(!dev_current) == MTD_ABSENT &&
1483 IS_UBI(dev_current) == IS_UBI(!dev_current)) {
Oliver Metze745d4c2013-08-30 00:56:01 +02001484 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001485 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001486 fprintf(stderr, "Incompatible flash types!\n");
Stefano Babic4b0a2932017-04-05 18:08:03 +02001487 ret = -EINVAL;
1488 goto open_cleanup;
wdenk34b613e2002-12-17 01:51:00 +00001489 }
wdenk3f9ab982003-04-12 23:38:12 +00001490
Tom Rini4c1192c2018-07-01 22:10:33 -04001491 crc1 = crc32(0, (uint8_t *)redundant->data, ENV_SIZE);
1492
1493 crc1_ok = (crc1 == redundant->crc);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001494 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001495
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001496 if (crc0_ok && !crc1_ok) {
1497 dev_current = 0;
1498 } else if (!crc0_ok && crc1_ok) {
1499 dev_current = 1;
1500 } else if (!crc0_ok && !crc1_ok) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001501 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001502 "Warning: Bad CRC, using default environment\n");
Alex Kiernane9c62202018-03-09 12:12:59 +00001503 memcpy(environment.data, default_environment,
1504 sizeof(default_environment));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001505 dev_current = 0;
1506 } else {
1507 switch (environment.flag_scheme) {
1508 case FLAG_BOOLEAN:
Simon Glass4686d8f2019-08-01 09:47:08 -06001509 if (flag0 == ENV_REDUND_ACTIVE &&
1510 flag1 == ENV_REDUND_OBSOLETE) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001511 dev_current = 0;
Simon Glass4686d8f2019-08-01 09:47:08 -06001512 } else if (flag0 == ENV_REDUND_OBSOLETE &&
1513 flag1 == ENV_REDUND_ACTIVE) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001514 dev_current = 1;
1515 } else if (flag0 == flag1) {
1516 dev_current = 0;
1517 } else if (flag0 == 0xFF) {
1518 dev_current = 0;
1519 } else if (flag1 == 0xFF) {
1520 dev_current = 1;
1521 } else {
1522 dev_current = 0;
1523 }
1524 break;
1525 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001526 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001527 dev_current = 1;
1528 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001529 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001530 dev_current = 0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001531 else /* flag1 > flag0 */
Jon Povey93ccd472011-03-11 14:10:56 +09001532 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001533 break;
1534 default:
Alex Kiernane9c62202018-03-09 12:12:59 +00001535 fprintf(stderr, "Unknown flag scheme %u\n",
1536 environment.flag_scheme);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001537 return -1;
1538 }
1539 }
1540
1541 /*
1542 * If we are reading, we don't need the flag and the CRC any
1543 * more, if we are writing, we will re-calculate CRC and update
1544 * flags before writing out
1545 */
1546 if (dev_current) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001547 environment.image = addr1;
1548 environment.crc = &redundant->crc;
1549 environment.flags = &redundant->flags;
1550 environment.data = redundant->data;
1551 free(addr0);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001552 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001553 environment.image = addr0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001554 /* Other pointers are already set */
Alex Kiernane9c62202018-03-09 12:12:59 +00001555 free(addr1);
wdenk34b613e2002-12-17 01:51:00 +00001556 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001557#ifdef DEBUG
1558 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1559#endif
wdenk34b613e2002-12-17 01:51:00 +00001560 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001561 return 0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001562
Alex Kiernane9c62202018-03-09 12:12:59 +00001563 open_cleanup:
Stefano Babic4b0a2932017-04-05 18:08:03 +02001564 if (addr0)
1565 free(addr0);
1566
1567 if (addr1)
Björn Stenberg4d1734d2019-04-17 16:56:54 +02001568 free(addr1);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001569
1570 return ret;
1571}
1572
1573/*
1574 * Simply free allocated buffer with environment
1575 */
1576int fw_env_close(struct env_opts *opts)
1577{
1578 if (environment.image)
1579 free(environment.image);
1580
1581 environment.image = NULL;
1582
1583 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001584}
1585
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001586static int check_device_config(int dev)
1587{
1588 struct stat st;
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001589 int32_t lnum = 0;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001590 int fd, rc = 0;
1591
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001592 /* Fills in IS_UBI(), converts DEVNAME() with ubi volume name */
1593 ubi_check_dev(dev);
1594
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001595 fd = open(DEVNAME(dev), O_RDONLY);
1596 if (fd < 0) {
1597 fprintf(stderr,
Alex Kiernane9c62202018-03-09 12:12:59 +00001598 "Cannot open %s: %s\n", DEVNAME(dev), strerror(errno));
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001599 return -1;
1600 }
1601
1602 rc = fstat(fd, &st);
1603 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001604 fprintf(stderr, "Cannot stat the file %s\n", DEVNAME(dev));
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001605 goto err;
1606 }
1607
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001608 if (IS_UBI(dev)) {
1609 rc = ioctl(fd, UBI_IOCEBISMAP, &lnum);
1610 if (rc < 0) {
1611 fprintf(stderr, "Cannot get UBI information for %s\n",
1612 DEVNAME(dev));
1613 goto err;
1614 }
1615 } else if (S_ISCHR(st.st_mode)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001616 struct mtd_info_user mtdinfo;
1617 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1618 if (rc < 0) {
1619 fprintf(stderr, "Cannot get MTD information for %s\n",
1620 DEVNAME(dev));
1621 goto err;
1622 }
1623 if (mtdinfo.type != MTD_NORFLASH &&
1624 mtdinfo.type != MTD_NANDFLASH &&
1625 mtdinfo.type != MTD_DATAFLASH &&
1626 mtdinfo.type != MTD_UBIVOLUME) {
1627 fprintf(stderr, "Unsupported flash type %u on %s\n",
1628 mtdinfo.type, DEVNAME(dev));
1629 goto err;
1630 }
1631 DEVTYPE(dev) = mtdinfo.type;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001632 if (DEVESIZE(dev) == 0)
1633 /* Assume the erase size is the same as the env-size */
1634 DEVESIZE(dev) = ENVSIZE(dev);
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001635 } else {
Stefan Agnerb21e6022016-07-13 17:14:38 -07001636 uint64_t size;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001637 DEVTYPE(dev) = MTD_ABSENT;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001638 if (DEVESIZE(dev) == 0)
1639 /* Assume the erase size to be 512 bytes */
1640 DEVESIZE(dev) = 0x200;
Stefan Agnerb21e6022016-07-13 17:14:38 -07001641
1642 /*
1643 * Check for negative offsets, treat it as backwards offset
1644 * from the end of the block device
1645 */
1646 if (DEVOFFSET(dev) < 0) {
1647 rc = ioctl(fd, BLKGETSIZE64, &size);
1648 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001649 fprintf(stderr,
1650 "Could not get block device size on %s\n",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001651 DEVNAME(dev));
1652 goto err;
1653 }
1654
1655 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1656#ifdef DEBUG
Alex Kiernane9c62202018-03-09 12:12:59 +00001657 fprintf(stderr,
1658 "Calculated device offset 0x%llx on %s\n",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001659 DEVOFFSET(dev), DEVNAME(dev));
1660#endif
1661 }
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001662 }
1663
Max Krummenacher6c44f542016-11-19 13:58:56 +01001664 if (ENVSECTORS(dev) == 0)
1665 /* Assume enough sectors to cover the environment */
1666 ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev));
1667
1668 if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001669 fprintf(stderr,
1670 "Environment does not start on (erase) block boundary\n");
Max Krummenacher6c44f542016-11-19 13:58:56 +01001671 errno = EINVAL;
1672 return -1;
1673 }
1674
1675 if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001676 fprintf(stderr,
1677 "Environment does not fit into available sectors\n");
Max Krummenacher6c44f542016-11-19 13:58:56 +01001678 errno = EINVAL;
1679 return -1;
1680 }
1681
Alex Kiernane9c62202018-03-09 12:12:59 +00001682 err:
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001683 close(fd);
1684 return rc;
1685}
wdenk34b613e2002-12-17 01:51:00 +00001686
Andreas Fenkart24371902016-04-05 23:13:42 +02001687static int parse_config(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001688{
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001689 int rc;
wdenk34b613e2002-12-17 01:51:00 +00001690
Andreas Fenkarte6511302016-05-31 09:21:56 +02001691 if (!opts)
1692 opts = &default_opts;
Anatolij Gustschine89ead32016-04-29 22:00:11 +02001693
Andreas Fenkarte6511302016-05-31 09:21:56 +02001694#if defined(CONFIG_FILE)
wdenke7f34c62003-01-11 09:48:40 +00001695 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart24371902016-04-05 23:13:42 +02001696 if (get_config(opts->config_file)) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001697 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart24371902016-04-05 23:13:42 +02001698 opts->config_file);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001699 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001700 }
wdenke7f34c62003-01-11 09:48:40 +00001701#else
Alex Kiernane9c62202018-03-09 12:12:59 +00001702 DEVNAME(0) = DEVICE1_NAME;
1703 DEVOFFSET(0) = DEVICE1_OFFSET;
1704 ENVSIZE(0) = ENV1_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001705
1706 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1707 * know DEVTYPE
1708 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001709#ifdef DEVICE1_ESIZE
Alex Kiernane9c62202018-03-09 12:12:59 +00001710 DEVESIZE(0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001711#endif
1712#ifdef DEVICE1_ENVSECTORS
Alex Kiernane9c62202018-03-09 12:12:59 +00001713 ENVSECTORS(0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001714#endif
1715
wdenk34b613e2002-12-17 01:51:00 +00001716#ifdef HAVE_REDUND
Alex Kiernane9c62202018-03-09 12:12:59 +00001717 DEVNAME(1) = DEVICE2_NAME;
1718 DEVOFFSET(1) = DEVICE2_OFFSET;
1719 ENVSIZE(1) = ENV2_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001720
1721 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1722 * know DEVTYPE
1723 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001724#ifdef DEVICE2_ESIZE
Alex Kiernane9c62202018-03-09 12:12:59 +00001725 DEVESIZE(1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001726#endif
1727#ifdef DEVICE2_ENVSECTORS
Alex Kiernane9c62202018-03-09 12:12:59 +00001728 ENVSECTORS(1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001729#endif
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001730 have_redund_env = 1;
wdenk34b613e2002-12-17 01:51:00 +00001731#endif
wdenke7f34c62003-01-11 09:48:40 +00001732#endif
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001733 rc = check_device_config(0);
1734 if (rc < 0)
1735 return rc;
wdenk3f9ab982003-04-12 23:38:12 +00001736
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001737 if (have_redund_env) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001738 rc = check_device_config(1);
1739 if (rc < 0)
1740 return rc;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001741
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001742 if (ENVSIZE(0) != ENVSIZE(1)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001743 fprintf(stderr,
Philip Molloyf60f7512019-03-31 03:44:57 +00001744 "Redundant environments have unequal size\n");
Andreas Fenkart31f2e052016-08-17 23:41:53 +02001745 return -1;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001746 }
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001747 }
1748
1749 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001750 if (have_redund_env)
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001751 usable_envsize -= sizeof(char);
1752
wdenk34b613e2002-12-17 01:51:00 +00001753 return 0;
1754}
wdenke7f34c62003-01-11 09:48:40 +00001755
1756#if defined(CONFIG_FILE)
Alex Kiernane9c62202018-03-09 12:12:59 +00001757static int get_config(char *fname)
wdenke7f34c62003-01-11 09:48:40 +00001758{
1759 FILE *fp;
1760 int i = 0;
1761 int rc;
Alex Kiernan66df4e22018-06-07 12:20:05 +00001762 char *line = NULL;
1763 size_t linesize = 0;
Tom Rini313e9122014-03-28 12:03:33 -04001764 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001765
Alex Kiernane9c62202018-03-09 12:12:59 +00001766 fp = fopen(fname, "r");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001767 if (fp == NULL)
1768 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001769
Alex Kiernan66df4e22018-06-07 12:20:05 +00001770 while (i < 2 && getline(&line, &linesize, fp) != -1) {
1771 /* Skip comment strings */
1772 if (line[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001773 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001774
Alex Kiernan66df4e22018-06-07 12:20:05 +00001775 rc = sscanf(line, "%ms %lli %lx %lx %lx",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001776 &devname,
1777 &DEVOFFSET(i),
Alex Kiernane9c62202018-03-09 12:12:59 +00001778 &ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001779
Remy Bohmercb969eb2011-02-12 19:06:26 +01001780 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001781 continue;
1782
Tom Rini313e9122014-03-28 12:03:33 -04001783 DEVNAME(i) = devname;
1784
Max Krummenacher6c44f542016-11-19 13:58:56 +01001785 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1786 * know DEVTYPE
1787 */
wdenke7f34c62003-01-11 09:48:40 +00001788
1789 i++;
1790 }
Alex Kiernan66df4e22018-06-07 12:20:05 +00001791 free(line);
Alex Kiernane9c62202018-03-09 12:12:59 +00001792 fclose(fp);
wdenk3f9ab982003-04-12 23:38:12 +00001793
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001794 have_redund_env = i - 1;
Alex Kiernane9c62202018-03-09 12:12:59 +00001795 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001796 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001797 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001798 } else
1799 return 0;
1800}
1801#endif