2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
15 #include <env_flags.h>
17 #include <linux/stringify.h>
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
30 # include <linux/mtd/mtd.h>
32 # define __user /* nothing */
33 # include <mtd/mtd-user.h>
38 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
40 #define min(x, y) ({ \
41 typeof(x) _min1 = (x); \
42 typeof(y) _min2 = (y); \
43 (void) (&_min1 == &_min2); \
44 _min1 < _min2 ? _min1 : _min2; })
47 const char *devname
; /* Device name */
48 ulong devoff
; /* Device offset */
49 ulong env_size
; /* environment size */
50 ulong erase_size
; /* device erase size */
51 ulong env_sectors
; /* number of environment sectors */
52 uint8_t mtd_type
; /* type of the MTD device */
55 static struct envdev_s envdevices
[2] =
58 .mtd_type
= MTD_ABSENT
,
60 .mtd_type
= MTD_ABSENT
,
63 static int dev_current
;
65 #define DEVNAME(i) envdevices[(i)].devname
66 #define DEVOFFSET(i) envdevices[(i)].devoff
67 #define ENVSIZE(i) envdevices[(i)].env_size
68 #define DEVESIZE(i) envdevices[(i)].erase_size
69 #define ENVSECTORS(i) envdevices[(i)].env_sectors
70 #define DEVTYPE(i) envdevices[(i)].mtd_type
72 #define CUR_ENVSIZE ENVSIZE(dev_current)
74 static unsigned long usable_envsize
;
75 #define ENV_SIZE usable_envsize
77 struct env_image_single
{
78 uint32_t crc
; /* CRC32 over data bytes */
82 struct env_image_redundant
{
83 uint32_t crc
; /* CRC32 over data bytes */
84 unsigned char flags
; /* active or obsolete */
99 enum flag_scheme flag_scheme
;
102 static struct environment environment
= {
103 .flag_scheme
= FLAG_NONE
,
106 static int env_aes_cbc_crypt(char *data
, const int enc
, uint8_t *key
);
108 static int HaveRedundEnv
= 0;
110 static unsigned char active_flag
= 1;
111 /* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
112 static unsigned char obsolete_flag
= 0;
114 #define DEFAULT_ENV_INSTANCE_STATIC
115 #include <env_default.h>
117 static int flash_io (int mode
);
118 static char *envmatch (char * s1
, char * s2
);
119 static int parse_config(struct env_opts
*opts
);
121 #if defined(CONFIG_FILE)
122 static int get_config (char *);
125 static char *skip_chars(char *s
)
127 for (; *s
!= '\0'; s
++) {
134 static char *skip_blanks(char *s
)
136 for (; *s
!= '\0'; s
++) {
144 * Search the environment for a variable.
145 * Return the value, if found, or NULL, if not found.
147 char *fw_getenv (char *name
)
151 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
154 for (nxt
= env
; *nxt
; ++nxt
) {
155 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
156 fprintf (stderr
, "## Error: "
157 "environment not terminated\n");
161 val
= envmatch (name
, env
);
170 * Search the default environment for a variable.
171 * Return the value, if found, or NULL, if not found.
173 char *fw_getdefenv(char *name
)
177 for (env
= default_environment
; *env
; env
= nxt
+ 1) {
180 for (nxt
= env
; *nxt
; ++nxt
) {
181 if (nxt
>= &default_environment
[ENV_SIZE
]) {
182 fprintf(stderr
, "## Error: "
183 "default environment not terminated\n");
187 val
= envmatch(name
, env
);
195 int parse_aes_key(char *key
, uint8_t *bin_key
)
197 char tmp
[5] = { '0', 'x', 0, 0, 0 };
201 if (strnlen(key
, 64) != 32) {
203 "## Error: '-a' option requires 16-byte AES key\n");
207 for (i
= 0; i
< 16; i
++) {
211 ul
= strtoul(tmp
, NULL
, 16);
214 "## Error: '-a' option requires valid AES key\n");
217 bin_key
[i
] = ul
& 0xff;
224 * Print the current definition of one, or more, or all
225 * environment variables
227 int fw_printenv(int argc
, char *argv
[], int value_only
, struct env_opts
*opts
)
232 if (fw_env_open(opts
))
235 if (argc
== 0) { /* Print all env variables */
236 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
237 for (nxt
= env
; *nxt
; ++nxt
) {
238 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
239 fprintf (stderr
, "## Error: "
240 "environment not terminated\n");
245 printf ("%s\n", env
);
250 if (value_only
&& argc
!= 1) {
252 "## Error: `-n' option requires exactly one argument\n");
256 for (i
= 0; i
< argc
; ++i
) { /* print single env variables */
257 char *name
= argv
[i
];
260 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
262 for (nxt
= env
; *nxt
; ++nxt
) {
263 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
264 fprintf (stderr
, "## Error: "
265 "environment not terminated\n");
269 val
= envmatch (name
, env
);
272 fputs (name
, stdout
);
280 fprintf (stderr
, "## Error: \"%s\" not defined\n", name
);
288 int fw_env_close(struct env_opts
*opts
)
292 if (opts
->aes_flag
) {
293 ret
= env_aes_cbc_crypt(environment
.data
, 1,
297 "Error: can't encrypt env for flash\n");
305 *environment
.crc
= crc32(0, (uint8_t *) environment
.data
, ENV_SIZE
);
307 /* write environment back to flash */
308 if (flash_io(O_RDWR
)) {
310 "Error: can't write fw_env to flash\n");
319 * Set/Clear a single variable in the environment.
320 * This is called in sequence to update the environment
321 * in RAM without updating the copy in flash after each set
323 int fw_env_write(char *name
, char *value
)
328 int deleting
, creating
, overwriting
;
331 * search if variable with this name already exists
333 for (nxt
= env
= environment
.data
; *env
; env
= nxt
+ 1) {
334 for (nxt
= env
; *nxt
; ++nxt
) {
335 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
336 fprintf(stderr
, "## Error: "
337 "environment not terminated\n");
342 if ((oldval
= envmatch (name
, env
)) != NULL
)
346 deleting
= (oldval
&& !(value
&& strlen(value
)));
347 creating
= (!oldval
&& (value
&& strlen(value
)));
348 overwriting
= (oldval
&& (value
&& strlen(value
)));
350 /* check for permission */
352 if (env_flags_validate_varaccess(name
,
353 ENV_FLAGS_VARACCESS_PREVENT_DELETE
)) {
354 printf("Can't delete \"%s\"\n", name
);
358 } else if (overwriting
) {
359 if (env_flags_validate_varaccess(name
,
360 ENV_FLAGS_VARACCESS_PREVENT_OVERWR
)) {
361 printf("Can't overwrite \"%s\"\n", name
);
364 } else if (env_flags_validate_varaccess(name
,
365 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR
)) {
366 const char *defval
= fw_getdefenv(name
);
370 if (strcmp(oldval
, defval
)
372 printf("Can't overwrite \"%s\"\n", name
);
377 } else if (creating
) {
378 if (env_flags_validate_varaccess(name
,
379 ENV_FLAGS_VARACCESS_PREVENT_CREATE
)) {
380 printf("Can't create \"%s\"\n", name
);
388 if (deleting
|| overwriting
) {
389 if (*++nxt
== '\0') {
394 if ((*env
== '\0') && (*nxt
== '\0'))
403 if (!value
|| !strlen(value
))
407 * Append new definition at the end
409 for (env
= environment
.data
; *env
|| *(env
+ 1); ++env
);
410 if (env
> environment
.data
)
414 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
416 len
= strlen (name
) + 2;
417 /* add '=' for first arg, ' ' for all others */
418 len
+= strlen(value
) + 1;
420 if (len
> (&environment
.data
[ENV_SIZE
] - env
)) {
422 "Error: environment overflow, \"%s\" deleted\n",
427 while ((*env
= *name
++) != '\0')
430 while ((*++env
= *value
++) != '\0')
433 /* end is marked with double '\0' */
440 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
442 * EINVAL - need at least 1 argument
443 * EROFS - certain variables ("ethaddr", "serial#") cannot be
444 * modified or deleted
447 int fw_setenv(int argc
, char *argv
[], struct env_opts
*opts
)
456 fprintf(stderr
, "## Error: variable name missing\n");
461 if (fw_env_open(opts
)) {
462 fprintf(stderr
, "Error: environment not initialized\n");
470 if (env_flags_validate_env_set_params(name
, valv
, valc
) < 0)
474 for (i
= 0; i
< valc
; ++i
) {
476 size_t val_len
= strlen(val
);
479 value
[len
- 1] = ' ';
480 value
= realloc(value
, len
+ val_len
+ 1);
483 "Cannot malloc %zu bytes: %s\n",
484 len
, strerror(errno
));
488 memcpy(value
+ len
, val
, val_len
);
493 fw_env_write(name
, value
);
497 return fw_env_close(opts
);
501 * Parse a file and configure the u-boot variables.
502 * The script file has a very simple format, as follows:
504 * Each line has a couple with name, value:
505 * <white spaces>variable_name<white spaces>variable_value
507 * Both variable_name and variable_value are interpreted as strings.
508 * Any character after <white spaces> and before ending \r\n is interpreted
509 * as variable's value (no comment allowed on these lines !)
511 * Comments are allowed if the first character in the line is #
513 * Returns -1 and sets errno error codes:
517 int fw_parse_script(char *fname
, struct env_opts
*opts
)
520 char dump
[1024]; /* Maximum line length in the file */
527 if (fw_env_open(opts
)) {
528 fprintf(stderr
, "Error: environment not initialized\n");
532 if (strcmp(fname
, "-") == 0)
535 fp
= fopen(fname
, "r");
537 fprintf(stderr
, "I cannot open %s for reading\n",
543 while (fgets(dump
, sizeof(dump
), fp
)) {
548 * Read a whole line from the file. If the line is too long
549 * or is not terminated, reports an error and exit.
551 if (dump
[len
- 1] != '\n') {
553 "Line %d not corrected terminated or too long\n",
559 /* Drop ending line feed / carriage return */
561 if (len
&& dump
[len
- 1] == '\r')
564 /* Skip comment or empty lines */
565 if (len
== 0 || dump
[0] == '#')
569 * Search for variable's name,
570 * remove leading whitespaces
572 name
= skip_blanks(dump
);
576 /* The first white space is the end of variable name */
577 val
= skip_chars(name
);
581 if ((val
- name
) < len
)
582 val
= skip_blanks(val
);
588 fprintf(stderr
, "Setting %s : %s\n",
589 name
, val
? val
: " removed");
592 if (env_flags_validate_type(name
, val
) < 0) {
598 * If there is an error setting a variable,
599 * try to save the environment and returns an error
601 if (fw_env_write(name
, val
)) {
603 "fw_env_write returns with error : %s\n",
611 /* Close file if not stdin */
612 if (strcmp(fname
, "-") != 0)
615 ret
|= fw_env_close(opts
);
621 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
624 * < 0 - failed to test
626 static int flash_bad_block (int fd
, uint8_t mtd_type
, loff_t
*blockstart
)
628 if (mtd_type
== MTD_NANDFLASH
) {
629 int badblock
= ioctl (fd
, MEMGETBADBLOCK
, blockstart
);
632 perror ("Cannot read bad block mark");
638 fprintf (stderr
, "Bad block at 0x%llx, "
639 "skipping\n", *blockstart
);
649 * Read data from flash at an offset into a provided buffer. On NAND it skips
650 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
651 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
653 static int flash_read_buf (int dev
, int fd
, void *buf
, size_t count
,
654 off_t offset
, uint8_t mtd_type
)
656 size_t blocklen
; /* erase / write length - one block on NAND,
658 size_t processed
= 0; /* progress counter */
659 size_t readlen
= count
; /* current read length */
660 off_t top_of_range
; /* end of the last block we may use */
661 off_t block_seek
; /* offset inside the current block to the start
663 loff_t blockstart
; /* running start of the current block -
664 MEMGETBADBLOCK needs 64 bits */
667 blockstart
= (offset
/ DEVESIZE (dev
)) * DEVESIZE (dev
);
669 /* Offset inside a block */
670 block_seek
= offset
- blockstart
;
672 if (mtd_type
== MTD_NANDFLASH
) {
674 * NAND: calculate which blocks we are reading. We have
675 * to read one block at a time to skip bad blocks.
677 blocklen
= DEVESIZE (dev
);
680 * To calculate the top of the range, we have to use the
681 * global DEVOFFSET (dev), which can be different from offset
683 top_of_range
= ((DEVOFFSET(dev
) / blocklen
) +
684 ENVSECTORS (dev
)) * blocklen
;
686 /* Limit to one block for the first read */
687 if (readlen
> blocklen
- block_seek
)
688 readlen
= blocklen
- block_seek
;
691 top_of_range
= offset
+ count
;
694 /* This only runs once on NOR flash */
695 while (processed
< count
) {
696 rc
= flash_bad_block (fd
, mtd_type
, &blockstart
);
697 if (rc
< 0) /* block test failed */
700 if (blockstart
+ block_seek
+ readlen
> top_of_range
) {
701 /* End of range is reached */
703 "Too few good blocks within range\n");
707 if (rc
) { /* block is bad */
708 blockstart
+= blocklen
;
713 * If a block is bad, we retry in the next block at the same
714 * offset - see common/env_nand.c::writeenv()
716 lseek (fd
, blockstart
+ block_seek
, SEEK_SET
);
718 rc
= read (fd
, buf
+ processed
, readlen
);
720 fprintf (stderr
, "Read error on %s: %s\n",
721 DEVNAME (dev
), strerror (errno
));
725 fprintf(stderr
, "Read 0x%x bytes at 0x%llx on %s\n",
726 rc
, blockstart
+ block_seek
, DEVNAME(dev
));
728 processed
+= readlen
;
729 readlen
= min (blocklen
, count
- processed
);
731 blockstart
+= blocklen
;
738 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
739 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
740 * erase and write the whole data at once.
742 static int flash_write_buf (int dev
, int fd
, void *buf
, size_t count
,
743 off_t offset
, uint8_t mtd_type
)
746 struct erase_info_user erase
;
747 size_t blocklen
; /* length of NAND block / NOR erase sector */
748 size_t erase_len
; /* whole area that can be erased - may include
750 size_t erasesize
; /* erase / write length - one block on NAND,
752 size_t processed
= 0; /* progress counter */
753 size_t write_total
; /* total size to actually write - excluding
755 off_t erase_offset
; /* offset to the first erase block (aligned)
757 off_t block_seek
; /* offset inside the erase block to the start
759 off_t top_of_range
; /* end of the last block we may use */
760 loff_t blockstart
; /* running start of the current block -
761 MEMGETBADBLOCK needs 64 bits */
765 * For mtd devices only offset and size of the environment do matter
767 if (mtd_type
== MTD_ABSENT
) {
769 top_of_range
= offset
+ count
;
770 erase_len
= blocklen
;
773 write_total
= blocklen
;
775 blocklen
= DEVESIZE(dev
);
777 top_of_range
= ((DEVOFFSET(dev
) / blocklen
) +
778 ENVSECTORS(dev
)) * blocklen
;
780 erase_offset
= (offset
/ blocklen
) * blocklen
;
782 /* Maximum area we may use */
783 erase_len
= top_of_range
- erase_offset
;
785 blockstart
= erase_offset
;
786 /* Offset inside a block */
787 block_seek
= offset
- erase_offset
;
790 * Data size we actually write: from the start of the block
791 * to the start of the data, then count bytes of data, and
792 * to the end of the block
794 write_total
= ((block_seek
+ count
+ blocklen
- 1) /
795 blocklen
) * blocklen
;
799 * Support data anywhere within erase sectors: read out the complete
800 * area to be erased, replace the environment image, write the whole
803 if (write_total
> count
) {
804 data
= malloc (erase_len
);
807 "Cannot malloc %zu bytes: %s\n",
808 erase_len
, strerror (errno
));
812 rc
= flash_read_buf (dev
, fd
, data
, write_total
, erase_offset
,
814 if (write_total
!= rc
)
818 fprintf(stderr
, "Preserving data ");
820 fprintf(stderr
, "0x%x - 0x%lx", 0, block_seek
- 1);
821 if (block_seek
+ count
!= write_total
) {
823 fprintf(stderr
, " and ");
824 fprintf(stderr
, "0x%lx - 0x%x",
825 block_seek
+ count
, write_total
- 1);
827 fprintf(stderr
, "\n");
829 /* Overwrite the old environment */
830 memcpy (data
+ block_seek
, buf
, count
);
833 * We get here, iff offset is block-aligned and count is a
834 * multiple of blocklen - see write_total calculation above
839 if (mtd_type
== MTD_NANDFLASH
) {
841 * NAND: calculate which blocks we are writing. We have
842 * to write one block at a time to skip bad blocks.
844 erasesize
= blocklen
;
846 erasesize
= erase_len
;
849 erase
.length
= erasesize
;
851 /* This only runs once on NOR flash and SPI-dataflash */
852 while (processed
< write_total
) {
853 rc
= flash_bad_block (fd
, mtd_type
, &blockstart
);
854 if (rc
< 0) /* block test failed */
857 if (blockstart
+ erasesize
> top_of_range
) {
858 fprintf (stderr
, "End of range reached, aborting\n");
862 if (rc
) { /* block is bad */
863 blockstart
+= blocklen
;
867 if (mtd_type
!= MTD_ABSENT
) {
868 erase
.start
= blockstart
;
869 ioctl(fd
, MEMUNLOCK
, &erase
);
870 /* These do not need an explicit erase cycle */
871 if (mtd_type
!= MTD_DATAFLASH
)
872 if (ioctl(fd
, MEMERASE
, &erase
) != 0) {
874 "MTD erase error on %s: %s\n",
875 DEVNAME(dev
), strerror(errno
));
880 if (lseek (fd
, blockstart
, SEEK_SET
) == -1) {
882 "Seek error on %s: %s\n",
883 DEVNAME (dev
), strerror (errno
));
888 fprintf(stderr
, "Write 0x%x bytes at 0x%llx\n", erasesize
,
891 if (write (fd
, data
+ processed
, erasesize
) != erasesize
) {
892 fprintf (stderr
, "Write error on %s: %s\n",
893 DEVNAME (dev
), strerror (errno
));
897 if (mtd_type
!= MTD_ABSENT
)
898 ioctl(fd
, MEMLOCK
, &erase
);
900 processed
+= erasesize
;
902 blockstart
+= erasesize
;
905 if (write_total
> count
)
912 * Set obsolete flag at offset - NOR flash only
914 static int flash_flag_obsolete (int dev
, int fd
, off_t offset
)
917 struct erase_info_user erase
;
919 erase
.start
= DEVOFFSET (dev
);
920 erase
.length
= DEVESIZE (dev
);
921 /* This relies on the fact, that obsolete_flag == 0 */
922 rc
= lseek (fd
, offset
, SEEK_SET
);
924 fprintf (stderr
, "Cannot seek to set the flag on %s \n",
928 ioctl (fd
, MEMUNLOCK
, &erase
);
929 rc
= write (fd
, &obsolete_flag
, sizeof (obsolete_flag
));
930 ioctl (fd
, MEMLOCK
, &erase
);
932 perror ("Could not set obsolete flag");
937 /* Encrypt or decrypt the environment before writing or reading it. */
938 static int env_aes_cbc_crypt(char *payload
, const int enc
, uint8_t *key
)
940 uint8_t *data
= (uint8_t *)payload
;
941 const int len
= usable_envsize
;
942 uint8_t key_exp
[AES_EXPAND_KEY_LENGTH
];
945 /* First we expand the key. */
946 aes_expand_key(key
, key_exp
);
948 /* Calculate the number of AES blocks to encrypt. */
949 aes_blocks
= DIV_ROUND_UP(len
, AES_KEY_LENGTH
);
952 aes_cbc_encrypt_blocks(key_exp
, data
, data
, aes_blocks
);
954 aes_cbc_decrypt_blocks(key_exp
, data
, data
, aes_blocks
);
959 static int flash_write (int fd_current
, int fd_target
, int dev_target
)
963 switch (environment
.flag_scheme
) {
966 case FLAG_INCREMENTAL
:
967 (*environment
.flags
)++;
970 *environment
.flags
= active_flag
;
973 fprintf (stderr
, "Unimplemented flash scheme %u \n",
974 environment
.flag_scheme
);
979 fprintf(stderr
, "Writing new environment at 0x%lx on %s\n",
980 DEVOFFSET (dev_target
), DEVNAME (dev_target
));
983 rc
= flash_write_buf(dev_target
, fd_target
, environment
.image
,
984 CUR_ENVSIZE
, DEVOFFSET(dev_target
),
985 DEVTYPE(dev_target
));
989 if (environment
.flag_scheme
== FLAG_BOOLEAN
) {
990 /* Have to set obsolete flag */
991 off_t offset
= DEVOFFSET (dev_current
) +
992 offsetof (struct env_image_redundant
, flags
);
995 "Setting obsolete flag in environment at 0x%lx on %s\n",
996 DEVOFFSET (dev_current
), DEVNAME (dev_current
));
998 flash_flag_obsolete (dev_current
, fd_current
, offset
);
1004 static int flash_read (int fd
)
1006 struct mtd_info_user mtdinfo
;
1010 rc
= fstat(fd
, &st
);
1012 fprintf(stderr
, "Cannot stat the file %s\n",
1013 DEVNAME(dev_current
));
1017 if (S_ISCHR(st
.st_mode
)) {
1018 rc
= ioctl(fd
, MEMGETINFO
, &mtdinfo
);
1020 fprintf(stderr
, "Cannot get MTD information for %s\n",
1021 DEVNAME(dev_current
));
1024 if (mtdinfo
.type
!= MTD_NORFLASH
&&
1025 mtdinfo
.type
!= MTD_NANDFLASH
&&
1026 mtdinfo
.type
!= MTD_DATAFLASH
&&
1027 mtdinfo
.type
!= MTD_UBIVOLUME
) {
1028 fprintf (stderr
, "Unsupported flash type %u on %s\n",
1029 mtdinfo
.type
, DEVNAME(dev_current
));
1033 memset(&mtdinfo
, 0, sizeof(mtdinfo
));
1034 mtdinfo
.type
= MTD_ABSENT
;
1037 DEVTYPE(dev_current
) = mtdinfo
.type
;
1039 rc
= flash_read_buf(dev_current
, fd
, environment
.image
, CUR_ENVSIZE
,
1040 DEVOFFSET (dev_current
), mtdinfo
.type
);
1041 if (rc
!= CUR_ENVSIZE
)
1047 static int flash_io (int mode
)
1049 int fd_current
, fd_target
, rc
, dev_target
;
1051 /* dev_current: fd_current, erase_current */
1052 fd_current
= open (DEVNAME (dev_current
), mode
);
1053 if (fd_current
< 0) {
1055 "Can't open %s: %s\n",
1056 DEVNAME (dev_current
), strerror (errno
));
1060 if (mode
== O_RDWR
) {
1061 if (HaveRedundEnv
) {
1062 /* switch to next partition for writing */
1063 dev_target
= !dev_current
;
1064 /* dev_target: fd_target, erase_target */
1065 fd_target
= open (DEVNAME (dev_target
), mode
);
1066 if (fd_target
< 0) {
1068 "Can't open %s: %s\n",
1069 DEVNAME (dev_target
),
1075 dev_target
= dev_current
;
1076 fd_target
= fd_current
;
1079 rc
= flash_write (fd_current
, fd_target
, dev_target
);
1081 if (HaveRedundEnv
) {
1082 if (close (fd_target
)) {
1084 "I/O error on %s: %s\n",
1085 DEVNAME (dev_target
),
1091 rc
= flash_read (fd_current
);
1095 if (close (fd_current
)) {
1097 "I/O error on %s: %s\n",
1098 DEVNAME (dev_current
), strerror (errno
));
1106 * s1 is either a simple 'name', or a 'name=value' pair.
1107 * s2 is a 'name=value' pair.
1108 * If the names match, return the value of s2, else NULL.
1111 static char *envmatch (char * s1
, char * s2
)
1113 if (s1
== NULL
|| s2
== NULL
)
1116 while (*s1
== *s2
++)
1119 if (*s1
== '\0' && *(s2
- 1) == '=')
1125 * Prevent confusion if running from erased flash memory
1127 int fw_env_open(struct env_opts
*opts
)
1130 unsigned char flag0
;
1134 unsigned char flag1
;
1139 struct env_image_single
*single
;
1140 struct env_image_redundant
*redundant
;
1142 if (parse_config(opts
)) /* should fill envdevices */
1145 addr0
= calloc(1, CUR_ENVSIZE
);
1146 if (addr0
== NULL
) {
1148 "Not enough memory for environment (%ld bytes)\n",
1153 /* read environment from FLASH to local buffer */
1154 environment
.image
= addr0
;
1156 if (HaveRedundEnv
) {
1158 environment
.crc
= &redundant
->crc
;
1159 environment
.flags
= &redundant
->flags
;
1160 environment
.data
= redundant
->data
;
1163 environment
.crc
= &single
->crc
;
1164 environment
.flags
= NULL
;
1165 environment
.data
= single
->data
;
1169 if (flash_io (O_RDONLY
))
1172 crc0
= crc32 (0, (uint8_t *) environment
.data
, ENV_SIZE
);
1174 if (opts
->aes_flag
) {
1175 ret
= env_aes_cbc_crypt(environment
.data
, 0,
1181 crc0_ok
= (crc0
== *environment
.crc
);
1182 if (!HaveRedundEnv
) {
1185 "Warning: Bad CRC, using default environment\n");
1186 memcpy(environment
.data
, default_environment
, sizeof default_environment
);
1189 flag0
= *environment
.flags
;
1192 addr1
= calloc(1, CUR_ENVSIZE
);
1193 if (addr1
== NULL
) {
1195 "Not enough memory for environment (%ld bytes)\n",
1202 * have to set environment.image for flash_read(), careful -
1203 * other pointers in environment still point inside addr0
1205 environment
.image
= addr1
;
1206 if (flash_io (O_RDONLY
))
1209 /* Check flag scheme compatibility */
1210 if (DEVTYPE(dev_current
) == MTD_NORFLASH
&&
1211 DEVTYPE(!dev_current
) == MTD_NORFLASH
) {
1212 environment
.flag_scheme
= FLAG_BOOLEAN
;
1213 } else if (DEVTYPE(dev_current
) == MTD_NANDFLASH
&&
1214 DEVTYPE(!dev_current
) == MTD_NANDFLASH
) {
1215 environment
.flag_scheme
= FLAG_INCREMENTAL
;
1216 } else if (DEVTYPE(dev_current
) == MTD_DATAFLASH
&&
1217 DEVTYPE(!dev_current
) == MTD_DATAFLASH
) {
1218 environment
.flag_scheme
= FLAG_BOOLEAN
;
1219 } else if (DEVTYPE(dev_current
) == MTD_UBIVOLUME
&&
1220 DEVTYPE(!dev_current
) == MTD_UBIVOLUME
) {
1221 environment
.flag_scheme
= FLAG_INCREMENTAL
;
1222 } else if (DEVTYPE(dev_current
) == MTD_ABSENT
&&
1223 DEVTYPE(!dev_current
) == MTD_ABSENT
) {
1224 environment
.flag_scheme
= FLAG_INCREMENTAL
;
1226 fprintf (stderr
, "Incompatible flash types!\n");
1230 crc1
= crc32 (0, (uint8_t *) redundant
->data
, ENV_SIZE
);
1232 if (opts
->aes_flag
) {
1233 ret
= env_aes_cbc_crypt(redundant
->data
, 0,
1239 crc1_ok
= (crc1
== redundant
->crc
);
1240 flag1
= redundant
->flags
;
1242 if (crc0_ok
&& !crc1_ok
) {
1244 } else if (!crc0_ok
&& crc1_ok
) {
1246 } else if (!crc0_ok
&& !crc1_ok
) {
1248 "Warning: Bad CRC, using default environment\n");
1249 memcpy (environment
.data
, default_environment
,
1250 sizeof default_environment
);
1253 switch (environment
.flag_scheme
) {
1255 if (flag0
== active_flag
&&
1256 flag1
== obsolete_flag
) {
1258 } else if (flag0
== obsolete_flag
&&
1259 flag1
== active_flag
) {
1261 } else if (flag0
== flag1
) {
1263 } else if (flag0
== 0xFF) {
1265 } else if (flag1
== 0xFF) {
1271 case FLAG_INCREMENTAL
:
1272 if (flag0
== 255 && flag1
== 0)
1274 else if ((flag1
== 255 && flag0
== 0) ||
1277 else /* flag1 > flag0 */
1281 fprintf (stderr
, "Unknown flag scheme %u \n",
1282 environment
.flag_scheme
);
1288 * If we are reading, we don't need the flag and the CRC any
1289 * more, if we are writing, we will re-calculate CRC and update
1290 * flags before writing out
1293 environment
.image
= addr1
;
1294 environment
.crc
= &redundant
->crc
;
1295 environment
.flags
= &redundant
->flags
;
1296 environment
.data
= redundant
->data
;
1299 environment
.image
= addr0
;
1300 /* Other pointers are already set */
1304 fprintf(stderr
, "Selected env in %s\n", DEVNAME(dev_current
));
1311 static int parse_config(struct env_opts
*opts
)
1315 #if defined(CONFIG_FILE)
1316 if (!common_args
.config_file
)
1317 common_args
.config_file
= CONFIG_FILE
;
1319 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
1320 if (get_config(opts
->config_file
)) {
1321 fprintf(stderr
, "Cannot parse config file '%s': %m\n",
1326 DEVNAME (0) = DEVICE1_NAME
;
1327 DEVOFFSET (0) = DEVICE1_OFFSET
;
1328 ENVSIZE (0) = ENV1_SIZE
;
1329 /* Default values are: erase-size=env-size */
1330 DEVESIZE (0) = ENVSIZE (0);
1331 /* #sectors=env-size/erase-size (rounded up) */
1332 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
1333 #ifdef DEVICE1_ESIZE
1334 DEVESIZE (0) = DEVICE1_ESIZE
;
1336 #ifdef DEVICE1_ENVSECTORS
1337 ENVSECTORS (0) = DEVICE1_ENVSECTORS
;
1341 DEVNAME (1) = DEVICE2_NAME
;
1342 DEVOFFSET (1) = DEVICE2_OFFSET
;
1343 ENVSIZE (1) = ENV2_SIZE
;
1344 /* Default values are: erase-size=env-size */
1345 DEVESIZE (1) = ENVSIZE (1);
1346 /* #sectors=env-size/erase-size (rounded up) */
1347 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
1348 #ifdef DEVICE2_ESIZE
1349 DEVESIZE (1) = DEVICE2_ESIZE
;
1351 #ifdef DEVICE2_ENVSECTORS
1352 ENVSECTORS (1) = DEVICE2_ENVSECTORS
;
1357 if (stat (DEVNAME (0), &st
)) {
1359 "Cannot access MTD device %s: %s\n",
1360 DEVNAME (0), strerror (errno
));
1364 if (HaveRedundEnv
&& stat (DEVNAME (1), &st
)) {
1366 "Cannot access MTD device %s: %s\n",
1367 DEVNAME (1), strerror (errno
));
1371 if (HaveRedundEnv
&& ENVSIZE(0) != ENVSIZE(1)) {
1372 ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1));
1374 "Redundant environments have inequal size, set to 0x%08lx\n",
1378 usable_envsize
= CUR_ENVSIZE
- sizeof(uint32_t);
1380 usable_envsize
-= sizeof(char);
1383 usable_envsize
&= ~(AES_KEY_LENGTH
- 1);
1388 #if defined(CONFIG_FILE)
1389 static int get_config (char *fname
)
1397 fp
= fopen (fname
, "r");
1401 while (i
< 2 && fgets (dump
, sizeof (dump
), fp
)) {
1402 /* Skip incomplete conversions and comment strings */
1406 rc
= sscanf (dump
, "%ms %lx %lx %lx %lx",
1416 DEVNAME(i
) = devname
;
1419 /* Assume the erase size is the same as the env-size */
1420 DEVESIZE(i
) = ENVSIZE(i
);
1423 /* Assume enough env sectors to cover the environment */
1424 ENVSECTORS (i
) = (ENVSIZE(i
) + DEVESIZE(i
) - 1) / DEVESIZE(i
);
1430 HaveRedundEnv
= i
- 1;
1431 if (!i
) { /* No valid entries found */