blob: 8ff70522430dee31f776a6f31c8eae7442df83de [file] [log] [blame]
wdenk34b613e2002-12-17 01:51:00 +00001/*
Detlev Zundel2111d3e2010-07-30 11:22:15 +02002 * (C) Copyright 2000-2010
wdenk34b613e2002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02005 * (C) Copyright 2008
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
7 *
wdenk34b613e2002-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
wdenk3f9ab982003-04-12 23:38:12 +000018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenk34b613e2002-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>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010037
38#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020039# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010040# include <linux/mtd/mtd.h>
41#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010042# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010043# include <mtd/mtd-user.h>
44#endif
wdenk34b613e2002-12-17 01:51:00 +000045
Markus Klotzbücher94db5572007-11-27 10:23:20 +010046#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000047
Stefano Babicada628b2010-05-24 12:08:16 +020048#define WHITESPACE(c) ((c == '\t') || (c == ' '))
wdenk34b613e2002-12-17 01:51:00 +000049
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020050#define min(x, y) ({ \
51 typeof(x) _min1 = (x); \
52 typeof(y) _min2 = (y); \
53 (void) (&_min1 == &_min2); \
54 _min1 < _min2 ? _min1 : _min2; })
55
56struct envdev_s {
Markus Klotzbücher94db5572007-11-27 10:23:20 +010057 char devname[16]; /* Device name */
wdenk3f9ab982003-04-12 23:38:12 +000058 ulong devoff; /* Device offset */
59 ulong env_size; /* environment size */
60 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020061 ulong env_sectors; /* number of environment sectors */
62 uint8_t mtd_type; /* type of the MTD device */
63};
wdenk34b613e2002-12-17 01:51:00 +000064
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020065static struct envdev_s envdevices[2] =
66{
67 {
68 .mtd_type = MTD_ABSENT,
69 }, {
70 .mtd_type = MTD_ABSENT,
71 },
72};
73static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000074
75#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000076#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000077#define ENVSIZE(i) envdevices[(i)].env_size
78#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020079#define ENVSECTORS(i) envdevices[(i)].env_sectors
80#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk34b613e2002-12-17 01:51:00 +000081
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020082#define CONFIG_ENV_SIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000083
wdenke7f34c62003-01-11 09:48:40 +000084#define ENV_SIZE getenvsize()
wdenk34b613e2002-12-17 01:51:00 +000085
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020086struct env_image_single {
87 uint32_t crc; /* CRC32 over data bytes */
88 char data[];
89};
wdenk34b613e2002-12-17 01:51:00 +000090
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020091struct env_image_redundant {
92 uint32_t crc; /* CRC32 over data bytes */
93 unsigned char flags; /* active or obsolete */
94 char data[];
95};
96
97enum flag_scheme {
98 FLAG_NONE,
99 FLAG_BOOLEAN,
100 FLAG_INCREMENTAL,
101};
102
103struct environment {
104 void *image;
105 uint32_t *crc;
106 unsigned char *flags;
107 char *data;
108 enum flag_scheme flag_scheme;
109};
110
111static struct environment environment = {
112 .flag_scheme = FLAG_NONE,
113};
wdenk34b613e2002-12-17 01:51:00 +0000114
wdenke7f34c62003-01-11 09:48:40 +0000115static int HaveRedundEnv = 0;
116
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100117static unsigned char active_flag = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200118/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100119static unsigned char obsolete_flag = 0;
wdenke7f34c62003-01-11 09:48:40 +0000120
wdenk34b613e2002-12-17 01:51:00 +0000121
122#define XMK_STR(x) #x
123#define MK_STR(x) XMK_STR(x)
124
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100125static char default_environment[] = {
wdenke7f34c62003-01-11 09:48:40 +0000126#if defined(CONFIG_BOOTARGS)
wdenk3f9ab982003-04-12 23:38:12 +0000127 "bootargs=" CONFIG_BOOTARGS "\0"
wdenk34b613e2002-12-17 01:51:00 +0000128#endif
wdenke7f34c62003-01-11 09:48:40 +0000129#if defined(CONFIG_BOOTCOMMAND)
wdenk3f9ab982003-04-12 23:38:12 +0000130 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
wdenk34b613e2002-12-17 01:51:00 +0000131#endif
wdenke7f34c62003-01-11 09:48:40 +0000132#if defined(CONFIG_RAMBOOTCOMMAND)
wdenk3f9ab982003-04-12 23:38:12 +0000133 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
wdenke7f34c62003-01-11 09:48:40 +0000134#endif
135#if defined(CONFIG_NFSBOOTCOMMAND)
wdenk3f9ab982003-04-12 23:38:12 +0000136 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
wdenke7f34c62003-01-11 09:48:40 +0000137#endif
138#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
wdenk3f9ab982003-04-12 23:38:12 +0000139 "bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000140#endif
wdenke7f34c62003-01-11 09:48:40 +0000141#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
wdenk3f9ab982003-04-12 23:38:12 +0000142 "baudrate=" MK_STR (CONFIG_BAUDRATE) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000143#endif
wdenke7f34c62003-01-11 09:48:40 +0000144#ifdef CONFIG_LOADS_ECHO
wdenk3f9ab982003-04-12 23:38:12 +0000145 "loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000146#endif
wdenk34b613e2002-12-17 01:51:00 +0000147#ifdef CONFIG_ETHADDR
wdenk3f9ab982003-04-12 23:38:12 +0000148 "ethaddr=" MK_STR (CONFIG_ETHADDR) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000149#endif
wdenke7f34c62003-01-11 09:48:40 +0000150#ifdef CONFIG_ETH1ADDR
wdenk3f9ab982003-04-12 23:38:12 +0000151 "eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000152#endif
153#ifdef CONFIG_ETH2ADDR
wdenk3f9ab982003-04-12 23:38:12 +0000154 "eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000155#endif
wdenk54070ab2004-12-31 09:32:47 +0000156#ifdef CONFIG_ETH3ADDR
157 "eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0"
158#endif
richardretanubune5167f12008-09-29 18:28:23 -0400159#ifdef CONFIG_ETH4ADDR
160 "eth4addr=" MK_STR (CONFIG_ETH4ADDR) "\0"
161#endif
162#ifdef CONFIG_ETH5ADDR
163 "eth5addr=" MK_STR (CONFIG_ETH5ADDR) "\0"
164#endif
wdenke7f34c62003-01-11 09:48:40 +0000165#ifdef CONFIG_ETHPRIME
wdenk3f9ab982003-04-12 23:38:12 +0000166 "ethprime=" CONFIG_ETHPRIME "\0"
wdenke7f34c62003-01-11 09:48:40 +0000167#endif
wdenk34b613e2002-12-17 01:51:00 +0000168#ifdef CONFIG_IPADDR
wdenk3f9ab982003-04-12 23:38:12 +0000169 "ipaddr=" MK_STR (CONFIG_IPADDR) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000170#endif
171#ifdef CONFIG_SERVERIP
wdenk3f9ab982003-04-12 23:38:12 +0000172 "serverip=" MK_STR (CONFIG_SERVERIP) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000173#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200174#ifdef CONFIG_SYS_AUTOLOAD
175 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
wdenke7f34c62003-01-11 09:48:40 +0000176#endif
177#ifdef CONFIG_ROOTPATH
wdenk3f9ab982003-04-12 23:38:12 +0000178 "rootpath=" MK_STR (CONFIG_ROOTPATH) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000179#endif
180#ifdef CONFIG_GATEWAYIP
wdenk3f9ab982003-04-12 23:38:12 +0000181 "gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000182#endif
183#ifdef CONFIG_NETMASK
wdenk3f9ab982003-04-12 23:38:12 +0000184 "netmask=" MK_STR (CONFIG_NETMASK) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000185#endif
186#ifdef CONFIG_HOSTNAME
wdenk3f9ab982003-04-12 23:38:12 +0000187 "hostname=" MK_STR (CONFIG_HOSTNAME) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000188#endif
189#ifdef CONFIG_BOOTFILE
wdenk3f9ab982003-04-12 23:38:12 +0000190 "bootfile=" MK_STR (CONFIG_BOOTFILE) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000191#endif
192#ifdef CONFIG_LOADADDR
wdenk3f9ab982003-04-12 23:38:12 +0000193 "loadaddr=" MK_STR (CONFIG_LOADADDR) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000194#endif
195#ifdef CONFIG_PREBOOT
wdenk3f9ab982003-04-12 23:38:12 +0000196 "preboot=" CONFIG_PREBOOT "\0"
wdenke7f34c62003-01-11 09:48:40 +0000197#endif
198#ifdef CONFIG_CLOCKS_IN_MHZ
wdenk3f9ab982003-04-12 23:38:12 +0000199 "clocks_in_mhz=" "1" "\0"
wdenke7f34c62003-01-11 09:48:40 +0000200#endif
stroesef5dd4102003-02-14 11:21:23 +0000201#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
wdenk3f9ab982003-04-12 23:38:12 +0000202 "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0"
stroesef5dd4102003-02-14 11:21:23 +0000203#endif
wdenke7f34c62003-01-11 09:48:40 +0000204#ifdef CONFIG_EXTRA_ENV_SETTINGS
205 CONFIG_EXTRA_ENV_SETTINGS
206#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200207 "\0" /* Termimate struct environment data with 2 NULs */
wdenk34b613e2002-12-17 01:51:00 +0000208};
209
wdenk3f9ab982003-04-12 23:38:12 +0000210static int flash_io (int mode);
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100211static char *envmatch (char * s1, char * s2);
wdenk3f9ab982003-04-12 23:38:12 +0000212static int parse_config (void);
213
wdenke7f34c62003-01-11 09:48:40 +0000214#if defined(CONFIG_FILE)
wdenk3f9ab982003-04-12 23:38:12 +0000215static int get_config (char *);
wdenke7f34c62003-01-11 09:48:40 +0000216#endif
wdenk3f9ab982003-04-12 23:38:12 +0000217static inline ulong getenvsize (void)
wdenke7f34c62003-01-11 09:48:40 +0000218{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200219 ulong rc = CONFIG_ENV_SIZE - sizeof (long);
wdenk3f9ab982003-04-12 23:38:12 +0000220
wdenke7f34c62003-01-11 09:48:40 +0000221 if (HaveRedundEnv)
wdenk3f9ab982003-04-12 23:38:12 +0000222 rc -= sizeof (char);
wdenke7f34c62003-01-11 09:48:40 +0000223 return rc;
224}
wdenk34b613e2002-12-17 01:51:00 +0000225
Stefano Babicada628b2010-05-24 12:08:16 +0200226static char *fw_string_blank(char *s, int noblank)
227{
228 int i;
229 int len = strlen(s);
230
231 for (i = 0; i < len; i++, s++) {
232 if ((noblank && !WHITESPACE(*s)) ||
233 (!noblank && WHITESPACE(*s)))
234 break;
235 }
236 if (i == len)
237 return NULL;
238
239 return s;
240}
241
wdenk34b613e2002-12-17 01:51:00 +0000242/*
243 * Search the environment for a variable.
244 * Return the value, if found, or NULL, if not found.
245 */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100246char *fw_getenv (char *name)
wdenk34b613e2002-12-17 01:51:00 +0000247{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100248 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000249
Stefano Babicada628b2010-05-24 12:08:16 +0200250 if (fw_env_open())
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200251 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000252
wdenk3f9ab982003-04-12 23:38:12 +0000253 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100254 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000255
wdenk3f9ab982003-04-12 23:38:12 +0000256 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000257 if (nxt >= &environment.data[ENV_SIZE]) {
258 fprintf (stderr, "## Error: "
259 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200260 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000261 }
262 }
wdenk3f9ab982003-04-12 23:38:12 +0000263 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000264 if (!val)
265 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200266 return val;
wdenk34b613e2002-12-17 01:51:00 +0000267 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200268 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000269}
270
271/*
272 * Print the current definition of one, or more, or all
273 * environment variables
274 */
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700275int fw_printenv (int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000276{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100277 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000278 int i, n_flag;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700279 int rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000280
Stefano Babicada628b2010-05-24 12:08:16 +0200281 if (fw_env_open())
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200282 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000283
wdenk3f9ab982003-04-12 23:38:12 +0000284 if (argc == 1) { /* Print all env variables */
285 for (env = environment.data; *env; env = nxt + 1) {
286 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000287 if (nxt >= &environment.data[ENV_SIZE]) {
288 fprintf (stderr, "## Error: "
289 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200290 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000291 }
292 }
293
wdenk3f9ab982003-04-12 23:38:12 +0000294 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000295 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200296 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000297 }
298
wdenk3f9ab982003-04-12 23:38:12 +0000299 if (strcmp (argv[1], "-n") == 0) {
wdenk34b613e2002-12-17 01:51:00 +0000300 n_flag = 1;
301 ++argv;
302 --argc;
303 if (argc != 2) {
304 fprintf (stderr, "## Error: "
305 "`-n' option requires exactly one argument\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200306 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000307 }
308 } else {
309 n_flag = 0;
310 }
311
wdenk3f9ab982003-04-12 23:38:12 +0000312 for (i = 1; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100313 char *name = argv[i];
314 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000315
wdenk3f9ab982003-04-12 23:38:12 +0000316 for (env = environment.data; *env; env = nxt + 1) {
wdenk34b613e2002-12-17 01:51:00 +0000317
wdenk3f9ab982003-04-12 23:38:12 +0000318 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000319 if (nxt >= &environment.data[ENV_SIZE]) {
320 fprintf (stderr, "## Error: "
321 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200322 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000323 }
324 }
wdenk3f9ab982003-04-12 23:38:12 +0000325 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000326 if (val) {
327 if (!n_flag) {
328 fputs (name, stdout);
wdenk3f9ab982003-04-12 23:38:12 +0000329 putc ('=', stdout);
wdenk34b613e2002-12-17 01:51:00 +0000330 }
wdenk3f9ab982003-04-12 23:38:12 +0000331 puts (val);
wdenk34b613e2002-12-17 01:51:00 +0000332 break;
333 }
334 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700335 if (!val) {
wdenk3f9ab982003-04-12 23:38:12 +0000336 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700337 rc = -1;
338 }
wdenk34b613e2002-12-17 01:51:00 +0000339 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700340
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200341 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000342}
343
Stefano Babicada628b2010-05-24 12:08:16 +0200344int fw_env_close(void)
wdenk34b613e2002-12-17 01:51:00 +0000345{
Stefano Babicada628b2010-05-24 12:08:16 +0200346 /*
347 * Update CRC
348 */
349 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000350
Stefano Babicada628b2010-05-24 12:08:16 +0200351 /* write environment back to flash */
352 if (flash_io(O_RDWR)) {
353 fprintf(stderr,
354 "Error: can't write fw_env to flash\n");
355 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000356 }
357
Stefano Babicada628b2010-05-24 12:08:16 +0200358 return 0;
359}
wdenk34b613e2002-12-17 01:51:00 +0000360
Stefano Babicada628b2010-05-24 12:08:16 +0200361
362/*
363 * Set/Clear a single variable in the environment.
364 * This is called in sequence to update the environment
365 * in RAM without updating the copy in flash after each set
366 */
367int fw_env_write(char *name, char *value)
368{
369 int len;
370 char *env, *nxt;
371 char *oldval = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000372
373 /*
374 * search if variable with this name already exists
375 */
wdenk3f9ab982003-04-12 23:38:12 +0000376 for (nxt = env = environment.data; *env; env = nxt + 1) {
377 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000378 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200379 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000380 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200381 errno = EINVAL;
382 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000383 }
384 }
wdenk3f9ab982003-04-12 23:38:12 +0000385 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000386 break;
387 }
388
389 /*
390 * Delete any existing definition
391 */
392 if (oldval) {
393 /*
394 * Ethernet Address and serial# can be set only once
395 */
396 if ((strcmp (name, "ethaddr") == 0) ||
wdenk3f9ab982003-04-12 23:38:12 +0000397 (strcmp (name, "serial#") == 0)) {
wdenk34b613e2002-12-17 01:51:00 +0000398 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200399 errno = EROFS;
400 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000401 }
402
403 if (*++nxt == '\0') {
404 *env = '\0';
405 } else {
406 for (;;) {
407 *env = *nxt++;
408 if ((*env == '\0') && (*nxt == '\0'))
409 break;
410 ++env;
411 }
412 }
413 *++env = '\0';
414 }
415
416 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200417 if (!value || !strlen(value))
418 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000419
420 /*
421 * Append new definition at the end
422 */
wdenk3f9ab982003-04-12 23:38:12 +0000423 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000424 if (env > environment.data)
425 ++env;
426 /*
427 * Overflow when:
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200428 * "name" + "=" + "val" +"\0\0" > CONFIG_ENV_SIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000429 */
wdenk3f9ab982003-04-12 23:38:12 +0000430 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000431 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200432 len += strlen(value) + 1;
433
wdenk3f9ab982003-04-12 23:38:12 +0000434 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000435 fprintf (stderr,
436 "Error: environment overflow, \"%s\" deleted\n",
437 name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200438 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000439 }
Stefano Babicada628b2010-05-24 12:08:16 +0200440
wdenk34b613e2002-12-17 01:51:00 +0000441 while ((*env = *name++) != '\0')
442 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200443 *env = '=';
444 while ((*++env = *value++) != '\0')
445 ;
446
447 /* end is marked with double '\0' */
448 *++env = '\0';
449
450 return 0;
451}
452
453/*
454 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
455 * 0 - OK
456 * EINVAL - need at least 1 argument
457 * EROFS - certain variables ("ethaddr", "serial#") cannot be
458 * modified or deleted
459 *
460 */
461int fw_setenv(int argc, char *argv[])
462{
463 int i, len;
464 char *name;
465 char *value = NULL;
466 char *tmpval = NULL;
467
468 if (argc < 2) {
469 errno = EINVAL;
470 return -1;
471 }
472
473 if (fw_env_open()) {
474 fprintf(stderr, "Error: environment not initialized\n");
475 return -1;
476 }
477
478 name = argv[1];
479
480 len = strlen(name) + 2;
481 for (i = 2; i < argc; ++i)
482 len += strlen(argv[i]) + 1;
483
484 /* Allocate enough place to the data string */
wdenk3f9ab982003-04-12 23:38:12 +0000485 for (i = 2; i < argc; ++i) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100486 char *val = argv[i];
Stefano Babicada628b2010-05-24 12:08:16 +0200487 if (!value) {
488 value = (char *)malloc(len - strlen(name));
489 if (!value) {
490 fprintf(stderr,
491 "Cannot malloc %u bytes: %s\n",
492 len - strlen(name), strerror(errno));
493 return -1;
494 }
495 memset(value, 0, len - strlen(name));
496 tmpval = value;
497 }
498 if (i != 2)
499 *tmpval++ = ' ';
500 while (*val != '\0')
501 *tmpval++ = *val++;
wdenk34b613e2002-12-17 01:51:00 +0000502 }
503
Stefano Babicada628b2010-05-24 12:08:16 +0200504 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000505
Stefano Babicada628b2010-05-24 12:08:16 +0200506 if (value)
507 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000508
Stefano Babicada628b2010-05-24 12:08:16 +0200509 return fw_env_close();
510}
wdenk34b613e2002-12-17 01:51:00 +0000511
Stefano Babicada628b2010-05-24 12:08:16 +0200512/*
513 * Parse a file and configure the u-boot variables.
514 * The script file has a very simple format, as follows:
515 *
516 * Each line has a couple with name, value:
517 * <white spaces>variable_name<white spaces>variable_value
518 *
519 * Both variable_name and variable_value are interpreted as strings.
520 * Any character after <white spaces> and before ending \r\n is interpreted
521 * as variable's value (no comment allowed on these lines !)
522 *
523 * Comments are allowed if the first character in the line is #
524 *
525 * Returns -1 and sets errno error codes:
526 * 0 - OK
527 * -1 - Error
528 */
529int fw_parse_script(char *fname)
530{
531 FILE *fp;
532 char dump[1024]; /* Maximum line length in the file */
533 char *name;
534 char *val;
535 int lineno = 0;
536 int len;
537 int ret = 0;
538
539 if (fw_env_open()) {
540 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200541 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000542 }
543
Stefano Babicada628b2010-05-24 12:08:16 +0200544 if (strcmp(fname, "-") == 0)
545 fp = stdin;
546 else {
547 fp = fopen(fname, "r");
548 if (fp == NULL) {
549 fprintf(stderr, "I cannot open %s for reading\n",
550 fname);
551 return -1;
552 }
553 }
554
555 while (fgets(dump, sizeof(dump), fp)) {
556 lineno++;
557 len = strlen(dump);
558
559 /*
560 * Read a whole line from the file. If the line is too long
561 * or is not terminated, reports an error and exit.
562 */
563 if (dump[len - 1] != '\n') {
564 fprintf(stderr,
565 "Line %d not corrected terminated or too long\n",
566 lineno);
567 ret = -1;
568 break;
569 }
570
571 /* Drop ending line feed / carriage return */
572 while (len > 0 && (dump[len - 1] == '\n' ||
573 dump[len - 1] == '\r')) {
574 dump[len - 1] = '\0';
575 len--;
576 }
577
578 /* Skip comment or empty lines */
579 if ((len == 0) || dump[0] == '#')
580 continue;
581
582 /*
583 * Search for variable's name,
584 * remove leading whitespaces
585 */
586 name = fw_string_blank(dump, 1);
587 if (!name)
588 continue;
589
590 /* The first white space is the end of variable name */
591 val = fw_string_blank(name, 0);
592 len = strlen(name);
593 if (val) {
594 *val++ = '\0';
595 if ((val - name) < len)
596 val = fw_string_blank(val, 1);
597 else
598 val = NULL;
599 }
600
601#ifdef DEBUG
602 fprintf(stderr, "Setting %s : %s\n",
603 name, val ? val : " removed");
604#endif
605
606 /*
607 * If there is an error setting a variable,
608 * try to save the environment and returns an error
609 */
610 if (fw_env_write(name, val)) {
611 fprintf(stderr,
612 "fw_env_write returns with error : %s\n",
613 strerror(errno));
614 ret = -1;
615 break;
616 }
617
618 }
619
620 /* Close file if not stdin */
621 if (strcmp(fname, "-") != 0)
622 fclose(fp);
623
624 ret |= fw_env_close();
625
626 return ret;
627
wdenk34b613e2002-12-17 01:51:00 +0000628}
629
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200630/*
631 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
632 * 0 - block is good
633 * > 0 - block is bad
634 * < 0 - failed to test
635 */
636static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000637{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200638 if (mtd_type == MTD_NANDFLASH) {
639 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000640
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200641 if (badblock < 0) {
642 perror ("Cannot read bad block mark");
643 return badblock;
644 }
645
646 if (badblock) {
647#ifdef DEBUG
648 fprintf (stderr, "Bad block at 0x%llx, "
649 "skipping\n", *blockstart);
650#endif
651 return badblock;
652 }
wdenk34b613e2002-12-17 01:51:00 +0000653 }
654
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200655 return 0;
656}
657
658/*
659 * Read data from flash at an offset into a provided buffer. On NAND it skips
660 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
661 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
662 */
663static int flash_read_buf (int dev, int fd, void *buf, size_t count,
664 off_t offset, uint8_t mtd_type)
665{
666 size_t blocklen; /* erase / write length - one block on NAND,
667 0 on NOR */
668 size_t processed = 0; /* progress counter */
669 size_t readlen = count; /* current read length */
670 off_t top_of_range; /* end of the last block we may use */
671 off_t block_seek; /* offset inside the current block to the start
672 of the data */
673 loff_t blockstart; /* running start of the current block -
674 MEMGETBADBLOCK needs 64 bits */
675 int rc;
676
677 /*
678 * Start of the first block to be read, relies on the fact, that
679 * erase sector size is always a power of 2
680 */
681 blockstart = offset & ~(DEVESIZE (dev) - 1);
682
683 /* Offset inside a block */
684 block_seek = offset - blockstart;
685
686 if (mtd_type == MTD_NANDFLASH) {
687 /*
688 * NAND: calculate which blocks we are reading. We have
689 * to read one block at a time to skip bad blocks.
690 */
691 blocklen = DEVESIZE (dev);
692
693 /*
694 * To calculate the top of the range, we have to use the
695 * global DEVOFFSET (dev), which can be different from offset
696 */
697 top_of_range = (DEVOFFSET (dev) & ~(blocklen - 1)) +
698 ENVSECTORS (dev) * blocklen;
699
700 /* Limit to one block for the first read */
701 if (readlen > blocklen - block_seek)
702 readlen = blocklen - block_seek;
703 } else {
704 blocklen = 0;
705 top_of_range = offset + count;
wdenke7f34c62003-01-11 09:48:40 +0000706 }
wdenk34b613e2002-12-17 01:51:00 +0000707
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200708 /* This only runs once on NOR flash */
709 while (processed < count) {
710 rc = flash_bad_block (fd, mtd_type, &blockstart);
711 if (rc < 0) /* block test failed */
712 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000713
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200714 if (blockstart + block_seek + readlen > top_of_range) {
715 /* End of range is reached */
716 fprintf (stderr,
717 "Too few good blocks within range\n");
718 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000719 }
720
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200721 if (rc) { /* block is bad */
722 blockstart += blocklen;
723 continue;
wdenk34b613e2002-12-17 01:51:00 +0000724 }
725
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200726 /*
727 * If a block is bad, we retry in the next block at the same
728 * offset - see common/env_nand.c::writeenv()
729 */
730 lseek (fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000731
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200732 rc = read (fd, buf + processed, readlen);
733 if (rc != readlen) {
734 fprintf (stderr, "Read error on %s: %s\n",
735 DEVNAME (dev), strerror (errno));
736 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000737 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200738#ifdef DEBUG
739 fprintf (stderr, "Read 0x%x bytes at 0x%llx\n",
740 rc, blockstart + block_seek);
741#endif
742 processed += readlen;
743 readlen = min (blocklen, count - processed);
744 block_seek = 0;
745 blockstart += blocklen;
746 }
747
748 return processed;
749}
750
751/*
752 * Write count bytes at offset, but stay within ENVSETCORS (dev) sectors of
753 * DEVOFFSET (dev). Similar to the read case above, on NOR we erase and write
754 * the whole data at once.
755 */
756static int flash_write_buf (int dev, int fd, void *buf, size_t count,
757 off_t offset, uint8_t mtd_type)
758{
759 void *data;
760 struct erase_info_user erase;
761 size_t blocklen; /* length of NAND block / NOR erase sector */
762 size_t erase_len; /* whole area that can be erased - may include
763 bad blocks */
764 size_t erasesize; /* erase / write length - one block on NAND,
765 whole area on NOR */
766 size_t processed = 0; /* progress counter */
767 size_t write_total; /* total size to actually write - excludinig
768 bad blocks */
769 off_t erase_offset; /* offset to the first erase block (aligned)
770 below offset */
771 off_t block_seek; /* offset inside the erase block to the start
772 of the data */
773 off_t top_of_range; /* end of the last block we may use */
774 loff_t blockstart; /* running start of the current block -
775 MEMGETBADBLOCK needs 64 bits */
776 int rc;
777
778 blocklen = DEVESIZE (dev);
779
780 /* Erase sector size is always a power of 2 */
781 top_of_range = (DEVOFFSET (dev) & ~(blocklen - 1)) +
782 ENVSECTORS (dev) * blocklen;
783
784 erase_offset = offset & ~(blocklen - 1);
wdenk34b613e2002-12-17 01:51:00 +0000785
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200786 /* Maximum area we may use */
787 erase_len = top_of_range - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +0000788
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200789 blockstart = erase_offset;
790 /* Offset inside a block */
791 block_seek = offset - erase_offset;
792
793 /*
794 * Data size we actually have to write: from the start of the block
795 * to the start of the data, then count bytes of data, and to the
796 * end of the block
797 */
798 write_total = (block_seek + count + blocklen - 1) & ~(blocklen - 1);
799
800 /*
801 * Support data anywhere within erase sectors: read out the complete
802 * area to be erased, replace the environment image, write the whole
803 * block back again.
804 */
805 if (write_total > count) {
806 data = malloc (erase_len);
807 if (!data) {
wdenke7f34c62003-01-11 09:48:40 +0000808 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200809 "Cannot malloc %u bytes: %s\n",
810 erase_len, strerror (errno));
811 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000812 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200813
814 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
815 mtd_type);
816 if (write_total != rc)
817 return -1;
818
819 /* Overwrite the old environment */
820 memcpy (data + block_seek, buf, count);
821 } else {
822 /*
823 * We get here, iff offset is block-aligned and count is a
824 * multiple of blocklen - see write_total calculation above
825 */
826 data = buf;
827 }
828
829 if (mtd_type == MTD_NANDFLASH) {
830 /*
831 * NAND: calculate which blocks we are writing. We have
832 * to write one block at a time to skip bad blocks.
833 */
834 erasesize = blocklen;
835 } else {
836 erasesize = erase_len;
837 }
838
839 erase.length = erasesize;
840
841 /* This only runs once on NOR flash */
842 while (processed < write_total) {
843 rc = flash_bad_block (fd, mtd_type, &blockstart);
844 if (rc < 0) /* block test failed */
845 return rc;
846
847 if (blockstart + erasesize > top_of_range) {
848 fprintf (stderr, "End of range reached, aborting\n");
849 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000850 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200851
852 if (rc) { /* block is bad */
853 blockstart += blocklen;
854 continue;
855 }
856
857 erase.start = blockstart;
858 ioctl (fd, MEMUNLOCK, &erase);
859
860 if (ioctl (fd, MEMERASE, &erase) != 0) {
861 fprintf (stderr, "MTD erase error on %s: %s\n",
862 DEVNAME (dev),
863 strerror (errno));
864 return -1;
865 }
866
867 if (lseek (fd, blockstart, SEEK_SET) == -1) {
wdenk34b613e2002-12-17 01:51:00 +0000868 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200869 "Seek error on %s: %s\n",
870 DEVNAME (dev), strerror (errno));
871 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000872 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200873
874#ifdef DEBUG
875 printf ("Write 0x%x bytes at 0x%llx\n", erasesize, blockstart);
876#endif
877 if (write (fd, data + processed, erasesize) != erasesize) {
878 fprintf (stderr, "Write error on %s: %s\n",
879 DEVNAME (dev), strerror (errno));
880 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000881 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200882
883 ioctl (fd, MEMLOCK, &erase);
884
885 processed += blocklen;
886 block_seek = 0;
887 blockstart += blocklen;
888 }
889
890 if (write_total > count)
891 free (data);
892
893 return processed;
894}
895
896/*
897 * Set obsolete flag at offset - NOR flash only
898 */
899static int flash_flag_obsolete (int dev, int fd, off_t offset)
900{
901 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200902 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200903
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200904 erase.start = DEVOFFSET (dev);
905 erase.length = DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200906 /* This relies on the fact, that obsolete_flag == 0 */
907 rc = lseek (fd, offset, SEEK_SET);
908 if (rc < 0) {
909 fprintf (stderr, "Cannot seek to set the flag on %s \n",
910 DEVNAME (dev));
911 return rc;
912 }
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200913 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200914 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200915 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200916 if (rc < 0)
917 perror ("Could not set obsolete flag");
918
919 return rc;
920}
921
922static int flash_write (int fd_current, int fd_target, int dev_target)
923{
924 int rc;
925
926 switch (environment.flag_scheme) {
927 case FLAG_NONE:
928 break;
929 case FLAG_INCREMENTAL:
930 (*environment.flags)++;
931 break;
932 case FLAG_BOOLEAN:
933 *environment.flags = active_flag;
934 break;
935 default:
936 fprintf (stderr, "Unimplemented flash scheme %u \n",
937 environment.flag_scheme);
938 return -1;
939 }
940
941#ifdef DEBUG
942 printf ("Writing new environment at 0x%lx on %s\n",
943 DEVOFFSET (dev_target), DEVNAME (dev_target));
944#endif
945 rc = flash_write_buf (dev_target, fd_target, environment.image,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200946 CONFIG_ENV_SIZE, DEVOFFSET (dev_target),
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200947 DEVTYPE(dev_target));
948 if (rc < 0)
949 return rc;
950
951 if (environment.flag_scheme == FLAG_BOOLEAN) {
952 /* Have to set obsolete flag */
953 off_t offset = DEVOFFSET (dev_current) +
954 offsetof (struct env_image_redundant, flags);
955#ifdef DEBUG
956 printf ("Setting obsolete flag in environment at 0x%lx on %s\n",
957 DEVOFFSET (dev_current), DEVNAME (dev_current));
958#endif
959 flash_flag_obsolete (dev_current, fd_current, offset);
960 }
961
962 return 0;
963}
964
965static int flash_read (int fd)
966{
967 struct mtd_info_user mtdinfo;
968 int rc;
969
970 rc = ioctl (fd, MEMGETINFO, &mtdinfo);
971 if (rc < 0) {
972 perror ("Cannot get MTD information");
973 return -1;
974 }
975
976 if (mtdinfo.type != MTD_NORFLASH && mtdinfo.type != MTD_NANDFLASH) {
977 fprintf (stderr, "Unsupported flash type %u\n", mtdinfo.type);
978 return -1;
979 }
980
981 DEVTYPE(dev_current) = mtdinfo.type;
982
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200983 rc = flash_read_buf (dev_current, fd, environment.image, CONFIG_ENV_SIZE,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200984 DEVOFFSET (dev_current), mtdinfo.type);
985
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200986 return (rc != CONFIG_ENV_SIZE) ? -1 : 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200987}
988
989static int flash_io (int mode)
990{
991 int fd_current, fd_target, rc, dev_target;
992
993 /* dev_current: fd_current, erase_current */
994 fd_current = open (DEVNAME (dev_current), mode);
995 if (fd_current < 0) {
996 fprintf (stderr,
997 "Can't open %s: %s\n",
998 DEVNAME (dev_current), strerror (errno));
999 return -1;
1000 }
1001
1002 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +00001003 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001004 /* switch to next partition for writing */
1005 dev_target = !dev_current;
1006 /* dev_target: fd_target, erase_target */
1007 fd_target = open (DEVNAME (dev_target), mode);
1008 if (fd_target < 0) {
wdenke7f34c62003-01-11 09:48:40 +00001009 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001010 "Can't open %s: %s\n",
1011 DEVNAME (dev_target),
1012 strerror (errno));
1013 rc = -1;
1014 goto exit;
wdenke7f34c62003-01-11 09:48:40 +00001015 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001016 } else {
1017 dev_target = dev_current;
1018 fd_target = fd_current;
wdenk34b613e2002-12-17 01:51:00 +00001019 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001020
1021 rc = flash_write (fd_current, fd_target, dev_target);
1022
wdenke7f34c62003-01-11 09:48:40 +00001023 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001024 if (close (fd_target)) {
wdenke7f34c62003-01-11 09:48:40 +00001025 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001026 "I/O error on %s: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001027 DEVNAME (dev_target),
wdenk3f9ab982003-04-12 23:38:12 +00001028 strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001029 rc = -1;
wdenke7f34c62003-01-11 09:48:40 +00001030 }
wdenk34b613e2002-12-17 01:51:00 +00001031 }
wdenk34b613e2002-12-17 01:51:00 +00001032 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001033 rc = flash_read (fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001034 }
1035
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001036exit:
1037 if (close (fd_current)) {
wdenk34b613e2002-12-17 01:51:00 +00001038 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001039 "I/O error on %s: %s\n",
1040 DEVNAME (dev_current), strerror (errno));
1041 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001042 }
1043
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001044 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001045}
1046
1047/*
1048 * s1 is either a simple 'name', or a 'name=value' pair.
1049 * s2 is a 'name=value' pair.
1050 * If the names match, return the value of s2, else NULL.
1051 */
1052
Markus Klotzbücher94db5572007-11-27 10:23:20 +01001053static char *envmatch (char * s1, char * s2)
wdenk34b613e2002-12-17 01:51:00 +00001054{
1055
1056 while (*s1 == *s2++)
1057 if (*s1++ == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001058 return s2;
wdenk3f9ab982003-04-12 23:38:12 +00001059 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001060 return s2;
1061 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001062}
1063
1064/*
1065 * Prevent confusion if running from erased flash memory
1066 */
Stefano Babicada628b2010-05-24 12:08:16 +02001067int fw_env_open(void)
wdenk34b613e2002-12-17 01:51:00 +00001068{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001069 int crc0, crc0_ok;
1070 char flag0;
1071 void *addr0;
1072
wdenk34b613e2002-12-17 01:51:00 +00001073 int crc1, crc1_ok;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001074 char flag1;
1075 void *addr1;
wdenke7f34c62003-01-11 09:48:40 +00001076
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001077 struct env_image_single *single;
1078 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001079
wdenk3f9ab982003-04-12 23:38:12 +00001080 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001081 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001082
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +02001083 addr0 = calloc (1, CONFIG_ENV_SIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001084 if (addr0 == NULL) {
wdenk3f9ab982003-04-12 23:38:12 +00001085 fprintf (stderr,
1086 "Not enough memory for environment (%ld bytes)\n",
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +02001087 CONFIG_ENV_SIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001088 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001089 }
wdenk3f9ab982003-04-12 23:38:12 +00001090
wdenke7f34c62003-01-11 09:48:40 +00001091 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001092 environment.image = addr0;
1093
1094 if (HaveRedundEnv) {
1095 redundant = addr0;
1096 environment.crc = &redundant->crc;
1097 environment.flags = &redundant->flags;
1098 environment.data = redundant->data;
1099 } else {
1100 single = addr0;
1101 environment.crc = &single->crc;
1102 environment.flags = NULL;
1103 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001104 }
wdenk3f9ab982003-04-12 23:38:12 +00001105
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001106 dev_current = 0;
1107 if (flash_io (O_RDONLY))
1108 return -1;
1109
1110 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1111 crc0_ok = (crc0 == *environment.crc);
wdenke7f34c62003-01-11 09:48:40 +00001112 if (!HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001113 if (!crc0_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001114 fprintf (stderr,
1115 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkc5203f92006-03-12 23:27:46 +01001116 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk34b613e2002-12-17 01:51:00 +00001117 }
wdenke7f34c62003-01-11 09:48:40 +00001118 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001119 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001120
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001121 dev_current = 1;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +02001122 addr1 = calloc (1, CONFIG_ENV_SIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001123 if (addr1 == NULL) {
wdenk3f9ab982003-04-12 23:38:12 +00001124 fprintf (stderr,
1125 "Not enough memory for environment (%ld bytes)\n",
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +02001126 CONFIG_ENV_SIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001127 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001128 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001129 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001130
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001131 /*
1132 * have to set environment.image for flash_read(), careful -
1133 * other pointers in environment still point inside addr0
1134 */
1135 environment.image = addr1;
1136 if (flash_io (O_RDONLY))
1137 return -1;
1138
1139 /* Check flag scheme compatibility */
1140 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1141 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1142 environment.flag_scheme = FLAG_BOOLEAN;
1143 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1144 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1145 environment.flag_scheme = FLAG_INCREMENTAL;
1146 } else {
1147 fprintf (stderr, "Incompatible flash types!\n");
1148 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001149 }
wdenk3f9ab982003-04-12 23:38:12 +00001150
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001151 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1152 crc1_ok = (crc1 == redundant->crc);
1153 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001154
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001155 if (crc0_ok && !crc1_ok) {
1156 dev_current = 0;
1157 } else if (!crc0_ok && crc1_ok) {
1158 dev_current = 1;
1159 } else if (!crc0_ok && !crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001160 fprintf (stderr,
1161 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001162 memcpy (environment.data, default_environment,
1163 sizeof default_environment);
1164 dev_current = 0;
1165 } else {
1166 switch (environment.flag_scheme) {
1167 case FLAG_BOOLEAN:
1168 if (flag0 == active_flag &&
1169 flag1 == obsolete_flag) {
1170 dev_current = 0;
1171 } else if (flag0 == obsolete_flag &&
1172 flag1 == active_flag) {
1173 dev_current = 1;
1174 } else if (flag0 == flag1) {
1175 dev_current = 0;
1176 } else if (flag0 == 0xFF) {
1177 dev_current = 0;
1178 } else if (flag1 == 0xFF) {
1179 dev_current = 1;
1180 } else {
1181 dev_current = 0;
1182 }
1183 break;
1184 case FLAG_INCREMENTAL:
1185 if ((flag0 == 255 && flag1 == 0) ||
1186 flag1 > flag0)
1187 dev_current = 1;
1188 else if ((flag1 == 255 && flag0 == 0) ||
1189 flag0 > flag1)
1190 dev_current = 0;
1191 else /* flags are equal - almost impossible */
1192 dev_current = 0;
1193 break;
1194 default:
1195 fprintf (stderr, "Unknown flag scheme %u \n",
1196 environment.flag_scheme);
1197 return -1;
1198 }
1199 }
1200
1201 /*
1202 * If we are reading, we don't need the flag and the CRC any
1203 * more, if we are writing, we will re-calculate CRC and update
1204 * flags before writing out
1205 */
1206 if (dev_current) {
1207 environment.image = addr1;
1208 environment.crc = &redundant->crc;
1209 environment.flags = &redundant->flags;
1210 environment.data = redundant->data;
1211 free (addr0);
1212 } else {
1213 environment.image = addr0;
1214 /* Other pointers are already set */
wdenk3f9ab982003-04-12 23:38:12 +00001215 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +00001216 }
wdenk34b613e2002-12-17 01:51:00 +00001217 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001218 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001219}
1220
1221
wdenk3f9ab982003-04-12 23:38:12 +00001222static int parse_config ()
wdenk34b613e2002-12-17 01:51:00 +00001223{
1224 struct stat st;
1225
wdenke7f34c62003-01-11 09:48:40 +00001226#if defined(CONFIG_FILE)
1227 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
wdenk3f9ab982003-04-12 23:38:12 +00001228 if (get_config (CONFIG_FILE)) {
wdenke7f34c62003-01-11 09:48:40 +00001229 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001230 "Cannot parse config file: %s\n", strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001231 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001232 }
wdenke7f34c62003-01-11 09:48:40 +00001233#else
wdenk3f9ab982003-04-12 23:38:12 +00001234 strcpy (DEVNAME (0), DEVICE1_NAME);
1235 DEVOFFSET (0) = DEVICE1_OFFSET;
1236 ENVSIZE (0) = ENV1_SIZE;
1237 DEVESIZE (0) = DEVICE1_ESIZE;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001238 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
wdenk34b613e2002-12-17 01:51:00 +00001239#ifdef HAVE_REDUND
wdenk3f9ab982003-04-12 23:38:12 +00001240 strcpy (DEVNAME (1), DEVICE2_NAME);
1241 DEVOFFSET (1) = DEVICE2_OFFSET;
1242 ENVSIZE (1) = ENV2_SIZE;
1243 DEVESIZE (1) = DEVICE2_ESIZE;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001244 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
wdenke7f34c62003-01-11 09:48:40 +00001245 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +00001246#endif
wdenke7f34c62003-01-11 09:48:40 +00001247#endif
wdenk3f9ab982003-04-12 23:38:12 +00001248 if (stat (DEVNAME (0), &st)) {
1249 fprintf (stderr,
1250 "Cannot access MTD device %s: %s\n",
1251 DEVNAME (0), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001252 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001253 }
wdenk3f9ab982003-04-12 23:38:12 +00001254
1255 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1256 fprintf (stderr,
1257 "Cannot access MTD device %s: %s\n",
Wolfgang Denk8e6213c2005-09-25 01:20:38 +02001258 DEVNAME (1), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001259 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001260 }
wdenk34b613e2002-12-17 01:51:00 +00001261 return 0;
1262}
wdenke7f34c62003-01-11 09:48:40 +00001263
1264#if defined(CONFIG_FILE)
1265static int get_config (char *fname)
1266{
1267 FILE *fp;
1268 int i = 0;
1269 int rc;
1270 char dump[128];
1271
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001272 fp = fopen (fname, "r");
1273 if (fp == NULL)
1274 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001275
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001276 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenke7f34c62003-01-11 09:48:40 +00001277 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001278 if (dump[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001279 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001280
1281 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1282 DEVNAME (i),
1283 &DEVOFFSET (i),
1284 &ENVSIZE (i),
1285 &DEVESIZE (i),
1286 &ENVSECTORS (i));
1287
1288 if (rc < 4)
1289 continue;
1290
1291 if (rc < 5)
1292 /* Default - 1 sector */
1293 ENVSECTORS (i) = 1;
wdenke7f34c62003-01-11 09:48:40 +00001294
1295 i++;
1296 }
wdenk3f9ab982003-04-12 23:38:12 +00001297 fclose (fp);
1298
wdenke7f34c62003-01-11 09:48:40 +00001299 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +00001300 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001301 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001302 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001303 } else
1304 return 0;
1305}
1306#endif