]>
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 <sjg@chromium.org> | |
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 | */ | |
106 | BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG); | |
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 | ||
134 | gd->env_load_prio = prio; | |
8a3a7e22 | 135 | |
d30ba231 | 136 | return env_locations[prio]; |
c9d728dd SG |
137 | } |
138 | ||
8a3a7e22 MR |
139 | |
140 | /** | |
141 | * env_driver_lookup() - Finds the most suited environment location | |
142 | * @op: operations performed on the environment | |
143 | * @prio: priority between the multiple environments, 0 being the | |
144 | * highest priority | |
145 | * | |
146 | * This will try to find the available environment with the highest | |
147 | * priority in the system. | |
148 | * | |
149 | * Returns: | |
150 | * NULL on error, a pointer to a struct env_driver otherwise | |
151 | */ | |
152 | static struct env_driver *env_driver_lookup(enum env_operation op, int prio) | |
c9d728dd | 153 | { |
8a3a7e22 | 154 | enum env_location loc = env_get_location(op, prio); |
c9d728dd SG |
155 | struct env_driver *drv; |
156 | ||
8a3a7e22 MR |
157 | if (loc == ENVL_UNKNOWN) |
158 | return NULL; | |
159 | ||
52746c43 | 160 | drv = _env_driver_lookup(loc); |
c9d728dd SG |
161 | if (!drv) { |
162 | debug("%s: No environment driver for location %d\n", __func__, | |
163 | loc); | |
164 | return NULL; | |
165 | } | |
166 | ||
167 | return drv; | |
168 | } | |
169 | ||
b2cdef48 | 170 | __weak int env_get_char_spec(int index) |
c9d728dd | 171 | { |
b2cdef48 GS |
172 | return *(uchar *)(gd->env_addr + index); |
173 | } | |
c9d728dd | 174 | |
b2cdef48 GS |
175 | int env_get_char(int index) |
176 | { | |
2d7cb5b4 | 177 | if (gd->env_valid == ENV_INVALID) |
a69d0f60 | 178 | return default_environment[index]; |
b2cdef48 GS |
179 | else |
180 | return env_get_char_spec(index); | |
c9d728dd SG |
181 | } |
182 | ||
183 | int env_load(void) | |
184 | { | |
8a3a7e22 | 185 | struct env_driver *drv; |
293a172b | 186 | int best_prio = -1; |
8a3a7e22 | 187 | int prio; |
c9d728dd | 188 | |
8a3a7e22 MR |
189 | for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) { |
190 | int ret; | |
191 | ||
192 | if (!drv->load) | |
193 | continue; | |
194 | ||
1d446087 MR |
195 | if (!env_has_inited(drv->location)) |
196 | continue; | |
197 | ||
3574ba01 | 198 | printf("Loading Environment from %s... ", drv->name); |
13bbfb4a SP |
199 | /* |
200 | * In error case, the error message must be printed during | |
201 | * drv->load() in some underlying API, and it must be exactly | |
202 | * one message. | |
203 | */ | |
8a3a7e22 | 204 | ret = drv->load(); |
293a172b | 205 | if (!ret) { |
3574ba01 | 206 | printf("OK\n"); |
8a3a7e22 | 207 | return 0; |
293a172b SP |
208 | } else if (ret == -ENOMSG) { |
209 | /* Handle "bad CRC" case */ | |
210 | if (best_prio == -1) | |
211 | best_prio = prio; | |
212 | } else { | |
213 | debug("Failed (%d)\n", ret); | |
13bbfb4a | 214 | } |
c9d728dd SG |
215 | } |
216 | ||
d30ba231 NF |
217 | /* |
218 | * In case of invalid environment, we set the 'default' env location | |
293a172b SP |
219 | * to the best choice, i.e.: |
220 | * 1. Environment location with bad CRC, if such location was found | |
221 | * 2. Otherwise use the location with highest priority | |
222 | * | |
223 | * This way, next calls to env_save() will restore the environment | |
224 | * at the right place. | |
d30ba231 | 225 | */ |
293a172b SP |
226 | if (best_prio >= 0) |
227 | debug("Selecting environment with bad CRC\n"); | |
228 | else | |
229 | best_prio = 0; | |
230 | env_get_location(ENVOP_LOAD, best_prio); | |
d30ba231 | 231 | |
8a3a7e22 | 232 | return -ENODEV; |
c9d728dd SG |
233 | } |
234 | ||
235 | int env_save(void) | |
236 | { | |
8a3a7e22 | 237 | struct env_driver *drv; |
c9d728dd | 238 | |
d30ba231 NF |
239 | drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio); |
240 | if (drv) { | |
8a3a7e22 MR |
241 | int ret; |
242 | ||
243 | if (!drv->save) | |
d30ba231 | 244 | return -ENODEV; |
8a3a7e22 | 245 | |
1d446087 | 246 | if (!env_has_inited(drv->location)) |
d30ba231 | 247 | return -ENODEV; |
1d446087 | 248 | |
9efac3c8 | 249 | printf("Saving Environment to %s... ", drv->name); |
8a3a7e22 | 250 | ret = drv->save(); |
3574ba01 MR |
251 | if (ret) |
252 | printf("Failed (%d)\n", ret); | |
253 | else | |
254 | printf("OK\n"); | |
255 | ||
8a3a7e22 MR |
256 | if (!ret) |
257 | return 0; | |
c9d728dd SG |
258 | } |
259 | ||
8a3a7e22 | 260 | return -ENODEV; |
c9d728dd SG |
261 | } |
262 | ||
cd121bdb FW |
263 | int env_erase(void) |
264 | { | |
265 | struct env_driver *drv; | |
266 | ||
267 | drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio); | |
268 | if (drv) { | |
269 | int ret; | |
270 | ||
271 | if (!drv->erase) | |
272 | return -ENODEV; | |
273 | ||
274 | if (!env_has_inited(drv->location)) | |
275 | return -ENODEV; | |
276 | ||
277 | printf("Erasing Environment on %s... ", drv->name); | |
278 | ret = drv->erase(); | |
279 | if (ret) | |
280 | printf("Failed (%d)\n", ret); | |
281 | else | |
282 | printf("OK\n"); | |
283 | ||
284 | if (!ret) | |
285 | return 0; | |
286 | } | |
287 | ||
288 | return -ENODEV; | |
289 | } | |
290 | ||
6eeae424 | 291 | int env_init(void) |
c9d728dd | 292 | { |
8a3a7e22 | 293 | struct env_driver *drv; |
7938822a | 294 | int ret = -ENOENT; |
8a3a7e22 MR |
295 | int prio; |
296 | ||
297 | for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) { | |
1d446087 MR |
298 | if (!drv->init || !(ret = drv->init())) |
299 | env_set_inited(drv->location); | |
8a3a7e22 | 300 | |
1d446087 | 301 | debug("%s: Environment %s init done (ret=%d)\n", __func__, |
8a3a7e22 MR |
302 | drv->name, ret); |
303 | } | |
304 | ||
305 | if (!prio) | |
306 | return -ENODEV; | |
307 | ||
7938822a SG |
308 | if (ret == -ENOENT) { |
309 | gd->env_addr = (ulong)&default_environment[0]; | |
eeba55cb | 310 | gd->env_valid = ENV_VALID; |
7938822a SG |
311 | |
312 | return 0; | |
c9d728dd SG |
313 | } |
314 | ||
8a3a7e22 | 315 | return ret; |
c9d728dd | 316 | } |