blob: 692abda7318fc07b2ae7b1fc0b14ef44c2e1f2a0 [file] [log] [blame]
wdenk34b613e2002-12-17 01:51:00 +00001/*
Detlev Zundel2111d3e2010-07-30 11:22:15 +02002 * (C) Copyright 2000-2010
wdenk34b613e2002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02005 * (C) Copyright 2008
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00009 */
10
Jörg Krause983487f2015-04-22 21:36:22 +020011#define _GNU_SOURCE
12
Peter Robinson656ee042015-12-09 07:15:33 +000013#include <compiler.h>
wdenk34b613e2002-12-17 01:51:00 +000014#include <errno.h>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060015#include <env_flags.h>
wdenk34b613e2002-12-17 01:51:00 +000016#include <fcntl.h>
Joe Hershbergere79a91f2012-10-12 10:23:37 +000017#include <linux/stringify.h>
Andreas Fenkartd0e12cb2016-03-11 09:39:35 +010018#include <ctype.h>
wdenk34b613e2002-12-17 01:51:00 +000019#include <stdio.h>
20#include <stdlib.h>
21#include <stddef.h>
22#include <string.h>
23#include <sys/types.h>
24#include <sys/ioctl.h>
25#include <sys/stat.h>
26#include <unistd.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010027
28#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020029# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010030# include <linux/mtd/mtd.h>
31#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010032# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010033# include <mtd/mtd-user.h>
34#endif
wdenk34b613e2002-12-17 01:51:00 +000035
Markus Klotzbücher94db5572007-11-27 10:23:20 +010036#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000037
Andreas Fenkarte6511302016-05-31 09:21:56 +020038struct env_opts default_opts = {
39#ifdef CONFIG_FILE
40 .config_file = CONFIG_FILE
41#endif
42};
43
Marek Vasut05ed2a52014-03-05 19:59:52 +010044#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
45
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020046#define min(x, y) ({ \
47 typeof(x) _min1 = (x); \
48 typeof(y) _min2 = (y); \
49 (void) (&_min1 == &_min2); \
50 _min1 < _min2 ? _min1 : _min2; })
51
52struct envdev_s {
Tom Rini313e9122014-03-28 12:03:33 -040053 const char *devname; /* Device name */
wdenk3f9ab982003-04-12 23:38:12 +000054 ulong devoff; /* Device offset */
55 ulong env_size; /* environment size */
56 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020057 ulong env_sectors; /* number of environment sectors */
58 uint8_t mtd_type; /* type of the MTD device */
59};
wdenk34b613e2002-12-17 01:51:00 +000060
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020061static struct envdev_s envdevices[2] =
62{
63 {
64 .mtd_type = MTD_ABSENT,
65 }, {
66 .mtd_type = MTD_ABSENT,
67 },
68};
69static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000070
71#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000072#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000073#define ENVSIZE(i) envdevices[(i)].env_size
74#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020075#define ENVSECTORS(i) envdevices[(i)].env_sectors
76#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk34b613e2002-12-17 01:51:00 +000077
Joe Hershberger671adc42012-10-03 09:38:46 +000078#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000079
Andreas Fenkartb8bf4042016-04-19 22:43:42 +020080static unsigned long usable_envsize;
81#define ENV_SIZE usable_envsize
wdenk34b613e2002-12-17 01:51:00 +000082
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020083struct env_image_single {
84 uint32_t crc; /* CRC32 over data bytes */
85 char data[];
86};
wdenk34b613e2002-12-17 01:51:00 +000087
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020088struct env_image_redundant {
89 uint32_t crc; /* CRC32 over data bytes */
90 unsigned char flags; /* active or obsolete */
91 char data[];
92};
93
94enum flag_scheme {
95 FLAG_NONE,
96 FLAG_BOOLEAN,
97 FLAG_INCREMENTAL,
98};
99
100struct environment {
101 void *image;
102 uint32_t *crc;
103 unsigned char *flags;
104 char *data;
105 enum flag_scheme flag_scheme;
106};
107
108static struct environment environment = {
109 .flag_scheme = FLAG_NONE,
110};
wdenk34b613e2002-12-17 01:51:00 +0000111
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200112static int env_aes_cbc_crypt(char *data, const int enc, uint8_t *key);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100113
wdenke7f34c62003-01-11 09:48:40 +0000114static int HaveRedundEnv = 0;
115
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100116static unsigned char active_flag = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200117/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100118static unsigned char obsolete_flag = 0;
wdenke7f34c62003-01-11 09:48:40 +0000119
Joe Hershberger81321142012-10-12 08:48:51 +0000120#define DEFAULT_ENV_INSTANCE_STATIC
121#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000122
wdenk3f9ab982003-04-12 23:38:12 +0000123static int flash_io (int mode);
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100124static char *envmatch (char * s1, char * s2);
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/*
150 * Search the environment for a variable.
151 * Return the value, if found, or NULL, if not found.
152 */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100153char *fw_getenv (char *name)
wdenk34b613e2002-12-17 01:51:00 +0000154{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100155 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000156
wdenk3f9ab982003-04-12 23:38:12 +0000157 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100158 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000159
wdenk3f9ab982003-04-12 23:38:12 +0000160 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000161 if (nxt >= &environment.data[ENV_SIZE]) {
162 fprintf (stderr, "## Error: "
163 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200164 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000165 }
166 }
wdenk3f9ab982003-04-12 23:38:12 +0000167 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000168 if (!val)
169 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200170 return val;
wdenk34b613e2002-12-17 01:51:00 +0000171 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200172 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000173}
174
175/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600176 * Search the default environment for a variable.
177 * Return the value, if found, or NULL, if not found.
178 */
179char *fw_getdefenv(char *name)
180{
181 char *env, *nxt;
182
183 for (env = default_environment; *env; env = nxt + 1) {
184 char *val;
185
186 for (nxt = env; *nxt; ++nxt) {
187 if (nxt >= &default_environment[ENV_SIZE]) {
188 fprintf(stderr, "## Error: "
189 "default environment not terminated\n");
190 return NULL;
191 }
192 }
193 val = envmatch(name, env);
194 if (!val)
195 continue;
196 return val;
197 }
198 return NULL;
199}
200
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100201int parse_aes_key(char *key, uint8_t *bin_key)
Marek Vasut05ed2a52014-03-05 19:59:52 +0100202{
203 char tmp[5] = { '0', 'x', 0, 0, 0 };
204 unsigned long ul;
205 int i;
206
207 if (strnlen(key, 64) != 32) {
208 fprintf(stderr,
209 "## Error: '-a' option requires 16-byte AES key\n");
210 return -1;
211 }
212
213 for (i = 0; i < 16; i++) {
214 tmp[2] = key[0];
215 tmp[3] = key[1];
216 errno = 0;
217 ul = strtoul(tmp, NULL, 16);
218 if (errno) {
219 fprintf(stderr,
220 "## Error: '-a' option requires valid AES key\n");
221 return -1;
222 }
Andreas Fenkarta34e5c52015-12-09 13:13:22 +0100223 bin_key[i] = ul & 0xff;
Marek Vasut05ed2a52014-03-05 19:59:52 +0100224 key += 2;
225 }
Marek Vasut05ed2a52014-03-05 19:59:52 +0100226 return 0;
227}
228
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600229/*
wdenk34b613e2002-12-17 01:51:00 +0000230 * Print the current definition of one, or more, or all
231 * environment variables
232 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200233int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000234{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100235 char *env, *nxt;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100236 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000237
Andreas Fenkarte6511302016-05-31 09:21:56 +0200238 if (!opts)
239 opts = &default_opts;
240
Andreas Fenkart24371902016-04-05 23:13:42 +0200241 if (fw_env_open(opts))
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200242 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000243
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100244 if (argc == 0) { /* Print all env variables */
wdenk3f9ab982003-04-12 23:38:12 +0000245 for (env = environment.data; *env; env = nxt + 1) {
246 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000247 if (nxt >= &environment.data[ENV_SIZE]) {
248 fprintf (stderr, "## Error: "
249 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200250 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000251 }
252 }
253
wdenk3f9ab982003-04-12 23:38:12 +0000254 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000255 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200256 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000257 }
258
Andreas Fenkart87bcee42016-04-19 22:43:41 +0200259 if (value_only && argc != 1) {
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100260 fprintf(stderr,
261 "## Error: `-n' option requires exactly one argument\n");
262 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000263 }
264
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100265 for (i = 0; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100266 char *name = argv[i];
267 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000268
wdenk3f9ab982003-04-12 23:38:12 +0000269 for (env = environment.data; *env; env = nxt + 1) {
wdenk34b613e2002-12-17 01:51:00 +0000270
wdenk3f9ab982003-04-12 23:38:12 +0000271 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000272 if (nxt >= &environment.data[ENV_SIZE]) {
273 fprintf (stderr, "## Error: "
274 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200275 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000276 }
277 }
wdenk3f9ab982003-04-12 23:38:12 +0000278 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000279 if (val) {
Andreas Fenkart87bcee42016-04-19 22:43:41 +0200280 if (!value_only) {
wdenk34b613e2002-12-17 01:51:00 +0000281 fputs (name, stdout);
wdenk3f9ab982003-04-12 23:38:12 +0000282 putc ('=', stdout);
wdenk34b613e2002-12-17 01:51:00 +0000283 }
wdenk3f9ab982003-04-12 23:38:12 +0000284 puts (val);
wdenk34b613e2002-12-17 01:51:00 +0000285 break;
286 }
287 }
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;
291 }
wdenk34b613e2002-12-17 01:51:00 +0000292 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700293
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200294 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000295}
296
Andreas Fenkart24371902016-04-05 23:13:42 +0200297int fw_env_close(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000298{
Marek Vasut05ed2a52014-03-05 19:59:52 +0100299 int ret;
Andreas Fenkart24371902016-04-05 23:13:42 +0200300
Andreas Fenkarte6511302016-05-31 09:21:56 +0200301 if (!opts)
302 opts = &default_opts;
303
Andreas Fenkart24371902016-04-05 23:13:42 +0200304 if (opts->aes_flag) {
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200305 ret = env_aes_cbc_crypt(environment.data, 1,
Andreas Fenkart24371902016-04-05 23:13:42 +0200306 opts->aes_key);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100307 if (ret) {
308 fprintf(stderr,
309 "Error: can't encrypt env for flash\n");
310 return ret;
311 }
312 }
313
Stefano Babicada628b2010-05-24 12:08:16 +0200314 /*
315 * Update CRC
316 */
317 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000318
Stefano Babicada628b2010-05-24 12:08:16 +0200319 /* write environment back to flash */
320 if (flash_io(O_RDWR)) {
321 fprintf(stderr,
322 "Error: can't write fw_env to flash\n");
323 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000324 }
325
Stefano Babicada628b2010-05-24 12:08:16 +0200326 return 0;
327}
wdenk34b613e2002-12-17 01:51:00 +0000328
Stefano Babicada628b2010-05-24 12:08:16 +0200329
330/*
331 * Set/Clear a single variable in the environment.
332 * This is called in sequence to update the environment
333 * in RAM without updating the copy in flash after each set
334 */
335int fw_env_write(char *name, char *value)
336{
337 int len;
338 char *env, *nxt;
339 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600340 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000341
342 /*
343 * search if variable with this name already exists
344 */
wdenk3f9ab982003-04-12 23:38:12 +0000345 for (nxt = env = environment.data; *env; env = nxt + 1) {
346 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000347 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200348 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000349 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200350 errno = EINVAL;
351 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000352 }
353 }
wdenk3f9ab982003-04-12 23:38:12 +0000354 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000355 break;
356 }
357
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600358 deleting = (oldval && !(value && strlen(value)));
359 creating = (!oldval && (value && strlen(value)));
360 overwriting = (oldval && (value && strlen(value)));
361
362 /* check for permission */
363 if (deleting) {
364 if (env_flags_validate_varaccess(name,
365 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
366 printf("Can't delete \"%s\"\n", name);
367 errno = EROFS;
368 return -1;
369 }
370 } else if (overwriting) {
371 if (env_flags_validate_varaccess(name,
372 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
373 printf("Can't overwrite \"%s\"\n", name);
374 errno = EROFS;
375 return -1;
376 } else if (env_flags_validate_varaccess(name,
377 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
378 const char *defval = fw_getdefenv(name);
379
380 if (defval == NULL)
381 defval = "";
382 if (strcmp(oldval, defval)
383 != 0) {
384 printf("Can't overwrite \"%s\"\n", name);
385 errno = EROFS;
386 return -1;
387 }
388 }
389 } else if (creating) {
390 if (env_flags_validate_varaccess(name,
391 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
392 printf("Can't create \"%s\"\n", name);
393 errno = EROFS;
394 return -1;
395 }
396 } else
397 /* Nothing to do */
398 return 0;
399
400 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000401 if (*++nxt == '\0') {
402 *env = '\0';
403 } else {
404 for (;;) {
405 *env = *nxt++;
406 if ((*env == '\0') && (*nxt == '\0'))
407 break;
408 ++env;
409 }
410 }
411 *++env = '\0';
412 }
413
414 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200415 if (!value || !strlen(value))
416 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000417
418 /*
419 * Append new definition at the end
420 */
wdenk3f9ab982003-04-12 23:38:12 +0000421 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000422 if (env > environment.data)
423 ++env;
424 /*
425 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000426 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000427 */
wdenk3f9ab982003-04-12 23:38:12 +0000428 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000429 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200430 len += strlen(value) + 1;
431
wdenk3f9ab982003-04-12 23:38:12 +0000432 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000433 fprintf (stderr,
434 "Error: environment overflow, \"%s\" deleted\n",
435 name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200436 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000437 }
Stefano Babicada628b2010-05-24 12:08:16 +0200438
wdenk34b613e2002-12-17 01:51:00 +0000439 while ((*env = *name++) != '\0')
440 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200441 *env = '=';
442 while ((*++env = *value++) != '\0')
443 ;
444
445 /* end is marked with double '\0' */
446 *++env = '\0';
447
448 return 0;
449}
450
451/*
452 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
453 * 0 - OK
454 * EINVAL - need at least 1 argument
455 * EROFS - certain variables ("ethaddr", "serial#") cannot be
456 * modified or deleted
457 *
458 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200459int fw_setenv(int argc, char *argv[], struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200460{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100461 int i;
Mike Frysinger178ec772012-11-10 19:47:45 +0000462 size_t len;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100463 char *name, **valv;
Stefano Babicada628b2010-05-24 12:08:16 +0200464 char *value = NULL;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100465 int valc;
Stefano Babicada628b2010-05-24 12:08:16 +0200466
Andreas Fenkarte6511302016-05-31 09:21:56 +0200467 if (!opts)
468 opts = &default_opts;
469
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100470 if (argc < 1) {
471 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasut05ed2a52014-03-05 19:59:52 +0100472 errno = EINVAL;
473 return -1;
474 }
475
Andreas Fenkart24371902016-04-05 23:13:42 +0200476 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200477 fprintf(stderr, "Error: environment not initialized\n");
478 return -1;
479 }
480
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100481 name = argv[0];
482 valv = argv + 1;
483 valc = argc - 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200484
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100485 if (env_flags_validate_env_set_params(name, valv, valc) < 0)
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600486 return 1;
487
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000488 len = 0;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100489 for (i = 0; i < valc; ++i) {
490 char *val = valv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000491 size_t val_len = strlen(val);
492
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000493 if (value)
494 value[len - 1] = ' ';
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000495 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200496 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000497 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200498 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000499 len, strerror(errno));
500 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200501 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000502
503 memcpy(value + len, val, val_len);
504 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000505 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000506 }
507
Stefano Babicada628b2010-05-24 12:08:16 +0200508 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000509
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000510 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000511
Andreas Fenkart24371902016-04-05 23:13:42 +0200512 return fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200513}
wdenk34b613e2002-12-17 01:51:00 +0000514
Stefano Babicada628b2010-05-24 12:08:16 +0200515/*
516 * Parse a file and configure the u-boot variables.
517 * The script file has a very simple format, as follows:
518 *
519 * Each line has a couple with name, value:
520 * <white spaces>variable_name<white spaces>variable_value
521 *
522 * Both variable_name and variable_value are interpreted as strings.
523 * Any character after <white spaces> and before ending \r\n is interpreted
524 * as variable's value (no comment allowed on these lines !)
525 *
526 * Comments are allowed if the first character in the line is #
527 *
528 * Returns -1 and sets errno error codes:
529 * 0 - OK
530 * -1 - Error
531 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200532int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200533{
534 FILE *fp;
535 char dump[1024]; /* Maximum line length in the file */
536 char *name;
537 char *val;
538 int lineno = 0;
539 int len;
540 int ret = 0;
541
Andreas Fenkarte6511302016-05-31 09:21:56 +0200542 if (!opts)
543 opts = &default_opts;
544
Andreas Fenkart24371902016-04-05 23:13:42 +0200545 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200546 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200547 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000548 }
549
Stefano Babicada628b2010-05-24 12:08:16 +0200550 if (strcmp(fname, "-") == 0)
551 fp = stdin;
552 else {
553 fp = fopen(fname, "r");
554 if (fp == NULL) {
555 fprintf(stderr, "I cannot open %s for reading\n",
556 fname);
557 return -1;
558 }
559 }
560
561 while (fgets(dump, sizeof(dump), fp)) {
562 lineno++;
563 len = strlen(dump);
564
565 /*
566 * Read a whole line from the file. If the line is too long
567 * or is not terminated, reports an error and exit.
568 */
569 if (dump[len - 1] != '\n') {
570 fprintf(stderr,
571 "Line %d not corrected terminated or too long\n",
572 lineno);
573 ret = -1;
574 break;
575 }
576
577 /* Drop ending line feed / carriage return */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100578 dump[--len] = '\0';
579 if (len && dump[len - 1] == '\r')
580 dump[--len] = '\0';
Stefano Babicada628b2010-05-24 12:08:16 +0200581
582 /* Skip comment or empty lines */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100583 if (len == 0 || dump[0] == '#')
Stefano Babicada628b2010-05-24 12:08:16 +0200584 continue;
585
586 /*
587 * Search for variable's name,
588 * remove leading whitespaces
589 */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100590 name = skip_blanks(dump);
Stefano Babicada628b2010-05-24 12:08:16 +0200591 if (!name)
592 continue;
593
594 /* The first white space is the end of variable name */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100595 val = skip_chars(name);
Stefano Babicada628b2010-05-24 12:08:16 +0200596 len = strlen(name);
597 if (val) {
598 *val++ = '\0';
599 if ((val - name) < len)
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100600 val = skip_blanks(val);
Stefano Babicada628b2010-05-24 12:08:16 +0200601 else
602 val = NULL;
603 }
604
605#ifdef DEBUG
606 fprintf(stderr, "Setting %s : %s\n",
607 name, val ? val : " removed");
608#endif
609
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600610 if (env_flags_validate_type(name, val) < 0) {
611 ret = -1;
612 break;
613 }
614
Stefano Babicada628b2010-05-24 12:08:16 +0200615 /*
616 * If there is an error setting a variable,
617 * try to save the environment and returns an error
618 */
619 if (fw_env_write(name, val)) {
620 fprintf(stderr,
621 "fw_env_write returns with error : %s\n",
622 strerror(errno));
623 ret = -1;
624 break;
625 }
626
627 }
628
629 /* Close file if not stdin */
630 if (strcmp(fname, "-") != 0)
631 fclose(fp);
632
Andreas Fenkart24371902016-04-05 23:13:42 +0200633 ret |= fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200634
635 return ret;
wdenk34b613e2002-12-17 01:51:00 +0000636}
637
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200638/*
639 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
640 * 0 - block is good
641 * > 0 - block is bad
642 * < 0 - failed to test
643 */
644static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000645{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200646 if (mtd_type == MTD_NANDFLASH) {
647 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000648
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200649 if (badblock < 0) {
650 perror ("Cannot read bad block mark");
651 return badblock;
652 }
653
654 if (badblock) {
655#ifdef DEBUG
656 fprintf (stderr, "Bad block at 0x%llx, "
657 "skipping\n", *blockstart);
658#endif
659 return badblock;
660 }
wdenk34b613e2002-12-17 01:51:00 +0000661 }
662
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200663 return 0;
664}
665
666/*
667 * Read data from flash at an offset into a provided buffer. On NAND it skips
668 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
669 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
670 */
671static int flash_read_buf (int dev, int fd, void *buf, size_t count,
672 off_t offset, uint8_t mtd_type)
673{
674 size_t blocklen; /* erase / write length - one block on NAND,
675 0 on NOR */
676 size_t processed = 0; /* progress counter */
677 size_t readlen = count; /* current read length */
678 off_t top_of_range; /* end of the last block we may use */
679 off_t block_seek; /* offset inside the current block to the start
680 of the data */
681 loff_t blockstart; /* running start of the current block -
682 MEMGETBADBLOCK needs 64 bits */
683 int rc;
684
Remy Bohmercb969eb2011-02-12 19:06:26 +0100685 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200686
687 /* Offset inside a block */
688 block_seek = offset - blockstart;
689
690 if (mtd_type == MTD_NANDFLASH) {
691 /*
692 * NAND: calculate which blocks we are reading. We have
693 * to read one block at a time to skip bad blocks.
694 */
695 blocklen = DEVESIZE (dev);
696
697 /*
698 * To calculate the top of the range, we have to use the
699 * global DEVOFFSET (dev), which can be different from offset
700 */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100701 top_of_range = ((DEVOFFSET(dev) / blocklen) +
702 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200703
704 /* Limit to one block for the first read */
705 if (readlen > blocklen - block_seek)
706 readlen = blocklen - block_seek;
707 } else {
708 blocklen = 0;
709 top_of_range = offset + count;
wdenke7f34c62003-01-11 09:48:40 +0000710 }
wdenk34b613e2002-12-17 01:51:00 +0000711
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200712 /* This only runs once on NOR flash */
713 while (processed < count) {
714 rc = flash_bad_block (fd, mtd_type, &blockstart);
715 if (rc < 0) /* block test failed */
716 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000717
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200718 if (blockstart + block_seek + readlen > top_of_range) {
719 /* End of range is reached */
720 fprintf (stderr,
721 "Too few good blocks within range\n");
722 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000723 }
724
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200725 if (rc) { /* block is bad */
726 blockstart += blocklen;
727 continue;
wdenk34b613e2002-12-17 01:51:00 +0000728 }
729
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200730 /*
731 * If a block is bad, we retry in the next block at the same
732 * offset - see common/env_nand.c::writeenv()
733 */
734 lseek (fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000735
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200736 rc = read (fd, buf + processed, readlen);
737 if (rc != readlen) {
738 fprintf (stderr, "Read error on %s: %s\n",
739 DEVNAME (dev), strerror (errno));
740 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000741 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200742#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000743 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
744 rc, blockstart + block_seek, DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200745#endif
746 processed += readlen;
747 readlen = min (blocklen, count - processed);
748 block_seek = 0;
749 blockstart += blocklen;
750 }
751
752 return processed;
753}
754
755/*
Remy Bohmercb969eb2011-02-12 19:06:26 +0100756 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
757 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
758 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200759 */
760static int flash_write_buf (int dev, int fd, void *buf, size_t count,
761 off_t offset, uint8_t mtd_type)
762{
763 void *data;
764 struct erase_info_user erase;
765 size_t blocklen; /* length of NAND block / NOR erase sector */
766 size_t erase_len; /* whole area that can be erased - may include
767 bad blocks */
768 size_t erasesize; /* erase / write length - one block on NAND,
769 whole area on NOR */
770 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100771 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200772 bad blocks */
773 off_t erase_offset; /* offset to the first erase block (aligned)
774 below offset */
775 off_t block_seek; /* offset inside the erase block to the start
776 of the data */
777 off_t top_of_range; /* end of the last block we may use */
778 loff_t blockstart; /* running start of the current block -
779 MEMGETBADBLOCK needs 64 bits */
780 int rc;
781
Oliver Metze9c66ab2013-08-30 00:56:02 +0200782 /*
783 * For mtd devices only offset and size of the environment do matter
784 */
785 if (mtd_type == MTD_ABSENT) {
786 blocklen = count;
787 top_of_range = offset + count;
788 erase_len = blocklen;
789 blockstart = offset;
790 block_seek = 0;
791 write_total = blocklen;
792 } else {
793 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200794
Oliver Metze9c66ab2013-08-30 00:56:02 +0200795 top_of_range = ((DEVOFFSET(dev) / blocklen) +
796 ENVSECTORS(dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200797
Oliver Metze9c66ab2013-08-30 00:56:02 +0200798 erase_offset = (offset / blocklen) * blocklen;
wdenk34b613e2002-12-17 01:51:00 +0000799
Oliver Metze9c66ab2013-08-30 00:56:02 +0200800 /* Maximum area we may use */
801 erase_len = top_of_range - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +0000802
Oliver Metze9c66ab2013-08-30 00:56:02 +0200803 blockstart = erase_offset;
804 /* Offset inside a block */
805 block_seek = offset - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200806
Oliver Metze9c66ab2013-08-30 00:56:02 +0200807 /*
808 * Data size we actually write: from the start of the block
809 * to the start of the data, then count bytes of data, and
810 * to the end of the block
811 */
812 write_total = ((block_seek + count + blocklen - 1) /
813 blocklen) * blocklen;
814 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200815
816 /*
817 * Support data anywhere within erase sectors: read out the complete
818 * area to be erased, replace the environment image, write the whole
819 * block back again.
820 */
821 if (write_total > count) {
822 data = malloc (erase_len);
823 if (!data) {
wdenke7f34c62003-01-11 09:48:40 +0000824 fprintf (stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200825 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200826 erase_len, strerror (errno));
827 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000828 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200829
830 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
831 mtd_type);
832 if (write_total != rc)
833 return -1;
834
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000835#ifdef DEBUG
836 fprintf(stderr, "Preserving data ");
837 if (block_seek != 0)
838 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
839 if (block_seek + count != write_total) {
840 if (block_seek != 0)
841 fprintf(stderr, " and ");
842 fprintf(stderr, "0x%lx - 0x%x",
843 block_seek + count, write_total - 1);
844 }
845 fprintf(stderr, "\n");
846#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200847 /* Overwrite the old environment */
848 memcpy (data + block_seek, buf, count);
849 } else {
850 /*
851 * We get here, iff offset is block-aligned and count is a
852 * multiple of blocklen - see write_total calculation above
853 */
854 data = buf;
855 }
856
857 if (mtd_type == MTD_NANDFLASH) {
858 /*
859 * NAND: calculate which blocks we are writing. We have
860 * to write one block at a time to skip bad blocks.
861 */
862 erasesize = blocklen;
863 } else {
864 erasesize = erase_len;
865 }
866
867 erase.length = erasesize;
868
Remy Bohmercb969eb2011-02-12 19:06:26 +0100869 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200870 while (processed < write_total) {
871 rc = flash_bad_block (fd, mtd_type, &blockstart);
872 if (rc < 0) /* block test failed */
873 return rc;
874
875 if (blockstart + erasesize > top_of_range) {
876 fprintf (stderr, "End of range reached, aborting\n");
877 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000878 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200879
880 if (rc) { /* block is bad */
881 blockstart += blocklen;
882 continue;
883 }
884
Oliver Metze9c66ab2013-08-30 00:56:02 +0200885 if (mtd_type != MTD_ABSENT) {
886 erase.start = blockstart;
887 ioctl(fd, MEMUNLOCK, &erase);
888 /* These do not need an explicit erase cycle */
889 if (mtd_type != MTD_DATAFLASH)
890 if (ioctl(fd, MEMERASE, &erase) != 0) {
891 fprintf(stderr,
892 "MTD erase error on %s: %s\n",
893 DEVNAME(dev), strerror(errno));
894 return -1;
895 }
896 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200897
898 if (lseek (fd, blockstart, SEEK_SET) == -1) {
wdenk34b613e2002-12-17 01:51:00 +0000899 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200900 "Seek error on %s: %s\n",
901 DEVNAME (dev), strerror (errno));
902 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000903 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200904
905#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000906 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
907 blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200908#endif
909 if (write (fd, data + processed, erasesize) != erasesize) {
910 fprintf (stderr, "Write error on %s: %s\n",
911 DEVNAME (dev), strerror (errno));
912 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000913 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200914
Oliver Metze9c66ab2013-08-30 00:56:02 +0200915 if (mtd_type != MTD_ABSENT)
916 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200917
Dustin Byford0983cad2014-03-06 20:48:23 -0800918 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200919 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -0800920 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200921 }
922
923 if (write_total > count)
924 free (data);
925
926 return processed;
927}
928
929/*
930 * Set obsolete flag at offset - NOR flash only
931 */
932static int flash_flag_obsolete (int dev, int fd, off_t offset)
933{
934 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200935 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200936
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200937 erase.start = DEVOFFSET (dev);
938 erase.length = DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200939 /* This relies on the fact, that obsolete_flag == 0 */
940 rc = lseek (fd, offset, SEEK_SET);
941 if (rc < 0) {
942 fprintf (stderr, "Cannot seek to set the flag on %s \n",
943 DEVNAME (dev));
944 return rc;
945 }
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200946 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200947 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200948 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200949 if (rc < 0)
950 perror ("Could not set obsolete flag");
951
952 return rc;
953}
954
Marek Vasut05ed2a52014-03-05 19:59:52 +0100955/* Encrypt or decrypt the environment before writing or reading it. */
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200956static int env_aes_cbc_crypt(char *payload, const int enc, uint8_t *key)
Marek Vasut05ed2a52014-03-05 19:59:52 +0100957{
958 uint8_t *data = (uint8_t *)payload;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +0200959 const int len = usable_envsize;
Marek Vasut05ed2a52014-03-05 19:59:52 +0100960 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
961 uint32_t aes_blocks;
962
963 /* First we expand the key. */
Andreas Fenkartedcaee62016-04-19 22:43:39 +0200964 aes_expand_key(key, key_exp);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100965
966 /* Calculate the number of AES blocks to encrypt. */
967 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
968
969 if (enc)
970 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
971 else
972 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
973
974 return 0;
975}
976
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200977static int flash_write (int fd_current, int fd_target, int dev_target)
978{
979 int rc;
980
981 switch (environment.flag_scheme) {
982 case FLAG_NONE:
983 break;
984 case FLAG_INCREMENTAL:
985 (*environment.flags)++;
986 break;
987 case FLAG_BOOLEAN:
988 *environment.flags = active_flag;
989 break;
990 default:
991 fprintf (stderr, "Unimplemented flash scheme %u \n",
992 environment.flag_scheme);
993 return -1;
994 }
995
996#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000997 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200998 DEVOFFSET (dev_target), DEVNAME (dev_target));
999#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +01001000
Joe Hershberger671adc42012-10-03 09:38:46 +00001001 rc = flash_write_buf(dev_target, fd_target, environment.image,
1002 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001003 DEVTYPE(dev_target));
1004 if (rc < 0)
1005 return rc;
1006
1007 if (environment.flag_scheme == FLAG_BOOLEAN) {
1008 /* Have to set obsolete flag */
1009 off_t offset = DEVOFFSET (dev_current) +
1010 offsetof (struct env_image_redundant, flags);
1011#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001012 fprintf(stderr,
1013 "Setting obsolete flag in environment at 0x%lx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001014 DEVOFFSET (dev_current), DEVNAME (dev_current));
1015#endif
1016 flash_flag_obsolete (dev_current, fd_current, offset);
1017 }
1018
1019 return 0;
1020}
1021
1022static int flash_read (int fd)
1023{
1024 struct mtd_info_user mtdinfo;
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001025 struct stat st;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001026 int rc;
1027
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001028 rc = fstat(fd, &st);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001029 if (rc < 0) {
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001030 fprintf(stderr, "Cannot stat the file %s\n",
1031 DEVNAME(dev_current));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001032 return -1;
1033 }
1034
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001035 if (S_ISCHR(st.st_mode)) {
1036 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1037 if (rc < 0) {
1038 fprintf(stderr, "Cannot get MTD information for %s\n",
1039 DEVNAME(dev_current));
1040 return -1;
1041 }
1042 if (mtdinfo.type != MTD_NORFLASH &&
1043 mtdinfo.type != MTD_NANDFLASH &&
Joe Hershberger0c5faa82013-04-08 10:32:51 +00001044 mtdinfo.type != MTD_DATAFLASH &&
1045 mtdinfo.type != MTD_UBIVOLUME) {
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001046 fprintf (stderr, "Unsupported flash type %u on %s\n",
1047 mtdinfo.type, DEVNAME(dev_current));
1048 return -1;
1049 }
1050 } else {
1051 memset(&mtdinfo, 0, sizeof(mtdinfo));
1052 mtdinfo.type = MTD_ABSENT;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001053 }
1054
1055 DEVTYPE(dev_current) = mtdinfo.type;
1056
Joe Hershberger671adc42012-10-03 09:38:46 +00001057 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001058 DEVOFFSET (dev_current), mtdinfo.type);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001059 if (rc != CUR_ENVSIZE)
1060 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001061
Marek Vasut05ed2a52014-03-05 19:59:52 +01001062 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001063}
1064
1065static int flash_io (int mode)
1066{
1067 int fd_current, fd_target, rc, dev_target;
1068
1069 /* dev_current: fd_current, erase_current */
1070 fd_current = open (DEVNAME (dev_current), mode);
1071 if (fd_current < 0) {
1072 fprintf (stderr,
1073 "Can't open %s: %s\n",
1074 DEVNAME (dev_current), strerror (errno));
1075 return -1;
1076 }
1077
1078 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +00001079 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001080 /* switch to next partition for writing */
1081 dev_target = !dev_current;
1082 /* dev_target: fd_target, erase_target */
1083 fd_target = open (DEVNAME (dev_target), mode);
1084 if (fd_target < 0) {
wdenke7f34c62003-01-11 09:48:40 +00001085 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001086 "Can't open %s: %s\n",
1087 DEVNAME (dev_target),
1088 strerror (errno));
1089 rc = -1;
1090 goto exit;
wdenke7f34c62003-01-11 09:48:40 +00001091 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001092 } else {
1093 dev_target = dev_current;
1094 fd_target = fd_current;
wdenk34b613e2002-12-17 01:51:00 +00001095 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001096
1097 rc = flash_write (fd_current, fd_target, dev_target);
1098
wdenke7f34c62003-01-11 09:48:40 +00001099 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001100 if (close (fd_target)) {
wdenke7f34c62003-01-11 09:48:40 +00001101 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001102 "I/O error on %s: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001103 DEVNAME (dev_target),
wdenk3f9ab982003-04-12 23:38:12 +00001104 strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001105 rc = -1;
wdenke7f34c62003-01-11 09:48:40 +00001106 }
wdenk34b613e2002-12-17 01:51:00 +00001107 }
wdenk34b613e2002-12-17 01:51:00 +00001108 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001109 rc = flash_read (fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001110 }
1111
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001112exit:
1113 if (close (fd_current)) {
wdenk34b613e2002-12-17 01:51:00 +00001114 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001115 "I/O error on %s: %s\n",
1116 DEVNAME (dev_current), strerror (errno));
1117 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001118 }
1119
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001120 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001121}
1122
1123/*
1124 * s1 is either a simple 'name', or a 'name=value' pair.
1125 * s2 is a 'name=value' pair.
1126 * If the names match, return the value of s2, else NULL.
1127 */
1128
Markus Klotzbücher94db5572007-11-27 10:23:20 +01001129static char *envmatch (char * s1, char * s2)
wdenk34b613e2002-12-17 01:51:00 +00001130{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +00001131 if (s1 == NULL || s2 == NULL)
1132 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001133
1134 while (*s1 == *s2++)
1135 if (*s1++ == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001136 return s2;
wdenk3f9ab982003-04-12 23:38:12 +00001137 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001138 return s2;
1139 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001140}
1141
1142/*
1143 * Prevent confusion if running from erased flash memory
1144 */
Andreas Fenkart24371902016-04-05 23:13:42 +02001145int fw_env_open(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001146{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001147 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001148 unsigned char flag0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001149 void *addr0;
1150
wdenk34b613e2002-12-17 01:51:00 +00001151 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001152 unsigned char flag1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001153 void *addr1;
wdenke7f34c62003-01-11 09:48:40 +00001154
Marek Vasut05ed2a52014-03-05 19:59:52 +01001155 int ret;
1156
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001157 struct env_image_single *single;
1158 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001159
Andreas Fenkarte6511302016-05-31 09:21:56 +02001160 if (!opts)
1161 opts = &default_opts;
1162
Andreas Fenkart24371902016-04-05 23:13:42 +02001163 if (parse_config(opts)) /* should fill envdevices */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001164 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001165
Joe Hershberger671adc42012-10-03 09:38:46 +00001166 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001167 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001168 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001169 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001170 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001171 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001172 }
wdenk3f9ab982003-04-12 23:38:12 +00001173
wdenke7f34c62003-01-11 09:48:40 +00001174 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001175 environment.image = addr0;
1176
1177 if (HaveRedundEnv) {
1178 redundant = addr0;
1179 environment.crc = &redundant->crc;
1180 environment.flags = &redundant->flags;
1181 environment.data = redundant->data;
1182 } else {
1183 single = addr0;
1184 environment.crc = &single->crc;
1185 environment.flags = NULL;
1186 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001187 }
wdenk3f9ab982003-04-12 23:38:12 +00001188
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001189 dev_current = 0;
1190 if (flash_io (O_RDONLY))
1191 return -1;
1192
1193 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001194
Andreas Fenkart24371902016-04-05 23:13:42 +02001195 if (opts->aes_flag) {
Andreas Fenkartedcaee62016-04-19 22:43:39 +02001196 ret = env_aes_cbc_crypt(environment.data, 0,
Andreas Fenkart24371902016-04-05 23:13:42 +02001197 opts->aes_key);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001198 if (ret)
1199 return ret;
1200 }
1201
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001202 crc0_ok = (crc0 == *environment.crc);
wdenke7f34c62003-01-11 09:48:40 +00001203 if (!HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001204 if (!crc0_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001205 fprintf (stderr,
1206 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkc5203f92006-03-12 23:27:46 +01001207 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk34b613e2002-12-17 01:51:00 +00001208 }
wdenke7f34c62003-01-11 09:48:40 +00001209 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001210 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001211
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001212 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001213 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001214 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001215 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001216 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001217 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001218 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001219 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001220 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001221
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001222 /*
1223 * have to set environment.image for flash_read(), careful -
1224 * other pointers in environment still point inside addr0
1225 */
1226 environment.image = addr1;
1227 if (flash_io (O_RDONLY))
1228 return -1;
1229
1230 /* Check flag scheme compatibility */
1231 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1232 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1233 environment.flag_scheme = FLAG_BOOLEAN;
1234 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1235 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1236 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001237 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1238 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1239 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001240 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1241 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1242 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001243 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1244 DEVTYPE(!dev_current) == MTD_ABSENT) {
1245 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001246 } else {
1247 fprintf (stderr, "Incompatible flash types!\n");
1248 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001249 }
wdenk3f9ab982003-04-12 23:38:12 +00001250
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001251 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001252
Andreas Fenkart24371902016-04-05 23:13:42 +02001253 if (opts->aes_flag) {
Andreas Fenkartedcaee62016-04-19 22:43:39 +02001254 ret = env_aes_cbc_crypt(redundant->data, 0,
Andreas Fenkart24371902016-04-05 23:13:42 +02001255 opts->aes_key);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001256 if (ret)
1257 return ret;
1258 }
1259
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001260 crc1_ok = (crc1 == redundant->crc);
1261 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001262
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001263 if (crc0_ok && !crc1_ok) {
1264 dev_current = 0;
1265 } else if (!crc0_ok && crc1_ok) {
1266 dev_current = 1;
1267 } else if (!crc0_ok && !crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001268 fprintf (stderr,
1269 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001270 memcpy (environment.data, default_environment,
1271 sizeof default_environment);
1272 dev_current = 0;
1273 } else {
1274 switch (environment.flag_scheme) {
1275 case FLAG_BOOLEAN:
1276 if (flag0 == active_flag &&
1277 flag1 == obsolete_flag) {
1278 dev_current = 0;
1279 } else if (flag0 == obsolete_flag &&
1280 flag1 == active_flag) {
1281 dev_current = 1;
1282 } else if (flag0 == flag1) {
1283 dev_current = 0;
1284 } else if (flag0 == 0xFF) {
1285 dev_current = 0;
1286 } else if (flag1 == 0xFF) {
1287 dev_current = 1;
1288 } else {
1289 dev_current = 0;
1290 }
1291 break;
1292 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001293 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001294 dev_current = 1;
1295 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001296 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001297 dev_current = 0;
Jon Povey93ccd472011-03-11 14:10:56 +09001298 else /* flag1 > flag0 */
1299 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001300 break;
1301 default:
1302 fprintf (stderr, "Unknown flag scheme %u \n",
1303 environment.flag_scheme);
1304 return -1;
1305 }
1306 }
1307
1308 /*
1309 * If we are reading, we don't need the flag and the CRC any
1310 * more, if we are writing, we will re-calculate CRC and update
1311 * flags before writing out
1312 */
1313 if (dev_current) {
1314 environment.image = addr1;
1315 environment.crc = &redundant->crc;
1316 environment.flags = &redundant->flags;
1317 environment.data = redundant->data;
1318 free (addr0);
1319 } else {
1320 environment.image = addr0;
1321 /* Other pointers are already set */
wdenk3f9ab982003-04-12 23:38:12 +00001322 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +00001323 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001324#ifdef DEBUG
1325 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1326#endif
wdenk34b613e2002-12-17 01:51:00 +00001327 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001328 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001329}
1330
1331
Andreas Fenkart24371902016-04-05 23:13:42 +02001332static int parse_config(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001333{
1334 struct stat st;
1335
Andreas Fenkarte6511302016-05-31 09:21:56 +02001336 if (!opts)
1337 opts = &default_opts;
Anatolij Gustschine89ead32016-04-29 22:00:11 +02001338
Andreas Fenkarte6511302016-05-31 09:21:56 +02001339#if defined(CONFIG_FILE)
wdenke7f34c62003-01-11 09:48:40 +00001340 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart24371902016-04-05 23:13:42 +02001341 if (get_config(opts->config_file)) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001342 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart24371902016-04-05 23:13:42 +02001343 opts->config_file);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001344 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001345 }
wdenke7f34c62003-01-11 09:48:40 +00001346#else
Tom Rini313e9122014-03-28 12:03:33 -04001347 DEVNAME (0) = DEVICE1_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001348 DEVOFFSET (0) = DEVICE1_OFFSET;
1349 ENVSIZE (0) = ENV1_SIZE;
Dustin Byford2f6df462014-03-06 20:48:22 -08001350 /* Default values are: erase-size=env-size */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001351 DEVESIZE (0) = ENVSIZE (0);
Dustin Byford2f6df462014-03-06 20:48:22 -08001352 /* #sectors=env-size/erase-size (rounded up) */
1353 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
Remy Bohmercb969eb2011-02-12 19:06:26 +01001354#ifdef DEVICE1_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001355 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001356#endif
1357#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001358 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001359#endif
1360
wdenk34b613e2002-12-17 01:51:00 +00001361#ifdef HAVE_REDUND
Tom Rini313e9122014-03-28 12:03:33 -04001362 DEVNAME (1) = DEVICE2_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001363 DEVOFFSET (1) = DEVICE2_OFFSET;
1364 ENVSIZE (1) = ENV2_SIZE;
Dustin Byford2f6df462014-03-06 20:48:22 -08001365 /* Default values are: erase-size=env-size */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001366 DEVESIZE (1) = ENVSIZE (1);
Dustin Byford2f6df462014-03-06 20:48:22 -08001367 /* #sectors=env-size/erase-size (rounded up) */
1368 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
Remy Bohmercb969eb2011-02-12 19:06:26 +01001369#ifdef DEVICE2_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001370 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001371#endif
1372#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001373 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001374#endif
wdenke7f34c62003-01-11 09:48:40 +00001375 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +00001376#endif
wdenke7f34c62003-01-11 09:48:40 +00001377#endif
wdenk3f9ab982003-04-12 23:38:12 +00001378 if (stat (DEVNAME (0), &st)) {
1379 fprintf (stderr,
1380 "Cannot access MTD device %s: %s\n",
1381 DEVNAME (0), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001382 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001383 }
wdenk3f9ab982003-04-12 23:38:12 +00001384
1385 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1386 fprintf (stderr,
1387 "Cannot access MTD device %s: %s\n",
Wolfgang Denk8e6213c2005-09-25 01:20:38 +02001388 DEVNAME (1), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001389 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001390 }
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001391
1392 if (HaveRedundEnv && ENVSIZE(0) != ENVSIZE(1)) {
1393 ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1));
1394 fprintf(stderr,
1395 "Redundant environments have inequal size, set to 0x%08lx\n",
1396 ENVSIZE(1));
1397 }
1398
1399 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
1400 if (HaveRedundEnv)
1401 usable_envsize -= sizeof(char);
1402
Andreas Fenkart24371902016-04-05 23:13:42 +02001403 if (opts->aes_flag)
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001404 usable_envsize &= ~(AES_KEY_LENGTH - 1);
1405
wdenk34b613e2002-12-17 01:51:00 +00001406 return 0;
1407}
wdenke7f34c62003-01-11 09:48:40 +00001408
1409#if defined(CONFIG_FILE)
1410static int get_config (char *fname)
1411{
1412 FILE *fp;
1413 int i = 0;
1414 int rc;
1415 char dump[128];
Tom Rini313e9122014-03-28 12:03:33 -04001416 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001417
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001418 fp = fopen (fname, "r");
1419 if (fp == NULL)
1420 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001421
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001422 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenke7f34c62003-01-11 09:48:40 +00001423 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001424 if (dump[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001425 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001426
Tom Rini313e9122014-03-28 12:03:33 -04001427 rc = sscanf (dump, "%ms %lx %lx %lx %lx",
1428 &devname,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001429 &DEVOFFSET (i),
1430 &ENVSIZE (i),
1431 &DEVESIZE (i),
1432 &ENVSECTORS (i));
1433
Remy Bohmercb969eb2011-02-12 19:06:26 +01001434 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001435 continue;
1436
Tom Rini313e9122014-03-28 12:03:33 -04001437 DEVNAME(i) = devname;
1438
Remy Bohmercb969eb2011-02-12 19:06:26 +01001439 if (rc < 4)
1440 /* Assume the erase size is the same as the env-size */
1441 DEVESIZE(i) = ENVSIZE(i);
1442
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001443 if (rc < 5)
Dustin Byford2f6df462014-03-06 20:48:22 -08001444 /* Assume enough env sectors to cover the environment */
1445 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
wdenke7f34c62003-01-11 09:48:40 +00001446
1447 i++;
1448 }
wdenk3f9ab982003-04-12 23:38:12 +00001449 fclose (fp);
1450
wdenke7f34c62003-01-11 09:48:40 +00001451 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +00001452 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001453 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001454 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001455 } else
1456 return 0;
1457}
1458#endif