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_ENV_VARS_UBOOT_CONFIG
207 "arch=" CONFIG_SYS_ARCH "\0"
208 "cpu=" CONFIG_SYS_CPU "\0"
209 "board=" CONFIG_SYS_BOARD "\0"
210 #ifdef CONFIG_SYS_VENDOR
211 "vendor=" CONFIG_SYS_VENDOR "\0"
213 #ifdef CONFIG_SYS_SOC
214 "soc=" CONFIG_SYS_SOC "\0"
217 #ifdef CONFIG_EXTRA_ENV_SETTINGS
218 CONFIG_EXTRA_ENV_SETTINGS
220 "\0" /* Termimate struct environment data with 2 NULs */
223 static int flash_io (int mode);
224 static char *envmatch (char * s1, char * s2);
225 static int parse_config (void);
227 #if defined(CONFIG_FILE)
228 static int get_config (char *);
230 static inline ulong getenvsize (void)
232 ulong rc = CONFIG_ENV_SIZE - sizeof (long);
239 static char *fw_string_blank(char *s, int noblank)
244 for (i = 0; i < len; i++, s++) {
245 if ((noblank && !WHITESPACE(*s)) ||
246 (!noblank && WHITESPACE(*s)))
256 * Search the environment for a variable.
257 * Return the value, if found, or NULL, if not found.
259 char *fw_getenv (char *name)
266 for (env = environment.data; *env; env = nxt + 1) {
269 for (nxt = env; *nxt; ++nxt) {
270 if (nxt >= &environment.data[ENV_SIZE]) {
271 fprintf (stderr, "## Error: "
272 "environment not terminated\n");
276 val = envmatch (name, env);
285 * Print the current definition of one, or more, or all
286 * environment variables
288 int fw_printenv (int argc, char *argv[])
297 if (argc == 1) { /* Print all env variables */
298 for (env = environment.data; *env; env = nxt + 1) {
299 for (nxt = env; *nxt; ++nxt) {
300 if (nxt >= &environment.data[ENV_SIZE]) {
301 fprintf (stderr, "## Error: "
302 "environment not terminated\n");
307 printf ("%s\n", env);
312 if (strcmp (argv[1], "-n") == 0) {
317 fprintf (stderr, "## Error: "
318 "`-n' option requires exactly one argument\n");
325 for (i = 1; i < argc; ++i) { /* print single env variables */
326 char *name = argv[i];
329 for (env = environment.data; *env; env = nxt + 1) {
331 for (nxt = env; *nxt; ++nxt) {
332 if (nxt >= &environment.data[ENV_SIZE]) {
333 fprintf (stderr, "## Error: "
334 "environment not terminated\n");
338 val = envmatch (name, env);
341 fputs (name, stdout);
349 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
357 int fw_env_close(void)
362 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
364 /* write environment back to flash */
365 if (flash_io(O_RDWR)) {
367 "Error: can't write fw_env to flash\n");
376 * Set/Clear a single variable in the environment.
377 * This is called in sequence to update the environment
378 * in RAM without updating the copy in flash after each set
380 int fw_env_write(char *name, char *value)
387 * search if variable with this name already exists
389 for (nxt = env = environment.data; *env; env = nxt + 1) {
390 for (nxt = env; *nxt; ++nxt) {
391 if (nxt >= &environment.data[ENV_SIZE]) {
392 fprintf(stderr, "## Error: "
393 "environment not terminated\n");
398 if ((oldval = envmatch (name, env)) != NULL)
403 * Delete any existing definition
406 #ifndef CONFIG_ENV_OVERWRITE
408 * Ethernet Address and serial# can be set only once
411 (strcmp(name, "serial#") == 0) ||
412 ((strcmp(name, "ethaddr") == 0)
413 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
414 && (strcmp(oldval, MK_STR(CONFIG_ETHADDR)) != 0)
415 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
417 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
421 #endif /* CONFIG_ENV_OVERWRITE */
423 if (*++nxt == '\0') {
428 if ((*env == '\0') && (*nxt == '\0'))
437 if (!value || !strlen(value))
441 * Append new definition at the end
443 for (env = environment.data; *env || *(env + 1); ++env);
444 if (env > environment.data)
448 * "name" + "=" + "val" +"\0\0" > CONFIG_ENV_SIZE - (env-environment)
450 len = strlen (name) + 2;
451 /* add '=' for first arg, ' ' for all others */
452 len += strlen(value) + 1;
454 if (len > (&environment.data[ENV_SIZE] - env)) {
456 "Error: environment overflow, \"%s\" deleted\n",
461 while ((*env = *name++) != '\0')
464 while ((*++env = *value++) != '\0')
467 /* end is marked with double '\0' */
474 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
476 * EINVAL - need at least 1 argument
477 * EROFS - certain variables ("ethaddr", "serial#") cannot be
478 * modified or deleted
481 int fw_setenv(int argc, char *argv[])
494 fprintf(stderr, "Error: environment not initialized\n");
500 len = strlen(name) + 2;
501 for (i = 2; i < argc; ++i)
502 len += strlen(argv[i]) + 1;
504 /* Allocate enough place to the data string */
505 for (i = 2; i < argc; ++i) {
508 value = (char *)malloc(len - strlen(name));
511 "Cannot malloc %zu bytes: %s\n",
512 len - strlen(name), strerror(errno));
515 memset(value, 0, len - strlen(name));
524 fw_env_write(name, value);
529 return fw_env_close();
533 * Parse a file and configure the u-boot variables.
534 * The script file has a very simple format, as follows:
536 * Each line has a couple with name, value:
537 * <white spaces>variable_name<white spaces>variable_value
539 * Both variable_name and variable_value are interpreted as strings.
540 * Any character after <white spaces> and before ending \r\n is interpreted
541 * as variable's value (no comment allowed on these lines !)
543 * Comments are allowed if the first character in the line is #
545 * Returns -1 and sets errno error codes:
549 int fw_parse_script(char *fname)
552 char dump[1024]; /* Maximum line length in the file */
560 fprintf(stderr, "Error: environment not initialized\n");
564 if (strcmp(fname, "-") == 0)
567 fp = fopen(fname, "r");
569 fprintf(stderr, "I cannot open %s for reading\n",
575 while (fgets(dump, sizeof(dump), fp)) {
580 * Read a whole line from the file. If the line is too long
581 * or is not terminated, reports an error and exit.
583 if (dump[len - 1] != '\n') {
585 "Line %d not corrected terminated or too long\n",
591 /* Drop ending line feed / carriage return */
592 while (len > 0 && (dump[len - 1] == '\n' ||
593 dump[len - 1] == '\r')) {
594 dump[len - 1] = '\0';
598 /* Skip comment or empty lines */
599 if ((len == 0) || dump[0] == '#')
603 * Search for variable's name,
604 * remove leading whitespaces
606 name = fw_string_blank(dump, 1);
610 /* The first white space is the end of variable name */
611 val = fw_string_blank(name, 0);
615 if ((val - name) < len)
616 val = fw_string_blank(val, 1);
622 fprintf(stderr, "Setting %s : %s\n",
623 name, val ? val : " removed");
627 * If there is an error setting a variable,
628 * try to save the environment and returns an error
630 if (fw_env_write(name, val)) {
632 "fw_env_write returns with error : %s\n",
640 /* Close file if not stdin */
641 if (strcmp(fname, "-") != 0)
644 ret |= fw_env_close();
651 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
654 * < 0 - failed to test
656 static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
658 if (mtd_type == MTD_NANDFLASH) {
659 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
662 perror ("Cannot read bad block mark");
668 fprintf (stderr, "Bad block at 0x%llx, "
669 "skipping\n", *blockstart);
679 * Read data from flash at an offset into a provided buffer. On NAND it skips
680 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
681 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
683 static int flash_read_buf (int dev, int fd, void *buf, size_t count,
684 off_t offset, uint8_t mtd_type)
686 size_t blocklen; /* erase / write length - one block on NAND,
688 size_t processed = 0; /* progress counter */
689 size_t readlen = count; /* current read length */
690 off_t top_of_range; /* end of the last block we may use */
691 off_t block_seek; /* offset inside the current block to the start
693 loff_t blockstart; /* running start of the current block -
694 MEMGETBADBLOCK needs 64 bits */
697 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
699 /* Offset inside a block */
700 block_seek = offset - blockstart;
702 if (mtd_type == MTD_NANDFLASH) {
704 * NAND: calculate which blocks we are reading. We have
705 * to read one block at a time to skip bad blocks.
707 blocklen = DEVESIZE (dev);
710 * To calculate the top of the range, we have to use the
711 * global DEVOFFSET (dev), which can be different from offset
713 top_of_range = ((DEVOFFSET(dev) / blocklen) +
714 ENVSECTORS (dev)) * blocklen;
716 /* Limit to one block for the first read */
717 if (readlen > blocklen - block_seek)
718 readlen = blocklen - block_seek;
721 top_of_range = offset + count;
724 /* This only runs once on NOR flash */
725 while (processed < count) {
726 rc = flash_bad_block (fd, mtd_type, &blockstart);
727 if (rc < 0) /* block test failed */
730 if (blockstart + block_seek + readlen > top_of_range) {
731 /* End of range is reached */
733 "Too few good blocks within range\n");
737 if (rc) { /* block is bad */
738 blockstart += blocklen;
743 * If a block is bad, we retry in the next block at the same
744 * offset - see common/env_nand.c::writeenv()
746 lseek (fd, blockstart + block_seek, SEEK_SET);
748 rc = read (fd, buf + processed, readlen);
750 fprintf (stderr, "Read error on %s: %s\n",
751 DEVNAME (dev), strerror (errno));
755 fprintf (stderr, "Read 0x%x bytes at 0x%llx\n",
756 rc, blockstart + block_seek);
758 processed += readlen;
759 readlen = min (blocklen, count - processed);
761 blockstart += blocklen;
768 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
769 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
770 * erase and write the whole data at once.
772 static int flash_write_buf (int dev, int fd, void *buf, size_t count,
773 off_t offset, uint8_t mtd_type)
776 struct erase_info_user erase;
777 size_t blocklen; /* length of NAND block / NOR erase sector */
778 size_t erase_len; /* whole area that can be erased - may include
780 size_t erasesize; /* erase / write length - one block on NAND,
782 size_t processed = 0; /* progress counter */
783 size_t write_total; /* total size to actually write - excluding
785 off_t erase_offset; /* offset to the first erase block (aligned)
787 off_t block_seek; /* offset inside the erase block to the start
789 off_t top_of_range; /* end of the last block we may use */
790 loff_t blockstart; /* running start of the current block -
791 MEMGETBADBLOCK needs 64 bits */
794 blocklen = DEVESIZE (dev);
796 top_of_range = ((DEVOFFSET(dev) / blocklen) +
797 ENVSECTORS (dev)) * blocklen;
799 erase_offset = (offset / blocklen) * blocklen;
801 /* Maximum area we may use */
802 erase_len = top_of_range - erase_offset;
804 blockstart = erase_offset;
805 /* Offset inside a block */
806 block_seek = offset - erase_offset;
809 * Data size we actually have to write: from the start of the block
810 * to the start of the data, then count bytes of data, and to the
813 write_total = ((block_seek + count + blocklen - 1) /
814 blocklen) * blocklen;
817 * Support data anywhere within erase sectors: read out the complete
818 * area to be erased, replace the environment image, write the whole
821 if (write_total > count) {
822 data = malloc (erase_len);
825 "Cannot malloc %zu bytes: %s\n",
826 erase_len, strerror (errno));
830 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
832 if (write_total != rc)
835 /* Overwrite the old environment */
836 memcpy (data + block_seek, buf, count);
839 * We get here, iff offset is block-aligned and count is a
840 * multiple of blocklen - see write_total calculation above
845 if (mtd_type == MTD_NANDFLASH) {
847 * NAND: calculate which blocks we are writing. We have
848 * to write one block at a time to skip bad blocks.
850 erasesize = blocklen;
852 erasesize = erase_len;
855 erase.length = erasesize;
857 /* This only runs once on NOR flash and SPI-dataflash */
858 while (processed < write_total) {
859 rc = flash_bad_block (fd, mtd_type, &blockstart);
860 if (rc < 0) /* block test failed */
863 if (blockstart + erasesize > top_of_range) {
864 fprintf (stderr, "End of range reached, aborting\n");
868 if (rc) { /* block is bad */
869 blockstart += blocklen;
873 erase.start = blockstart;
874 ioctl (fd, MEMUNLOCK, &erase);
876 /* Dataflash does not need an explicit erase cycle */
877 if (mtd_type != MTD_DATAFLASH)
878 if (ioctl (fd, MEMERASE, &erase) != 0) {
879 fprintf (stderr, "MTD erase error on %s: %s\n",
885 if (lseek (fd, blockstart, SEEK_SET) == -1) {
887 "Seek error on %s: %s\n",
888 DEVNAME (dev), strerror (errno));
893 printf ("Write 0x%x bytes at 0x%llx\n", erasesize, blockstart);
895 if (write (fd, data + processed, erasesize) != erasesize) {
896 fprintf (stderr, "Write error on %s: %s\n",
897 DEVNAME (dev), strerror (errno));
901 ioctl (fd, MEMLOCK, &erase);
903 processed += blocklen;
905 blockstart += blocklen;
908 if (write_total > count)
915 * Set obsolete flag at offset - NOR flash only
917 static int flash_flag_obsolete (int dev, int fd, off_t offset)
920 struct erase_info_user erase;
922 erase.start = DEVOFFSET (dev);
923 erase.length = DEVESIZE (dev);
924 /* This relies on the fact, that obsolete_flag == 0 */
925 rc = lseek (fd, offset, SEEK_SET);
927 fprintf (stderr, "Cannot seek to set the flag on %s \n",
931 ioctl (fd, MEMUNLOCK, &erase);
932 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
933 ioctl (fd, MEMLOCK, &erase);
935 perror ("Could not set obsolete flag");
940 static int flash_write (int fd_current, int fd_target, int dev_target)
944 switch (environment.flag_scheme) {
947 case FLAG_INCREMENTAL:
948 (*environment.flags)++;
951 *environment.flags = active_flag;
954 fprintf (stderr, "Unimplemented flash scheme %u \n",
955 environment.flag_scheme);
960 printf ("Writing new environment at 0x%lx on %s\n",
961 DEVOFFSET (dev_target), DEVNAME (dev_target));
963 rc = flash_write_buf (dev_target, fd_target, environment.image,
964 CONFIG_ENV_SIZE, DEVOFFSET (dev_target),
965 DEVTYPE(dev_target));
969 if (environment.flag_scheme == FLAG_BOOLEAN) {
970 /* Have to set obsolete flag */
971 off_t offset = DEVOFFSET (dev_current) +
972 offsetof (struct env_image_redundant, flags);
974 printf ("Setting obsolete flag in environment at 0x%lx on %s\n",
975 DEVOFFSET (dev_current), DEVNAME (dev_current));
977 flash_flag_obsolete (dev_current, fd_current, offset);
983 static int flash_read (int fd)
985 struct mtd_info_user mtdinfo;
988 rc = ioctl (fd, MEMGETINFO, &mtdinfo);
990 perror ("Cannot get MTD information");
994 if (mtdinfo.type != MTD_NORFLASH &&
995 mtdinfo.type != MTD_NANDFLASH &&
996 mtdinfo.type != MTD_DATAFLASH) {
997 fprintf (stderr, "Unsupported flash type %u\n", mtdinfo.type);
1001 DEVTYPE(dev_current) = mtdinfo.type;
1003 rc = flash_read_buf (dev_current, fd, environment.image, CONFIG_ENV_SIZE,
1004 DEVOFFSET (dev_current), mtdinfo.type);
1006 return (rc != CONFIG_ENV_SIZE) ? -1 : 0;
1009 static int flash_io (int mode)
1011 int fd_current, fd_target, rc, dev_target;
1013 /* dev_current: fd_current, erase_current */
1014 fd_current = open (DEVNAME (dev_current), mode);
1015 if (fd_current < 0) {
1017 "Can't open %s: %s\n",
1018 DEVNAME (dev_current), strerror (errno));
1022 if (mode == O_RDWR) {
1023 if (HaveRedundEnv) {
1024 /* switch to next partition for writing */
1025 dev_target = !dev_current;
1026 /* dev_target: fd_target, erase_target */
1027 fd_target = open (DEVNAME (dev_target), mode);
1028 if (fd_target < 0) {
1030 "Can't open %s: %s\n",
1031 DEVNAME (dev_target),
1037 dev_target = dev_current;
1038 fd_target = fd_current;
1041 rc = flash_write (fd_current, fd_target, dev_target);
1043 if (HaveRedundEnv) {
1044 if (close (fd_target)) {
1046 "I/O error on %s: %s\n",
1047 DEVNAME (dev_target),
1053 rc = flash_read (fd_current);
1057 if (close (fd_current)) {
1059 "I/O error on %s: %s\n",
1060 DEVNAME (dev_current), strerror (errno));
1068 * s1 is either a simple 'name', or a 'name=value' pair.
1069 * s2 is a 'name=value' pair.
1070 * If the names match, return the value of s2, else NULL.
1073 static char *envmatch (char * s1, char * s2)
1076 while (*s1 == *s2++)
1079 if (*s1 == '\0' && *(s2 - 1) == '=')
1085 * Prevent confusion if running from erased flash memory
1087 int fw_env_open(void)
1090 unsigned char flag0;
1094 unsigned char flag1;
1097 struct env_image_single *single;
1098 struct env_image_redundant *redundant;
1100 if (parse_config ()) /* should fill envdevices */
1103 addr0 = calloc (1, CONFIG_ENV_SIZE);
1104 if (addr0 == NULL) {
1106 "Not enough memory for environment (%ld bytes)\n",
1111 /* read environment from FLASH to local buffer */
1112 environment.image = addr0;
1114 if (HaveRedundEnv) {
1116 environment.crc = &redundant->crc;
1117 environment.flags = &redundant->flags;
1118 environment.data = redundant->data;
1121 environment.crc = &single->crc;
1122 environment.flags = NULL;
1123 environment.data = single->data;
1127 if (flash_io (O_RDONLY))
1130 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1131 crc0_ok = (crc0 == *environment.crc);
1132 if (!HaveRedundEnv) {
1135 "Warning: Bad CRC, using default environment\n");
1136 memcpy(environment.data, default_environment, sizeof default_environment);
1139 flag0 = *environment.flags;
1142 addr1 = calloc (1, CONFIG_ENV_SIZE);
1143 if (addr1 == NULL) {
1145 "Not enough memory for environment (%ld bytes)\n",
1152 * have to set environment.image for flash_read(), careful -
1153 * other pointers in environment still point inside addr0
1155 environment.image = addr1;
1156 if (flash_io (O_RDONLY))
1159 /* Check flag scheme compatibility */
1160 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1161 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1162 environment.flag_scheme = FLAG_BOOLEAN;
1163 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1164 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1165 environment.flag_scheme = FLAG_INCREMENTAL;
1166 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1167 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1168 environment.flag_scheme = FLAG_BOOLEAN;
1170 fprintf (stderr, "Incompatible flash types!\n");
1174 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1175 crc1_ok = (crc1 == redundant->crc);
1176 flag1 = redundant->flags;
1178 if (crc0_ok && !crc1_ok) {
1180 } else if (!crc0_ok && crc1_ok) {
1182 } else if (!crc0_ok && !crc1_ok) {
1184 "Warning: Bad CRC, using default environment\n");
1185 memcpy (environment.data, default_environment,
1186 sizeof default_environment);
1189 switch (environment.flag_scheme) {
1191 if (flag0 == active_flag &&
1192 flag1 == obsolete_flag) {
1194 } else if (flag0 == obsolete_flag &&
1195 flag1 == active_flag) {
1197 } else if (flag0 == flag1) {
1199 } else if (flag0 == 0xFF) {
1201 } else if (flag1 == 0xFF) {
1207 case FLAG_INCREMENTAL:
1208 if (flag0 == 255 && flag1 == 0)
1210 else if ((flag1 == 255 && flag0 == 0) ||
1213 else /* flag1 > flag0 */
1217 fprintf (stderr, "Unknown flag scheme %u \n",
1218 environment.flag_scheme);
1224 * If we are reading, we don't need the flag and the CRC any
1225 * more, if we are writing, we will re-calculate CRC and update
1226 * flags before writing out
1229 environment.image = addr1;
1230 environment.crc = &redundant->crc;
1231 environment.flags = &redundant->flags;
1232 environment.data = redundant->data;
1235 environment.image = addr0;
1236 /* Other pointers are already set */
1244 static int parse_config ()
1248 #if defined(CONFIG_FILE)
1249 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
1250 if (get_config (CONFIG_FILE)) {
1252 "Cannot parse config file: %s\n", strerror (errno));
1256 strcpy (DEVNAME (0), DEVICE1_NAME);
1257 DEVOFFSET (0) = DEVICE1_OFFSET;
1258 ENVSIZE (0) = ENV1_SIZE;
1259 /* Default values are: erase-size=env-size, #sectors=1 */
1260 DEVESIZE (0) = ENVSIZE (0);
1262 #ifdef DEVICE1_ESIZE
1263 DEVESIZE (0) = DEVICE1_ESIZE;
1265 #ifdef DEVICE1_ENVSECTORS
1266 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
1270 strcpy (DEVNAME (1), DEVICE2_NAME);
1271 DEVOFFSET (1) = DEVICE2_OFFSET;
1272 ENVSIZE (1) = ENV2_SIZE;
1273 /* Default values are: erase-size=env-size, #sectors=1 */
1274 DEVESIZE (1) = ENVSIZE (1);
1276 #ifdef DEVICE2_ESIZE
1277 DEVESIZE (1) = DEVICE2_ESIZE;
1279 #ifdef DEVICE2_ENVSECTORS
1280 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
1285 if (stat (DEVNAME (0), &st)) {
1287 "Cannot access MTD device %s: %s\n",
1288 DEVNAME (0), strerror (errno));
1292 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1294 "Cannot access MTD device %s: %s\n",
1295 DEVNAME (1), strerror (errno));
1301 #if defined(CONFIG_FILE)
1302 static int get_config (char *fname)
1309 fp = fopen (fname, "r");
1313 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
1314 /* Skip incomplete conversions and comment strings */
1318 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1329 /* Assume the erase size is the same as the env-size */
1330 DEVESIZE(i) = ENVSIZE(i);
1333 /* Default - 1 sector */
1340 HaveRedundEnv = i - 1;
1341 if (!i) { /* No valid entries found */