blob: 01fc1d4e5741f2eb052f9da7aa0d18c49127e54d [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>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060028#include <env_flags.h>
wdenk34b613e2002-12-17 01:51:00 +000029#include <fcntl.h>
Joe Hershbergere79a91f2012-10-12 10:23:37 +000030#include <linux/stringify.h>
wdenk34b613e2002-12-17 01:51:00 +000031#include <stdio.h>
32#include <stdlib.h>
33#include <stddef.h>
34#include <string.h>
35#include <sys/types.h>
36#include <sys/ioctl.h>
37#include <sys/stat.h>
38#include <unistd.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010039
40#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020041# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010042# include <linux/mtd/mtd.h>
43#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010044# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010045# include <mtd/mtd-user.h>
46#endif
wdenk34b613e2002-12-17 01:51:00 +000047
Markus Klotzbücher94db5572007-11-27 10:23:20 +010048#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000049
Stefano Babicada628b2010-05-24 12:08:16 +020050#define WHITESPACE(c) ((c == '\t') || (c == ' '))
wdenk34b613e2002-12-17 01:51:00 +000051
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020052#define min(x, y) ({ \
53 typeof(x) _min1 = (x); \
54 typeof(y) _min2 = (y); \
55 (void) (&_min1 == &_min2); \
56 _min1 < _min2 ? _min1 : _min2; })
57
58struct envdev_s {
Markus Klotzbücher94db5572007-11-27 10:23:20 +010059 char devname[16]; /* Device name */
wdenk3f9ab982003-04-12 23:38:12 +000060 ulong devoff; /* Device offset */
61 ulong env_size; /* environment size */
62 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020063 ulong env_sectors; /* number of environment sectors */
64 uint8_t mtd_type; /* type of the MTD device */
65};
wdenk34b613e2002-12-17 01:51:00 +000066
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020067static struct envdev_s envdevices[2] =
68{
69 {
70 .mtd_type = MTD_ABSENT,
71 }, {
72 .mtd_type = MTD_ABSENT,
73 },
74};
75static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000076
77#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000078#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000079#define ENVSIZE(i) envdevices[(i)].env_size
80#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020081#define ENVSECTORS(i) envdevices[(i)].env_sectors
82#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk34b613e2002-12-17 01:51:00 +000083
Joe Hershberger671adc42012-10-03 09:38:46 +000084#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000085
wdenke7f34c62003-01-11 09:48:40 +000086#define ENV_SIZE getenvsize()
wdenk34b613e2002-12-17 01:51:00 +000087
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020088struct env_image_single {
89 uint32_t crc; /* CRC32 over data bytes */
90 char data[];
91};
wdenk34b613e2002-12-17 01:51:00 +000092
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020093struct env_image_redundant {
94 uint32_t crc; /* CRC32 over data bytes */
95 unsigned char flags; /* active or obsolete */
96 char data[];
97};
98
99enum flag_scheme {
100 FLAG_NONE,
101 FLAG_BOOLEAN,
102 FLAG_INCREMENTAL,
103};
104
105struct environment {
106 void *image;
107 uint32_t *crc;
108 unsigned char *flags;
109 char *data;
110 enum flag_scheme flag_scheme;
111};
112
113static struct environment environment = {
114 .flag_scheme = FLAG_NONE,
115};
wdenk34b613e2002-12-17 01:51:00 +0000116
wdenke7f34c62003-01-11 09:48:40 +0000117static int HaveRedundEnv = 0;
118
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100119static unsigned char active_flag = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200120/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100121static unsigned char obsolete_flag = 0;
wdenke7f34c62003-01-11 09:48:40 +0000122
Joe Hershberger81321142012-10-12 08:48:51 +0000123#define DEFAULT_ENV_INSTANCE_STATIC
124#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000125
wdenk3f9ab982003-04-12 23:38:12 +0000126static int flash_io (int mode);
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100127static char *envmatch (char * s1, char * s2);
wdenk3f9ab982003-04-12 23:38:12 +0000128static int parse_config (void);
129
wdenke7f34c62003-01-11 09:48:40 +0000130#if defined(CONFIG_FILE)
wdenk3f9ab982003-04-12 23:38:12 +0000131static int get_config (char *);
wdenke7f34c62003-01-11 09:48:40 +0000132#endif
wdenk3f9ab982003-04-12 23:38:12 +0000133static inline ulong getenvsize (void)
wdenke7f34c62003-01-11 09:48:40 +0000134{
Joe Hershberger671adc42012-10-03 09:38:46 +0000135 ulong rc = CUR_ENVSIZE - sizeof(long);
wdenk3f9ab982003-04-12 23:38:12 +0000136
wdenke7f34c62003-01-11 09:48:40 +0000137 if (HaveRedundEnv)
wdenk3f9ab982003-04-12 23:38:12 +0000138 rc -= sizeof (char);
wdenke7f34c62003-01-11 09:48:40 +0000139 return rc;
140}
wdenk34b613e2002-12-17 01:51:00 +0000141
Stefano Babicada628b2010-05-24 12:08:16 +0200142static char *fw_string_blank(char *s, int noblank)
143{
144 int i;
145 int len = strlen(s);
146
147 for (i = 0; i < len; i++, s++) {
148 if ((noblank && !WHITESPACE(*s)) ||
149 (!noblank && WHITESPACE(*s)))
150 break;
151 }
152 if (i == len)
153 return NULL;
154
155 return s;
156}
157
wdenk34b613e2002-12-17 01:51:00 +0000158/*
159 * Search the environment for a variable.
160 * Return the value, if found, or NULL, if not found.
161 */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100162char *fw_getenv (char *name)
wdenk34b613e2002-12-17 01:51:00 +0000163{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100164 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000165
wdenk3f9ab982003-04-12 23:38:12 +0000166 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100167 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000168
wdenk3f9ab982003-04-12 23:38:12 +0000169 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000170 if (nxt >= &environment.data[ENV_SIZE]) {
171 fprintf (stderr, "## Error: "
172 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200173 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000174 }
175 }
wdenk3f9ab982003-04-12 23:38:12 +0000176 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000177 if (!val)
178 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200179 return val;
wdenk34b613e2002-12-17 01:51:00 +0000180 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200181 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000182}
183
184/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600185 * Search the default environment for a variable.
186 * Return the value, if found, or NULL, if not found.
187 */
188char *fw_getdefenv(char *name)
189{
190 char *env, *nxt;
191
192 for (env = default_environment; *env; env = nxt + 1) {
193 char *val;
194
195 for (nxt = env; *nxt; ++nxt) {
196 if (nxt >= &default_environment[ENV_SIZE]) {
197 fprintf(stderr, "## Error: "
198 "default environment not terminated\n");
199 return NULL;
200 }
201 }
202 val = envmatch(name, env);
203 if (!val)
204 continue;
205 return val;
206 }
207 return NULL;
208}
209
210/*
wdenk34b613e2002-12-17 01:51:00 +0000211 * Print the current definition of one, or more, or all
212 * environment variables
213 */
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700214int fw_printenv (int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000215{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100216 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000217 int i, n_flag;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700218 int rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000219
Stefano Babicada628b2010-05-24 12:08:16 +0200220 if (fw_env_open())
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200221 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000222
wdenk3f9ab982003-04-12 23:38:12 +0000223 if (argc == 1) { /* Print all env variables */
224 for (env = environment.data; *env; env = nxt + 1) {
225 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000226 if (nxt >= &environment.data[ENV_SIZE]) {
227 fprintf (stderr, "## Error: "
228 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200229 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000230 }
231 }
232
wdenk3f9ab982003-04-12 23:38:12 +0000233 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000234 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200235 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000236 }
237
wdenk3f9ab982003-04-12 23:38:12 +0000238 if (strcmp (argv[1], "-n") == 0) {
wdenk34b613e2002-12-17 01:51:00 +0000239 n_flag = 1;
240 ++argv;
241 --argc;
242 if (argc != 2) {
243 fprintf (stderr, "## Error: "
244 "`-n' option requires exactly one argument\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200245 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000246 }
247 } else {
248 n_flag = 0;
249 }
250
wdenk3f9ab982003-04-12 23:38:12 +0000251 for (i = 1; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100252 char *name = argv[i];
253 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000254
wdenk3f9ab982003-04-12 23:38:12 +0000255 for (env = environment.data; *env; env = nxt + 1) {
wdenk34b613e2002-12-17 01:51:00 +0000256
wdenk3f9ab982003-04-12 23:38:12 +0000257 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000258 if (nxt >= &environment.data[ENV_SIZE]) {
259 fprintf (stderr, "## Error: "
260 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200261 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000262 }
263 }
wdenk3f9ab982003-04-12 23:38:12 +0000264 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000265 if (val) {
266 if (!n_flag) {
267 fputs (name, stdout);
wdenk3f9ab982003-04-12 23:38:12 +0000268 putc ('=', stdout);
wdenk34b613e2002-12-17 01:51:00 +0000269 }
wdenk3f9ab982003-04-12 23:38:12 +0000270 puts (val);
wdenk34b613e2002-12-17 01:51:00 +0000271 break;
272 }
273 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700274 if (!val) {
wdenk3f9ab982003-04-12 23:38:12 +0000275 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700276 rc = -1;
277 }
wdenk34b613e2002-12-17 01:51:00 +0000278 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700279
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200280 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000281}
282
Stefano Babicada628b2010-05-24 12:08:16 +0200283int fw_env_close(void)
wdenk34b613e2002-12-17 01:51:00 +0000284{
Stefano Babicada628b2010-05-24 12:08:16 +0200285 /*
286 * Update CRC
287 */
288 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000289
Stefano Babicada628b2010-05-24 12:08:16 +0200290 /* write environment back to flash */
291 if (flash_io(O_RDWR)) {
292 fprintf(stderr,
293 "Error: can't write fw_env to flash\n");
294 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000295 }
296
Stefano Babicada628b2010-05-24 12:08:16 +0200297 return 0;
298}
wdenk34b613e2002-12-17 01:51:00 +0000299
Stefano Babicada628b2010-05-24 12:08:16 +0200300
301/*
302 * Set/Clear a single variable in the environment.
303 * This is called in sequence to update the environment
304 * in RAM without updating the copy in flash after each set
305 */
306int fw_env_write(char *name, char *value)
307{
308 int len;
309 char *env, *nxt;
310 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600311 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000312
313 /*
314 * search if variable with this name already exists
315 */
wdenk3f9ab982003-04-12 23:38:12 +0000316 for (nxt = env = environment.data; *env; env = nxt + 1) {
317 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000318 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200319 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000320 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200321 errno = EINVAL;
322 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000323 }
324 }
wdenk3f9ab982003-04-12 23:38:12 +0000325 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000326 break;
327 }
328
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600329 deleting = (oldval && !(value && strlen(value)));
330 creating = (!oldval && (value && strlen(value)));
331 overwriting = (oldval && (value && strlen(value)));
332
333 /* check for permission */
334 if (deleting) {
335 if (env_flags_validate_varaccess(name,
336 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
337 printf("Can't delete \"%s\"\n", name);
338 errno = EROFS;
339 return -1;
340 }
341 } else if (overwriting) {
342 if (env_flags_validate_varaccess(name,
343 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
344 printf("Can't overwrite \"%s\"\n", name);
345 errno = EROFS;
346 return -1;
347 } else if (env_flags_validate_varaccess(name,
348 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
349 const char *defval = fw_getdefenv(name);
350
351 if (defval == NULL)
352 defval = "";
353 if (strcmp(oldval, defval)
354 != 0) {
355 printf("Can't overwrite \"%s\"\n", name);
356 errno = EROFS;
357 return -1;
358 }
359 }
360 } else if (creating) {
361 if (env_flags_validate_varaccess(name,
362 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
363 printf("Can't create \"%s\"\n", name);
364 errno = EROFS;
365 return -1;
366 }
367 } else
368 /* Nothing to do */
369 return 0;
370
371 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000372 if (*++nxt == '\0') {
373 *env = '\0';
374 } else {
375 for (;;) {
376 *env = *nxt++;
377 if ((*env == '\0') && (*nxt == '\0'))
378 break;
379 ++env;
380 }
381 }
382 *++env = '\0';
383 }
384
385 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200386 if (!value || !strlen(value))
387 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000388
389 /*
390 * Append new definition at the end
391 */
wdenk3f9ab982003-04-12 23:38:12 +0000392 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000393 if (env > environment.data)
394 ++env;
395 /*
396 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000397 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000398 */
wdenk3f9ab982003-04-12 23:38:12 +0000399 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000400 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200401 len += strlen(value) + 1;
402
wdenk3f9ab982003-04-12 23:38:12 +0000403 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000404 fprintf (stderr,
405 "Error: environment overflow, \"%s\" deleted\n",
406 name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200407 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000408 }
Stefano Babicada628b2010-05-24 12:08:16 +0200409
wdenk34b613e2002-12-17 01:51:00 +0000410 while ((*env = *name++) != '\0')
411 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200412 *env = '=';
413 while ((*++env = *value++) != '\0')
414 ;
415
416 /* end is marked with double '\0' */
417 *++env = '\0';
418
419 return 0;
420}
421
422/*
423 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
424 * 0 - OK
425 * EINVAL - need at least 1 argument
426 * EROFS - certain variables ("ethaddr", "serial#") cannot be
427 * modified or deleted
428 *
429 */
430int fw_setenv(int argc, char *argv[])
431{
Mike Frysinger178ec772012-11-10 19:47:45 +0000432 int i;
433 size_t len;
Stefano Babicada628b2010-05-24 12:08:16 +0200434 char *name;
435 char *value = NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200436
437 if (argc < 2) {
438 errno = EINVAL;
439 return -1;
440 }
441
442 if (fw_env_open()) {
443 fprintf(stderr, "Error: environment not initialized\n");
444 return -1;
445 }
446
447 name = argv[1];
448
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600449 if (env_flags_validate_env_set_params(argc, argv) < 0)
450 return 1;
451
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000452 len = 0;
wdenk3f9ab982003-04-12 23:38:12 +0000453 for (i = 2; i < argc; ++i) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100454 char *val = argv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000455 size_t val_len = strlen(val);
456
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000457 if (value)
458 value[len - 1] = ' ';
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000459 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200460 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000461 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200462 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000463 len, strerror(errno));
464 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200465 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000466
467 memcpy(value + len, val, val_len);
468 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000469 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000470 }
471
Stefano Babicada628b2010-05-24 12:08:16 +0200472 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000473
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000474 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000475
Stefano Babicada628b2010-05-24 12:08:16 +0200476 return fw_env_close();
477}
wdenk34b613e2002-12-17 01:51:00 +0000478
Stefano Babicada628b2010-05-24 12:08:16 +0200479/*
480 * Parse a file and configure the u-boot variables.
481 * The script file has a very simple format, as follows:
482 *
483 * Each line has a couple with name, value:
484 * <white spaces>variable_name<white spaces>variable_value
485 *
486 * Both variable_name and variable_value are interpreted as strings.
487 * Any character after <white spaces> and before ending \r\n is interpreted
488 * as variable's value (no comment allowed on these lines !)
489 *
490 * Comments are allowed if the first character in the line is #
491 *
492 * Returns -1 and sets errno error codes:
493 * 0 - OK
494 * -1 - Error
495 */
496int fw_parse_script(char *fname)
497{
498 FILE *fp;
499 char dump[1024]; /* Maximum line length in the file */
500 char *name;
501 char *val;
502 int lineno = 0;
503 int len;
504 int ret = 0;
505
506 if (fw_env_open()) {
507 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200508 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000509 }
510
Stefano Babicada628b2010-05-24 12:08:16 +0200511 if (strcmp(fname, "-") == 0)
512 fp = stdin;
513 else {
514 fp = fopen(fname, "r");
515 if (fp == NULL) {
516 fprintf(stderr, "I cannot open %s for reading\n",
517 fname);
518 return -1;
519 }
520 }
521
522 while (fgets(dump, sizeof(dump), fp)) {
523 lineno++;
524 len = strlen(dump);
525
526 /*
527 * Read a whole line from the file. If the line is too long
528 * or is not terminated, reports an error and exit.
529 */
530 if (dump[len - 1] != '\n') {
531 fprintf(stderr,
532 "Line %d not corrected terminated or too long\n",
533 lineno);
534 ret = -1;
535 break;
536 }
537
538 /* Drop ending line feed / carriage return */
539 while (len > 0 && (dump[len - 1] == '\n' ||
540 dump[len - 1] == '\r')) {
541 dump[len - 1] = '\0';
542 len--;
543 }
544
545 /* Skip comment or empty lines */
546 if ((len == 0) || dump[0] == '#')
547 continue;
548
549 /*
550 * Search for variable's name,
551 * remove leading whitespaces
552 */
553 name = fw_string_blank(dump, 1);
554 if (!name)
555 continue;
556
557 /* The first white space is the end of variable name */
558 val = fw_string_blank(name, 0);
559 len = strlen(name);
560 if (val) {
561 *val++ = '\0';
562 if ((val - name) < len)
563 val = fw_string_blank(val, 1);
564 else
565 val = NULL;
566 }
567
568#ifdef DEBUG
569 fprintf(stderr, "Setting %s : %s\n",
570 name, val ? val : " removed");
571#endif
572
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600573 if (env_flags_validate_type(name, val) < 0) {
574 ret = -1;
575 break;
576 }
577
Stefano Babicada628b2010-05-24 12:08:16 +0200578 /*
579 * If there is an error setting a variable,
580 * try to save the environment and returns an error
581 */
582 if (fw_env_write(name, val)) {
583 fprintf(stderr,
584 "fw_env_write returns with error : %s\n",
585 strerror(errno));
586 ret = -1;
587 break;
588 }
589
590 }
591
592 /* Close file if not stdin */
593 if (strcmp(fname, "-") != 0)
594 fclose(fp);
595
596 ret |= fw_env_close();
597
598 return ret;
599
wdenk34b613e2002-12-17 01:51:00 +0000600}
601
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200602/*
603 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
604 * 0 - block is good
605 * > 0 - block is bad
606 * < 0 - failed to test
607 */
608static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000609{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200610 if (mtd_type == MTD_NANDFLASH) {
611 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000612
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200613 if (badblock < 0) {
614 perror ("Cannot read bad block mark");
615 return badblock;
616 }
617
618 if (badblock) {
619#ifdef DEBUG
620 fprintf (stderr, "Bad block at 0x%llx, "
621 "skipping\n", *blockstart);
622#endif
623 return badblock;
624 }
wdenk34b613e2002-12-17 01:51:00 +0000625 }
626
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200627 return 0;
628}
629
630/*
631 * Read data from flash at an offset into a provided buffer. On NAND it skips
632 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
633 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
634 */
635static int flash_read_buf (int dev, int fd, void *buf, size_t count,
636 off_t offset, uint8_t mtd_type)
637{
638 size_t blocklen; /* erase / write length - one block on NAND,
639 0 on NOR */
640 size_t processed = 0; /* progress counter */
641 size_t readlen = count; /* current read length */
642 off_t top_of_range; /* end of the last block we may use */
643 off_t block_seek; /* offset inside the current block to the start
644 of the data */
645 loff_t blockstart; /* running start of the current block -
646 MEMGETBADBLOCK needs 64 bits */
647 int rc;
648
Remy Bohmercb969eb2011-02-12 19:06:26 +0100649 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200650
651 /* Offset inside a block */
652 block_seek = offset - blockstart;
653
654 if (mtd_type == MTD_NANDFLASH) {
655 /*
656 * NAND: calculate which blocks we are reading. We have
657 * to read one block at a time to skip bad blocks.
658 */
659 blocklen = DEVESIZE (dev);
660
661 /*
662 * To calculate the top of the range, we have to use the
663 * global DEVOFFSET (dev), which can be different from offset
664 */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100665 top_of_range = ((DEVOFFSET(dev) / blocklen) +
666 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200667
668 /* Limit to one block for the first read */
669 if (readlen > blocklen - block_seek)
670 readlen = blocklen - block_seek;
671 } else {
672 blocklen = 0;
673 top_of_range = offset + count;
wdenke7f34c62003-01-11 09:48:40 +0000674 }
wdenk34b613e2002-12-17 01:51:00 +0000675
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200676 /* This only runs once on NOR flash */
677 while (processed < count) {
678 rc = flash_bad_block (fd, mtd_type, &blockstart);
679 if (rc < 0) /* block test failed */
680 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000681
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200682 if (blockstart + block_seek + readlen > top_of_range) {
683 /* End of range is reached */
684 fprintf (stderr,
685 "Too few good blocks within range\n");
686 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000687 }
688
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200689 if (rc) { /* block is bad */
690 blockstart += blocklen;
691 continue;
wdenk34b613e2002-12-17 01:51:00 +0000692 }
693
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200694 /*
695 * If a block is bad, we retry in the next block at the same
696 * offset - see common/env_nand.c::writeenv()
697 */
698 lseek (fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000699
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200700 rc = read (fd, buf + processed, readlen);
701 if (rc != readlen) {
702 fprintf (stderr, "Read error on %s: %s\n",
703 DEVNAME (dev), strerror (errno));
704 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000705 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200706#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000707 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
708 rc, blockstart + block_seek, DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200709#endif
710 processed += readlen;
711 readlen = min (blocklen, count - processed);
712 block_seek = 0;
713 blockstart += blocklen;
714 }
715
716 return processed;
717}
718
719/*
Remy Bohmercb969eb2011-02-12 19:06:26 +0100720 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
721 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
722 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200723 */
724static int flash_write_buf (int dev, int fd, void *buf, size_t count,
725 off_t offset, uint8_t mtd_type)
726{
727 void *data;
728 struct erase_info_user erase;
729 size_t blocklen; /* length of NAND block / NOR erase sector */
730 size_t erase_len; /* whole area that can be erased - may include
731 bad blocks */
732 size_t erasesize; /* erase / write length - one block on NAND,
733 whole area on NOR */
734 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100735 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200736 bad blocks */
737 off_t erase_offset; /* offset to the first erase block (aligned)
738 below offset */
739 off_t block_seek; /* offset inside the erase block to the start
740 of the data */
741 off_t top_of_range; /* end of the last block we may use */
742 loff_t blockstart; /* running start of the current block -
743 MEMGETBADBLOCK needs 64 bits */
744 int rc;
745
746 blocklen = DEVESIZE (dev);
747
Remy Bohmercb969eb2011-02-12 19:06:26 +0100748 top_of_range = ((DEVOFFSET(dev) / blocklen) +
749 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200750
Remy Bohmercb969eb2011-02-12 19:06:26 +0100751 erase_offset = (offset / blocklen) * blocklen;
wdenk34b613e2002-12-17 01:51:00 +0000752
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200753 /* Maximum area we may use */
754 erase_len = top_of_range - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +0000755
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200756 blockstart = erase_offset;
757 /* Offset inside a block */
758 block_seek = offset - erase_offset;
759
760 /*
761 * Data size we actually have to write: from the start of the block
762 * to the start of the data, then count bytes of data, and to the
763 * end of the block
764 */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100765 write_total = ((block_seek + count + blocklen - 1) /
766 blocklen) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200767
768 /*
769 * Support data anywhere within erase sectors: read out the complete
770 * area to be erased, replace the environment image, write the whole
771 * block back again.
772 */
773 if (write_total > count) {
774 data = malloc (erase_len);
775 if (!data) {
wdenke7f34c62003-01-11 09:48:40 +0000776 fprintf (stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200777 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200778 erase_len, strerror (errno));
779 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000780 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200781
782 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
783 mtd_type);
784 if (write_total != rc)
785 return -1;
786
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000787#ifdef DEBUG
788 fprintf(stderr, "Preserving data ");
789 if (block_seek != 0)
790 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
791 if (block_seek + count != write_total) {
792 if (block_seek != 0)
793 fprintf(stderr, " and ");
794 fprintf(stderr, "0x%lx - 0x%x",
795 block_seek + count, write_total - 1);
796 }
797 fprintf(stderr, "\n");
798#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200799 /* Overwrite the old environment */
800 memcpy (data + block_seek, buf, count);
801 } else {
802 /*
803 * We get here, iff offset is block-aligned and count is a
804 * multiple of blocklen - see write_total calculation above
805 */
806 data = buf;
807 }
808
809 if (mtd_type == MTD_NANDFLASH) {
810 /*
811 * NAND: calculate which blocks we are writing. We have
812 * to write one block at a time to skip bad blocks.
813 */
814 erasesize = blocklen;
815 } else {
816 erasesize = erase_len;
817 }
818
819 erase.length = erasesize;
820
Remy Bohmercb969eb2011-02-12 19:06:26 +0100821 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200822 while (processed < write_total) {
823 rc = flash_bad_block (fd, mtd_type, &blockstart);
824 if (rc < 0) /* block test failed */
825 return rc;
826
827 if (blockstart + erasesize > top_of_range) {
828 fprintf (stderr, "End of range reached, aborting\n");
829 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000830 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200831
832 if (rc) { /* block is bad */
833 blockstart += blocklen;
834 continue;
835 }
836
837 erase.start = blockstart;
838 ioctl (fd, MEMUNLOCK, &erase);
Lubomir Rintel57cb6a32013-02-10 01:02:36 +0000839 /* These do not need an explicit erase cycle */
840 if (mtd_type != MTD_ABSENT &&
841 mtd_type != MTD_DATAFLASH)
Remy Bohmercb969eb2011-02-12 19:06:26 +0100842 if (ioctl (fd, MEMERASE, &erase) != 0) {
843 fprintf (stderr, "MTD erase error on %s: %s\n",
844 DEVNAME (dev),
845 strerror (errno));
846 return -1;
847 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200848
849 if (lseek (fd, blockstart, SEEK_SET) == -1) {
wdenk34b613e2002-12-17 01:51:00 +0000850 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200851 "Seek error on %s: %s\n",
852 DEVNAME (dev), strerror (errno));
853 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000854 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200855
856#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000857 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
858 blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200859#endif
860 if (write (fd, data + processed, erasesize) != erasesize) {
861 fprintf (stderr, "Write error on %s: %s\n",
862 DEVNAME (dev), strerror (errno));
863 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000864 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200865
866 ioctl (fd, MEMLOCK, &erase);
867
868 processed += blocklen;
869 block_seek = 0;
870 blockstart += blocklen;
871 }
872
873 if (write_total > count)
874 free (data);
875
876 return processed;
877}
878
879/*
880 * Set obsolete flag at offset - NOR flash only
881 */
882static int flash_flag_obsolete (int dev, int fd, off_t offset)
883{
884 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200885 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200886
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200887 erase.start = DEVOFFSET (dev);
888 erase.length = DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200889 /* This relies on the fact, that obsolete_flag == 0 */
890 rc = lseek (fd, offset, SEEK_SET);
891 if (rc < 0) {
892 fprintf (stderr, "Cannot seek to set the flag on %s \n",
893 DEVNAME (dev));
894 return rc;
895 }
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200896 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200897 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200898 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200899 if (rc < 0)
900 perror ("Could not set obsolete flag");
901
902 return rc;
903}
904
905static int flash_write (int fd_current, int fd_target, int dev_target)
906{
907 int rc;
908
909 switch (environment.flag_scheme) {
910 case FLAG_NONE:
911 break;
912 case FLAG_INCREMENTAL:
913 (*environment.flags)++;
914 break;
915 case FLAG_BOOLEAN:
916 *environment.flags = active_flag;
917 break;
918 default:
919 fprintf (stderr, "Unimplemented flash scheme %u \n",
920 environment.flag_scheme);
921 return -1;
922 }
923
924#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000925 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200926 DEVOFFSET (dev_target), DEVNAME (dev_target));
927#endif
Joe Hershberger671adc42012-10-03 09:38:46 +0000928 rc = flash_write_buf(dev_target, fd_target, environment.image,
929 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200930 DEVTYPE(dev_target));
931 if (rc < 0)
932 return rc;
933
934 if (environment.flag_scheme == FLAG_BOOLEAN) {
935 /* Have to set obsolete flag */
936 off_t offset = DEVOFFSET (dev_current) +
937 offsetof (struct env_image_redundant, flags);
938#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000939 fprintf(stderr,
940 "Setting obsolete flag in environment at 0x%lx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200941 DEVOFFSET (dev_current), DEVNAME (dev_current));
942#endif
943 flash_flag_obsolete (dev_current, fd_current, offset);
944 }
945
946 return 0;
947}
948
949static int flash_read (int fd)
950{
951 struct mtd_info_user mtdinfo;
Lubomir Rintel57cb6a32013-02-10 01:02:36 +0000952 struct stat st;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200953 int rc;
954
Lubomir Rintel57cb6a32013-02-10 01:02:36 +0000955 rc = fstat(fd, &st);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200956 if (rc < 0) {
Lubomir Rintel57cb6a32013-02-10 01:02:36 +0000957 fprintf(stderr, "Cannot stat the file %s\n",
958 DEVNAME(dev_current));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200959 return -1;
960 }
961
Lubomir Rintel57cb6a32013-02-10 01:02:36 +0000962 if (S_ISCHR(st.st_mode)) {
963 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
964 if (rc < 0) {
965 fprintf(stderr, "Cannot get MTD information for %s\n",
966 DEVNAME(dev_current));
967 return -1;
968 }
969 if (mtdinfo.type != MTD_NORFLASH &&
970 mtdinfo.type != MTD_NANDFLASH &&
Joe Hershberger0c5faa82013-04-08 10:32:51 +0000971 mtdinfo.type != MTD_DATAFLASH &&
972 mtdinfo.type != MTD_UBIVOLUME) {
Lubomir Rintel57cb6a32013-02-10 01:02:36 +0000973 fprintf (stderr, "Unsupported flash type %u on %s\n",
974 mtdinfo.type, DEVNAME(dev_current));
975 return -1;
976 }
977 } else {
978 memset(&mtdinfo, 0, sizeof(mtdinfo));
979 mtdinfo.type = MTD_ABSENT;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200980 }
981
982 DEVTYPE(dev_current) = mtdinfo.type;
983
Joe Hershberger671adc42012-10-03 09:38:46 +0000984 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200985 DEVOFFSET (dev_current), mtdinfo.type);
986
Joe Hershberger671adc42012-10-03 09:38:46 +0000987 return (rc != CUR_ENVSIZE) ? -1 : 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200988}
989
990static int flash_io (int mode)
991{
992 int fd_current, fd_target, rc, dev_target;
993
994 /* dev_current: fd_current, erase_current */
995 fd_current = open (DEVNAME (dev_current), mode);
996 if (fd_current < 0) {
997 fprintf (stderr,
998 "Can't open %s: %s\n",
999 DEVNAME (dev_current), strerror (errno));
1000 return -1;
1001 }
1002
1003 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +00001004 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001005 /* switch to next partition for writing */
1006 dev_target = !dev_current;
1007 /* dev_target: fd_target, erase_target */
1008 fd_target = open (DEVNAME (dev_target), mode);
1009 if (fd_target < 0) {
wdenke7f34c62003-01-11 09:48:40 +00001010 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001011 "Can't open %s: %s\n",
1012 DEVNAME (dev_target),
1013 strerror (errno));
1014 rc = -1;
1015 goto exit;
wdenke7f34c62003-01-11 09:48:40 +00001016 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001017 } else {
1018 dev_target = dev_current;
1019 fd_target = fd_current;
wdenk34b613e2002-12-17 01:51:00 +00001020 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001021
1022 rc = flash_write (fd_current, fd_target, dev_target);
1023
wdenke7f34c62003-01-11 09:48:40 +00001024 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001025 if (close (fd_target)) {
wdenke7f34c62003-01-11 09:48:40 +00001026 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001027 "I/O error on %s: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001028 DEVNAME (dev_target),
wdenk3f9ab982003-04-12 23:38:12 +00001029 strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001030 rc = -1;
wdenke7f34c62003-01-11 09:48:40 +00001031 }
wdenk34b613e2002-12-17 01:51:00 +00001032 }
wdenk34b613e2002-12-17 01:51:00 +00001033 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001034 rc = flash_read (fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001035 }
1036
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001037exit:
1038 if (close (fd_current)) {
wdenk34b613e2002-12-17 01:51:00 +00001039 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001040 "I/O error on %s: %s\n",
1041 DEVNAME (dev_current), strerror (errno));
1042 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001043 }
1044
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001045 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001046}
1047
1048/*
1049 * s1 is either a simple 'name', or a 'name=value' pair.
1050 * s2 is a 'name=value' pair.
1051 * If the names match, return the value of s2, else NULL.
1052 */
1053
Markus Klotzbücher94db5572007-11-27 10:23:20 +01001054static char *envmatch (char * s1, char * s2)
wdenk34b613e2002-12-17 01:51:00 +00001055{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +00001056 if (s1 == NULL || s2 == NULL)
1057 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001058
1059 while (*s1 == *s2++)
1060 if (*s1++ == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001061 return s2;
wdenk3f9ab982003-04-12 23:38:12 +00001062 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001063 return s2;
1064 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001065}
1066
1067/*
1068 * Prevent confusion if running from erased flash memory
1069 */
Stefano Babicada628b2010-05-24 12:08:16 +02001070int fw_env_open(void)
wdenk34b613e2002-12-17 01:51:00 +00001071{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001072 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001073 unsigned char flag0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001074 void *addr0;
1075
wdenk34b613e2002-12-17 01:51:00 +00001076 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001077 unsigned char flag1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001078 void *addr1;
wdenke7f34c62003-01-11 09:48:40 +00001079
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001080 struct env_image_single *single;
1081 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001082
wdenk3f9ab982003-04-12 23:38:12 +00001083 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001084 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001085
Joe Hershberger671adc42012-10-03 09:38:46 +00001086 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001087 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001088 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001089 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001090 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001091 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001092 }
wdenk3f9ab982003-04-12 23:38:12 +00001093
wdenke7f34c62003-01-11 09:48:40 +00001094 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001095 environment.image = addr0;
1096
1097 if (HaveRedundEnv) {
1098 redundant = addr0;
1099 environment.crc = &redundant->crc;
1100 environment.flags = &redundant->flags;
1101 environment.data = redundant->data;
1102 } else {
1103 single = addr0;
1104 environment.crc = &single->crc;
1105 environment.flags = NULL;
1106 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001107 }
wdenk3f9ab982003-04-12 23:38:12 +00001108
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001109 dev_current = 0;
1110 if (flash_io (O_RDONLY))
1111 return -1;
1112
1113 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1114 crc0_ok = (crc0 == *environment.crc);
wdenke7f34c62003-01-11 09:48:40 +00001115 if (!HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001116 if (!crc0_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001117 fprintf (stderr,
1118 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkc5203f92006-03-12 23:27:46 +01001119 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk34b613e2002-12-17 01:51:00 +00001120 }
wdenke7f34c62003-01-11 09:48:40 +00001121 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001122 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001123
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001124 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001125 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001126 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001127 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001128 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001129 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001130 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001131 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001132 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001133
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001134 /*
1135 * have to set environment.image for flash_read(), careful -
1136 * other pointers in environment still point inside addr0
1137 */
1138 environment.image = addr1;
1139 if (flash_io (O_RDONLY))
1140 return -1;
1141
1142 /* Check flag scheme compatibility */
1143 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1144 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1145 environment.flag_scheme = FLAG_BOOLEAN;
1146 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1147 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1148 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001149 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1150 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1151 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001152 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1153 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1154 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001155 } else {
1156 fprintf (stderr, "Incompatible flash types!\n");
1157 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001158 }
wdenk3f9ab982003-04-12 23:38:12 +00001159
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001160 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1161 crc1_ok = (crc1 == redundant->crc);
1162 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001163
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001164 if (crc0_ok && !crc1_ok) {
1165 dev_current = 0;
1166 } else if (!crc0_ok && crc1_ok) {
1167 dev_current = 1;
1168 } else if (!crc0_ok && !crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001169 fprintf (stderr,
1170 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001171 memcpy (environment.data, default_environment,
1172 sizeof default_environment);
1173 dev_current = 0;
1174 } else {
1175 switch (environment.flag_scheme) {
1176 case FLAG_BOOLEAN:
1177 if (flag0 == active_flag &&
1178 flag1 == obsolete_flag) {
1179 dev_current = 0;
1180 } else if (flag0 == obsolete_flag &&
1181 flag1 == active_flag) {
1182 dev_current = 1;
1183 } else if (flag0 == flag1) {
1184 dev_current = 0;
1185 } else if (flag0 == 0xFF) {
1186 dev_current = 0;
1187 } else if (flag1 == 0xFF) {
1188 dev_current = 1;
1189 } else {
1190 dev_current = 0;
1191 }
1192 break;
1193 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001194 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001195 dev_current = 1;
1196 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001197 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001198 dev_current = 0;
Jon Povey93ccd472011-03-11 14:10:56 +09001199 else /* flag1 > flag0 */
1200 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001201 break;
1202 default:
1203 fprintf (stderr, "Unknown flag scheme %u \n",
1204 environment.flag_scheme);
1205 return -1;
1206 }
1207 }
1208
1209 /*
1210 * If we are reading, we don't need the flag and the CRC any
1211 * more, if we are writing, we will re-calculate CRC and update
1212 * flags before writing out
1213 */
1214 if (dev_current) {
1215 environment.image = addr1;
1216 environment.crc = &redundant->crc;
1217 environment.flags = &redundant->flags;
1218 environment.data = redundant->data;
1219 free (addr0);
1220 } else {
1221 environment.image = addr0;
1222 /* Other pointers are already set */
wdenk3f9ab982003-04-12 23:38:12 +00001223 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +00001224 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001225#ifdef DEBUG
1226 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1227#endif
wdenk34b613e2002-12-17 01:51:00 +00001228 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001229 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001230}
1231
1232
wdenk3f9ab982003-04-12 23:38:12 +00001233static int parse_config ()
wdenk34b613e2002-12-17 01:51:00 +00001234{
1235 struct stat st;
1236
wdenke7f34c62003-01-11 09:48:40 +00001237#if defined(CONFIG_FILE)
1238 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
wdenk3f9ab982003-04-12 23:38:12 +00001239 if (get_config (CONFIG_FILE)) {
wdenke7f34c62003-01-11 09:48:40 +00001240 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001241 "Cannot parse config file: %s\n", strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001242 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001243 }
wdenke7f34c62003-01-11 09:48:40 +00001244#else
wdenk3f9ab982003-04-12 23:38:12 +00001245 strcpy (DEVNAME (0), DEVICE1_NAME);
1246 DEVOFFSET (0) = DEVICE1_OFFSET;
1247 ENVSIZE (0) = ENV1_SIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001248 /* Default values are: erase-size=env-size, #sectors=1 */
1249 DEVESIZE (0) = ENVSIZE (0);
1250 ENVSECTORS (0) = 1;
1251#ifdef DEVICE1_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001252 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001253#endif
1254#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001255 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001256#endif
1257
wdenk34b613e2002-12-17 01:51:00 +00001258#ifdef HAVE_REDUND
wdenk3f9ab982003-04-12 23:38:12 +00001259 strcpy (DEVNAME (1), DEVICE2_NAME);
1260 DEVOFFSET (1) = DEVICE2_OFFSET;
1261 ENVSIZE (1) = ENV2_SIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001262 /* Default values are: erase-size=env-size, #sectors=1 */
1263 DEVESIZE (1) = ENVSIZE (1);
1264 ENVSECTORS (1) = 1;
1265#ifdef DEVICE2_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001266 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001267#endif
1268#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001269 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001270#endif
wdenke7f34c62003-01-11 09:48:40 +00001271 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +00001272#endif
wdenke7f34c62003-01-11 09:48:40 +00001273#endif
wdenk3f9ab982003-04-12 23:38:12 +00001274 if (stat (DEVNAME (0), &st)) {
1275 fprintf (stderr,
1276 "Cannot access MTD device %s: %s\n",
1277 DEVNAME (0), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001278 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001279 }
wdenk3f9ab982003-04-12 23:38:12 +00001280
1281 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1282 fprintf (stderr,
1283 "Cannot access MTD device %s: %s\n",
Wolfgang Denk8e6213c2005-09-25 01:20:38 +02001284 DEVNAME (1), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001285 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001286 }
wdenk34b613e2002-12-17 01:51:00 +00001287 return 0;
1288}
wdenke7f34c62003-01-11 09:48:40 +00001289
1290#if defined(CONFIG_FILE)
1291static int get_config (char *fname)
1292{
1293 FILE *fp;
1294 int i = 0;
1295 int rc;
1296 char dump[128];
1297
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001298 fp = fopen (fname, "r");
1299 if (fp == NULL)
1300 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001301
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001302 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenke7f34c62003-01-11 09:48:40 +00001303 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001304 if (dump[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001305 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001306
1307 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1308 DEVNAME (i),
1309 &DEVOFFSET (i),
1310 &ENVSIZE (i),
1311 &DEVESIZE (i),
1312 &ENVSECTORS (i));
1313
Remy Bohmercb969eb2011-02-12 19:06:26 +01001314 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001315 continue;
1316
Remy Bohmercb969eb2011-02-12 19:06:26 +01001317 if (rc < 4)
1318 /* Assume the erase size is the same as the env-size */
1319 DEVESIZE(i) = ENVSIZE(i);
1320
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001321 if (rc < 5)
1322 /* Default - 1 sector */
1323 ENVSECTORS (i) = 1;
wdenke7f34c62003-01-11 09:48:40 +00001324
1325 i++;
1326 }
wdenk3f9ab982003-04-12 23:38:12 +00001327 fclose (fp);
1328
wdenke7f34c62003-01-11 09:48:40 +00001329 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +00001330 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001331 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001332 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001333 } else
1334 return 0;
1335}
1336#endif