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 static struct env_driver
*_env_driver_lookup(enum env_location loc
)
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
++) {
21 if (loc
== entry
->location
)
29 static enum env_location env_locations
[] = {
30 #ifdef CONFIG_ENV_IS_IN_EEPROM
33 #ifdef CONFIG_ENV_IS_IN_EXT4
36 #ifdef CONFIG_ENV_IS_IN_FAT
39 #ifdef CONFIG_ENV_IS_IN_FLASH
42 #ifdef CONFIG_ENV_IS_IN_MMC
45 #ifdef CONFIG_ENV_IS_IN_NAND
48 #ifdef CONFIG_ENV_IS_IN_NVRAM
51 #ifdef CONFIG_ENV_IS_IN_REMOTE
54 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
57 #ifdef CONFIG_ENV_IS_IN_UBI
60 #ifdef CONFIG_ENV_IS_NOWHERE
65 static enum env_location env_load_location
= ENVL_UNKNOWN
;
67 static bool env_has_inited(enum env_location location
)
69 return gd
->env_has_init
& BIT(location
);
72 static void env_set_inited(enum env_location location
)
75 * We're using a 32-bits bitmask stored in gd (env_has_init)
76 * using the above enum value as the bit index. We need to
77 * make sure that we're not overflowing it.
79 BUILD_BUG_ON(ARRAY_SIZE(env_locations
) > BITS_PER_LONG
);
81 gd
->env_has_init
|= BIT(location
);
85 * env_get_location() - Returns the best env location for a board
86 * @op: operations performed on the environment
87 * @prio: priority between the multiple environments, 0 being the
90 * This will return the preferred environment for the given priority.
92 * All implementations are free to use the operation, the priority and
93 * any other data relevant to their choice, but must take into account
94 * the fact that the lowest prority (0) is the most important location
95 * in the system. The following locations should be returned by order
96 * of descending priorities, from the highest to the lowest priority.
99 * an enum env_location value on success, a negative error code otherwise
101 static enum env_location
env_get_location(enum env_operation op
, int prio
)
107 if (prio
>= ARRAY_SIZE(env_locations
))
110 env_load_location
= env_locations
[prio
];
111 return env_load_location
;
114 return env_load_location
;
122 * env_driver_lookup() - Finds the most suited environment location
123 * @op: operations performed on the environment
124 * @prio: priority between the multiple environments, 0 being the
127 * This will try to find the available environment with the highest
128 * priority in the system.
131 * NULL on error, a pointer to a struct env_driver otherwise
133 static struct env_driver
*env_driver_lookup(enum env_operation op
, int prio
)
135 enum env_location loc
= env_get_location(op
, prio
);
136 struct env_driver
*drv
;
138 if (loc
== ENVL_UNKNOWN
)
141 drv
= _env_driver_lookup(loc
);
143 debug("%s: No environment driver for location %d\n", __func__
,
151 int env_get_char(int index
)
153 struct env_driver
*drv
;
156 if (gd
->env_valid
== ENV_INVALID
)
157 return default_environment
[index
];
159 for (prio
= 0; (drv
= env_driver_lookup(ENVOP_GET_CHAR
, prio
)); prio
++) {
165 if (!env_has_inited(drv
->location
))
168 ret
= drv
->get_char(index
);
172 debug("%s: Environment %s failed to load (err=%d)\n", __func__
,
181 struct env_driver
*drv
;
184 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
);
196 printf("Failed (%d)\n", ret
);
209 struct env_driver
*drv
;
212 for (prio
= 0; (drv
= env_driver_lookup(ENVOP_SAVE
, prio
)); prio
++) {
218 if (!env_has_inited(drv
->location
))
221 printf("Saving Environment to %s... ", drv
->name
);
224 printf("Failed (%d)\n", ret
);
237 struct env_driver
*drv
;
241 for (prio
= 0; (drv
= env_driver_lookup(ENVOP_INIT
, prio
)); prio
++) {
242 if (!drv
->init
|| !(ret
= drv
->init()))
243 env_set_inited(drv
->location
);
245 debug("%s: Environment %s init done (ret=%d)\n", __func__
,
252 if (ret
== -ENOENT
) {
253 gd
->env_addr
= (ulong
)&default_environment
[0];
254 gd
->env_valid
= ENV_VALID
;