2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
12 #include <env_flags.h>
14 #include <linux/stringify.h>
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
26 # include <linux/mtd/mtd.h>
28 # define __user /* nothing */
29 # include <mtd/mtd-user.h>
34 #define WHITESPACE(c) ((c == '\t') || (c == ' '))
36 #define min(x, y) ({ \
37 typeof(x) _min1 = (x); \
38 typeof(y) _min2 = (y); \
39 (void) (&_min1 == &_min2); \
40 _min1 < _min2 ? _min1 : _min2; })
43 const char *devname; /* Device name */
44 ulong devoff; /* Device offset */
45 ulong env_size; /* environment size */
46 ulong erase_size; /* device erase size */
47 ulong env_sectors; /* number of environment sectors */
48 uint8_t mtd_type; /* type of the MTD device */
51 static struct envdev_s envdevices[2] =
54 .mtd_type = MTD_ABSENT,
56 .mtd_type = MTD_ABSENT,
59 static int dev_current;
61 #define DEVNAME(i) envdevices[(i)].devname
62 #define DEVOFFSET(i) envdevices[(i)].devoff
63 #define ENVSIZE(i) envdevices[(i)].env_size
64 #define DEVESIZE(i) envdevices[(i)].erase_size
65 #define ENVSECTORS(i) envdevices[(i)].env_sectors
66 #define DEVTYPE(i) envdevices[(i)].mtd_type
68 #define CUR_ENVSIZE ENVSIZE(dev_current)
70 #define ENV_SIZE getenvsize()
72 struct env_image_single {
73 uint32_t crc; /* CRC32 over data bytes */
77 struct env_image_redundant {
78 uint32_t crc; /* CRC32 over data bytes */
79 unsigned char flags; /* active or obsolete */
94 enum flag_scheme flag_scheme;
97 static struct environment environment = {
98 .flag_scheme = FLAG_NONE,
101 static int HaveRedundEnv = 0;
103 static unsigned char active_flag = 1;
104 /* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
105 static unsigned char obsolete_flag = 0;
107 #define DEFAULT_ENV_INSTANCE_STATIC
108 #include <env_default.h>
110 static int flash_io (int mode);
111 static char *envmatch (char * s1, char * s2);
112 static int parse_config (void);
114 #if defined(CONFIG_FILE)
115 static int get_config (char *);
117 static inline ulong getenvsize (void)
119 ulong rc = CUR_ENVSIZE - sizeof(long);
126 static char *fw_string_blank(char *s, int noblank)
131 for (i = 0; i < len; i++, s++) {
132 if ((noblank && !WHITESPACE(*s)) ||
133 (!noblank && WHITESPACE(*s)))
143 * Search the environment for a variable.
144 * Return the value, if found, or NULL, if not found.
146 char *fw_getenv (char *name)
150 for (env = environment.data; *env; env = nxt + 1) {
153 for (nxt = env; *nxt; ++nxt) {
154 if (nxt >= &environment.data[ENV_SIZE]) {
155 fprintf (stderr, "## Error: "
156 "environment not terminated\n");
160 val = envmatch (name, env);
169 * Search the default environment for a variable.
170 * Return the value, if found, or NULL, if not found.
172 char *fw_getdefenv(char *name)
176 for (env = default_environment; *env; env = nxt + 1) {
179 for (nxt = env; *nxt; ++nxt) {
180 if (nxt >= &default_environment[ENV_SIZE]) {
181 fprintf(stderr, "## Error: "
182 "default environment not terminated\n");
186 val = envmatch(name, env);
195 * Print the current definition of one, or more, or all
196 * environment variables
198 int fw_printenv (int argc, char *argv[])
207 if (argc == 1) { /* Print all env variables */
208 for (env = environment.data; *env; env = nxt + 1) {
209 for (nxt = env; *nxt; ++nxt) {
210 if (nxt >= &environment.data[ENV_SIZE]) {
211 fprintf (stderr, "## Error: "
212 "environment not terminated\n");
217 printf ("%s\n", env);
222 if (strcmp (argv[1], "-n") == 0) {
227 fprintf (stderr, "## Error: "
228 "`-n' option requires exactly one argument\n");
235 for (i = 1; i < argc; ++i) { /* print single env variables */
236 char *name = argv[i];
239 for (env = environment.data; *env; env = nxt + 1) {
241 for (nxt = env; *nxt; ++nxt) {
242 if (nxt >= &environment.data[ENV_SIZE]) {
243 fprintf (stderr, "## Error: "
244 "environment not terminated\n");
248 val = envmatch (name, env);
251 fputs (name, stdout);
259 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
267 int fw_env_close(void)
272 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
274 /* write environment back to flash */
275 if (flash_io(O_RDWR)) {
277 "Error: can't write fw_env to flash\n");
286 * Set/Clear a single variable in the environment.
287 * This is called in sequence to update the environment
288 * in RAM without updating the copy in flash after each set
290 int fw_env_write(char *name, char *value)
295 int deleting, creating, overwriting;
298 * search if variable with this name already exists
300 for (nxt = env = environment.data; *env; env = nxt + 1) {
301 for (nxt = env; *nxt; ++nxt) {
302 if (nxt >= &environment.data[ENV_SIZE]) {
303 fprintf(stderr, "## Error: "
304 "environment not terminated\n");
309 if ((oldval = envmatch (name, env)) != NULL)
313 deleting = (oldval && !(value && strlen(value)));
314 creating = (!oldval && (value && strlen(value)));
315 overwriting = (oldval && (value && strlen(value)));
317 /* check for permission */
319 if (env_flags_validate_varaccess(name,
320 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
321 printf("Can't delete \"%s\"\n", name);
325 } else if (overwriting) {
326 if (env_flags_validate_varaccess(name,
327 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
328 printf("Can't overwrite \"%s\"\n", name);
331 } else if (env_flags_validate_varaccess(name,
332 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
333 const char *defval = fw_getdefenv(name);
337 if (strcmp(oldval, defval)
339 printf("Can't overwrite \"%s\"\n", name);
344 } else if (creating) {
345 if (env_flags_validate_varaccess(name,
346 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
347 printf("Can't create \"%s\"\n", name);
355 if (deleting || overwriting) {
356 if (*++nxt == '\0') {
361 if ((*env == '\0') && (*nxt == '\0'))
370 if (!value || !strlen(value))
374 * Append new definition at the end
376 for (env = environment.data; *env || *(env + 1); ++env);
377 if (env > environment.data)
381 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
383 len = strlen (name) + 2;
384 /* add '=' for first arg, ' ' for all others */
385 len += strlen(value) + 1;
387 if (len > (&environment.data[ENV_SIZE] - env)) {
389 "Error: environment overflow, \"%s\" deleted\n",
394 while ((*env = *name++) != '\0')
397 while ((*++env = *value++) != '\0')
400 /* end is marked with double '\0' */
407 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
409 * EINVAL - need at least 1 argument
410 * EROFS - certain variables ("ethaddr", "serial#") cannot be
411 * modified or deleted
414 int fw_setenv(int argc, char *argv[])
427 fprintf(stderr, "Error: environment not initialized\n");
433 if (env_flags_validate_env_set_params(argc, argv) < 0)
437 for (i = 2; i < argc; ++i) {
439 size_t val_len = strlen(val);
442 value[len - 1] = ' ';
443 value = realloc(value, len + val_len + 1);
446 "Cannot malloc %zu bytes: %s\n",
447 len, strerror(errno));
451 memcpy(value + len, val, val_len);
456 fw_env_write(name, value);
460 return fw_env_close();
464 * Parse a file and configure the u-boot variables.
465 * The script file has a very simple format, as follows:
467 * Each line has a couple with name, value:
468 * <white spaces>variable_name<white spaces>variable_value
470 * Both variable_name and variable_value are interpreted as strings.
471 * Any character after <white spaces> and before ending \r\n is interpreted
472 * as variable's value (no comment allowed on these lines !)
474 * Comments are allowed if the first character in the line is #
476 * Returns -1 and sets errno error codes:
480 int fw_parse_script(char *fname)
483 char dump[1024]; /* Maximum line length in the file */
491 fprintf(stderr, "Error: environment not initialized\n");
495 if (strcmp(fname, "-") == 0)
498 fp = fopen(fname, "r");
500 fprintf(stderr, "I cannot open %s for reading\n",
506 while (fgets(dump, sizeof(dump), fp)) {
511 * Read a whole line from the file. If the line is too long
512 * or is not terminated, reports an error and exit.
514 if (dump[len - 1] != '\n') {
516 "Line %d not corrected terminated or too long\n",
522 /* Drop ending line feed / carriage return */
523 while (len > 0 && (dump[len - 1] == '\n' ||
524 dump[len - 1] == '\r')) {
525 dump[len - 1] = '\0';
529 /* Skip comment or empty lines */
530 if ((len == 0) || dump[0] == '#')
534 * Search for variable's name,
535 * remove leading whitespaces
537 name = fw_string_blank(dump, 1);
541 /* The first white space is the end of variable name */
542 val = fw_string_blank(name, 0);
546 if ((val - name) < len)
547 val = fw_string_blank(val, 1);
553 fprintf(stderr, "Setting %s : %s\n",
554 name, val ? val : " removed");
557 if (env_flags_validate_type(name, val) < 0) {
563 * If there is an error setting a variable,
564 * try to save the environment and returns an error
566 if (fw_env_write(name, val)) {
568 "fw_env_write returns with error : %s\n",
576 /* Close file if not stdin */
577 if (strcmp(fname, "-") != 0)
580 ret |= fw_env_close();
587 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
590 * < 0 - failed to test
592 static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
594 if (mtd_type == MTD_NANDFLASH) {
595 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
598 perror ("Cannot read bad block mark");
604 fprintf (stderr, "Bad block at 0x%llx, "
605 "skipping\n", *blockstart);
615 * Read data from flash at an offset into a provided buffer. On NAND it skips
616 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
617 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
619 static int flash_read_buf (int dev, int fd, void *buf, size_t count,
620 off_t offset, uint8_t mtd_type)
622 size_t blocklen; /* erase / write length - one block on NAND,
624 size_t processed = 0; /* progress counter */
625 size_t readlen = count; /* current read length */
626 off_t top_of_range; /* end of the last block we may use */
627 off_t block_seek; /* offset inside the current block to the start
629 loff_t blockstart; /* running start of the current block -
630 MEMGETBADBLOCK needs 64 bits */
633 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
635 /* Offset inside a block */
636 block_seek = offset - blockstart;
638 if (mtd_type == MTD_NANDFLASH) {
640 * NAND: calculate which blocks we are reading. We have
641 * to read one block at a time to skip bad blocks.
643 blocklen = DEVESIZE (dev);
646 * To calculate the top of the range, we have to use the
647 * global DEVOFFSET (dev), which can be different from offset
649 top_of_range = ((DEVOFFSET(dev) / blocklen) +
650 ENVSECTORS (dev)) * blocklen;
652 /* Limit to one block for the first read */
653 if (readlen > blocklen - block_seek)
654 readlen = blocklen - block_seek;
657 top_of_range = offset + count;
660 /* This only runs once on NOR flash */
661 while (processed < count) {
662 rc = flash_bad_block (fd, mtd_type, &blockstart);
663 if (rc < 0) /* block test failed */
666 if (blockstart + block_seek + readlen > top_of_range) {
667 /* End of range is reached */
669 "Too few good blocks within range\n");
673 if (rc) { /* block is bad */
674 blockstart += blocklen;
679 * If a block is bad, we retry in the next block at the same
680 * offset - see common/env_nand.c::writeenv()
682 lseek (fd, blockstart + block_seek, SEEK_SET);
684 rc = read (fd, buf + processed, readlen);
686 fprintf (stderr, "Read error on %s: %s\n",
687 DEVNAME (dev), strerror (errno));
691 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
692 rc, blockstart + block_seek, DEVNAME(dev));
694 processed += readlen;
695 readlen = min (blocklen, count - processed);
697 blockstart += blocklen;
704 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
705 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
706 * erase and write the whole data at once.
708 static int flash_write_buf (int dev, int fd, void *buf, size_t count,
709 off_t offset, uint8_t mtd_type)
712 struct erase_info_user erase;
713 size_t blocklen; /* length of NAND block / NOR erase sector */
714 size_t erase_len; /* whole area that can be erased - may include
716 size_t erasesize; /* erase / write length - one block on NAND,
718 size_t processed = 0; /* progress counter */
719 size_t write_total; /* total size to actually write - excluding
721 off_t erase_offset; /* offset to the first erase block (aligned)
723 off_t block_seek; /* offset inside the erase block to the start
725 off_t top_of_range; /* end of the last block we may use */
726 loff_t blockstart; /* running start of the current block -
727 MEMGETBADBLOCK needs 64 bits */
731 * For mtd devices only offset and size of the environment do matter
733 if (mtd_type == MTD_ABSENT) {
735 top_of_range = offset + count;
736 erase_len = blocklen;
739 write_total = blocklen;
741 blocklen = DEVESIZE(dev);
743 top_of_range = ((DEVOFFSET(dev) / blocklen) +
744 ENVSECTORS(dev)) * blocklen;
746 erase_offset = (offset / blocklen) * blocklen;
748 /* Maximum area we may use */
749 erase_len = top_of_range - erase_offset;
751 blockstart = erase_offset;
752 /* Offset inside a block */
753 block_seek = offset - erase_offset;
756 * Data size we actually write: from the start of the block
757 * to the start of the data, then count bytes of data, and
758 * to the end of the block
760 write_total = ((block_seek + count + blocklen - 1) /
761 blocklen) * blocklen;
765 * Support data anywhere within erase sectors: read out the complete
766 * area to be erased, replace the environment image, write the whole
769 if (write_total > count) {
770 data = malloc (erase_len);
773 "Cannot malloc %zu bytes: %s\n",
774 erase_len, strerror (errno));
778 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
780 if (write_total != rc)
784 fprintf(stderr, "Preserving data ");
786 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
787 if (block_seek + count != write_total) {
789 fprintf(stderr, " and ");
790 fprintf(stderr, "0x%lx - 0x%x",
791 block_seek + count, write_total - 1);
793 fprintf(stderr, "\n");
795 /* Overwrite the old environment */
796 memcpy (data + block_seek, buf, count);
799 * We get here, iff offset is block-aligned and count is a
800 * multiple of blocklen - see write_total calculation above
805 if (mtd_type == MTD_NANDFLASH) {
807 * NAND: calculate which blocks we are writing. We have
808 * to write one block at a time to skip bad blocks.
810 erasesize = blocklen;
812 erasesize = erase_len;
815 erase.length = erasesize;
817 /* This only runs once on NOR flash and SPI-dataflash */
818 while (processed < write_total) {
819 rc = flash_bad_block (fd, mtd_type, &blockstart);
820 if (rc < 0) /* block test failed */
823 if (blockstart + erasesize > top_of_range) {
824 fprintf (stderr, "End of range reached, aborting\n");
828 if (rc) { /* block is bad */
829 blockstart += blocklen;
833 if (mtd_type != MTD_ABSENT) {
834 erase.start = blockstart;
835 ioctl(fd, MEMUNLOCK, &erase);
836 /* These do not need an explicit erase cycle */
837 if (mtd_type != MTD_DATAFLASH)
838 if (ioctl(fd, MEMERASE, &erase) != 0) {
840 "MTD erase error on %s: %s\n",
841 DEVNAME(dev), strerror(errno));
846 if (lseek (fd, blockstart, SEEK_SET) == -1) {
848 "Seek error on %s: %s\n",
849 DEVNAME (dev), strerror (errno));
854 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
857 if (write (fd, data + processed, erasesize) != erasesize) {
858 fprintf (stderr, "Write error on %s: %s\n",
859 DEVNAME (dev), strerror (errno));
863 if (mtd_type != MTD_ABSENT)
864 ioctl(fd, MEMLOCK, &erase);
866 processed += erasesize;
868 blockstart += erasesize;
871 if (write_total > count)
878 * Set obsolete flag at offset - NOR flash only
880 static int flash_flag_obsolete (int dev, int fd, off_t offset)
883 struct erase_info_user erase;
885 erase.start = DEVOFFSET (dev);
886 erase.length = DEVESIZE (dev);
887 /* This relies on the fact, that obsolete_flag == 0 */
888 rc = lseek (fd, offset, SEEK_SET);
890 fprintf (stderr, "Cannot seek to set the flag on %s \n",
894 ioctl (fd, MEMUNLOCK, &erase);
895 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
896 ioctl (fd, MEMLOCK, &erase);
898 perror ("Could not set obsolete flag");
903 static int flash_write (int fd_current, int fd_target, int dev_target)
907 switch (environment.flag_scheme) {
910 case FLAG_INCREMENTAL:
911 (*environment.flags)++;
914 *environment.flags = active_flag;
917 fprintf (stderr, "Unimplemented flash scheme %u \n",
918 environment.flag_scheme);
923 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
924 DEVOFFSET (dev_target), DEVNAME (dev_target));
926 rc = flash_write_buf(dev_target, fd_target, environment.image,
927 CUR_ENVSIZE, DEVOFFSET(dev_target),
928 DEVTYPE(dev_target));
932 if (environment.flag_scheme == FLAG_BOOLEAN) {
933 /* Have to set obsolete flag */
934 off_t offset = DEVOFFSET (dev_current) +
935 offsetof (struct env_image_redundant, flags);
938 "Setting obsolete flag in environment at 0x%lx on %s\n",
939 DEVOFFSET (dev_current), DEVNAME (dev_current));
941 flash_flag_obsolete (dev_current, fd_current, offset);
947 static int flash_read (int fd)
949 struct mtd_info_user mtdinfo;
955 fprintf(stderr, "Cannot stat the file %s\n",
956 DEVNAME(dev_current));
960 if (S_ISCHR(st.st_mode)) {
961 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
963 fprintf(stderr, "Cannot get MTD information for %s\n",
964 DEVNAME(dev_current));
967 if (mtdinfo.type != MTD_NORFLASH &&
968 mtdinfo.type != MTD_NANDFLASH &&
969 mtdinfo.type != MTD_DATAFLASH &&
970 mtdinfo.type != MTD_UBIVOLUME) {
971 fprintf (stderr, "Unsupported flash type %u on %s\n",
972 mtdinfo.type, DEVNAME(dev_current));
976 memset(&mtdinfo, 0, sizeof(mtdinfo));
977 mtdinfo.type = MTD_ABSENT;
980 DEVTYPE(dev_current) = mtdinfo.type;
982 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
983 DEVOFFSET (dev_current), mtdinfo.type);
985 return (rc != CUR_ENVSIZE) ? -1 : 0;
988 static int flash_io (int mode)
990 int fd_current, fd_target, rc, dev_target;
992 /* dev_current: fd_current, erase_current */
993 fd_current = open (DEVNAME (dev_current), mode);
994 if (fd_current < 0) {
996 "Can't open %s: %s\n",
997 DEVNAME (dev_current), strerror (errno));
1001 if (mode == O_RDWR) {
1002 if (HaveRedundEnv) {
1003 /* switch to next partition for writing */
1004 dev_target = !dev_current;
1005 /* dev_target: fd_target, erase_target */
1006 fd_target = open (DEVNAME (dev_target), mode);
1007 if (fd_target < 0) {
1009 "Can't open %s: %s\n",
1010 DEVNAME (dev_target),
1016 dev_target = dev_current;
1017 fd_target = fd_current;
1020 rc = flash_write (fd_current, fd_target, dev_target);
1022 if (HaveRedundEnv) {
1023 if (close (fd_target)) {
1025 "I/O error on %s: %s\n",
1026 DEVNAME (dev_target),
1032 rc = flash_read (fd_current);
1036 if (close (fd_current)) {
1038 "I/O error on %s: %s\n",
1039 DEVNAME (dev_current), strerror (errno));
1047 * s1 is either a simple 'name', or a 'name=value' pair.
1048 * s2 is a 'name=value' pair.
1049 * If the names match, return the value of s2, else NULL.
1052 static char *envmatch (char * s1, char * s2)
1054 if (s1 == NULL || s2 == NULL)
1057 while (*s1 == *s2++)
1060 if (*s1 == '\0' && *(s2 - 1) == '=')
1066 * Prevent confusion if running from erased flash memory
1068 int fw_env_open(void)
1071 unsigned char flag0;
1075 unsigned char flag1;
1078 struct env_image_single *single;
1079 struct env_image_redundant *redundant;
1081 if (parse_config ()) /* should fill envdevices */
1084 addr0 = calloc(1, CUR_ENVSIZE);
1085 if (addr0 == NULL) {
1087 "Not enough memory for environment (%ld bytes)\n",
1092 /* read environment from FLASH to local buffer */
1093 environment.image = addr0;
1095 if (HaveRedundEnv) {
1097 environment.crc = &redundant->crc;
1098 environment.flags = &redundant->flags;
1099 environment.data = redundant->data;
1102 environment.crc = &single->crc;
1103 environment.flags = NULL;
1104 environment.data = single->data;
1108 if (flash_io (O_RDONLY))
1111 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1112 crc0_ok = (crc0 == *environment.crc);
1113 if (!HaveRedundEnv) {
1116 "Warning: Bad CRC, using default environment\n");
1117 memcpy(environment.data, default_environment, sizeof default_environment);
1120 flag0 = *environment.flags;
1123 addr1 = calloc(1, CUR_ENVSIZE);
1124 if (addr1 == NULL) {
1126 "Not enough memory for environment (%ld bytes)\n",
1133 * have to set environment.image for flash_read(), careful -
1134 * other pointers in environment still point inside addr0
1136 environment.image = addr1;
1137 if (flash_io (O_RDONLY))
1140 /* Check flag scheme compatibility */
1141 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1142 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1143 environment.flag_scheme = FLAG_BOOLEAN;
1144 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1145 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1146 environment.flag_scheme = FLAG_INCREMENTAL;
1147 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1148 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1149 environment.flag_scheme = FLAG_BOOLEAN;
1150 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1151 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1152 environment.flag_scheme = FLAG_INCREMENTAL;
1153 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1154 DEVTYPE(!dev_current) == MTD_ABSENT) {
1155 environment.flag_scheme = FLAG_INCREMENTAL;
1157 fprintf (stderr, "Incompatible flash types!\n");
1161 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1162 crc1_ok = (crc1 == redundant->crc);
1163 flag1 = redundant->flags;
1165 if (crc0_ok && !crc1_ok) {
1167 } else if (!crc0_ok && crc1_ok) {
1169 } else if (!crc0_ok && !crc1_ok) {
1171 "Warning: Bad CRC, using default environment\n");
1172 memcpy (environment.data, default_environment,
1173 sizeof default_environment);
1176 switch (environment.flag_scheme) {
1178 if (flag0 == active_flag &&
1179 flag1 == obsolete_flag) {
1181 } else if (flag0 == obsolete_flag &&
1182 flag1 == active_flag) {
1184 } else if (flag0 == flag1) {
1186 } else if (flag0 == 0xFF) {
1188 } else if (flag1 == 0xFF) {
1194 case FLAG_INCREMENTAL:
1195 if (flag0 == 255 && flag1 == 0)
1197 else if ((flag1 == 255 && flag0 == 0) ||
1200 else /* flag1 > flag0 */
1204 fprintf (stderr, "Unknown flag scheme %u \n",
1205 environment.flag_scheme);
1211 * If we are reading, we don't need the flag and the CRC any
1212 * more, if we are writing, we will re-calculate CRC and update
1213 * flags before writing out
1216 environment.image = addr1;
1217 environment.crc = &redundant->crc;
1218 environment.flags = &redundant->flags;
1219 environment.data = redundant->data;
1222 environment.image = addr0;
1223 /* Other pointers are already set */
1227 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1234 static int parse_config ()
1238 #if defined(CONFIG_FILE)
1239 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
1240 if (get_config (CONFIG_FILE)) {
1242 "Cannot parse config file: %s\n", strerror (errno));
1246 DEVNAME (0) = DEVICE1_NAME;
1247 DEVOFFSET (0) = DEVICE1_OFFSET;
1248 ENVSIZE (0) = ENV1_SIZE;
1249 /* Default values are: erase-size=env-size */
1250 DEVESIZE (0) = ENVSIZE (0);
1251 /* #sectors=env-size/erase-size (rounded up) */
1252 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
1253 #ifdef DEVICE1_ESIZE
1254 DEVESIZE (0) = DEVICE1_ESIZE;
1256 #ifdef DEVICE1_ENVSECTORS
1257 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
1261 DEVNAME (1) = DEVICE2_NAME;
1262 DEVOFFSET (1) = DEVICE2_OFFSET;
1263 ENVSIZE (1) = ENV2_SIZE;
1264 /* Default values are: erase-size=env-size */
1265 DEVESIZE (1) = ENVSIZE (1);
1266 /* #sectors=env-size/erase-size (rounded up) */
1267 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
1268 #ifdef DEVICE2_ESIZE
1269 DEVESIZE (1) = DEVICE2_ESIZE;
1271 #ifdef DEVICE2_ENVSECTORS
1272 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
1277 if (stat (DEVNAME (0), &st)) {
1279 "Cannot access MTD device %s: %s\n",
1280 DEVNAME (0), strerror (errno));
1284 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1286 "Cannot access MTD device %s: %s\n",
1287 DEVNAME (1), strerror (errno));
1293 #if defined(CONFIG_FILE)
1294 static int get_config (char *fname)
1302 fp = fopen (fname, "r");
1306 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
1307 /* Skip incomplete conversions and comment strings */
1311 rc = sscanf (dump, "%ms %lx %lx %lx %lx",
1321 DEVNAME(i) = devname;
1324 /* Assume the erase size is the same as the env-size */
1325 DEVESIZE(i) = ENVSIZE(i);
1328 /* Assume enough env sectors to cover the environment */
1329 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
1335 HaveRedundEnv = i - 1;
1336 if (!i) { /* No valid entries found */