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
->init
+= gd
->reloc_off
;
33 static struct env_driver
*_env_driver_lookup(enum env_location loc
)
35 struct env_driver
*drv
;
36 const int n_ents
= ll_entry_count(struct env_driver
, env_driver
);
37 struct env_driver
*entry
;
39 drv
= ll_entry_start(struct env_driver
, env_driver
);
40 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
41 if (loc
== entry
->location
)
49 static enum env_location env_locations
[] = {
50 #ifdef CONFIG_ENV_IS_IN_EEPROM
53 #ifdef CONFIG_ENV_IS_IN_EXT4
56 #ifdef CONFIG_ENV_IS_IN_FAT
59 #ifdef CONFIG_ENV_IS_IN_FLASH
62 #ifdef CONFIG_ENV_IS_IN_MMC
65 #ifdef CONFIG_ENV_IS_IN_NAND
68 #ifdef CONFIG_ENV_IS_IN_NVRAM
71 #ifdef CONFIG_ENV_IS_IN_REMOTE
74 #ifdef CONFIG_ENV_IS_IN_SATA
77 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
80 #ifdef CONFIG_ENV_IS_IN_UBI
83 #ifdef CONFIG_ENV_IS_NOWHERE
88 static bool env_has_inited(enum env_location location
)
90 return gd
->env_has_init
& BIT(location
);
93 static void env_set_inited(enum env_location location
)
96 * We're using a 32-bits bitmask stored in gd (env_has_init)
97 * using the above enum value as the bit index. We need to
98 * make sure that we're not overflowing it.
100 BUILD_BUG_ON(ARRAY_SIZE(env_locations
) > BITS_PER_LONG
);
102 gd
->env_has_init
|= BIT(location
);
106 * env_get_location() - Returns the best env location for a board
107 * @op: operations performed on the environment
108 * @prio: priority between the multiple environments, 0 being the
111 * This will return the preferred environment for the given priority.
112 * This is overridable by boards if they need to.
114 * All implementations are free to use the operation, the priority and
115 * any other data relevant to their choice, but must take into account
116 * the fact that the lowest prority (0) is the most important location
117 * in the system. The following locations should be returned by order
118 * of descending priorities, from the highest to the lowest priority.
121 * an enum env_location value on success, a negative error code otherwise
123 __weak
enum env_location
env_get_location(enum env_operation op
, int prio
)
125 if (prio
>= ARRAY_SIZE(env_locations
))
128 gd
->env_load_prio
= prio
;
130 return env_locations
[prio
];
135 * env_driver_lookup() - Finds the most suited environment location
136 * @op: operations performed on the environment
137 * @prio: priority between the multiple environments, 0 being the
140 * This will try to find the available environment with the highest
141 * priority in the system.
144 * NULL on error, a pointer to a struct env_driver otherwise
146 static struct env_driver
*env_driver_lookup(enum env_operation op
, int prio
)
148 enum env_location loc
= env_get_location(op
, prio
);
149 struct env_driver
*drv
;
151 if (loc
== ENVL_UNKNOWN
)
154 drv
= _env_driver_lookup(loc
);
156 debug("%s: No environment driver for location %d\n", __func__
,
164 __weak
int env_get_char_spec(int index
)
166 return *(uchar
*)(gd
->env_addr
+ index
);
169 int env_get_char(int index
)
171 if (gd
->env_valid
== ENV_INVALID
)
172 return default_environment
[index
];
174 return env_get_char_spec(index
);
179 struct env_driver
*drv
;
183 for (prio
= 0; (drv
= env_driver_lookup(ENVOP_LOAD
, prio
)); prio
++) {
189 if (!env_has_inited(drv
->location
))
192 printf("Loading Environment from %s... ", drv
->name
);
194 * In error case, the error message must be printed during
195 * drv->load() in some underlying API, and it must be exactly
202 } else if (ret
== -ENOMSG
) {
203 /* Handle "bad CRC" case */
207 debug("Failed (%d)\n", ret
);
212 * In case of invalid environment, we set the 'default' env location
213 * to the best choice, i.e.:
214 * 1. Environment location with bad CRC, if such location was found
215 * 2. Otherwise use the location with highest priority
217 * This way, next calls to env_save() will restore the environment
218 * at the right place.
221 debug("Selecting environment with bad CRC\n");
224 env_get_location(ENVOP_LOAD
, best_prio
);
231 struct env_driver
*drv
;
233 drv
= env_driver_lookup(ENVOP_SAVE
, gd
->env_load_prio
);
240 if (!env_has_inited(drv
->location
))
243 printf("Saving Environment to %s... ", drv
->name
);
246 printf("Failed (%d)\n", ret
);
259 struct env_driver
*drv
;
263 for (prio
= 0; (drv
= env_driver_lookup(ENVOP_INIT
, prio
)); prio
++) {
264 if (!drv
->init
|| !(ret
= drv
->init()))
265 env_set_inited(drv
->location
);
267 debug("%s: Environment %s init done (ret=%d)\n", __func__
,
274 if (ret
== -ENOENT
) {
275 gd
->env_addr
= (ulong
)&default_environment
[0];
276 gd
->env_valid
= ENV_VALID
;