]>
Commit | Line | Data |
---|---|---|
2b74433f JH |
1 | /* |
2 | * (c) Copyright 2012 by National Instruments, | |
3 | * Joe Hershberger <joe.hershberger@ni.com> | |
4 | * | |
3765b3e7 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
2b74433f JH |
6 | */ |
7 | ||
8 | #include <common.h> | |
9 | ||
10 | #include <command.h> | |
11 | #include <environment.h> | |
12 | #include <errno.h> | |
13 | #include <malloc.h> | |
cf92e05c | 14 | #include <memalign.h> |
2b74433f JH |
15 | #include <search.h> |
16 | #include <ubi_uboot.h> | |
17 | #undef crc32 | |
18 | ||
19 | char *env_name_spec = "UBI"; | |
20 | ||
21 | env_t *env_ptr; | |
22 | ||
23 | DECLARE_GLOBAL_DATA_PTR; | |
24 | ||
25 | int env_init(void) | |
26 | { | |
27 | /* use default */ | |
28 | gd->env_addr = (ulong)&default_environment[0]; | |
29 | gd->env_valid = 1; | |
30 | ||
31 | return 0; | |
32 | } | |
33 | ||
34 | #ifdef CONFIG_CMD_SAVEENV | |
785881f7 JH |
35 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
36 | static unsigned char env_flags; | |
37 | ||
38 | int saveenv(void) | |
39 | { | |
40 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); | |
7ce1526e | 41 | int ret; |
785881f7 | 42 | |
7ce1526e MV |
43 | ret = env_export(env_new); |
44 | if (ret) | |
45 | return ret; | |
785881f7 JH |
46 | |
47 | if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { | |
48 | printf("\n** Cannot find mtd partition \"%s\"\n", | |
49 | CONFIG_ENV_UBI_PART); | |
50 | return 1; | |
51 | } | |
52 | ||
785881f7 JH |
53 | env_new->flags = ++env_flags; /* increase the serial */ |
54 | ||
55 | if (gd->env_valid == 1) { | |
56 | puts("Writing to redundant UBI... "); | |
57 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND, | |
58 | (void *)env_new, CONFIG_ENV_SIZE)) { | |
59 | printf("\n** Unable to write env to %s:%s **\n", | |
60 | CONFIG_ENV_UBI_PART, | |
61 | CONFIG_ENV_UBI_VOLUME_REDUND); | |
62 | return 1; | |
63 | } | |
64 | } else { | |
65 | puts("Writing to UBI... "); | |
66 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, | |
67 | (void *)env_new, CONFIG_ENV_SIZE)) { | |
68 | printf("\n** Unable to write env to %s:%s **\n", | |
69 | CONFIG_ENV_UBI_PART, | |
70 | CONFIG_ENV_UBI_VOLUME); | |
71 | return 1; | |
72 | } | |
73 | } | |
74 | ||
75 | puts("done\n"); | |
76 | ||
77 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; | |
78 | ||
79 | return 0; | |
80 | } | |
81 | #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | |
2b74433f JH |
82 | int saveenv(void) |
83 | { | |
84 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); | |
7ce1526e | 85 | int ret; |
2b74433f | 86 | |
7ce1526e MV |
87 | ret = env_export(env_new); |
88 | if (ret) | |
89 | return ret; | |
2b74433f JH |
90 | |
91 | if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { | |
92 | printf("\n** Cannot find mtd partition \"%s\"\n", | |
93 | CONFIG_ENV_UBI_PART); | |
94 | return 1; | |
95 | } | |
96 | ||
2b74433f JH |
97 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new, |
98 | CONFIG_ENV_SIZE)) { | |
99 | printf("\n** Unable to write env to %s:%s **\n", | |
100 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); | |
101 | return 1; | |
102 | } | |
103 | ||
104 | puts("done\n"); | |
105 | return 0; | |
106 | } | |
785881f7 | 107 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
2b74433f JH |
108 | #endif /* CONFIG_CMD_SAVEENV */ |
109 | ||
785881f7 JH |
110 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
111 | void env_relocate_spec(void) | |
112 | { | |
113 | ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE); | |
114 | ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE); | |
115 | int crc1_ok = 0, crc2_ok = 0; | |
116 | env_t *ep, *tmp_env1, *tmp_env2; | |
117 | ||
c1f51e0f MN |
118 | /* |
119 | * In case we have restarted u-boot there is a chance that buffer | |
120 | * contains old environment (from the previous boot). | |
121 | * If UBI volume is zero size, ubi_volume_read() doesn't modify the | |
122 | * buffer. | |
123 | * We need to clear buffer manually here, so the invalid CRC will | |
124 | * cause setting default environment as expected. | |
125 | */ | |
126 | memset(env1_buf, 0x0, CONFIG_ENV_SIZE); | |
127 | memset(env2_buf, 0x0, CONFIG_ENV_SIZE); | |
128 | ||
785881f7 JH |
129 | tmp_env1 = (env_t *)env1_buf; |
130 | tmp_env2 = (env_t *)env2_buf; | |
131 | ||
132 | if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { | |
133 | printf("\n** Cannot find mtd partition \"%s\"\n", | |
134 | CONFIG_ENV_UBI_PART); | |
135 | set_default_env(NULL); | |
136 | return; | |
137 | } | |
138 | ||
139 | if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1, | |
140 | CONFIG_ENV_SIZE)) { | |
141 | printf("\n** Unable to read env from %s:%s **\n", | |
142 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); | |
143 | } | |
144 | ||
145 | if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2, | |
146 | CONFIG_ENV_SIZE)) { | |
147 | printf("\n** Unable to read redundant env from %s:%s **\n", | |
148 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND); | |
149 | } | |
150 | ||
151 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc; | |
152 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc; | |
153 | ||
154 | if (!crc1_ok && !crc2_ok) { | |
155 | set_default_env("!bad CRC"); | |
156 | return; | |
157 | } else if (crc1_ok && !crc2_ok) { | |
158 | gd->env_valid = 1; | |
159 | } else if (!crc1_ok && crc2_ok) { | |
160 | gd->env_valid = 2; | |
161 | } else { | |
162 | /* both ok - check serial */ | |
163 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) | |
164 | gd->env_valid = 2; | |
165 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) | |
166 | gd->env_valid = 1; | |
167 | else if (tmp_env1->flags > tmp_env2->flags) | |
168 | gd->env_valid = 1; | |
169 | else if (tmp_env2->flags > tmp_env1->flags) | |
170 | gd->env_valid = 2; | |
171 | else /* flags are equal - almost impossible */ | |
172 | gd->env_valid = 1; | |
173 | } | |
174 | ||
175 | if (gd->env_valid == 1) | |
176 | ep = tmp_env1; | |
177 | else | |
178 | ep = tmp_env2; | |
179 | ||
180 | env_flags = ep->flags; | |
181 | env_import((char *)ep, 0); | |
182 | } | |
183 | #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | |
2b74433f JH |
184 | void env_relocate_spec(void) |
185 | { | |
186 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); | |
187 | ||
c1f51e0f MN |
188 | /* |
189 | * In case we have restarted u-boot there is a chance that buffer | |
190 | * contains old environment (from the previous boot). | |
191 | * If UBI volume is zero size, ubi_volume_read() doesn't modify the | |
192 | * buffer. | |
193 | * We need to clear buffer manually here, so the invalid CRC will | |
194 | * cause setting default environment as expected. | |
195 | */ | |
196 | memset(buf, 0x0, CONFIG_ENV_SIZE); | |
197 | ||
2b74433f JH |
198 | if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { |
199 | printf("\n** Cannot find mtd partition \"%s\"\n", | |
200 | CONFIG_ENV_UBI_PART); | |
201 | set_default_env(NULL); | |
202 | return; | |
203 | } | |
204 | ||
a7c06cd3 | 205 | if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) { |
2b74433f JH |
206 | printf("\n** Unable to read env from %s:%s **\n", |
207 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); | |
208 | set_default_env(NULL); | |
209 | return; | |
210 | } | |
211 | ||
212 | env_import(buf, 1); | |
213 | } | |
785881f7 | 214 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |