blob: 1420ac58a35fe8ebfbd726b6bc22e26675ba9a7c [file] [log] [blame]
wdenk34b613e2002-12-17 01:51:00 +00001/*
Detlev Zundel2111d3e2010-07-30 11:22:15 +02002 * (C) Copyright 2000-2010
wdenk34b613e2002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02005 * (C) Copyright 2008
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00009 */
10
Jörg Krause983487f2015-04-22 21:36:22 +020011#define _GNU_SOURCE
12
Peter Robinson656ee042015-12-09 07:15:33 +000013#include <compiler.h>
wdenk34b613e2002-12-17 01:51:00 +000014#include <errno.h>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060015#include <env_flags.h>
wdenk34b613e2002-12-17 01:51:00 +000016#include <fcntl.h>
Joe Hershbergere79a91f2012-10-12 10:23:37 +000017#include <linux/stringify.h>
Andreas Fenkartd0e12cb2016-03-11 09:39:35 +010018#include <ctype.h>
wdenk34b613e2002-12-17 01:51:00 +000019#include <stdio.h>
20#include <stdlib.h>
21#include <stddef.h>
22#include <string.h>
23#include <sys/types.h>
24#include <sys/ioctl.h>
25#include <sys/stat.h>
26#include <unistd.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010027
28#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020029# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010030# include <linux/mtd/mtd.h>
31#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010032# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010033# include <mtd/mtd-user.h>
34#endif
wdenk34b613e2002-12-17 01:51:00 +000035
Markus Klotzbücher94db5572007-11-27 10:23:20 +010036#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000037
Andreas Fenkart7fce4e22016-03-25 14:52:19 +010038struct common_args common_args;
39struct printenv_args printenv_args;
40struct setenv_args setenv_args;
41
Marek Vasut05ed2a52014-03-05 19:59:52 +010042#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
43
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020044#define min(x, y) ({ \
45 typeof(x) _min1 = (x); \
46 typeof(y) _min2 = (y); \
47 (void) (&_min1 == &_min2); \
48 _min1 < _min2 ? _min1 : _min2; })
49
50struct envdev_s {
Tom Rini313e9122014-03-28 12:03:33 -040051 const char *devname; /* Device name */
wdenk3f9ab982003-04-12 23:38:12 +000052 ulong devoff; /* Device offset */
53 ulong env_size; /* environment size */
54 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020055 ulong env_sectors; /* number of environment sectors */
56 uint8_t mtd_type; /* type of the MTD device */
57};
wdenk34b613e2002-12-17 01:51:00 +000058
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020059static struct envdev_s envdevices[2] =
60{
61 {
62 .mtd_type = MTD_ABSENT,
63 }, {
64 .mtd_type = MTD_ABSENT,
65 },
66};
67static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000068
69#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000070#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000071#define ENVSIZE(i) envdevices[(i)].env_size
72#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020073#define ENVSECTORS(i) envdevices[(i)].env_sectors
74#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk34b613e2002-12-17 01:51:00 +000075
Joe Hershberger671adc42012-10-03 09:38:46 +000076#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000077
wdenke7f34c62003-01-11 09:48:40 +000078#define ENV_SIZE getenvsize()
wdenk34b613e2002-12-17 01:51:00 +000079
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020080struct env_image_single {
81 uint32_t crc; /* CRC32 over data bytes */
82 char data[];
83};
wdenk34b613e2002-12-17 01:51:00 +000084
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020085struct env_image_redundant {
86 uint32_t crc; /* CRC32 over data bytes */
87 unsigned char flags; /* active or obsolete */
88 char data[];
89};
90
91enum flag_scheme {
92 FLAG_NONE,
93 FLAG_BOOLEAN,
94 FLAG_INCREMENTAL,
95};
96
97struct environment {
98 void *image;
99 uint32_t *crc;
100 unsigned char *flags;
101 char *data;
102 enum flag_scheme flag_scheme;
103};
104
105static struct environment environment = {
106 .flag_scheme = FLAG_NONE,
107};
wdenk34b613e2002-12-17 01:51:00 +0000108
Marek Vasut05ed2a52014-03-05 19:59:52 +0100109static int env_aes_cbc_crypt(char *data, const int enc);
110
wdenke7f34c62003-01-11 09:48:40 +0000111static int HaveRedundEnv = 0;
112
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100113static unsigned char active_flag = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200114/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100115static unsigned char obsolete_flag = 0;
wdenke7f34c62003-01-11 09:48:40 +0000116
Joe Hershberger81321142012-10-12 08:48:51 +0000117#define DEFAULT_ENV_INSTANCE_STATIC
118#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000119
wdenk3f9ab982003-04-12 23:38:12 +0000120static int flash_io (int mode);
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100121static char *envmatch (char * s1, char * s2);
wdenk3f9ab982003-04-12 23:38:12 +0000122static int parse_config (void);
123
wdenke7f34c62003-01-11 09:48:40 +0000124#if defined(CONFIG_FILE)
wdenk3f9ab982003-04-12 23:38:12 +0000125static int get_config (char *);
wdenke7f34c62003-01-11 09:48:40 +0000126#endif
wdenk3f9ab982003-04-12 23:38:12 +0000127static inline ulong getenvsize (void)
wdenke7f34c62003-01-11 09:48:40 +0000128{
Dominic Sacré6da74662014-11-05 00:30:18 +0100129 ulong rc = CUR_ENVSIZE - sizeof(uint32_t);
wdenk3f9ab982003-04-12 23:38:12 +0000130
wdenke7f34c62003-01-11 09:48:40 +0000131 if (HaveRedundEnv)
wdenk3f9ab982003-04-12 23:38:12 +0000132 rc -= sizeof (char);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100133
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100134 if (common_args.aes_flag)
Marek Vasut05ed2a52014-03-05 19:59:52 +0100135 rc &= ~(AES_KEY_LENGTH - 1);
136
wdenke7f34c62003-01-11 09:48:40 +0000137 return rc;
138}
wdenk34b613e2002-12-17 01:51:00 +0000139
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100140static char *skip_chars(char *s)
Stefano Babicada628b2010-05-24 12:08:16 +0200141{
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100142 for (; *s != '\0'; s++) {
143 if (isblank(*s))
144 return s;
145 }
146 return NULL;
147}
Stefano Babicada628b2010-05-24 12:08:16 +0200148
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100149static char *skip_blanks(char *s)
150{
151 for (; *s != '\0'; s++) {
152 if (!isblank(*s))
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100153 return s;
Stefano Babicada628b2010-05-24 12:08:16 +0200154 }
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100155 return NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200156}
157
wdenk34b613e2002-12-17 01:51:00 +0000158/*
159 * Search the environment for a variable.
160 * Return the value, if found, or NULL, if not found.
161 */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100162char *fw_getenv (char *name)
wdenk34b613e2002-12-17 01:51:00 +0000163{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100164 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000165
wdenk3f9ab982003-04-12 23:38:12 +0000166 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100167 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000168
wdenk3f9ab982003-04-12 23:38:12 +0000169 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000170 if (nxt >= &environment.data[ENV_SIZE]) {
171 fprintf (stderr, "## Error: "
172 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200173 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000174 }
175 }
wdenk3f9ab982003-04-12 23:38:12 +0000176 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000177 if (!val)
178 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200179 return val;
wdenk34b613e2002-12-17 01:51:00 +0000180 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200181 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000182}
183
184/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600185 * Search the default environment for a variable.
186 * Return the value, if found, or NULL, if not found.
187 */
188char *fw_getdefenv(char *name)
189{
190 char *env, *nxt;
191
192 for (env = default_environment; *env; env = nxt + 1) {
193 char *val;
194
195 for (nxt = env; *nxt; ++nxt) {
196 if (nxt >= &default_environment[ENV_SIZE]) {
197 fprintf(stderr, "## Error: "
198 "default environment not terminated\n");
199 return NULL;
200 }
201 }
202 val = envmatch(name, env);
203 if (!val)
204 continue;
205 return val;
206 }
207 return NULL;
208}
209
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100210int parse_aes_key(char *key, uint8_t *bin_key)
Marek Vasut05ed2a52014-03-05 19:59:52 +0100211{
212 char tmp[5] = { '0', 'x', 0, 0, 0 };
213 unsigned long ul;
214 int i;
215
216 if (strnlen(key, 64) != 32) {
217 fprintf(stderr,
218 "## Error: '-a' option requires 16-byte AES key\n");
219 return -1;
220 }
221
222 for (i = 0; i < 16; i++) {
223 tmp[2] = key[0];
224 tmp[3] = key[1];
225 errno = 0;
226 ul = strtoul(tmp, NULL, 16);
227 if (errno) {
228 fprintf(stderr,
229 "## Error: '-a' option requires valid AES key\n");
230 return -1;
231 }
Andreas Fenkarta34e5c52015-12-09 13:13:22 +0100232 bin_key[i] = ul & 0xff;
Marek Vasut05ed2a52014-03-05 19:59:52 +0100233 key += 2;
234 }
Marek Vasut05ed2a52014-03-05 19:59:52 +0100235 return 0;
236}
237
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600238/*
wdenk34b613e2002-12-17 01:51:00 +0000239 * Print the current definition of one, or more, or all
240 * environment variables
241 */
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700242int fw_printenv (int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000243{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100244 char *env, *nxt;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100245 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000246
Stefano Babicada628b2010-05-24 12:08:16 +0200247 if (fw_env_open())
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200248 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000249
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100250 if (argc == 0) { /* Print all env variables */
wdenk3f9ab982003-04-12 23:38:12 +0000251 for (env = environment.data; *env; env = nxt + 1) {
252 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000253 if (nxt >= &environment.data[ENV_SIZE]) {
254 fprintf (stderr, "## Error: "
255 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200256 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000257 }
258 }
259
wdenk3f9ab982003-04-12 23:38:12 +0000260 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000261 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200262 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000263 }
264
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100265 if (printenv_args.name_suppress && argc != 1) {
266 fprintf(stderr,
267 "## Error: `-n' option requires exactly one argument\n");
268 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000269 }
270
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100271 for (i = 0; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100272 char *name = argv[i];
273 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000274
wdenk3f9ab982003-04-12 23:38:12 +0000275 for (env = environment.data; *env; env = nxt + 1) {
wdenk34b613e2002-12-17 01:51:00 +0000276
wdenk3f9ab982003-04-12 23:38:12 +0000277 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000278 if (nxt >= &environment.data[ENV_SIZE]) {
279 fprintf (stderr, "## Error: "
280 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200281 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000282 }
283 }
wdenk3f9ab982003-04-12 23:38:12 +0000284 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000285 if (val) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100286 if (!printenv_args.name_suppress) {
wdenk34b613e2002-12-17 01:51:00 +0000287 fputs (name, stdout);
wdenk3f9ab982003-04-12 23:38:12 +0000288 putc ('=', stdout);
wdenk34b613e2002-12-17 01:51:00 +0000289 }
wdenk3f9ab982003-04-12 23:38:12 +0000290 puts (val);
wdenk34b613e2002-12-17 01:51:00 +0000291 break;
292 }
293 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700294 if (!val) {
wdenk3f9ab982003-04-12 23:38:12 +0000295 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700296 rc = -1;
297 }
wdenk34b613e2002-12-17 01:51:00 +0000298 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700299
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200300 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000301}
302
Stefano Babicada628b2010-05-24 12:08:16 +0200303int fw_env_close(void)
wdenk34b613e2002-12-17 01:51:00 +0000304{
Marek Vasut05ed2a52014-03-05 19:59:52 +0100305 int ret;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100306 if (common_args.aes_flag) {
Marek Vasut05ed2a52014-03-05 19:59:52 +0100307 ret = env_aes_cbc_crypt(environment.data, 1);
308 if (ret) {
309 fprintf(stderr,
310 "Error: can't encrypt env for flash\n");
311 return ret;
312 }
313 }
314
Stefano Babicada628b2010-05-24 12:08:16 +0200315 /*
316 * Update CRC
317 */
318 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000319
Stefano Babicada628b2010-05-24 12:08:16 +0200320 /* write environment back to flash */
321 if (flash_io(O_RDWR)) {
322 fprintf(stderr,
323 "Error: can't write fw_env to flash\n");
324 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000325 }
326
Stefano Babicada628b2010-05-24 12:08:16 +0200327 return 0;
328}
wdenk34b613e2002-12-17 01:51:00 +0000329
Stefano Babicada628b2010-05-24 12:08:16 +0200330
331/*
332 * Set/Clear a single variable in the environment.
333 * This is called in sequence to update the environment
334 * in RAM without updating the copy in flash after each set
335 */
336int fw_env_write(char *name, char *value)
337{
338 int len;
339 char *env, *nxt;
340 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600341 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000342
343 /*
344 * search if variable with this name already exists
345 */
wdenk3f9ab982003-04-12 23:38:12 +0000346 for (nxt = env = environment.data; *env; env = nxt + 1) {
347 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000348 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200349 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000350 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200351 errno = EINVAL;
352 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000353 }
354 }
wdenk3f9ab982003-04-12 23:38:12 +0000355 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000356 break;
357 }
358
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600359 deleting = (oldval && !(value && strlen(value)));
360 creating = (!oldval && (value && strlen(value)));
361 overwriting = (oldval && (value && strlen(value)));
362
363 /* check for permission */
364 if (deleting) {
365 if (env_flags_validate_varaccess(name,
366 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
367 printf("Can't delete \"%s\"\n", name);
368 errno = EROFS;
369 return -1;
370 }
371 } else if (overwriting) {
372 if (env_flags_validate_varaccess(name,
373 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
374 printf("Can't overwrite \"%s\"\n", name);
375 errno = EROFS;
376 return -1;
377 } else if (env_flags_validate_varaccess(name,
378 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
379 const char *defval = fw_getdefenv(name);
380
381 if (defval == NULL)
382 defval = "";
383 if (strcmp(oldval, defval)
384 != 0) {
385 printf("Can't overwrite \"%s\"\n", name);
386 errno = EROFS;
387 return -1;
388 }
389 }
390 } else if (creating) {
391 if (env_flags_validate_varaccess(name,
392 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
393 printf("Can't create \"%s\"\n", name);
394 errno = EROFS;
395 return -1;
396 }
397 } else
398 /* Nothing to do */
399 return 0;
400
401 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000402 if (*++nxt == '\0') {
403 *env = '\0';
404 } else {
405 for (;;) {
406 *env = *nxt++;
407 if ((*env == '\0') && (*nxt == '\0'))
408 break;
409 ++env;
410 }
411 }
412 *++env = '\0';
413 }
414
415 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200416 if (!value || !strlen(value))
417 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000418
419 /*
420 * Append new definition at the end
421 */
wdenk3f9ab982003-04-12 23:38:12 +0000422 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000423 if (env > environment.data)
424 ++env;
425 /*
426 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000427 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000428 */
wdenk3f9ab982003-04-12 23:38:12 +0000429 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000430 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200431 len += strlen(value) + 1;
432
wdenk3f9ab982003-04-12 23:38:12 +0000433 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000434 fprintf (stderr,
435 "Error: environment overflow, \"%s\" deleted\n",
436 name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200437 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000438 }
Stefano Babicada628b2010-05-24 12:08:16 +0200439
wdenk34b613e2002-12-17 01:51:00 +0000440 while ((*env = *name++) != '\0')
441 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200442 *env = '=';
443 while ((*++env = *value++) != '\0')
444 ;
445
446 /* end is marked with double '\0' */
447 *++env = '\0';
448
449 return 0;
450}
451
452/*
453 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
454 * 0 - OK
455 * EINVAL - need at least 1 argument
456 * EROFS - certain variables ("ethaddr", "serial#") cannot be
457 * modified or deleted
458 *
459 */
460int fw_setenv(int argc, char *argv[])
461{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100462 int i;
Mike Frysinger178ec772012-11-10 19:47:45 +0000463 size_t len;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100464 char *name, **valv;
Stefano Babicada628b2010-05-24 12:08:16 +0200465 char *value = NULL;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100466 int valc;
Stefano Babicada628b2010-05-24 12:08:16 +0200467
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100468 if (argc < 1) {
469 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasut05ed2a52014-03-05 19:59:52 +0100470 errno = EINVAL;
471 return -1;
472 }
473
Stefano Babicada628b2010-05-24 12:08:16 +0200474 if (fw_env_open()) {
475 fprintf(stderr, "Error: environment not initialized\n");
476 return -1;
477 }
478
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100479 name = argv[0];
480 valv = argv + 1;
481 valc = argc - 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200482
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100483 if (env_flags_validate_env_set_params(name, valv, valc) < 0)
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600484 return 1;
485
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000486 len = 0;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100487 for (i = 0; i < valc; ++i) {
488 char *val = valv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000489 size_t val_len = strlen(val);
490
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000491 if (value)
492 value[len - 1] = ' ';
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000493 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200494 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000495 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200496 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000497 len, strerror(errno));
498 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200499 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000500
501 memcpy(value + len, val, val_len);
502 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000503 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000504 }
505
Stefano Babicada628b2010-05-24 12:08:16 +0200506 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000507
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000508 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000509
Stefano Babicada628b2010-05-24 12:08:16 +0200510 return fw_env_close();
511}
wdenk34b613e2002-12-17 01:51:00 +0000512
Stefano Babicada628b2010-05-24 12:08:16 +0200513/*
514 * Parse a file and configure the u-boot variables.
515 * The script file has a very simple format, as follows:
516 *
517 * Each line has a couple with name, value:
518 * <white spaces>variable_name<white spaces>variable_value
519 *
520 * Both variable_name and variable_value are interpreted as strings.
521 * Any character after <white spaces> and before ending \r\n is interpreted
522 * as variable's value (no comment allowed on these lines !)
523 *
524 * Comments are allowed if the first character in the line is #
525 *
526 * Returns -1 and sets errno error codes:
527 * 0 - OK
528 * -1 - Error
529 */
530int fw_parse_script(char *fname)
531{
532 FILE *fp;
533 char dump[1024]; /* Maximum line length in the file */
534 char *name;
535 char *val;
536 int lineno = 0;
537 int len;
538 int ret = 0;
539
540 if (fw_env_open()) {
541 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200542 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000543 }
544
Stefano Babicada628b2010-05-24 12:08:16 +0200545 if (strcmp(fname, "-") == 0)
546 fp = stdin;
547 else {
548 fp = fopen(fname, "r");
549 if (fp == NULL) {
550 fprintf(stderr, "I cannot open %s for reading\n",
551 fname);
552 return -1;
553 }
554 }
555
556 while (fgets(dump, sizeof(dump), fp)) {
557 lineno++;
558 len = strlen(dump);
559
560 /*
561 * Read a whole line from the file. If the line is too long
562 * or is not terminated, reports an error and exit.
563 */
564 if (dump[len - 1] != '\n') {
565 fprintf(stderr,
566 "Line %d not corrected terminated or too long\n",
567 lineno);
568 ret = -1;
569 break;
570 }
571
572 /* Drop ending line feed / carriage return */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100573 dump[--len] = '\0';
574 if (len && dump[len - 1] == '\r')
575 dump[--len] = '\0';
Stefano Babicada628b2010-05-24 12:08:16 +0200576
577 /* Skip comment or empty lines */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100578 if (len == 0 || dump[0] == '#')
Stefano Babicada628b2010-05-24 12:08:16 +0200579 continue;
580
581 /*
582 * Search for variable's name,
583 * remove leading whitespaces
584 */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100585 name = skip_blanks(dump);
Stefano Babicada628b2010-05-24 12:08:16 +0200586 if (!name)
587 continue;
588
589 /* The first white space is the end of variable name */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100590 val = skip_chars(name);
Stefano Babicada628b2010-05-24 12:08:16 +0200591 len = strlen(name);
592 if (val) {
593 *val++ = '\0';
594 if ((val - name) < len)
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100595 val = skip_blanks(val);
Stefano Babicada628b2010-05-24 12:08:16 +0200596 else
597 val = NULL;
598 }
599
600#ifdef DEBUG
601 fprintf(stderr, "Setting %s : %s\n",
602 name, val ? val : " removed");
603#endif
604
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600605 if (env_flags_validate_type(name, val) < 0) {
606 ret = -1;
607 break;
608 }
609
Stefano Babicada628b2010-05-24 12:08:16 +0200610 /*
611 * If there is an error setting a variable,
612 * try to save the environment and returns an error
613 */
614 if (fw_env_write(name, val)) {
615 fprintf(stderr,
616 "fw_env_write returns with error : %s\n",
617 strerror(errno));
618 ret = -1;
619 break;
620 }
621
622 }
623
624 /* Close file if not stdin */
625 if (strcmp(fname, "-") != 0)
626 fclose(fp);
627
628 ret |= fw_env_close();
629
630 return ret;
631
wdenk34b613e2002-12-17 01:51:00 +0000632}
633
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200634/*
635 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
636 * 0 - block is good
637 * > 0 - block is bad
638 * < 0 - failed to test
639 */
640static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000641{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200642 if (mtd_type == MTD_NANDFLASH) {
643 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000644
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200645 if (badblock < 0) {
646 perror ("Cannot read bad block mark");
647 return badblock;
648 }
649
650 if (badblock) {
651#ifdef DEBUG
652 fprintf (stderr, "Bad block at 0x%llx, "
653 "skipping\n", *blockstart);
654#endif
655 return badblock;
656 }
wdenk34b613e2002-12-17 01:51:00 +0000657 }
658
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200659 return 0;
660}
661
662/*
663 * Read data from flash at an offset into a provided buffer. On NAND it skips
664 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
665 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
666 */
667static int flash_read_buf (int dev, int fd, void *buf, size_t count,
668 off_t offset, uint8_t mtd_type)
669{
670 size_t blocklen; /* erase / write length - one block on NAND,
671 0 on NOR */
672 size_t processed = 0; /* progress counter */
673 size_t readlen = count; /* current read length */
674 off_t top_of_range; /* end of the last block we may use */
675 off_t block_seek; /* offset inside the current block to the start
676 of the data */
677 loff_t blockstart; /* running start of the current block -
678 MEMGETBADBLOCK needs 64 bits */
679 int rc;
680
Remy Bohmercb969eb2011-02-12 19:06:26 +0100681 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200682
683 /* Offset inside a block */
684 block_seek = offset - blockstart;
685
686 if (mtd_type == MTD_NANDFLASH) {
687 /*
688 * NAND: calculate which blocks we are reading. We have
689 * to read one block at a time to skip bad blocks.
690 */
691 blocklen = DEVESIZE (dev);
692
693 /*
694 * To calculate the top of the range, we have to use the
695 * global DEVOFFSET (dev), which can be different from offset
696 */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100697 top_of_range = ((DEVOFFSET(dev) / blocklen) +
698 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200699
700 /* Limit to one block for the first read */
701 if (readlen > blocklen - block_seek)
702 readlen = blocklen - block_seek;
703 } else {
704 blocklen = 0;
705 top_of_range = offset + count;
wdenke7f34c62003-01-11 09:48:40 +0000706 }
wdenk34b613e2002-12-17 01:51:00 +0000707
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200708 /* This only runs once on NOR flash */
709 while (processed < count) {
710 rc = flash_bad_block (fd, mtd_type, &blockstart);
711 if (rc < 0) /* block test failed */
712 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000713
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200714 if (blockstart + block_seek + readlen > top_of_range) {
715 /* End of range is reached */
716 fprintf (stderr,
717 "Too few good blocks within range\n");
718 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000719 }
720
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200721 if (rc) { /* block is bad */
722 blockstart += blocklen;
723 continue;
wdenk34b613e2002-12-17 01:51:00 +0000724 }
725
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200726 /*
727 * If a block is bad, we retry in the next block at the same
728 * offset - see common/env_nand.c::writeenv()
729 */
730 lseek (fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000731
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200732 rc = read (fd, buf + processed, readlen);
733 if (rc != readlen) {
734 fprintf (stderr, "Read error on %s: %s\n",
735 DEVNAME (dev), strerror (errno));
736 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000737 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200738#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000739 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
740 rc, blockstart + block_seek, DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200741#endif
742 processed += readlen;
743 readlen = min (blocklen, count - processed);
744 block_seek = 0;
745 blockstart += blocklen;
746 }
747
748 return processed;
749}
750
751/*
Remy Bohmercb969eb2011-02-12 19:06:26 +0100752 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
753 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
754 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200755 */
756static int flash_write_buf (int dev, int fd, void *buf, size_t count,
757 off_t offset, uint8_t mtd_type)
758{
759 void *data;
760 struct erase_info_user erase;
761 size_t blocklen; /* length of NAND block / NOR erase sector */
762 size_t erase_len; /* whole area that can be erased - may include
763 bad blocks */
764 size_t erasesize; /* erase / write length - one block on NAND,
765 whole area on NOR */
766 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100767 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200768 bad blocks */
769 off_t erase_offset; /* offset to the first erase block (aligned)
770 below offset */
771 off_t block_seek; /* offset inside the erase block to the start
772 of the data */
773 off_t top_of_range; /* end of the last block we may use */
774 loff_t blockstart; /* running start of the current block -
775 MEMGETBADBLOCK needs 64 bits */
776 int rc;
777
Oliver Metze9c66ab2013-08-30 00:56:02 +0200778 /*
779 * For mtd devices only offset and size of the environment do matter
780 */
781 if (mtd_type == MTD_ABSENT) {
782 blocklen = count;
783 top_of_range = offset + count;
784 erase_len = blocklen;
785 blockstart = offset;
786 block_seek = 0;
787 write_total = blocklen;
788 } else {
789 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200790
Oliver Metze9c66ab2013-08-30 00:56:02 +0200791 top_of_range = ((DEVOFFSET(dev) / blocklen) +
792 ENVSECTORS(dev)) * blocklen;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200793
Oliver Metze9c66ab2013-08-30 00:56:02 +0200794 erase_offset = (offset / blocklen) * blocklen;
wdenk34b613e2002-12-17 01:51:00 +0000795
Oliver Metze9c66ab2013-08-30 00:56:02 +0200796 /* Maximum area we may use */
797 erase_len = top_of_range - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +0000798
Oliver Metze9c66ab2013-08-30 00:56:02 +0200799 blockstart = erase_offset;
800 /* Offset inside a block */
801 block_seek = offset - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200802
Oliver Metze9c66ab2013-08-30 00:56:02 +0200803 /*
804 * Data size we actually write: from the start of the block
805 * to the start of the data, then count bytes of data, and
806 * to the end of the block
807 */
808 write_total = ((block_seek + count + blocklen - 1) /
809 blocklen) * blocklen;
810 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200811
812 /*
813 * Support data anywhere within erase sectors: read out the complete
814 * area to be erased, replace the environment image, write the whole
815 * block back again.
816 */
817 if (write_total > count) {
818 data = malloc (erase_len);
819 if (!data) {
wdenke7f34c62003-01-11 09:48:40 +0000820 fprintf (stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200821 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200822 erase_len, strerror (errno));
823 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000824 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200825
826 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
827 mtd_type);
828 if (write_total != rc)
829 return -1;
830
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000831#ifdef DEBUG
832 fprintf(stderr, "Preserving data ");
833 if (block_seek != 0)
834 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
835 if (block_seek + count != write_total) {
836 if (block_seek != 0)
837 fprintf(stderr, " and ");
838 fprintf(stderr, "0x%lx - 0x%x",
839 block_seek + count, write_total - 1);
840 }
841 fprintf(stderr, "\n");
842#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200843 /* Overwrite the old environment */
844 memcpy (data + block_seek, buf, count);
845 } else {
846 /*
847 * We get here, iff offset is block-aligned and count is a
848 * multiple of blocklen - see write_total calculation above
849 */
850 data = buf;
851 }
852
853 if (mtd_type == MTD_NANDFLASH) {
854 /*
855 * NAND: calculate which blocks we are writing. We have
856 * to write one block at a time to skip bad blocks.
857 */
858 erasesize = blocklen;
859 } else {
860 erasesize = erase_len;
861 }
862
863 erase.length = erasesize;
864
Remy Bohmercb969eb2011-02-12 19:06:26 +0100865 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200866 while (processed < write_total) {
867 rc = flash_bad_block (fd, mtd_type, &blockstart);
868 if (rc < 0) /* block test failed */
869 return rc;
870
871 if (blockstart + erasesize > top_of_range) {
872 fprintf (stderr, "End of range reached, aborting\n");
873 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000874 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200875
876 if (rc) { /* block is bad */
877 blockstart += blocklen;
878 continue;
879 }
880
Oliver Metze9c66ab2013-08-30 00:56:02 +0200881 if (mtd_type != MTD_ABSENT) {
882 erase.start = blockstart;
883 ioctl(fd, MEMUNLOCK, &erase);
884 /* These do not need an explicit erase cycle */
885 if (mtd_type != MTD_DATAFLASH)
886 if (ioctl(fd, MEMERASE, &erase) != 0) {
887 fprintf(stderr,
888 "MTD erase error on %s: %s\n",
889 DEVNAME(dev), strerror(errno));
890 return -1;
891 }
892 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200893
894 if (lseek (fd, blockstart, SEEK_SET) == -1) {
wdenk34b613e2002-12-17 01:51:00 +0000895 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200896 "Seek error on %s: %s\n",
897 DEVNAME (dev), strerror (errno));
898 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000899 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200900
901#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000902 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
903 blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200904#endif
905 if (write (fd, data + processed, erasesize) != erasesize) {
906 fprintf (stderr, "Write error on %s: %s\n",
907 DEVNAME (dev), strerror (errno));
908 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000909 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200910
Oliver Metze9c66ab2013-08-30 00:56:02 +0200911 if (mtd_type != MTD_ABSENT)
912 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200913
Dustin Byford0983cad2014-03-06 20:48:23 -0800914 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200915 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -0800916 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200917 }
918
919 if (write_total > count)
920 free (data);
921
922 return processed;
923}
924
925/*
926 * Set obsolete flag at offset - NOR flash only
927 */
928static int flash_flag_obsolete (int dev, int fd, off_t offset)
929{
930 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200931 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200932
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200933 erase.start = DEVOFFSET (dev);
934 erase.length = DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200935 /* This relies on the fact, that obsolete_flag == 0 */
936 rc = lseek (fd, offset, SEEK_SET);
937 if (rc < 0) {
938 fprintf (stderr, "Cannot seek to set the flag on %s \n",
939 DEVNAME (dev));
940 return rc;
941 }
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200942 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200943 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200944 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200945 if (rc < 0)
946 perror ("Could not set obsolete flag");
947
948 return rc;
949}
950
Marek Vasut05ed2a52014-03-05 19:59:52 +0100951/* Encrypt or decrypt the environment before writing or reading it. */
952static int env_aes_cbc_crypt(char *payload, const int enc)
953{
954 uint8_t *data = (uint8_t *)payload;
955 const int len = getenvsize();
956 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
957 uint32_t aes_blocks;
958
959 /* First we expand the key. */
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100960 aes_expand_key(common_args.aes_key, key_exp);
Marek Vasut05ed2a52014-03-05 19:59:52 +0100961
962 /* Calculate the number of AES blocks to encrypt. */
963 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
964
965 if (enc)
966 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
967 else
968 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
969
970 return 0;
971}
972
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200973static int flash_write (int fd_current, int fd_target, int dev_target)
974{
975 int rc;
976
977 switch (environment.flag_scheme) {
978 case FLAG_NONE:
979 break;
980 case FLAG_INCREMENTAL:
981 (*environment.flags)++;
982 break;
983 case FLAG_BOOLEAN:
984 *environment.flags = active_flag;
985 break;
986 default:
987 fprintf (stderr, "Unimplemented flash scheme %u \n",
988 environment.flag_scheme);
989 return -1;
990 }
991
992#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000993 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200994 DEVOFFSET (dev_target), DEVNAME (dev_target));
995#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +0100996
Joe Hershberger671adc42012-10-03 09:38:46 +0000997 rc = flash_write_buf(dev_target, fd_target, environment.image,
998 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200999 DEVTYPE(dev_target));
1000 if (rc < 0)
1001 return rc;
1002
1003 if (environment.flag_scheme == FLAG_BOOLEAN) {
1004 /* Have to set obsolete flag */
1005 off_t offset = DEVOFFSET (dev_current) +
1006 offsetof (struct env_image_redundant, flags);
1007#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001008 fprintf(stderr,
1009 "Setting obsolete flag in environment at 0x%lx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001010 DEVOFFSET (dev_current), DEVNAME (dev_current));
1011#endif
1012 flash_flag_obsolete (dev_current, fd_current, offset);
1013 }
1014
1015 return 0;
1016}
1017
1018static int flash_read (int fd)
1019{
1020 struct mtd_info_user mtdinfo;
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001021 struct stat st;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001022 int rc;
1023
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001024 rc = fstat(fd, &st);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001025 if (rc < 0) {
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001026 fprintf(stderr, "Cannot stat the file %s\n",
1027 DEVNAME(dev_current));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001028 return -1;
1029 }
1030
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001031 if (S_ISCHR(st.st_mode)) {
1032 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1033 if (rc < 0) {
1034 fprintf(stderr, "Cannot get MTD information for %s\n",
1035 DEVNAME(dev_current));
1036 return -1;
1037 }
1038 if (mtdinfo.type != MTD_NORFLASH &&
1039 mtdinfo.type != MTD_NANDFLASH &&
Joe Hershberger0c5faa82013-04-08 10:32:51 +00001040 mtdinfo.type != MTD_DATAFLASH &&
1041 mtdinfo.type != MTD_UBIVOLUME) {
Lubomir Rintel57cb6a32013-02-10 01:02:36 +00001042 fprintf (stderr, "Unsupported flash type %u on %s\n",
1043 mtdinfo.type, DEVNAME(dev_current));
1044 return -1;
1045 }
1046 } else {
1047 memset(&mtdinfo, 0, sizeof(mtdinfo));
1048 mtdinfo.type = MTD_ABSENT;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001049 }
1050
1051 DEVTYPE(dev_current) = mtdinfo.type;
1052
Joe Hershberger671adc42012-10-03 09:38:46 +00001053 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001054 DEVOFFSET (dev_current), mtdinfo.type);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001055 if (rc != CUR_ENVSIZE)
1056 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001057
Marek Vasut05ed2a52014-03-05 19:59:52 +01001058 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001059}
1060
1061static int flash_io (int mode)
1062{
1063 int fd_current, fd_target, rc, dev_target;
1064
1065 /* dev_current: fd_current, erase_current */
1066 fd_current = open (DEVNAME (dev_current), mode);
1067 if (fd_current < 0) {
1068 fprintf (stderr,
1069 "Can't open %s: %s\n",
1070 DEVNAME (dev_current), strerror (errno));
1071 return -1;
1072 }
1073
1074 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +00001075 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001076 /* switch to next partition for writing */
1077 dev_target = !dev_current;
1078 /* dev_target: fd_target, erase_target */
1079 fd_target = open (DEVNAME (dev_target), mode);
1080 if (fd_target < 0) {
wdenke7f34c62003-01-11 09:48:40 +00001081 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001082 "Can't open %s: %s\n",
1083 DEVNAME (dev_target),
1084 strerror (errno));
1085 rc = -1;
1086 goto exit;
wdenke7f34c62003-01-11 09:48:40 +00001087 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001088 } else {
1089 dev_target = dev_current;
1090 fd_target = fd_current;
wdenk34b613e2002-12-17 01:51:00 +00001091 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001092
1093 rc = flash_write (fd_current, fd_target, dev_target);
1094
wdenke7f34c62003-01-11 09:48:40 +00001095 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001096 if (close (fd_target)) {
wdenke7f34c62003-01-11 09:48:40 +00001097 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001098 "I/O error on %s: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001099 DEVNAME (dev_target),
wdenk3f9ab982003-04-12 23:38:12 +00001100 strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001101 rc = -1;
wdenke7f34c62003-01-11 09:48:40 +00001102 }
wdenk34b613e2002-12-17 01:51:00 +00001103 }
wdenk34b613e2002-12-17 01:51:00 +00001104 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001105 rc = flash_read (fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001106 }
1107
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001108exit:
1109 if (close (fd_current)) {
wdenk34b613e2002-12-17 01:51:00 +00001110 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001111 "I/O error on %s: %s\n",
1112 DEVNAME (dev_current), strerror (errno));
1113 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001114 }
1115
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001116 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001117}
1118
1119/*
1120 * s1 is either a simple 'name', or a 'name=value' pair.
1121 * s2 is a 'name=value' pair.
1122 * If the names match, return the value of s2, else NULL.
1123 */
1124
Markus Klotzbücher94db5572007-11-27 10:23:20 +01001125static char *envmatch (char * s1, char * s2)
wdenk34b613e2002-12-17 01:51:00 +00001126{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +00001127 if (s1 == NULL || s2 == NULL)
1128 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001129
1130 while (*s1 == *s2++)
1131 if (*s1++ == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001132 return s2;
wdenk3f9ab982003-04-12 23:38:12 +00001133 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001134 return s2;
1135 return NULL;
wdenk34b613e2002-12-17 01:51:00 +00001136}
1137
1138/*
1139 * Prevent confusion if running from erased flash memory
1140 */
Stefano Babicada628b2010-05-24 12:08:16 +02001141int fw_env_open(void)
wdenk34b613e2002-12-17 01:51:00 +00001142{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001143 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001144 unsigned char flag0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001145 void *addr0;
1146
wdenk34b613e2002-12-17 01:51:00 +00001147 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001148 unsigned char flag1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001149 void *addr1;
wdenke7f34c62003-01-11 09:48:40 +00001150
Marek Vasut05ed2a52014-03-05 19:59:52 +01001151 int ret;
1152
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001153 struct env_image_single *single;
1154 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001155
wdenk3f9ab982003-04-12 23:38:12 +00001156 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001157 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001158
Joe Hershberger671adc42012-10-03 09:38:46 +00001159 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001160 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001161 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001162 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001163 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001164 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001165 }
wdenk3f9ab982003-04-12 23:38:12 +00001166
wdenke7f34c62003-01-11 09:48:40 +00001167 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001168 environment.image = addr0;
1169
1170 if (HaveRedundEnv) {
1171 redundant = addr0;
1172 environment.crc = &redundant->crc;
1173 environment.flags = &redundant->flags;
1174 environment.data = redundant->data;
1175 } else {
1176 single = addr0;
1177 environment.crc = &single->crc;
1178 environment.flags = NULL;
1179 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001180 }
wdenk3f9ab982003-04-12 23:38:12 +00001181
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001182 dev_current = 0;
1183 if (flash_io (O_RDONLY))
1184 return -1;
1185
1186 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001187
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001188 if (common_args.aes_flag) {
Marek Vasut05ed2a52014-03-05 19:59:52 +01001189 ret = env_aes_cbc_crypt(environment.data, 0);
1190 if (ret)
1191 return ret;
1192 }
1193
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001194 crc0_ok = (crc0 == *environment.crc);
wdenke7f34c62003-01-11 09:48:40 +00001195 if (!HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001196 if (!crc0_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001197 fprintf (stderr,
1198 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkc5203f92006-03-12 23:27:46 +01001199 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk34b613e2002-12-17 01:51:00 +00001200 }
wdenke7f34c62003-01-11 09:48:40 +00001201 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001202 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001203
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001204 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001205 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001206 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001207 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001208 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001209 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001210 return -1;
wdenk3f9ab982003-04-12 23:38:12 +00001211 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001212 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001213
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001214 /*
1215 * have to set environment.image for flash_read(), careful -
1216 * other pointers in environment still point inside addr0
1217 */
1218 environment.image = addr1;
1219 if (flash_io (O_RDONLY))
1220 return -1;
1221
1222 /* Check flag scheme compatibility */
1223 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1224 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1225 environment.flag_scheme = FLAG_BOOLEAN;
1226 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1227 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1228 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001229 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1230 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1231 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001232 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1233 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1234 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001235 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1236 DEVTYPE(!dev_current) == MTD_ABSENT) {
1237 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001238 } else {
1239 fprintf (stderr, "Incompatible flash types!\n");
1240 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001241 }
wdenk3f9ab982003-04-12 23:38:12 +00001242
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001243 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001244
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001245 if (common_args.aes_flag) {
Marek Vasut05ed2a52014-03-05 19:59:52 +01001246 ret = env_aes_cbc_crypt(redundant->data, 0);
1247 if (ret)
1248 return ret;
1249 }
1250
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001251 crc1_ok = (crc1 == redundant->crc);
1252 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001253
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001254 if (crc0_ok && !crc1_ok) {
1255 dev_current = 0;
1256 } else if (!crc0_ok && crc1_ok) {
1257 dev_current = 1;
1258 } else if (!crc0_ok && !crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001259 fprintf (stderr,
1260 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001261 memcpy (environment.data, default_environment,
1262 sizeof default_environment);
1263 dev_current = 0;
1264 } else {
1265 switch (environment.flag_scheme) {
1266 case FLAG_BOOLEAN:
1267 if (flag0 == active_flag &&
1268 flag1 == obsolete_flag) {
1269 dev_current = 0;
1270 } else if (flag0 == obsolete_flag &&
1271 flag1 == active_flag) {
1272 dev_current = 1;
1273 } else if (flag0 == flag1) {
1274 dev_current = 0;
1275 } else if (flag0 == 0xFF) {
1276 dev_current = 0;
1277 } else if (flag1 == 0xFF) {
1278 dev_current = 1;
1279 } else {
1280 dev_current = 0;
1281 }
1282 break;
1283 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001284 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001285 dev_current = 1;
1286 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001287 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001288 dev_current = 0;
Jon Povey93ccd472011-03-11 14:10:56 +09001289 else /* flag1 > flag0 */
1290 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001291 break;
1292 default:
1293 fprintf (stderr, "Unknown flag scheme %u \n",
1294 environment.flag_scheme);
1295 return -1;
1296 }
1297 }
1298
1299 /*
1300 * If we are reading, we don't need the flag and the CRC any
1301 * more, if we are writing, we will re-calculate CRC and update
1302 * flags before writing out
1303 */
1304 if (dev_current) {
1305 environment.image = addr1;
1306 environment.crc = &redundant->crc;
1307 environment.flags = &redundant->flags;
1308 environment.data = redundant->data;
1309 free (addr0);
1310 } else {
1311 environment.image = addr0;
1312 /* Other pointers are already set */
wdenk3f9ab982003-04-12 23:38:12 +00001313 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +00001314 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001315#ifdef DEBUG
1316 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1317#endif
wdenk34b613e2002-12-17 01:51:00 +00001318 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001319 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001320}
1321
1322
wdenk3f9ab982003-04-12 23:38:12 +00001323static int parse_config ()
wdenk34b613e2002-12-17 01:51:00 +00001324{
1325 struct stat st;
1326
wdenke7f34c62003-01-11 09:48:40 +00001327#if defined(CONFIG_FILE)
1328 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001329 if (get_config(common_args.config_file)) {
1330 fprintf(stderr, "Cannot parse config file '%s': %m\n",
1331 common_args.config_file);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001332 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001333 }
wdenke7f34c62003-01-11 09:48:40 +00001334#else
Tom Rini313e9122014-03-28 12:03:33 -04001335 DEVNAME (0) = DEVICE1_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001336 DEVOFFSET (0) = DEVICE1_OFFSET;
1337 ENVSIZE (0) = ENV1_SIZE;
Dustin Byford2f6df462014-03-06 20:48:22 -08001338 /* Default values are: erase-size=env-size */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001339 DEVESIZE (0) = ENVSIZE (0);
Dustin Byford2f6df462014-03-06 20:48:22 -08001340 /* #sectors=env-size/erase-size (rounded up) */
1341 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
Remy Bohmercb969eb2011-02-12 19:06:26 +01001342#ifdef DEVICE1_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001343 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001344#endif
1345#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001346 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001347#endif
1348
wdenk34b613e2002-12-17 01:51:00 +00001349#ifdef HAVE_REDUND
Tom Rini313e9122014-03-28 12:03:33 -04001350 DEVNAME (1) = DEVICE2_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001351 DEVOFFSET (1) = DEVICE2_OFFSET;
1352 ENVSIZE (1) = ENV2_SIZE;
Dustin Byford2f6df462014-03-06 20:48:22 -08001353 /* Default values are: erase-size=env-size */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001354 DEVESIZE (1) = ENVSIZE (1);
Dustin Byford2f6df462014-03-06 20:48:22 -08001355 /* #sectors=env-size/erase-size (rounded up) */
1356 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
Remy Bohmercb969eb2011-02-12 19:06:26 +01001357#ifdef DEVICE2_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001358 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001359#endif
1360#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001361 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001362#endif
wdenke7f34c62003-01-11 09:48:40 +00001363 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +00001364#endif
wdenke7f34c62003-01-11 09:48:40 +00001365#endif
wdenk3f9ab982003-04-12 23:38:12 +00001366 if (stat (DEVNAME (0), &st)) {
1367 fprintf (stderr,
1368 "Cannot access MTD device %s: %s\n",
1369 DEVNAME (0), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001370 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001371 }
wdenk3f9ab982003-04-12 23:38:12 +00001372
1373 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1374 fprintf (stderr,
1375 "Cannot access MTD device %s: %s\n",
Wolfgang Denk8e6213c2005-09-25 01:20:38 +02001376 DEVNAME (1), strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001377 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001378 }
wdenk34b613e2002-12-17 01:51:00 +00001379 return 0;
1380}
wdenke7f34c62003-01-11 09:48:40 +00001381
1382#if defined(CONFIG_FILE)
1383static int get_config (char *fname)
1384{
1385 FILE *fp;
1386 int i = 0;
1387 int rc;
1388 char dump[128];
Tom Rini313e9122014-03-28 12:03:33 -04001389 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001390
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001391 fp = fopen (fname, "r");
1392 if (fp == NULL)
1393 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001394
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001395 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenke7f34c62003-01-11 09:48:40 +00001396 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001397 if (dump[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001398 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001399
Tom Rini313e9122014-03-28 12:03:33 -04001400 rc = sscanf (dump, "%ms %lx %lx %lx %lx",
1401 &devname,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001402 &DEVOFFSET (i),
1403 &ENVSIZE (i),
1404 &DEVESIZE (i),
1405 &ENVSECTORS (i));
1406
Remy Bohmercb969eb2011-02-12 19:06:26 +01001407 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001408 continue;
1409
Tom Rini313e9122014-03-28 12:03:33 -04001410 DEVNAME(i) = devname;
1411
Remy Bohmercb969eb2011-02-12 19:06:26 +01001412 if (rc < 4)
1413 /* Assume the erase size is the same as the env-size */
1414 DEVESIZE(i) = ENVSIZE(i);
1415
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001416 if (rc < 5)
Dustin Byford2f6df462014-03-06 20:48:22 -08001417 /* Assume enough env sectors to cover the environment */
1418 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
wdenke7f34c62003-01-11 09:48:40 +00001419
1420 i++;
1421 }
wdenk3f9ab982003-04-12 23:38:12 +00001422 fclose (fp);
1423
wdenke7f34c62003-01-11 09:48:40 +00001424 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +00001425 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001426 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001427 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001428 } else
1429 return 0;
1430}
1431#endif