1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Google, Inc
9 #include <env_internal.h>
11 #include <asm/global_data.h>
12 #include <linux/bitops.h>
13 #include <linux/bug.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
18 void env_fix_drivers(void)
20 struct env_driver *drv;
21 const int n_ents = ll_entry_count(struct env_driver, env_driver);
22 struct env_driver *entry;
24 drv = ll_entry_start(struct env_driver, env_driver);
25 for (entry = drv; entry != drv + n_ents; entry++) {
27 entry->name += gd->reloc_off;
29 entry->load += gd->reloc_off;
31 entry->save += gd->reloc_off;
33 entry->erase += gd->reloc_off;
35 entry->init += gd->reloc_off;
40 static struct env_driver *_env_driver_lookup(enum env_location loc)
42 struct env_driver *drv;
43 const int n_ents = ll_entry_count(struct env_driver, env_driver);
44 struct env_driver *entry;
46 drv = ll_entry_start(struct env_driver, env_driver);
47 for (entry = drv; entry != drv + n_ents; entry++) {
48 if (loc == entry->location)
56 static enum env_location env_locations[] = {
57 #ifdef CONFIG_ENV_IS_IN_EEPROM
60 #ifdef CONFIG_ENV_IS_IN_EXT4
63 #ifdef CONFIG_ENV_IS_IN_FAT
66 #ifdef CONFIG_ENV_IS_IN_FLASH
69 #ifdef CONFIG_ENV_IS_IN_MMC
72 #ifdef CONFIG_ENV_IS_IN_NAND
75 #ifdef CONFIG_ENV_IS_IN_NVRAM
78 #ifdef CONFIG_ENV_IS_IN_REMOTE
81 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
84 #ifdef CONFIG_ENV_IS_IN_UBI
87 #ifdef CONFIG_ENV_IS_NOWHERE
92 static bool env_has_inited(enum env_location location)
94 return gd->env_has_init & BIT(location);
97 static void env_set_inited(enum env_location location)
100 * We're using a 32-bits bitmask stored in gd (env_has_init)
101 * using the above enum value as the bit index. We need to
102 * make sure that we're not overflowing it.
104 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
106 gd->env_has_init |= BIT(location);
110 * arch_env_get_location() - Returns the best env location for an arch
111 * @op: operations performed on the environment
112 * @prio: priority between the multiple environments, 0 being the
115 * This will return the preferred environment for the given priority.
116 * This is overridable by architectures if they need to and has lower
117 * priority than board side env_get_location() override.
119 * All implementations are free to use the operation, the priority and
120 * any other data relevant to their choice, but must take into account
121 * the fact that the lowest prority (0) is the most important location
122 * in the system. The following locations should be returned by order
123 * of descending priorities, from the highest to the lowest priority.
126 * an enum env_location value on success, a negative error code otherwise
128 __weak enum env_location arch_env_get_location(enum env_operation op, int prio)
130 if (prio >= ARRAY_SIZE(env_locations))
133 return env_locations[prio];
137 * env_get_location() - Returns the best env location for a board
138 * @op: operations performed on the environment
139 * @prio: priority between the multiple environments, 0 being the
142 * This will return the preferred environment for the given priority.
143 * This is overridable by boards if they need to.
145 * All implementations are free to use the operation, the priority and
146 * any other data relevant to their choice, but must take into account
147 * the fact that the lowest prority (0) is the most important location
148 * in the system. The following locations should be returned by order
149 * of descending priorities, from the highest to the lowest priority.
152 * an enum env_location value on success, a negative error code otherwise
154 __weak enum env_location env_get_location(enum env_operation op, int prio)
156 return arch_env_get_location(op, prio);
160 * env_driver_lookup() - Finds the most suited environment location
161 * @op: operations performed on the environment
162 * @prio: priority between the multiple environments, 0 being the
165 * This will try to find the available environment with the highest
166 * priority in the system.
169 * NULL on error, a pointer to a struct env_driver otherwise
171 static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
173 enum env_location loc = env_get_location(op, prio);
174 struct env_driver *drv;
176 if (loc == ENVL_UNKNOWN)
179 drv = _env_driver_lookup(loc);
181 debug("%s: No environment driver for location %d\n", __func__,
191 struct env_driver *drv;
195 if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
197 * When using a list of writeable variables, the baseline comes
198 * from the built-in default env. So load this first.
200 env_set_default(NULL, 0);
203 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
206 if (!env_has_inited(drv->location))
209 printf("Loading Environment from %s... ", drv->name);
211 * In error case, the error message must be printed during
212 * drv->load() in some underlying API, and it must be exactly
218 gd->env_load_prio = prio;
220 #if !CONFIG_IS_ENABLED(ENV_APPEND)
223 } else if (ret == -ENOMSG) {
224 /* Handle "bad CRC" case */
228 debug("Failed (%d)\n", ret);
233 * In case of invalid environment, we set the 'default' env location
234 * to the best choice, i.e.:
235 * 1. Environment location with bad CRC, if such location was found
236 * 2. Otherwise use the location with highest priority
238 * This way, next calls to env_save() will restore the environment
239 * at the right place.
242 debug("Selecting environment with bad CRC\n");
246 gd->env_load_prio = best_prio;
253 struct env_driver *drv;
255 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
259 printf("Loading Environment from %s... ", drv->name);
261 if (!env_has_inited(drv->location)) {
262 printf("not initialized\n");
268 printf("Failed (%d)\n", ret);
281 struct env_driver *drv;
283 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
287 printf("Saving Environment to %s... ", drv->name);
289 printf("not possible\n");
293 if (!env_has_inited(drv->location)) {
294 printf("not initialized\n");
300 printf("Failed (%d)\n", ret);
313 struct env_driver *drv;
315 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
320 printf("not possible\n");
324 if (!env_has_inited(drv->location)) {
325 printf("not initialized\n");
329 printf("Erasing Environment on %s... ", drv->name);
332 printf("Failed (%d)\n", ret);
345 struct env_driver *drv;
349 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
350 if (!drv->init || !(ret = drv->init()))
351 env_set_inited(drv->location);
353 env_set_inited(drv->location);
355 debug("%s: Environment %s init done (ret=%d)\n", __func__,
358 if (gd->env_valid == ENV_INVALID)
365 if (ret == -ENOENT) {
366 gd->env_addr = (ulong)&default_environment[0];
367 gd->env_valid = ENV_VALID;
375 int env_select(const char *name)
377 struct env_driver *drv;
378 const int n_ents = ll_entry_count(struct env_driver, env_driver);
379 struct env_driver *entry;
383 printf("Select Environment on %s: ", name);
385 /* search ENV driver by name */
386 drv = ll_entry_start(struct env_driver, env_driver);
387 for (entry = drv; entry != drv + n_ents; entry++) {
388 if (!strcmp(entry->name, name)) {
395 printf("driver not found\n");
399 /* search priority by driver */
400 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
401 if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
402 /* when priority change, reset the ENV flags */
403 if (gd->env_load_prio != prio) {
404 gd->env_load_prio = prio;
405 gd->env_valid = ENV_INVALID;
406 gd->flags &= ~GD_FLG_ENV_DEFAULT;
412 printf("priority not found\n");