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 CMD_GETENV "fw_printenv"
49 #define CMD_SETENV "fw_setenv"
51 #define min(x, y) ({ \
52 typeof(x) _min1 = (x); \
53 typeof(y) _min2 = (y); \
54 (void) (&_min1 == &_min2); \
55 _min1 < _min2 ? _min1 : _min2; })
58 char devname
[16]; /* Device name */
59 ulong devoff
; /* Device offset */
60 ulong env_size
; /* environment size */
61 ulong erase_size
; /* device erase size */
62 ulong env_sectors
; /* number of environment sectors */
63 uint8_t mtd_type
; /* type of the MTD device */
66 static struct envdev_s envdevices
[2] =
69 .mtd_type
= MTD_ABSENT
,
71 .mtd_type
= MTD_ABSENT
,
74 static int dev_current
;
76 #define DEVNAME(i) envdevices[(i)].devname
77 #define DEVOFFSET(i) envdevices[(i)].devoff
78 #define ENVSIZE(i) envdevices[(i)].env_size
79 #define DEVESIZE(i) envdevices[(i)].erase_size
80 #define ENVSECTORS(i) envdevices[(i)].env_sectors
81 #define DEVTYPE(i) envdevices[(i)].mtd_type
83 #define CONFIG_ENV_SIZE ENVSIZE(dev_current)
85 #define ENV_SIZE getenvsize()
87 struct env_image_single
{
88 uint32_t crc
; /* CRC32 over data bytes */
92 struct env_image_redundant
{
93 uint32_t crc
; /* CRC32 over data bytes */
94 unsigned char flags
; /* active or obsolete */
107 unsigned char *flags
;
109 enum flag_scheme flag_scheme
;
112 static struct environment environment
= {
113 .flag_scheme
= FLAG_NONE
,
116 static int HaveRedundEnv
= 0;
118 static unsigned char active_flag
= 1;
119 /* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
120 static unsigned char obsolete_flag
= 0;
123 #define XMK_STR(x) #x
124 #define MK_STR(x) XMK_STR(x)
126 static char default_environment
[] = {
127 #if defined(CONFIG_BOOTARGS)
128 "bootargs=" CONFIG_BOOTARGS
"\0"
130 #if defined(CONFIG_BOOTCOMMAND)
131 "bootcmd=" CONFIG_BOOTCOMMAND
"\0"
133 #if defined(CONFIG_RAMBOOTCOMMAND)
134 "ramboot=" CONFIG_RAMBOOTCOMMAND
"\0"
136 #if defined(CONFIG_NFSBOOTCOMMAND)
137 "nfsboot=" CONFIG_NFSBOOTCOMMAND
"\0"
139 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
140 "bootdelay=" MK_STR (CONFIG_BOOTDELAY
) "\0"
142 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
143 "baudrate=" MK_STR (CONFIG_BAUDRATE
) "\0"
145 #ifdef CONFIG_LOADS_ECHO
146 "loads_echo=" MK_STR (CONFIG_LOADS_ECHO
) "\0"
148 #ifdef CONFIG_ETHADDR
149 "ethaddr=" MK_STR (CONFIG_ETHADDR
) "\0"
151 #ifdef CONFIG_ETH1ADDR
152 "eth1addr=" MK_STR (CONFIG_ETH1ADDR
) "\0"
154 #ifdef CONFIG_ETH2ADDR
155 "eth2addr=" MK_STR (CONFIG_ETH2ADDR
) "\0"
157 #ifdef CONFIG_ETH3ADDR
158 "eth3addr=" MK_STR (CONFIG_ETH3ADDR
) "\0"
160 #ifdef CONFIG_ETH4ADDR
161 "eth4addr=" MK_STR (CONFIG_ETH4ADDR
) "\0"
163 #ifdef CONFIG_ETH5ADDR
164 "eth5addr=" MK_STR (CONFIG_ETH5ADDR
) "\0"
166 #ifdef CONFIG_ETHPRIME
167 "ethprime=" CONFIG_ETHPRIME
"\0"
170 "ipaddr=" MK_STR (CONFIG_IPADDR
) "\0"
172 #ifdef CONFIG_SERVERIP
173 "serverip=" MK_STR (CONFIG_SERVERIP
) "\0"
175 #ifdef CONFIG_SYS_AUTOLOAD
176 "autoload=" CONFIG_SYS_AUTOLOAD
"\0"
178 #ifdef CONFIG_ROOTPATH
179 "rootpath=" MK_STR (CONFIG_ROOTPATH
) "\0"
181 #ifdef CONFIG_GATEWAYIP
182 "gatewayip=" MK_STR (CONFIG_GATEWAYIP
) "\0"
184 #ifdef CONFIG_NETMASK
185 "netmask=" MK_STR (CONFIG_NETMASK
) "\0"
187 #ifdef CONFIG_HOSTNAME
188 "hostname=" MK_STR (CONFIG_HOSTNAME
) "\0"
190 #ifdef CONFIG_BOOTFILE
191 "bootfile=" MK_STR (CONFIG_BOOTFILE
) "\0"
193 #ifdef CONFIG_LOADADDR
194 "loadaddr=" MK_STR (CONFIG_LOADADDR
) "\0"
196 #ifdef CONFIG_PREBOOT
197 "preboot=" CONFIG_PREBOOT
"\0"
199 #ifdef CONFIG_CLOCKS_IN_MHZ
200 "clocks_in_mhz=" "1" "\0"
202 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
203 "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY
) "\0"
205 #ifdef CONFIG_EXTRA_ENV_SETTINGS
206 CONFIG_EXTRA_ENV_SETTINGS
208 "\0" /* Termimate struct environment data with 2 NULs */
211 static int flash_io (int mode
);
212 static char *envmatch (char * s1
, char * s2
);
213 static int env_init (void);
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);
229 * Search the environment for a variable.
230 * Return the value, if found, or NULL, if not found.
232 char *fw_getenv (char *name
)
239 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
242 for (nxt
= env
; *nxt
; ++nxt
) {
243 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
244 fprintf (stderr
, "## Error: "
245 "environment not terminated\n");
249 val
= envmatch (name
, env
);
258 * Print the current definition of one, or more, or all
259 * environment variables
261 int fw_printenv (int argc
, char *argv
[])
270 if (argc
== 1) { /* Print all env variables */
271 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
272 for (nxt
= env
; *nxt
; ++nxt
) {
273 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
274 fprintf (stderr
, "## Error: "
275 "environment not terminated\n");
280 printf ("%s\n", env
);
285 if (strcmp (argv
[1], "-n") == 0) {
290 fprintf (stderr
, "## Error: "
291 "`-n' option requires exactly one argument\n");
298 for (i
= 1; i
< argc
; ++i
) { /* print single env variables */
299 char *name
= argv
[i
];
302 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
304 for (nxt
= env
; *nxt
; ++nxt
) {
305 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
306 fprintf (stderr
, "## Error: "
307 "environment not terminated\n");
311 val
= envmatch (name
, env
);
314 fputs (name
, stdout
);
322 fprintf (stderr
, "## Error: \"%s\" not defined\n", name
);
331 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
333 * EINVAL - need at least 1 argument
334 * EROFS - certain variables ("ethaddr", "serial#") cannot be
335 * modified or deleted
338 int fw_setenv (int argc
, char *argv
[])
356 * search if variable with this name already exists
358 for (nxt
= env
= environment
.data
; *env
; env
= nxt
+ 1) {
359 for (nxt
= env
; *nxt
; ++nxt
) {
360 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
361 fprintf (stderr
, "## Error: "
362 "environment not terminated\n");
367 if ((oldval
= envmatch (name
, env
)) != NULL
)
372 * Delete any existing definition
376 * Ethernet Address and serial# can be set only once
378 if ((strcmp (name
, "ethaddr") == 0) ||
379 (strcmp (name
, "serial#") == 0)) {
380 fprintf (stderr
, "Can't overwrite \"%s\"\n", name
);
385 if (*++nxt
== '\0') {
390 if ((*env
== '\0') && (*nxt
== '\0'))
403 * Append new definition at the end
405 for (env
= environment
.data
; *env
|| *(env
+ 1); ++env
);
406 if (env
> environment
.data
)
410 * "name" + "=" + "val" +"\0\0" > CONFIG_ENV_SIZE - (env-environment)
412 len
= strlen (name
) + 2;
413 /* add '=' for first arg, ' ' for all others */
414 for (i
= 2; i
< argc
; ++i
) {
415 len
+= strlen (argv
[i
]) + 1;
417 if (len
> (&environment
.data
[ENV_SIZE
] - env
)) {
419 "Error: environment overflow, \"%s\" deleted\n",
423 while ((*env
= *name
++) != '\0')
425 for (i
= 2; i
< argc
; ++i
) {
428 *env
= (i
== 2) ? '=' : ' ';
429 while ((*++env
= *val
++) != '\0');
432 /* end is marked with double '\0' */
440 *environment
.crc
= crc32 (0, (uint8_t *) environment
.data
, ENV_SIZE
);
442 /* write environment back to flash */
443 if (flash_io (O_RDWR
)) {
444 fprintf (stderr
, "Error: can't write fw_env to flash\n");
452 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
455 * < 0 - failed to test
457 static int flash_bad_block (int fd
, uint8_t mtd_type
, loff_t
*blockstart
)
459 if (mtd_type
== MTD_NANDFLASH
) {
460 int badblock
= ioctl (fd
, MEMGETBADBLOCK
, blockstart
);
463 perror ("Cannot read bad block mark");
469 fprintf (stderr
, "Bad block at 0x%llx, "
470 "skipping\n", *blockstart
);
480 * Read data from flash at an offset into a provided buffer. On NAND it skips
481 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
482 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
484 static int flash_read_buf (int dev
, int fd
, void *buf
, size_t count
,
485 off_t offset
, uint8_t mtd_type
)
487 size_t blocklen
; /* erase / write length - one block on NAND,
489 size_t processed
= 0; /* progress counter */
490 size_t readlen
= count
; /* current read length */
491 off_t top_of_range
; /* end of the last block we may use */
492 off_t block_seek
; /* offset inside the current block to the start
494 loff_t blockstart
; /* running start of the current block -
495 MEMGETBADBLOCK needs 64 bits */
499 * Start of the first block to be read, relies on the fact, that
500 * erase sector size is always a power of 2
502 blockstart
= offset
& ~(DEVESIZE (dev
) - 1);
504 /* Offset inside a block */
505 block_seek
= offset
- blockstart
;
507 if (mtd_type
== MTD_NANDFLASH
) {
509 * NAND: calculate which blocks we are reading. We have
510 * to read one block at a time to skip bad blocks.
512 blocklen
= DEVESIZE (dev
);
515 * To calculate the top of the range, we have to use the
516 * global DEVOFFSET (dev), which can be different from offset
518 top_of_range
= (DEVOFFSET (dev
) & ~(blocklen
- 1)) +
519 ENVSECTORS (dev
) * blocklen
;
521 /* Limit to one block for the first read */
522 if (readlen
> blocklen
- block_seek
)
523 readlen
= blocklen
- block_seek
;
526 top_of_range
= offset
+ count
;
529 /* This only runs once on NOR flash */
530 while (processed
< count
) {
531 rc
= flash_bad_block (fd
, mtd_type
, &blockstart
);
532 if (rc
< 0) /* block test failed */
535 if (blockstart
+ block_seek
+ readlen
> top_of_range
) {
536 /* End of range is reached */
538 "Too few good blocks within range\n");
542 if (rc
) { /* block is bad */
543 blockstart
+= blocklen
;
548 * If a block is bad, we retry in the next block at the same
549 * offset - see common/env_nand.c::writeenv()
551 lseek (fd
, blockstart
+ block_seek
, SEEK_SET
);
553 rc
= read (fd
, buf
+ processed
, readlen
);
555 fprintf (stderr
, "Read error on %s: %s\n",
556 DEVNAME (dev
), strerror (errno
));
560 fprintf (stderr
, "Read 0x%x bytes at 0x%llx\n",
561 rc
, blockstart
+ block_seek
);
563 processed
+= readlen
;
564 readlen
= min (blocklen
, count
- processed
);
566 blockstart
+= blocklen
;
573 * Write count bytes at offset, but stay within ENVSETCORS (dev) sectors of
574 * DEVOFFSET (dev). Similar to the read case above, on NOR we erase and write
575 * the whole data at once.
577 static int flash_write_buf (int dev
, int fd
, void *buf
, size_t count
,
578 off_t offset
, uint8_t mtd_type
)
581 struct erase_info_user erase
;
582 size_t blocklen
; /* length of NAND block / NOR erase sector */
583 size_t erase_len
; /* whole area that can be erased - may include
585 size_t erasesize
; /* erase / write length - one block on NAND,
587 size_t processed
= 0; /* progress counter */
588 size_t write_total
; /* total size to actually write - excludinig
590 off_t erase_offset
; /* offset to the first erase block (aligned)
592 off_t block_seek
; /* offset inside the erase block to the start
594 off_t top_of_range
; /* end of the last block we may use */
595 loff_t blockstart
; /* running start of the current block -
596 MEMGETBADBLOCK needs 64 bits */
599 blocklen
= DEVESIZE (dev
);
601 /* Erase sector size is always a power of 2 */
602 top_of_range
= (DEVOFFSET (dev
) & ~(blocklen
- 1)) +
603 ENVSECTORS (dev
) * blocklen
;
605 erase_offset
= offset
& ~(blocklen
- 1);
607 /* Maximum area we may use */
608 erase_len
= top_of_range
- erase_offset
;
610 blockstart
= erase_offset
;
611 /* Offset inside a block */
612 block_seek
= offset
- erase_offset
;
615 * Data size we actually have to write: from the start of the block
616 * to the start of the data, then count bytes of data, and to the
619 write_total
= (block_seek
+ count
+ blocklen
- 1) & ~(blocklen
- 1);
622 * Support data anywhere within erase sectors: read out the complete
623 * area to be erased, replace the environment image, write the whole
626 if (write_total
> count
) {
627 data
= malloc (erase_len
);
630 "Cannot malloc %u bytes: %s\n",
631 erase_len
, strerror (errno
));
635 rc
= flash_read_buf (dev
, fd
, data
, write_total
, erase_offset
,
637 if (write_total
!= rc
)
640 /* Overwrite the old environment */
641 memcpy (data
+ block_seek
, buf
, count
);
644 * We get here, iff offset is block-aligned and count is a
645 * multiple of blocklen - see write_total calculation above
650 if (mtd_type
== MTD_NANDFLASH
) {
652 * NAND: calculate which blocks we are writing. We have
653 * to write one block at a time to skip bad blocks.
655 erasesize
= blocklen
;
657 erasesize
= erase_len
;
660 erase
.length
= erasesize
;
662 /* This only runs once on NOR flash */
663 while (processed
< write_total
) {
664 rc
= flash_bad_block (fd
, mtd_type
, &blockstart
);
665 if (rc
< 0) /* block test failed */
668 if (blockstart
+ erasesize
> top_of_range
) {
669 fprintf (stderr
, "End of range reached, aborting\n");
673 if (rc
) { /* block is bad */
674 blockstart
+= blocklen
;
678 erase
.start
= blockstart
;
679 ioctl (fd
, MEMUNLOCK
, &erase
);
681 if (ioctl (fd
, MEMERASE
, &erase
) != 0) {
682 fprintf (stderr
, "MTD erase error on %s: %s\n",
688 if (lseek (fd
, blockstart
, SEEK_SET
) == -1) {
690 "Seek error on %s: %s\n",
691 DEVNAME (dev
), strerror (errno
));
696 printf ("Write 0x%x bytes at 0x%llx\n", erasesize
, blockstart
);
698 if (write (fd
, data
+ processed
, erasesize
) != erasesize
) {
699 fprintf (stderr
, "Write error on %s: %s\n",
700 DEVNAME (dev
), strerror (errno
));
704 ioctl (fd
, MEMLOCK
, &erase
);
706 processed
+= blocklen
;
708 blockstart
+= blocklen
;
711 if (write_total
> count
)
718 * Set obsolete flag at offset - NOR flash only
720 static int flash_flag_obsolete (int dev
, int fd
, off_t offset
)
724 /* This relies on the fact, that obsolete_flag == 0 */
725 rc
= lseek (fd
, offset
, SEEK_SET
);
727 fprintf (stderr
, "Cannot seek to set the flag on %s \n",
731 rc
= write (fd
, &obsolete_flag
, sizeof (obsolete_flag
));
733 perror ("Could not set obsolete flag");
738 static int flash_write (int fd_current
, int fd_target
, int dev_target
)
742 switch (environment
.flag_scheme
) {
745 case FLAG_INCREMENTAL
:
746 (*environment
.flags
)++;
749 *environment
.flags
= active_flag
;
752 fprintf (stderr
, "Unimplemented flash scheme %u \n",
753 environment
.flag_scheme
);
758 printf ("Writing new environment at 0x%lx on %s\n",
759 DEVOFFSET (dev_target
), DEVNAME (dev_target
));
761 rc
= flash_write_buf (dev_target
, fd_target
, environment
.image
,
762 CONFIG_ENV_SIZE
, DEVOFFSET (dev_target
),
763 DEVTYPE(dev_target
));
767 if (environment
.flag_scheme
== FLAG_BOOLEAN
) {
768 /* Have to set obsolete flag */
769 off_t offset
= DEVOFFSET (dev_current
) +
770 offsetof (struct env_image_redundant
, flags
);
772 printf ("Setting obsolete flag in environment at 0x%lx on %s\n",
773 DEVOFFSET (dev_current
), DEVNAME (dev_current
));
775 flash_flag_obsolete (dev_current
, fd_current
, offset
);
781 static int flash_read (int fd
)
783 struct mtd_info_user mtdinfo
;
786 rc
= ioctl (fd
, MEMGETINFO
, &mtdinfo
);
788 perror ("Cannot get MTD information");
792 if (mtdinfo
.type
!= MTD_NORFLASH
&& mtdinfo
.type
!= MTD_NANDFLASH
) {
793 fprintf (stderr
, "Unsupported flash type %u\n", mtdinfo
.type
);
797 DEVTYPE(dev_current
) = mtdinfo
.type
;
799 rc
= flash_read_buf (dev_current
, fd
, environment
.image
, CONFIG_ENV_SIZE
,
800 DEVOFFSET (dev_current
), mtdinfo
.type
);
802 return (rc
!= CONFIG_ENV_SIZE
) ? -1 : 0;
805 static int flash_io (int mode
)
807 int fd_current
, fd_target
, rc
, dev_target
;
809 /* dev_current: fd_current, erase_current */
810 fd_current
= open (DEVNAME (dev_current
), mode
);
811 if (fd_current
< 0) {
813 "Can't open %s: %s\n",
814 DEVNAME (dev_current
), strerror (errno
));
818 if (mode
== O_RDWR
) {
820 /* switch to next partition for writing */
821 dev_target
= !dev_current
;
822 /* dev_target: fd_target, erase_target */
823 fd_target
= open (DEVNAME (dev_target
), mode
);
826 "Can't open %s: %s\n",
827 DEVNAME (dev_target
),
833 dev_target
= dev_current
;
834 fd_target
= fd_current
;
837 rc
= flash_write (fd_current
, fd_target
, dev_target
);
840 if (close (fd_target
)) {
842 "I/O error on %s: %s\n",
843 DEVNAME (dev_target
),
849 rc
= flash_read (fd_current
);
853 if (close (fd_current
)) {
855 "I/O error on %s: %s\n",
856 DEVNAME (dev_current
), strerror (errno
));
864 * s1 is either a simple 'name', or a 'name=value' pair.
865 * s2 is a 'name=value' pair.
866 * If the names match, return the value of s2, else NULL.
869 static char *envmatch (char * s1
, char * s2
)
875 if (*s1
== '\0' && *(s2
- 1) == '=')
881 * Prevent confusion if running from erased flash memory
883 static int env_init (void)
893 struct env_image_single
*single
;
894 struct env_image_redundant
*redundant
;
896 if (parse_config ()) /* should fill envdevices */
899 addr0
= calloc (1, CONFIG_ENV_SIZE
);
902 "Not enough memory for environment (%ld bytes)\n",
907 /* read environment from FLASH to local buffer */
908 environment
.image
= addr0
;
912 environment
.crc
= &redundant
->crc
;
913 environment
.flags
= &redundant
->flags
;
914 environment
.data
= redundant
->data
;
917 environment
.crc
= &single
->crc
;
918 environment
.flags
= NULL
;
919 environment
.data
= single
->data
;
923 if (flash_io (O_RDONLY
))
926 crc0
= crc32 (0, (uint8_t *) environment
.data
, ENV_SIZE
);
927 crc0_ok
= (crc0
== *environment
.crc
);
928 if (!HaveRedundEnv
) {
931 "Warning: Bad CRC, using default environment\n");
932 memcpy(environment
.data
, default_environment
, sizeof default_environment
);
935 flag0
= *environment
.flags
;
938 addr1
= calloc (1, CONFIG_ENV_SIZE
);
941 "Not enough memory for environment (%ld bytes)\n",
948 * have to set environment.image for flash_read(), careful -
949 * other pointers in environment still point inside addr0
951 environment
.image
= addr1
;
952 if (flash_io (O_RDONLY
))
955 /* Check flag scheme compatibility */
956 if (DEVTYPE(dev_current
) == MTD_NORFLASH
&&
957 DEVTYPE(!dev_current
) == MTD_NORFLASH
) {
958 environment
.flag_scheme
= FLAG_BOOLEAN
;
959 } else if (DEVTYPE(dev_current
) == MTD_NANDFLASH
&&
960 DEVTYPE(!dev_current
) == MTD_NANDFLASH
) {
961 environment
.flag_scheme
= FLAG_INCREMENTAL
;
963 fprintf (stderr
, "Incompatible flash types!\n");
967 crc1
= crc32 (0, (uint8_t *) redundant
->data
, ENV_SIZE
);
968 crc1_ok
= (crc1
== redundant
->crc
);
969 flag1
= redundant
->flags
;
971 if (crc0_ok
&& !crc1_ok
) {
973 } else if (!crc0_ok
&& crc1_ok
) {
975 } else if (!crc0_ok
&& !crc1_ok
) {
977 "Warning: Bad CRC, using default environment\n");
978 memcpy (environment
.data
, default_environment
,
979 sizeof default_environment
);
982 switch (environment
.flag_scheme
) {
984 if (flag0
== active_flag
&&
985 flag1
== obsolete_flag
) {
987 } else if (flag0
== obsolete_flag
&&
988 flag1
== active_flag
) {
990 } else if (flag0
== flag1
) {
992 } else if (flag0
== 0xFF) {
994 } else if (flag1
== 0xFF) {
1000 case FLAG_INCREMENTAL
:
1001 if ((flag0
== 255 && flag1
== 0) ||
1004 else if ((flag1
== 255 && flag0
== 0) ||
1007 else /* flags are equal - almost impossible */
1011 fprintf (stderr
, "Unknown flag scheme %u \n",
1012 environment
.flag_scheme
);
1018 * If we are reading, we don't need the flag and the CRC any
1019 * more, if we are writing, we will re-calculate CRC and update
1020 * flags before writing out
1023 environment
.image
= addr1
;
1024 environment
.crc
= &redundant
->crc
;
1025 environment
.flags
= &redundant
->flags
;
1026 environment
.data
= redundant
->data
;
1029 environment
.image
= addr0
;
1030 /* Other pointers are already set */
1038 static int parse_config ()
1042 #if defined(CONFIG_FILE)
1043 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
1044 if (get_config (CONFIG_FILE
)) {
1046 "Cannot parse config file: %s\n", strerror (errno
));
1050 strcpy (DEVNAME (0), DEVICE1_NAME
);
1051 DEVOFFSET (0) = DEVICE1_OFFSET
;
1052 ENVSIZE (0) = ENV1_SIZE
;
1053 DEVESIZE (0) = DEVICE1_ESIZE
;
1054 ENVSECTORS (0) = DEVICE1_ENVSECTORS
;
1056 strcpy (DEVNAME (1), DEVICE2_NAME
);
1057 DEVOFFSET (1) = DEVICE2_OFFSET
;
1058 ENVSIZE (1) = ENV2_SIZE
;
1059 DEVESIZE (1) = DEVICE2_ESIZE
;
1060 ENVSECTORS (1) = DEVICE2_ENVSECTORS
;
1064 if (stat (DEVNAME (0), &st
)) {
1066 "Cannot access MTD device %s: %s\n",
1067 DEVNAME (0), strerror (errno
));
1071 if (HaveRedundEnv
&& stat (DEVNAME (1), &st
)) {
1073 "Cannot access MTD device %s: %s\n",
1074 DEVNAME (1), strerror (errno
));
1080 #if defined(CONFIG_FILE)
1081 static int get_config (char *fname
)
1088 fp
= fopen (fname
, "r");
1092 while (i
< 2 && fgets (dump
, sizeof (dump
), fp
)) {
1093 /* Skip incomplete conversions and comment strings */
1097 rc
= sscanf (dump
, "%s %lx %lx %lx %lx",
1108 /* Default - 1 sector */
1115 HaveRedundEnv
= i
- 1;
1116 if (!i
) { /* No valid entries found */