blob: 577ce2de4725249ba4989d02f78c57595d6f93c6 [file] [log] [blame]
wdenk6aff3112002-12-17 01:51:00 +00001/*
Detlev Zundel0aef7bc2010-07-30 11:22:15 +02002 * (C) Copyright 2000-2010
wdenk6aff3112002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02005 * (C) Copyright 2008
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk6aff3112002-12-17 01:51:00 +00009 */
10
11#include <errno.h>
Joe Hershberger30fd4fad2012-12-11 22:16:32 -060012#include <env_flags.h>
wdenk6aff3112002-12-17 01:51:00 +000013#include <fcntl.h>
Joe Hershberger06134212012-10-12 10:23:37 +000014#include <linux/stringify.h>
wdenk6aff3112002-12-17 01:51:00 +000015#include <stdio.h>
16#include <stdlib.h>
17#include <stddef.h>
18#include <string.h>
19#include <sys/types.h>
20#include <sys/ioctl.h>
21#include <sys/stat.h>
22#include <unistd.h>
wdenk6aff3112002-12-17 01:51:00 +000023
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010024#ifdef MTD_OLD
Wolfgang Denk1711f3b2008-09-02 21:17:36 +020025# include <stdint.h>
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010026# include <linux/mtd/mtd.h>
27#else
Wolfgang Denkc83d7ca2008-01-08 22:58:27 +010028# define __user /* nothing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010029# include <mtd/mtd-user.h>
30#endif
31
32#include "fw_env.h"
wdenk6aff3112002-12-17 01:51:00 +000033
Stefano Babicbd7b26f2010-05-24 12:08:16 +020034#define WHITESPACE(c) ((c == '\t') || (c == ' '))
wdenk6aff3112002-12-17 01:51:00 +000035
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020036#define min(x, y) ({ \
37 typeof(x) _min1 = (x); \
38 typeof(y) _min2 = (y); \
39 (void) (&_min1 == &_min2); \
40 _min1 < _min2 ? _min1 : _min2; })
41
42struct envdev_s {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010043 char devname[16]; /* Device name */
wdenk4a6fd342003-04-12 23:38:12 +000044 ulong devoff; /* Device offset */
45 ulong env_size; /* environment size */
46 ulong erase_size; /* device erase size */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020047 ulong env_sectors; /* number of environment sectors */
48 uint8_t mtd_type; /* type of the MTD device */
49};
wdenk6aff3112002-12-17 01:51:00 +000050
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020051static struct envdev_s envdevices[2] =
52{
53 {
54 .mtd_type = MTD_ABSENT,
55 }, {
56 .mtd_type = MTD_ABSENT,
57 },
58};
59static int dev_current;
wdenk6aff3112002-12-17 01:51:00 +000060
61#define DEVNAME(i) envdevices[(i)].devname
wdenkd0fb80c2003-01-11 09:48:40 +000062#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk6aff3112002-12-17 01:51:00 +000063#define ENVSIZE(i) envdevices[(i)].env_size
64#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020065#define ENVSECTORS(i) envdevices[(i)].env_sectors
66#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk6aff3112002-12-17 01:51:00 +000067
Joe Hershberger497f2052012-10-03 09:38:46 +000068#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk6aff3112002-12-17 01:51:00 +000069
wdenkd0fb80c2003-01-11 09:48:40 +000070#define ENV_SIZE getenvsize()
wdenk6aff3112002-12-17 01:51:00 +000071
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020072struct env_image_single {
73 uint32_t crc; /* CRC32 over data bytes */
74 char data[];
75};
wdenk6aff3112002-12-17 01:51:00 +000076
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020077struct env_image_redundant {
78 uint32_t crc; /* CRC32 over data bytes */
79 unsigned char flags; /* active or obsolete */
80 char data[];
81};
82
83enum flag_scheme {
84 FLAG_NONE,
85 FLAG_BOOLEAN,
86 FLAG_INCREMENTAL,
87};
88
89struct environment {
90 void *image;
91 uint32_t *crc;
92 unsigned char *flags;
93 char *data;
94 enum flag_scheme flag_scheme;
95};
96
97static struct environment environment = {
98 .flag_scheme = FLAG_NONE,
99};
wdenk6aff3112002-12-17 01:51:00 +0000100
wdenkd0fb80c2003-01-11 09:48:40 +0000101static int HaveRedundEnv = 0;
102
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100103static unsigned char active_flag = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200104/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100105static unsigned char obsolete_flag = 0;
wdenkd0fb80c2003-01-11 09:48:40 +0000106
Joe Hershbergerddd84182012-10-12 08:48:51 +0000107#define DEFAULT_ENV_INSTANCE_STATIC
108#include <env_default.h>
wdenk6aff3112002-12-17 01:51:00 +0000109
wdenk4a6fd342003-04-12 23:38:12 +0000110static int flash_io (int mode);
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100111static char *envmatch (char * s1, char * s2);
wdenk4a6fd342003-04-12 23:38:12 +0000112static int parse_config (void);
113
wdenkd0fb80c2003-01-11 09:48:40 +0000114#if defined(CONFIG_FILE)
wdenk4a6fd342003-04-12 23:38:12 +0000115static int get_config (char *);
wdenkd0fb80c2003-01-11 09:48:40 +0000116#endif
wdenk4a6fd342003-04-12 23:38:12 +0000117static inline ulong getenvsize (void)
wdenkd0fb80c2003-01-11 09:48:40 +0000118{
Joe Hershberger497f2052012-10-03 09:38:46 +0000119 ulong rc = CUR_ENVSIZE - sizeof(long);
wdenk4a6fd342003-04-12 23:38:12 +0000120
wdenkd0fb80c2003-01-11 09:48:40 +0000121 if (HaveRedundEnv)
wdenk4a6fd342003-04-12 23:38:12 +0000122 rc -= sizeof (char);
wdenkd0fb80c2003-01-11 09:48:40 +0000123 return rc;
124}
wdenk6aff3112002-12-17 01:51:00 +0000125
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200126static char *fw_string_blank(char *s, int noblank)
127{
128 int i;
129 int len = strlen(s);
130
131 for (i = 0; i < len; i++, s++) {
132 if ((noblank && !WHITESPACE(*s)) ||
133 (!noblank && WHITESPACE(*s)))
134 break;
135 }
136 if (i == len)
137 return NULL;
138
139 return s;
140}
141
wdenk6aff3112002-12-17 01:51:00 +0000142/*
143 * Search the environment for a variable.
144 * Return the value, if found, or NULL, if not found.
145 */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100146char *fw_getenv (char *name)
wdenk6aff3112002-12-17 01:51:00 +0000147{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100148 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000149
wdenk4a6fd342003-04-12 23:38:12 +0000150 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100151 char *val;
wdenk6aff3112002-12-17 01:51:00 +0000152
wdenk4a6fd342003-04-12 23:38:12 +0000153 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000154 if (nxt >= &environment.data[ENV_SIZE]) {
155 fprintf (stderr, "## Error: "
156 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200157 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000158 }
159 }
wdenk4a6fd342003-04-12 23:38:12 +0000160 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000161 if (!val)
162 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200163 return val;
wdenk6aff3112002-12-17 01:51:00 +0000164 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200165 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000166}
167
168/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600169 * Search the default environment for a variable.
170 * Return the value, if found, or NULL, if not found.
171 */
172char *fw_getdefenv(char *name)
173{
174 char *env, *nxt;
175
176 for (env = default_environment; *env; env = nxt + 1) {
177 char *val;
178
179 for (nxt = env; *nxt; ++nxt) {
180 if (nxt >= &default_environment[ENV_SIZE]) {
181 fprintf(stderr, "## Error: "
182 "default environment not terminated\n");
183 return NULL;
184 }
185 }
186 val = envmatch(name, env);
187 if (!val)
188 continue;
189 return val;
190 }
191 return NULL;
192}
193
194/*
wdenk6aff3112002-12-17 01:51:00 +0000195 * Print the current definition of one, or more, or all
196 * environment variables
197 */
Grant Ericksonbc117562008-05-06 20:16:15 -0700198int fw_printenv (int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000199{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100200 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000201 int i, n_flag;
Grant Ericksonbc117562008-05-06 20:16:15 -0700202 int rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000203
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200204 if (fw_env_open())
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200205 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000206
wdenk4a6fd342003-04-12 23:38:12 +0000207 if (argc == 1) { /* Print all env variables */
208 for (env = environment.data; *env; env = nxt + 1) {
209 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000210 if (nxt >= &environment.data[ENV_SIZE]) {
211 fprintf (stderr, "## Error: "
212 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200213 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000214 }
215 }
216
wdenk4a6fd342003-04-12 23:38:12 +0000217 printf ("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000218 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200219 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000220 }
221
wdenk4a6fd342003-04-12 23:38:12 +0000222 if (strcmp (argv[1], "-n") == 0) {
wdenk6aff3112002-12-17 01:51:00 +0000223 n_flag = 1;
224 ++argv;
225 --argc;
226 if (argc != 2) {
227 fprintf (stderr, "## Error: "
228 "`-n' option requires exactly one argument\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200229 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000230 }
231 } else {
232 n_flag = 0;
233 }
234
wdenk4a6fd342003-04-12 23:38:12 +0000235 for (i = 1; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100236 char *name = argv[i];
237 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000238
wdenk4a6fd342003-04-12 23:38:12 +0000239 for (env = environment.data; *env; env = nxt + 1) {
wdenk6aff3112002-12-17 01:51:00 +0000240
wdenk4a6fd342003-04-12 23:38:12 +0000241 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000242 if (nxt >= &environment.data[ENV_SIZE]) {
243 fprintf (stderr, "## Error: "
244 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200245 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000246 }
247 }
wdenk4a6fd342003-04-12 23:38:12 +0000248 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000249 if (val) {
250 if (!n_flag) {
251 fputs (name, stdout);
wdenk4a6fd342003-04-12 23:38:12 +0000252 putc ('=', stdout);
wdenk6aff3112002-12-17 01:51:00 +0000253 }
wdenk4a6fd342003-04-12 23:38:12 +0000254 puts (val);
wdenk6aff3112002-12-17 01:51:00 +0000255 break;
256 }
257 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700258 if (!val) {
wdenk4a6fd342003-04-12 23:38:12 +0000259 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700260 rc = -1;
261 }
wdenk6aff3112002-12-17 01:51:00 +0000262 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700263
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200264 return rc;
wdenk6aff3112002-12-17 01:51:00 +0000265}
266
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200267int fw_env_close(void)
wdenk6aff3112002-12-17 01:51:00 +0000268{
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200269 /*
270 * Update CRC
271 */
272 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000273
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200274 /* write environment back to flash */
275 if (flash_io(O_RDWR)) {
276 fprintf(stderr,
277 "Error: can't write fw_env to flash\n");
278 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000279 }
280
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200281 return 0;
282}
wdenk6aff3112002-12-17 01:51:00 +0000283
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200284
285/*
286 * Set/Clear a single variable in the environment.
287 * This is called in sequence to update the environment
288 * in RAM without updating the copy in flash after each set
289 */
290int fw_env_write(char *name, char *value)
291{
292 int len;
293 char *env, *nxt;
294 char *oldval = NULL;
Joe Hershberger267541f2012-12-11 22:16:34 -0600295 int deleting, creating, overwriting;
wdenk6aff3112002-12-17 01:51:00 +0000296
297 /*
298 * search if variable with this name already exists
299 */
wdenk4a6fd342003-04-12 23:38:12 +0000300 for (nxt = env = environment.data; *env; env = nxt + 1) {
301 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000302 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200303 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000304 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200305 errno = EINVAL;
306 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000307 }
308 }
wdenk4a6fd342003-04-12 23:38:12 +0000309 if ((oldval = envmatch (name, env)) != NULL)
wdenk6aff3112002-12-17 01:51:00 +0000310 break;
311 }
312
Joe Hershberger267541f2012-12-11 22:16:34 -0600313 deleting = (oldval && !(value && strlen(value)));
314 creating = (!oldval && (value && strlen(value)));
315 overwriting = (oldval && (value && strlen(value)));
316
317 /* check for permission */
318 if (deleting) {
319 if (env_flags_validate_varaccess(name,
320 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
321 printf("Can't delete \"%s\"\n", name);
322 errno = EROFS;
323 return -1;
324 }
325 } else if (overwriting) {
326 if (env_flags_validate_varaccess(name,
327 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
328 printf("Can't overwrite \"%s\"\n", name);
329 errno = EROFS;
330 return -1;
331 } else if (env_flags_validate_varaccess(name,
332 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
333 const char *defval = fw_getdefenv(name);
334
335 if (defval == NULL)
336 defval = "";
337 if (strcmp(oldval, defval)
338 != 0) {
339 printf("Can't overwrite \"%s\"\n", name);
340 errno = EROFS;
341 return -1;
342 }
343 }
344 } else if (creating) {
345 if (env_flags_validate_varaccess(name,
346 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
347 printf("Can't create \"%s\"\n", name);
348 errno = EROFS;
349 return -1;
350 }
351 } else
352 /* Nothing to do */
353 return 0;
354
355 if (deleting || overwriting) {
wdenk6aff3112002-12-17 01:51:00 +0000356 if (*++nxt == '\0') {
357 *env = '\0';
358 } else {
359 for (;;) {
360 *env = *nxt++;
361 if ((*env == '\0') && (*nxt == '\0'))
362 break;
363 ++env;
364 }
365 }
366 *++env = '\0';
367 }
368
369 /* Delete only ? */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200370 if (!value || !strlen(value))
371 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000372
373 /*
374 * Append new definition at the end
375 */
wdenk4a6fd342003-04-12 23:38:12 +0000376 for (env = environment.data; *env || *(env + 1); ++env);
wdenk6aff3112002-12-17 01:51:00 +0000377 if (env > environment.data)
378 ++env;
379 /*
380 * Overflow when:
Joe Hershberger497f2052012-10-03 09:38:46 +0000381 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk6aff3112002-12-17 01:51:00 +0000382 */
wdenk4a6fd342003-04-12 23:38:12 +0000383 len = strlen (name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000384 /* add '=' for first arg, ' ' for all others */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200385 len += strlen(value) + 1;
386
wdenk4a6fd342003-04-12 23:38:12 +0000387 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk6aff3112002-12-17 01:51:00 +0000388 fprintf (stderr,
389 "Error: environment overflow, \"%s\" deleted\n",
390 name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200391 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000392 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200393
wdenk6aff3112002-12-17 01:51:00 +0000394 while ((*env = *name++) != '\0')
395 env++;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200396 *env = '=';
397 while ((*++env = *value++) != '\0')
398 ;
wdenk6aff3112002-12-17 01:51:00 +0000399
400 /* end is marked with double '\0' */
401 *++env = '\0';
402
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200403 return 0;
404}
wdenk6aff3112002-12-17 01:51:00 +0000405
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200406/*
407 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
408 * 0 - OK
409 * EINVAL - need at least 1 argument
410 * EROFS - certain variables ("ethaddr", "serial#") cannot be
411 * modified or deleted
412 *
413 */
414int fw_setenv(int argc, char *argv[])
415{
Mike Frysinger3779c8e2012-11-10 19:47:45 +0000416 int i;
417 size_t len;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200418 char *name;
419 char *value = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000420
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200421 if (argc < 2) {
422 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200423 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000424 }
425
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200426 if (fw_env_open()) {
427 fprintf(stderr, "Error: environment not initialized\n");
428 return -1;
429 }
430
431 name = argv[1];
432
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600433 if (env_flags_validate_env_set_params(argc, argv) < 0)
434 return 1;
435
Joe Hershberger62a34a02012-10-03 09:38:47 +0000436 len = 0;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200437 for (i = 2; i < argc; ++i) {
438 char *val = argv[i];
Joe Hershberger62a34a02012-10-03 09:38:47 +0000439 size_t val_len = strlen(val);
440
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000441 if (value)
442 value[len - 1] = ' ';
Joe Hershberger62a34a02012-10-03 09:38:47 +0000443 value = realloc(value, len + val_len + 1);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200444 if (!value) {
Joe Hershberger62a34a02012-10-03 09:38:47 +0000445 fprintf(stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200446 "Cannot malloc %zu bytes: %s\n",
Joe Hershberger62a34a02012-10-03 09:38:47 +0000447 len, strerror(errno));
448 return -1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200449 }
Joe Hershberger62a34a02012-10-03 09:38:47 +0000450
451 memcpy(value + len, val, val_len);
452 len += val_len;
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000453 value[len++] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200454 }
455
456 fw_env_write(name, value);
457
Joe Hershberger62a34a02012-10-03 09:38:47 +0000458 free(value);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200459
460 return fw_env_close();
461}
462
463/*
464 * Parse a file and configure the u-boot variables.
465 * The script file has a very simple format, as follows:
466 *
467 * Each line has a couple with name, value:
468 * <white spaces>variable_name<white spaces>variable_value
469 *
470 * Both variable_name and variable_value are interpreted as strings.
471 * Any character after <white spaces> and before ending \r\n is interpreted
472 * as variable's value (no comment allowed on these lines !)
473 *
474 * Comments are allowed if the first character in the line is #
475 *
476 * Returns -1 and sets errno error codes:
477 * 0 - OK
478 * -1 - Error
479 */
480int fw_parse_script(char *fname)
481{
482 FILE *fp;
483 char dump[1024]; /* Maximum line length in the file */
484 char *name;
485 char *val;
486 int lineno = 0;
487 int len;
488 int ret = 0;
489
490 if (fw_env_open()) {
491 fprintf(stderr, "Error: environment not initialized\n");
492 return -1;
493 }
494
495 if (strcmp(fname, "-") == 0)
496 fp = stdin;
497 else {
498 fp = fopen(fname, "r");
499 if (fp == NULL) {
500 fprintf(stderr, "I cannot open %s for reading\n",
501 fname);
502 return -1;
503 }
504 }
505
506 while (fgets(dump, sizeof(dump), fp)) {
507 lineno++;
508 len = strlen(dump);
509
510 /*
511 * Read a whole line from the file. If the line is too long
512 * or is not terminated, reports an error and exit.
513 */
514 if (dump[len - 1] != '\n') {
515 fprintf(stderr,
516 "Line %d not corrected terminated or too long\n",
517 lineno);
518 ret = -1;
519 break;
520 }
521
522 /* Drop ending line feed / carriage return */
523 while (len > 0 && (dump[len - 1] == '\n' ||
524 dump[len - 1] == '\r')) {
525 dump[len - 1] = '\0';
526 len--;
527 }
528
529 /* Skip comment or empty lines */
530 if ((len == 0) || dump[0] == '#')
531 continue;
532
533 /*
534 * Search for variable's name,
535 * remove leading whitespaces
536 */
537 name = fw_string_blank(dump, 1);
538 if (!name)
539 continue;
540
541 /* The first white space is the end of variable name */
542 val = fw_string_blank(name, 0);
543 len = strlen(name);
544 if (val) {
545 *val++ = '\0';
546 if ((val - name) < len)
547 val = fw_string_blank(val, 1);
548 else
549 val = NULL;
550 }
551
552#ifdef DEBUG
553 fprintf(stderr, "Setting %s : %s\n",
554 name, val ? val : " removed");
555#endif
556
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600557 if (env_flags_validate_type(name, val) < 0) {
558 ret = -1;
559 break;
560 }
561
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200562 /*
563 * If there is an error setting a variable,
564 * try to save the environment and returns an error
565 */
566 if (fw_env_write(name, val)) {
567 fprintf(stderr,
568 "fw_env_write returns with error : %s\n",
569 strerror(errno));
570 ret = -1;
571 break;
572 }
573
574 }
575
576 /* Close file if not stdin */
577 if (strcmp(fname, "-") != 0)
578 fclose(fp);
579
580 ret |= fw_env_close();
581
582 return ret;
583
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200584}
585
586/*
587 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
588 * 0 - block is good
589 * > 0 - block is bad
590 * < 0 - failed to test
591 */
592static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
593{
594 if (mtd_type == MTD_NANDFLASH) {
595 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
596
597 if (badblock < 0) {
598 perror ("Cannot read bad block mark");
599 return badblock;
600 }
601
602 if (badblock) {
603#ifdef DEBUG
604 fprintf (stderr, "Bad block at 0x%llx, "
605 "skipping\n", *blockstart);
606#endif
607 return badblock;
608 }
609 }
610
611 return 0;
612}
613
614/*
615 * Read data from flash at an offset into a provided buffer. On NAND it skips
616 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
617 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
618 */
619static int flash_read_buf (int dev, int fd, void *buf, size_t count,
620 off_t offset, uint8_t mtd_type)
621{
622 size_t blocklen; /* erase / write length - one block on NAND,
623 0 on NOR */
624 size_t processed = 0; /* progress counter */
625 size_t readlen = count; /* current read length */
626 off_t top_of_range; /* end of the last block we may use */
627 off_t block_seek; /* offset inside the current block to the start
628 of the data */
629 loff_t blockstart; /* running start of the current block -
630 MEMGETBADBLOCK needs 64 bits */
631 int rc;
632
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100633 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200634
635 /* Offset inside a block */
636 block_seek = offset - blockstart;
637
638 if (mtd_type == MTD_NANDFLASH) {
639 /*
640 * NAND: calculate which blocks we are reading. We have
641 * to read one block at a time to skip bad blocks.
642 */
643 blocklen = DEVESIZE (dev);
644
645 /*
646 * To calculate the top of the range, we have to use the
647 * global DEVOFFSET (dev), which can be different from offset
648 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100649 top_of_range = ((DEVOFFSET(dev) / blocklen) +
650 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200651
652 /* Limit to one block for the first read */
653 if (readlen > blocklen - block_seek)
654 readlen = blocklen - block_seek;
655 } else {
656 blocklen = 0;
657 top_of_range = offset + count;
658 }
659
660 /* This only runs once on NOR flash */
661 while (processed < count) {
662 rc = flash_bad_block (fd, mtd_type, &blockstart);
663 if (rc < 0) /* block test failed */
664 return -1;
665
666 if (blockstart + block_seek + readlen > top_of_range) {
667 /* End of range is reached */
668 fprintf (stderr,
669 "Too few good blocks within range\n");
670 return -1;
671 }
672
673 if (rc) { /* block is bad */
674 blockstart += blocklen;
675 continue;
676 }
677
678 /*
679 * If a block is bad, we retry in the next block at the same
680 * offset - see common/env_nand.c::writeenv()
681 */
682 lseek (fd, blockstart + block_seek, SEEK_SET);
683
684 rc = read (fd, buf + processed, readlen);
685 if (rc != readlen) {
686 fprintf (stderr, "Read error on %s: %s\n",
687 DEVNAME (dev), strerror (errno));
688 return -1;
689 }
690#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000691 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
692 rc, blockstart + block_seek, DEVNAME(dev));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200693#endif
694 processed += readlen;
695 readlen = min (blocklen, count - processed);
696 block_seek = 0;
697 blockstart += blocklen;
698 }
699
700 return processed;
701}
702
703/*
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100704 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
705 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
706 * erase and write the whole data at once.
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200707 */
708static int flash_write_buf (int dev, int fd, void *buf, size_t count,
709 off_t offset, uint8_t mtd_type)
710{
711 void *data;
712 struct erase_info_user erase;
713 size_t blocklen; /* length of NAND block / NOR erase sector */
714 size_t erase_len; /* whole area that can be erased - may include
715 bad blocks */
716 size_t erasesize; /* erase / write length - one block on NAND,
717 whole area on NOR */
718 size_t processed = 0; /* progress counter */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100719 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200720 bad blocks */
721 off_t erase_offset; /* offset to the first erase block (aligned)
722 below offset */
723 off_t block_seek; /* offset inside the erase block to the start
724 of the data */
725 off_t top_of_range; /* end of the last block we may use */
726 loff_t blockstart; /* running start of the current block -
727 MEMGETBADBLOCK needs 64 bits */
728 int rc;
729
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200730 /*
Oliver Metze387efb2013-08-30 00:56:02 +0200731 * For mtd devices only offset and size of the environment do matter
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200732 */
Oliver Metze387efb2013-08-30 00:56:02 +0200733 if (mtd_type == MTD_ABSENT) {
734 blocklen = count;
735 top_of_range = offset + count;
736 erase_len = blocklen;
737 blockstart = offset;
738 block_seek = 0;
739 write_total = blocklen;
740 } else {
741 blocklen = DEVESIZE(dev);
742
743 top_of_range = ((DEVOFFSET(dev) / blocklen) +
744 ENVSECTORS(dev)) * blocklen;
745
746 erase_offset = (offset / blocklen) * blocklen;
747
748 /* Maximum area we may use */
749 erase_len = top_of_range - erase_offset;
750
751 blockstart = erase_offset;
752 /* Offset inside a block */
753 block_seek = offset - erase_offset;
754
755 /*
756 * Data size we actually write: from the start of the block
757 * to the start of the data, then count bytes of data, and
758 * to the end of the block
759 */
760 write_total = ((block_seek + count + blocklen - 1) /
761 blocklen) * blocklen;
762 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200763
764 /*
765 * Support data anywhere within erase sectors: read out the complete
766 * area to be erased, replace the environment image, write the whole
767 * block back again.
768 */
769 if (write_total > count) {
770 data = malloc (erase_len);
771 if (!data) {
772 fprintf (stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200773 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200774 erase_len, strerror (errno));
775 return -1;
776 }
777
778 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
779 mtd_type);
780 if (write_total != rc)
781 return -1;
782
Joe Hershberger74620e12012-10-15 15:29:25 +0000783#ifdef DEBUG
784 fprintf(stderr, "Preserving data ");
785 if (block_seek != 0)
786 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
787 if (block_seek + count != write_total) {
788 if (block_seek != 0)
789 fprintf(stderr, " and ");
790 fprintf(stderr, "0x%lx - 0x%x",
791 block_seek + count, write_total - 1);
792 }
793 fprintf(stderr, "\n");
794#endif
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200795 /* Overwrite the old environment */
796 memcpy (data + block_seek, buf, count);
797 } else {
798 /*
799 * We get here, iff offset is block-aligned and count is a
800 * multiple of blocklen - see write_total calculation above
801 */
802 data = buf;
803 }
804
805 if (mtd_type == MTD_NANDFLASH) {
806 /*
807 * NAND: calculate which blocks we are writing. We have
808 * to write one block at a time to skip bad blocks.
809 */
810 erasesize = blocklen;
811 } else {
812 erasesize = erase_len;
813 }
814
815 erase.length = erasesize;
816
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100817 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200818 while (processed < write_total) {
819 rc = flash_bad_block (fd, mtd_type, &blockstart);
820 if (rc < 0) /* block test failed */
821 return rc;
822
823 if (blockstart + erasesize > top_of_range) {
824 fprintf (stderr, "End of range reached, aborting\n");
825 return -1;
826 }
827
828 if (rc) { /* block is bad */
829 blockstart += blocklen;
830 continue;
831 }
832
Oliver Metze387efb2013-08-30 00:56:02 +0200833 if (mtd_type != MTD_ABSENT) {
834 erase.start = blockstart;
835 ioctl(fd, MEMUNLOCK, &erase);
836 /* These do not need an explicit erase cycle */
837 if (mtd_type != MTD_DATAFLASH)
838 if (ioctl(fd, MEMERASE, &erase) != 0) {
839 fprintf(stderr,
840 "MTD erase error on %s: %s\n",
841 DEVNAME(dev), strerror(errno));
842 return -1;
843 }
844 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200845
846 if (lseek (fd, blockstart, SEEK_SET) == -1) {
847 fprintf (stderr,
848 "Seek error on %s: %s\n",
849 DEVNAME (dev), strerror (errno));
850 return -1;
851 }
852
853#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000854 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
855 blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200856#endif
857 if (write (fd, data + processed, erasesize) != erasesize) {
858 fprintf (stderr, "Write error on %s: %s\n",
859 DEVNAME (dev), strerror (errno));
860 return -1;
861 }
862
Oliver Metze387efb2013-08-30 00:56:02 +0200863 if (mtd_type != MTD_ABSENT)
864 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200865
866 processed += blocklen;
867 block_seek = 0;
868 blockstart += blocklen;
869 }
870
871 if (write_total > count)
872 free (data);
873
874 return processed;
875}
876
877/*
878 * Set obsolete flag at offset - NOR flash only
879 */
880static int flash_flag_obsolete (int dev, int fd, off_t offset)
881{
882 int rc;
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200883 struct erase_info_user erase;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200884
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200885 erase.start = DEVOFFSET (dev);
886 erase.length = DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200887 /* This relies on the fact, that obsolete_flag == 0 */
888 rc = lseek (fd, offset, SEEK_SET);
889 if (rc < 0) {
890 fprintf (stderr, "Cannot seek to set the flag on %s \n",
891 DEVNAME (dev));
892 return rc;
893 }
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200894 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200895 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200896 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200897 if (rc < 0)
898 perror ("Could not set obsolete flag");
899
900 return rc;
901}
902
903static int flash_write (int fd_current, int fd_target, int dev_target)
904{
905 int rc;
906
907 switch (environment.flag_scheme) {
908 case FLAG_NONE:
909 break;
910 case FLAG_INCREMENTAL:
911 (*environment.flags)++;
912 break;
913 case FLAG_BOOLEAN:
914 *environment.flags = active_flag;
915 break;
916 default:
917 fprintf (stderr, "Unimplemented flash scheme %u \n",
918 environment.flag_scheme);
919 return -1;
920 }
921
922#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000923 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200924 DEVOFFSET (dev_target), DEVNAME (dev_target));
925#endif
Joe Hershberger497f2052012-10-03 09:38:46 +0000926 rc = flash_write_buf(dev_target, fd_target, environment.image,
927 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200928 DEVTYPE(dev_target));
929 if (rc < 0)
930 return rc;
931
932 if (environment.flag_scheme == FLAG_BOOLEAN) {
933 /* Have to set obsolete flag */
934 off_t offset = DEVOFFSET (dev_current) +
935 offsetof (struct env_image_redundant, flags);
936#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000937 fprintf(stderr,
938 "Setting obsolete flag in environment at 0x%lx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200939 DEVOFFSET (dev_current), DEVNAME (dev_current));
940#endif
941 flash_flag_obsolete (dev_current, fd_current, offset);
942 }
943
944 return 0;
945}
946
947static int flash_read (int fd)
948{
949 struct mtd_info_user mtdinfo;
Lubomir Rintelf1932b72013-02-10 01:02:36 +0000950 struct stat st;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200951 int rc;
952
Lubomir Rintelf1932b72013-02-10 01:02:36 +0000953 rc = fstat(fd, &st);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200954 if (rc < 0) {
Lubomir Rintelf1932b72013-02-10 01:02:36 +0000955 fprintf(stderr, "Cannot stat the file %s\n",
956 DEVNAME(dev_current));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200957 return -1;
958 }
959
Lubomir Rintelf1932b72013-02-10 01:02:36 +0000960 if (S_ISCHR(st.st_mode)) {
961 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
962 if (rc < 0) {
963 fprintf(stderr, "Cannot get MTD information for %s\n",
964 DEVNAME(dev_current));
965 return -1;
966 }
967 if (mtdinfo.type != MTD_NORFLASH &&
968 mtdinfo.type != MTD_NANDFLASH &&
Joe Hershberger2b744332013-04-08 10:32:51 +0000969 mtdinfo.type != MTD_DATAFLASH &&
970 mtdinfo.type != MTD_UBIVOLUME) {
Lubomir Rintelf1932b72013-02-10 01:02:36 +0000971 fprintf (stderr, "Unsupported flash type %u on %s\n",
972 mtdinfo.type, DEVNAME(dev_current));
973 return -1;
974 }
975 } else {
976 memset(&mtdinfo, 0, sizeof(mtdinfo));
977 mtdinfo.type = MTD_ABSENT;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200978 }
979
980 DEVTYPE(dev_current) = mtdinfo.type;
981
Joe Hershberger497f2052012-10-03 09:38:46 +0000982 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200983 DEVOFFSET (dev_current), mtdinfo.type);
984
Joe Hershberger497f2052012-10-03 09:38:46 +0000985 return (rc != CUR_ENVSIZE) ? -1 : 0;
wdenk6aff3112002-12-17 01:51:00 +0000986}
987
988static int flash_io (int mode)
989{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200990 int fd_current, fd_target, rc, dev_target;
wdenk6aff3112002-12-17 01:51:00 +0000991
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200992 /* dev_current: fd_current, erase_current */
993 fd_current = open (DEVNAME (dev_current), mode);
994 if (fd_current < 0) {
wdenk4a6fd342003-04-12 23:38:12 +0000995 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200996 "Can't open %s: %s\n",
997 DEVNAME (dev_current), strerror (errno));
998 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +0000999 }
wdenk6aff3112002-12-17 01:51:00 +00001000
1001 if (mode == O_RDWR) {
wdenkd0fb80c2003-01-11 09:48:40 +00001002 if (HaveRedundEnv) {
1003 /* switch to next partition for writing */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001004 dev_target = !dev_current;
1005 /* dev_target: fd_target, erase_target */
1006 fd_target = open (DEVNAME (dev_target), mode);
1007 if (fd_target < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001008 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001009 "Can't open %s: %s\n",
1010 DEVNAME (dev_target),
1011 strerror (errno));
1012 rc = -1;
1013 goto exit;
wdenkd0fb80c2003-01-11 09:48:40 +00001014 }
1015 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001016 dev_target = dev_current;
1017 fd_target = fd_current;
wdenk6aff3112002-12-17 01:51:00 +00001018 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001019
1020 rc = flash_write (fd_current, fd_target, dev_target);
wdenk6aff3112002-12-17 01:51:00 +00001021
wdenkd0fb80c2003-01-11 09:48:40 +00001022 if (HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001023 if (close (fd_target)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001024 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001025 "I/O error on %s: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001026 DEVNAME (dev_target),
wdenk4a6fd342003-04-12 23:38:12 +00001027 strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001028 rc = -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001029 }
wdenk6aff3112002-12-17 01:51:00 +00001030 }
wdenk6aff3112002-12-17 01:51:00 +00001031 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001032 rc = flash_read (fd_current);
wdenk6aff3112002-12-17 01:51:00 +00001033 }
1034
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001035exit:
1036 if (close (fd_current)) {
wdenk6aff3112002-12-17 01:51:00 +00001037 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001038 "I/O error on %s: %s\n",
1039 DEVNAME (dev_current), strerror (errno));
1040 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001041 }
1042
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001043 return rc;
wdenk6aff3112002-12-17 01:51:00 +00001044}
1045
1046/*
1047 * s1 is either a simple 'name', or a 'name=value' pair.
1048 * s2 is a 'name=value' pair.
1049 * If the names match, return the value of s2, else NULL.
1050 */
1051
Markus Klotzbücher6de66b32007-11-27 10:23:20 +01001052static char *envmatch (char * s1, char * s2)
wdenk6aff3112002-12-17 01:51:00 +00001053{
Joe Hershberger586197d2012-10-03 09:38:50 +00001054 if (s1 == NULL || s2 == NULL)
1055 return NULL;
wdenk6aff3112002-12-17 01:51:00 +00001056
1057 while (*s1 == *s2++)
1058 if (*s1++ == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001059 return s2;
wdenk4a6fd342003-04-12 23:38:12 +00001060 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001061 return s2;
1062 return NULL;
wdenk6aff3112002-12-17 01:51:00 +00001063}
1064
1065/*
1066 * Prevent confusion if running from erased flash memory
1067 */
Stefano Babicbd7b26f2010-05-24 12:08:16 +02001068int fw_env_open(void)
wdenk6aff3112002-12-17 01:51:00 +00001069{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001070 int crc0, crc0_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001071 unsigned char flag0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001072 void *addr0;
wdenkd0fb80c2003-01-11 09:48:40 +00001073
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001074 int crc1, crc1_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001075 unsigned char flag1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001076 void *addr1;
1077
1078 struct env_image_single *single;
1079 struct env_image_redundant *redundant;
wdenk6aff3112002-12-17 01:51:00 +00001080
wdenk4a6fd342003-04-12 23:38:12 +00001081 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001082 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001083
Joe Hershberger497f2052012-10-03 09:38:46 +00001084 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001085 if (addr0 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001086 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001087 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001088 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001089 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001090 }
wdenk4a6fd342003-04-12 23:38:12 +00001091
wdenkd0fb80c2003-01-11 09:48:40 +00001092 /* read environment from FLASH to local buffer */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001093 environment.image = addr0;
1094
1095 if (HaveRedundEnv) {
1096 redundant = addr0;
1097 environment.crc = &redundant->crc;
1098 environment.flags = &redundant->flags;
1099 environment.data = redundant->data;
1100 } else {
1101 single = addr0;
1102 environment.crc = &single->crc;
1103 environment.flags = NULL;
1104 environment.data = single->data;
wdenkd0fb80c2003-01-11 09:48:40 +00001105 }
wdenk4a6fd342003-04-12 23:38:12 +00001106
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001107 dev_current = 0;
1108 if (flash_io (O_RDONLY))
1109 return -1;
1110
1111 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1112 crc0_ok = (crc0 == *environment.crc);
wdenkd0fb80c2003-01-11 09:48:40 +00001113 if (!HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001114 if (!crc0_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001115 fprintf (stderr,
1116 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +01001117 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +00001118 }
wdenkd0fb80c2003-01-11 09:48:40 +00001119 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001120 flag0 = *environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +00001121
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001122 dev_current = 1;
Joe Hershberger497f2052012-10-03 09:38:46 +00001123 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001124 if (addr1 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001125 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001126 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001127 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001128 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001129 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001130 redundant = addr1;
wdenk4a6fd342003-04-12 23:38:12 +00001131
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001132 /*
1133 * have to set environment.image for flash_read(), careful -
1134 * other pointers in environment still point inside addr0
1135 */
1136 environment.image = addr1;
1137 if (flash_io (O_RDONLY))
1138 return -1;
1139
1140 /* Check flag scheme compatibility */
1141 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1142 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1143 environment.flag_scheme = FLAG_BOOLEAN;
1144 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1145 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1146 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001147 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1148 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1149 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershberger785881f2013-04-08 10:32:52 +00001150 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1151 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1152 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metz23ef62d2013-08-30 00:56:01 +02001153 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1154 DEVTYPE(!dev_current) == MTD_ABSENT) {
1155 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001156 } else {
1157 fprintf (stderr, "Incompatible flash types!\n");
1158 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001159 }
wdenk4a6fd342003-04-12 23:38:12 +00001160
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001161 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1162 crc1_ok = (crc1 == redundant->crc);
1163 flag1 = redundant->flags;
wdenk4a6fd342003-04-12 23:38:12 +00001164
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001165 if (crc0_ok && !crc1_ok) {
1166 dev_current = 0;
1167 } else if (!crc0_ok && crc1_ok) {
1168 dev_current = 1;
1169 } else if (!crc0_ok && !crc1_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001170 fprintf (stderr,
1171 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001172 memcpy (environment.data, default_environment,
1173 sizeof default_environment);
1174 dev_current = 0;
1175 } else {
1176 switch (environment.flag_scheme) {
1177 case FLAG_BOOLEAN:
1178 if (flag0 == active_flag &&
1179 flag1 == obsolete_flag) {
1180 dev_current = 0;
1181 } else if (flag0 == obsolete_flag &&
1182 flag1 == active_flag) {
1183 dev_current = 1;
1184 } else if (flag0 == flag1) {
1185 dev_current = 0;
1186 } else if (flag0 == 0xFF) {
1187 dev_current = 0;
1188 } else if (flag1 == 0xFF) {
1189 dev_current = 1;
1190 } else {
1191 dev_current = 0;
1192 }
1193 break;
1194 case FLAG_INCREMENTAL:
Jon Povey735eb0f2011-03-11 14:10:56 +09001195 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001196 dev_current = 1;
1197 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey735eb0f2011-03-11 14:10:56 +09001198 flag0 >= flag1)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001199 dev_current = 0;
Jon Povey735eb0f2011-03-11 14:10:56 +09001200 else /* flag1 > flag0 */
1201 dev_current = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001202 break;
1203 default:
1204 fprintf (stderr, "Unknown flag scheme %u \n",
1205 environment.flag_scheme);
1206 return -1;
1207 }
1208 }
1209
1210 /*
1211 * If we are reading, we don't need the flag and the CRC any
1212 * more, if we are writing, we will re-calculate CRC and update
1213 * flags before writing out
1214 */
1215 if (dev_current) {
1216 environment.image = addr1;
1217 environment.crc = &redundant->crc;
1218 environment.flags = &redundant->flags;
1219 environment.data = redundant->data;
1220 free (addr0);
1221 } else {
1222 environment.image = addr0;
1223 /* Other pointers are already set */
wdenk4a6fd342003-04-12 23:38:12 +00001224 free (addr1);
wdenk6aff3112002-12-17 01:51:00 +00001225 }
Joe Hershberger74620e12012-10-15 15:29:25 +00001226#ifdef DEBUG
1227 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1228#endif
wdenk6aff3112002-12-17 01:51:00 +00001229 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001230 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001231}
1232
1233
wdenk4a6fd342003-04-12 23:38:12 +00001234static int parse_config ()
wdenk6aff3112002-12-17 01:51:00 +00001235{
1236 struct stat st;
1237
wdenkd0fb80c2003-01-11 09:48:40 +00001238#if defined(CONFIG_FILE)
1239 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
wdenk4a6fd342003-04-12 23:38:12 +00001240 if (get_config (CONFIG_FILE)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001241 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001242 "Cannot parse config file: %s\n", strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001243 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001244 }
wdenkd0fb80c2003-01-11 09:48:40 +00001245#else
wdenk4a6fd342003-04-12 23:38:12 +00001246 strcpy (DEVNAME (0), DEVICE1_NAME);
1247 DEVOFFSET (0) = DEVICE1_OFFSET;
1248 ENVSIZE (0) = ENV1_SIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001249 /* Default values are: erase-size=env-size, #sectors=1 */
1250 DEVESIZE (0) = ENVSIZE (0);
1251 ENVSECTORS (0) = 1;
1252#ifdef DEVICE1_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001253 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001254#endif
1255#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001256 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001257#endif
1258
wdenk6aff3112002-12-17 01:51:00 +00001259#ifdef HAVE_REDUND
wdenk4a6fd342003-04-12 23:38:12 +00001260 strcpy (DEVNAME (1), DEVICE2_NAME);
1261 DEVOFFSET (1) = DEVICE2_OFFSET;
1262 ENVSIZE (1) = ENV2_SIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001263 /* Default values are: erase-size=env-size, #sectors=1 */
1264 DEVESIZE (1) = ENVSIZE (1);
1265 ENVSECTORS (1) = 1;
1266#ifdef DEVICE2_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001267 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001268#endif
1269#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001270 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001271#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001272 HaveRedundEnv = 1;
wdenk6aff3112002-12-17 01:51:00 +00001273#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001274#endif
wdenk4a6fd342003-04-12 23:38:12 +00001275 if (stat (DEVNAME (0), &st)) {
1276 fprintf (stderr,
1277 "Cannot access MTD device %s: %s\n",
1278 DEVNAME (0), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001279 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001280 }
wdenk4a6fd342003-04-12 23:38:12 +00001281
1282 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1283 fprintf (stderr,
1284 "Cannot access MTD device %s: %s\n",
Wolfgang Denke2146b62005-09-25 01:20:38 +02001285 DEVNAME (1), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001286 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001287 }
wdenk6aff3112002-12-17 01:51:00 +00001288 return 0;
1289}
wdenkd0fb80c2003-01-11 09:48:40 +00001290
1291#if defined(CONFIG_FILE)
1292static int get_config (char *fname)
1293{
1294 FILE *fp;
1295 int i = 0;
1296 int rc;
1297 char dump[128];
1298
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001299 fp = fopen (fname, "r");
1300 if (fp == NULL)
1301 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001302
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001303 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001304 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001305 if (dump[0] == '#')
wdenkd0fb80c2003-01-11 09:48:40 +00001306 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001307
1308 rc = sscanf (dump, "%s %lx %lx %lx %lx",
1309 DEVNAME (i),
1310 &DEVOFFSET (i),
1311 &ENVSIZE (i),
1312 &DEVESIZE (i),
1313 &ENVSECTORS (i));
1314
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001315 if (rc < 3)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001316 continue;
1317
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001318 if (rc < 4)
1319 /* Assume the erase size is the same as the env-size */
1320 DEVESIZE(i) = ENVSIZE(i);
1321
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001322 if (rc < 5)
1323 /* Default - 1 sector */
1324 ENVSECTORS (i) = 1;
wdenkd0fb80c2003-01-11 09:48:40 +00001325
1326 i++;
1327 }
wdenk4a6fd342003-04-12 23:38:12 +00001328 fclose (fp);
1329
wdenkd0fb80c2003-01-11 09:48:40 +00001330 HaveRedundEnv = i - 1;
wdenk4a6fd342003-04-12 23:38:12 +00001331 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +00001332 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001333 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001334 } else
1335 return 0;
1336}
1337#endif