blob: 52e0bec0897a0645a0d2de2394e13a92488cbc9f [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00009 */
10
Jörg Krause983487f2015-04-22 21:36:22 +020011#define _GNU_SOURCE
12
Peter Robinson656ee042015-12-09 07:15:33 +000013#include <compiler.h>
wdenk34b613e2002-12-17 01:51:00 +000014#include <errno.h>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060015#include <env_flags.h>
wdenk34b613e2002-12-17 01:51:00 +000016#include <fcntl.h>
Joe Hershbergere79a91f2012-10-12 10:23:37 +000017#include <linux/stringify.h>
Andreas Fenkartd0e12cb2016-03-11 09:39:35 +010018#include <ctype.h>
wdenk34b613e2002-12-17 01:51:00 +000019#include <stdio.h>
20#include <stdlib.h>
21#include <stddef.h>
22#include <string.h>
23#include <sys/types.h>
24#include <sys/ioctl.h>
25#include <sys/stat.h>
26#include <unistd.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010027
28#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020029# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010030# include <linux/mtd/mtd.h>
31#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010032# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010033# include <mtd/mtd-user.h>
34#endif
wdenk34b613e2002-12-17 01:51:00 +000035
Markus Klotzbücher94db5572007-11-27 10:23:20 +010036#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000037
Marek Vasut05ed2a52014-03-05 19:59:52 +010038#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
39
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020040#define min(x, y) ({ \
41 typeof(x) _min1 = (x); \
42 typeof(y) _min2 = (y); \
43 (void) (&_min1 == &_min2); \
44 _min1 < _min2 ? _min1 : _min2; })
45
46struct envdev_s {
Tom Rini313e9122014-03-28 12:03:33 -040047 const char *devname; /* Device name */
wdenk3f9ab982003-04-12 23:38:12 +000048 ulong devoff; /* Device offset */
49 ulong env_size; /* environment size */
50 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020051 ulong env_sectors; /* number of environment sectors */
52 uint8_t mtd_type; /* type of the MTD device */
53};
wdenk34b613e2002-12-17 01:51:00 +000054
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020055static struct envdev_s envdevices[2] =
56{
57 {
58 .mtd_type = MTD_ABSENT,
59 }, {
60 .mtd_type = MTD_ABSENT,
61 },
62};
63static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000064
65#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000066#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000067#define ENVSIZE(i) envdevices[(i)].env_size
68#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020069#define ENVSECTORS(i) envdevices[(i)].env_sectors
70#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk34b613e2002-12-17 01:51:00 +000071
Joe Hershberger671adc42012-10-03 09:38:46 +000072#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000073
Andreas Fenkartb8bf4042016-04-19 22:43:42 +020074static unsigned long usable_envsize;
75#define ENV_SIZE usable_envsize
wdenk34b613e2002-12-17 01:51:00 +000076
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020077struct env_image_single {
78 uint32_t crc; /* CRC32 over data bytes */
79 char data[];
80};
wdenk34b613e2002-12-17 01:51:00 +000081
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020082struct env_image_redundant {
83 uint32_t crc; /* CRC32 over data bytes */
84 unsigned char flags; /* active or obsolete */
85 char data[];
86};
87
88enum flag_scheme {
89 FLAG_NONE,
90 FLAG_BOOLEAN,
91 FLAG_INCREMENTAL,
92};
93
94struct environment {
95 void *image;
96 uint32_t *crc;
97 unsigned char *flags;
98 char *data;
99 enum flag_scheme flag_scheme;
100};
101
102static struct environment environment = {
103 .flag_scheme = FLAG_NONE,
104};
wdenk34b613e2002-12-17 01:51:00 +0000105
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200106static int env_aes_cbc_crypt(char *data, const int enc, uint8_t *key);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100107
wdenke7f34c62003-01-11 09:48:40 +0000108static int HaveRedundEnv = 0;
109
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100110static unsigned char active_flag = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200111/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100112static unsigned char obsolete_flag = 0;
wdenke7f34c62003-01-11 09:48:40 +0000113
Joe Hershberger81321142012-10-12 08:48:51 +0000114#define DEFAULT_ENV_INSTANCE_STATIC
115#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000116
wdenk3f9ab982003-04-12 23:38:12 +0000117static int flash_io (int mode);
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100118static char *envmatch (char * s1, char * s2);
Andreas Fenkart24371902016-04-05 23:13:42 +0200119static int parse_config(struct env_opts *opts);
wdenk3f9ab982003-04-12 23:38:12 +0000120
wdenke7f34c62003-01-11 09:48:40 +0000121#if defined(CONFIG_FILE)
wdenk3f9ab982003-04-12 23:38:12 +0000122static int get_config (char *);
wdenke7f34c62003-01-11 09:48:40 +0000123#endif
wdenk34b613e2002-12-17 01:51:00 +0000124
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100125static char *skip_chars(char *s)
Stefano Babicada628b2010-05-24 12:08:16 +0200126{
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100127 for (; *s != '\0'; s++) {
128 if (isblank(*s))
129 return s;
130 }
131 return NULL;
132}
Stefano Babicada628b2010-05-24 12:08:16 +0200133
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100134static char *skip_blanks(char *s)
135{
136 for (; *s != '\0'; s++) {
137 if (!isblank(*s))
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100138 return s;
Stefano Babicada628b2010-05-24 12:08:16 +0200139 }
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100140 return NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200141}
142
wdenk34b613e2002-12-17 01:51:00 +0000143/*
144 * Search the environment for a variable.
145 * Return the value, if found, or NULL, if not found.
146 */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100147char *fw_getenv (char *name)
wdenk34b613e2002-12-17 01:51:00 +0000148{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100149 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000150
wdenk3f9ab982003-04-12 23:38:12 +0000151 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100152 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000153
wdenk3f9ab982003-04-12 23:38:12 +0000154 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000155 if (nxt >= &environment.data[ENV_SIZE]) {
156 fprintf (stderr, "## Error: "
157 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200158 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000159 }
160 }
wdenk3f9ab982003-04-12 23:38:12 +0000161 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000162 if (!val)
163 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200164 return val;
wdenk34b613e2002-12-17 01:51:00 +0000165 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200166 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000167}
168
169/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600170 * Search the default environment for a variable.
171 * Return the value, if found, or NULL, if not found.
172 */
173char *fw_getdefenv(char *name)
174{
175 char *env, *nxt;
176
177 for (env = default_environment; *env; env = nxt + 1) {
178 char *val;
179
180 for (nxt = env; *nxt; ++nxt) {
181 if (nxt >= &default_environment[ENV_SIZE]) {
182 fprintf(stderr, "## Error: "
183 "default environment not terminated\n");
184 return NULL;
185 }
186 }
187 val = envmatch(name, env);
188 if (!val)
189 continue;
190 return val;
191 }
192 return NULL;
193}
194
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100195int parse_aes_key(char *key, uint8_t *bin_key)
Marek Vasut05ed2a52014-03-05 19:59:52 +0100196{
197 char tmp[5] = { '0', 'x', 0, 0, 0 };
198 unsigned long ul;
199 int i;
200
201 if (strnlen(key, 64) != 32) {
202 fprintf(stderr,
203 "## Error: '-a' option requires 16-byte AES key\n");
204 return -1;
205 }
206
207 for (i = 0; i < 16; i++) {
208 tmp[2] = key[0];
209 tmp[3] = key[1];
210 errno = 0;
211 ul = strtoul(tmp, NULL, 16);
212 if (errno) {
213 fprintf(stderr,
214 "## Error: '-a' option requires valid AES key\n");
215 return -1;
216 }
Andreas Fenkarta34e5c52015-12-09 13:13:22 +0100217 bin_key[i] = ul & 0xff;
Marek Vasut05ed2a52014-03-05 19:59:52 +0100218 key += 2;
219 }
Marek Vasut05ed2a52014-03-05 19:59:52 +0100220 return 0;
221}
222
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600223/*
wdenk34b613e2002-12-17 01:51:00 +0000224 * Print the current definition of one, or more, or all
225 * environment variables
226 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200227int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000228{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100229 char *env, *nxt;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100230 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000231
Andreas Fenkart24371902016-04-05 23:13:42 +0200232 if (fw_env_open(opts))
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200233 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000234
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100235 if (argc == 0) { /* Print all env variables */
wdenk3f9ab982003-04-12 23:38:12 +0000236 for (env = environment.data; *env; env = nxt + 1) {
237 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000238 if (nxt >= &environment.data[ENV_SIZE]) {
239 fprintf (stderr, "## Error: "
240 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200241 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000242 }
243 }
244
wdenk3f9ab982003-04-12 23:38:12 +0000245 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000246 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200247 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000248 }
249
Andreas Fenkart87bcee42016-04-19 22:43:41 +0200250 if (value_only && argc != 1) {
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100251 fprintf(stderr,
252 "## Error: `-n' option requires exactly one argument\n");
253 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000254 }
255
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100256 for (i = 0; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100257 char *name = argv[i];
258 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000259
wdenk3f9ab982003-04-12 23:38:12 +0000260 for (env = environment.data; *env; env = nxt + 1) {
wdenk34b613e2002-12-17 01:51:00 +0000261
wdenk3f9ab982003-04-12 23:38:12 +0000262 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000263 if (nxt >= &environment.data[ENV_SIZE]) {
264 fprintf (stderr, "## Error: "
265 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200266 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000267 }
268 }
wdenk3f9ab982003-04-12 23:38:12 +0000269 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000270 if (val) {
Andreas Fenkart87bcee42016-04-19 22:43:41 +0200271 if (!value_only) {
wdenk34b613e2002-12-17 01:51:00 +0000272 fputs (name, stdout);
wdenk3f9ab982003-04-12 23:38:12 +0000273 putc ('=', stdout);
wdenk34b613e2002-12-17 01:51:00 +0000274 }
wdenk3f9ab982003-04-12 23:38:12 +0000275 puts (val);
wdenk34b613e2002-12-17 01:51:00 +0000276 break;
277 }
278 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700279 if (!val) {
wdenk3f9ab982003-04-12 23:38:12 +0000280 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700281 rc = -1;
282 }
wdenk34b613e2002-12-17 01:51:00 +0000283 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700284
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200285 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000286}
287
Andreas Fenkart24371902016-04-05 23:13:42 +0200288int fw_env_close(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000289{
Marek Vasut05ed2a52014-03-05 19:59:52 +0100290 int ret;
Andreas Fenkart24371902016-04-05 23:13:42 +0200291
292 if (opts->aes_flag) {
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200293 ret = env_aes_cbc_crypt(environment.data, 1,
Andreas Fenkart24371902016-04-05 23:13:42 +0200294 opts->aes_key);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100295 if (ret) {
296 fprintf(stderr,
297 "Error: can't encrypt env for flash\n");
298 return ret;
299 }
300 }
301
Stefano Babicada628b2010-05-24 12:08:16 +0200302 /*
303 * Update CRC
304 */
305 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000306
Stefano Babicada628b2010-05-24 12:08:16 +0200307 /* write environment back to flash */
308 if (flash_io(O_RDWR)) {
309 fprintf(stderr,
310 "Error: can't write fw_env to flash\n");
311 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000312 }
313
Stefano Babicada628b2010-05-24 12:08:16 +0200314 return 0;
315}
wdenk34b613e2002-12-17 01:51:00 +0000316
Stefano Babicada628b2010-05-24 12:08:16 +0200317
318/*
319 * Set/Clear a single variable in the environment.
320 * This is called in sequence to update the environment
321 * in RAM without updating the copy in flash after each set
322 */
323int fw_env_write(char *name, char *value)
324{
325 int len;
326 char *env, *nxt;
327 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600328 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000329
330 /*
331 * search if variable with this name already exists
332 */
wdenk3f9ab982003-04-12 23:38:12 +0000333 for (nxt = env = environment.data; *env; env = nxt + 1) {
334 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000335 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200336 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000337 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200338 errno = EINVAL;
339 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000340 }
341 }
wdenk3f9ab982003-04-12 23:38:12 +0000342 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000343 break;
344 }
345
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600346 deleting = (oldval && !(value && strlen(value)));
347 creating = (!oldval && (value && strlen(value)));
348 overwriting = (oldval && (value && strlen(value)));
349
350 /* check for permission */
351 if (deleting) {
352 if (env_flags_validate_varaccess(name,
353 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
354 printf("Can't delete \"%s\"\n", name);
355 errno = EROFS;
356 return -1;
357 }
358 } else if (overwriting) {
359 if (env_flags_validate_varaccess(name,
360 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
361 printf("Can't overwrite \"%s\"\n", name);
362 errno = EROFS;
363 return -1;
364 } else if (env_flags_validate_varaccess(name,
365 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
366 const char *defval = fw_getdefenv(name);
367
368 if (defval == NULL)
369 defval = "";
370 if (strcmp(oldval, defval)
371 != 0) {
372 printf("Can't overwrite \"%s\"\n", name);
373 errno = EROFS;
374 return -1;
375 }
376 }
377 } else if (creating) {
378 if (env_flags_validate_varaccess(name,
379 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
380 printf("Can't create \"%s\"\n", name);
381 errno = EROFS;
382 return -1;
383 }
384 } else
385 /* Nothing to do */
386 return 0;
387
388 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000389 if (*++nxt == '\0') {
390 *env = '\0';
391 } else {
392 for (;;) {
393 *env = *nxt++;
394 if ((*env == '\0') && (*nxt == '\0'))
395 break;
396 ++env;
397 }
398 }
399 *++env = '\0';
400 }
401
402 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200403 if (!value || !strlen(value))
404 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000405
406 /*
407 * Append new definition at the end
408 */
wdenk3f9ab982003-04-12 23:38:12 +0000409 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000410 if (env > environment.data)
411 ++env;
412 /*
413 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000414 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000415 */
wdenk3f9ab982003-04-12 23:38:12 +0000416 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000417 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200418 len += strlen(value) + 1;
419
wdenk3f9ab982003-04-12 23:38:12 +0000420 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000421 fprintf (stderr,
422 "Error: environment overflow, \"%s\" deleted\n",
423 name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200424 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000425 }
Stefano Babicada628b2010-05-24 12:08:16 +0200426
wdenk34b613e2002-12-17 01:51:00 +0000427 while ((*env = *name++) != '\0')
428 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200429 *env = '=';
430 while ((*++env = *value++) != '\0')
431 ;
432
433 /* end is marked with double '\0' */
434 *++env = '\0';
435
436 return 0;
437}
438
439/*
440 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
441 * 0 - OK
442 * EINVAL - need at least 1 argument
443 * EROFS - certain variables ("ethaddr", "serial#") cannot be
444 * modified or deleted
445 *
446 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200447int fw_setenv(int argc, char *argv[], struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200448{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100449 int i;
Mike Frysinger178ec772012-11-10 19:47:45 +0000450 size_t len;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100451 char *name, **valv;
Stefano Babicada628b2010-05-24 12:08:16 +0200452 char *value = NULL;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100453 int valc;
Stefano Babicada628b2010-05-24 12:08:16 +0200454
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100455 if (argc < 1) {
456 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasut05ed2a52014-03-05 19:59:52 +0100457 errno = EINVAL;
458 return -1;
459 }
460
Andreas Fenkart24371902016-04-05 23:13:42 +0200461 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200462 fprintf(stderr, "Error: environment not initialized\n");
463 return -1;
464 }
465
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100466 name = argv[0];
467 valv = argv + 1;
468 valc = argc - 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200469
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100470 if (env_flags_validate_env_set_params(name, valv, valc) < 0)
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600471 return 1;
472
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000473 len = 0;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100474 for (i = 0; i < valc; ++i) {
475 char *val = valv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000476 size_t val_len = strlen(val);
477
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000478 if (value)
479 value[len - 1] = ' ';
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000480 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200481 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000482 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200483 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000484 len, strerror(errno));
485 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200486 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000487
488 memcpy(value + len, val, val_len);
489 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000490 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000491 }
492
Stefano Babicada628b2010-05-24 12:08:16 +0200493 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000494
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000495 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000496
Andreas Fenkart24371902016-04-05 23:13:42 +0200497 return fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200498}
wdenk34b613e2002-12-17 01:51:00 +0000499
Stefano Babicada628b2010-05-24 12:08:16 +0200500/*
501 * Parse a file and configure the u-boot variables.
502 * The script file has a very simple format, as follows:
503 *
504 * Each line has a couple with name, value:
505 * <white spaces>variable_name<white spaces>variable_value
506 *
507 * Both variable_name and variable_value are interpreted as strings.
508 * Any character after <white spaces> and before ending \r\n is interpreted
509 * as variable's value (no comment allowed on these lines !)
510 *
511 * Comments are allowed if the first character in the line is #
512 *
513 * Returns -1 and sets errno error codes:
514 * 0 - OK
515 * -1 - Error
516 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200517int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200518{
519 FILE *fp;
520 char dump[1024]; /* Maximum line length in the file */
521 char *name;
522 char *val;
523 int lineno = 0;
524 int len;
525 int ret = 0;
526
Andreas Fenkart24371902016-04-05 23:13:42 +0200527 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200528 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200529 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000530 }
531
Stefano Babicada628b2010-05-24 12:08:16 +0200532 if (strcmp(fname, "-") == 0)
533 fp = stdin;
534 else {
535 fp = fopen(fname, "r");
536 if (fp == NULL) {
537 fprintf(stderr, "I cannot open %s for reading\n",
538 fname);
539 return -1;
540 }
541 }
542
543 while (fgets(dump, sizeof(dump), fp)) {
544 lineno++;
545 len = strlen(dump);
546
547 /*
548 * Read a whole line from the file. If the line is too long
549 * or is not terminated, reports an error and exit.
550 */
551 if (dump[len - 1] != '\n') {
552 fprintf(stderr,
553 "Line %d not corrected terminated or too long\n",
554 lineno);
555 ret = -1;
556 break;
557 }
558
559 /* Drop ending line feed / carriage return */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100560 dump[--len] = '\0';
561 if (len && dump[len - 1] == '\r')
562 dump[--len] = '\0';
Stefano Babicada628b2010-05-24 12:08:16 +0200563
564 /* Skip comment or empty lines */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100565 if (len == 0 || dump[0] == '#')
Stefano Babicada628b2010-05-24 12:08:16 +0200566 continue;
567
568 /*
569 * Search for variable's name,
570 * remove leading whitespaces
571 */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100572 name = skip_blanks(dump);
Stefano Babicada628b2010-05-24 12:08:16 +0200573 if (!name)
574 continue;
575
576 /* The first white space is the end of variable name */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100577 val = skip_chars(name);
Stefano Babicada628b2010-05-24 12:08:16 +0200578 len = strlen(name);
579 if (val) {
580 *val++ = '\0';
581 if ((val - name) < len)
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100582 val = skip_blanks(val);
Stefano Babicada628b2010-05-24 12:08:16 +0200583 else
584 val = NULL;
585 }
586
587#ifdef DEBUG
588 fprintf(stderr, "Setting %s : %s\n",
589 name, val ? val : " removed");
590#endif
591
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600592 if (env_flags_validate_type(name, val) < 0) {
593 ret = -1;
594 break;
595 }
596
Stefano Babicada628b2010-05-24 12:08:16 +0200597 /*
598 * If there is an error setting a variable,
599 * try to save the environment and returns an error
600 */
601 if (fw_env_write(name, val)) {
602 fprintf(stderr,
603 "fw_env_write returns with error : %s\n",
604 strerror(errno));
605 ret = -1;
606 break;
607 }
608
609 }
610
611 /* Close file if not stdin */
612 if (strcmp(fname, "-") != 0)
613 fclose(fp);
614
Andreas Fenkart24371902016-04-05 23:13:42 +0200615 ret |= fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200616
617 return ret;
wdenk34b613e2002-12-17 01:51:00 +0000618}
619
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200620/*
621 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
622 * 0 - block is good
623 * > 0 - block is bad
624 * < 0 - failed to test
625 */
626static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000627{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200628 if (mtd_type == MTD_NANDFLASH) {
629 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000630
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200631 if (badblock < 0) {
632 perror ("Cannot read bad block mark");
633 return badblock;
634 }
635
636 if (badblock) {
637#ifdef DEBUG
638 fprintf (stderr, "Bad block at 0x%llx, "
639 "skipping\n", *blockstart);
640#endif
641 return badblock;
642 }
wdenk34b613e2002-12-17 01:51:00 +0000643 }
644
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200645 return 0;
646}
647
648/*
649 * Read data from flash at an offset into a provided buffer. On NAND it skips
650 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
651 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
652 */
653static int flash_read_buf (int dev, int fd, void *buf, size_t count,
654 off_t offset, uint8_t mtd_type)
655{
656 size_t blocklen; /* erase / write length - one block on NAND,
657 0 on NOR */
658 size_t processed = 0; /* progress counter */
659 size_t readlen = count; /* current read length */
660 off_t top_of_range; /* end of the last block we may use */
661 off_t block_seek; /* offset inside the current block to the start
662 of the data */
663 loff_t blockstart; /* running start of the current block -
664 MEMGETBADBLOCK needs 64 bits */
665 int rc;
666
Remy Bohmercb969eb2011-02-12 19:06:26 +0100667 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200668
669 /* Offset inside a block */
670 block_seek = offset - blockstart;
671
672 if (mtd_type == MTD_NANDFLASH) {
673 /*
674 * NAND: calculate which blocks we are reading. We have
675 * to read one block at a time to skip bad blocks.
676 */
677 blocklen = DEVESIZE (dev);
678
679 /*
680 * To calculate the top of the range, we have to use the
681 * global DEVOFFSET (dev), which can be different from offset
682 */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100683 top_of_range = ((DEVOFFSET(dev) / blocklen) +
684 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200685
686 /* Limit to one block for the first read */
687 if (readlen > blocklen - block_seek)
688 readlen = blocklen - block_seek;
689 } else {
690 blocklen = 0;
691 top_of_range = offset + count;
wdenke7f34c62003-01-11 09:48:40 +0000692 }
wdenk34b613e2002-12-17 01:51:00 +0000693
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200694 /* This only runs once on NOR flash */
695 while (processed < count) {
696 rc = flash_bad_block (fd, mtd_type, &blockstart);
697 if (rc < 0) /* block test failed */
698 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000699
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200700 if (blockstart + block_seek + readlen > top_of_range) {
701 /* End of range is reached */
702 fprintf (stderr,
703 "Too few good blocks within range\n");
704 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000705 }
706
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200707 if (rc) { /* block is bad */
708 blockstart += blocklen;
709 continue;
wdenk34b613e2002-12-17 01:51:00 +0000710 }
711
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200712 /*
713 * If a block is bad, we retry in the next block at the same
714 * offset - see common/env_nand.c::writeenv()
715 */
716 lseek (fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000717
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200718 rc = read (fd, buf + processed, readlen);
719 if (rc != readlen) {
720 fprintf (stderr, "Read error on %s: %s\n",
721 DEVNAME (dev), strerror (errno));
722 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000723 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200724#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000725 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
726 rc, blockstart + block_seek, DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200727#endif
728 processed += readlen;
729 readlen = min (blocklen, count - processed);
730 block_seek = 0;
731 blockstart += blocklen;
732 }
733
734 return processed;
735}
736
737/*
Remy Bohmercb969eb2011-02-12 19:06:26 +0100738 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
739 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
740 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200741 */
742static int flash_write_buf (int dev, int fd, void *buf, size_t count,
743 off_t offset, uint8_t mtd_type)
744{
745 void *data;
746 struct erase_info_user erase;
747 size_t blocklen; /* length of NAND block / NOR erase sector */
748 size_t erase_len; /* whole area that can be erased - may include
749 bad blocks */
750 size_t erasesize; /* erase / write length - one block on NAND,
751 whole area on NOR */
752 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100753 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200754 bad blocks */
755 off_t erase_offset; /* offset to the first erase block (aligned)
756 below offset */
757 off_t block_seek; /* offset inside the erase block to the start
758 of the data */
759 off_t top_of_range; /* end of the last block we may use */
760 loff_t blockstart; /* running start of the current block -
761 MEMGETBADBLOCK needs 64 bits */
762 int rc;
763
Oliver Metze9c66ab2013-08-30 00:56:02 +0200764 /*
765 * For mtd devices only offset and size of the environment do matter
766 */
767 if (mtd_type == MTD_ABSENT) {
768 blocklen = count;
769 top_of_range = offset + count;
770 erase_len = blocklen;
771 blockstart = offset;
772 block_seek = 0;
773 write_total = blocklen;
774 } else {
775 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200776
Oliver Metze9c66ab2013-08-30 00:56:02 +0200777 top_of_range = ((DEVOFFSET(dev) / blocklen) +
778 ENVSECTORS(dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200779
Oliver Metze9c66ab2013-08-30 00:56:02 +0200780 erase_offset = (offset / blocklen) * blocklen;
wdenk34b613e2002-12-17 01:51:00 +0000781
Oliver Metze9c66ab2013-08-30 00:56:02 +0200782 /* Maximum area we may use */
783 erase_len = top_of_range - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +0000784
Oliver Metze9c66ab2013-08-30 00:56:02 +0200785 blockstart = erase_offset;
786 /* Offset inside a block */
787 block_seek = offset - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200788
Oliver Metze9c66ab2013-08-30 00:56:02 +0200789 /*
790 * Data size we actually write: from the start of the block
791 * to the start of the data, then count bytes of data, and
792 * to the end of the block
793 */
794 write_total = ((block_seek + count + blocklen - 1) /
795 blocklen) * blocklen;
796 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200797
798 /*
799 * Support data anywhere within erase sectors: read out the complete
800 * area to be erased, replace the environment image, write the whole
801 * block back again.
802 */
803 if (write_total > count) {
804 data = malloc (erase_len);
805 if (!data) {
wdenke7f34c62003-01-11 09:48:40 +0000806 fprintf (stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200807 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200808 erase_len, strerror (errno));
809 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000810 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200811
812 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
813 mtd_type);
814 if (write_total != rc)
815 return -1;
816
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000817#ifdef DEBUG
818 fprintf(stderr, "Preserving data ");
819 if (block_seek != 0)
820 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
821 if (block_seek + count != write_total) {
822 if (block_seek != 0)
823 fprintf(stderr, " and ");
824 fprintf(stderr, "0x%lx - 0x%x",
825 block_seek + count, write_total - 1);
826 }
827 fprintf(stderr, "\n");
828#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200829 /* Overwrite the old environment */
830 memcpy (data + block_seek, buf, count);
831 } else {
832 /*
833 * We get here, iff offset is block-aligned and count is a
834 * multiple of blocklen - see write_total calculation above
835 */
836 data = buf;
837 }
838
839 if (mtd_type == MTD_NANDFLASH) {
840 /*
841 * NAND: calculate which blocks we are writing. We have
842 * to write one block at a time to skip bad blocks.
843 */
844 erasesize = blocklen;
845 } else {
846 erasesize = erase_len;
847 }
848
849 erase.length = erasesize;
850
Remy Bohmercb969eb2011-02-12 19:06:26 +0100851 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200852 while (processed < write_total) {
853 rc = flash_bad_block (fd, mtd_type, &blockstart);
854 if (rc < 0) /* block test failed */
855 return rc;
856
857 if (blockstart + erasesize > top_of_range) {
858 fprintf (stderr, "End of range reached, aborting\n");
859 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000860 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200861
862 if (rc) { /* block is bad */
863 blockstart += blocklen;
864 continue;
865 }
866
Oliver Metze9c66ab2013-08-30 00:56:02 +0200867 if (mtd_type != MTD_ABSENT) {
868 erase.start = blockstart;
869 ioctl(fd, MEMUNLOCK, &erase);
870 /* These do not need an explicit erase cycle */
871 if (mtd_type != MTD_DATAFLASH)
872 if (ioctl(fd, MEMERASE, &erase) != 0) {
873 fprintf(stderr,
874 "MTD erase error on %s: %s\n",
875 DEVNAME(dev), strerror(errno));
876 return -1;
877 }
878 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200879
880 if (lseek (fd, blockstart, SEEK_SET) == -1) {
wdenk34b613e2002-12-17 01:51:00 +0000881 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200882 "Seek error on %s: %s\n",
883 DEVNAME (dev), strerror (errno));
884 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000885 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200886
887#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000888 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
889 blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200890#endif
891 if (write (fd, data + processed, erasesize) != erasesize) {
892 fprintf (stderr, "Write error on %s: %s\n",
893 DEVNAME (dev), strerror (errno));
894 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000895 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200896
Oliver Metze9c66ab2013-08-30 00:56:02 +0200897 if (mtd_type != MTD_ABSENT)
898 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200899
Dustin Byford0983cad2014-03-06 20:48:23 -0800900 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200901 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -0800902 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200903 }
904
905 if (write_total > count)
906 free (data);
907
908 return processed;
909}
910
911/*
912 * Set obsolete flag at offset - NOR flash only
913 */
914static int flash_flag_obsolete (int dev, int fd, off_t offset)
915{
916 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200917 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200918
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200919 erase.start = DEVOFFSET (dev);
920 erase.length = DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200921 /* This relies on the fact, that obsolete_flag == 0 */
922 rc = lseek (fd, offset, SEEK_SET);
923 if (rc < 0) {
924 fprintf (stderr, "Cannot seek to set the flag on %s \n",
925 DEVNAME (dev));
926 return rc;
927 }
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200928 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200929 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200930 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200931 if (rc < 0)
932 perror ("Could not set obsolete flag");
933
934 return rc;
935}
936
Marek Vasut05ed2a52014-03-05 19:59:52 +0100937/* Encrypt or decrypt the environment before writing or reading it. */
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200938static int env_aes_cbc_crypt(char *payload, const int enc, uint8_t *key)
Marek Vasut05ed2a52014-03-05 19:59:52 +0100939{
940 uint8_t *data = (uint8_t *)payload;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +0200941 const int len = usable_envsize;
Marek Vasut05ed2a52014-03-05 19:59:52 +0100942 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
943 uint32_t aes_blocks;
944
945 /* First we expand the key. */
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200946 aes_expand_key(key, key_exp);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100947
948 /* Calculate the number of AES blocks to encrypt. */
949 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
950
951 if (enc)
952 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
953 else
954 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
955
956 return 0;
957}
958
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200959static int flash_write (int fd_current, int fd_target, int dev_target)
960{
961 int rc;
962
963 switch (environment.flag_scheme) {
964 case FLAG_NONE:
965 break;
966 case FLAG_INCREMENTAL:
967 (*environment.flags)++;
968 break;
969 case FLAG_BOOLEAN:
970 *environment.flags = active_flag;
971 break;
972 default:
973 fprintf (stderr, "Unimplemented flash scheme %u \n",
974 environment.flag_scheme);
975 return -1;
976 }
977
978#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000979 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200980 DEVOFFSET (dev_target), DEVNAME (dev_target));
981#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +0100982
Joe Hershberger671adc42012-10-03 09:38:46 +0000983 rc = flash_write_buf(dev_target, fd_target, environment.image,
984 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200985 DEVTYPE(dev_target));
986 if (rc < 0)
987 return rc;
988
989 if (environment.flag_scheme == FLAG_BOOLEAN) {
990 /* Have to set obsolete flag */
991 off_t offset = DEVOFFSET (dev_current) +
992 offsetof (struct env_image_redundant, flags);
993#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000994 fprintf(stderr,
995 "Setting obsolete flag in environment at 0x%lx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200996 DEVOFFSET (dev_current), DEVNAME (dev_current));
997#endif
998 flash_flag_obsolete (dev_current, fd_current, offset);
999 }
1000
1001 return 0;
1002}
1003
1004static int flash_read (int fd)
1005{
1006 struct mtd_info_user mtdinfo;
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001007 struct stat st;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001008 int rc;
1009
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001010 rc = fstat(fd, &st);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001011 if (rc < 0) {
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001012 fprintf(stderr, "Cannot stat the file %s\n",
1013 DEVNAME(dev_current));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001014 return -1;
1015 }
1016
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001017 if (S_ISCHR(st.st_mode)) {
1018 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1019 if (rc < 0) {
1020 fprintf(stderr, "Cannot get MTD information for %s\n",
1021 DEVNAME(dev_current));
1022 return -1;
1023 }
1024 if (mtdinfo.type != MTD_NORFLASH &&
1025 mtdinfo.type != MTD_NANDFLASH &&
Joe Hershberger0c5faa82013-04-08 10:32:51 +00001026 mtdinfo.type != MTD_DATAFLASH &&
1027 mtdinfo.type != MTD_UBIVOLUME) {
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001028 fprintf (stderr, "Unsupported flash type %u on %s\n",
1029 mtdinfo.type, DEVNAME(dev_current));
1030 return -1;
1031 }
1032 } else {
1033 memset(&mtdinfo, 0, sizeof(mtdinfo));
1034 mtdinfo.type = MTD_ABSENT;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001035 }
1036
1037 DEVTYPE(dev_current) = mtdinfo.type;
1038
Joe Hershberger671adc42012-10-03 09:38:46 +00001039 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001040 DEVOFFSET (dev_current), mtdinfo.type);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001041 if (rc != CUR_ENVSIZE)
1042 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001043
Marek Vasut05ed2a52014-03-05 19:59:52 +01001044 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001045}
1046
1047static int flash_io (int mode)
1048{
1049 int fd_current, fd_target, rc, dev_target;
1050
1051 /* dev_current: fd_current, erase_current */
1052 fd_current = open (DEVNAME (dev_current), mode);
1053 if (fd_current < 0) {
1054 fprintf (stderr,
1055 "Can't open %s: %s\n",
1056 DEVNAME (dev_current), strerror (errno));
1057 return -1;
1058 }
1059
1060 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +00001061 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001062 /* switch to next partition for writing */
1063 dev_target = !dev_current;
1064 /* dev_target: fd_target, erase_target */
1065 fd_target = open (DEVNAME (dev_target), mode);
1066 if (fd_target < 0) {
wdenke7f34c62003-01-11 09:48:40 +00001067 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001068 "Can't open %s: %s\n",
1069 DEVNAME (dev_target),
1070 strerror (errno));
1071 rc = -1;
1072 goto exit;
wdenke7f34c62003-01-11 09:48:40 +00001073 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001074 } else {
1075 dev_target = dev_current;
1076 fd_target = fd_current;
wdenk34b613e2002-12-17 01:51:00 +00001077 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001078
1079 rc = flash_write (fd_current, fd_target, dev_target);
1080
wdenke7f34c62003-01-11 09:48:40 +00001081 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001082 if (close (fd_target)) {
wdenke7f34c62003-01-11 09:48:40 +00001083 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001084 "I/O error on %s: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001085 DEVNAME (dev_target),
wdenk3f9ab982003-04-12 23:38:12 +00001086 strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001087 rc = -1;
wdenke7f34c62003-01-11 09:48:40 +00001088 }
wdenk34b613e2002-12-17 01:51:00 +00001089 }
wdenk34b613e2002-12-17 01:51:00 +00001090 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001091 rc = flash_read (fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001092 }
1093
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001094exit:
1095 if (close (fd_current)) {
wdenk34b613e2002-12-17 01:51:00 +00001096 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001097 "I/O error on %s: %s\n",
1098 DEVNAME (dev_current), strerror (errno));
1099 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001100 }
1101
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001102 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001103}
1104
1105/*
1106 * s1 is either a simple 'name', or a 'name=value' pair.
1107 * s2 is a 'name=value' pair.
1108 * If the names match, return the value of s2, else NULL.
1109 */
1110
Markus Klotzbücher94db5572007-11-27 10:23:20 +01001111static char *envmatch (char * s1, char * s2)
wdenk34b613e2002-12-17 01:51:00 +00001112{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +00001113 if (s1 == NULL || s2 == NULL)
1114 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001115
1116 while (*s1 == *s2++)
1117 if (*s1++ == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001118 return s2;
wdenk3f9ab982003-04-12 23:38:12 +00001119 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001120 return s2;
1121 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001122}
1123
1124/*
1125 * Prevent confusion if running from erased flash memory
1126 */
Andreas Fenkart24371902016-04-05 23:13:42 +02001127int fw_env_open(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001128{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001129 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001130 unsigned char flag0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001131 void *addr0;
1132
wdenk34b613e2002-12-17 01:51:00 +00001133 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001134 unsigned char flag1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001135 void *addr1;
wdenke7f34c62003-01-11 09:48:40 +00001136
Marek Vasut05ed2a52014-03-05 19:59:52 +01001137 int ret;
1138
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001139 struct env_image_single *single;
1140 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001141
Andreas Fenkart24371902016-04-05 23:13:42 +02001142 if (parse_config(opts)) /* should fill envdevices */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001143 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001144
Joe Hershberger671adc42012-10-03 09:38:46 +00001145 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001146 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001147 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001148 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001149 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001150 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001151 }
wdenk3f9ab982003-04-12 23:38:12 +00001152
wdenke7f34c62003-01-11 09:48:40 +00001153 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001154 environment.image = addr0;
1155
1156 if (HaveRedundEnv) {
1157 redundant = addr0;
1158 environment.crc = &redundant->crc;
1159 environment.flags = &redundant->flags;
1160 environment.data = redundant->data;
1161 } else {
1162 single = addr0;
1163 environment.crc = &single->crc;
1164 environment.flags = NULL;
1165 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001166 }
wdenk3f9ab982003-04-12 23:38:12 +00001167
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001168 dev_current = 0;
1169 if (flash_io (O_RDONLY))
1170 return -1;
1171
1172 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001173
Andreas Fenkart24371902016-04-05 23:13:42 +02001174 if (opts->aes_flag) {
Andreas Fenkartedcaee62016-04-19 22:43:39 +02001175 ret = env_aes_cbc_crypt(environment.data, 0,
Andreas Fenkart24371902016-04-05 23:13:42 +02001176 opts->aes_key);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001177 if (ret)
1178 return ret;
1179 }
1180
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001181 crc0_ok = (crc0 == *environment.crc);
wdenke7f34c62003-01-11 09:48:40 +00001182 if (!HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001183 if (!crc0_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001184 fprintf (stderr,
1185 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkc5203f92006-03-12 23:27:46 +01001186 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk34b613e2002-12-17 01:51:00 +00001187 }
wdenke7f34c62003-01-11 09:48:40 +00001188 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001189 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001190
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001191 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001192 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001193 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001194 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001195 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001196 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001197 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001198 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001199 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001200
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001201 /*
1202 * have to set environment.image for flash_read(), careful -
1203 * other pointers in environment still point inside addr0
1204 */
1205 environment.image = addr1;
1206 if (flash_io (O_RDONLY))
1207 return -1;
1208
1209 /* Check flag scheme compatibility */
1210 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1211 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1212 environment.flag_scheme = FLAG_BOOLEAN;
1213 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1214 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1215 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001216 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1217 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1218 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001219 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1220 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1221 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001222 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1223 DEVTYPE(!dev_current) == MTD_ABSENT) {
1224 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001225 } else {
1226 fprintf (stderr, "Incompatible flash types!\n");
1227 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001228 }
wdenk3f9ab982003-04-12 23:38:12 +00001229
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001230 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001231
Andreas Fenkart24371902016-04-05 23:13:42 +02001232 if (opts->aes_flag) {
Andreas Fenkartedcaee62016-04-19 22:43:39 +02001233 ret = env_aes_cbc_crypt(redundant->data, 0,
Andreas Fenkart24371902016-04-05 23:13:42 +02001234 opts->aes_key);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001235 if (ret)
1236 return ret;
1237 }
1238
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001239 crc1_ok = (crc1 == redundant->crc);
1240 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001241
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001242 if (crc0_ok && !crc1_ok) {
1243 dev_current = 0;
1244 } else if (!crc0_ok && crc1_ok) {
1245 dev_current = 1;
1246 } else if (!crc0_ok && !crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001247 fprintf (stderr,
1248 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001249 memcpy (environment.data, default_environment,
1250 sizeof default_environment);
1251 dev_current = 0;
1252 } else {
1253 switch (environment.flag_scheme) {
1254 case FLAG_BOOLEAN:
1255 if (flag0 == active_flag &&
1256 flag1 == obsolete_flag) {
1257 dev_current = 0;
1258 } else if (flag0 == obsolete_flag &&
1259 flag1 == active_flag) {
1260 dev_current = 1;
1261 } else if (flag0 == flag1) {
1262 dev_current = 0;
1263 } else if (flag0 == 0xFF) {
1264 dev_current = 0;
1265 } else if (flag1 == 0xFF) {
1266 dev_current = 1;
1267 } else {
1268 dev_current = 0;
1269 }
1270 break;
1271 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001272 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001273 dev_current = 1;
1274 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001275 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001276 dev_current = 0;
Jon Povey93ccd472011-03-11 14:10:56 +09001277 else /* flag1 > flag0 */
1278 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001279 break;
1280 default:
1281 fprintf (stderr, "Unknown flag scheme %u \n",
1282 environment.flag_scheme);
1283 return -1;
1284 }
1285 }
1286
1287 /*
1288 * If we are reading, we don't need the flag and the CRC any
1289 * more, if we are writing, we will re-calculate CRC and update
1290 * flags before writing out
1291 */
1292 if (dev_current) {
1293 environment.image = addr1;
1294 environment.crc = &redundant->crc;
1295 environment.flags = &redundant->flags;
1296 environment.data = redundant->data;
1297 free (addr0);
1298 } else {
1299 environment.image = addr0;
1300 /* Other pointers are already set */
wdenk3f9ab982003-04-12 23:38:12 +00001301 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +00001302 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001303#ifdef DEBUG
1304 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1305#endif
wdenk34b613e2002-12-17 01:51:00 +00001306 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001307 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001308}
1309
1310
Andreas Fenkart24371902016-04-05 23:13:42 +02001311static int parse_config(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001312{
1313 struct stat st;
1314
wdenke7f34c62003-01-11 09:48:40 +00001315#if defined(CONFIG_FILE)
Anatolij Gustschine89ead32016-04-29 22:00:11 +02001316 if (!common_args.config_file)
1317 common_args.config_file = CONFIG_FILE;
1318
wdenke7f34c62003-01-11 09:48:40 +00001319 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart24371902016-04-05 23:13:42 +02001320 if (get_config(opts->config_file)) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001321 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart24371902016-04-05 23:13:42 +02001322 opts->config_file);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001323 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001324 }
wdenke7f34c62003-01-11 09:48:40 +00001325#else
Tom Rini313e9122014-03-28 12:03:33 -04001326 DEVNAME (0) = DEVICE1_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001327 DEVOFFSET (0) = DEVICE1_OFFSET;
1328 ENVSIZE (0) = ENV1_SIZE;
Dustin Byford2f6df462014-03-06 20:48:22 -08001329 /* Default values are: erase-size=env-size */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001330 DEVESIZE (0) = ENVSIZE (0);
Dustin Byford2f6df462014-03-06 20:48:22 -08001331 /* #sectors=env-size/erase-size (rounded up) */
1332 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
Remy Bohmercb969eb2011-02-12 19:06:26 +01001333#ifdef DEVICE1_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001334 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001335#endif
1336#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001337 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001338#endif
1339
wdenk34b613e2002-12-17 01:51:00 +00001340#ifdef HAVE_REDUND
Tom Rini313e9122014-03-28 12:03:33 -04001341 DEVNAME (1) = DEVICE2_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001342 DEVOFFSET (1) = DEVICE2_OFFSET;
1343 ENVSIZE (1) = ENV2_SIZE;
Dustin Byford2f6df462014-03-06 20:48:22 -08001344 /* Default values are: erase-size=env-size */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001345 DEVESIZE (1) = ENVSIZE (1);
Dustin Byford2f6df462014-03-06 20:48:22 -08001346 /* #sectors=env-size/erase-size (rounded up) */
1347 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
Remy Bohmercb969eb2011-02-12 19:06:26 +01001348#ifdef DEVICE2_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001349 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001350#endif
1351#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001352 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001353#endif
wdenke7f34c62003-01-11 09:48:40 +00001354 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +00001355#endif
wdenke7f34c62003-01-11 09:48:40 +00001356#endif
wdenk3f9ab982003-04-12 23:38:12 +00001357 if (stat (DEVNAME (0), &st)) {
1358 fprintf (stderr,
1359 "Cannot access MTD device %s: %s\n",
1360 DEVNAME (0), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001361 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001362 }
wdenk3f9ab982003-04-12 23:38:12 +00001363
1364 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1365 fprintf (stderr,
1366 "Cannot access MTD device %s: %s\n",
Wolfgang Denk8e6213c2005-09-25 01:20:38 +02001367 DEVNAME (1), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001368 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001369 }
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001370
1371 if (HaveRedundEnv && ENVSIZE(0) != ENVSIZE(1)) {
1372 ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1));
1373 fprintf(stderr,
1374 "Redundant environments have inequal size, set to 0x%08lx\n",
1375 ENVSIZE(1));
1376 }
1377
1378 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
1379 if (HaveRedundEnv)
1380 usable_envsize -= sizeof(char);
1381
Andreas Fenkart24371902016-04-05 23:13:42 +02001382 if (opts->aes_flag)
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001383 usable_envsize &= ~(AES_KEY_LENGTH - 1);
1384
wdenk34b613e2002-12-17 01:51:00 +00001385 return 0;
1386}
wdenke7f34c62003-01-11 09:48:40 +00001387
1388#if defined(CONFIG_FILE)
1389static int get_config (char *fname)
1390{
1391 FILE *fp;
1392 int i = 0;
1393 int rc;
1394 char dump[128];
Tom Rini313e9122014-03-28 12:03:33 -04001395 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001396
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001397 fp = fopen (fname, "r");
1398 if (fp == NULL)
1399 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001400
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001401 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenke7f34c62003-01-11 09:48:40 +00001402 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001403 if (dump[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001404 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001405
Tom Rini313e9122014-03-28 12:03:33 -04001406 rc = sscanf (dump, "%ms %lx %lx %lx %lx",
1407 &devname,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001408 &DEVOFFSET (i),
1409 &ENVSIZE (i),
1410 &DEVESIZE (i),
1411 &ENVSECTORS (i));
1412
Remy Bohmercb969eb2011-02-12 19:06:26 +01001413 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001414 continue;
1415
Tom Rini313e9122014-03-28 12:03:33 -04001416 DEVNAME(i) = devname;
1417
Remy Bohmercb969eb2011-02-12 19:06:26 +01001418 if (rc < 4)
1419 /* Assume the erase size is the same as the env-size */
1420 DEVESIZE(i) = ENVSIZE(i);
1421
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001422 if (rc < 5)
Dustin Byford2f6df462014-03-06 20:48:22 -08001423 /* Assume enough env sectors to cover the environment */
1424 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
wdenke7f34c62003-01-11 09:48:40 +00001425
1426 i++;
1427 }
wdenk3f9ab982003-04-12 23:38:12 +00001428 fclose (fp);
1429
wdenke7f34c62003-01-11 09:48:40 +00001430 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +00001431 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001432 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001433 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001434 } else
1435 return 0;
1436}
1437#endif