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,
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>
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 CONFIG_ENV_SIZE 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;
124 #define XMK_STR(x) #x
125 #define MK_STR(x) XMK_STR(x)
127 static char default_environment[] = {
128 #if defined(CONFIG_BOOTARGS)
129 "bootargs=" CONFIG_BOOTARGS "\0"
131 #if defined(CONFIG_BOOTCOMMAND)
132 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
134 #if defined(CONFIG_RAMBOOTCOMMAND)
135 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
137 #if defined(CONFIG_NFSBOOTCOMMAND)
138 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
140 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
141 "bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0"
143 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
144 "baudrate=" MK_STR (CONFIG_BAUDRATE) "\0"
146 #ifdef CONFIG_LOADS_ECHO
147 "loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0"
149 #ifdef CONFIG_ETHADDR
150 "ethaddr=" MK_STR (CONFIG_ETHADDR) "\0"
152 #ifdef CONFIG_ETH1ADDR
153 "eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0"
155 #ifdef CONFIG_ETH2ADDR
156 "eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0"
158 #ifdef CONFIG_ETH3ADDR
159 "eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0"
161 #ifdef CONFIG_ETH4ADDR
162 "eth4addr=" MK_STR (CONFIG_ETH4ADDR) "\0"
164 #ifdef CONFIG_ETH5ADDR
165 "eth5addr=" MK_STR (CONFIG_ETH5ADDR) "\0"
167 #ifdef CONFIG_ETHPRIME
168 "ethprime=" CONFIG_ETHPRIME "\0"
171 "ipaddr=" MK_STR (CONFIG_IPADDR) "\0"
173 #ifdef CONFIG_SERVERIP
174 "serverip=" MK_STR (CONFIG_SERVERIP) "\0"
176 #ifdef CONFIG_SYS_AUTOLOAD
177 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
179 #ifdef CONFIG_ROOTPATH
180 "rootpath=" CONFIG_ROOTPATH "\0"
182 #ifdef CONFIG_GATEWAYIP
183 "gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0"
185 #ifdef CONFIG_NETMASK
186 "netmask=" MK_STR (CONFIG_NETMASK) "\0"
188 #ifdef CONFIG_HOSTNAME
189 "hostname=" MK_STR (CONFIG_HOSTNAME) "\0"
191 #ifdef CONFIG_BOOTFILE
192 "bootfile=" CONFIG_BOOTFILE "\0"
194 #ifdef CONFIG_LOADADDR
195 "loadaddr=" MK_STR (CONFIG_LOADADDR) "\0"
197 #ifdef CONFIG_PREBOOT
198 "preboot=" CONFIG_PREBOOT "\0"
200 #ifdef CONFIG_CLOCKS_IN_MHZ
201 "clocks_in_mhz=" "1" "\0"
203 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
204 "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0"
206 #ifdef CONFIG_EXTRA_ENV_SETTINGS
207 CONFIG_EXTRA_ENV_SETTINGS
209 "\0" /* Termimate struct environment data with 2 NULs */
212 static int flash_io (int mode);
213 static char *envmatch (char * s1, char * s2);
214 static int parse_config (void);
216 #if defined(CONFIG_FILE)
217 static int get_config (char *);
219 static inline ulong getenvsize (void)
221 ulong rc = CONFIG_ENV_SIZE - sizeof (long);
228 static char *fw_string_blank(char *s, int noblank)
233 for (i = 0; i < len; i++, s++) {
234 if ((noblank && !WHITESPACE(*s)) ||
235 (!noblank && WHITESPACE(*s)))
245 * Search the environment for a variable.
246 * Return the value, if found, or NULL, if not found.
248 char *fw_getenv (char *name)
255 for (env = environment.data; *env; env = nxt + 1) {
258 for (nxt = env; *nxt; ++nxt) {
259 if (nxt >= &environment.data[ENV_SIZE]) {
260 fprintf (stderr, "## Error: "
261 "environment not terminated\n");
265 val = envmatch (name, env);
274 * Print the current definition of one, or more, or all
275 * environment variables
277 int fw_printenv (int argc, char *argv[])
286 if (argc == 1) { /* Print all env variables */
287 for (env = environment.data; *env; env = nxt + 1) {
288 for (nxt = env; *nxt; ++nxt) {
289 if (nxt >= &environment.data[ENV_SIZE]) {
290 fprintf (stderr, "## Error: "
291 "environment not terminated\n");
296 printf ("%s\n", env);
301 if (strcmp (argv[1], "-n") == 0) {
306 fprintf (stderr, "## Error: "
307 "`-n' option requires exactly one argument\n");
314 for (i = 1; i < argc; ++i) { /* print single env variables */
315 char *name = argv[i];
318 for (env = environment.data; *env; env = nxt + 1) {
320 for (nxt = env; *nxt; ++nxt) {
321 if (nxt >= &environment.data[ENV_SIZE]) {
322 fprintf (stderr, "## Error: "
323 "environment not terminated\n");
327 val = envmatch (name, env);
330 fputs (name, stdout);
338 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
346 int fw_env_close(void)
351 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
353 /* write environment back to flash */
354 if (flash_io(O_RDWR)) {
356 "Error: can't write fw_env to flash\n");
365 * Set/Clear a single variable in the environment.
366 * This is called in sequence to update the environment
367 * in RAM without updating the copy in flash after each set
369 int fw_env_write(char *name, char *value)
376 * search if variable with this name already exists
378 for (nxt = env = environment.data; *env; env = nxt + 1) {
379 for (nxt = env; *nxt; ++nxt) {
380 if (nxt >= &environment.data[ENV_SIZE]) {
381 fprintf(stderr, "## Error: "
382 "environment not terminated\n");
387 if ((oldval = envmatch (name, env)) != NULL)
392 * Delete any existing definition
395 #ifndef CONFIG_ENV_OVERWRITE
397 * Ethernet Address and serial# can be set only once
400 (strcmp(name, "serial#") == 0) ||
401 ((strcmp(name, "ethaddr") == 0)
402 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
403 && (strcmp(oldval, MK_STR(CONFIG_ETHADDR)) != 0)
404 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
406 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
410 #endif /* CONFIG_ENV_OVERWRITE */
412 if (*++nxt == '\0') {
417 if ((*env == '\0') && (*nxt == '\0'))
426 if (!value || !strlen(value))
430 * Append new definition at the end
432 for (env = environment.data; *env || *(env + 1); ++env);
433 if (env > environment.data)
437 * "name" + "=" + "val" +"\0\0" > CONFIG_ENV_SIZE - (env-environment)
439 len = strlen (name) + 2;
440 /* add '=' for first arg, ' ' for all others */
441 len += strlen(value) + 1;
443 if (len > (&environment.data[ENV_SIZE] - env)) {
445 "Error: environment overflow, \"%s\" deleted\n",
450 while ((*env = *name++) != '\0')
453 while ((*++env = *value++) != '\0')
456 /* end is marked with double '\0' */
463 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
465 * EINVAL - need at least 1 argument
466 * EROFS - certain variables ("ethaddr", "serial#") cannot be
467 * modified or deleted
470 int fw_setenv(int argc, char *argv[])
483 fprintf(stderr, "Error: environment not initialized\n");
489 len = strlen(name) + 2;
490 for (i = 2; i < argc; ++i)
491 len += strlen(argv[i]) + 1;
493 /* Allocate enough place to the data string */
494 for (i = 2; i < argc; ++i) {
497 value = (char *)malloc(len - strlen(name));
500 "Cannot malloc %zu bytes: %s\n",
501 len - strlen(name), strerror(errno));
504 memset(value, 0, len - strlen(name));
513 fw_env_write(name, value);
518 return fw_env_close();
522 * Parse a file and configure the u-boot variables.
523 * The script file has a very simple format, as follows:
525 * Each line has a couple with name, value:
526 * <white spaces>variable_name<white spaces>variable_value
528 * Both variable_name and variable_value are interpreted as strings.
529 * Any character after <white spaces> and before ending \r\n is interpreted
530 * as variable's value (no comment allowed on these lines !)
532 * Comments are allowed if the first character in the line is #
534 * Returns -1 and sets errno error codes:
538 int fw_parse_script(char *fname)
541 char dump[1024]; /* Maximum line length in the file */
549 fprintf(stderr, "Error: environment not initialized\n");
553 if (strcmp(fname, "-") == 0)
556 fp = fopen(fname, "r");
558 fprintf(stderr, "I cannot open %s for reading\n",
564 while (fgets(dump, sizeof(dump), fp)) {
569 * Read a whole line from the file. If the line is too long
570 * or is not terminated, reports an error and exit.
572 if (dump[len - 1] != '\n') {
574 "Line %d not corrected terminated or too long\n",
580 /* Drop ending line feed / carriage return */
581 while (len > 0 && (dump[len - 1] == '\n' ||
582 dump[len - 1] == '\r')) {
583 dump[len - 1] = '\0';
587 /* Skip comment or empty lines */
588 if ((len == 0) || dump[0] == '#')
592 * Search for variable's name,
593 * remove leading whitespaces
595 name = fw_string_blank(dump, 1);
599 /* The first white space is the end of variable name */
600 val = fw_string_blank(name, 0);
604 if ((val - name) < len)
605 val = fw_string_blank(val, 1);
611 fprintf(stderr, "Setting %s : %s\n",
612 name, val ? val : " removed");
616 * If there is an error setting a variable,
617 * try to save the environment and returns an error
619 if (fw_env_write(name, val)) {
621 "fw_env_write returns with error : %s\n",
629 /* Close file if not stdin */
630 if (strcmp(fname, "-") != 0)
633 ret |= fw_env_close();
640 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
643 * < 0 - failed to test
645 static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
647 if (mtd_type == MTD_NANDFLASH) {
648 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
651 perror ("Cannot read bad block mark");
657 fprintf (stderr, "Bad block at 0x%llx, "
658 "skipping\n", *blockstart);
668 * Read data from flash at an offset into a provided buffer. On NAND it skips
669 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
670 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
672 static int flash_read_buf (int dev, int fd, void *buf, size_t count,
673 off_t offset, uint8_t mtd_type)
675 size_t blocklen; /* erase / write length - one block on NAND,
677 size_t processed = 0; /* progress counter */
678 size_t readlen = count; /* current read length */
679 off_t top_of_range; /* end of the last block we may use */
680 off_t block_seek; /* offset inside the current block to the start
682 loff_t blockstart; /* running start of the current block -
683 MEMGETBADBLOCK needs 64 bits */
686 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
688 /* Offset inside a block */
689 block_seek = offset - blockstart;
691 if (mtd_type == MTD_NANDFLASH) {
693 * NAND: calculate which blocks we are reading. We have
694 * to read one block at a time to skip bad blocks.
696 blocklen = DEVESIZE (dev);
699 * To calculate the top of the range, we have to use the
700 * global DEVOFFSET (dev), which can be different from offset
702 top_of_range = ((DEVOFFSET(dev) / blocklen) +
703 ENVSECTORS (dev)) * blocklen;
705 /* Limit to one block for the first read */
706 if (readlen > blocklen - block_seek)
707 readlen = blocklen - block_seek;
710 top_of_range = offset + count;
713 /* This only runs once on NOR flash */
714 while (processed < count) {
715 rc = flash_bad_block (fd, mtd_type, &blockstart);
716 if (rc < 0) /* block test failed */
719 if (blockstart + block_seek + readlen > top_of_range) {
720 /* End of range is reached */
722 "Too few good blocks within range\n");
726 if (rc) { /* block is bad */
727 blockstart += blocklen;
732 * If a block is bad, we retry in the next block at the same
733 * offset - see common/env_nand.c::writeenv()
735 lseek (fd, blockstart + block_seek, SEEK_SET);
737 rc = read (fd, buf + processed, readlen);
739 fprintf (stderr, "Read error on %s: %s\n",
740 DEVNAME (dev), strerror (errno));
744 fprintf (stderr, "Read 0x%x bytes at 0x%llx\n",
745 rc, blockstart + block_seek);
747 processed += readlen;
748 readlen = min (blocklen, count - processed);
750 blockstart += blocklen;
757 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
758 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
759 * erase and write the whole data at once.
761 static int flash_write_buf (int dev, int fd, void *buf, size_t count,
762 off_t offset, uint8_t mtd_type)
765 struct erase_info_user erase;
766 size_t blocklen; /* length of NAND block / NOR erase sector */
767 size_t erase_len; /* whole area that can be erased - may include
769 size_t erasesize; /* erase / write length - one block on NAND,
771 size_t processed = 0; /* progress counter */
772 size_t write_total; /* total size to actually write - excluding
774 off_t erase_offset; /* offset to the first erase block (aligned)
776 off_t block_seek; /* offset inside the erase block to the start
778 off_t top_of_range; /* end of the last block we may use */
779 loff_t blockstart; /* running start of the current block -
780 MEMGETBADBLOCK needs 64 bits */
783 blocklen = DEVESIZE (dev);
785 top_of_range = ((DEVOFFSET(dev) / blocklen) +
786 ENVSECTORS (dev)) * blocklen;
788 erase_offset = (offset / blocklen) * blocklen;
790 /* Maximum area we may use */
791 erase_len = top_of_range - erase_offset;
793 blockstart = erase_offset;
794 /* Offset inside a block */
795 block_seek = offset - erase_offset;
798 * Data size we actually have to write: from the start of the block
799 * to the start of the data, then count bytes of data, and to the
802 write_total = ((block_seek + count + blocklen - 1) /
803 blocklen) * blocklen;
806 * Support data anywhere within erase sectors: read out the complete
807 * area to be erased, replace the environment image, write the whole
810 if (write_total > count) {
811 data = malloc (erase_len);
814 "Cannot malloc %zu bytes: %s\n",
815 erase_len, strerror (errno));
819 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
821 if (write_total != rc)
824 /* Overwrite the old environment */
825 memcpy (data + block_seek, buf, count);
828 * We get here, iff offset is block-aligned and count is a
829 * multiple of blocklen - see write_total calculation above
834 if (mtd_type == MTD_NANDFLASH) {
836 * NAND: calculate which blocks we are writing. We have
837 * to write one block at a time to skip bad blocks.
839 erasesize = blocklen;
841 erasesize = erase_len;
844 erase.length = erasesize;
846 /* This only runs once on NOR flash and SPI-dataflash */
847 while (processed < write_total) {
848 rc = flash_bad_block (fd, mtd_type, &blockstart);
849 if (rc < 0) /* block test failed */
852 if (blockstart + erasesize > top_of_range) {
853 fprintf (stderr, "End of range reached, aborting\n");
857 if (rc) { /* block is bad */
858 blockstart += blocklen;
862 erase.start = blockstart;
863 ioctl (fd, MEMUNLOCK, &erase);
865 /* Dataflash does not need an explicit erase cycle */
866 if (mtd_type != MTD_DATAFLASH)
867 if (ioctl (fd, MEMERASE, &erase) != 0) {
868 fprintf (stderr, "MTD erase error on %s: %s\n",
874 if (lseek (fd, blockstart, SEEK_SET) == -1) {
876 "Seek error on %s: %s\n",
877 DEVNAME (dev), strerror (errno));
882 printf ("Write 0x%x bytes at 0x%llx\n", erasesize, blockstart);
884 if (write (fd, data + processed, erasesize) != erasesize) {
885 fprintf (stderr, "Write error on %s: %s\n",
886 DEVNAME (dev), strerror (errno));
890 ioctl (fd, MEMLOCK, &erase);
892 processed += blocklen;
894 blockstart += blocklen;
897 if (write_total > count)
904 * Set obsolete flag at offset - NOR flash only
906 static int flash_flag_obsolete (int dev, int fd, off_t offset)
909 struct erase_info_user erase;
911 erase.start = DEVOFFSET (dev);
912 erase.length = DEVESIZE (dev);
913 /* This relies on the fact, that obsolete_flag == 0 */
914 rc = lseek (fd, offset, SEEK_SET);
916 fprintf (stderr, "Cannot seek to set the flag on %s \n",
920 ioctl (fd, MEMUNLOCK, &erase);
921 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
922 ioctl (fd, MEMLOCK, &erase);
924 perror ("Could not set obsolete flag");
929 static int flash_write (int fd_current, int fd_target, int dev_target)
933 switch (environment.flag_scheme) {
936 case FLAG_INCREMENTAL:
937 (*environment.flags)++;
940 *environment.flags = active_flag;
943 fprintf (stderr, "Unimplemented flash scheme %u \n",
944 environment.flag_scheme);
949 printf ("Writing new environment at 0x%lx on %s\n",
950 DEVOFFSET (dev_target), DEVNAME (dev_target));
952 rc = flash_write_buf (dev_target, fd_target, environment.image,
953 CONFIG_ENV_SIZE, DEVOFFSET (dev_target),
954 DEVTYPE(dev_target));
958 if (environment.flag_scheme == FLAG_BOOLEAN) {
959 /* Have to set obsolete flag */
960 off_t offset = DEVOFFSET (dev_current) +
961 offsetof (struct env_image_redundant, flags);
963 printf ("Setting obsolete flag in environment at 0x%lx on %s\n",
964 DEVOFFSET (dev_current), DEVNAME (dev_current));
966 flash_flag_obsolete (dev_current, fd_current, offset);
972 static int flash_read (int fd)
974 struct mtd_info_user mtdinfo;
977 rc = ioctl (fd, MEMGETINFO, &mtdinfo);
979 perror ("Cannot get MTD information");
983 if (mtdinfo.type != MTD_NORFLASH &&
984 mtdinfo.type != MTD_NANDFLASH &&
985 mtdinfo.type != MTD_DATAFLASH) {
986 fprintf (stderr, "Unsupported flash type %u\n", mtdinfo.type);
990 DEVTYPE(dev_current) = mtdinfo.type;
992 rc = flash_read_buf (dev_current, fd, environment.image, CONFIG_ENV_SIZE,
993 DEVOFFSET (dev_current), mtdinfo.type);
995 return (rc != CONFIG_ENV_SIZE) ? -1 : 0;
998 static int flash_io (int mode)
1000 int fd_current, fd_target, rc, dev_target;
1002 /* dev_current: fd_current, erase_current */
1003 fd_current = open (DEVNAME (dev_current), mode);
1004 if (fd_current < 0) {
1006 "Can't open %s: %s\n",
1007 DEVNAME (dev_current), strerror (errno));
1011 if (mode == O_RDWR) {
1012 if (HaveRedundEnv) {
1013 /* switch to next partition for writing */
1014 dev_target = !dev_current;
1015 /* dev_target: fd_target, erase_target */
1016 fd_target = open (DEVNAME (dev_target), mode);
1017 if (fd_target < 0) {
1019 "Can't open %s: %s\n",
1020 DEVNAME (dev_target),
1026 dev_target = dev_current;
1027 fd_target = fd_current;
1030 rc = flash_write (fd_current, fd_target, dev_target);
1032 if (HaveRedundEnv) {
1033 if (close (fd_target)) {
1035 "I/O error on %s: %s\n",
1036 DEVNAME (dev_target),
1042 rc = flash_read (fd_current);
1046 if (close (fd_current)) {
1048 "I/O error on %s: %s\n",
1049 DEVNAME (dev_current), strerror (errno));
1057 * s1 is either a simple 'name', or a 'name=value' pair.
1058 * s2 is a 'name=value' pair.
1059 * If the names match, return the value of s2, else NULL.
1062 static char *envmatch (char * s1, char * s2)
1065 while (*s1 == *s2++)
1068 if (*s1 == '\0' && *(s2 - 1) == '=')
1074 * Prevent confusion if running from erased flash memory
1076 int fw_env_open(void)
1079 unsigned char flag0;
1083 unsigned char flag1;
1086 struct env_image_single *single;
1087 struct env_image_redundant *redundant;
1089 if (parse_config ()) /* should fill envdevices */
1092 addr0 = calloc (1, CONFIG_ENV_SIZE);
1093 if (addr0 == NULL) {
1095 "Not enough memory for environment (%ld bytes)\n",
1100 /* read environment from FLASH to local buffer */
1101 environment.image = addr0;
1103 if (HaveRedundEnv) {
1105 environment.crc = &redundant->crc;
1106 environment.flags = &redundant->flags;
1107 environment.data = redundant->data;
1110 environment.crc = &single->crc;
1111 environment.flags = NULL;
1112 environment.data = single->data;
1116 if (flash_io (O_RDONLY))
1119 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1120 crc0_ok = (crc0 == *environment.crc);
1121 if (!HaveRedundEnv) {
1124 "Warning: Bad CRC, using default environment\n");
1125 memcpy(environment.data, default_environment, sizeof default_environment);
1128 flag0 = *environment.flags;
1131 addr1 = calloc (1, CONFIG_ENV_SIZE);
1132 if (addr1 == NULL) {
1134 "Not enough memory for environment (%ld bytes)\n",
1141 * have to set environment.image for flash_read(), careful -
1142 * other pointers in environment still point inside addr0
1144 environment.image = addr1;
1145 if (flash_io (O_RDONLY))
1148 /* Check flag scheme compatibility */
1149 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1150 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1151 environment.flag_scheme = FLAG_BOOLEAN;
1152 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1153 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1154 environment.flag_scheme = FLAG_INCREMENTAL;
1155 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1156 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1157 environment.flag_scheme = FLAG_BOOLEAN;
1159 fprintf (stderr, "Incompatible flash types!\n");
1163 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1164 crc1_ok = (crc1 == redundant->crc);
1165 flag1 = redundant->flags;
1167 if (crc0_ok && !crc1_ok) {
1169 } else if (!crc0_ok && crc1_ok) {
1171 } else if (!crc0_ok && !crc1_ok) {
1173 "Warning: Bad CRC, using default environment\n");
1174 memcpy (environment.data, default_environment,
1175 sizeof default_environment);
1178 switch (environment.flag_scheme) {
1180 if (flag0 == active_flag &&
1181 flag1 == obsolete_flag) {
1183 } else if (flag0 == obsolete_flag &&
1184 flag1 == active_flag) {
1186 } else if (flag0 == flag1) {
1188 } else if (flag0 == 0xFF) {
1190 } else if (flag1 == 0xFF) {
1196 case FLAG_INCREMENTAL:
1197 if (flag0 == 255 && flag1 == 0)
1199 else if ((flag1 == 255 && flag0 == 0) ||
1202 else /* flag1 > flag0 */
1206 fprintf (stderr, "Unknown flag scheme %u \n",
1207 environment.flag_scheme);
1213 * If we are reading, we don't need the flag and the CRC any
1214 * more, if we are writing, we will re-calculate CRC and update
1215 * flags before writing out
1218 environment.image = addr1;
1219 environment.crc = &redundant->crc;
1220 environment.flags = &redundant->flags;
1221 environment.data = redundant->data;
1224 environment.image = addr0;
1225 /* Other pointers are already set */
1233 static int parse_config ()
1237 #if defined(CONFIG_FILE)
1238 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
1239 if (get_config (CONFIG_FILE)) {
1241 "Cannot parse config file: %s\n", strerror (errno));
1245 strcpy (DEVNAME (0), DEVICE1_NAME);
1246 DEVOFFSET (0) = DEVICE1_OFFSET;
1247 ENVSIZE (0) = ENV1_SIZE;
1248 /* Default values are: erase-size=env-size, #sectors=1 */
1249 DEVESIZE (0) = ENVSIZE (0);
1251 #ifdef DEVICE1_ESIZE
1252 DEVESIZE (0) = DEVICE1_ESIZE;
1254 #ifdef DEVICE1_ENVSECTORS
1255 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
1259 strcpy (DEVNAME (1), DEVICE2_NAME);
1260 DEVOFFSET (1) = DEVICE2_OFFSET;
1261 ENVSIZE (1) = ENV2_SIZE;
1262 /* Default values are: erase-size=env-size, #sectors=1 */
1263 DEVESIZE (1) = ENVSIZE (1);
1265 #ifdef DEVICE2_ESIZE
1266 DEVESIZE (1) = DEVICE2_ESIZE;
1268 #ifdef DEVICE2_ENVSECTORS
1269 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
1274 if (stat (DEVNAME (0), &st)) {
1276 "Cannot access MTD device %s: %s\n",
1277 DEVNAME (0), strerror (errno));
1281 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1283 "Cannot access MTD device %s: %s\n",
1284 DEVNAME (1), strerror (errno));
1290 #if defined(CONFIG_FILE)
1291 static int get_config (char *fname)
1298 fp = fopen (fname, "r");
1302 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
1303 /* Skip incomplete conversions and comment strings */
1307 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1318 /* Assume the erase size is the same as the env-size */
1319 DEVESIZE(i) = ENVSIZE(i);
1322 /* Default - 1 sector */
1329 HaveRedundEnv = i - 1;
1330 if (!i) { /* No valid entries found */