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,
28 #include <env_flags.h>
30 #include <linux/stringify.h>
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
42 # include <linux/mtd/mtd.h>
44 # define __user /* nothing */
45 # include <mtd/mtd-user.h>
50 #define WHITESPACE(c) ((c == '\t') || (c == ' '))
52 #define min(x, y) ({ \
53 typeof(x) _min1 = (x); \
54 typeof(y) _min2 = (y); \
55 (void) (&_min1 == &_min2); \
56 _min1 < _min2 ? _min1 : _min2; })
59 char devname[16]; /* Device name */
60 ulong devoff; /* Device offset */
61 ulong env_size; /* environment size */
62 ulong erase_size; /* device erase size */
63 ulong env_sectors; /* number of environment sectors */
64 uint8_t mtd_type; /* type of the MTD device */
67 static struct envdev_s envdevices[2] =
70 .mtd_type = MTD_ABSENT,
72 .mtd_type = MTD_ABSENT,
75 static int dev_current;
77 #define DEVNAME(i) envdevices[(i)].devname
78 #define DEVOFFSET(i) envdevices[(i)].devoff
79 #define ENVSIZE(i) envdevices[(i)].env_size
80 #define DEVESIZE(i) envdevices[(i)].erase_size
81 #define ENVSECTORS(i) envdevices[(i)].env_sectors
82 #define DEVTYPE(i) envdevices[(i)].mtd_type
84 #define CUR_ENVSIZE ENVSIZE(dev_current)
86 #define ENV_SIZE getenvsize()
88 struct env_image_single {
89 uint32_t crc; /* CRC32 over data bytes */
93 struct env_image_redundant {
94 uint32_t crc; /* CRC32 over data bytes */
95 unsigned char flags; /* active or obsolete */
108 unsigned char *flags;
110 enum flag_scheme flag_scheme;
113 static struct environment environment = {
114 .flag_scheme = FLAG_NONE,
117 static int HaveRedundEnv = 0;
119 static unsigned char active_flag = 1;
120 /* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
121 static unsigned char obsolete_flag = 0;
123 #define DEFAULT_ENV_INSTANCE_STATIC
124 #include <env_default.h>
126 static int flash_io (int mode);
127 static char *envmatch (char * s1, char * s2);
128 static int parse_config (void);
130 #if defined(CONFIG_FILE)
131 static int get_config (char *);
133 static inline ulong getenvsize (void)
135 ulong rc = CUR_ENVSIZE - sizeof(long);
142 static char *fw_string_blank(char *s, int noblank)
147 for (i = 0; i < len; i++, s++) {
148 if ((noblank && !WHITESPACE(*s)) ||
149 (!noblank && WHITESPACE(*s)))
159 * Search the environment for a variable.
160 * Return the value, if found, or NULL, if not found.
162 char *fw_getenv (char *name)
166 for (env = environment.data; *env; env = nxt + 1) {
169 for (nxt = env; *nxt; ++nxt) {
170 if (nxt >= &environment.data[ENV_SIZE]) {
171 fprintf (stderr, "## Error: "
172 "environment not terminated\n");
176 val = envmatch (name, env);
185 * Print the current definition of one, or more, or all
186 * environment variables
188 int fw_printenv (int argc, char *argv[])
197 if (argc == 1) { /* Print all env variables */
198 for (env = environment.data; *env; env = nxt + 1) {
199 for (nxt = env; *nxt; ++nxt) {
200 if (nxt >= &environment.data[ENV_SIZE]) {
201 fprintf (stderr, "## Error: "
202 "environment not terminated\n");
207 printf ("%s\n", env);
212 if (strcmp (argv[1], "-n") == 0) {
217 fprintf (stderr, "## Error: "
218 "`-n' option requires exactly one argument\n");
225 for (i = 1; i < argc; ++i) { /* print single env variables */
226 char *name = argv[i];
229 for (env = environment.data; *env; env = nxt + 1) {
231 for (nxt = env; *nxt; ++nxt) {
232 if (nxt >= &environment.data[ENV_SIZE]) {
233 fprintf (stderr, "## Error: "
234 "environment not terminated\n");
238 val = envmatch (name, env);
241 fputs (name, stdout);
249 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
257 int fw_env_close(void)
262 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
264 /* write environment back to flash */
265 if (flash_io(O_RDWR)) {
267 "Error: can't write fw_env to flash\n");
276 * Set/Clear a single variable in the environment.
277 * This is called in sequence to update the environment
278 * in RAM without updating the copy in flash after each set
280 int fw_env_write(char *name, char *value)
287 * search if variable with this name already exists
289 for (nxt = env = environment.data; *env; env = nxt + 1) {
290 for (nxt = env; *nxt; ++nxt) {
291 if (nxt >= &environment.data[ENV_SIZE]) {
292 fprintf(stderr, "## Error: "
293 "environment not terminated\n");
298 if ((oldval = envmatch (name, env)) != NULL)
303 * Delete any existing definition
306 #ifndef CONFIG_ENV_OVERWRITE
308 * Ethernet Address and serial# can be set only once
311 (strcmp(name, "serial#") == 0) ||
312 ((strcmp(name, "ethaddr") == 0)
313 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
314 && (strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0)
315 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
317 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
321 #endif /* CONFIG_ENV_OVERWRITE */
323 if (*++nxt == '\0') {
328 if ((*env == '\0') && (*nxt == '\0'))
337 if (!value || !strlen(value))
341 * Append new definition at the end
343 for (env = environment.data; *env || *(env + 1); ++env);
344 if (env > environment.data)
348 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
350 len = strlen (name) + 2;
351 /* add '=' for first arg, ' ' for all others */
352 len += strlen(value) + 1;
354 if (len > (&environment.data[ENV_SIZE] - env)) {
356 "Error: environment overflow, \"%s\" deleted\n",
361 while ((*env = *name++) != '\0')
364 while ((*++env = *value++) != '\0')
367 /* end is marked with double '\0' */
374 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
376 * EINVAL - need at least 1 argument
377 * EROFS - certain variables ("ethaddr", "serial#") cannot be
378 * modified or deleted
381 int fw_setenv(int argc, char *argv[])
393 fprintf(stderr, "Error: environment not initialized\n");
399 if (env_flags_validate_env_set_params(argc, argv) < 0)
403 for (i = 2; i < argc; ++i) {
405 size_t val_len = strlen(val);
408 value[len - 1] = ' ';
409 value = realloc(value, len + val_len + 1);
412 "Cannot malloc %zu bytes: %s\n",
413 len, strerror(errno));
417 memcpy(value + len, val, val_len);
422 fw_env_write(name, value);
426 return fw_env_close();
430 * Parse a file and configure the u-boot variables.
431 * The script file has a very simple format, as follows:
433 * Each line has a couple with name, value:
434 * <white spaces>variable_name<white spaces>variable_value
436 * Both variable_name and variable_value are interpreted as strings.
437 * Any character after <white spaces> and before ending \r\n is interpreted
438 * as variable's value (no comment allowed on these lines !)
440 * Comments are allowed if the first character in the line is #
442 * Returns -1 and sets errno error codes:
446 int fw_parse_script(char *fname)
449 char dump[1024]; /* Maximum line length in the file */
457 fprintf(stderr, "Error: environment not initialized\n");
461 if (strcmp(fname, "-") == 0)
464 fp = fopen(fname, "r");
466 fprintf(stderr, "I cannot open %s for reading\n",
472 while (fgets(dump, sizeof(dump), fp)) {
477 * Read a whole line from the file. If the line is too long
478 * or is not terminated, reports an error and exit.
480 if (dump[len - 1] != '\n') {
482 "Line %d not corrected terminated or too long\n",
488 /* Drop ending line feed / carriage return */
489 while (len > 0 && (dump[len - 1] == '\n' ||
490 dump[len - 1] == '\r')) {
491 dump[len - 1] = '\0';
495 /* Skip comment or empty lines */
496 if ((len == 0) || dump[0] == '#')
500 * Search for variable's name,
501 * remove leading whitespaces
503 name = fw_string_blank(dump, 1);
507 /* The first white space is the end of variable name */
508 val = fw_string_blank(name, 0);
512 if ((val - name) < len)
513 val = fw_string_blank(val, 1);
519 fprintf(stderr, "Setting %s : %s\n",
520 name, val ? val : " removed");
523 if (env_flags_validate_type(name, val) < 0) {
529 * If there is an error setting a variable,
530 * try to save the environment and returns an error
532 if (fw_env_write(name, val)) {
534 "fw_env_write returns with error : %s\n",
542 /* Close file if not stdin */
543 if (strcmp(fname, "-") != 0)
546 ret |= fw_env_close();
553 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
556 * < 0 - failed to test
558 static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
560 if (mtd_type == MTD_NANDFLASH) {
561 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
564 perror ("Cannot read bad block mark");
570 fprintf (stderr, "Bad block at 0x%llx, "
571 "skipping\n", *blockstart);
581 * Read data from flash at an offset into a provided buffer. On NAND it skips
582 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
583 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
585 static int flash_read_buf (int dev, int fd, void *buf, size_t count,
586 off_t offset, uint8_t mtd_type)
588 size_t blocklen; /* erase / write length - one block on NAND,
590 size_t processed = 0; /* progress counter */
591 size_t readlen = count; /* current read length */
592 off_t top_of_range; /* end of the last block we may use */
593 off_t block_seek; /* offset inside the current block to the start
595 loff_t blockstart; /* running start of the current block -
596 MEMGETBADBLOCK needs 64 bits */
599 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
601 /* Offset inside a block */
602 block_seek = offset - blockstart;
604 if (mtd_type == MTD_NANDFLASH) {
606 * NAND: calculate which blocks we are reading. We have
607 * to read one block at a time to skip bad blocks.
609 blocklen = DEVESIZE (dev);
612 * To calculate the top of the range, we have to use the
613 * global DEVOFFSET (dev), which can be different from offset
615 top_of_range = ((DEVOFFSET(dev) / blocklen) +
616 ENVSECTORS (dev)) * blocklen;
618 /* Limit to one block for the first read */
619 if (readlen > blocklen - block_seek)
620 readlen = blocklen - block_seek;
623 top_of_range = offset + count;
626 /* This only runs once on NOR flash */
627 while (processed < count) {
628 rc = flash_bad_block (fd, mtd_type, &blockstart);
629 if (rc < 0) /* block test failed */
632 if (blockstart + block_seek + readlen > top_of_range) {
633 /* End of range is reached */
635 "Too few good blocks within range\n");
639 if (rc) { /* block is bad */
640 blockstart += blocklen;
645 * If a block is bad, we retry in the next block at the same
646 * offset - see common/env_nand.c::writeenv()
648 lseek (fd, blockstart + block_seek, SEEK_SET);
650 rc = read (fd, buf + processed, readlen);
652 fprintf (stderr, "Read error on %s: %s\n",
653 DEVNAME (dev), strerror (errno));
657 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
658 rc, blockstart + block_seek, DEVNAME(dev));
660 processed += readlen;
661 readlen = min (blocklen, count - processed);
663 blockstart += blocklen;
670 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
671 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
672 * erase and write the whole data at once.
674 static int flash_write_buf (int dev, int fd, void *buf, size_t count,
675 off_t offset, uint8_t mtd_type)
678 struct erase_info_user erase;
679 size_t blocklen; /* length of NAND block / NOR erase sector */
680 size_t erase_len; /* whole area that can be erased - may include
682 size_t erasesize; /* erase / write length - one block on NAND,
684 size_t processed = 0; /* progress counter */
685 size_t write_total; /* total size to actually write - excluding
687 off_t erase_offset; /* offset to the first erase block (aligned)
689 off_t block_seek; /* offset inside the erase block to the start
691 off_t top_of_range; /* end of the last block we may use */
692 loff_t blockstart; /* running start of the current block -
693 MEMGETBADBLOCK needs 64 bits */
696 blocklen = DEVESIZE (dev);
698 top_of_range = ((DEVOFFSET(dev) / blocklen) +
699 ENVSECTORS (dev)) * blocklen;
701 erase_offset = (offset / blocklen) * blocklen;
703 /* Maximum area we may use */
704 erase_len = top_of_range - erase_offset;
706 blockstart = erase_offset;
707 /* Offset inside a block */
708 block_seek = offset - erase_offset;
711 * Data size we actually have to write: from the start of the block
712 * to the start of the data, then count bytes of data, and to the
715 write_total = ((block_seek + count + blocklen - 1) /
716 blocklen) * blocklen;
719 * Support data anywhere within erase sectors: read out the complete
720 * area to be erased, replace the environment image, write the whole
723 if (write_total > count) {
724 data = malloc (erase_len);
727 "Cannot malloc %zu bytes: %s\n",
728 erase_len, strerror (errno));
732 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
734 if (write_total != rc)
738 fprintf(stderr, "Preserving data ");
740 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
741 if (block_seek + count != write_total) {
743 fprintf(stderr, " and ");
744 fprintf(stderr, "0x%lx - 0x%x",
745 block_seek + count, write_total - 1);
747 fprintf(stderr, "\n");
749 /* Overwrite the old environment */
750 memcpy (data + block_seek, buf, count);
753 * We get here, iff offset is block-aligned and count is a
754 * multiple of blocklen - see write_total calculation above
759 if (mtd_type == MTD_NANDFLASH) {
761 * NAND: calculate which blocks we are writing. We have
762 * to write one block at a time to skip bad blocks.
764 erasesize = blocklen;
766 erasesize = erase_len;
769 erase.length = erasesize;
771 /* This only runs once on NOR flash and SPI-dataflash */
772 while (processed < write_total) {
773 rc = flash_bad_block (fd, mtd_type, &blockstart);
774 if (rc < 0) /* block test failed */
777 if (blockstart + erasesize > top_of_range) {
778 fprintf (stderr, "End of range reached, aborting\n");
782 if (rc) { /* block is bad */
783 blockstart += blocklen;
787 erase.start = blockstart;
788 ioctl (fd, MEMUNLOCK, &erase);
790 /* Dataflash does not need an explicit erase cycle */
791 if (mtd_type != MTD_DATAFLASH)
792 if (ioctl (fd, MEMERASE, &erase) != 0) {
793 fprintf (stderr, "MTD erase error on %s: %s\n",
799 if (lseek (fd, blockstart, SEEK_SET) == -1) {
801 "Seek error on %s: %s\n",
802 DEVNAME (dev), strerror (errno));
807 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
810 if (write (fd, data + processed, erasesize) != erasesize) {
811 fprintf (stderr, "Write error on %s: %s\n",
812 DEVNAME (dev), strerror (errno));
816 ioctl (fd, MEMLOCK, &erase);
818 processed += blocklen;
820 blockstart += blocklen;
823 if (write_total > count)
830 * Set obsolete flag at offset - NOR flash only
832 static int flash_flag_obsolete (int dev, int fd, off_t offset)
835 struct erase_info_user erase;
837 erase.start = DEVOFFSET (dev);
838 erase.length = DEVESIZE (dev);
839 /* This relies on the fact, that obsolete_flag == 0 */
840 rc = lseek (fd, offset, SEEK_SET);
842 fprintf (stderr, "Cannot seek to set the flag on %s \n",
846 ioctl (fd, MEMUNLOCK, &erase);
847 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
848 ioctl (fd, MEMLOCK, &erase);
850 perror ("Could not set obsolete flag");
855 static int flash_write (int fd_current, int fd_target, int dev_target)
859 switch (environment.flag_scheme) {
862 case FLAG_INCREMENTAL:
863 (*environment.flags)++;
866 *environment.flags = active_flag;
869 fprintf (stderr, "Unimplemented flash scheme %u \n",
870 environment.flag_scheme);
875 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
876 DEVOFFSET (dev_target), DEVNAME (dev_target));
878 rc = flash_write_buf(dev_target, fd_target, environment.image,
879 CUR_ENVSIZE, DEVOFFSET(dev_target),
880 DEVTYPE(dev_target));
884 if (environment.flag_scheme == FLAG_BOOLEAN) {
885 /* Have to set obsolete flag */
886 off_t offset = DEVOFFSET (dev_current) +
887 offsetof (struct env_image_redundant, flags);
890 "Setting obsolete flag in environment at 0x%lx on %s\n",
891 DEVOFFSET (dev_current), DEVNAME (dev_current));
893 flash_flag_obsolete (dev_current, fd_current, offset);
899 static int flash_read (int fd)
901 struct mtd_info_user mtdinfo;
904 rc = ioctl (fd, MEMGETINFO, &mtdinfo);
906 perror ("Cannot get MTD information");
910 if (mtdinfo.type != MTD_NORFLASH &&
911 mtdinfo.type != MTD_NANDFLASH &&
912 mtdinfo.type != MTD_DATAFLASH) {
913 fprintf (stderr, "Unsupported flash type %u\n", mtdinfo.type);
917 DEVTYPE(dev_current) = mtdinfo.type;
919 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
920 DEVOFFSET (dev_current), mtdinfo.type);
922 return (rc != CUR_ENVSIZE) ? -1 : 0;
925 static int flash_io (int mode)
927 int fd_current, fd_target, rc, dev_target;
929 /* dev_current: fd_current, erase_current */
930 fd_current = open (DEVNAME (dev_current), mode);
931 if (fd_current < 0) {
933 "Can't open %s: %s\n",
934 DEVNAME (dev_current), strerror (errno));
938 if (mode == O_RDWR) {
940 /* switch to next partition for writing */
941 dev_target = !dev_current;
942 /* dev_target: fd_target, erase_target */
943 fd_target = open (DEVNAME (dev_target), mode);
946 "Can't open %s: %s\n",
947 DEVNAME (dev_target),
953 dev_target = dev_current;
954 fd_target = fd_current;
957 rc = flash_write (fd_current, fd_target, dev_target);
960 if (close (fd_target)) {
962 "I/O error on %s: %s\n",
963 DEVNAME (dev_target),
969 rc = flash_read (fd_current);
973 if (close (fd_current)) {
975 "I/O error on %s: %s\n",
976 DEVNAME (dev_current), strerror (errno));
984 * s1 is either a simple 'name', or a 'name=value' pair.
985 * s2 is a 'name=value' pair.
986 * If the names match, return the value of s2, else NULL.
989 static char *envmatch (char * s1, char * s2)
991 if (s1 == NULL || s2 == NULL)
997 if (*s1 == '\0' && *(s2 - 1) == '=')
1003 * Prevent confusion if running from erased flash memory
1005 int fw_env_open(void)
1008 unsigned char flag0;
1012 unsigned char flag1;
1015 struct env_image_single *single;
1016 struct env_image_redundant *redundant;
1018 if (parse_config ()) /* should fill envdevices */
1021 addr0 = calloc(1, CUR_ENVSIZE);
1022 if (addr0 == NULL) {
1024 "Not enough memory for environment (%ld bytes)\n",
1029 /* read environment from FLASH to local buffer */
1030 environment.image = addr0;
1032 if (HaveRedundEnv) {
1034 environment.crc = &redundant->crc;
1035 environment.flags = &redundant->flags;
1036 environment.data = redundant->data;
1039 environment.crc = &single->crc;
1040 environment.flags = NULL;
1041 environment.data = single->data;
1045 if (flash_io (O_RDONLY))
1048 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1049 crc0_ok = (crc0 == *environment.crc);
1050 if (!HaveRedundEnv) {
1053 "Warning: Bad CRC, using default environment\n");
1054 memcpy(environment.data, default_environment, sizeof default_environment);
1057 flag0 = *environment.flags;
1060 addr1 = calloc(1, CUR_ENVSIZE);
1061 if (addr1 == NULL) {
1063 "Not enough memory for environment (%ld bytes)\n",
1070 * have to set environment.image for flash_read(), careful -
1071 * other pointers in environment still point inside addr0
1073 environment.image = addr1;
1074 if (flash_io (O_RDONLY))
1077 /* Check flag scheme compatibility */
1078 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1079 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1080 environment.flag_scheme = FLAG_BOOLEAN;
1081 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1082 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1083 environment.flag_scheme = FLAG_INCREMENTAL;
1084 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1085 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1086 environment.flag_scheme = FLAG_BOOLEAN;
1088 fprintf (stderr, "Incompatible flash types!\n");
1092 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1093 crc1_ok = (crc1 == redundant->crc);
1094 flag1 = redundant->flags;
1096 if (crc0_ok && !crc1_ok) {
1098 } else if (!crc0_ok && crc1_ok) {
1100 } else if (!crc0_ok && !crc1_ok) {
1102 "Warning: Bad CRC, using default environment\n");
1103 memcpy (environment.data, default_environment,
1104 sizeof default_environment);
1107 switch (environment.flag_scheme) {
1109 if (flag0 == active_flag &&
1110 flag1 == obsolete_flag) {
1112 } else if (flag0 == obsolete_flag &&
1113 flag1 == active_flag) {
1115 } else if (flag0 == flag1) {
1117 } else if (flag0 == 0xFF) {
1119 } else if (flag1 == 0xFF) {
1125 case FLAG_INCREMENTAL:
1126 if (flag0 == 255 && flag1 == 0)
1128 else if ((flag1 == 255 && flag0 == 0) ||
1131 else /* flag1 > flag0 */
1135 fprintf (stderr, "Unknown flag scheme %u \n",
1136 environment.flag_scheme);
1142 * If we are reading, we don't need the flag and the CRC any
1143 * more, if we are writing, we will re-calculate CRC and update
1144 * flags before writing out
1147 environment.image = addr1;
1148 environment.crc = &redundant->crc;
1149 environment.flags = &redundant->flags;
1150 environment.data = redundant->data;
1153 environment.image = addr0;
1154 /* Other pointers are already set */
1158 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1165 static int parse_config ()
1169 #if defined(CONFIG_FILE)
1170 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
1171 if (get_config (CONFIG_FILE)) {
1173 "Cannot parse config file: %s\n", strerror (errno));
1177 strcpy (DEVNAME (0), DEVICE1_NAME);
1178 DEVOFFSET (0) = DEVICE1_OFFSET;
1179 ENVSIZE (0) = ENV1_SIZE;
1180 /* Default values are: erase-size=env-size, #sectors=1 */
1181 DEVESIZE (0) = ENVSIZE (0);
1183 #ifdef DEVICE1_ESIZE
1184 DEVESIZE (0) = DEVICE1_ESIZE;
1186 #ifdef DEVICE1_ENVSECTORS
1187 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
1191 strcpy (DEVNAME (1), DEVICE2_NAME);
1192 DEVOFFSET (1) = DEVICE2_OFFSET;
1193 ENVSIZE (1) = ENV2_SIZE;
1194 /* Default values are: erase-size=env-size, #sectors=1 */
1195 DEVESIZE (1) = ENVSIZE (1);
1197 #ifdef DEVICE2_ESIZE
1198 DEVESIZE (1) = DEVICE2_ESIZE;
1200 #ifdef DEVICE2_ENVSECTORS
1201 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
1206 if (stat (DEVNAME (0), &st)) {
1208 "Cannot access MTD device %s: %s\n",
1209 DEVNAME (0), strerror (errno));
1213 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1215 "Cannot access MTD device %s: %s\n",
1216 DEVNAME (1), strerror (errno));
1222 #if defined(CONFIG_FILE)
1223 static int get_config (char *fname)
1230 fp = fopen (fname, "r");
1234 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
1235 /* Skip incomplete conversions and comment strings */
1239 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1250 /* Assume the erase size is the same as the env-size */
1251 DEVESIZE(i) = ENVSIZE(i);
1254 /* Default - 1 sector */
1261 HaveRedundEnv = i - 1;
1262 if (!i) { /* No valid entries found */