blob: 0e3e34321f6467b9889295aaf9b46b3bee158e57 [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>
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080028#include <dirent.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010029
30#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020031# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010032# include <linux/mtd/mtd.h>
33#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010034# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010035# include <mtd/mtd-user.h>
36#endif
wdenk34b613e2002-12-17 01:51:00 +000037
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080038#include <mtd/ubi-user.h>
39
Stefano Babic831c4d52017-04-05 18:08:01 +020040#include "fw_env_private.h"
Markus Klotzbücher94db5572007-11-27 10:23:20 +010041#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000042
Andreas Fenkarte6511302016-05-31 09:21:56 +020043struct env_opts default_opts = {
44#ifdef CONFIG_FILE
45 .config_file = CONFIG_FILE
46#endif
47};
48
Marek Vasut05ed2a52014-03-05 19:59:52 +010049#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
50
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020051#define min(x, y) ({ \
52 typeof(x) _min1 = (x); \
53 typeof(y) _min2 = (y); \
54 (void) (&_min1 == &_min2); \
55 _min1 < _min2 ? _min1 : _min2; })
56
57struct envdev_s {
Tom Rini313e9122014-03-28 12:03:33 -040058 const char *devname; /* Device name */
Stefan Agnerb21e6022016-07-13 17:14:38 -070059 long long devoff; /* Device offset */
wdenk3f9ab982003-04-12 23:38:12 +000060 ulong env_size; /* environment size */
61 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020062 ulong env_sectors; /* number of environment sectors */
63 uint8_t mtd_type; /* type of the MTD device */
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080064 int is_ubi; /* set if we use UBI volume */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020065};
wdenk34b613e2002-12-17 01:51:00 +000066
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020067static struct envdev_s envdevices[2] =
68{
69 {
70 .mtd_type = MTD_ABSENT,
71 }, {
72 .mtd_type = MTD_ABSENT,
73 },
74};
75static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000076
77#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000078#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000079#define ENVSIZE(i) envdevices[(i)].env_size
80#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020081#define ENVSECTORS(i) envdevices[(i)].env_sectors
82#define DEVTYPE(i) envdevices[(i)].mtd_type
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080083#define IS_UBI(i) envdevices[(i)].is_ubi
wdenk34b613e2002-12-17 01:51:00 +000084
Joe Hershberger671adc42012-10-03 09:38:46 +000085#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000086
Andreas Fenkartb8bf4042016-04-19 22:43:42 +020087static unsigned long usable_envsize;
88#define ENV_SIZE usable_envsize
wdenk34b613e2002-12-17 01:51:00 +000089
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020090struct env_image_single {
91 uint32_t crc; /* CRC32 over data bytes */
92 char data[];
93};
wdenk34b613e2002-12-17 01:51:00 +000094
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020095struct env_image_redundant {
96 uint32_t crc; /* CRC32 over data bytes */
97 unsigned char flags; /* active or obsolete */
98 char data[];
99};
100
101enum flag_scheme {
102 FLAG_NONE,
103 FLAG_BOOLEAN,
104 FLAG_INCREMENTAL,
105};
106
107struct environment {
108 void *image;
109 uint32_t *crc;
110 unsigned char *flags;
111 char *data;
112 enum flag_scheme flag_scheme;
113};
114
115static struct environment environment = {
116 .flag_scheme = FLAG_NONE,
117};
wdenk34b613e2002-12-17 01:51:00 +0000118
wdenke7f34c62003-01-11 09:48:40 +0000119static int HaveRedundEnv = 0;
120
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100121static unsigned char active_flag = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200122/* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100123static unsigned char obsolete_flag = 0;
wdenke7f34c62003-01-11 09:48:40 +0000124
Joe Hershberger81321142012-10-12 08:48:51 +0000125#define DEFAULT_ENV_INSTANCE_STATIC
126#include <env_default.h>
wdenk34b613e2002-12-17 01:51:00 +0000127
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -0800128#define UBI_DEV_START "/dev/ubi"
129#define UBI_SYSFS "/sys/class/ubi"
130#define UBI_VOL_NAME_PATT "ubi%d_%d"
131
132static int is_ubi_devname(const char *devname)
133{
134 return !strncmp(devname, UBI_DEV_START, sizeof(UBI_DEV_START) - 1);
135}
136
137static int ubi_check_volume_sysfs_name(const char *volume_sysfs_name,
138 const char *volname)
139{
140 char path[256];
141 FILE *file;
142 char *name;
143 int ret;
144
145 strcpy(path, UBI_SYSFS "/");
146 strcat(path, volume_sysfs_name);
147 strcat(path, "/name");
148
149 file = fopen(path, "r");
150 if (!file)
151 return -1;
152
153 ret = fscanf(file, "%ms", &name);
154 fclose(file);
155 if (ret <= 0 || !name) {
156 fprintf(stderr,
157 "Failed to read from file %s, ret = %d, name = %s\n",
158 path, ret, name);
159 return -1;
160 }
161
162 if (!strcmp(name, volname)) {
163 free(name);
164 return 0;
165 }
166 free(name);
167
168 return -1;
169}
170
171static int ubi_get_volnum_by_name(int devnum, const char *volname)
172{
173 DIR *sysfs_ubi;
174 struct dirent *dirent;
175 int ret;
176 int tmp_devnum;
177 int volnum;
178
179 sysfs_ubi = opendir(UBI_SYSFS);
180 if (!sysfs_ubi)
181 return -1;
182
183#ifdef DEBUG
184 fprintf(stderr, "Looking for volume name \"%s\"\n", volname);
185#endif
186
187 while (1) {
188 dirent = readdir(sysfs_ubi);
189 if (!dirent)
190 return -1;
191
192 ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT,
193 &tmp_devnum, &volnum);
194 if (ret == 2 && devnum == tmp_devnum) {
195 if (ubi_check_volume_sysfs_name(dirent->d_name,
196 volname) == 0)
197 return volnum;
198 }
199 }
200
201 return -1;
202}
203
204static int ubi_get_devnum_by_devname(const char *devname)
205{
206 int devnum;
207 int ret;
208
209 ret = sscanf(devname + sizeof(UBI_DEV_START) - 1, "%d", &devnum);
210 if (ret != 1)
211 return -1;
212
213 return devnum;
214}
215
216static const char *ubi_get_volume_devname(const char *devname,
217 const char *volname)
218{
219 char *volume_devname;
220 int volnum;
221 int devnum;
222 int ret;
223
224 devnum = ubi_get_devnum_by_devname(devname);
225 if (devnum < 0)
226 return NULL;
227
228 volnum = ubi_get_volnum_by_name(devnum, volname);
229 if (volnum < 0)
230 return NULL;
231
232 ret = asprintf(&volume_devname, "%s_%d", devname, volnum);
233 if (ret < 0)
234 return NULL;
235
236#ifdef DEBUG
237 fprintf(stderr, "Found ubi volume \"%s:%s\" -> %s\n",
238 devname, volname, volume_devname);
239#endif
240
241 return volume_devname;
242}
243
244static void ubi_check_dev(unsigned int dev_id)
245{
246 char *devname = (char *)DEVNAME(dev_id);
247 char *pname;
248 const char *volname = NULL;
249 const char *volume_devname;
250
251 if (!is_ubi_devname(DEVNAME(dev_id)))
252 return;
253
254 IS_UBI(dev_id) = 1;
255
256 for (pname = devname; *pname != '\0'; pname++) {
257 if (*pname == ':') {
258 *pname = '\0';
259 volname = pname + 1;
260 break;
261 }
262 }
263
264 if (volname) {
265 /* Let's find real volume device name */
266 volume_devname = ubi_get_volume_devname(devname, volname);
267 if (!volume_devname) {
268 fprintf(stderr, "Didn't found ubi volume \"%s\"\n",
269 volname);
270 return;
271 }
272
273 free(devname);
274 DEVNAME(dev_id) = volume_devname;
275 }
276}
277
278static int ubi_update_start(int fd, int64_t bytes)
279{
280 if (ioctl(fd, UBI_IOCVOLUP, &bytes))
281 return -1;
282 return 0;
283}
284
285static int ubi_read(int fd, void *buf, size_t count)
286{
287 ssize_t ret;
288
289 while (count > 0) {
290 ret = read(fd, buf, count);
291 if (ret > 0) {
292 count -= ret;
293 buf += ret;
294
295 continue;
296 }
297
298 if (ret == 0) {
299 /*
300 * Happens in case of too short volume data size. If we
301 * return error status we will fail it will be treated
302 * as UBI device error.
303 *
304 * Leave catching this error to CRC check.
305 */
306 fprintf(stderr, "Warning: end of data on ubi volume\n");
307 return 0;
308 } else if (errno == EBADF) {
309 /*
310 * Happens in case of corrupted volume. The same as
311 * above, we cannot return error now, as we will still
312 * be able to successfully write environment later.
313 */
314 fprintf(stderr, "Warning: corrupted volume?\n");
315 return 0;
316 } else if (errno == EINTR) {
317 continue;
318 }
319
320 fprintf(stderr, "Cannot read %u bytes from ubi volume, %s\n",
321 (unsigned int)count, strerror(errno));
322 return -1;
323 }
324
325 return 0;
326}
327
328static int ubi_write(int fd, const void *buf, size_t count)
329{
330 ssize_t ret;
331
332 while (count > 0) {
333 ret = write(fd, buf, count);
334 if (ret <= 0) {
335 if (ret < 0 && errno == EINTR)
336 continue;
337
338 fprintf(stderr, "Cannot write %u bytes to ubi volume\n",
339 (unsigned int)count);
340 return -1;
341 }
342
343 count -= ret;
344 buf += ret;
345 }
346
347 return 0;
348}
349
wdenk3f9ab982003-04-12 23:38:12 +0000350static int flash_io (int mode);
Andreas Fenkart24371902016-04-05 23:13:42 +0200351static int parse_config(struct env_opts *opts);
wdenk3f9ab982003-04-12 23:38:12 +0000352
wdenke7f34c62003-01-11 09:48:40 +0000353#if defined(CONFIG_FILE)
wdenk3f9ab982003-04-12 23:38:12 +0000354static int get_config (char *);
wdenke7f34c62003-01-11 09:48:40 +0000355#endif
wdenk34b613e2002-12-17 01:51:00 +0000356
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100357static char *skip_chars(char *s)
Stefano Babicada628b2010-05-24 12:08:16 +0200358{
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100359 for (; *s != '\0'; s++) {
Stefan Agner87833242018-03-01 14:06:32 +0100360 if (isblank(*s) || *s == '=')
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100361 return s;
362 }
363 return NULL;
364}
Stefano Babicada628b2010-05-24 12:08:16 +0200365
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100366static char *skip_blanks(char *s)
367{
368 for (; *s != '\0'; s++) {
369 if (!isblank(*s))
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100370 return s;
Stefano Babicada628b2010-05-24 12:08:16 +0200371 }
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100372 return NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200373}
374
wdenk34b613e2002-12-17 01:51:00 +0000375/*
Andreas Fenkart3ff10742016-07-16 17:06:14 +0200376 * s1 is either a simple 'name', or a 'name=value' pair.
377 * s2 is a 'name=value' pair.
378 * If the names match, return the value of s2, else NULL.
379 */
380static char *envmatch(char *s1, char *s2)
381{
382 if (s1 == NULL || s2 == NULL)
383 return NULL;
384
385 while (*s1 == *s2++)
386 if (*s1++ == '=')
387 return s2;
388 if (*s1 == '\0' && *(s2 - 1) == '=')
389 return s2;
390 return NULL;
391}
392
393/**
wdenk34b613e2002-12-17 01:51:00 +0000394 * Search the environment for a variable.
395 * Return the value, if found, or NULL, if not found.
396 */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100397char *fw_getenv (char *name)
wdenk34b613e2002-12-17 01:51:00 +0000398{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100399 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000400
wdenk3f9ab982003-04-12 23:38:12 +0000401 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100402 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000403
wdenk3f9ab982003-04-12 23:38:12 +0000404 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000405 if (nxt >= &environment.data[ENV_SIZE]) {
406 fprintf (stderr, "## Error: "
407 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200408 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000409 }
410 }
wdenk3f9ab982003-04-12 23:38:12 +0000411 val = envmatch (name, env);
wdenk34b613e2002-12-17 01:51:00 +0000412 if (!val)
413 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200414 return val;
wdenk34b613e2002-12-17 01:51:00 +0000415 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200416 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000417}
418
419/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600420 * Search the default environment for a variable.
421 * Return the value, if found, or NULL, if not found.
422 */
423char *fw_getdefenv(char *name)
424{
425 char *env, *nxt;
426
427 for (env = default_environment; *env; env = nxt + 1) {
428 char *val;
429
430 for (nxt = env; *nxt; ++nxt) {
431 if (nxt >= &default_environment[ENV_SIZE]) {
432 fprintf(stderr, "## Error: "
433 "default environment not terminated\n");
434 return NULL;
435 }
436 }
437 val = envmatch(name, env);
438 if (!val)
439 continue;
440 return val;
441 }
442 return NULL;
443}
444
445/*
wdenk34b613e2002-12-17 01:51:00 +0000446 * Print the current definition of one, or more, or all
447 * environment variables
448 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200449int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000450{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100451 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000452
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200453 if (value_only && argc != 1) {
454 fprintf(stderr,
Alex Kiernan688cee92018-02-11 17:16:46 +0000455 "## Error: `-n'/`--noheader' option requires exactly one argument\n");
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200456 return -1;
457 }
458
Andreas Fenkarte6511302016-05-31 09:21:56 +0200459 if (!opts)
460 opts = &default_opts;
461
Andreas Fenkart24371902016-04-05 23:13:42 +0200462 if (fw_env_open(opts))
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200463 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000464
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100465 if (argc == 0) { /* Print all env variables */
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200466 char *env, *nxt;
wdenk3f9ab982003-04-12 23:38:12 +0000467 for (env = environment.data; *env; env = nxt + 1) {
468 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000469 if (nxt >= &environment.data[ENV_SIZE]) {
470 fprintf (stderr, "## Error: "
471 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200472 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000473 }
474 }
475
wdenk3f9ab982003-04-12 23:38:12 +0000476 printf ("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000477 }
Stefano Babic4b0a2932017-04-05 18:08:03 +0200478 fw_env_close(opts);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200479 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000480 }
481
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200482 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100483 char *name = argv[i];
484 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000485
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200486 val = fw_getenv(name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700487 if (!val) {
wdenk3f9ab982003-04-12 23:38:12 +0000488 fprintf (stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700489 rc = -1;
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200490 continue;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700491 }
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200492
493 if (value_only) {
494 puts(val);
495 break;
496 }
497
498 printf("%s=%s\n", name, val);
wdenk34b613e2002-12-17 01:51:00 +0000499 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700500
Stefano Babic4b0a2932017-04-05 18:08:03 +0200501 fw_env_close(opts);
502
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200503 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000504}
505
Stefano Babic4b0a2932017-04-05 18:08:03 +0200506int fw_env_flush(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000507{
Andreas Fenkarte6511302016-05-31 09:21:56 +0200508 if (!opts)
509 opts = &default_opts;
510
Stefano Babicada628b2010-05-24 12:08:16 +0200511 /*
512 * Update CRC
513 */
514 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000515
Stefano Babicada628b2010-05-24 12:08:16 +0200516 /* write environment back to flash */
517 if (flash_io(O_RDWR)) {
518 fprintf(stderr,
519 "Error: can't write fw_env to flash\n");
520 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000521 }
522
Stefano Babicada628b2010-05-24 12:08:16 +0200523 return 0;
524}
wdenk34b613e2002-12-17 01:51:00 +0000525
Stefano Babicada628b2010-05-24 12:08:16 +0200526
527/*
528 * Set/Clear a single variable in the environment.
529 * This is called in sequence to update the environment
530 * in RAM without updating the copy in flash after each set
531 */
532int fw_env_write(char *name, char *value)
533{
534 int len;
535 char *env, *nxt;
536 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600537 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000538
539 /*
540 * search if variable with this name already exists
541 */
wdenk3f9ab982003-04-12 23:38:12 +0000542 for (nxt = env = environment.data; *env; env = nxt + 1) {
543 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000544 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200545 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000546 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200547 errno = EINVAL;
548 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000549 }
550 }
wdenk3f9ab982003-04-12 23:38:12 +0000551 if ((oldval = envmatch (name, env)) != NULL)
wdenk34b613e2002-12-17 01:51:00 +0000552 break;
553 }
554
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600555 deleting = (oldval && !(value && strlen(value)));
556 creating = (!oldval && (value && strlen(value)));
557 overwriting = (oldval && (value && strlen(value)));
558
559 /* check for permission */
560 if (deleting) {
561 if (env_flags_validate_varaccess(name,
562 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
563 printf("Can't delete \"%s\"\n", name);
564 errno = EROFS;
565 return -1;
566 }
567 } else if (overwriting) {
568 if (env_flags_validate_varaccess(name,
569 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
570 printf("Can't overwrite \"%s\"\n", name);
571 errno = EROFS;
572 return -1;
573 } else if (env_flags_validate_varaccess(name,
574 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
575 const char *defval = fw_getdefenv(name);
576
577 if (defval == NULL)
578 defval = "";
579 if (strcmp(oldval, defval)
580 != 0) {
581 printf("Can't overwrite \"%s\"\n", name);
582 errno = EROFS;
583 return -1;
584 }
585 }
586 } else if (creating) {
587 if (env_flags_validate_varaccess(name,
588 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
589 printf("Can't create \"%s\"\n", name);
590 errno = EROFS;
591 return -1;
592 }
593 } else
594 /* Nothing to do */
595 return 0;
596
597 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000598 if (*++nxt == '\0') {
599 *env = '\0';
600 } else {
601 for (;;) {
602 *env = *nxt++;
603 if ((*env == '\0') && (*nxt == '\0'))
604 break;
605 ++env;
606 }
607 }
608 *++env = '\0';
609 }
610
611 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200612 if (!value || !strlen(value))
613 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000614
615 /*
616 * Append new definition at the end
617 */
wdenk3f9ab982003-04-12 23:38:12 +0000618 for (env = environment.data; *env || *(env + 1); ++env);
wdenk34b613e2002-12-17 01:51:00 +0000619 if (env > environment.data)
620 ++env;
621 /*
622 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000623 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000624 */
wdenk3f9ab982003-04-12 23:38:12 +0000625 len = strlen (name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000626 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200627 len += strlen(value) + 1;
628
wdenk3f9ab982003-04-12 23:38:12 +0000629 if (len > (&environment.data[ENV_SIZE] - env)) {
wdenk34b613e2002-12-17 01:51:00 +0000630 fprintf (stderr,
631 "Error: environment overflow, \"%s\" deleted\n",
632 name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200633 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000634 }
Stefano Babicada628b2010-05-24 12:08:16 +0200635
wdenk34b613e2002-12-17 01:51:00 +0000636 while ((*env = *name++) != '\0')
637 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200638 *env = '=';
639 while ((*++env = *value++) != '\0')
640 ;
641
642 /* end is marked with double '\0' */
643 *++env = '\0';
644
645 return 0;
646}
647
648/*
649 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
650 * 0 - OK
651 * EINVAL - need at least 1 argument
652 * EROFS - certain variables ("ethaddr", "serial#") cannot be
653 * modified or deleted
654 *
655 */
Simon Glass6a38e412017-08-03 12:22:09 -0600656int fw_env_set(int argc, char *argv[], struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200657{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100658 int i;
Mike Frysinger178ec772012-11-10 19:47:45 +0000659 size_t len;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100660 char *name, **valv;
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200661 char *oldval;
Stefano Babicada628b2010-05-24 12:08:16 +0200662 char *value = NULL;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100663 int valc;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200664 int ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200665
Andreas Fenkarte6511302016-05-31 09:21:56 +0200666 if (!opts)
667 opts = &default_opts;
668
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100669 if (argc < 1) {
670 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasut05ed2a52014-03-05 19:59:52 +0100671 errno = EINVAL;
672 return -1;
673 }
674
Andreas Fenkart24371902016-04-05 23:13:42 +0200675 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200676 fprintf(stderr, "Error: environment not initialized\n");
677 return -1;
678 }
679
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100680 name = argv[0];
681 valv = argv + 1;
682 valc = argc - 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200683
Stefano Babic4b0a2932017-04-05 18:08:03 +0200684 if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
685 fw_env_close(opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200686 return -1;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200687 }
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600688
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000689 len = 0;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100690 for (i = 0; i < valc; ++i) {
691 char *val = valv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000692 size_t val_len = strlen(val);
693
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000694 if (value)
695 value[len - 1] = ' ';
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200696 oldval = value;
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000697 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200698 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000699 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200700 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000701 len, strerror(errno));
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200702 free(oldval);
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000703 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200704 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000705
706 memcpy(value + len, val, val_len);
707 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000708 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000709 }
710
Stefano Babicada628b2010-05-24 12:08:16 +0200711 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000712
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000713 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000714
Stefano Babic4b0a2932017-04-05 18:08:03 +0200715 ret = fw_env_flush(opts);
716 fw_env_close(opts);
717
718 return ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200719}
wdenk34b613e2002-12-17 01:51:00 +0000720
Stefano Babicada628b2010-05-24 12:08:16 +0200721/*
722 * Parse a file and configure the u-boot variables.
723 * The script file has a very simple format, as follows:
724 *
725 * Each line has a couple with name, value:
726 * <white spaces>variable_name<white spaces>variable_value
727 *
728 * Both variable_name and variable_value are interpreted as strings.
729 * Any character after <white spaces> and before ending \r\n is interpreted
730 * as variable's value (no comment allowed on these lines !)
731 *
732 * Comments are allowed if the first character in the line is #
733 *
734 * Returns -1 and sets errno error codes:
735 * 0 - OK
736 * -1 - Error
737 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200738int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200739{
740 FILE *fp;
741 char dump[1024]; /* Maximum line length in the file */
742 char *name;
743 char *val;
744 int lineno = 0;
745 int len;
746 int ret = 0;
747
Andreas Fenkarte6511302016-05-31 09:21:56 +0200748 if (!opts)
749 opts = &default_opts;
750
Andreas Fenkart24371902016-04-05 23:13:42 +0200751 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200752 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200753 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000754 }
755
Stefano Babicada628b2010-05-24 12:08:16 +0200756 if (strcmp(fname, "-") == 0)
757 fp = stdin;
758 else {
759 fp = fopen(fname, "r");
760 if (fp == NULL) {
761 fprintf(stderr, "I cannot open %s for reading\n",
762 fname);
763 return -1;
764 }
765 }
766
767 while (fgets(dump, sizeof(dump), fp)) {
768 lineno++;
769 len = strlen(dump);
770
771 /*
772 * Read a whole line from the file. If the line is too long
773 * or is not terminated, reports an error and exit.
774 */
775 if (dump[len - 1] != '\n') {
776 fprintf(stderr,
777 "Line %d not corrected terminated or too long\n",
778 lineno);
779 ret = -1;
780 break;
781 }
782
783 /* Drop ending line feed / carriage return */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100784 dump[--len] = '\0';
785 if (len && dump[len - 1] == '\r')
786 dump[--len] = '\0';
Stefano Babicada628b2010-05-24 12:08:16 +0200787
788 /* Skip comment or empty lines */
Andreas Fenkarte3ff0dc2016-03-11 09:39:38 +0100789 if (len == 0 || dump[0] == '#')
Stefano Babicada628b2010-05-24 12:08:16 +0200790 continue;
791
792 /*
793 * Search for variable's name,
794 * remove leading whitespaces
795 */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100796 name = skip_blanks(dump);
Stefano Babicada628b2010-05-24 12:08:16 +0200797 if (!name)
798 continue;
799
800 /* The first white space is the end of variable name */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100801 val = skip_chars(name);
Stefano Babicada628b2010-05-24 12:08:16 +0200802 len = strlen(name);
803 if (val) {
804 *val++ = '\0';
805 if ((val - name) < len)
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100806 val = skip_blanks(val);
Stefano Babicada628b2010-05-24 12:08:16 +0200807 else
808 val = NULL;
809 }
810
811#ifdef DEBUG
812 fprintf(stderr, "Setting %s : %s\n",
813 name, val ? val : " removed");
814#endif
815
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600816 if (env_flags_validate_type(name, val) < 0) {
817 ret = -1;
818 break;
819 }
820
Stefano Babicada628b2010-05-24 12:08:16 +0200821 /*
822 * If there is an error setting a variable,
823 * try to save the environment and returns an error
824 */
825 if (fw_env_write(name, val)) {
826 fprintf(stderr,
827 "fw_env_write returns with error : %s\n",
828 strerror(errno));
829 ret = -1;
830 break;
831 }
832
833 }
834
835 /* Close file if not stdin */
836 if (strcmp(fname, "-") != 0)
837 fclose(fp);
838
Stefano Babic4b0a2932017-04-05 18:08:03 +0200839 ret |= fw_env_flush(opts);
840
841 fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200842
843 return ret;
wdenk34b613e2002-12-17 01:51:00 +0000844}
845
Andreas Fenkart22281582016-08-29 23:16:57 +0200846/**
847 * environment_end() - compute offset of first byte right after environemnt
848 * @dev - index of enviroment buffer
849 * Return:
850 * device offset of first byte right after environemnt
851 */
852off_t environment_end(int dev)
853{
854 /* environment is block aligned */
855 return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
856}
857
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200858/*
859 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
860 * 0 - block is good
861 * > 0 - block is bad
862 * < 0 - failed to test
863 */
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200864static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000865{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200866 if (mtd_type == MTD_NANDFLASH) {
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200867 int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000868
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200869 if (badblock < 0) {
870 perror ("Cannot read bad block mark");
871 return badblock;
872 }
873
874 if (badblock) {
875#ifdef DEBUG
Marcin Niestroj20890732016-05-06 14:58:29 +0200876 fprintf (stderr, "Bad block at 0x%llx, skipping\n",
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200877 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200878#endif
879 return badblock;
880 }
wdenk34b613e2002-12-17 01:51:00 +0000881 }
882
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200883 return 0;
884}
885
886/*
887 * Read data from flash at an offset into a provided buffer. On NAND it skips
888 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
889 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
890 */
891static int flash_read_buf (int dev, int fd, void *buf, size_t count,
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200892 off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200893{
894 size_t blocklen; /* erase / write length - one block on NAND,
895 0 on NOR */
896 size_t processed = 0; /* progress counter */
897 size_t readlen = count; /* current read length */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200898 off_t block_seek; /* offset inside the current block to the start
899 of the data */
900 loff_t blockstart; /* running start of the current block -
901 MEMGETBADBLOCK needs 64 bits */
902 int rc;
903
Remy Bohmercb969eb2011-02-12 19:06:26 +0100904 blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200905
906 /* Offset inside a block */
907 block_seek = offset - blockstart;
908
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200909 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200910 /*
911 * NAND: calculate which blocks we are reading. We have
912 * to read one block at a time to skip bad blocks.
913 */
914 blocklen = DEVESIZE (dev);
915
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200916 /* Limit to one block for the first read */
917 if (readlen > blocklen - block_seek)
918 readlen = blocklen - block_seek;
919 } else {
920 blocklen = 0;
wdenke7f34c62003-01-11 09:48:40 +0000921 }
wdenk34b613e2002-12-17 01:51:00 +0000922
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200923 /* This only runs once on NOR flash */
924 while (processed < count) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200925 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200926 if (rc < 0) /* block test failed */
927 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000928
Andreas Fenkart22281582016-08-29 23:16:57 +0200929 if (blockstart + block_seek + readlen > environment_end(dev)) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200930 /* End of range is reached */
931 fprintf (stderr,
932 "Too few good blocks within range\n");
933 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000934 }
935
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200936 if (rc) { /* block is bad */
937 blockstart += blocklen;
938 continue;
wdenk34b613e2002-12-17 01:51:00 +0000939 }
940
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200941 /*
942 * If a block is bad, we retry in the next block at the same
Tom Rini282f2702017-09-05 14:01:29 -0400943 * offset - see env/nand.c::writeenv()
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200944 */
945 lseek (fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000946
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200947 rc = read (fd, buf + processed, readlen);
948 if (rc != readlen) {
949 fprintf (stderr, "Read error on %s: %s\n",
950 DEVNAME (dev), strerror (errno));
951 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000952 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200953#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +0000954 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
Marcin Niestroj20890732016-05-06 14:58:29 +0200955 rc, (unsigned long long) blockstart + block_seek,
956 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200957#endif
958 processed += readlen;
959 readlen = min (blocklen, count - processed);
960 block_seek = 0;
961 blockstart += blocklen;
962 }
963
964 return processed;
965}
966
967/*
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200968 * Write count bytes from begin of environment, but stay within
969 * ENVSECTORS(dev) sectors of
Remy Bohmercb969eb2011-02-12 19:06:26 +0100970 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
971 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200972 */
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200973static int flash_write_buf(int dev, int fd, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200974{
975 void *data;
976 struct erase_info_user erase;
977 size_t blocklen; /* length of NAND block / NOR erase sector */
978 size_t erase_len; /* whole area that can be erased - may include
979 bad blocks */
980 size_t erasesize; /* erase / write length - one block on NAND,
981 whole area on NOR */
982 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100983 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200984 bad blocks */
985 off_t erase_offset; /* offset to the first erase block (aligned)
986 below offset */
987 off_t block_seek; /* offset inside the erase block to the start
988 of the data */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200989 loff_t blockstart; /* running start of the current block -
990 MEMGETBADBLOCK needs 64 bits */
991 int rc;
992
Oliver Metze9c66ab2013-08-30 00:56:02 +0200993 /*
994 * For mtd devices only offset and size of the environment do matter
995 */
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200996 if (DEVTYPE(dev) == MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +0200997 blocklen = count;
Oliver Metze9c66ab2013-08-30 00:56:02 +0200998 erase_len = blocklen;
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200999 blockstart = DEVOFFSET(dev);
Oliver Metze9c66ab2013-08-30 00:56:02 +02001000 block_seek = 0;
1001 write_total = blocklen;
1002 } else {
1003 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001004
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001005 erase_offset = DEVOFFSET(dev);
wdenk34b613e2002-12-17 01:51:00 +00001006
Oliver Metze9c66ab2013-08-30 00:56:02 +02001007 /* Maximum area we may use */
Andreas Fenkart22281582016-08-29 23:16:57 +02001008 erase_len = environment_end(dev) - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +00001009
Oliver Metze9c66ab2013-08-30 00:56:02 +02001010 blockstart = erase_offset;
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001011
Oliver Metze9c66ab2013-08-30 00:56:02 +02001012 /* Offset inside a block */
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001013 block_seek = DEVOFFSET(dev) - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001014
Oliver Metze9c66ab2013-08-30 00:56:02 +02001015 /*
1016 * Data size we actually write: from the start of the block
1017 * to the start of the data, then count bytes of data, and
1018 * to the end of the block
1019 */
1020 write_total = ((block_seek + count + blocklen - 1) /
1021 blocklen) * blocklen;
1022 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001023
1024 /*
1025 * Support data anywhere within erase sectors: read out the complete
1026 * area to be erased, replace the environment image, write the whole
1027 * block back again.
1028 */
1029 if (write_total > count) {
1030 data = malloc (erase_len);
1031 if (!data) {
wdenke7f34c62003-01-11 09:48:40 +00001032 fprintf (stderr,
Luka Perkovd5982712011-09-05 23:40:13 +02001033 "Cannot malloc %zu bytes: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001034 erase_len, strerror (errno));
1035 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001036 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001037
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001038 rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001039 if (write_total != rc)
1040 return -1;
1041
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001042#ifdef DEBUG
1043 fprintf(stderr, "Preserving data ");
1044 if (block_seek != 0)
1045 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
1046 if (block_seek + count != write_total) {
1047 if (block_seek != 0)
1048 fprintf(stderr, " and ");
Marcin Niestroj20890732016-05-06 14:58:29 +02001049 fprintf(stderr, "0x%lx - 0x%lx",
1050 (unsigned long) block_seek + count,
1051 (unsigned long) write_total - 1);
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001052 }
1053 fprintf(stderr, "\n");
1054#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001055 /* Overwrite the old environment */
1056 memcpy (data + block_seek, buf, count);
1057 } else {
1058 /*
1059 * We get here, iff offset is block-aligned and count is a
1060 * multiple of blocklen - see write_total calculation above
1061 */
1062 data = buf;
1063 }
1064
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001065 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001066 /*
1067 * NAND: calculate which blocks we are writing. We have
1068 * to write one block at a time to skip bad blocks.
1069 */
1070 erasesize = blocklen;
1071 } else {
1072 erasesize = erase_len;
1073 }
1074
1075 erase.length = erasesize;
1076
Remy Bohmercb969eb2011-02-12 19:06:26 +01001077 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001078 while (processed < write_total) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001079 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001080 if (rc < 0) /* block test failed */
1081 return rc;
1082
Andreas Fenkart22281582016-08-29 23:16:57 +02001083 if (blockstart + erasesize > environment_end(dev)) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001084 fprintf (stderr, "End of range reached, aborting\n");
1085 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001086 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001087
1088 if (rc) { /* block is bad */
1089 blockstart += blocklen;
1090 continue;
1091 }
1092
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001093 if (DEVTYPE(dev) != MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +02001094 erase.start = blockstart;
1095 ioctl(fd, MEMUNLOCK, &erase);
1096 /* These do not need an explicit erase cycle */
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001097 if (DEVTYPE(dev) != MTD_DATAFLASH)
Oliver Metze9c66ab2013-08-30 00:56:02 +02001098 if (ioctl(fd, MEMERASE, &erase) != 0) {
1099 fprintf(stderr,
1100 "MTD erase error on %s: %s\n",
1101 DEVNAME(dev), strerror(errno));
1102 return -1;
1103 }
1104 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001105
1106 if (lseek (fd, blockstart, SEEK_SET) == -1) {
wdenk34b613e2002-12-17 01:51:00 +00001107 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001108 "Seek error on %s: %s\n",
1109 DEVNAME (dev), strerror (errno));
1110 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001111 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001112
1113#ifdef DEBUG
Marcin Niestroj20890732016-05-06 14:58:29 +02001114 fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
1115 (unsigned long long) erasesize,
1116 (unsigned long long) blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001117#endif
1118 if (write (fd, data + processed, erasesize) != erasesize) {
1119 fprintf (stderr, "Write error on %s: %s\n",
1120 DEVNAME (dev), strerror (errno));
1121 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001122 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001123
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001124 if (DEVTYPE(dev) != MTD_ABSENT)
Oliver Metze9c66ab2013-08-30 00:56:02 +02001125 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001126
Dustin Byford0983cad2014-03-06 20:48:23 -08001127 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001128 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -08001129 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001130 }
1131
1132 if (write_total > count)
1133 free (data);
1134
1135 return processed;
1136}
1137
1138/*
1139 * Set obsolete flag at offset - NOR flash only
1140 */
1141static int flash_flag_obsolete (int dev, int fd, off_t offset)
1142{
1143 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +02001144 struct erase_info_user erase;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001145
Detlev Zundel2111d3e2010-07-30 11:22:15 +02001146 erase.start = DEVOFFSET (dev);
1147 erase.length = DEVESIZE (dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001148 /* This relies on the fact, that obsolete_flag == 0 */
1149 rc = lseek (fd, offset, SEEK_SET);
1150 if (rc < 0) {
1151 fprintf (stderr, "Cannot seek to set the flag on %s \n",
1152 DEVNAME (dev));
1153 return rc;
1154 }
Detlev Zundel2111d3e2010-07-30 11:22:15 +02001155 ioctl (fd, MEMUNLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001156 rc = write (fd, &obsolete_flag, sizeof (obsolete_flag));
Detlev Zundel2111d3e2010-07-30 11:22:15 +02001157 ioctl (fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001158 if (rc < 0)
1159 perror ("Could not set obsolete flag");
1160
1161 return rc;
1162}
1163
1164static int flash_write (int fd_current, int fd_target, int dev_target)
1165{
1166 int rc;
1167
1168 switch (environment.flag_scheme) {
1169 case FLAG_NONE:
1170 break;
1171 case FLAG_INCREMENTAL:
1172 (*environment.flags)++;
1173 break;
1174 case FLAG_BOOLEAN:
1175 *environment.flags = active_flag;
1176 break;
1177 default:
1178 fprintf (stderr, "Unimplemented flash scheme %u \n",
1179 environment.flag_scheme);
1180 return -1;
1181 }
1182
1183#ifdef DEBUG
Stefan Agnerb21e6022016-07-13 17:14:38 -07001184 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001185 DEVOFFSET (dev_target), DEVNAME (dev_target));
1186#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +01001187
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001188 if (IS_UBI(dev_target)) {
1189 if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0)
1190 return 0;
1191 return ubi_write(fd_target, environment.image, CUR_ENVSIZE);
1192 }
1193
Joe Hershberger671adc42012-10-03 09:38:46 +00001194 rc = flash_write_buf(dev_target, fd_target, environment.image,
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001195 CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001196 if (rc < 0)
1197 return rc;
1198
1199 if (environment.flag_scheme == FLAG_BOOLEAN) {
1200 /* Have to set obsolete flag */
1201 off_t offset = DEVOFFSET (dev_current) +
1202 offsetof (struct env_image_redundant, flags);
1203#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001204 fprintf(stderr,
Stefan Agnerb21e6022016-07-13 17:14:38 -07001205 "Setting obsolete flag in environment at 0x%llx on %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001206 DEVOFFSET (dev_current), DEVNAME (dev_current));
1207#endif
1208 flash_flag_obsolete (dev_current, fd_current, offset);
1209 }
1210
1211 return 0;
1212}
1213
1214static int flash_read (int fd)
1215{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001216 int rc;
1217
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001218 if (IS_UBI(dev_current)) {
1219 DEVTYPE(dev_current) = MTD_ABSENT;
1220
1221 return ubi_read(fd, environment.image, CUR_ENVSIZE);
1222 }
1223
Joe Hershberger671adc42012-10-03 09:38:46 +00001224 rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001225 DEVOFFSET(dev_current));
Marek Vasut05ed2a52014-03-05 19:59:52 +01001226 if (rc != CUR_ENVSIZE)
1227 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001228
Marek Vasut05ed2a52014-03-05 19:59:52 +01001229 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001230}
1231
1232static int flash_io (int mode)
1233{
1234 int fd_current, fd_target, rc, dev_target;
1235
1236 /* dev_current: fd_current, erase_current */
1237 fd_current = open (DEVNAME (dev_current), mode);
1238 if (fd_current < 0) {
1239 fprintf (stderr,
1240 "Can't open %s: %s\n",
1241 DEVNAME (dev_current), strerror (errno));
1242 return -1;
1243 }
1244
1245 if (mode == O_RDWR) {
wdenke7f34c62003-01-11 09:48:40 +00001246 if (HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001247 /* switch to next partition for writing */
1248 dev_target = !dev_current;
1249 /* dev_target: fd_target, erase_target */
1250 fd_target = open (DEVNAME (dev_target), mode);
1251 if (fd_target < 0) {
wdenke7f34c62003-01-11 09:48:40 +00001252 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001253 "Can't open %s: %s\n",
1254 DEVNAME (dev_target),
1255 strerror (errno));
1256 rc = -1;
1257 goto exit;
wdenke7f34c62003-01-11 09:48:40 +00001258 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001259 } else {
1260 dev_target = dev_current;
1261 fd_target = fd_current;
wdenk34b613e2002-12-17 01:51:00 +00001262 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001263
1264 rc = flash_write (fd_current, fd_target, dev_target);
1265
Lukasz Majewskibe3b78a2017-08-27 13:46:22 +02001266 if (fsync(fd_current) &&
1267 !(errno == EINVAL || errno == EROFS)) {
Michael Heimpold5a2b0f12013-05-20 21:34:42 +02001268 fprintf (stderr,
1269 "fsync failed on %s: %s\n",
1270 DEVNAME (dev_current), strerror (errno));
1271 }
1272
wdenke7f34c62003-01-11 09:48:40 +00001273 if (HaveRedundEnv) {
Lukasz Majewskibe3b78a2017-08-27 13:46:22 +02001274 if (fsync(fd_target) &&
1275 !(errno == EINVAL || errno == EROFS)) {
Michael Heimpold5a2b0f12013-05-20 21:34:42 +02001276 fprintf (stderr,
1277 "fsync failed on %s: %s\n",
1278 DEVNAME (dev_current), strerror (errno));
1279 }
1280
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001281 if (close (fd_target)) {
wdenke7f34c62003-01-11 09:48:40 +00001282 fprintf (stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001283 "I/O error on %s: %s\n",
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001284 DEVNAME (dev_target),
wdenk3f9ab982003-04-12 23:38:12 +00001285 strerror (errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001286 rc = -1;
wdenke7f34c62003-01-11 09:48:40 +00001287 }
wdenk34b613e2002-12-17 01:51:00 +00001288 }
wdenk34b613e2002-12-17 01:51:00 +00001289 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001290 rc = flash_read (fd_current);
wdenk34b613e2002-12-17 01:51:00 +00001291 }
1292
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001293exit:
1294 if (close (fd_current)) {
wdenk34b613e2002-12-17 01:51:00 +00001295 fprintf (stderr,
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001296 "I/O error on %s: %s\n",
1297 DEVNAME (dev_current), strerror (errno));
1298 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001299 }
1300
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001301 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001302}
1303
1304/*
wdenk34b613e2002-12-17 01:51:00 +00001305 * Prevent confusion if running from erased flash memory
1306 */
Andreas Fenkart24371902016-04-05 23:13:42 +02001307int fw_env_open(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001308{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001309 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001310 unsigned char flag0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001311 void *addr0 = NULL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001312
wdenk34b613e2002-12-17 01:51:00 +00001313 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001314 unsigned char flag1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001315 void *addr1 = NULL;
wdenke7f34c62003-01-11 09:48:40 +00001316
Marek Vasut05ed2a52014-03-05 19:59:52 +01001317 int ret;
1318
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001319 struct env_image_single *single;
1320 struct env_image_redundant *redundant;
wdenk34b613e2002-12-17 01:51:00 +00001321
Andreas Fenkarte6511302016-05-31 09:21:56 +02001322 if (!opts)
1323 opts = &default_opts;
1324
Andreas Fenkart24371902016-04-05 23:13:42 +02001325 if (parse_config(opts)) /* should fill envdevices */
Stefano Babic4b0a2932017-04-05 18:08:03 +02001326 return -EINVAL;
wdenk3f9ab982003-04-12 23:38:12 +00001327
Joe Hershberger671adc42012-10-03 09:38:46 +00001328 addr0 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001329 if (addr0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001330 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001331 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001332 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001333 ret = -ENOMEM;
1334 goto open_cleanup;
wdenke7f34c62003-01-11 09:48:40 +00001335 }
wdenk3f9ab982003-04-12 23:38:12 +00001336
wdenke7f34c62003-01-11 09:48:40 +00001337 /* read environment from FLASH to local buffer */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001338 environment.image = addr0;
1339
1340 if (HaveRedundEnv) {
1341 redundant = addr0;
1342 environment.crc = &redundant->crc;
1343 environment.flags = &redundant->flags;
1344 environment.data = redundant->data;
1345 } else {
1346 single = addr0;
1347 environment.crc = &single->crc;
1348 environment.flags = NULL;
1349 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001350 }
wdenk3f9ab982003-04-12 23:38:12 +00001351
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001352 dev_current = 0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001353 if (flash_io(O_RDONLY)) {
1354 ret = -EIO;
1355 goto open_cleanup;
1356 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001357
1358 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001359
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001360 crc0_ok = (crc0 == *environment.crc);
wdenke7f34c62003-01-11 09:48:40 +00001361 if (!HaveRedundEnv) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001362 if (!crc0_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001363 fprintf (stderr,
1364 "Warning: Bad CRC, using default environment\n");
Wolfgang Denkc5203f92006-03-12 23:27:46 +01001365 memcpy(environment.data, default_environment, sizeof default_environment);
wdenk34b613e2002-12-17 01:51:00 +00001366 }
wdenke7f34c62003-01-11 09:48:40 +00001367 } else {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001368 flag0 = *environment.flags;
wdenk3f9ab982003-04-12 23:38:12 +00001369
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001370 dev_current = 1;
Joe Hershberger671adc42012-10-03 09:38:46 +00001371 addr1 = calloc(1, CUR_ENVSIZE);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001372 if (addr1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001373 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001374 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001375 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001376 ret = -ENOMEM;
1377 goto open_cleanup;
wdenk3f9ab982003-04-12 23:38:12 +00001378 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001379 redundant = addr1;
wdenk3f9ab982003-04-12 23:38:12 +00001380
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001381 /*
1382 * have to set environment.image for flash_read(), careful -
1383 * other pointers in environment still point inside addr0
1384 */
1385 environment.image = addr1;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001386 if (flash_io(O_RDONLY)) {
1387 ret = -EIO;
1388 goto open_cleanup;
1389 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001390
1391 /* Check flag scheme compatibility */
1392 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1393 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1394 environment.flag_scheme = FLAG_BOOLEAN;
1395 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1396 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1397 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001398 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1399 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1400 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001401 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1402 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1403 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001404 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001405 DEVTYPE(!dev_current) == MTD_ABSENT &&
1406 IS_UBI(dev_current) == IS_UBI(!dev_current)) {
Oliver Metze745d4c2013-08-30 00:56:01 +02001407 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001408 } else {
1409 fprintf (stderr, "Incompatible flash types!\n");
Stefano Babic4b0a2932017-04-05 18:08:03 +02001410 ret = -EINVAL;
1411 goto open_cleanup;
wdenk34b613e2002-12-17 01:51:00 +00001412 }
wdenk3f9ab982003-04-12 23:38:12 +00001413
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001414 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
Marek Vasut05ed2a52014-03-05 19:59:52 +01001415
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001416 crc1_ok = (crc1 == redundant->crc);
1417 flag1 = redundant->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001418
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001419 if (crc0_ok && !crc1_ok) {
1420 dev_current = 0;
1421 } else if (!crc0_ok && crc1_ok) {
1422 dev_current = 1;
1423 } else if (!crc0_ok && !crc1_ok) {
wdenk3f9ab982003-04-12 23:38:12 +00001424 fprintf (stderr,
1425 "Warning: Bad CRC, using default environment\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001426 memcpy (environment.data, default_environment,
1427 sizeof default_environment);
1428 dev_current = 0;
1429 } else {
1430 switch (environment.flag_scheme) {
1431 case FLAG_BOOLEAN:
1432 if (flag0 == active_flag &&
1433 flag1 == obsolete_flag) {
1434 dev_current = 0;
1435 } else if (flag0 == obsolete_flag &&
1436 flag1 == active_flag) {
1437 dev_current = 1;
1438 } else if (flag0 == flag1) {
1439 dev_current = 0;
1440 } else if (flag0 == 0xFF) {
1441 dev_current = 0;
1442 } else if (flag1 == 0xFF) {
1443 dev_current = 1;
1444 } else {
1445 dev_current = 0;
1446 }
1447 break;
1448 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001449 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001450 dev_current = 1;
1451 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001452 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001453 dev_current = 0;
Jon Povey93ccd472011-03-11 14:10:56 +09001454 else /* flag1 > flag0 */
1455 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001456 break;
1457 default:
1458 fprintf (stderr, "Unknown flag scheme %u \n",
1459 environment.flag_scheme);
1460 return -1;
1461 }
1462 }
1463
1464 /*
1465 * If we are reading, we don't need the flag and the CRC any
1466 * more, if we are writing, we will re-calculate CRC and update
1467 * flags before writing out
1468 */
1469 if (dev_current) {
1470 environment.image = addr1;
1471 environment.crc = &redundant->crc;
1472 environment.flags = &redundant->flags;
1473 environment.data = redundant->data;
1474 free (addr0);
1475 } else {
1476 environment.image = addr0;
1477 /* Other pointers are already set */
wdenk3f9ab982003-04-12 23:38:12 +00001478 free (addr1);
wdenk34b613e2002-12-17 01:51:00 +00001479 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001480#ifdef DEBUG
1481 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1482#endif
wdenk34b613e2002-12-17 01:51:00 +00001483 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001484 return 0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001485
1486open_cleanup:
1487 if (addr0)
1488 free(addr0);
1489
1490 if (addr1)
1491 free(addr0);
1492
1493 return ret;
1494}
1495
1496/*
1497 * Simply free allocated buffer with environment
1498 */
1499int fw_env_close(struct env_opts *opts)
1500{
1501 if (environment.image)
1502 free(environment.image);
1503
1504 environment.image = NULL;
1505
1506 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001507}
1508
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001509static int check_device_config(int dev)
1510{
1511 struct stat st;
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001512 int32_t lnum = 0;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001513 int fd, rc = 0;
1514
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001515 /* Fills in IS_UBI(), converts DEVNAME() with ubi volume name */
1516 ubi_check_dev(dev);
1517
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001518 fd = open(DEVNAME(dev), O_RDONLY);
1519 if (fd < 0) {
1520 fprintf(stderr,
1521 "Cannot open %s: %s\n",
1522 DEVNAME(dev), strerror(errno));
1523 return -1;
1524 }
1525
1526 rc = fstat(fd, &st);
1527 if (rc < 0) {
1528 fprintf(stderr, "Cannot stat the file %s\n",
1529 DEVNAME(dev));
1530 goto err;
1531 }
1532
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001533 if (IS_UBI(dev)) {
1534 rc = ioctl(fd, UBI_IOCEBISMAP, &lnum);
1535 if (rc < 0) {
1536 fprintf(stderr, "Cannot get UBI information for %s\n",
1537 DEVNAME(dev));
1538 goto err;
1539 }
1540 } else if (S_ISCHR(st.st_mode)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001541 struct mtd_info_user mtdinfo;
1542 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1543 if (rc < 0) {
1544 fprintf(stderr, "Cannot get MTD information for %s\n",
1545 DEVNAME(dev));
1546 goto err;
1547 }
1548 if (mtdinfo.type != MTD_NORFLASH &&
1549 mtdinfo.type != MTD_NANDFLASH &&
1550 mtdinfo.type != MTD_DATAFLASH &&
1551 mtdinfo.type != MTD_UBIVOLUME) {
1552 fprintf(stderr, "Unsupported flash type %u on %s\n",
1553 mtdinfo.type, DEVNAME(dev));
1554 goto err;
1555 }
1556 DEVTYPE(dev) = mtdinfo.type;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001557 if (DEVESIZE(dev) == 0)
1558 /* Assume the erase size is the same as the env-size */
1559 DEVESIZE(dev) = ENVSIZE(dev);
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001560 } else {
Stefan Agnerb21e6022016-07-13 17:14:38 -07001561 uint64_t size;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001562 DEVTYPE(dev) = MTD_ABSENT;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001563 if (DEVESIZE(dev) == 0)
1564 /* Assume the erase size to be 512 bytes */
1565 DEVESIZE(dev) = 0x200;
Stefan Agnerb21e6022016-07-13 17:14:38 -07001566
1567 /*
1568 * Check for negative offsets, treat it as backwards offset
1569 * from the end of the block device
1570 */
1571 if (DEVOFFSET(dev) < 0) {
1572 rc = ioctl(fd, BLKGETSIZE64, &size);
1573 if (rc < 0) {
1574 fprintf(stderr, "Could not get block device size on %s\n",
1575 DEVNAME(dev));
1576 goto err;
1577 }
1578
1579 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1580#ifdef DEBUG
1581 fprintf(stderr, "Calculated device offset 0x%llx on %s\n",
1582 DEVOFFSET(dev), DEVNAME(dev));
1583#endif
1584 }
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001585 }
1586
Max Krummenacher6c44f542016-11-19 13:58:56 +01001587 if (ENVSECTORS(dev) == 0)
1588 /* Assume enough sectors to cover the environment */
1589 ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev));
1590
1591 if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
1592 fprintf(stderr, "Environment does not start on (erase) block boundary\n");
1593 errno = EINVAL;
1594 return -1;
1595 }
1596
1597 if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
1598 fprintf(stderr, "Environment does not fit into available sectors\n");
1599 errno = EINVAL;
1600 return -1;
1601 }
1602
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001603err:
1604 close(fd);
1605 return rc;
1606}
wdenk34b613e2002-12-17 01:51:00 +00001607
Andreas Fenkart24371902016-04-05 23:13:42 +02001608static int parse_config(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001609{
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001610 int rc;
wdenk34b613e2002-12-17 01:51:00 +00001611
Andreas Fenkarte6511302016-05-31 09:21:56 +02001612 if (!opts)
1613 opts = &default_opts;
Anatolij Gustschine89ead32016-04-29 22:00:11 +02001614
Andreas Fenkarte6511302016-05-31 09:21:56 +02001615#if defined(CONFIG_FILE)
wdenke7f34c62003-01-11 09:48:40 +00001616 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart24371902016-04-05 23:13:42 +02001617 if (get_config(opts->config_file)) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01001618 fprintf(stderr, "Cannot parse config file '%s': %m\n",
Andreas Fenkart24371902016-04-05 23:13:42 +02001619 opts->config_file);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001620 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001621 }
wdenke7f34c62003-01-11 09:48:40 +00001622#else
Tom Rini313e9122014-03-28 12:03:33 -04001623 DEVNAME (0) = DEVICE1_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001624 DEVOFFSET (0) = DEVICE1_OFFSET;
1625 ENVSIZE (0) = ENV1_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001626
1627 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1628 * know DEVTYPE
1629 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001630#ifdef DEVICE1_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001631 DEVESIZE (0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001632#endif
1633#ifdef DEVICE1_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001634 ENVSECTORS (0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001635#endif
1636
wdenk34b613e2002-12-17 01:51:00 +00001637#ifdef HAVE_REDUND
Tom Rini313e9122014-03-28 12:03:33 -04001638 DEVNAME (1) = DEVICE2_NAME;
wdenk3f9ab982003-04-12 23:38:12 +00001639 DEVOFFSET (1) = DEVICE2_OFFSET;
1640 ENVSIZE (1) = ENV2_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001641
1642 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1643 * know DEVTYPE
1644 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001645#ifdef DEVICE2_ESIZE
wdenk3f9ab982003-04-12 23:38:12 +00001646 DEVESIZE (1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001647#endif
1648#ifdef DEVICE2_ENVSECTORS
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001649 ENVSECTORS (1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001650#endif
wdenke7f34c62003-01-11 09:48:40 +00001651 HaveRedundEnv = 1;
wdenk34b613e2002-12-17 01:51:00 +00001652#endif
wdenke7f34c62003-01-11 09:48:40 +00001653#endif
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001654 rc = check_device_config(0);
1655 if (rc < 0)
1656 return rc;
wdenk3f9ab982003-04-12 23:38:12 +00001657
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001658 if (HaveRedundEnv) {
1659 rc = check_device_config(1);
1660 if (rc < 0)
1661 return rc;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001662
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001663 if (ENVSIZE(0) != ENVSIZE(1)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001664 fprintf(stderr,
Andreas Fenkart31f2e052016-08-17 23:41:53 +02001665 "Redundant environments have unequal size");
1666 return -1;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001667 }
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001668 }
1669
1670 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
1671 if (HaveRedundEnv)
1672 usable_envsize -= sizeof(char);
1673
wdenk34b613e2002-12-17 01:51:00 +00001674 return 0;
1675}
wdenke7f34c62003-01-11 09:48:40 +00001676
1677#if defined(CONFIG_FILE)
1678static int get_config (char *fname)
1679{
1680 FILE *fp;
1681 int i = 0;
1682 int rc;
1683 char dump[128];
Tom Rini313e9122014-03-28 12:03:33 -04001684 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001685
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001686 fp = fopen (fname, "r");
1687 if (fp == NULL)
1688 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001689
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001690 while (i < 2 && fgets (dump, sizeof (dump), fp)) {
wdenke7f34c62003-01-11 09:48:40 +00001691 /* Skip incomplete conversions and comment strings */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001692 if (dump[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001693 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001694
Stefan Agnerb21e6022016-07-13 17:14:38 -07001695 rc = sscanf(dump, "%ms %lli %lx %lx %lx",
1696 &devname,
1697 &DEVOFFSET(i),
1698 &ENVSIZE(i),
1699 &DEVESIZE(i),
1700 &ENVSECTORS(i));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001701
Remy Bohmercb969eb2011-02-12 19:06:26 +01001702 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001703 continue;
1704
Tom Rini313e9122014-03-28 12:03:33 -04001705 DEVNAME(i) = devname;
1706
Max Krummenacher6c44f542016-11-19 13:58:56 +01001707 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1708 * know DEVTYPE
1709 */
wdenke7f34c62003-01-11 09:48:40 +00001710
1711 i++;
1712 }
wdenk3f9ab982003-04-12 23:38:12 +00001713 fclose (fp);
1714
wdenke7f34c62003-01-11 09:48:40 +00001715 HaveRedundEnv = i - 1;
wdenk3f9ab982003-04-12 23:38:12 +00001716 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001717 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001718 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001719 } else
1720 return 0;
1721}
1722#endif