blob: 1a2c22756eeed6c840cb6b7fbc2b3f253e99b7ce [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
Benoît Thébaudeau8ae51ae2012-08-10 07:45:15 +0000206#ifdef CONFIG_ENV_VARS_UBOOT_CONFIG
207 "arch=" CONFIG_SYS_ARCH "\0"
208 "cpu=" CONFIG_SYS_CPU "\0"
209 "board=" CONFIG_SYS_BOARD "\0"
210#ifdef CONFIG_SYS_VENDOR
211 "vendor=" CONFIG_SYS_VENDOR "\0"
212#endif
213#ifdef CONFIG_SYS_SOC
214 "soc=" CONFIG_SYS_SOC "\0"
215#endif
216#endif
wdenkd0fb80c2003-01-11 09:48:40 +0000217#ifdef CONFIG_EXTRA_ENV_SETTINGS
218 CONFIG_EXTRA_ENV_SETTINGS
219#endif
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200220 "\0" /* Termimate struct environment data with 2 NULs */
wdenk6aff3112002-12-17 01:51:00 +0000221};
222
wdenk4a6fd342003-04-12 23:38:12 +0000223static int flash_io (int mode);
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100224static char *envmatch (char * s1, char * s2);
wdenk4a6fd342003-04-12 23:38:12 +0000225static int parse_config (void);
226
wdenkd0fb80c2003-01-11 09:48:40 +0000227#if defined(CONFIG_FILE)
wdenk4a6fd342003-04-12 23:38:12 +0000228static int get_config (char *);
wdenkd0fb80c2003-01-11 09:48:40 +0000229#endif
wdenk4a6fd342003-04-12 23:38:12 +0000230static inline ulong getenvsize (void)
wdenkd0fb80c2003-01-11 09:48:40 +0000231{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200232 ulong rc = CONFIG_ENV_SIZE - sizeof (long);
wdenk4a6fd342003-04-12 23:38:12 +0000233
wdenkd0fb80c2003-01-11 09:48:40 +0000234 if (HaveRedundEnv)
wdenk4a6fd342003-04-12 23:38:12 +0000235 rc -= sizeof (char);
wdenkd0fb80c2003-01-11 09:48:40 +0000236 return rc;
237}
wdenk6aff3112002-12-17 01:51:00 +0000238
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200239static char *fw_string_blank(char *s, int noblank)
240{
241 int i;
242 int len = strlen(s);
243
244 for (i = 0; i < len; i++, s++) {
245 if ((noblank && !WHITESPACE(*s)) ||
246 (!noblank && WHITESPACE(*s)))
247 break;
248 }
249 if (i == len)
250 return NULL;
251
252 return s;
253}
254
wdenk6aff3112002-12-17 01:51:00 +0000255/*
256 * Search the environment for a variable.
257 * Return the value, if found, or NULL, if not found.
258 */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100259char *fw_getenv (char *name)
wdenk6aff3112002-12-17 01:51:00 +0000260{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100261 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000262
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200263 if (fw_env_open())
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200264 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000265
wdenk4a6fd342003-04-12 23:38:12 +0000266 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100267 char *val;
wdenk6aff3112002-12-17 01:51:00 +0000268
wdenk4a6fd342003-04-12 23:38:12 +0000269 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000270 if (nxt >= &environment.data[ENV_SIZE]) {
271 fprintf (stderr, "## Error: "
272 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200273 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000274 }
275 }
wdenk4a6fd342003-04-12 23:38:12 +0000276 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000277 if (!val)
278 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200279 return val;
wdenk6aff3112002-12-17 01:51:00 +0000280 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200281 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000282}
283
284/*
285 * Print the current definition of one, or more, or all
286 * environment variables
287 */
Grant Ericksonbc117562008-05-06 20:16:15 -0700288int fw_printenv (int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000289{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100290 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000291 int i, n_flag;
Grant Ericksonbc117562008-05-06 20:16:15 -0700292 int rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000293
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200294 if (fw_env_open())
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200295 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000296
wdenk4a6fd342003-04-12 23:38:12 +0000297 if (argc == 1) { /* Print all env variables */
298 for (env = environment.data; *env; env = nxt + 1) {
299 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000300 if (nxt >= &environment.data[ENV_SIZE]) {
301 fprintf (stderr, "## Error: "
302 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200303 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000304 }
305 }
306
wdenk4a6fd342003-04-12 23:38:12 +0000307 printf ("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000308 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200309 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000310 }
311
wdenk4a6fd342003-04-12 23:38:12 +0000312 if (strcmp (argv[1], "-n") == 0) {
wdenk6aff3112002-12-17 01:51:00 +0000313 n_flag = 1;
314 ++argv;
315 --argc;
316 if (argc != 2) {
317 fprintf (stderr, "## Error: "
318 "`-n' option requires exactly one argument\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200319 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000320 }
321 } else {
322 n_flag = 0;
323 }
324
wdenk4a6fd342003-04-12 23:38:12 +0000325 for (i = 1; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100326 char *name = argv[i];
327 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000328
wdenk4a6fd342003-04-12 23:38:12 +0000329 for (env = environment.data; *env; env = nxt + 1) {
wdenk6aff3112002-12-17 01:51:00 +0000330
wdenk4a6fd342003-04-12 23:38:12 +0000331 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000332 if (nxt >= &environment.data[ENV_SIZE]) {
333 fprintf (stderr, "## Error: "
334 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200335 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000336 }
337 }
wdenk4a6fd342003-04-12 23:38:12 +0000338 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000339 if (val) {
340 if (!n_flag) {
341 fputs (name, stdout);
wdenk4a6fd342003-04-12 23:38:12 +0000342 putc ('=', stdout);
wdenk6aff3112002-12-17 01:51:00 +0000343 }
wdenk4a6fd342003-04-12 23:38:12 +0000344 puts (val);
wdenk6aff3112002-12-17 01:51:00 +0000345 break;
346 }
347 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700348 if (!val) {
wdenk4a6fd342003-04-12 23:38:12 +0000349 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700350 rc = -1;
351 }
wdenk6aff3112002-12-17 01:51:00 +0000352 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700353
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200354 return rc;
wdenk6aff3112002-12-17 01:51:00 +0000355}
356
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200357int fw_env_close(void)
wdenk6aff3112002-12-17 01:51:00 +0000358{
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200359 /*
360 * Update CRC
361 */
362 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000363
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200364 /* write environment back to flash */
365 if (flash_io(O_RDWR)) {
366 fprintf(stderr,
367 "Error: can't write fw_env to flash\n");
368 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000369 }
370
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200371 return 0;
372}
wdenk6aff3112002-12-17 01:51:00 +0000373
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200374
375/*
376 * Set/Clear a single variable in the environment.
377 * This is called in sequence to update the environment
378 * in RAM without updating the copy in flash after each set
379 */
380int fw_env_write(char *name, char *value)
381{
382 int len;
383 char *env, *nxt;
384 char *oldval = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000385
386 /*
387 * search if variable with this name already exists
388 */
wdenk4a6fd342003-04-12 23:38:12 +0000389 for (nxt = env = environment.data; *env; env = nxt + 1) {
390 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000391 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200392 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000393 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200394 errno = EINVAL;
395 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000396 }
397 }
wdenk4a6fd342003-04-12 23:38:12 +0000398 if ((oldval = envmatch (name, env)) != NULL)
wdenk6aff3112002-12-17 01:51:00 +0000399 break;
400 }
401
402 /*
403 * Delete any existing definition
404 */
405 if (oldval) {
Grant Ericksonaa701b92011-12-22 08:59:55 +0000406#ifndef CONFIG_ENV_OVERWRITE
wdenk6aff3112002-12-17 01:51:00 +0000407 /*
408 * Ethernet Address and serial# can be set only once
409 */
Grant Ericksonaa701b92011-12-22 08:59:55 +0000410 if (
411 (strcmp(name, "serial#") == 0) ||
412 ((strcmp(name, "ethaddr") == 0)
413#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
414 && (strcmp(oldval, MK_STR(CONFIG_ETHADDR)) != 0)
415#endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
416 ) ) {
wdenk6aff3112002-12-17 01:51:00 +0000417 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200418 errno = EROFS;
419 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000420 }
Grant Ericksonaa701b92011-12-22 08:59:55 +0000421#endif /* CONFIG_ENV_OVERWRITE */
wdenk6aff3112002-12-17 01:51:00 +0000422
423 if (*++nxt == '\0') {
424 *env = '\0';
425 } else {
426 for (;;) {
427 *env = *nxt++;
428 if ((*env == '\0') && (*nxt == '\0'))
429 break;
430 ++env;
431 }
432 }
433 *++env = '\0';
434 }
435
436 /* Delete only ? */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200437 if (!value || !strlen(value))
438 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000439
440 /*
441 * Append new definition at the end
442 */
wdenk4a6fd342003-04-12 23:38:12 +0000443 for (env = environment.data; *env || *(env + 1); ++env);
wdenk6aff3112002-12-17 01:51:00 +0000444 if (env > environment.data)
445 ++env;
446 /*
447 * Overflow when:
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200448 * "name" + "=" + "val" +"\0\0" > CONFIG_ENV_SIZE - (env-environment)
wdenk6aff3112002-12-17 01:51:00 +0000449 */
wdenk4a6fd342003-04-12 23:38:12 +0000450 len = strlen (name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000451 /* add '=' for first arg, ' ' for all others */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200452 len += strlen(value) + 1;
453
wdenk4a6fd342003-04-12 23:38:12 +0000454 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk6aff3112002-12-17 01:51:00 +0000455 fprintf (stderr,
456 "Error: environment overflow, \"%s\" deleted\n",
457 name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200458 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000459 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200460
wdenk6aff3112002-12-17 01:51:00 +0000461 while ((*env = *name++) != '\0')
462 env++;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200463 *env = '=';
464 while ((*++env = *value++) != '\0')
465 ;
wdenk6aff3112002-12-17 01:51:00 +0000466
467 /* end is marked with double '\0' */
468 *++env = '\0';
469
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200470 return 0;
471}
wdenk6aff3112002-12-17 01:51:00 +0000472
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200473/*
474 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
475 * 0 - OK
476 * EINVAL - need at least 1 argument
477 * EROFS - certain variables ("ethaddr", "serial#") cannot be
478 * modified or deleted
479 *
480 */
481int fw_setenv(int argc, char *argv[])
482{
483 int i, len;
484 char *name;
485 char *value = NULL;
486 char *tmpval = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000487
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200488 if (argc < 2) {
489 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200490 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000491 }
492
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200493 if (fw_env_open()) {
494 fprintf(stderr, "Error: environment not initialized\n");
495 return -1;
496 }
497
498 name = argv[1];
499
500 len = strlen(name) + 2;
501 for (i = 2; i < argc; ++i)
502 len += strlen(argv[i]) + 1;
503
504 /* Allocate enough place to the data string */
505 for (i = 2; i < argc; ++i) {
506 char *val = argv[i];
507 if (!value) {
508 value = (char *)malloc(len - strlen(name));
509 if (!value) {
510 fprintf(stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200511 "Cannot malloc %zu bytes: %s\n",
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200512 len - strlen(name), strerror(errno));
513 return -1;
514 }
515 memset(value, 0, len - strlen(name));
516 tmpval = value;
517 }
518 if (i != 2)
519 *tmpval++ = ' ';
520 while (*val != '\0')
521 *tmpval++ = *val++;
522 }
523
524 fw_env_write(name, value);
525
526 if (value)
527 free(value);
528
529 return fw_env_close();
530}
531
532/*
533 * Parse a file and configure the u-boot variables.
534 * The script file has a very simple format, as follows:
535 *
536 * Each line has a couple with name, value:
537 * <white spaces>variable_name<white spaces>variable_value
538 *
539 * Both variable_name and variable_value are interpreted as strings.
540 * Any character after <white spaces> and before ending \r\n is interpreted
541 * as variable's value (no comment allowed on these lines !)
542 *
543 * Comments are allowed if the first character in the line is #
544 *
545 * Returns -1 and sets errno error codes:
546 * 0 - OK
547 * -1 - Error
548 */
549int fw_parse_script(char *fname)
550{
551 FILE *fp;
552 char dump[1024]; /* Maximum line length in the file */
553 char *name;
554 char *val;
555 int lineno = 0;
556 int len;
557 int ret = 0;
558
559 if (fw_env_open()) {
560 fprintf(stderr, "Error: environment not initialized\n");
561 return -1;
562 }
563
564 if (strcmp(fname, "-") == 0)
565 fp = stdin;
566 else {
567 fp = fopen(fname, "r");
568 if (fp == NULL) {
569 fprintf(stderr, "I cannot open %s for reading\n",
570 fname);
571 return -1;
572 }
573 }
574
575 while (fgets(dump, sizeof(dump), fp)) {
576 lineno++;
577 len = strlen(dump);
578
579 /*
580 * Read a whole line from the file. If the line is too long
581 * or is not terminated, reports an error and exit.
582 */
583 if (dump[len - 1] != '\n') {
584 fprintf(stderr,
585 "Line %d not corrected terminated or too long\n",
586 lineno);
587 ret = -1;
588 break;
589 }
590
591 /* Drop ending line feed / carriage return */
592 while (len > 0 && (dump[len - 1] == '\n' ||
593 dump[len - 1] == '\r')) {
594 dump[len - 1] = '\0';
595 len--;
596 }
597
598 /* Skip comment or empty lines */
599 if ((len == 0) || dump[0] == '#')
600 continue;
601
602 /*
603 * Search for variable's name,
604 * remove leading whitespaces
605 */
606 name = fw_string_blank(dump, 1);
607 if (!name)
608 continue;
609
610 /* The first white space is the end of variable name */
611 val = fw_string_blank(name, 0);
612 len = strlen(name);
613 if (val) {
614 *val++ = '\0';
615 if ((val - name) < len)
616 val = fw_string_blank(val, 1);
617 else
618 val = NULL;
619 }
620
621#ifdef DEBUG
622 fprintf(stderr, "Setting %s : %s\n",
623 name, val ? val : " removed");
624#endif
625
626 /*
627 * If there is an error setting a variable,
628 * try to save the environment and returns an error
629 */
630 if (fw_env_write(name, val)) {
631 fprintf(stderr,
632 "fw_env_write returns with error : %s\n",
633 strerror(errno));
634 ret = -1;
635 break;
636 }
637
638 }
639
640 /* Close file if not stdin */
641 if (strcmp(fname, "-") != 0)
642 fclose(fp);
643
644 ret |= fw_env_close();
645
646 return ret;
647
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200648}
649
650/*
651 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
652 * 0 - block is good
653 * > 0 - block is bad
654 * < 0 - failed to test
655 */
656static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
657{
658 if (mtd_type == MTD_NANDFLASH) {
659 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
660
661 if (badblock < 0) {
662 perror ("Cannot read bad block mark");
663 return badblock;
664 }
665
666 if (badblock) {
667#ifdef DEBUG
668 fprintf (stderr, "Bad block at 0x%llx, "
669 "skipping\n", *blockstart);
670#endif
671 return badblock;
672 }
673 }
674
675 return 0;
676}
677
678/*
679 * Read data from flash at an offset into a provided buffer. On NAND it skips
680 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
681 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
682 */
683static int flash_read_buf (int dev, int fd, void *buf, size_t count,
684 off_t offset, uint8_t mtd_type)
685{
686 size_t blocklen; /* erase / write length - one block on NAND,
687 0 on NOR */
688 size_t processed = 0; /* progress counter */
689 size_t readlen = count; /* current read length */
690 off_t top_of_range; /* end of the last block we may use */
691 off_t block_seek; /* offset inside the current block to the start
692 of the data */
693 loff_t blockstart; /* running start of the current block -
694 MEMGETBADBLOCK needs 64 bits */
695 int rc;
696
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100697 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200698
699 /* Offset inside a block */
700 block_seek = offset - blockstart;
701
702 if (mtd_type == MTD_NANDFLASH) {
703 /*
704 * NAND: calculate which blocks we are reading. We have
705 * to read one block at a time to skip bad blocks.
706 */
707 blocklen = DEVESIZE (dev);
708
709 /*
710 * To calculate the top of the range, we have to use the
711 * global DEVOFFSET (dev), which can be different from offset
712 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100713 top_of_range = ((DEVOFFSET(dev) / blocklen) +
714 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200715
716 /* Limit to one block for the first read */
717 if (readlen > blocklen - block_seek)
718 readlen = blocklen - block_seek;
719 } else {
720 blocklen = 0;
721 top_of_range = offset + count;
722 }
723
724 /* This only runs once on NOR flash */
725 while (processed < count) {
726 rc = flash_bad_block (fd, mtd_type, &blockstart);
727 if (rc < 0) /* block test failed */
728 return -1;
729
730 if (blockstart + block_seek + readlen > top_of_range) {
731 /* End of range is reached */
732 fprintf (stderr,
733 "Too few good blocks within range\n");
734 return -1;
735 }
736
737 if (rc) { /* block is bad */
738 blockstart += blocklen;
739 continue;
740 }
741
742 /*
743 * If a block is bad, we retry in the next block at the same
744 * offset - see common/env_nand.c::writeenv()
745 */
746 lseek (fd, blockstart + block_seek, SEEK_SET);
747
748 rc = read (fd, buf + processed, readlen);
749 if (rc != readlen) {
750 fprintf (stderr, "Read error on %s: %s\n",
751 DEVNAME (dev), strerror (errno));
752 return -1;
753 }
754#ifdef DEBUG
755 fprintf (stderr, "Read 0x%x bytes at 0x%llx\n",
756 rc, blockstart + block_seek);
757#endif
758 processed += readlen;
759 readlen = min (blocklen, count - processed);
760 block_seek = 0;
761 blockstart += blocklen;
762 }
763
764 return processed;
765}
766
767/*
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100768 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
769 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
770 * erase and write the whole data at once.
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200771 */
772static int flash_write_buf (int dev, int fd, void *buf, size_t count,
773 off_t offset, uint8_t mtd_type)
774{
775 void *data;
776 struct erase_info_user erase;
777 size_t blocklen; /* length of NAND block / NOR erase sector */
778 size_t erase_len; /* whole area that can be erased - may include
779 bad blocks */
780 size_t erasesize; /* erase / write length - one block on NAND,
781 whole area on NOR */
782 size_t processed = 0; /* progress counter */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100783 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200784 bad blocks */
785 off_t erase_offset; /* offset to the first erase block (aligned)
786 below offset */
787 off_t block_seek; /* offset inside the erase block to the start
788 of the data */
789 off_t top_of_range; /* end of the last block we may use */
790 loff_t blockstart; /* running start of the current block -
791 MEMGETBADBLOCK needs 64 bits */
792 int rc;
793
794 blocklen = DEVESIZE (dev);
795
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100796 top_of_range = ((DEVOFFSET(dev) / blocklen) +
797 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200798
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100799 erase_offset = (offset / blocklen) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200800
801 /* Maximum area we may use */
802 erase_len = top_of_range - erase_offset;
803
804 blockstart = erase_offset;
805 /* Offset inside a block */
806 block_seek = offset - erase_offset;
807
808 /*
809 * Data size we actually have to write: from the start of the block
810 * to the start of the data, then count bytes of data, and to the
811 * end of the block
812 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100813 write_total = ((block_seek + count + blocklen - 1) /
814 blocklen) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200815
816 /*
817 * Support data anywhere within erase sectors: read out the complete
818 * area to be erased, replace the environment image, write the whole
819 * block back again.
820 */
821 if (write_total > count) {
822 data = malloc (erase_len);
823 if (!data) {
824 fprintf (stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200825 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200826 erase_len, strerror (errno));
827 return -1;
828 }
829
830 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
831 mtd_type);
832 if (write_total != rc)
833 return -1;
834
835 /* Overwrite the old environment */
836 memcpy (data + block_seek, buf, count);
837 } else {
838 /*
839 * We get here, iff offset is block-aligned and count is a
840 * multiple of blocklen - see write_total calculation above
841 */
842 data = buf;
843 }
844
845 if (mtd_type == MTD_NANDFLASH) {
846 /*
847 * NAND: calculate which blocks we are writing. We have
848 * to write one block at a time to skip bad blocks.
849 */
850 erasesize = blocklen;
851 } else {
852 erasesize = erase_len;
853 }
854
855 erase.length = erasesize;
856
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100857 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200858 while (processed < write_total) {
859 rc = flash_bad_block (fd, mtd_type, &blockstart);
860 if (rc < 0) /* block test failed */
861 return rc;
862
863 if (blockstart + erasesize > top_of_range) {
864 fprintf (stderr, "End of range reached, aborting\n");
865 return -1;
866 }
867
868 if (rc) { /* block is bad */
869 blockstart += blocklen;
870 continue;
871 }
872
873 erase.start = blockstart;
874 ioctl (fd, MEMUNLOCK, &erase);
875
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100876 /* Dataflash does not need an explicit erase cycle */
877 if (mtd_type != MTD_DATAFLASH)
878 if (ioctl (fd, MEMERASE, &erase) != 0) {
879 fprintf (stderr, "MTD erase error on %s: %s\n",
880 DEVNAME (dev),
881 strerror (errno));
882 return -1;
883 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200884
885 if (lseek (fd, blockstart, SEEK_SET) == -1) {
886 fprintf (stderr,
887 "Seek error on %s: %s\n",
888 DEVNAME (dev), strerror (errno));
889 return -1;
890 }
891
892#ifdef DEBUG
893 printf ("Write 0x%x bytes at 0x%llx\n", erasesize, blockstart);
894#endif
895 if (write (fd, data + processed, erasesize) != erasesize) {
896 fprintf (stderr, "Write error on %s: %s\n",
897 DEVNAME (dev), strerror (errno));
898 return -1;
899 }
900
901 ioctl (fd, MEMLOCK, &erase);
902
903 processed += blocklen;
904 block_seek = 0;
905 blockstart += blocklen;
906 }
907
908 if (write_total > count)
909 free (data);
910
911 return processed;
912}
913
914/*
915 * Set obsolete flag at offset - NOR flash only
916 */
917static int flash_flag_obsolete (int dev, int fd, off_t offset)
918{
919 int rc;
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200920 struct erase_info_user erase;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200921
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200922 erase.start = DEVOFFSET (dev);
923 erase.length = DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200924 /* This relies on the fact, that obsolete_flag == 0 */
925 rc = lseek (fd, offset, SEEK_SET);
926 if (rc < 0) {
927 fprintf (stderr, "Cannot seek to set the flag on %s \n",
928 DEVNAME (dev));
929 return rc;
930 }
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200931 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200932 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200933 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200934 if (rc < 0)
935 perror ("Could not set obsolete flag");
936
937 return rc;
938}
939
940static int flash_write (int fd_current, int fd_target, int dev_target)
941{
942 int rc;
943
944 switch (environment.flag_scheme) {
945 case FLAG_NONE:
946 break;
947 case FLAG_INCREMENTAL:
948 (*environment.flags)++;
949 break;
950 case FLAG_BOOLEAN:
951 *environment.flags = active_flag;
952 break;
953 default:
954 fprintf (stderr, "Unimplemented flash scheme %u \n",
955 environment.flag_scheme);
956 return -1;
957 }
958
959#ifdef DEBUG
960 printf ("Writing new environment at 0x%lx on %s\n",
961 DEVOFFSET (dev_target), DEVNAME (dev_target));
962#endif
963 rc = flash_write_buf (dev_target, fd_target, environment.image,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200964 CONFIG_ENV_SIZE, DEVOFFSET (dev_target),
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200965 DEVTYPE(dev_target));
966 if (rc < 0)
967 return rc;
968
969 if (environment.flag_scheme == FLAG_BOOLEAN) {
970 /* Have to set obsolete flag */
971 off_t offset = DEVOFFSET (dev_current) +
972 offsetof (struct env_image_redundant, flags);
973#ifdef DEBUG
974 printf ("Setting obsolete flag in environment at 0x%lx on %s\n",
975 DEVOFFSET (dev_current), DEVNAME (dev_current));
976#endif
977 flash_flag_obsolete (dev_current, fd_current, offset);
978 }
979
980 return 0;
981}
982
983static int flash_read (int fd)
984{
985 struct mtd_info_user mtdinfo;
986 int rc;
987
988 rc = ioctl (fd, MEMGETINFO, &mtdinfo);
989 if (rc < 0) {
990 perror ("Cannot get MTD information");
991 return -1;
992 }
993
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100994 if (mtdinfo.type != MTD_NORFLASH &&
995 mtdinfo.type != MTD_NANDFLASH &&
996 mtdinfo.type != MTD_DATAFLASH) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200997 fprintf (stderr, "Unsupported flash type %u\n", mtdinfo.type);
998 return -1;
999 }
1000
1001 DEVTYPE(dev_current) = mtdinfo.type;
1002
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +02001003 rc = flash_read_buf (dev_current, fd, environment.image, CONFIG_ENV_SIZE,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001004 DEVOFFSET (dev_current), mtdinfo.type);
1005
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +02001006 return (rc != CONFIG_ENV_SIZE) ? -1 : 0;
wdenk6aff3112002-12-17 01:51:00 +00001007}
1008
1009static int flash_io (int mode)
1010{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001011 int fd_current, fd_target, rc, dev_target;
wdenk6aff3112002-12-17 01:51:00 +00001012
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001013 /* dev_current: fd_current, erase_current */
1014 fd_current = open (DEVNAME (dev_current), mode);
1015 if (fd_current < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001016 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001017 "Can't open %s: %s\n",
1018 DEVNAME (dev_current), strerror (errno));
1019 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001020 }
wdenk6aff3112002-12-17 01:51:00 +00001021
1022 if (mode == O_RDWR) {
wdenkd0fb80c2003-01-11 09:48:40 +00001023 if (HaveRedundEnv) {
1024 /* switch to next partition for writing */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001025 dev_target = !dev_current;
1026 /* dev_target: fd_target, erase_target */
1027 fd_target = open (DEVNAME (dev_target), mode);
1028 if (fd_target < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001029 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001030 "Can't open %s: %s\n",
1031 DEVNAME (dev_target),
1032 strerror (errno));
1033 rc = -1;
1034 goto exit;
wdenkd0fb80c2003-01-11 09:48:40 +00001035 }
1036 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001037 dev_target = dev_current;
1038 fd_target = fd_current;
wdenk6aff3112002-12-17 01:51:00 +00001039 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001040
1041 rc = flash_write (fd_current, fd_target, dev_target);
wdenk6aff3112002-12-17 01:51:00 +00001042
wdenkd0fb80c2003-01-11 09:48:40 +00001043 if (HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001044 if (close (fd_target)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001045 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001046 "I/O error on %s: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001047 DEVNAME (dev_target),
wdenk4a6fd342003-04-12 23:38:12 +00001048 strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001049 rc = -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001050 }
wdenk6aff3112002-12-17 01:51:00 +00001051 }
wdenk6aff3112002-12-17 01:51:00 +00001052 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001053 rc = flash_read (fd_current);
wdenk6aff3112002-12-17 01:51:00 +00001054 }
1055
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001056exit:
1057 if (close (fd_current)) {
wdenk6aff3112002-12-17 01:51:00 +00001058 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001059 "I/O error on %s: %s\n",
1060 DEVNAME (dev_current), strerror (errno));
1061 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001062 }
1063
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001064 return rc;
wdenk6aff3112002-12-17 01:51:00 +00001065}
1066
1067/*
1068 * s1 is either a simple 'name', or a 'name=value' pair.
1069 * s2 is a 'name=value' pair.
1070 * If the names match, return the value of s2, else NULL.
1071 */
1072
Markus Klotzbücher6de66b32007-11-27 10:23:20 +01001073static char *envmatch (char * s1, char * s2)
wdenk6aff3112002-12-17 01:51:00 +00001074{
1075
1076 while (*s1 == *s2++)
1077 if (*s1++ == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001078 return s2;
wdenk4a6fd342003-04-12 23:38:12 +00001079 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001080 return s2;
1081 return NULL;
wdenk6aff3112002-12-17 01:51:00 +00001082}
1083
1084/*
1085 * Prevent confusion if running from erased flash memory
1086 */
Stefano Babicbd7b26f2010-05-24 12:08:16 +02001087int fw_env_open(void)
wdenk6aff3112002-12-17 01:51:00 +00001088{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001089 int crc0, crc0_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001090 unsigned char flag0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001091 void *addr0;
wdenkd0fb80c2003-01-11 09:48:40 +00001092
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001093 int crc1, crc1_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001094 unsigned char flag1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001095 void *addr1;
1096
1097 struct env_image_single *single;
1098 struct env_image_redundant *redundant;
wdenk6aff3112002-12-17 01:51:00 +00001099
wdenk4a6fd342003-04-12 23:38:12 +00001100 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001101 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001102
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +02001103 addr0 = calloc (1, CONFIG_ENV_SIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001104 if (addr0 == NULL) {
wdenk4a6fd342003-04-12 23:38:12 +00001105 fprintf (stderr,
1106 "Not enough memory for environment (%ld bytes)\n",
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +02001107 CONFIG_ENV_SIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001108 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001109 }
wdenk4a6fd342003-04-12 23:38:12 +00001110
wdenkd0fb80c2003-01-11 09:48:40 +00001111 /* read environment from FLASH to local buffer */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001112 environment.image = addr0;
1113
1114 if (HaveRedundEnv) {
1115 redundant = addr0;
1116 environment.crc = &redundant->crc;
1117 environment.flags = &redundant->flags;
1118 environment.data = redundant->data;
1119 } else {
1120 single = addr0;
1121 environment.crc = &single->crc;
1122 environment.flags = NULL;
1123 environment.data = single->data;
wdenkd0fb80c2003-01-11 09:48:40 +00001124 }
wdenk4a6fd342003-04-12 23:38:12 +00001125
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001126 dev_current = 0;
1127 if (flash_io (O_RDONLY))
1128 return -1;
1129
1130 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1131 crc0_ok = (crc0 == *environment.crc);
wdenkd0fb80c2003-01-11 09:48:40 +00001132 if (!HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001133 if (!crc0_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001134 fprintf (stderr,
1135 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +01001136 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +00001137 }
wdenkd0fb80c2003-01-11 09:48:40 +00001138 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001139 flag0 = *environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +00001140
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001141 dev_current = 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +02001142 addr1 = calloc (1, CONFIG_ENV_SIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001143 if (addr1 == NULL) {
wdenk4a6fd342003-04-12 23:38:12 +00001144 fprintf (stderr,
1145 "Not enough memory for environment (%ld bytes)\n",
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +02001146 CONFIG_ENV_SIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001147 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001148 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001149 redundant = addr1;
wdenk4a6fd342003-04-12 23:38:12 +00001150
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001151 /*
1152 * have to set environment.image for flash_read(), careful -
1153 * other pointers in environment still point inside addr0
1154 */
1155 environment.image = addr1;
1156 if (flash_io (O_RDONLY))
1157 return -1;
1158
1159 /* Check flag scheme compatibility */
1160 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1161 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1162 environment.flag_scheme = FLAG_BOOLEAN;
1163 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1164 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1165 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001166 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1167 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1168 environment.flag_scheme = FLAG_BOOLEAN;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001169 } else {
1170 fprintf (stderr, "Incompatible flash types!\n");
1171 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001172 }
wdenk4a6fd342003-04-12 23:38:12 +00001173
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001174 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1175 crc1_ok = (crc1 == redundant->crc);
1176 flag1 = redundant->flags;
wdenk4a6fd342003-04-12 23:38:12 +00001177
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001178 if (crc0_ok && !crc1_ok) {
1179 dev_current = 0;
1180 } else if (!crc0_ok && crc1_ok) {
1181 dev_current = 1;
1182 } else if (!crc0_ok && !crc1_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001183 fprintf (stderr,
1184 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001185 memcpy (environment.data, default_environment,
1186 sizeof default_environment);
1187 dev_current = 0;
1188 } else {
1189 switch (environment.flag_scheme) {
1190 case FLAG_BOOLEAN:
1191 if (flag0 == active_flag &&
1192 flag1 == obsolete_flag) {
1193 dev_current = 0;
1194 } else if (flag0 == obsolete_flag &&
1195 flag1 == active_flag) {
1196 dev_current = 1;
1197 } else if (flag0 == flag1) {
1198 dev_current = 0;
1199 } else if (flag0 == 0xFF) {
1200 dev_current = 0;
1201 } else if (flag1 == 0xFF) {
1202 dev_current = 1;
1203 } else {
1204 dev_current = 0;
1205 }
1206 break;
1207 case FLAG_INCREMENTAL:
Jon Povey735eb0f2011-03-11 14:10:56 +09001208 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001209 dev_current = 1;
1210 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey735eb0f2011-03-11 14:10:56 +09001211 flag0 >= flag1)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001212 dev_current = 0;
Jon Povey735eb0f2011-03-11 14:10:56 +09001213 else /* flag1 > flag0 */
1214 dev_current = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001215 break;
1216 default:
1217 fprintf (stderr, "Unknown flag scheme %u \n",
1218 environment.flag_scheme);
1219 return -1;
1220 }
1221 }
1222
1223 /*
1224 * If we are reading, we don't need the flag and the CRC any
1225 * more, if we are writing, we will re-calculate CRC and update
1226 * flags before writing out
1227 */
1228 if (dev_current) {
1229 environment.image = addr1;
1230 environment.crc = &redundant->crc;
1231 environment.flags = &redundant->flags;
1232 environment.data = redundant->data;
1233 free (addr0);
1234 } else {
1235 environment.image = addr0;
1236 /* Other pointers are already set */
wdenk4a6fd342003-04-12 23:38:12 +00001237 free (addr1);
wdenk6aff3112002-12-17 01:51:00 +00001238 }
wdenk6aff3112002-12-17 01:51:00 +00001239 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001240 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001241}
1242
1243
wdenk4a6fd342003-04-12 23:38:12 +00001244static int parse_config ()
wdenk6aff3112002-12-17 01:51:00 +00001245{
1246 struct stat st;
1247
wdenkd0fb80c2003-01-11 09:48:40 +00001248#if defined(CONFIG_FILE)
1249 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
wdenk4a6fd342003-04-12 23:38:12 +00001250 if (get_config (CONFIG_FILE)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001251 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001252 "Cannot parse config file: %s\n", strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001253 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001254 }
wdenkd0fb80c2003-01-11 09:48:40 +00001255#else
wdenk4a6fd342003-04-12 23:38:12 +00001256 strcpy (DEVNAME (0), DEVICE1_NAME);
1257 DEVOFFSET (0) = DEVICE1_OFFSET;
1258 ENVSIZE (0) = ENV1_SIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001259 /* Default values are: erase-size=env-size, #sectors=1 */
1260 DEVESIZE (0) = ENVSIZE (0);
1261 ENVSECTORS (0) = 1;
1262#ifdef DEVICE1_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001263 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001264#endif
1265#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001266 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001267#endif
1268
wdenk6aff3112002-12-17 01:51:00 +00001269#ifdef HAVE_REDUND
wdenk4a6fd342003-04-12 23:38:12 +00001270 strcpy (DEVNAME (1), DEVICE2_NAME);
1271 DEVOFFSET (1) = DEVICE2_OFFSET;
1272 ENVSIZE (1) = ENV2_SIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001273 /* Default values are: erase-size=env-size, #sectors=1 */
1274 DEVESIZE (1) = ENVSIZE (1);
1275 ENVSECTORS (1) = 1;
1276#ifdef DEVICE2_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001277 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001278#endif
1279#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001280 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001281#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001282 HaveRedundEnv = 1;
wdenk6aff3112002-12-17 01:51:00 +00001283#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001284#endif
wdenk4a6fd342003-04-12 23:38:12 +00001285 if (stat (DEVNAME (0), &st)) {
1286 fprintf (stderr,
1287 "Cannot access MTD device %s: %s\n",
1288 DEVNAME (0), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001289 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001290 }
wdenk4a6fd342003-04-12 23:38:12 +00001291
1292 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1293 fprintf (stderr,
1294 "Cannot access MTD device %s: %s\n",
Wolfgang Denke2146b62005-09-25 01:20:38 +02001295 DEVNAME (1), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001296 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001297 }
wdenk6aff3112002-12-17 01:51:00 +00001298 return 0;
1299}
wdenkd0fb80c2003-01-11 09:48:40 +00001300
1301#if defined(CONFIG_FILE)
1302static int get_config (char *fname)
1303{
1304 FILE *fp;
1305 int i = 0;
1306 int rc;
1307 char dump[128];
1308
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001309 fp = fopen (fname, "r");
1310 if (fp == NULL)
1311 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001312
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001313 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001314 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001315 if (dump[0] == '#')
wdenkd0fb80c2003-01-11 09:48:40 +00001316 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001317
1318 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1319 DEVNAME (i),
1320 &DEVOFFSET (i),
1321 &ENVSIZE (i),
1322 &DEVESIZE (i),
1323 &ENVSECTORS (i));
1324
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001325 if (rc < 3)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001326 continue;
1327
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001328 if (rc < 4)
1329 /* Assume the erase size is the same as the env-size */
1330 DEVESIZE(i) = ENVSIZE(i);
1331
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001332 if (rc < 5)
1333 /* Default - 1 sector */
1334 ENVSECTORS (i) = 1;
wdenkd0fb80c2003-01-11 09:48:40 +00001335
1336 i++;
1337 }
wdenk4a6fd342003-04-12 23:38:12 +00001338 fclose (fp);
1339
wdenkd0fb80c2003-01-11 09:48:40 +00001340 HaveRedundEnv = i - 1;
wdenk4a6fd342003-04-12 23:38:12 +00001341 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +00001342 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001343 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001344 } else
1345 return 0;
1346}
1347#endif