2 * (C) Copyright 2000-2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
36 # include <linux/mtd/mtd.h>
38 # define __user /* nothing */
39 # include <mtd/mtd-user.h>
44 #define CMD_GETENV "fw_printenv"
45 #define CMD_SETENV "fw_setenv"
47 typedef struct envdev_s
{
48 char devname
[16]; /* Device name */
49 ulong devoff
; /* Device offset */
50 ulong env_size
; /* environment size */
51 ulong erase_size
; /* device erase size */
54 static envdev_t envdevices
[2];
57 #define DEVNAME(i) envdevices[(i)].devname
58 #define DEVOFFSET(i) envdevices[(i)].devoff
59 #define ENVSIZE(i) envdevices[(i)].env_size
60 #define DEVESIZE(i) envdevices[(i)].erase_size
62 #define CFG_ENV_SIZE ENVSIZE(curdev)
64 #define ENV_SIZE getenvsize()
66 typedef struct environment_s
{
67 ulong crc
; /* CRC32 over data bytes */
68 unsigned char flags
; /* active or obsolete */
72 static env_t environment
;
74 static int HaveRedundEnv
= 0;
76 static unsigned char active_flag
= 1;
77 static unsigned char obsolete_flag
= 0;
81 #define MK_STR(x) XMK_STR(x)
83 static char default_environment
[] = {
84 #if defined(CONFIG_BOOTARGS)
85 "bootargs=" CONFIG_BOOTARGS
"\0"
87 #if defined(CONFIG_BOOTCOMMAND)
88 "bootcmd=" CONFIG_BOOTCOMMAND
"\0"
90 #if defined(CONFIG_RAMBOOTCOMMAND)
91 "ramboot=" CONFIG_RAMBOOTCOMMAND
"\0"
93 #if defined(CONFIG_NFSBOOTCOMMAND)
94 "nfsboot=" CONFIG_NFSBOOTCOMMAND
"\0"
96 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
97 "bootdelay=" MK_STR (CONFIG_BOOTDELAY
) "\0"
99 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
100 "baudrate=" MK_STR (CONFIG_BAUDRATE
) "\0"
102 #ifdef CONFIG_LOADS_ECHO
103 "loads_echo=" MK_STR (CONFIG_LOADS_ECHO
) "\0"
105 #ifdef CONFIG_ETHADDR
106 "ethaddr=" MK_STR (CONFIG_ETHADDR
) "\0"
108 #ifdef CONFIG_ETH1ADDR
109 "eth1addr=" MK_STR (CONFIG_ETH1ADDR
) "\0"
111 #ifdef CONFIG_ETH2ADDR
112 "eth2addr=" MK_STR (CONFIG_ETH2ADDR
) "\0"
114 #ifdef CONFIG_ETH3ADDR
115 "eth3addr=" MK_STR (CONFIG_ETH3ADDR
) "\0"
117 #ifdef CONFIG_ETHPRIME
118 "ethprime=" CONFIG_ETHPRIME
"\0"
121 "ipaddr=" MK_STR (CONFIG_IPADDR
) "\0"
123 #ifdef CONFIG_SERVERIP
124 "serverip=" MK_STR (CONFIG_SERVERIP
) "\0"
127 "autoload=" CFG_AUTOLOAD
"\0"
129 #ifdef CONFIG_ROOTPATH
130 "rootpath=" MK_STR (CONFIG_ROOTPATH
) "\0"
132 #ifdef CONFIG_GATEWAYIP
133 "gatewayip=" MK_STR (CONFIG_GATEWAYIP
) "\0"
135 #ifdef CONFIG_NETMASK
136 "netmask=" MK_STR (CONFIG_NETMASK
) "\0"
138 #ifdef CONFIG_HOSTNAME
139 "hostname=" MK_STR (CONFIG_HOSTNAME
) "\0"
141 #ifdef CONFIG_BOOTFILE
142 "bootfile=" MK_STR (CONFIG_BOOTFILE
) "\0"
144 #ifdef CONFIG_LOADADDR
145 "loadaddr=" MK_STR (CONFIG_LOADADDR
) "\0"
147 #ifdef CONFIG_PREBOOT
148 "preboot=" CONFIG_PREBOOT
"\0"
150 #ifdef CONFIG_CLOCKS_IN_MHZ
151 "clocks_in_mhz=" "1" "\0"
153 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
154 "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY
) "\0"
156 #ifdef CONFIG_EXTRA_ENV_SETTINGS
157 CONFIG_EXTRA_ENV_SETTINGS
159 "\0" /* Termimate env_t data with 2 NULs */
162 static int flash_io (int mode
);
163 static char *envmatch (char * s1
, char * s2
);
164 static int env_init (void);
165 static int parse_config (void);
167 #if defined(CONFIG_FILE)
168 static int get_config (char *);
170 static inline ulong
getenvsize (void)
172 ulong rc
= CFG_ENV_SIZE
- sizeof (long);
180 * Search the environment for a variable.
181 * Return the value, if found, or NULL, if not found.
183 char *fw_getenv (char *name
)
190 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
193 for (nxt
= env
; *nxt
; ++nxt
) {
194 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
195 fprintf (stderr
, "## Error: "
196 "environment not terminated\n");
200 val
= envmatch (name
, env
);
209 * Print the current definition of one, or more, or all
210 * environment variables
212 void fw_printenv (int argc
, char *argv
[])
220 if (argc
== 1) { /* Print all env variables */
221 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
222 for (nxt
= env
; *nxt
; ++nxt
) {
223 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
224 fprintf (stderr
, "## Error: "
225 "environment not terminated\n");
230 printf ("%s\n", env
);
235 if (strcmp (argv
[1], "-n") == 0) {
240 fprintf (stderr
, "## Error: "
241 "`-n' option requires exactly one argument\n");
248 for (i
= 1; i
< argc
; ++i
) { /* print single env variables */
249 char *name
= argv
[i
];
252 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
254 for (nxt
= env
; *nxt
; ++nxt
) {
255 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
256 fprintf (stderr
, "## Error: "
257 "environment not terminated\n");
261 val
= envmatch (name
, env
);
264 fputs (name
, stdout
);
272 fprintf (stderr
, "## Error: \"%s\" not defined\n", name
);
276 int fw_setone(char * name
, char * value
)
282 printf("setone %s=%s\n", name
, value
? value
: "<clear>");
284 * search if variable with this name already exists
286 for (nxt
= env
= environment
.data
; *env
; env
= nxt
+ 1) {
287 for (nxt
= env
; *nxt
; ++nxt
) {
288 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
289 fprintf (stderr
, "## Error: "
290 "environment not terminated\n");
294 if ((oldval
= envmatch (name
, env
)) != NULL
)
299 * Delete any existing definition
303 * Ethernet Address and serial# can be set only once
305 if ((strcmp (name
, "ethaddr") == 0) ||
306 (strcmp (name
, "serial#") == 0)) {
307 fprintf (stderr
, "Can't overwrite \"%s\"\n", name
);
311 if (*++nxt
== '\0') {
316 if ((*env
== '\0') && (*nxt
== '\0'))
329 * Append new definition at the end
331 for (env
= environment
.data
; *env
|| *(env
+ 1); ++env
);
332 if (env
> environment
.data
)
336 * "name" + "=" + "val" +"\0\0" > CFG_ENV_SIZE - (env-environment)
338 len
= strlen (name
) + 2 + strlen(value
);
339 /* add '=' for first arg, ' ' for all others */
341 if (len
> (&environment
.data
[ENV_SIZE
] - env
)) {
343 "Error: environment overflow, \"%s\" deleted\n",
347 while ((*env
= *name
++) != '\0')
350 while ((*env
= *value
++) != '\0')
353 /* end is marked with double '\0' */
360 * Deletes or sets environment variables. Returns errno style error codes:
362 * EINVAL - need at least 1 argument
363 * EROFS - certain variables ("ethaddr", "serial#") cannot be
364 * modified or deleted
367 int fw_setenv (int argc
, char *argv
[])
381 fw_setone(argv
[1], argc
> 2 ? argv
[2] : NULL
);
384 environment
.crc
= crc32 (0, (uint8_t*) environment
.data
, ENV_SIZE
);
386 /* write environment back to flash */
387 if (flash_io (O_RDWR
)) {
388 fprintf (stderr
, "Error: can't write fw_env to flash\n");
395 int fw_loadenv (int argc
, char *argv
[])
397 char * fname
= argc
> 1 ? argv
[1] : "/etc/fw_env.save";
398 FILE * f
= fopen(fname
, "r");
408 while (fgets(buffer
, sizeof(buffer
), f
)) {
409 if (buffer
[0] == '#' || buffer
[0] <= ' ') /* crude comments */
411 int l
= strlen(buffer
);
413 while (l
&& buffer
[l
-1] < ' ')
415 char * val
= strchr(buffer
, '=');
417 printf("Invalid environment '%s'\n", buffer
);
421 if (fw_setone(buffer
, val
))
427 environment
.crc
= crc32 (0, (uint8_t*) environment
.data
, ENV_SIZE
);
432 /* write environment back to flash */
433 if (flash_io (O_RDWR
)) {
434 fprintf (stderr
, "Error: can't write fw_env to flash\n");
440 static int flash_io (int mode
)
442 int fd
, fdr
, rc
, otherdev
, len
, resid
;
446 if ((fd
= open (DEVNAME (curdev
), mode
)) < 0) {
448 "Can't open %s: %s\n",
449 DEVNAME (curdev
), strerror (errno
));
453 len
= sizeof (environment
.crc
);
455 len
+= sizeof (environment
.flags
);
459 unsigned char *block
= malloc(ENV_SIZE
+ len
);
465 if (mode
== O_RDWR
) {
467 /* switch to next partition for writing */
469 if ((fdr
= open (DEVNAME (otherdev
), mode
)) < 0) {
471 "Can't open %s: %s\n",
480 printf ("Unlocking flash...\n");
481 erase
.length
= DEVESIZE (otherdev
);
482 erase
.start
= DEVOFFSET (otherdev
);
483 ioctl (fdr
, MEMUNLOCK
, &erase
);
486 erase
.length
= DEVESIZE (curdev
);
487 erase
.start
= DEVOFFSET (curdev
);
488 ioctl (fd
, MEMUNLOCK
, &erase
);
489 environment
.flags
= active_flag
;
493 resid
= DEVESIZE (otherdev
) - CFG_ENV_SIZE
;
495 if ((data
= malloc (resid
)) == NULL
) {
497 "Cannot malloc %d bytes: %s\n",
502 if (lseek (fdr
, DEVOFFSET (otherdev
) + CFG_ENV_SIZE
, SEEK_SET
)
504 fprintf (stderr
, "seek error on %s: %s\n",
509 if ((rc
= read (fdr
, data
, resid
)) != resid
) {
511 "read error on %s: %s\n",
518 printf ("Erasing old environment...\n");
520 erase
.length
= DEVESIZE (otherdev
);
521 erase
.start
= DEVOFFSET (otherdev
);
522 if (ioctl (fdr
, MEMERASE
, &erase
) != 0) {
523 fprintf (stderr
, "MTD erase error on %s: %s\n",
531 printf ("Writing environment to %s...\n", DEVNAME (otherdev
));
532 if (lseek (fdr
, DEVOFFSET (otherdev
), SEEK_SET
) == -1) {
534 "seek error on %s: %s\n",
535 DEVNAME (otherdev
), strerror (errno
));
538 // Michel Pollet <buserror@gmail.com>
539 // fix the unaligned write that no longer work on modern
542 memcpy(block
, &environment
, len
);
543 memcpy(block
+ len
, environment
.data
, ENV_SIZE
);
544 if (write (fdr
, block
, ENV_SIZE
+ len
) != ENV_SIZE
+ len
) {
546 "write error on %s: %s (%x write)\n",
547 DEVNAME (otherdev
), strerror (errno
), ENV_SIZE
+ len
);
551 if (write (fdr
, &environment
, len
) != len
) {
553 "CRC write error on %s: %s\n",
554 DEVNAME (otherdev
), strerror (errno
));
557 if (write (fdr
, environment
.data
, ENV_SIZE
) != ENV_SIZE
) {
559 "Write error on %s: %s\n",
560 DEVNAME (otherdev
), strerror (errno
));
565 if (write (fdr
, data
, resid
) != resid
) {
567 "write error on %s: %s\n",
568 DEVNAME (curdev
), strerror (errno
));
574 /* change flag on current active env partition */
575 if (lseek (fd
, DEVOFFSET (curdev
) + sizeof (ulong
), SEEK_SET
)
577 fprintf (stderr
, "seek error on %s: %s\n",
578 DEVNAME (curdev
), strerror (errno
));
581 if (write (fd
, &obsolete_flag
, sizeof (obsolete_flag
)) !=
582 sizeof (obsolete_flag
)) {
584 "Write error on %s: %s\n",
585 DEVNAME (curdev
), strerror (errno
));
590 printf ("Locking ...\n");
591 erase
.length
= DEVESIZE (otherdev
);
592 erase
.start
= DEVOFFSET (otherdev
);
593 ioctl (fdr
, MEMLOCK
, &erase
);
595 erase
.length
= DEVESIZE (curdev
);
596 erase
.start
= DEVOFFSET (curdev
);
597 ioctl (fd
, MEMLOCK
, &erase
);
600 "I/O error on %s: %s\n",
609 if (lseek (fd
, DEVOFFSET (curdev
), SEEK_SET
) == -1) {
611 "seek error on %s: %s\n",
612 DEVNAME (curdev
), strerror (errno
));
617 if (read (fd
, block
, ENV_SIZE
+ len
) != ENV_SIZE
+ len
) {
619 "read error on %s: %s\n",
620 DEVNAME (curdev
), strerror (errno
));
623 memcpy(&environment
, block
, len
);
624 memcpy(environment
.data
, block
+ len
, ENV_SIZE
);
626 if (read (fd
, &environment
, len
) != len
) {
628 "CRC read error on %s: %s\n",
629 DEVNAME (curdev
), strerror (errno
));
632 if ((rc
= read (fd
, environment
.data
, ENV_SIZE
)) != ENV_SIZE
) {
634 "Read error on %s: %s\n",
635 DEVNAME (curdev
), strerror (errno
));
646 "I/O error on %s: %s\n",
647 DEVNAME (curdev
), strerror (errno
));
656 * s1 is either a simple 'name', or a 'name=value' pair.
657 * s2 is a 'name=value' pair.
658 * If the names match, return the value of s2, else NULL.
661 static char *envmatch (char * s1
, char * s2
)
667 if (*s1
== '\0' && *(s2
- 1) == '=')
673 * Prevent confusion if running from erased flash memory
675 static int env_init (void)
681 char flag1
, flag2
, *addr2
;
683 if (parse_config ()) /* should fill envdevices */
686 if ((addr1
= calloc (1, ENV_SIZE
)) == NULL
) {
688 "Not enough memory for environment (%ld bytes)\n",
693 /* read environment from FLASH to local buffer */
694 environment
.data
= addr1
;
696 if (flash_io (O_RDONLY
)) {
700 crc1_ok
= ((crc1
= crc32 (0, (uint8_t *) environment
.data
, ENV_SIZE
))
702 if (!HaveRedundEnv
) {
705 "Warning: Bad CRC, using default environment\n");
706 memcpy(environment
.data
, default_environment
, sizeof default_environment
);
709 flag1
= environment
.flags
;
712 if ((addr2
= calloc (1, ENV_SIZE
)) == NULL
) {
714 "Not enough memory for environment (%ld bytes)\n",
718 environment
.data
= addr2
;
720 if (flash_io (O_RDONLY
)) {
724 crc2_ok
= ((crc2
= crc32 (0, (uint8_t *) environment
.data
, ENV_SIZE
))
726 flag2
= environment
.flags
;
728 if (crc1_ok
&& !crc2_ok
) {
729 environment
.data
= addr1
;
730 environment
.flags
= flag1
;
731 environment
.crc
= crc1
;
734 } else if (!crc1_ok
&& crc2_ok
) {
735 environment
.data
= addr2
;
736 environment
.flags
= flag2
;
737 environment
.crc
= crc2
;
740 } else if (!crc1_ok
&& !crc2_ok
) {
742 "Warning: Bad CRC, using default environment\n");
743 memcpy(environment
.data
, default_environment
, sizeof default_environment
);
746 } else if (flag1
== active_flag
&& flag2
== obsolete_flag
) {
747 environment
.data
= addr1
;
748 environment
.flags
= flag1
;
749 environment
.crc
= crc1
;
752 } else if (flag1
== obsolete_flag
&& flag2
== active_flag
) {
753 environment
.data
= addr2
;
754 environment
.flags
= flag2
;
755 environment
.crc
= crc2
;
758 } else if (flag1
== flag2
) {
759 environment
.data
= addr1
;
760 environment
.flags
= flag1
;
761 environment
.crc
= crc1
;
764 } else if (flag1
== 0xFF) {
765 environment
.data
= addr1
;
766 environment
.flags
= flag1
;
767 environment
.crc
= crc1
;
770 } else if (flag2
== 0xFF) {
771 environment
.data
= addr2
;
772 environment
.flags
= flag2
;
773 environment
.crc
= crc2
;
782 static int parse_config ()
786 #if defined(CONFIG_FILE)
787 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
788 if (get_config (CONFIG_FILE
)) {
790 "Cannot parse config file: %s\n", strerror (errno
));
794 strcpy (DEVNAME (0), DEVICE1_NAME
);
795 DEVOFFSET (0) = DEVICE1_OFFSET
;
796 ENVSIZE (0) = ENV1_SIZE
;
797 DEVESIZE (0) = DEVICE1_ESIZE
;
799 strcpy (DEVNAME (1), DEVICE2_NAME
);
800 DEVOFFSET (1) = DEVICE2_OFFSET
;
801 ENVSIZE (1) = ENV2_SIZE
;
802 DEVESIZE (1) = DEVICE2_ESIZE
;
806 if (stat (DEVNAME (0), &st
)) {
808 "Cannot access MTD device %s: %s\n",
809 DEVNAME (0), strerror (errno
));
813 if (HaveRedundEnv
&& stat (DEVNAME (1), &st
)) {
815 "Cannot access MTD device %s: %s\n",
816 DEVNAME (1), strerror (errno
));
822 #if defined(CONFIG_FILE)
823 static int get_config (char *fname
)
830 if ((fp
= fopen (fname
, "r")) == NULL
) {
834 while ((i
< 2) && ((rc
= fscanf (fp
, "%s %lx %lx %lx",
838 &DEVESIZE (i
) )) != EOF
)) {
840 /* Skip incomplete conversions and comment strings */
841 if ((rc
< 3) || (*DEVNAME (i
) == '#')) {
842 fgets (dump
, sizeof (dump
), fp
); /* Consume till end */
850 HaveRedundEnv
= i
- 1;
851 if (!i
) { /* No valid entries found */