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>
34 #include <linux/mtd/mtd.h>
37 typedef unsigned char uchar
;
39 #define CMD_GETENV "fw_printenv"
40 #define CMD_SETENV "fw_setenv"
42 typedef struct envdev_s
{
43 uchar devname
[16]; /* Device name */
44 ulong devoff
; /* Device offset */
45 ulong env_size
; /* environment size */
46 ulong erase_size
; /* device erase size */
49 static envdev_t envdevices
[2];
52 #define DEVNAME(i) envdevices[(i)].devname
53 #define DEVOFFSET(i) envdevices[(i)].devoff
54 #define ENVSIZE(i) envdevices[(i)].env_size
55 #define DEVESIZE(i) envdevices[(i)].erase_size
57 #define CFG_ENV_SIZE ENVSIZE(curdev)
59 #define ENV_SIZE getenvsize()
61 typedef struct environment_s
{
62 ulong crc
; /* CRC32 over data bytes */
63 uchar flags
; /* active or obsolete */
67 static env_t environment
;
69 static int HaveRedundEnv
= 0;
71 static uchar active_flag
= 1;
72 static uchar obsolete_flag
= 0;
76 #define MK_STR(x) XMK_STR(x)
78 static uchar default_environment
[] = {
79 #if defined(CONFIG_BOOTARGS)
80 "bootargs=" CONFIG_BOOTARGS
"\0"
82 #if defined(CONFIG_BOOTCOMMAND)
83 "bootcmd=" CONFIG_BOOTCOMMAND
"\0"
85 #if defined(CONFIG_RAMBOOTCOMMAND)
86 "ramboot=" CONFIG_RAMBOOTCOMMAND
"\0"
88 #if defined(CONFIG_NFSBOOTCOMMAND)
89 "nfsboot=" CONFIG_NFSBOOTCOMMAND
"\0"
91 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
92 "bootdelay=" MK_STR (CONFIG_BOOTDELAY
) "\0"
94 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
95 "baudrate=" MK_STR (CONFIG_BAUDRATE
) "\0"
97 #ifdef CONFIG_LOADS_ECHO
98 "loads_echo=" MK_STR (CONFIG_LOADS_ECHO
) "\0"
100 #ifdef CONFIG_ETHADDR
101 "ethaddr=" MK_STR (CONFIG_ETHADDR
) "\0"
103 #ifdef CONFIG_ETH1ADDR
104 "eth1addr=" MK_STR (CONFIG_ETH1ADDR
) "\0"
106 #ifdef CONFIG_ETH2ADDR
107 "eth2addr=" MK_STR (CONFIG_ETH2ADDR
) "\0"
109 #ifdef CONFIG_ETH3ADDR
110 "eth3addr=" MK_STR (CONFIG_ETH3ADDR
) "\0"
112 #ifdef CONFIG_ETHPRIME
113 "ethprime=" CONFIG_ETHPRIME
"\0"
116 "ipaddr=" MK_STR (CONFIG_IPADDR
) "\0"
118 #ifdef CONFIG_SERVERIP
119 "serverip=" MK_STR (CONFIG_SERVERIP
) "\0"
122 "autoload=" CFG_AUTOLOAD
"\0"
124 #ifdef CONFIG_ROOTPATH
125 "rootpath=" MK_STR (CONFIG_ROOTPATH
) "\0"
127 #ifdef CONFIG_GATEWAYIP
128 "gatewayip=" MK_STR (CONFIG_GATEWAYIP
) "\0"
130 #ifdef CONFIG_NETMASK
131 "netmask=" MK_STR (CONFIG_NETMASK
) "\0"
133 #ifdef CONFIG_HOSTNAME
134 "hostname=" MK_STR (CONFIG_HOSTNAME
) "\0"
136 #ifdef CONFIG_BOOTFILE
137 "bootfile=" MK_STR (CONFIG_BOOTFILE
) "\0"
139 #ifdef CONFIG_LOADADDR
140 "loadaddr=" MK_STR (CONFIG_LOADADDR
) "\0"
142 #ifdef CONFIG_PREBOOT
143 "preboot=" CONFIG_PREBOOT
"\0"
145 #ifdef CONFIG_CLOCKS_IN_MHZ
146 "clocks_in_mhz=" "1" "\0"
148 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
149 "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY
) "\0"
151 #ifdef CONFIG_EXTRA_ENV_SETTINGS
152 CONFIG_EXTRA_ENV_SETTINGS
154 "\0" /* Termimate env_t data with 2 NULs */
157 static int flash_io (int mode
);
158 static uchar
*envmatch (uchar
* s1
, uchar
* s2
);
159 static int env_init (void);
160 static int parse_config (void);
162 #if defined(CONFIG_FILE)
163 static int get_config (char *);
165 static inline ulong
getenvsize (void)
167 ulong rc
= CFG_ENV_SIZE
- sizeof (long);
175 * Search the environment for a variable.
176 * Return the value, if found, or NULL, if not found.
178 unsigned char *fw_getenv (unsigned char *name
)
185 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
188 for (nxt
= env
; *nxt
; ++nxt
) {
189 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
190 fprintf (stderr
, "## Error: "
191 "environment not terminated\n");
195 val
= envmatch (name
, env
);
204 * Print the current definition of one, or more, or all
205 * environment variables
207 void fw_printenv (int argc
, char *argv
[])
215 if (argc
== 1) { /* Print all env variables */
216 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
217 for (nxt
= env
; *nxt
; ++nxt
) {
218 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
219 fprintf (stderr
, "## Error: "
220 "environment not terminated\n");
225 printf ("%s\n", env
);
230 if (strcmp (argv
[1], "-n") == 0) {
235 fprintf (stderr
, "## Error: "
236 "`-n' option requires exactly one argument\n");
243 for (i
= 1; i
< argc
; ++i
) { /* print single env variables */
244 uchar
*name
= argv
[i
];
247 for (env
= environment
.data
; *env
; env
= nxt
+ 1) {
249 for (nxt
= env
; *nxt
; ++nxt
) {
250 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
251 fprintf (stderr
, "## Error: "
252 "environment not terminated\n");
256 val
= envmatch (name
, env
);
259 fputs (name
, stdout
);
267 fprintf (stderr
, "## Error: \"%s\" not defined\n", name
);
272 * Deletes or sets environment variables. Returns errno style error codes:
274 * EINVAL - need at least 1 argument
275 * EROFS - certain variables ("ethaddr", "serial#") cannot be
276 * modified or deleted
279 int fw_setenv (int argc
, char *argv
[])
283 uchar
*oldval
= NULL
;
296 * search if variable with this name already exists
298 for (nxt
= 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");
306 if ((oldval
= envmatch (name
, env
)) != NULL
)
311 * Delete any existing definition
315 * Ethernet Address and serial# can be set only once
317 if ((strcmp (name
, "ethaddr") == 0) ||
318 (strcmp (name
, "serial#") == 0)) {
319 fprintf (stderr
, "Can't overwrite \"%s\"\n", name
);
323 if (*++nxt
== '\0') {
328 if ((*env
== '\0') && (*nxt
== '\0'))
341 * Append new definition at the end
343 for (env
= environment
.data
; *env
|| *(env
+ 1); ++env
);
344 if (env
> environment
.data
)
348 * "name" + "=" + "val" +"\0\0" > CFG_ENV_SIZE - (env-environment)
350 len
= strlen (name
) + 2;
351 /* add '=' for first arg, ' ' for all others */
352 for (i
= 2; i
< argc
; ++i
) {
353 len
+= strlen (argv
[i
]) + 1;
355 if (len
> (&environment
.data
[ENV_SIZE
] - env
)) {
357 "Error: environment overflow, \"%s\" deleted\n",
361 while ((*env
= *name
++) != '\0')
363 for (i
= 2; i
< argc
; ++i
) {
364 uchar
*val
= argv
[i
];
366 *env
= (i
== 2) ? '=' : ' ';
367 while ((*++env
= *val
++) != '\0');
370 /* end is marked with double '\0' */
376 environment
.crc
= crc32 (0, environment
.data
, ENV_SIZE
);
378 /* write environment back to flash */
379 if (flash_io (O_RDWR
)) {
380 fprintf (stderr
, "Error: can't write fw_env to flash\n");
387 static int flash_io (int mode
)
389 int fd
, fdr
, rc
, otherdev
, len
, resid
;
393 if ((fd
= open (DEVNAME (curdev
), mode
)) < 0) {
395 "Can't open %s: %s\n",
396 DEVNAME (curdev
), strerror (errno
));
400 len
= sizeof (environment
.crc
);
402 len
+= sizeof (environment
.flags
);
405 if (mode
== O_RDWR
) {
407 /* switch to next partition for writing */
409 if ((fdr
= open (DEVNAME (otherdev
), mode
)) < 0) {
411 "Can't open %s: %s\n",
420 printf ("Unlocking flash...\n");
421 erase
.length
= DEVESIZE (otherdev
);
422 erase
.start
= DEVOFFSET (otherdev
);
423 ioctl (fdr
, MEMUNLOCK
, &erase
);
426 erase
.length
= DEVESIZE (curdev
);
427 erase
.start
= DEVOFFSET (curdev
);
428 ioctl (fd
, MEMUNLOCK
, &erase
);
429 environment
.flags
= active_flag
;
433 resid
= DEVESIZE (otherdev
) - CFG_ENV_SIZE
;
435 if ((data
= malloc (resid
)) == NULL
) {
437 "Cannot malloc %d bytes: %s\n",
442 if (lseek (fdr
, DEVOFFSET (otherdev
) + CFG_ENV_SIZE
, SEEK_SET
)
444 fprintf (stderr
, "seek error on %s: %s\n",
449 if ((rc
= read (fdr
, data
, resid
)) != resid
) {
451 "read error on %s: %s\n",
458 printf ("Erasing old environment...\n");
460 erase
.length
= DEVESIZE (otherdev
);
461 erase
.start
= DEVOFFSET (otherdev
);
462 if (ioctl (fdr
, MEMERASE
, &erase
) != 0) {
463 fprintf (stderr
, "MTD erase error on %s: %s\n",
471 printf ("Writing environment to %s...\n", DEVNAME (otherdev
));
472 if (lseek (fdr
, DEVOFFSET (otherdev
), SEEK_SET
) == -1) {
474 "seek error on %s: %s\n",
475 DEVNAME (otherdev
), strerror (errno
));
478 if (write (fdr
, &environment
, len
) != len
) {
480 "CRC write error on %s: %s\n",
481 DEVNAME (otherdev
), strerror (errno
));
484 if (write (fdr
, environment
.data
, ENV_SIZE
) != ENV_SIZE
) {
486 "Write error on %s: %s\n",
487 DEVNAME (otherdev
), strerror (errno
));
491 if (write (fdr
, data
, resid
) != resid
) {
493 "write error on %s: %s\n",
494 DEVNAME (curdev
), strerror (errno
));
500 /* change flag on current active env partition */
501 if (lseek (fd
, DEVOFFSET (curdev
) + sizeof (ulong
), SEEK_SET
)
503 fprintf (stderr
, "seek error on %s: %s\n",
504 DEVNAME (curdev
), strerror (errno
));
507 if (write (fd
, &obsolete_flag
, sizeof (obsolete_flag
)) !=
508 sizeof (obsolete_flag
)) {
510 "Write error on %s: %s\n",
511 DEVNAME (curdev
), strerror (errno
));
516 printf ("Locking ...\n");
517 erase
.length
= DEVESIZE (otherdev
);
518 erase
.start
= DEVOFFSET (otherdev
);
519 ioctl (fdr
, MEMLOCK
, &erase
);
521 erase
.length
= DEVESIZE (curdev
);
522 erase
.start
= DEVOFFSET (curdev
);
523 ioctl (fd
, MEMLOCK
, &erase
);
526 "I/O error on %s: %s\n",
535 if (lseek (fd
, DEVOFFSET (curdev
), SEEK_SET
) == -1) {
537 "seek error on %s: %s\n",
538 DEVNAME (curdev
), strerror (errno
));
541 if (read (fd
, &environment
, len
) != len
) {
543 "CRC read error on %s: %s\n",
544 DEVNAME (curdev
), strerror (errno
));
547 if ((rc
= read (fd
, environment
.data
, ENV_SIZE
)) != ENV_SIZE
) {
549 "Read error on %s: %s\n",
550 DEVNAME (curdev
), strerror (errno
));
557 "I/O error on %s: %s\n",
558 DEVNAME (curdev
), strerror (errno
));
567 * s1 is either a simple 'name', or a 'name=value' pair.
568 * s2 is a 'name=value' pair.
569 * If the names match, return the value of s2, else NULL.
572 static uchar
*envmatch (uchar
* s1
, uchar
* s2
)
578 if (*s1
== '\0' && *(s2
- 1) == '=')
584 * Prevent confusion if running from erased flash memory
586 static int env_init (void)
592 uchar flag1
, flag2
, *addr2
;
594 if (parse_config ()) /* should fill envdevices */
597 if ((addr1
= calloc (1, ENV_SIZE
)) == NULL
) {
599 "Not enough memory for environment (%ld bytes)\n",
604 /* read environment from FLASH to local buffer */
605 environment
.data
= addr1
;
607 if (flash_io (O_RDONLY
)) {
611 crc1_ok
= ((crc1
= crc32 (0, environment
.data
, ENV_SIZE
))
613 if (!HaveRedundEnv
) {
616 "Warning: Bad CRC, using default environment\n");
617 environment
.data
= default_environment
;
621 flag1
= environment
.flags
;
624 if ((addr2
= calloc (1, ENV_SIZE
)) == NULL
) {
626 "Not enough memory for environment (%ld bytes)\n",
630 environment
.data
= addr2
;
632 if (flash_io (O_RDONLY
)) {
636 crc2_ok
= ((crc2
= crc32 (0, environment
.data
, ENV_SIZE
))
638 flag2
= environment
.flags
;
640 if (crc1_ok
&& !crc2_ok
) {
641 environment
.data
= addr1
;
642 environment
.flags
= flag1
;
643 environment
.crc
= crc1
;
646 } else if (!crc1_ok
&& crc2_ok
) {
647 environment
.data
= addr2
;
648 environment
.flags
= flag2
;
649 environment
.crc
= crc2
;
652 } else if (!crc1_ok
&& !crc2_ok
) {
654 "Warning: Bad CRC, using default environment\n");
655 environment
.data
= default_environment
;
659 } else if (flag1
== active_flag
&& flag2
== obsolete_flag
) {
660 environment
.data
= addr1
;
661 environment
.flags
= flag1
;
662 environment
.crc
= crc1
;
665 } else if (flag1
== obsolete_flag
&& flag2
== active_flag
) {
666 environment
.data
= addr2
;
667 environment
.flags
= flag2
;
668 environment
.crc
= crc2
;
671 } else if (flag1
== flag2
) {
672 environment
.data
= addr1
;
673 environment
.flags
= flag1
;
674 environment
.crc
= crc1
;
677 } else if (flag1
== 0xFF) {
678 environment
.data
= addr1
;
679 environment
.flags
= flag1
;
680 environment
.crc
= crc1
;
683 } else if (flag2
== 0xFF) {
684 environment
.data
= addr2
;
685 environment
.flags
= flag2
;
686 environment
.crc
= crc2
;
695 static int parse_config ()
699 #if defined(CONFIG_FILE)
700 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
701 if (get_config (CONFIG_FILE
)) {
703 "Cannot parse config file: %s\n", strerror (errno
));
707 strcpy (DEVNAME (0), DEVICE1_NAME
);
708 DEVOFFSET (0) = DEVICE1_OFFSET
;
709 ENVSIZE (0) = ENV1_SIZE
;
710 DEVESIZE (0) = DEVICE1_ESIZE
;
712 strcpy (DEVNAME (1), DEVICE2_NAME
);
713 DEVOFFSET (1) = DEVICE2_OFFSET
;
714 ENVSIZE (1) = ENV2_SIZE
;
715 DEVESIZE (1) = DEVICE2_ESIZE
;
719 if (stat (DEVNAME (0), &st
)) {
721 "Cannot access MTD device %s: %s\n",
722 DEVNAME (0), strerror (errno
));
726 if (HaveRedundEnv
&& stat (DEVNAME (1), &st
)) {
728 "Cannot access MTD device %s: %s\n",
729 DEVNAME (1), strerror (errno
));
735 #if defined(CONFIG_FILE)
736 static int get_config (char *fname
)
743 if ((fp
= fopen (fname
, "r")) == NULL
) {
747 while ((i
< 2) && ((rc
= fscanf (fp
, "%s %lx %lx %lx",
751 &DEVESIZE (i
) )) != EOF
)) {
753 /* Skip incomplete conversions and comment strings */
754 if ((rc
< 3) || (*DEVNAME (i
) == '#')) {
755 fgets (dump
, sizeof (dump
), fp
); /* Consume till end */
763 HaveRedundEnv
= i
- 1;
764 if (!i
) { /* No valid entries found */