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