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