blob: 90eb5fa4cb9bd2e756a8f6b6f2278d2998494d8d [file] [log] [blame]
wdenk6aff3112002-12-17 01:51:00 +00001/*
Detlev Zundel0aef7bc2010-07-30 11:22:15 +02002 * (C) Copyright 2000-2010
wdenk6aff3112002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02005 * (C) Copyright 2008
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk6aff3112002-12-17 01:51:00 +00009 */
10
Jörg Krause26e355d2015-04-22 21:36:22 +020011#define _GNU_SOURCE
12
Peter Robinson69bf2d22015-12-09 07:15:33 +000013#include <compiler.h>
wdenk6aff3112002-12-17 01:51:00 +000014#include <errno.h>
Joe Hershberger30fd4fad2012-12-11 22:16:32 -060015#include <env_flags.h>
wdenk6aff3112002-12-17 01:51:00 +000016#include <fcntl.h>
Stefan Agnerf4742ca2016-07-13 17:14:38 -070017#include <linux/fs.h>
Joe Hershberger06134212012-10-12 10:23:37 +000018#include <linux/stringify.h>
Andreas Fenkart10667e12016-03-11 09:39:35 +010019#include <ctype.h>
wdenk6aff3112002-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>
wdenk6aff3112002-12-17 01:51:00 +000028
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010029#ifdef MTD_OLD
Wolfgang Denk1711f3b2008-09-02 21:17:36 +020030# include <stdint.h>
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010031# include <linux/mtd/mtd.h>
32#else
Wolfgang Denkc83d7ca2008-01-08 22:58:27 +010033# define __user /* nothing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010034# include <mtd/mtd-user.h>
35#endif
36
37#include "fw_env.h"
wdenk6aff3112002-12-17 01:51:00 +000038
Andreas Fenkart14070e62016-05-31 09:21:56 +020039struct env_opts default_opts = {
40#ifdef CONFIG_FILE
41 .config_file = CONFIG_FILE
42#endif
43};
44
Marek Vasuta8a752c2014-03-05 19:59:52 +010045#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
46
Guennadi Liakhovetski56086922008-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 Rini229695f2014-03-28 12:03:33 -040054 const char *devname; /* Device name */
Stefan Agnerf4742ca2016-07-13 17:14:38 -070055 long long devoff; /* Device offset */
wdenk4a6fd342003-04-12 23:38:12 +000056 ulong env_size; /* environment size */
57 ulong erase_size; /* device erase size */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020058 ulong env_sectors; /* number of environment sectors */
59 uint8_t mtd_type; /* type of the MTD device */
60};
wdenk6aff3112002-12-17 01:51:00 +000061
Guennadi Liakhovetski56086922008-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;
wdenk6aff3112002-12-17 01:51:00 +000071
72#define DEVNAME(i) envdevices[(i)].devname
wdenkd0fb80c2003-01-11 09:48:40 +000073#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk6aff3112002-12-17 01:51:00 +000074#define ENVSIZE(i) envdevices[(i)].env_size
75#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020076#define ENVSECTORS(i) envdevices[(i)].env_sectors
77#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk6aff3112002-12-17 01:51:00 +000078
Joe Hershberger497f2052012-10-03 09:38:46 +000079#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk6aff3112002-12-17 01:51:00 +000080
Andreas Fenkartf71cee42016-04-19 22:43:42 +020081static unsigned long usable_envsize;
82#define ENV_SIZE usable_envsize
wdenk6aff3112002-12-17 01:51:00 +000083
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020084struct env_image_single {
85 uint32_t crc; /* CRC32 over data bytes */
86 char data[];
87};
wdenk6aff3112002-12-17 01:51:00 +000088
Guennadi Liakhovetski56086922008-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};
wdenk6aff3112002-12-17 01:51:00 +0000112
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200113static int env_aes_cbc_crypt(char *data, const int enc, uint8_t *key);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100114
wdenkd0fb80c2003-01-11 09:48:40 +0000115static int HaveRedundEnv = 0;
116
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100117static unsigned char active_flag = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200118/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100119static unsigned char obsolete_flag = 0;
wdenkd0fb80c2003-01-11 09:48:40 +0000120
Joe Hershbergerddd84182012-10-12 08:48:51 +0000121#define DEFAULT_ENV_INSTANCE_STATIC
122#include <env_default.h>
wdenk6aff3112002-12-17 01:51:00 +0000123
wdenk4a6fd342003-04-12 23:38:12 +0000124static int flash_io (int mode);
Andreas Fenkart81974f42016-04-05 23:13:42 +0200125static int parse_config(struct env_opts *opts);
wdenk4a6fd342003-04-12 23:38:12 +0000126
wdenkd0fb80c2003-01-11 09:48:40 +0000127#if defined(CONFIG_FILE)
wdenk4a6fd342003-04-12 23:38:12 +0000128static int get_config (char *);
wdenkd0fb80c2003-01-11 09:48:40 +0000129#endif
wdenk6aff3112002-12-17 01:51:00 +0000130
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100131static char *skip_chars(char *s)
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200132{
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100133 for (; *s != '\0'; s++) {
134 if (isblank(*s))
135 return s;
136 }
137 return NULL;
138}
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200139
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100140static char *skip_blanks(char *s)
141{
142 for (; *s != '\0'; s++) {
143 if (!isblank(*s))
Andreas Fenkart9583efc2016-03-11 09:39:36 +0100144 return s;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200145 }
Andreas Fenkart9583efc2016-03-11 09:39:36 +0100146 return NULL;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200147}
148
wdenk6aff3112002-12-17 01:51:00 +0000149/*
Andreas Fenkart1b7427c2016-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/**
wdenk6aff3112002-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ücher6de66b32007-11-27 10:23:20 +0100171char *fw_getenv (char *name)
wdenk6aff3112002-12-17 01:51:00 +0000172{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100173 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000174
wdenk4a6fd342003-04-12 23:38:12 +0000175 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100176 char *val;
wdenk6aff3112002-12-17 01:51:00 +0000177
wdenk4a6fd342003-04-12 23:38:12 +0000178 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000179 if (nxt >= &environment.data[ENV_SIZE]) {
180 fprintf (stderr, "## Error: "
181 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200182 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000183 }
184 }
wdenk4a6fd342003-04-12 23:38:12 +0000185 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000186 if (!val)
187 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200188 return val;
wdenk6aff3112002-12-17 01:51:00 +0000189 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200190 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000191}
192
193/*
Joe Hershberger267541f2012-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 Fenkart371ee132015-12-09 13:13:24 +0100219int parse_aes_key(char *key, uint8_t *bin_key)
Marek Vasuta8a752c2014-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 Fenkart8b27abd2015-12-09 13:13:22 +0100241 bin_key[i] = ul & 0xff;
Marek Vasuta8a752c2014-03-05 19:59:52 +0100242 key += 2;
243 }
Marek Vasuta8a752c2014-03-05 19:59:52 +0100244 return 0;
245}
246
Joe Hershberger267541f2012-12-11 22:16:34 -0600247/*
wdenk6aff3112002-12-17 01:51:00 +0000248 * Print the current definition of one, or more, or all
249 * environment variables
250 */
Andreas Fenkart81974f42016-04-05 23:13:42 +0200251int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +0000252{
Andreas Fenkart371ee132015-12-09 13:13:24 +0100253 int i, rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000254
Andreas Fenkartc5c41c42016-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 Fenkart14070e62016-05-31 09:21:56 +0200261 if (!opts)
262 opts = &default_opts;
263
Andreas Fenkart81974f42016-04-05 23:13:42 +0200264 if (fw_env_open(opts))
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200265 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000266
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100267 if (argc == 0) { /* Print all env variables */
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200268 char *env, *nxt;
wdenk4a6fd342003-04-12 23:38:12 +0000269 for (env = environment.data; *env; env = nxt + 1) {
270 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000271 if (nxt >= &environment.data[ENV_SIZE]) {
272 fprintf (stderr, "## Error: "
273 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200274 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000275 }
276 }
277
wdenk4a6fd342003-04-12 23:38:12 +0000278 printf ("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000279 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200280 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000281 }
282
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200283 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100284 char *name = argv[i];
285 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000286
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200287 val = fw_getenv(name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700288 if (!val) {
wdenk4a6fd342003-04-12 23:38:12 +0000289 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700290 rc = -1;
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200291 continue;
Grant Ericksonbc117562008-05-06 20:16:15 -0700292 }
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200293
294 if (value_only) {
295 puts(val);
296 break;
297 }
298
299 printf("%s=%s\n", name, val);
wdenk6aff3112002-12-17 01:51:00 +0000300 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700301
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200302 return rc;
wdenk6aff3112002-12-17 01:51:00 +0000303}
304
Andreas Fenkart81974f42016-04-05 23:13:42 +0200305int fw_env_close(struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +0000306{
Marek Vasuta8a752c2014-03-05 19:59:52 +0100307 int ret;
Andreas Fenkart81974f42016-04-05 23:13:42 +0200308
Andreas Fenkart14070e62016-05-31 09:21:56 +0200309 if (!opts)
310 opts = &default_opts;
311
Andreas Fenkart81974f42016-04-05 23:13:42 +0200312 if (opts->aes_flag) {
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200313 ret = env_aes_cbc_crypt(environment.data, 1,
Andreas Fenkart81974f42016-04-05 23:13:42 +0200314 opts->aes_key);
Marek Vasuta8a752c2014-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 Babicbd7b26f2010-05-24 12:08:16 +0200322 /*
323 * Update CRC
324 */
325 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000326
Stefano Babicbd7b26f2010-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;
wdenk6aff3112002-12-17 01:51:00 +0000332 }
333
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200334 return 0;
335}
wdenk6aff3112002-12-17 01:51:00 +0000336
Stefano Babicbd7b26f2010-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 Hershberger267541f2012-12-11 22:16:34 -0600348 int deleting, creating, overwriting;
wdenk6aff3112002-12-17 01:51:00 +0000349
350 /*
351 * search if variable with this name already exists
352 */
wdenk4a6fd342003-04-12 23:38:12 +0000353 for (nxt = env = environment.data; *env; env = nxt + 1) {
354 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000355 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200356 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000357 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200358 errno = EINVAL;
359 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000360 }
361 }
wdenk4a6fd342003-04-12 23:38:12 +0000362 if ((oldval = envmatch (name, env)) != NULL)
wdenk6aff3112002-12-17 01:51:00 +0000363 break;
364 }
365
Joe Hershberger267541f2012-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) {
wdenk6aff3112002-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 Babicbd7b26f2010-05-24 12:08:16 +0200423 if (!value || !strlen(value))
424 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000425
426 /*
427 * Append new definition at the end
428 */
wdenk4a6fd342003-04-12 23:38:12 +0000429 for (env = environment.data; *env || *(env + 1); ++env);
wdenk6aff3112002-12-17 01:51:00 +0000430 if (env > environment.data)
431 ++env;
432 /*
433 * Overflow when:
Joe Hershberger497f2052012-10-03 09:38:46 +0000434 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk6aff3112002-12-17 01:51:00 +0000435 */
wdenk4a6fd342003-04-12 23:38:12 +0000436 len = strlen (name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000437 /* add '=' for first arg, ' ' for all others */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200438 len += strlen(value) + 1;
439
wdenk4a6fd342003-04-12 23:38:12 +0000440 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk6aff3112002-12-17 01:51:00 +0000441 fprintf (stderr,
442 "Error: environment overflow, \"%s\" deleted\n",
443 name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200444 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000445 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200446
wdenk6aff3112002-12-17 01:51:00 +0000447 while ((*env = *name++) != '\0')
448 env++;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200449 *env = '=';
450 while ((*++env = *value++) != '\0')
451 ;
wdenk6aff3112002-12-17 01:51:00 +0000452
453 /* end is marked with double '\0' */
454 *++env = '\0';
455
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200456 return 0;
457}
wdenk6aff3112002-12-17 01:51:00 +0000458
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200459/*
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 Fenkart81974f42016-04-05 23:13:42 +0200467int fw_setenv(int argc, char *argv[], struct env_opts *opts)
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200468{
Andreas Fenkart371ee132015-12-09 13:13:24 +0100469 int i;
Mike Frysinger3779c8e2012-11-10 19:47:45 +0000470 size_t len;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100471 char *name, **valv;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200472 char *value = NULL;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100473 int valc;
wdenk6aff3112002-12-17 01:51:00 +0000474
Andreas Fenkart14070e62016-05-31 09:21:56 +0200475 if (!opts)
476 opts = &default_opts;
477
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100478 if (argc < 1) {
479 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasuta8a752c2014-03-05 19:59:52 +0100480 errno = EINVAL;
481 return -1;
482 }
483
Andreas Fenkart81974f42016-04-05 23:13:42 +0200484 if (fw_env_open(opts)) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200485 fprintf(stderr, "Error: environment not initialized\n");
486 return -1;
487 }
488
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100489 name = argv[0];
490 valv = argv + 1;
491 valc = argc - 1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200492
Andreas Fenkart167f5252015-12-09 13:13:21 +0100493 if (env_flags_validate_env_set_params(name, valv, valc) < 0)
Andreas Fenkartfd4e3282016-07-16 17:06:13 +0200494 return -1;
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600495
Joe Hershberger62a34a02012-10-03 09:38:47 +0000496 len = 0;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100497 for (i = 0; i < valc; ++i) {
498 char *val = valv[i];
Joe Hershberger62a34a02012-10-03 09:38:47 +0000499 size_t val_len = strlen(val);
500
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000501 if (value)
502 value[len - 1] = ' ';
Joe Hershberger62a34a02012-10-03 09:38:47 +0000503 value = realloc(value, len + val_len + 1);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200504 if (!value) {
Joe Hershberger62a34a02012-10-03 09:38:47 +0000505 fprintf(stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200506 "Cannot malloc %zu bytes: %s\n",
Joe Hershberger62a34a02012-10-03 09:38:47 +0000507 len, strerror(errno));
508 return -1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200509 }
Joe Hershberger62a34a02012-10-03 09:38:47 +0000510
511 memcpy(value + len, val, val_len);
512 len += val_len;
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000513 value[len++] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200514 }
515
516 fw_env_write(name, value);
517
Joe Hershberger62a34a02012-10-03 09:38:47 +0000518 free(value);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200519
Andreas Fenkart81974f42016-04-05 23:13:42 +0200520 return fw_env_close(opts);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200521}
522
523/*
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 Fenkart81974f42016-04-05 23:13:42 +0200540int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicbd7b26f2010-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 Fenkart14070e62016-05-31 09:21:56 +0200550 if (!opts)
551 opts = &default_opts;
552
Andreas Fenkart81974f42016-04-05 23:13:42 +0200553 if (fw_env_open(opts)) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200554 fprintf(stderr, "Error: environment not initialized\n");
555 return -1;
556 }
557
558 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 Fenkart69067a32016-03-11 09:39:38 +0100586 dump[--len] = '\0';
587 if (len && dump[len - 1] == '\r')
588 dump[--len] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200589
590 /* Skip comment or empty lines */
Andreas Fenkart69067a32016-03-11 09:39:38 +0100591 if (len == 0 || dump[0] == '#')
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200592 continue;
593
594 /*
595 * Search for variable's name,
596 * remove leading whitespaces
597 */
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100598 name = skip_blanks(dump);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200599 if (!name)
600 continue;
601
602 /* The first white space is the end of variable name */
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100603 val = skip_chars(name);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200604 len = strlen(name);
605 if (val) {
606 *val++ = '\0';
607 if ((val - name) < len)
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100608 val = skip_blanks(val);
Stefano Babicbd7b26f2010-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 Hershberger30fd4fad2012-12-11 22:16:32 -0600618 if (env_flags_validate_type(name, val) < 0) {
619 ret = -1;
620 break;
621 }
622
Stefano Babicbd7b26f2010-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 Fenkart81974f42016-04-05 23:13:42 +0200641 ret |= fw_env_close(opts);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200642
643 return ret;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200644}
645
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200646/**
647 * environment_end() - compute offset of first byte right after environemnt
648 * @dev - index of enviroment buffer
649 * Return:
650 * device offset of first byte right after environemnt
651 */
652off_t environment_end(int dev)
653{
654 /* environment is block aligned */
655 return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
656}
657
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200658/*
659 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
660 * 0 - block is good
661 * > 0 - block is bad
662 * < 0 - failed to test
663 */
664static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
665{
666 if (mtd_type == MTD_NANDFLASH) {
667 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
668
669 if (badblock < 0) {
670 perror ("Cannot read bad block mark");
671 return badblock;
672 }
673
674 if (badblock) {
675#ifdef DEBUG
Marcin Niestroj81c878d2016-05-06 14:58:29 +0200676 fprintf (stderr, "Bad block at 0x%llx, skipping\n",
677 (unsigned long long) *blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200678#endif
679 return badblock;
680 }
681 }
682
683 return 0;
684}
685
686/*
687 * Read data from flash at an offset into a provided buffer. On NAND it skips
688 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
689 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
690 */
691static int flash_read_buf (int dev, int fd, void *buf, size_t count,
692 off_t offset, uint8_t mtd_type)
693{
694 size_t blocklen; /* erase / write length - one block on NAND,
695 0 on NOR */
696 size_t processed = 0; /* progress counter */
697 size_t readlen = count; /* current read length */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200698 off_t block_seek; /* offset inside the current block to the start
699 of the data */
700 loff_t blockstart; /* running start of the current block -
701 MEMGETBADBLOCK needs 64 bits */
702 int rc;
703
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100704 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200705
706 /* Offset inside a block */
707 block_seek = offset - blockstart;
708
709 if (mtd_type == MTD_NANDFLASH) {
710 /*
711 * NAND: calculate which blocks we are reading. We have
712 * to read one block at a time to skip bad blocks.
713 */
714 blocklen = DEVESIZE (dev);
715
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200716 /* Limit to one block for the first read */
717 if (readlen > blocklen - block_seek)
718 readlen = blocklen - block_seek;
719 } else {
720 blocklen = 0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200721 }
722
723 /* This only runs once on NOR flash */
724 while (processed < count) {
725 rc = flash_bad_block (fd, mtd_type, &blockstart);
726 if (rc < 0) /* block test failed */
727 return -1;
728
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200729 if (blockstart + block_seek + readlen > environment_end(dev)) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200730 /* End of range is reached */
731 fprintf (stderr,
732 "Too few good blocks within range\n");
733 return -1;
734 }
735
736 if (rc) { /* block is bad */
737 blockstart += blocklen;
738 continue;
739 }
740
741 /*
742 * If a block is bad, we retry in the next block at the same
743 * offset - see common/env_nand.c::writeenv()
744 */
745 lseek (fd, blockstart + block_seek, SEEK_SET);
746
747 rc = read (fd, buf + processed, readlen);
748 if (rc != readlen) {
749 fprintf (stderr, "Read error on %s: %s\n",
750 DEVNAME (dev), strerror (errno));
751 return -1;
752 }
753#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000754 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
Marcin Niestroj81c878d2016-05-06 14:58:29 +0200755 rc, (unsigned long long) blockstart + block_seek,
756 DEVNAME(dev));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200757#endif
758 processed += readlen;
759 readlen = min (blocklen, count - processed);
760 block_seek = 0;
761 blockstart += blocklen;
762 }
763
764 return processed;
765}
766
767/*
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100768 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
769 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
770 * erase and write the whole data at once.
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200771 */
772static int flash_write_buf (int dev, int fd, void *buf, size_t count,
773 off_t offset, uint8_t mtd_type)
774{
775 void *data;
776 struct erase_info_user erase;
777 size_t blocklen; /* length of NAND block / NOR erase sector */
778 size_t erase_len; /* whole area that can be erased - may include
779 bad blocks */
780 size_t erasesize; /* erase / write length - one block on NAND,
781 whole area on NOR */
782 size_t processed = 0; /* progress counter */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100783 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200784 bad blocks */
785 off_t erase_offset; /* offset to the first erase block (aligned)
786 below offset */
787 off_t block_seek; /* offset inside the erase block to the start
788 of the data */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200789 loff_t blockstart; /* running start of the current block -
790 MEMGETBADBLOCK needs 64 bits */
791 int rc;
792
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200793 /*
Oliver Metze387efb2013-08-30 00:56:02 +0200794 * For mtd devices only offset and size of the environment do matter
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200795 */
Oliver Metze387efb2013-08-30 00:56:02 +0200796 if (mtd_type == MTD_ABSENT) {
797 blocklen = count;
Oliver Metze387efb2013-08-30 00:56:02 +0200798 erase_len = blocklen;
799 blockstart = offset;
800 block_seek = 0;
801 write_total = blocklen;
802 } else {
803 blocklen = DEVESIZE(dev);
804
Oliver Metze387efb2013-08-30 00:56:02 +0200805 erase_offset = (offset / blocklen) * blocklen;
806
807 /* Maximum area we may use */
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200808 erase_len = environment_end(dev) - erase_offset;
Oliver Metze387efb2013-08-30 00:56:02 +0200809
810 blockstart = erase_offset;
811 /* Offset inside a block */
812 block_seek = offset - erase_offset;
813
814 /*
815 * Data size we actually write: from the start of the block
816 * to the start of the data, then count bytes of data, and
817 * to the end of the block
818 */
819 write_total = ((block_seek + count + blocklen - 1) /
820 blocklen) * blocklen;
821 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200822
823 /*
824 * Support data anywhere within erase sectors: read out the complete
825 * area to be erased, replace the environment image, write the whole
826 * block back again.
827 */
828 if (write_total > count) {
829 data = malloc (erase_len);
830 if (!data) {
831 fprintf (stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200832 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200833 erase_len, strerror (errno));
834 return -1;
835 }
836
837 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
838 mtd_type);
839 if (write_total != rc)
840 return -1;
841
Joe Hershberger74620e12012-10-15 15:29:25 +0000842#ifdef DEBUG
843 fprintf(stderr, "Preserving data ");
844 if (block_seek != 0)
845 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
846 if (block_seek + count != write_total) {
847 if (block_seek != 0)
848 fprintf(stderr, " and ");
Marcin Niestroj81c878d2016-05-06 14:58:29 +0200849 fprintf(stderr, "0x%lx - 0x%lx",
850 (unsigned long) block_seek + count,
851 (unsigned long) write_total - 1);
Joe Hershberger74620e12012-10-15 15:29:25 +0000852 }
853 fprintf(stderr, "\n");
854#endif
Guennadi Liakhovetski56086922008-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 Bohmer9eeaa8e2011-02-12 19:06:26 +0100877 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetski56086922008-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
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200883 if (blockstart + erasesize > environment_end(dev)) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200884 fprintf (stderr, "End of range reached, aborting\n");
885 return -1;
886 }
887
888 if (rc) { /* block is bad */
889 blockstart += blocklen;
890 continue;
891 }
892
Oliver Metze387efb2013-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 Liakhovetski56086922008-09-04 13:01:49 +0200905
906 if (lseek (fd, blockstart, SEEK_SET) == -1) {
907 fprintf (stderr,
908 "Seek error on %s: %s\n",
909 DEVNAME (dev), strerror (errno));
910 return -1;
911 }
912
913#ifdef DEBUG
Marcin Niestroj81c878d2016-05-06 14:58:29 +0200914 fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
915 (unsigned long long) erasesize,
916 (unsigned long long) blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200917#endif
918 if (write (fd, data + processed, erasesize) != erasesize) {
919 fprintf (stderr, "Write error on %s: %s\n",
920 DEVNAME (dev), strerror (errno));
921 return -1;
922 }
923
Oliver Metze387efb2013-08-30 00:56:02 +0200924 if (mtd_type != MTD_ABSENT)
925 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200926
Dustin Byford4b774ff2014-03-06 20:48:23 -0800927 processed += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200928 block_seek = 0;
Dustin Byford4b774ff2014-03-06 20:48:23 -0800929 blockstart += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200930 }
931
932 if (write_total > count)
933 free (data);
934
935 return processed;
936}
937
938/*
939 * Set obsolete flag at offset - NOR flash only
940 */
941static int flash_flag_obsolete (int dev, int fd, off_t offset)
942{
943 int rc;
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200944 struct erase_info_user erase;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200945
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200946 erase.start = DEVOFFSET (dev);
947 erase.length = DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200948 /* This relies on the fact, that obsolete_flag == 0 */
949 rc = lseek (fd, offset, SEEK_SET);
950 if (rc < 0) {
951 fprintf (stderr, "Cannot seek to set the flag on %s \n",
952 DEVNAME (dev));
953 return rc;
954 }
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200955 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200956 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200957 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200958 if (rc < 0)
959 perror ("Could not set obsolete flag");
960
961 return rc;
962}
963
Marek Vasuta8a752c2014-03-05 19:59:52 +0100964/* Encrypt or decrypt the environment before writing or reading it. */
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200965static int env_aes_cbc_crypt(char *payload, const int enc, uint8_t *key)
Marek Vasuta8a752c2014-03-05 19:59:52 +0100966{
967 uint8_t *data = (uint8_t *)payload;
Andreas Fenkartf71cee42016-04-19 22:43:42 +0200968 const int len = usable_envsize;
Marek Vasuta8a752c2014-03-05 19:59:52 +0100969 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
970 uint32_t aes_blocks;
971
972 /* First we expand the key. */
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200973 aes_expand_key(key, key_exp);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100974
975 /* Calculate the number of AES blocks to encrypt. */
976 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
977
978 if (enc)
979 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
980 else
981 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
982
983 return 0;
984}
985
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200986static int flash_write (int fd_current, int fd_target, int dev_target)
987{
988 int rc;
989
990 switch (environment.flag_scheme) {
991 case FLAG_NONE:
992 break;
993 case FLAG_INCREMENTAL:
994 (*environment.flags)++;
995 break;
996 case FLAG_BOOLEAN:
997 *environment.flags = active_flag;
998 break;
999 default:
1000 fprintf (stderr, "Unimplemented flash scheme %u \n",
1001 environment.flag_scheme);
1002 return -1;
1003 }
1004
1005#ifdef DEBUG
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001006 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001007 DEVOFFSET (dev_target), DEVNAME (dev_target));
1008#endif
Marek Vasuta8a752c2014-03-05 19:59:52 +01001009
Joe Hershberger497f2052012-10-03 09:38:46 +00001010 rc = flash_write_buf(dev_target, fd_target, environment.image,
1011 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001012 DEVTYPE(dev_target));
1013 if (rc < 0)
1014 return rc;
1015
1016 if (environment.flag_scheme == FLAG_BOOLEAN) {
1017 /* Have to set obsolete flag */
1018 off_t offset = DEVOFFSET (dev_current) +
1019 offsetof (struct env_image_redundant, flags);
1020#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +00001021 fprintf(stderr,
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001022 "Setting obsolete flag in environment at 0x%llx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001023 DEVOFFSET (dev_current), DEVNAME (dev_current));
1024#endif
1025 flash_flag_obsolete (dev_current, fd_current, offset);
1026 }
1027
1028 return 0;
1029}
1030
1031static int flash_read (int fd)
1032{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001033 int rc;
1034
Joe Hershberger497f2052012-10-03 09:38:46 +00001035 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Stefan Agner14fb5b22016-07-13 17:14:37 -07001036 DEVOFFSET(dev_current), DEVTYPE(dev_current));
Marek Vasuta8a752c2014-03-05 19:59:52 +01001037 if (rc != CUR_ENVSIZE)
1038 return -1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001039
Marek Vasuta8a752c2014-03-05 19:59:52 +01001040 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001041}
1042
1043static int flash_io (int mode)
1044{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001045 int fd_current, fd_target, rc, dev_target;
wdenk6aff3112002-12-17 01:51:00 +00001046
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001047 /* dev_current: fd_current, erase_current */
1048 fd_current = open (DEVNAME (dev_current), mode);
1049 if (fd_current < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001050 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001051 "Can't open %s: %s\n",
1052 DEVNAME (dev_current), strerror (errno));
1053 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001054 }
wdenk6aff3112002-12-17 01:51:00 +00001055
1056 if (mode == O_RDWR) {
wdenkd0fb80c2003-01-11 09:48:40 +00001057 if (HaveRedundEnv) {
1058 /* switch to next partition for writing */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001059 dev_target = !dev_current;
1060 /* dev_target: fd_target, erase_target */
1061 fd_target = open (DEVNAME (dev_target), mode);
1062 if (fd_target < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001063 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001064 "Can't open %s: %s\n",
1065 DEVNAME (dev_target),
1066 strerror (errno));
1067 rc = -1;
1068 goto exit;
wdenkd0fb80c2003-01-11 09:48:40 +00001069 }
1070 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001071 dev_target = dev_current;
1072 fd_target = fd_current;
wdenk6aff3112002-12-17 01:51:00 +00001073 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001074
1075 rc = flash_write (fd_current, fd_target, dev_target);
wdenk6aff3112002-12-17 01:51:00 +00001076
wdenkd0fb80c2003-01-11 09:48:40 +00001077 if (HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001078 if (close (fd_target)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001079 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001080 "I/O error on %s: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001081 DEVNAME (dev_target),
wdenk4a6fd342003-04-12 23:38:12 +00001082 strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001083 rc = -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001084 }
wdenk6aff3112002-12-17 01:51:00 +00001085 }
wdenk6aff3112002-12-17 01:51:00 +00001086 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001087 rc = flash_read (fd_current);
wdenk6aff3112002-12-17 01:51:00 +00001088 }
1089
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001090exit:
1091 if (close (fd_current)) {
wdenk6aff3112002-12-17 01:51:00 +00001092 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001093 "I/O error on %s: %s\n",
1094 DEVNAME (dev_current), strerror (errno));
1095 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001096 }
1097
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001098 return rc;
wdenk6aff3112002-12-17 01:51:00 +00001099}
1100
1101/*
wdenk6aff3112002-12-17 01:51:00 +00001102 * Prevent confusion if running from erased flash memory
1103 */
Andreas Fenkart81974f42016-04-05 23:13:42 +02001104int fw_env_open(struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +00001105{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001106 int crc0, crc0_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001107 unsigned char flag0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001108 void *addr0;
wdenkd0fb80c2003-01-11 09:48:40 +00001109
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001110 int crc1, crc1_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001111 unsigned char flag1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001112 void *addr1;
1113
Marek Vasuta8a752c2014-03-05 19:59:52 +01001114 int ret;
1115
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001116 struct env_image_single *single;
1117 struct env_image_redundant *redundant;
wdenk6aff3112002-12-17 01:51:00 +00001118
Andreas Fenkart14070e62016-05-31 09:21:56 +02001119 if (!opts)
1120 opts = &default_opts;
1121
Andreas Fenkart81974f42016-04-05 23:13:42 +02001122 if (parse_config(opts)) /* should fill envdevices */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001123 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001124
Joe Hershberger497f2052012-10-03 09:38:46 +00001125 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001126 if (addr0 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001127 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001128 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001129 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001130 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001131 }
wdenk4a6fd342003-04-12 23:38:12 +00001132
wdenkd0fb80c2003-01-11 09:48:40 +00001133 /* read environment from FLASH to local buffer */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001134 environment.image = addr0;
1135
1136 if (HaveRedundEnv) {
1137 redundant = addr0;
1138 environment.crc = &redundant->crc;
1139 environment.flags = &redundant->flags;
1140 environment.data = redundant->data;
1141 } else {
1142 single = addr0;
1143 environment.crc = &single->crc;
1144 environment.flags = NULL;
1145 environment.data = single->data;
wdenkd0fb80c2003-01-11 09:48:40 +00001146 }
wdenk4a6fd342003-04-12 23:38:12 +00001147
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001148 dev_current = 0;
1149 if (flash_io (O_RDONLY))
1150 return -1;
1151
1152 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001153
Andreas Fenkart81974f42016-04-05 23:13:42 +02001154 if (opts->aes_flag) {
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +02001155 ret = env_aes_cbc_crypt(environment.data, 0,
Andreas Fenkart81974f42016-04-05 23:13:42 +02001156 opts->aes_key);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001157 if (ret)
1158 return ret;
1159 }
1160
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001161 crc0_ok = (crc0 == *environment.crc);
wdenkd0fb80c2003-01-11 09:48:40 +00001162 if (!HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001163 if (!crc0_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001164 fprintf (stderr,
1165 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +01001166 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +00001167 }
wdenkd0fb80c2003-01-11 09:48:40 +00001168 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001169 flag0 = *environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +00001170
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001171 dev_current = 1;
Joe Hershberger497f2052012-10-03 09:38:46 +00001172 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001173 if (addr1 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001174 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001175 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001176 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001177 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001178 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001179 redundant = addr1;
wdenk4a6fd342003-04-12 23:38:12 +00001180
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001181 /*
1182 * have to set environment.image for flash_read(), careful -
1183 * other pointers in environment still point inside addr0
1184 */
1185 environment.image = addr1;
1186 if (flash_io (O_RDONLY))
1187 return -1;
1188
1189 /* Check flag scheme compatibility */
1190 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1191 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1192 environment.flag_scheme = FLAG_BOOLEAN;
1193 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1194 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1195 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001196 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1197 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1198 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershberger785881f2013-04-08 10:32:52 +00001199 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1200 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1201 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metz23ef62d2013-08-30 00:56:01 +02001202 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1203 DEVTYPE(!dev_current) == MTD_ABSENT) {
1204 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001205 } else {
1206 fprintf (stderr, "Incompatible flash types!\n");
1207 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001208 }
wdenk4a6fd342003-04-12 23:38:12 +00001209
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001210 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001211
Andreas Fenkart81974f42016-04-05 23:13:42 +02001212 if (opts->aes_flag) {
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +02001213 ret = env_aes_cbc_crypt(redundant->data, 0,
Andreas Fenkart81974f42016-04-05 23:13:42 +02001214 opts->aes_key);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001215 if (ret)
1216 return ret;
1217 }
1218
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001219 crc1_ok = (crc1 == redundant->crc);
1220 flag1 = redundant->flags;
wdenk4a6fd342003-04-12 23:38:12 +00001221
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001222 if (crc0_ok && !crc1_ok) {
1223 dev_current = 0;
1224 } else if (!crc0_ok && crc1_ok) {
1225 dev_current = 1;
1226 } else if (!crc0_ok && !crc1_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001227 fprintf (stderr,
1228 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001229 memcpy (environment.data, default_environment,
1230 sizeof default_environment);
1231 dev_current = 0;
1232 } else {
1233 switch (environment.flag_scheme) {
1234 case FLAG_BOOLEAN:
1235 if (flag0 == active_flag &&
1236 flag1 == obsolete_flag) {
1237 dev_current = 0;
1238 } else if (flag0 == obsolete_flag &&
1239 flag1 == active_flag) {
1240 dev_current = 1;
1241 } else if (flag0 == flag1) {
1242 dev_current = 0;
1243 } else if (flag0 == 0xFF) {
1244 dev_current = 0;
1245 } else if (flag1 == 0xFF) {
1246 dev_current = 1;
1247 } else {
1248 dev_current = 0;
1249 }
1250 break;
1251 case FLAG_INCREMENTAL:
Jon Povey735eb0f2011-03-11 14:10:56 +09001252 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001253 dev_current = 1;
1254 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey735eb0f2011-03-11 14:10:56 +09001255 flag0 >= flag1)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001256 dev_current = 0;
Jon Povey735eb0f2011-03-11 14:10:56 +09001257 else /* flag1 > flag0 */
1258 dev_current = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001259 break;
1260 default:
1261 fprintf (stderr, "Unknown flag scheme %u \n",
1262 environment.flag_scheme);
1263 return -1;
1264 }
1265 }
1266
1267 /*
1268 * If we are reading, we don't need the flag and the CRC any
1269 * more, if we are writing, we will re-calculate CRC and update
1270 * flags before writing out
1271 */
1272 if (dev_current) {
1273 environment.image = addr1;
1274 environment.crc = &redundant->crc;
1275 environment.flags = &redundant->flags;
1276 environment.data = redundant->data;
1277 free (addr0);
1278 } else {
1279 environment.image = addr0;
1280 /* Other pointers are already set */
wdenk4a6fd342003-04-12 23:38:12 +00001281 free (addr1);
wdenk6aff3112002-12-17 01:51:00 +00001282 }
Joe Hershberger74620e12012-10-15 15:29:25 +00001283#ifdef DEBUG
1284 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1285#endif
wdenk6aff3112002-12-17 01:51:00 +00001286 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001287 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001288}
1289
Stefan Agner14fb5b22016-07-13 17:14:37 -07001290static int check_device_config(int dev)
1291{
1292 struct stat st;
1293 int fd, rc = 0;
1294
Andreas Fenkart183923d2016-08-11 21:39:17 +02001295 if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
Andreas Fenkart4ed6f432016-08-17 23:41:54 +02001296 fprintf(stderr, "Environment does not start on (erase) block boundary\n");
Andreas Fenkart183923d2016-08-11 21:39:17 +02001297 errno = EINVAL;
1298 return -1;
1299 }
1300
1301 if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
1302 fprintf(stderr, "Environment does not fit into available sectors\n");
1303 errno = EINVAL;
1304 return -1;
1305 }
1306
Stefan Agner14fb5b22016-07-13 17:14:37 -07001307 fd = open(DEVNAME(dev), O_RDONLY);
1308 if (fd < 0) {
1309 fprintf(stderr,
1310 "Cannot open %s: %s\n",
1311 DEVNAME(dev), strerror(errno));
1312 return -1;
1313 }
1314
1315 rc = fstat(fd, &st);
1316 if (rc < 0) {
1317 fprintf(stderr, "Cannot stat the file %s\n",
1318 DEVNAME(dev));
1319 goto err;
1320 }
1321
1322 if (S_ISCHR(st.st_mode)) {
1323 struct mtd_info_user mtdinfo;
1324 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1325 if (rc < 0) {
1326 fprintf(stderr, "Cannot get MTD information for %s\n",
1327 DEVNAME(dev));
1328 goto err;
1329 }
1330 if (mtdinfo.type != MTD_NORFLASH &&
1331 mtdinfo.type != MTD_NANDFLASH &&
1332 mtdinfo.type != MTD_DATAFLASH &&
1333 mtdinfo.type != MTD_UBIVOLUME) {
1334 fprintf(stderr, "Unsupported flash type %u on %s\n",
1335 mtdinfo.type, DEVNAME(dev));
1336 goto err;
1337 }
1338 DEVTYPE(dev) = mtdinfo.type;
1339 } else {
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001340 uint64_t size;
Stefan Agner14fb5b22016-07-13 17:14:37 -07001341 DEVTYPE(dev) = MTD_ABSENT;
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001342
1343 /*
1344 * Check for negative offsets, treat it as backwards offset
1345 * from the end of the block device
1346 */
1347 if (DEVOFFSET(dev) < 0) {
1348 rc = ioctl(fd, BLKGETSIZE64, &size);
1349 if (rc < 0) {
1350 fprintf(stderr, "Could not get block device size on %s\n",
1351 DEVNAME(dev));
1352 goto err;
1353 }
1354
1355 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1356#ifdef DEBUG
1357 fprintf(stderr, "Calculated device offset 0x%llx on %s\n",
1358 DEVOFFSET(dev), DEVNAME(dev));
1359#endif
1360 }
Stefan Agner14fb5b22016-07-13 17:14:37 -07001361 }
1362
1363err:
1364 close(fd);
1365 return rc;
1366}
wdenk6aff3112002-12-17 01:51:00 +00001367
Andreas Fenkart81974f42016-04-05 23:13:42 +02001368static int parse_config(struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +00001369{
Stefan Agner14fb5b22016-07-13 17:14:37 -07001370 int rc;
wdenk6aff3112002-12-17 01:51:00 +00001371
Andreas Fenkart14070e62016-05-31 09:21:56 +02001372 if (!opts)
1373 opts = &default_opts;
Anatolij Gustschin925c97c2016-04-29 22:00:11 +02001374
Andreas Fenkart14070e62016-05-31 09:21:56 +02001375#if defined(CONFIG_FILE)
wdenkd0fb80c2003-01-11 09:48:40 +00001376 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart81974f42016-04-05 23:13:42 +02001377 if (get_config(opts->config_file)) {
Andreas Fenkart371ee132015-12-09 13:13:24 +01001378 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart81974f42016-04-05 23:13:42 +02001379 opts->config_file);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001380 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001381 }
wdenkd0fb80c2003-01-11 09:48:40 +00001382#else
Tom Rini229695f2014-03-28 12:03:33 -04001383 DEVNAME (0) = DEVICE1_NAME;
wdenk4a6fd342003-04-12 23:38:12 +00001384 DEVOFFSET (0) = DEVICE1_OFFSET;
1385 ENVSIZE (0) = ENV1_SIZE;
Dustin Byford23869bf2014-03-06 20:48:22 -08001386 /* Default values are: erase-size=env-size */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001387 DEVESIZE (0) = ENVSIZE (0);
Dustin Byford23869bf2014-03-06 20:48:22 -08001388 /* #sectors=env-size/erase-size (rounded up) */
1389 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001390#ifdef DEVICE1_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001391 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001392#endif
1393#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001394 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001395#endif
1396
wdenk6aff3112002-12-17 01:51:00 +00001397#ifdef HAVE_REDUND
Tom Rini229695f2014-03-28 12:03:33 -04001398 DEVNAME (1) = DEVICE2_NAME;
wdenk4a6fd342003-04-12 23:38:12 +00001399 DEVOFFSET (1) = DEVICE2_OFFSET;
1400 ENVSIZE (1) = ENV2_SIZE;
Dustin Byford23869bf2014-03-06 20:48:22 -08001401 /* Default values are: erase-size=env-size */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001402 DEVESIZE (1) = ENVSIZE (1);
Dustin Byford23869bf2014-03-06 20:48:22 -08001403 /* #sectors=env-size/erase-size (rounded up) */
1404 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001405#ifdef DEVICE2_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001406 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001407#endif
1408#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001409 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001410#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001411 HaveRedundEnv = 1;
wdenk6aff3112002-12-17 01:51:00 +00001412#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001413#endif
Stefan Agner14fb5b22016-07-13 17:14:37 -07001414 rc = check_device_config(0);
1415 if (rc < 0)
1416 return rc;
wdenk4a6fd342003-04-12 23:38:12 +00001417
Stefan Agner14fb5b22016-07-13 17:14:37 -07001418 if (HaveRedundEnv) {
1419 rc = check_device_config(1);
1420 if (rc < 0)
1421 return rc;
Andreas Fenkartf71cee42016-04-19 22:43:42 +02001422
Stefan Agner14fb5b22016-07-13 17:14:37 -07001423 if (ENVSIZE(0) != ENVSIZE(1)) {
Stefan Agner14fb5b22016-07-13 17:14:37 -07001424 fprintf(stderr,
Andreas Fenkart490365c2016-08-17 23:41:53 +02001425 "Redundant environments have unequal size");
1426 return -1;
Stefan Agner14fb5b22016-07-13 17:14:37 -07001427 }
Andreas Fenkartf71cee42016-04-19 22:43:42 +02001428 }
1429
1430 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
1431 if (HaveRedundEnv)
1432 usable_envsize -= sizeof(char);
1433
Andreas Fenkart81974f42016-04-05 23:13:42 +02001434 if (opts->aes_flag)
Andreas Fenkartf71cee42016-04-19 22:43:42 +02001435 usable_envsize &= ~(AES_KEY_LENGTH - 1);
1436
wdenk6aff3112002-12-17 01:51:00 +00001437 return 0;
1438}
wdenkd0fb80c2003-01-11 09:48:40 +00001439
1440#if defined(CONFIG_FILE)
1441static int get_config (char *fname)
1442{
1443 FILE *fp;
1444 int i = 0;
1445 int rc;
1446 char dump[128];
Tom Rini229695f2014-03-28 12:03:33 -04001447 char *devname;
wdenkd0fb80c2003-01-11 09:48:40 +00001448
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001449 fp = fopen (fname, "r");
1450 if (fp == NULL)
1451 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001452
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001453 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001454 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001455 if (dump[0] == '#')
wdenkd0fb80c2003-01-11 09:48:40 +00001456 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001457
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001458 rc = sscanf(dump, "%ms %lli %lx %lx %lx",
1459 &devname,
1460 &DEVOFFSET(i),
1461 &ENVSIZE(i),
1462 &DEVESIZE(i),
1463 &ENVSECTORS(i));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001464
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001465 if (rc < 3)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001466 continue;
1467
Tom Rini229695f2014-03-28 12:03:33 -04001468 DEVNAME(i) = devname;
1469
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001470 if (rc < 4)
1471 /* Assume the erase size is the same as the env-size */
1472 DEVESIZE(i) = ENVSIZE(i);
1473
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001474 if (rc < 5)
Dustin Byford23869bf2014-03-06 20:48:22 -08001475 /* Assume enough env sectors to cover the environment */
1476 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
wdenkd0fb80c2003-01-11 09:48:40 +00001477
1478 i++;
1479 }
wdenk4a6fd342003-04-12 23:38:12 +00001480 fclose (fp);
1481
wdenkd0fb80c2003-01-11 09:48:40 +00001482 HaveRedundEnv = i - 1;
wdenk4a6fd342003-04-12 23:38:12 +00001483 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +00001484 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001485 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001486 } else
1487 return 0;
1488}
1489#endif