1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
8 #include <environment.h>
10 DECLARE_GLOBAL_DATA_PTR;
12 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
13 void env_fix_drivers(void)
15 struct env_driver *drv;
16 const int n_ents = ll_entry_count(struct env_driver, env_driver);
17 struct env_driver *entry;
19 drv = ll_entry_start(struct env_driver, env_driver);
20 for (entry = drv; entry != drv + n_ents; entry++) {
22 entry->name += gd->reloc_off;
24 entry->load += gd->reloc_off;
26 entry->save += gd->reloc_off;
28 entry->erase += gd->reloc_off;
30 entry->init += gd->reloc_off;
35 static struct env_driver *_env_driver_lookup(enum env_location loc)
37 struct env_driver *drv;
38 const int n_ents = ll_entry_count(struct env_driver, env_driver);
39 struct env_driver *entry;
41 drv = ll_entry_start(struct env_driver, env_driver);
42 for (entry = drv; entry != drv + n_ents; entry++) {
43 if (loc == entry->location)
51 static enum env_location env_locations[] = {
52 #ifdef CONFIG_ENV_IS_IN_EEPROM
55 #ifdef CONFIG_ENV_IS_IN_EXT4
58 #ifdef CONFIG_ENV_IS_IN_FAT
61 #ifdef CONFIG_ENV_IS_IN_FLASH
64 #ifdef CONFIG_ENV_IS_IN_MMC
67 #ifdef CONFIG_ENV_IS_IN_NAND
70 #ifdef CONFIG_ENV_IS_IN_NVRAM
73 #ifdef CONFIG_ENV_IS_IN_REMOTE
76 #ifdef CONFIG_ENV_IS_IN_SATA
79 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
82 #ifdef CONFIG_ENV_IS_IN_UBI
85 #ifdef CONFIG_ENV_IS_NOWHERE
90 static bool env_has_inited(enum env_location location)
92 return gd->env_has_init & BIT(location);
95 static void env_set_inited(enum env_location location)
98 * We're using a 32-bits bitmask stored in gd (env_has_init)
99 * using the above enum value as the bit index. We need to
100 * make sure that we're not overflowing it.
102 BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG);
104 gd->env_has_init |= BIT(location);
108 * env_get_location() - Returns the best env location for a board
109 * @op: operations performed on the environment
110 * @prio: priority between the multiple environments, 0 being the
113 * This will return the preferred environment for the given priority.
114 * This is overridable by boards if they need to.
116 * All implementations are free to use the operation, the priority and
117 * any other data relevant to their choice, but must take into account
118 * the fact that the lowest prority (0) is the most important location
119 * in the system. The following locations should be returned by order
120 * of descending priorities, from the highest to the lowest priority.
123 * an enum env_location value on success, a negative error code otherwise
125 __weak enum env_location env_get_location(enum env_operation op, int prio)
127 if (prio >= ARRAY_SIZE(env_locations))
130 gd->env_load_prio = prio;
132 return env_locations[prio];
137 * env_driver_lookup() - Finds the most suited environment location
138 * @op: operations performed on the environment
139 * @prio: priority between the multiple environments, 0 being the
142 * This will try to find the available environment with the highest
143 * priority in the system.
146 * NULL on error, a pointer to a struct env_driver otherwise
148 static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
150 enum env_location loc = env_get_location(op, prio);
151 struct env_driver *drv;
153 if (loc == ENVL_UNKNOWN)
156 drv = _env_driver_lookup(loc);
158 debug("%s: No environment driver for location %d\n", __func__,
166 __weak int env_get_char_spec(int index)
168 return *(uchar *)(gd->env_addr + index);
171 int env_get_char(int index)
173 if (gd->env_valid == ENV_INVALID)
174 return default_environment[index];
176 return env_get_char_spec(index);
181 struct env_driver *drv;
185 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
191 if (!env_has_inited(drv->location))
194 printf("Loading Environment from %s... ", drv->name);
196 * In error case, the error message must be printed during
197 * drv->load() in some underlying API, and it must be exactly
204 } else if (ret == -ENOMSG) {
205 /* Handle "bad CRC" case */
209 debug("Failed (%d)\n", ret);
214 * In case of invalid environment, we set the 'default' env location
215 * to the best choice, i.e.:
216 * 1. Environment location with bad CRC, if such location was found
217 * 2. Otherwise use the location with highest priority
219 * This way, next calls to env_save() will restore the environment
220 * at the right place.
223 debug("Selecting environment with bad CRC\n");
226 env_get_location(ENVOP_LOAD, best_prio);
233 struct env_driver *drv;
235 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
242 if (!env_has_inited(drv->location))
245 printf("Saving Environment to %s... ", drv->name);
248 printf("Failed (%d)\n", ret);
261 struct env_driver *drv;
263 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
270 if (!env_has_inited(drv->location))
273 printf("Erasing Environment on %s... ", drv->name);
276 printf("Failed (%d)\n", ret);
289 struct env_driver *drv;
293 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
294 if (!drv->init || !(ret = drv->init()))
295 env_set_inited(drv->location);
297 debug("%s: Environment %s init done (ret=%d)\n", __func__,
304 if (ret == -ENOENT) {
305 gd->env_addr = (ulong)&default_environment[0];
306 gd->env_valid = ENV_VALID;