1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Google, Inc
8 #include <env_internal.h>
10 #include <asm/global_data.h>
11 #include <linux/bitops.h>
12 #include <linux/bug.h>
13 #include <linux/errno.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 static struct env_driver *_env_driver_lookup(enum env_location loc)
19 struct env_driver *drv;
20 const int n_ents = ll_entry_count(struct env_driver, env_driver);
21 struct env_driver *entry;
23 drv = ll_entry_start(struct env_driver, env_driver);
24 for (entry = drv; entry != drv + n_ents; entry++) {
25 if (loc == entry->location)
33 static enum env_location env_locations[] = {
34 #ifdef CONFIG_ENV_IS_IN_EEPROM
37 #ifdef CONFIG_ENV_IS_IN_EXT4
40 #ifdef CONFIG_ENV_IS_IN_FAT
43 #ifdef CONFIG_ENV_IS_IN_FLASH
46 #ifdef CONFIG_ENV_IS_IN_MMC
49 #ifdef CONFIG_ENV_IS_IN_NAND
52 #ifdef CONFIG_ENV_IS_IN_NVRAM
55 #ifdef CONFIG_ENV_IS_IN_REMOTE
58 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
61 #ifdef CONFIG_ENV_IS_IN_UBI
64 #ifdef CONFIG_ENV_IS_NOWHERE
69 static bool env_has_inited(enum env_location location)
71 return gd->env_has_init & BIT(location);
74 static void env_set_inited(enum env_location location)
77 * We're using a 32-bits bitmask stored in gd (env_has_init)
78 * using the above enum value as the bit index. We need to
79 * make sure that we're not overflowing it.
81 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
83 gd->env_has_init |= BIT(location);
87 * arch_env_get_location() - Returns the best env location for an arch
88 * @op: operations performed on the environment
89 * @prio: priority between the multiple environments, 0 being the
92 * This will return the preferred environment for the given priority.
93 * This is overridable by architectures if they need to and has lower
94 * priority than board side env_get_location() override.
96 * All implementations are free to use the operation, the priority and
97 * any other data relevant to their choice, but must take into account
98 * the fact that the lowest prority (0) is the most important location
99 * in the system. The following locations should be returned by order
100 * of descending priorities, from the highest to the lowest priority.
103 * an enum env_location value on success, a negative error code otherwise
105 __weak enum env_location arch_env_get_location(enum env_operation op, int prio)
107 if (prio >= ARRAY_SIZE(env_locations))
110 return env_locations[prio];
114 * env_get_location() - Returns the best env location for a board
115 * @op: operations performed on the environment
116 * @prio: priority between the multiple environments, 0 being the
119 * This will return the preferred environment for the given priority.
120 * This is overridable by boards if they need to.
122 * All implementations are free to use the operation, the priority and
123 * any other data relevant to their choice, but must take into account
124 * the fact that the lowest prority (0) is the most important location
125 * in the system. The following locations should be returned by order
126 * of descending priorities, from the highest to the lowest priority.
129 * an enum env_location value on success, a negative error code otherwise
131 __weak enum env_location env_get_location(enum env_operation op, int prio)
133 return arch_env_get_location(op, 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__,
168 struct env_driver *drv;
172 if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
174 * When using a list of writeable variables, the baseline comes
175 * from the built-in default env. So load this first.
177 env_set_default(NULL, 0);
180 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
183 if (!env_has_inited(drv->location))
186 printf("Loading Environment from %s... ", drv->name);
188 * In error case, the error message must be printed during
189 * drv->load() in some underlying API, and it must be exactly
195 gd->env_load_prio = prio;
198 } else if (ret == -ENOMSG) {
199 /* Handle "bad CRC" case */
203 debug("Failed (%d)\n", ret);
208 * In case of invalid environment, we set the 'default' env location
209 * to the best choice, i.e.:
210 * 1. Environment location with bad CRC, if such location was found
211 * 2. Otherwise use the location with highest priority
213 * This way, next calls to env_save() will restore the environment
214 * at the right place.
217 debug("Selecting environment with bad CRC\n");
221 gd->env_load_prio = best_prio;
228 struct env_driver *drv;
230 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
234 printf("Loading Environment from %s... ", drv->name);
236 if (!env_has_inited(drv->location)) {
237 printf("not initialized\n");
243 printf("Failed (%d)\n", ret);
256 struct env_driver *drv;
258 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
262 printf("Saving Environment to %s... ", drv->name);
264 printf("not possible\n");
268 if (!env_has_inited(drv->location)) {
269 printf("not initialized\n");
275 printf("Failed (%d)\n", ret);
288 struct env_driver *drv;
290 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
295 printf("not possible\n");
299 if (!env_has_inited(drv->location)) {
300 printf("not initialized\n");
304 printf("Erasing Environment on %s... ", drv->name);
307 printf("Failed (%d)\n", ret);
320 struct env_driver *drv;
324 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
325 if (!drv->init || !(ret = drv->init()))
326 env_set_inited(drv->location);
328 env_set_inited(drv->location);
330 debug("%s: Environment %s init done (ret=%d)\n", __func__,
333 if (gd->env_valid == ENV_INVALID)
340 if (ret == -ENOENT) {
341 gd->env_addr = (ulong)&default_environment[0];
342 gd->env_valid = ENV_VALID;
350 int env_select(const char *name)
352 struct env_driver *drv;
353 const int n_ents = ll_entry_count(struct env_driver, env_driver);
354 struct env_driver *entry;
358 printf("Select Environment on %s: ", name);
360 /* search ENV driver by name */
361 drv = ll_entry_start(struct env_driver, env_driver);
362 for (entry = drv; entry != drv + n_ents; entry++) {
363 if (!strcmp(entry->name, name)) {
370 printf("driver not found\n");
374 /* search priority by driver */
375 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
376 if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
377 /* when priority change, reset the ENV flags */
378 if (gd->env_load_prio != prio) {
379 gd->env_load_prio = prio;
380 gd->env_valid = ENV_INVALID;
381 gd->flags &= ~GD_FLG_ENV_DEFAULT;
387 printf("priority not found\n");