blob: e4fc02d29bc0e9acb938b92a3e3c042b56b4d65e [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
Wolfgang Denk1711f3b2008-09-02 21:17:36 +020036# include <stdint.h>
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010037# include <linux/mtd/mtd.h>
38#else
Wolfgang Denkc83d7ca2008-01-08 22:58:27 +010039# define __user /* nothing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010040# include <mtd/mtd-user.h>
41#endif
42
43#include "fw_env.h"
wdenk6aff3112002-12-17 01:51:00 +000044
45#define CMD_GETENV "fw_printenv"
46#define CMD_SETENV "fw_setenv"
47
48typedef struct envdev_s {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010049 char devname[16]; /* Device name */
wdenk4a6fd342003-04-12 23:38:12 +000050 ulong devoff; /* Device offset */
51 ulong env_size; /* environment size */
52 ulong erase_size; /* device erase size */
wdenk6aff3112002-12-17 01:51:00 +000053} envdev_t;
54
55static envdev_t envdevices[2];
56static int curdev;
57
58#define DEVNAME(i) envdevices[(i)].devname
wdenkd0fb80c2003-01-11 09:48:40 +000059#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk6aff3112002-12-17 01:51:00 +000060#define ENVSIZE(i) envdevices[(i)].env_size
61#define DEVESIZE(i) envdevices[(i)].erase_size
62
63#define CFG_ENV_SIZE ENVSIZE(curdev)
64
wdenkd0fb80c2003-01-11 09:48:40 +000065#define ENV_SIZE getenvsize()
wdenk6aff3112002-12-17 01:51:00 +000066
67typedef struct environment_s {
wdenk4a6fd342003-04-12 23:38:12 +000068 ulong crc; /* CRC32 over data bytes */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010069 unsigned char flags; /* active or obsolete */
70 char *data;
wdenk6aff3112002-12-17 01:51:00 +000071} env_t;
72
73static env_t environment;
wdenk6aff3112002-12-17 01:51:00 +000074
wdenkd0fb80c2003-01-11 09:48:40 +000075static int HaveRedundEnv = 0;
76
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010077static unsigned char active_flag = 1;
78static unsigned char obsolete_flag = 0;
wdenkd0fb80c2003-01-11 09:48:40 +000079
wdenk6aff3112002-12-17 01:51:00 +000080
81#define XMK_STR(x) #x
82#define MK_STR(x) XMK_STR(x)
83
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010084static char default_environment[] = {
wdenkd0fb80c2003-01-11 09:48:40 +000085#if defined(CONFIG_BOOTARGS)
wdenk4a6fd342003-04-12 23:38:12 +000086 "bootargs=" CONFIG_BOOTARGS "\0"
wdenk6aff3112002-12-17 01:51:00 +000087#endif
wdenkd0fb80c2003-01-11 09:48:40 +000088#if defined(CONFIG_BOOTCOMMAND)
wdenk4a6fd342003-04-12 23:38:12 +000089 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
wdenk6aff3112002-12-17 01:51:00 +000090#endif
wdenkd0fb80c2003-01-11 09:48:40 +000091#if defined(CONFIG_RAMBOOTCOMMAND)
wdenk4a6fd342003-04-12 23:38:12 +000092 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
wdenkd0fb80c2003-01-11 09:48:40 +000093#endif
94#if defined(CONFIG_NFSBOOTCOMMAND)
wdenk4a6fd342003-04-12 23:38:12 +000095 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
wdenkd0fb80c2003-01-11 09:48:40 +000096#endif
97#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
wdenk4a6fd342003-04-12 23:38:12 +000098 "bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0"
wdenk6aff3112002-12-17 01:51:00 +000099#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000100#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
wdenk4a6fd342003-04-12 23:38:12 +0000101 "baudrate=" MK_STR (CONFIG_BAUDRATE) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000102#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000103#ifdef CONFIG_LOADS_ECHO
wdenk4a6fd342003-04-12 23:38:12 +0000104 "loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000105#endif
wdenk6aff3112002-12-17 01:51:00 +0000106#ifdef CONFIG_ETHADDR
wdenk4a6fd342003-04-12 23:38:12 +0000107 "ethaddr=" MK_STR (CONFIG_ETHADDR) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000108#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000109#ifdef CONFIG_ETH1ADDR
wdenk4a6fd342003-04-12 23:38:12 +0000110 "eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000111#endif
112#ifdef CONFIG_ETH2ADDR
wdenk4a6fd342003-04-12 23:38:12 +0000113 "eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000114#endif
wdenke2ffd592004-12-31 09:32:47 +0000115#ifdef CONFIG_ETH3ADDR
116 "eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0"
117#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000118#ifdef CONFIG_ETHPRIME
wdenk4a6fd342003-04-12 23:38:12 +0000119 "ethprime=" CONFIG_ETHPRIME "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000120#endif
wdenk6aff3112002-12-17 01:51:00 +0000121#ifdef CONFIG_IPADDR
wdenk4a6fd342003-04-12 23:38:12 +0000122 "ipaddr=" MK_STR (CONFIG_IPADDR) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000123#endif
124#ifdef CONFIG_SERVERIP
wdenk4a6fd342003-04-12 23:38:12 +0000125 "serverip=" MK_STR (CONFIG_SERVERIP) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000126#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000127#ifdef CFG_AUTOLOAD
wdenk4a6fd342003-04-12 23:38:12 +0000128 "autoload=" CFG_AUTOLOAD "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000129#endif
130#ifdef CONFIG_ROOTPATH
wdenk4a6fd342003-04-12 23:38:12 +0000131 "rootpath=" MK_STR (CONFIG_ROOTPATH) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000132#endif
133#ifdef CONFIG_GATEWAYIP
wdenk4a6fd342003-04-12 23:38:12 +0000134 "gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000135#endif
136#ifdef CONFIG_NETMASK
wdenk4a6fd342003-04-12 23:38:12 +0000137 "netmask=" MK_STR (CONFIG_NETMASK) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000138#endif
139#ifdef CONFIG_HOSTNAME
wdenk4a6fd342003-04-12 23:38:12 +0000140 "hostname=" MK_STR (CONFIG_HOSTNAME) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000141#endif
142#ifdef CONFIG_BOOTFILE
wdenk4a6fd342003-04-12 23:38:12 +0000143 "bootfile=" MK_STR (CONFIG_BOOTFILE) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000144#endif
145#ifdef CONFIG_LOADADDR
wdenk4a6fd342003-04-12 23:38:12 +0000146 "loadaddr=" MK_STR (CONFIG_LOADADDR) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000147#endif
148#ifdef CONFIG_PREBOOT
wdenk4a6fd342003-04-12 23:38:12 +0000149 "preboot=" CONFIG_PREBOOT "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000150#endif
151#ifdef CONFIG_CLOCKS_IN_MHZ
wdenk4a6fd342003-04-12 23:38:12 +0000152 "clocks_in_mhz=" "1" "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000153#endif
stroesead10dd92003-02-14 11:21:23 +0000154#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
wdenk4a6fd342003-04-12 23:38:12 +0000155 "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0"
stroesead10dd92003-02-14 11:21:23 +0000156#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000157#ifdef CONFIG_EXTRA_ENV_SETTINGS
158 CONFIG_EXTRA_ENV_SETTINGS
159#endif
wdenk4a6fd342003-04-12 23:38:12 +0000160 "\0" /* Termimate env_t data with 2 NULs */
wdenk6aff3112002-12-17 01:51:00 +0000161};
162
wdenk4a6fd342003-04-12 23:38:12 +0000163static int flash_io (int mode);
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100164static char *envmatch (char * s1, char * s2);
wdenk4a6fd342003-04-12 23:38:12 +0000165static int env_init (void);
166static int parse_config (void);
167
wdenkd0fb80c2003-01-11 09:48:40 +0000168#if defined(CONFIG_FILE)
wdenk4a6fd342003-04-12 23:38:12 +0000169static int get_config (char *);
wdenkd0fb80c2003-01-11 09:48:40 +0000170#endif
wdenk4a6fd342003-04-12 23:38:12 +0000171static inline ulong getenvsize (void)
wdenkd0fb80c2003-01-11 09:48:40 +0000172{
wdenk4a6fd342003-04-12 23:38:12 +0000173 ulong rc = CFG_ENV_SIZE - sizeof (long);
174
wdenkd0fb80c2003-01-11 09:48:40 +0000175 if (HaveRedundEnv)
wdenk4a6fd342003-04-12 23:38:12 +0000176 rc -= sizeof (char);
wdenkd0fb80c2003-01-11 09:48:40 +0000177 return rc;
178}
wdenk6aff3112002-12-17 01:51:00 +0000179
180/*
181 * Search the environment for a variable.
182 * Return the value, if found, or NULL, if not found.
183 */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100184char *fw_getenv (char *name)
wdenk6aff3112002-12-17 01:51:00 +0000185{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100186 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000187
wdenk4a6fd342003-04-12 23:38:12 +0000188 if (env_init ())
wdenk6aff3112002-12-17 01:51:00 +0000189 return (NULL);
190
wdenk4a6fd342003-04-12 23:38:12 +0000191 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100192 char *val;
wdenk6aff3112002-12-17 01:51:00 +0000193
wdenk4a6fd342003-04-12 23:38:12 +0000194 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000195 if (nxt >= &environment.data[ENV_SIZE]) {
196 fprintf (stderr, "## Error: "
197 "environment not terminated\n");
198 return (NULL);
199 }
200 }
wdenk4a6fd342003-04-12 23:38:12 +0000201 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000202 if (!val)
203 continue;
204 return (val);
205 }
206 return (NULL);
207}
208
209/*
210 * Print the current definition of one, or more, or all
211 * environment variables
212 */
Grant Ericksonbc117562008-05-06 20:16:15 -0700213int fw_printenv (int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000214{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100215 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000216 int i, n_flag;
Grant Ericksonbc117562008-05-06 20:16:15 -0700217 int rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000218
wdenk4a6fd342003-04-12 23:38:12 +0000219 if (env_init ())
Grant Ericksonbc117562008-05-06 20:16:15 -0700220 return (-1);
wdenk6aff3112002-12-17 01:51:00 +0000221
wdenk4a6fd342003-04-12 23:38:12 +0000222 if (argc == 1) { /* Print all env variables */
223 for (env = environment.data; *env; env = nxt + 1) {
224 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000225 if (nxt >= &environment.data[ENV_SIZE]) {
226 fprintf (stderr, "## Error: "
227 "environment not terminated\n");
Grant Ericksonbc117562008-05-06 20:16:15 -0700228 return (-1);
wdenk6aff3112002-12-17 01:51:00 +0000229 }
230 }
231
wdenk4a6fd342003-04-12 23:38:12 +0000232 printf ("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000233 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700234 return (0);
wdenk6aff3112002-12-17 01:51:00 +0000235 }
236
wdenk4a6fd342003-04-12 23:38:12 +0000237 if (strcmp (argv[1], "-n") == 0) {
wdenk6aff3112002-12-17 01:51:00 +0000238 n_flag = 1;
239 ++argv;
240 --argc;
241 if (argc != 2) {
242 fprintf (stderr, "## Error: "
243 "`-n' option requires exactly one argument\n");
Grant Ericksonbc117562008-05-06 20:16:15 -0700244 return (-1);
wdenk6aff3112002-12-17 01:51:00 +0000245 }
246 } else {
247 n_flag = 0;
248 }
249
wdenk4a6fd342003-04-12 23:38:12 +0000250 for (i = 1; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100251 char *name = argv[i];
252 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000253
wdenk4a6fd342003-04-12 23:38:12 +0000254 for (env = environment.data; *env; env = nxt + 1) {
wdenk6aff3112002-12-17 01:51:00 +0000255
wdenk4a6fd342003-04-12 23:38:12 +0000256 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000257 if (nxt >= &environment.data[ENV_SIZE]) {
258 fprintf (stderr, "## Error: "
259 "environment not terminated\n");
Grant Ericksonbc117562008-05-06 20:16:15 -0700260 return (-1);
wdenk6aff3112002-12-17 01:51:00 +0000261 }
262 }
wdenk4a6fd342003-04-12 23:38:12 +0000263 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000264 if (val) {
265 if (!n_flag) {
266 fputs (name, stdout);
wdenk4a6fd342003-04-12 23:38:12 +0000267 putc ('=', stdout);
wdenk6aff3112002-12-17 01:51:00 +0000268 }
wdenk4a6fd342003-04-12 23:38:12 +0000269 puts (val);
wdenk6aff3112002-12-17 01:51:00 +0000270 break;
271 }
272 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700273 if (!val) {
wdenk4a6fd342003-04-12 23:38:12 +0000274 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700275 rc = -1;
276 }
wdenk6aff3112002-12-17 01:51:00 +0000277 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700278
279 return (rc);
wdenk6aff3112002-12-17 01:51:00 +0000280}
281
282/*
283 * Deletes or sets environment variables. Returns errno style error codes:
284 * 0 - OK
285 * EINVAL - need at least 1 argument
286 * EROFS - certain variables ("ethaddr", "serial#") cannot be
287 * modified or deleted
288 *
289 */
290int fw_setenv (int argc, char *argv[])
291{
wdenk4a6fd342003-04-12 23:38:12 +0000292 int i, len;
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100293 char *env, *nxt;
294 char *oldval = NULL;
295 char *name;
wdenk6aff3112002-12-17 01:51:00 +0000296
297 if (argc < 2) {
298 return (EINVAL);
299 }
300
wdenk4a6fd342003-04-12 23:38:12 +0000301 if (env_init ())
wdenk6aff3112002-12-17 01:51:00 +0000302 return (errno);
303
304 name = argv[1];
305
306 /*
307 * search if variable with this name already exists
308 */
wdenk4a6fd342003-04-12 23:38:12 +0000309 for (nxt = env = environment.data; *env; env = nxt + 1) {
310 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000311 if (nxt >= &environment.data[ENV_SIZE]) {
312 fprintf (stderr, "## Error: "
313 "environment not terminated\n");
314 return (EINVAL);
315 }
316 }
wdenk4a6fd342003-04-12 23:38:12 +0000317 if ((oldval = envmatch (name, env)) != NULL)
wdenk6aff3112002-12-17 01:51:00 +0000318 break;
319 }
320
321 /*
322 * Delete any existing definition
323 */
324 if (oldval) {
325 /*
326 * Ethernet Address and serial# can be set only once
327 */
328 if ((strcmp (name, "ethaddr") == 0) ||
wdenk4a6fd342003-04-12 23:38:12 +0000329 (strcmp (name, "serial#") == 0)) {
wdenk6aff3112002-12-17 01:51:00 +0000330 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
331 return (EROFS);
332 }
333
334 if (*++nxt == '\0') {
335 *env = '\0';
336 } else {
337 for (;;) {
338 *env = *nxt++;
339 if ((*env == '\0') && (*nxt == '\0'))
340 break;
341 ++env;
342 }
343 }
344 *++env = '\0';
345 }
346
347 /* Delete only ? */
348 if (argc < 3)
349 goto WRITE_FLASH;
350
351 /*
352 * Append new definition at the end
353 */
wdenk4a6fd342003-04-12 23:38:12 +0000354 for (env = environment.data; *env || *(env + 1); ++env);
wdenk6aff3112002-12-17 01:51:00 +0000355 if (env > environment.data)
356 ++env;
357 /*
358 * Overflow when:
359 * "name" + "=" + "val" +"\0\0" > CFG_ENV_SIZE - (env-environment)
360 */
wdenk4a6fd342003-04-12 23:38:12 +0000361 len = strlen (name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000362 /* add '=' for first arg, ' ' for all others */
wdenk4a6fd342003-04-12 23:38:12 +0000363 for (i = 2; i < argc; ++i) {
364 len += strlen (argv[i]) + 1;
wdenk6aff3112002-12-17 01:51:00 +0000365 }
wdenk4a6fd342003-04-12 23:38:12 +0000366 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk6aff3112002-12-17 01:51:00 +0000367 fprintf (stderr,
368 "Error: environment overflow, \"%s\" deleted\n",
369 name);
370 return (-1);
371 }
372 while ((*env = *name++) != '\0')
373 env++;
wdenk4a6fd342003-04-12 23:38:12 +0000374 for (i = 2; i < argc; ++i) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100375 char *val = argv[i];
wdenk6aff3112002-12-17 01:51:00 +0000376
wdenk4a6fd342003-04-12 23:38:12 +0000377 *env = (i == 2) ? '=' : ' ';
378 while ((*++env = *val++) != '\0');
wdenk6aff3112002-12-17 01:51:00 +0000379 }
380
381 /* end is marked with double '\0' */
382 *++env = '\0';
383
wdenk4a6fd342003-04-12 23:38:12 +0000384 WRITE_FLASH:
wdenk6aff3112002-12-17 01:51:00 +0000385
386 /* Update CRC */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100387 environment.crc = crc32 (0, (uint8_t*) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000388
389 /* write environment back to flash */
390 if (flash_io (O_RDWR)) {
wdenk4a6fd342003-04-12 23:38:12 +0000391 fprintf (stderr, "Error: can't write fw_env to flash\n");
wdenk6aff3112002-12-17 01:51:00 +0000392 return (-1);
393 }
394
395 return (0);
396}
397
398static int flash_io (int mode)
399{
400 int fd, fdr, rc, otherdev, len, resid;
Wolfgang Denk1711f3b2008-09-02 21:17:36 +0200401 struct erase_info_user erase;
wdenk592c5ca2003-06-21 00:17:24 +0000402 char *data = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000403
wdenk4a6fd342003-04-12 23:38:12 +0000404 if ((fd = open (DEVNAME (curdev), mode)) < 0) {
405 fprintf (stderr,
406 "Can't open %s: %s\n",
407 DEVNAME (curdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000408 return (-1);
409 }
410
wdenk4a6fd342003-04-12 23:38:12 +0000411 len = sizeof (environment.crc);
wdenkd0fb80c2003-01-11 09:48:40 +0000412 if (HaveRedundEnv) {
wdenk4a6fd342003-04-12 23:38:12 +0000413 len += sizeof (environment.flags);
wdenkd0fb80c2003-01-11 09:48:40 +0000414 }
wdenk6aff3112002-12-17 01:51:00 +0000415
416 if (mode == O_RDWR) {
wdenkd0fb80c2003-01-11 09:48:40 +0000417 if (HaveRedundEnv) {
418 /* switch to next partition for writing */
419 otherdev = !curdev;
wdenk4a6fd342003-04-12 23:38:12 +0000420 if ((fdr = open (DEVNAME (otherdev), mode)) < 0) {
421 fprintf (stderr,
422 "Can't open %s: %s\n",
423 DEVNAME (otherdev),
424 strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000425 return (-1);
426 }
427 } else {
428 otherdev = curdev;
429 fdr = fd;
wdenk6aff3112002-12-17 01:51:00 +0000430 }
wdenk4a6fd342003-04-12 23:38:12 +0000431 printf ("Unlocking flash...\n");
432 erase.length = DEVESIZE (otherdev);
433 erase.start = DEVOFFSET (otherdev);
wdenk6aff3112002-12-17 01:51:00 +0000434 ioctl (fdr, MEMUNLOCK, &erase);
435
wdenkd0fb80c2003-01-11 09:48:40 +0000436 if (HaveRedundEnv) {
wdenk4a6fd342003-04-12 23:38:12 +0000437 erase.length = DEVESIZE (curdev);
438 erase.start = DEVOFFSET (curdev);
wdenkd0fb80c2003-01-11 09:48:40 +0000439 ioctl (fd, MEMUNLOCK, &erase);
440 environment.flags = active_flag;
441 }
442
wdenk4a6fd342003-04-12 23:38:12 +0000443 printf ("Done\n");
444 resid = DEVESIZE (otherdev) - CFG_ENV_SIZE;
wdenk6aff3112002-12-17 01:51:00 +0000445 if (resid) {
wdenk4a6fd342003-04-12 23:38:12 +0000446 if ((data = malloc (resid)) == NULL) {
447 fprintf (stderr,
448 "Cannot malloc %d bytes: %s\n",
449 resid,
450 strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000451 return (-1);
452 }
wdenk4a6fd342003-04-12 23:38:12 +0000453 if (lseek (fdr, DEVOFFSET (otherdev) + CFG_ENV_SIZE, SEEK_SET)
454 == -1) {
455 fprintf (stderr, "seek error on %s: %s\n",
456 DEVNAME (otherdev),
457 strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000458 return (-1);
459 }
460 if ((rc = read (fdr, data, resid)) != resid) {
461 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000462 "read error on %s: %s\n",
463 DEVNAME (otherdev),
464 strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000465 return (-1);
466 }
467 }
468
wdenk4a6fd342003-04-12 23:38:12 +0000469 printf ("Erasing old environment...\n");
wdenk6aff3112002-12-17 01:51:00 +0000470
wdenk4a6fd342003-04-12 23:38:12 +0000471 erase.length = DEVESIZE (otherdev);
472 erase.start = DEVOFFSET (otherdev);
wdenk6aff3112002-12-17 01:51:00 +0000473 if (ioctl (fdr, MEMERASE, &erase) != 0) {
474 fprintf (stderr, "MTD erase error on %s: %s\n",
wdenk4a6fd342003-04-12 23:38:12 +0000475 DEVNAME (otherdev),
476 strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000477 return (-1);
478 }
479
wdenk4a6fd342003-04-12 23:38:12 +0000480 printf ("Done\n");
wdenk6aff3112002-12-17 01:51:00 +0000481
wdenk4a6fd342003-04-12 23:38:12 +0000482 printf ("Writing environment to %s...\n", DEVNAME (otherdev));
483 if (lseek (fdr, DEVOFFSET (otherdev), SEEK_SET) == -1) {
wdenkd0fb80c2003-01-11 09:48:40 +0000484 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000485 "seek error on %s: %s\n",
486 DEVNAME (otherdev), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000487 return (-1);
488 }
wdenk4a6fd342003-04-12 23:38:12 +0000489 if (write (fdr, &environment, len) != len) {
wdenk6aff3112002-12-17 01:51:00 +0000490 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000491 "CRC write error on %s: %s\n",
492 DEVNAME (otherdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000493 return (-1);
494 }
wdenk4a6fd342003-04-12 23:38:12 +0000495 if (write (fdr, environment.data, ENV_SIZE) != ENV_SIZE) {
wdenk6aff3112002-12-17 01:51:00 +0000496 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000497 "Write error on %s: %s\n",
498 DEVNAME (otherdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000499 return (-1);
500 }
501 if (resid) {
502 if (write (fdr, data, resid) != resid) {
503 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000504 "write error on %s: %s\n",
505 DEVNAME (curdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000506 return (-1);
507 }
wdenk4a6fd342003-04-12 23:38:12 +0000508 free (data);
wdenk6aff3112002-12-17 01:51:00 +0000509 }
wdenkd0fb80c2003-01-11 09:48:40 +0000510 if (HaveRedundEnv) {
511 /* change flag on current active env partition */
wdenk4a6fd342003-04-12 23:38:12 +0000512 if (lseek (fd, DEVOFFSET (curdev) + sizeof (ulong), SEEK_SET)
513 == -1) {
514 fprintf (stderr, "seek error on %s: %s\n",
515 DEVNAME (curdev), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000516 return (-1);
517 }
wdenk4a6fd342003-04-12 23:38:12 +0000518 if (write (fd, &obsolete_flag, sizeof (obsolete_flag)) !=
519 sizeof (obsolete_flag)) {
wdenkd0fb80c2003-01-11 09:48:40 +0000520 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000521 "Write error on %s: %s\n",
522 DEVNAME (curdev), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000523 return (-1);
524 }
wdenk6aff3112002-12-17 01:51:00 +0000525 }
wdenk4a6fd342003-04-12 23:38:12 +0000526 printf ("Done\n");
527 printf ("Locking ...\n");
528 erase.length = DEVESIZE (otherdev);
529 erase.start = DEVOFFSET (otherdev);
wdenk6aff3112002-12-17 01:51:00 +0000530 ioctl (fdr, MEMLOCK, &erase);
wdenkd0fb80c2003-01-11 09:48:40 +0000531 if (HaveRedundEnv) {
wdenk4a6fd342003-04-12 23:38:12 +0000532 erase.length = DEVESIZE (curdev);
533 erase.start = DEVOFFSET (curdev);
wdenkd0fb80c2003-01-11 09:48:40 +0000534 ioctl (fd, MEMLOCK, &erase);
wdenk4a6fd342003-04-12 23:38:12 +0000535 if (close (fdr)) {
wdenkd0fb80c2003-01-11 09:48:40 +0000536 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000537 "I/O error on %s: %s\n",
538 DEVNAME (otherdev),
539 strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000540 return (-1);
541 }
wdenk6aff3112002-12-17 01:51:00 +0000542 }
wdenk4a6fd342003-04-12 23:38:12 +0000543 printf ("Done\n");
wdenk6aff3112002-12-17 01:51:00 +0000544 } else {
wdenkd0fb80c2003-01-11 09:48:40 +0000545
wdenk4a6fd342003-04-12 23:38:12 +0000546 if (lseek (fd, DEVOFFSET (curdev), SEEK_SET) == -1) {
wdenkd0fb80c2003-01-11 09:48:40 +0000547 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000548 "seek error on %s: %s\n",
549 DEVNAME (curdev), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000550 return (-1);
551 }
wdenk6aff3112002-12-17 01:51:00 +0000552 if (read (fd, &environment, len) != len) {
553 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000554 "CRC read error on %s: %s\n",
555 DEVNAME (curdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000556 return (-1);
557 }
558 if ((rc = read (fd, environment.data, ENV_SIZE)) != ENV_SIZE) {
559 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000560 "Read error on %s: %s\n",
561 DEVNAME (curdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000562 return (-1);
563 }
564 }
565
wdenk4a6fd342003-04-12 23:38:12 +0000566 if (close (fd)) {
wdenk6aff3112002-12-17 01:51:00 +0000567 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000568 "I/O error on %s: %s\n",
569 DEVNAME (curdev), strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000570 return (-1);
571 }
572
573 /* everything ok */
574 return (0);
575}
576
577/*
578 * s1 is either a simple 'name', or a 'name=value' pair.
579 * s2 is a 'name=value' pair.
580 * If the names match, return the value of s2, else NULL.
581 */
582
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100583static char *envmatch (char * s1, char * s2)
wdenk6aff3112002-12-17 01:51:00 +0000584{
585
586 while (*s1 == *s2++)
587 if (*s1++ == '=')
wdenk4a6fd342003-04-12 23:38:12 +0000588 return (s2);
589 if (*s1 == '\0' && *(s2 - 1) == '=')
590 return (s2);
591 return (NULL);
wdenk6aff3112002-12-17 01:51:00 +0000592}
593
594/*
595 * Prevent confusion if running from erased flash memory
596 */
wdenk4a6fd342003-04-12 23:38:12 +0000597static int env_init (void)
wdenk6aff3112002-12-17 01:51:00 +0000598{
599 int crc1, crc1_ok;
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100600 char *addr1;
wdenkd0fb80c2003-01-11 09:48:40 +0000601
wdenk6aff3112002-12-17 01:51:00 +0000602 int crc2, crc2_ok;
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100603 char flag1, flag2, *addr2;
wdenk6aff3112002-12-17 01:51:00 +0000604
wdenk4a6fd342003-04-12 23:38:12 +0000605 if (parse_config ()) /* should fill envdevices */
wdenkd0fb80c2003-01-11 09:48:40 +0000606 return 1;
wdenk4a6fd342003-04-12 23:38:12 +0000607
wdenkd0fb80c2003-01-11 09:48:40 +0000608 if ((addr1 = calloc (1, ENV_SIZE)) == NULL) {
wdenk4a6fd342003-04-12 23:38:12 +0000609 fprintf (stderr,
610 "Not enough memory for environment (%ld bytes)\n",
611 ENV_SIZE);
wdenkd0fb80c2003-01-11 09:48:40 +0000612 return (errno);
613 }
wdenk4a6fd342003-04-12 23:38:12 +0000614
wdenkd0fb80c2003-01-11 09:48:40 +0000615 /* read environment from FLASH to local buffer */
616 environment.data = addr1;
617 curdev = 0;
618 if (flash_io (O_RDONLY)) {
619 return (errno);
620 }
wdenk4a6fd342003-04-12 23:38:12 +0000621
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100622 crc1_ok = ((crc1 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE))
wdenkd0fb80c2003-01-11 09:48:40 +0000623 == environment.crc);
624 if (!HaveRedundEnv) {
wdenk6aff3112002-12-17 01:51:00 +0000625 if (!crc1_ok) {
wdenk4a6fd342003-04-12 23:38:12 +0000626 fprintf (stderr,
627 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +0100628 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +0000629 }
wdenkd0fb80c2003-01-11 09:48:40 +0000630 } else {
wdenk6aff3112002-12-17 01:51:00 +0000631 flag1 = environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +0000632
wdenk6aff3112002-12-17 01:51:00 +0000633 curdev = 1;
634 if ((addr2 = calloc (1, ENV_SIZE)) == NULL) {
wdenk4a6fd342003-04-12 23:38:12 +0000635 fprintf (stderr,
636 "Not enough memory for environment (%ld bytes)\n",
637 ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000638 return (errno);
wdenk4a6fd342003-04-12 23:38:12 +0000639 }
wdenk6aff3112002-12-17 01:51:00 +0000640 environment.data = addr2;
wdenk4a6fd342003-04-12 23:38:12 +0000641
wdenk6aff3112002-12-17 01:51:00 +0000642 if (flash_io (O_RDONLY)) {
643 return (errno);
644 }
wdenk4a6fd342003-04-12 23:38:12 +0000645
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100646 crc2_ok = ((crc2 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE))
wdenk6aff3112002-12-17 01:51:00 +0000647 == environment.crc);
648 flag2 = environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +0000649
650 if (crc1_ok && !crc2_ok) {
651 environment.data = addr1;
wdenk6aff3112002-12-17 01:51:00 +0000652 environment.flags = flag1;
653 environment.crc = crc1;
654 curdev = 0;
wdenk4a6fd342003-04-12 23:38:12 +0000655 free (addr2);
656 } else if (!crc1_ok && crc2_ok) {
657 environment.data = addr2;
wdenk6aff3112002-12-17 01:51:00 +0000658 environment.flags = flag2;
659 environment.crc = crc2;
660 curdev = 1;
wdenk4a6fd342003-04-12 23:38:12 +0000661 free (addr1);
662 } else if (!crc1_ok && !crc2_ok) {
663 fprintf (stderr,
664 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +0100665 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +0000666 curdev = 0;
wdenk4a6fd342003-04-12 23:38:12 +0000667 free (addr1);
668 } else if (flag1 == active_flag && flag2 == obsolete_flag) {
669 environment.data = addr1;
wdenk6aff3112002-12-17 01:51:00 +0000670 environment.flags = flag1;
671 environment.crc = crc1;
672 curdev = 0;
wdenk4a6fd342003-04-12 23:38:12 +0000673 free (addr2);
674 } else if (flag1 == obsolete_flag && flag2 == active_flag) {
675 environment.data = addr2;
wdenk6aff3112002-12-17 01:51:00 +0000676 environment.flags = flag2;
677 environment.crc = crc2;
678 curdev = 1;
wdenk4a6fd342003-04-12 23:38:12 +0000679 free (addr1);
680 } else if (flag1 == flag2) {
681 environment.data = addr1;
wdenk6aff3112002-12-17 01:51:00 +0000682 environment.flags = flag1;
683 environment.crc = crc1;
684 curdev = 0;
wdenk4a6fd342003-04-12 23:38:12 +0000685 free (addr2);
686 } else if (flag1 == 0xFF) {
687 environment.data = addr1;
wdenk6aff3112002-12-17 01:51:00 +0000688 environment.flags = flag1;
689 environment.crc = crc1;
690 curdev = 0;
wdenk4a6fd342003-04-12 23:38:12 +0000691 free (addr2);
692 } else if (flag2 == 0xFF) {
693 environment.data = addr2;
wdenk6aff3112002-12-17 01:51:00 +0000694 environment.flags = flag2;
695 environment.crc = crc2;
696 curdev = 1;
wdenk4a6fd342003-04-12 23:38:12 +0000697 free (addr1);
wdenk6aff3112002-12-17 01:51:00 +0000698 }
wdenk6aff3112002-12-17 01:51:00 +0000699 }
700 return (0);
701}
702
703
wdenk4a6fd342003-04-12 23:38:12 +0000704static int parse_config ()
wdenk6aff3112002-12-17 01:51:00 +0000705{
706 struct stat st;
707
wdenkd0fb80c2003-01-11 09:48:40 +0000708#if defined(CONFIG_FILE)
709 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
wdenk4a6fd342003-04-12 23:38:12 +0000710 if (get_config (CONFIG_FILE)) {
wdenkd0fb80c2003-01-11 09:48:40 +0000711 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +0000712 "Cannot parse config file: %s\n", strerror (errno));
wdenk6aff3112002-12-17 01:51:00 +0000713 return 1;
714 }
wdenkd0fb80c2003-01-11 09:48:40 +0000715#else
wdenk4a6fd342003-04-12 23:38:12 +0000716 strcpy (DEVNAME (0), DEVICE1_NAME);
717 DEVOFFSET (0) = DEVICE1_OFFSET;
718 ENVSIZE (0) = ENV1_SIZE;
719 DEVESIZE (0) = DEVICE1_ESIZE;
wdenk6aff3112002-12-17 01:51:00 +0000720#ifdef HAVE_REDUND
wdenk4a6fd342003-04-12 23:38:12 +0000721 strcpy (DEVNAME (1), DEVICE2_NAME);
722 DEVOFFSET (1) = DEVICE2_OFFSET;
723 ENVSIZE (1) = ENV2_SIZE;
724 DEVESIZE (1) = DEVICE2_ESIZE;
wdenkd0fb80c2003-01-11 09:48:40 +0000725 HaveRedundEnv = 1;
wdenk6aff3112002-12-17 01:51:00 +0000726#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000727#endif
wdenk4a6fd342003-04-12 23:38:12 +0000728 if (stat (DEVNAME (0), &st)) {
729 fprintf (stderr,
730 "Cannot access MTD device %s: %s\n",
731 DEVNAME (0), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000732 return 1;
733 }
wdenk4a6fd342003-04-12 23:38:12 +0000734
735 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
736 fprintf (stderr,
737 "Cannot access MTD device %s: %s\n",
Wolfgang Denke2146b62005-09-25 01:20:38 +0200738 DEVNAME (1), strerror (errno));
wdenkd0fb80c2003-01-11 09:48:40 +0000739 return 1;
740 }
wdenk6aff3112002-12-17 01:51:00 +0000741 return 0;
742}
wdenkd0fb80c2003-01-11 09:48:40 +0000743
744#if defined(CONFIG_FILE)
745static int get_config (char *fname)
746{
747 FILE *fp;
748 int i = 0;
749 int rc;
750 char dump[128];
751
wdenk4a6fd342003-04-12 23:38:12 +0000752 if ((fp = fopen (fname, "r")) == NULL) {
wdenkd0fb80c2003-01-11 09:48:40 +0000753 return 1;
754 }
755
wdenk4a6fd342003-04-12 23:38:12 +0000756 while ((i < 2) && ((rc = fscanf (fp, "%s %lx %lx %lx",
757 DEVNAME (i),
758 &DEVOFFSET (i),
759 &ENVSIZE (i),
760 &DEVESIZE (i) )) != EOF)) {
wdenkd0fb80c2003-01-11 09:48:40 +0000761
762 /* Skip incomplete conversions and comment strings */
wdenk4a6fd342003-04-12 23:38:12 +0000763 if ((rc < 3) || (*DEVNAME (i) == '#')) {
764 fgets (dump, sizeof (dump), fp); /* Consume till end */
wdenkd0fb80c2003-01-11 09:48:40 +0000765 continue;
766 }
767
768 i++;
769 }
wdenk4a6fd342003-04-12 23:38:12 +0000770 fclose (fp);
771
wdenkd0fb80c2003-01-11 09:48:40 +0000772 HaveRedundEnv = i - 1;
wdenk4a6fd342003-04-12 23:38:12 +0000773 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +0000774 errno = EINVAL;
775 return 1;
776 } else
777 return 0;
778}
779#endif