1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
9 #include <env_internal.h>
12 DECLARE_GLOBAL_DATA_PTR;
14 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
15 void env_fix_drivers(void)
17 struct env_driver *drv;
18 const int n_ents = ll_entry_count(struct env_driver, env_driver);
19 struct env_driver *entry;
21 drv = ll_entry_start(struct env_driver, env_driver);
22 for (entry = drv; entry != drv + n_ents; entry++) {
24 entry->name += gd->reloc_off;
26 entry->load += gd->reloc_off;
28 entry->save += gd->reloc_off;
30 entry->erase += gd->reloc_off;
32 entry->init += gd->reloc_off;
37 static struct env_driver *_env_driver_lookup(enum env_location loc)
39 struct env_driver *drv;
40 const int n_ents = ll_entry_count(struct env_driver, env_driver);
41 struct env_driver *entry;
43 drv = ll_entry_start(struct env_driver, env_driver);
44 for (entry = drv; entry != drv + n_ents; entry++) {
45 if (loc == entry->location)
53 static enum env_location env_locations[] = {
54 #ifdef CONFIG_ENV_IS_IN_EEPROM
57 #ifdef CONFIG_ENV_IS_IN_EXT4
60 #ifdef CONFIG_ENV_IS_IN_FAT
63 #ifdef CONFIG_ENV_IS_IN_FLASH
66 #ifdef CONFIG_ENV_IS_IN_MMC
69 #ifdef CONFIG_ENV_IS_IN_NAND
72 #ifdef CONFIG_ENV_IS_IN_NVRAM
75 #ifdef CONFIG_ENV_IS_IN_REMOTE
78 #ifdef CONFIG_ENV_IS_IN_SATA
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(ARRAY_SIZE(env_locations) > BITS_PER_LONG);
106 gd->env_has_init |= BIT(location);
110 * env_get_location() - Returns the best env location for a board
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 boards if they need to.
118 * All implementations are free to use the operation, the priority and
119 * any other data relevant to their choice, but must take into account
120 * the fact that the lowest prority (0) is the most important location
121 * in the system. The following locations should be returned by order
122 * of descending priorities, from the highest to the lowest priority.
125 * an enum env_location value on success, a negative error code otherwise
127 __weak enum env_location env_get_location(enum env_operation op, int prio)
129 if (prio >= ARRAY_SIZE(env_locations))
132 gd->env_load_prio = prio;
134 return env_locations[prio];
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
144 * This will try to find the available environment with the highest
145 * priority in the system.
148 * NULL on error, a pointer to a struct env_driver otherwise
150 static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
152 enum env_location loc = env_get_location(op, prio);
153 struct env_driver *drv;
155 if (loc == ENVL_UNKNOWN)
158 drv = _env_driver_lookup(loc);
160 debug("%s: No environment driver for location %d\n", __func__,
168 __weak int env_get_char_spec(int index)
170 return *(uchar *)(gd->env_addr + index);
173 int env_get_char(int index)
175 if (gd->env_valid == ENV_INVALID)
176 return default_environment[index];
178 return env_get_char_spec(index);
183 struct env_driver *drv;
187 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
193 if (!env_has_inited(drv->location))
196 printf("Loading Environment from %s... ", drv->name);
198 * In error case, the error message must be printed during
199 * drv->load() in some underlying API, and it must be exactly
206 } else if (ret == -ENOMSG) {
207 /* Handle "bad CRC" case */
211 debug("Failed (%d)\n", ret);
216 * In case of invalid environment, we set the 'default' env location
217 * to the best choice, i.e.:
218 * 1. Environment location with bad CRC, if such location was found
219 * 2. Otherwise use the location with highest priority
221 * This way, next calls to env_save() will restore the environment
222 * at the right place.
225 debug("Selecting environment with bad CRC\n");
228 env_get_location(ENVOP_LOAD, best_prio);
235 struct env_driver *drv;
237 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
244 if (!env_has_inited(drv->location))
247 printf("Saving Environment to %s... ", drv->name);
250 printf("Failed (%d)\n", ret);
263 struct env_driver *drv;
265 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
272 if (!env_has_inited(drv->location))
275 printf("Erasing Environment on %s... ", drv->name);
278 printf("Failed (%d)\n", ret);
291 struct env_driver *drv;
295 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
296 if (!drv->init || !(ret = drv->init()))
297 env_set_inited(drv->location);
299 debug("%s: Environment %s init done (ret=%d)\n", __func__,
306 if (ret == -ENOENT) {
307 gd->env_addr = (ulong)&default_environment[0];
308 gd->env_valid = ENV_VALID;