blob: b8bca91c38d969500e6ad7fe9b54539b964adcfe [file] [log] [blame]
wdenk6aff3112002-12-17 01:51:00 +00001/*
Grant Ericksonbc117562008-05-06 20:16:15 -07002 * (C) Copyright 2000-2008
wdenk6aff3112002-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
wdenk4a6fd342003-04-12 23:38:12 +000015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenk6aff3112002-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>
wdenk6aff3112002-12-17 01:51:00 +000034
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010035#ifdef MTD_OLD
36# include <linux/mtd/mtd.h>
37#else
Wolfgang Denkc83d7ca2008-01-08 22:58:27 +010038# define __user /* nothing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010039# include <mtd/mtd-user.h>
40#endif
41
42#include "fw_env.h"
wdenk6aff3112002-12-17 01:51:00 +000043
44#define CMD_GETENV "fw_printenv"
45#define CMD_SETENV "fw_setenv"
46
47typedef struct envdev_s {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010048 char devname[16]; /* Device name */
wdenk4a6fd342003-04-12 23:38:12 +000049 ulong devoff; /* Device offset */
50 ulong env_size; /* environment size */
51 ulong erase_size; /* device erase size */
wdenk6aff3112002-12-17 01:51:00 +000052} envdev_t;
53
54static envdev_t envdevices[2];
55static int curdev;
56
57#define DEVNAME(i) envdevices[(i)].devname
wdenkd0fb80c2003-01-11 09:48:40 +000058#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk6aff3112002-12-17 01:51:00 +000059#define ENVSIZE(i) envdevices[(i)].env_size
60#define DEVESIZE(i) envdevices[(i)].erase_size
61
62#define CFG_ENV_SIZE ENVSIZE(curdev)
63
wdenkd0fb80c2003-01-11 09:48:40 +000064#define ENV_SIZE getenvsize()
wdenk6aff3112002-12-17 01:51:00 +000065
66typedef struct environment_s {
wdenk4a6fd342003-04-12 23:38:12 +000067 ulong crc; /* CRC32 over data bytes */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010068 unsigned char flags; /* active or obsolete */
69 char *data;
wdenk6aff3112002-12-17 01:51:00 +000070} env_t;
71
72static env_t environment;
wdenk6aff3112002-12-17 01:51:00 +000073
wdenkd0fb80c2003-01-11 09:48:40 +000074static int HaveRedundEnv = 0;
75
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010076static unsigned char active_flag = 1;
77static unsigned char obsolete_flag = 0;
wdenkd0fb80c2003-01-11 09:48:40 +000078
wdenk6aff3112002-12-17 01:51:00 +000079
80#define XMK_STR(x) #x
81#define MK_STR(x) XMK_STR(x)
82
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010083static char default_environment[] = {
wdenkd0fb80c2003-01-11 09:48:40 +000084#if defined(CONFIG_BOOTARGS)
wdenk4a6fd342003-04-12 23:38:12 +000085 "bootargs=" CONFIG_BOOTARGS "\0"
wdenk6aff3112002-12-17 01:51:00 +000086#endif
wdenkd0fb80c2003-01-11 09:48:40 +000087#if defined(CONFIG_BOOTCOMMAND)
wdenk4a6fd342003-04-12 23:38:12 +000088 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
wdenk6aff3112002-12-17 01:51:00 +000089#endif
wdenkd0fb80c2003-01-11 09:48:40 +000090#if defined(CONFIG_RAMBOOTCOMMAND)
wdenk4a6fd342003-04-12 23:38:12 +000091 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
wdenkd0fb80c2003-01-11 09:48:40 +000092#endif
93#if defined(CONFIG_NFSBOOTCOMMAND)
wdenk4a6fd342003-04-12 23:38:12 +000094 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
wdenkd0fb80c2003-01-11 09:48:40 +000095#endif
96#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
wdenk4a6fd342003-04-12 23:38:12 +000097 "bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0"
wdenk6aff3112002-12-17 01:51:00 +000098#endif
wdenkd0fb80c2003-01-11 09:48:40 +000099#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
wdenk4a6fd342003-04-12 23:38:12 +0000100 "baudrate=" MK_STR (CONFIG_BAUDRATE) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000101#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000102#ifdef CONFIG_LOADS_ECHO
wdenk4a6fd342003-04-12 23:38:12 +0000103 "loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000104#endif
wdenk6aff3112002-12-17 01:51:00 +0000105#ifdef CONFIG_ETHADDR
wdenk4a6fd342003-04-12 23:38:12 +0000106 "ethaddr=" MK_STR (CONFIG_ETHADDR) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000107#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000108#ifdef CONFIG_ETH1ADDR
wdenk4a6fd342003-04-12 23:38:12 +0000109 "eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000110#endif
111#ifdef CONFIG_ETH2ADDR
wdenk4a6fd342003-04-12 23:38:12 +0000112 "eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000113#endif
wdenke2ffd592004-12-31 09:32:47 +0000114#ifdef CONFIG_ETH3ADDR
115 "eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0"
116#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000117#ifdef CONFIG_ETHPRIME
wdenk4a6fd342003-04-12 23:38:12 +0000118 "ethprime=" CONFIG_ETHPRIME "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000119#endif
wdenk6aff3112002-12-17 01:51:00 +0000120#ifdef CONFIG_IPADDR
wdenk4a6fd342003-04-12 23:38:12 +0000121 "ipaddr=" MK_STR (CONFIG_IPADDR) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000122#endif
123#ifdef CONFIG_SERVERIP
wdenk4a6fd342003-04-12 23:38:12 +0000124 "serverip=" MK_STR (CONFIG_SERVERIP) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000125#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000126#ifdef CFG_AUTOLOAD
wdenk4a6fd342003-04-12 23:38:12 +0000127 "autoload=" CFG_AUTOLOAD "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000128#endif
129#ifdef CONFIG_ROOTPATH
wdenk4a6fd342003-04-12 23:38:12 +0000130 "rootpath=" MK_STR (CONFIG_ROOTPATH) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000131#endif
132#ifdef CONFIG_GATEWAYIP
wdenk4a6fd342003-04-12 23:38:12 +0000133 "gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000134#endif
135#ifdef CONFIG_NETMASK
wdenk4a6fd342003-04-12 23:38:12 +0000136 "netmask=" MK_STR (CONFIG_NETMASK) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000137#endif
138#ifdef CONFIG_HOSTNAME
wdenk4a6fd342003-04-12 23:38:12 +0000139 "hostname=" MK_STR (CONFIG_HOSTNAME) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000140#endif
141#ifdef CONFIG_BOOTFILE
wdenk4a6fd342003-04-12 23:38:12 +0000142 "bootfile=" MK_STR (CONFIG_BOOTFILE) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000143#endif
144#ifdef CONFIG_LOADADDR
wdenk4a6fd342003-04-12 23:38:12 +0000145 "loadaddr=" MK_STR (CONFIG_LOADADDR) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000146#endif
147#ifdef CONFIG_PREBOOT
wdenk4a6fd342003-04-12 23:38:12 +0000148 "preboot=" CONFIG_PREBOOT "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000149#endif
150#ifdef CONFIG_CLOCKS_IN_MHZ
wdenk4a6fd342003-04-12 23:38:12 +0000151 "clocks_in_mhz=" "1" "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000152#endif
stroesead10dd92003-02-14 11:21:23 +0000153#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
wdenk4a6fd342003-04-12 23:38:12 +0000154 "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0"
stroesead10dd92003-02-14 11:21:23 +0000155#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000156#ifdef CONFIG_EXTRA_ENV_SETTINGS
157 CONFIG_EXTRA_ENV_SETTINGS
158#endif
wdenk4a6fd342003-04-12 23:38:12 +0000159 "\0" /* Termimate env_t data with 2 NULs */
wdenk6aff3112002-12-17 01:51:00 +0000160};
161
wdenk4a6fd342003-04-12 23:38:12 +0000162static int flash_io (int mode);
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100163static char *envmatch (char * s1, char * s2);
wdenk4a6fd342003-04-12 23:38:12 +0000164static int env_init (void);
165static int parse_config (void);
166
wdenkd0fb80c2003-01-11 09:48:40 +0000167#if defined(CONFIG_FILE)
wdenk4a6fd342003-04-12 23:38:12 +0000168static int get_config (char *);
wdenkd0fb80c2003-01-11 09:48:40 +0000169#endif
wdenk4a6fd342003-04-12 23:38:12 +0000170static inline ulong getenvsize (void)
wdenkd0fb80c2003-01-11 09:48:40 +0000171{
wdenk4a6fd342003-04-12 23:38:12 +0000172 ulong rc = CFG_ENV_SIZE - sizeof (long);
173
wdenkd0fb80c2003-01-11 09:48:40 +0000174 if (HaveRedundEnv)
wdenk4a6fd342003-04-12 23:38:12 +0000175 rc -= sizeof (char);
wdenkd0fb80c2003-01-11 09:48:40 +0000176 return rc;
177}
wdenk6aff3112002-12-17 01:51:00 +0000178
179/*
180 * Search the environment for a variable.
181 * Return the value, if found, or NULL, if not found.
182 */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100183char *fw_getenv (char *name)
wdenk6aff3112002-12-17 01:51:00 +0000184{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100185 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000186
wdenk4a6fd342003-04-12 23:38:12 +0000187 if (env_init ())
wdenk6aff3112002-12-17 01:51:00 +0000188 return (NULL);
189
wdenk4a6fd342003-04-12 23:38:12 +0000190 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100191 char *val;
wdenk6aff3112002-12-17 01:51:00 +0000192
wdenk4a6fd342003-04-12 23:38:12 +0000193 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000194 if (nxt >= &environment.data[ENV_SIZE]) {
195 fprintf (stderr, "## Error: "
196 "environment not terminated\n");
197 return (NULL);
198 }
199 }
wdenk4a6fd342003-04-12 23:38:12 +0000200 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000201 if (!val)
202 continue;
203 return (val);
204 }
205 return (NULL);
206}
207
208/*
209 * Print the current definition of one, or more, or all
210 * environment variables
211 */
Grant Ericksonbc117562008-05-06 20:16:15 -0700212int fw_printenv (int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000213{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100214 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000215 int i, n_flag;
Grant Ericksonbc117562008-05-06 20:16:15 -0700216 int rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000217
wdenk4a6fd342003-04-12 23:38:12 +0000218 if (env_init ())
Grant Ericksonbc117562008-05-06 20:16:15 -0700219 return (-1);
wdenk6aff3112002-12-17 01:51:00 +0000220
wdenk4a6fd342003-04-12 23:38:12 +0000221 if (argc == 1) { /* Print all env variables */
222 for (env = environment.data; *env; env = nxt + 1) {
223 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000224 if (nxt >= &environment.data[ENV_SIZE]) {
225 fprintf (stderr, "## Error: "
226 "environment not terminated\n");
Grant Ericksonbc117562008-05-06 20:16:15 -0700227 return (-1);
wdenk6aff3112002-12-17 01:51:00 +0000228 }
229 }
230
wdenk4a6fd342003-04-12 23:38:12 +0000231 printf ("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000232 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700233 return (0);
wdenk6aff3112002-12-17 01:51:00 +0000234 }
235
wdenk4a6fd342003-04-12 23:38:12 +0000236 if (strcmp (argv[1], "-n") == 0) {
wdenk6aff3112002-12-17 01:51:00 +0000237 n_flag = 1;
238 ++argv;
239 --argc;
240 if (argc != 2) {
241 fprintf (stderr, "## Error: "
242 "`-n' option requires exactly one argument\n");
Grant Ericksonbc117562008-05-06 20:16:15 -0700243 return (-1);
wdenk6aff3112002-12-17 01:51:00 +0000244 }
245 } else {
246 n_flag = 0;
247 }
248
wdenk4a6fd342003-04-12 23:38:12 +0000249 for (i = 1; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100250 char *name = argv[i];
251 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000252
wdenk4a6fd342003-04-12 23:38:12 +0000253 for (env = environment.data; *env; env = nxt + 1) {
wdenk6aff3112002-12-17 01:51:00 +0000254
wdenk4a6fd342003-04-12 23:38:12 +0000255 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000256 if (nxt >= &environment.data[ENV_SIZE]) {
257 fprintf (stderr, "## Error: "
258 "environment not terminated\n");
Grant Ericksonbc117562008-05-06 20:16:15 -0700259 return (-1);
wdenk6aff3112002-12-17 01:51:00 +0000260 }
261 }
wdenk4a6fd342003-04-12 23:38:12 +0000262 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000263 if (val) {
264 if (!n_flag) {
265 fputs (name, stdout);
wdenk4a6fd342003-04-12 23:38:12 +0000266 putc ('=', stdout);
wdenk6aff3112002-12-17 01:51:00 +0000267 }
wdenk4a6fd342003-04-12 23:38:12 +0000268 puts (val);
wdenk6aff3112002-12-17 01:51:00 +0000269 break;
270 }
271 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700272 if (!val) {
wdenk4a6fd342003-04-12 23:38:12 +0000273 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700274 rc = -1;
275 }
wdenk6aff3112002-12-17 01:51:00 +0000276 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700277
278 return (rc);
wdenk6aff3112002-12-17 01:51:00 +0000279}
280
281/*
282 * Deletes or sets environment variables. Returns errno style error codes:
283 * 0 - OK
284 * EINVAL - need at least 1 argument
285 * EROFS - certain variables ("ethaddr", "serial#") cannot be
286 * modified or deleted
287 *
288 */
289int fw_setenv (int argc, char *argv[])
290{
wdenk4a6fd342003-04-12 23:38:12 +0000291 int i, len;
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100292 char *env, *nxt;
293 char *oldval = NULL;
294 char *name;
wdenk6aff3112002-12-17 01:51:00 +0000295
296 if (argc < 2) {
297 return (EINVAL);
298 }
299
wdenk4a6fd342003-04-12 23:38:12 +0000300 if (env_init ())
wdenk6aff3112002-12-17 01:51:00 +0000301 return (errno);
302
303 name = argv[1];
304
305 /*
306 * search if variable with this name already exists
307 */
wdenk4a6fd342003-04-12 23:38:12 +0000308 for (nxt = env = environment.data; *env; env = nxt + 1) {
309 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000310 if (nxt >= &environment.data[ENV_SIZE]) {
311 fprintf (stderr, "## Error: "
312 "environment not terminated\n");
313 return (EINVAL);
314 }
315 }
wdenk4a6fd342003-04-12 23:38:12 +0000316 if ((oldval = envmatch (name, env)) != NULL)
wdenk6aff3112002-12-17 01:51:00 +0000317 break;
318 }
319
320 /*
321 * Delete any existing definition
322 */
323 if (oldval) {
324 /*
325 * Ethernet Address and serial# can be set only once
326 */
327 if ((strcmp (name, "ethaddr") == 0) ||
wdenk4a6fd342003-04-12 23:38:12 +0000328 (strcmp (name, "serial#") == 0)) {
wdenk6aff3112002-12-17 01:51:00 +0000329 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
330 return (EROFS);
331 }
332
333 if (*++nxt == '\0') {
334 *env = '\0';
335 } else {
336 for (;;) {
337 *env = *nxt++;
338 if ((*env == '\0') && (*nxt == '\0'))
339 break;
340 ++env;
341 }
342 }
343 *++env = '\0';
344 }
345
346 /* Delete only ? */
347 if (argc < 3)
348 goto WRITE_FLASH;
349
350 /*
351 * Append new definition at the end
352 */
wdenk4a6fd342003-04-12 23:38:12 +0000353 for (env = environment.data; *env || *(env + 1); ++env);
wdenk6aff3112002-12-17 01:51:00 +0000354 if (env > environment.data)
355 ++env;
356 /*
357 * Overflow when:
358 * "name" + "=" + "val" +"\0\0" > CFG_ENV_SIZE - (env-environment)
359 */
wdenk4a6fd342003-04-12 23:38:12 +0000360 len = strlen (name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000361 /* add '=' for first arg, ' ' for all others */
wdenk4a6fd342003-04-12 23:38:12 +0000362 for (i = 2; i < argc; ++i) {
363 len += strlen (argv[i]) + 1;
wdenk6aff3112002-12-17 01:51:00 +0000364 }
wdenk4a6fd342003-04-12 23:38:12 +0000365 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk6aff3112002-12-17 01:51:00 +0000366 fprintf (stderr,
367 "Error: environment overflow, \"%s\" deleted\n",
368 name);
369 return (-1);
370 }
371 while ((*env = *name++) != '\0')
372 env++;
wdenk4a6fd342003-04-12 23:38:12 +0000373 for (i = 2; i < argc; ++i) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100374 char *val = argv[i];
wdenk6aff3112002-12-17 01:51:00 +0000375
wdenk4a6fd342003-04-12 23:38:12 +0000376 *env = (i == 2) ? '=' : ' ';
377 while ((*++env = *val++) != '\0');
wdenk6aff3112002-12-17 01:51:00 +0000378 }
379
380 /* end is marked with double '\0' */
381 *++env = '\0';
382
wdenk4a6fd342003-04-12 23:38:12 +0000383 WRITE_FLASH:
wdenk6aff3112002-12-17 01:51:00 +0000384
385 /* Update CRC */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100386 environment.crc = crc32 (0, (uint8_t*) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000387
388 /* write environment back to flash */
389 if (flash_io (O_RDWR)) {
wdenk4a6fd342003-04-12 23:38:12 +0000390 fprintf (stderr, "Error: can't write fw_env to flash\n");
wdenk6aff3112002-12-17 01:51:00 +0000391 return (-1);
392 }
393
394 return (0);
395}
396
397static int flash_io (int mode)
398{
399 int fd, fdr, rc, otherdev, len, resid;
400 erase_info_t erase;
wdenk592c5ca2003-06-21 00:17:24 +0000401 char *data = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000402
wdenk4a6fd342003-04-12 23:38:12 +0000403 if ((fd = open (DEVNAME (curdev), mode)) < 0) {
404 fprintf (stderr,
405 "Can't open %s: %s\n",
406 DEVNAME (curdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000407 return (-1);
408 }
409
wdenk4a6fd342003-04-12 23:38:12 +0000410 len = sizeof (environment.crc);
wdenkd0fb80c2003-01-11 09:48:40 +0000411 if (HaveRedundEnv) {
wdenk4a6fd342003-04-12 23:38:12 +0000412 len += sizeof (environment.flags);
wdenkd0fb80c2003-01-11 09:48:40 +0000413 }
wdenk6aff3112002-12-17 01:51:00 +0000414
415 if (mode == O_RDWR) {
wdenkd0fb80c2003-01-11 09:48:40 +0000416 if (HaveRedundEnv) {
417 /* switch to next partition for writing */
418 otherdev = !curdev;
wdenk4a6fd342003-04-12 23:38:12 +0000419 if ((fdr = open (DEVNAME (otherdev), mode)) < 0) {
420 fprintf (stderr,
421 "Can't open %s: %s\n",
422 DEVNAME (otherdev),
423 strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000424 return (-1);
425 }
426 } else {
427 otherdev = curdev;
428 fdr = fd;
wdenk6aff3112002-12-17 01:51:00 +0000429 }
wdenk4a6fd342003-04-12 23:38:12 +0000430 printf ("Unlocking flash...\n");
431 erase.length = DEVESIZE (otherdev);
432 erase.start = DEVOFFSET (otherdev);
wdenk6aff3112002-12-17 01:51:00 +0000433 ioctl (fdr, MEMUNLOCK, &erase);
434
wdenkd0fb80c2003-01-11 09:48:40 +0000435 if (HaveRedundEnv) {
wdenk4a6fd342003-04-12 23:38:12 +0000436 erase.length = DEVESIZE (curdev);
437 erase.start = DEVOFFSET (curdev);
wdenkd0fb80c2003-01-11 09:48:40 +0000438 ioctl (fd, MEMUNLOCK, &erase);
439 environment.flags = active_flag;
440 }
441
wdenk4a6fd342003-04-12 23:38:12 +0000442 printf ("Done\n");
443 resid = DEVESIZE (otherdev) - CFG_ENV_SIZE;
wdenk6aff3112002-12-17 01:51:00 +0000444 if (resid) {
wdenk4a6fd342003-04-12 23:38:12 +0000445 if ((data = malloc (resid)) == NULL) {
446 fprintf (stderr,
447 "Cannot malloc %d bytes: %s\n",
448 resid,
449 strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000450 return (-1);
451 }
wdenk4a6fd342003-04-12 23:38:12 +0000452 if (lseek (fdr, DEVOFFSET (otherdev) + CFG_ENV_SIZE, SEEK_SET)
453 == -1) {
454 fprintf (stderr, "seek error on %s: %s\n",
455 DEVNAME (otherdev),
456 strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000457 return (-1);
458 }
459 if ((rc = read (fdr, data, resid)) != resid) {
460 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000461 "read error on %s: %s\n",
462 DEVNAME (otherdev),
463 strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000464 return (-1);
465 }
466 }
467
wdenk4a6fd342003-04-12 23:38:12 +0000468 printf ("Erasing old environment...\n");
wdenk6aff3112002-12-17 01:51:00 +0000469
wdenk4a6fd342003-04-12 23:38:12 +0000470 erase.length = DEVESIZE (otherdev);
471 erase.start = DEVOFFSET (otherdev);
wdenk6aff3112002-12-17 01:51:00 +0000472 if (ioctl (fdr, MEMERASE, &erase) != 0) {
473 fprintf (stderr, "MTD erase error on %s: %s\n",
wdenk4a6fd342003-04-12 23:38:12 +0000474 DEVNAME (otherdev),
475 strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000476 return (-1);
477 }
478
wdenk4a6fd342003-04-12 23:38:12 +0000479 printf ("Done\n");
wdenk6aff3112002-12-17 01:51:00 +0000480
wdenk4a6fd342003-04-12 23:38:12 +0000481 printf ("Writing environment to %s...\n", DEVNAME (otherdev));
482 if (lseek (fdr, DEVOFFSET (otherdev), SEEK_SET) == -1) {
wdenkd0fb80c2003-01-11 09:48:40 +0000483 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000484 "seek error on %s: %s\n",
485 DEVNAME (otherdev), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000486 return (-1);
487 }
wdenk4a6fd342003-04-12 23:38:12 +0000488 if (write (fdr, &environment, len) != len) {
wdenk6aff3112002-12-17 01:51:00 +0000489 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000490 "CRC write error on %s: %s\n",
491 DEVNAME (otherdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000492 return (-1);
493 }
wdenk4a6fd342003-04-12 23:38:12 +0000494 if (write (fdr, environment.data, ENV_SIZE) != ENV_SIZE) {
wdenk6aff3112002-12-17 01:51:00 +0000495 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000496 "Write error on %s: %s\n",
497 DEVNAME (otherdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000498 return (-1);
499 }
500 if (resid) {
501 if (write (fdr, data, resid) != resid) {
502 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000503 "write error on %s: %s\n",
504 DEVNAME (curdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000505 return (-1);
506 }
wdenk4a6fd342003-04-12 23:38:12 +0000507 free (data);
wdenk6aff3112002-12-17 01:51:00 +0000508 }
wdenkd0fb80c2003-01-11 09:48:40 +0000509 if (HaveRedundEnv) {
510 /* change flag on current active env partition */
wdenk4a6fd342003-04-12 23:38:12 +0000511 if (lseek (fd, DEVOFFSET (curdev) + sizeof (ulong), SEEK_SET)
512 == -1) {
513 fprintf (stderr, "seek error on %s: %s\n",
514 DEVNAME (curdev), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000515 return (-1);
516 }
wdenk4a6fd342003-04-12 23:38:12 +0000517 if (write (fd, &obsolete_flag, sizeof (obsolete_flag)) !=
518 sizeof (obsolete_flag)) {
wdenkd0fb80c2003-01-11 09:48:40 +0000519 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000520 "Write error on %s: %s\n",
521 DEVNAME (curdev), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000522 return (-1);
523 }
wdenk6aff3112002-12-17 01:51:00 +0000524 }
wdenk4a6fd342003-04-12 23:38:12 +0000525 printf ("Done\n");
526 printf ("Locking ...\n");
527 erase.length = DEVESIZE (otherdev);
528 erase.start = DEVOFFSET (otherdev);
wdenk6aff3112002-12-17 01:51:00 +0000529 ioctl (fdr, MEMLOCK, &erase);
wdenkd0fb80c2003-01-11 09:48:40 +0000530 if (HaveRedundEnv) {
wdenk4a6fd342003-04-12 23:38:12 +0000531 erase.length = DEVESIZE (curdev);
532 erase.start = DEVOFFSET (curdev);
wdenkd0fb80c2003-01-11 09:48:40 +0000533 ioctl (fd, MEMLOCK, &erase);
wdenk4a6fd342003-04-12 23:38:12 +0000534 if (close (fdr)) {
wdenkd0fb80c2003-01-11 09:48:40 +0000535 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000536 "I/O error on %s: %s\n",
537 DEVNAME (otherdev),
538 strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000539 return (-1);
540 }
wdenk6aff3112002-12-17 01:51:00 +0000541 }
wdenk4a6fd342003-04-12 23:38:12 +0000542 printf ("Done\n");
wdenk6aff3112002-12-17 01:51:00 +0000543 } else {
wdenkd0fb80c2003-01-11 09:48:40 +0000544
wdenk4a6fd342003-04-12 23:38:12 +0000545 if (lseek (fd, DEVOFFSET (curdev), SEEK_SET) == -1) {
wdenkd0fb80c2003-01-11 09:48:40 +0000546 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000547 "seek error on %s: %s\n",
548 DEVNAME (curdev), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000549 return (-1);
550 }
wdenk6aff3112002-12-17 01:51:00 +0000551 if (read (fd, &environment, len) != len) {
552 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000553 "CRC read error on %s: %s\n",
554 DEVNAME (curdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000555 return (-1);
556 }
557 if ((rc = read (fd, environment.data, ENV_SIZE)) != ENV_SIZE) {
558 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000559 "Read error on %s: %s\n",
560 DEVNAME (curdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000561 return (-1);
562 }
563 }
564
wdenk4a6fd342003-04-12 23:38:12 +0000565 if (close (fd)) {
wdenk6aff3112002-12-17 01:51:00 +0000566 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000567 "I/O error on %s: %s\n",
568 DEVNAME (curdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000569 return (-1);
570 }
571
572 /* everything ok */
573 return (0);
574}
575
576/*
577 * s1 is either a simple 'name', or a 'name=value' pair.
578 * s2 is a 'name=value' pair.
579 * If the names match, return the value of s2, else NULL.
580 */
581
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100582static char *envmatch (char * s1, char * s2)
wdenk6aff3112002-12-17 01:51:00 +0000583{
584
585 while (*s1 == *s2++)
586 if (*s1++ == '=')
wdenk4a6fd342003-04-12 23:38:12 +0000587 return (s2);
588 if (*s1 == '\0' && *(s2 - 1) == '=')
589 return (s2);
590 return (NULL);
wdenk6aff3112002-12-17 01:51:00 +0000591}
592
593/*
594 * Prevent confusion if running from erased flash memory
595 */
wdenk4a6fd342003-04-12 23:38:12 +0000596static int env_init (void)
wdenk6aff3112002-12-17 01:51:00 +0000597{
598 int crc1, crc1_ok;
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100599 char *addr1;
wdenkd0fb80c2003-01-11 09:48:40 +0000600
wdenk6aff3112002-12-17 01:51:00 +0000601 int crc2, crc2_ok;
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100602 char flag1, flag2, *addr2;
wdenk6aff3112002-12-17 01:51:00 +0000603
wdenk4a6fd342003-04-12 23:38:12 +0000604 if (parse_config ()) /* should fill envdevices */
wdenkd0fb80c2003-01-11 09:48:40 +0000605 return 1;
wdenk4a6fd342003-04-12 23:38:12 +0000606
wdenkd0fb80c2003-01-11 09:48:40 +0000607 if ((addr1 = calloc (1, ENV_SIZE)) == NULL) {
wdenk4a6fd342003-04-12 23:38:12 +0000608 fprintf (stderr,
609 "Not enough memory for environment (%ld bytes)\n",
610 ENV_SIZE);
wdenkd0fb80c2003-01-11 09:48:40 +0000611 return (errno);
612 }
wdenk4a6fd342003-04-12 23:38:12 +0000613
wdenkd0fb80c2003-01-11 09:48:40 +0000614 /* read environment from FLASH to local buffer */
615 environment.data = addr1;
616 curdev = 0;
617 if (flash_io (O_RDONLY)) {
618 return (errno);
619 }
wdenk4a6fd342003-04-12 23:38:12 +0000620
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100621 crc1_ok = ((crc1 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE))
wdenkd0fb80c2003-01-11 09:48:40 +0000622 == environment.crc);
623 if (!HaveRedundEnv) {
wdenk6aff3112002-12-17 01:51:00 +0000624 if (!crc1_ok) {
wdenk4a6fd342003-04-12 23:38:12 +0000625 fprintf (stderr,
626 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +0100627 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +0000628 }
wdenkd0fb80c2003-01-11 09:48:40 +0000629 } else {
wdenk6aff3112002-12-17 01:51:00 +0000630 flag1 = environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +0000631
wdenk6aff3112002-12-17 01:51:00 +0000632 curdev = 1;
633 if ((addr2 = calloc (1, ENV_SIZE)) == NULL) {
wdenk4a6fd342003-04-12 23:38:12 +0000634 fprintf (stderr,
635 "Not enough memory for environment (%ld bytes)\n",
636 ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000637 return (errno);
wdenk4a6fd342003-04-12 23:38:12 +0000638 }
wdenk6aff3112002-12-17 01:51:00 +0000639 environment.data = addr2;
wdenk4a6fd342003-04-12 23:38:12 +0000640
wdenk6aff3112002-12-17 01:51:00 +0000641 if (flash_io (O_RDONLY)) {
642 return (errno);
643 }
wdenk4a6fd342003-04-12 23:38:12 +0000644
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100645 crc2_ok = ((crc2 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE))
wdenk6aff3112002-12-17 01:51:00 +0000646 == environment.crc);
647 flag2 = environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +0000648
649 if (crc1_ok && !crc2_ok) {
650 environment.data = addr1;
wdenk6aff3112002-12-17 01:51:00 +0000651 environment.flags = flag1;
652 environment.crc = crc1;
653 curdev = 0;
wdenk4a6fd342003-04-12 23:38:12 +0000654 free (addr2);
655 } else if (!crc1_ok && crc2_ok) {
656 environment.data = addr2;
wdenk6aff3112002-12-17 01:51:00 +0000657 environment.flags = flag2;
658 environment.crc = crc2;
659 curdev = 1;
wdenk4a6fd342003-04-12 23:38:12 +0000660 free (addr1);
661 } else if (!crc1_ok && !crc2_ok) {
662 fprintf (stderr,
663 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +0100664 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +0000665 curdev = 0;
wdenk4a6fd342003-04-12 23:38:12 +0000666 free (addr1);
667 } else if (flag1 == active_flag && flag2 == obsolete_flag) {
668 environment.data = addr1;
wdenk6aff3112002-12-17 01:51:00 +0000669 environment.flags = flag1;
670 environment.crc = crc1;
671 curdev = 0;
wdenk4a6fd342003-04-12 23:38:12 +0000672 free (addr2);
673 } else if (flag1 == obsolete_flag && flag2 == active_flag) {
674 environment.data = addr2;
wdenk6aff3112002-12-17 01:51:00 +0000675 environment.flags = flag2;
676 environment.crc = crc2;
677 curdev = 1;
wdenk4a6fd342003-04-12 23:38:12 +0000678 free (addr1);
679 } else if (flag1 == flag2) {
680 environment.data = addr1;
wdenk6aff3112002-12-17 01:51:00 +0000681 environment.flags = flag1;
682 environment.crc = crc1;
683 curdev = 0;
wdenk4a6fd342003-04-12 23:38:12 +0000684 free (addr2);
685 } else if (flag1 == 0xFF) {
686 environment.data = addr1;
wdenk6aff3112002-12-17 01:51:00 +0000687 environment.flags = flag1;
688 environment.crc = crc1;
689 curdev = 0;
wdenk4a6fd342003-04-12 23:38:12 +0000690 free (addr2);
691 } else if (flag2 == 0xFF) {
692 environment.data = addr2;
wdenk6aff3112002-12-17 01:51:00 +0000693 environment.flags = flag2;
694 environment.crc = crc2;
695 curdev = 1;
wdenk4a6fd342003-04-12 23:38:12 +0000696 free (addr1);
wdenk6aff3112002-12-17 01:51:00 +0000697 }
wdenk6aff3112002-12-17 01:51:00 +0000698 }
699 return (0);
700}
701
702
wdenk4a6fd342003-04-12 23:38:12 +0000703static int parse_config ()
wdenk6aff3112002-12-17 01:51:00 +0000704{
705 struct stat st;
706
wdenkd0fb80c2003-01-11 09:48:40 +0000707#if defined(CONFIG_FILE)
708 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
wdenk4a6fd342003-04-12 23:38:12 +0000709 if (get_config (CONFIG_FILE)) {
wdenkd0fb80c2003-01-11 09:48:40 +0000710 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000711 "Cannot parse config file: %s\n", strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000712 return 1;
713 }
wdenkd0fb80c2003-01-11 09:48:40 +0000714#else
wdenk4a6fd342003-04-12 23:38:12 +0000715 strcpy (DEVNAME (0), DEVICE1_NAME);
716 DEVOFFSET (0) = DEVICE1_OFFSET;
717 ENVSIZE (0) = ENV1_SIZE;
718 DEVESIZE (0) = DEVICE1_ESIZE;
wdenk6aff3112002-12-17 01:51:00 +0000719#ifdef HAVE_REDUND
wdenk4a6fd342003-04-12 23:38:12 +0000720 strcpy (DEVNAME (1), DEVICE2_NAME);
721 DEVOFFSET (1) = DEVICE2_OFFSET;
722 ENVSIZE (1) = ENV2_SIZE;
723 DEVESIZE (1) = DEVICE2_ESIZE;
wdenkd0fb80c2003-01-11 09:48:40 +0000724 HaveRedundEnv = 1;
wdenk6aff3112002-12-17 01:51:00 +0000725#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000726#endif
wdenk4a6fd342003-04-12 23:38:12 +0000727 if (stat (DEVNAME (0), &st)) {
728 fprintf (stderr,
729 "Cannot access MTD device %s: %s\n",
730 DEVNAME (0), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000731 return 1;
732 }
wdenk4a6fd342003-04-12 23:38:12 +0000733
734 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
735 fprintf (stderr,
736 "Cannot access MTD device %s: %s\n",
Wolfgang Denke2146b62005-09-25 01:20:38 +0200737 DEVNAME (1), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000738 return 1;
739 }
wdenk6aff3112002-12-17 01:51:00 +0000740 return 0;
741}
wdenkd0fb80c2003-01-11 09:48:40 +0000742
743#if defined(CONFIG_FILE)
744static int get_config (char *fname)
745{
746 FILE *fp;
747 int i = 0;
748 int rc;
749 char dump[128];
750
wdenk4a6fd342003-04-12 23:38:12 +0000751 if ((fp = fopen (fname, "r")) == NULL) {
wdenkd0fb80c2003-01-11 09:48:40 +0000752 return 1;
753 }
754
wdenk4a6fd342003-04-12 23:38:12 +0000755 while ((i < 2) && ((rc = fscanf (fp, "%s %lx %lx %lx",
756 DEVNAME (i),
757 &DEVOFFSET (i),
758 &ENVSIZE (i),
759 &DEVESIZE (i) )) != EOF)) {
wdenkd0fb80c2003-01-11 09:48:40 +0000760
761 /* Skip incomplete conversions and comment strings */
wdenk4a6fd342003-04-12 23:38:12 +0000762 if ((rc < 3) || (*DEVNAME (i) == '#')) {
763 fgets (dump, sizeof (dump), fp); /* Consume till end */
wdenkd0fb80c2003-01-11 09:48:40 +0000764 continue;
765 }
766
767 i++;
768 }
wdenk4a6fd342003-04-12 23:38:12 +0000769 fclose (fp);
770
wdenkd0fb80c2003-01-11 09:48:40 +0000771 HaveRedundEnv = i - 1;
wdenk4a6fd342003-04-12 23:38:12 +0000772 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +0000773 errno = EINVAL;
774 return 1;
775 } else
776 return 0;
777}
778#endif