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