blob: 425faf380fbb283562bde553fa80e6fe294c4422 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00002/*
Detlev Zundel2111d3e2010-07-30 11:22:15 +02003 * (C) Copyright 2000-2010
wdenk34b613e2002-12-17 01:51:00 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02006 * (C) Copyright 2008
7 * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
wdenk34b613e2002-12-17 01:51:00 +00008 */
9
Jörg Krause983487f2015-04-22 21:36:22 +020010#define _GNU_SOURCE
11
Rasmus Villemoes68254602024-10-16 17:10:35 +020012#include "fw_env_private.h"
13
Peter Robinson656ee042015-12-09 07:15:33 +000014#include <compiler.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060015#include <env.h>
wdenk34b613e2002-12-17 01:51:00 +000016#include <errno.h>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060017#include <env_flags.h>
wdenk34b613e2002-12-17 01:51:00 +000018#include <fcntl.h>
Alex Kiernan7150aaa2018-03-09 12:13:02 +000019#include <libgen.h>
Stefan Agnerb21e6022016-07-13 17:14:38 -070020#include <linux/fs.h>
Joe Hershbergere79a91f2012-10-12 10:23:37 +000021#include <linux/stringify.h>
Andreas Fenkartd0e12cb2016-03-11 09:39:35 +010022#include <ctype.h>
wdenk34b613e2002-12-17 01:51:00 +000023#include <stdio.h>
24#include <stdlib.h>
25#include <stddef.h>
26#include <string.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <sys/stat.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070030#include <u-boot/crc.h>
wdenk34b613e2002-12-17 01:51:00 +000031#include <unistd.h>
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080032#include <dirent.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010033
34#ifdef MTD_OLD
Wolfgang Denk4cd94a72008-09-02 21:17:36 +020035# include <stdint.h>
Markus Klotzbücher94db5572007-11-27 10:23:20 +010036# include <linux/mtd/mtd.h>
37#else
Wolfgang Denkc7d16fd2008-01-08 22:58:27 +010038# define __user /* nothing */
Markus Klotzbücher94db5572007-11-27 10:23:20 +010039# include <mtd/mtd-user.h>
40#endif
wdenk34b613e2002-12-17 01:51:00 +000041
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080042#include <mtd/ubi-user.h>
43
Markus Klotzbücher94db5572007-11-27 10:23:20 +010044#include "fw_env.h"
wdenk34b613e2002-12-17 01:51:00 +000045
Andreas Fenkarte6511302016-05-31 09:21:56 +020046struct env_opts default_opts = {
47#ifdef CONFIG_FILE
48 .config_file = CONFIG_FILE
49#endif
50};
51
Marek Vasut05ed2a52014-03-05 19:59:52 +010052#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
53
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020054#define min(x, y) ({ \
55 typeof(x) _min1 = (x); \
56 typeof(y) _min2 = (y); \
57 (void) (&_min1 == &_min2); \
58 _min1 < _min2 ? _min1 : _min2; })
59
60struct envdev_s {
Tom Rini313e9122014-03-28 12:03:33 -040061 const char *devname; /* Device name */
Stefan Agnerb21e6022016-07-13 17:14:38 -070062 long long devoff; /* Device offset */
wdenk3f9ab982003-04-12 23:38:12 +000063 ulong env_size; /* environment size */
64 ulong erase_size; /* device erase size */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020065 ulong env_sectors; /* number of environment sectors */
66 uint8_t mtd_type; /* type of the MTD device */
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080067 int is_ubi; /* set if we use UBI volume */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020068};
wdenk34b613e2002-12-17 01:51:00 +000069
Alex Kiernane9c62202018-03-09 12:12:59 +000070static struct envdev_s envdevices[2] = {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020071 {
72 .mtd_type = MTD_ABSENT,
73 }, {
74 .mtd_type = MTD_ABSENT,
75 },
76};
Alex Kiernane9c62202018-03-09 12:12:59 +000077
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020078static int dev_current;
wdenk34b613e2002-12-17 01:51:00 +000079
80#define DEVNAME(i) envdevices[(i)].devname
wdenke7f34c62003-01-11 09:48:40 +000081#define DEVOFFSET(i) envdevices[(i)].devoff
wdenk34b613e2002-12-17 01:51:00 +000082#define ENVSIZE(i) envdevices[(i)].env_size
83#define DEVESIZE(i) envdevices[(i)].erase_size
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020084#define ENVSECTORS(i) envdevices[(i)].env_sectors
85#define DEVTYPE(i) envdevices[(i)].mtd_type
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -080086#define IS_UBI(i) envdevices[(i)].is_ubi
wdenk34b613e2002-12-17 01:51:00 +000087
Joe Hershberger671adc42012-10-03 09:38:46 +000088#define CUR_ENVSIZE ENVSIZE(dev_current)
wdenk34b613e2002-12-17 01:51:00 +000089
Andreas Fenkartb8bf4042016-04-19 22:43:42 +020090static unsigned long usable_envsize;
91#define ENV_SIZE usable_envsize
wdenk34b613e2002-12-17 01:51:00 +000092
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020093struct env_image_single {
Alex Kiernane9c62202018-03-09 12:12:59 +000094 uint32_t crc; /* CRC32 over data bytes */
95 char data[];
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020096};
wdenk34b613e2002-12-17 01:51:00 +000097
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +020098struct env_image_redundant {
Alex Kiernane9c62202018-03-09 12:12:59 +000099 uint32_t crc; /* CRC32 over data bytes */
100 unsigned char flags; /* active or obsolete */
101 char data[];
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200102};
103
104enum flag_scheme {
105 FLAG_NONE,
106 FLAG_BOOLEAN,
107 FLAG_INCREMENTAL,
108};
109
110struct environment {
Alex Kiernane9c62202018-03-09 12:12:59 +0000111 void *image;
112 uint32_t *crc;
113 unsigned char *flags;
114 char *data;
115 enum flag_scheme flag_scheme;
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200116 int dirty;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200117};
118
119static struct environment environment = {
120 .flag_scheme = FLAG_NONE,
121};
wdenk34b613e2002-12-17 01:51:00 +0000122
Alex Kiernanf46d0ca2018-03-09 12:13:00 +0000123static int have_redund_env;
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,
Miaoqian Lin64f2dda2022-09-19 08:28:09 +0400196 volname) == 0) {
197 closedir(sysfs_ubi);
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -0800198 return volnum;
Miaoqian Lin64f2dda2022-09-19 08:28:09 +0400199 }
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -0800200 }
201 }
Miaoqian Lin64f2dda2022-09-19 08:28:09 +0400202 closedir(sysfs_ubi);
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -0800203
204 return -1;
205}
206
207static int ubi_get_devnum_by_devname(const char *devname)
208{
209 int devnum;
210 int ret;
211
212 ret = sscanf(devname + sizeof(UBI_DEV_START) - 1, "%d", &devnum);
213 if (ret != 1)
214 return -1;
215
216 return devnum;
217}
218
219static const char *ubi_get_volume_devname(const char *devname,
220 const char *volname)
221{
222 char *volume_devname;
223 int volnum;
224 int devnum;
225 int ret;
226
227 devnum = ubi_get_devnum_by_devname(devname);
228 if (devnum < 0)
229 return NULL;
230
231 volnum = ubi_get_volnum_by_name(devnum, volname);
232 if (volnum < 0)
233 return NULL;
234
235 ret = asprintf(&volume_devname, "%s_%d", devname, volnum);
236 if (ret < 0)
237 return NULL;
238
239#ifdef DEBUG
240 fprintf(stderr, "Found ubi volume \"%s:%s\" -> %s\n",
241 devname, volname, volume_devname);
242#endif
243
244 return volume_devname;
245}
246
247static void ubi_check_dev(unsigned int dev_id)
248{
249 char *devname = (char *)DEVNAME(dev_id);
250 char *pname;
251 const char *volname = NULL;
252 const char *volume_devname;
253
254 if (!is_ubi_devname(DEVNAME(dev_id)))
255 return;
256
257 IS_UBI(dev_id) = 1;
258
259 for (pname = devname; *pname != '\0'; pname++) {
260 if (*pname == ':') {
261 *pname = '\0';
262 volname = pname + 1;
263 break;
264 }
265 }
266
267 if (volname) {
268 /* Let's find real volume device name */
269 volume_devname = ubi_get_volume_devname(devname, volname);
270 if (!volume_devname) {
271 fprintf(stderr, "Didn't found ubi volume \"%s\"\n",
272 volname);
273 return;
274 }
275
276 free(devname);
277 DEVNAME(dev_id) = volume_devname;
278 }
279}
280
281static int ubi_update_start(int fd, int64_t bytes)
282{
283 if (ioctl(fd, UBI_IOCVOLUP, &bytes))
284 return -1;
285 return 0;
286}
287
288static int ubi_read(int fd, void *buf, size_t count)
289{
290 ssize_t ret;
291
292 while (count > 0) {
293 ret = read(fd, buf, count);
294 if (ret > 0) {
295 count -= ret;
296 buf += ret;
297
298 continue;
299 }
300
301 if (ret == 0) {
302 /*
303 * Happens in case of too short volume data size. If we
304 * return error status we will fail it will be treated
305 * as UBI device error.
306 *
307 * Leave catching this error to CRC check.
308 */
309 fprintf(stderr, "Warning: end of data on ubi volume\n");
310 return 0;
311 } else if (errno == EBADF) {
312 /*
313 * Happens in case of corrupted volume. The same as
314 * above, we cannot return error now, as we will still
315 * be able to successfully write environment later.
316 */
317 fprintf(stderr, "Warning: corrupted volume?\n");
318 return 0;
319 } else if (errno == EINTR) {
320 continue;
321 }
322
323 fprintf(stderr, "Cannot read %u bytes from ubi volume, %s\n",
324 (unsigned int)count, strerror(errno));
325 return -1;
326 }
327
328 return 0;
329}
330
331static int ubi_write(int fd, const void *buf, size_t count)
332{
333 ssize_t ret;
334
335 while (count > 0) {
336 ret = write(fd, buf, count);
337 if (ret <= 0) {
338 if (ret < 0 && errno == EINTR)
339 continue;
340
341 fprintf(stderr, "Cannot write %u bytes to ubi volume\n",
342 (unsigned int)count);
343 return -1;
344 }
345
346 count -= ret;
347 buf += ret;
348 }
349
350 return 0;
351}
352
Rafał Miłecki840ec192022-01-12 12:47:05 +0100353static int flash_io(int mode, void *buf, size_t count);
Andreas Fenkart24371902016-04-05 23:13:42 +0200354static int parse_config(struct env_opts *opts);
wdenk3f9ab982003-04-12 23:38:12 +0000355
wdenke7f34c62003-01-11 09:48:40 +0000356#if defined(CONFIG_FILE)
Alex Kiernane9c62202018-03-09 12:12:59 +0000357static int get_config(char *);
wdenke7f34c62003-01-11 09:48:40 +0000358#endif
wdenk34b613e2002-12-17 01:51:00 +0000359
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100360static char *skip_chars(char *s)
Stefano Babicada628b2010-05-24 12:08:16 +0200361{
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100362 for (; *s != '\0'; s++) {
Stefan Agner87833242018-03-01 14:06:32 +0100363 if (isblank(*s) || *s == '=')
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100364 return s;
365 }
366 return NULL;
367}
Stefano Babicada628b2010-05-24 12:08:16 +0200368
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100369static char *skip_blanks(char *s)
370{
371 for (; *s != '\0'; s++) {
372 if (!isblank(*s))
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100373 return s;
Stefano Babicada628b2010-05-24 12:08:16 +0200374 }
Andreas Fenkarte42c68e2016-03-11 09:39:36 +0100375 return NULL;
Stefano Babicada628b2010-05-24 12:08:16 +0200376}
377
wdenk34b613e2002-12-17 01:51:00 +0000378/*
Andreas Fenkart3ff10742016-07-16 17:06:14 +0200379 * s1 is either a simple 'name', or a 'name=value' pair.
380 * s2 is a 'name=value' pair.
381 * If the names match, return the value of s2, else NULL.
382 */
383static char *envmatch(char *s1, char *s2)
384{
385 if (s1 == NULL || s2 == NULL)
386 return NULL;
387
388 while (*s1 == *s2++)
389 if (*s1++ == '=')
390 return s2;
391 if (*s1 == '\0' && *(s2 - 1) == '=')
392 return s2;
393 return NULL;
394}
395
396/**
wdenk34b613e2002-12-17 01:51:00 +0000397 * Search the environment for a variable.
398 * Return the value, if found, or NULL, if not found.
399 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000400char *fw_getenv(char *name)
wdenk34b613e2002-12-17 01:51:00 +0000401{
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100402 char *env, *nxt;
wdenk34b613e2002-12-17 01:51:00 +0000403
wdenk3f9ab982003-04-12 23:38:12 +0000404 for (env = environment.data; *env; env = nxt + 1) {
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100405 char *val;
wdenk34b613e2002-12-17 01:51:00 +0000406
wdenk3f9ab982003-04-12 23:38:12 +0000407 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000408 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000409 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000410 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200411 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000412 }
413 }
Alex Kiernane9c62202018-03-09 12:12:59 +0000414 val = envmatch(name, env);
wdenk34b613e2002-12-17 01:51:00 +0000415 if (!val)
416 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200417 return val;
wdenk34b613e2002-12-17 01:51:00 +0000418 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200419 return NULL;
wdenk34b613e2002-12-17 01:51:00 +0000420}
421
422/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600423 * Search the default environment for a variable.
424 * Return the value, if found, or NULL, if not found.
425 */
426char *fw_getdefenv(char *name)
427{
428 char *env, *nxt;
429
430 for (env = default_environment; *env; env = nxt + 1) {
431 char *val;
432
433 for (nxt = env; *nxt; ++nxt) {
434 if (nxt >= &default_environment[ENV_SIZE]) {
435 fprintf(stderr, "## Error: "
436 "default environment not terminated\n");
437 return NULL;
438 }
439 }
440 val = envmatch(name, env);
441 if (!val)
442 continue;
443 return val;
444 }
445 return NULL;
446}
447
448/*
wdenk34b613e2002-12-17 01:51:00 +0000449 * Print the current definition of one, or more, or all
450 * environment variables
451 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200452int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000453{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100454 int i, rc = 0;
wdenk34b613e2002-12-17 01:51:00 +0000455
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200456 if (value_only && argc != 1) {
457 fprintf(stderr,
Alex Kiernan688cee92018-02-11 17:16:46 +0000458 "## Error: `-n'/`--noheader' option requires exactly one argument\n");
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200459 return -1;
460 }
461
Andreas Fenkarte6511302016-05-31 09:21:56 +0200462 if (!opts)
463 opts = &default_opts;
464
Andreas Fenkart24371902016-04-05 23:13:42 +0200465 if (fw_env_open(opts))
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200466 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000467
Alex Kiernane9c62202018-03-09 12:12:59 +0000468 if (argc == 0) { /* Print all env variables */
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200469 char *env, *nxt;
wdenk3f9ab982003-04-12 23:38:12 +0000470 for (env = environment.data; *env; env = nxt + 1) {
471 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000472 if (nxt >= &environment.data[ENV_SIZE]) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000473 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000474 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200475 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000476 }
477 }
478
Alex Kiernane9c62202018-03-09 12:12:59 +0000479 printf("%s\n", env);
wdenk34b613e2002-12-17 01:51:00 +0000480 }
Stefano Babic4b0a2932017-04-05 18:08:03 +0200481 fw_env_close(opts);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200482 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000483 }
484
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200485 for (i = 0; i < argc; ++i) { /* print a subset of env variables */
Markus Klotzbücher94db5572007-11-27 10:23:20 +0100486 char *name = argv[i];
487 char *val = NULL;
wdenk34b613e2002-12-17 01:51:00 +0000488
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200489 val = fw_getenv(name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700490 if (!val) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000491 fprintf(stderr, "## Error: \"%s\" not defined\n", name);
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700492 rc = -1;
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200493 continue;
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700494 }
Andreas Fenkart33aaef92016-07-16 17:06:15 +0200495
496 if (value_only) {
497 puts(val);
498 break;
499 }
500
501 printf("%s=%s\n", name, val);
wdenk34b613e2002-12-17 01:51:00 +0000502 }
Grant Erickson73a4e5e2008-05-06 20:16:15 -0700503
Stefano Babic4b0a2932017-04-05 18:08:03 +0200504 fw_env_close(opts);
505
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200506 return rc;
wdenk34b613e2002-12-17 01:51:00 +0000507}
508
Stefano Babic4b0a2932017-04-05 18:08:03 +0200509int fw_env_flush(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +0000510{
Andreas Fenkarte6511302016-05-31 09:21:56 +0200511 if (!opts)
512 opts = &default_opts;
513
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200514 if (!environment.dirty)
515 return 0;
516
Stefano Babicada628b2010-05-24 12:08:16 +0200517 /*
518 * Update CRC
519 */
520 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
wdenk34b613e2002-12-17 01:51:00 +0000521
Stefano Babicada628b2010-05-24 12:08:16 +0200522 /* write environment back to flash */
Rafał Miłecki840ec192022-01-12 12:47:05 +0100523 if (flash_io(O_RDWR, environment.image, CUR_ENVSIZE)) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000524 fprintf(stderr, "Error: can't write fw_env to flash\n");
525 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000526 }
527
Stefano Babicada628b2010-05-24 12:08:16 +0200528 return 0;
529}
wdenk34b613e2002-12-17 01:51:00 +0000530
Stefano Babicada628b2010-05-24 12:08:16 +0200531/*
532 * Set/Clear a single variable in the environment.
533 * This is called in sequence to update the environment
534 * in RAM without updating the copy in flash after each set
535 */
536int fw_env_write(char *name, char *value)
537{
538 int len;
539 char *env, *nxt;
540 char *oldval = NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600541 int deleting, creating, overwriting;
wdenk34b613e2002-12-17 01:51:00 +0000542
543 /*
544 * search if variable with this name already exists
545 */
wdenk3f9ab982003-04-12 23:38:12 +0000546 for (nxt = env = environment.data; *env; env = nxt + 1) {
547 for (nxt = env; *nxt; ++nxt) {
wdenk34b613e2002-12-17 01:51:00 +0000548 if (nxt >= &environment.data[ENV_SIZE]) {
Stefano Babicada628b2010-05-24 12:08:16 +0200549 fprintf(stderr, "## Error: "
wdenk34b613e2002-12-17 01:51:00 +0000550 "environment not terminated\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200551 errno = EINVAL;
552 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000553 }
554 }
Alex Kiernane9c62202018-03-09 12:12:59 +0000555 oldval = envmatch(name, env);
556 if (oldval)
wdenk34b613e2002-12-17 01:51:00 +0000557 break;
558 }
559
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600560 deleting = (oldval && !(value && strlen(value)));
561 creating = (!oldval && (value && strlen(value)));
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200562 overwriting = (oldval && (value && strlen(value) &&
563 strcmp(oldval, value)));
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600564
565 /* check for permission */
566 if (deleting) {
567 if (env_flags_validate_varaccess(name,
568 ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
569 printf("Can't delete \"%s\"\n", name);
570 errno = EROFS;
571 return -1;
572 }
573 } else if (overwriting) {
574 if (env_flags_validate_varaccess(name,
575 ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
576 printf("Can't overwrite \"%s\"\n", name);
577 errno = EROFS;
578 return -1;
579 } else if (env_flags_validate_varaccess(name,
Alex Kiernane9c62202018-03-09 12:12:59 +0000580 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600581 const char *defval = fw_getdefenv(name);
582
583 if (defval == NULL)
584 defval = "";
585 if (strcmp(oldval, defval)
586 != 0) {
587 printf("Can't overwrite \"%s\"\n", name);
588 errno = EROFS;
589 return -1;
590 }
591 }
592 } else if (creating) {
593 if (env_flags_validate_varaccess(name,
594 ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
595 printf("Can't create \"%s\"\n", name);
596 errno = EROFS;
597 return -1;
598 }
599 } else
600 /* Nothing to do */
601 return 0;
602
Rasmus Villemoes06a79642018-09-27 22:45:26 +0200603 environment.dirty = 1;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600604 if (deleting || overwriting) {
wdenk34b613e2002-12-17 01:51:00 +0000605 if (*++nxt == '\0') {
606 *env = '\0';
607 } else {
608 for (;;) {
609 *env = *nxt++;
610 if ((*env == '\0') && (*nxt == '\0'))
611 break;
612 ++env;
613 }
614 }
615 *++env = '\0';
616 }
617
618 /* Delete only ? */
Stefano Babicada628b2010-05-24 12:08:16 +0200619 if (!value || !strlen(value))
620 return 0;
wdenk34b613e2002-12-17 01:51:00 +0000621
622 /*
623 * Append new definition at the end
624 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000625 for (env = environment.data; *env || *(env + 1); ++env)
626 ;
wdenk34b613e2002-12-17 01:51:00 +0000627 if (env > environment.data)
628 ++env;
629 /*
630 * Overflow when:
Joe Hershberger671adc42012-10-03 09:38:46 +0000631 * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
wdenk34b613e2002-12-17 01:51:00 +0000632 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000633 len = strlen(name) + 2;
wdenk34b613e2002-12-17 01:51:00 +0000634 /* add '=' for first arg, ' ' for all others */
Stefano Babicada628b2010-05-24 12:08:16 +0200635 len += strlen(value) + 1;
636
wdenk3f9ab982003-04-12 23:38:12 +0000637 if (len > (&environment.data[ENV_SIZE] - env)) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000638 fprintf(stderr,
639 "Error: environment overflow, \"%s\" deleted\n", name);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200640 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000641 }
Stefano Babicada628b2010-05-24 12:08:16 +0200642
wdenk34b613e2002-12-17 01:51:00 +0000643 while ((*env = *name++) != '\0')
644 env++;
Stefano Babicada628b2010-05-24 12:08:16 +0200645 *env = '=';
646 while ((*++env = *value++) != '\0')
647 ;
648
649 /* end is marked with double '\0' */
650 *++env = '\0';
651
652 return 0;
653}
654
655/*
656 * Deletes or sets environment variables. Returns -1 and sets errno error codes:
657 * 0 - OK
658 * EINVAL - need at least 1 argument
659 * EROFS - certain variables ("ethaddr", "serial#") cannot be
660 * modified or deleted
661 *
662 */
Simon Glass6a38e412017-08-03 12:22:09 -0600663int fw_env_set(int argc, char *argv[], struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200664{
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100665 int i;
Mike Frysinger178ec772012-11-10 19:47:45 +0000666 size_t len;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100667 char *name, **valv;
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200668 char *oldval;
Stefano Babicada628b2010-05-24 12:08:16 +0200669 char *value = NULL;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100670 int valc;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200671 int ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200672
Andreas Fenkarte6511302016-05-31 09:21:56 +0200673 if (!opts)
674 opts = &default_opts;
675
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100676 if (argc < 1) {
677 fprintf(stderr, "## Error: variable name missing\n");
Marek Vasut05ed2a52014-03-05 19:59:52 +0100678 errno = EINVAL;
679 return -1;
680 }
681
Andreas Fenkart24371902016-04-05 23:13:42 +0200682 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200683 fprintf(stderr, "Error: environment not initialized\n");
684 return -1;
685 }
686
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100687 name = argv[0];
688 valv = argv + 1;
689 valc = argc - 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200690
Stefano Babic4b0a2932017-04-05 18:08:03 +0200691 if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
692 fw_env_close(opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200693 return -1;
Stefano Babic4b0a2932017-04-05 18:08:03 +0200694 }
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600695
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000696 len = 0;
Andreas Fenkart4e336ee2015-12-09 13:13:21 +0100697 for (i = 0; i < valc; ++i) {
698 char *val = valv[i];
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000699 size_t val_len = strlen(val);
700
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000701 if (value)
702 value[len - 1] = ' ';
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200703 oldval = value;
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000704 value = realloc(value, len + val_len + 1);
Stefano Babicada628b2010-05-24 12:08:16 +0200705 if (!value) {
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000706 fprintf(stderr,
Luka Perkovd5982712011-09-05 23:40:13 +0200707 "Cannot malloc %zu bytes: %s\n",
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000708 len, strerror(errno));
xypron.glpk@gmx.de2a3325c2017-04-15 13:05:40 +0200709 free(oldval);
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000710 return -1;
Stefano Babicada628b2010-05-24 12:08:16 +0200711 }
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000712
713 memcpy(value + len, val, val_len);
714 len += val_len;
Joe Hershberger03bf21d2012-10-15 15:29:24 +0000715 value[len++] = '\0';
wdenk34b613e2002-12-17 01:51:00 +0000716 }
717
Stefano Babicada628b2010-05-24 12:08:16 +0200718 fw_env_write(name, value);
wdenk34b613e2002-12-17 01:51:00 +0000719
Joe Hershbergered97f4c2012-10-03 09:38:47 +0000720 free(value);
wdenk34b613e2002-12-17 01:51:00 +0000721
Stefano Babic4b0a2932017-04-05 18:08:03 +0200722 ret = fw_env_flush(opts);
723 fw_env_close(opts);
724
725 return ret;
Stefano Babicada628b2010-05-24 12:08:16 +0200726}
wdenk34b613e2002-12-17 01:51:00 +0000727
Stefano Babicada628b2010-05-24 12:08:16 +0200728/*
729 * Parse a file and configure the u-boot variables.
730 * The script file has a very simple format, as follows:
731 *
732 * Each line has a couple with name, value:
733 * <white spaces>variable_name<white spaces>variable_value
734 *
735 * Both variable_name and variable_value are interpreted as strings.
736 * Any character after <white spaces> and before ending \r\n is interpreted
737 * as variable's value (no comment allowed on these lines !)
738 *
739 * Comments are allowed if the first character in the line is #
740 *
741 * Returns -1 and sets errno error codes:
742 * 0 - OK
743 * -1 - Error
744 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200745int fw_parse_script(char *fname, struct env_opts *opts)
Stefano Babicada628b2010-05-24 12:08:16 +0200746{
747 FILE *fp;
Alex Kiernan66df4e22018-06-07 12:20:05 +0000748 char *line = NULL;
749 size_t linesize = 0;
Stefano Babicada628b2010-05-24 12:08:16 +0200750 char *name;
751 char *val;
752 int lineno = 0;
753 int len;
754 int ret = 0;
755
Andreas Fenkarte6511302016-05-31 09:21:56 +0200756 if (!opts)
757 opts = &default_opts;
758
Andreas Fenkart24371902016-04-05 23:13:42 +0200759 if (fw_env_open(opts)) {
Stefano Babicada628b2010-05-24 12:08:16 +0200760 fprintf(stderr, "Error: environment not initialized\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200761 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000762 }
763
Stefano Babicada628b2010-05-24 12:08:16 +0200764 if (strcmp(fname, "-") == 0)
765 fp = stdin;
766 else {
767 fp = fopen(fname, "r");
768 if (fp == NULL) {
769 fprintf(stderr, "I cannot open %s for reading\n",
Alex Kiernane9c62202018-03-09 12:12:59 +0000770 fname);
Stefano Babicada628b2010-05-24 12:08:16 +0200771 return -1;
772 }
773 }
774
Alex Kiernan66df4e22018-06-07 12:20:05 +0000775 while ((len = getline(&line, &linesize, fp)) != -1) {
Stefano Babicada628b2010-05-24 12:08:16 +0200776 lineno++;
Stefano Babicada628b2010-05-24 12:08:16 +0200777
778 /*
Alex Kiernan66df4e22018-06-07 12:20:05 +0000779 * Read a whole line from the file. If the line is not
780 * terminated, reports an error and exit.
Stefano Babicada628b2010-05-24 12:08:16 +0200781 */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000782 if (line[len - 1] != '\n') {
Stefano Babicada628b2010-05-24 12:08:16 +0200783 fprintf(stderr,
Alex Kiernan66df4e22018-06-07 12:20:05 +0000784 "Line %d not correctly terminated\n",
Stefano Babicada628b2010-05-24 12:08:16 +0200785 lineno);
786 ret = -1;
787 break;
788 }
789
790 /* Drop ending line feed / carriage return */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000791 line[--len] = '\0';
792 if (len && line[len - 1] == '\r')
793 line[--len] = '\0';
Stefano Babicada628b2010-05-24 12:08:16 +0200794
795 /* Skip comment or empty lines */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000796 if (len == 0 || line[0] == '#')
Stefano Babicada628b2010-05-24 12:08:16 +0200797 continue;
798
799 /*
Alex Kiernan66df4e22018-06-07 12:20:05 +0000800 * Search for variable's name remove leading whitespaces
Stefano Babicada628b2010-05-24 12:08:16 +0200801 */
Alex Kiernan66df4e22018-06-07 12:20:05 +0000802 name = skip_blanks(line);
Stefano Babicada628b2010-05-24 12:08:16 +0200803 if (!name)
804 continue;
805
806 /* The first white space is the end of variable name */
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100807 val = skip_chars(name);
Stefano Babicada628b2010-05-24 12:08:16 +0200808 len = strlen(name);
809 if (val) {
810 *val++ = '\0';
811 if ((val - name) < len)
Andreas Fenkart191d26f2016-03-11 09:39:37 +0100812 val = skip_blanks(val);
Stefano Babicada628b2010-05-24 12:08:16 +0200813 else
814 val = NULL;
815 }
Stefano Babicada628b2010-05-24 12:08:16 +0200816#ifdef DEBUG
817 fprintf(stderr, "Setting %s : %s\n",
818 name, val ? val : " removed");
819#endif
820
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600821 if (env_flags_validate_type(name, val) < 0) {
822 ret = -1;
823 break;
824 }
825
Stefano Babicada628b2010-05-24 12:08:16 +0200826 /*
827 * If there is an error setting a variable,
828 * try to save the environment and returns an error
829 */
830 if (fw_env_write(name, val)) {
831 fprintf(stderr,
Alex Kiernane9c62202018-03-09 12:12:59 +0000832 "fw_env_write returns with error : %s\n",
Stefano Babicada628b2010-05-24 12:08:16 +0200833 strerror(errno));
834 ret = -1;
835 break;
836 }
837
838 }
Alex Kiernan66df4e22018-06-07 12:20:05 +0000839 free(line);
Stefano Babicada628b2010-05-24 12:08:16 +0200840
841 /* Close file if not stdin */
842 if (strcmp(fname, "-") != 0)
843 fclose(fp);
844
Stefano Babic4b0a2932017-04-05 18:08:03 +0200845 ret |= fw_env_flush(opts);
846
847 fw_env_close(opts);
Stefano Babicada628b2010-05-24 12:08:16 +0200848
849 return ret;
wdenk34b613e2002-12-17 01:51:00 +0000850}
851
Andreas Fenkart22281582016-08-29 23:16:57 +0200852/**
Shyam Sainiefe757c2018-06-07 19:47:19 +0530853 * environment_end() - compute offset of first byte right after environment
Andreas Fenkart22281582016-08-29 23:16:57 +0200854 * @dev - index of enviroment buffer
855 * Return:
Shyam Sainiefe757c2018-06-07 19:47:19 +0530856 * device offset of first byte right after environment
Andreas Fenkart22281582016-08-29 23:16:57 +0200857 */
858off_t environment_end(int dev)
859{
860 /* environment is block aligned */
861 return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
862}
863
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200864/*
865 * Test for bad block on NAND, just returns 0 on NOR, on NAND:
866 * 0 - block is good
867 * > 0 - block is bad
868 * < 0 - failed to test
869 */
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200870static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
wdenk34b613e2002-12-17 01:51:00 +0000871{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200872 if (mtd_type == MTD_NANDFLASH) {
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200873 int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart);
wdenk34b613e2002-12-17 01:51:00 +0000874
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200875 if (badblock < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +0000876 perror("Cannot read bad block mark");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200877 return badblock;
878 }
879
880 if (badblock) {
881#ifdef DEBUG
Alex Kiernane9c62202018-03-09 12:12:59 +0000882 fprintf(stderr, "Bad block at 0x%llx, skipping\n",
Andreas Fenkartae3b3202016-08-29 23:16:58 +0200883 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200884#endif
885 return badblock;
886 }
wdenk34b613e2002-12-17 01:51:00 +0000887 }
888
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200889 return 0;
890}
891
892/*
893 * Read data from flash at an offset into a provided buffer. On NAND it skips
894 * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from
895 * the DEVOFFSET (dev) block. On NOR the loop is only run once.
896 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000897static int flash_read_buf(int dev, int fd, void *buf, size_t count,
898 off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200899{
900 size_t blocklen; /* erase / write length - one block on NAND,
901 0 on NOR */
902 size_t processed = 0; /* progress counter */
903 size_t readlen = count; /* current read length */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200904 off_t block_seek; /* offset inside the current block to the start
905 of the data */
906 loff_t blockstart; /* running start of the current block -
907 MEMGETBADBLOCK needs 64 bits */
908 int rc;
909
Alex Kiernane9c62202018-03-09 12:12:59 +0000910 blockstart = (offset / DEVESIZE(dev)) * DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200911
912 /* Offset inside a block */
913 block_seek = offset - blockstart;
914
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200915 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200916 /*
917 * NAND: calculate which blocks we are reading. We have
918 * to read one block at a time to skip bad blocks.
919 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000920 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200921
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200922 /* Limit to one block for the first read */
923 if (readlen > blocklen - block_seek)
924 readlen = blocklen - block_seek;
925 } else {
926 blocklen = 0;
wdenke7f34c62003-01-11 09:48:40 +0000927 }
wdenk34b613e2002-12-17 01:51:00 +0000928
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200929 /* This only runs once on NOR flash */
930 while (processed < count) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +0200931 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernane9c62202018-03-09 12:12:59 +0000932 if (rc < 0) /* block test failed */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200933 return -1;
wdenk34b613e2002-12-17 01:51:00 +0000934
Andreas Fenkart22281582016-08-29 23:16:57 +0200935 if (blockstart + block_seek + readlen > environment_end(dev)) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200936 /* End of range is reached */
Alex Kiernane9c62202018-03-09 12:12:59 +0000937 fprintf(stderr, "Too few good blocks within range\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200938 return -1;
wdenke7f34c62003-01-11 09:48:40 +0000939 }
940
Alex Kiernane9c62202018-03-09 12:12:59 +0000941 if (rc) { /* block is bad */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200942 blockstart += blocklen;
943 continue;
wdenk34b613e2002-12-17 01:51:00 +0000944 }
945
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200946 /*
947 * If a block is bad, we retry in the next block at the same
Tom Rini282f2702017-09-05 14:01:29 -0400948 * offset - see env/nand.c::writeenv()
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200949 */
Alex Kiernane9c62202018-03-09 12:12:59 +0000950 lseek(fd, blockstart + block_seek, SEEK_SET);
wdenk34b613e2002-12-17 01:51:00 +0000951
Rafał Miłeckib509c7d2023-12-13 13:13:54 +0100952 while (readlen) {
953 rc = read(fd, buf + processed, readlen);
954 if (rc == -1) {
955 fprintf(stderr, "Read error on %s: %s\n",
956 DEVNAME(dev), strerror(errno));
957 return -1;
958 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200959#ifdef DEBUG
Rafał Miłeckib509c7d2023-12-13 13:13:54 +0100960 fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
961 rc, (unsigned long long)blockstart + block_seek,
962 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200963#endif
Rafał Miłeckib509c7d2023-12-13 13:13:54 +0100964 processed += rc;
Thibault Ferrante8586cda2021-08-24 17:29:50 +0200965 readlen -= rc;
Thibault Ferrante8586cda2021-08-24 17:29:50 +0200966 }
Rafał Miłeckib509c7d2023-12-13 13:13:54 +0100967
968 blockstart += blocklen;
969 readlen = min(blocklen, count - processed);
970 block_seek = 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200971 }
972
973 return processed;
974}
975
976/*
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200977 * Write count bytes from begin of environment, but stay within
978 * ENVSECTORS(dev) sectors of
Remy Bohmercb969eb2011-02-12 19:06:26 +0100979 * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
980 * erase and write the whole data at once.
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200981 */
Andreas Fenkart0b49b842016-08-29 23:17:00 +0200982static int flash_write_buf(int dev, int fd, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200983{
984 void *data;
985 struct erase_info_user erase;
986 size_t blocklen; /* length of NAND block / NOR erase sector */
987 size_t erase_len; /* whole area that can be erased - may include
988 bad blocks */
989 size_t erasesize; /* erase / write length - one block on NAND,
990 whole area on NOR */
991 size_t processed = 0; /* progress counter */
Remy Bohmercb969eb2011-02-12 19:06:26 +0100992 size_t write_total; /* total size to actually write - excluding
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200993 bad blocks */
994 off_t erase_offset; /* offset to the first erase block (aligned)
995 below offset */
996 off_t block_seek; /* offset inside the erase block to the start
997 of the data */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +0200998 loff_t blockstart; /* running start of the current block -
999 MEMGETBADBLOCK needs 64 bits */
Simon Glassf8205632020-08-01 10:30:39 -06001000 int was_locked = 0; /* flash lock flag */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001001 int rc;
1002
Oliver Metze9c66ab2013-08-30 00:56:02 +02001003 /*
1004 * For mtd devices only offset and size of the environment do matter
1005 */
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001006 if (DEVTYPE(dev) == MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +02001007 blocklen = count;
Oliver Metze9c66ab2013-08-30 00:56:02 +02001008 erase_len = blocklen;
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001009 blockstart = DEVOFFSET(dev);
Oliver Metze9c66ab2013-08-30 00:56:02 +02001010 block_seek = 0;
1011 write_total = blocklen;
1012 } else {
1013 blocklen = DEVESIZE(dev);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001014
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001015 erase_offset = DEVOFFSET(dev);
wdenk34b613e2002-12-17 01:51:00 +00001016
Oliver Metze9c66ab2013-08-30 00:56:02 +02001017 /* Maximum area we may use */
Andreas Fenkart22281582016-08-29 23:16:57 +02001018 erase_len = environment_end(dev) - erase_offset;
wdenk34b613e2002-12-17 01:51:00 +00001019
Oliver Metze9c66ab2013-08-30 00:56:02 +02001020 blockstart = erase_offset;
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001021
Oliver Metze9c66ab2013-08-30 00:56:02 +02001022 /* Offset inside a block */
Andreas Fenkart0b49b842016-08-29 23:17:00 +02001023 block_seek = DEVOFFSET(dev) - erase_offset;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001024
Oliver Metze9c66ab2013-08-30 00:56:02 +02001025 /*
1026 * Data size we actually write: from the start of the block
1027 * to the start of the data, then count bytes of data, and
1028 * to the end of the block
1029 */
1030 write_total = ((block_seek + count + blocklen - 1) /
Alex Kiernane9c62202018-03-09 12:12:59 +00001031 blocklen) * blocklen;
Oliver Metze9c66ab2013-08-30 00:56:02 +02001032 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001033
1034 /*
1035 * Support data anywhere within erase sectors: read out the complete
1036 * area to be erased, replace the environment image, write the whole
1037 * block back again.
1038 */
1039 if (write_total > count) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001040 data = malloc(erase_len);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001041 if (!data) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001042 fprintf(stderr,
1043 "Cannot malloc %zu bytes: %s\n",
1044 erase_len, strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001045 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001046 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001047
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001048 rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001049 if (write_total != rc)
1050 return -1;
1051
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001052#ifdef DEBUG
1053 fprintf(stderr, "Preserving data ");
1054 if (block_seek != 0)
1055 fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
1056 if (block_seek + count != write_total) {
1057 if (block_seek != 0)
1058 fprintf(stderr, " and ");
Marcin Niestroj20890732016-05-06 14:58:29 +02001059 fprintf(stderr, "0x%lx - 0x%lx",
Alex Kiernane9c62202018-03-09 12:12:59 +00001060 (unsigned long)block_seek + count,
1061 (unsigned long)write_total - 1);
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001062 }
1063 fprintf(stderr, "\n");
1064#endif
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001065 /* Overwrite the old environment */
Alex Kiernane9c62202018-03-09 12:12:59 +00001066 memcpy(data + block_seek, buf, count);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001067 } else {
1068 /*
1069 * We get here, iff offset is block-aligned and count is a
1070 * multiple of blocklen - see write_total calculation above
1071 */
1072 data = buf;
1073 }
1074
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001075 if (DEVTYPE(dev) == MTD_NANDFLASH) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001076 /*
1077 * NAND: calculate which blocks we are writing. We have
1078 * to write one block at a time to skip bad blocks.
1079 */
1080 erasesize = blocklen;
1081 } else {
1082 erasesize = erase_len;
1083 }
1084
1085 erase.length = erasesize;
1086
Remy Bohmercb969eb2011-02-12 19:06:26 +01001087 /* This only runs once on NOR flash and SPI-dataflash */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001088 while (processed < write_total) {
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001089 rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
Alex Kiernane9c62202018-03-09 12:12:59 +00001090 if (rc < 0) /* block test failed */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001091 return rc;
1092
Andreas Fenkart22281582016-08-29 23:16:57 +02001093 if (blockstart + erasesize > environment_end(dev)) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001094 fprintf(stderr, "End of range reached, aborting\n");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001095 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001096 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001097
Alex Kiernane9c62202018-03-09 12:12:59 +00001098 if (rc) { /* block is bad */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001099 blockstart += blocklen;
1100 continue;
1101 }
1102
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001103 if (DEVTYPE(dev) != MTD_ABSENT) {
Oliver Metze9c66ab2013-08-30 00:56:02 +02001104 erase.start = blockstart;
Joakim Tjernlund85206c02021-12-08 15:33:11 +01001105 was_locked = ioctl(fd, MEMISLOCKED, &erase);
1106 /* treat any errors as unlocked flash */
1107 if (was_locked < 0)
1108 was_locked = 0;
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001109 if (was_locked)
1110 ioctl(fd, MEMUNLOCK, &erase);
Oliver Metze9c66ab2013-08-30 00:56:02 +02001111 /* These do not need an explicit erase cycle */
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001112 if (DEVTYPE(dev) != MTD_DATAFLASH)
Oliver Metze9c66ab2013-08-30 00:56:02 +02001113 if (ioctl(fd, MEMERASE, &erase) != 0) {
1114 fprintf(stderr,
1115 "MTD erase error on %s: %s\n",
1116 DEVNAME(dev), strerror(errno));
1117 return -1;
1118 }
1119 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001120
Alex Kiernane9c62202018-03-09 12:12:59 +00001121 if (lseek(fd, blockstart, SEEK_SET) == -1) {
1122 fprintf(stderr,
1123 "Seek error on %s: %s\n",
1124 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001125 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001126 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001127#ifdef DEBUG
Marcin Niestroj20890732016-05-06 14:58:29 +02001128 fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001129 (unsigned long long)erasesize,
1130 (unsigned long long)blockstart);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001131#endif
Alex Kiernane9c62202018-03-09 12:12:59 +00001132 if (write(fd, data + processed, erasesize) != erasesize) {
1133 fprintf(stderr, "Write error on %s: %s\n",
1134 DEVNAME(dev), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001135 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001136 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001137
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001138 if (DEVTYPE(dev) != MTD_ABSENT) {
1139 if (was_locked)
1140 ioctl(fd, MEMLOCK, &erase);
1141 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001142
Alex Kiernane9c62202018-03-09 12:12:59 +00001143 processed += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001144 block_seek = 0;
Dustin Byford0983cad2014-03-06 20:48:23 -08001145 blockstart += erasesize;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001146 }
1147
1148 if (write_total > count)
Alex Kiernane9c62202018-03-09 12:12:59 +00001149 free(data);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001150
1151 return processed;
1152}
1153
1154/*
1155 * Set obsolete flag at offset - NOR flash only
1156 */
Alex Kiernane9c62202018-03-09 12:12:59 +00001157static int flash_flag_obsolete(int dev, int fd, off_t offset)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001158{
1159 int rc;
Detlev Zundel2111d3e2010-07-30 11:22:15 +02001160 struct erase_info_user erase;
Pierre-Jean Texierd7f95452019-08-26 13:06:16 +02001161 char tmp = ENV_REDUND_OBSOLETE;
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001162 int was_locked; /* flash lock flag */
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001163
Alex Kiernane9c62202018-03-09 12:12:59 +00001164 erase.start = DEVOFFSET(dev);
1165 erase.length = DEVESIZE(dev);
Simon Glass4686d8f2019-08-01 09:47:08 -06001166 /* This relies on the fact, that ENV_REDUND_OBSOLETE == 0 */
Alex Kiernane9c62202018-03-09 12:12:59 +00001167 rc = lseek(fd, offset, SEEK_SET);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001168 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001169 fprintf(stderr, "Cannot seek to set the flag on %s\n",
1170 DEVNAME(dev));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001171 return rc;
1172 }
Joakim Tjernlund85206c02021-12-08 15:33:11 +01001173 was_locked = ioctl(fd, MEMISLOCKED, &erase);
1174 /* treat any errors as unlocked flash */
1175 if (was_locked < 0)
1176 was_locked = 0;
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001177 if (was_locked)
1178 ioctl(fd, MEMUNLOCK, &erase);
Pierre-Jean Texierd7f95452019-08-26 13:06:16 +02001179 rc = write(fd, &tmp, sizeof(tmp));
Ivan Mikhaylov20c21bf2020-07-10 19:54:18 +03001180 if (was_locked)
1181 ioctl(fd, MEMLOCK, &erase);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001182 if (rc < 0)
Alex Kiernane9c62202018-03-09 12:12:59 +00001183 perror("Could not set obsolete flag");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001184
1185 return rc;
1186}
1187
Rafał Miłecki840ec192022-01-12 12:47:05 +01001188static int flash_write(int fd_current, int fd_target, int dev_target, void *buf,
1189 size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001190{
1191 int rc;
1192
1193 switch (environment.flag_scheme) {
1194 case FLAG_NONE:
1195 break;
1196 case FLAG_INCREMENTAL:
1197 (*environment.flags)++;
1198 break;
1199 case FLAG_BOOLEAN:
Simon Glass4686d8f2019-08-01 09:47:08 -06001200 *environment.flags = ENV_REDUND_ACTIVE;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001201 break;
1202 default:
Alex Kiernane9c62202018-03-09 12:12:59 +00001203 fprintf(stderr, "Unimplemented flash scheme %u\n",
1204 environment.flag_scheme);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001205 return -1;
1206 }
1207
1208#ifdef DEBUG
Stefan Agnerb21e6022016-07-13 17:14:38 -07001209 fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001210 DEVOFFSET(dev_target), DEVNAME(dev_target));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001211#endif
Marek Vasut05ed2a52014-03-05 19:59:52 +01001212
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001213 if (IS_UBI(dev_target)) {
1214 if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0)
Martin Hundebøll710e9c12020-11-30 12:11:32 +01001215 return -1;
Rafał Miłecki840ec192022-01-12 12:47:05 +01001216 return ubi_write(fd_target, buf, count);
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001217 }
1218
Rafał Miłecki840ec192022-01-12 12:47:05 +01001219 rc = flash_write_buf(dev_target, fd_target, buf, count);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001220 if (rc < 0)
1221 return rc;
1222
1223 if (environment.flag_scheme == FLAG_BOOLEAN) {
1224 /* Have to set obsolete flag */
Alex Kiernane9c62202018-03-09 12:12:59 +00001225 off_t offset = DEVOFFSET(dev_current) +
1226 offsetof(struct env_image_redundant, flags);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001227#ifdef DEBUG
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001228 fprintf(stderr,
Stefan Agnerb21e6022016-07-13 17:14:38 -07001229 "Setting obsolete flag in environment at 0x%llx on %s\n",
Alex Kiernane9c62202018-03-09 12:12:59 +00001230 DEVOFFSET(dev_current), DEVNAME(dev_current));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001231#endif
Alex Kiernane9c62202018-03-09 12:12:59 +00001232 flash_flag_obsolete(dev_current, fd_current, offset);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001233 }
1234
1235 return 0;
1236}
1237
Rafał Miłecki840ec192022-01-12 12:47:05 +01001238static int flash_read(int fd, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001239{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001240 int rc;
1241
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001242 if (IS_UBI(dev_current)) {
1243 DEVTYPE(dev_current) = MTD_ABSENT;
1244
Rafał Miłecki840ec192022-01-12 12:47:05 +01001245 return ubi_read(fd, buf, count);
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001246 }
1247
Rafał Miłecki840ec192022-01-12 12:47:05 +01001248 rc = flash_read_buf(dev_current, fd, buf, count,
Andreas Fenkart5e755c72016-08-29 23:16:59 +02001249 DEVOFFSET(dev_current));
Marek Vasut05ed2a52014-03-05 19:59:52 +01001250 if (rc != CUR_ENVSIZE)
1251 return -1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001252
Marek Vasut05ed2a52014-03-05 19:59:52 +01001253 return 0;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001254}
1255
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001256static int flash_open_tempfile(const char **dname, const char **target_temp)
1257{
1258 char *dup_name = strdup(DEVNAME(dev_current));
1259 char *temp_name = NULL;
1260 int rc = -1;
1261
1262 if (!dup_name)
1263 return -1;
1264
1265 *dname = dirname(dup_name);
1266 if (!*dname)
1267 goto err;
1268
1269 rc = asprintf(&temp_name, "%s/XXXXXX", *dname);
1270 if (rc == -1)
1271 goto err;
1272
1273 rc = mkstemp(temp_name);
1274 if (rc == -1) {
1275 /* fall back to in place write */
1276 fprintf(stderr,
1277 "Can't create %s: %s\n", temp_name, strerror(errno));
1278 free(temp_name);
1279 } else {
1280 *target_temp = temp_name;
1281 /* deliberately leak dup_name as dname /might/ point into
1282 * it and we need it for our caller
1283 */
1284 dup_name = NULL;
1285 }
1286
1287err:
1288 if (dup_name)
1289 free(dup_name);
1290
1291 return rc;
1292}
1293
Rafał Miłecki840ec192022-01-12 12:47:05 +01001294static int flash_io_write(int fd_current, void *buf, size_t count)
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001295{
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001296 int fd_target = -1, rc, dev_target;
1297 const char *dname, *target_temp = NULL;
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001298
1299 if (have_redund_env) {
1300 /* switch to next partition for writing */
1301 dev_target = !dev_current;
1302 /* dev_target: fd_target, erase_target */
1303 fd_target = open(DEVNAME(dev_target), O_RDWR);
1304 if (fd_target < 0) {
1305 fprintf(stderr,
1306 "Can't open %s: %s\n",
1307 DEVNAME(dev_target), strerror(errno));
1308 rc = -1;
1309 goto exit;
1310 }
1311 } else {
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001312 struct stat sb;
1313
1314 if (fstat(fd_current, &sb) == 0 && S_ISREG(sb.st_mode)) {
1315 /* if any part of flash_open_tempfile() fails we fall
1316 * back to in-place writes
1317 */
1318 fd_target = flash_open_tempfile(&dname, &target_temp);
1319 }
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001320 dev_target = dev_current;
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001321 if (fd_target == -1)
1322 fd_target = fd_current;
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001323 }
1324
Rafał Miłecki840ec192022-01-12 12:47:05 +01001325 rc = flash_write(fd_current, fd_target, dev_target, buf, count);
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001326
1327 if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) {
1328 fprintf(stderr,
1329 "fsync failed on %s: %s\n",
1330 DEVNAME(dev_current), strerror(errno));
1331 }
1332
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001333 if (fd_current != fd_target) {
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001334 if (fsync(fd_target) &&
1335 !(errno == EINVAL || errno == EROFS)) {
1336 fprintf(stderr,
1337 "fsync failed on %s: %s\n",
1338 DEVNAME(dev_current), strerror(errno));
1339 }
1340
1341 if (close(fd_target)) {
1342 fprintf(stderr,
1343 "I/O error on %s: %s\n",
1344 DEVNAME(dev_target), strerror(errno));
1345 rc = -1;
1346 }
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001347
Alex Kiernanad4f3d02019-06-12 15:27:55 +01001348 if (rc >= 0 && target_temp) {
Alex Kiernan7150aaa2018-03-09 12:13:02 +00001349 int dir_fd;
1350
1351 dir_fd = open(dname, O_DIRECTORY | O_RDONLY);
1352 if (dir_fd == -1)
1353 fprintf(stderr,
1354 "Can't open %s: %s\n",
1355 dname, strerror(errno));
1356
1357 if (rename(target_temp, DEVNAME(dev_target))) {
1358 fprintf(stderr,
1359 "rename failed %s => %s: %s\n",
1360 target_temp, DEVNAME(dev_target),
1361 strerror(errno));
1362 rc = -1;
1363 }
1364
1365 if (dir_fd != -1 && fsync(dir_fd))
1366 fprintf(stderr,
1367 "fsync failed on %s: %s\n",
1368 dname, strerror(errno));
1369
1370 if (dir_fd != -1 && close(dir_fd))
1371 fprintf(stderr,
1372 "I/O error on %s: %s\n",
1373 dname, strerror(errno));
1374 }
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001375 }
1376 exit:
1377 return rc;
1378}
1379
Rafał Miłecki840ec192022-01-12 12:47:05 +01001380static int flash_io(int mode, void *buf, size_t count)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001381{
Alex Kiernanbe1e7882018-03-09 12:13:01 +00001382 int fd_current, rc;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001383
1384 /* dev_current: fd_current, erase_current */
Alex Kiernane9c62202018-03-09 12:12:59 +00001385 fd_current = open(DEVNAME(dev_current), mode);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001386 if (fd_current < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001387 fprintf(stderr,
1388 "Can't open %s: %s\n",
1389 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001390 return -1;
1391 }
1392
1393 if (mode == O_RDWR) {
Rafał Miłecki840ec192022-01-12 12:47:05 +01001394 rc = flash_io_write(fd_current, buf, count);
wdenk34b613e2002-12-17 01:51:00 +00001395 } else {
Rafał Miłecki840ec192022-01-12 12:47:05 +01001396 rc = flash_read(fd_current, buf, count);
wdenk34b613e2002-12-17 01:51:00 +00001397 }
1398
Alex Kiernane9c62202018-03-09 12:12:59 +00001399 if (close(fd_current)) {
1400 fprintf(stderr,
1401 "I/O error on %s: %s\n",
1402 DEVNAME(dev_current), strerror(errno));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001403 return -1;
wdenk34b613e2002-12-17 01:51:00 +00001404 }
1405
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001406 return rc;
wdenk34b613e2002-12-17 01:51:00 +00001407}
1408
1409/*
wdenk34b613e2002-12-17 01:51:00 +00001410 * Prevent confusion if running from erased flash memory
1411 */
Andreas Fenkart24371902016-04-05 23:13:42 +02001412int fw_env_open(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001413{
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001414 int crc0, crc0_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001415 unsigned char flag0;
Rafał Miłecki260de102023-12-13 10:23:44 +01001416 void *buf0 = NULL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001417
wdenk34b613e2002-12-17 01:51:00 +00001418 int crc1, crc1_ok;
Jon Povey93ccd472011-03-11 14:10:56 +09001419 unsigned char flag1;
Rafał Miłecki260de102023-12-13 10:23:44 +01001420 void *buf1 = NULL;
wdenke7f34c62003-01-11 09:48:40 +00001421
Marek Vasut05ed2a52014-03-05 19:59:52 +01001422 int ret;
1423
Andreas Fenkarte6511302016-05-31 09:21:56 +02001424 if (!opts)
1425 opts = &default_opts;
1426
Alex Kiernane9c62202018-03-09 12:12:59 +00001427 if (parse_config(opts)) /* should fill envdevices */
Stefano Babic4b0a2932017-04-05 18:08:03 +02001428 return -EINVAL;
wdenk3f9ab982003-04-12 23:38:12 +00001429
Rafał Miłecki260de102023-12-13 10:23:44 +01001430 buf0 = calloc(1, CUR_ENVSIZE);
1431 if (buf0 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001432 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001433 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001434 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001435 ret = -ENOMEM;
1436 goto open_cleanup;
wdenke7f34c62003-01-11 09:48:40 +00001437 }
wdenk3f9ab982003-04-12 23:38:12 +00001438
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001439 dev_current = 0;
Rafał Miłecki260de102023-12-13 10:23:44 +01001440 if (flash_io(O_RDONLY, buf0, CUR_ENVSIZE)) {
Stefano Babic4b0a2932017-04-05 18:08:03 +02001441 ret = -EIO;
1442 goto open_cleanup;
1443 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001444
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001445 if (!have_redund_env) {
Rafał Miłecki260de102023-12-13 10:23:44 +01001446 struct env_image_single *single = buf0;
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001447
1448 crc0 = crc32(0, (uint8_t *)single->data, ENV_SIZE);
1449 crc0_ok = (crc0 == single->crc);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001450 if (!crc0_ok) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001451 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001452 "Warning: Bad CRC, using default environment\n");
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001453 memcpy(single->data, default_environment,
Alex Kiernane9c62202018-03-09 12:12:59 +00001454 sizeof(default_environment));
Rasmus Villemoes06a79642018-09-27 22:45:26 +02001455 environment.dirty = 1;
wdenk34b613e2002-12-17 01:51:00 +00001456 }
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001457
Rafał Miłecki260de102023-12-13 10:23:44 +01001458 environment.image = buf0;
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001459 environment.crc = &single->crc;
1460 environment.flags = NULL;
1461 environment.data = single->data;
wdenke7f34c62003-01-11 09:48:40 +00001462 } else {
Rafał Miłecki260de102023-12-13 10:23:44 +01001463 struct env_image_redundant *redundant0 = buf0;
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001464 struct env_image_redundant *redundant1;
1465
1466 crc0 = crc32(0, (uint8_t *)redundant0->data, ENV_SIZE);
1467 crc0_ok = (crc0 == redundant0->crc);
1468
1469 flag0 = redundant0->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001470
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001471 dev_current = 1;
Rafał Miłecki260de102023-12-13 10:23:44 +01001472 buf1 = calloc(1, CUR_ENVSIZE);
1473 if (buf1 == NULL) {
Joe Hershberger671adc42012-10-03 09:38:46 +00001474 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001475 "Not enough memory for environment (%ld bytes)\n",
Joe Hershberger671adc42012-10-03 09:38:46 +00001476 CUR_ENVSIZE);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001477 ret = -ENOMEM;
1478 goto open_cleanup;
wdenk3f9ab982003-04-12 23:38:12 +00001479 }
Rafał Miłecki260de102023-12-13 10:23:44 +01001480 redundant1 = buf1;
wdenk3f9ab982003-04-12 23:38:12 +00001481
Rafał Miłecki260de102023-12-13 10:23:44 +01001482 if (flash_io(O_RDONLY, buf1, CUR_ENVSIZE)) {
Tom Rini4c1192c2018-07-01 22:10:33 -04001483 ret = -EIO;
1484 goto open_cleanup;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001485 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001486
1487 /* Check flag scheme compatibility */
1488 if (DEVTYPE(dev_current) == MTD_NORFLASH &&
1489 DEVTYPE(!dev_current) == MTD_NORFLASH) {
1490 environment.flag_scheme = FLAG_BOOLEAN;
1491 } else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
1492 DEVTYPE(!dev_current) == MTD_NANDFLASH) {
1493 environment.flag_scheme = FLAG_INCREMENTAL;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001494 } else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
1495 DEVTYPE(!dev_current) == MTD_DATAFLASH) {
1496 environment.flag_scheme = FLAG_BOOLEAN;
Joe Hershbergerdb14e862013-04-08 10:32:52 +00001497 } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
1498 DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
1499 environment.flag_scheme = FLAG_INCREMENTAL;
Oliver Metze745d4c2013-08-30 00:56:01 +02001500 } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001501 DEVTYPE(!dev_current) == MTD_ABSENT &&
1502 IS_UBI(dev_current) == IS_UBI(!dev_current)) {
Oliver Metze745d4c2013-08-30 00:56:01 +02001503 environment.flag_scheme = FLAG_INCREMENTAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001504 } else {
Alex Kiernane9c62202018-03-09 12:12:59 +00001505 fprintf(stderr, "Incompatible flash types!\n");
Stefano Babic4b0a2932017-04-05 18:08:03 +02001506 ret = -EINVAL;
1507 goto open_cleanup;
wdenk34b613e2002-12-17 01:51:00 +00001508 }
wdenk3f9ab982003-04-12 23:38:12 +00001509
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001510 crc1 = crc32(0, (uint8_t *)redundant1->data, ENV_SIZE);
Tom Rini4c1192c2018-07-01 22:10:33 -04001511
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001512 crc1_ok = (crc1 == redundant1->crc);
1513 flag1 = redundant1->flags;
wdenk3f9ab982003-04-12 23:38:12 +00001514
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001515 if (memcmp(redundant0->data, redundant1->data, ENV_SIZE) ||
Rasmus Villemoes06a79642018-09-27 22:45:26 +02001516 !crc0_ok || !crc1_ok)
1517 environment.dirty = 1;
1518
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001519 if (crc0_ok && !crc1_ok) {
1520 dev_current = 0;
1521 } else if (!crc0_ok && crc1_ok) {
1522 dev_current = 1;
1523 } else if (!crc0_ok && !crc1_ok) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001524 fprintf(stderr,
wdenk3f9ab982003-04-12 23:38:12 +00001525 "Warning: Bad CRC, using default environment\n");
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001526 memcpy(redundant0->data, default_environment,
Alex Kiernane9c62202018-03-09 12:12:59 +00001527 sizeof(default_environment));
Rasmus Villemoes06a79642018-09-27 22:45:26 +02001528 environment.dirty = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001529 dev_current = 0;
1530 } else {
1531 switch (environment.flag_scheme) {
1532 case FLAG_BOOLEAN:
Simon Glass4686d8f2019-08-01 09:47:08 -06001533 if (flag0 == ENV_REDUND_ACTIVE &&
1534 flag1 == ENV_REDUND_OBSOLETE) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001535 dev_current = 0;
Simon Glass4686d8f2019-08-01 09:47:08 -06001536 } else if (flag0 == ENV_REDUND_OBSOLETE &&
1537 flag1 == ENV_REDUND_ACTIVE) {
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001538 dev_current = 1;
1539 } else if (flag0 == flag1) {
1540 dev_current = 0;
1541 } else if (flag0 == 0xFF) {
1542 dev_current = 0;
1543 } else if (flag1 == 0xFF) {
1544 dev_current = 1;
1545 } else {
1546 dev_current = 0;
1547 }
1548 break;
1549 case FLAG_INCREMENTAL:
Jon Povey93ccd472011-03-11 14:10:56 +09001550 if (flag0 == 255 && flag1 == 0)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001551 dev_current = 1;
1552 else if ((flag1 == 255 && flag0 == 0) ||
Jon Povey93ccd472011-03-11 14:10:56 +09001553 flag0 >= flag1)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001554 dev_current = 0;
Alex Kiernane9c62202018-03-09 12:12:59 +00001555 else /* flag1 > flag0 */
Jon Povey93ccd472011-03-11 14:10:56 +09001556 dev_current = 1;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001557 break;
1558 default:
Alex Kiernane9c62202018-03-09 12:12:59 +00001559 fprintf(stderr, "Unknown flag scheme %u\n",
1560 environment.flag_scheme);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001561 return -1;
1562 }
1563 }
1564
1565 /*
1566 * If we are reading, we don't need the flag and the CRC any
1567 * more, if we are writing, we will re-calculate CRC and update
1568 * flags before writing out
1569 */
1570 if (dev_current) {
Rafał Miłecki260de102023-12-13 10:23:44 +01001571 environment.image = buf1;
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001572 environment.crc = &redundant1->crc;
1573 environment.flags = &redundant1->flags;
1574 environment.data = redundant1->data;
Rafał Miłecki260de102023-12-13 10:23:44 +01001575 free(buf0);
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001576 } else {
Rafał Miłecki260de102023-12-13 10:23:44 +01001577 environment.image = buf0;
Rafał Miłecki02acbf12022-01-12 12:47:06 +01001578 environment.crc = &redundant0->crc;
1579 environment.flags = &redundant0->flags;
1580 environment.data = redundant0->data;
Rafał Miłecki260de102023-12-13 10:23:44 +01001581 free(buf1);
wdenk34b613e2002-12-17 01:51:00 +00001582 }
Joe Hershbergera8dd2c62012-10-15 15:29:25 +00001583#ifdef DEBUG
1584 fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
1585#endif
wdenk34b613e2002-12-17 01:51:00 +00001586 }
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001587 return 0;
Stefano Babic4b0a2932017-04-05 18:08:03 +02001588
Alex Kiernane9c62202018-03-09 12:12:59 +00001589 open_cleanup:
Rafał Miłecki260de102023-12-13 10:23:44 +01001590 free(buf0);
1591 free(buf1);
Stefano Babic4b0a2932017-04-05 18:08:03 +02001592
1593 return ret;
1594}
1595
1596/*
1597 * Simply free allocated buffer with environment
1598 */
1599int fw_env_close(struct env_opts *opts)
1600{
1601 if (environment.image)
1602 free(environment.image);
1603
1604 environment.image = NULL;
1605
1606 return 0;
wdenk34b613e2002-12-17 01:51:00 +00001607}
1608
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001609static int check_device_config(int dev)
1610{
1611 struct stat st;
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001612 int32_t lnum = 0;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001613 int fd, rc = 0;
1614
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001615 /* Fills in IS_UBI(), converts DEVNAME() with ubi volume name */
1616 ubi_check_dev(dev);
1617
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001618 fd = open(DEVNAME(dev), O_RDONLY);
1619 if (fd < 0) {
1620 fprintf(stderr,
Alex Kiernane9c62202018-03-09 12:12:59 +00001621 "Cannot open %s: %s\n", DEVNAME(dev), strerror(errno));
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001622 return -1;
1623 }
1624
1625 rc = fstat(fd, &st);
1626 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001627 fprintf(stderr, "Cannot stat the file %s\n", DEVNAME(dev));
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001628 goto err;
1629 }
1630
S. Lockwood-Childsa1015c22017-11-14 23:01:26 -08001631 if (IS_UBI(dev)) {
1632 rc = ioctl(fd, UBI_IOCEBISMAP, &lnum);
1633 if (rc < 0) {
1634 fprintf(stderr, "Cannot get UBI information for %s\n",
1635 DEVNAME(dev));
1636 goto err;
1637 }
1638 } else if (S_ISCHR(st.st_mode)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001639 struct mtd_info_user mtdinfo;
1640 rc = ioctl(fd, MEMGETINFO, &mtdinfo);
1641 if (rc < 0) {
1642 fprintf(stderr, "Cannot get MTD information for %s\n",
1643 DEVNAME(dev));
1644 goto err;
1645 }
1646 if (mtdinfo.type != MTD_NORFLASH &&
1647 mtdinfo.type != MTD_NANDFLASH &&
1648 mtdinfo.type != MTD_DATAFLASH &&
1649 mtdinfo.type != MTD_UBIVOLUME) {
1650 fprintf(stderr, "Unsupported flash type %u on %s\n",
1651 mtdinfo.type, DEVNAME(dev));
1652 goto err;
1653 }
1654 DEVTYPE(dev) = mtdinfo.type;
Rasmus Villemoesfaf29982020-03-24 13:57:40 +01001655 if (DEVESIZE(dev) == 0 && ENVSECTORS(dev) == 0 &&
Anthony Loiseauade0dc32023-12-21 23:44:38 +01001656 mtdinfo.erasesize > 0) {
1657 if (mtdinfo.type == MTD_NORFLASH)
1658 DEVESIZE(dev) = mtdinfo.erasesize;
1659 else if (mtdinfo.type == MTD_NANDFLASH) {
1660 DEVESIZE(dev) = mtdinfo.erasesize;
1661 ENVSECTORS(dev) =
1662 mtdinfo.size / mtdinfo.erasesize;
1663 }
1664 }
Max Krummenacher6c44f542016-11-19 13:58:56 +01001665 if (DEVESIZE(dev) == 0)
1666 /* Assume the erase size is the same as the env-size */
1667 DEVESIZE(dev) = ENVSIZE(dev);
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001668 } else {
Stefan Agnerb21e6022016-07-13 17:14:38 -07001669 uint64_t size;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001670 DEVTYPE(dev) = MTD_ABSENT;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001671 if (DEVESIZE(dev) == 0)
1672 /* Assume the erase size to be 512 bytes */
1673 DEVESIZE(dev) = 0x200;
Stefan Agnerb21e6022016-07-13 17:14:38 -07001674
1675 /*
1676 * Check for negative offsets, treat it as backwards offset
1677 * from the end of the block device
1678 */
1679 if (DEVOFFSET(dev) < 0) {
1680 rc = ioctl(fd, BLKGETSIZE64, &size);
1681 if (rc < 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001682 fprintf(stderr,
1683 "Could not get block device size on %s\n",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001684 DEVNAME(dev));
1685 goto err;
1686 }
1687
1688 DEVOFFSET(dev) = DEVOFFSET(dev) + size;
1689#ifdef DEBUG
Alex Kiernane9c62202018-03-09 12:12:59 +00001690 fprintf(stderr,
1691 "Calculated device offset 0x%llx on %s\n",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001692 DEVOFFSET(dev), DEVNAME(dev));
1693#endif
1694 }
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001695 }
1696
Max Krummenacher6c44f542016-11-19 13:58:56 +01001697 if (ENVSECTORS(dev) == 0)
1698 /* Assume enough sectors to cover the environment */
1699 ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev));
1700
1701 if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001702 fprintf(stderr,
1703 "Environment does not start on (erase) block boundary\n");
Max Krummenacher6c44f542016-11-19 13:58:56 +01001704 errno = EINVAL;
1705 return -1;
1706 }
1707
1708 if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
Alex Kiernane9c62202018-03-09 12:12:59 +00001709 fprintf(stderr,
1710 "Environment does not fit into available sectors\n");
Max Krummenacher6c44f542016-11-19 13:58:56 +01001711 errno = EINVAL;
1712 return -1;
1713 }
1714
Alex Kiernane9c62202018-03-09 12:12:59 +00001715 err:
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001716 close(fd);
1717 return rc;
1718}
wdenk34b613e2002-12-17 01:51:00 +00001719
Rafał Miłecki326d5902022-06-16 20:59:03 +02001720static int find_nvmem_device(void)
1721{
1722 const char *path = "/sys/bus/nvmem/devices";
1723 struct dirent *dent;
1724 char *nvmem = NULL;
1725 char comp[256];
1726 char buf[32];
1727 int bytes;
1728 DIR *dir;
1729
1730 dir = opendir(path);
1731 if (!dir) {
1732 return -EIO;
1733 }
1734
1735 while (!nvmem && (dent = readdir(dir))) {
Rafał Miłeckia2c52912023-12-12 18:33:27 +01001736 struct stat s;
Rafał Miłecki326d5902022-06-16 20:59:03 +02001737 FILE *fp;
Jaehoon Chung18ca4492022-10-13 17:41:21 +09001738 size_t size;
Rafał Miłecki326d5902022-06-16 20:59:03 +02001739
1740 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
1741 continue;
1742 }
1743
1744 bytes = snprintf(comp, sizeof(comp), "%s/%s/of_node/compatible", path, dent->d_name);
1745 if (bytes < 0 || bytes == sizeof(comp)) {
1746 continue;
1747 }
1748
1749 fp = fopen(comp, "r");
1750 if (!fp) {
1751 continue;
1752 }
1753
Rafał Miłeckia2c52912023-12-12 18:33:27 +01001754 if (fstat(fileno(fp), &s)) {
1755 fprintf(stderr, "Failed to fstat %s\n", comp);
1756 goto next;
1757 }
1758
1759 if (s.st_size >= sizeof(buf)) {
1760 goto next;
1761 }
1762
1763 size = fread(buf, s.st_size, 1, fp);
Jaehoon Chung18ca4492022-10-13 17:41:21 +09001764 if (size != 1) {
1765 fprintf(stderr,
1766 "read failed about %s\n", comp);
Rafał Miłeckia2c52912023-12-12 18:33:27 +01001767 goto next;
Jaehoon Chung18ca4492022-10-13 17:41:21 +09001768 }
Rafał Miłeckia2c52912023-12-12 18:33:27 +01001769 buf[s.st_size] = '\0';
Rafał Miłecki326d5902022-06-16 20:59:03 +02001770
1771 if (!strcmp(buf, "u-boot,env")) {
1772 bytes = asprintf(&nvmem, "%s/%s/nvmem", path, dent->d_name);
1773 if (bytes < 0) {
1774 nvmem = NULL;
1775 }
1776 }
1777
Rafał Miłeckia2c52912023-12-12 18:33:27 +01001778next:
Rafał Miłecki326d5902022-06-16 20:59:03 +02001779 fclose(fp);
1780 }
1781
1782 closedir(dir);
1783
1784 if (nvmem) {
1785 struct stat s;
1786
1787 stat(nvmem, &s);
1788
1789 DEVNAME(0) = nvmem;
1790 DEVOFFSET(0) = 0;
1791 ENVSIZE(0) = s.st_size;
1792
1793 return 0;
1794 }
1795
1796 return -ENOENT;
1797}
1798
Andreas Fenkart24371902016-04-05 23:13:42 +02001799static int parse_config(struct env_opts *opts)
wdenk34b613e2002-12-17 01:51:00 +00001800{
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001801 int rc;
wdenk34b613e2002-12-17 01:51:00 +00001802
Andreas Fenkarte6511302016-05-31 09:21:56 +02001803 if (!opts)
1804 opts = &default_opts;
Anatolij Gustschine89ead32016-04-29 22:00:11 +02001805
Andreas Fenkarte6511302016-05-31 09:21:56 +02001806#if defined(CONFIG_FILE)
wdenke7f34c62003-01-11 09:48:40 +00001807 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
Andreas Fenkart24371902016-04-05 23:13:42 +02001808 if (get_config(opts->config_file)) {
Rafał Miłecki326d5902022-06-16 20:59:03 +02001809 if (find_nvmem_device()) {
1810 fprintf(stderr, "Cannot parse config file '%s': %m\n",
1811 opts->config_file);
1812 fprintf(stderr, "Failed to find NVMEM device\n");
1813 return -1;
1814 }
wdenk34b613e2002-12-17 01:51:00 +00001815 }
wdenke7f34c62003-01-11 09:48:40 +00001816#else
Alex Kiernane9c62202018-03-09 12:12:59 +00001817 DEVNAME(0) = DEVICE1_NAME;
1818 DEVOFFSET(0) = DEVICE1_OFFSET;
1819 ENVSIZE(0) = ENV1_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001820
1821 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1822 * know DEVTYPE
1823 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001824#ifdef DEVICE1_ESIZE
Alex Kiernane9c62202018-03-09 12:12:59 +00001825 DEVESIZE(0) = DEVICE1_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001826#endif
1827#ifdef DEVICE1_ENVSECTORS
Alex Kiernane9c62202018-03-09 12:12:59 +00001828 ENVSECTORS(0) = DEVICE1_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001829#endif
1830
wdenk34b613e2002-12-17 01:51:00 +00001831#ifdef HAVE_REDUND
Alex Kiernane9c62202018-03-09 12:12:59 +00001832 DEVNAME(1) = DEVICE2_NAME;
1833 DEVOFFSET(1) = DEVICE2_OFFSET;
1834 ENVSIZE(1) = ENV2_SIZE;
Max Krummenacher6c44f542016-11-19 13:58:56 +01001835
1836 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1837 * know DEVTYPE
1838 */
Remy Bohmercb969eb2011-02-12 19:06:26 +01001839#ifdef DEVICE2_ESIZE
Alex Kiernane9c62202018-03-09 12:12:59 +00001840 DEVESIZE(1) = DEVICE2_ESIZE;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001841#endif
1842#ifdef DEVICE2_ENVSECTORS
Alex Kiernane9c62202018-03-09 12:12:59 +00001843 ENVSECTORS(1) = DEVICE2_ENVSECTORS;
Remy Bohmercb969eb2011-02-12 19:06:26 +01001844#endif
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001845 have_redund_env = 1;
wdenk34b613e2002-12-17 01:51:00 +00001846#endif
wdenke7f34c62003-01-11 09:48:40 +00001847#endif
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001848 rc = check_device_config(0);
1849 if (rc < 0)
1850 return rc;
wdenk3f9ab982003-04-12 23:38:12 +00001851
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001852 if (have_redund_env) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001853 rc = check_device_config(1);
1854 if (rc < 0)
1855 return rc;
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001856
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001857 if (ENVSIZE(0) != ENVSIZE(1)) {
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001858 fprintf(stderr,
Philip Molloyf60f7512019-03-31 03:44:57 +00001859 "Redundant environments have unequal size\n");
Andreas Fenkart31f2e052016-08-17 23:41:53 +02001860 return -1;
Stefan Agner21dbd6a2016-07-13 17:14:37 -07001861 }
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001862 }
1863
1864 usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001865 if (have_redund_env)
Andreas Fenkartb8bf4042016-04-19 22:43:42 +02001866 usable_envsize -= sizeof(char);
1867
wdenk34b613e2002-12-17 01:51:00 +00001868 return 0;
1869}
wdenke7f34c62003-01-11 09:48:40 +00001870
1871#if defined(CONFIG_FILE)
Alex Kiernane9c62202018-03-09 12:12:59 +00001872static int get_config(char *fname)
wdenke7f34c62003-01-11 09:48:40 +00001873{
1874 FILE *fp;
1875 int i = 0;
1876 int rc;
Alex Kiernan66df4e22018-06-07 12:20:05 +00001877 char *line = NULL;
1878 size_t linesize = 0;
Tom Rini313e9122014-03-28 12:03:33 -04001879 char *devname;
wdenke7f34c62003-01-11 09:48:40 +00001880
Alex Kiernane9c62202018-03-09 12:12:59 +00001881 fp = fopen(fname, "r");
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001882 if (fp == NULL)
1883 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001884
Alex Kiernan66df4e22018-06-07 12:20:05 +00001885 while (i < 2 && getline(&line, &linesize, fp) != -1) {
1886 /* Skip comment strings */
1887 if (line[0] == '#')
wdenke7f34c62003-01-11 09:48:40 +00001888 continue;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001889
Alex Kiernan66df4e22018-06-07 12:20:05 +00001890 rc = sscanf(line, "%ms %lli %lx %lx %lx",
Stefan Agnerb21e6022016-07-13 17:14:38 -07001891 &devname,
1892 &DEVOFFSET(i),
Alex Kiernane9c62202018-03-09 12:12:59 +00001893 &ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i));
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001894
Remy Bohmercb969eb2011-02-12 19:06:26 +01001895 if (rc < 3)
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001896 continue;
1897
Tom Rini313e9122014-03-28 12:03:33 -04001898 DEVNAME(i) = devname;
1899
Max Krummenacher6c44f542016-11-19 13:58:56 +01001900 /* Set defaults for DEVESIZE, ENVSECTORS later once we
1901 * know DEVTYPE
1902 */
wdenke7f34c62003-01-11 09:48:40 +00001903
1904 i++;
1905 }
Alex Kiernan66df4e22018-06-07 12:20:05 +00001906 free(line);
Alex Kiernane9c62202018-03-09 12:12:59 +00001907 fclose(fp);
wdenk3f9ab982003-04-12 23:38:12 +00001908
Alex Kiernanf46d0ca2018-03-09 12:13:00 +00001909 have_redund_env = i - 1;
Alex Kiernane9c62202018-03-09 12:12:59 +00001910 if (!i) { /* No valid entries found */
wdenke7f34c62003-01-11 09:48:40 +00001911 errno = EINVAL;
Guennadi Liakhovetskiffb379a2008-09-04 13:01:49 +02001912 return -1;
wdenke7f34c62003-01-11 09:48:40 +00001913 } else
1914 return 0;
1915}
1916#endif