blob: e292d2ba1db43e1f248fb30beadb9732762b0393 [file] [log] [blame]
wdenk6aff3112002-12-17 01:51:00 +00001/*
Detlev Zundel0aef7bc2010-07-30 11:22:15 +02002 * (C) Copyright 2000-2010
wdenk6aff3112002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02005 * (C) Copyright 2008
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
7 *
wdenk6aff3112002-12-17 01:51:00 +00008 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wdenk4a6fd342003-04-12 23:38:12 +000018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenk6aff3112002-12-17 01:51:00 +000019 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <errno.h>
28#include <fcntl.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <stddef.h>
32#include <string.h>
33#include <sys/types.h>
34#include <sys/ioctl.h>
35#include <sys/stat.h>
36#include <unistd.h>
wdenk6aff3112002-12-17 01:51:00 +000037
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010038#ifdef MTD_OLD
Wolfgang Denk1711f3b2008-09-02 21:17:36 +020039# include <stdint.h>
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010040# include <linux/mtd/mtd.h>
41#else
Wolfgang Denkc83d7ca2008-01-08 22:58:27 +010042# define __user /* nothing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010043# include <mtd/mtd-user.h>
44#endif
45
46#include "fw_env.h"
wdenk6aff3112002-12-17 01:51:00 +000047
Grant Ericksonaa701b92011-12-22 08:59:55 +000048#include <config.h>
49
Stefano Babicbd7b26f2010-05-24 12:08:16 +020050#define WHITESPACE(c) ((c == '\t') || (c == ' '))
wdenk6aff3112002-12-17 01:51:00 +000051
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020052#define min(x, y) ({ \
53 typeof(x) _min1 = (x); \
54 typeof(y) _min2 = (y); \
55 (void) (&_min1 == &_min2); \
56 _min1 < _min2 ? _min1 : _min2; })
57
58struct envdev_s {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010059 char devname[16]; /* Device name */
wdenk4a6fd342003-04-12 23:38:12 +000060 ulong devoff; /* Device offset */
61 ulong env_size; /* environment size */
62 ulong erase_size; /* device erase size */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020063 ulong env_sectors; /* number of environment sectors */
64 uint8_t mtd_type; /* type of the MTD device */
65};
wdenk6aff3112002-12-17 01:51:00 +000066
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020067static struct envdev_s envdevices[2] =
68{
69 {
70 .mtd_type = MTD_ABSENT,
71 }, {
72 .mtd_type = MTD_ABSENT,
73 },
74};
75static int dev_current;
wdenk6aff3112002-12-17 01:51:00 +000076
77#define DEVNAME(i) envdevices[(i)].devname
wdenkd0fb80c2003-01-11 09:48:40 +000078#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk6aff3112002-12-17 01:51:00 +000079#define ENVSIZE(i) envdevices[(i)].env_size
80#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020081#define ENVSECTORS(i) envdevices[(i)].env_sectors
82#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk6aff3112002-12-17 01:51:00 +000083
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020084#define CONFIG_ENV_SIZE ENVSIZE(dev_current)
wdenk6aff3112002-12-17 01:51:00 +000085
wdenkd0fb80c2003-01-11 09:48:40 +000086#define ENV_SIZE getenvsize()
wdenk6aff3112002-12-17 01:51:00 +000087
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020088struct env_image_single {
89 uint32_t crc; /* CRC32 over data bytes */
90 char data[];
91};
wdenk6aff3112002-12-17 01:51:00 +000092
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020093struct env_image_redundant {
94 uint32_t crc; /* CRC32 over data bytes */
95 unsigned char flags; /* active or obsolete */
96 char data[];
97};
98
99enum flag_scheme {
100 FLAG_NONE,
101 FLAG_BOOLEAN,
102 FLAG_INCREMENTAL,
103};
104
105struct environment {
106 void *image;
107 uint32_t *crc;
108 unsigned char *flags;
109 char *data;
110 enum flag_scheme flag_scheme;
111};
112
113static struct environment environment = {
114 .flag_scheme = FLAG_NONE,
115};
wdenk6aff3112002-12-17 01:51:00 +0000116
wdenkd0fb80c2003-01-11 09:48:40 +0000117static int HaveRedundEnv = 0;
118
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100119static unsigned char active_flag = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200120/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100121static unsigned char obsolete_flag = 0;
wdenkd0fb80c2003-01-11 09:48:40 +0000122
wdenk6aff3112002-12-17 01:51:00 +0000123
124#define XMK_STR(x) #x
125#define MK_STR(x) XMK_STR(x)
126
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100127static char default_environment[] = {
wdenkd0fb80c2003-01-11 09:48:40 +0000128#if defined(CONFIG_BOOTARGS)
wdenk4a6fd342003-04-12 23:38:12 +0000129 "bootargs=" CONFIG_BOOTARGS "\0"
wdenk6aff3112002-12-17 01:51:00 +0000130#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000131#if defined(CONFIG_BOOTCOMMAND)
wdenk4a6fd342003-04-12 23:38:12 +0000132 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
wdenk6aff3112002-12-17 01:51:00 +0000133#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000134#if defined(CONFIG_RAMBOOTCOMMAND)
wdenk4a6fd342003-04-12 23:38:12 +0000135 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000136#endif
137#if defined(CONFIG_NFSBOOTCOMMAND)
wdenk4a6fd342003-04-12 23:38:12 +0000138 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000139#endif
140#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
wdenk4a6fd342003-04-12 23:38:12 +0000141 "bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000142#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000143#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
wdenk4a6fd342003-04-12 23:38:12 +0000144 "baudrate=" MK_STR (CONFIG_BAUDRATE) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000145#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000146#ifdef CONFIG_LOADS_ECHO
wdenk4a6fd342003-04-12 23:38:12 +0000147 "loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000148#endif
wdenk6aff3112002-12-17 01:51:00 +0000149#ifdef CONFIG_ETHADDR
wdenk4a6fd342003-04-12 23:38:12 +0000150 "ethaddr=" MK_STR (CONFIG_ETHADDR) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000151#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000152#ifdef CONFIG_ETH1ADDR
wdenk4a6fd342003-04-12 23:38:12 +0000153 "eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000154#endif
155#ifdef CONFIG_ETH2ADDR
wdenk4a6fd342003-04-12 23:38:12 +0000156 "eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000157#endif
wdenke2ffd592004-12-31 09:32:47 +0000158#ifdef CONFIG_ETH3ADDR
159 "eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0"
160#endif
richardretanubunc68a05f2008-09-29 18:28:23 -0400161#ifdef CONFIG_ETH4ADDR
162 "eth4addr=" MK_STR (CONFIG_ETH4ADDR) "\0"
163#endif
164#ifdef CONFIG_ETH5ADDR
165 "eth5addr=" MK_STR (CONFIG_ETH5ADDR) "\0"
166#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000167#ifdef CONFIG_ETHPRIME
wdenk4a6fd342003-04-12 23:38:12 +0000168 "ethprime=" CONFIG_ETHPRIME "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000169#endif
wdenk6aff3112002-12-17 01:51:00 +0000170#ifdef CONFIG_IPADDR
wdenk4a6fd342003-04-12 23:38:12 +0000171 "ipaddr=" MK_STR (CONFIG_IPADDR) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000172#endif
173#ifdef CONFIG_SERVERIP
wdenk4a6fd342003-04-12 23:38:12 +0000174 "serverip=" MK_STR (CONFIG_SERVERIP) "\0"
wdenk6aff3112002-12-17 01:51:00 +0000175#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200176#ifdef CONFIG_SYS_AUTOLOAD
177 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000178#endif
179#ifdef CONFIG_ROOTPATH
Joe Hershberger8b3637c2011-10-13 13:03:47 +0000180 "rootpath=" CONFIG_ROOTPATH "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000181#endif
182#ifdef CONFIG_GATEWAYIP
wdenk4a6fd342003-04-12 23:38:12 +0000183 "gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000184#endif
185#ifdef CONFIG_NETMASK
wdenk4a6fd342003-04-12 23:38:12 +0000186 "netmask=" MK_STR (CONFIG_NETMASK) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000187#endif
188#ifdef CONFIG_HOSTNAME
wdenk4a6fd342003-04-12 23:38:12 +0000189 "hostname=" MK_STR (CONFIG_HOSTNAME) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000190#endif
191#ifdef CONFIG_BOOTFILE
Joe Hershbergerb3f44c22011-10-13 13:03:48 +0000192 "bootfile=" CONFIG_BOOTFILE "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000193#endif
194#ifdef CONFIG_LOADADDR
wdenk4a6fd342003-04-12 23:38:12 +0000195 "loadaddr=" MK_STR (CONFIG_LOADADDR) "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000196#endif
197#ifdef CONFIG_PREBOOT
wdenk4a6fd342003-04-12 23:38:12 +0000198 "preboot=" CONFIG_PREBOOT "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000199#endif
200#ifdef CONFIG_CLOCKS_IN_MHZ
wdenk4a6fd342003-04-12 23:38:12 +0000201 "clocks_in_mhz=" "1" "\0"
wdenkd0fb80c2003-01-11 09:48:40 +0000202#endif
stroesead10dd92003-02-14 11:21:23 +0000203#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
wdenk4a6fd342003-04-12 23:38:12 +0000204 "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0"
stroesead10dd92003-02-14 11:21:23 +0000205#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000206#ifdef CONFIG_EXTRA_ENV_SETTINGS
207 CONFIG_EXTRA_ENV_SETTINGS
208#endif
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200209 "\0" /* Termimate struct environment data with 2 NULs */
wdenk6aff3112002-12-17 01:51:00 +0000210};
211
wdenk4a6fd342003-04-12 23:38:12 +0000212static int flash_io (int mode);
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100213static char *envmatch (char * s1, char * s2);
wdenk4a6fd342003-04-12 23:38:12 +0000214static int parse_config (void);
215
wdenkd0fb80c2003-01-11 09:48:40 +0000216#if defined(CONFIG_FILE)
wdenk4a6fd342003-04-12 23:38:12 +0000217static int get_config (char *);
wdenkd0fb80c2003-01-11 09:48:40 +0000218#endif
wdenk4a6fd342003-04-12 23:38:12 +0000219static inline ulong getenvsize (void)
wdenkd0fb80c2003-01-11 09:48:40 +0000220{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200221 ulong rc = CONFIG_ENV_SIZE - sizeof (long);
wdenk4a6fd342003-04-12 23:38:12 +0000222
wdenkd0fb80c2003-01-11 09:48:40 +0000223 if (HaveRedundEnv)
wdenk4a6fd342003-04-12 23:38:12 +0000224 rc -= sizeof (char);
wdenkd0fb80c2003-01-11 09:48:40 +0000225 return rc;
226}
wdenk6aff3112002-12-17 01:51:00 +0000227
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200228static char *fw_string_blank(char *s, int noblank)
229{
230 int i;
231 int len = strlen(s);
232
233 for (i = 0; i < len; i++, s++) {
234 if ((noblank && !WHITESPACE(*s)) ||
235 (!noblank && WHITESPACE(*s)))
236 break;
237 }
238 if (i == len)
239 return NULL;
240
241 return s;
242}
243
wdenk6aff3112002-12-17 01:51:00 +0000244/*
245 * Search the environment for a variable.
246 * Return the value, if found, or NULL, if not found.
247 */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100248char *fw_getenv (char *name)
wdenk6aff3112002-12-17 01:51:00 +0000249{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100250 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000251
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200252 if (fw_env_open())
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200253 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000254
wdenk4a6fd342003-04-12 23:38:12 +0000255 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100256 char *val;
wdenk6aff3112002-12-17 01:51:00 +0000257
wdenk4a6fd342003-04-12 23:38:12 +0000258 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000259 if (nxt >= &environment.data[ENV_SIZE]) {
260 fprintf (stderr, "## Error: "
261 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200262 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000263 }
264 }
wdenk4a6fd342003-04-12 23:38:12 +0000265 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000266 if (!val)
267 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200268 return val;
wdenk6aff3112002-12-17 01:51:00 +0000269 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200270 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000271}
272
273/*
274 * Print the current definition of one, or more, or all
275 * environment variables
276 */
Grant Ericksonbc117562008-05-06 20:16:15 -0700277int fw_printenv (int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000278{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100279 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000280 int i, n_flag;
Grant Ericksonbc117562008-05-06 20:16:15 -0700281 int rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000282
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200283 if (fw_env_open())
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200284 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000285
wdenk4a6fd342003-04-12 23:38:12 +0000286 if (argc == 1) { /* Print all env variables */
287 for (env = environment.data; *env; env = nxt + 1) {
288 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000289 if (nxt >= &environment.data[ENV_SIZE]) {
290 fprintf (stderr, "## Error: "
291 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200292 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000293 }
294 }
295
wdenk4a6fd342003-04-12 23:38:12 +0000296 printf ("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000297 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200298 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000299 }
300
wdenk4a6fd342003-04-12 23:38:12 +0000301 if (strcmp (argv[1], "-n") == 0) {
wdenk6aff3112002-12-17 01:51:00 +0000302 n_flag = 1;
303 ++argv;
304 --argc;
305 if (argc != 2) {
306 fprintf (stderr, "## Error: "
307 "`-n' option requires exactly one argument\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200308 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000309 }
310 } else {
311 n_flag = 0;
312 }
313
wdenk4a6fd342003-04-12 23:38:12 +0000314 for (i = 1; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100315 char *name = argv[i];
316 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000317
wdenk4a6fd342003-04-12 23:38:12 +0000318 for (env = environment.data; *env; env = nxt + 1) {
wdenk6aff3112002-12-17 01:51:00 +0000319
wdenk4a6fd342003-04-12 23:38:12 +0000320 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000321 if (nxt >= &environment.data[ENV_SIZE]) {
322 fprintf (stderr, "## Error: "
323 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200324 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000325 }
326 }
wdenk4a6fd342003-04-12 23:38:12 +0000327 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000328 if (val) {
329 if (!n_flag) {
330 fputs (name, stdout);
wdenk4a6fd342003-04-12 23:38:12 +0000331 putc ('=', stdout);
wdenk6aff3112002-12-17 01:51:00 +0000332 }
wdenk4a6fd342003-04-12 23:38:12 +0000333 puts (val);
wdenk6aff3112002-12-17 01:51:00 +0000334 break;
335 }
336 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700337 if (!val) {
wdenk4a6fd342003-04-12 23:38:12 +0000338 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700339 rc = -1;
340 }
wdenk6aff3112002-12-17 01:51:00 +0000341 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700342
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200343 return rc;
wdenk6aff3112002-12-17 01:51:00 +0000344}
345
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200346int fw_env_close(void)
wdenk6aff3112002-12-17 01:51:00 +0000347{
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200348 /*
349 * Update CRC
350 */
351 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000352
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200353 /* write environment back to flash */
354 if (flash_io(O_RDWR)) {
355 fprintf(stderr,
356 "Error: can't write fw_env to flash\n");
357 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000358 }
359
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200360 return 0;
361}
wdenk6aff3112002-12-17 01:51:00 +0000362
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200363
364/*
365 * Set/Clear a single variable in the environment.
366 * This is called in sequence to update the environment
367 * in RAM without updating the copy in flash after each set
368 */
369int fw_env_write(char *name, char *value)
370{
371 int len;
372 char *env, *nxt;
373 char *oldval = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000374
375 /*
376 * search if variable with this name already exists
377 */
wdenk4a6fd342003-04-12 23:38:12 +0000378 for (nxt = env = environment.data; *env; env = nxt + 1) {
379 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000380 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200381 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000382 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200383 errno = EINVAL;
384 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000385 }
386 }
wdenk4a6fd342003-04-12 23:38:12 +0000387 if ((oldval = envmatch (name, env)) != NULL)
wdenk6aff3112002-12-17 01:51:00 +0000388 break;
389 }
390
391 /*
392 * Delete any existing definition
393 */
394 if (oldval) {
Grant Ericksonaa701b92011-12-22 08:59:55 +0000395#ifndef CONFIG_ENV_OVERWRITE
wdenk6aff3112002-12-17 01:51:00 +0000396 /*
397 * Ethernet Address and serial# can be set only once
398 */
Grant Ericksonaa701b92011-12-22 08:59:55 +0000399 if (
400 (strcmp(name, "serial#") == 0) ||
401 ((strcmp(name, "ethaddr") == 0)
402#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
403 && (strcmp(oldval, MK_STR(CONFIG_ETHADDR)) != 0)
404#endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
405 ) ) {
wdenk6aff3112002-12-17 01:51:00 +0000406 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200407 errno = EROFS;
408 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000409 }
Grant Ericksonaa701b92011-12-22 08:59:55 +0000410#endif /* CONFIG_ENV_OVERWRITE */
wdenk6aff3112002-12-17 01:51:00 +0000411
412 if (*++nxt == '\0') {
413 *env = '\0';
414 } else {
415 for (;;) {
416 *env = *nxt++;
417 if ((*env == '\0') && (*nxt == '\0'))
418 break;
419 ++env;
420 }
421 }
422 *++env = '\0';
423 }
424
425 /* Delete only ? */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200426 if (!value || !strlen(value))
427 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000428
429 /*
430 * Append new definition at the end
431 */
wdenk4a6fd342003-04-12 23:38:12 +0000432 for (env = environment.data; *env || *(env + 1); ++env);
wdenk6aff3112002-12-17 01:51:00 +0000433 if (env > environment.data)
434 ++env;
435 /*
436 * Overflow when:
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200437 * "name" + "=" + "val" +"\0\0" > CONFIG_ENV_SIZE - (env-environment)
wdenk6aff3112002-12-17 01:51:00 +0000438 */
wdenk4a6fd342003-04-12 23:38:12 +0000439 len = strlen (name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000440 /* add '=' for first arg, ' ' for all others */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200441 len += strlen(value) + 1;
442
wdenk4a6fd342003-04-12 23:38:12 +0000443 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk6aff3112002-12-17 01:51:00 +0000444 fprintf (stderr,
445 "Error: environment overflow, \"%s\" deleted\n",
446 name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200447 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000448 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200449
wdenk6aff3112002-12-17 01:51:00 +0000450 while ((*env = *name++) != '\0')
451 env++;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200452 *env = '=';
453 while ((*++env = *value++) != '\0')
454 ;
wdenk6aff3112002-12-17 01:51:00 +0000455
456 /* end is marked with double '\0' */
457 *++env = '\0';
458
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200459 return 0;
460}
wdenk6aff3112002-12-17 01:51:00 +0000461
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200462/*
463 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
464 * 0 - OK
465 * EINVAL - need at least 1 argument
466 * EROFS - certain variables ("ethaddr", "serial#") cannot be
467 * modified or deleted
468 *
469 */
470int fw_setenv(int argc, char *argv[])
471{
472 int i, len;
473 char *name;
474 char *value = NULL;
475 char *tmpval = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000476
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200477 if (argc < 2) {
478 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200479 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000480 }
481
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200482 if (fw_env_open()) {
483 fprintf(stderr, "Error: environment not initialized\n");
484 return -1;
485 }
486
487 name = argv[1];
488
489 len = strlen(name) + 2;
490 for (i = 2; i < argc; ++i)
491 len += strlen(argv[i]) + 1;
492
493 /* Allocate enough place to the data string */
494 for (i = 2; i < argc; ++i) {
495 char *val = argv[i];
496 if (!value) {
497 value = (char *)malloc(len - strlen(name));
498 if (!value) {
499 fprintf(stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200500 "Cannot malloc %zu bytes: %s\n",
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200501 len - strlen(name), strerror(errno));
502 return -1;
503 }
504 memset(value, 0, len - strlen(name));
505 tmpval = value;
506 }
507 if (i != 2)
508 *tmpval++ = ' ';
509 while (*val != '\0')
510 *tmpval++ = *val++;
511 }
512
513 fw_env_write(name, value);
514
515 if (value)
516 free(value);
517
518 return fw_env_close();
519}
520
521/*
522 * Parse a file and configure the u-boot variables.
523 * The script file has a very simple format, as follows:
524 *
525 * Each line has a couple with name, value:
526 * <white spaces>variable_name<white spaces>variable_value
527 *
528 * Both variable_name and variable_value are interpreted as strings.
529 * Any character after <white spaces> and before ending \r\n is interpreted
530 * as variable's value (no comment allowed on these lines !)
531 *
532 * Comments are allowed if the first character in the line is #
533 *
534 * Returns -1 and sets errno error codes:
535 * 0 - OK
536 * -1 - Error
537 */
538int fw_parse_script(char *fname)
539{
540 FILE *fp;
541 char dump[1024]; /* Maximum line length in the file */
542 char *name;
543 char *val;
544 int lineno = 0;
545 int len;
546 int ret = 0;
547
548 if (fw_env_open()) {
549 fprintf(stderr, "Error: environment not initialized\n");
550 return -1;
551 }
552
553 if (strcmp(fname, "-") == 0)
554 fp = stdin;
555 else {
556 fp = fopen(fname, "r");
557 if (fp == NULL) {
558 fprintf(stderr, "I cannot open %s for reading\n",
559 fname);
560 return -1;
561 }
562 }
563
564 while (fgets(dump, sizeof(dump), fp)) {
565 lineno++;
566 len = strlen(dump);
567
568 /*
569 * Read a whole line from the file. If the line is too long
570 * or is not terminated, reports an error and exit.
571 */
572 if (dump[len - 1] != '\n') {
573 fprintf(stderr,
574 "Line %d not corrected terminated or too long\n",
575 lineno);
576 ret = -1;
577 break;
578 }
579
580 /* Drop ending line feed / carriage return */
581 while (len > 0 && (dump[len - 1] == '\n' ||
582 dump[len - 1] == '\r')) {
583 dump[len - 1] = '\0';
584 len--;
585 }
586
587 /* Skip comment or empty lines */
588 if ((len == 0) || dump[0] == '#')
589 continue;
590
591 /*
592 * Search for variable's name,
593 * remove leading whitespaces
594 */
595 name = fw_string_blank(dump, 1);
596 if (!name)
597 continue;
598
599 /* The first white space is the end of variable name */
600 val = fw_string_blank(name, 0);
601 len = strlen(name);
602 if (val) {
603 *val++ = '\0';
604 if ((val - name) < len)
605 val = fw_string_blank(val, 1);
606 else
607 val = NULL;
608 }
609
610#ifdef DEBUG
611 fprintf(stderr, "Setting %s : %s\n",
612 name, val ? val : " removed");
613#endif
614
615 /*
616 * If there is an error setting a variable,
617 * try to save the environment and returns an error
618 */
619 if (fw_env_write(name, val)) {
620 fprintf(stderr,
621 "fw_env_write returns with error : %s\n",
622 strerror(errno));
623 ret = -1;
624 break;
625 }
626
627 }
628
629 /* Close file if not stdin */
630 if (strcmp(fname, "-") != 0)
631 fclose(fp);
632
633 ret |= fw_env_close();
634
635 return ret;
636
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200637}
638
639/*
640 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
641 * 0 - block is good
642 * > 0 - block is bad
643 * < 0 - failed to test
644 */
645static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
646{
647 if (mtd_type == MTD_NANDFLASH) {
648 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
649
650 if (badblock < 0) {
651 perror ("Cannot read bad block mark");
652 return badblock;
653 }
654
655 if (badblock) {
656#ifdef DEBUG
657 fprintf (stderr, "Bad block at 0x%llx, "
658 "skipping\n", *blockstart);
659#endif
660 return badblock;
661 }
662 }
663
664 return 0;
665}
666
667/*
668 * Read data from flash at an offset into a provided buffer. On NAND it skips
669 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
670 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
671 */
672static int flash_read_buf (int dev, int fd, void *buf, size_t count,
673 off_t offset, uint8_t mtd_type)
674{
675 size_t blocklen; /* erase / write length - one block on NAND,
676 0 on NOR */
677 size_t processed = 0; /* progress counter */
678 size_t readlen = count; /* current read length */
679 off_t top_of_range; /* end of the last block we may use */
680 off_t block_seek; /* offset inside the current block to the start
681 of the data */
682 loff_t blockstart; /* running start of the current block -
683 MEMGETBADBLOCK needs 64 bits */
684 int rc;
685
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100686 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200687
688 /* Offset inside a block */
689 block_seek = offset - blockstart;
690
691 if (mtd_type == MTD_NANDFLASH) {
692 /*
693 * NAND: calculate which blocks we are reading. We have
694 * to read one block at a time to skip bad blocks.
695 */
696 blocklen = DEVESIZE (dev);
697
698 /*
699 * To calculate the top of the range, we have to use the
700 * global DEVOFFSET (dev), which can be different from offset
701 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100702 top_of_range = ((DEVOFFSET(dev) / blocklen) +
703 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200704
705 /* Limit to one block for the first read */
706 if (readlen > blocklen - block_seek)
707 readlen = blocklen - block_seek;
708 } else {
709 blocklen = 0;
710 top_of_range = offset + count;
711 }
712
713 /* This only runs once on NOR flash */
714 while (processed < count) {
715 rc = flash_bad_block (fd, mtd_type, &blockstart);
716 if (rc < 0) /* block test failed */
717 return -1;
718
719 if (blockstart + block_seek + readlen > top_of_range) {
720 /* End of range is reached */
721 fprintf (stderr,
722 "Too few good blocks within range\n");
723 return -1;
724 }
725
726 if (rc) { /* block is bad */
727 blockstart += blocklen;
728 continue;
729 }
730
731 /*
732 * If a block is bad, we retry in the next block at the same
733 * offset - see common/env_nand.c::writeenv()
734 */
735 lseek (fd, blockstart + block_seek, SEEK_SET);
736
737 rc = read (fd, buf + processed, readlen);
738 if (rc != readlen) {
739 fprintf (stderr, "Read error on %s: %s\n",
740 DEVNAME (dev), strerror (errno));
741 return -1;
742 }
743#ifdef DEBUG
744 fprintf (stderr, "Read 0x%x bytes at 0x%llx\n",
745 rc, blockstart + block_seek);
746#endif
747 processed += readlen;
748 readlen = min (blocklen, count - processed);
749 block_seek = 0;
750 blockstart += blocklen;
751 }
752
753 return processed;
754}
755
756/*
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100757 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
758 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
759 * erase and write the whole data at once.
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200760 */
761static int flash_write_buf (int dev, int fd, void *buf, size_t count,
762 off_t offset, uint8_t mtd_type)
763{
764 void *data;
765 struct erase_info_user erase;
766 size_t blocklen; /* length of NAND block / NOR erase sector */
767 size_t erase_len; /* whole area that can be erased - may include
768 bad blocks */
769 size_t erasesize; /* erase / write length - one block on NAND,
770 whole area on NOR */
771 size_t processed = 0; /* progress counter */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100772 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200773 bad blocks */
774 off_t erase_offset; /* offset to the first erase block (aligned)
775 below offset */
776 off_t block_seek; /* offset inside the erase block to the start
777 of the data */
778 off_t top_of_range; /* end of the last block we may use */
779 loff_t blockstart; /* running start of the current block -
780 MEMGETBADBLOCK needs 64 bits */
781 int rc;
782
783 blocklen = DEVESIZE (dev);
784
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100785 top_of_range = ((DEVOFFSET(dev) / blocklen) +
786 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200787
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100788 erase_offset = (offset / blocklen) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200789
790 /* Maximum area we may use */
791 erase_len = top_of_range - erase_offset;
792
793 blockstart = erase_offset;
794 /* Offset inside a block */
795 block_seek = offset - erase_offset;
796
797 /*
798 * Data size we actually have to write: from the start of the block
799 * to the start of the data, then count bytes of data, and to the
800 * end of the block
801 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100802 write_total = ((block_seek + count + blocklen - 1) /
803 blocklen) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200804
805 /*
806 * Support data anywhere within erase sectors: read out the complete
807 * area to be erased, replace the environment image, write the whole
808 * block back again.
809 */
810 if (write_total > count) {
811 data = malloc (erase_len);
812 if (!data) {
813 fprintf (stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200814 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200815 erase_len, strerror (errno));
816 return -1;
817 }
818
819 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
820 mtd_type);
821 if (write_total != rc)
822 return -1;
823
824 /* Overwrite the old environment */
825 memcpy (data + block_seek, buf, count);
826 } else {
827 /*
828 * We get here, iff offset is block-aligned and count is a
829 * multiple of blocklen - see write_total calculation above
830 */
831 data = buf;
832 }
833
834 if (mtd_type == MTD_NANDFLASH) {
835 /*
836 * NAND: calculate which blocks we are writing. We have
837 * to write one block at a time to skip bad blocks.
838 */
839 erasesize = blocklen;
840 } else {
841 erasesize = erase_len;
842 }
843
844 erase.length = erasesize;
845
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100846 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200847 while (processed < write_total) {
848 rc = flash_bad_block (fd, mtd_type, &blockstart);
849 if (rc < 0) /* block test failed */
850 return rc;
851
852 if (blockstart + erasesize > top_of_range) {
853 fprintf (stderr, "End of range reached, aborting\n");
854 return -1;
855 }
856
857 if (rc) { /* block is bad */
858 blockstart += blocklen;
859 continue;
860 }
861
862 erase.start = blockstart;
863 ioctl (fd, MEMUNLOCK, &erase);
864
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100865 /* Dataflash does not need an explicit erase cycle */
866 if (mtd_type != MTD_DATAFLASH)
867 if (ioctl (fd, MEMERASE, &erase) != 0) {
868 fprintf (stderr, "MTD erase error on %s: %s\n",
869 DEVNAME (dev),
870 strerror (errno));
871 return -1;
872 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200873
874 if (lseek (fd, blockstart, SEEK_SET) == -1) {
875 fprintf (stderr,
876 "Seek error on %s: %s\n",
877 DEVNAME (dev), strerror (errno));
878 return -1;
879 }
880
881#ifdef DEBUG
882 printf ("Write 0x%x bytes at 0x%llx\n", erasesize, blockstart);
883#endif
884 if (write (fd, data + processed, erasesize) != erasesize) {
885 fprintf (stderr, "Write error on %s: %s\n",
886 DEVNAME (dev), strerror (errno));
887 return -1;
888 }
889
890 ioctl (fd, MEMLOCK, &erase);
891
892 processed += blocklen;
893 block_seek = 0;
894 blockstart += blocklen;
895 }
896
897 if (write_total > count)
898 free (data);
899
900 return processed;
901}
902
903/*
904 * Set obsolete flag at offset - NOR flash only
905 */
906static int flash_flag_obsolete (int dev, int fd, off_t offset)
907{
908 int rc;
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200909 struct erase_info_user erase;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200910
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200911 erase.start = DEVOFFSET (dev);
912 erase.length = DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200913 /* This relies on the fact, that obsolete_flag == 0 */
914 rc = lseek (fd, offset, SEEK_SET);
915 if (rc < 0) {
916 fprintf (stderr, "Cannot seek to set the flag on %s \n",
917 DEVNAME (dev));
918 return rc;
919 }
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200920 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200921 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200922 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200923 if (rc < 0)
924 perror ("Could not set obsolete flag");
925
926 return rc;
927}
928
929static int flash_write (int fd_current, int fd_target, int dev_target)
930{
931 int rc;
932
933 switch (environment.flag_scheme) {
934 case FLAG_NONE:
935 break;
936 case FLAG_INCREMENTAL:
937 (*environment.flags)++;
938 break;
939 case FLAG_BOOLEAN:
940 *environment.flags = active_flag;
941 break;
942 default:
943 fprintf (stderr, "Unimplemented flash scheme %u \n",
944 environment.flag_scheme);
945 return -1;
946 }
947
948#ifdef DEBUG
949 printf ("Writing new environment at 0x%lx on %s\n",
950 DEVOFFSET (dev_target), DEVNAME (dev_target));
951#endif
952 rc = flash_write_buf (dev_target, fd_target, environment.image,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200953 CONFIG_ENV_SIZE, DEVOFFSET (dev_target),
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200954 DEVTYPE(dev_target));
955 if (rc < 0)
956 return rc;
957
958 if (environment.flag_scheme == FLAG_BOOLEAN) {
959 /* Have to set obsolete flag */
960 off_t offset = DEVOFFSET (dev_current) +
961 offsetof (struct env_image_redundant, flags);
962#ifdef DEBUG
963 printf ("Setting obsolete flag in environment at 0x%lx on %s\n",
964 DEVOFFSET (dev_current), DEVNAME (dev_current));
965#endif
966 flash_flag_obsolete (dev_current, fd_current, offset);
967 }
968
969 return 0;
970}
971
972static int flash_read (int fd)
973{
974 struct mtd_info_user mtdinfo;
975 int rc;
976
977 rc = ioctl (fd, MEMGETINFO, &mtdinfo);
978 if (rc < 0) {
979 perror ("Cannot get MTD information");
980 return -1;
981 }
982
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100983 if (mtdinfo.type != MTD_NORFLASH &&
984 mtdinfo.type != MTD_NANDFLASH &&
985 mtdinfo.type != MTD_DATAFLASH) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200986 fprintf (stderr, "Unsupported flash type %u\n", mtdinfo.type);
987 return -1;
988 }
989
990 DEVTYPE(dev_current) = mtdinfo.type;
991
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200992 rc = flash_read_buf (dev_current, fd, environment.image, CONFIG_ENV_SIZE,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200993 DEVOFFSET (dev_current), mtdinfo.type);
994
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200995 return (rc != CONFIG_ENV_SIZE) ? -1 : 0;
wdenk6aff3112002-12-17 01:51:00 +0000996}
997
998static int flash_io (int mode)
999{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001000 int fd_current, fd_target, rc, dev_target;
wdenk6aff3112002-12-17 01:51:00 +00001001
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001002 /* dev_current: fd_current, erase_current */
1003 fd_current = open (DEVNAME (dev_current), mode);
1004 if (fd_current < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001005 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001006 "Can't open %s: %s\n",
1007 DEVNAME (dev_current), strerror (errno));
1008 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001009 }
wdenk6aff3112002-12-17 01:51:00 +00001010
1011 if (mode == O_RDWR) {
wdenkd0fb80c2003-01-11 09:48:40 +00001012 if (HaveRedundEnv) {
1013 /* switch to next partition for writing */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001014 dev_target = !dev_current;
1015 /* dev_target: fd_target, erase_target */
1016 fd_target = open (DEVNAME (dev_target), mode);
1017 if (fd_target < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001018 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001019 "Can't open %s: %s\n",
1020 DEVNAME (dev_target),
1021 strerror (errno));
1022 rc = -1;
1023 goto exit;
wdenkd0fb80c2003-01-11 09:48:40 +00001024 }
1025 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001026 dev_target = dev_current;
1027 fd_target = fd_current;
wdenk6aff3112002-12-17 01:51:00 +00001028 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001029
1030 rc = flash_write (fd_current, fd_target, dev_target);
wdenk6aff3112002-12-17 01:51:00 +00001031
wdenkd0fb80c2003-01-11 09:48:40 +00001032 if (HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001033 if (close (fd_target)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001034 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001035 "I/O error on %s: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001036 DEVNAME (dev_target),
wdenk4a6fd342003-04-12 23:38:12 +00001037 strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001038 rc = -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001039 }
wdenk6aff3112002-12-17 01:51:00 +00001040 }
wdenk6aff3112002-12-17 01:51:00 +00001041 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001042 rc = flash_read (fd_current);
wdenk6aff3112002-12-17 01:51:00 +00001043 }
1044
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001045exit:
1046 if (close (fd_current)) {
wdenk6aff3112002-12-17 01:51:00 +00001047 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001048 "I/O error on %s: %s\n",
1049 DEVNAME (dev_current), strerror (errno));
1050 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001051 }
1052
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001053 return rc;
wdenk6aff3112002-12-17 01:51:00 +00001054}
1055
1056/*
1057 * s1 is either a simple 'name', or a 'name=value' pair.
1058 * s2 is a 'name=value' pair.
1059 * If the names match, return the value of s2, else NULL.
1060 */
1061
Markus Klotzbücher6de66b32007-11-27 10:23:20 +01001062static char *envmatch (char * s1, char * s2)
wdenk6aff3112002-12-17 01:51:00 +00001063{
1064
1065 while (*s1 == *s2++)
1066 if (*s1++ == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001067 return s2;
wdenk4a6fd342003-04-12 23:38:12 +00001068 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001069 return s2;
1070 return NULL;
wdenk6aff3112002-12-17 01:51:00 +00001071}
1072
1073/*
1074 * Prevent confusion if running from erased flash memory
1075 */
Stefano Babicbd7b26f2010-05-24 12:08:16 +02001076int fw_env_open(void)
wdenk6aff3112002-12-17 01:51:00 +00001077{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001078 int crc0, crc0_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001079 unsigned char flag0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001080 void *addr0;
wdenkd0fb80c2003-01-11 09:48:40 +00001081
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001082 int crc1, crc1_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001083 unsigned char flag1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001084 void *addr1;
1085
1086 struct env_image_single *single;
1087 struct env_image_redundant *redundant;
wdenk6aff3112002-12-17 01:51:00 +00001088
wdenk4a6fd342003-04-12 23:38:12 +00001089 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001090 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001091
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +02001092 addr0 = calloc (1, CONFIG_ENV_SIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001093 if (addr0 == NULL) {
wdenk4a6fd342003-04-12 23:38:12 +00001094 fprintf (stderr,
1095 "Not enough memory for environment (%ld bytes)\n",
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +02001096 CONFIG_ENV_SIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001097 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001098 }
wdenk4a6fd342003-04-12 23:38:12 +00001099
wdenkd0fb80c2003-01-11 09:48:40 +00001100 /* read environment from FLASH to local buffer */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001101 environment.image = addr0;
1102
1103 if (HaveRedundEnv) {
1104 redundant = addr0;
1105 environment.crc = &redundant->crc;
1106 environment.flags = &redundant->flags;
1107 environment.data = redundant->data;
1108 } else {
1109 single = addr0;
1110 environment.crc = &single->crc;
1111 environment.flags = NULL;
1112 environment.data = single->data;
wdenkd0fb80c2003-01-11 09:48:40 +00001113 }
wdenk4a6fd342003-04-12 23:38:12 +00001114
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001115 dev_current = 0;
1116 if (flash_io (O_RDONLY))
1117 return -1;
1118
1119 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1120 crc0_ok = (crc0 == *environment.crc);
wdenkd0fb80c2003-01-11 09:48:40 +00001121 if (!HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001122 if (!crc0_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001123 fprintf (stderr,
1124 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +01001125 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +00001126 }
wdenkd0fb80c2003-01-11 09:48:40 +00001127 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001128 flag0 = *environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +00001129
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001130 dev_current = 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +02001131 addr1 = calloc (1, CONFIG_ENV_SIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001132 if (addr1 == NULL) {
wdenk4a6fd342003-04-12 23:38:12 +00001133 fprintf (stderr,
1134 "Not enough memory for environment (%ld bytes)\n",
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +02001135 CONFIG_ENV_SIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001136 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001137 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001138 redundant = addr1;
wdenk4a6fd342003-04-12 23:38:12 +00001139
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001140 /*
1141 * have to set environment.image for flash_read(), careful -
1142 * other pointers in environment still point inside addr0
1143 */
1144 environment.image = addr1;
1145 if (flash_io (O_RDONLY))
1146 return -1;
1147
1148 /* Check flag scheme compatibility */
1149 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1150 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1151 environment.flag_scheme = FLAG_BOOLEAN;
1152 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1153 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1154 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001155 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1156 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1157 environment.flag_scheme = FLAG_BOOLEAN;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001158 } else {
1159 fprintf (stderr, "Incompatible flash types!\n");
1160 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001161 }
wdenk4a6fd342003-04-12 23:38:12 +00001162
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001163 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1164 crc1_ok = (crc1 == redundant->crc);
1165 flag1 = redundant->flags;
wdenk4a6fd342003-04-12 23:38:12 +00001166
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001167 if (crc0_ok && !crc1_ok) {
1168 dev_current = 0;
1169 } else if (!crc0_ok && crc1_ok) {
1170 dev_current = 1;
1171 } else if (!crc0_ok && !crc1_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001172 fprintf (stderr,
1173 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001174 memcpy (environment.data, default_environment,
1175 sizeof default_environment);
1176 dev_current = 0;
1177 } else {
1178 switch (environment.flag_scheme) {
1179 case FLAG_BOOLEAN:
1180 if (flag0 == active_flag &&
1181 flag1 == obsolete_flag) {
1182 dev_current = 0;
1183 } else if (flag0 == obsolete_flag &&
1184 flag1 == active_flag) {
1185 dev_current = 1;
1186 } else if (flag0 == flag1) {
1187 dev_current = 0;
1188 } else if (flag0 == 0xFF) {
1189 dev_current = 0;
1190 } else if (flag1 == 0xFF) {
1191 dev_current = 1;
1192 } else {
1193 dev_current = 0;
1194 }
1195 break;
1196 case FLAG_INCREMENTAL:
Jon Povey735eb0f2011-03-11 14:10:56 +09001197 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001198 dev_current = 1;
1199 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey735eb0f2011-03-11 14:10:56 +09001200 flag0 >= flag1)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001201 dev_current = 0;
Jon Povey735eb0f2011-03-11 14:10:56 +09001202 else /* flag1 > flag0 */
1203 dev_current = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001204 break;
1205 default:
1206 fprintf (stderr, "Unknown flag scheme %u \n",
1207 environment.flag_scheme);
1208 return -1;
1209 }
1210 }
1211
1212 /*
1213 * If we are reading, we don't need the flag and the CRC any
1214 * more, if we are writing, we will re-calculate CRC and update
1215 * flags before writing out
1216 */
1217 if (dev_current) {
1218 environment.image = addr1;
1219 environment.crc = &redundant->crc;
1220 environment.flags = &redundant->flags;
1221 environment.data = redundant->data;
1222 free (addr0);
1223 } else {
1224 environment.image = addr0;
1225 /* Other pointers are already set */
wdenk4a6fd342003-04-12 23:38:12 +00001226 free (addr1);
wdenk6aff3112002-12-17 01:51:00 +00001227 }
wdenk6aff3112002-12-17 01:51:00 +00001228 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001229 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001230}
1231
1232
wdenk4a6fd342003-04-12 23:38:12 +00001233static int parse_config ()
wdenk6aff3112002-12-17 01:51:00 +00001234{
1235 struct stat st;
1236
wdenkd0fb80c2003-01-11 09:48:40 +00001237#if defined(CONFIG_FILE)
1238 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
wdenk4a6fd342003-04-12 23:38:12 +00001239 if (get_config (CONFIG_FILE)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001240 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001241 "Cannot parse config file: %s\n", strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001242 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001243 }
wdenkd0fb80c2003-01-11 09:48:40 +00001244#else
wdenk4a6fd342003-04-12 23:38:12 +00001245 strcpy (DEVNAME (0), DEVICE1_NAME);
1246 DEVOFFSET (0) = DEVICE1_OFFSET;
1247 ENVSIZE (0) = ENV1_SIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001248 /* Default values are: erase-size=env-size, #sectors=1 */
1249 DEVESIZE (0) = ENVSIZE (0);
1250 ENVSECTORS (0) = 1;
1251#ifdef DEVICE1_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001252 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001253#endif
1254#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001255 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001256#endif
1257
wdenk6aff3112002-12-17 01:51:00 +00001258#ifdef HAVE_REDUND
wdenk4a6fd342003-04-12 23:38:12 +00001259 strcpy (DEVNAME (1), DEVICE2_NAME);
1260 DEVOFFSET (1) = DEVICE2_OFFSET;
1261 ENVSIZE (1) = ENV2_SIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001262 /* Default values are: erase-size=env-size, #sectors=1 */
1263 DEVESIZE (1) = ENVSIZE (1);
1264 ENVSECTORS (1) = 1;
1265#ifdef DEVICE2_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001266 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001267#endif
1268#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001269 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001270#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001271 HaveRedundEnv = 1;
wdenk6aff3112002-12-17 01:51:00 +00001272#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001273#endif
wdenk4a6fd342003-04-12 23:38:12 +00001274 if (stat (DEVNAME (0), &st)) {
1275 fprintf (stderr,
1276 "Cannot access MTD device %s: %s\n",
1277 DEVNAME (0), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001278 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001279 }
wdenk4a6fd342003-04-12 23:38:12 +00001280
1281 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1282 fprintf (stderr,
1283 "Cannot access MTD device %s: %s\n",
Wolfgang Denke2146b62005-09-25 01:20:38 +02001284 DEVNAME (1), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001285 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001286 }
wdenk6aff3112002-12-17 01:51:00 +00001287 return 0;
1288}
wdenkd0fb80c2003-01-11 09:48:40 +00001289
1290#if defined(CONFIG_FILE)
1291static int get_config (char *fname)
1292{
1293 FILE *fp;
1294 int i = 0;
1295 int rc;
1296 char dump[128];
1297
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001298 fp = fopen (fname, "r");
1299 if (fp == NULL)
1300 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001301
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001302 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001303 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001304 if (dump[0] == '#')
wdenkd0fb80c2003-01-11 09:48:40 +00001305 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001306
1307 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1308 DEVNAME (i),
1309 &DEVOFFSET (i),
1310 &ENVSIZE (i),
1311 &DEVESIZE (i),
1312 &ENVSECTORS (i));
1313
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001314 if (rc < 3)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001315 continue;
1316
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001317 if (rc < 4)
1318 /* Assume the erase size is the same as the env-size */
1319 DEVESIZE(i) = ENVSIZE(i);
1320
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001321 if (rc < 5)
1322 /* Default - 1 sector */
1323 ENVSECTORS (i) = 1;
wdenkd0fb80c2003-01-11 09:48:40 +00001324
1325 i++;
1326 }
wdenk4a6fd342003-04-12 23:38:12 +00001327 fclose (fp);
1328
wdenkd0fb80c2003-01-11 09:48:40 +00001329 HaveRedundEnv = i - 1;
wdenk4a6fd342003-04-12 23:38:12 +00001330 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +00001331 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001332 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001333 } else
1334 return 0;
1335}
1336#endif