wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1 | /* |
Detlev Zundel | 0aef7bc | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 5 | * (C) Copyright 2008 |
| 6 | * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de. |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Jörg Krause | 26e355d | 2015-04-22 21:36:22 +0200 | [diff] [blame] | 11 | #define _GNU_SOURCE |
| 12 | |
Peter Robinson | 69bf2d2 | 2015-12-09 07:15:33 +0000 | [diff] [blame] | 13 | #include <compiler.h> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 14 | #include <errno.h> |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 15 | #include <env_flags.h> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 16 | #include <fcntl.h> |
Stefan Agner | f4742ca | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 17 | #include <linux/fs.h> |
Joe Hershberger | 0613421 | 2012-10-12 10:23:37 +0000 | [diff] [blame] | 18 | #include <linux/stringify.h> |
Andreas Fenkart | 10667e1 | 2016-03-11 09:39:35 +0100 | [diff] [blame] | 19 | #include <ctype.h> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 20 | #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> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 28 | |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 29 | #ifdef MTD_OLD |
Wolfgang Denk | 1711f3b | 2008-09-02 21:17:36 +0200 | [diff] [blame] | 30 | # include <stdint.h> |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 31 | # include <linux/mtd/mtd.h> |
| 32 | #else |
Wolfgang Denk | c83d7ca | 2008-01-08 22:58:27 +0100 | [diff] [blame] | 33 | # define __user /* nothing */ |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 34 | # include <mtd/mtd-user.h> |
| 35 | #endif |
| 36 | |
Stefano Babic | 9d80b49 | 2017-04-05 18:08:01 +0200 | [diff] [blame] | 37 | #include "fw_env_private.h" |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 38 | #include "fw_env.h" |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 39 | |
Andreas Fenkart | 14070e6 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 40 | struct env_opts default_opts = { |
| 41 | #ifdef CONFIG_FILE |
| 42 | .config_file = CONFIG_FILE |
| 43 | #endif |
| 44 | }; |
| 45 | |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 46 | #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
| 47 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 48 | #define min(x, y) ({ \ |
| 49 | typeof(x) _min1 = (x); \ |
| 50 | typeof(y) _min2 = (y); \ |
| 51 | (void) (&_min1 == &_min2); \ |
| 52 | _min1 < _min2 ? _min1 : _min2; }) |
| 53 | |
| 54 | struct envdev_s { |
Tom Rini | 229695f | 2014-03-28 12:03:33 -0400 | [diff] [blame] | 55 | const char *devname; /* Device name */ |
Stefan Agner | f4742ca | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 56 | long long devoff; /* Device offset */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 57 | ulong env_size; /* environment size */ |
| 58 | ulong erase_size; /* device erase size */ |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 59 | ulong env_sectors; /* number of environment sectors */ |
| 60 | uint8_t mtd_type; /* type of the MTD device */ |
| 61 | }; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 62 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 63 | static struct envdev_s envdevices[2] = |
| 64 | { |
| 65 | { |
| 66 | .mtd_type = MTD_ABSENT, |
| 67 | }, { |
| 68 | .mtd_type = MTD_ABSENT, |
| 69 | }, |
| 70 | }; |
| 71 | static int dev_current; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 72 | |
| 73 | #define DEVNAME(i) envdevices[(i)].devname |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 74 | #define DEVOFFSET(i) envdevices[(i)].devoff |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 75 | #define ENVSIZE(i) envdevices[(i)].env_size |
| 76 | #define DEVESIZE(i) envdevices[(i)].erase_size |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 77 | #define ENVSECTORS(i) envdevices[(i)].env_sectors |
| 78 | #define DEVTYPE(i) envdevices[(i)].mtd_type |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 79 | |
Joe Hershberger | 497f205 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 80 | #define CUR_ENVSIZE ENVSIZE(dev_current) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 81 | |
Andreas Fenkart | f71cee4 | 2016-04-19 22:43:42 +0200 | [diff] [blame] | 82 | static unsigned long usable_envsize; |
| 83 | #define ENV_SIZE usable_envsize |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 84 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 85 | struct env_image_single { |
| 86 | uint32_t crc; /* CRC32 over data bytes */ |
| 87 | char data[]; |
| 88 | }; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 89 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 90 | struct env_image_redundant { |
| 91 | uint32_t crc; /* CRC32 over data bytes */ |
| 92 | unsigned char flags; /* active or obsolete */ |
| 93 | char data[]; |
| 94 | }; |
| 95 | |
| 96 | enum flag_scheme { |
| 97 | FLAG_NONE, |
| 98 | FLAG_BOOLEAN, |
| 99 | FLAG_INCREMENTAL, |
| 100 | }; |
| 101 | |
| 102 | struct environment { |
| 103 | void *image; |
| 104 | uint32_t *crc; |
| 105 | unsigned char *flags; |
| 106 | char *data; |
| 107 | enum flag_scheme flag_scheme; |
| 108 | }; |
| 109 | |
| 110 | static struct environment environment = { |
| 111 | .flag_scheme = FLAG_NONE, |
| 112 | }; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 113 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 114 | static int HaveRedundEnv = 0; |
| 115 | |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 116 | static unsigned char active_flag = 1; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 117 | /* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */ |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 118 | static unsigned char obsolete_flag = 0; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 119 | |
Joe Hershberger | ddd8418 | 2012-10-12 08:48:51 +0000 | [diff] [blame] | 120 | #define DEFAULT_ENV_INSTANCE_STATIC |
| 121 | #include <env_default.h> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 122 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 123 | static int flash_io (int mode); |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 124 | static int parse_config(struct env_opts *opts); |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 125 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 126 | #if defined(CONFIG_FILE) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 127 | static int get_config (char *); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 128 | #endif |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 129 | |
Andreas Fenkart | 938c29f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 130 | static char *skip_chars(char *s) |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 131 | { |
Andreas Fenkart | 938c29f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 132 | for (; *s != '\0'; s++) { |
| 133 | if (isblank(*s)) |
| 134 | return s; |
| 135 | } |
| 136 | return NULL; |
| 137 | } |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 138 | |
Andreas Fenkart | 938c29f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 139 | static char *skip_blanks(char *s) |
| 140 | { |
| 141 | for (; *s != '\0'; s++) { |
| 142 | if (!isblank(*s)) |
Andreas Fenkart | 9583efc | 2016-03-11 09:39:36 +0100 | [diff] [blame] | 143 | return s; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 144 | } |
Andreas Fenkart | 9583efc | 2016-03-11 09:39:36 +0100 | [diff] [blame] | 145 | return NULL; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 146 | } |
| 147 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 148 | /* |
Andreas Fenkart | 1b7427c | 2016-07-16 17:06:14 +0200 | [diff] [blame] | 149 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 150 | * s2 is a 'name=value' pair. |
| 151 | * If the names match, return the value of s2, else NULL. |
| 152 | */ |
| 153 | static char *envmatch(char *s1, char *s2) |
| 154 | { |
| 155 | if (s1 == NULL || s2 == NULL) |
| 156 | return NULL; |
| 157 | |
| 158 | while (*s1 == *s2++) |
| 159 | if (*s1++ == '=') |
| 160 | return s2; |
| 161 | if (*s1 == '\0' && *(s2 - 1) == '=') |
| 162 | return s2; |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | /** |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 167 | * Search the environment for a variable. |
| 168 | * Return the value, if found, or NULL, if not found. |
| 169 | */ |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 170 | char *fw_getenv (char *name) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 171 | { |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 172 | char *env, *nxt; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 173 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 174 | for (env = environment.data; *env; env = nxt + 1) { |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 175 | char *val; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 176 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 177 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 178 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 179 | fprintf (stderr, "## Error: " |
| 180 | "environment not terminated\n"); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 181 | return NULL; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 184 | val = envmatch (name, env); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 185 | if (!val) |
| 186 | continue; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 187 | return val; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 188 | } |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 189 | return NULL; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 193 | * Search the default environment for a variable. |
| 194 | * Return the value, if found, or NULL, if not found. |
| 195 | */ |
| 196 | char *fw_getdefenv(char *name) |
| 197 | { |
| 198 | char *env, *nxt; |
| 199 | |
| 200 | for (env = default_environment; *env; env = nxt + 1) { |
| 201 | char *val; |
| 202 | |
| 203 | for (nxt = env; *nxt; ++nxt) { |
| 204 | if (nxt >= &default_environment[ENV_SIZE]) { |
| 205 | fprintf(stderr, "## Error: " |
| 206 | "default environment not terminated\n"); |
| 207 | return NULL; |
| 208 | } |
| 209 | } |
| 210 | val = envmatch(name, env); |
| 211 | if (!val) |
| 212 | continue; |
| 213 | return val; |
| 214 | } |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | /* |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 219 | * Print the current definition of one, or more, or all |
| 220 | * environment variables |
| 221 | */ |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 222 | int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 223 | { |
Andreas Fenkart | 371ee13 | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 224 | int i, rc = 0; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 225 | |
Andreas Fenkart | c5c41c4 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 226 | if (value_only && argc != 1) { |
| 227 | fprintf(stderr, |
| 228 | "## Error: `-n' option requires exactly one argument\n"); |
| 229 | return -1; |
| 230 | } |
| 231 | |
Andreas Fenkart | 14070e6 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 232 | if (!opts) |
| 233 | opts = &default_opts; |
| 234 | |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 235 | if (fw_env_open(opts)) |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 236 | return -1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 237 | |
Andreas Fenkart | 1ce6869 | 2015-12-09 13:13:25 +0100 | [diff] [blame] | 238 | if (argc == 0) { /* Print all env variables */ |
Andreas Fenkart | c5c41c4 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 239 | char *env, *nxt; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 240 | for (env = environment.data; *env; env = nxt + 1) { |
| 241 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 242 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 243 | fprintf (stderr, "## Error: " |
| 244 | "environment not terminated\n"); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 245 | return -1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 249 | printf ("%s\n", env); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 250 | } |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 251 | fw_env_close(opts); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 252 | return 0; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Andreas Fenkart | c5c41c4 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 255 | for (i = 0; i < argc; ++i) { /* print a subset of env variables */ |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 256 | char *name = argv[i]; |
| 257 | char *val = NULL; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 258 | |
Andreas Fenkart | c5c41c4 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 259 | val = fw_getenv(name); |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 260 | if (!val) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 261 | fprintf (stderr, "## Error: \"%s\" not defined\n", name); |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 262 | rc = -1; |
Andreas Fenkart | c5c41c4 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 263 | continue; |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 264 | } |
Andreas Fenkart | c5c41c4 | 2016-07-16 17:06:15 +0200 | [diff] [blame] | 265 | |
| 266 | if (value_only) { |
| 267 | puts(val); |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | printf("%s=%s\n", name, val); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 272 | } |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 273 | |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 274 | fw_env_close(opts); |
| 275 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 276 | return rc; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 279 | int fw_env_flush(struct env_opts *opts) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 280 | { |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 281 | int ret; |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 282 | |
Andreas Fenkart | 14070e6 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 283 | if (!opts) |
| 284 | opts = &default_opts; |
| 285 | |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 286 | /* |
| 287 | * Update CRC |
| 288 | */ |
| 289 | *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 290 | |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 291 | /* write environment back to flash */ |
| 292 | if (flash_io(O_RDWR)) { |
| 293 | fprintf(stderr, |
| 294 | "Error: can't write fw_env to flash\n"); |
| 295 | return -1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 298 | return 0; |
| 299 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 300 | |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Set/Clear a single variable in the environment. |
| 304 | * This is called in sequence to update the environment |
| 305 | * in RAM without updating the copy in flash after each set |
| 306 | */ |
| 307 | int fw_env_write(char *name, char *value) |
| 308 | { |
| 309 | int len; |
| 310 | char *env, *nxt; |
| 311 | char *oldval = NULL; |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 312 | int deleting, creating, overwriting; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | * search if variable with this name already exists |
| 316 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 317 | for (nxt = env = environment.data; *env; env = nxt + 1) { |
| 318 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 319 | if (nxt >= &environment.data[ENV_SIZE]) { |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 320 | fprintf(stderr, "## Error: " |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 321 | "environment not terminated\n"); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 322 | errno = EINVAL; |
| 323 | return -1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 326 | if ((oldval = envmatch (name, env)) != NULL) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 327 | break; |
| 328 | } |
| 329 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 330 | deleting = (oldval && !(value && strlen(value))); |
| 331 | creating = (!oldval && (value && strlen(value))); |
| 332 | overwriting = (oldval && (value && strlen(value))); |
| 333 | |
| 334 | /* check for permission */ |
| 335 | if (deleting) { |
| 336 | if (env_flags_validate_varaccess(name, |
| 337 | ENV_FLAGS_VARACCESS_PREVENT_DELETE)) { |
| 338 | printf("Can't delete \"%s\"\n", name); |
| 339 | errno = EROFS; |
| 340 | return -1; |
| 341 | } |
| 342 | } else if (overwriting) { |
| 343 | if (env_flags_validate_varaccess(name, |
| 344 | ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) { |
| 345 | printf("Can't overwrite \"%s\"\n", name); |
| 346 | errno = EROFS; |
| 347 | return -1; |
| 348 | } else if (env_flags_validate_varaccess(name, |
| 349 | ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) { |
| 350 | const char *defval = fw_getdefenv(name); |
| 351 | |
| 352 | if (defval == NULL) |
| 353 | defval = ""; |
| 354 | if (strcmp(oldval, defval) |
| 355 | != 0) { |
| 356 | printf("Can't overwrite \"%s\"\n", name); |
| 357 | errno = EROFS; |
| 358 | return -1; |
| 359 | } |
| 360 | } |
| 361 | } else if (creating) { |
| 362 | if (env_flags_validate_varaccess(name, |
| 363 | ENV_FLAGS_VARACCESS_PREVENT_CREATE)) { |
| 364 | printf("Can't create \"%s\"\n", name); |
| 365 | errno = EROFS; |
| 366 | return -1; |
| 367 | } |
| 368 | } else |
| 369 | /* Nothing to do */ |
| 370 | return 0; |
| 371 | |
| 372 | if (deleting || overwriting) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 373 | if (*++nxt == '\0') { |
| 374 | *env = '\0'; |
| 375 | } else { |
| 376 | for (;;) { |
| 377 | *env = *nxt++; |
| 378 | if ((*env == '\0') && (*nxt == '\0')) |
| 379 | break; |
| 380 | ++env; |
| 381 | } |
| 382 | } |
| 383 | *++env = '\0'; |
| 384 | } |
| 385 | |
| 386 | /* Delete only ? */ |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 387 | if (!value || !strlen(value)) |
| 388 | return 0; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 389 | |
| 390 | /* |
| 391 | * Append new definition at the end |
| 392 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 393 | for (env = environment.data; *env || *(env + 1); ++env); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 394 | if (env > environment.data) |
| 395 | ++env; |
| 396 | /* |
| 397 | * Overflow when: |
Joe Hershberger | 497f205 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 398 | * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 399 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 400 | len = strlen (name) + 2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 401 | /* add '=' for first arg, ' ' for all others */ |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 402 | len += strlen(value) + 1; |
| 403 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 404 | if (len > (&environment.data[ENV_SIZE] - env)) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 405 | fprintf (stderr, |
| 406 | "Error: environment overflow, \"%s\" deleted\n", |
| 407 | name); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 408 | return -1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 409 | } |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 410 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 411 | while ((*env = *name++) != '\0') |
| 412 | env++; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 413 | *env = '='; |
| 414 | while ((*++env = *value++) != '\0') |
| 415 | ; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 416 | |
| 417 | /* end is marked with double '\0' */ |
| 418 | *++env = '\0'; |
| 419 | |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 420 | return 0; |
| 421 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 422 | |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 423 | /* |
| 424 | * Deletes or sets environment variables. Returns -1 and sets errno error codes: |
| 425 | * 0 - OK |
| 426 | * EINVAL - need at least 1 argument |
| 427 | * EROFS - certain variables ("ethaddr", "serial#") cannot be |
| 428 | * modified or deleted |
| 429 | * |
| 430 | */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 431 | int fw_env_set(int argc, char *argv[], struct env_opts *opts) |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 432 | { |
Andreas Fenkart | 371ee13 | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 433 | int i; |
Mike Frysinger | 3779c8e | 2012-11-10 19:47:45 +0000 | [diff] [blame] | 434 | size_t len; |
Andreas Fenkart | 167f525 | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 435 | char *name, **valv; |
xypron.glpk@gmx.de | ddc6a9d | 2017-04-15 13:05:40 +0200 | [diff] [blame] | 436 | char *oldval; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 437 | char *value = NULL; |
Andreas Fenkart | 167f525 | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 438 | int valc; |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 439 | int ret; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 440 | |
Andreas Fenkart | 14070e6 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 441 | if (!opts) |
| 442 | opts = &default_opts; |
| 443 | |
Andreas Fenkart | 1ce6869 | 2015-12-09 13:13:25 +0100 | [diff] [blame] | 444 | if (argc < 1) { |
| 445 | fprintf(stderr, "## Error: variable name missing\n"); |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 446 | errno = EINVAL; |
| 447 | return -1; |
| 448 | } |
| 449 | |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 450 | if (fw_env_open(opts)) { |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 451 | fprintf(stderr, "Error: environment not initialized\n"); |
| 452 | return -1; |
| 453 | } |
| 454 | |
Andreas Fenkart | 1ce6869 | 2015-12-09 13:13:25 +0100 | [diff] [blame] | 455 | name = argv[0]; |
| 456 | valv = argv + 1; |
| 457 | valc = argc - 1; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 458 | |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 459 | if (env_flags_validate_env_set_params(name, valv, valc) < 0) { |
| 460 | fw_env_close(opts); |
Andreas Fenkart | fd4e328 | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 461 | return -1; |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 462 | } |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 463 | |
Joe Hershberger | 62a34a0 | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 464 | len = 0; |
Andreas Fenkart | 167f525 | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 465 | for (i = 0; i < valc; ++i) { |
| 466 | char *val = valv[i]; |
Joe Hershberger | 62a34a0 | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 467 | size_t val_len = strlen(val); |
| 468 | |
Joe Hershberger | ce2f580 | 2012-10-15 15:29:24 +0000 | [diff] [blame] | 469 | if (value) |
| 470 | value[len - 1] = ' '; |
xypron.glpk@gmx.de | ddc6a9d | 2017-04-15 13:05:40 +0200 | [diff] [blame] | 471 | oldval = value; |
Joe Hershberger | 62a34a0 | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 472 | value = realloc(value, len + val_len + 1); |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 473 | if (!value) { |
Joe Hershberger | 62a34a0 | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 474 | fprintf(stderr, |
Luka Perkov | 8603b69 | 2011-09-05 23:40:13 +0200 | [diff] [blame] | 475 | "Cannot malloc %zu bytes: %s\n", |
Joe Hershberger | 62a34a0 | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 476 | len, strerror(errno)); |
xypron.glpk@gmx.de | ddc6a9d | 2017-04-15 13:05:40 +0200 | [diff] [blame] | 477 | free(oldval); |
Joe Hershberger | 62a34a0 | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 478 | return -1; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 479 | } |
Joe Hershberger | 62a34a0 | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 480 | |
| 481 | memcpy(value + len, val, val_len); |
| 482 | len += val_len; |
Joe Hershberger | ce2f580 | 2012-10-15 15:29:24 +0000 | [diff] [blame] | 483 | value[len++] = '\0'; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | fw_env_write(name, value); |
| 487 | |
Joe Hershberger | 62a34a0 | 2012-10-03 09:38:47 +0000 | [diff] [blame] | 488 | free(value); |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 489 | |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 490 | ret = fw_env_flush(opts); |
| 491 | fw_env_close(opts); |
| 492 | |
| 493 | return ret; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Parse a file and configure the u-boot variables. |
| 498 | * The script file has a very simple format, as follows: |
| 499 | * |
| 500 | * Each line has a couple with name, value: |
| 501 | * <white spaces>variable_name<white spaces>variable_value |
| 502 | * |
| 503 | * Both variable_name and variable_value are interpreted as strings. |
| 504 | * Any character after <white spaces> and before ending \r\n is interpreted |
| 505 | * as variable's value (no comment allowed on these lines !) |
| 506 | * |
| 507 | * Comments are allowed if the first character in the line is # |
| 508 | * |
| 509 | * Returns -1 and sets errno error codes: |
| 510 | * 0 - OK |
| 511 | * -1 - Error |
| 512 | */ |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 513 | int fw_parse_script(char *fname, struct env_opts *opts) |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 514 | { |
| 515 | FILE *fp; |
| 516 | char dump[1024]; /* Maximum line length in the file */ |
| 517 | char *name; |
| 518 | char *val; |
| 519 | int lineno = 0; |
| 520 | int len; |
| 521 | int ret = 0; |
| 522 | |
Andreas Fenkart | 14070e6 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 523 | if (!opts) |
| 524 | opts = &default_opts; |
| 525 | |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 526 | if (fw_env_open(opts)) { |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 527 | fprintf(stderr, "Error: environment not initialized\n"); |
| 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | if (strcmp(fname, "-") == 0) |
| 532 | fp = stdin; |
| 533 | else { |
| 534 | fp = fopen(fname, "r"); |
| 535 | if (fp == NULL) { |
| 536 | fprintf(stderr, "I cannot open %s for reading\n", |
| 537 | fname); |
| 538 | return -1; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | while (fgets(dump, sizeof(dump), fp)) { |
| 543 | lineno++; |
| 544 | len = strlen(dump); |
| 545 | |
| 546 | /* |
| 547 | * Read a whole line from the file. If the line is too long |
| 548 | * or is not terminated, reports an error and exit. |
| 549 | */ |
| 550 | if (dump[len - 1] != '\n') { |
| 551 | fprintf(stderr, |
| 552 | "Line %d not corrected terminated or too long\n", |
| 553 | lineno); |
| 554 | ret = -1; |
| 555 | break; |
| 556 | } |
| 557 | |
| 558 | /* Drop ending line feed / carriage return */ |
Andreas Fenkart | 69067a3 | 2016-03-11 09:39:38 +0100 | [diff] [blame] | 559 | dump[--len] = '\0'; |
| 560 | if (len && dump[len - 1] == '\r') |
| 561 | dump[--len] = '\0'; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 562 | |
| 563 | /* Skip comment or empty lines */ |
Andreas Fenkart | 69067a3 | 2016-03-11 09:39:38 +0100 | [diff] [blame] | 564 | if (len == 0 || dump[0] == '#') |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 565 | continue; |
| 566 | |
| 567 | /* |
| 568 | * Search for variable's name, |
| 569 | * remove leading whitespaces |
| 570 | */ |
Andreas Fenkart | 938c29f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 571 | name = skip_blanks(dump); |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 572 | if (!name) |
| 573 | continue; |
| 574 | |
| 575 | /* The first white space is the end of variable name */ |
Andreas Fenkart | 938c29f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 576 | val = skip_chars(name); |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 577 | len = strlen(name); |
| 578 | if (val) { |
| 579 | *val++ = '\0'; |
| 580 | if ((val - name) < len) |
Andreas Fenkart | 938c29f | 2016-03-11 09:39:37 +0100 | [diff] [blame] | 581 | val = skip_blanks(val); |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 582 | else |
| 583 | val = NULL; |
| 584 | } |
| 585 | |
| 586 | #ifdef DEBUG |
| 587 | fprintf(stderr, "Setting %s : %s\n", |
| 588 | name, val ? val : " removed"); |
| 589 | #endif |
| 590 | |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 591 | if (env_flags_validate_type(name, val) < 0) { |
| 592 | ret = -1; |
| 593 | break; |
| 594 | } |
| 595 | |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 596 | /* |
| 597 | * If there is an error setting a variable, |
| 598 | * try to save the environment and returns an error |
| 599 | */ |
| 600 | if (fw_env_write(name, val)) { |
| 601 | fprintf(stderr, |
| 602 | "fw_env_write returns with error : %s\n", |
| 603 | strerror(errno)); |
| 604 | ret = -1; |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | } |
| 609 | |
| 610 | /* Close file if not stdin */ |
| 611 | if (strcmp(fname, "-") != 0) |
| 612 | fclose(fp); |
| 613 | |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 614 | ret |= fw_env_flush(opts); |
| 615 | |
| 616 | fw_env_close(opts); |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 617 | |
| 618 | return ret; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Andreas Fenkart | e2c9351 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 621 | /** |
| 622 | * environment_end() - compute offset of first byte right after environemnt |
| 623 | * @dev - index of enviroment buffer |
| 624 | * Return: |
| 625 | * device offset of first byte right after environemnt |
| 626 | */ |
| 627 | off_t environment_end(int dev) |
| 628 | { |
| 629 | /* environment is block aligned */ |
| 630 | return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev); |
| 631 | } |
| 632 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 633 | /* |
| 634 | * Test for bad block on NAND, just returns 0 on NOR, on NAND: |
| 635 | * 0 - block is good |
| 636 | * > 0 - block is bad |
| 637 | * < 0 - failed to test |
| 638 | */ |
Andreas Fenkart | c6012bb | 2016-08-29 23:16:58 +0200 | [diff] [blame] | 639 | static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart) |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 640 | { |
| 641 | if (mtd_type == MTD_NANDFLASH) { |
Andreas Fenkart | c6012bb | 2016-08-29 23:16:58 +0200 | [diff] [blame] | 642 | int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 643 | |
| 644 | if (badblock < 0) { |
| 645 | perror ("Cannot read bad block mark"); |
| 646 | return badblock; |
| 647 | } |
| 648 | |
| 649 | if (badblock) { |
| 650 | #ifdef DEBUG |
Marcin Niestroj | 81c878d | 2016-05-06 14:58:29 +0200 | [diff] [blame] | 651 | fprintf (stderr, "Bad block at 0x%llx, skipping\n", |
Andreas Fenkart | c6012bb | 2016-08-29 23:16:58 +0200 | [diff] [blame] | 652 | (unsigned long long)blockstart); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 653 | #endif |
| 654 | return badblock; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * Read data from flash at an offset into a provided buffer. On NAND it skips |
| 663 | * bad blocks but makes sure it stays within ENVSECTORS (dev) starting from |
| 664 | * the DEVOFFSET (dev) block. On NOR the loop is only run once. |
| 665 | */ |
| 666 | static int flash_read_buf (int dev, int fd, void *buf, size_t count, |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 667 | off_t offset) |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 668 | { |
| 669 | size_t blocklen; /* erase / write length - one block on NAND, |
| 670 | 0 on NOR */ |
| 671 | size_t processed = 0; /* progress counter */ |
| 672 | size_t readlen = count; /* current read length */ |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 673 | off_t block_seek; /* offset inside the current block to the start |
| 674 | of the data */ |
| 675 | loff_t blockstart; /* running start of the current block - |
| 676 | MEMGETBADBLOCK needs 64 bits */ |
| 677 | int rc; |
| 678 | |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 679 | blockstart = (offset / DEVESIZE (dev)) * DEVESIZE (dev); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 680 | |
| 681 | /* Offset inside a block */ |
| 682 | block_seek = offset - blockstart; |
| 683 | |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 684 | if (DEVTYPE(dev) == MTD_NANDFLASH) { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 685 | /* |
| 686 | * NAND: calculate which blocks we are reading. We have |
| 687 | * to read one block at a time to skip bad blocks. |
| 688 | */ |
| 689 | blocklen = DEVESIZE (dev); |
| 690 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 691 | /* Limit to one block for the first read */ |
| 692 | if (readlen > blocklen - block_seek) |
| 693 | readlen = blocklen - block_seek; |
| 694 | } else { |
| 695 | blocklen = 0; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* This only runs once on NOR flash */ |
| 699 | while (processed < count) { |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 700 | rc = flash_bad_block(fd, DEVTYPE(dev), blockstart); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 701 | if (rc < 0) /* block test failed */ |
| 702 | return -1; |
| 703 | |
Andreas Fenkart | e2c9351 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 704 | if (blockstart + block_seek + readlen > environment_end(dev)) { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 705 | /* End of range is reached */ |
| 706 | fprintf (stderr, |
| 707 | "Too few good blocks within range\n"); |
| 708 | return -1; |
| 709 | } |
| 710 | |
| 711 | if (rc) { /* block is bad */ |
| 712 | blockstart += blocklen; |
| 713 | continue; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * If a block is bad, we retry in the next block at the same |
Tom Rini | 2d3229b | 2017-09-05 14:01:29 -0400 | [diff] [blame] | 718 | * offset - see env/nand.c::writeenv() |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 719 | */ |
| 720 | lseek (fd, blockstart + block_seek, SEEK_SET); |
| 721 | |
| 722 | rc = read (fd, buf + processed, readlen); |
| 723 | if (rc != readlen) { |
| 724 | fprintf (stderr, "Read error on %s: %s\n", |
| 725 | DEVNAME (dev), strerror (errno)); |
| 726 | return -1; |
| 727 | } |
| 728 | #ifdef DEBUG |
Joe Hershberger | 74620e1 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 729 | fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n", |
Marcin Niestroj | 81c878d | 2016-05-06 14:58:29 +0200 | [diff] [blame] | 730 | rc, (unsigned long long) blockstart + block_seek, |
| 731 | DEVNAME(dev)); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 732 | #endif |
| 733 | processed += readlen; |
| 734 | readlen = min (blocklen, count - processed); |
| 735 | block_seek = 0; |
| 736 | blockstart += blocklen; |
| 737 | } |
| 738 | |
| 739 | return processed; |
| 740 | } |
| 741 | |
| 742 | /* |
Andreas Fenkart | 24307d6 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 743 | * Write count bytes from begin of environment, but stay within |
| 744 | * ENVSECTORS(dev) sectors of |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 745 | * DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we |
| 746 | * erase and write the whole data at once. |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 747 | */ |
Andreas Fenkart | 24307d6 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 748 | static int flash_write_buf(int dev, int fd, void *buf, size_t count) |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 749 | { |
| 750 | void *data; |
| 751 | struct erase_info_user erase; |
| 752 | size_t blocklen; /* length of NAND block / NOR erase sector */ |
| 753 | size_t erase_len; /* whole area that can be erased - may include |
| 754 | bad blocks */ |
| 755 | size_t erasesize; /* erase / write length - one block on NAND, |
| 756 | whole area on NOR */ |
| 757 | size_t processed = 0; /* progress counter */ |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 758 | size_t write_total; /* total size to actually write - excluding |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 759 | bad blocks */ |
| 760 | off_t erase_offset; /* offset to the first erase block (aligned) |
| 761 | below offset */ |
| 762 | off_t block_seek; /* offset inside the erase block to the start |
| 763 | of the data */ |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 764 | loff_t blockstart; /* running start of the current block - |
| 765 | MEMGETBADBLOCK needs 64 bits */ |
| 766 | int rc; |
| 767 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 768 | /* |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 769 | * For mtd devices only offset and size of the environment do matter |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 770 | */ |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 771 | if (DEVTYPE(dev) == MTD_ABSENT) { |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 772 | blocklen = count; |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 773 | erase_len = blocklen; |
Andreas Fenkart | 24307d6 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 774 | blockstart = DEVOFFSET(dev); |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 775 | block_seek = 0; |
| 776 | write_total = blocklen; |
| 777 | } else { |
| 778 | blocklen = DEVESIZE(dev); |
| 779 | |
Andreas Fenkart | 24307d6 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 780 | erase_offset = DEVOFFSET(dev); |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 781 | |
| 782 | /* Maximum area we may use */ |
Andreas Fenkart | e2c9351 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 783 | erase_len = environment_end(dev) - erase_offset; |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 784 | |
| 785 | blockstart = erase_offset; |
Andreas Fenkart | 24307d6 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 786 | |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 787 | /* Offset inside a block */ |
Andreas Fenkart | 24307d6 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 788 | block_seek = DEVOFFSET(dev) - erase_offset; |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 789 | |
| 790 | /* |
| 791 | * Data size we actually write: from the start of the block |
| 792 | * to the start of the data, then count bytes of data, and |
| 793 | * to the end of the block |
| 794 | */ |
| 795 | write_total = ((block_seek + count + blocklen - 1) / |
| 796 | blocklen) * blocklen; |
| 797 | } |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 798 | |
| 799 | /* |
| 800 | * Support data anywhere within erase sectors: read out the complete |
| 801 | * area to be erased, replace the environment image, write the whole |
| 802 | * block back again. |
| 803 | */ |
| 804 | if (write_total > count) { |
| 805 | data = malloc (erase_len); |
| 806 | if (!data) { |
| 807 | fprintf (stderr, |
Luka Perkov | 8603b69 | 2011-09-05 23:40:13 +0200 | [diff] [blame] | 808 | "Cannot malloc %zu bytes: %s\n", |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 809 | erase_len, strerror (errno)); |
| 810 | return -1; |
| 811 | } |
| 812 | |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 813 | rc = flash_read_buf(dev, fd, data, write_total, erase_offset); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 814 | if (write_total != rc) |
| 815 | return -1; |
| 816 | |
Joe Hershberger | 74620e1 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 817 | #ifdef DEBUG |
| 818 | fprintf(stderr, "Preserving data "); |
| 819 | if (block_seek != 0) |
| 820 | fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1); |
| 821 | if (block_seek + count != write_total) { |
| 822 | if (block_seek != 0) |
| 823 | fprintf(stderr, " and "); |
Marcin Niestroj | 81c878d | 2016-05-06 14:58:29 +0200 | [diff] [blame] | 824 | fprintf(stderr, "0x%lx - 0x%lx", |
| 825 | (unsigned long) block_seek + count, |
| 826 | (unsigned long) write_total - 1); |
Joe Hershberger | 74620e1 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 827 | } |
| 828 | fprintf(stderr, "\n"); |
| 829 | #endif |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 830 | /* Overwrite the old environment */ |
| 831 | memcpy (data + block_seek, buf, count); |
| 832 | } else { |
| 833 | /* |
| 834 | * We get here, iff offset is block-aligned and count is a |
| 835 | * multiple of blocklen - see write_total calculation above |
| 836 | */ |
| 837 | data = buf; |
| 838 | } |
| 839 | |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 840 | if (DEVTYPE(dev) == MTD_NANDFLASH) { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 841 | /* |
| 842 | * NAND: calculate which blocks we are writing. We have |
| 843 | * to write one block at a time to skip bad blocks. |
| 844 | */ |
| 845 | erasesize = blocklen; |
| 846 | } else { |
| 847 | erasesize = erase_len; |
| 848 | } |
| 849 | |
| 850 | erase.length = erasesize; |
| 851 | |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 852 | /* This only runs once on NOR flash and SPI-dataflash */ |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 853 | while (processed < write_total) { |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 854 | rc = flash_bad_block(fd, DEVTYPE(dev), blockstart); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 855 | if (rc < 0) /* block test failed */ |
| 856 | return rc; |
| 857 | |
Andreas Fenkart | e2c9351 | 2016-08-29 23:16:57 +0200 | [diff] [blame] | 858 | if (blockstart + erasesize > environment_end(dev)) { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 859 | fprintf (stderr, "End of range reached, aborting\n"); |
| 860 | return -1; |
| 861 | } |
| 862 | |
| 863 | if (rc) { /* block is bad */ |
| 864 | blockstart += blocklen; |
| 865 | continue; |
| 866 | } |
| 867 | |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 868 | if (DEVTYPE(dev) != MTD_ABSENT) { |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 869 | erase.start = blockstart; |
| 870 | ioctl(fd, MEMUNLOCK, &erase); |
| 871 | /* These do not need an explicit erase cycle */ |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 872 | if (DEVTYPE(dev) != MTD_DATAFLASH) |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 873 | if (ioctl(fd, MEMERASE, &erase) != 0) { |
| 874 | fprintf(stderr, |
| 875 | "MTD erase error on %s: %s\n", |
| 876 | DEVNAME(dev), strerror(errno)); |
| 877 | return -1; |
| 878 | } |
| 879 | } |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 880 | |
| 881 | if (lseek (fd, blockstart, SEEK_SET) == -1) { |
| 882 | fprintf (stderr, |
| 883 | "Seek error on %s: %s\n", |
| 884 | DEVNAME (dev), strerror (errno)); |
| 885 | return -1; |
| 886 | } |
| 887 | |
| 888 | #ifdef DEBUG |
Marcin Niestroj | 81c878d | 2016-05-06 14:58:29 +0200 | [diff] [blame] | 889 | fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n", |
| 890 | (unsigned long long) erasesize, |
| 891 | (unsigned long long) blockstart); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 892 | #endif |
| 893 | if (write (fd, data + processed, erasesize) != erasesize) { |
| 894 | fprintf (stderr, "Write error on %s: %s\n", |
| 895 | DEVNAME (dev), strerror (errno)); |
| 896 | return -1; |
| 897 | } |
| 898 | |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 899 | if (DEVTYPE(dev) != MTD_ABSENT) |
Oliver Metz | e387efb | 2013-08-30 00:56:02 +0200 | [diff] [blame] | 900 | ioctl(fd, MEMLOCK, &erase); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 901 | |
Dustin Byford | 4b774ff | 2014-03-06 20:48:23 -0800 | [diff] [blame] | 902 | processed += erasesize; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 903 | block_seek = 0; |
Dustin Byford | 4b774ff | 2014-03-06 20:48:23 -0800 | [diff] [blame] | 904 | blockstart += erasesize; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | if (write_total > count) |
| 908 | free (data); |
| 909 | |
| 910 | return processed; |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * Set obsolete flag at offset - NOR flash only |
| 915 | */ |
| 916 | static int flash_flag_obsolete (int dev, int fd, off_t offset) |
| 917 | { |
| 918 | int rc; |
Detlev Zundel | 0aef7bc | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 919 | struct erase_info_user erase; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 920 | |
Detlev Zundel | 0aef7bc | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 921 | erase.start = DEVOFFSET (dev); |
| 922 | erase.length = DEVESIZE (dev); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 923 | /* This relies on the fact, that obsolete_flag == 0 */ |
| 924 | rc = lseek (fd, offset, SEEK_SET); |
| 925 | if (rc < 0) { |
| 926 | fprintf (stderr, "Cannot seek to set the flag on %s \n", |
| 927 | DEVNAME (dev)); |
| 928 | return rc; |
| 929 | } |
Detlev Zundel | 0aef7bc | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 930 | ioctl (fd, MEMUNLOCK, &erase); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 931 | rc = write (fd, &obsolete_flag, sizeof (obsolete_flag)); |
Detlev Zundel | 0aef7bc | 2010-07-30 11:22:15 +0200 | [diff] [blame] | 932 | ioctl (fd, MEMLOCK, &erase); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 933 | if (rc < 0) |
| 934 | perror ("Could not set obsolete flag"); |
| 935 | |
| 936 | return rc; |
| 937 | } |
| 938 | |
| 939 | static int flash_write (int fd_current, int fd_target, int dev_target) |
| 940 | { |
| 941 | int rc; |
| 942 | |
| 943 | switch (environment.flag_scheme) { |
| 944 | case FLAG_NONE: |
| 945 | break; |
| 946 | case FLAG_INCREMENTAL: |
| 947 | (*environment.flags)++; |
| 948 | break; |
| 949 | case FLAG_BOOLEAN: |
| 950 | *environment.flags = active_flag; |
| 951 | break; |
| 952 | default: |
| 953 | fprintf (stderr, "Unimplemented flash scheme %u \n", |
| 954 | environment.flag_scheme); |
| 955 | return -1; |
| 956 | } |
| 957 | |
| 958 | #ifdef DEBUG |
Stefan Agner | f4742ca | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 959 | fprintf(stderr, "Writing new environment at 0x%llx on %s\n", |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 960 | DEVOFFSET (dev_target), DEVNAME (dev_target)); |
| 961 | #endif |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 962 | |
Joe Hershberger | 497f205 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 963 | rc = flash_write_buf(dev_target, fd_target, environment.image, |
Andreas Fenkart | 24307d6 | 2016-08-29 23:17:00 +0200 | [diff] [blame] | 964 | CUR_ENVSIZE); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 965 | if (rc < 0) |
| 966 | return rc; |
| 967 | |
| 968 | if (environment.flag_scheme == FLAG_BOOLEAN) { |
| 969 | /* Have to set obsolete flag */ |
| 970 | off_t offset = DEVOFFSET (dev_current) + |
| 971 | offsetof (struct env_image_redundant, flags); |
| 972 | #ifdef DEBUG |
Joe Hershberger | 74620e1 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 973 | fprintf(stderr, |
Stefan Agner | f4742ca | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 974 | "Setting obsolete flag in environment at 0x%llx on %s\n", |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 975 | DEVOFFSET (dev_current), DEVNAME (dev_current)); |
| 976 | #endif |
| 977 | flash_flag_obsolete (dev_current, fd_current, offset); |
| 978 | } |
| 979 | |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | static int flash_read (int fd) |
| 984 | { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 985 | int rc; |
| 986 | |
Joe Hershberger | 497f205 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 987 | rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE, |
Andreas Fenkart | ff95e57 | 2016-08-29 23:16:59 +0200 | [diff] [blame] | 988 | DEVOFFSET(dev_current)); |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 989 | if (rc != CUR_ENVSIZE) |
| 990 | return -1; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 991 | |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 992 | return 0; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | static int flash_io (int mode) |
| 996 | { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 997 | int fd_current, fd_target, rc, dev_target; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 998 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 999 | /* dev_current: fd_current, erase_current */ |
| 1000 | fd_current = open (DEVNAME (dev_current), mode); |
| 1001 | if (fd_current < 0) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1002 | fprintf (stderr, |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1003 | "Can't open %s: %s\n", |
| 1004 | DEVNAME (dev_current), strerror (errno)); |
| 1005 | return -1; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1006 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1007 | |
| 1008 | if (mode == O_RDWR) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1009 | if (HaveRedundEnv) { |
| 1010 | /* switch to next partition for writing */ |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1011 | dev_target = !dev_current; |
| 1012 | /* dev_target: fd_target, erase_target */ |
| 1013 | fd_target = open (DEVNAME (dev_target), mode); |
| 1014 | if (fd_target < 0) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1015 | fprintf (stderr, |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1016 | "Can't open %s: %s\n", |
| 1017 | DEVNAME (dev_target), |
| 1018 | strerror (errno)); |
| 1019 | rc = -1; |
| 1020 | goto exit; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } else { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1023 | dev_target = dev_current; |
| 1024 | fd_target = fd_current; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1025 | } |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1026 | |
| 1027 | rc = flash_write (fd_current, fd_target, dev_target); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1028 | |
Lukasz Majewski | 40fe89c | 2017-08-27 13:46:22 +0200 | [diff] [blame] | 1029 | if (fsync(fd_current) && |
| 1030 | !(errno == EINVAL || errno == EROFS)) { |
Michael Heimpold | 7e99e14 | 2013-05-20 21:34:42 +0200 | [diff] [blame] | 1031 | fprintf (stderr, |
| 1032 | "fsync failed on %s: %s\n", |
| 1033 | DEVNAME (dev_current), strerror (errno)); |
| 1034 | } |
| 1035 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1036 | if (HaveRedundEnv) { |
Lukasz Majewski | 40fe89c | 2017-08-27 13:46:22 +0200 | [diff] [blame] | 1037 | if (fsync(fd_target) && |
| 1038 | !(errno == EINVAL || errno == EROFS)) { |
Michael Heimpold | 7e99e14 | 2013-05-20 21:34:42 +0200 | [diff] [blame] | 1039 | fprintf (stderr, |
| 1040 | "fsync failed on %s: %s\n", |
| 1041 | DEVNAME (dev_current), strerror (errno)); |
| 1042 | } |
| 1043 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1044 | if (close (fd_target)) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1045 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1046 | "I/O error on %s: %s\n", |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1047 | DEVNAME (dev_target), |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1048 | strerror (errno)); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1049 | rc = -1; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1050 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1051 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1052 | } else { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1053 | rc = flash_read (fd_current); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1056 | exit: |
| 1057 | if (close (fd_current)) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1058 | fprintf (stderr, |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1059 | "I/O error on %s: %s\n", |
| 1060 | DEVNAME (dev_current), strerror (errno)); |
| 1061 | return -1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1064 | return rc; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /* |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1068 | * Prevent confusion if running from erased flash memory |
| 1069 | */ |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1070 | int fw_env_open(struct env_opts *opts) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1071 | { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1072 | int crc0, crc0_ok; |
Jon Povey | 735eb0f | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1073 | unsigned char flag0; |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1074 | void *addr0 = NULL; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1075 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1076 | int crc1, crc1_ok; |
Jon Povey | 735eb0f | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1077 | unsigned char flag1; |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1078 | void *addr1 = NULL; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1079 | |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 1080 | int ret; |
| 1081 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1082 | struct env_image_single *single; |
| 1083 | struct env_image_redundant *redundant; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1084 | |
Andreas Fenkart | 14070e6 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 1085 | if (!opts) |
| 1086 | opts = &default_opts; |
| 1087 | |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1088 | if (parse_config(opts)) /* should fill envdevices */ |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1089 | return -EINVAL; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1090 | |
Joe Hershberger | 497f205 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1091 | addr0 = calloc(1, CUR_ENVSIZE); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1092 | if (addr0 == NULL) { |
Joe Hershberger | 497f205 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1093 | fprintf(stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1094 | "Not enough memory for environment (%ld bytes)\n", |
Joe Hershberger | 497f205 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1095 | CUR_ENVSIZE); |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1096 | ret = -ENOMEM; |
| 1097 | goto open_cleanup; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1098 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1099 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1100 | /* read environment from FLASH to local buffer */ |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1101 | environment.image = addr0; |
| 1102 | |
| 1103 | if (HaveRedundEnv) { |
| 1104 | redundant = addr0; |
| 1105 | environment.crc = &redundant->crc; |
| 1106 | environment.flags = &redundant->flags; |
| 1107 | environment.data = redundant->data; |
| 1108 | } else { |
| 1109 | single = addr0; |
| 1110 | environment.crc = &single->crc; |
| 1111 | environment.flags = NULL; |
| 1112 | environment.data = single->data; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1113 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1114 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1115 | dev_current = 0; |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1116 | if (flash_io(O_RDONLY)) { |
| 1117 | ret = -EIO; |
| 1118 | goto open_cleanup; |
| 1119 | } |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1120 | |
| 1121 | crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE); |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 1122 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1123 | crc0_ok = (crc0 == *environment.crc); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1124 | if (!HaveRedundEnv) { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1125 | if (!crc0_ok) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1126 | fprintf (stderr, |
| 1127 | "Warning: Bad CRC, using default environment\n"); |
Wolfgang Denk | f07217c | 2006-03-12 23:27:46 +0100 | [diff] [blame] | 1128 | memcpy(environment.data, default_environment, sizeof default_environment); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1129 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1130 | } else { |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1131 | flag0 = *environment.flags; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1132 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1133 | dev_current = 1; |
Joe Hershberger | 497f205 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1134 | addr1 = calloc(1, CUR_ENVSIZE); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1135 | if (addr1 == NULL) { |
Joe Hershberger | 497f205 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1136 | fprintf(stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1137 | "Not enough memory for environment (%ld bytes)\n", |
Joe Hershberger | 497f205 | 2012-10-03 09:38:46 +0000 | [diff] [blame] | 1138 | CUR_ENVSIZE); |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1139 | ret = -ENOMEM; |
| 1140 | goto open_cleanup; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1141 | } |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1142 | redundant = addr1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1143 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1144 | /* |
| 1145 | * have to set environment.image for flash_read(), careful - |
| 1146 | * other pointers in environment still point inside addr0 |
| 1147 | */ |
| 1148 | environment.image = addr1; |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1149 | if (flash_io(O_RDONLY)) { |
| 1150 | ret = -EIO; |
| 1151 | goto open_cleanup; |
| 1152 | } |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1153 | |
| 1154 | /* Check flag scheme compatibility */ |
| 1155 | if (DEVTYPE(dev_current) == MTD_NORFLASH && |
| 1156 | DEVTYPE(!dev_current) == MTD_NORFLASH) { |
| 1157 | environment.flag_scheme = FLAG_BOOLEAN; |
| 1158 | } else if (DEVTYPE(dev_current) == MTD_NANDFLASH && |
| 1159 | DEVTYPE(!dev_current) == MTD_NANDFLASH) { |
| 1160 | environment.flag_scheme = FLAG_INCREMENTAL; |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1161 | } else if (DEVTYPE(dev_current) == MTD_DATAFLASH && |
| 1162 | DEVTYPE(!dev_current) == MTD_DATAFLASH) { |
| 1163 | environment.flag_scheme = FLAG_BOOLEAN; |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 1164 | } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME && |
| 1165 | DEVTYPE(!dev_current) == MTD_UBIVOLUME) { |
| 1166 | environment.flag_scheme = FLAG_INCREMENTAL; |
Oliver Metz | 23ef62d | 2013-08-30 00:56:01 +0200 | [diff] [blame] | 1167 | } else if (DEVTYPE(dev_current) == MTD_ABSENT && |
| 1168 | DEVTYPE(!dev_current) == MTD_ABSENT) { |
| 1169 | environment.flag_scheme = FLAG_INCREMENTAL; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1170 | } else { |
| 1171 | fprintf (stderr, "Incompatible flash types!\n"); |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1172 | ret = -EINVAL; |
| 1173 | goto open_cleanup; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1174 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1175 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1176 | crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE); |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 1177 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1178 | crc1_ok = (crc1 == redundant->crc); |
| 1179 | flag1 = redundant->flags; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1180 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1181 | if (crc0_ok && !crc1_ok) { |
| 1182 | dev_current = 0; |
| 1183 | } else if (!crc0_ok && crc1_ok) { |
| 1184 | dev_current = 1; |
| 1185 | } else if (!crc0_ok && !crc1_ok) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1186 | fprintf (stderr, |
| 1187 | "Warning: Bad CRC, using default environment\n"); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1188 | memcpy (environment.data, default_environment, |
| 1189 | sizeof default_environment); |
| 1190 | dev_current = 0; |
| 1191 | } else { |
| 1192 | switch (environment.flag_scheme) { |
| 1193 | case FLAG_BOOLEAN: |
| 1194 | if (flag0 == active_flag && |
| 1195 | flag1 == obsolete_flag) { |
| 1196 | dev_current = 0; |
| 1197 | } else if (flag0 == obsolete_flag && |
| 1198 | flag1 == active_flag) { |
| 1199 | dev_current = 1; |
| 1200 | } else if (flag0 == flag1) { |
| 1201 | dev_current = 0; |
| 1202 | } else if (flag0 == 0xFF) { |
| 1203 | dev_current = 0; |
| 1204 | } else if (flag1 == 0xFF) { |
| 1205 | dev_current = 1; |
| 1206 | } else { |
| 1207 | dev_current = 0; |
| 1208 | } |
| 1209 | break; |
| 1210 | case FLAG_INCREMENTAL: |
Jon Povey | 735eb0f | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1211 | if (flag0 == 255 && flag1 == 0) |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1212 | dev_current = 1; |
| 1213 | else if ((flag1 == 255 && flag0 == 0) || |
Jon Povey | 735eb0f | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1214 | flag0 >= flag1) |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1215 | dev_current = 0; |
Jon Povey | 735eb0f | 2011-03-11 14:10:56 +0900 | [diff] [blame] | 1216 | else /* flag1 > flag0 */ |
| 1217 | dev_current = 1; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1218 | break; |
| 1219 | default: |
| 1220 | fprintf (stderr, "Unknown flag scheme %u \n", |
| 1221 | environment.flag_scheme); |
| 1222 | return -1; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * If we are reading, we don't need the flag and the CRC any |
| 1228 | * more, if we are writing, we will re-calculate CRC and update |
| 1229 | * flags before writing out |
| 1230 | */ |
| 1231 | if (dev_current) { |
| 1232 | environment.image = addr1; |
| 1233 | environment.crc = &redundant->crc; |
| 1234 | environment.flags = &redundant->flags; |
| 1235 | environment.data = redundant->data; |
| 1236 | free (addr0); |
| 1237 | } else { |
| 1238 | environment.image = addr0; |
| 1239 | /* Other pointers are already set */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1240 | free (addr1); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1241 | } |
Joe Hershberger | 74620e1 | 2012-10-15 15:29:25 +0000 | [diff] [blame] | 1242 | #ifdef DEBUG |
| 1243 | fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current)); |
| 1244 | #endif |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1245 | } |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1246 | return 0; |
Stefano Babic | 33f0086 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 1247 | |
| 1248 | open_cleanup: |
| 1249 | if (addr0) |
| 1250 | free(addr0); |
| 1251 | |
| 1252 | if (addr1) |
| 1253 | free(addr0); |
| 1254 | |
| 1255 | return ret; |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * Simply free allocated buffer with environment |
| 1260 | */ |
| 1261 | int fw_env_close(struct env_opts *opts) |
| 1262 | { |
| 1263 | if (environment.image) |
| 1264 | free(environment.image); |
| 1265 | |
| 1266 | environment.image = NULL; |
| 1267 | |
| 1268 | return 0; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1271 | static int check_device_config(int dev) |
| 1272 | { |
| 1273 | struct stat st; |
| 1274 | int fd, rc = 0; |
| 1275 | |
| 1276 | fd = open(DEVNAME(dev), O_RDONLY); |
| 1277 | if (fd < 0) { |
| 1278 | fprintf(stderr, |
| 1279 | "Cannot open %s: %s\n", |
| 1280 | DEVNAME(dev), strerror(errno)); |
| 1281 | return -1; |
| 1282 | } |
| 1283 | |
| 1284 | rc = fstat(fd, &st); |
| 1285 | if (rc < 0) { |
| 1286 | fprintf(stderr, "Cannot stat the file %s\n", |
| 1287 | DEVNAME(dev)); |
| 1288 | goto err; |
| 1289 | } |
| 1290 | |
| 1291 | if (S_ISCHR(st.st_mode)) { |
| 1292 | struct mtd_info_user mtdinfo; |
| 1293 | rc = ioctl(fd, MEMGETINFO, &mtdinfo); |
| 1294 | if (rc < 0) { |
| 1295 | fprintf(stderr, "Cannot get MTD information for %s\n", |
| 1296 | DEVNAME(dev)); |
| 1297 | goto err; |
| 1298 | } |
| 1299 | if (mtdinfo.type != MTD_NORFLASH && |
| 1300 | mtdinfo.type != MTD_NANDFLASH && |
| 1301 | mtdinfo.type != MTD_DATAFLASH && |
| 1302 | mtdinfo.type != MTD_UBIVOLUME) { |
| 1303 | fprintf(stderr, "Unsupported flash type %u on %s\n", |
| 1304 | mtdinfo.type, DEVNAME(dev)); |
| 1305 | goto err; |
| 1306 | } |
| 1307 | DEVTYPE(dev) = mtdinfo.type; |
Max Krummenacher | 333ee16 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1308 | if (DEVESIZE(dev) == 0) |
| 1309 | /* Assume the erase size is the same as the env-size */ |
| 1310 | DEVESIZE(dev) = ENVSIZE(dev); |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1311 | } else { |
Stefan Agner | f4742ca | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1312 | uint64_t size; |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1313 | DEVTYPE(dev) = MTD_ABSENT; |
Max Krummenacher | 333ee16 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1314 | if (DEVESIZE(dev) == 0) |
| 1315 | /* Assume the erase size to be 512 bytes */ |
| 1316 | DEVESIZE(dev) = 0x200; |
Stefan Agner | f4742ca | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1317 | |
| 1318 | /* |
| 1319 | * Check for negative offsets, treat it as backwards offset |
| 1320 | * from the end of the block device |
| 1321 | */ |
| 1322 | if (DEVOFFSET(dev) < 0) { |
| 1323 | rc = ioctl(fd, BLKGETSIZE64, &size); |
| 1324 | if (rc < 0) { |
| 1325 | fprintf(stderr, "Could not get block device size on %s\n", |
| 1326 | DEVNAME(dev)); |
| 1327 | goto err; |
| 1328 | } |
| 1329 | |
| 1330 | DEVOFFSET(dev) = DEVOFFSET(dev) + size; |
| 1331 | #ifdef DEBUG |
| 1332 | fprintf(stderr, "Calculated device offset 0x%llx on %s\n", |
| 1333 | DEVOFFSET(dev), DEVNAME(dev)); |
| 1334 | #endif |
| 1335 | } |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
Max Krummenacher | 333ee16 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1338 | if (ENVSECTORS(dev) == 0) |
| 1339 | /* Assume enough sectors to cover the environment */ |
| 1340 | ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev)); |
| 1341 | |
| 1342 | if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) { |
| 1343 | fprintf(stderr, "Environment does not start on (erase) block boundary\n"); |
| 1344 | errno = EINVAL; |
| 1345 | return -1; |
| 1346 | } |
| 1347 | |
| 1348 | if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) { |
| 1349 | fprintf(stderr, "Environment does not fit into available sectors\n"); |
| 1350 | errno = EINVAL; |
| 1351 | return -1; |
| 1352 | } |
| 1353 | |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1354 | err: |
| 1355 | close(fd); |
| 1356 | return rc; |
| 1357 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1358 | |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1359 | static int parse_config(struct env_opts *opts) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1360 | { |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1361 | int rc; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1362 | |
Andreas Fenkart | 14070e6 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 1363 | if (!opts) |
| 1364 | opts = &default_opts; |
Anatolij Gustschin | 925c97c | 2016-04-29 22:00:11 +0200 | [diff] [blame] | 1365 | |
Andreas Fenkart | 14070e6 | 2016-05-31 09:21:56 +0200 | [diff] [blame] | 1366 | #if defined(CONFIG_FILE) |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1367 | /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1368 | if (get_config(opts->config_file)) { |
Andreas Fenkart | 371ee13 | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 1369 | fprintf(stderr, "Cannot parse config file '%s': %m\n", |
Andreas Fenkart | 81974f4 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 1370 | opts->config_file); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1371 | return -1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1372 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1373 | #else |
Tom Rini | 229695f | 2014-03-28 12:03:33 -0400 | [diff] [blame] | 1374 | DEVNAME (0) = DEVICE1_NAME; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1375 | DEVOFFSET (0) = DEVICE1_OFFSET; |
| 1376 | ENVSIZE (0) = ENV1_SIZE; |
Max Krummenacher | 333ee16 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1377 | |
| 1378 | /* Set defaults for DEVESIZE, ENVSECTORS later once we |
| 1379 | * know DEVTYPE |
| 1380 | */ |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1381 | #ifdef DEVICE1_ESIZE |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1382 | DEVESIZE (0) = DEVICE1_ESIZE; |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1383 | #endif |
| 1384 | #ifdef DEVICE1_ENVSECTORS |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1385 | ENVSECTORS (0) = DEVICE1_ENVSECTORS; |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1386 | #endif |
| 1387 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1388 | #ifdef HAVE_REDUND |
Tom Rini | 229695f | 2014-03-28 12:03:33 -0400 | [diff] [blame] | 1389 | DEVNAME (1) = DEVICE2_NAME; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1390 | DEVOFFSET (1) = DEVICE2_OFFSET; |
| 1391 | ENVSIZE (1) = ENV2_SIZE; |
Max Krummenacher | 333ee16 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1392 | |
| 1393 | /* Set defaults for DEVESIZE, ENVSECTORS later once we |
| 1394 | * know DEVTYPE |
| 1395 | */ |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1396 | #ifdef DEVICE2_ESIZE |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1397 | DEVESIZE (1) = DEVICE2_ESIZE; |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1398 | #endif |
| 1399 | #ifdef DEVICE2_ENVSECTORS |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1400 | ENVSECTORS (1) = DEVICE2_ENVSECTORS; |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1401 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1402 | HaveRedundEnv = 1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1403 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1404 | #endif |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1405 | rc = check_device_config(0); |
| 1406 | if (rc < 0) |
| 1407 | return rc; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1408 | |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1409 | if (HaveRedundEnv) { |
| 1410 | rc = check_device_config(1); |
| 1411 | if (rc < 0) |
| 1412 | return rc; |
Andreas Fenkart | f71cee4 | 2016-04-19 22:43:42 +0200 | [diff] [blame] | 1413 | |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1414 | if (ENVSIZE(0) != ENVSIZE(1)) { |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1415 | fprintf(stderr, |
Andreas Fenkart | 490365c | 2016-08-17 23:41:53 +0200 | [diff] [blame] | 1416 | "Redundant environments have unequal size"); |
| 1417 | return -1; |
Stefan Agner | 14fb5b2 | 2016-07-13 17:14:37 -0700 | [diff] [blame] | 1418 | } |
Andreas Fenkart | f71cee4 | 2016-04-19 22:43:42 +0200 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | usable_envsize = CUR_ENVSIZE - sizeof(uint32_t); |
| 1422 | if (HaveRedundEnv) |
| 1423 | usable_envsize -= sizeof(char); |
| 1424 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1425 | return 0; |
| 1426 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1427 | |
| 1428 | #if defined(CONFIG_FILE) |
| 1429 | static int get_config (char *fname) |
| 1430 | { |
| 1431 | FILE *fp; |
| 1432 | int i = 0; |
| 1433 | int rc; |
| 1434 | char dump[128]; |
Tom Rini | 229695f | 2014-03-28 12:03:33 -0400 | [diff] [blame] | 1435 | char *devname; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1436 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1437 | fp = fopen (fname, "r"); |
| 1438 | if (fp == NULL) |
| 1439 | return -1; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1440 | |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1441 | while (i < 2 && fgets (dump, sizeof (dump), fp)) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1442 | /* Skip incomplete conversions and comment strings */ |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1443 | if (dump[0] == '#') |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1444 | continue; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1445 | |
Stefan Agner | f4742ca | 2016-07-13 17:14:38 -0700 | [diff] [blame] | 1446 | rc = sscanf(dump, "%ms %lli %lx %lx %lx", |
| 1447 | &devname, |
| 1448 | &DEVOFFSET(i), |
| 1449 | &ENVSIZE(i), |
| 1450 | &DEVESIZE(i), |
| 1451 | &ENVSECTORS(i)); |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1452 | |
Remy Bohmer | 9eeaa8e | 2011-02-12 19:06:26 +0100 | [diff] [blame] | 1453 | if (rc < 3) |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1454 | continue; |
| 1455 | |
Tom Rini | 229695f | 2014-03-28 12:03:33 -0400 | [diff] [blame] | 1456 | DEVNAME(i) = devname; |
| 1457 | |
Max Krummenacher | 333ee16 | 2016-11-19 13:58:56 +0100 | [diff] [blame] | 1458 | /* Set defaults for DEVESIZE, ENVSECTORS later once we |
| 1459 | * know DEVTYPE |
| 1460 | */ |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1461 | |
| 1462 | i++; |
| 1463 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1464 | fclose (fp); |
| 1465 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1466 | HaveRedundEnv = i - 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 1467 | if (!i) { /* No valid entries found */ |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1468 | errno = EINVAL; |
Guennadi Liakhovetski | 5608692 | 2008-09-04 13:01:49 +0200 | [diff] [blame] | 1469 | return -1; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 1470 | } else |
| 1471 | return 0; |
| 1472 | } |
| 1473 | #endif |