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
);
277 * Deletes or sets environment variables. Returns errno style error codes:
279 * EINVAL - need at least 1 argument
280 * EROFS - certain variables ("ethaddr", "serial#") cannot be
281 * modified or deleted
284 int fw_setenv (int argc
, char *argv
[])
301 * search if variable with this name already exists
303 for (nxt
= 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 if ((oldval
= envmatch (name
, env
)) != NULL
)
316 * Delete any existing definition
320 * Ethernet Address and serial# can be set only once
322 if ((strcmp (name
, "ethaddr") == 0) ||
323 (strcmp (name
, "serial#") == 0)) {
324 fprintf (stderr
, "Can't overwrite \"%s\"\n", name
);
328 if (*++nxt
== '\0') {
333 if ((*env
== '\0') && (*nxt
== '\0'))
346 * Append new definition at the end
348 for (env
= environment
.data
; *env
|| *(env
+ 1); ++env
);
349 if (env
> environment
.data
)
353 * "name" + "=" + "val" +"\0\0" > CFG_ENV_SIZE - (env-environment)
355 len
= strlen (name
) + 2;
356 /* add '=' for first arg, ' ' for all others */
357 for (i
= 2; i
< argc
; ++i
) {
358 len
+= strlen (argv
[i
]) + 1;
360 if (len
> (&environment
.data
[ENV_SIZE
] - env
)) {
362 "Error: environment overflow, \"%s\" deleted\n",
366 while ((*env
= *name
++) != '\0')
368 for (i
= 2; i
< argc
; ++i
) {
371 *env
= (i
== 2) ? '=' : ' ';
372 while ((*++env
= *val
++) != '\0');
375 /* end is marked with double '\0' */
381 environment
.crc
= crc32 (0, (uint8_t*) environment
.data
, ENV_SIZE
);
383 /* write environment back to flash */
384 if (flash_io (O_RDWR
)) {
385 fprintf (stderr
, "Error: can't write fw_env to flash\n");
392 static int flash_io (int mode
)
394 int fd
, fdr
, rc
, otherdev
, len
, resid
;
398 if ((fd
= open (DEVNAME (curdev
), mode
)) < 0) {
400 "Can't open %s: %s\n",
401 DEVNAME (curdev
), strerror (errno
));
405 len
= sizeof (environment
.crc
);
407 len
+= sizeof (environment
.flags
);
410 if (mode
== O_RDWR
) {
412 /* switch to next partition for writing */
414 if ((fdr
= open (DEVNAME (otherdev
), mode
)) < 0) {
416 "Can't open %s: %s\n",
425 printf ("Unlocking flash...\n");
426 erase
.length
= DEVESIZE (otherdev
);
427 erase
.start
= DEVOFFSET (otherdev
);
428 ioctl (fdr
, MEMUNLOCK
, &erase
);
431 erase
.length
= DEVESIZE (curdev
);
432 erase
.start
= DEVOFFSET (curdev
);
433 ioctl (fd
, MEMUNLOCK
, &erase
);
434 environment
.flags
= active_flag
;
438 resid
= DEVESIZE (otherdev
) - CFG_ENV_SIZE
;
440 if ((data
= malloc (resid
)) == NULL
) {
442 "Cannot malloc %d bytes: %s\n",
447 if (lseek (fdr
, DEVOFFSET (otherdev
) + CFG_ENV_SIZE
, SEEK_SET
)
449 fprintf (stderr
, "seek error on %s: %s\n",
454 if ((rc
= read (fdr
, data
, resid
)) != resid
) {
456 "read error on %s: %s\n",
463 printf ("Erasing old environment...\n");
465 erase
.length
= DEVESIZE (otherdev
);
466 erase
.start
= DEVOFFSET (otherdev
);
467 if (ioctl (fdr
, MEMERASE
, &erase
) != 0) {
468 fprintf (stderr
, "MTD erase error on %s: %s\n",
476 printf ("Writing environment to %s...\n", DEVNAME (otherdev
));
477 if (lseek (fdr
, DEVOFFSET (otherdev
), SEEK_SET
) == -1) {
479 "seek error on %s: %s\n",
480 DEVNAME (otherdev
), strerror (errno
));
483 if (write (fdr
, &environment
, len
) != len
) {
485 "CRC write error on %s: %s\n",
486 DEVNAME (otherdev
), strerror (errno
));
489 if (write (fdr
, environment
.data
, ENV_SIZE
) != ENV_SIZE
) {
491 "Write error on %s: %s\n",
492 DEVNAME (otherdev
), strerror (errno
));
496 if (write (fdr
, data
, resid
) != resid
) {
498 "write error on %s: %s\n",
499 DEVNAME (curdev
), strerror (errno
));
505 /* change flag on current active env partition */
506 if (lseek (fd
, DEVOFFSET (curdev
) + sizeof (ulong
), SEEK_SET
)
508 fprintf (stderr
, "seek error on %s: %s\n",
509 DEVNAME (curdev
), strerror (errno
));
512 if (write (fd
, &obsolete_flag
, sizeof (obsolete_flag
)) !=
513 sizeof (obsolete_flag
)) {
515 "Write error on %s: %s\n",
516 DEVNAME (curdev
), strerror (errno
));
521 printf ("Locking ...\n");
522 erase
.length
= DEVESIZE (otherdev
);
523 erase
.start
= DEVOFFSET (otherdev
);
524 ioctl (fdr
, MEMLOCK
, &erase
);
526 erase
.length
= DEVESIZE (curdev
);
527 erase
.start
= DEVOFFSET (curdev
);
528 ioctl (fd
, MEMLOCK
, &erase
);
531 "I/O error on %s: %s\n",
540 if (lseek (fd
, DEVOFFSET (curdev
), SEEK_SET
) == -1) {
542 "seek error on %s: %s\n",
543 DEVNAME (curdev
), strerror (errno
));
546 if (read (fd
, &environment
, len
) != len
) {
548 "CRC read error on %s: %s\n",
549 DEVNAME (curdev
), strerror (errno
));
552 if ((rc
= read (fd
, environment
.data
, ENV_SIZE
)) != ENV_SIZE
) {
554 "Read error on %s: %s\n",
555 DEVNAME (curdev
), strerror (errno
));
562 "I/O error on %s: %s\n",
563 DEVNAME (curdev
), strerror (errno
));
572 * s1 is either a simple 'name', or a 'name=value' pair.
573 * s2 is a 'name=value' pair.
574 * If the names match, return the value of s2, else NULL.
577 static char *envmatch (char * s1
, char * s2
)
583 if (*s1
== '\0' && *(s2
- 1) == '=')
589 * Prevent confusion if running from erased flash memory
591 static int env_init (void)
597 char flag1
, flag2
, *addr2
;
599 if (parse_config ()) /* should fill envdevices */
602 if ((addr1
= calloc (1, ENV_SIZE
)) == NULL
) {
604 "Not enough memory for environment (%ld bytes)\n",
609 /* read environment from FLASH to local buffer */
610 environment
.data
= addr1
;
612 if (flash_io (O_RDONLY
)) {
616 crc1_ok
= ((crc1
= crc32 (0, (uint8_t *) environment
.data
, ENV_SIZE
))
618 if (!HaveRedundEnv
) {
621 "Warning: Bad CRC, using default environment\n");
622 memcpy(environment
.data
, default_environment
, sizeof default_environment
);
625 flag1
= environment
.flags
;
628 if ((addr2
= calloc (1, ENV_SIZE
)) == NULL
) {
630 "Not enough memory for environment (%ld bytes)\n",
634 environment
.data
= addr2
;
636 if (flash_io (O_RDONLY
)) {
640 crc2_ok
= ((crc2
= crc32 (0, (uint8_t *) environment
.data
, ENV_SIZE
))
642 flag2
= environment
.flags
;
644 if (crc1_ok
&& !crc2_ok
) {
645 environment
.data
= addr1
;
646 environment
.flags
= flag1
;
647 environment
.crc
= crc1
;
650 } else if (!crc1_ok
&& crc2_ok
) {
651 environment
.data
= addr2
;
652 environment
.flags
= flag2
;
653 environment
.crc
= crc2
;
656 } else if (!crc1_ok
&& !crc2_ok
) {
658 "Warning: Bad CRC, using default environment\n");
659 memcpy(environment
.data
, default_environment
, sizeof default_environment
);
662 } else if (flag1
== active_flag
&& flag2
== obsolete_flag
) {
663 environment
.data
= addr1
;
664 environment
.flags
= flag1
;
665 environment
.crc
= crc1
;
668 } else if (flag1
== obsolete_flag
&& flag2
== active_flag
) {
669 environment
.data
= addr2
;
670 environment
.flags
= flag2
;
671 environment
.crc
= crc2
;
674 } else if (flag1
== flag2
) {
675 environment
.data
= addr1
;
676 environment
.flags
= flag1
;
677 environment
.crc
= crc1
;
680 } else if (flag1
== 0xFF) {
681 environment
.data
= addr1
;
682 environment
.flags
= flag1
;
683 environment
.crc
= crc1
;
686 } else if (flag2
== 0xFF) {
687 environment
.data
= addr2
;
688 environment
.flags
= flag2
;
689 environment
.crc
= crc2
;
698 static int parse_config ()
702 #if defined(CONFIG_FILE)
703 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
704 if (get_config (CONFIG_FILE
)) {
706 "Cannot parse config file: %s\n", strerror (errno
));
710 strcpy (DEVNAME (0), DEVICE1_NAME
);
711 DEVOFFSET (0) = DEVICE1_OFFSET
;
712 ENVSIZE (0) = ENV1_SIZE
;
713 DEVESIZE (0) = DEVICE1_ESIZE
;
715 strcpy (DEVNAME (1), DEVICE2_NAME
);
716 DEVOFFSET (1) = DEVICE2_OFFSET
;
717 ENVSIZE (1) = ENV2_SIZE
;
718 DEVESIZE (1) = DEVICE2_ESIZE
;
722 if (stat (DEVNAME (0), &st
)) {
724 "Cannot access MTD device %s: %s\n",
725 DEVNAME (0), strerror (errno
));
729 if (HaveRedundEnv
&& stat (DEVNAME (1), &st
)) {
731 "Cannot access MTD device %s: %s\n",
732 DEVNAME (1), strerror (errno
));
738 #if defined(CONFIG_FILE)
739 static int get_config (char *fname
)
746 if ((fp
= fopen (fname
, "r")) == NULL
) {
750 while ((i
< 2) && ((rc
= fscanf (fp
, "%s %lx %lx %lx",
754 &DEVESIZE (i
) )) != EOF
)) {
756 /* Skip incomplete conversions and comment strings */
757 if ((rc
< 3) || (*DEVNAME (i
) == '#')) {
758 fgets (dump
, sizeof (dump
), fp
); /* Consume till end */
766 HaveRedundEnv
= i
- 1;
767 if (!i
) { /* No valid entries found */