blob: ee17a6901609f5759d936bb858767286880b766f [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>
Joe Hershberger06134212012-10-12 10:23:37 +000017#include <linux/stringify.h>
wdenk6aff3112002-12-17 01:51:00 +000018#include <stdio.h>
19#include <stdlib.h>
20#include <stddef.h>
21#include <string.h>
22#include <sys/types.h>
23#include <sys/ioctl.h>
24#include <sys/stat.h>
25#include <unistd.h>
wdenk6aff3112002-12-17 01:51:00 +000026
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010027#ifdef MTD_OLD
Wolfgang Denk1711f3b2008-09-02 21:17:36 +020028# include <stdint.h>
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010029# include <linux/mtd/mtd.h>
30#else
Wolfgang Denkc83d7ca2008-01-08 22:58:27 +010031# define __user /* nothing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010032# include <mtd/mtd-user.h>
33#endif
34
35#include "fw_env.h"
wdenk6aff3112002-12-17 01:51:00 +000036
Marek Vasuta8a752c2014-03-05 19:59:52 +010037#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
38
Stefano Babicbd7b26f2010-05-24 12:08:16 +020039#define WHITESPACE(c) ((c == '\t') || (c == ' '))
wdenk6aff3112002-12-17 01:51:00 +000040
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020041#define min(x, y) ({ \
42 typeof(x) _min1 = (x); \
43 typeof(y) _min2 = (y); \
44 (void) (&_min1 == &_min2); \
45 _min1 < _min2 ? _min1 : _min2; })
46
47struct envdev_s {
Tom Rini229695f2014-03-28 12:03:33 -040048 const char *devname; /* Device name */
wdenk4a6fd342003-04-12 23:38:12 +000049 ulong devoff; /* Device offset */
50 ulong env_size; /* environment size */
51 ulong erase_size; /* device erase size */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020052 ulong env_sectors; /* number of environment sectors */
53 uint8_t mtd_type; /* type of the MTD device */
54};
wdenk6aff3112002-12-17 01:51:00 +000055
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020056static struct envdev_s envdevices[2] =
57{
58 {
59 .mtd_type = MTD_ABSENT,
60 }, {
61 .mtd_type = MTD_ABSENT,
62 },
63};
64static int dev_current;
wdenk6aff3112002-12-17 01:51:00 +000065
66#define DEVNAME(i) envdevices[(i)].devname
wdenkd0fb80c2003-01-11 09:48:40 +000067#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk6aff3112002-12-17 01:51:00 +000068#define ENVSIZE(i) envdevices[(i)].env_size
69#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020070#define ENVSECTORS(i) envdevices[(i)].env_sectors
71#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk6aff3112002-12-17 01:51:00 +000072
Joe Hershberger497f2052012-10-03 09:38:46 +000073#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk6aff3112002-12-17 01:51:00 +000074
wdenkd0fb80c2003-01-11 09:48:40 +000075#define ENV_SIZE getenvsize()
wdenk6aff3112002-12-17 01:51:00 +000076
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020077struct env_image_single {
78 uint32_t crc; /* CRC32 over data bytes */
79 char data[];
80};
wdenk6aff3112002-12-17 01:51:00 +000081
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020082struct env_image_redundant {
83 uint32_t crc; /* CRC32 over data bytes */
84 unsigned char flags; /* active or obsolete */
85 char data[];
86};
87
88enum flag_scheme {
89 FLAG_NONE,
90 FLAG_BOOLEAN,
91 FLAG_INCREMENTAL,
92};
93
94struct environment {
95 void *image;
96 uint32_t *crc;
97 unsigned char *flags;
98 char *data;
99 enum flag_scheme flag_scheme;
100};
101
102static struct environment environment = {
103 .flag_scheme = FLAG_NONE,
104};
wdenk6aff3112002-12-17 01:51:00 +0000105
Marek Vasuta8a752c2014-03-05 19:59:52 +0100106static int env_aes_cbc_crypt(char *data, const int enc);
107
wdenkd0fb80c2003-01-11 09:48:40 +0000108static int HaveRedundEnv = 0;
109
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100110static unsigned char active_flag = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200111/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100112static unsigned char obsolete_flag = 0;
wdenkd0fb80c2003-01-11 09:48:40 +0000113
Joe Hershbergerddd84182012-10-12 08:48:51 +0000114#define DEFAULT_ENV_INSTANCE_STATIC
115#include <env_default.h>
wdenk6aff3112002-12-17 01:51:00 +0000116
wdenk4a6fd342003-04-12 23:38:12 +0000117static int flash_io (int mode);
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100118static char *envmatch (char * s1, char * s2);
wdenk4a6fd342003-04-12 23:38:12 +0000119static int parse_config (void);
120
wdenkd0fb80c2003-01-11 09:48:40 +0000121#if defined(CONFIG_FILE)
wdenk4a6fd342003-04-12 23:38:12 +0000122static int get_config (char *);
wdenkd0fb80c2003-01-11 09:48:40 +0000123#endif
wdenk4a6fd342003-04-12 23:38:12 +0000124static inline ulong getenvsize (void)
wdenkd0fb80c2003-01-11 09:48:40 +0000125{
Dominic Sacréea195272014-11-05 00:30:18 +0100126 ulong rc = CUR_ENVSIZE - sizeof(uint32_t);
wdenk4a6fd342003-04-12 23:38:12 +0000127
wdenkd0fb80c2003-01-11 09:48:40 +0000128 if (HaveRedundEnv)
wdenk4a6fd342003-04-12 23:38:12 +0000129 rc -= sizeof (char);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100130
Andreas Fenkart371ee132015-12-09 13:13:24 +0100131 if (common_args.aes_flag)
Marek Vasuta8a752c2014-03-05 19:59:52 +0100132 rc &= ~(AES_KEY_LENGTH - 1);
133
wdenkd0fb80c2003-01-11 09:48:40 +0000134 return rc;
135}
wdenk6aff3112002-12-17 01:51:00 +0000136
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200137static char *fw_string_blank(char *s, int noblank)
138{
139 int i;
140 int len = strlen(s);
141
142 for (i = 0; i < len; i++, s++) {
143 if ((noblank && !WHITESPACE(*s)) ||
144 (!noblank && WHITESPACE(*s)))
145 break;
146 }
147 if (i == len)
148 return NULL;
149
150 return s;
151}
152
wdenk6aff3112002-12-17 01:51:00 +0000153/*
154 * Search the environment for a variable.
155 * Return the value, if found, or NULL, if not found.
156 */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100157char *fw_getenv (char *name)
wdenk6aff3112002-12-17 01:51:00 +0000158{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100159 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000160
wdenk4a6fd342003-04-12 23:38:12 +0000161 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100162 char *val;
wdenk6aff3112002-12-17 01:51:00 +0000163
wdenk4a6fd342003-04-12 23:38:12 +0000164 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000165 if (nxt >= &environment.data[ENV_SIZE]) {
166 fprintf (stderr, "## Error: "
167 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200168 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000169 }
170 }
wdenk4a6fd342003-04-12 23:38:12 +0000171 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000172 if (!val)
173 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200174 return val;
wdenk6aff3112002-12-17 01:51:00 +0000175 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200176 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000177}
178
179/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600180 * Search the default environment for a variable.
181 * Return the value, if found, or NULL, if not found.
182 */
183char *fw_getdefenv(char *name)
184{
185 char *env, *nxt;
186
187 for (env = default_environment; *env; env = nxt + 1) {
188 char *val;
189
190 for (nxt = env; *nxt; ++nxt) {
191 if (nxt >= &default_environment[ENV_SIZE]) {
192 fprintf(stderr, "## Error: "
193 "default environment not terminated\n");
194 return NULL;
195 }
196 }
197 val = envmatch(name, env);
198 if (!val)
199 continue;
200 return val;
201 }
202 return NULL;
203}
204
Andreas Fenkart371ee132015-12-09 13:13:24 +0100205int parse_aes_key(char *key, uint8_t *bin_key)
Marek Vasuta8a752c2014-03-05 19:59:52 +0100206{
207 char tmp[5] = { '0', 'x', 0, 0, 0 };
208 unsigned long ul;
209 int i;
210
211 if (strnlen(key, 64) != 32) {
212 fprintf(stderr,
213 "## Error: '-a' option requires 16-byte AES key\n");
214 return -1;
215 }
216
217 for (i = 0; i < 16; i++) {
218 tmp[2] = key[0];
219 tmp[3] = key[1];
220 errno = 0;
221 ul = strtoul(tmp, NULL, 16);
222 if (errno) {
223 fprintf(stderr,
224 "## Error: '-a' option requires valid AES key\n");
225 return -1;
226 }
Andreas Fenkart8b27abd2015-12-09 13:13:22 +0100227 bin_key[i] = ul & 0xff;
Marek Vasuta8a752c2014-03-05 19:59:52 +0100228 key += 2;
229 }
Marek Vasuta8a752c2014-03-05 19:59:52 +0100230 return 0;
231}
232
Joe Hershberger267541f2012-12-11 22:16:34 -0600233/*
wdenk6aff3112002-12-17 01:51:00 +0000234 * Print the current definition of one, or more, or all
235 * environment variables
236 */
Grant Ericksonbc117562008-05-06 20:16:15 -0700237int fw_printenv (int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000238{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100239 char *env, *nxt;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100240 int i, rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000241
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200242 if (fw_env_open())
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200243 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000244
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100245 if (argc == 0) { /* Print all env variables */
wdenk4a6fd342003-04-12 23:38:12 +0000246 for (env = environment.data; *env; env = nxt + 1) {
247 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000248 if (nxt >= &environment.data[ENV_SIZE]) {
249 fprintf (stderr, "## Error: "
250 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200251 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000252 }
253 }
254
wdenk4a6fd342003-04-12 23:38:12 +0000255 printf ("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000256 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200257 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000258 }
259
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100260 if (printenv_args.name_suppress && argc != 1) {
261 fprintf(stderr,
262 "## Error: `-n' option requires exactly one argument\n");
263 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000264 }
265
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100266 for (i = 0; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100267 char *name = argv[i];
268 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000269
wdenk4a6fd342003-04-12 23:38:12 +0000270 for (env = environment.data; *env; env = nxt + 1) {
wdenk6aff3112002-12-17 01:51:00 +0000271
wdenk4a6fd342003-04-12 23:38:12 +0000272 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000273 if (nxt >= &environment.data[ENV_SIZE]) {
274 fprintf (stderr, "## Error: "
275 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200276 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000277 }
278 }
wdenk4a6fd342003-04-12 23:38:12 +0000279 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000280 if (val) {
Andreas Fenkart371ee132015-12-09 13:13:24 +0100281 if (!printenv_args.name_suppress) {
wdenk6aff3112002-12-17 01:51:00 +0000282 fputs (name, stdout);
wdenk4a6fd342003-04-12 23:38:12 +0000283 putc ('=', stdout);
wdenk6aff3112002-12-17 01:51:00 +0000284 }
wdenk4a6fd342003-04-12 23:38:12 +0000285 puts (val);
wdenk6aff3112002-12-17 01:51:00 +0000286 break;
287 }
288 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700289 if (!val) {
wdenk4a6fd342003-04-12 23:38:12 +0000290 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700291 rc = -1;
292 }
wdenk6aff3112002-12-17 01:51:00 +0000293 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700294
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200295 return rc;
wdenk6aff3112002-12-17 01:51:00 +0000296}
297
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200298int fw_env_close(void)
wdenk6aff3112002-12-17 01:51:00 +0000299{
Marek Vasuta8a752c2014-03-05 19:59:52 +0100300 int ret;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100301 if (common_args.aes_flag) {
Marek Vasuta8a752c2014-03-05 19:59:52 +0100302 ret = env_aes_cbc_crypt(environment.data, 1);
303 if (ret) {
304 fprintf(stderr,
305 "Error: can't encrypt env for flash\n");
306 return ret;
307 }
308 }
309
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200310 /*
311 * Update CRC
312 */
313 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000314
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200315 /* write environment back to flash */
316 if (flash_io(O_RDWR)) {
317 fprintf(stderr,
318 "Error: can't write fw_env to flash\n");
319 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000320 }
321
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200322 return 0;
323}
wdenk6aff3112002-12-17 01:51:00 +0000324
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200325
326/*
327 * Set/Clear a single variable in the environment.
328 * This is called in sequence to update the environment
329 * in RAM without updating the copy in flash after each set
330 */
331int fw_env_write(char *name, char *value)
332{
333 int len;
334 char *env, *nxt;
335 char *oldval = NULL;
Joe Hershberger267541f2012-12-11 22:16:34 -0600336 int deleting, creating, overwriting;
wdenk6aff3112002-12-17 01:51:00 +0000337
338 /*
339 * search if variable with this name already exists
340 */
wdenk4a6fd342003-04-12 23:38:12 +0000341 for (nxt = env = environment.data; *env; env = nxt + 1) {
342 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000343 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200344 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000345 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200346 errno = EINVAL;
347 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000348 }
349 }
wdenk4a6fd342003-04-12 23:38:12 +0000350 if ((oldval = envmatch (name, env)) != NULL)
wdenk6aff3112002-12-17 01:51:00 +0000351 break;
352 }
353
Joe Hershberger267541f2012-12-11 22:16:34 -0600354 deleting = (oldval && !(value && strlen(value)));
355 creating = (!oldval && (value && strlen(value)));
356 overwriting = (oldval && (value && strlen(value)));
357
358 /* check for permission */
359 if (deleting) {
360 if (env_flags_validate_varaccess(name,
361 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
362 printf("Can't delete \"%s\"\n", name);
363 errno = EROFS;
364 return -1;
365 }
366 } else if (overwriting) {
367 if (env_flags_validate_varaccess(name,
368 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
369 printf("Can't overwrite \"%s\"\n", name);
370 errno = EROFS;
371 return -1;
372 } else if (env_flags_validate_varaccess(name,
373 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
374 const char *defval = fw_getdefenv(name);
375
376 if (defval == NULL)
377 defval = "";
378 if (strcmp(oldval, defval)
379 != 0) {
380 printf("Can't overwrite \"%s\"\n", name);
381 errno = EROFS;
382 return -1;
383 }
384 }
385 } else if (creating) {
386 if (env_flags_validate_varaccess(name,
387 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
388 printf("Can't create \"%s\"\n", name);
389 errno = EROFS;
390 return -1;
391 }
392 } else
393 /* Nothing to do */
394 return 0;
395
396 if (deleting || overwriting) {
wdenk6aff3112002-12-17 01:51:00 +0000397 if (*++nxt == '\0') {
398 *env = '\0';
399 } else {
400 for (;;) {
401 *env = *nxt++;
402 if ((*env == '\0') && (*nxt == '\0'))
403 break;
404 ++env;
405 }
406 }
407 *++env = '\0';
408 }
409
410 /* Delete only ? */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200411 if (!value || !strlen(value))
412 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000413
414 /*
415 * Append new definition at the end
416 */
wdenk4a6fd342003-04-12 23:38:12 +0000417 for (env = environment.data; *env || *(env + 1); ++env);
wdenk6aff3112002-12-17 01:51:00 +0000418 if (env > environment.data)
419 ++env;
420 /*
421 * Overflow when:
Joe Hershberger497f2052012-10-03 09:38:46 +0000422 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk6aff3112002-12-17 01:51:00 +0000423 */
wdenk4a6fd342003-04-12 23:38:12 +0000424 len = strlen (name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000425 /* add '=' for first arg, ' ' for all others */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200426 len += strlen(value) + 1;
427
wdenk4a6fd342003-04-12 23:38:12 +0000428 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk6aff3112002-12-17 01:51:00 +0000429 fprintf (stderr,
430 "Error: environment overflow, \"%s\" deleted\n",
431 name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200432 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000433 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200434
wdenk6aff3112002-12-17 01:51:00 +0000435 while ((*env = *name++) != '\0')
436 env++;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200437 *env = '=';
438 while ((*++env = *value++) != '\0')
439 ;
wdenk6aff3112002-12-17 01:51:00 +0000440
441 /* end is marked with double '\0' */
442 *++env = '\0';
443
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200444 return 0;
445}
wdenk6aff3112002-12-17 01:51:00 +0000446
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200447/*
448 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
449 * 0 - OK
450 * EINVAL - need at least 1 argument
451 * EROFS - certain variables ("ethaddr", "serial#") cannot be
452 * modified or deleted
453 *
454 */
455int fw_setenv(int argc, char *argv[])
456{
Andreas Fenkart371ee132015-12-09 13:13:24 +0100457 int i;
Mike Frysinger3779c8e2012-11-10 19:47:45 +0000458 size_t len;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100459 char *name, **valv;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200460 char *value = NULL;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100461 int valc;
wdenk6aff3112002-12-17 01:51:00 +0000462
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100463 if (argc < 1) {
464 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasuta8a752c2014-03-05 19:59:52 +0100465 errno = EINVAL;
466 return -1;
467 }
468
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200469 if (fw_env_open()) {
470 fprintf(stderr, "Error: environment not initialized\n");
471 return -1;
472 }
473
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100474 name = argv[0];
475 valv = argv + 1;
476 valc = argc - 1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200477
Andreas Fenkart167f5252015-12-09 13:13:21 +0100478 if (env_flags_validate_env_set_params(name, valv, valc) < 0)
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600479 return 1;
480
Joe Hershberger62a34a02012-10-03 09:38:47 +0000481 len = 0;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100482 for (i = 0; i < valc; ++i) {
483 char *val = valv[i];
Joe Hershberger62a34a02012-10-03 09:38:47 +0000484 size_t val_len = strlen(val);
485
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000486 if (value)
487 value[len - 1] = ' ';
Joe Hershberger62a34a02012-10-03 09:38:47 +0000488 value = realloc(value, len + val_len + 1);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200489 if (!value) {
Joe Hershberger62a34a02012-10-03 09:38:47 +0000490 fprintf(stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200491 "Cannot malloc %zu bytes: %s\n",
Joe Hershberger62a34a02012-10-03 09:38:47 +0000492 len, strerror(errno));
493 return -1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200494 }
Joe Hershberger62a34a02012-10-03 09:38:47 +0000495
496 memcpy(value + len, val, val_len);
497 len += val_len;
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000498 value[len++] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200499 }
500
501 fw_env_write(name, value);
502
Joe Hershberger62a34a02012-10-03 09:38:47 +0000503 free(value);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200504
505 return fw_env_close();
506}
507
508/*
509 * Parse a file and configure the u-boot variables.
510 * The script file has a very simple format, as follows:
511 *
512 * Each line has a couple with name, value:
513 * <white spaces>variable_name<white spaces>variable_value
514 *
515 * Both variable_name and variable_value are interpreted as strings.
516 * Any character after <white spaces> and before ending \r\n is interpreted
517 * as variable's value (no comment allowed on these lines !)
518 *
519 * Comments are allowed if the first character in the line is #
520 *
521 * Returns -1 and sets errno error codes:
522 * 0 - OK
523 * -1 - Error
524 */
525int fw_parse_script(char *fname)
526{
527 FILE *fp;
528 char dump[1024]; /* Maximum line length in the file */
529 char *name;
530 char *val;
531 int lineno = 0;
532 int len;
533 int ret = 0;
534
535 if (fw_env_open()) {
536 fprintf(stderr, "Error: environment not initialized\n");
537 return -1;
538 }
539
540 if (strcmp(fname, "-") == 0)
541 fp = stdin;
542 else {
543 fp = fopen(fname, "r");
544 if (fp == NULL) {
545 fprintf(stderr, "I cannot open %s for reading\n",
546 fname);
547 return -1;
548 }
549 }
550
551 while (fgets(dump, sizeof(dump), fp)) {
552 lineno++;
553 len = strlen(dump);
554
555 /*
556 * Read a whole line from the file. If the line is too long
557 * or is not terminated, reports an error and exit.
558 */
559 if (dump[len - 1] != '\n') {
560 fprintf(stderr,
561 "Line %d not corrected terminated or too long\n",
562 lineno);
563 ret = -1;
564 break;
565 }
566
567 /* Drop ending line feed / carriage return */
568 while (len > 0 && (dump[len - 1] == '\n' ||
569 dump[len - 1] == '\r')) {
570 dump[len - 1] = '\0';
571 len--;
572 }
573
574 /* Skip comment or empty lines */
575 if ((len == 0) || dump[0] == '#')
576 continue;
577
578 /*
579 * Search for variable's name,
580 * remove leading whitespaces
581 */
582 name = fw_string_blank(dump, 1);
583 if (!name)
584 continue;
585
586 /* The first white space is the end of variable name */
587 val = fw_string_blank(name, 0);
588 len = strlen(name);
589 if (val) {
590 *val++ = '\0';
591 if ((val - name) < len)
592 val = fw_string_blank(val, 1);
593 else
594 val = NULL;
595 }
596
597#ifdef DEBUG
598 fprintf(stderr, "Setting %s : %s\n",
599 name, val ? val : " removed");
600#endif
601
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600602 if (env_flags_validate_type(name, val) < 0) {
603 ret = -1;
604 break;
605 }
606
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200607 /*
608 * If there is an error setting a variable,
609 * try to save the environment and returns an error
610 */
611 if (fw_env_write(name, val)) {
612 fprintf(stderr,
613 "fw_env_write returns with error : %s\n",
614 strerror(errno));
615 ret = -1;
616 break;
617 }
618
619 }
620
621 /* Close file if not stdin */
622 if (strcmp(fname, "-") != 0)
623 fclose(fp);
624
625 ret |= fw_env_close();
626
627 return ret;
628
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200629}
630
631/*
632 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
633 * 0 - block is good
634 * > 0 - block is bad
635 * < 0 - failed to test
636 */
637static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
638{
639 if (mtd_type == MTD_NANDFLASH) {
640 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
641
642 if (badblock < 0) {
643 perror ("Cannot read bad block mark");
644 return badblock;
645 }
646
647 if (badblock) {
648#ifdef DEBUG
649 fprintf (stderr, "Bad block at 0x%llx, "
650 "skipping\n", *blockstart);
651#endif
652 return badblock;
653 }
654 }
655
656 return 0;
657}
658
659/*
660 * Read data from flash at an offset into a provided buffer. On NAND it skips
661 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
662 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
663 */
664static int flash_read_buf (int dev, int fd, void *buf, size_t count,
665 off_t offset, uint8_t mtd_type)
666{
667 size_t blocklen; /* erase / write length - one block on NAND,
668 0 on NOR */
669 size_t processed = 0; /* progress counter */
670 size_t readlen = count; /* current read length */
671 off_t top_of_range; /* end of the last block we may use */
672 off_t block_seek; /* offset inside the current block to the start
673 of the data */
674 loff_t blockstart; /* running start of the current block -
675 MEMGETBADBLOCK needs 64 bits */
676 int rc;
677
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100678 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200679
680 /* Offset inside a block */
681 block_seek = offset - blockstart;
682
683 if (mtd_type == MTD_NANDFLASH) {
684 /*
685 * NAND: calculate which blocks we are reading. We have
686 * to read one block at a time to skip bad blocks.
687 */
688 blocklen = DEVESIZE (dev);
689
690 /*
691 * To calculate the top of the range, we have to use the
692 * global DEVOFFSET (dev), which can be different from offset
693 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100694 top_of_range = ((DEVOFFSET(dev) / blocklen) +
695 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200696
697 /* Limit to one block for the first read */
698 if (readlen > blocklen - block_seek)
699 readlen = blocklen - block_seek;
700 } else {
701 blocklen = 0;
702 top_of_range = offset + count;
703 }
704
705 /* This only runs once on NOR flash */
706 while (processed < count) {
707 rc = flash_bad_block (fd, mtd_type, &blockstart);
708 if (rc < 0) /* block test failed */
709 return -1;
710
711 if (blockstart + block_seek + readlen > top_of_range) {
712 /* End of range is reached */
713 fprintf (stderr,
714 "Too few good blocks within range\n");
715 return -1;
716 }
717
718 if (rc) { /* block is bad */
719 blockstart += blocklen;
720 continue;
721 }
722
723 /*
724 * If a block is bad, we retry in the next block at the same
725 * offset - see common/env_nand.c::writeenv()
726 */
727 lseek (fd, blockstart + block_seek, SEEK_SET);
728
729 rc = read (fd, buf + processed, readlen);
730 if (rc != readlen) {
731 fprintf (stderr, "Read error on %s: %s\n",
732 DEVNAME (dev), strerror (errno));
733 return -1;
734 }
735#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000736 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
737 rc, blockstart + block_seek, DEVNAME(dev));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200738#endif
739 processed += readlen;
740 readlen = min (blocklen, count - processed);
741 block_seek = 0;
742 blockstart += blocklen;
743 }
744
745 return processed;
746}
747
748/*
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100749 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
750 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
751 * erase and write the whole data at once.
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200752 */
753static int flash_write_buf (int dev, int fd, void *buf, size_t count,
754 off_t offset, uint8_t mtd_type)
755{
756 void *data;
757 struct erase_info_user erase;
758 size_t blocklen; /* length of NAND block / NOR erase sector */
759 size_t erase_len; /* whole area that can be erased - may include
760 bad blocks */
761 size_t erasesize; /* erase / write length - one block on NAND,
762 whole area on NOR */
763 size_t processed = 0; /* progress counter */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100764 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200765 bad blocks */
766 off_t erase_offset; /* offset to the first erase block (aligned)
767 below offset */
768 off_t block_seek; /* offset inside the erase block to the start
769 of the data */
770 off_t top_of_range; /* end of the last block we may use */
771 loff_t blockstart; /* running start of the current block -
772 MEMGETBADBLOCK needs 64 bits */
773 int rc;
774
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200775 /*
Oliver Metze387efb2013-08-30 00:56:02 +0200776 * For mtd devices only offset and size of the environment do matter
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200777 */
Oliver Metze387efb2013-08-30 00:56:02 +0200778 if (mtd_type == MTD_ABSENT) {
779 blocklen = count;
780 top_of_range = offset + count;
781 erase_len = blocklen;
782 blockstart = offset;
783 block_seek = 0;
784 write_total = blocklen;
785 } else {
786 blocklen = DEVESIZE(dev);
787
788 top_of_range = ((DEVOFFSET(dev) / blocklen) +
789 ENVSECTORS(dev)) * blocklen;
790
791 erase_offset = (offset / blocklen) * blocklen;
792
793 /* Maximum area we may use */
794 erase_len = top_of_range - erase_offset;
795
796 blockstart = erase_offset;
797 /* Offset inside a block */
798 block_seek = offset - erase_offset;
799
800 /*
801 * Data size we actually write: from the start of the block
802 * to the start of the data, then count bytes of data, and
803 * to the end of the block
804 */
805 write_total = ((block_seek + count + blocklen - 1) /
806 blocklen) * blocklen;
807 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200808
809 /*
810 * Support data anywhere within erase sectors: read out the complete
811 * area to be erased, replace the environment image, write the whole
812 * block back again.
813 */
814 if (write_total > count) {
815 data = malloc (erase_len);
816 if (!data) {
817 fprintf (stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200818 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200819 erase_len, strerror (errno));
820 return -1;
821 }
822
823 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
824 mtd_type);
825 if (write_total != rc)
826 return -1;
827
Joe Hershberger74620e12012-10-15 15:29:25 +0000828#ifdef DEBUG
829 fprintf(stderr, "Preserving data ");
830 if (block_seek != 0)
831 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
832 if (block_seek + count != write_total) {
833 if (block_seek != 0)
834 fprintf(stderr, " and ");
835 fprintf(stderr, "0x%lx - 0x%x",
836 block_seek + count, write_total - 1);
837 }
838 fprintf(stderr, "\n");
839#endif
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200840 /* Overwrite the old environment */
841 memcpy (data + block_seek, buf, count);
842 } else {
843 /*
844 * We get here, iff offset is block-aligned and count is a
845 * multiple of blocklen - see write_total calculation above
846 */
847 data = buf;
848 }
849
850 if (mtd_type == MTD_NANDFLASH) {
851 /*
852 * NAND: calculate which blocks we are writing. We have
853 * to write one block at a time to skip bad blocks.
854 */
855 erasesize = blocklen;
856 } else {
857 erasesize = erase_len;
858 }
859
860 erase.length = erasesize;
861
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100862 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200863 while (processed < write_total) {
864 rc = flash_bad_block (fd, mtd_type, &blockstart);
865 if (rc < 0) /* block test failed */
866 return rc;
867
868 if (blockstart + erasesize > top_of_range) {
869 fprintf (stderr, "End of range reached, aborting\n");
870 return -1;
871 }
872
873 if (rc) { /* block is bad */
874 blockstart += blocklen;
875 continue;
876 }
877
Oliver Metze387efb2013-08-30 00:56:02 +0200878 if (mtd_type != MTD_ABSENT) {
879 erase.start = blockstart;
880 ioctl(fd, MEMUNLOCK, &erase);
881 /* These do not need an explicit erase cycle */
882 if (mtd_type != MTD_DATAFLASH)
883 if (ioctl(fd, MEMERASE, &erase) != 0) {
884 fprintf(stderr,
885 "MTD erase error on %s: %s\n",
886 DEVNAME(dev), strerror(errno));
887 return -1;
888 }
889 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200890
891 if (lseek (fd, blockstart, SEEK_SET) == -1) {
892 fprintf (stderr,
893 "Seek error on %s: %s\n",
894 DEVNAME (dev), strerror (errno));
895 return -1;
896 }
897
898#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000899 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
900 blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200901#endif
902 if (write (fd, data + processed, erasesize) != erasesize) {
903 fprintf (stderr, "Write error on %s: %s\n",
904 DEVNAME (dev), strerror (errno));
905 return -1;
906 }
907
Oliver Metze387efb2013-08-30 00:56:02 +0200908 if (mtd_type != MTD_ABSENT)
909 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200910
Dustin Byford4b774ff2014-03-06 20:48:23 -0800911 processed += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200912 block_seek = 0;
Dustin Byford4b774ff2014-03-06 20:48:23 -0800913 blockstart += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200914 }
915
916 if (write_total > count)
917 free (data);
918
919 return processed;
920}
921
922/*
923 * Set obsolete flag at offset - NOR flash only
924 */
925static int flash_flag_obsolete (int dev, int fd, off_t offset)
926{
927 int rc;
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200928 struct erase_info_user erase;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200929
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200930 erase.start = DEVOFFSET (dev);
931 erase.length = DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200932 /* This relies on the fact, that obsolete_flag == 0 */
933 rc = lseek (fd, offset, SEEK_SET);
934 if (rc < 0) {
935 fprintf (stderr, "Cannot seek to set the flag on %s \n",
936 DEVNAME (dev));
937 return rc;
938 }
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200939 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200940 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200941 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200942 if (rc < 0)
943 perror ("Could not set obsolete flag");
944
945 return rc;
946}
947
Marek Vasuta8a752c2014-03-05 19:59:52 +0100948/* Encrypt or decrypt the environment before writing or reading it. */
949static int env_aes_cbc_crypt(char *payload, const int enc)
950{
951 uint8_t *data = (uint8_t *)payload;
952 const int len = getenvsize();
953 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
954 uint32_t aes_blocks;
955
956 /* First we expand the key. */
Andreas Fenkart371ee132015-12-09 13:13:24 +0100957 aes_expand_key(common_args.aes_key, key_exp);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100958
959 /* Calculate the number of AES blocks to encrypt. */
960 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
961
962 if (enc)
963 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
964 else
965 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
966
967 return 0;
968}
969
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200970static int flash_write (int fd_current, int fd_target, int dev_target)
971{
972 int rc;
973
974 switch (environment.flag_scheme) {
975 case FLAG_NONE:
976 break;
977 case FLAG_INCREMENTAL:
978 (*environment.flags)++;
979 break;
980 case FLAG_BOOLEAN:
981 *environment.flags = active_flag;
982 break;
983 default:
984 fprintf (stderr, "Unimplemented flash scheme %u \n",
985 environment.flag_scheme);
986 return -1;
987 }
988
989#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000990 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200991 DEVOFFSET (dev_target), DEVNAME (dev_target));
992#endif
Marek Vasuta8a752c2014-03-05 19:59:52 +0100993
Joe Hershberger497f2052012-10-03 09:38:46 +0000994 rc = flash_write_buf(dev_target, fd_target, environment.image,
995 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200996 DEVTYPE(dev_target));
997 if (rc < 0)
998 return rc;
999
1000 if (environment.flag_scheme == FLAG_BOOLEAN) {
1001 /* Have to set obsolete flag */
1002 off_t offset = DEVOFFSET (dev_current) +
1003 offsetof (struct env_image_redundant, flags);
1004#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +00001005 fprintf(stderr,
1006 "Setting obsolete flag in environment at 0x%lx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001007 DEVOFFSET (dev_current), DEVNAME (dev_current));
1008#endif
1009 flash_flag_obsolete (dev_current, fd_current, offset);
1010 }
1011
1012 return 0;
1013}
1014
1015static int flash_read (int fd)
1016{
1017 struct mtd_info_user mtdinfo;
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001018 struct stat st;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001019 int rc;
1020
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001021 rc = fstat(fd, &st);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001022 if (rc < 0) {
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001023 fprintf(stderr, "Cannot stat the file %s\n",
1024 DEVNAME(dev_current));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001025 return -1;
1026 }
1027
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001028 if (S_ISCHR(st.st_mode)) {
1029 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1030 if (rc < 0) {
1031 fprintf(stderr, "Cannot get MTD information for %s\n",
1032 DEVNAME(dev_current));
1033 return -1;
1034 }
1035 if (mtdinfo.type != MTD_NORFLASH &&
1036 mtdinfo.type != MTD_NANDFLASH &&
Joe Hershberger2b744332013-04-08 10:32:51 +00001037 mtdinfo.type != MTD_DATAFLASH &&
1038 mtdinfo.type != MTD_UBIVOLUME) {
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001039 fprintf (stderr, "Unsupported flash type %u on %s\n",
1040 mtdinfo.type, DEVNAME(dev_current));
1041 return -1;
1042 }
1043 } else {
1044 memset(&mtdinfo, 0, sizeof(mtdinfo));
1045 mtdinfo.type = MTD_ABSENT;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001046 }
1047
1048 DEVTYPE(dev_current) = mtdinfo.type;
1049
Joe Hershberger497f2052012-10-03 09:38:46 +00001050 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001051 DEVOFFSET (dev_current), mtdinfo.type);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001052 if (rc != CUR_ENVSIZE)
1053 return -1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001054
Marek Vasuta8a752c2014-03-05 19:59:52 +01001055 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001056}
1057
1058static int flash_io (int mode)
1059{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001060 int fd_current, fd_target, rc, dev_target;
wdenk6aff3112002-12-17 01:51:00 +00001061
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001062 /* dev_current: fd_current, erase_current */
1063 fd_current = open (DEVNAME (dev_current), mode);
1064 if (fd_current < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001065 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001066 "Can't open %s: %s\n",
1067 DEVNAME (dev_current), strerror (errno));
1068 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001069 }
wdenk6aff3112002-12-17 01:51:00 +00001070
1071 if (mode == O_RDWR) {
wdenkd0fb80c2003-01-11 09:48:40 +00001072 if (HaveRedundEnv) {
1073 /* switch to next partition for writing */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001074 dev_target = !dev_current;
1075 /* dev_target: fd_target, erase_target */
1076 fd_target = open (DEVNAME (dev_target), mode);
1077 if (fd_target < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001078 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001079 "Can't open %s: %s\n",
1080 DEVNAME (dev_target),
1081 strerror (errno));
1082 rc = -1;
1083 goto exit;
wdenkd0fb80c2003-01-11 09:48:40 +00001084 }
1085 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001086 dev_target = dev_current;
1087 fd_target = fd_current;
wdenk6aff3112002-12-17 01:51:00 +00001088 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001089
1090 rc = flash_write (fd_current, fd_target, dev_target);
wdenk6aff3112002-12-17 01:51:00 +00001091
wdenkd0fb80c2003-01-11 09:48:40 +00001092 if (HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001093 if (close (fd_target)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001094 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001095 "I/O error on %s: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001096 DEVNAME (dev_target),
wdenk4a6fd342003-04-12 23:38:12 +00001097 strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001098 rc = -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001099 }
wdenk6aff3112002-12-17 01:51:00 +00001100 }
wdenk6aff3112002-12-17 01:51:00 +00001101 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001102 rc = flash_read (fd_current);
wdenk6aff3112002-12-17 01:51:00 +00001103 }
1104
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001105exit:
1106 if (close (fd_current)) {
wdenk6aff3112002-12-17 01:51:00 +00001107 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001108 "I/O error on %s: %s\n",
1109 DEVNAME (dev_current), strerror (errno));
1110 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001111 }
1112
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001113 return rc;
wdenk6aff3112002-12-17 01:51:00 +00001114}
1115
1116/*
1117 * s1 is either a simple 'name', or a 'name=value' pair.
1118 * s2 is a 'name=value' pair.
1119 * If the names match, return the value of s2, else NULL.
1120 */
1121
Markus Klotzbücher6de66b32007-11-27 10:23:20 +01001122static char *envmatch (char * s1, char * s2)
wdenk6aff3112002-12-17 01:51:00 +00001123{
Joe Hershberger586197d2012-10-03 09:38:50 +00001124 if (s1 == NULL || s2 == NULL)
1125 return NULL;
wdenk6aff3112002-12-17 01:51:00 +00001126
1127 while (*s1 == *s2++)
1128 if (*s1++ == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001129 return s2;
wdenk4a6fd342003-04-12 23:38:12 +00001130 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001131 return s2;
1132 return NULL;
wdenk6aff3112002-12-17 01:51:00 +00001133}
1134
1135/*
1136 * Prevent confusion if running from erased flash memory
1137 */
Stefano Babicbd7b26f2010-05-24 12:08:16 +02001138int fw_env_open(void)
wdenk6aff3112002-12-17 01:51:00 +00001139{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001140 int crc0, crc0_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001141 unsigned char flag0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001142 void *addr0;
wdenkd0fb80c2003-01-11 09:48:40 +00001143
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001144 int crc1, crc1_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001145 unsigned char flag1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001146 void *addr1;
1147
Marek Vasuta8a752c2014-03-05 19:59:52 +01001148 int ret;
1149
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001150 struct env_image_single *single;
1151 struct env_image_redundant *redundant;
wdenk6aff3112002-12-17 01:51:00 +00001152
wdenk4a6fd342003-04-12 23:38:12 +00001153 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001154 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001155
Joe Hershberger497f2052012-10-03 09:38:46 +00001156 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001157 if (addr0 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001158 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001159 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001160 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001161 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001162 }
wdenk4a6fd342003-04-12 23:38:12 +00001163
wdenkd0fb80c2003-01-11 09:48:40 +00001164 /* read environment from FLASH to local buffer */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001165 environment.image = addr0;
1166
1167 if (HaveRedundEnv) {
1168 redundant = addr0;
1169 environment.crc = &redundant->crc;
1170 environment.flags = &redundant->flags;
1171 environment.data = redundant->data;
1172 } else {
1173 single = addr0;
1174 environment.crc = &single->crc;
1175 environment.flags = NULL;
1176 environment.data = single->data;
wdenkd0fb80c2003-01-11 09:48:40 +00001177 }
wdenk4a6fd342003-04-12 23:38:12 +00001178
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001179 dev_current = 0;
1180 if (flash_io (O_RDONLY))
1181 return -1;
1182
1183 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001184
Andreas Fenkart371ee132015-12-09 13:13:24 +01001185 if (common_args.aes_flag) {
Marek Vasuta8a752c2014-03-05 19:59:52 +01001186 ret = env_aes_cbc_crypt(environment.data, 0);
1187 if (ret)
1188 return ret;
1189 }
1190
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001191 crc0_ok = (crc0 == *environment.crc);
wdenkd0fb80c2003-01-11 09:48:40 +00001192 if (!HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001193 if (!crc0_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001194 fprintf (stderr,
1195 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +01001196 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +00001197 }
wdenkd0fb80c2003-01-11 09:48:40 +00001198 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001199 flag0 = *environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +00001200
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001201 dev_current = 1;
Joe Hershberger497f2052012-10-03 09:38:46 +00001202 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001203 if (addr1 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001204 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001205 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001206 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001207 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001208 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001209 redundant = addr1;
wdenk4a6fd342003-04-12 23:38:12 +00001210
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001211 /*
1212 * have to set environment.image for flash_read(), careful -
1213 * other pointers in environment still point inside addr0
1214 */
1215 environment.image = addr1;
1216 if (flash_io (O_RDONLY))
1217 return -1;
1218
1219 /* Check flag scheme compatibility */
1220 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1221 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1222 environment.flag_scheme = FLAG_BOOLEAN;
1223 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1224 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1225 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001226 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1227 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1228 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershberger785881f2013-04-08 10:32:52 +00001229 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1230 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1231 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metz23ef62d2013-08-30 00:56:01 +02001232 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1233 DEVTYPE(!dev_current) == MTD_ABSENT) {
1234 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001235 } else {
1236 fprintf (stderr, "Incompatible flash types!\n");
1237 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001238 }
wdenk4a6fd342003-04-12 23:38:12 +00001239
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001240 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001241
Andreas Fenkart371ee132015-12-09 13:13:24 +01001242 if (common_args.aes_flag) {
Marek Vasuta8a752c2014-03-05 19:59:52 +01001243 ret = env_aes_cbc_crypt(redundant->data, 0);
1244 if (ret)
1245 return ret;
1246 }
1247
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001248 crc1_ok = (crc1 == redundant->crc);
1249 flag1 = redundant->flags;
wdenk4a6fd342003-04-12 23:38:12 +00001250
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001251 if (crc0_ok && !crc1_ok) {
1252 dev_current = 0;
1253 } else if (!crc0_ok && crc1_ok) {
1254 dev_current = 1;
1255 } else if (!crc0_ok && !crc1_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001256 fprintf (stderr,
1257 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001258 memcpy (environment.data, default_environment,
1259 sizeof default_environment);
1260 dev_current = 0;
1261 } else {
1262 switch (environment.flag_scheme) {
1263 case FLAG_BOOLEAN:
1264 if (flag0 == active_flag &&
1265 flag1 == obsolete_flag) {
1266 dev_current = 0;
1267 } else if (flag0 == obsolete_flag &&
1268 flag1 == active_flag) {
1269 dev_current = 1;
1270 } else if (flag0 == flag1) {
1271 dev_current = 0;
1272 } else if (flag0 == 0xFF) {
1273 dev_current = 0;
1274 } else if (flag1 == 0xFF) {
1275 dev_current = 1;
1276 } else {
1277 dev_current = 0;
1278 }
1279 break;
1280 case FLAG_INCREMENTAL:
Jon Povey735eb0f2011-03-11 14:10:56 +09001281 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001282 dev_current = 1;
1283 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey735eb0f2011-03-11 14:10:56 +09001284 flag0 >= flag1)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001285 dev_current = 0;
Jon Povey735eb0f2011-03-11 14:10:56 +09001286 else /* flag1 > flag0 */
1287 dev_current = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001288 break;
1289 default:
1290 fprintf (stderr, "Unknown flag scheme %u \n",
1291 environment.flag_scheme);
1292 return -1;
1293 }
1294 }
1295
1296 /*
1297 * If we are reading, we don't need the flag and the CRC any
1298 * more, if we are writing, we will re-calculate CRC and update
1299 * flags before writing out
1300 */
1301 if (dev_current) {
1302 environment.image = addr1;
1303 environment.crc = &redundant->crc;
1304 environment.flags = &redundant->flags;
1305 environment.data = redundant->data;
1306 free (addr0);
1307 } else {
1308 environment.image = addr0;
1309 /* Other pointers are already set */
wdenk4a6fd342003-04-12 23:38:12 +00001310 free (addr1);
wdenk6aff3112002-12-17 01:51:00 +00001311 }
Joe Hershberger74620e12012-10-15 15:29:25 +00001312#ifdef DEBUG
1313 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1314#endif
wdenk6aff3112002-12-17 01:51:00 +00001315 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001316 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001317}
1318
1319
wdenk4a6fd342003-04-12 23:38:12 +00001320static int parse_config ()
wdenk6aff3112002-12-17 01:51:00 +00001321{
1322 struct stat st;
1323
wdenkd0fb80c2003-01-11 09:48:40 +00001324#if defined(CONFIG_FILE)
1325 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart371ee132015-12-09 13:13:24 +01001326 if (get_config(common_args.config_file)) {
1327 fprintf(stderr, "Cannot parse config file '%s': %m\n",
1328 common_args.config_file);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001329 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001330 }
wdenkd0fb80c2003-01-11 09:48:40 +00001331#else
Tom Rini229695f2014-03-28 12:03:33 -04001332 DEVNAME (0) = DEVICE1_NAME;
wdenk4a6fd342003-04-12 23:38:12 +00001333 DEVOFFSET (0) = DEVICE1_OFFSET;
1334 ENVSIZE (0) = ENV1_SIZE;
Dustin Byford23869bf2014-03-06 20:48:22 -08001335 /* Default values are: erase-size=env-size */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001336 DEVESIZE (0) = ENVSIZE (0);
Dustin Byford23869bf2014-03-06 20:48:22 -08001337 /* #sectors=env-size/erase-size (rounded up) */
1338 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001339#ifdef DEVICE1_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001340 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001341#endif
1342#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001343 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001344#endif
1345
wdenk6aff3112002-12-17 01:51:00 +00001346#ifdef HAVE_REDUND
Tom Rini229695f2014-03-28 12:03:33 -04001347 DEVNAME (1) = DEVICE2_NAME;
wdenk4a6fd342003-04-12 23:38:12 +00001348 DEVOFFSET (1) = DEVICE2_OFFSET;
1349 ENVSIZE (1) = ENV2_SIZE;
Dustin Byford23869bf2014-03-06 20:48:22 -08001350 /* Default values are: erase-size=env-size */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001351 DEVESIZE (1) = ENVSIZE (1);
Dustin Byford23869bf2014-03-06 20:48:22 -08001352 /* #sectors=env-size/erase-size (rounded up) */
1353 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001354#ifdef DEVICE2_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001355 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001356#endif
1357#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001358 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001359#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001360 HaveRedundEnv = 1;
wdenk6aff3112002-12-17 01:51:00 +00001361#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001362#endif
wdenk4a6fd342003-04-12 23:38:12 +00001363 if (stat (DEVNAME (0), &st)) {
1364 fprintf (stderr,
1365 "Cannot access MTD device %s: %s\n",
1366 DEVNAME (0), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001367 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001368 }
wdenk4a6fd342003-04-12 23:38:12 +00001369
1370 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1371 fprintf (stderr,
1372 "Cannot access MTD device %s: %s\n",
Wolfgang Denke2146b62005-09-25 01:20:38 +02001373 DEVNAME (1), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001374 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001375 }
wdenk6aff3112002-12-17 01:51:00 +00001376 return 0;
1377}
wdenkd0fb80c2003-01-11 09:48:40 +00001378
1379#if defined(CONFIG_FILE)
1380static int get_config (char *fname)
1381{
1382 FILE *fp;
1383 int i = 0;
1384 int rc;
1385 char dump[128];
Tom Rini229695f2014-03-28 12:03:33 -04001386 char *devname;
wdenkd0fb80c2003-01-11 09:48:40 +00001387
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001388 fp = fopen (fname, "r");
1389 if (fp == NULL)
1390 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001391
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001392 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001393 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001394 if (dump[0] == '#')
wdenkd0fb80c2003-01-11 09:48:40 +00001395 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001396
Tom Rini229695f2014-03-28 12:03:33 -04001397 rc = sscanf (dump, "%ms %lx %lx %lx %lx",
1398 &devname,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001399 &DEVOFFSET (i),
1400 &ENVSIZE (i),
1401 &DEVESIZE (i),
1402 &ENVSECTORS (i));
1403
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001404 if (rc < 3)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001405 continue;
1406
Tom Rini229695f2014-03-28 12:03:33 -04001407 DEVNAME(i) = devname;
1408
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001409 if (rc < 4)
1410 /* Assume the erase size is the same as the env-size */
1411 DEVESIZE(i) = ENVSIZE(i);
1412
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001413 if (rc < 5)
Dustin Byford23869bf2014-03-06 20:48:22 -08001414 /* Assume enough env sectors to cover the environment */
1415 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
wdenkd0fb80c2003-01-11 09:48:40 +00001416
1417 i++;
1418 }
wdenk4a6fd342003-04-12 23:38:12 +00001419 fclose (fp);
1420
wdenkd0fb80c2003-01-11 09:48:40 +00001421 HaveRedundEnv = i - 1;
wdenk4a6fd342003-04-12 23:38:12 +00001422 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +00001423 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001424 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001425 } else
1426 return 0;
1427}
1428#endif