]>
Commit | Line | Data |
---|---|---|
ea882baf WD |
1 | /* |
2 | * LowLevel function for DataFlash environment support | |
5779d8d9 WD |
3 | * Author : Gilles Gastaldi (Atmel) |
4 | * | |
1a459660 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
5779d8d9 WD |
6 | */ |
7 | #include <common.h> | |
5779d8d9 WD |
8 | #include <command.h> |
9 | #include <environment.h> | |
10 | #include <linux/stddef.h> | |
5779d8d9 | 11 | #include <dataflash.h> |
ea882baf WD |
12 | #include <search.h> |
13 | #include <errno.h> | |
5779d8d9 | 14 | |
d87080b7 WD |
15 | DECLARE_GLOBAL_DATA_PTR; |
16 | ||
0901d9f8 | 17 | env_t *env_ptr; |
5779d8d9 | 18 | |
0901d9f8 | 19 | char *env_name_spec = "dataflash"; |
5779d8d9 | 20 | |
ea882baf | 21 | uchar env_get_char_spec(int index) |
5779d8d9 WD |
22 | { |
23 | uchar c; | |
ea882baf | 24 | |
0901d9f8 | 25 | read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data), |
ea882baf | 26 | 1, (char *)&c); |
0901d9f8 | 27 | return c; |
5779d8d9 WD |
28 | } |
29 | ||
ea882baf | 30 | void env_relocate_spec(void) |
5779d8d9 | 31 | { |
cca2011e BS |
32 | ulong crc, new = 0; |
33 | unsigned off; | |
cd0f4fa1 | 34 | char buf[CONFIG_ENV_SIZE]; |
ea882baf | 35 | |
cca2011e BS |
36 | /* Read old CRC */ |
37 | read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), | |
38 | sizeof(ulong), (char *)&crc); | |
39 | ||
40 | /* Read whole environment */ | |
cd0f4fa1 TR |
41 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); |
42 | ||
cca2011e BS |
43 | /* Calculate the CRC */ |
44 | off = offsetof(env_t, data); | |
45 | new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE); | |
46 | ||
47 | if (crc == new) | |
48 | env_import(buf, 1); | |
49 | else | |
50 | set_default_env("!bad CRC"); | |
5779d8d9 WD |
51 | } |
52 | ||
ea882baf WD |
53 | #ifdef CONFIG_ENV_OFFSET_REDUND |
54 | #error No support for redundant environment on dataflash yet! | |
55 | #endif | |
56 | ||
5779d8d9 WD |
57 | int saveenv(void) |
58 | { | |
cd0f4fa1 | 59 | env_t env_new; |
ea882baf WD |
60 | ssize_t len; |
61 | char *res; | |
62 | ||
cd0f4fa1 | 63 | res = (char *)&env_new.data; |
be11235a | 64 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
ea882baf WD |
65 | if (len < 0) { |
66 | error("Cannot export environment: errno = %d\n", errno); | |
67 | return 1; | |
68 | } | |
cd0f4fa1 | 69 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
ea882baf WD |
70 | |
71 | return write_dataflash(CONFIG_ENV_ADDR, | |
cd0f4fa1 | 72 | (unsigned long)&env_new, |
ea882baf | 73 | CONFIG_ENV_SIZE); |
5779d8d9 WD |
74 | } |
75 | ||
ea882baf WD |
76 | /* |
77 | * Initialize environment use | |
5779d8d9 | 78 | * |
ea882baf | 79 | * We are still running from ROM, so data use is limited. |
5779d8d9 WD |
80 | * Use a (moderately small) buffer on the stack |
81 | */ | |
82 | int env_init(void) | |
83 | { | |
cca2011e BS |
84 | /* use default */ |
85 | gd->env_addr = (ulong)&default_environment[0]; | |
86 | gd->env_valid = 1; | |
5779d8d9 | 87 | |
ea882baf | 88 | return 0; |
5779d8d9 | 89 | } |