]> git.ipfire.org Git - people/ms/u-boot.git/blame - tools/env/fw_env.c
tools: env: fw_string_blank: return from loop when item found
[people/ms/u-boot.git] / tools / env / fw_env.c
CommitLineData
6aff3115 1/*
0aef7bc7 2 * (C) Copyright 2000-2010
6aff3115
WD
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
56086921
GL
5 * (C) Copyright 2008
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
7 *
1a459660 8 * SPDX-License-Identifier: GPL-2.0+
6aff3115
WD
9 */
10
26e355d1
JK
11#define _GNU_SOURCE
12
69bf2d2f 13#include <compiler.h>
6aff3115 14#include <errno.h>
30fd4fad 15#include <env_flags.h>
6aff3115 16#include <fcntl.h>
06134211 17#include <linux/stringify.h>
10667e15 18#include <ctype.h>
6aff3115
WD
19#include <stdio.h>
20#include <stdlib.h>
21#include <stddef.h>
22#include <string.h>
23#include <sys/types.h>
24#include <sys/ioctl.h>
25#include <sys/stat.h>
26#include <unistd.h>
6aff3115 27
6de66b35 28#ifdef MTD_OLD
1711f3bd 29# include <stdint.h>
6de66b35
MK
30# include <linux/mtd/mtd.h>
31#else
c83d7ca4 32# define __user /* nothing */
6de66b35
MK
33# include <mtd/mtd-user.h>
34#endif
35
36#include "fw_env.h"
6aff3115 37
a8a752c0
MV
38#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
39
56086921
GL
40#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 {
229695fe 47 const char *devname; /* Device name */
4a6fd34b
WD
48 ulong devoff; /* Device offset */
49 ulong env_size; /* environment size */
50 ulong erase_size; /* device erase size */
56086921
GL
51 ulong env_sectors; /* number of environment sectors */
52 uint8_t mtd_type; /* type of the MTD device */
53};
6aff3115 54
56086921
GL
55static struct envdev_s envdevices[2] =
56{
57 {
58 .mtd_type = MTD_ABSENT,
59 }, {
60 .mtd_type = MTD_ABSENT,
61 },
62};
63static int dev_current;
6aff3115
WD
64
65#define DEVNAME(i) envdevices[(i)].devname
d0fb80c3 66#define DEVOFFSET(i) envdevices[(i)].devoff
6aff3115
WD
67#define ENVSIZE(i) envdevices[(i)].env_size
68#define DEVESIZE(i) envdevices[(i)].erase_size
56086921
GL
69#define ENVSECTORS(i) envdevices[(i)].env_sectors
70#define DEVTYPE(i) envdevices[(i)].mtd_type
6aff3115 71
497f2053 72#define CUR_ENVSIZE ENVSIZE(dev_current)
6aff3115 73
d0fb80c3 74#define ENV_SIZE getenvsize()
6aff3115 75
56086921
GL
76struct env_image_single {
77 uint32_t crc; /* CRC32 over data bytes */
78 char data[];
79};
6aff3115 80
56086921
GL
81struct 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};
6aff3115 104
a8a752c0
MV
105static int env_aes_cbc_crypt(char *data, const int enc);
106
d0fb80c3
WD
107static int HaveRedundEnv = 0;
108
6de66b35 109static unsigned char active_flag = 1;
56086921 110/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
6de66b35 111static unsigned char obsolete_flag = 0;
d0fb80c3 112
ddd8418f
JH
113#define DEFAULT_ENV_INSTANCE_STATIC
114#include <env_default.h>
6aff3115 115
4a6fd34b 116static int flash_io (int mode);
6de66b35 117static char *envmatch (char * s1, char * s2);
4a6fd34b
WD
118static int parse_config (void);
119
d0fb80c3 120#if defined(CONFIG_FILE)
4a6fd34b 121static int get_config (char *);
d0fb80c3 122#endif
4a6fd34b 123static inline ulong getenvsize (void)
d0fb80c3 124{
ea19527c 125 ulong rc = CUR_ENVSIZE - sizeof(uint32_t);
4a6fd34b 126
d0fb80c3 127 if (HaveRedundEnv)
4a6fd34b 128 rc -= sizeof (char);
a8a752c0 129
371ee137 130 if (common_args.aes_flag)
a8a752c0
MV
131 rc &= ~(AES_KEY_LENGTH - 1);
132
d0fb80c3
WD
133 return rc;
134}
6aff3115 135
bd7b26f8
SB
136static char *fw_string_blank(char *s, int noblank)
137{
138 int i;
139 int len = strlen(s);
140
141 for (i = 0; i < len; i++, s++) {
10667e15
AF
142 if ((noblank && !isblank(*s)) ||
143 (!noblank && isblank(*s)))
9583efcc 144 return s;
bd7b26f8 145 }
9583efcc 146 return NULL;
bd7b26f8
SB
147}
148
6aff3115
WD
149/*
150 * Search the environment for a variable.
151 * Return the value, if found, or NULL, if not found.
152 */
6de66b35 153char *fw_getenv (char *name)
6aff3115 154{
6de66b35 155 char *env, *nxt;
6aff3115 156
4a6fd34b 157 for (env = environment.data; *env; env = nxt + 1) {
6de66b35 158 char *val;
6aff3115 159
4a6fd34b 160 for (nxt = env; *nxt; ++nxt) {
6aff3115
WD
161 if (nxt >= &environment.data[ENV_SIZE]) {
162 fprintf (stderr, "## Error: "
163 "environment not terminated\n");
56086921 164 return NULL;
6aff3115
WD
165 }
166 }
4a6fd34b 167 val = envmatch (name, env);
6aff3115
WD
168 if (!val)
169 continue;
56086921 170 return val;
6aff3115 171 }
56086921 172 return NULL;
6aff3115
WD
173}
174
267541f7
JH
175/*
176 * Search the default environment for a variable.
177 * Return the value, if found, or NULL, if not found.
178 */
179char *fw_getdefenv(char *name)
180{
181 char *env, *nxt;
182
183 for (env = default_environment; *env; env = nxt + 1) {
184 char *val;
185
186 for (nxt = env; *nxt; ++nxt) {
187 if (nxt >= &default_environment[ENV_SIZE]) {
188 fprintf(stderr, "## Error: "
189 "default environment not terminated\n");
190 return NULL;
191 }
192 }
193 val = envmatch(name, env);
194 if (!val)
195 continue;
196 return val;
197 }
198 return NULL;
199}
200
371ee137 201int parse_aes_key(char *key, uint8_t *bin_key)
a8a752c0
MV
202{
203 char tmp[5] = { '0', 'x', 0, 0, 0 };
204 unsigned long ul;
205 int i;
206
207 if (strnlen(key, 64) != 32) {
208 fprintf(stderr,
209 "## Error: '-a' option requires 16-byte AES key\n");
210 return -1;
211 }
212
213 for (i = 0; i < 16; i++) {
214 tmp[2] = key[0];
215 tmp[3] = key[1];
216 errno = 0;
217 ul = strtoul(tmp, NULL, 16);
218 if (errno) {
219 fprintf(stderr,
220 "## Error: '-a' option requires valid AES key\n");
221 return -1;
222 }
8b27abd8 223 bin_key[i] = ul & 0xff;
a8a752c0
MV
224 key += 2;
225 }
a8a752c0
MV
226 return 0;
227}
228
6aff3115
WD
229/*
230 * Print the current definition of one, or more, or all
231 * environment variables
232 */
bc11756d 233int fw_printenv (int argc, char *argv[])
6aff3115 234{
6de66b35 235 char *env, *nxt;
371ee137 236 int i, rc = 0;
6aff3115 237
bd7b26f8 238 if (fw_env_open())
56086921 239 return -1;
6aff3115 240
1ce68697 241 if (argc == 0) { /* Print all env variables */
4a6fd34b
WD
242 for (env = environment.data; *env; env = nxt + 1) {
243 for (nxt = env; *nxt; ++nxt) {
6aff3115
WD
244 if (nxt >= &environment.data[ENV_SIZE]) {
245 fprintf (stderr, "## Error: "
246 "environment not terminated\n");
56086921 247 return -1;
6aff3115
WD
248 }
249 }
250
4a6fd34b 251 printf ("%s\n", env);
6aff3115 252 }
56086921 253 return 0;
6aff3115
WD
254 }
255
1ce68697
AF
256 if (printenv_args.name_suppress && argc != 1) {
257 fprintf(stderr,
258 "## Error: `-n' option requires exactly one argument\n");
259 return -1;
6aff3115
WD
260 }
261
1ce68697 262 for (i = 0; i < argc; ++i) { /* print single env variables */
6de66b35
MK
263 char *name = argv[i];
264 char *val = NULL;
6aff3115 265
4a6fd34b 266 for (env = environment.data; *env; env = nxt + 1) {
6aff3115 267
4a6fd34b 268 for (nxt = env; *nxt; ++nxt) {
6aff3115
WD
269 if (nxt >= &environment.data[ENV_SIZE]) {
270 fprintf (stderr, "## Error: "
271 "environment not terminated\n");
56086921 272 return -1;
6aff3115
WD
273 }
274 }
4a6fd34b 275 val = envmatch (name, env);
6aff3115 276 if (val) {
371ee137 277 if (!printenv_args.name_suppress) {
6aff3115 278 fputs (name, stdout);
4a6fd34b 279 putc ('=', stdout);
6aff3115 280 }
4a6fd34b 281 puts (val);
6aff3115
WD
282 break;
283 }
284 }
bc11756d 285 if (!val) {
4a6fd34b 286 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
bc11756d
GE
287 rc = -1;
288 }
6aff3115 289 }
bc11756d 290
56086921 291 return rc;
6aff3115
WD
292}
293
bd7b26f8 294int fw_env_close(void)
6aff3115 295{
a8a752c0 296 int ret;
371ee137 297 if (common_args.aes_flag) {
a8a752c0
MV
298 ret = env_aes_cbc_crypt(environment.data, 1);
299 if (ret) {
300 fprintf(stderr,
301 "Error: can't encrypt env for flash\n");
302 return ret;
303 }
304 }
305
bd7b26f8
SB
306 /*
307 * Update CRC
308 */
309 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
6aff3115 310
bd7b26f8
SB
311 /* write environment back to flash */
312 if (flash_io(O_RDWR)) {
313 fprintf(stderr,
314 "Error: can't write fw_env to flash\n");
315 return -1;
6aff3115
WD
316 }
317
bd7b26f8
SB
318 return 0;
319}
6aff3115 320
bd7b26f8
SB
321
322/*
323 * Set/Clear a single variable in the environment.
324 * This is called in sequence to update the environment
325 * in RAM without updating the copy in flash after each set
326 */
327int fw_env_write(char *name, char *value)
328{
329 int len;
330 char *env, *nxt;
331 char *oldval = NULL;
267541f7 332 int deleting, creating, overwriting;
6aff3115
WD
333
334 /*
335 * search if variable with this name already exists
336 */
4a6fd34b
WD
337 for (nxt = env = environment.data; *env; env = nxt + 1) {
338 for (nxt = env; *nxt; ++nxt) {
6aff3115 339 if (nxt >= &environment.data[ENV_SIZE]) {
bd7b26f8 340 fprintf(stderr, "## Error: "
6aff3115 341 "environment not terminated\n");
56086921
GL
342 errno = EINVAL;
343 return -1;
6aff3115
WD
344 }
345 }
4a6fd34b 346 if ((oldval = envmatch (name, env)) != NULL)
6aff3115
WD
347 break;
348 }
349
267541f7
JH
350 deleting = (oldval && !(value && strlen(value)));
351 creating = (!oldval && (value && strlen(value)));
352 overwriting = (oldval && (value && strlen(value)));
353
354 /* check for permission */
355 if (deleting) {
356 if (env_flags_validate_varaccess(name,
357 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
358 printf("Can't delete \"%s\"\n", name);
359 errno = EROFS;
360 return -1;
361 }
362 } else if (overwriting) {
363 if (env_flags_validate_varaccess(name,
364 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
365 printf("Can't overwrite \"%s\"\n", name);
366 errno = EROFS;
367 return -1;
368 } else if (env_flags_validate_varaccess(name,
369 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
370 const char *defval = fw_getdefenv(name);
371
372 if (defval == NULL)
373 defval = "";
374 if (strcmp(oldval, defval)
375 != 0) {
376 printf("Can't overwrite \"%s\"\n", name);
377 errno = EROFS;
378 return -1;
379 }
380 }
381 } else if (creating) {
382 if (env_flags_validate_varaccess(name,
383 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
384 printf("Can't create \"%s\"\n", name);
385 errno = EROFS;
386 return -1;
387 }
388 } else
389 /* Nothing to do */
390 return 0;
391
392 if (deleting || overwriting) {
6aff3115
WD
393 if (*++nxt == '\0') {
394 *env = '\0';
395 } else {
396 for (;;) {
397 *env = *nxt++;
398 if ((*env == '\0') && (*nxt == '\0'))
399 break;
400 ++env;
401 }
402 }
403 *++env = '\0';
404 }
405
406 /* Delete only ? */
bd7b26f8
SB
407 if (!value || !strlen(value))
408 return 0;
6aff3115
WD
409
410 /*
411 * Append new definition at the end
412 */
4a6fd34b 413 for (env = environment.data; *env || *(env + 1); ++env);
6aff3115
WD
414 if (env > environment.data)
415 ++env;
416 /*
417 * Overflow when:
497f2053 418 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
6aff3115 419 */
4a6fd34b 420 len = strlen (name) + 2;
6aff3115 421 /* add '=' for first arg, ' ' for all others */
bd7b26f8
SB
422 len += strlen(value) + 1;
423
4a6fd34b 424 if (len > (&environment.data[ENV_SIZE] - env)) {
6aff3115
WD
425 fprintf (stderr,
426 "Error: environment overflow, \"%s\" deleted\n",
427 name);
56086921 428 return -1;
6aff3115 429 }
bd7b26f8 430
6aff3115
WD
431 while ((*env = *name++) != '\0')
432 env++;
bd7b26f8
SB
433 *env = '=';
434 while ((*++env = *value++) != '\0')
435 ;
436
437 /* end is marked with double '\0' */
438 *++env = '\0';
439
440 return 0;
441}
442
443/*
444 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
445 * 0 - OK
446 * EINVAL - need at least 1 argument
447 * EROFS - certain variables ("ethaddr", "serial#") cannot be
448 * modified or deleted
449 *
450 */
451int fw_setenv(int argc, char *argv[])
452{
371ee137 453 int i;
3779c8e3 454 size_t len;
167f5258 455 char *name, **valv;
bd7b26f8 456 char *value = NULL;
167f5258 457 int valc;
bd7b26f8 458
1ce68697
AF
459 if (argc < 1) {
460 fprintf(stderr, "## Error: variable name missing\n");
a8a752c0
MV
461 errno = EINVAL;
462 return -1;
463 }
464
bd7b26f8
SB
465 if (fw_env_open()) {
466 fprintf(stderr, "Error: environment not initialized\n");
467 return -1;
468 }
469
1ce68697
AF
470 name = argv[0];
471 valv = argv + 1;
472 valc = argc - 1;
bd7b26f8 473
167f5258 474 if (env_flags_validate_env_set_params(name, valv, valc) < 0)
30fd4fad
JH
475 return 1;
476
62a34a04 477 len = 0;
167f5258
AF
478 for (i = 0; i < valc; ++i) {
479 char *val = valv[i];
62a34a04
JH
480 size_t val_len = strlen(val);
481
ce2f5800
JH
482 if (value)
483 value[len - 1] = ' ';
62a34a04 484 value = realloc(value, len + val_len + 1);
bd7b26f8 485 if (!value) {
62a34a04 486 fprintf(stderr,
8603b69b 487 "Cannot malloc %zu bytes: %s\n",
62a34a04
JH
488 len, strerror(errno));
489 return -1;
bd7b26f8 490 }
62a34a04
JH
491
492 memcpy(value + len, val, val_len);
493 len += val_len;
ce2f5800 494 value[len++] = '\0';
6aff3115
WD
495 }
496
bd7b26f8 497 fw_env_write(name, value);
6aff3115 498
62a34a04 499 free(value);
6aff3115 500
bd7b26f8
SB
501 return fw_env_close();
502}
6aff3115 503
bd7b26f8
SB
504/*
505 * Parse a file and configure the u-boot variables.
506 * The script file has a very simple format, as follows:
507 *
508 * Each line has a couple with name, value:
509 * <white spaces>variable_name<white spaces>variable_value
510 *
511 * Both variable_name and variable_value are interpreted as strings.
512 * Any character after <white spaces> and before ending \r\n is interpreted
513 * as variable's value (no comment allowed on these lines !)
514 *
515 * Comments are allowed if the first character in the line is #
516 *
517 * Returns -1 and sets errno error codes:
518 * 0 - OK
519 * -1 - Error
520 */
521int fw_parse_script(char *fname)
522{
523 FILE *fp;
524 char dump[1024]; /* Maximum line length in the file */
525 char *name;
526 char *val;
527 int lineno = 0;
528 int len;
529 int ret = 0;
530
531 if (fw_env_open()) {
532 fprintf(stderr, "Error: environment not initialized\n");
56086921 533 return -1;
6aff3115
WD
534 }
535
bd7b26f8
SB
536 if (strcmp(fname, "-") == 0)
537 fp = stdin;
538 else {
539 fp = fopen(fname, "r");
540 if (fp == NULL) {
541 fprintf(stderr, "I cannot open %s for reading\n",
542 fname);
543 return -1;
544 }
545 }
546
547 while (fgets(dump, sizeof(dump), fp)) {
548 lineno++;
549 len = strlen(dump);
550
551 /*
552 * Read a whole line from the file. If the line is too long
553 * or is not terminated, reports an error and exit.
554 */
555 if (dump[len - 1] != '\n') {
556 fprintf(stderr,
557 "Line %d not corrected terminated or too long\n",
558 lineno);
559 ret = -1;
560 break;
561 }
562
563 /* Drop ending line feed / carriage return */
564 while (len > 0 && (dump[len - 1] == '\n' ||
565 dump[len - 1] == '\r')) {
566 dump[len - 1] = '\0';
567 len--;
568 }
569
570 /* Skip comment or empty lines */
571 if ((len == 0) || dump[0] == '#')
572 continue;
573
574 /*
575 * Search for variable's name,
576 * remove leading whitespaces
577 */
578 name = fw_string_blank(dump, 1);
579 if (!name)
580 continue;
581
582 /* The first white space is the end of variable name */
583 val = fw_string_blank(name, 0);
584 len = strlen(name);
585 if (val) {
586 *val++ = '\0';
587 if ((val - name) < len)
588 val = fw_string_blank(val, 1);
589 else
590 val = NULL;
591 }
592
593#ifdef DEBUG
594 fprintf(stderr, "Setting %s : %s\n",
595 name, val ? val : " removed");
596#endif
597
30fd4fad
JH
598 if (env_flags_validate_type(name, val) < 0) {
599 ret = -1;
600 break;
601 }
602
bd7b26f8
SB
603 /*
604 * If there is an error setting a variable,
605 * try to save the environment and returns an error
606 */
607 if (fw_env_write(name, val)) {
608 fprintf(stderr,
609 "fw_env_write returns with error : %s\n",
610 strerror(errno));
611 ret = -1;
612 break;
613 }
614
615 }
616
617 /* Close file if not stdin */
618 if (strcmp(fname, "-") != 0)
619 fclose(fp);
620
621 ret |= fw_env_close();
622
623 return ret;
624
6aff3115
WD
625}
626
56086921
GL
627/*
628 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
629 * 0 - block is good
630 * > 0 - block is bad
631 * < 0 - failed to test
632 */
633static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
6aff3115 634{
56086921
GL
635 if (mtd_type == MTD_NANDFLASH) {
636 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
6aff3115 637
56086921
GL
638 if (badblock < 0) {
639 perror ("Cannot read bad block mark");
640 return badblock;
641 }
642
643 if (badblock) {
644#ifdef DEBUG
645 fprintf (stderr, "Bad block at 0x%llx, "
646 "skipping\n", *blockstart);
647#endif
648 return badblock;
649 }
6aff3115
WD
650 }
651
56086921
GL
652 return 0;
653}
654
655/*
656 * Read data from flash at an offset into a provided buffer. On NAND it skips
657 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
658 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
659 */
660static int flash_read_buf (int dev, int fd, void *buf, size_t count,
661 off_t offset, uint8_t mtd_type)
662{
663 size_t blocklen; /* erase / write length - one block on NAND,
664 0 on NOR */
665 size_t processed = 0; /* progress counter */
666 size_t readlen = count; /* current read length */
667 off_t top_of_range; /* end of the last block we may use */
668 off_t block_seek; /* offset inside the current block to the start
669 of the data */
670 loff_t blockstart; /* running start of the current block -
671 MEMGETBADBLOCK needs 64 bits */
672 int rc;
673
9eeaa8e6 674 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
56086921
GL
675
676 /* Offset inside a block */
677 block_seek = offset - blockstart;
678
679 if (mtd_type == MTD_NANDFLASH) {
680 /*
681 * NAND: calculate which blocks we are reading. We have
682 * to read one block at a time to skip bad blocks.
683 */
684 blocklen = DEVESIZE (dev);
685
686 /*
687 * To calculate the top of the range, we have to use the
688 * global DEVOFFSET (dev), which can be different from offset
689 */
9eeaa8e6
RB
690 top_of_range = ((DEVOFFSET(dev) / blocklen) +
691 ENVSECTORS (dev)) * blocklen;
56086921
GL
692
693 /* Limit to one block for the first read */
694 if (readlen > blocklen - block_seek)
695 readlen = blocklen - block_seek;
696 } else {
697 blocklen = 0;
698 top_of_range = offset + count;
d0fb80c3 699 }
6aff3115 700
56086921
GL
701 /* This only runs once on NOR flash */
702 while (processed < count) {
703 rc = flash_bad_block (fd, mtd_type, &blockstart);
704 if (rc < 0) /* block test failed */
705 return -1;
6aff3115 706
56086921
GL
707 if (blockstart + block_seek + readlen > top_of_range) {
708 /* End of range is reached */
709 fprintf (stderr,
710 "Too few good blocks within range\n");
711 return -1;
d0fb80c3
WD
712 }
713
56086921
GL
714 if (rc) { /* block is bad */
715 blockstart += blocklen;
716 continue;
6aff3115
WD
717 }
718
56086921
GL
719 /*
720 * If a block is bad, we retry in the next block at the same
721 * offset - see common/env_nand.c::writeenv()
722 */
723 lseek (fd, blockstart + block_seek, SEEK_SET);
6aff3115 724
56086921
GL
725 rc = read (fd, buf + processed, readlen);
726 if (rc != readlen) {
727 fprintf (stderr, "Read error on %s: %s\n",
728 DEVNAME (dev), strerror (errno));
729 return -1;
6aff3115 730 }
56086921 731#ifdef DEBUG
74620e1c
JH
732 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
733 rc, blockstart + block_seek, DEVNAME(dev));
56086921
GL
734#endif
735 processed += readlen;
736 readlen = min (blocklen, count - processed);
737 block_seek = 0;
738 blockstart += blocklen;
739 }
740
741 return processed;
742}
743
744/*
9eeaa8e6
RB
745 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
746 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
747 * erase and write the whole data at once.
56086921
GL
748 */
749static int flash_write_buf (int dev, int fd, void *buf, size_t count,
750 off_t offset, uint8_t mtd_type)
751{
752 void *data;
753 struct erase_info_user erase;
754 size_t blocklen; /* length of NAND block / NOR erase sector */
755 size_t erase_len; /* whole area that can be erased - may include
756 bad blocks */
757 size_t erasesize; /* erase / write length - one block on NAND,
758 whole area on NOR */
759 size_t processed = 0; /* progress counter */
9eeaa8e6 760 size_t write_total; /* total size to actually write - excluding
56086921
GL
761 bad blocks */
762 off_t erase_offset; /* offset to the first erase block (aligned)
763 below offset */
764 off_t block_seek; /* offset inside the erase block to the start
765 of the data */
766 off_t top_of_range; /* end of the last block we may use */
767 loff_t blockstart; /* running start of the current block -
768 MEMGETBADBLOCK needs 64 bits */
769 int rc;
770
e387efbd
OM
771 /*
772 * For mtd devices only offset and size of the environment do matter
773 */
774 if (mtd_type == MTD_ABSENT) {
775 blocklen = count;
776 top_of_range = offset + count;
777 erase_len = blocklen;
778 blockstart = offset;
779 block_seek = 0;
780 write_total = blocklen;
781 } else {
782 blocklen = DEVESIZE(dev);
56086921 783
e387efbd
OM
784 top_of_range = ((DEVOFFSET(dev) / blocklen) +
785 ENVSECTORS(dev)) * blocklen;
6aff3115 786
e387efbd 787 erase_offset = (offset / blocklen) * blocklen;
6aff3115 788
e387efbd
OM
789 /* Maximum area we may use */
790 erase_len = top_of_range - erase_offset;
56086921 791
e387efbd
OM
792 blockstart = erase_offset;
793 /* Offset inside a block */
794 block_seek = offset - erase_offset;
56086921 795
e387efbd
OM
796 /*
797 * Data size we actually write: from the start of the block
798 * to the start of the data, then count bytes of data, and
799 * to the end of the block
800 */
801 write_total = ((block_seek + count + blocklen - 1) /
802 blocklen) * blocklen;
803 }
56086921
GL
804
805 /*
806 * Support data anywhere within erase sectors: read out the complete
807 * area to be erased, replace the environment image, write the whole
808 * block back again.
809 */
810 if (write_total > count) {
811 data = malloc (erase_len);
812 if (!data) {
d0fb80c3 813 fprintf (stderr,
8603b69b 814 "Cannot malloc %zu bytes: %s\n",
56086921
GL
815 erase_len, strerror (errno));
816 return -1;
d0fb80c3 817 }
56086921
GL
818
819 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
820 mtd_type);
821 if (write_total != rc)
822 return -1;
823
74620e1c
JH
824#ifdef DEBUG
825 fprintf(stderr, "Preserving data ");
826 if (block_seek != 0)
827 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
828 if (block_seek + count != write_total) {
829 if (block_seek != 0)
830 fprintf(stderr, " and ");
831 fprintf(stderr, "0x%lx - 0x%x",
832 block_seek + count, write_total - 1);
833 }
834 fprintf(stderr, "\n");
835#endif
56086921
GL
836 /* Overwrite the old environment */
837 memcpy (data + block_seek, buf, count);
838 } else {
839 /*
840 * We get here, iff offset is block-aligned and count is a
841 * multiple of blocklen - see write_total calculation above
842 */
843 data = buf;
844 }
845
846 if (mtd_type == MTD_NANDFLASH) {
847 /*
848 * NAND: calculate which blocks we are writing. We have
849 * to write one block at a time to skip bad blocks.
850 */
851 erasesize = blocklen;
852 } else {
853 erasesize = erase_len;
854 }
855
856 erase.length = erasesize;
857
9eeaa8e6 858 /* This only runs once on NOR flash and SPI-dataflash */
56086921
GL
859 while (processed < write_total) {
860 rc = flash_bad_block (fd, mtd_type, &blockstart);
861 if (rc < 0) /* block test failed */
862 return rc;
863
864 if (blockstart + erasesize > top_of_range) {
865 fprintf (stderr, "End of range reached, aborting\n");
866 return -1;
6aff3115 867 }
56086921
GL
868
869 if (rc) { /* block is bad */
870 blockstart += blocklen;
871 continue;
872 }
873
e387efbd
OM
874 if (mtd_type != MTD_ABSENT) {
875 erase.start = blockstart;
876 ioctl(fd, MEMUNLOCK, &erase);
877 /* These do not need an explicit erase cycle */
878 if (mtd_type != MTD_DATAFLASH)
879 if (ioctl(fd, MEMERASE, &erase) != 0) {
880 fprintf(stderr,
881 "MTD erase error on %s: %s\n",
882 DEVNAME(dev), strerror(errno));
883 return -1;
884 }
885 }
56086921
GL
886
887 if (lseek (fd, blockstart, SEEK_SET) == -1) {
6aff3115 888 fprintf (stderr,
56086921
GL
889 "Seek error on %s: %s\n",
890 DEVNAME (dev), strerror (errno));
891 return -1;
6aff3115 892 }
56086921
GL
893
894#ifdef DEBUG
74620e1c
JH
895 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
896 blockstart);
56086921
GL
897#endif
898 if (write (fd, data + processed, erasesize) != erasesize) {
899 fprintf (stderr, "Write error on %s: %s\n",
900 DEVNAME (dev), strerror (errno));
901 return -1;
6aff3115 902 }
56086921 903
e387efbd
OM
904 if (mtd_type != MTD_ABSENT)
905 ioctl(fd, MEMLOCK, &erase);
56086921 906
4b774ff1 907 processed += erasesize;
56086921 908 block_seek = 0;
4b774ff1 909 blockstart += erasesize;
56086921
GL
910 }
911
912 if (write_total > count)
913 free (data);
914
915 return processed;
916}
917
918/*
919 * Set obsolete flag at offset - NOR flash only
920 */
921static int flash_flag_obsolete (int dev, int fd, off_t offset)
922{
923 int rc;
0aef7bc7 924 struct erase_info_user erase;
56086921 925
0aef7bc7
DZ
926 erase.start = DEVOFFSET (dev);
927 erase.length = DEVESIZE (dev);
56086921
GL
928 /* This relies on the fact, that obsolete_flag == 0 */
929 rc = lseek (fd, offset, SEEK_SET);
930 if (rc < 0) {
931 fprintf (stderr, "Cannot seek to set the flag on %s \n",
932 DEVNAME (dev));
933 return rc;
934 }
0aef7bc7 935 ioctl (fd, MEMUNLOCK, &erase);
56086921 936 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
0aef7bc7 937 ioctl (fd, MEMLOCK, &erase);
56086921
GL
938 if (rc < 0)
939 perror ("Could not set obsolete flag");
940
941 return rc;
942}
943
a8a752c0
MV
944/* Encrypt or decrypt the environment before writing or reading it. */
945static int env_aes_cbc_crypt(char *payload, const int enc)
946{
947 uint8_t *data = (uint8_t *)payload;
948 const int len = getenvsize();
949 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
950 uint32_t aes_blocks;
951
952 /* First we expand the key. */
371ee137 953 aes_expand_key(common_args.aes_key, key_exp);
a8a752c0
MV
954
955 /* Calculate the number of AES blocks to encrypt. */
956 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
957
958 if (enc)
959 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
960 else
961 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
962
963 return 0;
964}
965
56086921
GL
966static int flash_write (int fd_current, int fd_target, int dev_target)
967{
968 int rc;
969
970 switch (environment.flag_scheme) {
971 case FLAG_NONE:
972 break;
973 case FLAG_INCREMENTAL:
974 (*environment.flags)++;
975 break;
976 case FLAG_BOOLEAN:
977 *environment.flags = active_flag;
978 break;
979 default:
980 fprintf (stderr, "Unimplemented flash scheme %u \n",
981 environment.flag_scheme);
982 return -1;
983 }
984
985#ifdef DEBUG
74620e1c 986 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
56086921
GL
987 DEVOFFSET (dev_target), DEVNAME (dev_target));
988#endif
a8a752c0 989
497f2053
JH
990 rc = flash_write_buf(dev_target, fd_target, environment.image,
991 CUR_ENVSIZE, DEVOFFSET(dev_target),
56086921
GL
992 DEVTYPE(dev_target));
993 if (rc < 0)
994 return rc;
995
996 if (environment.flag_scheme == FLAG_BOOLEAN) {
997 /* Have to set obsolete flag */
998 off_t offset = DEVOFFSET (dev_current) +
999 offsetof (struct env_image_redundant, flags);
1000#ifdef DEBUG
74620e1c
JH
1001 fprintf(stderr,
1002 "Setting obsolete flag in environment at 0x%lx on %s\n",
56086921
GL
1003 DEVOFFSET (dev_current), DEVNAME (dev_current));
1004#endif
1005 flash_flag_obsolete (dev_current, fd_current, offset);
1006 }
1007
1008 return 0;
1009}
1010
1011static int flash_read (int fd)
1012{
1013 struct mtd_info_user mtdinfo;
f1932b78 1014 struct stat st;
56086921
GL
1015 int rc;
1016
f1932b78 1017 rc = fstat(fd, &st);
56086921 1018 if (rc < 0) {
f1932b78
LR
1019 fprintf(stderr, "Cannot stat the file %s\n",
1020 DEVNAME(dev_current));
56086921
GL
1021 return -1;
1022 }
1023
f1932b78
LR
1024 if (S_ISCHR(st.st_mode)) {
1025 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1026 if (rc < 0) {
1027 fprintf(stderr, "Cannot get MTD information for %s\n",
1028 DEVNAME(dev_current));
1029 return -1;
1030 }
1031 if (mtdinfo.type != MTD_NORFLASH &&
1032 mtdinfo.type != MTD_NANDFLASH &&
2b74433f
JH
1033 mtdinfo.type != MTD_DATAFLASH &&
1034 mtdinfo.type != MTD_UBIVOLUME) {
f1932b78
LR
1035 fprintf (stderr, "Unsupported flash type %u on %s\n",
1036 mtdinfo.type, DEVNAME(dev_current));
1037 return -1;
1038 }
1039 } else {
1040 memset(&mtdinfo, 0, sizeof(mtdinfo));
1041 mtdinfo.type = MTD_ABSENT;
56086921
GL
1042 }
1043
1044 DEVTYPE(dev_current) = mtdinfo.type;
1045
497f2053 1046 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
56086921 1047 DEVOFFSET (dev_current), mtdinfo.type);
a8a752c0
MV
1048 if (rc != CUR_ENVSIZE)
1049 return -1;
56086921 1050
a8a752c0 1051 return 0;
56086921
GL
1052}
1053
1054static int flash_io (int mode)
1055{
1056 int fd_current, fd_target, rc, dev_target;
1057
1058 /* dev_current: fd_current, erase_current */
1059 fd_current = open (DEVNAME (dev_current), mode);
1060 if (fd_current < 0) {
1061 fprintf (stderr,
1062 "Can't open %s: %s\n",
1063 DEVNAME (dev_current), strerror (errno));
1064 return -1;
1065 }
1066
1067 if (mode == O_RDWR) {
d0fb80c3 1068 if (HaveRedundEnv) {
56086921
GL
1069 /* switch to next partition for writing */
1070 dev_target = !dev_current;
1071 /* dev_target: fd_target, erase_target */
1072 fd_target = open (DEVNAME (dev_target), mode);
1073 if (fd_target < 0) {
d0fb80c3 1074 fprintf (stderr,
56086921
GL
1075 "Can't open %s: %s\n",
1076 DEVNAME (dev_target),
1077 strerror (errno));
1078 rc = -1;
1079 goto exit;
d0fb80c3 1080 }
56086921
GL
1081 } else {
1082 dev_target = dev_current;
1083 fd_target = fd_current;
6aff3115 1084 }
56086921
GL
1085
1086 rc = flash_write (fd_current, fd_target, dev_target);
1087
d0fb80c3 1088 if (HaveRedundEnv) {
56086921 1089 if (close (fd_target)) {
d0fb80c3 1090 fprintf (stderr,
4a6fd34b 1091 "I/O error on %s: %s\n",
56086921 1092 DEVNAME (dev_target),
4a6fd34b 1093 strerror (errno));
56086921 1094 rc = -1;
d0fb80c3 1095 }
6aff3115 1096 }
6aff3115 1097 } else {
56086921 1098 rc = flash_read (fd_current);
6aff3115
WD
1099 }
1100
56086921
GL
1101exit:
1102 if (close (fd_current)) {
6aff3115 1103 fprintf (stderr,
56086921
GL
1104 "I/O error on %s: %s\n",
1105 DEVNAME (dev_current), strerror (errno));
1106 return -1;
6aff3115
WD
1107 }
1108
56086921 1109 return rc;
6aff3115
WD
1110}
1111
1112/*
1113 * s1 is either a simple 'name', or a 'name=value' pair.
1114 * s2 is a 'name=value' pair.
1115 * If the names match, return the value of s2, else NULL.
1116 */
1117
6de66b35 1118static char *envmatch (char * s1, char * s2)
6aff3115 1119{
586197df
JH
1120 if (s1 == NULL || s2 == NULL)
1121 return NULL;
6aff3115
WD
1122
1123 while (*s1 == *s2++)
1124 if (*s1++ == '=')
56086921 1125 return s2;
4a6fd34b 1126 if (*s1 == '\0' && *(s2 - 1) == '=')
56086921
GL
1127 return s2;
1128 return NULL;
6aff3115
WD
1129}
1130
1131/*
1132 * Prevent confusion if running from erased flash memory
1133 */
bd7b26f8 1134int fw_env_open(void)
6aff3115 1135{
56086921 1136 int crc0, crc0_ok;
735eb0f0 1137 unsigned char flag0;
56086921
GL
1138 void *addr0;
1139
6aff3115 1140 int crc1, crc1_ok;
735eb0f0 1141 unsigned char flag1;
56086921 1142 void *addr1;
d0fb80c3 1143
a8a752c0
MV
1144 int ret;
1145
56086921
GL
1146 struct env_image_single *single;
1147 struct env_image_redundant *redundant;
6aff3115 1148
4a6fd34b 1149 if (parse_config ()) /* should fill envdevices */
56086921 1150 return -1;
4a6fd34b 1151
497f2053 1152 addr0 = calloc(1, CUR_ENVSIZE);
56086921 1153 if (addr0 == NULL) {
497f2053 1154 fprintf(stderr,
4a6fd34b 1155 "Not enough memory for environment (%ld bytes)\n",
497f2053 1156 CUR_ENVSIZE);
56086921 1157 return -1;
d0fb80c3 1158 }
4a6fd34b 1159
d0fb80c3 1160 /* read environment from FLASH to local buffer */
56086921
GL
1161 environment.image = addr0;
1162
1163 if (HaveRedundEnv) {
1164 redundant = addr0;
1165 environment.crc = &redundant->crc;
1166 environment.flags = &redundant->flags;
1167 environment.data = redundant->data;
1168 } else {
1169 single = addr0;
1170 environment.crc = &single->crc;
1171 environment.flags = NULL;
1172 environment.data = single->data;
d0fb80c3 1173 }
4a6fd34b 1174
56086921
GL
1175 dev_current = 0;
1176 if (flash_io (O_RDONLY))
1177 return -1;
1178
1179 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
a8a752c0 1180
371ee137 1181 if (common_args.aes_flag) {
a8a752c0
MV
1182 ret = env_aes_cbc_crypt(environment.data, 0);
1183 if (ret)
1184 return ret;
1185 }
1186
56086921 1187 crc0_ok = (crc0 == *environment.crc);
d0fb80c3 1188 if (!HaveRedundEnv) {
56086921 1189 if (!crc0_ok) {
4a6fd34b
WD
1190 fprintf (stderr,
1191 "Warning: Bad CRC, using default environment\n");
f07217c9 1192 memcpy(environment.data, default_environment, sizeof default_environment);
6aff3115 1193 }
d0fb80c3 1194 } else {
56086921 1195 flag0 = *environment.flags;
4a6fd34b 1196
56086921 1197 dev_current = 1;
497f2053 1198 addr1 = calloc(1, CUR_ENVSIZE);
56086921 1199 if (addr1 == NULL) {
497f2053 1200 fprintf(stderr,
4a6fd34b 1201 "Not enough memory for environment (%ld bytes)\n",
497f2053 1202 CUR_ENVSIZE);
56086921 1203 return -1;
4a6fd34b 1204 }
56086921 1205 redundant = addr1;
4a6fd34b 1206
56086921
GL
1207 /*
1208 * have to set environment.image for flash_read(), careful -
1209 * other pointers in environment still point inside addr0
1210 */
1211 environment.image = addr1;
1212 if (flash_io (O_RDONLY))
1213 return -1;
1214
1215 /* Check flag scheme compatibility */
1216 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1217 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1218 environment.flag_scheme = FLAG_BOOLEAN;
1219 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1220 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1221 environment.flag_scheme = FLAG_INCREMENTAL;
9eeaa8e6
RB
1222 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1223 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1224 environment.flag_scheme = FLAG_BOOLEAN;
785881f7
JH
1225 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1226 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1227 environment.flag_scheme = FLAG_INCREMENTAL;
23ef62d7
OM
1228 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1229 DEVTYPE(!dev_current) == MTD_ABSENT) {
1230 environment.flag_scheme = FLAG_INCREMENTAL;
56086921
GL
1231 } else {
1232 fprintf (stderr, "Incompatible flash types!\n");
1233 return -1;
6aff3115 1234 }
4a6fd34b 1235
56086921 1236 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
a8a752c0 1237
371ee137 1238 if (common_args.aes_flag) {
a8a752c0
MV
1239 ret = env_aes_cbc_crypt(redundant->data, 0);
1240 if (ret)
1241 return ret;
1242 }
1243
56086921
GL
1244 crc1_ok = (crc1 == redundant->crc);
1245 flag1 = redundant->flags;
1246
1247 if (crc0_ok && !crc1_ok) {
1248 dev_current = 0;
1249 } else if (!crc0_ok && crc1_ok) {
1250 dev_current = 1;
1251 } else if (!crc0_ok && !crc1_ok) {
4a6fd34b
WD
1252 fprintf (stderr,
1253 "Warning: Bad CRC, using default environment\n");
56086921
GL
1254 memcpy (environment.data, default_environment,
1255 sizeof default_environment);
1256 dev_current = 0;
1257 } else {
1258 switch (environment.flag_scheme) {
1259 case FLAG_BOOLEAN:
1260 if (flag0 == active_flag &&
1261 flag1 == obsolete_flag) {
1262 dev_current = 0;
1263 } else if (flag0 == obsolete_flag &&
1264 flag1 == active_flag) {
1265 dev_current = 1;
1266 } else if (flag0 == flag1) {
1267 dev_current = 0;
1268 } else if (flag0 == 0xFF) {
1269 dev_current = 0;
1270 } else if (flag1 == 0xFF) {
1271 dev_current = 1;
1272 } else {
1273 dev_current = 0;
1274 }
1275 break;
1276 case FLAG_INCREMENTAL:
735eb0f0 1277 if (flag0 == 255 && flag1 == 0)
56086921
GL
1278 dev_current = 1;
1279 else if ((flag1 == 255 && flag0 == 0) ||
735eb0f0 1280 flag0 >= flag1)
56086921 1281 dev_current = 0;
735eb0f0
JP
1282 else /* flag1 > flag0 */
1283 dev_current = 1;
56086921
GL
1284 break;
1285 default:
1286 fprintf (stderr, "Unknown flag scheme %u \n",
1287 environment.flag_scheme);
1288 return -1;
1289 }
1290 }
1291
1292 /*
1293 * If we are reading, we don't need the flag and the CRC any
1294 * more, if we are writing, we will re-calculate CRC and update
1295 * flags before writing out
1296 */
1297 if (dev_current) {
1298 environment.image = addr1;
1299 environment.crc = &redundant->crc;
1300 environment.flags = &redundant->flags;
1301 environment.data = redundant->data;
1302 free (addr0);
1303 } else {
1304 environment.image = addr0;
1305 /* Other pointers are already set */
4a6fd34b 1306 free (addr1);
6aff3115 1307 }
74620e1c
JH
1308#ifdef DEBUG
1309 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1310#endif
6aff3115 1311 }
56086921 1312 return 0;
6aff3115
WD
1313}
1314
1315
4a6fd34b 1316static int parse_config ()
6aff3115
WD
1317{
1318 struct stat st;
1319
d0fb80c3
WD
1320#if defined(CONFIG_FILE)
1321 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
371ee137
AF
1322 if (get_config(common_args.config_file)) {
1323 fprintf(stderr, "Cannot parse config file '%s': %m\n",
1324 common_args.config_file);
56086921 1325 return -1;
6aff3115 1326 }
d0fb80c3 1327#else
229695fe 1328 DEVNAME (0) = DEVICE1_NAME;
4a6fd34b
WD
1329 DEVOFFSET (0) = DEVICE1_OFFSET;
1330 ENVSIZE (0) = ENV1_SIZE;
23869bf8 1331 /* Default values are: erase-size=env-size */
9eeaa8e6 1332 DEVESIZE (0) = ENVSIZE (0);
23869bf8
DB
1333 /* #sectors=env-size/erase-size (rounded up) */
1334 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
9eeaa8e6 1335#ifdef DEVICE1_ESIZE
4a6fd34b 1336 DEVESIZE (0) = DEVICE1_ESIZE;
9eeaa8e6
RB
1337#endif
1338#ifdef DEVICE1_ENVSECTORS
56086921 1339 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
9eeaa8e6
RB
1340#endif
1341
6aff3115 1342#ifdef HAVE_REDUND
229695fe 1343 DEVNAME (1) = DEVICE2_NAME;
4a6fd34b
WD
1344 DEVOFFSET (1) = DEVICE2_OFFSET;
1345 ENVSIZE (1) = ENV2_SIZE;
23869bf8 1346 /* Default values are: erase-size=env-size */
9eeaa8e6 1347 DEVESIZE (1) = ENVSIZE (1);
23869bf8
DB
1348 /* #sectors=env-size/erase-size (rounded up) */
1349 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
9eeaa8e6 1350#ifdef DEVICE2_ESIZE
4a6fd34b 1351 DEVESIZE (1) = DEVICE2_ESIZE;
9eeaa8e6
RB
1352#endif
1353#ifdef DEVICE2_ENVSECTORS
56086921 1354 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
9eeaa8e6 1355#endif
d0fb80c3 1356 HaveRedundEnv = 1;
6aff3115 1357#endif
d0fb80c3 1358#endif
4a6fd34b
WD
1359 if (stat (DEVNAME (0), &st)) {
1360 fprintf (stderr,
1361 "Cannot access MTD device %s: %s\n",
1362 DEVNAME (0), strerror (errno));
56086921 1363 return -1;
d0fb80c3 1364 }
4a6fd34b
WD
1365
1366 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1367 fprintf (stderr,
1368 "Cannot access MTD device %s: %s\n",
e2146b6a 1369 DEVNAME (1), strerror (errno));
56086921 1370 return -1;
d0fb80c3 1371 }
6aff3115
WD
1372 return 0;
1373}
d0fb80c3
WD
1374
1375#if defined(CONFIG_FILE)
1376static int get_config (char *fname)
1377{
1378 FILE *fp;
1379 int i = 0;
1380 int rc;
1381 char dump[128];
229695fe 1382 char *devname;
d0fb80c3 1383
56086921
GL
1384 fp = fopen (fname, "r");
1385 if (fp == NULL)
1386 return -1;
d0fb80c3 1387
56086921 1388 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
d0fb80c3 1389 /* Skip incomplete conversions and comment strings */
56086921 1390 if (dump[0] == '#')
d0fb80c3 1391 continue;
56086921 1392
229695fe
TR
1393 rc = sscanf (dump, "%ms %lx %lx %lx %lx",
1394 &devname,
56086921
GL
1395 &DEVOFFSET (i),
1396 &ENVSIZE (i),
1397 &DEVESIZE (i),
1398 &ENVSECTORS (i));
1399
9eeaa8e6 1400 if (rc < 3)
56086921
GL
1401 continue;
1402
229695fe
TR
1403 DEVNAME(i) = devname;
1404
9eeaa8e6
RB
1405 if (rc < 4)
1406 /* Assume the erase size is the same as the env-size */
1407 DEVESIZE(i) = ENVSIZE(i);
1408
56086921 1409 if (rc < 5)
23869bf8
DB
1410 /* Assume enough env sectors to cover the environment */
1411 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
d0fb80c3
WD
1412
1413 i++;
1414 }
4a6fd34b
WD
1415 fclose (fp);
1416
d0fb80c3 1417 HaveRedundEnv = i - 1;
4a6fd34b 1418 if (!i) { /* No valid entries found */
d0fb80c3 1419 errno = EINVAL;
56086921 1420 return -1;
d0fb80c3
WD
1421 } else
1422 return 0;
1423}
1424#endif