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