blob: 74c0498d5a671c415f785b1386d8965585e89405 [file] [log] [blame]
wdenk34b613e2002-12-17 01:51:00 +00001/*
wdenk3f9ab982003-04-12 23:38:12 +00002 * (C) Copyright 2000-2003
wdenk34b613e2002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wdenk3f9ab982003-04-12 23:38:12 +000015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenk34b613e2002-12-17 01:51:00 +000016 * GNU General Public License for more details.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24#include <errno.h>
25#include <fcntl.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <stddef.h>
29#include <string.h>
30#include <sys/types.h>
31#include <sys/ioctl.h>
32#include <sys/stat.h>
33#include <unistd.h>
34#include <linux/mtd/mtd.h>
35#include "fw_env.h"
36
wdenk3f9ab982003-04-12 23:38:12 +000037typedef unsigned char uchar;
wdenk34b613e2002-12-17 01:51:00 +000038
39#define CMD_GETENV "fw_printenv"
40#define CMD_SETENV "fw_setenv"
41
42typedef struct envdev_s {
wdenk3f9ab982003-04-12 23:38:12 +000043 uchar devname[16]; /* Device name */
44 ulong devoff; /* Device offset */
45 ulong env_size; /* environment size */
46 ulong erase_size; /* device erase size */
wdenk34b613e2002-12-17 01:51:00 +000047} envdev_t;
48
49static envdev_t envdevices[2];
50static int curdev;
51
52#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000053#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000054#define ENVSIZE(i) envdevices[(i)].env_size
55#define DEVESIZE(i) envdevices[(i)].erase_size
56
57#define CFG_ENV_SIZE ENVSIZE(curdev)
58
wdenke7f34c62003-01-11 09:48:40 +000059#define ENV_SIZE getenvsize()
wdenk34b613e2002-12-17 01:51:00 +000060
61typedef struct environment_s {
wdenk3f9ab982003-04-12 23:38:12 +000062 ulong crc; /* CRC32 over data bytes */
63 uchar flags; /* active or obsolete */
wdenk34b613e2002-12-17 01:51:00 +000064 uchar *data;
65} env_t;
66
67static env_t environment;
wdenk34b613e2002-12-17 01:51:00 +000068
wdenke7f34c62003-01-11 09:48:40 +000069static int HaveRedundEnv = 0;
70
wdenk34b613e2002-12-17 01:51:00 +000071static uchar active_flag = 1;
72static uchar obsolete_flag = 0;
wdenke7f34c62003-01-11 09:48:40 +000073
wdenk34b613e2002-12-17 01:51:00 +000074
75#define XMK_STR(x) #x
76#define MK_STR(x) XMK_STR(x)
77
78static uchar default_environment[] = {
wdenke7f34c62003-01-11 09:48:40 +000079#if defined(CONFIG_BOOTARGS)
wdenk3f9ab982003-04-12 23:38:12 +000080 "bootargs=" CONFIG_BOOTARGS "\0"
wdenk34b613e2002-12-17 01:51:00 +000081#endif
wdenke7f34c62003-01-11 09:48:40 +000082#if defined(CONFIG_BOOTCOMMAND)
wdenk3f9ab982003-04-12 23:38:12 +000083 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
wdenk34b613e2002-12-17 01:51:00 +000084#endif
wdenke7f34c62003-01-11 09:48:40 +000085#if defined(CONFIG_RAMBOOTCOMMAND)
wdenk3f9ab982003-04-12 23:38:12 +000086 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
wdenke7f34c62003-01-11 09:48:40 +000087#endif
88#if defined(CONFIG_NFSBOOTCOMMAND)
wdenk3f9ab982003-04-12 23:38:12 +000089 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
wdenke7f34c62003-01-11 09:48:40 +000090#endif
91#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
wdenk3f9ab982003-04-12 23:38:12 +000092 "bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0"
wdenk34b613e2002-12-17 01:51:00 +000093#endif
wdenke7f34c62003-01-11 09:48:40 +000094#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
wdenk3f9ab982003-04-12 23:38:12 +000095 "baudrate=" MK_STR (CONFIG_BAUDRATE) "\0"
wdenk34b613e2002-12-17 01:51:00 +000096#endif
wdenke7f34c62003-01-11 09:48:40 +000097#ifdef CONFIG_LOADS_ECHO
wdenk3f9ab982003-04-12 23:38:12 +000098 "loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0"
wdenke7f34c62003-01-11 09:48:40 +000099#endif
wdenk34b613e2002-12-17 01:51:00 +0000100#ifdef CONFIG_ETHADDR
wdenk3f9ab982003-04-12 23:38:12 +0000101 "ethaddr=" MK_STR (CONFIG_ETHADDR) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000102#endif
wdenke7f34c62003-01-11 09:48:40 +0000103#ifdef CONFIG_ETH1ADDR
wdenk3f9ab982003-04-12 23:38:12 +0000104 "eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000105#endif
106#ifdef CONFIG_ETH2ADDR
wdenk3f9ab982003-04-12 23:38:12 +0000107 "eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000108#endif
wdenk54070ab2004-12-31 09:32:47 +0000109#ifdef CONFIG_ETH3ADDR
110 "eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0"
111#endif
wdenke7f34c62003-01-11 09:48:40 +0000112#ifdef CONFIG_ETHPRIME
wdenk3f9ab982003-04-12 23:38:12 +0000113 "ethprime=" CONFIG_ETHPRIME "\0"
wdenke7f34c62003-01-11 09:48:40 +0000114#endif
wdenk34b613e2002-12-17 01:51:00 +0000115#ifdef CONFIG_IPADDR
wdenk3f9ab982003-04-12 23:38:12 +0000116 "ipaddr=" MK_STR (CONFIG_IPADDR) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000117#endif
118#ifdef CONFIG_SERVERIP
wdenk3f9ab982003-04-12 23:38:12 +0000119 "serverip=" MK_STR (CONFIG_SERVERIP) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000120#endif
wdenke7f34c62003-01-11 09:48:40 +0000121#ifdef CFG_AUTOLOAD
wdenk3f9ab982003-04-12 23:38:12 +0000122 "autoload=" CFG_AUTOLOAD "\0"
wdenke7f34c62003-01-11 09:48:40 +0000123#endif
124#ifdef CONFIG_ROOTPATH
wdenk3f9ab982003-04-12 23:38:12 +0000125 "rootpath=" MK_STR (CONFIG_ROOTPATH) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000126#endif
127#ifdef CONFIG_GATEWAYIP
wdenk3f9ab982003-04-12 23:38:12 +0000128 "gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000129#endif
130#ifdef CONFIG_NETMASK
wdenk3f9ab982003-04-12 23:38:12 +0000131 "netmask=" MK_STR (CONFIG_NETMASK) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000132#endif
133#ifdef CONFIG_HOSTNAME
wdenk3f9ab982003-04-12 23:38:12 +0000134 "hostname=" MK_STR (CONFIG_HOSTNAME) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000135#endif
136#ifdef CONFIG_BOOTFILE
wdenk3f9ab982003-04-12 23:38:12 +0000137 "bootfile=" MK_STR (CONFIG_BOOTFILE) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000138#endif
139#ifdef CONFIG_LOADADDR
wdenk3f9ab982003-04-12 23:38:12 +0000140 "loadaddr=" MK_STR (CONFIG_LOADADDR) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000141#endif
142#ifdef CONFIG_PREBOOT
wdenk3f9ab982003-04-12 23:38:12 +0000143 "preboot=" CONFIG_PREBOOT "\0"
wdenke7f34c62003-01-11 09:48:40 +0000144#endif
145#ifdef CONFIG_CLOCKS_IN_MHZ
wdenk3f9ab982003-04-12 23:38:12 +0000146 "clocks_in_mhz=" "1" "\0"
wdenke7f34c62003-01-11 09:48:40 +0000147#endif
stroesef5dd4102003-02-14 11:21:23 +0000148#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
wdenk3f9ab982003-04-12 23:38:12 +0000149 "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0"
stroesef5dd4102003-02-14 11:21:23 +0000150#endif
wdenke7f34c62003-01-11 09:48:40 +0000151#ifdef CONFIG_EXTRA_ENV_SETTINGS
152 CONFIG_EXTRA_ENV_SETTINGS
153#endif
wdenk3f9ab982003-04-12 23:38:12 +0000154 "\0" /* Termimate env_t data with 2 NULs */
wdenk34b613e2002-12-17 01:51:00 +0000155};
156
wdenk3f9ab982003-04-12 23:38:12 +0000157static int flash_io (int mode);
158static uchar *envmatch (uchar * s1, uchar * s2);
159static int env_init (void);
160static int parse_config (void);
161
wdenke7f34c62003-01-11 09:48:40 +0000162#if defined(CONFIG_FILE)
wdenk3f9ab982003-04-12 23:38:12 +0000163static int get_config (char *);
wdenke7f34c62003-01-11 09:48:40 +0000164#endif
wdenk3f9ab982003-04-12 23:38:12 +0000165static inline ulong getenvsize (void)
wdenke7f34c62003-01-11 09:48:40 +0000166{
wdenk3f9ab982003-04-12 23:38:12 +0000167 ulong rc = CFG_ENV_SIZE - sizeof (long);
168
wdenke7f34c62003-01-11 09:48:40 +0000169 if (HaveRedundEnv)
wdenk3f9ab982003-04-12 23:38:12 +0000170 rc -= sizeof (char);
wdenke7f34c62003-01-11 09:48:40 +0000171 return rc;
172}
wdenk34b613e2002-12-17 01:51:00 +0000173
174/*
175 * Search the environment for a variable.
176 * Return the value, if found, or NULL, if not found.
177 */
178unsigned char *fw_getenv (unsigned char *name)
179{
180 uchar *env, *nxt;
181
wdenk3f9ab982003-04-12 23:38:12 +0000182 if (env_init ())
wdenk34b613e2002-12-17 01:51:00 +0000183 return (NULL);
184
wdenk3f9ab982003-04-12 23:38:12 +0000185 for (env = environment.data; *env; env = nxt + 1) {
wdenk34b613e2002-12-17 01:51:00 +0000186 uchar *val;
187
wdenk3f9ab982003-04-12 23:38:12 +0000188 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000189 if (nxt >= &environment.data[ENV_SIZE]) {
190 fprintf (stderr, "## Error: "
191 "environment not terminated\n");
192 return (NULL);
193 }
194 }
wdenk3f9ab982003-04-12 23:38:12 +0000195 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000196 if (!val)
197 continue;
198 return (val);
199 }
200 return (NULL);
201}
202
203/*
204 * Print the current definition of one, or more, or all
205 * environment variables
206 */
wdenk3f9ab982003-04-12 23:38:12 +0000207void fw_printenv (int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000208{
209 uchar *env, *nxt;
210 int i, n_flag;
211
wdenk3f9ab982003-04-12 23:38:12 +0000212 if (env_init ())
wdenk34b613e2002-12-17 01:51:00 +0000213 return;
214
wdenk3f9ab982003-04-12 23:38:12 +0000215 if (argc == 1) { /* Print all env variables */
216 for (env = environment.data; *env; env = nxt + 1) {
217 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000218 if (nxt >= &environment.data[ENV_SIZE]) {
219 fprintf (stderr, "## Error: "
220 "environment not terminated\n");
221 return;
222 }
223 }
224
wdenk3f9ab982003-04-12 23:38:12 +0000225 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000226 }
227 return;
228 }
229
wdenk3f9ab982003-04-12 23:38:12 +0000230 if (strcmp (argv[1], "-n") == 0) {
wdenk34b613e2002-12-17 01:51:00 +0000231 n_flag = 1;
232 ++argv;
233 --argc;
234 if (argc != 2) {
235 fprintf (stderr, "## Error: "
236 "`-n' option requires exactly one argument\n");
237 return;
238 }
239 } else {
240 n_flag = 0;
241 }
242
wdenk3f9ab982003-04-12 23:38:12 +0000243 for (i = 1; i < argc; ++i) { /* print single env variables */
wdenk34b613e2002-12-17 01:51:00 +0000244 uchar *name = argv[i];
245 uchar *val = NULL;
246
wdenk3f9ab982003-04-12 23:38:12 +0000247 for (env = environment.data; *env; env = nxt + 1) {
wdenk34b613e2002-12-17 01:51:00 +0000248
wdenk3f9ab982003-04-12 23:38:12 +0000249 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000250 if (nxt >= &environment.data[ENV_SIZE]) {
251 fprintf (stderr, "## Error: "
252 "environment not terminated\n");
253 return;
254 }
255 }
wdenk3f9ab982003-04-12 23:38:12 +0000256 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000257 if (val) {
258 if (!n_flag) {
259 fputs (name, stdout);
wdenk3f9ab982003-04-12 23:38:12 +0000260 putc ('=', stdout);
wdenk34b613e2002-12-17 01:51:00 +0000261 }
wdenk3f9ab982003-04-12 23:38:12 +0000262 puts (val);
wdenk34b613e2002-12-17 01:51:00 +0000263 break;
264 }
265 }
266 if (!val)
wdenk3f9ab982003-04-12 23:38:12 +0000267 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
wdenk34b613e2002-12-17 01:51:00 +0000268 }
269}
270
271/*
272 * Deletes or sets environment variables. Returns errno style error codes:
273 * 0 - OK
274 * EINVAL - need at least 1 argument
275 * EROFS - certain variables ("ethaddr", "serial#") cannot be
276 * modified or deleted
277 *
278 */
279int fw_setenv (int argc, char *argv[])
280{
wdenk3f9ab982003-04-12 23:38:12 +0000281 int i, len;
wdenk34b613e2002-12-17 01:51:00 +0000282 uchar *env, *nxt;
283 uchar *oldval = NULL;
284 uchar *name;
285
286 if (argc < 2) {
287 return (EINVAL);
288 }
289
wdenk3f9ab982003-04-12 23:38:12 +0000290 if (env_init ())
wdenk34b613e2002-12-17 01:51:00 +0000291 return (errno);
292
293 name = argv[1];
294
295 /*
296 * search if variable with this name already exists
297 */
wdenk3f9ab982003-04-12 23:38:12 +0000298 for (nxt = env = environment.data; *env; env = nxt + 1) {
299 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000300 if (nxt >= &environment.data[ENV_SIZE]) {
301 fprintf (stderr, "## Error: "
302 "environment not terminated\n");
303 return (EINVAL);
304 }
305 }
wdenk3f9ab982003-04-12 23:38:12 +0000306 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000307 break;
308 }
309
310 /*
311 * Delete any existing definition
312 */
313 if (oldval) {
314 /*
315 * Ethernet Address and serial# can be set only once
316 */
317 if ((strcmp (name, "ethaddr") == 0) ||
wdenk3f9ab982003-04-12 23:38:12 +0000318 (strcmp (name, "serial#") == 0)) {
wdenk34b613e2002-12-17 01:51:00 +0000319 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
320 return (EROFS);
321 }
322
323 if (*++nxt == '\0') {
324 *env = '\0';
325 } else {
326 for (;;) {
327 *env = *nxt++;
328 if ((*env == '\0') && (*nxt == '\0'))
329 break;
330 ++env;
331 }
332 }
333 *++env = '\0';
334 }
335
336 /* Delete only ? */
337 if (argc < 3)
338 goto WRITE_FLASH;
339
340 /*
341 * Append new definition at the end
342 */
wdenk3f9ab982003-04-12 23:38:12 +0000343 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000344 if (env > environment.data)
345 ++env;
346 /*
347 * Overflow when:
348 * "name" + "=" + "val" +"\0\0" > CFG_ENV_SIZE - (env-environment)
349 */
wdenk3f9ab982003-04-12 23:38:12 +0000350 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000351 /* add '=' for first arg, ' ' for all others */
wdenk3f9ab982003-04-12 23:38:12 +0000352 for (i = 2; i < argc; ++i) {
353 len += strlen (argv[i]) + 1;
wdenk34b613e2002-12-17 01:51:00 +0000354 }
wdenk3f9ab982003-04-12 23:38:12 +0000355 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000356 fprintf (stderr,
357 "Error: environment overflow, \"%s\" deleted\n",
358 name);
359 return (-1);
360 }
361 while ((*env = *name++) != '\0')
362 env++;
wdenk3f9ab982003-04-12 23:38:12 +0000363 for (i = 2; i < argc; ++i) {
wdenk34b613e2002-12-17 01:51:00 +0000364 uchar *val = argv[i];
365
wdenk3f9ab982003-04-12 23:38:12 +0000366 *env = (i == 2) ? '=' : ' ';
367 while ((*++env = *val++) != '\0');
wdenk34b613e2002-12-17 01:51:00 +0000368 }
369
370 /* end is marked with double '\0' */
371 *++env = '\0';
372
wdenk3f9ab982003-04-12 23:38:12 +0000373 WRITE_FLASH:
wdenk34b613e2002-12-17 01:51:00 +0000374
375 /* Update CRC */
wdenk3f9ab982003-04-12 23:38:12 +0000376 environment.crc = crc32 (0, environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000377
378 /* write environment back to flash */
379 if (flash_io (O_RDWR)) {
wdenk3f9ab982003-04-12 23:38:12 +0000380 fprintf (stderr, "Error: can't write fw_env to flash\n");
wdenk34b613e2002-12-17 01:51:00 +0000381 return (-1);
382 }
383
384 return (0);
385}
386
387static int flash_io (int mode)
388{
389 int fd, fdr, rc, otherdev, len, resid;
390 erase_info_t erase;
wdenkdbae5042003-06-21 00:17:24 +0000391 char *data = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000392
wdenk3f9ab982003-04-12 23:38:12 +0000393 if ((fd = open (DEVNAME (curdev), mode)) < 0) {
394 fprintf (stderr,
395 "Can't open %s: %s\n",
396 DEVNAME (curdev), strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000397 return (-1);
398 }
399
wdenk3f9ab982003-04-12 23:38:12 +0000400 len = sizeof (environment.crc);
wdenke7f34c62003-01-11 09:48:40 +0000401 if (HaveRedundEnv) {
wdenk3f9ab982003-04-12 23:38:12 +0000402 len += sizeof (environment.flags);
wdenke7f34c62003-01-11 09:48:40 +0000403 }
wdenk34b613e2002-12-17 01:51:00 +0000404
405 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +0000406 if (HaveRedundEnv) {
407 /* switch to next partition for writing */
408 otherdev = !curdev;
wdenk3f9ab982003-04-12 23:38:12 +0000409 if ((fdr = open (DEVNAME (otherdev), mode)) < 0) {
410 fprintf (stderr,
411 "Can't open %s: %s\n",
412 DEVNAME (otherdev),
413 strerror (errno));
wdenke7f34c62003-01-11 09:48:40 +0000414 return (-1);
415 }
416 } else {
417 otherdev = curdev;
418 fdr = fd;
wdenk34b613e2002-12-17 01:51:00 +0000419 }
wdenk3f9ab982003-04-12 23:38:12 +0000420 printf ("Unlocking flash...\n");
421 erase.length = DEVESIZE (otherdev);
422 erase.start = DEVOFFSET (otherdev);
wdenk34b613e2002-12-17 01:51:00 +0000423 ioctl (fdr, MEMUNLOCK, &erase);
424
wdenke7f34c62003-01-11 09:48:40 +0000425 if (HaveRedundEnv) {
wdenk3f9ab982003-04-12 23:38:12 +0000426 erase.length = DEVESIZE (curdev);
427 erase.start = DEVOFFSET (curdev);
wdenke7f34c62003-01-11 09:48:40 +0000428 ioctl (fd, MEMUNLOCK, &erase);
429 environment.flags = active_flag;
430 }
431
wdenk3f9ab982003-04-12 23:38:12 +0000432 printf ("Done\n");
433 resid = DEVESIZE (otherdev) - CFG_ENV_SIZE;
wdenk34b613e2002-12-17 01:51:00 +0000434 if (resid) {
wdenk3f9ab982003-04-12 23:38:12 +0000435 if ((data = malloc (resid)) == NULL) {
436 fprintf (stderr,
437 "Cannot malloc %d bytes: %s\n",
438 resid,
439 strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000440 return (-1);
441 }
wdenk3f9ab982003-04-12 23:38:12 +0000442 if (lseek (fdr, DEVOFFSET (otherdev) + CFG_ENV_SIZE, SEEK_SET)
443 == -1) {
444 fprintf (stderr, "seek error on %s: %s\n",
445 DEVNAME (otherdev),
446 strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000447 return (-1);
448 }
449 if ((rc = read (fdr, data, resid)) != resid) {
450 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000451 "read error on %s: %s\n",
452 DEVNAME (otherdev),
453 strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000454 return (-1);
455 }
456 }
457
wdenk3f9ab982003-04-12 23:38:12 +0000458 printf ("Erasing old environment...\n");
wdenk34b613e2002-12-17 01:51:00 +0000459
wdenk3f9ab982003-04-12 23:38:12 +0000460 erase.length = DEVESIZE (otherdev);
461 erase.start = DEVOFFSET (otherdev);
wdenk34b613e2002-12-17 01:51:00 +0000462 if (ioctl (fdr, MEMERASE, &erase) != 0) {
463 fprintf (stderr, "MTD erase error on %s: %s\n",
wdenk3f9ab982003-04-12 23:38:12 +0000464 DEVNAME (otherdev),
465 strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000466 return (-1);
467 }
468
wdenk3f9ab982003-04-12 23:38:12 +0000469 printf ("Done\n");
wdenk34b613e2002-12-17 01:51:00 +0000470
wdenk3f9ab982003-04-12 23:38:12 +0000471 printf ("Writing environment to %s...\n", DEVNAME (otherdev));
472 if (lseek (fdr, DEVOFFSET (otherdev), SEEK_SET) == -1) {
wdenke7f34c62003-01-11 09:48:40 +0000473 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000474 "seek error on %s: %s\n",
475 DEVNAME (otherdev), strerror (errno));
wdenke7f34c62003-01-11 09:48:40 +0000476 return (-1);
477 }
wdenk3f9ab982003-04-12 23:38:12 +0000478 if (write (fdr, &environment, len) != len) {
wdenk34b613e2002-12-17 01:51:00 +0000479 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000480 "CRC write error on %s: %s\n",
481 DEVNAME (otherdev), strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000482 return (-1);
483 }
wdenk3f9ab982003-04-12 23:38:12 +0000484 if (write (fdr, environment.data, ENV_SIZE) != ENV_SIZE) {
wdenk34b613e2002-12-17 01:51:00 +0000485 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000486 "Write error on %s: %s\n",
487 DEVNAME (otherdev), strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000488 return (-1);
489 }
490 if (resid) {
491 if (write (fdr, data, resid) != resid) {
492 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000493 "write error on %s: %s\n",
494 DEVNAME (curdev), strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000495 return (-1);
496 }
wdenk3f9ab982003-04-12 23:38:12 +0000497 free (data);
wdenk34b613e2002-12-17 01:51:00 +0000498 }
wdenke7f34c62003-01-11 09:48:40 +0000499 if (HaveRedundEnv) {
500 /* change flag on current active env partition */
wdenk3f9ab982003-04-12 23:38:12 +0000501 if (lseek (fd, DEVOFFSET (curdev) + sizeof (ulong), SEEK_SET)
502 == -1) {
503 fprintf (stderr, "seek error on %s: %s\n",
504 DEVNAME (curdev), strerror (errno));
wdenke7f34c62003-01-11 09:48:40 +0000505 return (-1);
506 }
wdenk3f9ab982003-04-12 23:38:12 +0000507 if (write (fd, &obsolete_flag, sizeof (obsolete_flag)) !=
508 sizeof (obsolete_flag)) {
wdenke7f34c62003-01-11 09:48:40 +0000509 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000510 "Write error on %s: %s\n",
511 DEVNAME (curdev), strerror (errno));
wdenke7f34c62003-01-11 09:48:40 +0000512 return (-1);
513 }
wdenk34b613e2002-12-17 01:51:00 +0000514 }
wdenk3f9ab982003-04-12 23:38:12 +0000515 printf ("Done\n");
516 printf ("Locking ...\n");
517 erase.length = DEVESIZE (otherdev);
518 erase.start = DEVOFFSET (otherdev);
wdenk34b613e2002-12-17 01:51:00 +0000519 ioctl (fdr, MEMLOCK, &erase);
wdenke7f34c62003-01-11 09:48:40 +0000520 if (HaveRedundEnv) {
wdenk3f9ab982003-04-12 23:38:12 +0000521 erase.length = DEVESIZE (curdev);
522 erase.start = DEVOFFSET (curdev);
wdenke7f34c62003-01-11 09:48:40 +0000523 ioctl (fd, MEMLOCK, &erase);
wdenk3f9ab982003-04-12 23:38:12 +0000524 if (close (fdr)) {
wdenke7f34c62003-01-11 09:48:40 +0000525 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000526 "I/O error on %s: %s\n",
527 DEVNAME (otherdev),
528 strerror (errno));
wdenke7f34c62003-01-11 09:48:40 +0000529 return (-1);
530 }
wdenk34b613e2002-12-17 01:51:00 +0000531 }
wdenk3f9ab982003-04-12 23:38:12 +0000532 printf ("Done\n");
wdenk34b613e2002-12-17 01:51:00 +0000533 } else {
wdenke7f34c62003-01-11 09:48:40 +0000534
wdenk3f9ab982003-04-12 23:38:12 +0000535 if (lseek (fd, DEVOFFSET (curdev), SEEK_SET) == -1) {
wdenke7f34c62003-01-11 09:48:40 +0000536 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000537 "seek error on %s: %s\n",
538 DEVNAME (curdev), strerror (errno));
wdenke7f34c62003-01-11 09:48:40 +0000539 return (-1);
540 }
wdenk34b613e2002-12-17 01:51:00 +0000541 if (read (fd, &environment, len) != len) {
542 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000543 "CRC read error on %s: %s\n",
544 DEVNAME (curdev), strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000545 return (-1);
546 }
547 if ((rc = read (fd, environment.data, ENV_SIZE)) != ENV_SIZE) {
548 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000549 "Read error on %s: %s\n",
550 DEVNAME (curdev), strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000551 return (-1);
552 }
553 }
554
wdenk3f9ab982003-04-12 23:38:12 +0000555 if (close (fd)) {
wdenk34b613e2002-12-17 01:51:00 +0000556 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000557 "I/O error on %s: %s\n",
558 DEVNAME (curdev), strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000559 return (-1);
560 }
561
562 /* everything ok */
563 return (0);
564}
565
566/*
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.
570 */
571
wdenk3f9ab982003-04-12 23:38:12 +0000572static uchar *envmatch (uchar * s1, uchar * s2)
wdenk34b613e2002-12-17 01:51:00 +0000573{
574
575 while (*s1 == *s2++)
576 if (*s1++ == '=')
wdenk3f9ab982003-04-12 23:38:12 +0000577 return (s2);
578 if (*s1 == '\0' && *(s2 - 1) == '=')
579 return (s2);
580 return (NULL);
wdenk34b613e2002-12-17 01:51:00 +0000581}
582
583/*
584 * Prevent confusion if running from erased flash memory
585 */
wdenk3f9ab982003-04-12 23:38:12 +0000586static int env_init (void)
wdenk34b613e2002-12-17 01:51:00 +0000587{
588 int crc1, crc1_ok;
589 uchar *addr1;
wdenke7f34c62003-01-11 09:48:40 +0000590
wdenk34b613e2002-12-17 01:51:00 +0000591 int crc2, crc2_ok;
592 uchar flag1, flag2, *addr2;
wdenk34b613e2002-12-17 01:51:00 +0000593
wdenk3f9ab982003-04-12 23:38:12 +0000594 if (parse_config ()) /* should fill envdevices */
wdenke7f34c62003-01-11 09:48:40 +0000595 return 1;
wdenk3f9ab982003-04-12 23:38:12 +0000596
wdenke7f34c62003-01-11 09:48:40 +0000597 if ((addr1 = calloc (1, ENV_SIZE)) == NULL) {
wdenk3f9ab982003-04-12 23:38:12 +0000598 fprintf (stderr,
599 "Not enough memory for environment (%ld bytes)\n",
600 ENV_SIZE);
wdenke7f34c62003-01-11 09:48:40 +0000601 return (errno);
602 }
wdenk3f9ab982003-04-12 23:38:12 +0000603
wdenke7f34c62003-01-11 09:48:40 +0000604 /* read environment from FLASH to local buffer */
605 environment.data = addr1;
606 curdev = 0;
607 if (flash_io (O_RDONLY)) {
608 return (errno);
609 }
wdenk3f9ab982003-04-12 23:38:12 +0000610
611 crc1_ok = ((crc1 = crc32 (0, environment.data, ENV_SIZE))
wdenke7f34c62003-01-11 09:48:40 +0000612 == environment.crc);
613 if (!HaveRedundEnv) {
wdenk34b613e2002-12-17 01:51:00 +0000614 if (!crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +0000615 fprintf (stderr,
616 "Warning: Bad CRC, using default environment\n");
wdenk34b613e2002-12-17 01:51:00 +0000617 environment.data = default_environment;
wdenk3f9ab982003-04-12 23:38:12 +0000618 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +0000619 }
wdenke7f34c62003-01-11 09:48:40 +0000620 } else {
wdenk34b613e2002-12-17 01:51:00 +0000621 flag1 = environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +0000622
wdenk34b613e2002-12-17 01:51:00 +0000623 curdev = 1;
624 if ((addr2 = calloc (1, ENV_SIZE)) == NULL) {
wdenk3f9ab982003-04-12 23:38:12 +0000625 fprintf (stderr,
626 "Not enough memory for environment (%ld bytes)\n",
627 ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000628 return (errno);
wdenk3f9ab982003-04-12 23:38:12 +0000629 }
wdenk34b613e2002-12-17 01:51:00 +0000630 environment.data = addr2;
wdenk3f9ab982003-04-12 23:38:12 +0000631
wdenk34b613e2002-12-17 01:51:00 +0000632 if (flash_io (O_RDONLY)) {
633 return (errno);
634 }
wdenk3f9ab982003-04-12 23:38:12 +0000635
636 crc2_ok = ((crc2 = crc32 (0, environment.data, ENV_SIZE))
wdenk34b613e2002-12-17 01:51:00 +0000637 == environment.crc);
638 flag2 = environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +0000639
640 if (crc1_ok && !crc2_ok) {
641 environment.data = addr1;
wdenk34b613e2002-12-17 01:51:00 +0000642 environment.flags = flag1;
643 environment.crc = crc1;
644 curdev = 0;
wdenk3f9ab982003-04-12 23:38:12 +0000645 free (addr2);
646 } else if (!crc1_ok && crc2_ok) {
647 environment.data = addr2;
wdenk34b613e2002-12-17 01:51:00 +0000648 environment.flags = flag2;
649 environment.crc = crc2;
650 curdev = 1;
wdenk3f9ab982003-04-12 23:38:12 +0000651 free (addr1);
652 } else if (!crc1_ok && !crc2_ok) {
653 fprintf (stderr,
654 "Warning: Bad CRC, using default environment\n");
wdenk34b613e2002-12-17 01:51:00 +0000655 environment.data = default_environment;
656 curdev = 0;
wdenk3f9ab982003-04-12 23:38:12 +0000657 free (addr2);
658 free (addr1);
659 } else if (flag1 == active_flag && flag2 == obsolete_flag) {
660 environment.data = addr1;
wdenk34b613e2002-12-17 01:51:00 +0000661 environment.flags = flag1;
662 environment.crc = crc1;
663 curdev = 0;
wdenk3f9ab982003-04-12 23:38:12 +0000664 free (addr2);
665 } else if (flag1 == obsolete_flag && flag2 == active_flag) {
666 environment.data = addr2;
wdenk34b613e2002-12-17 01:51:00 +0000667 environment.flags = flag2;
668 environment.crc = crc2;
669 curdev = 1;
wdenk3f9ab982003-04-12 23:38:12 +0000670 free (addr1);
671 } else if (flag1 == flag2) {
672 environment.data = addr1;
wdenk34b613e2002-12-17 01:51:00 +0000673 environment.flags = flag1;
674 environment.crc = crc1;
675 curdev = 0;
wdenk3f9ab982003-04-12 23:38:12 +0000676 free (addr2);
677 } else if (flag1 == 0xFF) {
678 environment.data = addr1;
wdenk34b613e2002-12-17 01:51:00 +0000679 environment.flags = flag1;
680 environment.crc = crc1;
681 curdev = 0;
wdenk3f9ab982003-04-12 23:38:12 +0000682 free (addr2);
683 } else if (flag2 == 0xFF) {
684 environment.data = addr2;
wdenk34b613e2002-12-17 01:51:00 +0000685 environment.flags = flag2;
686 environment.crc = crc2;
687 curdev = 1;
wdenk3f9ab982003-04-12 23:38:12 +0000688 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +0000689 }
wdenk34b613e2002-12-17 01:51:00 +0000690 }
691 return (0);
692}
693
694
wdenk3f9ab982003-04-12 23:38:12 +0000695static int parse_config ()
wdenk34b613e2002-12-17 01:51:00 +0000696{
697 struct stat st;
698
wdenke7f34c62003-01-11 09:48:40 +0000699#if defined(CONFIG_FILE)
700 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
wdenk3f9ab982003-04-12 23:38:12 +0000701 if (get_config (CONFIG_FILE)) {
wdenke7f34c62003-01-11 09:48:40 +0000702 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +0000703 "Cannot parse config file: %s\n", strerror (errno));
wdenk34b613e2002-12-17 01:51:00 +0000704 return 1;
705 }
wdenke7f34c62003-01-11 09:48:40 +0000706#else
wdenk3f9ab982003-04-12 23:38:12 +0000707 strcpy (DEVNAME (0), DEVICE1_NAME);
708 DEVOFFSET (0) = DEVICE1_OFFSET;
709 ENVSIZE (0) = ENV1_SIZE;
710 DEVESIZE (0) = DEVICE1_ESIZE;
wdenk34b613e2002-12-17 01:51:00 +0000711#ifdef HAVE_REDUND
wdenk3f9ab982003-04-12 23:38:12 +0000712 strcpy (DEVNAME (1), DEVICE2_NAME);
713 DEVOFFSET (1) = DEVICE2_OFFSET;
714 ENVSIZE (1) = ENV2_SIZE;
715 DEVESIZE (1) = DEVICE2_ESIZE;
wdenke7f34c62003-01-11 09:48:40 +0000716 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +0000717#endif
wdenke7f34c62003-01-11 09:48:40 +0000718#endif
wdenk3f9ab982003-04-12 23:38:12 +0000719 if (stat (DEVNAME (0), &st)) {
720 fprintf (stderr,
721 "Cannot access MTD device %s: %s\n",
722 DEVNAME (0), strerror (errno));
wdenke7f34c62003-01-11 09:48:40 +0000723 return 1;
724 }
wdenk3f9ab982003-04-12 23:38:12 +0000725
726 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
727 fprintf (stderr,
728 "Cannot access MTD device %s: %s\n",
Wolfgang Denk8e6213c2005-09-25 01:20:38 +0200729 DEVNAME (1), strerror (errno));
wdenke7f34c62003-01-11 09:48:40 +0000730 return 1;
731 }
wdenk34b613e2002-12-17 01:51:00 +0000732 return 0;
733}
wdenke7f34c62003-01-11 09:48:40 +0000734
735#if defined(CONFIG_FILE)
736static int get_config (char *fname)
737{
738 FILE *fp;
739 int i = 0;
740 int rc;
741 char dump[128];
742
wdenk3f9ab982003-04-12 23:38:12 +0000743 if ((fp = fopen (fname, "r")) == NULL) {
wdenke7f34c62003-01-11 09:48:40 +0000744 return 1;
745 }
746
wdenk3f9ab982003-04-12 23:38:12 +0000747 while ((i < 2) && ((rc = fscanf (fp, "%s %lx %lx %lx",
748 DEVNAME (i),
749 &DEVOFFSET (i),
750 &ENVSIZE (i),
751 &DEVESIZE (i) )) != EOF)) {
wdenke7f34c62003-01-11 09:48:40 +0000752
753 /* Skip incomplete conversions and comment strings */
wdenk3f9ab982003-04-12 23:38:12 +0000754 if ((rc < 3) || (*DEVNAME (i) == '#')) {
755 fgets (dump, sizeof (dump), fp); /* Consume till end */
wdenke7f34c62003-01-11 09:48:40 +0000756 continue;
757 }
758
759 i++;
760 }
wdenk3f9ab982003-04-12 23:38:12 +0000761 fclose (fp);
762
wdenke7f34c62003-01-11 09:48:40 +0000763 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +0000764 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +0000765 errno = EINVAL;
766 return 1;
767 } else
768 return 0;
769}
770#endif