blob: 30d5b037f09b21a252d15ca8fc447c570de43138 [file] [log] [blame]
wdenk34b613e2002-12-17 01:51:00 +00001/*
Detlev Zundel2111d3e2010-07-30 11:22:15 +02002 * (C) Copyright 2000-2010
wdenk34b613e2002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02005 * (C) Copyright 2008
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00009 */
10
11#include <errno.h>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060012#include <env_flags.h>
wdenk34b613e2002-12-17 01:51:00 +000013#include <fcntl.h>
Joe Hershbergere79a91f2012-10-12 10:23:37 +000014#include <linux/stringify.h>
wdenk34b613e2002-12-17 01:51:00 +000015#include <stdio.h>
16#include <stdlib.h>
17#include <stddef.h>
18#include <string.h>
19#include <sys/types.h>
20#include <sys/ioctl.h>
21#include <sys/stat.h>
22#include <unistd.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010023
24#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020025# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010026# include <linux/mtd/mtd.h>
27#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010028# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010029# include <mtd/mtd-user.h>
30#endif
wdenk34b613e2002-12-17 01:51:00 +000031
Markus Klotzbücher94db5572007-11-27 10:23:20 +010032#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000033
Marek Vasut05ed2a52014-03-05 19:59:52 +010034#include <aes.h>
35
36#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
37
Stefano Babicada628b2010-05-24 12:08:16 +020038#define WHITESPACE(c) ((c == '\t') || (c == ' '))
wdenk34b613e2002-12-17 01:51:00 +000039
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020040#define min(x, y) ({ \
41 typeof(x) _min1 = (x); \
42 typeof(y) _min2 = (y); \
43 (void) (&_min1 == &_min2); \
44 _min1 < _min2 ? _min1 : _min2; })
45
46struct envdev_s {
Tom Rini313e9122014-03-28 12:03:33 -040047 const char *devname; /* Device name */
wdenk3f9ab982003-04-12 23:38:12 +000048 ulong devoff; /* Device offset */
49 ulong env_size; /* environment size */
50 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020051 ulong env_sectors; /* number of environment sectors */
52 uint8_t mtd_type; /* type of the MTD device */
53};
wdenk34b613e2002-12-17 01:51:00 +000054
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020055static struct envdev_s envdevices[2] =
56{
57 {
58 .mtd_type = MTD_ABSENT,
59 }, {
60 .mtd_type = MTD_ABSENT,
61 },
62};
63static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000064
65#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000066#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000067#define ENVSIZE(i) envdevices[(i)].env_size
68#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020069#define ENVSECTORS(i) envdevices[(i)].env_sectors
70#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk34b613e2002-12-17 01:51:00 +000071
Joe Hershberger671adc42012-10-03 09:38:46 +000072#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000073
wdenke7f34c62003-01-11 09:48:40 +000074#define ENV_SIZE getenvsize()
wdenk34b613e2002-12-17 01:51:00 +000075
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020076struct env_image_single {
77 uint32_t crc; /* CRC32 over data bytes */
78 char data[];
79};
wdenk34b613e2002-12-17 01:51:00 +000080
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020081struct env_image_redundant {
82 uint32_t crc; /* CRC32 over data bytes */
83 unsigned char flags; /* active or obsolete */
84 char data[];
85};
86
87enum flag_scheme {
88 FLAG_NONE,
89 FLAG_BOOLEAN,
90 FLAG_INCREMENTAL,
91};
92
93struct environment {
94 void *image;
95 uint32_t *crc;
96 unsigned char *flags;
97 char *data;
98 enum flag_scheme flag_scheme;
99};
100
101static struct environment environment = {
102 .flag_scheme = FLAG_NONE,
103};
wdenk34b613e2002-12-17 01:51:00 +0000104
Marek Vasut05ed2a52014-03-05 19:59:52 +0100105/* Is AES encryption used? */
106static int aes_flag;
107static uint8_t aes_key[AES_KEY_LENGTH] = { 0 };
108static int env_aes_cbc_crypt(char *data, const int enc);
109
wdenke7f34c62003-01-11 09:48:40 +0000110static int HaveRedundEnv = 0;
111
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100112static unsigned char active_flag = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200113/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100114static unsigned char obsolete_flag = 0;
wdenke7f34c62003-01-11 09:48:40 +0000115
Joe Hershberger81321142012-10-12 08:48:51 +0000116#define DEFAULT_ENV_INSTANCE_STATIC
117#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000118
wdenk3f9ab982003-04-12 23:38:12 +0000119static int flash_io (int mode);
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100120static char *envmatch (char * s1, char * s2);
wdenk3f9ab982003-04-12 23:38:12 +0000121static int parse_config (void);
122
wdenke7f34c62003-01-11 09:48:40 +0000123#if defined(CONFIG_FILE)
wdenk3f9ab982003-04-12 23:38:12 +0000124static int get_config (char *);
wdenke7f34c62003-01-11 09:48:40 +0000125#endif
wdenk3f9ab982003-04-12 23:38:12 +0000126static inline ulong getenvsize (void)
wdenke7f34c62003-01-11 09:48:40 +0000127{
Joe Hershberger671adc42012-10-03 09:38:46 +0000128 ulong rc = CUR_ENVSIZE - sizeof(long);
wdenk3f9ab982003-04-12 23:38:12 +0000129
wdenke7f34c62003-01-11 09:48:40 +0000130 if (HaveRedundEnv)
wdenk3f9ab982003-04-12 23:38:12 +0000131 rc -= sizeof (char);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100132
133 if (aes_flag)
134 rc &= ~(AES_KEY_LENGTH - 1);
135
wdenke7f34c62003-01-11 09:48:40 +0000136 return rc;
137}
wdenk34b613e2002-12-17 01:51:00 +0000138
Stefano Babicada628b2010-05-24 12:08:16 +0200139static char *fw_string_blank(char *s, int noblank)
140{
141 int i;
142 int len = strlen(s);
143
144 for (i = 0; i < len; i++, s++) {
145 if ((noblank && !WHITESPACE(*s)) ||
146 (!noblank && WHITESPACE(*s)))
147 break;
148 }
149 if (i == len)
150 return NULL;
151
152 return s;
153}
154
wdenk34b613e2002-12-17 01:51:00 +0000155/*
156 * Search the environment for a variable.
157 * Return the value, if found, or NULL, if not found.
158 */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100159char *fw_getenv (char *name)
wdenk34b613e2002-12-17 01:51:00 +0000160{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100161 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000162
wdenk3f9ab982003-04-12 23:38:12 +0000163 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100164 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000165
wdenk3f9ab982003-04-12 23:38:12 +0000166 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000167 if (nxt >= &environment.data[ENV_SIZE]) {
168 fprintf (stderr, "## Error: "
169 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200170 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000171 }
172 }
wdenk3f9ab982003-04-12 23:38:12 +0000173 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000174 if (!val)
175 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200176 return val;
wdenk34b613e2002-12-17 01:51:00 +0000177 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200178 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000179}
180
181/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600182 * Search the default environment for a variable.
183 * Return the value, if found, or NULL, if not found.
184 */
185char *fw_getdefenv(char *name)
186{
187 char *env, *nxt;
188
189 for (env = default_environment; *env; env = nxt + 1) {
190 char *val;
191
192 for (nxt = env; *nxt; ++nxt) {
193 if (nxt >= &default_environment[ENV_SIZE]) {
194 fprintf(stderr, "## Error: "
195 "default environment not terminated\n");
196 return NULL;
197 }
198 }
199 val = envmatch(name, env);
200 if (!val)
201 continue;
202 return val;
203 }
204 return NULL;
205}
206
Marek Vasut05ed2a52014-03-05 19:59:52 +0100207static int parse_aes_key(char *key)
208{
209 char tmp[5] = { '0', 'x', 0, 0, 0 };
210 unsigned long ul;
211 int i;
212
213 if (strnlen(key, 64) != 32) {
214 fprintf(stderr,
215 "## Error: '-a' option requires 16-byte AES key\n");
216 return -1;
217 }
218
219 for (i = 0; i < 16; i++) {
220 tmp[2] = key[0];
221 tmp[3] = key[1];
222 errno = 0;
223 ul = strtoul(tmp, NULL, 16);
224 if (errno) {
225 fprintf(stderr,
226 "## Error: '-a' option requires valid AES key\n");
227 return -1;
228 }
229 aes_key[i] = ul & 0xff;
230 key += 2;
231 }
232 aes_flag = 1;
233
234 return 0;
235}
236
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600237/*
wdenk34b613e2002-12-17 01:51:00 +0000238 * Print the current definition of one, or more, or all
239 * environment variables
240 */
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700241int fw_printenv (int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000242{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100243 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000244 int i, n_flag;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700245 int rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000246
Marek Vasut05ed2a52014-03-05 19:59:52 +0100247 if (argc >= 2 && strcmp(argv[1], "-a") == 0) {
248 if (argc < 3) {
249 fprintf(stderr,
250 "## Error: '-a' option requires AES key\n");
251 return -1;
252 }
253 rc = parse_aes_key(argv[2]);
254 if (rc)
255 return rc;
256 argv += 2;
257 argc -= 2;
258 }
259
Stefano Babicada628b2010-05-24 12:08:16 +0200260 if (fw_env_open())
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200261 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000262
wdenk3f9ab982003-04-12 23:38:12 +0000263 if (argc == 1) { /* Print all env variables */
264 for (env = environment.data; *env; env = nxt + 1) {
265 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000266 if (nxt >= &environment.data[ENV_SIZE]) {
267 fprintf (stderr, "## Error: "
268 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200269 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000270 }
271 }
272
wdenk3f9ab982003-04-12 23:38:12 +0000273 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000274 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200275 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000276 }
277
wdenk3f9ab982003-04-12 23:38:12 +0000278 if (strcmp (argv[1], "-n") == 0) {
wdenk34b613e2002-12-17 01:51:00 +0000279 n_flag = 1;
280 ++argv;
281 --argc;
282 if (argc != 2) {
283 fprintf (stderr, "## Error: "
284 "`-n' option requires exactly one argument\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200285 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000286 }
287 } else {
288 n_flag = 0;
289 }
290
wdenk3f9ab982003-04-12 23:38:12 +0000291 for (i = 1; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100292 char *name = argv[i];
293 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000294
wdenk3f9ab982003-04-12 23:38:12 +0000295 for (env = environment.data; *env; env = nxt + 1) {
wdenk34b613e2002-12-17 01:51:00 +0000296
wdenk3f9ab982003-04-12 23:38:12 +0000297 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000298 if (nxt >= &environment.data[ENV_SIZE]) {
299 fprintf (stderr, "## Error: "
300 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200301 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000302 }
303 }
wdenk3f9ab982003-04-12 23:38:12 +0000304 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000305 if (val) {
306 if (!n_flag) {
307 fputs (name, stdout);
wdenk3f9ab982003-04-12 23:38:12 +0000308 putc ('=', stdout);
wdenk34b613e2002-12-17 01:51:00 +0000309 }
wdenk3f9ab982003-04-12 23:38:12 +0000310 puts (val);
wdenk34b613e2002-12-17 01:51:00 +0000311 break;
312 }
313 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700314 if (!val) {
wdenk3f9ab982003-04-12 23:38:12 +0000315 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700316 rc = -1;
317 }
wdenk34b613e2002-12-17 01:51:00 +0000318 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700319
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200320 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000321}
322
Stefano Babicada628b2010-05-24 12:08:16 +0200323int fw_env_close(void)
wdenk34b613e2002-12-17 01:51:00 +0000324{
Marek Vasut05ed2a52014-03-05 19:59:52 +0100325 int ret;
326 if (aes_flag) {
327 ret = env_aes_cbc_crypt(environment.data, 1);
328 if (ret) {
329 fprintf(stderr,
330 "Error: can't encrypt env for flash\n");
331 return ret;
332 }
333 }
334
Stefano Babicada628b2010-05-24 12:08:16 +0200335 /*
336 * Update CRC
337 */
338 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000339
Stefano Babicada628b2010-05-24 12:08:16 +0200340 /* write environment back to flash */
341 if (flash_io(O_RDWR)) {
342 fprintf(stderr,
343 "Error: can't write fw_env to flash\n");
344 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000345 }
346
Stefano Babicada628b2010-05-24 12:08:16 +0200347 return 0;
348}
wdenk34b613e2002-12-17 01:51:00 +0000349
Stefano Babicada628b2010-05-24 12:08:16 +0200350
351/*
352 * Set/Clear a single variable in the environment.
353 * This is called in sequence to update the environment
354 * in RAM without updating the copy in flash after each set
355 */
356int fw_env_write(char *name, char *value)
357{
358 int len;
359 char *env, *nxt;
360 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600361 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000362
363 /*
364 * search if variable with this name already exists
365 */
wdenk3f9ab982003-04-12 23:38:12 +0000366 for (nxt = env = environment.data; *env; env = nxt + 1) {
367 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000368 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200369 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000370 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200371 errno = EINVAL;
372 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000373 }
374 }
wdenk3f9ab982003-04-12 23:38:12 +0000375 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000376 break;
377 }
378
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600379 deleting = (oldval && !(value && strlen(value)));
380 creating = (!oldval && (value && strlen(value)));
381 overwriting = (oldval && (value && strlen(value)));
382
383 /* check for permission */
384 if (deleting) {
385 if (env_flags_validate_varaccess(name,
386 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
387 printf("Can't delete \"%s\"\n", name);
388 errno = EROFS;
389 return -1;
390 }
391 } else if (overwriting) {
392 if (env_flags_validate_varaccess(name,
393 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
394 printf("Can't overwrite \"%s\"\n", name);
395 errno = EROFS;
396 return -1;
397 } else if (env_flags_validate_varaccess(name,
398 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
399 const char *defval = fw_getdefenv(name);
400
401 if (defval == NULL)
402 defval = "";
403 if (strcmp(oldval, defval)
404 != 0) {
405 printf("Can't overwrite \"%s\"\n", name);
406 errno = EROFS;
407 return -1;
408 }
409 }
410 } else if (creating) {
411 if (env_flags_validate_varaccess(name,
412 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
413 printf("Can't create \"%s\"\n", name);
414 errno = EROFS;
415 return -1;
416 }
417 } else
418 /* Nothing to do */
419 return 0;
420
421 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000422 if (*++nxt == '\0') {
423 *env = '\0';
424 } else {
425 for (;;) {
426 *env = *nxt++;
427 if ((*env == '\0') && (*nxt == '\0'))
428 break;
429 ++env;
430 }
431 }
432 *++env = '\0';
433 }
434
435 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200436 if (!value || !strlen(value))
437 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000438
439 /*
440 * Append new definition at the end
441 */
wdenk3f9ab982003-04-12 23:38:12 +0000442 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000443 if (env > environment.data)
444 ++env;
445 /*
446 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000447 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000448 */
wdenk3f9ab982003-04-12 23:38:12 +0000449 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000450 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200451 len += strlen(value) + 1;
452
wdenk3f9ab982003-04-12 23:38:12 +0000453 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000454 fprintf (stderr,
455 "Error: environment overflow, \"%s\" deleted\n",
456 name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200457 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000458 }
Stefano Babicada628b2010-05-24 12:08:16 +0200459
wdenk34b613e2002-12-17 01:51:00 +0000460 while ((*env = *name++) != '\0')
461 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200462 *env = '=';
463 while ((*++env = *value++) != '\0')
464 ;
465
466 /* end is marked with double '\0' */
467 *++env = '\0';
468
469 return 0;
470}
471
472/*
473 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
474 * 0 - OK
475 * EINVAL - need at least 1 argument
476 * EROFS - certain variables ("ethaddr", "serial#") cannot be
477 * modified or deleted
478 *
479 */
480int fw_setenv(int argc, char *argv[])
481{
Marek Vasut05ed2a52014-03-05 19:59:52 +0100482 int i, rc;
Mike Frysinger178ec772012-11-10 19:47:45 +0000483 size_t len;
Stefano Babicada628b2010-05-24 12:08:16 +0200484 char *name;
485 char *value = NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200486
487 if (argc < 2) {
488 errno = EINVAL;
489 return -1;
490 }
491
Marek Vasut05ed2a52014-03-05 19:59:52 +0100492 if (strcmp(argv[1], "-a") == 0) {
493 if (argc < 3) {
494 fprintf(stderr,
495 "## Error: '-a' option requires AES key\n");
496 return -1;
497 }
498 rc = parse_aes_key(argv[2]);
499 if (rc)
500 return rc;
501 argv += 2;
502 argc -= 2;
503 }
504
505 if (argc < 2) {
506 errno = EINVAL;
507 return -1;
508 }
509
Stefano Babicada628b2010-05-24 12:08:16 +0200510 if (fw_env_open()) {
511 fprintf(stderr, "Error: environment not initialized\n");
512 return -1;
513 }
514
515 name = argv[1];
516
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600517 if (env_flags_validate_env_set_params(argc, argv) < 0)
518 return 1;
519
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000520 len = 0;
wdenk3f9ab982003-04-12 23:38:12 +0000521 for (i = 2; i < argc; ++i) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100522 char *val = argv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000523 size_t val_len = strlen(val);
524
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000525 if (value)
526 value[len - 1] = ' ';
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000527 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200528 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000529 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200530 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000531 len, strerror(errno));
532 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200533 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000534
535 memcpy(value + len, val, val_len);
536 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000537 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000538 }
539
Stefano Babicada628b2010-05-24 12:08:16 +0200540 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000541
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000542 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000543
Stefano Babicada628b2010-05-24 12:08:16 +0200544 return fw_env_close();
545}
wdenk34b613e2002-12-17 01:51:00 +0000546
Stefano Babicada628b2010-05-24 12:08:16 +0200547/*
548 * Parse a file and configure the u-boot variables.
549 * The script file has a very simple format, as follows:
550 *
551 * Each line has a couple with name, value:
552 * <white spaces>variable_name<white spaces>variable_value
553 *
554 * Both variable_name and variable_value are interpreted as strings.
555 * Any character after <white spaces> and before ending \r\n is interpreted
556 * as variable's value (no comment allowed on these lines !)
557 *
558 * Comments are allowed if the first character in the line is #
559 *
560 * Returns -1 and sets errno error codes:
561 * 0 - OK
562 * -1 - Error
563 */
564int fw_parse_script(char *fname)
565{
566 FILE *fp;
567 char dump[1024]; /* Maximum line length in the file */
568 char *name;
569 char *val;
570 int lineno = 0;
571 int len;
572 int ret = 0;
573
574 if (fw_env_open()) {
575 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200576 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000577 }
578
Stefano Babicada628b2010-05-24 12:08:16 +0200579 if (strcmp(fname, "-") == 0)
580 fp = stdin;
581 else {
582 fp = fopen(fname, "r");
583 if (fp == NULL) {
584 fprintf(stderr, "I cannot open %s for reading\n",
585 fname);
586 return -1;
587 }
588 }
589
590 while (fgets(dump, sizeof(dump), fp)) {
591 lineno++;
592 len = strlen(dump);
593
594 /*
595 * Read a whole line from the file. If the line is too long
596 * or is not terminated, reports an error and exit.
597 */
598 if (dump[len - 1] != '\n') {
599 fprintf(stderr,
600 "Line %d not corrected terminated or too long\n",
601 lineno);
602 ret = -1;
603 break;
604 }
605
606 /* Drop ending line feed / carriage return */
607 while (len > 0 && (dump[len - 1] == '\n' ||
608 dump[len - 1] == '\r')) {
609 dump[len - 1] = '\0';
610 len--;
611 }
612
613 /* Skip comment or empty lines */
614 if ((len == 0) || dump[0] == '#')
615 continue;
616
617 /*
618 * Search for variable's name,
619 * remove leading whitespaces
620 */
621 name = fw_string_blank(dump, 1);
622 if (!name)
623 continue;
624
625 /* The first white space is the end of variable name */
626 val = fw_string_blank(name, 0);
627 len = strlen(name);
628 if (val) {
629 *val++ = '\0';
630 if ((val - name) < len)
631 val = fw_string_blank(val, 1);
632 else
633 val = NULL;
634 }
635
636#ifdef DEBUG
637 fprintf(stderr, "Setting %s : %s\n",
638 name, val ? val : " removed");
639#endif
640
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600641 if (env_flags_validate_type(name, val) < 0) {
642 ret = -1;
643 break;
644 }
645
Stefano Babicada628b2010-05-24 12:08:16 +0200646 /*
647 * If there is an error setting a variable,
648 * try to save the environment and returns an error
649 */
650 if (fw_env_write(name, val)) {
651 fprintf(stderr,
652 "fw_env_write returns with error : %s\n",
653 strerror(errno));
654 ret = -1;
655 break;
656 }
657
658 }
659
660 /* Close file if not stdin */
661 if (strcmp(fname, "-") != 0)
662 fclose(fp);
663
664 ret |= fw_env_close();
665
666 return ret;
667
wdenk34b613e2002-12-17 01:51:00 +0000668}
669
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200670/*
671 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
672 * 0 - block is good
673 * > 0 - block is bad
674 * < 0 - failed to test
675 */
676static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000677{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200678 if (mtd_type == MTD_NANDFLASH) {
679 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000680
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200681 if (badblock < 0) {
682 perror ("Cannot read bad block mark");
683 return badblock;
684 }
685
686 if (badblock) {
687#ifdef DEBUG
688 fprintf (stderr, "Bad block at 0x%llx, "
689 "skipping\n", *blockstart);
690#endif
691 return badblock;
692 }
wdenk34b613e2002-12-17 01:51:00 +0000693 }
694
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200695 return 0;
696}
697
698/*
699 * Read data from flash at an offset into a provided buffer. On NAND it skips
700 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
701 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
702 */
703static int flash_read_buf (int dev, int fd, void *buf, size_t count,
704 off_t offset, uint8_t mtd_type)
705{
706 size_t blocklen; /* erase / write length - one block on NAND,
707 0 on NOR */
708 size_t processed = 0; /* progress counter */
709 size_t readlen = count; /* current read length */
710 off_t top_of_range; /* end of the last block we may use */
711 off_t block_seek; /* offset inside the current block to the start
712 of the data */
713 loff_t blockstart; /* running start of the current block -
714 MEMGETBADBLOCK needs 64 bits */
715 int rc;
716
Remy Bohmercb969eb2011-02-12 19:06:26 +0100717 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200718
719 /* Offset inside a block */
720 block_seek = offset - blockstart;
721
722 if (mtd_type == MTD_NANDFLASH) {
723 /*
724 * NAND: calculate which blocks we are reading. We have
725 * to read one block at a time to skip bad blocks.
726 */
727 blocklen = DEVESIZE (dev);
728
729 /*
730 * To calculate the top of the range, we have to use the
731 * global DEVOFFSET (dev), which can be different from offset
732 */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100733 top_of_range = ((DEVOFFSET(dev) / blocklen) +
734 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200735
736 /* Limit to one block for the first read */
737 if (readlen > blocklen - block_seek)
738 readlen = blocklen - block_seek;
739 } else {
740 blocklen = 0;
741 top_of_range = offset + count;
wdenke7f34c62003-01-11 09:48:40 +0000742 }
wdenk34b613e2002-12-17 01:51:00 +0000743
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200744 /* This only runs once on NOR flash */
745 while (processed < count) {
746 rc = flash_bad_block (fd, mtd_type, &blockstart);
747 if (rc < 0) /* block test failed */
748 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000749
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200750 if (blockstart + block_seek + readlen > top_of_range) {
751 /* End of range is reached */
752 fprintf (stderr,
753 "Too few good blocks within range\n");
754 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000755 }
756
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200757 if (rc) { /* block is bad */
758 blockstart += blocklen;
759 continue;
wdenk34b613e2002-12-17 01:51:00 +0000760 }
761
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200762 /*
763 * If a block is bad, we retry in the next block at the same
764 * offset - see common/env_nand.c::writeenv()
765 */
766 lseek (fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000767
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200768 rc = read (fd, buf + processed, readlen);
769 if (rc != readlen) {
770 fprintf (stderr, "Read error on %s: %s\n",
771 DEVNAME (dev), strerror (errno));
772 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000773 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200774#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000775 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
776 rc, blockstart + block_seek, DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200777#endif
778 processed += readlen;
779 readlen = min (blocklen, count - processed);
780 block_seek = 0;
781 blockstart += blocklen;
782 }
783
784 return processed;
785}
786
787/*
Remy Bohmercb969eb2011-02-12 19:06:26 +0100788 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
789 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
790 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200791 */
792static int flash_write_buf (int dev, int fd, void *buf, size_t count,
793 off_t offset, uint8_t mtd_type)
794{
795 void *data;
796 struct erase_info_user erase;
797 size_t blocklen; /* length of NAND block / NOR erase sector */
798 size_t erase_len; /* whole area that can be erased - may include
799 bad blocks */
800 size_t erasesize; /* erase / write length - one block on NAND,
801 whole area on NOR */
802 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100803 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200804 bad blocks */
805 off_t erase_offset; /* offset to the first erase block (aligned)
806 below offset */
807 off_t block_seek; /* offset inside the erase block to the start
808 of the data */
809 off_t top_of_range; /* end of the last block we may use */
810 loff_t blockstart; /* running start of the current block -
811 MEMGETBADBLOCK needs 64 bits */
812 int rc;
813
Oliver Metze9c66ab2013-08-30 00:56:02 +0200814 /*
815 * For mtd devices only offset and size of the environment do matter
816 */
817 if (mtd_type == MTD_ABSENT) {
818 blocklen = count;
819 top_of_range = offset + count;
820 erase_len = blocklen;
821 blockstart = offset;
822 block_seek = 0;
823 write_total = blocklen;
824 } else {
825 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200826
Oliver Metze9c66ab2013-08-30 00:56:02 +0200827 top_of_range = ((DEVOFFSET(dev) / blocklen) +
828 ENVSECTORS(dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200829
Oliver Metze9c66ab2013-08-30 00:56:02 +0200830 erase_offset = (offset / blocklen) * blocklen;
wdenk34b613e2002-12-17 01:51:00 +0000831
Oliver Metze9c66ab2013-08-30 00:56:02 +0200832 /* Maximum area we may use */
833 erase_len = top_of_range - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +0000834
Oliver Metze9c66ab2013-08-30 00:56:02 +0200835 blockstart = erase_offset;
836 /* Offset inside a block */
837 block_seek = offset - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200838
Oliver Metze9c66ab2013-08-30 00:56:02 +0200839 /*
840 * Data size we actually write: from the start of the block
841 * to the start of the data, then count bytes of data, and
842 * to the end of the block
843 */
844 write_total = ((block_seek + count + blocklen - 1) /
845 blocklen) * blocklen;
846 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200847
848 /*
849 * Support data anywhere within erase sectors: read out the complete
850 * area to be erased, replace the environment image, write the whole
851 * block back again.
852 */
853 if (write_total > count) {
854 data = malloc (erase_len);
855 if (!data) {
wdenke7f34c62003-01-11 09:48:40 +0000856 fprintf (stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200857 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200858 erase_len, strerror (errno));
859 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000860 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200861
862 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
863 mtd_type);
864 if (write_total != rc)
865 return -1;
866
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000867#ifdef DEBUG
868 fprintf(stderr, "Preserving data ");
869 if (block_seek != 0)
870 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
871 if (block_seek + count != write_total) {
872 if (block_seek != 0)
873 fprintf(stderr, " and ");
874 fprintf(stderr, "0x%lx - 0x%x",
875 block_seek + count, write_total - 1);
876 }
877 fprintf(stderr, "\n");
878#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200879 /* Overwrite the old environment */
880 memcpy (data + block_seek, buf, count);
881 } else {
882 /*
883 * We get here, iff offset is block-aligned and count is a
884 * multiple of blocklen - see write_total calculation above
885 */
886 data = buf;
887 }
888
889 if (mtd_type == MTD_NANDFLASH) {
890 /*
891 * NAND: calculate which blocks we are writing. We have
892 * to write one block at a time to skip bad blocks.
893 */
894 erasesize = blocklen;
895 } else {
896 erasesize = erase_len;
897 }
898
899 erase.length = erasesize;
900
Remy Bohmercb969eb2011-02-12 19:06:26 +0100901 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200902 while (processed < write_total) {
903 rc = flash_bad_block (fd, mtd_type, &blockstart);
904 if (rc < 0) /* block test failed */
905 return rc;
906
907 if (blockstart + erasesize > top_of_range) {
908 fprintf (stderr, "End of range reached, aborting\n");
909 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000910 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200911
912 if (rc) { /* block is bad */
913 blockstart += blocklen;
914 continue;
915 }
916
Oliver Metze9c66ab2013-08-30 00:56:02 +0200917 if (mtd_type != MTD_ABSENT) {
918 erase.start = blockstart;
919 ioctl(fd, MEMUNLOCK, &erase);
920 /* These do not need an explicit erase cycle */
921 if (mtd_type != MTD_DATAFLASH)
922 if (ioctl(fd, MEMERASE, &erase) != 0) {
923 fprintf(stderr,
924 "MTD erase error on %s: %s\n",
925 DEVNAME(dev), strerror(errno));
926 return -1;
927 }
928 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200929
930 if (lseek (fd, blockstart, SEEK_SET) == -1) {
wdenk34b613e2002-12-17 01:51:00 +0000931 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200932 "Seek error on %s: %s\n",
933 DEVNAME (dev), strerror (errno));
934 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000935 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200936
937#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000938 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
939 blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200940#endif
941 if (write (fd, data + processed, erasesize) != erasesize) {
942 fprintf (stderr, "Write error on %s: %s\n",
943 DEVNAME (dev), strerror (errno));
944 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000945 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200946
Oliver Metze9c66ab2013-08-30 00:56:02 +0200947 if (mtd_type != MTD_ABSENT)
948 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200949
Dustin Byford0983cad2014-03-06 20:48:23 -0800950 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200951 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -0800952 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200953 }
954
955 if (write_total > count)
956 free (data);
957
958 return processed;
959}
960
961/*
962 * Set obsolete flag at offset - NOR flash only
963 */
964static int flash_flag_obsolete (int dev, int fd, off_t offset)
965{
966 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200967 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200968
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200969 erase.start = DEVOFFSET (dev);
970 erase.length = DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200971 /* This relies on the fact, that obsolete_flag == 0 */
972 rc = lseek (fd, offset, SEEK_SET);
973 if (rc < 0) {
974 fprintf (stderr, "Cannot seek to set the flag on %s \n",
975 DEVNAME (dev));
976 return rc;
977 }
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200978 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200979 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200980 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200981 if (rc < 0)
982 perror ("Could not set obsolete flag");
983
984 return rc;
985}
986
Marek Vasut05ed2a52014-03-05 19:59:52 +0100987/* Encrypt or decrypt the environment before writing or reading it. */
988static int env_aes_cbc_crypt(char *payload, const int enc)
989{
990 uint8_t *data = (uint8_t *)payload;
991 const int len = getenvsize();
992 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
993 uint32_t aes_blocks;
994
995 /* First we expand the key. */
996 aes_expand_key(aes_key, key_exp);
997
998 /* Calculate the number of AES blocks to encrypt. */
999 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
1000
1001 if (enc)
1002 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
1003 else
1004 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
1005
1006 return 0;
1007}
1008
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001009static int flash_write (int fd_current, int fd_target, int dev_target)
1010{
1011 int rc;
1012
1013 switch (environment.flag_scheme) {
1014 case FLAG_NONE:
1015 break;
1016 case FLAG_INCREMENTAL:
1017 (*environment.flags)++;
1018 break;
1019 case FLAG_BOOLEAN:
1020 *environment.flags = active_flag;
1021 break;
1022 default:
1023 fprintf (stderr, "Unimplemented flash scheme %u \n",
1024 environment.flag_scheme);
1025 return -1;
1026 }
1027
1028#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001029 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001030 DEVOFFSET (dev_target), DEVNAME (dev_target));
1031#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +01001032
Joe Hershberger671adc42012-10-03 09:38:46 +00001033 rc = flash_write_buf(dev_target, fd_target, environment.image,
1034 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001035 DEVTYPE(dev_target));
1036 if (rc < 0)
1037 return rc;
1038
1039 if (environment.flag_scheme == FLAG_BOOLEAN) {
1040 /* Have to set obsolete flag */
1041 off_t offset = DEVOFFSET (dev_current) +
1042 offsetof (struct env_image_redundant, flags);
1043#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001044 fprintf(stderr,
1045 "Setting obsolete flag in environment at 0x%lx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001046 DEVOFFSET (dev_current), DEVNAME (dev_current));
1047#endif
1048 flash_flag_obsolete (dev_current, fd_current, offset);
1049 }
1050
1051 return 0;
1052}
1053
1054static int flash_read (int fd)
1055{
1056 struct mtd_info_user mtdinfo;
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001057 struct stat st;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001058 int rc;
1059
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001060 rc = fstat(fd, &st);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001061 if (rc < 0) {
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001062 fprintf(stderr, "Cannot stat the file %s\n",
1063 DEVNAME(dev_current));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001064 return -1;
1065 }
1066
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001067 if (S_ISCHR(st.st_mode)) {
1068 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1069 if (rc < 0) {
1070 fprintf(stderr, "Cannot get MTD information for %s\n",
1071 DEVNAME(dev_current));
1072 return -1;
1073 }
1074 if (mtdinfo.type != MTD_NORFLASH &&
1075 mtdinfo.type != MTD_NANDFLASH &&
Joe Hershberger0c5faa82013-04-08 10:32:51 +00001076 mtdinfo.type != MTD_DATAFLASH &&
1077 mtdinfo.type != MTD_UBIVOLUME) {
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001078 fprintf (stderr, "Unsupported flash type %u on %s\n",
1079 mtdinfo.type, DEVNAME(dev_current));
1080 return -1;
1081 }
1082 } else {
1083 memset(&mtdinfo, 0, sizeof(mtdinfo));
1084 mtdinfo.type = MTD_ABSENT;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001085 }
1086
1087 DEVTYPE(dev_current) = mtdinfo.type;
1088
Joe Hershberger671adc42012-10-03 09:38:46 +00001089 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001090 DEVOFFSET (dev_current), mtdinfo.type);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001091 if (rc != CUR_ENVSIZE)
1092 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001093
Marek Vasut05ed2a52014-03-05 19:59:52 +01001094 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001095}
1096
1097static int flash_io (int mode)
1098{
1099 int fd_current, fd_target, rc, dev_target;
1100
1101 /* dev_current: fd_current, erase_current */
1102 fd_current = open (DEVNAME (dev_current), mode);
1103 if (fd_current < 0) {
1104 fprintf (stderr,
1105 "Can't open %s: %s\n",
1106 DEVNAME (dev_current), strerror (errno));
1107 return -1;
1108 }
1109
1110 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +00001111 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001112 /* switch to next partition for writing */
1113 dev_target = !dev_current;
1114 /* dev_target: fd_target, erase_target */
1115 fd_target = open (DEVNAME (dev_target), mode);
1116 if (fd_target < 0) {
wdenke7f34c62003-01-11 09:48:40 +00001117 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001118 "Can't open %s: %s\n",
1119 DEVNAME (dev_target),
1120 strerror (errno));
1121 rc = -1;
1122 goto exit;
wdenke7f34c62003-01-11 09:48:40 +00001123 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001124 } else {
1125 dev_target = dev_current;
1126 fd_target = fd_current;
wdenk34b613e2002-12-17 01:51:00 +00001127 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001128
1129 rc = flash_write (fd_current, fd_target, dev_target);
1130
wdenke7f34c62003-01-11 09:48:40 +00001131 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001132 if (close (fd_target)) {
wdenke7f34c62003-01-11 09:48:40 +00001133 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001134 "I/O error on %s: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001135 DEVNAME (dev_target),
wdenk3f9ab982003-04-12 23:38:12 +00001136 strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001137 rc = -1;
wdenke7f34c62003-01-11 09:48:40 +00001138 }
wdenk34b613e2002-12-17 01:51:00 +00001139 }
wdenk34b613e2002-12-17 01:51:00 +00001140 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001141 rc = flash_read (fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001142 }
1143
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001144exit:
1145 if (close (fd_current)) {
wdenk34b613e2002-12-17 01:51:00 +00001146 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001147 "I/O error on %s: %s\n",
1148 DEVNAME (dev_current), strerror (errno));
1149 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001150 }
1151
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001152 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001153}
1154
1155/*
1156 * s1 is either a simple 'name', or a 'name=value' pair.
1157 * s2 is a 'name=value' pair.
1158 * If the names match, return the value of s2, else NULL.
1159 */
1160
Markus Klotzbücher94db5572007-11-27 10:23:20 +01001161static char *envmatch (char * s1, char * s2)
wdenk34b613e2002-12-17 01:51:00 +00001162{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +00001163 if (s1 == NULL || s2 == NULL)
1164 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001165
1166 while (*s1 == *s2++)
1167 if (*s1++ == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001168 return s2;
wdenk3f9ab982003-04-12 23:38:12 +00001169 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001170 return s2;
1171 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001172}
1173
1174/*
1175 * Prevent confusion if running from erased flash memory
1176 */
Stefano Babicada628b2010-05-24 12:08:16 +02001177int fw_env_open(void)
wdenk34b613e2002-12-17 01:51:00 +00001178{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001179 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001180 unsigned char flag0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001181 void *addr0;
1182
wdenk34b613e2002-12-17 01:51:00 +00001183 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001184 unsigned char flag1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001185 void *addr1;
wdenke7f34c62003-01-11 09:48:40 +00001186
Marek Vasut05ed2a52014-03-05 19:59:52 +01001187 int ret;
1188
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001189 struct env_image_single *single;
1190 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001191
wdenk3f9ab982003-04-12 23:38:12 +00001192 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001193 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001194
Joe Hershberger671adc42012-10-03 09:38:46 +00001195 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001196 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001197 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001198 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001199 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001200 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001201 }
wdenk3f9ab982003-04-12 23:38:12 +00001202
wdenke7f34c62003-01-11 09:48:40 +00001203 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001204 environment.image = addr0;
1205
1206 if (HaveRedundEnv) {
1207 redundant = addr0;
1208 environment.crc = &redundant->crc;
1209 environment.flags = &redundant->flags;
1210 environment.data = redundant->data;
1211 } else {
1212 single = addr0;
1213 environment.crc = &single->crc;
1214 environment.flags = NULL;
1215 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001216 }
wdenk3f9ab982003-04-12 23:38:12 +00001217
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001218 dev_current = 0;
1219 if (flash_io (O_RDONLY))
1220 return -1;
1221
1222 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001223
1224 if (aes_flag) {
1225 ret = env_aes_cbc_crypt(environment.data, 0);
1226 if (ret)
1227 return ret;
1228 }
1229
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001230 crc0_ok = (crc0 == *environment.crc);
wdenke7f34c62003-01-11 09:48:40 +00001231 if (!HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001232 if (!crc0_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001233 fprintf (stderr,
1234 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkc5203f92006-03-12 23:27:46 +01001235 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk34b613e2002-12-17 01:51:00 +00001236 }
wdenke7f34c62003-01-11 09:48:40 +00001237 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001238 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001239
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001240 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001241 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001242 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001243 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001244 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001245 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001246 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001247 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001248 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001249
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001250 /*
1251 * have to set environment.image for flash_read(), careful -
1252 * other pointers in environment still point inside addr0
1253 */
1254 environment.image = addr1;
1255 if (flash_io (O_RDONLY))
1256 return -1;
1257
1258 /* Check flag scheme compatibility */
1259 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1260 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1261 environment.flag_scheme = FLAG_BOOLEAN;
1262 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1263 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1264 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001265 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1266 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1267 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001268 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1269 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1270 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001271 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1272 DEVTYPE(!dev_current) == MTD_ABSENT) {
1273 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001274 } else {
1275 fprintf (stderr, "Incompatible flash types!\n");
1276 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001277 }
wdenk3f9ab982003-04-12 23:38:12 +00001278
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001279 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001280
1281 if (aes_flag) {
1282 ret = env_aes_cbc_crypt(redundant->data, 0);
1283 if (ret)
1284 return ret;
1285 }
1286
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001287 crc1_ok = (crc1 == redundant->crc);
1288 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001289
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001290 if (crc0_ok && !crc1_ok) {
1291 dev_current = 0;
1292 } else if (!crc0_ok && crc1_ok) {
1293 dev_current = 1;
1294 } else if (!crc0_ok && !crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001295 fprintf (stderr,
1296 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001297 memcpy (environment.data, default_environment,
1298 sizeof default_environment);
1299 dev_current = 0;
1300 } else {
1301 switch (environment.flag_scheme) {
1302 case FLAG_BOOLEAN:
1303 if (flag0 == active_flag &&
1304 flag1 == obsolete_flag) {
1305 dev_current = 0;
1306 } else if (flag0 == obsolete_flag &&
1307 flag1 == active_flag) {
1308 dev_current = 1;
1309 } else if (flag0 == flag1) {
1310 dev_current = 0;
1311 } else if (flag0 == 0xFF) {
1312 dev_current = 0;
1313 } else if (flag1 == 0xFF) {
1314 dev_current = 1;
1315 } else {
1316 dev_current = 0;
1317 }
1318 break;
1319 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001320 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001321 dev_current = 1;
1322 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001323 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001324 dev_current = 0;
Jon Povey93ccd472011-03-11 14:10:56 +09001325 else /* flag1 > flag0 */
1326 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001327 break;
1328 default:
1329 fprintf (stderr, "Unknown flag scheme %u \n",
1330 environment.flag_scheme);
1331 return -1;
1332 }
1333 }
1334
1335 /*
1336 * If we are reading, we don't need the flag and the CRC any
1337 * more, if we are writing, we will re-calculate CRC and update
1338 * flags before writing out
1339 */
1340 if (dev_current) {
1341 environment.image = addr1;
1342 environment.crc = &redundant->crc;
1343 environment.flags = &redundant->flags;
1344 environment.data = redundant->data;
1345 free (addr0);
1346 } else {
1347 environment.image = addr0;
1348 /* Other pointers are already set */
wdenk3f9ab982003-04-12 23:38:12 +00001349 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +00001350 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001351#ifdef DEBUG
1352 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1353#endif
wdenk34b613e2002-12-17 01:51:00 +00001354 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001355 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001356}
1357
1358
wdenk3f9ab982003-04-12 23:38:12 +00001359static int parse_config ()
wdenk34b613e2002-12-17 01:51:00 +00001360{
1361 struct stat st;
1362
wdenke7f34c62003-01-11 09:48:40 +00001363#if defined(CONFIG_FILE)
1364 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
wdenk3f9ab982003-04-12 23:38:12 +00001365 if (get_config (CONFIG_FILE)) {
wdenke7f34c62003-01-11 09:48:40 +00001366 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001367 "Cannot parse config file: %s\n", strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001368 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001369 }
wdenke7f34c62003-01-11 09:48:40 +00001370#else
Tom Rini313e9122014-03-28 12:03:33 -04001371 DEVNAME (0) = DEVICE1_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001372 DEVOFFSET (0) = DEVICE1_OFFSET;
1373 ENVSIZE (0) = ENV1_SIZE;
Dustin Byford2f6df462014-03-06 20:48:22 -08001374 /* Default values are: erase-size=env-size */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001375 DEVESIZE (0) = ENVSIZE (0);
Dustin Byford2f6df462014-03-06 20:48:22 -08001376 /* #sectors=env-size/erase-size (rounded up) */
1377 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
Remy Bohmercb969eb2011-02-12 19:06:26 +01001378#ifdef DEVICE1_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001379 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001380#endif
1381#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001382 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001383#endif
1384
wdenk34b613e2002-12-17 01:51:00 +00001385#ifdef HAVE_REDUND
Tom Rini313e9122014-03-28 12:03:33 -04001386 DEVNAME (1) = DEVICE2_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001387 DEVOFFSET (1) = DEVICE2_OFFSET;
1388 ENVSIZE (1) = ENV2_SIZE;
Dustin Byford2f6df462014-03-06 20:48:22 -08001389 /* Default values are: erase-size=env-size */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001390 DEVESIZE (1) = ENVSIZE (1);
Dustin Byford2f6df462014-03-06 20:48:22 -08001391 /* #sectors=env-size/erase-size (rounded up) */
1392 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
Remy Bohmercb969eb2011-02-12 19:06:26 +01001393#ifdef DEVICE2_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001394 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001395#endif
1396#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001397 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001398#endif
wdenke7f34c62003-01-11 09:48:40 +00001399 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +00001400#endif
wdenke7f34c62003-01-11 09:48:40 +00001401#endif
wdenk3f9ab982003-04-12 23:38:12 +00001402 if (stat (DEVNAME (0), &st)) {
1403 fprintf (stderr,
1404 "Cannot access MTD device %s: %s\n",
1405 DEVNAME (0), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001406 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001407 }
wdenk3f9ab982003-04-12 23:38:12 +00001408
1409 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1410 fprintf (stderr,
1411 "Cannot access MTD device %s: %s\n",
Wolfgang Denk8e6213c2005-09-25 01:20:38 +02001412 DEVNAME (1), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001413 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001414 }
wdenk34b613e2002-12-17 01:51:00 +00001415 return 0;
1416}
wdenke7f34c62003-01-11 09:48:40 +00001417
1418#if defined(CONFIG_FILE)
1419static int get_config (char *fname)
1420{
1421 FILE *fp;
1422 int i = 0;
1423 int rc;
1424 char dump[128];
Tom Rini313e9122014-03-28 12:03:33 -04001425 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001426
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001427 fp = fopen (fname, "r");
1428 if (fp == NULL)
1429 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001430
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001431 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenke7f34c62003-01-11 09:48:40 +00001432 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001433 if (dump[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001434 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001435
Tom Rini313e9122014-03-28 12:03:33 -04001436 rc = sscanf (dump, "%ms %lx %lx %lx %lx",
1437 &devname,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001438 &DEVOFFSET (i),
1439 &ENVSIZE (i),
1440 &DEVESIZE (i),
1441 &ENVSECTORS (i));
1442
Remy Bohmercb969eb2011-02-12 19:06:26 +01001443 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001444 continue;
1445
Tom Rini313e9122014-03-28 12:03:33 -04001446 DEVNAME(i) = devname;
1447
Remy Bohmercb969eb2011-02-12 19:06:26 +01001448 if (rc < 4)
1449 /* Assume the erase size is the same as the env-size */
1450 DEVESIZE(i) = ENVSIZE(i);
1451
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001452 if (rc < 5)
Dustin Byford2f6df462014-03-06 20:48:22 -08001453 /* Assume enough env sectors to cover the environment */
1454 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
wdenke7f34c62003-01-11 09:48:40 +00001455
1456 i++;
1457 }
wdenk3f9ab982003-04-12 23:38:12 +00001458 fclose (fp);
1459
wdenke7f34c62003-01-11 09:48:40 +00001460 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +00001461 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001462 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001463 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001464 } else
1465 return 0;
1466}
1467#endif