blob: 963a6152a50193d0b2fcb51ce7769c2fa3817d6f [file] [log] [blame]
wdenk34b613e2002-12-17 01:51:00 +00001/*
Detlev Zundel2111d3e2010-07-30 11:22:15 +02002 * (C) Copyright 2000-2010
wdenk34b613e2002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02005 * (C) Copyright 2008
6 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00009 */
10
Jörg Krause983487f2015-04-22 21:36:22 +020011#define _GNU_SOURCE
12
Peter Robinson656ee042015-12-09 07:15:33 +000013#include <compiler.h>
wdenk34b613e2002-12-17 01:51:00 +000014#include <errno.h>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060015#include <env_flags.h>
wdenk34b613e2002-12-17 01:51:00 +000016#include <fcntl.h>
Stefan Agnerb21e6022016-07-13 17:14:38 -070017#include <linux/fs.h>
Joe Hershbergere79a91f2012-10-12 10:23:37 +000018#include <linux/stringify.h>
Andreas Fenkartd0e12cb2016-03-11 09:39:35 +010019#include <ctype.h>
wdenk34b613e2002-12-17 01:51:00 +000020#include <stdio.h>
21#include <stdlib.h>
22#include <stddef.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/ioctl.h>
26#include <sys/stat.h>
27#include <unistd.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010028
29#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020030# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010031# include <linux/mtd/mtd.h>
32#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010033# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010034# include <mtd/mtd-user.h>
35#endif
wdenk34b613e2002-12-17 01:51:00 +000036
Stefano Babic831c4d52017-04-05 18:08:01 +020037#include "fw_env_private.h"
Markus Klotzbücher94db5572007-11-27 10:23:20 +010038#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000039
Andreas Fenkarte6511302016-05-31 09:21:56 +020040struct env_opts default_opts = {
41#ifdef CONFIG_FILE
42 .config_file = CONFIG_FILE
43#endif
44};
45
Marek Vasut05ed2a52014-03-05 19:59:52 +010046#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
47
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020048#define min(x, y) ({ \
49 typeof(x) _min1 = (x); \
50 typeof(y) _min2 = (y); \
51 (void) (&_min1 == &_min2); \
52 _min1 < _min2 ? _min1 : _min2; })
53
54struct envdev_s {
Tom Rini313e9122014-03-28 12:03:33 -040055 const char *devname; /* Device name */
Stefan Agnerb21e6022016-07-13 17:14:38 -070056 long long devoff; /* Device offset */
wdenk3f9ab982003-04-12 23:38:12 +000057 ulong env_size; /* environment size */
58 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020059 ulong env_sectors; /* number of environment sectors */
60 uint8_t mtd_type; /* type of the MTD device */
61};
wdenk34b613e2002-12-17 01:51:00 +000062
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020063static struct envdev_s envdevices[2] =
64{
65 {
66 .mtd_type = MTD_ABSENT,
67 }, {
68 .mtd_type = MTD_ABSENT,
69 },
70};
71static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000072
73#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000074#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000075#define ENVSIZE(i) envdevices[(i)].env_size
76#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020077#define ENVSECTORS(i) envdevices[(i)].env_sectors
78#define DEVTYPE(i) envdevices[(i)].mtd_type
wdenk34b613e2002-12-17 01:51:00 +000079
Joe Hershberger671adc42012-10-03 09:38:46 +000080#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000081
Andreas Fenkartb8bf4042016-04-19 22:43:42 +020082static unsigned long usable_envsize;
83#define ENV_SIZE usable_envsize
wdenk34b613e2002-12-17 01:51:00 +000084
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020085struct env_image_single {
86 uint32_t crc; /* CRC32 over data bytes */
87 char data[];
88};
wdenk34b613e2002-12-17 01:51:00 +000089
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020090struct env_image_redundant {
91 uint32_t crc; /* CRC32 over data bytes */
92 unsigned char flags; /* active or obsolete */
93 char data[];
94};
95
96enum flag_scheme {
97 FLAG_NONE,
98 FLAG_BOOLEAN,
99 FLAG_INCREMENTAL,
100};
101
102struct environment {
103 void *image;
104 uint32_t *crc;
105 unsigned char *flags;
106 char *data;
107 enum flag_scheme flag_scheme;
108};
109
110static struct environment environment = {
111 .flag_scheme = FLAG_NONE,
112};
wdenk34b613e2002-12-17 01:51:00 +0000113
wdenke7f34c62003-01-11 09:48:40 +0000114static int HaveRedundEnv = 0;
115
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100116static unsigned char active_flag = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200117/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100118static unsigned char obsolete_flag = 0;
wdenke7f34c62003-01-11 09:48:40 +0000119
Joe Hershberger81321142012-10-12 08:48:51 +0000120#define DEFAULT_ENV_INSTANCE_STATIC
121#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000122
wdenk3f9ab982003-04-12 23:38:12 +0000123static int flash_io (int mode);
Andreas Fenkart24371902016-04-05 23:13:42 +0200124static int parse_config(struct env_opts *opts);
wdenk3f9ab982003-04-12 23:38:12 +0000125
wdenke7f34c62003-01-11 09:48:40 +0000126#if defined(CONFIG_FILE)
wdenk3f9ab982003-04-12 23:38:12 +0000127static int get_config (char *);
wdenke7f34c62003-01-11 09:48:40 +0000128#endif
wdenk34b613e2002-12-17 01:51:00 +0000129
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100130static char *skip_chars(char *s)
Stefano Babicada628b2010-05-24 12:08:16 +0200131{
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100132 for (; *s != '\0'; s++) {
133 if (isblank(*s))
134 return s;
135 }
136 return NULL;
137}
Stefano Babicada628b2010-05-24 12:08:16 +0200138
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100139static char *skip_blanks(char *s)
140{
141 for (; *s != '\0'; s++) {
142 if (!isblank(*s))
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100143 return s;
Stefano Babicada628b2010-05-24 12:08:16 +0200144 }
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100145 return NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200146}
147
wdenk34b613e2002-12-17 01:51:00 +0000148/*
Andreas Fenkart3ff10742016-07-16 17:06:14 +0200149 * s1 is either a simple 'name', or a 'name=value' pair.
150 * s2 is a 'name=value' pair.
151 * If the names match, return the value of s2, else NULL.
152 */
153static char *envmatch(char *s1, char *s2)
154{
155 if (s1 == NULL || s2 == NULL)
156 return NULL;
157
158 while (*s1 == *s2++)
159 if (*s1++ == '=')
160 return s2;
161 if (*s1 == '\0' && *(s2 - 1) == '=')
162 return s2;
163 return NULL;
164}
165
166/**
wdenk34b613e2002-12-17 01:51:00 +0000167 * Search the environment for a variable.
168 * Return the value, if found, or NULL, if not found.
169 */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100170char *fw_getenv (char *name)
wdenk34b613e2002-12-17 01:51:00 +0000171{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100172 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000173
wdenk3f9ab982003-04-12 23:38:12 +0000174 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100175 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000176
wdenk3f9ab982003-04-12 23:38:12 +0000177 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000178 if (nxt >= &environment.data[ENV_SIZE]) {
179 fprintf (stderr, "## Error: "
180 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200181 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000182 }
183 }
wdenk3f9ab982003-04-12 23:38:12 +0000184 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000185 if (!val)
186 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200187 return val;
wdenk34b613e2002-12-17 01:51:00 +0000188 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200189 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000190}
191
192/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600193 * Search the default environment for a variable.
194 * Return the value, if found, or NULL, if not found.
195 */
196char *fw_getdefenv(char *name)
197{
198 char *env, *nxt;
199
200 for (env = default_environment; *env; env = nxt + 1) {
201 char *val;
202
203 for (nxt = env; *nxt; ++nxt) {
204 if (nxt >= &default_environment[ENV_SIZE]) {
205 fprintf(stderr, "## Error: "
206 "default environment not terminated\n");
207 return NULL;
208 }
209 }
210 val = envmatch(name, env);
211 if (!val)
212 continue;
213 return val;
214 }
215 return NULL;
216}
217
218/*
wdenk34b613e2002-12-17 01:51:00 +0000219 * Print the current definition of one, or more, or all
220 * environment variables
221 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200222int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000223{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100224 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000225
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200226 if (value_only && argc != 1) {
227 fprintf(stderr,
228 "## Error: `-n' option requires exactly one argument\n");
229 return -1;
230 }
231
Andreas Fenkarte6511302016-05-31 09:21:56 +0200232 if (!opts)
233 opts = &default_opts;
234
Andreas Fenkart24371902016-04-05 23:13:42 +0200235 if (fw_env_open(opts))
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200236 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000237
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100238 if (argc == 0) { /* Print all env variables */
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200239 char *env, *nxt;
wdenk3f9ab982003-04-12 23:38:12 +0000240 for (env = environment.data; *env; env = nxt + 1) {
241 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000242 if (nxt >= &environment.data[ENV_SIZE]) {
243 fprintf (stderr, "## Error: "
244 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200245 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000246 }
247 }
248
wdenk3f9ab982003-04-12 23:38:12 +0000249 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000250 }
Stefano Babic4b0a2932017-04-05 18:08:03 +0200251 fw_env_close(opts);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200252 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000253 }
254
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200255 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100256 char *name = argv[i];
257 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000258
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200259 val = fw_getenv(name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700260 if (!val) {
wdenk3f9ab982003-04-12 23:38:12 +0000261 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700262 rc = -1;
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200263 continue;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700264 }
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200265
266 if (value_only) {
267 puts(val);
268 break;
269 }
270
271 printf("%s=%s\n", name, val);
wdenk34b613e2002-12-17 01:51:00 +0000272 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700273
Stefano Babic4b0a2932017-04-05 18:08:03 +0200274 fw_env_close(opts);
275
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200276 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000277}
278
Stefano Babic4b0a2932017-04-05 18:08:03 +0200279int fw_env_flush(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000280{
Marek Vasut05ed2a52014-03-05 19:59:52 +0100281 int ret;
Andreas Fenkart24371902016-04-05 23:13:42 +0200282
Andreas Fenkarte6511302016-05-31 09:21:56 +0200283 if (!opts)
284 opts = &default_opts;
285
Stefano Babicada628b2010-05-24 12:08:16 +0200286 /*
287 * Update CRC
288 */
289 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000290
Stefano Babicada628b2010-05-24 12:08:16 +0200291 /* write environment back to flash */
292 if (flash_io(O_RDWR)) {
293 fprintf(stderr,
294 "Error: can't write fw_env to flash\n");
295 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000296 }
297
Stefano Babicada628b2010-05-24 12:08:16 +0200298 return 0;
299}
wdenk34b613e2002-12-17 01:51:00 +0000300
Stefano Babicada628b2010-05-24 12:08:16 +0200301
302/*
303 * Set/Clear a single variable in the environment.
304 * This is called in sequence to update the environment
305 * in RAM without updating the copy in flash after each set
306 */
307int fw_env_write(char *name, char *value)
308{
309 int len;
310 char *env, *nxt;
311 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600312 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000313
314 /*
315 * search if variable with this name already exists
316 */
wdenk3f9ab982003-04-12 23:38:12 +0000317 for (nxt = env = environment.data; *env; env = nxt + 1) {
318 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000319 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200320 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000321 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200322 errno = EINVAL;
323 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000324 }
325 }
wdenk3f9ab982003-04-12 23:38:12 +0000326 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000327 break;
328 }
329
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600330 deleting = (oldval && !(value && strlen(value)));
331 creating = (!oldval && (value && strlen(value)));
332 overwriting = (oldval && (value && strlen(value)));
333
334 /* check for permission */
335 if (deleting) {
336 if (env_flags_validate_varaccess(name,
337 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
338 printf("Can't delete \"%s\"\n", name);
339 errno = EROFS;
340 return -1;
341 }
342 } else if (overwriting) {
343 if (env_flags_validate_varaccess(name,
344 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
345 printf("Can't overwrite \"%s\"\n", name);
346 errno = EROFS;
347 return -1;
348 } else if (env_flags_validate_varaccess(name,
349 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
350 const char *defval = fw_getdefenv(name);
351
352 if (defval == NULL)
353 defval = "";
354 if (strcmp(oldval, defval)
355 != 0) {
356 printf("Can't overwrite \"%s\"\n", name);
357 errno = EROFS;
358 return -1;
359 }
360 }
361 } else if (creating) {
362 if (env_flags_validate_varaccess(name,
363 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
364 printf("Can't create \"%s\"\n", name);
365 errno = EROFS;
366 return -1;
367 }
368 } else
369 /* Nothing to do */
370 return 0;
371
372 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000373 if (*++nxt == '\0') {
374 *env = '\0';
375 } else {
376 for (;;) {
377 *env = *nxt++;
378 if ((*env == '\0') && (*nxt == '\0'))
379 break;
380 ++env;
381 }
382 }
383 *++env = '\0';
384 }
385
386 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200387 if (!value || !strlen(value))
388 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000389
390 /*
391 * Append new definition at the end
392 */
wdenk3f9ab982003-04-12 23:38:12 +0000393 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000394 if (env > environment.data)
395 ++env;
396 /*
397 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000398 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000399 */
wdenk3f9ab982003-04-12 23:38:12 +0000400 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000401 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200402 len += strlen(value) + 1;
403
wdenk3f9ab982003-04-12 23:38:12 +0000404 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000405 fprintf (stderr,
406 "Error: environment overflow, \"%s\" deleted\n",
407 name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200408 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000409 }
Stefano Babicada628b2010-05-24 12:08:16 +0200410
wdenk34b613e2002-12-17 01:51:00 +0000411 while ((*env = *name++) != '\0')
412 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200413 *env = '=';
414 while ((*++env = *value++) != '\0')
415 ;
416
417 /* end is marked with double '\0' */
418 *++env = '\0';
419
420 return 0;
421}
422
423/*
424 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
425 * 0 - OK
426 * EINVAL - need at least 1 argument
427 * EROFS - certain variables ("ethaddr", "serial#") cannot be
428 * modified or deleted
429 *
430 */
Simon Glass6a38e412017-08-03 12:22:09 -0600431int fw_env_set(int argc, char *argv[], struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200432{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100433 int i;
Mike Frysinger178ec772012-11-10 19:47:45 +0000434 size_t len;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100435 char *name, **valv;
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200436 char *oldval;
Stefano Babicada628b2010-05-24 12:08:16 +0200437 char *value = NULL;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100438 int valc;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200439 int ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200440
Andreas Fenkarte6511302016-05-31 09:21:56 +0200441 if (!opts)
442 opts = &default_opts;
443
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100444 if (argc < 1) {
445 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasut05ed2a52014-03-05 19:59:52 +0100446 errno = EINVAL;
447 return -1;
448 }
449
Andreas Fenkart24371902016-04-05 23:13:42 +0200450 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200451 fprintf(stderr, "Error: environment not initialized\n");
452 return -1;
453 }
454
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100455 name = argv[0];
456 valv = argv + 1;
457 valc = argc - 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200458
Stefano Babic4b0a2932017-04-05 18:08:03 +0200459 if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
460 fw_env_close(opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200461 return -1;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200462 }
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600463
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000464 len = 0;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100465 for (i = 0; i < valc; ++i) {
466 char *val = valv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000467 size_t val_len = strlen(val);
468
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000469 if (value)
470 value[len - 1] = ' ';
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200471 oldval = value;
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000472 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200473 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000474 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200475 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000476 len, strerror(errno));
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200477 free(oldval);
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000478 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200479 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000480
481 memcpy(value + len, val, val_len);
482 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000483 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000484 }
485
Stefano Babicada628b2010-05-24 12:08:16 +0200486 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000487
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000488 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000489
Stefano Babic4b0a2932017-04-05 18:08:03 +0200490 ret = fw_env_flush(opts);
491 fw_env_close(opts);
492
493 return ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200494}
wdenk34b613e2002-12-17 01:51:00 +0000495
Stefano Babicada628b2010-05-24 12:08:16 +0200496/*
497 * Parse a file and configure the u-boot variables.
498 * The script file has a very simple format, as follows:
499 *
500 * Each line has a couple with name, value:
501 * <white spaces>variable_name<white spaces>variable_value
502 *
503 * Both variable_name and variable_value are interpreted as strings.
504 * Any character after <white spaces> and before ending \r\n is interpreted
505 * as variable's value (no comment allowed on these lines !)
506 *
507 * Comments are allowed if the first character in the line is #
508 *
509 * Returns -1 and sets errno error codes:
510 * 0 - OK
511 * -1 - Error
512 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200513int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200514{
515 FILE *fp;
516 char dump[1024]; /* Maximum line length in the file */
517 char *name;
518 char *val;
519 int lineno = 0;
520 int len;
521 int ret = 0;
522
Andreas Fenkarte6511302016-05-31 09:21:56 +0200523 if (!opts)
524 opts = &default_opts;
525
Andreas Fenkart24371902016-04-05 23:13:42 +0200526 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200527 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200528 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000529 }
530
Stefano Babicada628b2010-05-24 12:08:16 +0200531 if (strcmp(fname, "-") == 0)
532 fp = stdin;
533 else {
534 fp = fopen(fname, "r");
535 if (fp == NULL) {
536 fprintf(stderr, "I cannot open %s for reading\n",
537 fname);
538 return -1;
539 }
540 }
541
542 while (fgets(dump, sizeof(dump), fp)) {
543 lineno++;
544 len = strlen(dump);
545
546 /*
547 * Read a whole line from the file. If the line is too long
548 * or is not terminated, reports an error and exit.
549 */
550 if (dump[len - 1] != '\n') {
551 fprintf(stderr,
552 "Line %d not corrected terminated or too long\n",
553 lineno);
554 ret = -1;
555 break;
556 }
557
558 /* Drop ending line feed / carriage return */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100559 dump[--len] = '\0';
560 if (len && dump[len - 1] == '\r')
561 dump[--len] = '\0';
Stefano Babicada628b2010-05-24 12:08:16 +0200562
563 /* Skip comment or empty lines */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100564 if (len == 0 || dump[0] == '#')
Stefano Babicada628b2010-05-24 12:08:16 +0200565 continue;
566
567 /*
568 * Search for variable's name,
569 * remove leading whitespaces
570 */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100571 name = skip_blanks(dump);
Stefano Babicada628b2010-05-24 12:08:16 +0200572 if (!name)
573 continue;
574
575 /* The first white space is the end of variable name */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100576 val = skip_chars(name);
Stefano Babicada628b2010-05-24 12:08:16 +0200577 len = strlen(name);
578 if (val) {
579 *val++ = '\0';
580 if ((val - name) < len)
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100581 val = skip_blanks(val);
Stefano Babicada628b2010-05-24 12:08:16 +0200582 else
583 val = NULL;
584 }
585
586#ifdef DEBUG
587 fprintf(stderr, "Setting %s : %s\n",
588 name, val ? val : " removed");
589#endif
590
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600591 if (env_flags_validate_type(name, val) < 0) {
592 ret = -1;
593 break;
594 }
595
Stefano Babicada628b2010-05-24 12:08:16 +0200596 /*
597 * If there is an error setting a variable,
598 * try to save the environment and returns an error
599 */
600 if (fw_env_write(name, val)) {
601 fprintf(stderr,
602 "fw_env_write returns with error : %s\n",
603 strerror(errno));
604 ret = -1;
605 break;
606 }
607
608 }
609
610 /* Close file if not stdin */
611 if (strcmp(fname, "-") != 0)
612 fclose(fp);
613
Stefano Babic4b0a2932017-04-05 18:08:03 +0200614 ret |= fw_env_flush(opts);
615
616 fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200617
618 return ret;
wdenk34b613e2002-12-17 01:51:00 +0000619}
620
Andreas Fenkart22281582016-08-29 23:16:57 +0200621/**
622 * environment_end() - compute offset of first byte right after environemnt
623 * @dev - index of enviroment buffer
624 * Return:
625 * device offset of first byte right after environemnt
626 */
627off_t environment_end(int dev)
628{
629 /* environment is block aligned */
630 return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
631}
632
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200633/*
634 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
635 * 0 - block is good
636 * > 0 - block is bad
637 * < 0 - failed to test
638 */
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200639static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000640{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200641 if (mtd_type == MTD_NANDFLASH) {
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200642 int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000643
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200644 if (badblock < 0) {
645 perror ("Cannot read bad block mark");
646 return badblock;
647 }
648
649 if (badblock) {
650#ifdef DEBUG
Marcin Niestroj20890732016-05-06 14:58:29 +0200651 fprintf (stderr, "Bad block at 0x%llx, skipping\n",
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200652 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200653#endif
654 return badblock;
655 }
wdenk34b613e2002-12-17 01:51:00 +0000656 }
657
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200658 return 0;
659}
660
661/*
662 * Read data from flash at an offset into a provided buffer. On NAND it skips
663 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
664 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
665 */
666static int flash_read_buf (int dev, int fd, void *buf, size_t count,
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200667 off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200668{
669 size_t blocklen; /* erase / write length - one block on NAND,
670 0 on NOR */
671 size_t processed = 0; /* progress counter */
672 size_t readlen = count; /* current read length */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200673 off_t block_seek; /* offset inside the current block to the start
674 of the data */
675 loff_t blockstart; /* running start of the current block -
676 MEMGETBADBLOCK needs 64 bits */
677 int rc;
678
Remy Bohmercb969eb2011-02-12 19:06:26 +0100679 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200680
681 /* Offset inside a block */
682 block_seek = offset - blockstart;
683
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200684 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200685 /*
686 * NAND: calculate which blocks we are reading. We have
687 * to read one block at a time to skip bad blocks.
688 */
689 blocklen = DEVESIZE (dev);
690
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200691 /* Limit to one block for the first read */
692 if (readlen > blocklen - block_seek)
693 readlen = blocklen - block_seek;
694 } else {
695 blocklen = 0;
wdenke7f34c62003-01-11 09:48:40 +0000696 }
wdenk34b613e2002-12-17 01:51:00 +0000697
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200698 /* This only runs once on NOR flash */
699 while (processed < count) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200700 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200701 if (rc < 0) /* block test failed */
702 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000703
Andreas Fenkart22281582016-08-29 23:16:57 +0200704 if (blockstart + block_seek + readlen > environment_end(dev)) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200705 /* End of range is reached */
706 fprintf (stderr,
707 "Too few good blocks within range\n");
708 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000709 }
710
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200711 if (rc) { /* block is bad */
712 blockstart += blocklen;
713 continue;
wdenk34b613e2002-12-17 01:51:00 +0000714 }
715
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200716 /*
717 * If a block is bad, we retry in the next block at the same
Tom Rini282f2702017-09-05 14:01:29 -0400718 * offset - see env/nand.c::writeenv()
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200719 */
720 lseek (fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000721
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200722 rc = read (fd, buf + processed, readlen);
723 if (rc != readlen) {
724 fprintf (stderr, "Read error on %s: %s\n",
725 DEVNAME (dev), strerror (errno));
726 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000727 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200728#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000729 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
Marcin Niestroj20890732016-05-06 14:58:29 +0200730 rc, (unsigned long long) blockstart + block_seek,
731 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200732#endif
733 processed += readlen;
734 readlen = min (blocklen, count - processed);
735 block_seek = 0;
736 blockstart += blocklen;
737 }
738
739 return processed;
740}
741
742/*
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200743 * Write count bytes from begin of environment, but stay within
744 * ENVSECTORS(dev) sectors of
Remy Bohmercb969eb2011-02-12 19:06:26 +0100745 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
746 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200747 */
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200748static int flash_write_buf(int dev, int fd, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200749{
750 void *data;
751 struct erase_info_user erase;
752 size_t blocklen; /* length of NAND block / NOR erase sector */
753 size_t erase_len; /* whole area that can be erased - may include
754 bad blocks */
755 size_t erasesize; /* erase / write length - one block on NAND,
756 whole area on NOR */
757 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100758 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200759 bad blocks */
760 off_t erase_offset; /* offset to the first erase block (aligned)
761 below offset */
762 off_t block_seek; /* offset inside the erase block to the start
763 of the data */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200764 loff_t blockstart; /* running start of the current block -
765 MEMGETBADBLOCK needs 64 bits */
766 int rc;
767
Oliver Metze9c66ab2013-08-30 00:56:02 +0200768 /*
769 * For mtd devices only offset and size of the environment do matter
770 */
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200771 if (DEVTYPE(dev) == MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +0200772 blocklen = count;
Oliver Metze9c66ab2013-08-30 00:56:02 +0200773 erase_len = blocklen;
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200774 blockstart = DEVOFFSET(dev);
Oliver Metze9c66ab2013-08-30 00:56:02 +0200775 block_seek = 0;
776 write_total = blocklen;
777 } else {
778 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200779
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200780 erase_offset = DEVOFFSET(dev);
wdenk34b613e2002-12-17 01:51:00 +0000781
Oliver Metze9c66ab2013-08-30 00:56:02 +0200782 /* Maximum area we may use */
Andreas Fenkart22281582016-08-29 23:16:57 +0200783 erase_len = environment_end(dev) - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +0000784
Oliver Metze9c66ab2013-08-30 00:56:02 +0200785 blockstart = erase_offset;
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200786
Oliver Metze9c66ab2013-08-30 00:56:02 +0200787 /* Offset inside a block */
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200788 block_seek = DEVOFFSET(dev) - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200789
Oliver Metze9c66ab2013-08-30 00:56:02 +0200790 /*
791 * Data size we actually write: from the start of the block
792 * to the start of the data, then count bytes of data, and
793 * to the end of the block
794 */
795 write_total = ((block_seek + count + blocklen - 1) /
796 blocklen) * blocklen;
797 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200798
799 /*
800 * Support data anywhere within erase sectors: read out the complete
801 * area to be erased, replace the environment image, write the whole
802 * block back again.
803 */
804 if (write_total > count) {
805 data = malloc (erase_len);
806 if (!data) {
wdenke7f34c62003-01-11 09:48:40 +0000807 fprintf (stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200808 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200809 erase_len, strerror (errno));
810 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000811 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200812
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200813 rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200814 if (write_total != rc)
815 return -1;
816
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000817#ifdef DEBUG
818 fprintf(stderr, "Preserving data ");
819 if (block_seek != 0)
820 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
821 if (block_seek + count != write_total) {
822 if (block_seek != 0)
823 fprintf(stderr, " and ");
Marcin Niestroj20890732016-05-06 14:58:29 +0200824 fprintf(stderr, "0x%lx - 0x%lx",
825 (unsigned long) block_seek + count,
826 (unsigned long) write_total - 1);
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000827 }
828 fprintf(stderr, "\n");
829#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200830 /* Overwrite the old environment */
831 memcpy (data + block_seek, buf, count);
832 } else {
833 /*
834 * We get here, iff offset is block-aligned and count is a
835 * multiple of blocklen - see write_total calculation above
836 */
837 data = buf;
838 }
839
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200840 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200841 /*
842 * NAND: calculate which blocks we are writing. We have
843 * to write one block at a time to skip bad blocks.
844 */
845 erasesize = blocklen;
846 } else {
847 erasesize = erase_len;
848 }
849
850 erase.length = erasesize;
851
Remy Bohmercb969eb2011-02-12 19:06:26 +0100852 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200853 while (processed < write_total) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200854 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200855 if (rc < 0) /* block test failed */
856 return rc;
857
Andreas Fenkart22281582016-08-29 23:16:57 +0200858 if (blockstart + erasesize > environment_end(dev)) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200859 fprintf (stderr, "End of range reached, aborting\n");
860 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000861 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200862
863 if (rc) { /* block is bad */
864 blockstart += blocklen;
865 continue;
866 }
867
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200868 if (DEVTYPE(dev) != MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +0200869 erase.start = blockstart;
870 ioctl(fd, MEMUNLOCK, &erase);
871 /* These do not need an explicit erase cycle */
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200872 if (DEVTYPE(dev) != MTD_DATAFLASH)
Oliver Metze9c66ab2013-08-30 00:56:02 +0200873 if (ioctl(fd, MEMERASE, &erase) != 0) {
874 fprintf(stderr,
875 "MTD erase error on %s: %s\n",
876 DEVNAME(dev), strerror(errno));
877 return -1;
878 }
879 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200880
881 if (lseek (fd, blockstart, SEEK_SET) == -1) {
wdenk34b613e2002-12-17 01:51:00 +0000882 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200883 "Seek error on %s: %s\n",
884 DEVNAME (dev), strerror (errno));
885 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000886 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200887
888#ifdef DEBUG
Marcin Niestroj20890732016-05-06 14:58:29 +0200889 fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
890 (unsigned long long) erasesize,
891 (unsigned long long) blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200892#endif
893 if (write (fd, data + processed, erasesize) != erasesize) {
894 fprintf (stderr, "Write error on %s: %s\n",
895 DEVNAME (dev), strerror (errno));
896 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000897 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200898
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200899 if (DEVTYPE(dev) != MTD_ABSENT)
Oliver Metze9c66ab2013-08-30 00:56:02 +0200900 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200901
Dustin Byford0983cad2014-03-06 20:48:23 -0800902 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200903 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -0800904 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200905 }
906
907 if (write_total > count)
908 free (data);
909
910 return processed;
911}
912
913/*
914 * Set obsolete flag at offset - NOR flash only
915 */
916static int flash_flag_obsolete (int dev, int fd, off_t offset)
917{
918 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200919 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200920
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200921 erase.start = DEVOFFSET (dev);
922 erase.length = DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200923 /* This relies on the fact, that obsolete_flag == 0 */
924 rc = lseek (fd, offset, SEEK_SET);
925 if (rc < 0) {
926 fprintf (stderr, "Cannot seek to set the flag on %s \n",
927 DEVNAME (dev));
928 return rc;
929 }
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200930 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200931 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel2111d3e2010-07-30 11:22:15 +0200932 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200933 if (rc < 0)
934 perror ("Could not set obsolete flag");
935
936 return rc;
937}
938
939static int flash_write (int fd_current, int fd_target, int dev_target)
940{
941 int rc;
942
943 switch (environment.flag_scheme) {
944 case FLAG_NONE:
945 break;
946 case FLAG_INCREMENTAL:
947 (*environment.flags)++;
948 break;
949 case FLAG_BOOLEAN:
950 *environment.flags = active_flag;
951 break;
952 default:
953 fprintf (stderr, "Unimplemented flash scheme %u \n",
954 environment.flag_scheme);
955 return -1;
956 }
957
958#ifdef DEBUG
Stefan Agnerb21e6022016-07-13 17:14:38 -0700959 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200960 DEVOFFSET (dev_target), DEVNAME (dev_target));
961#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +0100962
Joe Hershberger671adc42012-10-03 09:38:46 +0000963 rc = flash_write_buf(dev_target, fd_target, environment.image,
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200964 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200965 if (rc < 0)
966 return rc;
967
968 if (environment.flag_scheme == FLAG_BOOLEAN) {
969 /* Have to set obsolete flag */
970 off_t offset = DEVOFFSET (dev_current) +
971 offsetof (struct env_image_redundant, flags);
972#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000973 fprintf(stderr,
Stefan Agnerb21e6022016-07-13 17:14:38 -0700974 "Setting obsolete flag in environment at 0x%llx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200975 DEVOFFSET (dev_current), DEVNAME (dev_current));
976#endif
977 flash_flag_obsolete (dev_current, fd_current, offset);
978 }
979
980 return 0;
981}
982
983static int flash_read (int fd)
984{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200985 int rc;
986
Joe Hershberger671adc42012-10-03 09:38:46 +0000987 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200988 DEVOFFSET(dev_current));
Marek Vasut05ed2a52014-03-05 19:59:52 +0100989 if (rc != CUR_ENVSIZE)
990 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200991
Marek Vasut05ed2a52014-03-05 19:59:52 +0100992 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200993}
994
995static int flash_io (int mode)
996{
997 int fd_current, fd_target, rc, dev_target;
998
999 /* dev_current: fd_current, erase_current */
1000 fd_current = open (DEVNAME (dev_current), mode);
1001 if (fd_current < 0) {
1002 fprintf (stderr,
1003 "Can't open %s: %s\n",
1004 DEVNAME (dev_current), strerror (errno));
1005 return -1;
1006 }
1007
1008 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +00001009 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001010 /* switch to next partition for writing */
1011 dev_target = !dev_current;
1012 /* dev_target: fd_target, erase_target */
1013 fd_target = open (DEVNAME (dev_target), mode);
1014 if (fd_target < 0) {
wdenke7f34c62003-01-11 09:48:40 +00001015 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001016 "Can't open %s: %s\n",
1017 DEVNAME (dev_target),
1018 strerror (errno));
1019 rc = -1;
1020 goto exit;
wdenke7f34c62003-01-11 09:48:40 +00001021 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001022 } else {
1023 dev_target = dev_current;
1024 fd_target = fd_current;
wdenk34b613e2002-12-17 01:51:00 +00001025 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001026
1027 rc = flash_write (fd_current, fd_target, dev_target);
1028
Lukasz Majewskibe3b78a2017-08-27 13:46:22 +02001029 if (fsync(fd_current) &&
1030 !(errno == EINVAL || errno == EROFS)) {
Michael Heimpold5a2b0f12013-05-20 21:34:42 +02001031 fprintf (stderr,
1032 "fsync failed on %s: %s\n",
1033 DEVNAME (dev_current), strerror (errno));
1034 }
1035
wdenke7f34c62003-01-11 09:48:40 +00001036 if (HaveRedundEnv) {
Lukasz Majewskibe3b78a2017-08-27 13:46:22 +02001037 if (fsync(fd_target) &&
1038 !(errno == EINVAL || errno == EROFS)) {
Michael Heimpold5a2b0f12013-05-20 21:34:42 +02001039 fprintf (stderr,
1040 "fsync failed on %s: %s\n",
1041 DEVNAME (dev_current), strerror (errno));
1042 }
1043
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001044 if (close (fd_target)) {
wdenke7f34c62003-01-11 09:48:40 +00001045 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001046 "I/O error on %s: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001047 DEVNAME (dev_target),
wdenk3f9ab982003-04-12 23:38:12 +00001048 strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001049 rc = -1;
wdenke7f34c62003-01-11 09:48:40 +00001050 }
wdenk34b613e2002-12-17 01:51:00 +00001051 }
wdenk34b613e2002-12-17 01:51:00 +00001052 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001053 rc = flash_read (fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001054 }
1055
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001056exit:
1057 if (close (fd_current)) {
wdenk34b613e2002-12-17 01:51:00 +00001058 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001059 "I/O error on %s: %s\n",
1060 DEVNAME (dev_current), strerror (errno));
1061 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001062 }
1063
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001064 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001065}
1066
1067/*
wdenk34b613e2002-12-17 01:51:00 +00001068 * Prevent confusion if running from erased flash memory
1069 */
Andreas Fenkart24371902016-04-05 23:13:42 +02001070int fw_env_open(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001071{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001072 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001073 unsigned char flag0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001074 void *addr0 = NULL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001075
wdenk34b613e2002-12-17 01:51:00 +00001076 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001077 unsigned char flag1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001078 void *addr1 = NULL;
wdenke7f34c62003-01-11 09:48:40 +00001079
Marek Vasut05ed2a52014-03-05 19:59:52 +01001080 int ret;
1081
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001082 struct env_image_single *single;
1083 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001084
Andreas Fenkarte6511302016-05-31 09:21:56 +02001085 if (!opts)
1086 opts = &default_opts;
1087
Andreas Fenkart24371902016-04-05 23:13:42 +02001088 if (parse_config(opts)) /* should fill envdevices */
Stefano Babic4b0a2932017-04-05 18:08:03 +02001089 return -EINVAL;
wdenk3f9ab982003-04-12 23:38:12 +00001090
Joe Hershberger671adc42012-10-03 09:38:46 +00001091 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001092 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001093 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001094 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001095 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001096 ret = -ENOMEM;
1097 goto open_cleanup;
wdenke7f34c62003-01-11 09:48:40 +00001098 }
wdenk3f9ab982003-04-12 23:38:12 +00001099
wdenke7f34c62003-01-11 09:48:40 +00001100 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001101 environment.image = addr0;
1102
1103 if (HaveRedundEnv) {
1104 redundant = addr0;
1105 environment.crc = &redundant->crc;
1106 environment.flags = &redundant->flags;
1107 environment.data = redundant->data;
1108 } else {
1109 single = addr0;
1110 environment.crc = &single->crc;
1111 environment.flags = NULL;
1112 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001113 }
wdenk3f9ab982003-04-12 23:38:12 +00001114
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001115 dev_current = 0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001116 if (flash_io(O_RDONLY)) {
1117 ret = -EIO;
1118 goto open_cleanup;
1119 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001120
1121 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001122
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001123 crc0_ok = (crc0 == *environment.crc);
wdenke7f34c62003-01-11 09:48:40 +00001124 if (!HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001125 if (!crc0_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001126 fprintf (stderr,
1127 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkc5203f92006-03-12 23:27:46 +01001128 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk34b613e2002-12-17 01:51:00 +00001129 }
wdenke7f34c62003-01-11 09:48:40 +00001130 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001131 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001132
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001133 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001134 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001135 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001136 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001137 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001138 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001139 ret = -ENOMEM;
1140 goto open_cleanup;
wdenk3f9ab982003-04-12 23:38:12 +00001141 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001142 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001143
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001144 /*
1145 * have to set environment.image for flash_read(), careful -
1146 * other pointers in environment still point inside addr0
1147 */
1148 environment.image = addr1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001149 if (flash_io(O_RDONLY)) {
1150 ret = -EIO;
1151 goto open_cleanup;
1152 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001153
1154 /* Check flag scheme compatibility */
1155 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1156 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1157 environment.flag_scheme = FLAG_BOOLEAN;
1158 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1159 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1160 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001161 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1162 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1163 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001164 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1165 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1166 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001167 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
1168 DEVTYPE(!dev_current) == MTD_ABSENT) {
1169 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001170 } else {
1171 fprintf (stderr, "Incompatible flash types!\n");
Stefano Babic4b0a2932017-04-05 18:08:03 +02001172 ret = -EINVAL;
1173 goto open_cleanup;
wdenk34b613e2002-12-17 01:51:00 +00001174 }
wdenk3f9ab982003-04-12 23:38:12 +00001175
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001176 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001177
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001178 crc1_ok = (crc1 == redundant->crc);
1179 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001180
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001181 if (crc0_ok && !crc1_ok) {
1182 dev_current = 0;
1183 } else if (!crc0_ok && crc1_ok) {
1184 dev_current = 1;
1185 } else if (!crc0_ok && !crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001186 fprintf (stderr,
1187 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001188 memcpy (environment.data, default_environment,
1189 sizeof default_environment);
1190 dev_current = 0;
1191 } else {
1192 switch (environment.flag_scheme) {
1193 case FLAG_BOOLEAN:
1194 if (flag0 == active_flag &&
1195 flag1 == obsolete_flag) {
1196 dev_current = 0;
1197 } else if (flag0 == obsolete_flag &&
1198 flag1 == active_flag) {
1199 dev_current = 1;
1200 } else if (flag0 == flag1) {
1201 dev_current = 0;
1202 } else if (flag0 == 0xFF) {
1203 dev_current = 0;
1204 } else if (flag1 == 0xFF) {
1205 dev_current = 1;
1206 } else {
1207 dev_current = 0;
1208 }
1209 break;
1210 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001211 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001212 dev_current = 1;
1213 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001214 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001215 dev_current = 0;
Jon Povey93ccd472011-03-11 14:10:56 +09001216 else /* flag1 > flag0 */
1217 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001218 break;
1219 default:
1220 fprintf (stderr, "Unknown flag scheme %u \n",
1221 environment.flag_scheme);
1222 return -1;
1223 }
1224 }
1225
1226 /*
1227 * If we are reading, we don't need the flag and the CRC any
1228 * more, if we are writing, we will re-calculate CRC and update
1229 * flags before writing out
1230 */
1231 if (dev_current) {
1232 environment.image = addr1;
1233 environment.crc = &redundant->crc;
1234 environment.flags = &redundant->flags;
1235 environment.data = redundant->data;
1236 free (addr0);
1237 } else {
1238 environment.image = addr0;
1239 /* Other pointers are already set */
wdenk3f9ab982003-04-12 23:38:12 +00001240 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +00001241 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001242#ifdef DEBUG
1243 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1244#endif
wdenk34b613e2002-12-17 01:51:00 +00001245 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001246 return 0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001247
1248open_cleanup:
1249 if (addr0)
1250 free(addr0);
1251
1252 if (addr1)
1253 free(addr0);
1254
1255 return ret;
1256}
1257
1258/*
1259 * Simply free allocated buffer with environment
1260 */
1261int fw_env_close(struct env_opts *opts)
1262{
1263 if (environment.image)
1264 free(environment.image);
1265
1266 environment.image = NULL;
1267
1268 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001269}
1270
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001271static int check_device_config(int dev)
1272{
1273 struct stat st;
1274 int fd, rc = 0;
1275
1276 fd = open(DEVNAME(dev), O_RDONLY);
1277 if (fd < 0) {
1278 fprintf(stderr,
1279 "Cannot open %s: %s\n",
1280 DEVNAME(dev), strerror(errno));
1281 return -1;
1282 }
1283
1284 rc = fstat(fd, &st);
1285 if (rc < 0) {
1286 fprintf(stderr, "Cannot stat the file %s\n",
1287 DEVNAME(dev));
1288 goto err;
1289 }
1290
1291 if (S_ISCHR(st.st_mode)) {
1292 struct mtd_info_user mtdinfo;
1293 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1294 if (rc < 0) {
1295 fprintf(stderr, "Cannot get MTD information for %s\n",
1296 DEVNAME(dev));
1297 goto err;
1298 }
1299 if (mtdinfo.type != MTD_NORFLASH &&
1300 mtdinfo.type != MTD_NANDFLASH &&
1301 mtdinfo.type != MTD_DATAFLASH &&
1302 mtdinfo.type != MTD_UBIVOLUME) {
1303 fprintf(stderr, "Unsupported flash type %u on %s\n",
1304 mtdinfo.type, DEVNAME(dev));
1305 goto err;
1306 }
1307 DEVTYPE(dev) = mtdinfo.type;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001308 if (DEVESIZE(dev) == 0)
1309 /* Assume the erase size is the same as the env-size */
1310 DEVESIZE(dev) = ENVSIZE(dev);
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001311 } else {
Stefan Agnerb21e6022016-07-13 17:14:38 -07001312 uint64_t size;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001313 DEVTYPE(dev) = MTD_ABSENT;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001314 if (DEVESIZE(dev) == 0)
1315 /* Assume the erase size to be 512 bytes */
1316 DEVESIZE(dev) = 0x200;
Stefan Agnerb21e6022016-07-13 17:14:38 -07001317
1318 /*
1319 * Check for negative offsets, treat it as backwards offset
1320 * from the end of the block device
1321 */
1322 if (DEVOFFSET(dev) < 0) {
1323 rc = ioctl(fd, BLKGETSIZE64, &size);
1324 if (rc < 0) {
1325 fprintf(stderr, "Could not get block device size on %s\n",
1326 DEVNAME(dev));
1327 goto err;
1328 }
1329
1330 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1331#ifdef DEBUG
1332 fprintf(stderr, "Calculated device offset 0x%llx on %s\n",
1333 DEVOFFSET(dev), DEVNAME(dev));
1334#endif
1335 }
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001336 }
1337
Max Krummenacher6c44f542016-11-19 13:58:56 +01001338 if (ENVSECTORS(dev) == 0)
1339 /* Assume enough sectors to cover the environment */
1340 ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev));
1341
1342 if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
1343 fprintf(stderr, "Environment does not start on (erase) block boundary\n");
1344 errno = EINVAL;
1345 return -1;
1346 }
1347
1348 if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
1349 fprintf(stderr, "Environment does not fit into available sectors\n");
1350 errno = EINVAL;
1351 return -1;
1352 }
1353
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001354err:
1355 close(fd);
1356 return rc;
1357}
wdenk34b613e2002-12-17 01:51:00 +00001358
Andreas Fenkart24371902016-04-05 23:13:42 +02001359static int parse_config(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001360{
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001361 int rc;
wdenk34b613e2002-12-17 01:51:00 +00001362
Andreas Fenkarte6511302016-05-31 09:21:56 +02001363 if (!opts)
1364 opts = &default_opts;
Anatolij Gustschine89ead32016-04-29 22:00:11 +02001365
Andreas Fenkarte6511302016-05-31 09:21:56 +02001366#if defined(CONFIG_FILE)
wdenke7f34c62003-01-11 09:48:40 +00001367 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart24371902016-04-05 23:13:42 +02001368 if (get_config(opts->config_file)) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001369 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart24371902016-04-05 23:13:42 +02001370 opts->config_file);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001371 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001372 }
wdenke7f34c62003-01-11 09:48:40 +00001373#else
Tom Rini313e9122014-03-28 12:03:33 -04001374 DEVNAME (0) = DEVICE1_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001375 DEVOFFSET (0) = DEVICE1_OFFSET;
1376 ENVSIZE (0) = ENV1_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001377
1378 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1379 * know DEVTYPE
1380 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001381#ifdef DEVICE1_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001382 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001383#endif
1384#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001385 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001386#endif
1387
wdenk34b613e2002-12-17 01:51:00 +00001388#ifdef HAVE_REDUND
Tom Rini313e9122014-03-28 12:03:33 -04001389 DEVNAME (1) = DEVICE2_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001390 DEVOFFSET (1) = DEVICE2_OFFSET;
1391 ENVSIZE (1) = ENV2_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001392
1393 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1394 * know DEVTYPE
1395 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001396#ifdef DEVICE2_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001397 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001398#endif
1399#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001400 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001401#endif
wdenke7f34c62003-01-11 09:48:40 +00001402 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +00001403#endif
wdenke7f34c62003-01-11 09:48:40 +00001404#endif
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001405 rc = check_device_config(0);
1406 if (rc < 0)
1407 return rc;
wdenk3f9ab982003-04-12 23:38:12 +00001408
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001409 if (HaveRedundEnv) {
1410 rc = check_device_config(1);
1411 if (rc < 0)
1412 return rc;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001413
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001414 if (ENVSIZE(0) != ENVSIZE(1)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001415 fprintf(stderr,
Andreas Fenkart31f2e052016-08-17 23:41:53 +02001416 "Redundant environments have unequal size");
1417 return -1;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001418 }
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001419 }
1420
1421 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
1422 if (HaveRedundEnv)
1423 usable_envsize -= sizeof(char);
1424
wdenk34b613e2002-12-17 01:51:00 +00001425 return 0;
1426}
wdenke7f34c62003-01-11 09:48:40 +00001427
1428#if defined(CONFIG_FILE)
1429static int get_config (char *fname)
1430{
1431 FILE *fp;
1432 int i = 0;
1433 int rc;
1434 char dump[128];
Tom Rini313e9122014-03-28 12:03:33 -04001435 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001436
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001437 fp = fopen (fname, "r");
1438 if (fp == NULL)
1439 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001440
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001441 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenke7f34c62003-01-11 09:48:40 +00001442 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001443 if (dump[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001444 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001445
Stefan Agnerb21e6022016-07-13 17:14:38 -07001446 rc = sscanf(dump, "%ms %lli %lx %lx %lx",
1447 &devname,
1448 &DEVOFFSET(i),
1449 &ENVSIZE(i),
1450 &DEVESIZE(i),
1451 &ENVSECTORS(i));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001452
Remy Bohmercb969eb2011-02-12 19:06:26 +01001453 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001454 continue;
1455
Tom Rini313e9122014-03-28 12:03:33 -04001456 DEVNAME(i) = devname;
1457
Max Krummenacher6c44f542016-11-19 13:58:56 +01001458 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1459 * know DEVTYPE
1460 */
wdenke7f34c62003-01-11 09:48:40 +00001461
1462 i++;
1463 }
wdenk3f9ab982003-04-12 23:38:12 +00001464 fclose (fp);
1465
wdenke7f34c62003-01-11 09:48:40 +00001466 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +00001467 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001468 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001469 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001470 } else
1471 return 0;
1472}
1473#endif