2 * Copyright (C) 2017 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
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->init += gd->reloc_off;
34 static struct env_driver *_env_driver_lookup(enum env_location loc)
36 struct env_driver *drv;
37 const int n_ents = ll_entry_count(struct env_driver, env_driver);
38 struct env_driver *entry;
40 drv = ll_entry_start(struct env_driver, env_driver);
41 for (entry = drv; entry != drv + n_ents; entry++) {
42 if (loc == entry->location)
50 static enum env_location env_locations[] = {
51 #ifdef CONFIG_ENV_IS_IN_EEPROM
54 #ifdef CONFIG_ENV_IS_IN_EXT4
57 #ifdef CONFIG_ENV_IS_IN_FAT
60 #ifdef CONFIG_ENV_IS_IN_FLASH
63 #ifdef CONFIG_ENV_IS_IN_MMC
66 #ifdef CONFIG_ENV_IS_IN_NAND
69 #ifdef CONFIG_ENV_IS_IN_NVRAM
72 #ifdef CONFIG_ENV_IS_IN_REMOTE
75 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
78 #ifdef CONFIG_ENV_IS_IN_UBI
81 #ifdef CONFIG_ENV_IS_NOWHERE
86 static bool env_has_inited(enum env_location location)
88 return gd->env_has_init & BIT(location);
91 static void env_set_inited(enum env_location location)
94 * We're using a 32-bits bitmask stored in gd (env_has_init)
95 * using the above enum value as the bit index. We need to
96 * make sure that we're not overflowing it.
98 BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG);
100 gd->env_has_init |= BIT(location);
104 * env_get_location() - Returns the best env location for a board
105 * @op: operations performed on the environment
106 * @prio: priority between the multiple environments, 0 being the
109 * This will return the preferred environment for the given priority.
110 * This is overridable by boards if they need to.
112 * All implementations are free to use the operation, the priority and
113 * any other data relevant to their choice, but must take into account
114 * the fact that the lowest prority (0) is the most important location
115 * in the system. The following locations should be returned by order
116 * of descending priorities, from the highest to the lowest priority.
119 * an enum env_location value on success, a negative error code otherwise
121 __weak enum env_location env_get_location(enum env_operation op, int prio)
127 if (prio >= ARRAY_SIZE(env_locations))
130 gd->env_load_location = env_locations[prio];
131 return gd->env_load_location;
134 return gd->env_load_location;
142 * env_driver_lookup() - Finds the most suited environment location
143 * @op: operations performed on the environment
144 * @prio: priority between the multiple environments, 0 being the
147 * This will try to find the available environment with the highest
148 * priority in the system.
151 * NULL on error, a pointer to a struct env_driver otherwise
153 static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
155 enum env_location loc = env_get_location(op, prio);
156 struct env_driver *drv;
158 if (loc == ENVL_UNKNOWN)
161 drv = _env_driver_lookup(loc);
163 debug("%s: No environment driver for location %d\n", __func__,
171 __weak int env_get_char_spec(int index)
173 return *(uchar *)(gd->env_addr + index);
176 int env_get_char(int index)
178 if (gd->env_valid == ENV_INVALID)
179 return default_environment[index];
181 return env_get_char_spec(index);
186 struct env_driver *drv;
189 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
195 if (!env_has_inited(drv->location))
198 printf("Loading Environment from %s... ", drv->name);
201 printf("Failed (%d)\n", ret);
214 struct env_driver *drv;
217 for (prio = 0; (drv = env_driver_lookup(ENVOP_SAVE, prio)); prio++) {
223 if (!env_has_inited(drv->location))
226 printf("Saving Environment to %s... ", drv->name);
229 printf("Failed (%d)\n", ret);
242 struct env_driver *drv;
246 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
247 if (!drv->init || !(ret = drv->init()))
248 env_set_inited(drv->location);
250 debug("%s: Environment %s init done (ret=%d)\n", __func__,
257 if (ret == -ENOENT) {
258 gd->env_addr = (ulong)&default_environment[0];
259 gd->env_valid = ENV_VALID;