blob: ba11f7727b8ecea8c3f64b7d169c29c4699bd0b7 [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
wdenk6aff3112002-12-17 01:51:00 +000013#include <errno.h>
Joe Hershberger30fd4fad2012-12-11 22:16:32 -060014#include <env_flags.h>
wdenk6aff3112002-12-17 01:51:00 +000015#include <fcntl.h>
Joe Hershberger06134212012-10-12 10:23:37 +000016#include <linux/stringify.h>
wdenk6aff3112002-12-17 01:51:00 +000017#include <stdio.h>
18#include <stdlib.h>
19#include <stddef.h>
20#include <string.h>
21#include <sys/types.h>
22#include <sys/ioctl.h>
23#include <sys/stat.h>
24#include <unistd.h>
wdenk6aff3112002-12-17 01:51:00 +000025
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010026#ifdef MTD_OLD
Wolfgang Denk1711f3b2008-09-02 21:17:36 +020027# include <stdint.h>
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010028# include <linux/mtd/mtd.h>
29#else
Wolfgang Denkc83d7ca2008-01-08 22:58:27 +010030# define __user /* nothing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +010031# include <mtd/mtd-user.h>
32#endif
33
34#include "fw_env.h"
wdenk6aff3112002-12-17 01:51:00 +000035
Marek Vasuta8a752c2014-03-05 19:59:52 +010036#include <aes.h>
37
38#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
39
Stefano Babicbd7b26f2010-05-24 12:08:16 +020040#define WHITESPACE(c) ((c == '\t') || (c == ' '))
wdenk6aff3112002-12-17 01:51:00 +000041
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020042#define min(x, y) ({ \
43 typeof(x) _min1 = (x); \
44 typeof(y) _min2 = (y); \
45 (void) (&_min1 == &_min2); \
46 _min1 < _min2 ? _min1 : _min2; })
47
48struct envdev_s {
Tom Rini229695f2014-03-28 12:03:33 -040049 const char *devname; /* Device name */
wdenk4a6fd342003-04-12 23:38:12 +000050 ulong devoff; /* Device offset */
51 ulong env_size; /* environment size */
52 ulong erase_size; /* device erase size */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020053 ulong env_sectors; /* number of environment sectors */
54 uint8_t mtd_type; /* type of the MTD device */
55};
wdenk6aff3112002-12-17 01:51:00 +000056
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020057static struct envdev_s envdevices[2] =
58{
59 {
60 .mtd_type = MTD_ABSENT,
61 }, {
62 .mtd_type = MTD_ABSENT,
63 },
64};
65static int dev_current;
wdenk6aff3112002-12-17 01:51:00 +000066
67#define DEVNAME(i) envdevices[(i)].devname
wdenkd0fb80c2003-01-11 09:48:40 +000068#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk6aff3112002-12-17 01:51:00 +000069#define ENVSIZE(i) envdevices[(i)].env_size
70#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020071#define ENVSECTORS(i) envdevices[(i)].env_sectors
72#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk6aff3112002-12-17 01:51:00 +000073
Joe Hershberger497f2052012-10-03 09:38:46 +000074#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk6aff3112002-12-17 01:51:00 +000075
wdenkd0fb80c2003-01-11 09:48:40 +000076#define ENV_SIZE getenvsize()
wdenk6aff3112002-12-17 01:51:00 +000077
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020078struct env_image_single {
79 uint32_t crc; /* CRC32 over data bytes */
80 char data[];
81};
wdenk6aff3112002-12-17 01:51:00 +000082
Guennadi Liakhovetski56086922008-09-04 13:01:49 +020083struct env_image_redundant {
84 uint32_t crc; /* CRC32 over data bytes */
85 unsigned char flags; /* active or obsolete */
86 char data[];
87};
88
89enum flag_scheme {
90 FLAG_NONE,
91 FLAG_BOOLEAN,
92 FLAG_INCREMENTAL,
93};
94
95struct environment {
96 void *image;
97 uint32_t *crc;
98 unsigned char *flags;
99 char *data;
100 enum flag_scheme flag_scheme;
101};
102
103static struct environment environment = {
104 .flag_scheme = FLAG_NONE,
105};
wdenk6aff3112002-12-17 01:51:00 +0000106
Marek Vasuta8a752c2014-03-05 19:59:52 +0100107/* Is AES encryption used? */
108static int aes_flag;
109static uint8_t aes_key[AES_KEY_LENGTH] = { 0 };
110static int env_aes_cbc_crypt(char *data, const int enc);
111
wdenkd0fb80c2003-01-11 09:48:40 +0000112static int HaveRedundEnv = 0;
113
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100114static unsigned char active_flag = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200115/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100116static unsigned char obsolete_flag = 0;
wdenkd0fb80c2003-01-11 09:48:40 +0000117
Joe Hershbergerddd84182012-10-12 08:48:51 +0000118#define DEFAULT_ENV_INSTANCE_STATIC
119#include <env_default.h>
wdenk6aff3112002-12-17 01:51:00 +0000120
wdenk4a6fd342003-04-12 23:38:12 +0000121static int flash_io (int mode);
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100122static char *envmatch (char * s1, char * s2);
wdenk4a6fd342003-04-12 23:38:12 +0000123static int parse_config (void);
124
wdenkd0fb80c2003-01-11 09:48:40 +0000125#if defined(CONFIG_FILE)
wdenk4a6fd342003-04-12 23:38:12 +0000126static int get_config (char *);
Michael Heimpold9884f442015-10-27 22:34:49 +0100127static char *config_file = CONFIG_FILE;
wdenkd0fb80c2003-01-11 09:48:40 +0000128#endif
wdenk4a6fd342003-04-12 23:38:12 +0000129static inline ulong getenvsize (void)
wdenkd0fb80c2003-01-11 09:48:40 +0000130{
Dominic Sacréea195272014-11-05 00:30:18 +0100131 ulong rc = CUR_ENVSIZE - sizeof(uint32_t);
wdenk4a6fd342003-04-12 23:38:12 +0000132
wdenkd0fb80c2003-01-11 09:48:40 +0000133 if (HaveRedundEnv)
wdenk4a6fd342003-04-12 23:38:12 +0000134 rc -= sizeof (char);
Marek Vasuta8a752c2014-03-05 19:59:52 +0100135
136 if (aes_flag)
137 rc &= ~(AES_KEY_LENGTH - 1);
138
wdenkd0fb80c2003-01-11 09:48:40 +0000139 return rc;
140}
wdenk6aff3112002-12-17 01:51:00 +0000141
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200142static char *fw_string_blank(char *s, int noblank)
143{
144 int i;
145 int len = strlen(s);
146
147 for (i = 0; i < len; i++, s++) {
148 if ((noblank && !WHITESPACE(*s)) ||
149 (!noblank && WHITESPACE(*s)))
150 break;
151 }
152 if (i == len)
153 return NULL;
154
155 return s;
156}
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
Marek Vasuta8a752c2014-03-05 19:59:52 +0100210static int parse_aes_key(char *key)
211{
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 }
232 aes_key[i] = ul & 0xff;
233 key += 2;
234 }
235 aes_flag = 1;
236
237 return 0;
238}
239
Joe Hershberger267541f2012-12-11 22:16:34 -0600240/*
wdenk6aff3112002-12-17 01:51:00 +0000241 * Print the current definition of one, or more, or all
242 * environment variables
243 */
Grant Ericksonbc117562008-05-06 20:16:15 -0700244int fw_printenv (int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000245{
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100246 char *env, *nxt;
wdenk6aff3112002-12-17 01:51:00 +0000247 int i, n_flag;
Grant Ericksonbc117562008-05-06 20:16:15 -0700248 int rc = 0;
wdenk6aff3112002-12-17 01:51:00 +0000249
Michael Heimpold9884f442015-10-27 22:34:49 +0100250#ifdef CONFIG_FILE
251 if (argc >= 2 && strcmp(argv[1], "-c") == 0) {
252 if (argc < 3) {
253 fprintf(stderr,
254 "## Error: '-c' option requires the config file to use\n");
255 return -1;
256 }
257 config_file = argv[2];
258 argv += 2;
259 argc -= 2;
260 }
261#endif
262
Marek Vasuta8a752c2014-03-05 19:59:52 +0100263 if (argc >= 2 && strcmp(argv[1], "-a") == 0) {
264 if (argc < 3) {
265 fprintf(stderr,
266 "## Error: '-a' option requires AES key\n");
267 return -1;
268 }
269 rc = parse_aes_key(argv[2]);
270 if (rc)
271 return rc;
272 argv += 2;
273 argc -= 2;
274 }
275
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200276 if (fw_env_open())
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200277 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000278
wdenk4a6fd342003-04-12 23:38:12 +0000279 if (argc == 1) { /* Print all env variables */
280 for (env = environment.data; *env; env = nxt + 1) {
281 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000282 if (nxt >= &environment.data[ENV_SIZE]) {
283 fprintf (stderr, "## Error: "
284 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200285 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000286 }
287 }
288
wdenk4a6fd342003-04-12 23:38:12 +0000289 printf ("%s\n", env);
wdenk6aff3112002-12-17 01:51:00 +0000290 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200291 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000292 }
293
wdenk4a6fd342003-04-12 23:38:12 +0000294 if (strcmp (argv[1], "-n") == 0) {
wdenk6aff3112002-12-17 01:51:00 +0000295 n_flag = 1;
296 ++argv;
297 --argc;
298 if (argc != 2) {
299 fprintf (stderr, "## Error: "
300 "`-n' option requires exactly one argument\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200301 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000302 }
303 } else {
304 n_flag = 0;
305 }
306
wdenk4a6fd342003-04-12 23:38:12 +0000307 for (i = 1; i < argc; ++i) { /* print single env variables */
Markus Klotzbücher6de66b32007-11-27 10:23:20 +0100308 char *name = argv[i];
309 char *val = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000310
wdenk4a6fd342003-04-12 23:38:12 +0000311 for (env = environment.data; *env; env = nxt + 1) {
wdenk6aff3112002-12-17 01:51:00 +0000312
wdenk4a6fd342003-04-12 23:38:12 +0000313 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000314 if (nxt >= &environment.data[ENV_SIZE]) {
315 fprintf (stderr, "## Error: "
316 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200317 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000318 }
319 }
wdenk4a6fd342003-04-12 23:38:12 +0000320 val = envmatch (name, env);
wdenk6aff3112002-12-17 01:51:00 +0000321 if (val) {
322 if (!n_flag) {
323 fputs (name, stdout);
wdenk4a6fd342003-04-12 23:38:12 +0000324 putc ('=', stdout);
wdenk6aff3112002-12-17 01:51:00 +0000325 }
wdenk4a6fd342003-04-12 23:38:12 +0000326 puts (val);
wdenk6aff3112002-12-17 01:51:00 +0000327 break;
328 }
329 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700330 if (!val) {
wdenk4a6fd342003-04-12 23:38:12 +0000331 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Ericksonbc117562008-05-06 20:16:15 -0700332 rc = -1;
333 }
wdenk6aff3112002-12-17 01:51:00 +0000334 }
Grant Ericksonbc117562008-05-06 20:16:15 -0700335
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200336 return rc;
wdenk6aff3112002-12-17 01:51:00 +0000337}
338
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200339int fw_env_close(void)
wdenk6aff3112002-12-17 01:51:00 +0000340{
Marek Vasuta8a752c2014-03-05 19:59:52 +0100341 int ret;
342 if (aes_flag) {
343 ret = env_aes_cbc_crypt(environment.data, 1);
344 if (ret) {
345 fprintf(stderr,
346 "Error: can't encrypt env for flash\n");
347 return ret;
348 }
349 }
350
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200351 /*
352 * Update CRC
353 */
354 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk6aff3112002-12-17 01:51:00 +0000355
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200356 /* write environment back to flash */
357 if (flash_io(O_RDWR)) {
358 fprintf(stderr,
359 "Error: can't write fw_env to flash\n");
360 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000361 }
362
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200363 return 0;
364}
wdenk6aff3112002-12-17 01:51:00 +0000365
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200366
367/*
368 * Set/Clear a single variable in the environment.
369 * This is called in sequence to update the environment
370 * in RAM without updating the copy in flash after each set
371 */
372int fw_env_write(char *name, char *value)
373{
374 int len;
375 char *env, *nxt;
376 char *oldval = NULL;
Joe Hershberger267541f2012-12-11 22:16:34 -0600377 int deleting, creating, overwriting;
wdenk6aff3112002-12-17 01:51:00 +0000378
379 /*
380 * search if variable with this name already exists
381 */
wdenk4a6fd342003-04-12 23:38:12 +0000382 for (nxt = env = environment.data; *env; env = nxt + 1) {
383 for (nxt = env; *nxt; ++nxt) {
wdenk6aff3112002-12-17 01:51:00 +0000384 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200385 fprintf(stderr, "## Error: "
wdenk6aff3112002-12-17 01:51:00 +0000386 "environment not terminated\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200387 errno = EINVAL;
388 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000389 }
390 }
wdenk4a6fd342003-04-12 23:38:12 +0000391 if ((oldval = envmatch (name, env)) != NULL)
wdenk6aff3112002-12-17 01:51:00 +0000392 break;
393 }
394
Joe Hershberger267541f2012-12-11 22:16:34 -0600395 deleting = (oldval && !(value && strlen(value)));
396 creating = (!oldval && (value && strlen(value)));
397 overwriting = (oldval && (value && strlen(value)));
398
399 /* check for permission */
400 if (deleting) {
401 if (env_flags_validate_varaccess(name,
402 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
403 printf("Can't delete \"%s\"\n", name);
404 errno = EROFS;
405 return -1;
406 }
407 } else if (overwriting) {
408 if (env_flags_validate_varaccess(name,
409 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
410 printf("Can't overwrite \"%s\"\n", name);
411 errno = EROFS;
412 return -1;
413 } else if (env_flags_validate_varaccess(name,
414 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
415 const char *defval = fw_getdefenv(name);
416
417 if (defval == NULL)
418 defval = "";
419 if (strcmp(oldval, defval)
420 != 0) {
421 printf("Can't overwrite \"%s\"\n", name);
422 errno = EROFS;
423 return -1;
424 }
425 }
426 } else if (creating) {
427 if (env_flags_validate_varaccess(name,
428 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
429 printf("Can't create \"%s\"\n", name);
430 errno = EROFS;
431 return -1;
432 }
433 } else
434 /* Nothing to do */
435 return 0;
436
437 if (deleting || overwriting) {
wdenk6aff3112002-12-17 01:51:00 +0000438 if (*++nxt == '\0') {
439 *env = '\0';
440 } else {
441 for (;;) {
442 *env = *nxt++;
443 if ((*env == '\0') && (*nxt == '\0'))
444 break;
445 ++env;
446 }
447 }
448 *++env = '\0';
449 }
450
451 /* Delete only ? */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200452 if (!value || !strlen(value))
453 return 0;
wdenk6aff3112002-12-17 01:51:00 +0000454
455 /*
456 * Append new definition at the end
457 */
wdenk4a6fd342003-04-12 23:38:12 +0000458 for (env = environment.data; *env || *(env + 1); ++env);
wdenk6aff3112002-12-17 01:51:00 +0000459 if (env > environment.data)
460 ++env;
461 /*
462 * Overflow when:
Joe Hershberger497f2052012-10-03 09:38:46 +0000463 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk6aff3112002-12-17 01:51:00 +0000464 */
wdenk4a6fd342003-04-12 23:38:12 +0000465 len = strlen (name) + 2;
wdenk6aff3112002-12-17 01:51:00 +0000466 /* add '=' for first arg, ' ' for all others */
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200467 len += strlen(value) + 1;
468
wdenk4a6fd342003-04-12 23:38:12 +0000469 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk6aff3112002-12-17 01:51:00 +0000470 fprintf (stderr,
471 "Error: environment overflow, \"%s\" deleted\n",
472 name);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200473 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000474 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200475
wdenk6aff3112002-12-17 01:51:00 +0000476 while ((*env = *name++) != '\0')
477 env++;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200478 *env = '=';
479 while ((*++env = *value++) != '\0')
480 ;
wdenk6aff3112002-12-17 01:51:00 +0000481
482 /* end is marked with double '\0' */
483 *++env = '\0';
484
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200485 return 0;
486}
wdenk6aff3112002-12-17 01:51:00 +0000487
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200488/*
489 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
490 * 0 - OK
491 * EINVAL - need at least 1 argument
492 * EROFS - certain variables ("ethaddr", "serial#") cannot be
493 * modified or deleted
494 *
495 */
496int fw_setenv(int argc, char *argv[])
497{
Marek Vasuta8a752c2014-03-05 19:59:52 +0100498 int i, rc;
Mike Frysinger3779c8e2012-11-10 19:47:45 +0000499 size_t len;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200500 char *name;
501 char *value = NULL;
wdenk6aff3112002-12-17 01:51:00 +0000502
Michael Heimpold9884f442015-10-27 22:34:49 +0100503#ifdef CONFIG_FILE
504 if (argc >= 2 && strcmp(argv[1], "-c") == 0) {
505 if (argc < 3) {
506 fprintf(stderr,
507 "## Error: '-c' option requires the config file to use\n");
508 return -1;
509 }
510 config_file = argv[2];
511 argv += 2;
512 argc -= 2;
513 }
514#endif
515
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200516 if (argc < 2) {
517 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200518 return -1;
wdenk6aff3112002-12-17 01:51:00 +0000519 }
520
Marek Vasuta8a752c2014-03-05 19:59:52 +0100521 if (strcmp(argv[1], "-a") == 0) {
522 if (argc < 3) {
523 fprintf(stderr,
524 "## Error: '-a' option requires AES key\n");
525 return -1;
526 }
527 rc = parse_aes_key(argv[2]);
528 if (rc)
529 return rc;
530 argv += 2;
531 argc -= 2;
532 }
533
534 if (argc < 2) {
535 errno = EINVAL;
536 return -1;
537 }
538
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200539 if (fw_env_open()) {
540 fprintf(stderr, "Error: environment not initialized\n");
541 return -1;
542 }
543
544 name = argv[1];
545
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600546 if (env_flags_validate_env_set_params(argc, argv) < 0)
547 return 1;
548
Joe Hershberger62a34a02012-10-03 09:38:47 +0000549 len = 0;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200550 for (i = 2; i < argc; ++i) {
551 char *val = argv[i];
Joe Hershberger62a34a02012-10-03 09:38:47 +0000552 size_t val_len = strlen(val);
553
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000554 if (value)
555 value[len - 1] = ' ';
Joe Hershberger62a34a02012-10-03 09:38:47 +0000556 value = realloc(value, len + val_len + 1);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200557 if (!value) {
Joe Hershberger62a34a02012-10-03 09:38:47 +0000558 fprintf(stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200559 "Cannot malloc %zu bytes: %s\n",
Joe Hershberger62a34a02012-10-03 09:38:47 +0000560 len, strerror(errno));
561 return -1;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200562 }
Joe Hershberger62a34a02012-10-03 09:38:47 +0000563
564 memcpy(value + len, val, val_len);
565 len += val_len;
Joe Hershbergerce2f5802012-10-15 15:29:24 +0000566 value[len++] = '\0';
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200567 }
568
569 fw_env_write(name, value);
570
Joe Hershberger62a34a02012-10-03 09:38:47 +0000571 free(value);
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200572
573 return fw_env_close();
574}
575
576/*
577 * Parse a file and configure the u-boot variables.
578 * The script file has a very simple format, as follows:
579 *
580 * Each line has a couple with name, value:
581 * <white spaces>variable_name<white spaces>variable_value
582 *
583 * Both variable_name and variable_value are interpreted as strings.
584 * Any character after <white spaces> and before ending \r\n is interpreted
585 * as variable's value (no comment allowed on these lines !)
586 *
587 * Comments are allowed if the first character in the line is #
588 *
589 * Returns -1 and sets errno error codes:
590 * 0 - OK
591 * -1 - Error
592 */
593int fw_parse_script(char *fname)
594{
595 FILE *fp;
596 char dump[1024]; /* Maximum line length in the file */
597 char *name;
598 char *val;
599 int lineno = 0;
600 int len;
601 int ret = 0;
602
603 if (fw_env_open()) {
604 fprintf(stderr, "Error: environment not initialized\n");
605 return -1;
606 }
607
608 if (strcmp(fname, "-") == 0)
609 fp = stdin;
610 else {
611 fp = fopen(fname, "r");
612 if (fp == NULL) {
613 fprintf(stderr, "I cannot open %s for reading\n",
614 fname);
615 return -1;
616 }
617 }
618
619 while (fgets(dump, sizeof(dump), fp)) {
620 lineno++;
621 len = strlen(dump);
622
623 /*
624 * Read a whole line from the file. If the line is too long
625 * or is not terminated, reports an error and exit.
626 */
627 if (dump[len - 1] != '\n') {
628 fprintf(stderr,
629 "Line %d not corrected terminated or too long\n",
630 lineno);
631 ret = -1;
632 break;
633 }
634
635 /* Drop ending line feed / carriage return */
636 while (len > 0 && (dump[len - 1] == '\n' ||
637 dump[len - 1] == '\r')) {
638 dump[len - 1] = '\0';
639 len--;
640 }
641
642 /* Skip comment or empty lines */
643 if ((len == 0) || dump[0] == '#')
644 continue;
645
646 /*
647 * Search for variable's name,
648 * remove leading whitespaces
649 */
650 name = fw_string_blank(dump, 1);
651 if (!name)
652 continue;
653
654 /* The first white space is the end of variable name */
655 val = fw_string_blank(name, 0);
656 len = strlen(name);
657 if (val) {
658 *val++ = '\0';
659 if ((val - name) < len)
660 val = fw_string_blank(val, 1);
661 else
662 val = NULL;
663 }
664
665#ifdef DEBUG
666 fprintf(stderr, "Setting %s : %s\n",
667 name, val ? val : " removed");
668#endif
669
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600670 if (env_flags_validate_type(name, val) < 0) {
671 ret = -1;
672 break;
673 }
674
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200675 /*
676 * If there is an error setting a variable,
677 * try to save the environment and returns an error
678 */
679 if (fw_env_write(name, val)) {
680 fprintf(stderr,
681 "fw_env_write returns with error : %s\n",
682 strerror(errno));
683 ret = -1;
684 break;
685 }
686
687 }
688
689 /* Close file if not stdin */
690 if (strcmp(fname, "-") != 0)
691 fclose(fp);
692
693 ret |= fw_env_close();
694
695 return ret;
696
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200697}
698
699/*
700 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
701 * 0 - block is good
702 * > 0 - block is bad
703 * < 0 - failed to test
704 */
705static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
706{
707 if (mtd_type == MTD_NANDFLASH) {
708 int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
709
710 if (badblock < 0) {
711 perror ("Cannot read bad block mark");
712 return badblock;
713 }
714
715 if (badblock) {
716#ifdef DEBUG
717 fprintf (stderr, "Bad block at 0x%llx, "
718 "skipping\n", *blockstart);
719#endif
720 return badblock;
721 }
722 }
723
724 return 0;
725}
726
727/*
728 * Read data from flash at an offset into a provided buffer. On NAND it skips
729 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
730 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
731 */
732static int flash_read_buf (int dev, int fd, void *buf, size_t count,
733 off_t offset, uint8_t mtd_type)
734{
735 size_t blocklen; /* erase / write length - one block on NAND,
736 0 on NOR */
737 size_t processed = 0; /* progress counter */
738 size_t readlen = count; /* current read length */
739 off_t top_of_range; /* end of the last block we may use */
740 off_t block_seek; /* offset inside the current block to the start
741 of the data */
742 loff_t blockstart; /* running start of the current block -
743 MEMGETBADBLOCK needs 64 bits */
744 int rc;
745
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100746 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200747
748 /* Offset inside a block */
749 block_seek = offset - blockstart;
750
751 if (mtd_type == MTD_NANDFLASH) {
752 /*
753 * NAND: calculate which blocks we are reading. We have
754 * to read one block at a time to skip bad blocks.
755 */
756 blocklen = DEVESIZE (dev);
757
758 /*
759 * To calculate the top of the range, we have to use the
760 * global DEVOFFSET (dev), which can be different from offset
761 */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100762 top_of_range = ((DEVOFFSET(dev) / blocklen) +
763 ENVSECTORS (dev)) * blocklen;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200764
765 /* Limit to one block for the first read */
766 if (readlen > blocklen - block_seek)
767 readlen = blocklen - block_seek;
768 } else {
769 blocklen = 0;
770 top_of_range = offset + count;
771 }
772
773 /* This only runs once on NOR flash */
774 while (processed < count) {
775 rc = flash_bad_block (fd, mtd_type, &blockstart);
776 if (rc < 0) /* block test failed */
777 return -1;
778
779 if (blockstart + block_seek + readlen > top_of_range) {
780 /* End of range is reached */
781 fprintf (stderr,
782 "Too few good blocks within range\n");
783 return -1;
784 }
785
786 if (rc) { /* block is bad */
787 blockstart += blocklen;
788 continue;
789 }
790
791 /*
792 * If a block is bad, we retry in the next block at the same
793 * offset - see common/env_nand.c::writeenv()
794 */
795 lseek (fd, blockstart + block_seek, SEEK_SET);
796
797 rc = read (fd, buf + processed, readlen);
798 if (rc != readlen) {
799 fprintf (stderr, "Read error on %s: %s\n",
800 DEVNAME (dev), strerror (errno));
801 return -1;
802 }
803#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000804 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
805 rc, blockstart + block_seek, DEVNAME(dev));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200806#endif
807 processed += readlen;
808 readlen = min (blocklen, count - processed);
809 block_seek = 0;
810 blockstart += blocklen;
811 }
812
813 return processed;
814}
815
816/*
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100817 * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
818 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
819 * erase and write the whole data at once.
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200820 */
821static int flash_write_buf (int dev, int fd, void *buf, size_t count,
822 off_t offset, uint8_t mtd_type)
823{
824 void *data;
825 struct erase_info_user erase;
826 size_t blocklen; /* length of NAND block / NOR erase sector */
827 size_t erase_len; /* whole area that can be erased - may include
828 bad blocks */
829 size_t erasesize; /* erase / write length - one block on NAND,
830 whole area on NOR */
831 size_t processed = 0; /* progress counter */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100832 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200833 bad blocks */
834 off_t erase_offset; /* offset to the first erase block (aligned)
835 below offset */
836 off_t block_seek; /* offset inside the erase block to the start
837 of the data */
838 off_t top_of_range; /* end of the last block we may use */
839 loff_t blockstart; /* running start of the current block -
840 MEMGETBADBLOCK needs 64 bits */
841 int rc;
842
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200843 /*
Oliver Metze387efb2013-08-30 00:56:02 +0200844 * For mtd devices only offset and size of the environment do matter
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200845 */
Oliver Metze387efb2013-08-30 00:56:02 +0200846 if (mtd_type == MTD_ABSENT) {
847 blocklen = count;
848 top_of_range = offset + count;
849 erase_len = blocklen;
850 blockstart = offset;
851 block_seek = 0;
852 write_total = blocklen;
853 } else {
854 blocklen = DEVESIZE(dev);
855
856 top_of_range = ((DEVOFFSET(dev) / blocklen) +
857 ENVSECTORS(dev)) * blocklen;
858
859 erase_offset = (offset / blocklen) * blocklen;
860
861 /* Maximum area we may use */
862 erase_len = top_of_range - erase_offset;
863
864 blockstart = erase_offset;
865 /* Offset inside a block */
866 block_seek = offset - erase_offset;
867
868 /*
869 * Data size we actually write: from the start of the block
870 * to the start of the data, then count bytes of data, and
871 * to the end of the block
872 */
873 write_total = ((block_seek + count + blocklen - 1) /
874 blocklen) * blocklen;
875 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200876
877 /*
878 * Support data anywhere within erase sectors: read out the complete
879 * area to be erased, replace the environment image, write the whole
880 * block back again.
881 */
882 if (write_total > count) {
883 data = malloc (erase_len);
884 if (!data) {
885 fprintf (stderr,
Luka Perkov8603b692011-09-05 23:40:13 +0200886 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200887 erase_len, strerror (errno));
888 return -1;
889 }
890
891 rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
892 mtd_type);
893 if (write_total != rc)
894 return -1;
895
Joe Hershberger74620e12012-10-15 15:29:25 +0000896#ifdef DEBUG
897 fprintf(stderr, "Preserving data ");
898 if (block_seek != 0)
899 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
900 if (block_seek + count != write_total) {
901 if (block_seek != 0)
902 fprintf(stderr, " and ");
903 fprintf(stderr, "0x%lx - 0x%x",
904 block_seek + count, write_total - 1);
905 }
906 fprintf(stderr, "\n");
907#endif
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200908 /* Overwrite the old environment */
909 memcpy (data + block_seek, buf, count);
910 } else {
911 /*
912 * We get here, iff offset is block-aligned and count is a
913 * multiple of blocklen - see write_total calculation above
914 */
915 data = buf;
916 }
917
918 if (mtd_type == MTD_NANDFLASH) {
919 /*
920 * NAND: calculate which blocks we are writing. We have
921 * to write one block at a time to skip bad blocks.
922 */
923 erasesize = blocklen;
924 } else {
925 erasesize = erase_len;
926 }
927
928 erase.length = erasesize;
929
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +0100930 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200931 while (processed < write_total) {
932 rc = flash_bad_block (fd, mtd_type, &blockstart);
933 if (rc < 0) /* block test failed */
934 return rc;
935
936 if (blockstart + erasesize > top_of_range) {
937 fprintf (stderr, "End of range reached, aborting\n");
938 return -1;
939 }
940
941 if (rc) { /* block is bad */
942 blockstart += blocklen;
943 continue;
944 }
945
Oliver Metze387efb2013-08-30 00:56:02 +0200946 if (mtd_type != MTD_ABSENT) {
947 erase.start = blockstart;
948 ioctl(fd, MEMUNLOCK, &erase);
949 /* These do not need an explicit erase cycle */
950 if (mtd_type != MTD_DATAFLASH)
951 if (ioctl(fd, MEMERASE, &erase) != 0) {
952 fprintf(stderr,
953 "MTD erase error on %s: %s\n",
954 DEVNAME(dev), strerror(errno));
955 return -1;
956 }
957 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200958
959 if (lseek (fd, blockstart, SEEK_SET) == -1) {
960 fprintf (stderr,
961 "Seek error on %s: %s\n",
962 DEVNAME (dev), strerror (errno));
963 return -1;
964 }
965
966#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +0000967 fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
968 blockstart);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200969#endif
970 if (write (fd, data + processed, erasesize) != erasesize) {
971 fprintf (stderr, "Write error on %s: %s\n",
972 DEVNAME (dev), strerror (errno));
973 return -1;
974 }
975
Oliver Metze387efb2013-08-30 00:56:02 +0200976 if (mtd_type != MTD_ABSENT)
977 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200978
Dustin Byford4b774ff2014-03-06 20:48:23 -0800979 processed += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200980 block_seek = 0;
Dustin Byford4b774ff2014-03-06 20:48:23 -0800981 blockstart += erasesize;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200982 }
983
984 if (write_total > count)
985 free (data);
986
987 return processed;
988}
989
990/*
991 * Set obsolete flag at offset - NOR flash only
992 */
993static int flash_flag_obsolete (int dev, int fd, off_t offset)
994{
995 int rc;
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200996 struct erase_info_user erase;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +0200997
Detlev Zundel0aef7bc2010-07-30 11:22:15 +0200998 erase.start = DEVOFFSET (dev);
999 erase.length = DEVESIZE (dev);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001000 /* This relies on the fact, that obsolete_flag == 0 */
1001 rc = lseek (fd, offset, SEEK_SET);
1002 if (rc < 0) {
1003 fprintf (stderr, "Cannot seek to set the flag on %s \n",
1004 DEVNAME (dev));
1005 return rc;
1006 }
Detlev Zundel0aef7bc2010-07-30 11:22:15 +02001007 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001008 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel0aef7bc2010-07-30 11:22:15 +02001009 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001010 if (rc < 0)
1011 perror ("Could not set obsolete flag");
1012
1013 return rc;
1014}
1015
Marek Vasuta8a752c2014-03-05 19:59:52 +01001016/* Encrypt or decrypt the environment before writing or reading it. */
1017static int env_aes_cbc_crypt(char *payload, const int enc)
1018{
1019 uint8_t *data = (uint8_t *)payload;
1020 const int len = getenvsize();
1021 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
1022 uint32_t aes_blocks;
1023
1024 /* First we expand the key. */
1025 aes_expand_key(aes_key, key_exp);
1026
1027 /* Calculate the number of AES blocks to encrypt. */
1028 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
1029
1030 if (enc)
1031 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
1032 else
1033 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
1034
1035 return 0;
1036}
1037
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001038static int flash_write (int fd_current, int fd_target, int dev_target)
1039{
1040 int rc;
1041
1042 switch (environment.flag_scheme) {
1043 case FLAG_NONE:
1044 break;
1045 case FLAG_INCREMENTAL:
1046 (*environment.flags)++;
1047 break;
1048 case FLAG_BOOLEAN:
1049 *environment.flags = active_flag;
1050 break;
1051 default:
1052 fprintf (stderr, "Unimplemented flash scheme %u \n",
1053 environment.flag_scheme);
1054 return -1;
1055 }
1056
1057#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +00001058 fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001059 DEVOFFSET (dev_target), DEVNAME (dev_target));
1060#endif
Marek Vasuta8a752c2014-03-05 19:59:52 +01001061
Joe Hershberger497f2052012-10-03 09:38:46 +00001062 rc = flash_write_buf(dev_target, fd_target, environment.image,
1063 CUR_ENVSIZE, DEVOFFSET(dev_target),
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001064 DEVTYPE(dev_target));
1065 if (rc < 0)
1066 return rc;
1067
1068 if (environment.flag_scheme == FLAG_BOOLEAN) {
1069 /* Have to set obsolete flag */
1070 off_t offset = DEVOFFSET (dev_current) +
1071 offsetof (struct env_image_redundant, flags);
1072#ifdef DEBUG
Joe Hershberger74620e12012-10-15 15:29:25 +00001073 fprintf(stderr,
1074 "Setting obsolete flag in environment at 0x%lx on %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001075 DEVOFFSET (dev_current), DEVNAME (dev_current));
1076#endif
1077 flash_flag_obsolete (dev_current, fd_current, offset);
1078 }
1079
1080 return 0;
1081}
1082
1083static int flash_read (int fd)
1084{
1085 struct mtd_info_user mtdinfo;
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001086 struct stat st;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001087 int rc;
1088
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001089 rc = fstat(fd, &st);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001090 if (rc < 0) {
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001091 fprintf(stderr, "Cannot stat the file %s\n",
1092 DEVNAME(dev_current));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001093 return -1;
1094 }
1095
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001096 if (S_ISCHR(st.st_mode)) {
1097 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1098 if (rc < 0) {
1099 fprintf(stderr, "Cannot get MTD information for %s\n",
1100 DEVNAME(dev_current));
1101 return -1;
1102 }
1103 if (mtdinfo.type != MTD_NORFLASH &&
1104 mtdinfo.type != MTD_NANDFLASH &&
Joe Hershberger2b744332013-04-08 10:32:51 +00001105 mtdinfo.type != MTD_DATAFLASH &&
1106 mtdinfo.type != MTD_UBIVOLUME) {
Lubomir Rintelf1932b72013-02-10 01:02:36 +00001107 fprintf (stderr, "Unsupported flash type %u on %s\n",
1108 mtdinfo.type, DEVNAME(dev_current));
1109 return -1;
1110 }
1111 } else {
1112 memset(&mtdinfo, 0, sizeof(mtdinfo));
1113 mtdinfo.type = MTD_ABSENT;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001114 }
1115
1116 DEVTYPE(dev_current) = mtdinfo.type;
1117
Joe Hershberger497f2052012-10-03 09:38:46 +00001118 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001119 DEVOFFSET (dev_current), mtdinfo.type);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001120 if (rc != CUR_ENVSIZE)
1121 return -1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001122
Marek Vasuta8a752c2014-03-05 19:59:52 +01001123 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001124}
1125
1126static int flash_io (int mode)
1127{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001128 int fd_current, fd_target, rc, dev_target;
wdenk6aff3112002-12-17 01:51:00 +00001129
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001130 /* dev_current: fd_current, erase_current */
1131 fd_current = open (DEVNAME (dev_current), mode);
1132 if (fd_current < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001133 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001134 "Can't open %s: %s\n",
1135 DEVNAME (dev_current), strerror (errno));
1136 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001137 }
wdenk6aff3112002-12-17 01:51:00 +00001138
1139 if (mode == O_RDWR) {
wdenkd0fb80c2003-01-11 09:48:40 +00001140 if (HaveRedundEnv) {
1141 /* switch to next partition for writing */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001142 dev_target = !dev_current;
1143 /* dev_target: fd_target, erase_target */
1144 fd_target = open (DEVNAME (dev_target), mode);
1145 if (fd_target < 0) {
wdenk4a6fd342003-04-12 23:38:12 +00001146 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001147 "Can't open %s: %s\n",
1148 DEVNAME (dev_target),
1149 strerror (errno));
1150 rc = -1;
1151 goto exit;
wdenkd0fb80c2003-01-11 09:48:40 +00001152 }
1153 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001154 dev_target = dev_current;
1155 fd_target = fd_current;
wdenk6aff3112002-12-17 01:51:00 +00001156 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001157
1158 rc = flash_write (fd_current, fd_target, dev_target);
wdenk6aff3112002-12-17 01:51:00 +00001159
wdenkd0fb80c2003-01-11 09:48:40 +00001160 if (HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001161 if (close (fd_target)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001162 fprintf (stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001163 "I/O error on %s: %s\n",
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001164 DEVNAME (dev_target),
wdenk4a6fd342003-04-12 23:38:12 +00001165 strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001166 rc = -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001167 }
wdenk6aff3112002-12-17 01:51:00 +00001168 }
wdenk6aff3112002-12-17 01:51:00 +00001169 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001170 rc = flash_read (fd_current);
wdenk6aff3112002-12-17 01:51:00 +00001171 }
1172
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001173exit:
1174 if (close (fd_current)) {
wdenk6aff3112002-12-17 01:51:00 +00001175 fprintf (stderr,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001176 "I/O error on %s: %s\n",
1177 DEVNAME (dev_current), strerror (errno));
1178 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001179 }
1180
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001181 return rc;
wdenk6aff3112002-12-17 01:51:00 +00001182}
1183
1184/*
1185 * s1 is either a simple 'name', or a 'name=value' pair.
1186 * s2 is a 'name=value' pair.
1187 * If the names match, return the value of s2, else NULL.
1188 */
1189
Markus Klotzbücher6de66b32007-11-27 10:23:20 +01001190static char *envmatch (char * s1, char * s2)
wdenk6aff3112002-12-17 01:51:00 +00001191{
Joe Hershberger586197d2012-10-03 09:38:50 +00001192 if (s1 == NULL || s2 == NULL)
1193 return NULL;
wdenk6aff3112002-12-17 01:51:00 +00001194
1195 while (*s1 == *s2++)
1196 if (*s1++ == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001197 return s2;
wdenk4a6fd342003-04-12 23:38:12 +00001198 if (*s1 == '\0' && *(s2 - 1) == '=')
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001199 return s2;
1200 return NULL;
wdenk6aff3112002-12-17 01:51:00 +00001201}
1202
1203/*
1204 * Prevent confusion if running from erased flash memory
1205 */
Stefano Babicbd7b26f2010-05-24 12:08:16 +02001206int fw_env_open(void)
wdenk6aff3112002-12-17 01:51:00 +00001207{
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001208 int crc0, crc0_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001209 unsigned char flag0;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001210 void *addr0;
wdenkd0fb80c2003-01-11 09:48:40 +00001211
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001212 int crc1, crc1_ok;
Jon Povey735eb0f2011-03-11 14:10:56 +09001213 unsigned char flag1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001214 void *addr1;
1215
Marek Vasuta8a752c2014-03-05 19:59:52 +01001216 int ret;
1217
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001218 struct env_image_single *single;
1219 struct env_image_redundant *redundant;
wdenk6aff3112002-12-17 01:51:00 +00001220
wdenk4a6fd342003-04-12 23:38:12 +00001221 if (parse_config ()) /* should fill envdevices */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001222 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001223
Joe Hershberger497f2052012-10-03 09:38:46 +00001224 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001225 if (addr0 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001226 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001227 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001228 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001229 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001230 }
wdenk4a6fd342003-04-12 23:38:12 +00001231
wdenkd0fb80c2003-01-11 09:48:40 +00001232 /* read environment from FLASH to local buffer */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001233 environment.image = addr0;
1234
1235 if (HaveRedundEnv) {
1236 redundant = addr0;
1237 environment.crc = &redundant->crc;
1238 environment.flags = &redundant->flags;
1239 environment.data = redundant->data;
1240 } else {
1241 single = addr0;
1242 environment.crc = &single->crc;
1243 environment.flags = NULL;
1244 environment.data = single->data;
wdenkd0fb80c2003-01-11 09:48:40 +00001245 }
wdenk4a6fd342003-04-12 23:38:12 +00001246
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001247 dev_current = 0;
1248 if (flash_io (O_RDONLY))
1249 return -1;
1250
1251 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001252
1253 if (aes_flag) {
1254 ret = env_aes_cbc_crypt(environment.data, 0);
1255 if (ret)
1256 return ret;
1257 }
1258
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001259 crc0_ok = (crc0 == *environment.crc);
wdenkd0fb80c2003-01-11 09:48:40 +00001260 if (!HaveRedundEnv) {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001261 if (!crc0_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001262 fprintf (stderr,
1263 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkf07217c2006-03-12 23:27:46 +01001264 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk6aff3112002-12-17 01:51:00 +00001265 }
wdenkd0fb80c2003-01-11 09:48:40 +00001266 } else {
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001267 flag0 = *environment.flags;
wdenk4a6fd342003-04-12 23:38:12 +00001268
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001269 dev_current = 1;
Joe Hershberger497f2052012-10-03 09:38:46 +00001270 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001271 if (addr1 == NULL) {
Joe Hershberger497f2052012-10-03 09:38:46 +00001272 fprintf(stderr,
wdenk4a6fd342003-04-12 23:38:12 +00001273 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger497f2052012-10-03 09:38:46 +00001274 CUR_ENVSIZE);
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001275 return -1;
wdenk4a6fd342003-04-12 23:38:12 +00001276 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001277 redundant = addr1;
wdenk4a6fd342003-04-12 23:38:12 +00001278
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001279 /*
1280 * have to set environment.image for flash_read(), careful -
1281 * other pointers in environment still point inside addr0
1282 */
1283 environment.image = addr1;
1284 if (flash_io (O_RDONLY))
1285 return -1;
1286
1287 /* Check flag scheme compatibility */
1288 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1289 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1290 environment.flag_scheme = FLAG_BOOLEAN;
1291 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1292 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1293 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001294 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1295 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1296 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershberger785881f2013-04-08 10:32:52 +00001297 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1298 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1299 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metz23ef62d2013-08-30 00:56:01 +02001300 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1301 DEVTYPE(!dev_current) == MTD_ABSENT) {
1302 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001303 } else {
1304 fprintf (stderr, "Incompatible flash types!\n");
1305 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001306 }
wdenk4a6fd342003-04-12 23:38:12 +00001307
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001308 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasuta8a752c2014-03-05 19:59:52 +01001309
1310 if (aes_flag) {
1311 ret = env_aes_cbc_crypt(redundant->data, 0);
1312 if (ret)
1313 return ret;
1314 }
1315
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001316 crc1_ok = (crc1 == redundant->crc);
1317 flag1 = redundant->flags;
wdenk4a6fd342003-04-12 23:38:12 +00001318
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001319 if (crc0_ok && !crc1_ok) {
1320 dev_current = 0;
1321 } else if (!crc0_ok && crc1_ok) {
1322 dev_current = 1;
1323 } else if (!crc0_ok && !crc1_ok) {
wdenk4a6fd342003-04-12 23:38:12 +00001324 fprintf (stderr,
1325 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001326 memcpy (environment.data, default_environment,
1327 sizeof default_environment);
1328 dev_current = 0;
1329 } else {
1330 switch (environment.flag_scheme) {
1331 case FLAG_BOOLEAN:
1332 if (flag0 == active_flag &&
1333 flag1 == obsolete_flag) {
1334 dev_current = 0;
1335 } else if (flag0 == obsolete_flag &&
1336 flag1 == active_flag) {
1337 dev_current = 1;
1338 } else if (flag0 == flag1) {
1339 dev_current = 0;
1340 } else if (flag0 == 0xFF) {
1341 dev_current = 0;
1342 } else if (flag1 == 0xFF) {
1343 dev_current = 1;
1344 } else {
1345 dev_current = 0;
1346 }
1347 break;
1348 case FLAG_INCREMENTAL:
Jon Povey735eb0f2011-03-11 14:10:56 +09001349 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001350 dev_current = 1;
1351 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey735eb0f2011-03-11 14:10:56 +09001352 flag0 >= flag1)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001353 dev_current = 0;
Jon Povey735eb0f2011-03-11 14:10:56 +09001354 else /* flag1 > flag0 */
1355 dev_current = 1;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001356 break;
1357 default:
1358 fprintf (stderr, "Unknown flag scheme %u \n",
1359 environment.flag_scheme);
1360 return -1;
1361 }
1362 }
1363
1364 /*
1365 * If we are reading, we don't need the flag and the CRC any
1366 * more, if we are writing, we will re-calculate CRC and update
1367 * flags before writing out
1368 */
1369 if (dev_current) {
1370 environment.image = addr1;
1371 environment.crc = &redundant->crc;
1372 environment.flags = &redundant->flags;
1373 environment.data = redundant->data;
1374 free (addr0);
1375 } else {
1376 environment.image = addr0;
1377 /* Other pointers are already set */
wdenk4a6fd342003-04-12 23:38:12 +00001378 free (addr1);
wdenk6aff3112002-12-17 01:51:00 +00001379 }
Joe Hershberger74620e12012-10-15 15:29:25 +00001380#ifdef DEBUG
1381 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1382#endif
wdenk6aff3112002-12-17 01:51:00 +00001383 }
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001384 return 0;
wdenk6aff3112002-12-17 01:51:00 +00001385}
1386
1387
wdenk4a6fd342003-04-12 23:38:12 +00001388static int parse_config ()
wdenk6aff3112002-12-17 01:51:00 +00001389{
1390 struct stat st;
1391
wdenkd0fb80c2003-01-11 09:48:40 +00001392#if defined(CONFIG_FILE)
1393 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Michael Heimpold9884f442015-10-27 22:34:49 +01001394 if (get_config (config_file)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001395 fprintf (stderr,
Michael Heimpold9884f442015-10-27 22:34:49 +01001396 "Cannot parse config file '%s': %s\n", config_file, strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001397 return -1;
wdenk6aff3112002-12-17 01:51:00 +00001398 }
wdenkd0fb80c2003-01-11 09:48:40 +00001399#else
Tom Rini229695f2014-03-28 12:03:33 -04001400 DEVNAME (0) = DEVICE1_NAME;
wdenk4a6fd342003-04-12 23:38:12 +00001401 DEVOFFSET (0) = DEVICE1_OFFSET;
1402 ENVSIZE (0) = ENV1_SIZE;
Dustin Byford23869bf2014-03-06 20:48:22 -08001403 /* Default values are: erase-size=env-size */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001404 DEVESIZE (0) = ENVSIZE (0);
Dustin Byford23869bf2014-03-06 20:48:22 -08001405 /* #sectors=env-size/erase-size (rounded up) */
1406 ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001407#ifdef DEVICE1_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001408 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001409#endif
1410#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001411 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001412#endif
1413
wdenk6aff3112002-12-17 01:51:00 +00001414#ifdef HAVE_REDUND
Tom Rini229695f2014-03-28 12:03:33 -04001415 DEVNAME (1) = DEVICE2_NAME;
wdenk4a6fd342003-04-12 23:38:12 +00001416 DEVOFFSET (1) = DEVICE2_OFFSET;
1417 ENVSIZE (1) = ENV2_SIZE;
Dustin Byford23869bf2014-03-06 20:48:22 -08001418 /* Default values are: erase-size=env-size */
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001419 DEVESIZE (1) = ENVSIZE (1);
Dustin Byford23869bf2014-03-06 20:48:22 -08001420 /* #sectors=env-size/erase-size (rounded up) */
1421 ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001422#ifdef DEVICE2_ESIZE
wdenk4a6fd342003-04-12 23:38:12 +00001423 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001424#endif
1425#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001426 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001427#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001428 HaveRedundEnv = 1;
wdenk6aff3112002-12-17 01:51:00 +00001429#endif
wdenkd0fb80c2003-01-11 09:48:40 +00001430#endif
wdenk4a6fd342003-04-12 23:38:12 +00001431 if (stat (DEVNAME (0), &st)) {
1432 fprintf (stderr,
1433 "Cannot access MTD device %s: %s\n",
1434 DEVNAME (0), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001435 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001436 }
wdenk4a6fd342003-04-12 23:38:12 +00001437
1438 if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
1439 fprintf (stderr,
1440 "Cannot access MTD device %s: %s\n",
Wolfgang Denke2146b62005-09-25 01:20:38 +02001441 DEVNAME (1), strerror (errno));
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001442 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001443 }
wdenk6aff3112002-12-17 01:51:00 +00001444 return 0;
1445}
wdenkd0fb80c2003-01-11 09:48:40 +00001446
1447#if defined(CONFIG_FILE)
1448static int get_config (char *fname)
1449{
1450 FILE *fp;
1451 int i = 0;
1452 int rc;
1453 char dump[128];
Tom Rini229695f2014-03-28 12:03:33 -04001454 char *devname;
wdenkd0fb80c2003-01-11 09:48:40 +00001455
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001456 fp = fopen (fname, "r");
1457 if (fp == NULL)
1458 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001459
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001460 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenkd0fb80c2003-01-11 09:48:40 +00001461 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001462 if (dump[0] == '#')
wdenkd0fb80c2003-01-11 09:48:40 +00001463 continue;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001464
Tom Rini229695f2014-03-28 12:03:33 -04001465 rc = sscanf (dump, "%ms %lx %lx %lx %lx",
1466 &devname,
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001467 &DEVOFFSET (i),
1468 &ENVSIZE (i),
1469 &DEVESIZE (i),
1470 &ENVSECTORS (i));
1471
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001472 if (rc < 3)
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001473 continue;
1474
Tom Rini229695f2014-03-28 12:03:33 -04001475 DEVNAME(i) = devname;
1476
Remy Bohmer9eeaa8e2011-02-12 19:06:26 +01001477 if (rc < 4)
1478 /* Assume the erase size is the same as the env-size */
1479 DEVESIZE(i) = ENVSIZE(i);
1480
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001481 if (rc < 5)
Dustin Byford23869bf2014-03-06 20:48:22 -08001482 /* Assume enough env sectors to cover the environment */
1483 ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
wdenkd0fb80c2003-01-11 09:48:40 +00001484
1485 i++;
1486 }
wdenk4a6fd342003-04-12 23:38:12 +00001487 fclose (fp);
1488
wdenkd0fb80c2003-01-11 09:48:40 +00001489 HaveRedundEnv = i - 1;
wdenk4a6fd342003-04-12 23:38:12 +00001490 if (!i) { /* No valid entries found */
wdenkd0fb80c2003-01-11 09:48:40 +00001491 errno = EINVAL;
Guennadi Liakhovetski56086922008-09-04 13:01:49 +02001492 return -1;
wdenkd0fb80c2003-01-11 09:48:40 +00001493 } else
1494 return 0;
1495}
1496#endif