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>
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_SATA
84 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
87 #ifdef CONFIG_ENV_IS_IN_UBI
90 #ifdef CONFIG_ENV_IS_NOWHERE
95 static bool env_has_inited(enum env_location location)
97 return gd->env_has_init & BIT(location);
100 static void env_set_inited(enum env_location location)
103 * We're using a 32-bits bitmask stored in gd (env_has_init)
104 * using the above enum value as the bit index. We need to
105 * make sure that we're not overflowing it.
107 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
109 gd->env_has_init |= BIT(location);
113 * env_get_location() - Returns the best env location for a board
114 * @op: operations performed on the environment
115 * @prio: priority between the multiple environments, 0 being the
118 * This will return the preferred environment for the given priority.
119 * This is overridable by boards if they need to.
121 * All implementations are free to use the operation, the priority and
122 * any other data relevant to their choice, but must take into account
123 * the fact that the lowest prority (0) is the most important location
124 * in the system. The following locations should be returned by order
125 * of descending priorities, from the highest to the lowest priority.
128 * an enum env_location value on success, a negative error code otherwise
130 __weak enum env_location env_get_location(enum env_operation op, int prio)
132 if (prio >= ARRAY_SIZE(env_locations))
135 return env_locations[prio];
140 * env_driver_lookup() - Finds the most suited environment location
141 * @op: operations performed on the environment
142 * @prio: priority between the multiple environments, 0 being the
145 * This will try to find the available environment with the highest
146 * priority in the system.
149 * NULL on error, a pointer to a struct env_driver otherwise
151 static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
153 enum env_location loc = env_get_location(op, prio);
154 struct env_driver *drv;
156 if (loc == ENVL_UNKNOWN)
159 drv = _env_driver_lookup(loc);
161 debug("%s: No environment driver for location %d\n", __func__,
169 __weak int env_get_char_spec(int index)
171 return *(uchar *)(gd->env_addr + index);
174 int env_get_char(int index)
176 if (gd->env_valid == ENV_INVALID)
177 return default_environment[index];
179 return env_get_char_spec(index);
184 struct env_driver *drv;
188 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
203 gd->env_load_prio = prio;
205 #if !CONFIG_IS_ENABLED(ENV_APPEND)
208 } else if (ret == -ENOMSG) {
209 /* Handle "bad CRC" case */
213 debug("Failed (%d)\n", ret);
218 * In case of invalid environment, we set the 'default' env location
219 * to the best choice, i.e.:
220 * 1. Environment location with bad CRC, if such location was found
221 * 2. Otherwise use the location with highest priority
223 * This way, next calls to env_save() will restore the environment
224 * at the right place.
227 debug("Selecting environment with bad CRC\n");
231 gd->env_load_prio = best_prio;
238 struct env_driver *drv;
240 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
244 printf("Loading Environment from %s... ", drv->name);
246 if (!env_has_inited(drv->location)) {
247 printf("not initialized\n");
253 printf("Failed (%d)\n", ret);
266 struct env_driver *drv;
268 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
272 printf("Saving Environment to %s... ", drv->name);
274 printf("not possible\n");
278 if (!env_has_inited(drv->location)) {
279 printf("not initialized\n");
285 printf("Failed (%d)\n", ret);
298 struct env_driver *drv;
300 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
307 if (!env_has_inited(drv->location))
310 printf("Erasing Environment on %s... ", drv->name);
313 printf("Failed (%d)\n", ret);
326 struct env_driver *drv;
330 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
331 if (!drv->init || !(ret = drv->init()))
332 env_set_inited(drv->location);
334 env_set_inited(drv->location);
336 debug("%s: Environment %s init done (ret=%d)\n", __func__,
339 if (gd->env_valid == ENV_INVALID)
346 if (ret == -ENOENT) {
347 gd->env_addr = (ulong)&default_environment[0];
348 gd->env_valid = ENV_VALID;
356 int env_select(const char *name)
358 struct env_driver *drv;
359 const int n_ents = ll_entry_count(struct env_driver, env_driver);
360 struct env_driver *entry;
364 printf("Select Environment on %s: ", name);
366 /* search ENV driver by name */
367 drv = ll_entry_start(struct env_driver, env_driver);
368 for (entry = drv; entry != drv + n_ents; entry++) {
369 if (!strcmp(entry->name, name)) {
376 printf("driver not found\n");
380 /* search priority by driver */
381 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
382 if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
383 /* when priority change, reset the ENV flags */
384 if (gd->env_load_prio != prio) {
385 gd->env_load_prio = prio;
386 gd->env_valid = ENV_INVALID;
387 gd->flags &= ~GD_FLG_ENV_DEFAULT;
393 printf("priority not found\n");