2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <linux/stringify.h>
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
41 # include <linux/mtd/mtd.h>
43 # define __user /* nothing */
44 # include <mtd/mtd-user.h>
49 #define WHITESPACE(c) ((c == '\t') || (c == ' '))
51 #define min(x, y) ({ \
52 typeof(x) _min1 = (x); \
53 typeof(y) _min2 = (y); \
54 (void) (&_min1 == &_min2); \
55 _min1 < _min2 ? _min1 : _min2; })
58 char devname[16]; /* Device name */
59 ulong devoff; /* Device offset */
60 ulong env_size; /* environment size */
61 ulong erase_size; /* device erase size */
62 ulong env_sectors; /* number of environment sectors */
63 uint8_t mtd_type; /* type of the MTD device */
66 static struct envdev_s envdevices[2] =
69 .mtd_type = MTD_ABSENT,
71 .mtd_type = MTD_ABSENT,
74 static int dev_current;
76 #define DEVNAME(i) envdevices[(i)].devname
77 #define DEVOFFSET(i) envdevices[(i)].devoff
78 #define ENVSIZE(i) envdevices[(i)].env_size
79 #define DEVESIZE(i) envdevices[(i)].erase_size
80 #define ENVSECTORS(i) envdevices[(i)].env_sectors
81 #define DEVTYPE(i) envdevices[(i)].mtd_type
83 #define CUR_ENVSIZE ENVSIZE(dev_current)
85 #define ENV_SIZE getenvsize()
87 struct env_image_single {
88 uint32_t crc; /* CRC32 over data bytes */
92 struct env_image_redundant {
93 uint32_t crc; /* CRC32 over data bytes */
94 unsigned char flags; /* active or obsolete */
107 unsigned char *flags;
109 enum flag_scheme flag_scheme;
112 static struct environment environment = {
113 .flag_scheme = FLAG_NONE,
116 static int HaveRedundEnv = 0;
118 static unsigned char active_flag = 1;
119 /* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
120 static unsigned char obsolete_flag = 0;
122 #define DEFAULT_ENV_INSTANCE_STATIC
123 #include <env_default.h>
125 static int flash_io (int mode);
126 static char *envmatch (char * s1, char * s2);
127 static int parse_config (void);
129 #if defined(CONFIG_FILE)
130 static int get_config (char *);
132 static inline ulong getenvsize (void)
134 ulong rc = CUR_ENVSIZE - sizeof(long);
141 static char *fw_string_blank(char *s, int noblank)
146 for (i = 0; i < len; i++, s++) {
147 if ((noblank && !WHITESPACE(*s)) ||
148 (!noblank && WHITESPACE(*s)))
158 * Search the environment for a variable.
159 * Return the value, if found, or NULL, if not found.
161 char *fw_getenv (char *name)
165 for (env = environment.data; *env; env = nxt + 1) {
168 for (nxt = env; *nxt; ++nxt) {
169 if (nxt >= &environment.data[ENV_SIZE]) {
170 fprintf (stderr, "## Error: "
171 "environment not terminated\n");
175 val = envmatch (name, env);
184 * Print the current definition of one, or more, or all
185 * environment variables
187 int fw_printenv (int argc, char *argv[])
196 if (argc == 1) { /* Print all env variables */
197 for (env = environment.data; *env; env = nxt + 1) {
198 for (nxt = env; *nxt; ++nxt) {
199 if (nxt >= &environment.data[ENV_SIZE]) {
200 fprintf (stderr, "## Error: "
201 "environment not terminated\n");
206 printf ("%s\n", env);
211 if (strcmp (argv[1], "-n") == 0) {
216 fprintf (stderr, "## Error: "
217 "`-n' option requires exactly one argument\n");
224 for (i = 1; i < argc; ++i) { /* print single env variables */
225 char *name = argv[i];
228 for (env = environment.data; *env; env = nxt + 1) {
230 for (nxt = env; *nxt; ++nxt) {
231 if (nxt >= &environment.data[ENV_SIZE]) {
232 fprintf (stderr, "## Error: "
233 "environment not terminated\n");
237 val = envmatch (name, env);
240 fputs (name, stdout);
248 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
256 int fw_env_close(void)
261 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
263 /* write environment back to flash */
264 if (flash_io(O_RDWR)) {
266 "Error: can't write fw_env to flash\n");
275 * Set/Clear a single variable in the environment.
276 * This is called in sequence to update the environment
277 * in RAM without updating the copy in flash after each set
279 int fw_env_write(char *name, char *value)
286 * search if variable with this name already exists
288 for (nxt = env = environment.data; *env; env = nxt + 1) {
289 for (nxt = env; *nxt; ++nxt) {
290 if (nxt >= &environment.data[ENV_SIZE]) {
291 fprintf(stderr, "## Error: "
292 "environment not terminated\n");
297 if ((oldval = envmatch (name, env)) != NULL)
302 * Delete any existing definition
305 #ifndef CONFIG_ENV_OVERWRITE
307 * Ethernet Address and serial# can be set only once
310 (strcmp(name, "serial#") == 0) ||
311 ((strcmp(name, "ethaddr") == 0)
312 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
313 && (strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0)
314 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
316 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
320 #endif /* CONFIG_ENV_OVERWRITE */
322 if (*++nxt == '\0') {
327 if ((*env == '\0') && (*nxt == '\0'))
336 if (!value || !strlen(value))
340 * Append new definition at the end
342 for (env = environment.data; *env || *(env + 1); ++env);
343 if (env > environment.data)
347 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
349 len = strlen (name) + 2;
350 /* add '=' for first arg, ' ' for all others */
351 len += strlen(value) + 1;
353 if (len > (&environment.data[ENV_SIZE] - env)) {
355 "Error: environment overflow, \"%s\" deleted\n",
360 while ((*env = *name++) != '\0')
363 while ((*++env = *value++) != '\0')
366 /* end is marked with double '\0' */
373 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
375 * EINVAL - need at least 1 argument
376 * EROFS - certain variables ("ethaddr", "serial#") cannot be
377 * modified or deleted
380 int fw_setenv(int argc, char *argv[])
392 fprintf(stderr, "Error: environment not initialized\n");
399 for (i = 2; i < argc; ++i) {
401 size_t val_len = strlen(val);
404 value[len - 1] = ' ';
405 value = realloc(value, len + val_len + 1);
408 "Cannot malloc %zu bytes: %s\n",
409 len, strerror(errno));
413 memcpy(value + len, val, val_len);
418 fw_env_write(name, value);
422 return fw_env_close();
426 * Parse a file and configure the u-boot variables.
427 * The script file has a very simple format, as follows:
429 * Each line has a couple with name, value:
430 * <white spaces>variable_name<white spaces>variable_value
432 * Both variable_name and variable_value are interpreted as strings.
433 * Any character after <white spaces> and before ending \r\n is interpreted
434 * as variable's value (no comment allowed on these lines !)
436 * Comments are allowed if the first character in the line is #
438 * Returns -1 and sets errno error codes:
442 int fw_parse_script(char *fname)
445 char dump[1024]; /* Maximum line length in the file */
453 fprintf(stderr, "Error: environment not initialized\n");
457 if (strcmp(fname, "-") == 0)
460 fp = fopen(fname, "r");
462 fprintf(stderr, "I cannot open %s for reading\n",
468 while (fgets(dump, sizeof(dump), fp)) {
473 * Read a whole line from the file. If the line is too long
474 * or is not terminated, reports an error and exit.
476 if (dump[len - 1] != '\n') {
478 "Line %d not corrected terminated or too long\n",
484 /* Drop ending line feed / carriage return */
485 while (len > 0 && (dump[len - 1] == '\n' ||
486 dump[len - 1] == '\r')) {
487 dump[len - 1] = '\0';
491 /* Skip comment or empty lines */
492 if ((len == 0) || dump[0] == '#')
496 * Search for variable's name,
497 * remove leading whitespaces
499 name = fw_string_blank(dump, 1);
503 /* The first white space is the end of variable name */
504 val = fw_string_blank(name, 0);
508 if ((val - name) < len)
509 val = fw_string_blank(val, 1);
515 fprintf(stderr, "Setting %s : %s\n",
516 name, val ? val : " removed");
520 * If there is an error setting a variable,
521 * try to save the environment and returns an error
523 if (fw_env_write(name, val)) {
525 "fw_env_write returns with error : %s\n",
533 /* Close file if not stdin */
534 if (strcmp(fname, "-") != 0)
537 ret |= fw_env_close();
544 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
547 * < 0 - failed to test
549 static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
551 if (mtd_type == MTD_NANDFLASH) {
552 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
555 perror ("Cannot read bad block mark");
561 fprintf (stderr, "Bad block at 0x%llx, "
562 "skipping\n", *blockstart);
572 * Read data from flash at an offset into a provided buffer. On NAND it skips
573 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
574 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
576 static int flash_read_buf (int dev, int fd, void *buf, size_t count,
577 off_t offset, uint8_t mtd_type)
579 size_t blocklen; /* erase / write length - one block on NAND,
581 size_t processed = 0; /* progress counter */
582 size_t readlen = count; /* current read length */
583 off_t top_of_range; /* end of the last block we may use */
584 off_t block_seek; /* offset inside the current block to the start
586 loff_t blockstart; /* running start of the current block -
587 MEMGETBADBLOCK needs 64 bits */
590 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
592 /* Offset inside a block */
593 block_seek = offset - blockstart;
595 if (mtd_type == MTD_NANDFLASH) {
597 * NAND: calculate which blocks we are reading. We have
598 * to read one block at a time to skip bad blocks.
600 blocklen = DEVESIZE (dev);
603 * To calculate the top of the range, we have to use the
604 * global DEVOFFSET (dev), which can be different from offset
606 top_of_range = ((DEVOFFSET(dev) / blocklen) +
607 ENVSECTORS (dev)) * blocklen;
609 /* Limit to one block for the first read */
610 if (readlen > blocklen - block_seek)
611 readlen = blocklen - block_seek;
614 top_of_range = offset + count;
617 /* This only runs once on NOR flash */
618 while (processed < count) {
619 rc = flash_bad_block (fd, mtd_type, &blockstart);
620 if (rc < 0) /* block test failed */
623 if (blockstart + block_seek + readlen > top_of_range) {
624 /* End of range is reached */
626 "Too few good blocks within range\n");
630 if (rc) { /* block is bad */
631 blockstart += blocklen;
636 * If a block is bad, we retry in the next block at the same
637 * offset - see common/env_nand.c::writeenv()
639 lseek (fd, blockstart + block_seek, SEEK_SET);
641 rc = read (fd, buf + processed, readlen);
643 fprintf (stderr, "Read error on %s: %s\n",
644 DEVNAME (dev), strerror (errno));
648 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
649 rc, blockstart + block_seek, DEVNAME(dev));
651 processed += readlen;
652 readlen = min (blocklen, count - processed);
654 blockstart += blocklen;
661 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
662 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
663 * erase and write the whole data at once.
665 static int flash_write_buf (int dev, int fd, void *buf, size_t count,
666 off_t offset, uint8_t mtd_type)
669 struct erase_info_user erase;
670 size_t blocklen; /* length of NAND block / NOR erase sector */
671 size_t erase_len; /* whole area that can be erased - may include
673 size_t erasesize; /* erase / write length - one block on NAND,
675 size_t processed = 0; /* progress counter */
676 size_t write_total; /* total size to actually write - excluding
678 off_t erase_offset; /* offset to the first erase block (aligned)
680 off_t block_seek; /* offset inside the erase block to the start
682 off_t top_of_range; /* end of the last block we may use */
683 loff_t blockstart; /* running start of the current block -
684 MEMGETBADBLOCK needs 64 bits */
687 blocklen = DEVESIZE (dev);
689 top_of_range = ((DEVOFFSET(dev) / blocklen) +
690 ENVSECTORS (dev)) * blocklen;
692 erase_offset = (offset / blocklen) * blocklen;
694 /* Maximum area we may use */
695 erase_len = top_of_range - erase_offset;
697 blockstart = erase_offset;
698 /* Offset inside a block */
699 block_seek = offset - erase_offset;
702 * Data size we actually have to write: from the start of the block
703 * to the start of the data, then count bytes of data, and to the
706 write_total = ((block_seek + count + blocklen - 1) /
707 blocklen) * blocklen;
710 * Support data anywhere within erase sectors: read out the complete
711 * area to be erased, replace the environment image, write the whole
714 if (write_total > count) {
715 data = malloc (erase_len);
718 "Cannot malloc %zu bytes: %s\n",
719 erase_len, strerror (errno));
723 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
725 if (write_total != rc)
729 fprintf(stderr, "Preserving data ");
731 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
732 if (block_seek + count != write_total) {
734 fprintf(stderr, " and ");
735 fprintf(stderr, "0x%lx - 0x%x",
736 block_seek + count, write_total - 1);
738 fprintf(stderr, "\n");
740 /* Overwrite the old environment */
741 memcpy (data + block_seek, buf, count);
744 * We get here, iff offset is block-aligned and count is a
745 * multiple of blocklen - see write_total calculation above
750 if (mtd_type == MTD_NANDFLASH) {
752 * NAND: calculate which blocks we are writing. We have
753 * to write one block at a time to skip bad blocks.
755 erasesize = blocklen;
757 erasesize = erase_len;
760 erase.length = erasesize;
762 /* This only runs once on NOR flash and SPI-dataflash */
763 while (processed < write_total) {
764 rc = flash_bad_block (fd, mtd_type, &blockstart);
765 if (rc < 0) /* block test failed */
768 if (blockstart + erasesize > top_of_range) {
769 fprintf (stderr, "End of range reached, aborting\n");
773 if (rc) { /* block is bad */
774 blockstart += blocklen;
778 erase.start = blockstart;
779 ioctl (fd, MEMUNLOCK, &erase);
781 /* Dataflash does not need an explicit erase cycle */
782 if (mtd_type != MTD_DATAFLASH)
783 if (ioctl (fd, MEMERASE, &erase) != 0) {
784 fprintf (stderr, "MTD erase error on %s: %s\n",
790 if (lseek (fd, blockstart, SEEK_SET) == -1) {
792 "Seek error on %s: %s\n",
793 DEVNAME (dev), strerror (errno));
798 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
801 if (write (fd, data + processed, erasesize) != erasesize) {
802 fprintf (stderr, "Write error on %s: %s\n",
803 DEVNAME (dev), strerror (errno));
807 ioctl (fd, MEMLOCK, &erase);
809 processed += blocklen;
811 blockstart += blocklen;
814 if (write_total > count)
821 * Set obsolete flag at offset - NOR flash only
823 static int flash_flag_obsolete (int dev, int fd, off_t offset)
826 struct erase_info_user erase;
828 erase.start = DEVOFFSET (dev);
829 erase.length = DEVESIZE (dev);
830 /* This relies on the fact, that obsolete_flag == 0 */
831 rc = lseek (fd, offset, SEEK_SET);
833 fprintf (stderr, "Cannot seek to set the flag on %s \n",
837 ioctl (fd, MEMUNLOCK, &erase);
838 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
839 ioctl (fd, MEMLOCK, &erase);
841 perror ("Could not set obsolete flag");
846 static int flash_write (int fd_current, int fd_target, int dev_target)
850 switch (environment.flag_scheme) {
853 case FLAG_INCREMENTAL:
854 (*environment.flags)++;
857 *environment.flags = active_flag;
860 fprintf (stderr, "Unimplemented flash scheme %u \n",
861 environment.flag_scheme);
866 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
867 DEVOFFSET (dev_target), DEVNAME (dev_target));
869 rc = flash_write_buf(dev_target, fd_target, environment.image,
870 CUR_ENVSIZE, DEVOFFSET(dev_target),
871 DEVTYPE(dev_target));
875 if (environment.flag_scheme == FLAG_BOOLEAN) {
876 /* Have to set obsolete flag */
877 off_t offset = DEVOFFSET (dev_current) +
878 offsetof (struct env_image_redundant, flags);
881 "Setting obsolete flag in environment at 0x%lx on %s\n",
882 DEVOFFSET (dev_current), DEVNAME (dev_current));
884 flash_flag_obsolete (dev_current, fd_current, offset);
890 static int flash_read (int fd)
892 struct mtd_info_user mtdinfo;
895 rc = ioctl (fd, MEMGETINFO, &mtdinfo);
897 perror ("Cannot get MTD information");
901 if (mtdinfo.type != MTD_NORFLASH &&
902 mtdinfo.type != MTD_NANDFLASH &&
903 mtdinfo.type != MTD_DATAFLASH) {
904 fprintf (stderr, "Unsupported flash type %u\n", mtdinfo.type);
908 DEVTYPE(dev_current) = mtdinfo.type;
910 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
911 DEVOFFSET (dev_current), mtdinfo.type);
913 return (rc != CUR_ENVSIZE) ? -1 : 0;
916 static int flash_io (int mode)
918 int fd_current, fd_target, rc, dev_target;
920 /* dev_current: fd_current, erase_current */
921 fd_current = open (DEVNAME (dev_current), mode);
922 if (fd_current < 0) {
924 "Can't open %s: %s\n",
925 DEVNAME (dev_current), strerror (errno));
929 if (mode == O_RDWR) {
931 /* switch to next partition for writing */
932 dev_target = !dev_current;
933 /* dev_target: fd_target, erase_target */
934 fd_target = open (DEVNAME (dev_target), mode);
937 "Can't open %s: %s\n",
938 DEVNAME (dev_target),
944 dev_target = dev_current;
945 fd_target = fd_current;
948 rc = flash_write (fd_current, fd_target, dev_target);
951 if (close (fd_target)) {
953 "I/O error on %s: %s\n",
954 DEVNAME (dev_target),
960 rc = flash_read (fd_current);
964 if (close (fd_current)) {
966 "I/O error on %s: %s\n",
967 DEVNAME (dev_current), strerror (errno));
975 * s1 is either a simple 'name', or a 'name=value' pair.
976 * s2 is a 'name=value' pair.
977 * If the names match, return the value of s2, else NULL.
980 static char *envmatch (char * s1, char * s2)
982 if (s1 == NULL || s2 == NULL)
988 if (*s1 == '\0' && *(s2 - 1) == '=')
994 * Prevent confusion if running from erased flash memory
996 int fw_env_open(void)
1003 unsigned char flag1;
1006 struct env_image_single *single;
1007 struct env_image_redundant *redundant;
1009 if (parse_config ()) /* should fill envdevices */
1012 addr0 = calloc(1, CUR_ENVSIZE);
1013 if (addr0 == NULL) {
1015 "Not enough memory for environment (%ld bytes)\n",
1020 /* read environment from FLASH to local buffer */
1021 environment.image = addr0;
1023 if (HaveRedundEnv) {
1025 environment.crc = &redundant->crc;
1026 environment.flags = &redundant->flags;
1027 environment.data = redundant->data;
1030 environment.crc = &single->crc;
1031 environment.flags = NULL;
1032 environment.data = single->data;
1036 if (flash_io (O_RDONLY))
1039 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1040 crc0_ok = (crc0 == *environment.crc);
1041 if (!HaveRedundEnv) {
1044 "Warning: Bad CRC, using default environment\n");
1045 memcpy(environment.data, default_environment, sizeof default_environment);
1048 flag0 = *environment.flags;
1051 addr1 = calloc(1, CUR_ENVSIZE);
1052 if (addr1 == NULL) {
1054 "Not enough memory for environment (%ld bytes)\n",
1061 * have to set environment.image for flash_read(), careful -
1062 * other pointers in environment still point inside addr0
1064 environment.image = addr1;
1065 if (flash_io (O_RDONLY))
1068 /* Check flag scheme compatibility */
1069 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1070 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1071 environment.flag_scheme = FLAG_BOOLEAN;
1072 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1073 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1074 environment.flag_scheme = FLAG_INCREMENTAL;
1075 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1076 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1077 environment.flag_scheme = FLAG_BOOLEAN;
1079 fprintf (stderr, "Incompatible flash types!\n");
1083 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1084 crc1_ok = (crc1 == redundant->crc);
1085 flag1 = redundant->flags;
1087 if (crc0_ok && !crc1_ok) {
1089 } else if (!crc0_ok && crc1_ok) {
1091 } else if (!crc0_ok && !crc1_ok) {
1093 "Warning: Bad CRC, using default environment\n");
1094 memcpy (environment.data, default_environment,
1095 sizeof default_environment);
1098 switch (environment.flag_scheme) {
1100 if (flag0 == active_flag &&
1101 flag1 == obsolete_flag) {
1103 } else if (flag0 == obsolete_flag &&
1104 flag1 == active_flag) {
1106 } else if (flag0 == flag1) {
1108 } else if (flag0 == 0xFF) {
1110 } else if (flag1 == 0xFF) {
1116 case FLAG_INCREMENTAL:
1117 if (flag0 == 255 && flag1 == 0)
1119 else if ((flag1 == 255 && flag0 == 0) ||
1122 else /* flag1 > flag0 */
1126 fprintf (stderr, "Unknown flag scheme %u \n",
1127 environment.flag_scheme);
1133 * If we are reading, we don't need the flag and the CRC any
1134 * more, if we are writing, we will re-calculate CRC and update
1135 * flags before writing out
1138 environment.image = addr1;
1139 environment.crc = &redundant->crc;
1140 environment.flags = &redundant->flags;
1141 environment.data = redundant->data;
1144 environment.image = addr0;
1145 /* Other pointers are already set */
1149 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1156 static int parse_config ()
1160 #if defined(CONFIG_FILE)
1161 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
1162 if (get_config (CONFIG_FILE)) {
1164 "Cannot parse config file: %s\n", strerror (errno));
1168 strcpy (DEVNAME (0), DEVICE1_NAME);
1169 DEVOFFSET (0) = DEVICE1_OFFSET;
1170 ENVSIZE (0) = ENV1_SIZE;
1171 /* Default values are: erase-size=env-size, #sectors=1 */
1172 DEVESIZE (0) = ENVSIZE (0);
1174 #ifdef DEVICE1_ESIZE
1175 DEVESIZE (0) = DEVICE1_ESIZE;
1177 #ifdef DEVICE1_ENVSECTORS
1178 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
1182 strcpy (DEVNAME (1), DEVICE2_NAME);
1183 DEVOFFSET (1) = DEVICE2_OFFSET;
1184 ENVSIZE (1) = ENV2_SIZE;
1185 /* Default values are: erase-size=env-size, #sectors=1 */
1186 DEVESIZE (1) = ENVSIZE (1);
1188 #ifdef DEVICE2_ESIZE
1189 DEVESIZE (1) = DEVICE2_ESIZE;
1191 #ifdef DEVICE2_ENVSECTORS
1192 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
1197 if (stat (DEVNAME (0), &st)) {
1199 "Cannot access MTD device %s: %s\n",
1200 DEVNAME (0), strerror (errno));
1204 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1206 "Cannot access MTD device %s: %s\n",
1207 DEVNAME (1), strerror (errno));
1213 #if defined(CONFIG_FILE)
1214 static int get_config (char *fname)
1221 fp = fopen (fname, "r");
1225 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
1226 /* Skip incomplete conversions and comment strings */
1230 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1241 /* Assume the erase size is the same as the env-size */
1242 DEVESIZE(i) = ENVSIZE(i);
1245 /* Default - 1 sector */
1252 HaveRedundEnv = i - 1;
1253 if (!i) { /* No valid entries found */