blob: ccaa8896318ef2ac93cc1f21a04bfc4d9c49b96f [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
Joe Hershberger671adc42012-10-03 09:38:46 +000082#define CUR_ENVSIZE 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
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100122static char default_environment[] = {
wdenke7f34c62003-01-11 09:48:40 +0000123#if defined(CONFIG_BOOTARGS)
wdenk3f9ab982003-04-12 23:38:12 +0000124 "bootargs=" CONFIG_BOOTARGS "\0"
wdenk34b613e2002-12-17 01:51:00 +0000125#endif
wdenke7f34c62003-01-11 09:48:40 +0000126#if defined(CONFIG_BOOTCOMMAND)
wdenk3f9ab982003-04-12 23:38:12 +0000127 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
wdenk34b613e2002-12-17 01:51:00 +0000128#endif
wdenke7f34c62003-01-11 09:48:40 +0000129#if defined(CONFIG_RAMBOOTCOMMAND)
wdenk3f9ab982003-04-12 23:38:12 +0000130 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
wdenke7f34c62003-01-11 09:48:40 +0000131#endif
132#if defined(CONFIG_NFSBOOTCOMMAND)
wdenk3f9ab982003-04-12 23:38:12 +0000133 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
wdenke7f34c62003-01-11 09:48:40 +0000134#endif
135#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
Marek Vasut0b3176c2012-09-23 17:41:24 +0200136 "bootdelay=" __stringify(CONFIG_BOOTDELAY) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000137#endif
wdenke7f34c62003-01-11 09:48:40 +0000138#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
Marek Vasut0b3176c2012-09-23 17:41:24 +0200139 "baudrate=" __stringify(CONFIG_BAUDRATE) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000140#endif
wdenke7f34c62003-01-11 09:48:40 +0000141#ifdef CONFIG_LOADS_ECHO
Marek Vasut0b3176c2012-09-23 17:41:24 +0200142 "loads_echo=" __stringify(CONFIG_LOADS_ECHO) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000143#endif
wdenk34b613e2002-12-17 01:51:00 +0000144#ifdef CONFIG_ETHADDR
Marek Vasut0b3176c2012-09-23 17:41:24 +0200145 "ethaddr=" __stringify(CONFIG_ETHADDR) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000146#endif
wdenke7f34c62003-01-11 09:48:40 +0000147#ifdef CONFIG_ETH1ADDR
Marek Vasut0b3176c2012-09-23 17:41:24 +0200148 "eth1addr=" __stringify(CONFIG_ETH1ADDR) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000149#endif
150#ifdef CONFIG_ETH2ADDR
Marek Vasut0b3176c2012-09-23 17:41:24 +0200151 "eth2addr=" __stringify(CONFIG_ETH2ADDR) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000152#endif
wdenk54070ab2004-12-31 09:32:47 +0000153#ifdef CONFIG_ETH3ADDR
Marek Vasut0b3176c2012-09-23 17:41:24 +0200154 "eth3addr=" __stringify(CONFIG_ETH3ADDR) "\0"
wdenk54070ab2004-12-31 09:32:47 +0000155#endif
richardretanubune5167f12008-09-29 18:28:23 -0400156#ifdef CONFIG_ETH4ADDR
Marek Vasut0b3176c2012-09-23 17:41:24 +0200157 "eth4addr=" __stringify(CONFIG_ETH4ADDR) "\0"
richardretanubune5167f12008-09-29 18:28:23 -0400158#endif
159#ifdef CONFIG_ETH5ADDR
Marek Vasut0b3176c2012-09-23 17:41:24 +0200160 "eth5addr=" __stringify(CONFIG_ETH5ADDR) "\0"
richardretanubune5167f12008-09-29 18:28:23 -0400161#endif
wdenke7f34c62003-01-11 09:48:40 +0000162#ifdef CONFIG_ETHPRIME
wdenk3f9ab982003-04-12 23:38:12 +0000163 "ethprime=" CONFIG_ETHPRIME "\0"
wdenke7f34c62003-01-11 09:48:40 +0000164#endif
wdenk34b613e2002-12-17 01:51:00 +0000165#ifdef CONFIG_IPADDR
Marek Vasut0b3176c2012-09-23 17:41:24 +0200166 "ipaddr=" __stringify(CONFIG_IPADDR) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000167#endif
168#ifdef CONFIG_SERVERIP
Marek Vasut0b3176c2012-09-23 17:41:24 +0200169 "serverip=" __stringify(CONFIG_SERVERIP) "\0"
wdenk34b613e2002-12-17 01:51:00 +0000170#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200171#ifdef CONFIG_SYS_AUTOLOAD
172 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
wdenke7f34c62003-01-11 09:48:40 +0000173#endif
174#ifdef CONFIG_ROOTPATH
Joe Hershberger257ff782011-10-13 13:03:47 +0000175 "rootpath=" CONFIG_ROOTPATH "\0"
wdenke7f34c62003-01-11 09:48:40 +0000176#endif
177#ifdef CONFIG_GATEWAYIP
Marek Vasut0b3176c2012-09-23 17:41:24 +0200178 "gatewayip=" __stringify(CONFIG_GATEWAYIP) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000179#endif
180#ifdef CONFIG_NETMASK
Marek Vasut0b3176c2012-09-23 17:41:24 +0200181 "netmask=" __stringify(CONFIG_NETMASK) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000182#endif
183#ifdef CONFIG_HOSTNAME
Marek Vasut0b3176c2012-09-23 17:41:24 +0200184 "hostname=" __stringify(CONFIG_HOSTNAME) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000185#endif
186#ifdef CONFIG_BOOTFILE
Joe Hershbergere4da2482011-10-13 13:03:48 +0000187 "bootfile=" CONFIG_BOOTFILE "\0"
wdenke7f34c62003-01-11 09:48:40 +0000188#endif
189#ifdef CONFIG_LOADADDR
Marek Vasut0b3176c2012-09-23 17:41:24 +0200190 "loadaddr=" __stringify(CONFIG_LOADADDR) "\0"
wdenke7f34c62003-01-11 09:48:40 +0000191#endif
192#ifdef CONFIG_PREBOOT
wdenk3f9ab982003-04-12 23:38:12 +0000193 "preboot=" CONFIG_PREBOOT "\0"
wdenke7f34c62003-01-11 09:48:40 +0000194#endif
195#ifdef CONFIG_CLOCKS_IN_MHZ
wdenk3f9ab982003-04-12 23:38:12 +0000196 "clocks_in_mhz=" "1" "\0"
wdenke7f34c62003-01-11 09:48:40 +0000197#endif
stroesef5dd4102003-02-14 11:21:23 +0000198#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
Marek Vasut0b3176c2012-09-23 17:41:24 +0200199 "pcidelay=" __stringify(CONFIG_PCI_BOOTDELAY) "\0"
stroesef5dd4102003-02-14 11:21:23 +0000200#endif
Benoît Thébaudeaueb6aaeb2012-08-10 07:45:15 +0000201#ifdef CONFIG_ENV_VARS_UBOOT_CONFIG
202 "arch=" CONFIG_SYS_ARCH "\0"
203 "cpu=" CONFIG_SYS_CPU "\0"
204 "board=" CONFIG_SYS_BOARD "\0"
205#ifdef CONFIG_SYS_VENDOR
206 "vendor=" CONFIG_SYS_VENDOR "\0"
207#endif
208#ifdef CONFIG_SYS_SOC
209 "soc=" CONFIG_SYS_SOC "\0"
210#endif
211#endif
wdenke7f34c62003-01-11 09:48:40 +0000212#ifdef CONFIG_EXTRA_ENV_SETTINGS
213 CONFIG_EXTRA_ENV_SETTINGS
214#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200215 "\0" /* Termimate struct environment data with 2 NULs */
wdenk34b613e2002-12-17 01:51:00 +0000216};
217
wdenk3f9ab982003-04-12 23:38:12 +0000218static int flash_io (int mode);
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100219static char *envmatch (char * s1, char * s2);
wdenk3f9ab982003-04-12 23:38:12 +0000220static int parse_config (void);
221
wdenke7f34c62003-01-11 09:48:40 +0000222#if defined(CONFIG_FILE)
wdenk3f9ab982003-04-12 23:38:12 +0000223static int get_config (char *);
wdenke7f34c62003-01-11 09:48:40 +0000224#endif
wdenk3f9ab982003-04-12 23:38:12 +0000225static inline ulong getenvsize (void)
wdenke7f34c62003-01-11 09:48:40 +0000226{
Joe Hershberger671adc42012-10-03 09:38:46 +0000227 ulong rc = CUR_ENVSIZE - sizeof(long);
wdenk3f9ab982003-04-12 23:38:12 +0000228
wdenke7f34c62003-01-11 09:48:40 +0000229 if (HaveRedundEnv)
wdenk3f9ab982003-04-12 23:38:12 +0000230 rc -= sizeof (char);
wdenke7f34c62003-01-11 09:48:40 +0000231 return rc;
232}
wdenk34b613e2002-12-17 01:51:00 +0000233
Stefano Babicada628b2010-05-24 12:08:16 +0200234static char *fw_string_blank(char *s, int noblank)
235{
236 int i;
237 int len = strlen(s);
238
239 for (i = 0; i < len; i++, s++) {
240 if ((noblank && !WHITESPACE(*s)) ||
241 (!noblank && WHITESPACE(*s)))
242 break;
243 }
244 if (i == len)
245 return NULL;
246
247 return s;
248}
249
wdenk34b613e2002-12-17 01:51:00 +0000250/*
251 * Search the environment for a variable.
252 * Return the value, if found, or NULL, if not found.
253 */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100254char *fw_getenv (char *name)
wdenk34b613e2002-12-17 01:51:00 +0000255{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100256 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000257
Stefano Babicada628b2010-05-24 12:08:16 +0200258 if (fw_env_open())
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200259 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000260
wdenk3f9ab982003-04-12 23:38:12 +0000261 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100262 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000263
wdenk3f9ab982003-04-12 23:38:12 +0000264 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000265 if (nxt >= &environment.data[ENV_SIZE]) {
266 fprintf (stderr, "## Error: "
267 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200268 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000269 }
270 }
wdenk3f9ab982003-04-12 23:38:12 +0000271 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000272 if (!val)
273 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200274 return val;
wdenk34b613e2002-12-17 01:51:00 +0000275 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200276 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000277}
278
279/*
280 * Print the current definition of one, or more, or all
281 * environment variables
282 */
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700283int fw_printenv (int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000284{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100285 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000286 int i, n_flag;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700287 int rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000288
Stefano Babicada628b2010-05-24 12:08:16 +0200289 if (fw_env_open())
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200290 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000291
wdenk3f9ab982003-04-12 23:38:12 +0000292 if (argc == 1) { /* Print all env variables */
293 for (env = environment.data; *env; env = nxt + 1) {
294 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000295 if (nxt >= &environment.data[ENV_SIZE]) {
296 fprintf (stderr, "## Error: "
297 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200298 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000299 }
300 }
301
wdenk3f9ab982003-04-12 23:38:12 +0000302 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000303 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200304 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000305 }
306
wdenk3f9ab982003-04-12 23:38:12 +0000307 if (strcmp (argv[1], "-n") == 0) {
wdenk34b613e2002-12-17 01:51:00 +0000308 n_flag = 1;
309 ++argv;
310 --argc;
311 if (argc != 2) {
312 fprintf (stderr, "## Error: "
313 "`-n' option requires exactly one argument\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200314 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000315 }
316 } else {
317 n_flag = 0;
318 }
319
wdenk3f9ab982003-04-12 23:38:12 +0000320 for (i = 1; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100321 char *name = argv[i];
322 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000323
wdenk3f9ab982003-04-12 23:38:12 +0000324 for (env = environment.data; *env; env = nxt + 1) {
wdenk34b613e2002-12-17 01:51:00 +0000325
wdenk3f9ab982003-04-12 23:38:12 +0000326 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000327 if (nxt >= &environment.data[ENV_SIZE]) {
328 fprintf (stderr, "## Error: "
329 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200330 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000331 }
332 }
wdenk3f9ab982003-04-12 23:38:12 +0000333 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000334 if (val) {
335 if (!n_flag) {
336 fputs (name, stdout);
wdenk3f9ab982003-04-12 23:38:12 +0000337 putc ('=', stdout);
wdenk34b613e2002-12-17 01:51:00 +0000338 }
wdenk3f9ab982003-04-12 23:38:12 +0000339 puts (val);
wdenk34b613e2002-12-17 01:51:00 +0000340 break;
341 }
342 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700343 if (!val) {
wdenk3f9ab982003-04-12 23:38:12 +0000344 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700345 rc = -1;
346 }
wdenk34b613e2002-12-17 01:51:00 +0000347 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700348
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200349 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000350}
351
Stefano Babicada628b2010-05-24 12:08:16 +0200352int fw_env_close(void)
wdenk34b613e2002-12-17 01:51:00 +0000353{
Stefano Babicada628b2010-05-24 12:08:16 +0200354 /*
355 * Update CRC
356 */
357 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000358
Stefano Babicada628b2010-05-24 12:08:16 +0200359 /* write environment back to flash */
360 if (flash_io(O_RDWR)) {
361 fprintf(stderr,
362 "Error: can't write fw_env to flash\n");
363 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000364 }
365
Stefano Babicada628b2010-05-24 12:08:16 +0200366 return 0;
367}
wdenk34b613e2002-12-17 01:51:00 +0000368
Stefano Babicada628b2010-05-24 12:08:16 +0200369
370/*
371 * Set/Clear a single variable in the environment.
372 * This is called in sequence to update the environment
373 * in RAM without updating the copy in flash after each set
374 */
375int fw_env_write(char *name, char *value)
376{
377 int len;
378 char *env, *nxt;
379 char *oldval = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000380
381 /*
382 * search if variable with this name already exists
383 */
wdenk3f9ab982003-04-12 23:38:12 +0000384 for (nxt = env = environment.data; *env; env = nxt + 1) {
385 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000386 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200387 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000388 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200389 errno = EINVAL;
390 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000391 }
392 }
wdenk3f9ab982003-04-12 23:38:12 +0000393 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000394 break;
395 }
396
397 /*
398 * Delete any existing definition
399 */
400 if (oldval) {
Grant Erickson528178a2011-12-22 08:59:55 +0000401#ifndef CONFIG_ENV_OVERWRITE
wdenk34b613e2002-12-17 01:51:00 +0000402 /*
403 * Ethernet Address and serial# can be set only once
404 */
Grant Erickson528178a2011-12-22 08:59:55 +0000405 if (
406 (strcmp(name, "serial#") == 0) ||
407 ((strcmp(name, "ethaddr") == 0)
408#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
Marek Vasut0b3176c2012-09-23 17:41:24 +0200409 && (strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0)
Grant Erickson528178a2011-12-22 08:59:55 +0000410#endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
411 ) ) {
wdenk34b613e2002-12-17 01:51:00 +0000412 fprintf (stderr, "Can't overwrite \"%s\"\n", name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200413 errno = EROFS;
414 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000415 }
Grant Erickson528178a2011-12-22 08:59:55 +0000416#endif /* CONFIG_ENV_OVERWRITE */
wdenk34b613e2002-12-17 01:51:00 +0000417
418 if (*++nxt == '\0') {
419 *env = '\0';
420 } else {
421 for (;;) {
422 *env = *nxt++;
423 if ((*env == '\0') && (*nxt == '\0'))
424 break;
425 ++env;
426 }
427 }
428 *++env = '\0';
429 }
430
431 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200432 if (!value || !strlen(value))
433 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000434
435 /*
436 * Append new definition at the end
437 */
wdenk3f9ab982003-04-12 23:38:12 +0000438 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000439 if (env > environment.data)
440 ++env;
441 /*
442 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000443 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000444 */
wdenk3f9ab982003-04-12 23:38:12 +0000445 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000446 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200447 len += strlen(value) + 1;
448
wdenk3f9ab982003-04-12 23:38:12 +0000449 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000450 fprintf (stderr,
451 "Error: environment overflow, \"%s\" deleted\n",
452 name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200453 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000454 }
Stefano Babicada628b2010-05-24 12:08:16 +0200455
wdenk34b613e2002-12-17 01:51:00 +0000456 while ((*env = *name++) != '\0')
457 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200458 *env = '=';
459 while ((*++env = *value++) != '\0')
460 ;
461
462 /* end is marked with double '\0' */
463 *++env = '\0';
464
465 return 0;
466}
467
468/*
469 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
470 * 0 - OK
471 * EINVAL - need at least 1 argument
472 * EROFS - certain variables ("ethaddr", "serial#") cannot be
473 * modified or deleted
474 *
475 */
476int fw_setenv(int argc, char *argv[])
477{
478 int i, len;
479 char *name;
480 char *value = NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200481
482 if (argc < 2) {
483 errno = EINVAL;
484 return -1;
485 }
486
487 if (fw_env_open()) {
488 fprintf(stderr, "Error: environment not initialized\n");
489 return -1;
490 }
491
492 name = argv[1];
493
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000494 len = 0;
wdenk3f9ab982003-04-12 23:38:12 +0000495 for (i = 2; i < argc; ++i) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100496 char *val = argv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000497 size_t val_len = strlen(val);
498
499 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200500 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000501 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200502 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000503 len, strerror(errno));
504 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200505 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000506
507 memcpy(value + len, val, val_len);
508 len += val_len;
509 value[len++] = ' ';
wdenk34b613e2002-12-17 01:51:00 +0000510 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000511 value[len - 1] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000512
Stefano Babicada628b2010-05-24 12:08:16 +0200513 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000514
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000515 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000516
Stefano Babicada628b2010-05-24 12:08:16 +0200517 return fw_env_close();
518}
wdenk34b613e2002-12-17 01:51:00 +0000519
Stefano Babicada628b2010-05-24 12:08:16 +0200520/*
521 * Parse a file and configure the u-boot variables.
522 * The script file has a very simple format, as follows:
523 *
524 * Each line has a couple with name, value:
525 * <white spaces>variable_name<white spaces>variable_value
526 *
527 * Both variable_name and variable_value are interpreted as strings.
528 * Any character after <white spaces> and before ending \r\n is interpreted
529 * as variable's value (no comment allowed on these lines !)
530 *
531 * Comments are allowed if the first character in the line is #
532 *
533 * Returns -1 and sets errno error codes:
534 * 0 - OK
535 * -1 - Error
536 */
537int fw_parse_script(char *fname)
538{
539 FILE *fp;
540 char dump[1024]; /* Maximum line length in the file */
541 char *name;
542 char *val;
543 int lineno = 0;
544 int len;
545 int ret = 0;
546
547 if (fw_env_open()) {
548 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200549 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000550 }
551
Stefano Babicada628b2010-05-24 12:08:16 +0200552 if (strcmp(fname, "-") == 0)
553 fp = stdin;
554 else {
555 fp = fopen(fname, "r");
556 if (fp == NULL) {
557 fprintf(stderr, "I cannot open %s for reading\n",
558 fname);
559 return -1;
560 }
561 }
562
563 while (fgets(dump, sizeof(dump), fp)) {
564 lineno++;
565 len = strlen(dump);
566
567 /*
568 * Read a whole line from the file. If the line is too long
569 * or is not terminated, reports an error and exit.
570 */
571 if (dump[len - 1] != '\n') {
572 fprintf(stderr,
573 "Line %d not corrected terminated or too long\n",
574 lineno);
575 ret = -1;
576 break;
577 }
578
579 /* Drop ending line feed / carriage return */
580 while (len > 0 && (dump[len - 1] == '\n' ||
581 dump[len - 1] == '\r')) {
582 dump[len - 1] = '\0';
583 len--;
584 }
585
586 /* Skip comment or empty lines */
587 if ((len == 0) || dump[0] == '#')
588 continue;
589
590 /*
591 * Search for variable's name,
592 * remove leading whitespaces
593 */
594 name = fw_string_blank(dump, 1);
595 if (!name)
596 continue;
597
598 /* The first white space is the end of variable name */
599 val = fw_string_blank(name, 0);
600 len = strlen(name);
601 if (val) {
602 *val++ = '\0';
603 if ((val - name) < len)
604 val = fw_string_blank(val, 1);
605 else
606 val = NULL;
607 }
608
609#ifdef DEBUG
610 fprintf(stderr, "Setting %s : %s\n",
611 name, val ? val : " removed");
612#endif
613
614 /*
615 * If there is an error setting a variable,
616 * try to save the environment and returns an error
617 */
618 if (fw_env_write(name, val)) {
619 fprintf(stderr,
620 "fw_env_write returns with error : %s\n",
621 strerror(errno));
622 ret = -1;
623 break;
624 }
625
626 }
627
628 /* Close file if not stdin */
629 if (strcmp(fname, "-") != 0)
630 fclose(fp);
631
632 ret |= fw_env_close();
633
634 return ret;
635
wdenk34b613e2002-12-17 01:51:00 +0000636}
637
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200638/*
639 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
640 * 0 - block is good
641 * > 0 - block is bad
642 * < 0 - failed to test
643 */
644static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000645{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200646 if (mtd_type == MTD_NANDFLASH) {
647 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000648
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200649 if (badblock < 0) {
650 perror ("Cannot read bad block mark");
651 return badblock;
652 }
653
654 if (badblock) {
655#ifdef DEBUG
656 fprintf (stderr, "Bad block at 0x%llx, "
657 "skipping\n", *blockstart);
658#endif
659 return badblock;
660 }
wdenk34b613e2002-12-17 01:51:00 +0000661 }
662
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200663 return 0;
664}
665
666/*
667 * Read data from flash at an offset into a provided buffer. On NAND it skips
668 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
669 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
670 */
671static int flash_read_buf (int dev, int fd, void *buf, size_t count,
672 off_t offset, uint8_t mtd_type)
673{
674 size_t blocklen; /* erase / write length - one block on NAND,
675 0 on NOR */
676 size_t processed = 0; /* progress counter */
677 size_t readlen = count; /* current read length */
678 off_t top_of_range; /* end of the last block we may use */
679 off_t block_seek; /* offset inside the current block to the start
680 of the data */
681 loff_t blockstart; /* running start of the current block -
682 MEMGETBADBLOCK needs 64 bits */
683 int rc;
684
Remy Bohmercb969eb2011-02-12 19:06:26 +0100685 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200686
687 /* Offset inside a block */
688 block_seek = offset - blockstart;
689
690 if (mtd_type == MTD_NANDFLASH) {
691 /*
692 * NAND: calculate which blocks we are reading. We have
693 * to read one block at a time to skip bad blocks.
694 */
695 blocklen = DEVESIZE (dev);
696
697 /*
698 * To calculate the top of the range, we have to use the
699 * global DEVOFFSET (dev), which can be different from offset
700 */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100701 top_of_range = ((DEVOFFSET(dev) / blocklen) +
702 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200703
704 /* Limit to one block for the first read */
705 if (readlen > blocklen - block_seek)
706 readlen = blocklen - block_seek;
707 } else {
708 blocklen = 0;
709 top_of_range = offset + count;
wdenke7f34c62003-01-11 09:48:40 +0000710 }
wdenk34b613e2002-12-17 01:51:00 +0000711
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200712 /* This only runs once on NOR flash */
713 while (processed < count) {
714 rc = flash_bad_block (fd, mtd_type, &blockstart);
715 if (rc < 0) /* block test failed */
716 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000717
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200718 if (blockstart + block_seek + readlen > top_of_range) {
719 /* End of range is reached */
720 fprintf (stderr,
721 "Too few good blocks within range\n");
722 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000723 }
724
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200725 if (rc) { /* block is bad */
726 blockstart += blocklen;
727 continue;
wdenk34b613e2002-12-17 01:51:00 +0000728 }
729
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200730 /*
731 * If a block is bad, we retry in the next block at the same
732 * offset - see common/env_nand.c::writeenv()
733 */
734 lseek (fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000735
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200736 rc = read (fd, buf + processed, readlen);
737 if (rc != readlen) {
738 fprintf (stderr, "Read error on %s: %s\n",
739 DEVNAME (dev), strerror (errno));
740 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000741 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200742#ifdef DEBUG
743 fprintf (stderr, "Read 0x%x bytes at 0x%llx\n",
744 rc, blockstart + block_seek);
745#endif
746 processed += readlen;
747 readlen = min (blocklen, count - processed);
748 block_seek = 0;
749 blockstart += blocklen;
750 }
751
752 return processed;
753}
754
755/*
Remy Bohmercb969eb2011-02-12 19:06:26 +0100756 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
757 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
758 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200759 */
760static int flash_write_buf (int dev, int fd, void *buf, size_t count,
761 off_t offset, uint8_t mtd_type)
762{
763 void *data;
764 struct erase_info_user erase;
765 size_t blocklen; /* length of NAND block / NOR erase sector */
766 size_t erase_len; /* whole area that can be erased - may include
767 bad blocks */
768 size_t erasesize; /* erase / write length - one block on NAND,
769 whole area on NOR */
770 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100771 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200772 bad blocks */
773 off_t erase_offset; /* offset to the first erase block (aligned)
774 below offset */
775 off_t block_seek; /* offset inside the erase block to the start
776 of the data */
777 off_t top_of_range; /* end of the last block we may use */
778 loff_t blockstart; /* running start of the current block -
779 MEMGETBADBLOCK needs 64 bits */
780 int rc;
781
782 blocklen = DEVESIZE (dev);
783
Remy Bohmercb969eb2011-02-12 19:06:26 +0100784 top_of_range = ((DEVOFFSET(dev) / blocklen) +
785 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200786
Remy Bohmercb969eb2011-02-12 19:06:26 +0100787 erase_offset = (offset / blocklen) * blocklen;
wdenk34b613e2002-12-17 01:51:00 +0000788
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200789 /* Maximum area we may use */
790 erase_len = top_of_range - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +0000791
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200792 blockstart = erase_offset;
793 /* Offset inside a block */
794 block_seek = offset - erase_offset;
795
796 /*
797 * Data size we actually have to write: from the start of the block
798 * to the start of the data, then count bytes of data, and to the
799 * end of the block
800 */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100801 write_total = ((block_seek + count + blocklen - 1) /
802 blocklen) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200803
804 /*
805 * Support data anywhere within erase sectors: read out the complete
806 * area to be erased, replace the environment image, write the whole
807 * block back again.
808 */
809 if (write_total > count) {
810 data = malloc (erase_len);
811 if (!data) {
wdenke7f34c62003-01-11 09:48:40 +0000812 fprintf (stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200813 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200814 erase_len, strerror (errno));
815 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000816 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200817
818 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
819 mtd_type);
820 if (write_total != rc)
821 return -1;
822
823 /* Overwrite the old environment */
824 memcpy (data + block_seek, buf, count);
825 } else {
826 /*
827 * We get here, iff offset is block-aligned and count is a
828 * multiple of blocklen - see write_total calculation above
829 */
830 data = buf;
831 }
832
833 if (mtd_type == MTD_NANDFLASH) {
834 /*
835 * NAND: calculate which blocks we are writing. We have
836 * to write one block at a time to skip bad blocks.
837 */
838 erasesize = blocklen;
839 } else {
840 erasesize = erase_len;
841 }
842
843 erase.length = erasesize;
844
Remy Bohmercb969eb2011-02-12 19:06:26 +0100845 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200846 while (processed < write_total) {
847 rc = flash_bad_block (fd, mtd_type, &blockstart);
848 if (rc < 0) /* block test failed */
849 return rc;
850
851 if (blockstart + erasesize > top_of_range) {
852 fprintf (stderr, "End of range reached, aborting\n");
853 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000854 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200855
856 if (rc) { /* block is bad */
857 blockstart += blocklen;
858 continue;
859 }
860
861 erase.start = blockstart;
862 ioctl (fd, MEMUNLOCK, &erase);
863
Remy Bohmercb969eb2011-02-12 19:06:26 +0100864 /* Dataflash does not need an explicit erase cycle */
865 if (mtd_type != MTD_DATAFLASH)
866 if (ioctl (fd, MEMERASE, &erase) != 0) {
867 fprintf (stderr, "MTD erase error on %s: %s\n",
868 DEVNAME (dev),
869 strerror (errno));
870 return -1;
871 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200872
873 if (lseek (fd, blockstart, SEEK_SET) == -1) {
wdenk34b613e2002-12-17 01:51:00 +0000874 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200875 "Seek error on %s: %s\n",
876 DEVNAME (dev), strerror (errno));
877 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000878 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200879
880#ifdef DEBUG
881 printf ("Write 0x%x bytes at 0x%llx\n", erasesize, blockstart);
882#endif
883 if (write (fd, data + processed, erasesize) != erasesize) {
884 fprintf (stderr, "Write error on %s: %s\n",
885 DEVNAME (dev), strerror (errno));
886 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000887 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200888
889 ioctl (fd, MEMLOCK, &erase);
890
891 processed += blocklen;
892 block_seek = 0;
893 blockstart += blocklen;
894 }
895
896 if (write_total > count)
897 free (data);
898
899 return processed;
900}
901
902/*
903 * Set obsolete flag at offset - NOR flash only
904 */
905static int flash_flag_obsolete (int dev, int fd, off_t offset)
906{
907 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200908 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200909
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200910 erase.start = DEVOFFSET (dev);
911 erase.length = DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200912 /* This relies on the fact, that obsolete_flag == 0 */
913 rc = lseek (fd, offset, SEEK_SET);
914 if (rc < 0) {
915 fprintf (stderr, "Cannot seek to set the flag on %s \n",
916 DEVNAME (dev));
917 return rc;
918 }
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200919 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200920 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200921 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200922 if (rc < 0)
923 perror ("Could not set obsolete flag");
924
925 return rc;
926}
927
928static int flash_write (int fd_current, int fd_target, int dev_target)
929{
930 int rc;
931
932 switch (environment.flag_scheme) {
933 case FLAG_NONE:
934 break;
935 case FLAG_INCREMENTAL:
936 (*environment.flags)++;
937 break;
938 case FLAG_BOOLEAN:
939 *environment.flags = active_flag;
940 break;
941 default:
942 fprintf (stderr, "Unimplemented flash scheme %u \n",
943 environment.flag_scheme);
944 return -1;
945 }
946
947#ifdef DEBUG
948 printf ("Writing new environment at 0x%lx on %s\n",
949 DEVOFFSET (dev_target), DEVNAME (dev_target));
950#endif
Joe Hershberger671adc42012-10-03 09:38:46 +0000951 rc = flash_write_buf(dev_target, fd_target, environment.image,
952 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200953 DEVTYPE(dev_target));
954 if (rc < 0)
955 return rc;
956
957 if (environment.flag_scheme == FLAG_BOOLEAN) {
958 /* Have to set obsolete flag */
959 off_t offset = DEVOFFSET (dev_current) +
960 offsetof (struct env_image_redundant, flags);
961#ifdef DEBUG
962 printf ("Setting obsolete flag in environment at 0x%lx on %s\n",
963 DEVOFFSET (dev_current), DEVNAME (dev_current));
964#endif
965 flash_flag_obsolete (dev_current, fd_current, offset);
966 }
967
968 return 0;
969}
970
971static int flash_read (int fd)
972{
973 struct mtd_info_user mtdinfo;
974 int rc;
975
976 rc = ioctl (fd, MEMGETINFO, &mtdinfo);
977 if (rc < 0) {
978 perror ("Cannot get MTD information");
979 return -1;
980 }
981
Remy Bohmercb969eb2011-02-12 19:06:26 +0100982 if (mtdinfo.type != MTD_NORFLASH &&
983 mtdinfo.type != MTD_NANDFLASH &&
984 mtdinfo.type != MTD_DATAFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200985 fprintf (stderr, "Unsupported flash type %u\n", mtdinfo.type);
986 return -1;
987 }
988
989 DEVTYPE(dev_current) = mtdinfo.type;
990
Joe Hershberger671adc42012-10-03 09:38:46 +0000991 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200992 DEVOFFSET (dev_current), mtdinfo.type);
993
Joe Hershberger671adc42012-10-03 09:38:46 +0000994 return (rc != CUR_ENVSIZE) ? -1 : 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200995}
996
997static int flash_io (int mode)
998{
999 int fd_current, fd_target, rc, dev_target;
1000
1001 /* dev_current: fd_current, erase_current */
1002 fd_current = open (DEVNAME (dev_current), mode);
1003 if (fd_current < 0) {
1004 fprintf (stderr,
1005 "Can't open %s: %s\n",
1006 DEVNAME (dev_current), strerror (errno));
1007 return -1;
1008 }
1009
1010 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +00001011 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001012 /* switch to next partition for writing */
1013 dev_target = !dev_current;
1014 /* dev_target: fd_target, erase_target */
1015 fd_target = open (DEVNAME (dev_target), mode);
1016 if (fd_target < 0) {
wdenke7f34c62003-01-11 09:48:40 +00001017 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001018 "Can't open %s: %s\n",
1019 DEVNAME (dev_target),
1020 strerror (errno));
1021 rc = -1;
1022 goto exit;
wdenke7f34c62003-01-11 09:48:40 +00001023 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001024 } else {
1025 dev_target = dev_current;
1026 fd_target = fd_current;
wdenk34b613e2002-12-17 01:51:00 +00001027 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001028
1029 rc = flash_write (fd_current, fd_target, dev_target);
1030
wdenke7f34c62003-01-11 09:48:40 +00001031 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001032 if (close (fd_target)) {
wdenke7f34c62003-01-11 09:48:40 +00001033 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001034 "I/O error on %s: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001035 DEVNAME (dev_target),
wdenk3f9ab982003-04-12 23:38:12 +00001036 strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001037 rc = -1;
wdenke7f34c62003-01-11 09:48:40 +00001038 }
wdenk34b613e2002-12-17 01:51:00 +00001039 }
wdenk34b613e2002-12-17 01:51:00 +00001040 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001041 rc = flash_read (fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001042 }
1043
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001044exit:
1045 if (close (fd_current)) {
wdenk34b613e2002-12-17 01:51:00 +00001046 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001047 "I/O error on %s: %s\n",
1048 DEVNAME (dev_current), strerror (errno));
1049 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001050 }
1051
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001052 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001053}
1054
1055/*
1056 * s1 is either a simple 'name', or a 'name=value' pair.
1057 * s2 is a 'name=value' pair.
1058 * If the names match, return the value of s2, else NULL.
1059 */
1060
Markus Klotzbücher94db5572007-11-27 10:23:20 +01001061static char *envmatch (char * s1, char * s2)
wdenk34b613e2002-12-17 01:51:00 +00001062{
1063
1064 while (*s1 == *s2++)
1065 if (*s1++ == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001066 return s2;
wdenk3f9ab982003-04-12 23:38:12 +00001067 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001068 return s2;
1069 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001070}
1071
1072/*
1073 * Prevent confusion if running from erased flash memory
1074 */
Stefano Babicada628b2010-05-24 12:08:16 +02001075int fw_env_open(void)
wdenk34b613e2002-12-17 01:51:00 +00001076{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001077 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001078 unsigned char flag0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001079 void *addr0;
1080
wdenk34b613e2002-12-17 01:51:00 +00001081 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001082 unsigned char flag1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001083 void *addr1;
wdenke7f34c62003-01-11 09:48:40 +00001084
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001085 struct env_image_single *single;
1086 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001087
wdenk3f9ab982003-04-12 23:38:12 +00001088 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001089 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001090
Joe Hershberger671adc42012-10-03 09:38:46 +00001091 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001092 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001093 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001094 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001095 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001096 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001097 }
wdenk3f9ab982003-04-12 23:38:12 +00001098
wdenke7f34c62003-01-11 09:48:40 +00001099 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001100 environment.image = addr0;
1101
1102 if (HaveRedundEnv) {
1103 redundant = addr0;
1104 environment.crc = &redundant->crc;
1105 environment.flags = &redundant->flags;
1106 environment.data = redundant->data;
1107 } else {
1108 single = addr0;
1109 environment.crc = &single->crc;
1110 environment.flags = NULL;
1111 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001112 }
wdenk3f9ab982003-04-12 23:38:12 +00001113
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001114 dev_current = 0;
1115 if (flash_io (O_RDONLY))
1116 return -1;
1117
1118 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1119 crc0_ok = (crc0 == *environment.crc);
wdenke7f34c62003-01-11 09:48:40 +00001120 if (!HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001121 if (!crc0_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001122 fprintf (stderr,
1123 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkc5203f92006-03-12 23:27:46 +01001124 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk34b613e2002-12-17 01:51:00 +00001125 }
wdenke7f34c62003-01-11 09:48:40 +00001126 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001127 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001128
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001129 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001130 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001131 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001132 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001133 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001134 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001135 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001136 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001137 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001138
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001139 /*
1140 * have to set environment.image for flash_read(), careful -
1141 * other pointers in environment still point inside addr0
1142 */
1143 environment.image = addr1;
1144 if (flash_io (O_RDONLY))
1145 return -1;
1146
1147 /* Check flag scheme compatibility */
1148 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1149 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1150 environment.flag_scheme = FLAG_BOOLEAN;
1151 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1152 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1153 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001154 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1155 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1156 environment.flag_scheme = FLAG_BOOLEAN;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001157 } else {
1158 fprintf (stderr, "Incompatible flash types!\n");
1159 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001160 }
wdenk3f9ab982003-04-12 23:38:12 +00001161
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001162 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1163 crc1_ok = (crc1 == redundant->crc);
1164 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001165
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001166 if (crc0_ok && !crc1_ok) {
1167 dev_current = 0;
1168 } else if (!crc0_ok && crc1_ok) {
1169 dev_current = 1;
1170 } else if (!crc0_ok && !crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001171 fprintf (stderr,
1172 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001173 memcpy (environment.data, default_environment,
1174 sizeof default_environment);
1175 dev_current = 0;
1176 } else {
1177 switch (environment.flag_scheme) {
1178 case FLAG_BOOLEAN:
1179 if (flag0 == active_flag &&
1180 flag1 == obsolete_flag) {
1181 dev_current = 0;
1182 } else if (flag0 == obsolete_flag &&
1183 flag1 == active_flag) {
1184 dev_current = 1;
1185 } else if (flag0 == flag1) {
1186 dev_current = 0;
1187 } else if (flag0 == 0xFF) {
1188 dev_current = 0;
1189 } else if (flag1 == 0xFF) {
1190 dev_current = 1;
1191 } else {
1192 dev_current = 0;
1193 }
1194 break;
1195 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001196 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001197 dev_current = 1;
1198 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001199 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001200 dev_current = 0;
Jon Povey93ccd472011-03-11 14:10:56 +09001201 else /* flag1 > flag0 */
1202 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001203 break;
1204 default:
1205 fprintf (stderr, "Unknown flag scheme %u \n",
1206 environment.flag_scheme);
1207 return -1;
1208 }
1209 }
1210
1211 /*
1212 * If we are reading, we don't need the flag and the CRC any
1213 * more, if we are writing, we will re-calculate CRC and update
1214 * flags before writing out
1215 */
1216 if (dev_current) {
1217 environment.image = addr1;
1218 environment.crc = &redundant->crc;
1219 environment.flags = &redundant->flags;
1220 environment.data = redundant->data;
1221 free (addr0);
1222 } else {
1223 environment.image = addr0;
1224 /* Other pointers are already set */
wdenk3f9ab982003-04-12 23:38:12 +00001225 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +00001226 }
wdenk34b613e2002-12-17 01:51:00 +00001227 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001228 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001229}
1230
1231
wdenk3f9ab982003-04-12 23:38:12 +00001232static int parse_config ()
wdenk34b613e2002-12-17 01:51:00 +00001233{
1234 struct stat st;
1235
wdenke7f34c62003-01-11 09:48:40 +00001236#if defined(CONFIG_FILE)
1237 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
wdenk3f9ab982003-04-12 23:38:12 +00001238 if (get_config (CONFIG_FILE)) {
wdenke7f34c62003-01-11 09:48:40 +00001239 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001240 "Cannot parse config file: %s\n", strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001241 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001242 }
wdenke7f34c62003-01-11 09:48:40 +00001243#else
wdenk3f9ab982003-04-12 23:38:12 +00001244 strcpy (DEVNAME (0), DEVICE1_NAME);
1245 DEVOFFSET (0) = DEVICE1_OFFSET;
1246 ENVSIZE (0) = ENV1_SIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001247 /* Default values are: erase-size=env-size, #sectors=1 */
1248 DEVESIZE (0) = ENVSIZE (0);
1249 ENVSECTORS (0) = 1;
1250#ifdef DEVICE1_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001251 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001252#endif
1253#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001254 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001255#endif
1256
wdenk34b613e2002-12-17 01:51:00 +00001257#ifdef HAVE_REDUND
wdenk3f9ab982003-04-12 23:38:12 +00001258 strcpy (DEVNAME (1), DEVICE2_NAME);
1259 DEVOFFSET (1) = DEVICE2_OFFSET;
1260 ENVSIZE (1) = ENV2_SIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001261 /* Default values are: erase-size=env-size, #sectors=1 */
1262 DEVESIZE (1) = ENVSIZE (1);
1263 ENVSECTORS (1) = 1;
1264#ifdef DEVICE2_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001265 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001266#endif
1267#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001268 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001269#endif
wdenke7f34c62003-01-11 09:48:40 +00001270 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +00001271#endif
wdenke7f34c62003-01-11 09:48:40 +00001272#endif
wdenk3f9ab982003-04-12 23:38:12 +00001273 if (stat (DEVNAME (0), &st)) {
1274 fprintf (stderr,
1275 "Cannot access MTD device %s: %s\n",
1276 DEVNAME (0), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001277 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001278 }
wdenk3f9ab982003-04-12 23:38:12 +00001279
1280 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1281 fprintf (stderr,
1282 "Cannot access MTD device %s: %s\n",
Wolfgang Denk8e6213c2005-09-25 01:20:38 +02001283 DEVNAME (1), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001284 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001285 }
wdenk34b613e2002-12-17 01:51:00 +00001286 return 0;
1287}
wdenke7f34c62003-01-11 09:48:40 +00001288
1289#if defined(CONFIG_FILE)
1290static int get_config (char *fname)
1291{
1292 FILE *fp;
1293 int i = 0;
1294 int rc;
1295 char dump[128];
1296
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001297 fp = fopen (fname, "r");
1298 if (fp == NULL)
1299 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001300
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001301 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenke7f34c62003-01-11 09:48:40 +00001302 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001303 if (dump[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001304 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001305
1306 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1307 DEVNAME (i),
1308 &DEVOFFSET (i),
1309 &ENVSIZE (i),
1310 &DEVESIZE (i),
1311 &ENVSECTORS (i));
1312
Remy Bohmercb969eb2011-02-12 19:06:26 +01001313 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001314 continue;
1315
Remy Bohmercb969eb2011-02-12 19:06:26 +01001316 if (rc < 4)
1317 /* Assume the erase size is the same as the env-size */
1318 DEVESIZE(i) = ENVSIZE(i);
1319
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001320 if (rc < 5)
1321 /* Default - 1 sector */
1322 ENVSECTORS (i) = 1;
wdenke7f34c62003-01-11 09:48:40 +00001323
1324 i++;
1325 }
wdenk3f9ab982003-04-12 23:38:12 +00001326 fclose (fp);
1327
wdenke7f34c62003-01-11 09:48:40 +00001328 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +00001329 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001330 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001331 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001332 } else
1333 return 0;
1334}
1335#endif