1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Google, Inc
9 #include <env_internal.h>
11 #include <linux/bitops.h>
12 #include <linux/bug.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
17 void env_fix_drivers(void)
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++) {
26 entry->name += gd->reloc_off;
28 entry->load += gd->reloc_off;
30 entry->save += gd->reloc_off;
32 entry->erase += gd->reloc_off;
34 entry->init += gd->reloc_off;
39 static struct env_driver *_env_driver_lookup(enum env_location loc)
41 struct env_driver *drv;
42 const int n_ents = ll_entry_count(struct env_driver, env_driver);
43 struct env_driver *entry;
45 drv = ll_entry_start(struct env_driver, env_driver);
46 for (entry = drv; entry != drv + n_ents; entry++) {
47 if (loc == entry->location)
55 static enum env_location env_locations[] = {
56 #ifdef CONFIG_ENV_IS_IN_EEPROM
59 #ifdef CONFIG_ENV_IS_IN_EXT4
62 #ifdef CONFIG_ENV_IS_IN_FAT
65 #ifdef CONFIG_ENV_IS_IN_FLASH
68 #ifdef CONFIG_ENV_IS_IN_MMC
71 #ifdef CONFIG_ENV_IS_IN_NAND
74 #ifdef CONFIG_ENV_IS_IN_NVRAM
77 #ifdef CONFIG_ENV_IS_IN_REMOTE
80 #ifdef CONFIG_ENV_IS_IN_SATA
83 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
86 #ifdef CONFIG_ENV_IS_IN_UBI
89 #ifdef CONFIG_ENV_IS_NOWHERE
94 static bool env_has_inited(enum env_location location)
96 return gd->env_has_init & BIT(location);
99 static void env_set_inited(enum env_location location)
102 * We're using a 32-bits bitmask stored in gd (env_has_init)
103 * using the above enum value as the bit index. We need to
104 * make sure that we're not overflowing it.
106 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
108 gd->env_has_init |= BIT(location);
112 * env_get_location() - Returns the best env location for a board
113 * @op: operations performed on the environment
114 * @prio: priority between the multiple environments, 0 being the
117 * This will return the preferred environment for the given priority.
118 * This is overridable by boards if they need to.
120 * All implementations are free to use the operation, the priority and
121 * any other data relevant to their choice, but must take into account
122 * the fact that the lowest prority (0) is the most important location
123 * in the system. The following locations should be returned by order
124 * of descending priorities, from the highest to the lowest priority.
127 * an enum env_location value on success, a negative error code otherwise
129 __weak enum env_location env_get_location(enum env_operation op, int prio)
131 if (prio >= ARRAY_SIZE(env_locations))
134 return env_locations[prio];
139 * env_driver_lookup() - Finds the most suited environment location
140 * @op: operations performed on the environment
141 * @prio: priority between the multiple environments, 0 being the
144 * This will try to find the available environment with the highest
145 * priority in the system.
148 * NULL on error, a pointer to a struct env_driver otherwise
150 static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
152 enum env_location loc = env_get_location(op, prio);
153 struct env_driver *drv;
155 if (loc == ENVL_UNKNOWN)
158 drv = _env_driver_lookup(loc);
160 debug("%s: No environment driver for location %d\n", __func__,
168 __weak int env_get_char_spec(int index)
170 return *(uchar *)(gd->env_addr + index);
173 int env_get_char(int index)
175 if (gd->env_valid == ENV_INVALID)
176 return default_environment[index];
178 return env_get_char_spec(index);
183 struct env_driver *drv;
187 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
190 if (!env_has_inited(drv->location))
193 printf("Loading Environment from %s... ", drv->name);
195 * In error case, the error message must be printed during
196 * drv->load() in some underlying API, and it must be exactly
202 gd->env_load_prio = prio;
204 #if !CONFIG_IS_ENABLED(ENV_APPEND)
207 } else if (ret == -ENOMSG) {
208 /* Handle "bad CRC" case */
212 debug("Failed (%d)\n", ret);
217 * In case of invalid environment, we set the 'default' env location
218 * to the best choice, i.e.:
219 * 1. Environment location with bad CRC, if such location was found
220 * 2. Otherwise use the location with highest priority
222 * This way, next calls to env_save() will restore the environment
223 * at the right place.
226 debug("Selecting environment with bad CRC\n");
230 gd->env_load_prio = best_prio;
237 struct env_driver *drv;
239 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
243 printf("Loading Environment from %s... ", drv->name);
245 if (!env_has_inited(drv->location)) {
246 printf("not initialized\n");
252 printf("Failed (%d)\n", ret);
265 struct env_driver *drv;
267 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
271 printf("Saving Environment to %s... ", drv->name);
273 printf("not possible\n");
277 if (!env_has_inited(drv->location)) {
278 printf("not initialized\n");
284 printf("Failed (%d)\n", ret);
297 struct env_driver *drv;
299 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
306 if (!env_has_inited(drv->location))
309 printf("Erasing Environment on %s... ", drv->name);
312 printf("Failed (%d)\n", ret);
325 struct env_driver *drv;
329 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
330 if (!drv->init || !(ret = drv->init()))
331 env_set_inited(drv->location);
333 debug("%s: Environment %s init done (ret=%d)\n", __func__,
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");