blob: 0c448079cc3b5ac7eeffece63bfad54a85bdef52 [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>
Andreas Fenkart10667e12016-03-11 09:39:35 +010018#include <ctype.h>
wdenk6aff3112002-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>
wdenk6aff3112002-12-17 01:51:00 +000027
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010028#ifdef MTD_OLD
Wolfgang Denk1711f3b2008-09-02 21:17:36 +020029# include <stdint.h>
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010030# include <linux/mtd/mtd.h>
31#else
Wolfgang Denkc83d7ca2008-01-08 22:58:27 +010032# define __user /* nothing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010033# include <mtd/mtd-user.h>
34#endif
35
36#include "fw_env.h"
wdenk6aff3112002-12-17 01:51:00 +000037
Andreas Fenkart43cb65b2016-03-25 14:52:19 +010038struct common_args common_args;
39struct printenv_args printenv_args;
40struct setenv_args setenv_args;
41
Marek Vasuta8a752c2014-03-05 19:59:52 +010042#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
43
Guennadi Liakhovetski56086922008-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 Rini229695f2014-03-28 12:03:33 -040051 const char *devname; /* Device name */
wdenk4a6fd342003-04-12 23:38:12 +000052 ulong devoff; /* Device offset */
53 ulong env_size; /* environment size */
54 ulong erase_size; /* device erase size */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020055 ulong env_sectors; /* number of environment sectors */
56 uint8_t mtd_type; /* type of the MTD device */
57};
wdenk6aff3112002-12-17 01:51:00 +000058
Guennadi Liakhovetski56086922008-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;
wdenk6aff3112002-12-17 01:51:00 +000068
69#define DEVNAME(i) envdevices[(i)].devname
wdenkd0fb80c2003-01-11 09:48:40 +000070#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk6aff3112002-12-17 01:51:00 +000071#define ENVSIZE(i) envdevices[(i)].env_size
72#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020073#define ENVSECTORS(i) envdevices[(i)].env_sectors
74#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk6aff3112002-12-17 01:51:00 +000075
Joe Hershberger497f2052012-10-03 09:38:46 +000076#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk6aff3112002-12-17 01:51:00 +000077
wdenkd0fb80c2003-01-11 09:48:40 +000078#define ENV_SIZE getenvsize()
wdenk6aff3112002-12-17 01:51:00 +000079
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020080struct env_image_single {
81 uint32_t crc; /* CRC32 over data bytes */
82 char data[];
83};
wdenk6aff3112002-12-17 01:51:00 +000084
Guennadi Liakhovetski56086922008-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};
wdenk6aff3112002-12-17 01:51:00 +0000108
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200109static int env_aes_cbc_crypt(char *data, const int enc, uint8_t *key);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100110
wdenkd0fb80c2003-01-11 09:48:40 +0000111static int HaveRedundEnv = 0;
112
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100113static unsigned char active_flag = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200114/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100115static unsigned char obsolete_flag = 0;
wdenkd0fb80c2003-01-11 09:48:40 +0000116
Joe Hershbergerddd84182012-10-12 08:48:51 +0000117#define DEFAULT_ENV_INSTANCE_STATIC
118#include <env_default.h>
wdenk6aff3112002-12-17 01:51:00 +0000119
wdenk4a6fd342003-04-12 23:38:12 +0000120static int flash_io (int mode);
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100121static char *envmatch (char * s1, char * s2);
wdenk4a6fd342003-04-12 23:38:12 +0000122static int parse_config (void);
123
wdenkd0fb80c2003-01-11 09:48:40 +0000124#if defined(CONFIG_FILE)
wdenk4a6fd342003-04-12 23:38:12 +0000125static int get_config (char *);
wdenkd0fb80c2003-01-11 09:48:40 +0000126#endif
wdenk4a6fd342003-04-12 23:38:12 +0000127static inline ulong getenvsize (void)
wdenkd0fb80c2003-01-11 09:48:40 +0000128{
Dominic Sacréea195272014-11-05 00:30:18 +0100129 ulong rc = CUR_ENVSIZE - sizeof(uint32_t);
wdenk4a6fd342003-04-12 23:38:12 +0000130
wdenkd0fb80c2003-01-11 09:48:40 +0000131 if (HaveRedundEnv)
wdenk4a6fd342003-04-12 23:38:12 +0000132 rc -= sizeof (char);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100133
Andreas Fenkart371ee132015-12-09 13:13:24 +0100134 if (common_args.aes_flag)
Marek Vasuta8a752c2014-03-05 19:59:52 +0100135 rc &= ~(AES_KEY_LENGTH - 1);
136
wdenkd0fb80c2003-01-11 09:48:40 +0000137 return rc;
138}
wdenk6aff3112002-12-17 01:51:00 +0000139
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100140static char *skip_chars(char *s)
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200141{
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100142 for (; *s != '\0'; s++) {
143 if (isblank(*s))
144 return s;
145 }
146 return NULL;
147}
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200148
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100149static char *skip_blanks(char *s)
150{
151 for (; *s != '\0'; s++) {
152 if (!isblank(*s))
Andreas Fenkart9583efc2016-03-11 09:39:36 +0100153 return s;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200154 }
Andreas Fenkart9583efc2016-03-11 09:39:36 +0100155 return NULL;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200156}
157
wdenk6aff3112002-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ücher6de66b32007-11-27 10:23:20 +0100162char *fw_getenv (char *name)
wdenk6aff3112002-12-17 01:51:00 +0000163{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100164 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000165
wdenk4a6fd342003-04-12 23:38:12 +0000166 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100167 char *val;
wdenk6aff3112002-12-17 01:51:00 +0000168
wdenk4a6fd342003-04-12 23:38:12 +0000169 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000170 if (nxt >= &environment.data[ENV_SIZE]) {
171 fprintf (stderr, "## Error: "
172 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200173 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000174 }
175 }
wdenk4a6fd342003-04-12 23:38:12 +0000176 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000177 if (!val)
178 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200179 return val;
wdenk6aff3112002-12-17 01:51:00 +0000180 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200181 return NULL;
wdenk6aff3112002-12-17 01:51:00 +0000182}
183
184/*
Joe Hershberger267541f2012-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 Fenkart371ee132015-12-09 13:13:24 +0100210int parse_aes_key(char *key, uint8_t *bin_key)
Marek Vasuta8a752c2014-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 Fenkart8b27abd2015-12-09 13:13:22 +0100232 bin_key[i] = ul & 0xff;
Marek Vasuta8a752c2014-03-05 19:59:52 +0100233 key += 2;
234 }
Marek Vasuta8a752c2014-03-05 19:59:52 +0100235 return 0;
236}
237
Joe Hershberger267541f2012-12-11 22:16:34 -0600238/*
wdenk6aff3112002-12-17 01:51:00 +0000239 * Print the current definition of one, or more, or all
240 * environment variables
241 */
Grant Ericksonbc117562008-05-06 20:16:15 -0700242int fw_printenv (int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000243{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100244 char *env, *nxt;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100245 int i, rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000246
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200247 if (fw_env_open())
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200248 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000249
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100250 if (argc == 0) { /* Print all env variables */
wdenk4a6fd342003-04-12 23:38:12 +0000251 for (env = environment.data; *env; env = nxt + 1) {
252 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000253 if (nxt >= &environment.data[ENV_SIZE]) {
254 fprintf (stderr, "## Error: "
255 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200256 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000257 }
258 }
259
wdenk4a6fd342003-04-12 23:38:12 +0000260 printf ("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000261 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200262 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000263 }
264
Andreas Fenkart1ce68692015-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;
wdenk6aff3112002-12-17 01:51:00 +0000269 }
270
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100271 for (i = 0; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100272 char *name = argv[i];
273 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000274
wdenk4a6fd342003-04-12 23:38:12 +0000275 for (env = environment.data; *env; env = nxt + 1) {
wdenk6aff3112002-12-17 01:51:00 +0000276
wdenk4a6fd342003-04-12 23:38:12 +0000277 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000278 if (nxt >= &environment.data[ENV_SIZE]) {
279 fprintf (stderr, "## Error: "
280 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200281 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000282 }
283 }
wdenk4a6fd342003-04-12 23:38:12 +0000284 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000285 if (val) {
Andreas Fenkart371ee132015-12-09 13:13:24 +0100286 if (!printenv_args.name_suppress) {
wdenk6aff3112002-12-17 01:51:00 +0000287 fputs (name, stdout);
wdenk4a6fd342003-04-12 23:38:12 +0000288 putc ('=', stdout);
wdenk6aff3112002-12-17 01:51:00 +0000289 }
wdenk4a6fd342003-04-12 23:38:12 +0000290 puts (val);
wdenk6aff3112002-12-17 01:51:00 +0000291 break;
292 }
293 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700294 if (!val) {
wdenk4a6fd342003-04-12 23:38:12 +0000295 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700296 rc = -1;
297 }
wdenk6aff3112002-12-17 01:51:00 +0000298 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700299
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200300 return rc;
wdenk6aff3112002-12-17 01:51:00 +0000301}
302
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200303int fw_env_close(void)
wdenk6aff3112002-12-17 01:51:00 +0000304{
Marek Vasuta8a752c2014-03-05 19:59:52 +0100305 int ret;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100306 if (common_args.aes_flag) {
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200307 ret = env_aes_cbc_crypt(environment.data, 1,
308 common_args.aes_key);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100309 if (ret) {
310 fprintf(stderr,
311 "Error: can't encrypt env for flash\n");
312 return ret;
313 }
314 }
315
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200316 /*
317 * Update CRC
318 */
319 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000320
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200321 /* write environment back to flash */
322 if (flash_io(O_RDWR)) {
323 fprintf(stderr,
324 "Error: can't write fw_env to flash\n");
325 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000326 }
327
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200328 return 0;
329}
wdenk6aff3112002-12-17 01:51:00 +0000330
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200331
332/*
333 * Set/Clear a single variable in the environment.
334 * This is called in sequence to update the environment
335 * in RAM without updating the copy in flash after each set
336 */
337int fw_env_write(char *name, char *value)
338{
339 int len;
340 char *env, *nxt;
341 char *oldval = NULL;
Joe Hershberger267541f2012-12-11 22:16:34 -0600342 int deleting, creating, overwriting;
wdenk6aff3112002-12-17 01:51:00 +0000343
344 /*
345 * search if variable with this name already exists
346 */
wdenk4a6fd342003-04-12 23:38:12 +0000347 for (nxt = env = environment.data; *env; env = nxt + 1) {
348 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000349 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200350 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000351 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200352 errno = EINVAL;
353 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000354 }
355 }
wdenk4a6fd342003-04-12 23:38:12 +0000356 if ((oldval = envmatch (name, env)) != NULL)
wdenk6aff3112002-12-17 01:51:00 +0000357 break;
358 }
359
Joe Hershberger267541f2012-12-11 22:16:34 -0600360 deleting = (oldval && !(value && strlen(value)));
361 creating = (!oldval && (value && strlen(value)));
362 overwriting = (oldval && (value && strlen(value)));
363
364 /* check for permission */
365 if (deleting) {
366 if (env_flags_validate_varaccess(name,
367 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
368 printf("Can't delete \"%s\"\n", name);
369 errno = EROFS;
370 return -1;
371 }
372 } else if (overwriting) {
373 if (env_flags_validate_varaccess(name,
374 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
375 printf("Can't overwrite \"%s\"\n", name);
376 errno = EROFS;
377 return -1;
378 } else if (env_flags_validate_varaccess(name,
379 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
380 const char *defval = fw_getdefenv(name);
381
382 if (defval == NULL)
383 defval = "";
384 if (strcmp(oldval, defval)
385 != 0) {
386 printf("Can't overwrite \"%s\"\n", name);
387 errno = EROFS;
388 return -1;
389 }
390 }
391 } else if (creating) {
392 if (env_flags_validate_varaccess(name,
393 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
394 printf("Can't create \"%s\"\n", name);
395 errno = EROFS;
396 return -1;
397 }
398 } else
399 /* Nothing to do */
400 return 0;
401
402 if (deleting || overwriting) {
wdenk6aff3112002-12-17 01:51:00 +0000403 if (*++nxt == '\0') {
404 *env = '\0';
405 } else {
406 for (;;) {
407 *env = *nxt++;
408 if ((*env == '\0') && (*nxt == '\0'))
409 break;
410 ++env;
411 }
412 }
413 *++env = '\0';
414 }
415
416 /* Delete only ? */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200417 if (!value || !strlen(value))
418 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000419
420 /*
421 * Append new definition at the end
422 */
wdenk4a6fd342003-04-12 23:38:12 +0000423 for (env = environment.data; *env || *(env + 1); ++env);
wdenk6aff3112002-12-17 01:51:00 +0000424 if (env > environment.data)
425 ++env;
426 /*
427 * Overflow when:
Joe Hershberger497f2052012-10-03 09:38:46 +0000428 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk6aff3112002-12-17 01:51:00 +0000429 */
wdenk4a6fd342003-04-12 23:38:12 +0000430 len = strlen (name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000431 /* add '=' for first arg, ' ' for all others */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200432 len += strlen(value) + 1;
433
wdenk4a6fd342003-04-12 23:38:12 +0000434 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk6aff3112002-12-17 01:51:00 +0000435 fprintf (stderr,
436 "Error: environment overflow, \"%s\" deleted\n",
437 name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200438 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000439 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200440
wdenk6aff3112002-12-17 01:51:00 +0000441 while ((*env = *name++) != '\0')
442 env++;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200443 *env = '=';
444 while ((*++env = *value++) != '\0')
445 ;
wdenk6aff3112002-12-17 01:51:00 +0000446
447 /* end is marked with double '\0' */
448 *++env = '\0';
449
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200450 return 0;
451}
wdenk6aff3112002-12-17 01:51:00 +0000452
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200453/*
454 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
455 * 0 - OK
456 * EINVAL - need at least 1 argument
457 * EROFS - certain variables ("ethaddr", "serial#") cannot be
458 * modified or deleted
459 *
460 */
461int fw_setenv(int argc, char *argv[])
462{
Andreas Fenkart371ee132015-12-09 13:13:24 +0100463 int i;
Mike Frysinger3779c8e2012-11-10 19:47:45 +0000464 size_t len;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100465 char *name, **valv;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200466 char *value = NULL;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100467 int valc;
wdenk6aff3112002-12-17 01:51:00 +0000468
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100469 if (argc < 1) {
470 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasuta8a752c2014-03-05 19:59:52 +0100471 errno = EINVAL;
472 return -1;
473 }
474
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200475 if (fw_env_open()) {
476 fprintf(stderr, "Error: environment not initialized\n");
477 return -1;
478 }
479
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100480 name = argv[0];
481 valv = argv + 1;
482 valc = argc - 1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200483
Andreas Fenkart167f5252015-12-09 13:13:21 +0100484 if (env_flags_validate_env_set_params(name, valv, valc) < 0)
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600485 return 1;
486
Joe Hershberger62a34a02012-10-03 09:38:47 +0000487 len = 0;
Andreas Fenkart167f5252015-12-09 13:13:21 +0100488 for (i = 0; i < valc; ++i) {
489 char *val = valv[i];
Joe Hershberger62a34a02012-10-03 09:38:47 +0000490 size_t val_len = strlen(val);
491
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000492 if (value)
493 value[len - 1] = ' ';
Joe Hershberger62a34a02012-10-03 09:38:47 +0000494 value = realloc(value, len + val_len + 1);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200495 if (!value) {
Joe Hershberger62a34a02012-10-03 09:38:47 +0000496 fprintf(stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200497 "Cannot malloc %zu bytes: %s\n",
Joe Hershberger62a34a02012-10-03 09:38:47 +0000498 len, strerror(errno));
499 return -1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200500 }
Joe Hershberger62a34a02012-10-03 09:38:47 +0000501
502 memcpy(value + len, val, val_len);
503 len += val_len;
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000504 value[len++] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200505 }
506
507 fw_env_write(name, value);
508
Joe Hershberger62a34a02012-10-03 09:38:47 +0000509 free(value);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200510
511 return fw_env_close();
512}
513
514/*
515 * Parse a file and configure the u-boot variables.
516 * The script file has a very simple format, as follows:
517 *
518 * Each line has a couple with name, value:
519 * <white spaces>variable_name<white spaces>variable_value
520 *
521 * Both variable_name and variable_value are interpreted as strings.
522 * Any character after <white spaces> and before ending \r\n is interpreted
523 * as variable's value (no comment allowed on these lines !)
524 *
525 * Comments are allowed if the first character in the line is #
526 *
527 * Returns -1 and sets errno error codes:
528 * 0 - OK
529 * -1 - Error
530 */
531int fw_parse_script(char *fname)
532{
533 FILE *fp;
534 char dump[1024]; /* Maximum line length in the file */
535 char *name;
536 char *val;
537 int lineno = 0;
538 int len;
539 int ret = 0;
540
541 if (fw_env_open()) {
542 fprintf(stderr, "Error: environment not initialized\n");
543 return -1;
544 }
545
546 if (strcmp(fname, "-") == 0)
547 fp = stdin;
548 else {
549 fp = fopen(fname, "r");
550 if (fp == NULL) {
551 fprintf(stderr, "I cannot open %s for reading\n",
552 fname);
553 return -1;
554 }
555 }
556
557 while (fgets(dump, sizeof(dump), fp)) {
558 lineno++;
559 len = strlen(dump);
560
561 /*
562 * Read a whole line from the file. If the line is too long
563 * or is not terminated, reports an error and exit.
564 */
565 if (dump[len - 1] != '\n') {
566 fprintf(stderr,
567 "Line %d not corrected terminated or too long\n",
568 lineno);
569 ret = -1;
570 break;
571 }
572
573 /* Drop ending line feed / carriage return */
Andreas Fenkart69067a32016-03-11 09:39:38 +0100574 dump[--len] = '\0';
575 if (len && dump[len - 1] == '\r')
576 dump[--len] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200577
578 /* Skip comment or empty lines */
Andreas Fenkart69067a32016-03-11 09:39:38 +0100579 if (len == 0 || dump[0] == '#')
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200580 continue;
581
582 /*
583 * Search for variable's name,
584 * remove leading whitespaces
585 */
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100586 name = skip_blanks(dump);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200587 if (!name)
588 continue;
589
590 /* The first white space is the end of variable name */
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100591 val = skip_chars(name);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200592 len = strlen(name);
593 if (val) {
594 *val++ = '\0';
595 if ((val - name) < len)
Andreas Fenkart938c29f2016-03-11 09:39:37 +0100596 val = skip_blanks(val);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200597 else
598 val = NULL;
599 }
600
601#ifdef DEBUG
602 fprintf(stderr, "Setting %s : %s\n",
603 name, val ? val : " removed");
604#endif
605
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600606 if (env_flags_validate_type(name, val) < 0) {
607 ret = -1;
608 break;
609 }
610
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200611 /*
612 * If there is an error setting a variable,
613 * try to save the environment and returns an error
614 */
615 if (fw_env_write(name, val)) {
616 fprintf(stderr,
617 "fw_env_write returns with error : %s\n",
618 strerror(errno));
619 ret = -1;
620 break;
621 }
622
623 }
624
625 /* Close file if not stdin */
626 if (strcmp(fname, "-") != 0)
627 fclose(fp);
628
629 ret |= fw_env_close();
630
631 return ret;
632
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200633}
634
635/*
636 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
637 * 0 - block is good
638 * > 0 - block is bad
639 * < 0 - failed to test
640 */
641static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
642{
643 if (mtd_type == MTD_NANDFLASH) {
644 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
645
646 if (badblock < 0) {
647 perror ("Cannot read bad block mark");
648 return badblock;
649 }
650
651 if (badblock) {
652#ifdef DEBUG
653 fprintf (stderr, "Bad block at 0x%llx, "
654 "skipping\n", *blockstart);
655#endif
656 return badblock;
657 }
658 }
659
660 return 0;
661}
662
663/*
664 * Read data from flash at an offset into a provided buffer. On NAND it skips
665 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
666 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
667 */
668static int flash_read_buf (int dev, int fd, void *buf, size_t count,
669 off_t offset, uint8_t mtd_type)
670{
671 size_t blocklen; /* erase / write length - one block on NAND,
672 0 on NOR */
673 size_t processed = 0; /* progress counter */
674 size_t readlen = count; /* current read length */
675 off_t top_of_range; /* end of the last block we may use */
676 off_t block_seek; /* offset inside the current block to the start
677 of the data */
678 loff_t blockstart; /* running start of the current block -
679 MEMGETBADBLOCK needs 64 bits */
680 int rc;
681
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100682 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200683
684 /* Offset inside a block */
685 block_seek = offset - blockstart;
686
687 if (mtd_type == MTD_NANDFLASH) {
688 /*
689 * NAND: calculate which blocks we are reading. We have
690 * to read one block at a time to skip bad blocks.
691 */
692 blocklen = DEVESIZE (dev);
693
694 /*
695 * To calculate the top of the range, we have to use the
696 * global DEVOFFSET (dev), which can be different from offset
697 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100698 top_of_range = ((DEVOFFSET(dev) / blocklen) +
699 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200700
701 /* Limit to one block for the first read */
702 if (readlen > blocklen - block_seek)
703 readlen = blocklen - block_seek;
704 } else {
705 blocklen = 0;
706 top_of_range = offset + count;
707 }
708
709 /* This only runs once on NOR flash */
710 while (processed < count) {
711 rc = flash_bad_block (fd, mtd_type, &blockstart);
712 if (rc < 0) /* block test failed */
713 return -1;
714
715 if (blockstart + block_seek + readlen > top_of_range) {
716 /* End of range is reached */
717 fprintf (stderr,
718 "Too few good blocks within range\n");
719 return -1;
720 }
721
722 if (rc) { /* block is bad */
723 blockstart += blocklen;
724 continue;
725 }
726
727 /*
728 * If a block is bad, we retry in the next block at the same
729 * offset - see common/env_nand.c::writeenv()
730 */
731 lseek (fd, blockstart + block_seek, SEEK_SET);
732
733 rc = read (fd, buf + processed, readlen);
734 if (rc != readlen) {
735 fprintf (stderr, "Read error on %s: %s\n",
736 DEVNAME (dev), strerror (errno));
737 return -1;
738 }
739#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000740 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
741 rc, blockstart + block_seek, DEVNAME(dev));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200742#endif
743 processed += readlen;
744 readlen = min (blocklen, count - processed);
745 block_seek = 0;
746 blockstart += blocklen;
747 }
748
749 return processed;
750}
751
752/*
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100753 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
754 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
755 * erase and write the whole data at once.
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200756 */
757static int flash_write_buf (int dev, int fd, void *buf, size_t count,
758 off_t offset, uint8_t mtd_type)
759{
760 void *data;
761 struct erase_info_user erase;
762 size_t blocklen; /* length of NAND block / NOR erase sector */
763 size_t erase_len; /* whole area that can be erased - may include
764 bad blocks */
765 size_t erasesize; /* erase / write length - one block on NAND,
766 whole area on NOR */
767 size_t processed = 0; /* progress counter */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100768 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200769 bad blocks */
770 off_t erase_offset; /* offset to the first erase block (aligned)
771 below offset */
772 off_t block_seek; /* offset inside the erase block to the start
773 of the data */
774 off_t top_of_range; /* end of the last block we may use */
775 loff_t blockstart; /* running start of the current block -
776 MEMGETBADBLOCK needs 64 bits */
777 int rc;
778
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200779 /*
Oliver Metze387efb2013-08-30 00:56:02 +0200780 * For mtd devices only offset and size of the environment do matter
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200781 */
Oliver Metze387efb2013-08-30 00:56:02 +0200782 if (mtd_type == MTD_ABSENT) {
783 blocklen = count;
784 top_of_range = offset + count;
785 erase_len = blocklen;
786 blockstart = offset;
787 block_seek = 0;
788 write_total = blocklen;
789 } else {
790 blocklen = DEVESIZE(dev);
791
792 top_of_range = ((DEVOFFSET(dev) / blocklen) +
793 ENVSECTORS(dev)) * blocklen;
794
795 erase_offset = (offset / blocklen) * blocklen;
796
797 /* Maximum area we may use */
798 erase_len = top_of_range - erase_offset;
799
800 blockstart = erase_offset;
801 /* Offset inside a block */
802 block_seek = offset - erase_offset;
803
804 /*
805 * Data size we actually write: from the start of the block
806 * to the start of the data, then count bytes of data, and
807 * to the end of the block
808 */
809 write_total = ((block_seek + count + blocklen - 1) /
810 blocklen) * blocklen;
811 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200812
813 /*
814 * Support data anywhere within erase sectors: read out the complete
815 * area to be erased, replace the environment image, write the whole
816 * block back again.
817 */
818 if (write_total > count) {
819 data = malloc (erase_len);
820 if (!data) {
821 fprintf (stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200822 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200823 erase_len, strerror (errno));
824 return -1;
825 }
826
827 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
828 mtd_type);
829 if (write_total != rc)
830 return -1;
831
Joe Hershberger74620e12012-10-15 15:29:25 +0000832#ifdef DEBUG
833 fprintf(stderr, "Preserving data ");
834 if (block_seek != 0)
835 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
836 if (block_seek + count != write_total) {
837 if (block_seek != 0)
838 fprintf(stderr, " and ");
839 fprintf(stderr, "0x%lx - 0x%x",
840 block_seek + count, write_total - 1);
841 }
842 fprintf(stderr, "\n");
843#endif
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200844 /* Overwrite the old environment */
845 memcpy (data + block_seek, buf, count);
846 } else {
847 /*
848 * We get here, iff offset is block-aligned and count is a
849 * multiple of blocklen - see write_total calculation above
850 */
851 data = buf;
852 }
853
854 if (mtd_type == MTD_NANDFLASH) {
855 /*
856 * NAND: calculate which blocks we are writing. We have
857 * to write one block at a time to skip bad blocks.
858 */
859 erasesize = blocklen;
860 } else {
861 erasesize = erase_len;
862 }
863
864 erase.length = erasesize;
865
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100866 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200867 while (processed < write_total) {
868 rc = flash_bad_block (fd, mtd_type, &blockstart);
869 if (rc < 0) /* block test failed */
870 return rc;
871
872 if (blockstart + erasesize > top_of_range) {
873 fprintf (stderr, "End of range reached, aborting\n");
874 return -1;
875 }
876
877 if (rc) { /* block is bad */
878 blockstart += blocklen;
879 continue;
880 }
881
Oliver Metze387efb2013-08-30 00:56:02 +0200882 if (mtd_type != MTD_ABSENT) {
883 erase.start = blockstart;
884 ioctl(fd, MEMUNLOCK, &erase);
885 /* These do not need an explicit erase cycle */
886 if (mtd_type != MTD_DATAFLASH)
887 if (ioctl(fd, MEMERASE, &erase) != 0) {
888 fprintf(stderr,
889 "MTD erase error on %s: %s\n",
890 DEVNAME(dev), strerror(errno));
891 return -1;
892 }
893 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200894
895 if (lseek (fd, blockstart, SEEK_SET) == -1) {
896 fprintf (stderr,
897 "Seek error on %s: %s\n",
898 DEVNAME (dev), strerror (errno));
899 return -1;
900 }
901
902#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000903 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
904 blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200905#endif
906 if (write (fd, data + processed, erasesize) != erasesize) {
907 fprintf (stderr, "Write error on %s: %s\n",
908 DEVNAME (dev), strerror (errno));
909 return -1;
910 }
911
Oliver Metze387efb2013-08-30 00:56:02 +0200912 if (mtd_type != MTD_ABSENT)
913 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200914
Dustin Byford4b774ff2014-03-06 20:48:23 -0800915 processed += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200916 block_seek = 0;
Dustin Byford4b774ff2014-03-06 20:48:23 -0800917 blockstart += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200918 }
919
920 if (write_total > count)
921 free (data);
922
923 return processed;
924}
925
926/*
927 * Set obsolete flag at offset - NOR flash only
928 */
929static int flash_flag_obsolete (int dev, int fd, off_t offset)
930{
931 int rc;
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200932 struct erase_info_user erase;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200933
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200934 erase.start = DEVOFFSET (dev);
935 erase.length = DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200936 /* This relies on the fact, that obsolete_flag == 0 */
937 rc = lseek (fd, offset, SEEK_SET);
938 if (rc < 0) {
939 fprintf (stderr, "Cannot seek to set the flag on %s \n",
940 DEVNAME (dev));
941 return rc;
942 }
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200943 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200944 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200945 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200946 if (rc < 0)
947 perror ("Could not set obsolete flag");
948
949 return rc;
950}
951
Marek Vasuta8a752c2014-03-05 19:59:52 +0100952/* Encrypt or decrypt the environment before writing or reading it. */
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200953static int env_aes_cbc_crypt(char *payload, const int enc, uint8_t *key)
Marek Vasuta8a752c2014-03-05 19:59:52 +0100954{
955 uint8_t *data = (uint8_t *)payload;
956 const int len = getenvsize();
957 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
958 uint32_t aes_blocks;
959
960 /* First we expand the key. */
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +0200961 aes_expand_key(key, key_exp);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100962
963 /* Calculate the number of AES blocks to encrypt. */
964 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
965
966 if (enc)
967 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
968 else
969 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
970
971 return 0;
972}
973
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200974static int flash_write (int fd_current, int fd_target, int dev_target)
975{
976 int rc;
977
978 switch (environment.flag_scheme) {
979 case FLAG_NONE:
980 break;
981 case FLAG_INCREMENTAL:
982 (*environment.flags)++;
983 break;
984 case FLAG_BOOLEAN:
985 *environment.flags = active_flag;
986 break;
987 default:
988 fprintf (stderr, "Unimplemented flash scheme %u \n",
989 environment.flag_scheme);
990 return -1;
991 }
992
993#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000994 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200995 DEVOFFSET (dev_target), DEVNAME (dev_target));
996#endif
Marek Vasuta8a752c2014-03-05 19:59:52 +0100997
Joe Hershberger497f2052012-10-03 09:38:46 +0000998 rc = flash_write_buf(dev_target, fd_target, environment.image,
999 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001000 DEVTYPE(dev_target));
1001 if (rc < 0)
1002 return rc;
1003
1004 if (environment.flag_scheme == FLAG_BOOLEAN) {
1005 /* Have to set obsolete flag */
1006 off_t offset = DEVOFFSET (dev_current) +
1007 offsetof (struct env_image_redundant, flags);
1008#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +00001009 fprintf(stderr,
1010 "Setting obsolete flag in environment at 0x%lx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001011 DEVOFFSET (dev_current), DEVNAME (dev_current));
1012#endif
1013 flash_flag_obsolete (dev_current, fd_current, offset);
1014 }
1015
1016 return 0;
1017}
1018
1019static int flash_read (int fd)
1020{
1021 struct mtd_info_user mtdinfo;
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001022 struct stat st;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001023 int rc;
1024
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001025 rc = fstat(fd, &st);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001026 if (rc < 0) {
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001027 fprintf(stderr, "Cannot stat the file %s\n",
1028 DEVNAME(dev_current));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001029 return -1;
1030 }
1031
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001032 if (S_ISCHR(st.st_mode)) {
1033 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1034 if (rc < 0) {
1035 fprintf(stderr, "Cannot get MTD information for %s\n",
1036 DEVNAME(dev_current));
1037 return -1;
1038 }
1039 if (mtdinfo.type != MTD_NORFLASH &&
1040 mtdinfo.type != MTD_NANDFLASH &&
Joe Hershberger2b744332013-04-08 10:32:51 +00001041 mtdinfo.type != MTD_DATAFLASH &&
1042 mtdinfo.type != MTD_UBIVOLUME) {
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001043 fprintf (stderr, "Unsupported flash type %u on %s\n",
1044 mtdinfo.type, DEVNAME(dev_current));
1045 return -1;
1046 }
1047 } else {
1048 memset(&mtdinfo, 0, sizeof(mtdinfo));
1049 mtdinfo.type = MTD_ABSENT;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001050 }
1051
1052 DEVTYPE(dev_current) = mtdinfo.type;
1053
Joe Hershberger497f2052012-10-03 09:38:46 +00001054 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001055 DEVOFFSET (dev_current), mtdinfo.type);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001056 if (rc != CUR_ENVSIZE)
1057 return -1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001058
Marek Vasuta8a752c2014-03-05 19:59:52 +01001059 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001060}
1061
1062static int flash_io (int mode)
1063{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001064 int fd_current, fd_target, rc, dev_target;
wdenk6aff3112002-12-17 01:51:00 +00001065
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001066 /* dev_current: fd_current, erase_current */
1067 fd_current = open (DEVNAME (dev_current), mode);
1068 if (fd_current < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001069 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001070 "Can't open %s: %s\n",
1071 DEVNAME (dev_current), strerror (errno));
1072 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001073 }
wdenk6aff3112002-12-17 01:51:00 +00001074
1075 if (mode == O_RDWR) {
wdenkd0fb80c2003-01-11 09:48:40 +00001076 if (HaveRedundEnv) {
1077 /* switch to next partition for writing */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001078 dev_target = !dev_current;
1079 /* dev_target: fd_target, erase_target */
1080 fd_target = open (DEVNAME (dev_target), mode);
1081 if (fd_target < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001082 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001083 "Can't open %s: %s\n",
1084 DEVNAME (dev_target),
1085 strerror (errno));
1086 rc = -1;
1087 goto exit;
wdenkd0fb80c2003-01-11 09:48:40 +00001088 }
1089 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001090 dev_target = dev_current;
1091 fd_target = fd_current;
wdenk6aff3112002-12-17 01:51:00 +00001092 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001093
1094 rc = flash_write (fd_current, fd_target, dev_target);
wdenk6aff3112002-12-17 01:51:00 +00001095
wdenkd0fb80c2003-01-11 09:48:40 +00001096 if (HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001097 if (close (fd_target)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001098 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001099 "I/O error on %s: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001100 DEVNAME (dev_target),
wdenk4a6fd342003-04-12 23:38:12 +00001101 strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001102 rc = -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001103 }
wdenk6aff3112002-12-17 01:51:00 +00001104 }
wdenk6aff3112002-12-17 01:51:00 +00001105 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001106 rc = flash_read (fd_current);
wdenk6aff3112002-12-17 01:51:00 +00001107 }
1108
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001109exit:
1110 if (close (fd_current)) {
wdenk6aff3112002-12-17 01:51:00 +00001111 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001112 "I/O error on %s: %s\n",
1113 DEVNAME (dev_current), strerror (errno));
1114 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001115 }
1116
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001117 return rc;
wdenk6aff3112002-12-17 01:51:00 +00001118}
1119
1120/*
1121 * s1 is either a simple 'name', or a 'name=value' pair.
1122 * s2 is a 'name=value' pair.
1123 * If the names match, return the value of s2, else NULL.
1124 */
1125
Markus Klotzbücher6de66b32007-11-27 10:23:20 +01001126static char *envmatch (char * s1, char * s2)
wdenk6aff3112002-12-17 01:51:00 +00001127{
Joe Hershberger586197d2012-10-03 09:38:50 +00001128 if (s1 == NULL || s2 == NULL)
1129 return NULL;
wdenk6aff3112002-12-17 01:51:00 +00001130
1131 while (*s1 == *s2++)
1132 if (*s1++ == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001133 return s2;
wdenk4a6fd342003-04-12 23:38:12 +00001134 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001135 return s2;
1136 return NULL;
wdenk6aff3112002-12-17 01:51:00 +00001137}
1138
1139/*
1140 * Prevent confusion if running from erased flash memory
1141 */
Stefano Babicbd7b26f2010-05-24 12:08:16 +02001142int fw_env_open(void)
wdenk6aff3112002-12-17 01:51:00 +00001143{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001144 int crc0, crc0_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001145 unsigned char flag0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001146 void *addr0;
wdenkd0fb80c2003-01-11 09:48:40 +00001147
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001148 int crc1, crc1_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001149 unsigned char flag1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001150 void *addr1;
1151
Marek Vasuta8a752c2014-03-05 19:59:52 +01001152 int ret;
1153
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001154 struct env_image_single *single;
1155 struct env_image_redundant *redundant;
wdenk6aff3112002-12-17 01:51:00 +00001156
wdenk4a6fd342003-04-12 23:38:12 +00001157 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001158 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001159
Joe Hershberger497f2052012-10-03 09:38:46 +00001160 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001161 if (addr0 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001162 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001163 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001164 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001165 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001166 }
wdenk4a6fd342003-04-12 23:38:12 +00001167
wdenkd0fb80c2003-01-11 09:48:40 +00001168 /* read environment from FLASH to local buffer */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001169 environment.image = addr0;
1170
1171 if (HaveRedundEnv) {
1172 redundant = addr0;
1173 environment.crc = &redundant->crc;
1174 environment.flags = &redundant->flags;
1175 environment.data = redundant->data;
1176 } else {
1177 single = addr0;
1178 environment.crc = &single->crc;
1179 environment.flags = NULL;
1180 environment.data = single->data;
wdenkd0fb80c2003-01-11 09:48:40 +00001181 }
wdenk4a6fd342003-04-12 23:38:12 +00001182
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001183 dev_current = 0;
1184 if (flash_io (O_RDONLY))
1185 return -1;
1186
1187 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001188
Andreas Fenkart371ee132015-12-09 13:13:24 +01001189 if (common_args.aes_flag) {
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +02001190 ret = env_aes_cbc_crypt(environment.data, 0,
1191 common_args.aes_key);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001192 if (ret)
1193 return ret;
1194 }
1195
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001196 crc0_ok = (crc0 == *environment.crc);
wdenkd0fb80c2003-01-11 09:48:40 +00001197 if (!HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001198 if (!crc0_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001199 fprintf (stderr,
1200 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +01001201 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +00001202 }
wdenkd0fb80c2003-01-11 09:48:40 +00001203 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001204 flag0 = *environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +00001205
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001206 dev_current = 1;
Joe Hershberger497f2052012-10-03 09:38:46 +00001207 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001208 if (addr1 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001209 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001210 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001211 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001212 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001213 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001214 redundant = addr1;
wdenk4a6fd342003-04-12 23:38:12 +00001215
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001216 /*
1217 * have to set environment.image for flash_read(), careful -
1218 * other pointers in environment still point inside addr0
1219 */
1220 environment.image = addr1;
1221 if (flash_io (O_RDONLY))
1222 return -1;
1223
1224 /* Check flag scheme compatibility */
1225 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1226 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1227 environment.flag_scheme = FLAG_BOOLEAN;
1228 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1229 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1230 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001231 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1232 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1233 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershberger785881f2013-04-08 10:32:52 +00001234 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1235 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1236 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metz23ef62d2013-08-30 00:56:01 +02001237 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1238 DEVTYPE(!dev_current) == MTD_ABSENT) {
1239 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001240 } else {
1241 fprintf (stderr, "Incompatible flash types!\n");
1242 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001243 }
wdenk4a6fd342003-04-12 23:38:12 +00001244
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001245 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001246
Andreas Fenkart371ee132015-12-09 13:13:24 +01001247 if (common_args.aes_flag) {
Andreas Fenkartdcdc1f62016-04-19 22:43:39 +02001248 ret = env_aes_cbc_crypt(redundant->data, 0,
1249 common_args.aes_key);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001250 if (ret)
1251 return ret;
1252 }
1253
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001254 crc1_ok = (crc1 == redundant->crc);
1255 flag1 = redundant->flags;
wdenk4a6fd342003-04-12 23:38:12 +00001256
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001257 if (crc0_ok && !crc1_ok) {
1258 dev_current = 0;
1259 } else if (!crc0_ok && crc1_ok) {
1260 dev_current = 1;
1261 } else if (!crc0_ok && !crc1_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001262 fprintf (stderr,
1263 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001264 memcpy (environment.data, default_environment,
1265 sizeof default_environment);
1266 dev_current = 0;
1267 } else {
1268 switch (environment.flag_scheme) {
1269 case FLAG_BOOLEAN:
1270 if (flag0 == active_flag &&
1271 flag1 == obsolete_flag) {
1272 dev_current = 0;
1273 } else if (flag0 == obsolete_flag &&
1274 flag1 == active_flag) {
1275 dev_current = 1;
1276 } else if (flag0 == flag1) {
1277 dev_current = 0;
1278 } else if (flag0 == 0xFF) {
1279 dev_current = 0;
1280 } else if (flag1 == 0xFF) {
1281 dev_current = 1;
1282 } else {
1283 dev_current = 0;
1284 }
1285 break;
1286 case FLAG_INCREMENTAL:
Jon Povey735eb0f2011-03-11 14:10:56 +09001287 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001288 dev_current = 1;
1289 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey735eb0f2011-03-11 14:10:56 +09001290 flag0 >= flag1)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001291 dev_current = 0;
Jon Povey735eb0f2011-03-11 14:10:56 +09001292 else /* flag1 > flag0 */
1293 dev_current = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001294 break;
1295 default:
1296 fprintf (stderr, "Unknown flag scheme %u \n",
1297 environment.flag_scheme);
1298 return -1;
1299 }
1300 }
1301
1302 /*
1303 * If we are reading, we don't need the flag and the CRC any
1304 * more, if we are writing, we will re-calculate CRC and update
1305 * flags before writing out
1306 */
1307 if (dev_current) {
1308 environment.image = addr1;
1309 environment.crc = &redundant->crc;
1310 environment.flags = &redundant->flags;
1311 environment.data = redundant->data;
1312 free (addr0);
1313 } else {
1314 environment.image = addr0;
1315 /* Other pointers are already set */
wdenk4a6fd342003-04-12 23:38:12 +00001316 free (addr1);
wdenk6aff3112002-12-17 01:51:00 +00001317 }
Joe Hershberger74620e12012-10-15 15:29:25 +00001318#ifdef DEBUG
1319 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1320#endif
wdenk6aff3112002-12-17 01:51:00 +00001321 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001322 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001323}
1324
1325
wdenk4a6fd342003-04-12 23:38:12 +00001326static int parse_config ()
wdenk6aff3112002-12-17 01:51:00 +00001327{
1328 struct stat st;
1329
wdenkd0fb80c2003-01-11 09:48:40 +00001330#if defined(CONFIG_FILE)
Anatolij Gustschin925c97c2016-04-29 22:00:11 +02001331 if (!common_args.config_file)
1332 common_args.config_file = CONFIG_FILE;
1333
wdenkd0fb80c2003-01-11 09:48:40 +00001334 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart371ee132015-12-09 13:13:24 +01001335 if (get_config(common_args.config_file)) {
1336 fprintf(stderr, "Cannot parse config file '%s': %m\n",
1337 common_args.config_file);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001338 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001339 }
wdenkd0fb80c2003-01-11 09:48:40 +00001340#else
Tom Rini229695f2014-03-28 12:03:33 -04001341 DEVNAME (0) = DEVICE1_NAME;
wdenk4a6fd342003-04-12 23:38:12 +00001342 DEVOFFSET (0) = DEVICE1_OFFSET;
1343 ENVSIZE (0) = ENV1_SIZE;
Dustin Byford23869bf2014-03-06 20:48:22 -08001344 /* Default values are: erase-size=env-size */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001345 DEVESIZE (0) = ENVSIZE (0);
Dustin Byford23869bf2014-03-06 20:48:22 -08001346 /* #sectors=env-size/erase-size (rounded up) */
1347 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001348#ifdef DEVICE1_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001349 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001350#endif
1351#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001352 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001353#endif
1354
wdenk6aff3112002-12-17 01:51:00 +00001355#ifdef HAVE_REDUND
Tom Rini229695f2014-03-28 12:03:33 -04001356 DEVNAME (1) = DEVICE2_NAME;
wdenk4a6fd342003-04-12 23:38:12 +00001357 DEVOFFSET (1) = DEVICE2_OFFSET;
1358 ENVSIZE (1) = ENV2_SIZE;
Dustin Byford23869bf2014-03-06 20:48:22 -08001359 /* Default values are: erase-size=env-size */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001360 DEVESIZE (1) = ENVSIZE (1);
Dustin Byford23869bf2014-03-06 20:48:22 -08001361 /* #sectors=env-size/erase-size (rounded up) */
1362 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001363#ifdef DEVICE2_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001364 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001365#endif
1366#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001367 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001368#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001369 HaveRedundEnv = 1;
wdenk6aff3112002-12-17 01:51:00 +00001370#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001371#endif
wdenk4a6fd342003-04-12 23:38:12 +00001372 if (stat (DEVNAME (0), &st)) {
1373 fprintf (stderr,
1374 "Cannot access MTD device %s: %s\n",
1375 DEVNAME (0), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001376 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001377 }
wdenk4a6fd342003-04-12 23:38:12 +00001378
1379 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1380 fprintf (stderr,
1381 "Cannot access MTD device %s: %s\n",
Wolfgang Denke2146b62005-09-25 01:20:38 +02001382 DEVNAME (1), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001383 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001384 }
wdenk6aff3112002-12-17 01:51:00 +00001385 return 0;
1386}
wdenkd0fb80c2003-01-11 09:48:40 +00001387
1388#if defined(CONFIG_FILE)
1389static int get_config (char *fname)
1390{
1391 FILE *fp;
1392 int i = 0;
1393 int rc;
1394 char dump[128];
Tom Rini229695f2014-03-28 12:03:33 -04001395 char *devname;
wdenkd0fb80c2003-01-11 09:48:40 +00001396
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001397 fp = fopen (fname, "r");
1398 if (fp == NULL)
1399 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001400
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001401 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001402 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001403 if (dump[0] == '#')
wdenkd0fb80c2003-01-11 09:48:40 +00001404 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001405
Tom Rini229695f2014-03-28 12:03:33 -04001406 rc = sscanf (dump, "%ms %lx %lx %lx %lx",
1407 &devname,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001408 &DEVOFFSET (i),
1409 &ENVSIZE (i),
1410 &DEVESIZE (i),
1411 &ENVSECTORS (i));
1412
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001413 if (rc < 3)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001414 continue;
1415
Tom Rini229695f2014-03-28 12:03:33 -04001416 DEVNAME(i) = devname;
1417
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001418 if (rc < 4)
1419 /* Assume the erase size is the same as the env-size */
1420 DEVESIZE(i) = ENVSIZE(i);
1421
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001422 if (rc < 5)
Dustin Byford23869bf2014-03-06 20:48:22 -08001423 /* Assume enough env sectors to cover the environment */
1424 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
wdenkd0fb80c2003-01-11 09:48:40 +00001425
1426 i++;
1427 }
wdenk4a6fd342003-04-12 23:38:12 +00001428 fclose (fp);
1429
wdenkd0fb80c2003-01-11 09:48:40 +00001430 HaveRedundEnv = i - 1;
wdenk4a6fd342003-04-12 23:38:12 +00001431 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +00001432 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001433 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001434 } else
1435 return 0;
1436}
1437#endif