2 * (C) Copyright 2000-2008
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,
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
40 # include <linux/mtd/mtd.h>
42 # define __user /* nothing */
43 # include <mtd/mtd-user.h>
48 #define WHITESPACE(c) ((c == '\t') || (c == ' '))
50 #define min(x, y) ({ \
51 typeof(x) _min1 = (x); \
52 typeof(y) _min2 = (y); \
53 (void) (&_min1 == &_min2); \
54 _min1 < _min2 ? _min1 : _min2; })
57 char devname[16]; /* Device name */
58 ulong devoff; /* Device offset */
59 ulong env_size; /* environment size */
60 ulong erase_size; /* device erase size */
61 ulong env_sectors; /* number of environment sectors */
62 uint8_t mtd_type; /* type of the MTD device */
65 static struct envdev_s envdevices[2] =
68 .mtd_type = MTD_ABSENT,
70 .mtd_type = MTD_ABSENT,
73 static int dev_current;
75 #define DEVNAME(i) envdevices[(i)].devname
76 #define DEVOFFSET(i) envdevices[(i)].devoff
77 #define ENVSIZE(i) envdevices[(i)].env_size
78 #define DEVESIZE(i) envdevices[(i)].erase_size
79 #define ENVSECTORS(i) envdevices[(i)].env_sectors
80 #define DEVTYPE(i) envdevices[(i)].mtd_type
82 #define CONFIG_ENV_SIZE ENVSIZE(dev_current)
84 #define ENV_SIZE getenvsize()
86 struct env_image_single {
87 uint32_t crc; /* CRC32 over data bytes */
91 struct env_image_redundant {
92 uint32_t crc; /* CRC32 over data bytes */
93 unsigned char flags; /* active or obsolete */
106 unsigned char *flags;
108 enum flag_scheme flag_scheme;
111 static struct environment environment = {
112 .flag_scheme = FLAG_NONE,
115 static int HaveRedundEnv = 0;
117 static unsigned char active_flag = 1;
118 /* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
119 static unsigned char obsolete_flag = 0;
122 #define XMK_STR(x) #x
123 #define MK_STR(x) XMK_STR(x)
125 static char default_environment[] = {
126 #if defined(CONFIG_BOOTARGS)
127 "bootargs=" CONFIG_BOOTARGS "\0"
129 #if defined(CONFIG_BOOTCOMMAND)
130 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
132 #if defined(CONFIG_RAMBOOTCOMMAND)
133 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
135 #if defined(CONFIG_NFSBOOTCOMMAND)
136 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
138 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
139 "bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0"
141 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
142 "baudrate=" MK_STR (CONFIG_BAUDRATE) "\0"
144 #ifdef CONFIG_LOADS_ECHO
145 "loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0"
147 #ifdef CONFIG_ETHADDR
148 "ethaddr=" MK_STR (CONFIG_ETHADDR) "\0"
150 #ifdef CONFIG_ETH1ADDR
151 "eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0"
153 #ifdef CONFIG_ETH2ADDR
154 "eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0"
156 #ifdef CONFIG_ETH3ADDR
157 "eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0"
159 #ifdef CONFIG_ETH4ADDR
160 "eth4addr=" MK_STR (CONFIG_ETH4ADDR) "\0"
162 #ifdef CONFIG_ETH5ADDR
163 "eth5addr=" MK_STR (CONFIG_ETH5ADDR) "\0"
165 #ifdef CONFIG_ETHPRIME
166 "ethprime=" CONFIG_ETHPRIME "\0"
169 "ipaddr=" MK_STR (CONFIG_IPADDR) "\0"
171 #ifdef CONFIG_SERVERIP
172 "serverip=" MK_STR (CONFIG_SERVERIP) "\0"
174 #ifdef CONFIG_SYS_AUTOLOAD
175 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
177 #ifdef CONFIG_ROOTPATH
178 "rootpath=" MK_STR (CONFIG_ROOTPATH) "\0"
180 #ifdef CONFIG_GATEWAYIP
181 "gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0"
183 #ifdef CONFIG_NETMASK
184 "netmask=" MK_STR (CONFIG_NETMASK) "\0"
186 #ifdef CONFIG_HOSTNAME
187 "hostname=" MK_STR (CONFIG_HOSTNAME) "\0"
189 #ifdef CONFIG_BOOTFILE
190 "bootfile=" MK_STR (CONFIG_BOOTFILE) "\0"
192 #ifdef CONFIG_LOADADDR
193 "loadaddr=" MK_STR (CONFIG_LOADADDR) "\0"
195 #ifdef CONFIG_PREBOOT
196 "preboot=" CONFIG_PREBOOT "\0"
198 #ifdef CONFIG_CLOCKS_IN_MHZ
199 "clocks_in_mhz=" "1" "\0"
201 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
202 "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0"
204 #ifdef CONFIG_EXTRA_ENV_SETTINGS
205 CONFIG_EXTRA_ENV_SETTINGS
207 "\0" /* Termimate struct environment data with 2 NULs */
210 static int flash_io (int mode);
211 static char *envmatch (char * s1, char * s2);
212 static int parse_config (void);
214 #if defined(CONFIG_FILE)
215 static int get_config (char *);
217 static inline ulong getenvsize (void)
219 ulong rc = CONFIG_ENV_SIZE - sizeof (long);
226 static char *fw_string_blank(char *s, int noblank)
231 for (i = 0; i < len; i++, s++) {
232 if ((noblank && !WHITESPACE(*s)) ||
233 (!noblank && WHITESPACE(*s)))
243 * Search the environment for a variable.
244 * Return the value, if found, or NULL, if not found.
246 char *fw_getenv (char *name)
253 for (env = environment.data; *env; env = nxt + 1) {
256 for (nxt = env; *nxt; ++nxt) {
257 if (nxt >= &environment.data[ENV_SIZE]) {
258 fprintf (stderr, "## Error: "
259 "environment not terminated\n");
263 val = envmatch (name, env);
272 * Print the current definition of one, or more, or all
273 * environment variables
275 int fw_printenv (int argc, char *argv[])
284 if (argc == 1) { /* Print all env variables */
285 for (env = environment.data; *env; env = nxt + 1) {
286 for (nxt = env; *nxt; ++nxt) {
287 if (nxt >= &environment.data[ENV_SIZE]) {
288 fprintf (stderr, "## Error: "
289 "environment not terminated\n");
294 printf ("%s\n", env);
299 if (strcmp (argv[1], "-n") == 0) {
304 fprintf (stderr, "## Error: "
305 "`-n' option requires exactly one argument\n");
312 for (i = 1; i < argc; ++i) { /* print single env variables */
313 char *name = argv[i];
316 for (env = environment.data; *env; env = nxt + 1) {
318 for (nxt = env; *nxt; ++nxt) {
319 if (nxt >= &environment.data[ENV_SIZE]) {
320 fprintf (stderr, "## Error: "
321 "environment not terminated\n");
325 val = envmatch (name, env);
328 fputs (name, stdout);
336 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
344 int fw_env_close(void)
349 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
351 /* write environment back to flash */
352 if (flash_io(O_RDWR)) {
354 "Error: can't write fw_env to flash\n");
363 * Set/Clear a single variable in the environment.
364 * This is called in sequence to update the environment
365 * in RAM without updating the copy in flash after each set
367 int fw_env_write(char *name, char *value)
374 * search if variable with this name already exists
376 for (nxt = env = environment.data; *env; env = nxt + 1) {
377 for (nxt = env; *nxt; ++nxt) {
378 if (nxt >= &environment.data[ENV_SIZE]) {
379 fprintf(stderr, "## Error: "
380 "environment not terminated\n");
385 if ((oldval = envmatch (name, env)) != NULL)
390 * Delete any existing definition
394 * Ethernet Address and serial# can be set only once
396 if ((strcmp (name, "ethaddr") == 0) ||
397 (strcmp (name, "serial#") == 0)) {
398 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
403 if (*++nxt == '\0') {
408 if ((*env == '\0') && (*nxt == '\0'))
417 if (!value || !strlen(value))
421 * Append new definition at the end
423 for (env = environment.data; *env || *(env + 1); ++env);
424 if (env > environment.data)
428 * "name" + "=" + "val" +"\0\0" > CONFIG_ENV_SIZE - (env-environment)
430 len = strlen (name) + 2;
431 /* add '=' for first arg, ' ' for all others */
432 len += strlen(value) + 1;
434 if (len > (&environment.data[ENV_SIZE] - env)) {
436 "Error: environment overflow, \"%s\" deleted\n",
441 while ((*env = *name++) != '\0')
444 while ((*++env = *value++) != '\0')
447 /* end is marked with double '\0' */
454 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
456 * EINVAL - need at least 1 argument
457 * EROFS - certain variables ("ethaddr", "serial#") cannot be
458 * modified or deleted
461 int fw_setenv(int argc, char *argv[])
474 fprintf(stderr, "Error: environment not initialized\n");
480 len = strlen(name) + 2;
481 for (i = 2; i < argc; ++i)
482 len += strlen(argv[i]) + 1;
484 /* Allocate enough place to the data string */
485 for (i = 2; i < argc; ++i) {
488 value = (char *)malloc(len - strlen(name));
491 "Cannot malloc %u bytes: %s\n",
492 len - strlen(name), strerror(errno));
495 memset(value, 0, len - strlen(name));
504 fw_env_write(name, value);
509 return fw_env_close();
513 * Parse a file and configure the u-boot variables.
514 * The script file has a very simple format, as follows:
516 * Each line has a couple with name, value:
517 * <white spaces>variable_name<white spaces>variable_value
519 * Both variable_name and variable_value are interpreted as strings.
520 * Any character after <white spaces> and before ending \r\n is interpreted
521 * as variable's value (no comment allowed on these lines !)
523 * Comments are allowed if the first character in the line is #
525 * Returns -1 and sets errno error codes:
529 int fw_parse_script(char *fname)
532 char dump[1024]; /* Maximum line length in the file */
540 fprintf(stderr, "Error: environment not initialized\n");
544 if (strcmp(fname, "-") == 0)
547 fp = fopen(fname, "r");
549 fprintf(stderr, "I cannot open %s for reading\n",
555 while (fgets(dump, sizeof(dump), fp)) {
560 * Read a whole line from the file. If the line is too long
561 * or is not terminated, reports an error and exit.
563 if (dump[len - 1] != '\n') {
565 "Line %d not corrected terminated or too long\n",
571 /* Drop ending line feed / carriage return */
572 while (len > 0 && (dump[len - 1] == '\n' ||
573 dump[len - 1] == '\r')) {
574 dump[len - 1] = '\0';
578 /* Skip comment or empty lines */
579 if ((len == 0) || dump[0] == '#')
583 * Search for variable's name,
584 * remove leading whitespaces
586 name = fw_string_blank(dump, 1);
590 /* The first white space is the end of variable name */
591 val = fw_string_blank(name, 0);
595 if ((val - name) < len)
596 val = fw_string_blank(val, 1);
602 fprintf(stderr, "Setting %s : %s\n",
603 name, val ? val : " removed");
607 * If there is an error setting a variable,
608 * try to save the environment and returns an error
610 if (fw_env_write(name, val)) {
612 "fw_env_write returns with error : %s\n",
620 /* Close file if not stdin */
621 if (strcmp(fname, "-") != 0)
624 ret |= fw_env_close();
631 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
634 * < 0 - failed to test
636 static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
638 if (mtd_type == MTD_NANDFLASH) {
639 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
642 perror ("Cannot read bad block mark");
648 fprintf (stderr, "Bad block at 0x%llx, "
649 "skipping\n", *blockstart);
659 * Read data from flash at an offset into a provided buffer. On NAND it skips
660 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
661 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
663 static int flash_read_buf (int dev, int fd, void *buf, size_t count,
664 off_t offset, uint8_t mtd_type)
666 size_t blocklen; /* erase / write length - one block on NAND,
668 size_t processed = 0; /* progress counter */
669 size_t readlen = count; /* current read length */
670 off_t top_of_range; /* end of the last block we may use */
671 off_t block_seek; /* offset inside the current block to the start
673 loff_t blockstart; /* running start of the current block -
674 MEMGETBADBLOCK needs 64 bits */
678 * Start of the first block to be read, relies on the fact, that
679 * erase sector size is always a power of 2
681 blockstart = offset & ~(DEVESIZE (dev) - 1);
683 /* Offset inside a block */
684 block_seek = offset - blockstart;
686 if (mtd_type == MTD_NANDFLASH) {
688 * NAND: calculate which blocks we are reading. We have
689 * to read one block at a time to skip bad blocks.
691 blocklen = DEVESIZE (dev);
694 * To calculate the top of the range, we have to use the
695 * global DEVOFFSET (dev), which can be different from offset
697 top_of_range = (DEVOFFSET (dev) & ~(blocklen - 1)) +
698 ENVSECTORS (dev) * blocklen;
700 /* Limit to one block for the first read */
701 if (readlen > blocklen - block_seek)
702 readlen = blocklen - block_seek;
705 top_of_range = offset + count;
708 /* This only runs once on NOR flash */
709 while (processed < count) {
710 rc = flash_bad_block (fd, mtd_type, &blockstart);
711 if (rc < 0) /* block test failed */
714 if (blockstart + block_seek + readlen > top_of_range) {
715 /* End of range is reached */
717 "Too few good blocks within range\n");
721 if (rc) { /* block is bad */
722 blockstart += blocklen;
727 * If a block is bad, we retry in the next block at the same
728 * offset - see common/env_nand.c::writeenv()
730 lseek (fd, blockstart + block_seek, SEEK_SET);
732 rc = read (fd, buf + processed, readlen);
734 fprintf (stderr, "Read error on %s: %s\n",
735 DEVNAME (dev), strerror (errno));
739 fprintf (stderr, "Read 0x%x bytes at 0x%llx\n",
740 rc, blockstart + block_seek);
742 processed += readlen;
743 readlen = min (blocklen, count - processed);
745 blockstart += blocklen;
752 * Write count bytes at offset, but stay within ENVSETCORS (dev) sectors of
753 * DEVOFFSET (dev). Similar to the read case above, on NOR we erase and write
754 * the whole data at once.
756 static int flash_write_buf (int dev, int fd, void *buf, size_t count,
757 off_t offset, uint8_t mtd_type)
760 struct erase_info_user erase;
761 size_t blocklen; /* length of NAND block / NOR erase sector */
762 size_t erase_len; /* whole area that can be erased - may include
764 size_t erasesize; /* erase / write length - one block on NAND,
766 size_t processed = 0; /* progress counter */
767 size_t write_total; /* total size to actually write - excludinig
769 off_t erase_offset; /* offset to the first erase block (aligned)
771 off_t block_seek; /* offset inside the erase block to the start
773 off_t top_of_range; /* end of the last block we may use */
774 loff_t blockstart; /* running start of the current block -
775 MEMGETBADBLOCK needs 64 bits */
778 blocklen = DEVESIZE (dev);
780 /* Erase sector size is always a power of 2 */
781 top_of_range = (DEVOFFSET (dev) & ~(blocklen - 1)) +
782 ENVSECTORS (dev) * blocklen;
784 erase_offset = offset & ~(blocklen - 1);
786 /* Maximum area we may use */
787 erase_len = top_of_range - erase_offset;
789 blockstart = erase_offset;
790 /* Offset inside a block */
791 block_seek = offset - erase_offset;
794 * Data size we actually have to write: from the start of the block
795 * to the start of the data, then count bytes of data, and to the
798 write_total = (block_seek + count + blocklen - 1) & ~(blocklen - 1);
801 * Support data anywhere within erase sectors: read out the complete
802 * area to be erased, replace the environment image, write the whole
805 if (write_total > count) {
806 data = malloc (erase_len);
809 "Cannot malloc %u bytes: %s\n",
810 erase_len, strerror (errno));
814 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
816 if (write_total != rc)
819 /* Overwrite the old environment */
820 memcpy (data + block_seek, buf, count);
823 * We get here, iff offset is block-aligned and count is a
824 * multiple of blocklen - see write_total calculation above
829 if (mtd_type == MTD_NANDFLASH) {
831 * NAND: calculate which blocks we are writing. We have
832 * to write one block at a time to skip bad blocks.
834 erasesize = blocklen;
836 erasesize = erase_len;
839 erase.length = erasesize;
841 /* This only runs once on NOR flash */
842 while (processed < write_total) {
843 rc = flash_bad_block (fd, mtd_type, &blockstart);
844 if (rc < 0) /* block test failed */
847 if (blockstart + erasesize > top_of_range) {
848 fprintf (stderr, "End of range reached, aborting\n");
852 if (rc) { /* block is bad */
853 blockstart += blocklen;
857 erase.start = blockstart;
858 ioctl (fd, MEMUNLOCK, &erase);
860 if (ioctl (fd, MEMERASE, &erase) != 0) {
861 fprintf (stderr, "MTD erase error on %s: %s\n",
867 if (lseek (fd, blockstart, SEEK_SET) == -1) {
869 "Seek error on %s: %s\n",
870 DEVNAME (dev), strerror (errno));
875 printf ("Write 0x%x bytes at 0x%llx\n", erasesize, blockstart);
877 if (write (fd, data + processed, erasesize) != erasesize) {
878 fprintf (stderr, "Write error on %s: %s\n",
879 DEVNAME (dev), strerror (errno));
883 ioctl (fd, MEMLOCK, &erase);
885 processed += blocklen;
887 blockstart += blocklen;
890 if (write_total > count)
897 * Set obsolete flag at offset - NOR flash only
899 static int flash_flag_obsolete (int dev, int fd, off_t offset)
903 /* This relies on the fact, that obsolete_flag == 0 */
904 rc = lseek (fd, offset, SEEK_SET);
906 fprintf (stderr, "Cannot seek to set the flag on %s \n",
910 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
912 perror ("Could not set obsolete flag");
917 static int flash_write (int fd_current, int fd_target, int dev_target)
921 switch (environment.flag_scheme) {
924 case FLAG_INCREMENTAL:
925 (*environment.flags)++;
928 *environment.flags = active_flag;
931 fprintf (stderr, "Unimplemented flash scheme %u \n",
932 environment.flag_scheme);
937 printf ("Writing new environment at 0x%lx on %s\n",
938 DEVOFFSET (dev_target), DEVNAME (dev_target));
940 rc = flash_write_buf (dev_target, fd_target, environment.image,
941 CONFIG_ENV_SIZE, DEVOFFSET (dev_target),
942 DEVTYPE(dev_target));
946 if (environment.flag_scheme == FLAG_BOOLEAN) {
947 /* Have to set obsolete flag */
948 off_t offset = DEVOFFSET (dev_current) +
949 offsetof (struct env_image_redundant, flags);
951 printf ("Setting obsolete flag in environment at 0x%lx on %s\n",
952 DEVOFFSET (dev_current), DEVNAME (dev_current));
954 flash_flag_obsolete (dev_current, fd_current, offset);
960 static int flash_read (int fd)
962 struct mtd_info_user mtdinfo;
965 rc = ioctl (fd, MEMGETINFO, &mtdinfo);
967 perror ("Cannot get MTD information");
971 if (mtdinfo.type != MTD_NORFLASH && mtdinfo.type != MTD_NANDFLASH) {
972 fprintf (stderr, "Unsupported flash type %u\n", mtdinfo.type);
976 DEVTYPE(dev_current) = mtdinfo.type;
978 rc = flash_read_buf (dev_current, fd, environment.image, CONFIG_ENV_SIZE,
979 DEVOFFSET (dev_current), mtdinfo.type);
981 return (rc != CONFIG_ENV_SIZE) ? -1 : 0;
984 static int flash_io (int mode)
986 int fd_current, fd_target, rc, dev_target;
988 /* dev_current: fd_current, erase_current */
989 fd_current = open (DEVNAME (dev_current), mode);
990 if (fd_current < 0) {
992 "Can't open %s: %s\n",
993 DEVNAME (dev_current), strerror (errno));
997 if (mode == O_RDWR) {
999 /* switch to next partition for writing */
1000 dev_target = !dev_current;
1001 /* dev_target: fd_target, erase_target */
1002 fd_target = open (DEVNAME (dev_target), mode);
1003 if (fd_target < 0) {
1005 "Can't open %s: %s\n",
1006 DEVNAME (dev_target),
1012 dev_target = dev_current;
1013 fd_target = fd_current;
1016 rc = flash_write (fd_current, fd_target, dev_target);
1018 if (HaveRedundEnv) {
1019 if (close (fd_target)) {
1021 "I/O error on %s: %s\n",
1022 DEVNAME (dev_target),
1028 rc = flash_read (fd_current);
1032 if (close (fd_current)) {
1034 "I/O error on %s: %s\n",
1035 DEVNAME (dev_current), strerror (errno));
1043 * s1 is either a simple 'name', or a 'name=value' pair.
1044 * s2 is a 'name=value' pair.
1045 * If the names match, return the value of s2, else NULL.
1048 static char *envmatch (char * s1, char * s2)
1051 while (*s1 == *s2++)
1054 if (*s1 == '\0' && *(s2 - 1) == '=')
1060 * Prevent confusion if running from erased flash memory
1062 int fw_env_open(void)
1072 struct env_image_single *single;
1073 struct env_image_redundant *redundant;
1075 if (parse_config ()) /* should fill envdevices */
1078 addr0 = calloc (1, CONFIG_ENV_SIZE);
1079 if (addr0 == NULL) {
1081 "Not enough memory for environment (%ld bytes)\n",
1086 /* read environment from FLASH to local buffer */
1087 environment.image = addr0;
1089 if (HaveRedundEnv) {
1091 environment.crc = &redundant->crc;
1092 environment.flags = &redundant->flags;
1093 environment.data = redundant->data;
1096 environment.crc = &single->crc;
1097 environment.flags = NULL;
1098 environment.data = single->data;
1102 if (flash_io (O_RDONLY))
1105 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1106 crc0_ok = (crc0 == *environment.crc);
1107 if (!HaveRedundEnv) {
1110 "Warning: Bad CRC, using default environment\n");
1111 memcpy(environment.data, default_environment, sizeof default_environment);
1114 flag0 = *environment.flags;
1117 addr1 = calloc (1, CONFIG_ENV_SIZE);
1118 if (addr1 == NULL) {
1120 "Not enough memory for environment (%ld bytes)\n",
1127 * have to set environment.image for flash_read(), careful -
1128 * other pointers in environment still point inside addr0
1130 environment.image = addr1;
1131 if (flash_io (O_RDONLY))
1134 /* Check flag scheme compatibility */
1135 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1136 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1137 environment.flag_scheme = FLAG_BOOLEAN;
1138 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1139 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1140 environment.flag_scheme = FLAG_INCREMENTAL;
1142 fprintf (stderr, "Incompatible flash types!\n");
1146 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1147 crc1_ok = (crc1 == redundant->crc);
1148 flag1 = redundant->flags;
1150 if (crc0_ok && !crc1_ok) {
1152 } else if (!crc0_ok && crc1_ok) {
1154 } else if (!crc0_ok && !crc1_ok) {
1156 "Warning: Bad CRC, using default environment\n");
1157 memcpy (environment.data, default_environment,
1158 sizeof default_environment);
1161 switch (environment.flag_scheme) {
1163 if (flag0 == active_flag &&
1164 flag1 == obsolete_flag) {
1166 } else if (flag0 == obsolete_flag &&
1167 flag1 == active_flag) {
1169 } else if (flag0 == flag1) {
1171 } else if (flag0 == 0xFF) {
1173 } else if (flag1 == 0xFF) {
1179 case FLAG_INCREMENTAL:
1180 if ((flag0 == 255 && flag1 == 0) ||
1183 else if ((flag1 == 255 && flag0 == 0) ||
1186 else /* flags are equal - almost impossible */
1190 fprintf (stderr, "Unknown flag scheme %u \n",
1191 environment.flag_scheme);
1197 * If we are reading, we don't need the flag and the CRC any
1198 * more, if we are writing, we will re-calculate CRC and update
1199 * flags before writing out
1202 environment.image = addr1;
1203 environment.crc = &redundant->crc;
1204 environment.flags = &redundant->flags;
1205 environment.data = redundant->data;
1208 environment.image = addr0;
1209 /* Other pointers are already set */
1217 static int parse_config ()
1221 #if defined(CONFIG_FILE)
1222 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
1223 if (get_config (CONFIG_FILE)) {
1225 "Cannot parse config file: %s\n", strerror (errno));
1229 strcpy (DEVNAME (0), DEVICE1_NAME);
1230 DEVOFFSET (0) = DEVICE1_OFFSET;
1231 ENVSIZE (0) = ENV1_SIZE;
1232 DEVESIZE (0) = DEVICE1_ESIZE;
1233 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
1235 strcpy (DEVNAME (1), DEVICE2_NAME);
1236 DEVOFFSET (1) = DEVICE2_OFFSET;
1237 ENVSIZE (1) = ENV2_SIZE;
1238 DEVESIZE (1) = DEVICE2_ESIZE;
1239 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
1243 if (stat (DEVNAME (0), &st)) {
1245 "Cannot access MTD device %s: %s\n",
1246 DEVNAME (0), strerror (errno));
1250 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1252 "Cannot access MTD device %s: %s\n",
1253 DEVNAME (1), strerror (errno));
1259 #if defined(CONFIG_FILE)
1260 static int get_config (char *fname)
1267 fp = fopen (fname, "r");
1271 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
1272 /* Skip incomplete conversions and comment strings */
1276 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1287 /* Default - 1 sector */
1294 HaveRedundEnv = i - 1;
1295 if (!i) { /* No valid entries found */