blob: f06252d9168af09a6e31e48f59570e3380c36f22 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk6aff3112002-12-17 01:51:00 +00002/*
Detlev Zundel0aef7bc2010-07-30 11:22:15 +02003 * (C) Copyright 2000-2010
wdenk6aff3112002-12-17 01:51:00 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02006 * (C) Copyright 2008
7 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
wdenk6aff3112002-12-17 01:51:00 +00008 */
9
Jörg Krause26e355d2015-04-22 21:36:22 +020010#define _GNU_SOURCE
11
Peter Robinson69bf2d22015-12-09 07:15:33 +000012#include <compiler.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060013#include <env.h>
wdenk6aff3112002-12-17 01:51:00 +000014#include <errno.h>
Joe Hershberger30fd4fad2012-12-11 22:16:32 -060015#include <env_flags.h>
wdenk6aff3112002-12-17 01:51:00 +000016#include <fcntl.h>
Alex Kiernandbc34322018-03-09 12:13:02 +000017#include <libgen.h>
Stefan Agnerf4742ca2016-07-13 17:14:38 -070018#include <linux/fs.h>
Joe Hershberger06134212012-10-12 10:23:37 +000019#include <linux/stringify.h>
Andreas Fenkart10667e12016-03-11 09:39:35 +010020#include <ctype.h>
wdenk6aff3112002-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-Childs34255b92017-11-14 23:01:26 -080029#include <dirent.h>
wdenk6aff3112002-12-17 01:51:00 +000030
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010031#ifdef MTD_OLD
Wolfgang Denk1711f3b2008-09-02 21:17:36 +020032# include <stdint.h>
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010033# include <linux/mtd/mtd.h>
34#else
Wolfgang Denkc83d7ca2008-01-08 22:58:27 +010035# define __user /* nothing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010036# include <mtd/mtd-user.h>
37#endif
38
S. Lockwood-Childs34255b92017-11-14 23:01:26 -080039#include <mtd/ubi-user.h>
40
Stefano Babic9d80b492017-04-05 18:08:01 +020041#include "fw_env_private.h"
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010042#include "fw_env.h"
wdenk6aff3112002-12-17 01:51:00 +000043
Andreas Fenkart14070e62016-05-31 09:21:56 +020044struct env_opts default_opts = {
45#ifdef CONFIG_FILE
46 .config_file = CONFIG_FILE
47#endif
48};
49
Marek Vasuta8a752c2014-03-05 19:59:52 +010050#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
51
Guennadi Liakhovetski56086922008-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 Rini229695f2014-03-28 12:03:33 -040059 const char *devname; /* Device name */
Stefan Agnerf4742ca2016-07-13 17:14:38 -070060 long long devoff; /* Device offset */
wdenk4a6fd342003-04-12 23:38:12 +000061 ulong env_size; /* environment size */
62 ulong erase_size; /* device erase size */
Guennadi Liakhovetski56086922008-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-Childs34255b92017-11-14 23:01:26 -080065 int is_ubi; /* set if we use UBI volume */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020066};
wdenk6aff3112002-12-17 01:51:00 +000067
Alex Kiernanc7f52c42018-03-09 12:12:59 +000068static struct envdev_s envdevices[2] = {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020069 {
70 .mtd_type = MTD_ABSENT,
71 }, {
72 .mtd_type = MTD_ABSENT,
73 },
74};
Alex Kiernanc7f52c42018-03-09 12:12:59 +000075
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020076static int dev_current;
wdenk6aff3112002-12-17 01:51:00 +000077
78#define DEVNAME(i) envdevices[(i)].devname
wdenkd0fb80c2003-01-11 09:48:40 +000079#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk6aff3112002-12-17 01:51:00 +000080#define ENVSIZE(i) envdevices[(i)].env_size
81#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020082#define ENVSECTORS(i) envdevices[(i)].env_sectors
83#define DEVTYPE(i) envdevices[(i)].mtd_type
S. Lockwood-Childs34255b92017-11-14 23:01:26 -080084#define IS_UBI(i) envdevices[(i)].is_ubi
wdenk6aff3112002-12-17 01:51:00 +000085
Joe Hershberger497f2052012-10-03 09:38:46 +000086#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk6aff3112002-12-17 01:51:00 +000087
Andreas Fenkartf71cee42016-04-19 22:43:42 +020088static unsigned long usable_envsize;
89#define ENV_SIZE usable_envsize
wdenk6aff3112002-12-17 01:51:00 +000090
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020091struct env_image_single {
Alex Kiernanc7f52c42018-03-09 12:12:59 +000092 uint32_t crc; /* CRC32 over data bytes */
93 char data[];
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020094};
wdenk6aff3112002-12-17 01:51:00 +000095
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020096struct env_image_redundant {
Alex Kiernanc7f52c42018-03-09 12:12:59 +000097 uint32_t crc; /* CRC32 over data bytes */
98 unsigned char flags; /* active or obsolete */
99 char data[];
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200100};
101
102enum flag_scheme {
103 FLAG_NONE,
104 FLAG_BOOLEAN,
105 FLAG_INCREMENTAL,
106};
107
108struct environment {
Alex Kiernanc7f52c42018-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 Liakhovetski56086922008-09-04 13:01:49 +0200114};
115
116static struct environment environment = {
117 .flag_scheme = FLAG_NONE,
118};
wdenk6aff3112002-12-17 01:51:00 +0000119
Alex Kiernan2deb3ca2018-03-09 12:13:00 +0000120static int have_redund_env;
wdenkd0fb80c2003-01-11 09:48:40 +0000121
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100122static unsigned char active_flag = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200123/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100124static unsigned char obsolete_flag = 0;
wdenkd0fb80c2003-01-11 09:48:40 +0000125
Joe Hershbergerddd84182012-10-12 08:48:51 +0000126#define DEFAULT_ENV_INSTANCE_STATIC
127#include <env_default.h>
wdenk6aff3112002-12-17 01:51:00 +0000128
S. Lockwood-Childs34255b92017-11-14 23:01:26 -0800129#define UBI_DEV_START "/dev/ubi"
130#define UBI_SYSFS "/sys/class/ubi"
131#define UBI_VOL_NAME_PATT "ubi%d_%d"
132
133static int is_ubi_devname(const char *devname)
134{
135 return !strncmp(devname, UBI_DEV_START, sizeof(UBI_DEV_START) - 1);
136}
137
138static int ubi_check_volume_sysfs_name(const char *volume_sysfs_name,
139 const char *volname)
140{
141 char path[256];
142 FILE *file;
143 char *name;
144 int ret;
145
146 strcpy(path, UBI_SYSFS "/");
147 strcat(path, volume_sysfs_name);
148 strcat(path, "/name");
149
150 file = fopen(path, "r");
151 if (!file)
152 return -1;
153
154 ret = fscanf(file, "%ms", &name);
155 fclose(file);
156 if (ret <= 0 || !name) {
157 fprintf(stderr,
158 "Failed to read from file %s, ret = %d, name = %s\n",
159 path, ret, name);
160 return -1;
161 }
162
163 if (!strcmp(name, volname)) {
164 free(name);
165 return 0;
166 }
167 free(name);
168
169 return -1;
170}
171
172static int ubi_get_volnum_by_name(int devnum, const char *volname)
173{
174 DIR *sysfs_ubi;
175 struct dirent *dirent;
176 int ret;
177 int tmp_devnum;
178 int volnum;
179
180 sysfs_ubi = opendir(UBI_SYSFS);
181 if (!sysfs_ubi)
182 return -1;
183
184#ifdef DEBUG
185 fprintf(stderr, "Looking for volume name \"%s\"\n", volname);
186#endif
187
188 while (1) {
189 dirent = readdir(sysfs_ubi);
190 if (!dirent)
191 return -1;
192
193 ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT,
194 &tmp_devnum, &volnum);
195 if (ret == 2 && devnum == tmp_devnum) {
196 if (ubi_check_volume_sysfs_name(dirent->d_name,
197 volname) == 0)
198 return volnum;
199 }
200 }
201
202 return -1;
203}
204
205static int ubi_get_devnum_by_devname(const char *devname)
206{
207 int devnum;
208 int ret;
209
210 ret = sscanf(devname + sizeof(UBI_DEV_START) - 1, "%d", &devnum);
211 if (ret != 1)
212 return -1;
213
214 return devnum;
215}
216
217static const char *ubi_get_volume_devname(const char *devname,
218 const char *volname)
219{
220 char *volume_devname;
221 int volnum;
222 int devnum;
223 int ret;
224
225 devnum = ubi_get_devnum_by_devname(devname);
226 if (devnum < 0)
227 return NULL;
228
229 volnum = ubi_get_volnum_by_name(devnum, volname);
230 if (volnum < 0)
231 return NULL;
232
233 ret = asprintf(&volume_devname, "%s_%d", devname, volnum);
234 if (ret < 0)
235 return NULL;
236
237#ifdef DEBUG
238 fprintf(stderr, "Found ubi volume \"%s:%s\" -> %s\n",
239 devname, volname, volume_devname);
240#endif
241
242 return volume_devname;
243}
244
245static void ubi_check_dev(unsigned int dev_id)
246{
247 char *devname = (char *)DEVNAME(dev_id);
248 char *pname;
249 const char *volname = NULL;
250 const char *volume_devname;
251
252 if (!is_ubi_devname(DEVNAME(dev_id)))
253 return;
254
255 IS_UBI(dev_id) = 1;
256
257 for (pname = devname; *pname != '\0'; pname++) {
258 if (*pname == ':') {
259 *pname = '\0';
260 volname = pname + 1;
261 break;
262 }
263 }
264
265 if (volname) {
266 /* Let's find real volume device name */
267 volume_devname = ubi_get_volume_devname(devname, volname);
268 if (!volume_devname) {
269 fprintf(stderr, "Didn't found ubi volume \"%s\"\n",
270 volname);
271 return;
272 }
273
274 free(devname);
275 DEVNAME(dev_id) = volume_devname;
276 }
277}
278
279static int ubi_update_start(int fd, int64_t bytes)
280{
281 if (ioctl(fd, UBI_IOCVOLUP, &bytes))
282 return -1;
283 return 0;
284}
285
286static int ubi_read(int fd, void *buf, size_t count)
287{
288 ssize_t ret;
289
290 while (count > 0) {
291 ret = read(fd, buf, count);
292 if (ret > 0) {
293 count -= ret;
294 buf += ret;
295
296 continue;
297 }
298
299 if (ret == 0) {
300 /*
301 * Happens in case of too short volume data size. If we
302 * return error status we will fail it will be treated
303 * as UBI device error.
304 *
305 * Leave catching this error to CRC check.
306 */
307 fprintf(stderr, "Warning: end of data on ubi volume\n");
308 return 0;
309 } else if (errno == EBADF) {
310 /*
311 * Happens in case of corrupted volume. The same as
312 * above, we cannot return error now, as we will still
313 * be able to successfully write environment later.
314 */
315 fprintf(stderr, "Warning: corrupted volume?\n");
316 return 0;
317 } else if (errno == EINTR) {
318 continue;
319 }
320
321 fprintf(stderr, "Cannot read %u bytes from ubi volume, %s\n",
322 (unsigned int)count, strerror(errno));
323 return -1;
324 }
325
326 return 0;
327}
328
329static int ubi_write(int fd, const void *buf, size_t count)
330{
331 ssize_t ret;
332
333 while (count > 0) {
334 ret = write(fd, buf, count);
335 if (ret <= 0) {
336 if (ret < 0 && errno == EINTR)
337 continue;
338
339 fprintf(stderr, "Cannot write %u bytes to ubi volume\n",
340 (unsigned int)count);
341 return -1;
342 }
343
344 count -= ret;
345 buf += ret;
346 }
347
348 return 0;
349}
350
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000351static int flash_io(int mode);
Andreas Fenkart81974f42016-04-05 23:13:42 +0200352static int parse_config(struct env_opts *opts);
wdenk4a6fd342003-04-12 23:38:12 +0000353
wdenkd0fb80c2003-01-11 09:48:40 +0000354#if defined(CONFIG_FILE)
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000355static int get_config(char *);
wdenkd0fb80c2003-01-11 09:48:40 +0000356#endif
wdenk6aff3112002-12-17 01:51:00 +0000357
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100358static char *skip_chars(char *s)
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200359{
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100360 for (; *s != '\0'; s++) {
Stefan Agnercd655512018-03-01 14:06:32 +0100361 if (isblank(*s) || *s == '=')
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100362 return s;
363 }
364 return NULL;
365}
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200366
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100367static char *skip_blanks(char *s)
368{
369 for (; *s != '\0'; s++) {
370 if (!isblank(*s))
Andreas Fenkart9583efc2016-03-11 09:39:36 +0100371 return s;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200372 }
Andreas Fenkart9583efc2016-03-11 09:39:36 +0100373 return NULL;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200374}
375
wdenk6aff3112002-12-17 01:51:00 +0000376/*
Andreas Fenkart1b7427c2016-07-16 17:06:14 +0200377 * s1 is either a simple 'name', or a 'name=value' pair.
378 * s2 is a 'name=value' pair.
379 * If the names match, return the value of s2, else NULL.
380 */
381static char *envmatch(char *s1, char *s2)
382{
383 if (s1 == NULL || s2 == NULL)
384 return NULL;
385
386 while (*s1 == *s2++)
387 if (*s1++ == '=')
388 return s2;
389 if (*s1 == '\0' && *(s2 - 1) == '=')
390 return s2;
391 return NULL;
392}
393
394/**
wdenk6aff3112002-12-17 01:51:00 +0000395 * Search the environment for a variable.
396 * Return the value, if found, or NULL, if not found.
397 */
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000398char *fw_getenv(char *name)
wdenk6aff3112002-12-17 01:51:00 +0000399{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100400 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000401
wdenk4a6fd342003-04-12 23:38:12 +0000402 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100403 char *val;
wdenk6aff3112002-12-17 01:51:00 +0000404
wdenk4a6fd342003-04-12 23:38:12 +0000405 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000406 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000407 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000408 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200409 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000410 }
411 }
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000412 val = envmatch(name, env);
wdenk6aff3112002-12-17 01:51:00 +0000413 if (!val)
414 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200415 return val;
wdenk6aff3112002-12-17 01:51:00 +0000416 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200417 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000418}
419
420/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600421 * Search the default environment for a variable.
422 * Return the value, if found, or NULL, if not found.
423 */
424char *fw_getdefenv(char *name)
425{
426 char *env, *nxt;
427
428 for (env = default_environment; *env; env = nxt + 1) {
429 char *val;
430
431 for (nxt = env; *nxt; ++nxt) {
432 if (nxt >= &default_environment[ENV_SIZE]) {
433 fprintf(stderr, "## Error: "
434 "default environment not terminated\n");
435 return NULL;
436 }
437 }
438 val = envmatch(name, env);
439 if (!val)
440 continue;
441 return val;
442 }
443 return NULL;
444}
445
446/*
wdenk6aff3112002-12-17 01:51:00 +0000447 * Print the current definition of one, or more, or all
448 * environment variables
449 */
Andreas Fenkart81974f42016-04-05 23:13:42 +0200450int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +0000451{
Andreas Fenkart371ee132015-12-09 13:13:24 +0100452 int i, rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000453
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200454 if (value_only && argc != 1) {
455 fprintf(stderr,
Alex Kiernand877a6c2018-02-11 17:16:46 +0000456 "## Error: `-n'/`--noheader' option requires exactly one argument\n");
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200457 return -1;
458 }
459
Andreas Fenkart14070e62016-05-31 09:21:56 +0200460 if (!opts)
461 opts = &default_opts;
462
Andreas Fenkart81974f42016-04-05 23:13:42 +0200463 if (fw_env_open(opts))
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200464 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000465
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000466 if (argc == 0) { /* Print all env variables */
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200467 char *env, *nxt;
wdenk4a6fd342003-04-12 23:38:12 +0000468 for (env = environment.data; *env; env = nxt + 1) {
469 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000470 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000471 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000472 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200473 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000474 }
475 }
476
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000477 printf("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000478 }
Stefano Babic33f00862017-04-05 18:08:03 +0200479 fw_env_close(opts);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200480 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000481 }
482
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200483 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100484 char *name = argv[i];
485 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000486
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200487 val = fw_getenv(name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700488 if (!val) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000489 fprintf(stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700490 rc = -1;
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200491 continue;
Grant Ericksonbc117562008-05-06 20:16:15 -0700492 }
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200493
494 if (value_only) {
495 puts(val);
496 break;
497 }
498
499 printf("%s=%s\n", name, val);
wdenk6aff3112002-12-17 01:51:00 +0000500 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700501
Stefano Babic33f00862017-04-05 18:08:03 +0200502 fw_env_close(opts);
503
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200504 return rc;
wdenk6aff3112002-12-17 01:51:00 +0000505}
506
Stefano Babic33f00862017-04-05 18:08:03 +0200507int fw_env_flush(struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +0000508{
Andreas Fenkart14070e62016-05-31 09:21:56 +0200509 if (!opts)
510 opts = &default_opts;
511
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200512 /*
513 * Update CRC
514 */
515 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000516
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200517 /* write environment back to flash */
518 if (flash_io(O_RDWR)) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000519 fprintf(stderr, "Error: can't write fw_env to flash\n");
520 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000521 }
522
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200523 return 0;
524}
wdenk6aff3112002-12-17 01:51:00 +0000525
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200526/*
527 * Set/Clear a single variable in the environment.
528 * This is called in sequence to update the environment
529 * in RAM without updating the copy in flash after each set
530 */
531int fw_env_write(char *name, char *value)
532{
533 int len;
534 char *env, *nxt;
535 char *oldval = NULL;
Joe Hershberger267541f2012-12-11 22:16:34 -0600536 int deleting, creating, overwriting;
wdenk6aff3112002-12-17 01:51:00 +0000537
538 /*
539 * search if variable with this name already exists
540 */
wdenk4a6fd342003-04-12 23:38:12 +0000541 for (nxt = env = environment.data; *env; env = nxt + 1) {
542 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000543 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200544 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000545 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200546 errno = EINVAL;
547 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000548 }
549 }
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000550 oldval = envmatch(name, env);
551 if (oldval)
wdenk6aff3112002-12-17 01:51:00 +0000552 break;
553 }
554
Joe Hershberger267541f2012-12-11 22:16:34 -0600555 deleting = (oldval && !(value && strlen(value)));
556 creating = (!oldval && (value && strlen(value)));
557 overwriting = (oldval && (value && strlen(value)));
558
559 /* check for permission */
560 if (deleting) {
561 if (env_flags_validate_varaccess(name,
562 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
563 printf("Can't delete \"%s\"\n", name);
564 errno = EROFS;
565 return -1;
566 }
567 } else if (overwriting) {
568 if (env_flags_validate_varaccess(name,
569 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
570 printf("Can't overwrite \"%s\"\n", name);
571 errno = EROFS;
572 return -1;
573 } else if (env_flags_validate_varaccess(name,
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000574 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
Joe Hershberger267541f2012-12-11 22:16:34 -0600575 const char *defval = fw_getdefenv(name);
576
577 if (defval == NULL)
578 defval = "";
579 if (strcmp(oldval, defval)
580 != 0) {
581 printf("Can't overwrite \"%s\"\n", name);
582 errno = EROFS;
583 return -1;
584 }
585 }
586 } else if (creating) {
587 if (env_flags_validate_varaccess(name,
588 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
589 printf("Can't create \"%s\"\n", name);
590 errno = EROFS;
591 return -1;
592 }
593 } else
594 /* Nothing to do */
595 return 0;
596
597 if (deleting || overwriting) {
wdenk6aff3112002-12-17 01:51:00 +0000598 if (*++nxt == '\0') {
599 *env = '\0';
600 } else {
601 for (;;) {
602 *env = *nxt++;
603 if ((*env == '\0') && (*nxt == '\0'))
604 break;
605 ++env;
606 }
607 }
608 *++env = '\0';
609 }
610
611 /* Delete only ? */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200612 if (!value || !strlen(value))
613 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000614
615 /*
616 * Append new definition at the end
617 */
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000618 for (env = environment.data; *env || *(env + 1); ++env)
619 ;
wdenk6aff3112002-12-17 01:51:00 +0000620 if (env > environment.data)
621 ++env;
622 /*
623 * Overflow when:
Joe Hershberger497f2052012-10-03 09:38:46 +0000624 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk6aff3112002-12-17 01:51:00 +0000625 */
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000626 len = strlen(name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000627 /* add '=' for first arg, ' ' for all others */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200628 len += strlen(value) + 1;
629
wdenk4a6fd342003-04-12 23:38:12 +0000630 if (len > (&environment.data[ENV_SIZE] - env)) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000631 fprintf(stderr,
632 "Error: environment overflow, \"%s\" deleted\n", name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200633 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000634 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200635
wdenk6aff3112002-12-17 01:51:00 +0000636 while ((*env = *name++) != '\0')
637 env++;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200638 *env = '=';
639 while ((*++env = *value++) != '\0')
640 ;
wdenk6aff3112002-12-17 01:51:00 +0000641
642 /* end is marked with double '\0' */
643 *++env = '\0';
644
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200645 return 0;
646}
wdenk6aff3112002-12-17 01:51:00 +0000647
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200648/*
649 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
650 * 0 - OK
651 * EINVAL - need at least 1 argument
652 * EROFS - certain variables ("ethaddr", "serial#") cannot be
653 * modified or deleted
654 *
655 */
Simon Glass382bee52017-08-03 12:22:09 -0600656int fw_env_set(int argc, char *argv[], struct env_opts *opts)
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200657{
Andreas Fenkart371ee132015-12-09 13:13:24 +0100658 int i;
Mike Frysinger3779c8e2012-11-10 19:47:45 +0000659 size_t len;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100660 char *name, **valv;
xypron.glpk@gmx.deddc6a9d2017-04-15 13:05:40 +0200661 char *oldval;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200662 char *value = NULL;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100663 int valc;
Stefano Babic33f00862017-04-05 18:08:03 +0200664 int ret;
wdenk6aff3112002-12-17 01:51:00 +0000665
Andreas Fenkart14070e62016-05-31 09:21:56 +0200666 if (!opts)
667 opts = &default_opts;
668
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100669 if (argc < 1) {
670 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasuta8a752c2014-03-05 19:59:52 +0100671 errno = EINVAL;
672 return -1;
673 }
674
Andreas Fenkart81974f42016-04-05 23:13:42 +0200675 if (fw_env_open(opts)) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200676 fprintf(stderr, "Error: environment not initialized\n");
677 return -1;
678 }
679
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100680 name = argv[0];
681 valv = argv + 1;
682 valc = argc - 1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200683
Stefano Babic33f00862017-04-05 18:08:03 +0200684 if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
685 fw_env_close(opts);
Andreas Fenkartfd4e3282016-07-16 17:06:13 +0200686 return -1;
Stefano Babic33f00862017-04-05 18:08:03 +0200687 }
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600688
Joe Hershberger62a34a02012-10-03 09:38:47 +0000689 len = 0;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100690 for (i = 0; i < valc; ++i) {
691 char *val = valv[i];
Joe Hershberger62a34a02012-10-03 09:38:47 +0000692 size_t val_len = strlen(val);
693
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000694 if (value)
695 value[len - 1] = ' ';
xypron.glpk@gmx.deddc6a9d2017-04-15 13:05:40 +0200696 oldval = value;
Joe Hershberger62a34a02012-10-03 09:38:47 +0000697 value = realloc(value, len + val_len + 1);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200698 if (!value) {
Joe Hershberger62a34a02012-10-03 09:38:47 +0000699 fprintf(stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200700 "Cannot malloc %zu bytes: %s\n",
Joe Hershberger62a34a02012-10-03 09:38:47 +0000701 len, strerror(errno));
xypron.glpk@gmx.deddc6a9d2017-04-15 13:05:40 +0200702 free(oldval);
Joe Hershberger62a34a02012-10-03 09:38:47 +0000703 return -1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200704 }
Joe Hershberger62a34a02012-10-03 09:38:47 +0000705
706 memcpy(value + len, val, val_len);
707 len += val_len;
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000708 value[len++] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200709 }
710
711 fw_env_write(name, value);
712
Joe Hershberger62a34a02012-10-03 09:38:47 +0000713 free(value);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200714
Stefano Babic33f00862017-04-05 18:08:03 +0200715 ret = fw_env_flush(opts);
716 fw_env_close(opts);
717
718 return ret;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200719}
720
721/*
722 * Parse a file and configure the u-boot variables.
723 * The script file has a very simple format, as follows:
724 *
725 * Each line has a couple with name, value:
726 * <white spaces>variable_name<white spaces>variable_value
727 *
728 * Both variable_name and variable_value are interpreted as strings.
729 * Any character after <white spaces> and before ending \r\n is interpreted
730 * as variable's value (no comment allowed on these lines !)
731 *
732 * Comments are allowed if the first character in the line is #
733 *
734 * Returns -1 and sets errno error codes:
735 * 0 - OK
736 * -1 - Error
737 */
Andreas Fenkart81974f42016-04-05 23:13:42 +0200738int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200739{
740 FILE *fp;
Alex Kiernan94b233f2018-06-07 12:20:05 +0000741 char *line = NULL;
742 size_t linesize = 0;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200743 char *name;
744 char *val;
745 int lineno = 0;
746 int len;
747 int ret = 0;
748
Andreas Fenkart14070e62016-05-31 09:21:56 +0200749 if (!opts)
750 opts = &default_opts;
751
Andreas Fenkart81974f42016-04-05 23:13:42 +0200752 if (fw_env_open(opts)) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200753 fprintf(stderr, "Error: environment not initialized\n");
754 return -1;
755 }
756
757 if (strcmp(fname, "-") == 0)
758 fp = stdin;
759 else {
760 fp = fopen(fname, "r");
761 if (fp == NULL) {
762 fprintf(stderr, "I cannot open %s for reading\n",
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000763 fname);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200764 return -1;
765 }
766 }
767
Alex Kiernan94b233f2018-06-07 12:20:05 +0000768 while ((len = getline(&line, &linesize, fp)) != -1) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200769 lineno++;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200770
771 /*
Alex Kiernan94b233f2018-06-07 12:20:05 +0000772 * Read a whole line from the file. If the line is not
773 * terminated, reports an error and exit.
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200774 */
Alex Kiernan94b233f2018-06-07 12:20:05 +0000775 if (line[len - 1] != '\n') {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200776 fprintf(stderr,
Alex Kiernan94b233f2018-06-07 12:20:05 +0000777 "Line %d not correctly terminated\n",
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200778 lineno);
779 ret = -1;
780 break;
781 }
782
783 /* Drop ending line feed / carriage return */
Alex Kiernan94b233f2018-06-07 12:20:05 +0000784 line[--len] = '\0';
785 if (len && line[len - 1] == '\r')
786 line[--len] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200787
788 /* Skip comment or empty lines */
Alex Kiernan94b233f2018-06-07 12:20:05 +0000789 if (len == 0 || line[0] == '#')
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200790 continue;
791
792 /*
Alex Kiernan94b233f2018-06-07 12:20:05 +0000793 * Search for variable's name remove leading whitespaces
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200794 */
Alex Kiernan94b233f2018-06-07 12:20:05 +0000795 name = skip_blanks(line);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200796 if (!name)
797 continue;
798
799 /* The first white space is the end of variable name */
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100800 val = skip_chars(name);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200801 len = strlen(name);
802 if (val) {
803 *val++ = '\0';
804 if ((val - name) < len)
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100805 val = skip_blanks(val);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200806 else
807 val = NULL;
808 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200809#ifdef DEBUG
810 fprintf(stderr, "Setting %s : %s\n",
811 name, val ? val : " removed");
812#endif
813
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600814 if (env_flags_validate_type(name, val) < 0) {
815 ret = -1;
816 break;
817 }
818
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200819 /*
820 * If there is an error setting a variable,
821 * try to save the environment and returns an error
822 */
823 if (fw_env_write(name, val)) {
824 fprintf(stderr,
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000825 "fw_env_write returns with error : %s\n",
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200826 strerror(errno));
827 ret = -1;
828 break;
829 }
830
831 }
Alex Kiernan94b233f2018-06-07 12:20:05 +0000832 free(line);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200833
834 /* Close file if not stdin */
835 if (strcmp(fname, "-") != 0)
836 fclose(fp);
837
Stefano Babic33f00862017-04-05 18:08:03 +0200838 ret |= fw_env_flush(opts);
839
840 fw_env_close(opts);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200841
842 return ret;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200843}
844
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200845/**
Shyam Saini919d25c2018-06-07 19:47:19 +0530846 * environment_end() - compute offset of first byte right after environment
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200847 * @dev - index of enviroment buffer
848 * Return:
Shyam Saini919d25c2018-06-07 19:47:19 +0530849 * device offset of first byte right after environment
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200850 */
851off_t environment_end(int dev)
852{
853 /* environment is block aligned */
854 return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
855}
856
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200857/*
858 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
859 * 0 - block is good
860 * > 0 - block is bad
861 * < 0 - failed to test
862 */
Andreas Fenkartc6012bb2016-08-29 23:16:58 +0200863static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200864{
865 if (mtd_type == MTD_NANDFLASH) {
Andreas Fenkartc6012bb2016-08-29 23:16:58 +0200866 int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200867
868 if (badblock < 0) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000869 perror("Cannot read bad block mark");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200870 return badblock;
871 }
872
873 if (badblock) {
874#ifdef DEBUG
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000875 fprintf(stderr, "Bad block at 0x%llx, skipping\n",
Andreas Fenkartc6012bb2016-08-29 23:16:58 +0200876 (unsigned long long)blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200877#endif
878 return badblock;
879 }
880 }
881
882 return 0;
883}
884
885/*
886 * Read data from flash at an offset into a provided buffer. On NAND it skips
887 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
888 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
889 */
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000890static int flash_read_buf(int dev, int fd, void *buf, size_t count,
891 off_t offset)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200892{
893 size_t blocklen; /* erase / write length - one block on NAND,
894 0 on NOR */
895 size_t processed = 0; /* progress counter */
896 size_t readlen = count; /* current read length */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200897 off_t block_seek; /* offset inside the current block to the start
898 of the data */
899 loff_t blockstart; /* running start of the current block -
900 MEMGETBADBLOCK needs 64 bits */
901 int rc;
902
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000903 blockstart = (offset / DEVESIZE(dev)) * DEVESIZE(dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200904
905 /* Offset inside a block */
906 block_seek = offset - blockstart;
907
Andreas Fenkartff95e572016-08-29 23:16:59 +0200908 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200909 /*
910 * NAND: calculate which blocks we are reading. We have
911 * to read one block at a time to skip bad blocks.
912 */
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000913 blocklen = DEVESIZE(dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200914
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200915 /* Limit to one block for the first read */
916 if (readlen > blocklen - block_seek)
917 readlen = blocklen - block_seek;
918 } else {
919 blocklen = 0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200920 }
921
922 /* This only runs once on NOR flash */
923 while (processed < count) {
Andreas Fenkartff95e572016-08-29 23:16:59 +0200924 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000925 if (rc < 0) /* block test failed */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200926 return -1;
927
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200928 if (blockstart + block_seek + readlen > environment_end(dev)) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200929 /* End of range is reached */
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000930 fprintf(stderr, "Too few good blocks within range\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200931 return -1;
932 }
933
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000934 if (rc) { /* block is bad */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200935 blockstart += blocklen;
936 continue;
937 }
938
939 /*
940 * If a block is bad, we retry in the next block at the same
Tom Rini2d3229b2017-09-05 14:01:29 -0400941 * offset - see env/nand.c::writeenv()
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200942 */
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000943 lseek(fd, blockstart + block_seek, SEEK_SET);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200944
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000945 rc = read(fd, buf + processed, readlen);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200946 if (rc != readlen) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000947 fprintf(stderr, "Read error on %s: %s\n",
948 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200949 return -1;
950 }
951#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000952 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000953 rc, (unsigned long long)blockstart + block_seek,
Marcin Niestroj81c878d2016-05-06 14:58:29 +0200954 DEVNAME(dev));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200955#endif
956 processed += readlen;
Alex Kiernanc7f52c42018-03-09 12:12:59 +0000957 readlen = min(blocklen, count - processed);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200958 block_seek = 0;
959 blockstart += blocklen;
960 }
961
962 return processed;
963}
964
965/*
Andreas Fenkart24307d62016-08-29 23:17:00 +0200966 * Write count bytes from begin of environment, but stay within
967 * ENVSECTORS(dev) sectors of
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100968 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
969 * erase and write the whole data at once.
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200970 */
Andreas Fenkart24307d62016-08-29 23:17:00 +0200971static int flash_write_buf(int dev, int fd, void *buf, size_t count)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200972{
973 void *data;
974 struct erase_info_user erase;
975 size_t blocklen; /* length of NAND block / NOR erase sector */
976 size_t erase_len; /* whole area that can be erased - may include
977 bad blocks */
978 size_t erasesize; /* erase / write length - one block on NAND,
979 whole area on NOR */
980 size_t processed = 0; /* progress counter */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100981 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200982 bad blocks */
983 off_t erase_offset; /* offset to the first erase block (aligned)
984 below offset */
985 off_t block_seek; /* offset inside the erase block to the start
986 of the data */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200987 loff_t blockstart; /* running start of the current block -
988 MEMGETBADBLOCK needs 64 bits */
989 int rc;
990
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200991 /*
Oliver Metze387efb2013-08-30 00:56:02 +0200992 * For mtd devices only offset and size of the environment do matter
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200993 */
Andreas Fenkartff95e572016-08-29 23:16:59 +0200994 if (DEVTYPE(dev) == MTD_ABSENT) {
Oliver Metze387efb2013-08-30 00:56:02 +0200995 blocklen = count;
Oliver Metze387efb2013-08-30 00:56:02 +0200996 erase_len = blocklen;
Andreas Fenkart24307d62016-08-29 23:17:00 +0200997 blockstart = DEVOFFSET(dev);
Oliver Metze387efb2013-08-30 00:56:02 +0200998 block_seek = 0;
999 write_total = blocklen;
1000 } else {
1001 blocklen = DEVESIZE(dev);
1002
Andreas Fenkart24307d62016-08-29 23:17:00 +02001003 erase_offset = DEVOFFSET(dev);
Oliver Metze387efb2013-08-30 00:56:02 +02001004
1005 /* Maximum area we may use */
Andreas Fenkarte2c93512016-08-29 23:16:57 +02001006 erase_len = environment_end(dev) - erase_offset;
Oliver Metze387efb2013-08-30 00:56:02 +02001007
1008 blockstart = erase_offset;
Andreas Fenkart24307d62016-08-29 23:17:00 +02001009
Oliver Metze387efb2013-08-30 00:56:02 +02001010 /* Offset inside a block */
Andreas Fenkart24307d62016-08-29 23:17:00 +02001011 block_seek = DEVOFFSET(dev) - erase_offset;
Oliver Metze387efb2013-08-30 00:56:02 +02001012
1013 /*
1014 * Data size we actually write: from the start of the block
1015 * to the start of the data, then count bytes of data, and
1016 * to the end of the block
1017 */
1018 write_total = ((block_seek + count + blocklen - 1) /
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001019 blocklen) * blocklen;
Oliver Metze387efb2013-08-30 00:56:02 +02001020 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001021
1022 /*
1023 * Support data anywhere within erase sectors: read out the complete
1024 * area to be erased, replace the environment image, write the whole
1025 * block back again.
1026 */
1027 if (write_total > count) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001028 data = malloc(erase_len);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001029 if (!data) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001030 fprintf(stderr,
1031 "Cannot malloc %zu bytes: %s\n",
1032 erase_len, strerror(errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001033 return -1;
1034 }
1035
Andreas Fenkartff95e572016-08-29 23:16:59 +02001036 rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001037 if (write_total != rc)
1038 return -1;
1039
Joe Hershberger74620e12012-10-15 15:29:25 +00001040#ifdef DEBUG
1041 fprintf(stderr, "Preserving data ");
1042 if (block_seek != 0)
1043 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
1044 if (block_seek + count != write_total) {
1045 if (block_seek != 0)
1046 fprintf(stderr, " and ");
Marcin Niestroj81c878d2016-05-06 14:58:29 +02001047 fprintf(stderr, "0x%lx - 0x%lx",
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001048 (unsigned long)block_seek + count,
1049 (unsigned long)write_total - 1);
Joe Hershberger74620e12012-10-15 15:29:25 +00001050 }
1051 fprintf(stderr, "\n");
1052#endif
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001053 /* Overwrite the old environment */
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001054 memcpy(data + block_seek, buf, count);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001055 } else {
1056 /*
1057 * We get here, iff offset is block-aligned and count is a
1058 * multiple of blocklen - see write_total calculation above
1059 */
1060 data = buf;
1061 }
1062
Andreas Fenkartff95e572016-08-29 23:16:59 +02001063 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001064 /*
1065 * NAND: calculate which blocks we are writing. We have
1066 * to write one block at a time to skip bad blocks.
1067 */
1068 erasesize = blocklen;
1069 } else {
1070 erasesize = erase_len;
1071 }
1072
1073 erase.length = erasesize;
1074
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001075 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001076 while (processed < write_total) {
Andreas Fenkartff95e572016-08-29 23:16:59 +02001077 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001078 if (rc < 0) /* block test failed */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001079 return rc;
1080
Andreas Fenkarte2c93512016-08-29 23:16:57 +02001081 if (blockstart + erasesize > environment_end(dev)) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001082 fprintf(stderr, "End of range reached, aborting\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001083 return -1;
1084 }
1085
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001086 if (rc) { /* block is bad */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001087 blockstart += blocklen;
1088 continue;
1089 }
1090
Andreas Fenkartff95e572016-08-29 23:16:59 +02001091 if (DEVTYPE(dev) != MTD_ABSENT) {
Oliver Metze387efb2013-08-30 00:56:02 +02001092 erase.start = blockstart;
1093 ioctl(fd, MEMUNLOCK, &erase);
1094 /* These do not need an explicit erase cycle */
Andreas Fenkartff95e572016-08-29 23:16:59 +02001095 if (DEVTYPE(dev) != MTD_DATAFLASH)
Oliver Metze387efb2013-08-30 00:56:02 +02001096 if (ioctl(fd, MEMERASE, &erase) != 0) {
1097 fprintf(stderr,
1098 "MTD erase error on %s: %s\n",
1099 DEVNAME(dev), strerror(errno));
1100 return -1;
1101 }
1102 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001103
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001104 if (lseek(fd, blockstart, SEEK_SET) == -1) {
1105 fprintf(stderr,
1106 "Seek error on %s: %s\n",
1107 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001108 return -1;
1109 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001110#ifdef DEBUG
Marcin Niestroj81c878d2016-05-06 14:58:29 +02001111 fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001112 (unsigned long long)erasesize,
1113 (unsigned long long)blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001114#endif
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001115 if (write(fd, data + processed, erasesize) != erasesize) {
1116 fprintf(stderr, "Write error on %s: %s\n",
1117 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001118 return -1;
1119 }
1120
Andreas Fenkartff95e572016-08-29 23:16:59 +02001121 if (DEVTYPE(dev) != MTD_ABSENT)
Oliver Metze387efb2013-08-30 00:56:02 +02001122 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001123
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001124 processed += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001125 block_seek = 0;
Dustin Byford4b774ff2014-03-06 20:48:23 -08001126 blockstart += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001127 }
1128
1129 if (write_total > count)
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001130 free(data);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001131
1132 return processed;
1133}
1134
1135/*
1136 * Set obsolete flag at offset - NOR flash only
1137 */
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001138static int flash_flag_obsolete(int dev, int fd, off_t offset)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001139{
1140 int rc;
Detlev Zundel0aef7bc2010-07-30 11:22:15 +02001141 struct erase_info_user erase;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001142
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001143 erase.start = DEVOFFSET(dev);
1144 erase.length = DEVESIZE(dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001145 /* This relies on the fact, that obsolete_flag == 0 */
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001146 rc = lseek(fd, offset, SEEK_SET);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001147 if (rc < 0) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001148 fprintf(stderr, "Cannot seek to set the flag on %s\n",
1149 DEVNAME(dev));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001150 return rc;
1151 }
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001152 ioctl(fd, MEMUNLOCK, &erase);
1153 rc = write(fd, &obsolete_flag, sizeof(obsolete_flag));
1154 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001155 if (rc < 0)
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001156 perror("Could not set obsolete flag");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001157
1158 return rc;
1159}
1160
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001161static int flash_write(int fd_current, int fd_target, int dev_target)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001162{
1163 int rc;
1164
1165 switch (environment.flag_scheme) {
1166 case FLAG_NONE:
1167 break;
1168 case FLAG_INCREMENTAL:
1169 (*environment.flags)++;
1170 break;
1171 case FLAG_BOOLEAN:
1172 *environment.flags = active_flag;
1173 break;
1174 default:
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001175 fprintf(stderr, "Unimplemented flash scheme %u\n",
1176 environment.flag_scheme);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001177 return -1;
1178 }
1179
1180#ifdef DEBUG
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001181 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001182 DEVOFFSET(dev_target), DEVNAME(dev_target));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001183#endif
Marek Vasuta8a752c2014-03-05 19:59:52 +01001184
S. Lockwood-Childs34255b92017-11-14 23:01:26 -08001185 if (IS_UBI(dev_target)) {
1186 if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0)
1187 return 0;
1188 return ubi_write(fd_target, environment.image, CUR_ENVSIZE);
1189 }
1190
Joe Hershberger497f2052012-10-03 09:38:46 +00001191 rc = flash_write_buf(dev_target, fd_target, environment.image,
Andreas Fenkart24307d62016-08-29 23:17:00 +02001192 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001193 if (rc < 0)
1194 return rc;
1195
1196 if (environment.flag_scheme == FLAG_BOOLEAN) {
1197 /* Have to set obsolete flag */
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001198 off_t offset = DEVOFFSET(dev_current) +
1199 offsetof(struct env_image_redundant, flags);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001200#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +00001201 fprintf(stderr,
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001202 "Setting obsolete flag in environment at 0x%llx on %s\n",
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001203 DEVOFFSET(dev_current), DEVNAME(dev_current));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001204#endif
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001205 flash_flag_obsolete(dev_current, fd_current, offset);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001206 }
1207
1208 return 0;
1209}
1210
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001211static int flash_read(int fd)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001212{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001213 int rc;
1214
S. Lockwood-Childs34255b92017-11-14 23:01:26 -08001215 if (IS_UBI(dev_current)) {
1216 DEVTYPE(dev_current) = MTD_ABSENT;
1217
1218 return ubi_read(fd, environment.image, CUR_ENVSIZE);
1219 }
1220
Joe Hershberger497f2052012-10-03 09:38:46 +00001221 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Andreas Fenkartff95e572016-08-29 23:16:59 +02001222 DEVOFFSET(dev_current));
Marek Vasuta8a752c2014-03-05 19:59:52 +01001223 if (rc != CUR_ENVSIZE)
1224 return -1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001225
Marek Vasuta8a752c2014-03-05 19:59:52 +01001226 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001227}
1228
Alex Kiernandbc34322018-03-09 12:13:02 +00001229static int flash_open_tempfile(const char **dname, const char **target_temp)
1230{
1231 char *dup_name = strdup(DEVNAME(dev_current));
1232 char *temp_name = NULL;
1233 int rc = -1;
1234
1235 if (!dup_name)
1236 return -1;
1237
1238 *dname = dirname(dup_name);
1239 if (!*dname)
1240 goto err;
1241
1242 rc = asprintf(&temp_name, "%s/XXXXXX", *dname);
1243 if (rc == -1)
1244 goto err;
1245
1246 rc = mkstemp(temp_name);
1247 if (rc == -1) {
1248 /* fall back to in place write */
1249 fprintf(stderr,
1250 "Can't create %s: %s\n", temp_name, strerror(errno));
1251 free(temp_name);
1252 } else {
1253 *target_temp = temp_name;
1254 /* deliberately leak dup_name as dname /might/ point into
1255 * it and we need it for our caller
1256 */
1257 dup_name = NULL;
1258 }
1259
1260err:
1261 if (dup_name)
1262 free(dup_name);
1263
1264 return rc;
1265}
1266
Alex Kiernan899b5332018-03-09 12:13:01 +00001267static int flash_io_write(int fd_current)
1268{
Alex Kiernandbc34322018-03-09 12:13:02 +00001269 int fd_target = -1, rc, dev_target;
1270 const char *dname, *target_temp = NULL;
Alex Kiernan899b5332018-03-09 12:13:01 +00001271
1272 if (have_redund_env) {
1273 /* switch to next partition for writing */
1274 dev_target = !dev_current;
1275 /* dev_target: fd_target, erase_target */
1276 fd_target = open(DEVNAME(dev_target), O_RDWR);
1277 if (fd_target < 0) {
1278 fprintf(stderr,
1279 "Can't open %s: %s\n",
1280 DEVNAME(dev_target), strerror(errno));
1281 rc = -1;
1282 goto exit;
1283 }
1284 } else {
Alex Kiernandbc34322018-03-09 12:13:02 +00001285 struct stat sb;
1286
1287 if (fstat(fd_current, &sb) == 0 && S_ISREG(sb.st_mode)) {
1288 /* if any part of flash_open_tempfile() fails we fall
1289 * back to in-place writes
1290 */
1291 fd_target = flash_open_tempfile(&dname, &target_temp);
1292 }
Alex Kiernan899b5332018-03-09 12:13:01 +00001293 dev_target = dev_current;
Alex Kiernandbc34322018-03-09 12:13:02 +00001294 if (fd_target == -1)
1295 fd_target = fd_current;
Alex Kiernan899b5332018-03-09 12:13:01 +00001296 }
1297
1298 rc = flash_write(fd_current, fd_target, dev_target);
1299
1300 if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) {
1301 fprintf(stderr,
1302 "fsync failed on %s: %s\n",
1303 DEVNAME(dev_current), strerror(errno));
1304 }
1305
Alex Kiernandbc34322018-03-09 12:13:02 +00001306 if (fd_current != fd_target) {
Alex Kiernan899b5332018-03-09 12:13:01 +00001307 if (fsync(fd_target) &&
1308 !(errno == EINVAL || errno == EROFS)) {
1309 fprintf(stderr,
1310 "fsync failed on %s: %s\n",
1311 DEVNAME(dev_current), strerror(errno));
1312 }
1313
1314 if (close(fd_target)) {
1315 fprintf(stderr,
1316 "I/O error on %s: %s\n",
1317 DEVNAME(dev_target), strerror(errno));
1318 rc = -1;
1319 }
Alex Kiernandbc34322018-03-09 12:13:02 +00001320
Alex Kiernana7623112019-06-12 15:27:55 +01001321 if (rc >= 0 && target_temp) {
Alex Kiernandbc34322018-03-09 12:13:02 +00001322 int dir_fd;
1323
1324 dir_fd = open(dname, O_DIRECTORY | O_RDONLY);
1325 if (dir_fd == -1)
1326 fprintf(stderr,
1327 "Can't open %s: %s\n",
1328 dname, strerror(errno));
1329
1330 if (rename(target_temp, DEVNAME(dev_target))) {
1331 fprintf(stderr,
1332 "rename failed %s => %s: %s\n",
1333 target_temp, DEVNAME(dev_target),
1334 strerror(errno));
1335 rc = -1;
1336 }
1337
1338 if (dir_fd != -1 && fsync(dir_fd))
1339 fprintf(stderr,
1340 "fsync failed on %s: %s\n",
1341 dname, strerror(errno));
1342
1343 if (dir_fd != -1 && close(dir_fd))
1344 fprintf(stderr,
1345 "I/O error on %s: %s\n",
1346 dname, strerror(errno));
1347 }
Alex Kiernan899b5332018-03-09 12:13:01 +00001348 }
1349 exit:
1350 return rc;
1351}
1352
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001353static int flash_io(int mode)
wdenk6aff3112002-12-17 01:51:00 +00001354{
Alex Kiernan899b5332018-03-09 12:13:01 +00001355 int fd_current, rc;
wdenk6aff3112002-12-17 01:51:00 +00001356
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001357 /* dev_current: fd_current, erase_current */
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001358 fd_current = open(DEVNAME(dev_current), mode);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001359 if (fd_current < 0) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001360 fprintf(stderr,
1361 "Can't open %s: %s\n",
1362 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001363 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001364 }
wdenk6aff3112002-12-17 01:51:00 +00001365
1366 if (mode == O_RDWR) {
Alex Kiernan899b5332018-03-09 12:13:01 +00001367 rc = flash_io_write(fd_current);
wdenk6aff3112002-12-17 01:51:00 +00001368 } else {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001369 rc = flash_read(fd_current);
wdenk6aff3112002-12-17 01:51:00 +00001370 }
1371
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001372 if (close(fd_current)) {
1373 fprintf(stderr,
1374 "I/O error on %s: %s\n",
1375 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001376 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001377 }
1378
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001379 return rc;
wdenk6aff3112002-12-17 01:51:00 +00001380}
1381
1382/*
wdenk6aff3112002-12-17 01:51:00 +00001383 * Prevent confusion if running from erased flash memory
1384 */
Andreas Fenkart81974f42016-04-05 23:13:42 +02001385int fw_env_open(struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +00001386{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001387 int crc0, crc0_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001388 unsigned char flag0;
Stefano Babic33f00862017-04-05 18:08:03 +02001389 void *addr0 = NULL;
wdenkd0fb80c2003-01-11 09:48:40 +00001390
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001391 int crc1, crc1_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001392 unsigned char flag1;
Stefano Babic33f00862017-04-05 18:08:03 +02001393 void *addr1 = NULL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001394
Marek Vasuta8a752c2014-03-05 19:59:52 +01001395 int ret;
1396
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001397 struct env_image_single *single;
1398 struct env_image_redundant *redundant;
wdenk6aff3112002-12-17 01:51:00 +00001399
Andreas Fenkart14070e62016-05-31 09:21:56 +02001400 if (!opts)
1401 opts = &default_opts;
1402
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001403 if (parse_config(opts)) /* should fill envdevices */
Stefano Babic33f00862017-04-05 18:08:03 +02001404 return -EINVAL;
wdenk4a6fd342003-04-12 23:38:12 +00001405
Joe Hershberger497f2052012-10-03 09:38:46 +00001406 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001407 if (addr0 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001408 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001409 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001410 CUR_ENVSIZE);
Stefano Babic33f00862017-04-05 18:08:03 +02001411 ret = -ENOMEM;
1412 goto open_cleanup;
wdenkd0fb80c2003-01-11 09:48:40 +00001413 }
wdenk4a6fd342003-04-12 23:38:12 +00001414
wdenkd0fb80c2003-01-11 09:48:40 +00001415 /* read environment from FLASH to local buffer */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001416 environment.image = addr0;
1417
Alex Kiernan2deb3ca2018-03-09 12:13:00 +00001418 if (have_redund_env) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001419 redundant = addr0;
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001420 environment.crc = &redundant->crc;
1421 environment.flags = &redundant->flags;
1422 environment.data = redundant->data;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001423 } else {
1424 single = addr0;
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001425 environment.crc = &single->crc;
1426 environment.flags = NULL;
1427 environment.data = single->data;
wdenkd0fb80c2003-01-11 09:48:40 +00001428 }
wdenk4a6fd342003-04-12 23:38:12 +00001429
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001430 dev_current = 0;
Tom Rini168de202018-07-01 22:10:33 -04001431 if (flash_io(O_RDONLY)) {
Stefano Babic33f00862017-04-05 18:08:03 +02001432 ret = -EIO;
1433 goto open_cleanup;
1434 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001435
Tom Rini168de202018-07-01 22:10:33 -04001436 crc0 = crc32(0, (uint8_t *)environment.data, ENV_SIZE);
1437
1438 crc0_ok = (crc0 == *environment.crc);
Alex Kiernan2deb3ca2018-03-09 12:13:00 +00001439 if (!have_redund_env) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001440 if (!crc0_ok) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001441 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001442 "Warning: Bad CRC, using default environment\n");
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001443 memcpy(environment.data, default_environment,
1444 sizeof(default_environment));
wdenk6aff3112002-12-17 01:51:00 +00001445 }
wdenkd0fb80c2003-01-11 09:48:40 +00001446 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001447 flag0 = *environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +00001448
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001449 dev_current = 1;
Joe Hershberger497f2052012-10-03 09:38:46 +00001450 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001451 if (addr1 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001452 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001453 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001454 CUR_ENVSIZE);
Stefano Babic33f00862017-04-05 18:08:03 +02001455 ret = -ENOMEM;
1456 goto open_cleanup;
wdenk4a6fd342003-04-12 23:38:12 +00001457 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001458 redundant = addr1;
wdenk4a6fd342003-04-12 23:38:12 +00001459
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001460 /*
1461 * have to set environment.image for flash_read(), careful -
1462 * other pointers in environment still point inside addr0
1463 */
1464 environment.image = addr1;
Stefano Babic33f00862017-04-05 18:08:03 +02001465 if (flash_io(O_RDONLY)) {
Tom Rini168de202018-07-01 22:10:33 -04001466 ret = -EIO;
1467 goto open_cleanup;
Stefano Babic33f00862017-04-05 18:08:03 +02001468 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001469
1470 /* Check flag scheme compatibility */
1471 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1472 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1473 environment.flag_scheme = FLAG_BOOLEAN;
1474 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1475 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1476 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001477 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1478 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1479 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershberger785881f2013-04-08 10:32:52 +00001480 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1481 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1482 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metz23ef62d2013-08-30 00:56:01 +02001483 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
S. Lockwood-Childs34255b92017-11-14 23:01:26 -08001484 DEVTYPE(!dev_current) == MTD_ABSENT &&
1485 IS_UBI(dev_current) == IS_UBI(!dev_current)) {
Oliver Metz23ef62d2013-08-30 00:56:01 +02001486 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001487 } else {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001488 fprintf(stderr, "Incompatible flash types!\n");
Stefano Babic33f00862017-04-05 18:08:03 +02001489 ret = -EINVAL;
1490 goto open_cleanup;
wdenk6aff3112002-12-17 01:51:00 +00001491 }
wdenk4a6fd342003-04-12 23:38:12 +00001492
Tom Rini168de202018-07-01 22:10:33 -04001493 crc1 = crc32(0, (uint8_t *)redundant->data, ENV_SIZE);
1494
1495 crc1_ok = (crc1 == redundant->crc);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001496 flag1 = redundant->flags;
wdenk4a6fd342003-04-12 23:38:12 +00001497
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001498 if (crc0_ok && !crc1_ok) {
1499 dev_current = 0;
1500 } else if (!crc0_ok && crc1_ok) {
1501 dev_current = 1;
1502 } else if (!crc0_ok && !crc1_ok) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001503 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001504 "Warning: Bad CRC, using default environment\n");
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001505 memcpy(environment.data, default_environment,
1506 sizeof(default_environment));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001507 dev_current = 0;
1508 } else {
1509 switch (environment.flag_scheme) {
1510 case FLAG_BOOLEAN:
1511 if (flag0 == active_flag &&
1512 flag1 == obsolete_flag) {
1513 dev_current = 0;
1514 } else if (flag0 == obsolete_flag &&
1515 flag1 == active_flag) {
1516 dev_current = 1;
1517 } else if (flag0 == flag1) {
1518 dev_current = 0;
1519 } else if (flag0 == 0xFF) {
1520 dev_current = 0;
1521 } else if (flag1 == 0xFF) {
1522 dev_current = 1;
1523 } else {
1524 dev_current = 0;
1525 }
1526 break;
1527 case FLAG_INCREMENTAL:
Jon Povey735eb0f2011-03-11 14:10:56 +09001528 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001529 dev_current = 1;
1530 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey735eb0f2011-03-11 14:10:56 +09001531 flag0 >= flag1)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001532 dev_current = 0;
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001533 else /* flag1 > flag0 */
Jon Povey735eb0f2011-03-11 14:10:56 +09001534 dev_current = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001535 break;
1536 default:
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001537 fprintf(stderr, "Unknown flag scheme %u\n",
1538 environment.flag_scheme);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001539 return -1;
1540 }
1541 }
1542
1543 /*
1544 * If we are reading, we don't need the flag and the CRC any
1545 * more, if we are writing, we will re-calculate CRC and update
1546 * flags before writing out
1547 */
1548 if (dev_current) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001549 environment.image = addr1;
1550 environment.crc = &redundant->crc;
1551 environment.flags = &redundant->flags;
1552 environment.data = redundant->data;
1553 free(addr0);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001554 } else {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001555 environment.image = addr0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001556 /* Other pointers are already set */
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001557 free(addr1);
wdenk6aff3112002-12-17 01:51:00 +00001558 }
Joe Hershberger74620e12012-10-15 15:29:25 +00001559#ifdef DEBUG
1560 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1561#endif
wdenk6aff3112002-12-17 01:51:00 +00001562 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001563 return 0;
Stefano Babic33f00862017-04-05 18:08:03 +02001564
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001565 open_cleanup:
Stefano Babic33f00862017-04-05 18:08:03 +02001566 if (addr0)
1567 free(addr0);
1568
1569 if (addr1)
Björn Stenberg2b4ea2b2019-04-17 16:56:54 +02001570 free(addr1);
Stefano Babic33f00862017-04-05 18:08:03 +02001571
1572 return ret;
1573}
1574
1575/*
1576 * Simply free allocated buffer with environment
1577 */
1578int fw_env_close(struct env_opts *opts)
1579{
1580 if (environment.image)
1581 free(environment.image);
1582
1583 environment.image = NULL;
1584
1585 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001586}
1587
Stefan Agner14fb5b22016-07-13 17:14:37 -07001588static int check_device_config(int dev)
1589{
1590 struct stat st;
S. Lockwood-Childs34255b92017-11-14 23:01:26 -08001591 int32_t lnum = 0;
Stefan Agner14fb5b22016-07-13 17:14:37 -07001592 int fd, rc = 0;
1593
S. Lockwood-Childs34255b92017-11-14 23:01:26 -08001594 /* Fills in IS_UBI(), converts DEVNAME() with ubi volume name */
1595 ubi_check_dev(dev);
1596
Stefan Agner14fb5b22016-07-13 17:14:37 -07001597 fd = open(DEVNAME(dev), O_RDONLY);
1598 if (fd < 0) {
1599 fprintf(stderr,
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001600 "Cannot open %s: %s\n", DEVNAME(dev), strerror(errno));
Stefan Agner14fb5b22016-07-13 17:14:37 -07001601 return -1;
1602 }
1603
1604 rc = fstat(fd, &st);
1605 if (rc < 0) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001606 fprintf(stderr, "Cannot stat the file %s\n", DEVNAME(dev));
Stefan Agner14fb5b22016-07-13 17:14:37 -07001607 goto err;
1608 }
1609
S. Lockwood-Childs34255b92017-11-14 23:01:26 -08001610 if (IS_UBI(dev)) {
1611 rc = ioctl(fd, UBI_IOCEBISMAP, &lnum);
1612 if (rc < 0) {
1613 fprintf(stderr, "Cannot get UBI information for %s\n",
1614 DEVNAME(dev));
1615 goto err;
1616 }
1617 } else if (S_ISCHR(st.st_mode)) {
Stefan Agner14fb5b22016-07-13 17:14:37 -07001618 struct mtd_info_user mtdinfo;
1619 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1620 if (rc < 0) {
1621 fprintf(stderr, "Cannot get MTD information for %s\n",
1622 DEVNAME(dev));
1623 goto err;
1624 }
1625 if (mtdinfo.type != MTD_NORFLASH &&
1626 mtdinfo.type != MTD_NANDFLASH &&
1627 mtdinfo.type != MTD_DATAFLASH &&
1628 mtdinfo.type != MTD_UBIVOLUME) {
1629 fprintf(stderr, "Unsupported flash type %u on %s\n",
1630 mtdinfo.type, DEVNAME(dev));
1631 goto err;
1632 }
1633 DEVTYPE(dev) = mtdinfo.type;
Max Krummenacher333ee162016-11-19 13:58:56 +01001634 if (DEVESIZE(dev) == 0)
1635 /* Assume the erase size is the same as the env-size */
1636 DEVESIZE(dev) = ENVSIZE(dev);
Stefan Agner14fb5b22016-07-13 17:14:37 -07001637 } else {
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001638 uint64_t size;
Stefan Agner14fb5b22016-07-13 17:14:37 -07001639 DEVTYPE(dev) = MTD_ABSENT;
Max Krummenacher333ee162016-11-19 13:58:56 +01001640 if (DEVESIZE(dev) == 0)
1641 /* Assume the erase size to be 512 bytes */
1642 DEVESIZE(dev) = 0x200;
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001643
1644 /*
1645 * Check for negative offsets, treat it as backwards offset
1646 * from the end of the block device
1647 */
1648 if (DEVOFFSET(dev) < 0) {
1649 rc = ioctl(fd, BLKGETSIZE64, &size);
1650 if (rc < 0) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001651 fprintf(stderr,
1652 "Could not get block device size on %s\n",
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001653 DEVNAME(dev));
1654 goto err;
1655 }
1656
1657 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1658#ifdef DEBUG
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001659 fprintf(stderr,
1660 "Calculated device offset 0x%llx on %s\n",
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001661 DEVOFFSET(dev), DEVNAME(dev));
1662#endif
1663 }
Stefan Agner14fb5b22016-07-13 17:14:37 -07001664 }
1665
Max Krummenacher333ee162016-11-19 13:58:56 +01001666 if (ENVSECTORS(dev) == 0)
1667 /* Assume enough sectors to cover the environment */
1668 ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev));
1669
1670 if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001671 fprintf(stderr,
1672 "Environment does not start on (erase) block boundary\n");
Max Krummenacher333ee162016-11-19 13:58:56 +01001673 errno = EINVAL;
1674 return -1;
1675 }
1676
1677 if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001678 fprintf(stderr,
1679 "Environment does not fit into available sectors\n");
Max Krummenacher333ee162016-11-19 13:58:56 +01001680 errno = EINVAL;
1681 return -1;
1682 }
1683
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001684 err:
Stefan Agner14fb5b22016-07-13 17:14:37 -07001685 close(fd);
1686 return rc;
1687}
wdenk6aff3112002-12-17 01:51:00 +00001688
Andreas Fenkart81974f42016-04-05 23:13:42 +02001689static int parse_config(struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +00001690{
Stefan Agner14fb5b22016-07-13 17:14:37 -07001691 int rc;
wdenk6aff3112002-12-17 01:51:00 +00001692
Andreas Fenkart14070e62016-05-31 09:21:56 +02001693 if (!opts)
1694 opts = &default_opts;
Anatolij Gustschin925c97c2016-04-29 22:00:11 +02001695
Andreas Fenkart14070e62016-05-31 09:21:56 +02001696#if defined(CONFIG_FILE)
wdenkd0fb80c2003-01-11 09:48:40 +00001697 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart81974f42016-04-05 23:13:42 +02001698 if (get_config(opts->config_file)) {
Andreas Fenkart371ee132015-12-09 13:13:24 +01001699 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart81974f42016-04-05 23:13:42 +02001700 opts->config_file);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001701 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001702 }
wdenkd0fb80c2003-01-11 09:48:40 +00001703#else
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001704 DEVNAME(0) = DEVICE1_NAME;
1705 DEVOFFSET(0) = DEVICE1_OFFSET;
1706 ENVSIZE(0) = ENV1_SIZE;
Max Krummenacher333ee162016-11-19 13:58:56 +01001707
1708 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1709 * know DEVTYPE
1710 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001711#ifdef DEVICE1_ESIZE
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001712 DEVESIZE(0) = DEVICE1_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001713#endif
1714#ifdef DEVICE1_ENVSECTORS
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001715 ENVSECTORS(0) = DEVICE1_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001716#endif
1717
wdenk6aff3112002-12-17 01:51:00 +00001718#ifdef HAVE_REDUND
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001719 DEVNAME(1) = DEVICE2_NAME;
1720 DEVOFFSET(1) = DEVICE2_OFFSET;
1721 ENVSIZE(1) = ENV2_SIZE;
Max Krummenacher333ee162016-11-19 13:58:56 +01001722
1723 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1724 * know DEVTYPE
1725 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001726#ifdef DEVICE2_ESIZE
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001727 DEVESIZE(1) = DEVICE2_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001728#endif
1729#ifdef DEVICE2_ENVSECTORS
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001730 ENVSECTORS(1) = DEVICE2_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001731#endif
Alex Kiernan2deb3ca2018-03-09 12:13:00 +00001732 have_redund_env = 1;
wdenk6aff3112002-12-17 01:51:00 +00001733#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001734#endif
Stefan Agner14fb5b22016-07-13 17:14:37 -07001735 rc = check_device_config(0);
1736 if (rc < 0)
1737 return rc;
wdenk4a6fd342003-04-12 23:38:12 +00001738
Alex Kiernan2deb3ca2018-03-09 12:13:00 +00001739 if (have_redund_env) {
Stefan Agner14fb5b22016-07-13 17:14:37 -07001740 rc = check_device_config(1);
1741 if (rc < 0)
1742 return rc;
Andreas Fenkartf71cee42016-04-19 22:43:42 +02001743
Stefan Agner14fb5b22016-07-13 17:14:37 -07001744 if (ENVSIZE(0) != ENVSIZE(1)) {
Stefan Agner14fb5b22016-07-13 17:14:37 -07001745 fprintf(stderr,
Philip Molloy9337a082019-03-31 03:44:57 +00001746 "Redundant environments have unequal size\n");
Andreas Fenkart490365c2016-08-17 23:41:53 +02001747 return -1;
Stefan Agner14fb5b22016-07-13 17:14:37 -07001748 }
Andreas Fenkartf71cee42016-04-19 22:43:42 +02001749 }
1750
1751 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
Alex Kiernan2deb3ca2018-03-09 12:13:00 +00001752 if (have_redund_env)
Andreas Fenkartf71cee42016-04-19 22:43:42 +02001753 usable_envsize -= sizeof(char);
1754
wdenk6aff3112002-12-17 01:51:00 +00001755 return 0;
1756}
wdenkd0fb80c2003-01-11 09:48:40 +00001757
1758#if defined(CONFIG_FILE)
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001759static int get_config(char *fname)
wdenkd0fb80c2003-01-11 09:48:40 +00001760{
1761 FILE *fp;
1762 int i = 0;
1763 int rc;
Alex Kiernan94b233f2018-06-07 12:20:05 +00001764 char *line = NULL;
1765 size_t linesize = 0;
Tom Rini229695f2014-03-28 12:03:33 -04001766 char *devname;
wdenkd0fb80c2003-01-11 09:48:40 +00001767
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001768 fp = fopen(fname, "r");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001769 if (fp == NULL)
1770 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001771
Alex Kiernan94b233f2018-06-07 12:20:05 +00001772 while (i < 2 && getline(&line, &linesize, fp) != -1) {
1773 /* Skip comment strings */
1774 if (line[0] == '#')
wdenkd0fb80c2003-01-11 09:48:40 +00001775 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001776
Alex Kiernan94b233f2018-06-07 12:20:05 +00001777 rc = sscanf(line, "%ms %lli %lx %lx %lx",
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001778 &devname,
1779 &DEVOFFSET(i),
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001780 &ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001781
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001782 if (rc < 3)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001783 continue;
1784
Tom Rini229695f2014-03-28 12:03:33 -04001785 DEVNAME(i) = devname;
1786
Max Krummenacher333ee162016-11-19 13:58:56 +01001787 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1788 * know DEVTYPE
1789 */
wdenkd0fb80c2003-01-11 09:48:40 +00001790
1791 i++;
1792 }
Alex Kiernan94b233f2018-06-07 12:20:05 +00001793 free(line);
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001794 fclose(fp);
wdenk4a6fd342003-04-12 23:38:12 +00001795
Alex Kiernan2deb3ca2018-03-09 12:13:00 +00001796 have_redund_env = i - 1;
Alex Kiernanc7f52c42018-03-09 12:12:59 +00001797 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +00001798 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001799 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001800 } else
1801 return 0;
1802}
1803#endif