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