blob: 286165618304f9259daadde0c8262f10265539ca [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
Stefano Babic9d80b492017-04-05 18:08:01 +020037#include "fw_env_private.h"
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010038#include "fw_env.h"
wdenk6aff3112002-12-17 01:51:00 +000039
Andreas Fenkart14070e62016-05-31 09:21:56 +020040struct env_opts default_opts = {
41#ifdef CONFIG_FILE
42 .config_file = CONFIG_FILE
43#endif
44};
45
Marek Vasuta8a752c2014-03-05 19:59:52 +010046#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
47
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020048#define min(x, y) ({ \
49 typeof(x) _min1 = (x); \
50 typeof(y) _min2 = (y); \
51 (void) (&_min1 == &_min2); \
52 _min1 < _min2 ? _min1 : _min2; })
53
54struct envdev_s {
Tom Rini229695f2014-03-28 12:03:33 -040055 const char *devname; /* Device name */
Stefan Agnerf4742ca2016-07-13 17:14:38 -070056 long long devoff; /* Device offset */
wdenk4a6fd342003-04-12 23:38:12 +000057 ulong env_size; /* environment size */
58 ulong erase_size; /* device erase size */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020059 ulong env_sectors; /* number of environment sectors */
60 uint8_t mtd_type; /* type of the MTD device */
61};
wdenk6aff3112002-12-17 01:51:00 +000062
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020063static struct envdev_s envdevices[2] =
64{
65 {
66 .mtd_type = MTD_ABSENT,
67 }, {
68 .mtd_type = MTD_ABSENT,
69 },
70};
71static int dev_current;
wdenk6aff3112002-12-17 01:51:00 +000072
73#define DEVNAME(i) envdevices[(i)].devname
wdenkd0fb80c2003-01-11 09:48:40 +000074#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk6aff3112002-12-17 01:51:00 +000075#define ENVSIZE(i) envdevices[(i)].env_size
76#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020077#define ENVSECTORS(i) envdevices[(i)].env_sectors
78#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk6aff3112002-12-17 01:51:00 +000079
Joe Hershberger497f2052012-10-03 09:38:46 +000080#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk6aff3112002-12-17 01:51:00 +000081
Andreas Fenkartf71cee42016-04-19 22:43:42 +020082static unsigned long usable_envsize;
83#define ENV_SIZE usable_envsize
wdenk6aff3112002-12-17 01:51:00 +000084
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020085struct env_image_single {
86 uint32_t crc; /* CRC32 over data bytes */
87 char data[];
88};
wdenk6aff3112002-12-17 01:51:00 +000089
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020090struct env_image_redundant {
91 uint32_t crc; /* CRC32 over data bytes */
92 unsigned char flags; /* active or obsolete */
93 char data[];
94};
95
96enum flag_scheme {
97 FLAG_NONE,
98 FLAG_BOOLEAN,
99 FLAG_INCREMENTAL,
100};
101
102struct environment {
103 void *image;
104 uint32_t *crc;
105 unsigned char *flags;
106 char *data;
107 enum flag_scheme flag_scheme;
108};
109
110static struct environment environment = {
111 .flag_scheme = FLAG_NONE,
112};
wdenk6aff3112002-12-17 01:51:00 +0000113
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200114static int env_aes_cbc_crypt(char *data, const int enc, uint8_t *key);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100115
wdenkd0fb80c2003-01-11 09:48:40 +0000116static int HaveRedundEnv = 0;
117
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100118static unsigned char active_flag = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200119/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100120static unsigned char obsolete_flag = 0;
wdenkd0fb80c2003-01-11 09:48:40 +0000121
Joe Hershbergerddd84182012-10-12 08:48:51 +0000122#define DEFAULT_ENV_INSTANCE_STATIC
123#include <env_default.h>
wdenk6aff3112002-12-17 01:51:00 +0000124
wdenk4a6fd342003-04-12 23:38:12 +0000125static int flash_io (int mode);
Andreas Fenkart81974f42016-04-05 23:13:42 +0200126static int parse_config(struct env_opts *opts);
wdenk4a6fd342003-04-12 23:38:12 +0000127
wdenkd0fb80c2003-01-11 09:48:40 +0000128#if defined(CONFIG_FILE)
wdenk4a6fd342003-04-12 23:38:12 +0000129static int get_config (char *);
wdenkd0fb80c2003-01-11 09:48:40 +0000130#endif
wdenk6aff3112002-12-17 01:51:00 +0000131
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100132static char *skip_chars(char *s)
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200133{
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100134 for (; *s != '\0'; s++) {
135 if (isblank(*s))
136 return s;
137 }
138 return NULL;
139}
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200140
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100141static char *skip_blanks(char *s)
142{
143 for (; *s != '\0'; s++) {
144 if (!isblank(*s))
Andreas Fenkart9583efc2016-03-11 09:39:36 +0100145 return s;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200146 }
Andreas Fenkart9583efc2016-03-11 09:39:36 +0100147 return NULL;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200148}
149
wdenk6aff3112002-12-17 01:51:00 +0000150/*
Andreas Fenkart1b7427c2016-07-16 17:06:14 +0200151 * s1 is either a simple 'name', or a 'name=value' pair.
152 * s2 is a 'name=value' pair.
153 * If the names match, return the value of s2, else NULL.
154 */
155static char *envmatch(char *s1, char *s2)
156{
157 if (s1 == NULL || s2 == NULL)
158 return NULL;
159
160 while (*s1 == *s2++)
161 if (*s1++ == '=')
162 return s2;
163 if (*s1 == '\0' && *(s2 - 1) == '=')
164 return s2;
165 return NULL;
166}
167
168/**
wdenk6aff3112002-12-17 01:51:00 +0000169 * Search the environment for a variable.
170 * Return the value, if found, or NULL, if not found.
171 */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100172char *fw_getenv (char *name)
wdenk6aff3112002-12-17 01:51:00 +0000173{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100174 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000175
wdenk4a6fd342003-04-12 23:38:12 +0000176 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100177 char *val;
wdenk6aff3112002-12-17 01:51:00 +0000178
wdenk4a6fd342003-04-12 23:38:12 +0000179 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000180 if (nxt >= &environment.data[ENV_SIZE]) {
181 fprintf (stderr, "## Error: "
182 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200183 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000184 }
185 }
wdenk4a6fd342003-04-12 23:38:12 +0000186 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000187 if (!val)
188 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200189 return val;
wdenk6aff3112002-12-17 01:51:00 +0000190 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200191 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000192}
193
194/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600195 * Search the default environment for a variable.
196 * Return the value, if found, or NULL, if not found.
197 */
198char *fw_getdefenv(char *name)
199{
200 char *env, *nxt;
201
202 for (env = default_environment; *env; env = nxt + 1) {
203 char *val;
204
205 for (nxt = env; *nxt; ++nxt) {
206 if (nxt >= &default_environment[ENV_SIZE]) {
207 fprintf(stderr, "## Error: "
208 "default environment not terminated\n");
209 return NULL;
210 }
211 }
212 val = envmatch(name, env);
213 if (!val)
214 continue;
215 return val;
216 }
217 return NULL;
218}
219
Andreas Fenkart371ee132015-12-09 13:13:24 +0100220int parse_aes_key(char *key, uint8_t *bin_key)
Marek Vasuta8a752c2014-03-05 19:59:52 +0100221{
222 char tmp[5] = { '0', 'x', 0, 0, 0 };
223 unsigned long ul;
224 int i;
225
226 if (strnlen(key, 64) != 32) {
227 fprintf(stderr,
228 "## Error: '-a' option requires 16-byte AES key\n");
229 return -1;
230 }
231
232 for (i = 0; i < 16; i++) {
233 tmp[2] = key[0];
234 tmp[3] = key[1];
235 errno = 0;
236 ul = strtoul(tmp, NULL, 16);
237 if (errno) {
238 fprintf(stderr,
239 "## Error: '-a' option requires valid AES key\n");
240 return -1;
241 }
Andreas Fenkart8b27abd2015-12-09 13:13:22 +0100242 bin_key[i] = ul & 0xff;
Marek Vasuta8a752c2014-03-05 19:59:52 +0100243 key += 2;
244 }
Marek Vasuta8a752c2014-03-05 19:59:52 +0100245 return 0;
246}
247
Joe Hershberger267541f2012-12-11 22:16:34 -0600248/*
wdenk6aff3112002-12-17 01:51:00 +0000249 * Print the current definition of one, or more, or all
250 * environment variables
251 */
Andreas Fenkart81974f42016-04-05 23:13:42 +0200252int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +0000253{
Andreas Fenkart371ee132015-12-09 13:13:24 +0100254 int i, rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000255
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200256 if (value_only && argc != 1) {
257 fprintf(stderr,
258 "## Error: `-n' option requires exactly one argument\n");
259 return -1;
260 }
261
Andreas Fenkart14070e62016-05-31 09:21:56 +0200262 if (!opts)
263 opts = &default_opts;
264
Andreas Fenkart81974f42016-04-05 23:13:42 +0200265 if (fw_env_open(opts))
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200266 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000267
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100268 if (argc == 0) { /* Print all env variables */
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200269 char *env, *nxt;
wdenk4a6fd342003-04-12 23:38:12 +0000270 for (env = environment.data; *env; env = nxt + 1) {
271 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000272 if (nxt >= &environment.data[ENV_SIZE]) {
273 fprintf (stderr, "## Error: "
274 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200275 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000276 }
277 }
278
wdenk4a6fd342003-04-12 23:38:12 +0000279 printf ("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000280 }
Stefano Babic33f00862017-04-05 18:08:03 +0200281 fw_env_close(opts);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200282 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000283 }
284
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200285 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100286 char *name = argv[i];
287 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000288
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200289 val = fw_getenv(name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700290 if (!val) {
wdenk4a6fd342003-04-12 23:38:12 +0000291 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700292 rc = -1;
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200293 continue;
Grant Ericksonbc117562008-05-06 20:16:15 -0700294 }
Andreas Fenkartc5c41c42016-07-16 17:06:15 +0200295
296 if (value_only) {
297 puts(val);
298 break;
299 }
300
301 printf("%s=%s\n", name, val);
wdenk6aff3112002-12-17 01:51:00 +0000302 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700303
Stefano Babic33f00862017-04-05 18:08:03 +0200304 fw_env_close(opts);
305
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200306 return rc;
wdenk6aff3112002-12-17 01:51:00 +0000307}
308
Stefano Babic33f00862017-04-05 18:08:03 +0200309int fw_env_flush(struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +0000310{
Marek Vasuta8a752c2014-03-05 19:59:52 +0100311 int ret;
Andreas Fenkart81974f42016-04-05 23:13:42 +0200312
Andreas Fenkart14070e62016-05-31 09:21:56 +0200313 if (!opts)
314 opts = &default_opts;
315
Andreas Fenkart81974f42016-04-05 23:13:42 +0200316 if (opts->aes_flag) {
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200317 ret = env_aes_cbc_crypt(environment.data, 1,
Andreas Fenkart81974f42016-04-05 23:13:42 +0200318 opts->aes_key);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100319 if (ret) {
320 fprintf(stderr,
321 "Error: can't encrypt env for flash\n");
322 return ret;
323 }
324 }
325
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200326 /*
327 * Update CRC
328 */
329 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000330
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200331 /* write environment back to flash */
332 if (flash_io(O_RDWR)) {
333 fprintf(stderr,
334 "Error: can't write fw_env to flash\n");
335 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000336 }
337
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200338 return 0;
339}
wdenk6aff3112002-12-17 01:51:00 +0000340
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200341
342/*
343 * Set/Clear a single variable in the environment.
344 * This is called in sequence to update the environment
345 * in RAM without updating the copy in flash after each set
346 */
347int fw_env_write(char *name, char *value)
348{
349 int len;
350 char *env, *nxt;
351 char *oldval = NULL;
Joe Hershberger267541f2012-12-11 22:16:34 -0600352 int deleting, creating, overwriting;
wdenk6aff3112002-12-17 01:51:00 +0000353
354 /*
355 * search if variable with this name already exists
356 */
wdenk4a6fd342003-04-12 23:38:12 +0000357 for (nxt = env = environment.data; *env; env = nxt + 1) {
358 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000359 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200360 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000361 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200362 errno = EINVAL;
363 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000364 }
365 }
wdenk4a6fd342003-04-12 23:38:12 +0000366 if ((oldval = envmatch (name, env)) != NULL)
wdenk6aff3112002-12-17 01:51:00 +0000367 break;
368 }
369
Joe Hershberger267541f2012-12-11 22:16:34 -0600370 deleting = (oldval && !(value && strlen(value)));
371 creating = (!oldval && (value && strlen(value)));
372 overwriting = (oldval && (value && strlen(value)));
373
374 /* check for permission */
375 if (deleting) {
376 if (env_flags_validate_varaccess(name,
377 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
378 printf("Can't delete \"%s\"\n", name);
379 errno = EROFS;
380 return -1;
381 }
382 } else if (overwriting) {
383 if (env_flags_validate_varaccess(name,
384 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
385 printf("Can't overwrite \"%s\"\n", name);
386 errno = EROFS;
387 return -1;
388 } else if (env_flags_validate_varaccess(name,
389 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
390 const char *defval = fw_getdefenv(name);
391
392 if (defval == NULL)
393 defval = "";
394 if (strcmp(oldval, defval)
395 != 0) {
396 printf("Can't overwrite \"%s\"\n", name);
397 errno = EROFS;
398 return -1;
399 }
400 }
401 } else if (creating) {
402 if (env_flags_validate_varaccess(name,
403 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
404 printf("Can't create \"%s\"\n", name);
405 errno = EROFS;
406 return -1;
407 }
408 } else
409 /* Nothing to do */
410 return 0;
411
412 if (deleting || overwriting) {
wdenk6aff3112002-12-17 01:51:00 +0000413 if (*++nxt == '\0') {
414 *env = '\0';
415 } else {
416 for (;;) {
417 *env = *nxt++;
418 if ((*env == '\0') && (*nxt == '\0'))
419 break;
420 ++env;
421 }
422 }
423 *++env = '\0';
424 }
425
426 /* Delete only ? */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200427 if (!value || !strlen(value))
428 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000429
430 /*
431 * Append new definition at the end
432 */
wdenk4a6fd342003-04-12 23:38:12 +0000433 for (env = environment.data; *env || *(env + 1); ++env);
wdenk6aff3112002-12-17 01:51:00 +0000434 if (env > environment.data)
435 ++env;
436 /*
437 * Overflow when:
Joe Hershberger497f2052012-10-03 09:38:46 +0000438 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk6aff3112002-12-17 01:51:00 +0000439 */
wdenk4a6fd342003-04-12 23:38:12 +0000440 len = strlen (name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000441 /* add '=' for first arg, ' ' for all others */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200442 len += strlen(value) + 1;
443
wdenk4a6fd342003-04-12 23:38:12 +0000444 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk6aff3112002-12-17 01:51:00 +0000445 fprintf (stderr,
446 "Error: environment overflow, \"%s\" deleted\n",
447 name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200448 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000449 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200450
wdenk6aff3112002-12-17 01:51:00 +0000451 while ((*env = *name++) != '\0')
452 env++;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200453 *env = '=';
454 while ((*++env = *value++) != '\0')
455 ;
wdenk6aff3112002-12-17 01:51:00 +0000456
457 /* end is marked with double '\0' */
458 *++env = '\0';
459
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200460 return 0;
461}
wdenk6aff3112002-12-17 01:51:00 +0000462
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200463/*
464 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
465 * 0 - OK
466 * EINVAL - need at least 1 argument
467 * EROFS - certain variables ("ethaddr", "serial#") cannot be
468 * modified or deleted
469 *
470 */
Andreas Fenkart81974f42016-04-05 23:13:42 +0200471int fw_setenv(int argc, char *argv[], struct env_opts *opts)
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200472{
Andreas Fenkart371ee132015-12-09 13:13:24 +0100473 int i;
Mike Frysinger3779c8e2012-11-10 19:47:45 +0000474 size_t len;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100475 char *name, **valv;
xypron.glpk@gmx.deddc6a9d2017-04-15 13:05:40 +0200476 char *oldval;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200477 char *value = NULL;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100478 int valc;
Stefano Babic33f00862017-04-05 18:08:03 +0200479 int ret;
wdenk6aff3112002-12-17 01:51:00 +0000480
Andreas Fenkart14070e62016-05-31 09:21:56 +0200481 if (!opts)
482 opts = &default_opts;
483
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100484 if (argc < 1) {
485 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasuta8a752c2014-03-05 19:59:52 +0100486 errno = EINVAL;
487 return -1;
488 }
489
Andreas Fenkart81974f42016-04-05 23:13:42 +0200490 if (fw_env_open(opts)) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200491 fprintf(stderr, "Error: environment not initialized\n");
492 return -1;
493 }
494
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100495 name = argv[0];
496 valv = argv + 1;
497 valc = argc - 1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200498
Stefano Babic33f00862017-04-05 18:08:03 +0200499 if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
500 fw_env_close(opts);
Andreas Fenkartfd4e3282016-07-16 17:06:13 +0200501 return -1;
Stefano Babic33f00862017-04-05 18:08:03 +0200502 }
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600503
Joe Hershberger62a34a02012-10-03 09:38:47 +0000504 len = 0;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100505 for (i = 0; i < valc; ++i) {
506 char *val = valv[i];
Joe Hershberger62a34a02012-10-03 09:38:47 +0000507 size_t val_len = strlen(val);
508
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000509 if (value)
510 value[len - 1] = ' ';
xypron.glpk@gmx.deddc6a9d2017-04-15 13:05:40 +0200511 oldval = value;
Joe Hershberger62a34a02012-10-03 09:38:47 +0000512 value = realloc(value, len + val_len + 1);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200513 if (!value) {
Joe Hershberger62a34a02012-10-03 09:38:47 +0000514 fprintf(stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200515 "Cannot malloc %zu bytes: %s\n",
Joe Hershberger62a34a02012-10-03 09:38:47 +0000516 len, strerror(errno));
xypron.glpk@gmx.deddc6a9d2017-04-15 13:05:40 +0200517 free(oldval);
Joe Hershberger62a34a02012-10-03 09:38:47 +0000518 return -1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200519 }
Joe Hershberger62a34a02012-10-03 09:38:47 +0000520
521 memcpy(value + len, val, val_len);
522 len += val_len;
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000523 value[len++] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200524 }
525
526 fw_env_write(name, value);
527
Joe Hershberger62a34a02012-10-03 09:38:47 +0000528 free(value);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200529
Stefano Babic33f00862017-04-05 18:08:03 +0200530 ret = fw_env_flush(opts);
531 fw_env_close(opts);
532
533 return ret;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200534}
535
536/*
537 * Parse a file and configure the u-boot variables.
538 * The script file has a very simple format, as follows:
539 *
540 * Each line has a couple with name, value:
541 * <white spaces>variable_name<white spaces>variable_value
542 *
543 * Both variable_name and variable_value are interpreted as strings.
544 * Any character after <white spaces> and before ending \r\n is interpreted
545 * as variable's value (no comment allowed on these lines !)
546 *
547 * Comments are allowed if the first character in the line is #
548 *
549 * Returns -1 and sets errno error codes:
550 * 0 - OK
551 * -1 - Error
552 */
Andreas Fenkart81974f42016-04-05 23:13:42 +0200553int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200554{
555 FILE *fp;
556 char dump[1024]; /* Maximum line length in the file */
557 char *name;
558 char *val;
559 int lineno = 0;
560 int len;
561 int ret = 0;
562
Andreas Fenkart14070e62016-05-31 09:21:56 +0200563 if (!opts)
564 opts = &default_opts;
565
Andreas Fenkart81974f42016-04-05 23:13:42 +0200566 if (fw_env_open(opts)) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200567 fprintf(stderr, "Error: environment not initialized\n");
568 return -1;
569 }
570
571 if (strcmp(fname, "-") == 0)
572 fp = stdin;
573 else {
574 fp = fopen(fname, "r");
575 if (fp == NULL) {
576 fprintf(stderr, "I cannot open %s for reading\n",
577 fname);
578 return -1;
579 }
580 }
581
582 while (fgets(dump, sizeof(dump), fp)) {
583 lineno++;
584 len = strlen(dump);
585
586 /*
587 * Read a whole line from the file. If the line is too long
588 * or is not terminated, reports an error and exit.
589 */
590 if (dump[len - 1] != '\n') {
591 fprintf(stderr,
592 "Line %d not corrected terminated or too long\n",
593 lineno);
594 ret = -1;
595 break;
596 }
597
598 /* Drop ending line feed / carriage return */
Andreas Fenkart69067a32016-03-11 09:39:38 +0100599 dump[--len] = '\0';
600 if (len && dump[len - 1] == '\r')
601 dump[--len] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200602
603 /* Skip comment or empty lines */
Andreas Fenkart69067a32016-03-11 09:39:38 +0100604 if (len == 0 || dump[0] == '#')
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200605 continue;
606
607 /*
608 * Search for variable's name,
609 * remove leading whitespaces
610 */
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100611 name = skip_blanks(dump);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200612 if (!name)
613 continue;
614
615 /* The first white space is the end of variable name */
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100616 val = skip_chars(name);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200617 len = strlen(name);
618 if (val) {
619 *val++ = '\0';
620 if ((val - name) < len)
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100621 val = skip_blanks(val);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200622 else
623 val = NULL;
624 }
625
626#ifdef DEBUG
627 fprintf(stderr, "Setting %s : %s\n",
628 name, val ? val : " removed");
629#endif
630
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600631 if (env_flags_validate_type(name, val) < 0) {
632 ret = -1;
633 break;
634 }
635
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200636 /*
637 * If there is an error setting a variable,
638 * try to save the environment and returns an error
639 */
640 if (fw_env_write(name, val)) {
641 fprintf(stderr,
642 "fw_env_write returns with error : %s\n",
643 strerror(errno));
644 ret = -1;
645 break;
646 }
647
648 }
649
650 /* Close file if not stdin */
651 if (strcmp(fname, "-") != 0)
652 fclose(fp);
653
Stefano Babic33f00862017-04-05 18:08:03 +0200654 ret |= fw_env_flush(opts);
655
656 fw_env_close(opts);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200657
658 return ret;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200659}
660
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200661/**
662 * environment_end() - compute offset of first byte right after environemnt
663 * @dev - index of enviroment buffer
664 * Return:
665 * device offset of first byte right after environemnt
666 */
667off_t environment_end(int dev)
668{
669 /* environment is block aligned */
670 return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
671}
672
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200673/*
674 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
675 * 0 - block is good
676 * > 0 - block is bad
677 * < 0 - failed to test
678 */
Andreas Fenkartc6012bb2016-08-29 23:16:58 +0200679static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200680{
681 if (mtd_type == MTD_NANDFLASH) {
Andreas Fenkartc6012bb2016-08-29 23:16:58 +0200682 int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200683
684 if (badblock < 0) {
685 perror ("Cannot read bad block mark");
686 return badblock;
687 }
688
689 if (badblock) {
690#ifdef DEBUG
Marcin Niestroj81c878d2016-05-06 14:58:29 +0200691 fprintf (stderr, "Bad block at 0x%llx, skipping\n",
Andreas Fenkartc6012bb2016-08-29 23:16:58 +0200692 (unsigned long long)blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200693#endif
694 return badblock;
695 }
696 }
697
698 return 0;
699}
700
701/*
702 * Read data from flash at an offset into a provided buffer. On NAND it skips
703 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
704 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
705 */
706static int flash_read_buf (int dev, int fd, void *buf, size_t count,
Andreas Fenkartff95e572016-08-29 23:16:59 +0200707 off_t offset)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200708{
709 size_t blocklen; /* erase / write length - one block on NAND,
710 0 on NOR */
711 size_t processed = 0; /* progress counter */
712 size_t readlen = count; /* current read length */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200713 off_t block_seek; /* offset inside the current block to the start
714 of the data */
715 loff_t blockstart; /* running start of the current block -
716 MEMGETBADBLOCK needs 64 bits */
717 int rc;
718
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100719 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200720
721 /* Offset inside a block */
722 block_seek = offset - blockstart;
723
Andreas Fenkartff95e572016-08-29 23:16:59 +0200724 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200725 /*
726 * NAND: calculate which blocks we are reading. We have
727 * to read one block at a time to skip bad blocks.
728 */
729 blocklen = DEVESIZE (dev);
730
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200731 /* Limit to one block for the first read */
732 if (readlen > blocklen - block_seek)
733 readlen = blocklen - block_seek;
734 } else {
735 blocklen = 0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200736 }
737
738 /* This only runs once on NOR flash */
739 while (processed < count) {
Andreas Fenkartff95e572016-08-29 23:16:59 +0200740 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200741 if (rc < 0) /* block test failed */
742 return -1;
743
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200744 if (blockstart + block_seek + readlen > environment_end(dev)) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200745 /* End of range is reached */
746 fprintf (stderr,
747 "Too few good blocks within range\n");
748 return -1;
749 }
750
751 if (rc) { /* block is bad */
752 blockstart += blocklen;
753 continue;
754 }
755
756 /*
757 * If a block is bad, we retry in the next block at the same
758 * offset - see common/env_nand.c::writeenv()
759 */
760 lseek (fd, blockstart + block_seek, SEEK_SET);
761
762 rc = read (fd, buf + processed, readlen);
763 if (rc != readlen) {
764 fprintf (stderr, "Read error on %s: %s\n",
765 DEVNAME (dev), strerror (errno));
766 return -1;
767 }
768#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000769 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
Marcin Niestroj81c878d2016-05-06 14:58:29 +0200770 rc, (unsigned long long) blockstart + block_seek,
771 DEVNAME(dev));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200772#endif
773 processed += readlen;
774 readlen = min (blocklen, count - processed);
775 block_seek = 0;
776 blockstart += blocklen;
777 }
778
779 return processed;
780}
781
782/*
Andreas Fenkart24307d62016-08-29 23:17:00 +0200783 * Write count bytes from begin of environment, but stay within
784 * ENVSECTORS(dev) sectors of
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100785 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
786 * erase and write the whole data at once.
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200787 */
Andreas Fenkart24307d62016-08-29 23:17:00 +0200788static int flash_write_buf(int dev, int fd, void *buf, size_t count)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200789{
790 void *data;
791 struct erase_info_user erase;
792 size_t blocklen; /* length of NAND block / NOR erase sector */
793 size_t erase_len; /* whole area that can be erased - may include
794 bad blocks */
795 size_t erasesize; /* erase / write length - one block on NAND,
796 whole area on NOR */
797 size_t processed = 0; /* progress counter */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100798 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200799 bad blocks */
800 off_t erase_offset; /* offset to the first erase block (aligned)
801 below offset */
802 off_t block_seek; /* offset inside the erase block to the start
803 of the data */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200804 loff_t blockstart; /* running start of the current block -
805 MEMGETBADBLOCK needs 64 bits */
806 int rc;
807
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200808 /*
Oliver Metze387efb2013-08-30 00:56:02 +0200809 * For mtd devices only offset and size of the environment do matter
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200810 */
Andreas Fenkartff95e572016-08-29 23:16:59 +0200811 if (DEVTYPE(dev) == MTD_ABSENT) {
Oliver Metze387efb2013-08-30 00:56:02 +0200812 blocklen = count;
Oliver Metze387efb2013-08-30 00:56:02 +0200813 erase_len = blocklen;
Andreas Fenkart24307d62016-08-29 23:17:00 +0200814 blockstart = DEVOFFSET(dev);
Oliver Metze387efb2013-08-30 00:56:02 +0200815 block_seek = 0;
816 write_total = blocklen;
817 } else {
818 blocklen = DEVESIZE(dev);
819
Andreas Fenkart24307d62016-08-29 23:17:00 +0200820 erase_offset = DEVOFFSET(dev);
Oliver Metze387efb2013-08-30 00:56:02 +0200821
822 /* Maximum area we may use */
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200823 erase_len = environment_end(dev) - erase_offset;
Oliver Metze387efb2013-08-30 00:56:02 +0200824
825 blockstart = erase_offset;
Andreas Fenkart24307d62016-08-29 23:17:00 +0200826
Oliver Metze387efb2013-08-30 00:56:02 +0200827 /* Offset inside a block */
Andreas Fenkart24307d62016-08-29 23:17:00 +0200828 block_seek = DEVOFFSET(dev) - erase_offset;
Oliver Metze387efb2013-08-30 00:56:02 +0200829
830 /*
831 * Data size we actually write: from the start of the block
832 * to the start of the data, then count bytes of data, and
833 * to the end of the block
834 */
835 write_total = ((block_seek + count + blocklen - 1) /
836 blocklen) * blocklen;
837 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200838
839 /*
840 * Support data anywhere within erase sectors: read out the complete
841 * area to be erased, replace the environment image, write the whole
842 * block back again.
843 */
844 if (write_total > count) {
845 data = malloc (erase_len);
846 if (!data) {
847 fprintf (stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200848 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200849 erase_len, strerror (errno));
850 return -1;
851 }
852
Andreas Fenkartff95e572016-08-29 23:16:59 +0200853 rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200854 if (write_total != rc)
855 return -1;
856
Joe Hershberger74620e12012-10-15 15:29:25 +0000857#ifdef DEBUG
858 fprintf(stderr, "Preserving data ");
859 if (block_seek != 0)
860 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
861 if (block_seek + count != write_total) {
862 if (block_seek != 0)
863 fprintf(stderr, " and ");
Marcin Niestroj81c878d2016-05-06 14:58:29 +0200864 fprintf(stderr, "0x%lx - 0x%lx",
865 (unsigned long) block_seek + count,
866 (unsigned long) write_total - 1);
Joe Hershberger74620e12012-10-15 15:29:25 +0000867 }
868 fprintf(stderr, "\n");
869#endif
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200870 /* Overwrite the old environment */
871 memcpy (data + block_seek, buf, count);
872 } else {
873 /*
874 * We get here, iff offset is block-aligned and count is a
875 * multiple of blocklen - see write_total calculation above
876 */
877 data = buf;
878 }
879
Andreas Fenkartff95e572016-08-29 23:16:59 +0200880 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200881 /*
882 * NAND: calculate which blocks we are writing. We have
883 * to write one block at a time to skip bad blocks.
884 */
885 erasesize = blocklen;
886 } else {
887 erasesize = erase_len;
888 }
889
890 erase.length = erasesize;
891
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100892 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200893 while (processed < write_total) {
Andreas Fenkartff95e572016-08-29 23:16:59 +0200894 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200895 if (rc < 0) /* block test failed */
896 return rc;
897
Andreas Fenkarte2c93512016-08-29 23:16:57 +0200898 if (blockstart + erasesize > environment_end(dev)) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200899 fprintf (stderr, "End of range reached, aborting\n");
900 return -1;
901 }
902
903 if (rc) { /* block is bad */
904 blockstart += blocklen;
905 continue;
906 }
907
Andreas Fenkartff95e572016-08-29 23:16:59 +0200908 if (DEVTYPE(dev) != MTD_ABSENT) {
Oliver Metze387efb2013-08-30 00:56:02 +0200909 erase.start = blockstart;
910 ioctl(fd, MEMUNLOCK, &erase);
911 /* These do not need an explicit erase cycle */
Andreas Fenkartff95e572016-08-29 23:16:59 +0200912 if (DEVTYPE(dev) != MTD_DATAFLASH)
Oliver Metze387efb2013-08-30 00:56:02 +0200913 if (ioctl(fd, MEMERASE, &erase) != 0) {
914 fprintf(stderr,
915 "MTD erase error on %s: %s\n",
916 DEVNAME(dev), strerror(errno));
917 return -1;
918 }
919 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200920
921 if (lseek (fd, blockstart, SEEK_SET) == -1) {
922 fprintf (stderr,
923 "Seek error on %s: %s\n",
924 DEVNAME (dev), strerror (errno));
925 return -1;
926 }
927
928#ifdef DEBUG
Marcin Niestroj81c878d2016-05-06 14:58:29 +0200929 fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
930 (unsigned long long) erasesize,
931 (unsigned long long) blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200932#endif
933 if (write (fd, data + processed, erasesize) != erasesize) {
934 fprintf (stderr, "Write error on %s: %s\n",
935 DEVNAME (dev), strerror (errno));
936 return -1;
937 }
938
Andreas Fenkartff95e572016-08-29 23:16:59 +0200939 if (DEVTYPE(dev) != MTD_ABSENT)
Oliver Metze387efb2013-08-30 00:56:02 +0200940 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200941
Dustin Byford4b774ff2014-03-06 20:48:23 -0800942 processed += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200943 block_seek = 0;
Dustin Byford4b774ff2014-03-06 20:48:23 -0800944 blockstart += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200945 }
946
947 if (write_total > count)
948 free (data);
949
950 return processed;
951}
952
953/*
954 * Set obsolete flag at offset - NOR flash only
955 */
956static int flash_flag_obsolete (int dev, int fd, off_t offset)
957{
958 int rc;
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200959 struct erase_info_user erase;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200960
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200961 erase.start = DEVOFFSET (dev);
962 erase.length = DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200963 /* This relies on the fact, that obsolete_flag == 0 */
964 rc = lseek (fd, offset, SEEK_SET);
965 if (rc < 0) {
966 fprintf (stderr, "Cannot seek to set the flag on %s \n",
967 DEVNAME (dev));
968 return rc;
969 }
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200970 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200971 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200972 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200973 if (rc < 0)
974 perror ("Could not set obsolete flag");
975
976 return rc;
977}
978
Marek Vasuta8a752c2014-03-05 19:59:52 +0100979/* Encrypt or decrypt the environment before writing or reading it. */
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200980static int env_aes_cbc_crypt(char *payload, const int enc, uint8_t *key)
Marek Vasuta8a752c2014-03-05 19:59:52 +0100981{
982 uint8_t *data = (uint8_t *)payload;
Andreas Fenkartf71cee42016-04-19 22:43:42 +0200983 const int len = usable_envsize;
Marek Vasuta8a752c2014-03-05 19:59:52 +0100984 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
985 uint32_t aes_blocks;
986
987 /* First we expand the key. */
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200988 aes_expand_key(key, key_exp);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100989
990 /* Calculate the number of AES blocks to encrypt. */
991 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
992
993 if (enc)
994 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
995 else
996 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
997
998 return 0;
999}
1000
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001001static int flash_write (int fd_current, int fd_target, int dev_target)
1002{
1003 int rc;
1004
1005 switch (environment.flag_scheme) {
1006 case FLAG_NONE:
1007 break;
1008 case FLAG_INCREMENTAL:
1009 (*environment.flags)++;
1010 break;
1011 case FLAG_BOOLEAN:
1012 *environment.flags = active_flag;
1013 break;
1014 default:
1015 fprintf (stderr, "Unimplemented flash scheme %u \n",
1016 environment.flag_scheme);
1017 return -1;
1018 }
1019
1020#ifdef DEBUG
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001021 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001022 DEVOFFSET (dev_target), DEVNAME (dev_target));
1023#endif
Marek Vasuta8a752c2014-03-05 19:59:52 +01001024
Joe Hershberger497f2052012-10-03 09:38:46 +00001025 rc = flash_write_buf(dev_target, fd_target, environment.image,
Andreas Fenkart24307d62016-08-29 23:17:00 +02001026 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001027 if (rc < 0)
1028 return rc;
1029
1030 if (environment.flag_scheme == FLAG_BOOLEAN) {
1031 /* Have to set obsolete flag */
1032 off_t offset = DEVOFFSET (dev_current) +
1033 offsetof (struct env_image_redundant, flags);
1034#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +00001035 fprintf(stderr,
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001036 "Setting obsolete flag in environment at 0x%llx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001037 DEVOFFSET (dev_current), DEVNAME (dev_current));
1038#endif
1039 flash_flag_obsolete (dev_current, fd_current, offset);
1040 }
1041
1042 return 0;
1043}
1044
1045static int flash_read (int fd)
1046{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001047 int rc;
1048
Joe Hershberger497f2052012-10-03 09:38:46 +00001049 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Andreas Fenkartff95e572016-08-29 23:16:59 +02001050 DEVOFFSET(dev_current));
Marek Vasuta8a752c2014-03-05 19:59:52 +01001051 if (rc != CUR_ENVSIZE)
1052 return -1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001053
Marek Vasuta8a752c2014-03-05 19:59:52 +01001054 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001055}
1056
1057static int flash_io (int mode)
1058{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001059 int fd_current, fd_target, rc, dev_target;
wdenk6aff3112002-12-17 01:51:00 +00001060
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001061 /* dev_current: fd_current, erase_current */
1062 fd_current = open (DEVNAME (dev_current), mode);
1063 if (fd_current < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001064 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001065 "Can't open %s: %s\n",
1066 DEVNAME (dev_current), strerror (errno));
1067 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001068 }
wdenk6aff3112002-12-17 01:51:00 +00001069
1070 if (mode == O_RDWR) {
wdenkd0fb80c2003-01-11 09:48:40 +00001071 if (HaveRedundEnv) {
1072 /* switch to next partition for writing */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001073 dev_target = !dev_current;
1074 /* dev_target: fd_target, erase_target */
1075 fd_target = open (DEVNAME (dev_target), mode);
1076 if (fd_target < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001077 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001078 "Can't open %s: %s\n",
1079 DEVNAME (dev_target),
1080 strerror (errno));
1081 rc = -1;
1082 goto exit;
wdenkd0fb80c2003-01-11 09:48:40 +00001083 }
1084 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001085 dev_target = dev_current;
1086 fd_target = fd_current;
wdenk6aff3112002-12-17 01:51:00 +00001087 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001088
1089 rc = flash_write (fd_current, fd_target, dev_target);
wdenk6aff3112002-12-17 01:51:00 +00001090
wdenkd0fb80c2003-01-11 09:48:40 +00001091 if (HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001092 if (close (fd_target)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001093 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001094 "I/O error on %s: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001095 DEVNAME (dev_target),
wdenk4a6fd342003-04-12 23:38:12 +00001096 strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001097 rc = -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001098 }
wdenk6aff3112002-12-17 01:51:00 +00001099 }
wdenk6aff3112002-12-17 01:51:00 +00001100 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001101 rc = flash_read (fd_current);
wdenk6aff3112002-12-17 01:51:00 +00001102 }
1103
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001104exit:
1105 if (close (fd_current)) {
wdenk6aff3112002-12-17 01:51:00 +00001106 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001107 "I/O error on %s: %s\n",
1108 DEVNAME (dev_current), strerror (errno));
1109 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001110 }
1111
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001112 return rc;
wdenk6aff3112002-12-17 01:51:00 +00001113}
1114
1115/*
wdenk6aff3112002-12-17 01:51:00 +00001116 * Prevent confusion if running from erased flash memory
1117 */
Andreas Fenkart81974f42016-04-05 23:13:42 +02001118int fw_env_open(struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +00001119{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001120 int crc0, crc0_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001121 unsigned char flag0;
Stefano Babic33f00862017-04-05 18:08:03 +02001122 void *addr0 = NULL;
wdenkd0fb80c2003-01-11 09:48:40 +00001123
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001124 int crc1, crc1_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001125 unsigned char flag1;
Stefano Babic33f00862017-04-05 18:08:03 +02001126 void *addr1 = NULL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001127
Marek Vasuta8a752c2014-03-05 19:59:52 +01001128 int ret;
1129
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001130 struct env_image_single *single;
1131 struct env_image_redundant *redundant;
wdenk6aff3112002-12-17 01:51:00 +00001132
Andreas Fenkart14070e62016-05-31 09:21:56 +02001133 if (!opts)
1134 opts = &default_opts;
1135
Andreas Fenkart81974f42016-04-05 23:13:42 +02001136 if (parse_config(opts)) /* should fill envdevices */
Stefano Babic33f00862017-04-05 18:08:03 +02001137 return -EINVAL;
wdenk4a6fd342003-04-12 23:38:12 +00001138
Joe Hershberger497f2052012-10-03 09:38:46 +00001139 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001140 if (addr0 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001141 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001142 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001143 CUR_ENVSIZE);
Stefano Babic33f00862017-04-05 18:08:03 +02001144 ret = -ENOMEM;
1145 goto open_cleanup;
wdenkd0fb80c2003-01-11 09:48:40 +00001146 }
wdenk4a6fd342003-04-12 23:38:12 +00001147
wdenkd0fb80c2003-01-11 09:48:40 +00001148 /* read environment from FLASH to local buffer */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001149 environment.image = addr0;
1150
1151 if (HaveRedundEnv) {
1152 redundant = addr0;
1153 environment.crc = &redundant->crc;
1154 environment.flags = &redundant->flags;
1155 environment.data = redundant->data;
1156 } else {
1157 single = addr0;
1158 environment.crc = &single->crc;
1159 environment.flags = NULL;
1160 environment.data = single->data;
wdenkd0fb80c2003-01-11 09:48:40 +00001161 }
wdenk4a6fd342003-04-12 23:38:12 +00001162
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001163 dev_current = 0;
Stefano Babic33f00862017-04-05 18:08:03 +02001164 if (flash_io(O_RDONLY)) {
1165 ret = -EIO;
1166 goto open_cleanup;
1167 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001168
1169 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001170
Andreas Fenkart81974f42016-04-05 23:13:42 +02001171 if (opts->aes_flag) {
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +02001172 ret = env_aes_cbc_crypt(environment.data, 0,
Andreas Fenkart81974f42016-04-05 23:13:42 +02001173 opts->aes_key);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001174 if (ret)
Stefano Babic33f00862017-04-05 18:08:03 +02001175 goto open_cleanup;
Marek Vasuta8a752c2014-03-05 19:59:52 +01001176 }
1177
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001178 crc0_ok = (crc0 == *environment.crc);
wdenkd0fb80c2003-01-11 09:48:40 +00001179 if (!HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001180 if (!crc0_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001181 fprintf (stderr,
1182 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +01001183 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +00001184 }
wdenkd0fb80c2003-01-11 09:48:40 +00001185 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001186 flag0 = *environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +00001187
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001188 dev_current = 1;
Joe Hershberger497f2052012-10-03 09:38:46 +00001189 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001190 if (addr1 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001191 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001192 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001193 CUR_ENVSIZE);
Stefano Babic33f00862017-04-05 18:08:03 +02001194 ret = -ENOMEM;
1195 goto open_cleanup;
wdenk4a6fd342003-04-12 23:38:12 +00001196 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001197 redundant = addr1;
wdenk4a6fd342003-04-12 23:38:12 +00001198
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001199 /*
1200 * have to set environment.image for flash_read(), careful -
1201 * other pointers in environment still point inside addr0
1202 */
1203 environment.image = addr1;
Stefano Babic33f00862017-04-05 18:08:03 +02001204 if (flash_io(O_RDONLY)) {
1205 ret = -EIO;
1206 goto open_cleanup;
1207 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001208
1209 /* Check flag scheme compatibility */
1210 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1211 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1212 environment.flag_scheme = FLAG_BOOLEAN;
1213 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1214 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1215 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001216 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1217 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1218 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershberger785881f2013-04-08 10:32:52 +00001219 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1220 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1221 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metz23ef62d2013-08-30 00:56:01 +02001222 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1223 DEVTYPE(!dev_current) == MTD_ABSENT) {
1224 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001225 } else {
1226 fprintf (stderr, "Incompatible flash types!\n");
Stefano Babic33f00862017-04-05 18:08:03 +02001227 ret = -EINVAL;
1228 goto open_cleanup;
wdenk6aff3112002-12-17 01:51:00 +00001229 }
wdenk4a6fd342003-04-12 23:38:12 +00001230
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001231 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001232
Andreas Fenkart81974f42016-04-05 23:13:42 +02001233 if (opts->aes_flag) {
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +02001234 ret = env_aes_cbc_crypt(redundant->data, 0,
Andreas Fenkart81974f42016-04-05 23:13:42 +02001235 opts->aes_key);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001236 if (ret)
Stefano Babic33f00862017-04-05 18:08:03 +02001237 goto open_cleanup;
Marek Vasuta8a752c2014-03-05 19:59:52 +01001238 }
1239
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001240 crc1_ok = (crc1 == redundant->crc);
1241 flag1 = redundant->flags;
wdenk4a6fd342003-04-12 23:38:12 +00001242
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001243 if (crc0_ok && !crc1_ok) {
1244 dev_current = 0;
1245 } else if (!crc0_ok && crc1_ok) {
1246 dev_current = 1;
1247 } else if (!crc0_ok && !crc1_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001248 fprintf (stderr,
1249 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001250 memcpy (environment.data, default_environment,
1251 sizeof default_environment);
1252 dev_current = 0;
1253 } else {
1254 switch (environment.flag_scheme) {
1255 case FLAG_BOOLEAN:
1256 if (flag0 == active_flag &&
1257 flag1 == obsolete_flag) {
1258 dev_current = 0;
1259 } else if (flag0 == obsolete_flag &&
1260 flag1 == active_flag) {
1261 dev_current = 1;
1262 } else if (flag0 == flag1) {
1263 dev_current = 0;
1264 } else if (flag0 == 0xFF) {
1265 dev_current = 0;
1266 } else if (flag1 == 0xFF) {
1267 dev_current = 1;
1268 } else {
1269 dev_current = 0;
1270 }
1271 break;
1272 case FLAG_INCREMENTAL:
Jon Povey735eb0f2011-03-11 14:10:56 +09001273 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001274 dev_current = 1;
1275 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey735eb0f2011-03-11 14:10:56 +09001276 flag0 >= flag1)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001277 dev_current = 0;
Jon Povey735eb0f2011-03-11 14:10:56 +09001278 else /* flag1 > flag0 */
1279 dev_current = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001280 break;
1281 default:
1282 fprintf (stderr, "Unknown flag scheme %u \n",
1283 environment.flag_scheme);
1284 return -1;
1285 }
1286 }
1287
1288 /*
1289 * If we are reading, we don't need the flag and the CRC any
1290 * more, if we are writing, we will re-calculate CRC and update
1291 * flags before writing out
1292 */
1293 if (dev_current) {
1294 environment.image = addr1;
1295 environment.crc = &redundant->crc;
1296 environment.flags = &redundant->flags;
1297 environment.data = redundant->data;
1298 free (addr0);
1299 } else {
1300 environment.image = addr0;
1301 /* Other pointers are already set */
wdenk4a6fd342003-04-12 23:38:12 +00001302 free (addr1);
wdenk6aff3112002-12-17 01:51:00 +00001303 }
Joe Hershberger74620e12012-10-15 15:29:25 +00001304#ifdef DEBUG
1305 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1306#endif
wdenk6aff3112002-12-17 01:51:00 +00001307 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001308 return 0;
Stefano Babic33f00862017-04-05 18:08:03 +02001309
1310open_cleanup:
1311 if (addr0)
1312 free(addr0);
1313
1314 if (addr1)
1315 free(addr0);
1316
1317 return ret;
1318}
1319
1320/*
1321 * Simply free allocated buffer with environment
1322 */
1323int fw_env_close(struct env_opts *opts)
1324{
1325 if (environment.image)
1326 free(environment.image);
1327
1328 environment.image = NULL;
1329
1330 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001331}
1332
Stefan Agner14fb5b22016-07-13 17:14:37 -07001333static int check_device_config(int dev)
1334{
1335 struct stat st;
1336 int fd, rc = 0;
1337
1338 fd = open(DEVNAME(dev), O_RDONLY);
1339 if (fd < 0) {
1340 fprintf(stderr,
1341 "Cannot open %s: %s\n",
1342 DEVNAME(dev), strerror(errno));
1343 return -1;
1344 }
1345
1346 rc = fstat(fd, &st);
1347 if (rc < 0) {
1348 fprintf(stderr, "Cannot stat the file %s\n",
1349 DEVNAME(dev));
1350 goto err;
1351 }
1352
1353 if (S_ISCHR(st.st_mode)) {
1354 struct mtd_info_user mtdinfo;
1355 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1356 if (rc < 0) {
1357 fprintf(stderr, "Cannot get MTD information for %s\n",
1358 DEVNAME(dev));
1359 goto err;
1360 }
1361 if (mtdinfo.type != MTD_NORFLASH &&
1362 mtdinfo.type != MTD_NANDFLASH &&
1363 mtdinfo.type != MTD_DATAFLASH &&
1364 mtdinfo.type != MTD_UBIVOLUME) {
1365 fprintf(stderr, "Unsupported flash type %u on %s\n",
1366 mtdinfo.type, DEVNAME(dev));
1367 goto err;
1368 }
1369 DEVTYPE(dev) = mtdinfo.type;
Max Krummenacher333ee162016-11-19 13:58:56 +01001370 if (DEVESIZE(dev) == 0)
1371 /* Assume the erase size is the same as the env-size */
1372 DEVESIZE(dev) = ENVSIZE(dev);
Stefan Agner14fb5b22016-07-13 17:14:37 -07001373 } else {
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001374 uint64_t size;
Stefan Agner14fb5b22016-07-13 17:14:37 -07001375 DEVTYPE(dev) = MTD_ABSENT;
Max Krummenacher333ee162016-11-19 13:58:56 +01001376 if (DEVESIZE(dev) == 0)
1377 /* Assume the erase size to be 512 bytes */
1378 DEVESIZE(dev) = 0x200;
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001379
1380 /*
1381 * Check for negative offsets, treat it as backwards offset
1382 * from the end of the block device
1383 */
1384 if (DEVOFFSET(dev) < 0) {
1385 rc = ioctl(fd, BLKGETSIZE64, &size);
1386 if (rc < 0) {
1387 fprintf(stderr, "Could not get block device size on %s\n",
1388 DEVNAME(dev));
1389 goto err;
1390 }
1391
1392 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1393#ifdef DEBUG
1394 fprintf(stderr, "Calculated device offset 0x%llx on %s\n",
1395 DEVOFFSET(dev), DEVNAME(dev));
1396#endif
1397 }
Stefan Agner14fb5b22016-07-13 17:14:37 -07001398 }
1399
Max Krummenacher333ee162016-11-19 13:58:56 +01001400 if (ENVSECTORS(dev) == 0)
1401 /* Assume enough sectors to cover the environment */
1402 ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev));
1403
1404 if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
1405 fprintf(stderr, "Environment does not start on (erase) block boundary\n");
1406 errno = EINVAL;
1407 return -1;
1408 }
1409
1410 if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
1411 fprintf(stderr, "Environment does not fit into available sectors\n");
1412 errno = EINVAL;
1413 return -1;
1414 }
1415
Stefan Agner14fb5b22016-07-13 17:14:37 -07001416err:
1417 close(fd);
1418 return rc;
1419}
wdenk6aff3112002-12-17 01:51:00 +00001420
Andreas Fenkart81974f42016-04-05 23:13:42 +02001421static int parse_config(struct env_opts *opts)
wdenk6aff3112002-12-17 01:51:00 +00001422{
Stefan Agner14fb5b22016-07-13 17:14:37 -07001423 int rc;
wdenk6aff3112002-12-17 01:51:00 +00001424
Andreas Fenkart14070e62016-05-31 09:21:56 +02001425 if (!opts)
1426 opts = &default_opts;
Anatolij Gustschin925c97c2016-04-29 22:00:11 +02001427
Andreas Fenkart14070e62016-05-31 09:21:56 +02001428#if defined(CONFIG_FILE)
wdenkd0fb80c2003-01-11 09:48:40 +00001429 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart81974f42016-04-05 23:13:42 +02001430 if (get_config(opts->config_file)) {
Andreas Fenkart371ee132015-12-09 13:13:24 +01001431 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart81974f42016-04-05 23:13:42 +02001432 opts->config_file);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001433 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001434 }
wdenkd0fb80c2003-01-11 09:48:40 +00001435#else
Tom Rini229695f2014-03-28 12:03:33 -04001436 DEVNAME (0) = DEVICE1_NAME;
wdenk4a6fd342003-04-12 23:38:12 +00001437 DEVOFFSET (0) = DEVICE1_OFFSET;
1438 ENVSIZE (0) = ENV1_SIZE;
Max Krummenacher333ee162016-11-19 13:58:56 +01001439
1440 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1441 * know DEVTYPE
1442 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001443#ifdef DEVICE1_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001444 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001445#endif
1446#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001447 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001448#endif
1449
wdenk6aff3112002-12-17 01:51:00 +00001450#ifdef HAVE_REDUND
Tom Rini229695f2014-03-28 12:03:33 -04001451 DEVNAME (1) = DEVICE2_NAME;
wdenk4a6fd342003-04-12 23:38:12 +00001452 DEVOFFSET (1) = DEVICE2_OFFSET;
1453 ENVSIZE (1) = ENV2_SIZE;
Max Krummenacher333ee162016-11-19 13:58:56 +01001454
1455 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1456 * know DEVTYPE
1457 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001458#ifdef DEVICE2_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001459 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001460#endif
1461#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001462 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001463#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001464 HaveRedundEnv = 1;
wdenk6aff3112002-12-17 01:51:00 +00001465#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001466#endif
Stefan Agner14fb5b22016-07-13 17:14:37 -07001467 rc = check_device_config(0);
1468 if (rc < 0)
1469 return rc;
wdenk4a6fd342003-04-12 23:38:12 +00001470
Stefan Agner14fb5b22016-07-13 17:14:37 -07001471 if (HaveRedundEnv) {
1472 rc = check_device_config(1);
1473 if (rc < 0)
1474 return rc;
Andreas Fenkartf71cee42016-04-19 22:43:42 +02001475
Stefan Agner14fb5b22016-07-13 17:14:37 -07001476 if (ENVSIZE(0) != ENVSIZE(1)) {
Stefan Agner14fb5b22016-07-13 17:14:37 -07001477 fprintf(stderr,
Andreas Fenkart490365c2016-08-17 23:41:53 +02001478 "Redundant environments have unequal size");
1479 return -1;
Stefan Agner14fb5b22016-07-13 17:14:37 -07001480 }
Andreas Fenkartf71cee42016-04-19 22:43:42 +02001481 }
1482
1483 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
1484 if (HaveRedundEnv)
1485 usable_envsize -= sizeof(char);
1486
Andreas Fenkart81974f42016-04-05 23:13:42 +02001487 if (opts->aes_flag)
Andreas Fenkartf71cee42016-04-19 22:43:42 +02001488 usable_envsize &= ~(AES_KEY_LENGTH - 1);
1489
wdenk6aff3112002-12-17 01:51:00 +00001490 return 0;
1491}
wdenkd0fb80c2003-01-11 09:48:40 +00001492
1493#if defined(CONFIG_FILE)
1494static int get_config (char *fname)
1495{
1496 FILE *fp;
1497 int i = 0;
1498 int rc;
1499 char dump[128];
Tom Rini229695f2014-03-28 12:03:33 -04001500 char *devname;
wdenkd0fb80c2003-01-11 09:48:40 +00001501
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001502 fp = fopen (fname, "r");
1503 if (fp == NULL)
1504 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001505
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001506 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001507 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001508 if (dump[0] == '#')
wdenkd0fb80c2003-01-11 09:48:40 +00001509 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001510
Stefan Agnerf4742ca2016-07-13 17:14:38 -07001511 rc = sscanf(dump, "%ms %lli %lx %lx %lx",
1512 &devname,
1513 &DEVOFFSET(i),
1514 &ENVSIZE(i),
1515 &DEVESIZE(i),
1516 &ENVSECTORS(i));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001517
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001518 if (rc < 3)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001519 continue;
1520
Tom Rini229695f2014-03-28 12:03:33 -04001521 DEVNAME(i) = devname;
1522
Max Krummenacher333ee162016-11-19 13:58:56 +01001523 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1524 * know DEVTYPE
1525 */
wdenkd0fb80c2003-01-11 09:48:40 +00001526
1527 i++;
1528 }
wdenk4a6fd342003-04-12 23:38:12 +00001529 fclose (fp);
1530
wdenkd0fb80c2003-01-11 09:48:40 +00001531 HaveRedundEnv = i - 1;
wdenk4a6fd342003-04-12 23:38:12 +00001532 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +00001533 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001534 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001535 } else
1536 return 0;
1537}
1538#endif