]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
c9d728dd SG |
2 | /* |
3 | * Copyright (C) 2017 Google, Inc | |
4 | * Written by Simon Glass <[email protected]> | |
c9d728dd SG |
5 | */ |
6 | ||
7 | #include <common.h> | |
4bfd1f5d | 8 | #include <env.h> |
f3998fdc | 9 | #include <env_internal.h> |
f7ae49fc | 10 | #include <log.h> |
cd93d625 | 11 | #include <linux/bitops.h> |
eb41d8a1 | 12 | #include <linux/bug.h> |
c9d728dd SG |
13 | |
14 | DECLARE_GLOBAL_DATA_PTR; | |
15 | ||
7bcdf195 SDPP |
16 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
17 | void env_fix_drivers(void) | |
18 | { | |
19 | struct env_driver *drv; | |
20 | const int n_ents = ll_entry_count(struct env_driver, env_driver); | |
21 | struct env_driver *entry; | |
22 | ||
23 | drv = ll_entry_start(struct env_driver, env_driver); | |
24 | for (entry = drv; entry != drv + n_ents; entry++) { | |
25 | if (entry->name) | |
26 | entry->name += gd->reloc_off; | |
27 | if (entry->load) | |
28 | entry->load += gd->reloc_off; | |
29 | if (entry->save) | |
30 | entry->save += gd->reloc_off; | |
cd121bdb FW |
31 | if (entry->erase) |
32 | entry->erase += gd->reloc_off; | |
7bcdf195 SDPP |
33 | if (entry->init) |
34 | entry->init += gd->reloc_off; | |
35 | } | |
36 | } | |
37 | #endif | |
38 | ||
52746c43 | 39 | static struct env_driver *_env_driver_lookup(enum env_location loc) |
c9d728dd SG |
40 | { |
41 | struct env_driver *drv; | |
42 | const int n_ents = ll_entry_count(struct env_driver, env_driver); | |
43 | struct env_driver *entry; | |
44 | ||
45 | drv = ll_entry_start(struct env_driver, env_driver); | |
46 | for (entry = drv; entry != drv + n_ents; entry++) { | |
47 | if (loc == entry->location) | |
48 | return entry; | |
49 | } | |
50 | ||
51 | /* Not found */ | |
52 | return NULL; | |
53 | } | |
54 | ||
7d714a24 MR |
55 | static enum env_location env_locations[] = { |
56 | #ifdef CONFIG_ENV_IS_IN_EEPROM | |
57 | ENVL_EEPROM, | |
58 | #endif | |
59 | #ifdef CONFIG_ENV_IS_IN_EXT4 | |
60 | ENVL_EXT4, | |
61 | #endif | |
62 | #ifdef CONFIG_ENV_IS_IN_FAT | |
63 | ENVL_FAT, | |
64 | #endif | |
65 | #ifdef CONFIG_ENV_IS_IN_FLASH | |
66 | ENVL_FLASH, | |
67 | #endif | |
68 | #ifdef CONFIG_ENV_IS_IN_MMC | |
69 | ENVL_MMC, | |
70 | #endif | |
71 | #ifdef CONFIG_ENV_IS_IN_NAND | |
72 | ENVL_NAND, | |
73 | #endif | |
74 | #ifdef CONFIG_ENV_IS_IN_NVRAM | |
75 | ENVL_NVRAM, | |
76 | #endif | |
77 | #ifdef CONFIG_ENV_IS_IN_REMOTE | |
78 | ENVL_REMOTE, | |
79 | #endif | |
9a6a311d YL |
80 | #ifdef CONFIG_ENV_IS_IN_SATA |
81 | ENVL_ESATA, | |
82 | #endif | |
7d714a24 MR |
83 | #ifdef CONFIG_ENV_IS_IN_SPI_FLASH |
84 | ENVL_SPI_FLASH, | |
85 | #endif | |
86 | #ifdef CONFIG_ENV_IS_IN_UBI | |
87 | ENVL_UBI, | |
88 | #endif | |
89 | #ifdef CONFIG_ENV_IS_NOWHERE | |
90 | ENVL_NOWHERE, | |
91 | #endif | |
92 | }; | |
93 | ||
1d446087 MR |
94 | static bool env_has_inited(enum env_location location) |
95 | { | |
96 | return gd->env_has_init & BIT(location); | |
97 | } | |
98 | ||
99 | static void env_set_inited(enum env_location location) | |
100 | { | |
101 | /* | |
102 | * We're using a 32-bits bitmask stored in gd (env_has_init) | |
103 | * using the above enum value as the bit index. We need to | |
104 | * make sure that we're not overflowing it. | |
105 | */ | |
d5a6a5a9 | 106 | BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG); |
1d446087 MR |
107 | |
108 | gd->env_has_init |= BIT(location); | |
109 | } | |
110 | ||
8a3a7e22 MR |
111 | /** |
112 | * env_get_location() - Returns the best env location for a board | |
113 | * @op: operations performed on the environment | |
114 | * @prio: priority between the multiple environments, 0 being the | |
115 | * highest priority | |
116 | * | |
117 | * This will return the preferred environment for the given priority. | |
40c08a68 | 118 | * This is overridable by boards if they need to. |
8a3a7e22 MR |
119 | * |
120 | * All implementations are free to use the operation, the priority and | |
121 | * any other data relevant to their choice, but must take into account | |
122 | * the fact that the lowest prority (0) is the most important location | |
123 | * in the system. The following locations should be returned by order | |
124 | * of descending priorities, from the highest to the lowest priority. | |
125 | * | |
126 | * Returns: | |
127 | * an enum env_location value on success, a negative error code otherwise | |
128 | */ | |
40c08a68 | 129 | __weak enum env_location env_get_location(enum env_operation op, int prio) |
c9d728dd | 130 | { |
d30ba231 NF |
131 | if (prio >= ARRAY_SIZE(env_locations)) |
132 | return ENVL_UNKNOWN; | |
133 | ||
d30ba231 | 134 | return env_locations[prio]; |
c9d728dd SG |
135 | } |
136 | ||
8a3a7e22 MR |
137 | |
138 | /** | |
139 | * env_driver_lookup() - Finds the most suited environment location | |
140 | * @op: operations performed on the environment | |
141 | * @prio: priority between the multiple environments, 0 being the | |
142 | * highest priority | |
143 | * | |
144 | * This will try to find the available environment with the highest | |
145 | * priority in the system. | |
146 | * | |
147 | * Returns: | |
148 | * NULL on error, a pointer to a struct env_driver otherwise | |
149 | */ | |
150 | static struct env_driver *env_driver_lookup(enum env_operation op, int prio) | |
c9d728dd | 151 | { |
8a3a7e22 | 152 | enum env_location loc = env_get_location(op, prio); |
c9d728dd SG |
153 | struct env_driver *drv; |
154 | ||
8a3a7e22 MR |
155 | if (loc == ENVL_UNKNOWN) |
156 | return NULL; | |
157 | ||
52746c43 | 158 | drv = _env_driver_lookup(loc); |
c9d728dd SG |
159 | if (!drv) { |
160 | debug("%s: No environment driver for location %d\n", __func__, | |
161 | loc); | |
162 | return NULL; | |
163 | } | |
164 | ||
165 | return drv; | |
166 | } | |
167 | ||
b2cdef48 | 168 | __weak int env_get_char_spec(int index) |
c9d728dd | 169 | { |
b2cdef48 GS |
170 | return *(uchar *)(gd->env_addr + index); |
171 | } | |
c9d728dd | 172 | |
b2cdef48 GS |
173 | int env_get_char(int index) |
174 | { | |
2d7cb5b4 | 175 | if (gd->env_valid == ENV_INVALID) |
a69d0f60 | 176 | return default_environment[index]; |
b2cdef48 GS |
177 | else |
178 | return env_get_char_spec(index); | |
c9d728dd SG |
179 | } |
180 | ||
181 | int env_load(void) | |
182 | { | |
8a3a7e22 | 183 | struct env_driver *drv; |
293a172b | 184 | int best_prio = -1; |
8a3a7e22 | 185 | int prio; |
c9d728dd | 186 | |
8a3a7e22 MR |
187 | for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) { |
188 | int ret; | |
189 | ||
1d446087 MR |
190 | if (!env_has_inited(drv->location)) |
191 | continue; | |
192 | ||
3574ba01 | 193 | printf("Loading Environment from %s... ", drv->name); |
13bbfb4a SP |
194 | /* |
195 | * In error case, the error message must be printed during | |
196 | * drv->load() in some underlying API, and it must be exactly | |
197 | * one message. | |
198 | */ | |
8a3a7e22 | 199 | ret = drv->load(); |
293a172b | 200 | if (!ret) { |
3574ba01 | 201 | printf("OK\n"); |
6d8d8400 PD |
202 | gd->env_load_prio = prio; |
203 | ||
47f3b1f2 | 204 | #if !CONFIG_IS_ENABLED(ENV_APPEND) |
8a3a7e22 | 205 | return 0; |
47f3b1f2 | 206 | #endif |
293a172b SP |
207 | } else if (ret == -ENOMSG) { |
208 | /* Handle "bad CRC" case */ | |
209 | if (best_prio == -1) | |
210 | best_prio = prio; | |
211 | } else { | |
212 | debug("Failed (%d)\n", ret); | |
13bbfb4a | 213 | } |
c9d728dd SG |
214 | } |
215 | ||
d30ba231 NF |
216 | /* |
217 | * In case of invalid environment, we set the 'default' env location | |
293a172b SP |
218 | * to the best choice, i.e.: |
219 | * 1. Environment location with bad CRC, if such location was found | |
220 | * 2. Otherwise use the location with highest priority | |
221 | * | |
222 | * This way, next calls to env_save() will restore the environment | |
223 | * at the right place. | |
d30ba231 | 224 | */ |
293a172b SP |
225 | if (best_prio >= 0) |
226 | debug("Selecting environment with bad CRC\n"); | |
227 | else | |
228 | best_prio = 0; | |
6d8d8400 PD |
229 | |
230 | gd->env_load_prio = best_prio; | |
d30ba231 | 231 | |
8a3a7e22 | 232 | return -ENODEV; |
c9d728dd SG |
233 | } |
234 | ||
0115dd3a PD |
235 | int env_reload(void) |
236 | { | |
237 | struct env_driver *drv; | |
238 | ||
239 | drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio); | |
240 | if (drv) { | |
241 | int ret; | |
242 | ||
243 | printf("Loading Environment from %s... ", drv->name); | |
244 | ||
245 | if (!env_has_inited(drv->location)) { | |
246 | printf("not initialized\n"); | |
247 | return -ENODEV; | |
248 | } | |
249 | ||
250 | ret = drv->load(); | |
251 | if (ret) | |
252 | printf("Failed (%d)\n", ret); | |
253 | else | |
254 | printf("OK\n"); | |
255 | ||
256 | if (!ret) | |
257 | return 0; | |
258 | } | |
259 | ||
260 | return -ENODEV; | |
261 | } | |
262 | ||
c9d728dd SG |
263 | int env_save(void) |
264 | { | |
8a3a7e22 | 265 | struct env_driver *drv; |
c9d728dd | 266 | |
d30ba231 NF |
267 | drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio); |
268 | if (drv) { | |
8a3a7e22 MR |
269 | int ret; |
270 | ||
8968288c PD |
271 | printf("Saving Environment to %s... ", drv->name); |
272 | if (!drv->save) { | |
273 | printf("not possible\n"); | |
d30ba231 | 274 | return -ENODEV; |
8968288c | 275 | } |
8a3a7e22 | 276 | |
8968288c PD |
277 | if (!env_has_inited(drv->location)) { |
278 | printf("not initialized\n"); | |
d30ba231 | 279 | return -ENODEV; |
8968288c | 280 | } |
1d446087 | 281 | |
8a3a7e22 | 282 | ret = drv->save(); |
3574ba01 MR |
283 | if (ret) |
284 | printf("Failed (%d)\n", ret); | |
285 | else | |
286 | printf("OK\n"); | |
287 | ||
8a3a7e22 MR |
288 | if (!ret) |
289 | return 0; | |
c9d728dd SG |
290 | } |
291 | ||
8a3a7e22 | 292 | return -ENODEV; |
c9d728dd SG |
293 | } |
294 | ||
cd121bdb FW |
295 | int env_erase(void) |
296 | { | |
297 | struct env_driver *drv; | |
298 | ||
299 | drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio); | |
300 | if (drv) { | |
301 | int ret; | |
302 | ||
303 | if (!drv->erase) | |
304 | return -ENODEV; | |
305 | ||
306 | if (!env_has_inited(drv->location)) | |
307 | return -ENODEV; | |
308 | ||
309 | printf("Erasing Environment on %s... ", drv->name); | |
310 | ret = drv->erase(); | |
311 | if (ret) | |
312 | printf("Failed (%d)\n", ret); | |
313 | else | |
314 | printf("OK\n"); | |
315 | ||
316 | if (!ret) | |
317 | return 0; | |
318 | } | |
319 | ||
320 | return -ENODEV; | |
321 | } | |
322 | ||
6eeae424 | 323 | int env_init(void) |
c9d728dd | 324 | { |
8a3a7e22 | 325 | struct env_driver *drv; |
7938822a | 326 | int ret = -ENOENT; |
8a3a7e22 MR |
327 | int prio; |
328 | ||
329 | for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) { | |
1d446087 MR |
330 | if (!drv->init || !(ret = drv->init())) |
331 | env_set_inited(drv->location); | |
46ce9e77 HS |
332 | if (ret == -ENOENT) |
333 | env_set_inited(drv->location); | |
8a3a7e22 | 334 | |
1d446087 | 335 | debug("%s: Environment %s init done (ret=%d)\n", __func__, |
8a3a7e22 MR |
336 | drv->name, ret); |
337 | } | |
338 | ||
339 | if (!prio) | |
340 | return -ENODEV; | |
341 | ||
7938822a SG |
342 | if (ret == -ENOENT) { |
343 | gd->env_addr = (ulong)&default_environment[0]; | |
eeba55cb | 344 | gd->env_valid = ENV_VALID; |
7938822a SG |
345 | |
346 | return 0; | |
c9d728dd SG |
347 | } |
348 | ||
8a3a7e22 | 349 | return ret; |
c9d728dd | 350 | } |
a97d22eb PD |
351 | |
352 | int env_select(const char *name) | |
353 | { | |
354 | struct env_driver *drv; | |
355 | const int n_ents = ll_entry_count(struct env_driver, env_driver); | |
356 | struct env_driver *entry; | |
357 | int prio; | |
358 | bool found = false; | |
359 | ||
360 | printf("Select Environment on %s: ", name); | |
361 | ||
362 | /* search ENV driver by name */ | |
363 | drv = ll_entry_start(struct env_driver, env_driver); | |
364 | for (entry = drv; entry != drv + n_ents; entry++) { | |
365 | if (!strcmp(entry->name, name)) { | |
366 | found = true; | |
367 | break; | |
368 | } | |
369 | } | |
370 | ||
371 | if (!found) { | |
372 | printf("driver not found\n"); | |
373 | return -ENODEV; | |
374 | } | |
375 | ||
376 | /* search priority by driver */ | |
377 | for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) { | |
378 | if (entry->location == env_get_location(ENVOP_LOAD, prio)) { | |
379 | /* when priority change, reset the ENV flags */ | |
380 | if (gd->env_load_prio != prio) { | |
381 | gd->env_load_prio = prio; | |
382 | gd->env_valid = ENV_INVALID; | |
383 | gd->flags &= ~GD_FLG_ENV_DEFAULT; | |
384 | } | |
385 | printf("OK\n"); | |
386 | return 0; | |
387 | } | |
388 | } | |
389 | printf("priority not found\n"); | |
390 | ||
391 | return -ENODEV; | |
392 | } |