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 env_size
; /* environment size */
45 ulong erase_size
; /* device erase size */
48 static envdev_t envdevices
[2];
51 #define DEVNAME(i) envdevices[(i)].devname
52 #define ENVSIZE(i) envdevices[(i)].env_size
53 #define DEVESIZE(i) envdevices[(i)].erase_size
55 #define CFG_ENV_SIZE ENVSIZE(curdev)
58 #define ENV_SIZE (CFG_ENV_SIZE - sizeof(long) - 1)
60 #define ENV_SIZE (CFG_ENV_SIZE - sizeof(long))
63 typedef struct environment_s
{
64 ulong crc
; /* CRC32 over data bytes */
65 uchar flags
; /* active or obsolete */
69 static env_t environment
;
73 static uchar active_flag
= 1;
74 static uchar obsolete_flag
= 0;
78 #define MK_STR(x) XMK_STR(x)
80 static uchar default_environment
[] = {
81 #ifdef CONFIG_BOOTARGS
82 "bootargs=" CONFIG_BOOTARGS
"\0"
84 #ifdef CONFIG_BOOTCOMMAND
85 "bootcmd=" CONFIG_BOOTCOMMAND
"\0"
87 #if (CONFIG_BOOTDELAY >= 0)
88 "bootdelay=" MK_STR(CONFIG_BOOTDELAY
) "\0"
90 #if (CONFIG_BAUDRATE >= 0)
91 "baudrate=" MK_STR(CONFIG_BAUDRATE
) "\0"
94 "ethaddr=" MK_STR(CONFIG_ETHADDR
) "\0"
97 "ipaddr=" MK_STR(CONFIG_IPADDR
) "\0"
99 #ifdef CONFIG_SERVERIP
100 "serverip=" MK_STR(CONFIG_SERVERIP
) "\0"
105 static int flash_io (int mode
);
106 static uchar
*envmatch(uchar
*s1
, uchar
*s2
);
107 static int env_init(void);
108 static int parse_config(void);
112 * Search the environment for a variable.
113 * Return the value, if found, or NULL, if not found.
115 unsigned char *fw_getenv (unsigned char *name
)
122 for (env
=environment
.data
; *env
; env
=nxt
+1) {
125 for (nxt
=env
; *nxt
; ++nxt
) {
126 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
127 fprintf (stderr
, "## Error: "
128 "environment not terminated\n");
132 val
=envmatch(name
, env
);
141 * Print the current definition of one, or more, or all
142 * environment variables
144 void fw_printenv(int argc
, char *argv
[])
152 if (argc
== 1) { /* Print all env variables */
153 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");
167 if (strcmp(argv
[1], "-n") == 0) {
172 fprintf (stderr
, "## Error: "
173 "`-n' option requires exactly one argument\n");
180 for (i
=1; i
<argc
; ++i
) { /* print single env variables */
181 uchar
*name
= argv
[i
];
184 for (env
=environment
.data
; *env
; env
=nxt
+1) {
186 for (nxt
=env
; *nxt
; ++nxt
) {
187 if (nxt
>= &environment
.data
[ENV_SIZE
]) {
188 fprintf (stderr
, "## Error: "
189 "environment not terminated\n");
193 val
=envmatch(name
, env
);
196 fputs (name
, stdout
);
204 fprintf (stderr
, "## Error: \"%s\" not defined\n",
210 * Deletes or sets environment variables. Returns errno style error codes:
212 * EINVAL - need at least 1 argument
213 * EROFS - certain variables ("ethaddr", "serial#") cannot be
214 * modified or deleted
217 int fw_setenv (int argc
, char *argv
[])
221 uchar
*oldval
= NULL
;
234 * search if variable with this name already exists
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");
244 if ((oldval
=envmatch(name
, env
)) != NULL
)
249 * Delete any existing definition
253 * Ethernet Address and serial# can be set only once
255 if ((strcmp (name
, "ethaddr") == 0) ||
256 (strcmp (name
, "serial#") == 0) ) {
257 fprintf (stderr
, "Can't overwrite \"%s\"\n", name
);
261 if (*++nxt
== '\0') {
266 if ((*env
== '\0') && (*nxt
== '\0'))
279 * Append new definition at the end
281 for (env
=environment
.data
; *env
|| *(env
+1); ++env
)
283 if (env
> environment
.data
)
287 * "name" + "=" + "val" +"\0\0" > CFG_ENV_SIZE - (env-environment)
289 len
= strlen(name
) + 2;
290 /* add '=' for first arg, ' ' for all others */
291 for (i
=2; i
<argc
; ++i
) {
292 len
+= strlen(argv
[i
]) + 1;
294 if (len
> (&environment
.data
[ENV_SIZE
]-env
)) {
296 "Error: environment overflow, \"%s\" deleted\n",
300 while ((*env
= *name
++) != '\0')
302 for (i
=2; i
<argc
; ++i
) {
303 uchar
*val
= argv
[i
];
305 *env
= (i
==2) ? '=' : ' ';
306 while ((*++env
= *val
++) != '\0')
310 /* end is marked with double '\0' */
316 environment
.crc
= crc32(0, environment
.data
, ENV_SIZE
);
318 /* write environment back to flash */
319 if (flash_io (O_RDWR
)) {
321 "Error: can't write fw_env to flash\n");
328 static int flash_io (int mode
)
330 int fd
, fdr
, rc
, otherdev
, len
, resid
;
334 if ((fd
= open(DEVNAME(curdev
), mode
)) < 0) {
336 "Can't open %s: %s\n",
337 DEVNAME(curdev
), strerror(errno
));
341 len
= sizeof(environment
.crc
) + sizeof(environment
.flags
);
343 if (mode
== O_RDWR
) {
345 /* switch to next partition for writing */
347 if ((fdr
= open(DEVNAME(otherdev
), mode
)) < 0) {
349 "Can't open %s: %s\n",
350 DEVNAME(otherdev
), strerror(errno
));
356 len
= sizeof(environment
.crc
);
358 printf("Unlocking flash...\n");
359 erase
.length
= DEVESIZE(otherdev
);
361 ioctl (fdr
, MEMUNLOCK
, &erase
);
364 erase
.length
= DEVESIZE(curdev
);
366 ioctl (fd
, MEMUNLOCK
, &erase
);
367 environment
.flags
= active_flag
;
370 resid
= DEVESIZE(otherdev
) - CFG_ENV_SIZE
;
372 if ((data
= malloc(resid
)) == NULL
) {
374 "Cannot malloc %d bytes: %s\n",
375 resid
, strerror(errno
));
378 if (lseek (fdr
, CFG_ENV_SIZE
, SEEK_SET
) == -1) {
380 "seek error on %s: %s\n",
381 DEVNAME(curdev
), strerror(errno
));
384 if ((rc
= read (fdr
, data
, resid
)) != resid
) {
386 "read error on %s: %s\n",
387 DEVNAME(curdev
), strerror(errno
));
390 if (lseek (fdr
, 0, SEEK_SET
) == -1) {
392 "seek error on %s: %s\n",
393 DEVNAME(curdev
), strerror(errno
));
398 printf("Erasing old environment...\n");
400 erase
.length
= DEVESIZE(otherdev
);
401 if (ioctl (fdr
, MEMERASE
, &erase
) != 0) {
402 fprintf (stderr
, "MTD erase error on %s: %s\n",
403 DEVNAME(otherdev
), strerror(errno
));
409 printf("Writing environment to %s...\n",DEVNAME(otherdev
));
410 if (write(fdr
, &environment
, len
) != len
) {
412 "CRC write error on %s: %s\n",
413 DEVNAME(otherdev
), strerror(errno
));
416 if (write(fdr
, environment
.data
, ENV_SIZE
) != ENV_SIZE
) {
418 "Write error on %s: %s\n",
419 DEVNAME(otherdev
), strerror(errno
));
423 if (write (fdr
, data
, resid
) != resid
) {
425 "write error on %s: %s\n",
426 DEVNAME(curdev
), strerror(errno
));
432 /* change flag on current active env partition */
433 if (lseek (fd
, sizeof(ulong
), SEEK_SET
) == -1) {
435 "seek error on %s: %s\n",
436 DEVNAME(curdev
), strerror(errno
));
439 if (write (fd
, &obsolete_flag
, sizeof(obsolete_flag
)) !=
440 sizeof(obsolete_flag
)) {
442 "Write error on %s: %s\n",
443 DEVNAME(curdev
), strerror(errno
));
448 printf("Locking ...\n");
449 erase
.length
= DEVESIZE(otherdev
);
451 ioctl (fdr
, MEMLOCK
, &erase
);
453 erase
.length
= DEVESIZE(curdev
);
455 ioctl (fd
, MEMLOCK
, &erase
);
458 "I/O error on %s: %s\n",
459 DEVNAME(otherdev
), strerror(errno
));
466 len
= sizeof(environment
.crc
);
468 if (read (fd
, &environment
, len
) != len
) {
470 "CRC read error on %s: %s\n",
471 DEVNAME(curdev
), strerror(errno
));
474 if ((rc
= read (fd
, environment
.data
, ENV_SIZE
)) != ENV_SIZE
) {
476 "Read error on %s: %s\n",
477 DEVNAME(curdev
), strerror(errno
));
484 "I/O error on %s: %s\n",
485 DEVNAME(curdev
), strerror(errno
));
494 * s1 is either a simple 'name', or a 'name=value' pair.
495 * s2 is a 'name=value' pair.
496 * If the names match, return the value of s2, else NULL.
500 envmatch (uchar
*s1
, uchar
*s2
)
506 if (*s1
== '\0' && *(s2
-1) == '=')
512 * Prevent confusion if running from erased flash memory
514 static int env_init(void)
520 uchar flag1
, flag2
, *addr2
;
526 if (parse_config()) /* should fill envdevices */
529 if ((addr1
= calloc (1, ENV_SIZE
)) == NULL
) {
531 "Not enough memory for environment (%ld bytes)\n",
536 /* read environment from FLASH to local buffer */
537 environment
.data
= addr1
;
539 if (flash_io (O_RDONLY
)) {
543 crc1_ok
= ((crc1
= crc32(0, environment
.data
, ENV_SIZE
))
548 "Warning: Bad CRC, using default environment\n");
549 environment
.data
= default_environment
;
553 flag1
= environment
.flags
;
556 if ((addr2
= calloc (1, ENV_SIZE
)) == NULL
) {
558 "Not enough memory for environment (%ld bytes)\n",
562 environment
.data
= addr2
;
564 if (flash_io (O_RDONLY
)) {
568 crc2_ok
= ((crc2
= crc32(0, environment
.data
, ENV_SIZE
))
570 flag2
= environment
.flags
;
572 if (crc1_ok
&& ! crc2_ok
) {
573 environment
.data
= addr1
;
574 environment
.flags
= flag1
;
575 environment
.crc
= crc1
;
579 else if (! crc1_ok
&& crc2_ok
) {
580 environment
.data
= addr2
;
581 environment
.flags
= flag2
;
582 environment
.crc
= crc2
;
586 else if (! crc1_ok
&& ! crc2_ok
) {
588 "Warning: Bad CRC, using default environment\n");
589 environment
.data
= default_environment
;
594 else if (flag1
== active_flag
&& flag2
== obsolete_flag
) {
595 environment
.data
= addr1
;
596 environment
.flags
= flag1
;
597 environment
.crc
= crc1
;
601 else if (flag1
== obsolete_flag
&& flag2
== active_flag
) {
602 environment
.data
= addr2
;
603 environment
.flags
= flag2
;
604 environment
.crc
= crc2
;
608 else if (flag1
== flag2
) {
609 environment
.data
= addr1
;
610 environment
.flags
= flag1
;
611 environment
.crc
= crc1
;
615 else if (flag1
== 0xFF) {
616 environment
.data
= addr1
;
617 environment
.flags
= flag1
;
618 environment
.crc
= crc1
;
622 else if (flag2
== 0xFF) {
623 environment
.data
= addr2
;
624 environment
.flags
= flag2
;
625 environment
.crc
= crc2
;
636 static int parse_config()
640 if (stat (DEVICE1_NAME
, &st
)) {
642 "Cannot access MTD device %s: %s\n",
643 DEVICE1_NAME
, strerror(errno
));
647 strcpy(DEVNAME(0), DEVICE1_NAME
);
648 ENVSIZE(0) = ENV1_SIZE
;
649 DEVESIZE(0) = DEVICE1_ESIZE
;
651 if (stat (DEVICE2_NAME
, &st
)) {
653 "Cannot access MTD device %s: %s\n",
654 DEVICE2_NAME
, strerror(errno
));
657 strcpy(DEVNAME(1), DEVICE2_NAME
);
658 ENVSIZE(1) = ENV2_SIZE
;
659 DEVESIZE(1) = DEVICE2_ESIZE
;