1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
9 #include <environment.h>
11 DECLARE_GLOBAL_DATA_PTR;
13 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
14 void env_fix_drivers(void)
16 struct env_driver *drv;
17 const int n_ents = ll_entry_count(struct env_driver, env_driver);
18 struct env_driver *entry;
20 drv = ll_entry_start(struct env_driver, env_driver);
21 for (entry = drv; entry != drv + n_ents; entry++) {
23 entry->name += gd->reloc_off;
25 entry->load += gd->reloc_off;
27 entry->save += gd->reloc_off;
29 entry->erase += gd->reloc_off;
31 entry->init += gd->reloc_off;
36 static struct env_driver *_env_driver_lookup(enum env_location loc)
38 struct env_driver *drv;
39 const int n_ents = ll_entry_count(struct env_driver, env_driver);
40 struct env_driver *entry;
42 drv = ll_entry_start(struct env_driver, env_driver);
43 for (entry = drv; entry != drv + n_ents; entry++) {
44 if (loc == entry->location)
52 static enum env_location env_locations[] = {
53 #ifdef CONFIG_ENV_IS_IN_EEPROM
56 #ifdef CONFIG_ENV_IS_IN_EXT4
59 #ifdef CONFIG_ENV_IS_IN_FAT
62 #ifdef CONFIG_ENV_IS_IN_FLASH
65 #ifdef CONFIG_ENV_IS_IN_MMC
68 #ifdef CONFIG_ENV_IS_IN_NAND
71 #ifdef CONFIG_ENV_IS_IN_NVRAM
74 #ifdef CONFIG_ENV_IS_IN_REMOTE
77 #ifdef CONFIG_ENV_IS_IN_SATA
80 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
83 #ifdef CONFIG_ENV_IS_IN_UBI
86 #ifdef CONFIG_ENV_IS_NOWHERE
91 static bool env_has_inited(enum env_location location)
93 return gd->env_has_init & BIT(location);
96 static void env_set_inited(enum env_location location)
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.
103 BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG);
105 gd->env_has_init |= BIT(location);
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
114 * This will return the preferred environment for the given priority.
115 * This is overridable by boards if they need to.
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.
124 * an enum env_location value on success, a negative error code otherwise
126 __weak enum env_location env_get_location(enum env_operation op, int prio)
128 if (prio >= ARRAY_SIZE(env_locations))
131 gd->env_load_prio = prio;
133 return env_locations[prio];
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
143 * This will try to find the available environment with the highest
144 * priority in the system.
147 * NULL on error, a pointer to a struct env_driver otherwise
149 static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
151 enum env_location loc = env_get_location(op, prio);
152 struct env_driver *drv;
154 if (loc == ENVL_UNKNOWN)
157 drv = _env_driver_lookup(loc);
159 debug("%s: No environment driver for location %d\n", __func__,
167 __weak int env_get_char_spec(int index)
169 return *(uchar *)(gd->env_addr + index);
172 int env_get_char(int index)
174 if (gd->env_valid == ENV_INVALID)
175 return default_environment[index];
177 return env_get_char_spec(index);
182 struct env_driver *drv;
186 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
192 if (!env_has_inited(drv->location))
195 printf("Loading Environment from %s... ", drv->name);
197 * In error case, the error message must be printed during
198 * drv->load() in some underlying API, and it must be exactly
205 } else if (ret == -ENOMSG) {
206 /* Handle "bad CRC" case */
210 debug("Failed (%d)\n", ret);
215 * In case of invalid environment, we set the 'default' env location
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
220 * This way, next calls to env_save() will restore the environment
221 * at the right place.
224 debug("Selecting environment with bad CRC\n");
227 env_get_location(ENVOP_LOAD, best_prio);
234 struct env_driver *drv;
236 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
243 if (!env_has_inited(drv->location))
246 printf("Saving Environment to %s... ", drv->name);
249 printf("Failed (%d)\n", ret);
262 struct env_driver *drv;
264 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
271 if (!env_has_inited(drv->location))
274 printf("Erasing Environment on %s... ", drv->name);
277 printf("Failed (%d)\n", ret);
290 struct env_driver *drv;
294 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
295 if (!drv->init || !(ret = drv->init()))
296 env_set_inited(drv->location);
298 debug("%s: Environment %s init done (ret=%d)\n", __func__,
305 if (ret == -ENOENT) {
306 gd->env_addr = (ulong)&default_environment[0];
307 gd->env_valid = ENV_VALID;