blob: 34ceebda9b854d1011b8fcd16a3d36fa1a6d9d5e [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>
Stefan Agnerb21e6022016-07-13 17:14:38 -070017#include <linux/fs.h>
Joe Hershbergere79a91f2012-10-12 10:23:37 +000018#include <linux/stringify.h>
Andreas Fenkartd0e12cb2016-03-11 09:39:35 +010019#include <ctype.h>
wdenk34b613e2002-12-17 01:51:00 +000020#include <stdio.h>
21#include <stdlib.h>
22#include <stddef.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/ioctl.h>
26#include <sys/stat.h>
27#include <unistd.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010028
29#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020030# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010031# include <linux/mtd/mtd.h>
32#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010033# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010034# include <mtd/mtd-user.h>
35#endif
wdenk34b613e2002-12-17 01:51:00 +000036
Markus Klotzbücher94db5572007-11-27 10:23:20 +010037#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000038
Andreas Fenkarte6511302016-05-31 09:21:56 +020039struct env_opts default_opts = {
40#ifdef CONFIG_FILE
41 .config_file = CONFIG_FILE
42#endif
43};
44
Marek Vasut05ed2a52014-03-05 19:59:52 +010045#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
46
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020047#define min(x, y) ({ \
48 typeof(x) _min1 = (x); \
49 typeof(y) _min2 = (y); \
50 (void) (&_min1 == &_min2); \
51 _min1 < _min2 ? _min1 : _min2; })
52
53struct envdev_s {
Tom Rini313e9122014-03-28 12:03:33 -040054 const char *devname; /* Device name */
Stefan Agnerb21e6022016-07-13 17:14:38 -070055 long long devoff; /* Device offset */
wdenk3f9ab982003-04-12 23:38:12 +000056 ulong env_size; /* environment size */
57 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020058 ulong env_sectors; /* number of environment sectors */
59 uint8_t mtd_type; /* type of the MTD device */
60};
wdenk34b613e2002-12-17 01:51:00 +000061
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020062static struct envdev_s envdevices[2] =
63{
64 {
65 .mtd_type = MTD_ABSENT,
66 }, {
67 .mtd_type = MTD_ABSENT,
68 },
69};
70static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000071
72#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000073#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000074#define ENVSIZE(i) envdevices[(i)].env_size
75#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020076#define ENVSECTORS(i) envdevices[(i)].env_sectors
77#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk34b613e2002-12-17 01:51:00 +000078
Joe Hershberger671adc42012-10-03 09:38:46 +000079#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000080
Andreas Fenkartb8bf4042016-04-19 22:43:42 +020081static unsigned long usable_envsize;
82#define ENV_SIZE usable_envsize
wdenk34b613e2002-12-17 01:51:00 +000083
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020084struct env_image_single {
85 uint32_t crc; /* CRC32 over data bytes */
86 char data[];
87};
wdenk34b613e2002-12-17 01:51:00 +000088
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020089struct env_image_redundant {
90 uint32_t crc; /* CRC32 over data bytes */
91 unsigned char flags; /* active or obsolete */
92 char data[];
93};
94
95enum flag_scheme {
96 FLAG_NONE,
97 FLAG_BOOLEAN,
98 FLAG_INCREMENTAL,
99};
100
101struct environment {
102 void *image;
103 uint32_t *crc;
104 unsigned char *flags;
105 char *data;
106 enum flag_scheme flag_scheme;
107};
108
109static struct environment environment = {
110 .flag_scheme = FLAG_NONE,
111};
wdenk34b613e2002-12-17 01:51:00 +0000112
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200113static int env_aes_cbc_crypt(char *data, const int enc, uint8_t *key);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100114
wdenke7f34c62003-01-11 09:48:40 +0000115static int HaveRedundEnv = 0;
116
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100117static unsigned char active_flag = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200118/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100119static unsigned char obsolete_flag = 0;
wdenke7f34c62003-01-11 09:48:40 +0000120
Joe Hershberger81321142012-10-12 08:48:51 +0000121#define DEFAULT_ENV_INSTANCE_STATIC
122#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000123
wdenk3f9ab982003-04-12 23:38:12 +0000124static int flash_io (int mode);
Andreas Fenkart24371902016-04-05 23:13:42 +0200125static int parse_config(struct env_opts *opts);
wdenk3f9ab982003-04-12 23:38:12 +0000126
wdenke7f34c62003-01-11 09:48:40 +0000127#if defined(CONFIG_FILE)
wdenk3f9ab982003-04-12 23:38:12 +0000128static int get_config (char *);
wdenke7f34c62003-01-11 09:48:40 +0000129#endif
wdenk34b613e2002-12-17 01:51:00 +0000130
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100131static char *skip_chars(char *s)
Stefano Babicada628b2010-05-24 12:08:16 +0200132{
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100133 for (; *s != '\0'; s++) {
134 if (isblank(*s))
135 return s;
136 }
137 return NULL;
138}
Stefano Babicada628b2010-05-24 12:08:16 +0200139
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100140static char *skip_blanks(char *s)
141{
142 for (; *s != '\0'; s++) {
143 if (!isblank(*s))
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100144 return s;
Stefano Babicada628b2010-05-24 12:08:16 +0200145 }
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100146 return NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200147}
148
wdenk34b613e2002-12-17 01:51:00 +0000149/*
Andreas Fenkart3ff10742016-07-16 17:06:14 +0200150 * s1 is either a simple 'name', or a 'name=value' pair.
151 * s2 is a 'name=value' pair.
152 * If the names match, return the value of s2, else NULL.
153 */
154static char *envmatch(char *s1, char *s2)
155{
156 if (s1 == NULL || s2 == NULL)
157 return NULL;
158
159 while (*s1 == *s2++)
160 if (*s1++ == '=')
161 return s2;
162 if (*s1 == '\0' && *(s2 - 1) == '=')
163 return s2;
164 return NULL;
165}
166
167/**
wdenk34b613e2002-12-17 01:51:00 +0000168 * Search the environment for a variable.
169 * Return the value, if found, or NULL, if not found.
170 */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100171char *fw_getenv (char *name)
wdenk34b613e2002-12-17 01:51:00 +0000172{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100173 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000174
wdenk3f9ab982003-04-12 23:38:12 +0000175 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100176 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000177
wdenk3f9ab982003-04-12 23:38:12 +0000178 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000179 if (nxt >= &environment.data[ENV_SIZE]) {
180 fprintf (stderr, "## Error: "
181 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200182 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000183 }
184 }
wdenk3f9ab982003-04-12 23:38:12 +0000185 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000186 if (!val)
187 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200188 return val;
wdenk34b613e2002-12-17 01:51:00 +0000189 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200190 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000191}
192
193/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600194 * Search the default environment for a variable.
195 * Return the value, if found, or NULL, if not found.
196 */
197char *fw_getdefenv(char *name)
198{
199 char *env, *nxt;
200
201 for (env = default_environment; *env; env = nxt + 1) {
202 char *val;
203
204 for (nxt = env; *nxt; ++nxt) {
205 if (nxt >= &default_environment[ENV_SIZE]) {
206 fprintf(stderr, "## Error: "
207 "default environment not terminated\n");
208 return NULL;
209 }
210 }
211 val = envmatch(name, env);
212 if (!val)
213 continue;
214 return val;
215 }
216 return NULL;
217}
218
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100219int parse_aes_key(char *key, uint8_t *bin_key)
Marek Vasut05ed2a52014-03-05 19:59:52 +0100220{
221 char tmp[5] = { '0', 'x', 0, 0, 0 };
222 unsigned long ul;
223 int i;
224
225 if (strnlen(key, 64) != 32) {
226 fprintf(stderr,
227 "## Error: '-a' option requires 16-byte AES key\n");
228 return -1;
229 }
230
231 for (i = 0; i < 16; i++) {
232 tmp[2] = key[0];
233 tmp[3] = key[1];
234 errno = 0;
235 ul = strtoul(tmp, NULL, 16);
236 if (errno) {
237 fprintf(stderr,
238 "## Error: '-a' option requires valid AES key\n");
239 return -1;
240 }
Andreas Fenkarta34e5c52015-12-09 13:13:22 +0100241 bin_key[i] = ul & 0xff;
Marek Vasut05ed2a52014-03-05 19:59:52 +0100242 key += 2;
243 }
Marek Vasut05ed2a52014-03-05 19:59:52 +0100244 return 0;
245}
246
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600247/*
wdenk34b613e2002-12-17 01:51:00 +0000248 * Print the current definition of one, or more, or all
249 * environment variables
250 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200251int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000252{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100253 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000254
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200255 if (value_only && argc != 1) {
256 fprintf(stderr,
257 "## Error: `-n' option requires exactly one argument\n");
258 return -1;
259 }
260
Andreas Fenkarte6511302016-05-31 09:21:56 +0200261 if (!opts)
262 opts = &default_opts;
263
Andreas Fenkart24371902016-04-05 23:13:42 +0200264 if (fw_env_open(opts))
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200265 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000266
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100267 if (argc == 0) { /* Print all env variables */
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200268 char *env, *nxt;
wdenk3f9ab982003-04-12 23:38:12 +0000269 for (env = environment.data; *env; env = nxt + 1) {
270 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000271 if (nxt >= &environment.data[ENV_SIZE]) {
272 fprintf (stderr, "## Error: "
273 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200274 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000275 }
276 }
277
wdenk3f9ab982003-04-12 23:38:12 +0000278 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000279 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200280 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000281 }
282
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200283 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100284 char *name = argv[i];
285 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000286
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200287 val = fw_getenv(name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700288 if (!val) {
wdenk3f9ab982003-04-12 23:38:12 +0000289 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700290 rc = -1;
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200291 continue;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700292 }
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200293
294 if (value_only) {
295 puts(val);
296 break;
297 }
298
299 printf("%s=%s\n", name, val);
wdenk34b613e2002-12-17 01:51:00 +0000300 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700301
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200302 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000303}
304
Andreas Fenkart24371902016-04-05 23:13:42 +0200305int fw_env_close(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000306{
Marek Vasut05ed2a52014-03-05 19:59:52 +0100307 int ret;
Andreas Fenkart24371902016-04-05 23:13:42 +0200308
Andreas Fenkarte6511302016-05-31 09:21:56 +0200309 if (!opts)
310 opts = &default_opts;
311
Andreas Fenkart24371902016-04-05 23:13:42 +0200312 if (opts->aes_flag) {
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200313 ret = env_aes_cbc_crypt(environment.data, 1,
Andreas Fenkart24371902016-04-05 23:13:42 +0200314 opts->aes_key);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100315 if (ret) {
316 fprintf(stderr,
317 "Error: can't encrypt env for flash\n");
318 return ret;
319 }
320 }
321
Stefano Babicada628b2010-05-24 12:08:16 +0200322 /*
323 * Update CRC
324 */
325 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000326
Stefano Babicada628b2010-05-24 12:08:16 +0200327 /* write environment back to flash */
328 if (flash_io(O_RDWR)) {
329 fprintf(stderr,
330 "Error: can't write fw_env to flash\n");
331 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000332 }
333
Stefano Babicada628b2010-05-24 12:08:16 +0200334 return 0;
335}
wdenk34b613e2002-12-17 01:51:00 +0000336
Stefano Babicada628b2010-05-24 12:08:16 +0200337
338/*
339 * Set/Clear a single variable in the environment.
340 * This is called in sequence to update the environment
341 * in RAM without updating the copy in flash after each set
342 */
343int fw_env_write(char *name, char *value)
344{
345 int len;
346 char *env, *nxt;
347 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600348 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000349
350 /*
351 * search if variable with this name already exists
352 */
wdenk3f9ab982003-04-12 23:38:12 +0000353 for (nxt = env = environment.data; *env; env = nxt + 1) {
354 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000355 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200356 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000357 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200358 errno = EINVAL;
359 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000360 }
361 }
wdenk3f9ab982003-04-12 23:38:12 +0000362 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000363 break;
364 }
365
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600366 deleting = (oldval && !(value && strlen(value)));
367 creating = (!oldval && (value && strlen(value)));
368 overwriting = (oldval && (value && strlen(value)));
369
370 /* check for permission */
371 if (deleting) {
372 if (env_flags_validate_varaccess(name,
373 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
374 printf("Can't delete \"%s\"\n", name);
375 errno = EROFS;
376 return -1;
377 }
378 } else if (overwriting) {
379 if (env_flags_validate_varaccess(name,
380 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
381 printf("Can't overwrite \"%s\"\n", name);
382 errno = EROFS;
383 return -1;
384 } else if (env_flags_validate_varaccess(name,
385 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
386 const char *defval = fw_getdefenv(name);
387
388 if (defval == NULL)
389 defval = "";
390 if (strcmp(oldval, defval)
391 != 0) {
392 printf("Can't overwrite \"%s\"\n", name);
393 errno = EROFS;
394 return -1;
395 }
396 }
397 } else if (creating) {
398 if (env_flags_validate_varaccess(name,
399 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
400 printf("Can't create \"%s\"\n", name);
401 errno = EROFS;
402 return -1;
403 }
404 } else
405 /* Nothing to do */
406 return 0;
407
408 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000409 if (*++nxt == '\0') {
410 *env = '\0';
411 } else {
412 for (;;) {
413 *env = *nxt++;
414 if ((*env == '\0') && (*nxt == '\0'))
415 break;
416 ++env;
417 }
418 }
419 *++env = '\0';
420 }
421
422 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200423 if (!value || !strlen(value))
424 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000425
426 /*
427 * Append new definition at the end
428 */
wdenk3f9ab982003-04-12 23:38:12 +0000429 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000430 if (env > environment.data)
431 ++env;
432 /*
433 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000434 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000435 */
wdenk3f9ab982003-04-12 23:38:12 +0000436 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000437 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200438 len += strlen(value) + 1;
439
wdenk3f9ab982003-04-12 23:38:12 +0000440 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000441 fprintf (stderr,
442 "Error: environment overflow, \"%s\" deleted\n",
443 name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200444 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000445 }
Stefano Babicada628b2010-05-24 12:08:16 +0200446
wdenk34b613e2002-12-17 01:51:00 +0000447 while ((*env = *name++) != '\0')
448 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200449 *env = '=';
450 while ((*++env = *value++) != '\0')
451 ;
452
453 /* end is marked with double '\0' */
454 *++env = '\0';
455
456 return 0;
457}
458
459/*
460 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
461 * 0 - OK
462 * EINVAL - need at least 1 argument
463 * EROFS - certain variables ("ethaddr", "serial#") cannot be
464 * modified or deleted
465 *
466 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200467int fw_setenv(int argc, char *argv[], struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200468{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100469 int i;
Mike Frysinger178ec772012-11-10 19:47:45 +0000470 size_t len;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100471 char *name, **valv;
Stefano Babicada628b2010-05-24 12:08:16 +0200472 char *value = NULL;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100473 int valc;
Stefano Babicada628b2010-05-24 12:08:16 +0200474
Andreas Fenkarte6511302016-05-31 09:21:56 +0200475 if (!opts)
476 opts = &default_opts;
477
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100478 if (argc < 1) {
479 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasut05ed2a52014-03-05 19:59:52 +0100480 errno = EINVAL;
481 return -1;
482 }
483
Andreas Fenkart24371902016-04-05 23:13:42 +0200484 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200485 fprintf(stderr, "Error: environment not initialized\n");
486 return -1;
487 }
488
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100489 name = argv[0];
490 valv = argv + 1;
491 valc = argc - 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200492
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100493 if (env_flags_validate_env_set_params(name, valv, valc) < 0)
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200494 return -1;
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600495
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000496 len = 0;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100497 for (i = 0; i < valc; ++i) {
498 char *val = valv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000499 size_t val_len = strlen(val);
500
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000501 if (value)
502 value[len - 1] = ' ';
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000503 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200504 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000505 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200506 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000507 len, strerror(errno));
508 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200509 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000510
511 memcpy(value + len, val, val_len);
512 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000513 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000514 }
515
Stefano Babicada628b2010-05-24 12:08:16 +0200516 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000517
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000518 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000519
Andreas Fenkart24371902016-04-05 23:13:42 +0200520 return fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200521}
wdenk34b613e2002-12-17 01:51:00 +0000522
Stefano Babicada628b2010-05-24 12:08:16 +0200523/*
524 * Parse a file and configure the u-boot variables.
525 * The script file has a very simple format, as follows:
526 *
527 * Each line has a couple with name, value:
528 * <white spaces>variable_name<white spaces>variable_value
529 *
530 * Both variable_name and variable_value are interpreted as strings.
531 * Any character after <white spaces> and before ending \r\n is interpreted
532 * as variable's value (no comment allowed on these lines !)
533 *
534 * Comments are allowed if the first character in the line is #
535 *
536 * Returns -1 and sets errno error codes:
537 * 0 - OK
538 * -1 - Error
539 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200540int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200541{
542 FILE *fp;
543 char dump[1024]; /* Maximum line length in the file */
544 char *name;
545 char *val;
546 int lineno = 0;
547 int len;
548 int ret = 0;
549
Andreas Fenkarte6511302016-05-31 09:21:56 +0200550 if (!opts)
551 opts = &default_opts;
552
Andreas Fenkart24371902016-04-05 23:13:42 +0200553 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200554 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200555 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000556 }
557
Stefano Babicada628b2010-05-24 12:08:16 +0200558 if (strcmp(fname, "-") == 0)
559 fp = stdin;
560 else {
561 fp = fopen(fname, "r");
562 if (fp == NULL) {
563 fprintf(stderr, "I cannot open %s for reading\n",
564 fname);
565 return -1;
566 }
567 }
568
569 while (fgets(dump, sizeof(dump), fp)) {
570 lineno++;
571 len = strlen(dump);
572
573 /*
574 * Read a whole line from the file. If the line is too long
575 * or is not terminated, reports an error and exit.
576 */
577 if (dump[len - 1] != '\n') {
578 fprintf(stderr,
579 "Line %d not corrected terminated or too long\n",
580 lineno);
581 ret = -1;
582 break;
583 }
584
585 /* Drop ending line feed / carriage return */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100586 dump[--len] = '\0';
587 if (len && dump[len - 1] == '\r')
588 dump[--len] = '\0';
Stefano Babicada628b2010-05-24 12:08:16 +0200589
590 /* Skip comment or empty lines */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100591 if (len == 0 || dump[0] == '#')
Stefano Babicada628b2010-05-24 12:08:16 +0200592 continue;
593
594 /*
595 * Search for variable's name,
596 * remove leading whitespaces
597 */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100598 name = skip_blanks(dump);
Stefano Babicada628b2010-05-24 12:08:16 +0200599 if (!name)
600 continue;
601
602 /* The first white space is the end of variable name */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100603 val = skip_chars(name);
Stefano Babicada628b2010-05-24 12:08:16 +0200604 len = strlen(name);
605 if (val) {
606 *val++ = '\0';
607 if ((val - name) < len)
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100608 val = skip_blanks(val);
Stefano Babicada628b2010-05-24 12:08:16 +0200609 else
610 val = NULL;
611 }
612
613#ifdef DEBUG
614 fprintf(stderr, "Setting %s : %s\n",
615 name, val ? val : " removed");
616#endif
617
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600618 if (env_flags_validate_type(name, val) < 0) {
619 ret = -1;
620 break;
621 }
622
Stefano Babicada628b2010-05-24 12:08:16 +0200623 /*
624 * If there is an error setting a variable,
625 * try to save the environment and returns an error
626 */
627 if (fw_env_write(name, val)) {
628 fprintf(stderr,
629 "fw_env_write returns with error : %s\n",
630 strerror(errno));
631 ret = -1;
632 break;
633 }
634
635 }
636
637 /* Close file if not stdin */
638 if (strcmp(fname, "-") != 0)
639 fclose(fp);
640
Andreas Fenkart24371902016-04-05 23:13:42 +0200641 ret |= fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200642
643 return ret;
wdenk34b613e2002-12-17 01:51:00 +0000644}
645
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200646/*
647 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
648 * 0 - block is good
649 * > 0 - block is bad
650 * < 0 - failed to test
651 */
652static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000653{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200654 if (mtd_type == MTD_NANDFLASH) {
655 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000656
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200657 if (badblock < 0) {
658 perror ("Cannot read bad block mark");
659 return badblock;
660 }
661
662 if (badblock) {
663#ifdef DEBUG
664 fprintf (stderr, "Bad block at 0x%llx, "
665 "skipping\n", *blockstart);
666#endif
667 return badblock;
668 }
wdenk34b613e2002-12-17 01:51:00 +0000669 }
670
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200671 return 0;
672}
673
674/*
675 * Read data from flash at an offset into a provided buffer. On NAND it skips
676 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
677 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
678 */
679static int flash_read_buf (int dev, int fd, void *buf, size_t count,
680 off_t offset, uint8_t mtd_type)
681{
682 size_t blocklen; /* erase / write length - one block on NAND,
683 0 on NOR */
684 size_t processed = 0; /* progress counter */
685 size_t readlen = count; /* current read length */
686 off_t top_of_range; /* end of the last block we may use */
687 off_t block_seek; /* offset inside the current block to the start
688 of the data */
689 loff_t blockstart; /* running start of the current block -
690 MEMGETBADBLOCK needs 64 bits */
691 int rc;
692
Remy Bohmercb969eb2011-02-12 19:06:26 +0100693 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200694
695 /* Offset inside a block */
696 block_seek = offset - blockstart;
697
698 if (mtd_type == MTD_NANDFLASH) {
699 /*
700 * NAND: calculate which blocks we are reading. We have
701 * to read one block at a time to skip bad blocks.
702 */
703 blocklen = DEVESIZE (dev);
704
705 /*
706 * To calculate the top of the range, we have to use the
707 * global DEVOFFSET (dev), which can be different from offset
708 */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100709 top_of_range = ((DEVOFFSET(dev) / blocklen) +
710 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200711
712 /* Limit to one block for the first read */
713 if (readlen > blocklen - block_seek)
714 readlen = blocklen - block_seek;
715 } else {
716 blocklen = 0;
717 top_of_range = offset + count;
wdenke7f34c62003-01-11 09:48:40 +0000718 }
wdenk34b613e2002-12-17 01:51:00 +0000719
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200720 /* This only runs once on NOR flash */
721 while (processed < count) {
722 rc = flash_bad_block (fd, mtd_type, &blockstart);
723 if (rc < 0) /* block test failed */
724 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000725
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200726 if (blockstart + block_seek + readlen > top_of_range) {
727 /* End of range is reached */
728 fprintf (stderr,
729 "Too few good blocks within range\n");
730 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000731 }
732
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200733 if (rc) { /* block is bad */
734 blockstart += blocklen;
735 continue;
wdenk34b613e2002-12-17 01:51:00 +0000736 }
737
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200738 /*
739 * If a block is bad, we retry in the next block at the same
740 * offset - see common/env_nand.c::writeenv()
741 */
742 lseek (fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000743
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200744 rc = read (fd, buf + processed, readlen);
745 if (rc != readlen) {
746 fprintf (stderr, "Read error on %s: %s\n",
747 DEVNAME (dev), strerror (errno));
748 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000749 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200750#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000751 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
752 rc, blockstart + block_seek, DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200753#endif
754 processed += readlen;
755 readlen = min (blocklen, count - processed);
756 block_seek = 0;
757 blockstart += blocklen;
758 }
759
760 return processed;
761}
762
763/*
Remy Bohmercb969eb2011-02-12 19:06:26 +0100764 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
765 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
766 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200767 */
768static int flash_write_buf (int dev, int fd, void *buf, size_t count,
769 off_t offset, uint8_t mtd_type)
770{
771 void *data;
772 struct erase_info_user erase;
773 size_t blocklen; /* length of NAND block / NOR erase sector */
774 size_t erase_len; /* whole area that can be erased - may include
775 bad blocks */
776 size_t erasesize; /* erase / write length - one block on NAND,
777 whole area on NOR */
778 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100779 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200780 bad blocks */
781 off_t erase_offset; /* offset to the first erase block (aligned)
782 below offset */
783 off_t block_seek; /* offset inside the erase block to the start
784 of the data */
785 off_t top_of_range; /* end of the last block we may use */
786 loff_t blockstart; /* running start of the current block -
787 MEMGETBADBLOCK needs 64 bits */
788 int rc;
789
Oliver Metze9c66ab2013-08-30 00:56:02 +0200790 /*
791 * For mtd devices only offset and size of the environment do matter
792 */
793 if (mtd_type == MTD_ABSENT) {
794 blocklen = count;
795 top_of_range = offset + count;
796 erase_len = blocklen;
797 blockstart = offset;
798 block_seek = 0;
799 write_total = blocklen;
800 } else {
801 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200802
Oliver Metze9c66ab2013-08-30 00:56:02 +0200803 top_of_range = ((DEVOFFSET(dev) / blocklen) +
804 ENVSECTORS(dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200805
Oliver Metze9c66ab2013-08-30 00:56:02 +0200806 erase_offset = (offset / blocklen) * blocklen;
wdenk34b613e2002-12-17 01:51:00 +0000807
Oliver Metze9c66ab2013-08-30 00:56:02 +0200808 /* Maximum area we may use */
809 erase_len = top_of_range - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +0000810
Oliver Metze9c66ab2013-08-30 00:56:02 +0200811 blockstart = erase_offset;
812 /* Offset inside a block */
813 block_seek = offset - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200814
Oliver Metze9c66ab2013-08-30 00:56:02 +0200815 /*
816 * Data size we actually write: from the start of the block
817 * to the start of the data, then count bytes of data, and
818 * to the end of the block
819 */
820 write_total = ((block_seek + count + blocklen - 1) /
821 blocklen) * blocklen;
822 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200823
824 /*
825 * Support data anywhere within erase sectors: read out the complete
826 * area to be erased, replace the environment image, write the whole
827 * block back again.
828 */
829 if (write_total > count) {
830 data = malloc (erase_len);
831 if (!data) {
wdenke7f34c62003-01-11 09:48:40 +0000832 fprintf (stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200833 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200834 erase_len, strerror (errno));
835 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000836 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200837
838 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
839 mtd_type);
840 if (write_total != rc)
841 return -1;
842
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000843#ifdef DEBUG
844 fprintf(stderr, "Preserving data ");
845 if (block_seek != 0)
846 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
847 if (block_seek + count != write_total) {
848 if (block_seek != 0)
849 fprintf(stderr, " and ");
850 fprintf(stderr, "0x%lx - 0x%x",
851 block_seek + count, write_total - 1);
852 }
853 fprintf(stderr, "\n");
854#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200855 /* Overwrite the old environment */
856 memcpy (data + block_seek, buf, count);
857 } else {
858 /*
859 * We get here, iff offset is block-aligned and count is a
860 * multiple of blocklen - see write_total calculation above
861 */
862 data = buf;
863 }
864
865 if (mtd_type == MTD_NANDFLASH) {
866 /*
867 * NAND: calculate which blocks we are writing. We have
868 * to write one block at a time to skip bad blocks.
869 */
870 erasesize = blocklen;
871 } else {
872 erasesize = erase_len;
873 }
874
875 erase.length = erasesize;
876
Remy Bohmercb969eb2011-02-12 19:06:26 +0100877 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200878 while (processed < write_total) {
879 rc = flash_bad_block (fd, mtd_type, &blockstart);
880 if (rc < 0) /* block test failed */
881 return rc;
882
883 if (blockstart + erasesize > top_of_range) {
884 fprintf (stderr, "End of range reached, aborting\n");
885 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000886 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200887
888 if (rc) { /* block is bad */
889 blockstart += blocklen;
890 continue;
891 }
892
Oliver Metze9c66ab2013-08-30 00:56:02 +0200893 if (mtd_type != MTD_ABSENT) {
894 erase.start = blockstart;
895 ioctl(fd, MEMUNLOCK, &erase);
896 /* These do not need an explicit erase cycle */
897 if (mtd_type != MTD_DATAFLASH)
898 if (ioctl(fd, MEMERASE, &erase) != 0) {
899 fprintf(stderr,
900 "MTD erase error on %s: %s\n",
901 DEVNAME(dev), strerror(errno));
902 return -1;
903 }
904 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200905
906 if (lseek (fd, blockstart, SEEK_SET) == -1) {
wdenk34b613e2002-12-17 01:51:00 +0000907 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200908 "Seek error on %s: %s\n",
909 DEVNAME (dev), strerror (errno));
910 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000911 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200912
913#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000914 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
915 blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200916#endif
917 if (write (fd, data + processed, erasesize) != erasesize) {
918 fprintf (stderr, "Write error on %s: %s\n",
919 DEVNAME (dev), strerror (errno));
920 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000921 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200922
Oliver Metze9c66ab2013-08-30 00:56:02 +0200923 if (mtd_type != MTD_ABSENT)
924 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200925
Dustin Byford0983cad2014-03-06 20:48:23 -0800926 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200927 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -0800928 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200929 }
930
931 if (write_total > count)
932 free (data);
933
934 return processed;
935}
936
937/*
938 * Set obsolete flag at offset - NOR flash only
939 */
940static int flash_flag_obsolete (int dev, int fd, off_t offset)
941{
942 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200943 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200944
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200945 erase.start = DEVOFFSET (dev);
946 erase.length = DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200947 /* This relies on the fact, that obsolete_flag == 0 */
948 rc = lseek (fd, offset, SEEK_SET);
949 if (rc < 0) {
950 fprintf (stderr, "Cannot seek to set the flag on %s \n",
951 DEVNAME (dev));
952 return rc;
953 }
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200954 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200955 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200956 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200957 if (rc < 0)
958 perror ("Could not set obsolete flag");
959
960 return rc;
961}
962
Marek Vasut05ed2a52014-03-05 19:59:52 +0100963/* Encrypt or decrypt the environment before writing or reading it. */
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200964static int env_aes_cbc_crypt(char *payload, const int enc, uint8_t *key)
Marek Vasut05ed2a52014-03-05 19:59:52 +0100965{
966 uint8_t *data = (uint8_t *)payload;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +0200967 const int len = usable_envsize;
Marek Vasut05ed2a52014-03-05 19:59:52 +0100968 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
969 uint32_t aes_blocks;
970
971 /* First we expand the key. */
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200972 aes_expand_key(key, key_exp);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100973
974 /* Calculate the number of AES blocks to encrypt. */
975 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
976
977 if (enc)
978 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
979 else
980 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
981
982 return 0;
983}
984
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200985static int flash_write (int fd_current, int fd_target, int dev_target)
986{
987 int rc;
988
989 switch (environment.flag_scheme) {
990 case FLAG_NONE:
991 break;
992 case FLAG_INCREMENTAL:
993 (*environment.flags)++;
994 break;
995 case FLAG_BOOLEAN:
996 *environment.flags = active_flag;
997 break;
998 default:
999 fprintf (stderr, "Unimplemented flash scheme %u \n",
1000 environment.flag_scheme);
1001 return -1;
1002 }
1003
1004#ifdef DEBUG
Stefan Agnerb21e6022016-07-13 17:14:38 -07001005 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001006 DEVOFFSET (dev_target), DEVNAME (dev_target));
1007#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +01001008
Joe Hershberger671adc42012-10-03 09:38:46 +00001009 rc = flash_write_buf(dev_target, fd_target, environment.image,
1010 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001011 DEVTYPE(dev_target));
1012 if (rc < 0)
1013 return rc;
1014
1015 if (environment.flag_scheme == FLAG_BOOLEAN) {
1016 /* Have to set obsolete flag */
1017 off_t offset = DEVOFFSET (dev_current) +
1018 offsetof (struct env_image_redundant, flags);
1019#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001020 fprintf(stderr,
Stefan Agnerb21e6022016-07-13 17:14:38 -07001021 "Setting obsolete flag in environment at 0x%llx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001022 DEVOFFSET (dev_current), DEVNAME (dev_current));
1023#endif
1024 flash_flag_obsolete (dev_current, fd_current, offset);
1025 }
1026
1027 return 0;
1028}
1029
1030static int flash_read (int fd)
1031{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001032 int rc;
1033
Joe Hershberger671adc42012-10-03 09:38:46 +00001034 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001035 DEVOFFSET(dev_current), DEVTYPE(dev_current));
Marek Vasut05ed2a52014-03-05 19:59:52 +01001036 if (rc != CUR_ENVSIZE)
1037 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001038
Marek Vasut05ed2a52014-03-05 19:59:52 +01001039 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001040}
1041
1042static int flash_io (int mode)
1043{
1044 int fd_current, fd_target, rc, dev_target;
1045
1046 /* dev_current: fd_current, erase_current */
1047 fd_current = open (DEVNAME (dev_current), mode);
1048 if (fd_current < 0) {
1049 fprintf (stderr,
1050 "Can't open %s: %s\n",
1051 DEVNAME (dev_current), strerror (errno));
1052 return -1;
1053 }
1054
1055 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +00001056 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001057 /* switch to next partition for writing */
1058 dev_target = !dev_current;
1059 /* dev_target: fd_target, erase_target */
1060 fd_target = open (DEVNAME (dev_target), mode);
1061 if (fd_target < 0) {
wdenke7f34c62003-01-11 09:48:40 +00001062 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001063 "Can't open %s: %s\n",
1064 DEVNAME (dev_target),
1065 strerror (errno));
1066 rc = -1;
1067 goto exit;
wdenke7f34c62003-01-11 09:48:40 +00001068 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001069 } else {
1070 dev_target = dev_current;
1071 fd_target = fd_current;
wdenk34b613e2002-12-17 01:51:00 +00001072 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001073
1074 rc = flash_write (fd_current, fd_target, dev_target);
1075
wdenke7f34c62003-01-11 09:48:40 +00001076 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001077 if (close (fd_target)) {
wdenke7f34c62003-01-11 09:48:40 +00001078 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001079 "I/O error on %s: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001080 DEVNAME (dev_target),
wdenk3f9ab982003-04-12 23:38:12 +00001081 strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001082 rc = -1;
wdenke7f34c62003-01-11 09:48:40 +00001083 }
wdenk34b613e2002-12-17 01:51:00 +00001084 }
wdenk34b613e2002-12-17 01:51:00 +00001085 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001086 rc = flash_read (fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001087 }
1088
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001089exit:
1090 if (close (fd_current)) {
wdenk34b613e2002-12-17 01:51:00 +00001091 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001092 "I/O error on %s: %s\n",
1093 DEVNAME (dev_current), strerror (errno));
1094 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001095 }
1096
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001097 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001098}
1099
1100/*
wdenk34b613e2002-12-17 01:51:00 +00001101 * Prevent confusion if running from erased flash memory
1102 */
Andreas Fenkart24371902016-04-05 23:13:42 +02001103int fw_env_open(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001104{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001105 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001106 unsigned char flag0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001107 void *addr0;
1108
wdenk34b613e2002-12-17 01:51:00 +00001109 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001110 unsigned char flag1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001111 void *addr1;
wdenke7f34c62003-01-11 09:48:40 +00001112
Marek Vasut05ed2a52014-03-05 19:59:52 +01001113 int ret;
1114
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001115 struct env_image_single *single;
1116 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001117
Andreas Fenkarte6511302016-05-31 09:21:56 +02001118 if (!opts)
1119 opts = &default_opts;
1120
Andreas Fenkart24371902016-04-05 23:13:42 +02001121 if (parse_config(opts)) /* should fill envdevices */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001122 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001123
Joe Hershberger671adc42012-10-03 09:38:46 +00001124 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001125 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001126 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001127 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001128 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001129 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001130 }
wdenk3f9ab982003-04-12 23:38:12 +00001131
wdenke7f34c62003-01-11 09:48:40 +00001132 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001133 environment.image = addr0;
1134
1135 if (HaveRedundEnv) {
1136 redundant = addr0;
1137 environment.crc = &redundant->crc;
1138 environment.flags = &redundant->flags;
1139 environment.data = redundant->data;
1140 } else {
1141 single = addr0;
1142 environment.crc = &single->crc;
1143 environment.flags = NULL;
1144 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001145 }
wdenk3f9ab982003-04-12 23:38:12 +00001146
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001147 dev_current = 0;
1148 if (flash_io (O_RDONLY))
1149 return -1;
1150
1151 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001152
Andreas Fenkart24371902016-04-05 23:13:42 +02001153 if (opts->aes_flag) {
Andreas Fenkartedcaee62016-04-19 22:43:39 +02001154 ret = env_aes_cbc_crypt(environment.data, 0,
Andreas Fenkart24371902016-04-05 23:13:42 +02001155 opts->aes_key);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001156 if (ret)
1157 return ret;
1158 }
1159
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001160 crc0_ok = (crc0 == *environment.crc);
wdenke7f34c62003-01-11 09:48:40 +00001161 if (!HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001162 if (!crc0_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001163 fprintf (stderr,
1164 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkc5203f92006-03-12 23:27:46 +01001165 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk34b613e2002-12-17 01:51:00 +00001166 }
wdenke7f34c62003-01-11 09:48:40 +00001167 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001168 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001169
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001170 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001171 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001172 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001173 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001174 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001175 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001176 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001177 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001178 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001179
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001180 /*
1181 * have to set environment.image for flash_read(), careful -
1182 * other pointers in environment still point inside addr0
1183 */
1184 environment.image = addr1;
1185 if (flash_io (O_RDONLY))
1186 return -1;
1187
1188 /* Check flag scheme compatibility */
1189 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1190 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1191 environment.flag_scheme = FLAG_BOOLEAN;
1192 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1193 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1194 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001195 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1196 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1197 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001198 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1199 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1200 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001201 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1202 DEVTYPE(!dev_current) == MTD_ABSENT) {
1203 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001204 } else {
1205 fprintf (stderr, "Incompatible flash types!\n");
1206 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001207 }
wdenk3f9ab982003-04-12 23:38:12 +00001208
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001209 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001210
Andreas Fenkart24371902016-04-05 23:13:42 +02001211 if (opts->aes_flag) {
Andreas Fenkartedcaee62016-04-19 22:43:39 +02001212 ret = env_aes_cbc_crypt(redundant->data, 0,
Andreas Fenkart24371902016-04-05 23:13:42 +02001213 opts->aes_key);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001214 if (ret)
1215 return ret;
1216 }
1217
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001218 crc1_ok = (crc1 == redundant->crc);
1219 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001220
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001221 if (crc0_ok && !crc1_ok) {
1222 dev_current = 0;
1223 } else if (!crc0_ok && crc1_ok) {
1224 dev_current = 1;
1225 } else if (!crc0_ok && !crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001226 fprintf (stderr,
1227 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001228 memcpy (environment.data, default_environment,
1229 sizeof default_environment);
1230 dev_current = 0;
1231 } else {
1232 switch (environment.flag_scheme) {
1233 case FLAG_BOOLEAN:
1234 if (flag0 == active_flag &&
1235 flag1 == obsolete_flag) {
1236 dev_current = 0;
1237 } else if (flag0 == obsolete_flag &&
1238 flag1 == active_flag) {
1239 dev_current = 1;
1240 } else if (flag0 == flag1) {
1241 dev_current = 0;
1242 } else if (flag0 == 0xFF) {
1243 dev_current = 0;
1244 } else if (flag1 == 0xFF) {
1245 dev_current = 1;
1246 } else {
1247 dev_current = 0;
1248 }
1249 break;
1250 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001251 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001252 dev_current = 1;
1253 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001254 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001255 dev_current = 0;
Jon Povey93ccd472011-03-11 14:10:56 +09001256 else /* flag1 > flag0 */
1257 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001258 break;
1259 default:
1260 fprintf (stderr, "Unknown flag scheme %u \n",
1261 environment.flag_scheme);
1262 return -1;
1263 }
1264 }
1265
1266 /*
1267 * If we are reading, we don't need the flag and the CRC any
1268 * more, if we are writing, we will re-calculate CRC and update
1269 * flags before writing out
1270 */
1271 if (dev_current) {
1272 environment.image = addr1;
1273 environment.crc = &redundant->crc;
1274 environment.flags = &redundant->flags;
1275 environment.data = redundant->data;
1276 free (addr0);
1277 } else {
1278 environment.image = addr0;
1279 /* Other pointers are already set */
wdenk3f9ab982003-04-12 23:38:12 +00001280 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +00001281 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001282#ifdef DEBUG
1283 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1284#endif
wdenk34b613e2002-12-17 01:51:00 +00001285 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001286 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001287}
1288
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001289static int check_device_config(int dev)
1290{
1291 struct stat st;
1292 int fd, rc = 0;
1293
1294 fd = open(DEVNAME(dev), O_RDONLY);
1295 if (fd < 0) {
1296 fprintf(stderr,
1297 "Cannot open %s: %s\n",
1298 DEVNAME(dev), strerror(errno));
1299 return -1;
1300 }
1301
1302 rc = fstat(fd, &st);
1303 if (rc < 0) {
1304 fprintf(stderr, "Cannot stat the file %s\n",
1305 DEVNAME(dev));
1306 goto err;
1307 }
1308
1309 if (S_ISCHR(st.st_mode)) {
1310 struct mtd_info_user mtdinfo;
1311 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1312 if (rc < 0) {
1313 fprintf(stderr, "Cannot get MTD information for %s\n",
1314 DEVNAME(dev));
1315 goto err;
1316 }
1317 if (mtdinfo.type != MTD_NORFLASH &&
1318 mtdinfo.type != MTD_NANDFLASH &&
1319 mtdinfo.type != MTD_DATAFLASH &&
1320 mtdinfo.type != MTD_UBIVOLUME) {
1321 fprintf(stderr, "Unsupported flash type %u on %s\n",
1322 mtdinfo.type, DEVNAME(dev));
1323 goto err;
1324 }
1325 DEVTYPE(dev) = mtdinfo.type;
1326 } else {
Stefan Agnerb21e6022016-07-13 17:14:38 -07001327 uint64_t size;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001328 DEVTYPE(dev) = MTD_ABSENT;
Stefan Agnerb21e6022016-07-13 17:14:38 -07001329
1330 /*
1331 * Check for negative offsets, treat it as backwards offset
1332 * from the end of the block device
1333 */
1334 if (DEVOFFSET(dev) < 0) {
1335 rc = ioctl(fd, BLKGETSIZE64, &size);
1336 if (rc < 0) {
1337 fprintf(stderr, "Could not get block device size on %s\n",
1338 DEVNAME(dev));
1339 goto err;
1340 }
1341
1342 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1343#ifdef DEBUG
1344 fprintf(stderr, "Calculated device offset 0x%llx on %s\n",
1345 DEVOFFSET(dev), DEVNAME(dev));
1346#endif
1347 }
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001348 }
1349
1350err:
1351 close(fd);
1352 return rc;
1353}
wdenk34b613e2002-12-17 01:51:00 +00001354
Andreas Fenkart24371902016-04-05 23:13:42 +02001355static int parse_config(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001356{
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001357 int rc;
wdenk34b613e2002-12-17 01:51:00 +00001358
Andreas Fenkarte6511302016-05-31 09:21:56 +02001359 if (!opts)
1360 opts = &default_opts;
Anatolij Gustschine89ead32016-04-29 22:00:11 +02001361
Andreas Fenkarte6511302016-05-31 09:21:56 +02001362#if defined(CONFIG_FILE)
wdenke7f34c62003-01-11 09:48:40 +00001363 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart24371902016-04-05 23:13:42 +02001364 if (get_config(opts->config_file)) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001365 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart24371902016-04-05 23:13:42 +02001366 opts->config_file);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001367 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001368 }
wdenke7f34c62003-01-11 09:48:40 +00001369#else
Tom Rini313e9122014-03-28 12:03:33 -04001370 DEVNAME (0) = DEVICE1_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001371 DEVOFFSET (0) = DEVICE1_OFFSET;
1372 ENVSIZE (0) = ENV1_SIZE;
Dustin Byford2f6df462014-03-06 20:48:22 -08001373 /* Default values are: erase-size=env-size */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001374 DEVESIZE (0) = ENVSIZE (0);
Dustin Byford2f6df462014-03-06 20:48:22 -08001375 /* #sectors=env-size/erase-size (rounded up) */
1376 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
Remy Bohmercb969eb2011-02-12 19:06:26 +01001377#ifdef DEVICE1_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001378 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001379#endif
1380#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001381 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001382#endif
1383
wdenk34b613e2002-12-17 01:51:00 +00001384#ifdef HAVE_REDUND
Tom Rini313e9122014-03-28 12:03:33 -04001385 DEVNAME (1) = DEVICE2_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001386 DEVOFFSET (1) = DEVICE2_OFFSET;
1387 ENVSIZE (1) = ENV2_SIZE;
Dustin Byford2f6df462014-03-06 20:48:22 -08001388 /* Default values are: erase-size=env-size */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001389 DEVESIZE (1) = ENVSIZE (1);
Dustin Byford2f6df462014-03-06 20:48:22 -08001390 /* #sectors=env-size/erase-size (rounded up) */
1391 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
Remy Bohmercb969eb2011-02-12 19:06:26 +01001392#ifdef DEVICE2_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001393 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001394#endif
1395#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001396 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001397#endif
wdenke7f34c62003-01-11 09:48:40 +00001398 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +00001399#endif
wdenke7f34c62003-01-11 09:48:40 +00001400#endif
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001401 rc = check_device_config(0);
1402 if (rc < 0)
1403 return rc;
wdenk3f9ab982003-04-12 23:38:12 +00001404
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001405 if (HaveRedundEnv) {
1406 rc = check_device_config(1);
1407 if (rc < 0)
1408 return rc;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001409
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001410 if (ENVSIZE(0) != ENVSIZE(1)) {
1411 ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1));
1412 fprintf(stderr,
1413 "Redundant environments have inequal size, set to 0x%08lx\n",
1414 ENVSIZE(1));
1415 }
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001416 }
1417
1418 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
1419 if (HaveRedundEnv)
1420 usable_envsize -= sizeof(char);
1421
Andreas Fenkart24371902016-04-05 23:13:42 +02001422 if (opts->aes_flag)
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001423 usable_envsize &= ~(AES_KEY_LENGTH - 1);
1424
wdenk34b613e2002-12-17 01:51:00 +00001425 return 0;
1426}
wdenke7f34c62003-01-11 09:48:40 +00001427
1428#if defined(CONFIG_FILE)
1429static int get_config (char *fname)
1430{
1431 FILE *fp;
1432 int i = 0;
1433 int rc;
1434 char dump[128];
Tom Rini313e9122014-03-28 12:03:33 -04001435 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001436
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001437 fp = fopen (fname, "r");
1438 if (fp == NULL)
1439 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001440
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001441 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenke7f34c62003-01-11 09:48:40 +00001442 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001443 if (dump[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001444 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001445
Stefan Agnerb21e6022016-07-13 17:14:38 -07001446 rc = sscanf(dump, "%ms %lli %lx %lx %lx",
1447 &devname,
1448 &DEVOFFSET(i),
1449 &ENVSIZE(i),
1450 &DEVESIZE(i),
1451 &ENVSECTORS(i));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001452
Remy Bohmercb969eb2011-02-12 19:06:26 +01001453 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001454 continue;
1455
Tom Rini313e9122014-03-28 12:03:33 -04001456 DEVNAME(i) = devname;
1457
Remy Bohmercb969eb2011-02-12 19:06:26 +01001458 if (rc < 4)
1459 /* Assume the erase size is the same as the env-size */
1460 DEVESIZE(i) = ENVSIZE(i);
1461
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001462 if (rc < 5)
Dustin Byford2f6df462014-03-06 20:48:22 -08001463 /* Assume enough env sectors to cover the environment */
1464 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
wdenke7f34c62003-01-11 09:48:40 +00001465
1466 i++;
1467 }
wdenk3f9ab982003-04-12 23:38:12 +00001468 fclose (fp);
1469
wdenke7f34c62003-01-11 09:48:40 +00001470 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +00001471 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001472 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001473 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001474 } else
1475 return 0;
1476}
1477#endif