]>
Commit | Line | Data |
---|---|---|
c9d728dd SG |
1 | /* |
2 | * Copyright (C) 2017 Google, Inc | |
3 | * Written by Simon Glass <sjg@chromium.org> | |
4 | * | |
5 | * SPDX-License-Identifier: GPL-2.0+ | |
6 | */ | |
7 | ||
8 | #include <common.h> | |
9 | #include <environment.h> | |
10 | ||
11 | DECLARE_GLOBAL_DATA_PTR; | |
12 | ||
52746c43 | 13 | static struct env_driver *_env_driver_lookup(enum env_location loc) |
c9d728dd SG |
14 | { |
15 | struct env_driver *drv; | |
16 | const int n_ents = ll_entry_count(struct env_driver, env_driver); | |
17 | struct env_driver *entry; | |
18 | ||
19 | drv = ll_entry_start(struct env_driver, env_driver); | |
20 | for (entry = drv; entry != drv + n_ents; entry++) { | |
21 | if (loc == entry->location) | |
22 | return entry; | |
23 | } | |
24 | ||
25 | /* Not found */ | |
26 | return NULL; | |
27 | } | |
28 | ||
7d714a24 MR |
29 | static enum env_location env_locations[] = { |
30 | #ifdef CONFIG_ENV_IS_IN_EEPROM | |
31 | ENVL_EEPROM, | |
32 | #endif | |
33 | #ifdef CONFIG_ENV_IS_IN_EXT4 | |
34 | ENVL_EXT4, | |
35 | #endif | |
36 | #ifdef CONFIG_ENV_IS_IN_FAT | |
37 | ENVL_FAT, | |
38 | #endif | |
39 | #ifdef CONFIG_ENV_IS_IN_FLASH | |
40 | ENVL_FLASH, | |
41 | #endif | |
42 | #ifdef CONFIG_ENV_IS_IN_MMC | |
43 | ENVL_MMC, | |
44 | #endif | |
45 | #ifdef CONFIG_ENV_IS_IN_NAND | |
46 | ENVL_NAND, | |
47 | #endif | |
48 | #ifdef CONFIG_ENV_IS_IN_NVRAM | |
49 | ENVL_NVRAM, | |
50 | #endif | |
51 | #ifdef CONFIG_ENV_IS_IN_REMOTE | |
52 | ENVL_REMOTE, | |
53 | #endif | |
54 | #ifdef CONFIG_ENV_IS_IN_SPI_FLASH | |
55 | ENVL_SPI_FLASH, | |
56 | #endif | |
57 | #ifdef CONFIG_ENV_IS_IN_UBI | |
58 | ENVL_UBI, | |
59 | #endif | |
60 | #ifdef CONFIG_ENV_IS_NOWHERE | |
61 | ENVL_NOWHERE, | |
62 | #endif | |
63 | }; | |
64 | ||
65 | static enum env_location env_load_location = ENVL_UNKNOWN; | |
66 | ||
1d446087 MR |
67 | static bool env_has_inited(enum env_location location) |
68 | { | |
69 | return gd->env_has_init & BIT(location); | |
70 | } | |
71 | ||
72 | static void env_set_inited(enum env_location location) | |
73 | { | |
74 | /* | |
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. | |
78 | */ | |
79 | BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG); | |
80 | ||
81 | gd->env_has_init |= BIT(location); | |
82 | } | |
83 | ||
8a3a7e22 MR |
84 | /** |
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 | |
88 | * highest priority | |
89 | * | |
90 | * This will return the preferred environment for the given priority. | |
91 | * | |
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. | |
97 | * | |
98 | * Returns: | |
99 | * an enum env_location value on success, a negative error code otherwise | |
100 | */ | |
101 | static enum env_location env_get_location(enum env_operation op, int prio) | |
c9d728dd | 102 | { |
7d714a24 MR |
103 | switch (op) { |
104 | case ENVOP_GET_CHAR: | |
105 | case ENVOP_INIT: | |
106 | case ENVOP_LOAD: | |
107 | if (prio >= ARRAY_SIZE(env_locations)) | |
108 | return ENVL_UNKNOWN; | |
109 | ||
110 | env_load_location = env_locations[prio]; | |
111 | return env_load_location; | |
112 | ||
113 | case ENVOP_SAVE: | |
114 | return env_load_location; | |
115 | } | |
8a3a7e22 | 116 | |
7d714a24 | 117 | return ENVL_UNKNOWN; |
c9d728dd SG |
118 | } |
119 | ||
8a3a7e22 MR |
120 | |
121 | /** | |
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 | |
125 | * highest priority | |
126 | * | |
127 | * This will try to find the available environment with the highest | |
128 | * priority in the system. | |
129 | * | |
130 | * Returns: | |
131 | * NULL on error, a pointer to a struct env_driver otherwise | |
132 | */ | |
133 | static struct env_driver *env_driver_lookup(enum env_operation op, int prio) | |
c9d728dd | 134 | { |
8a3a7e22 | 135 | enum env_location loc = env_get_location(op, prio); |
c9d728dd SG |
136 | struct env_driver *drv; |
137 | ||
8a3a7e22 MR |
138 | if (loc == ENVL_UNKNOWN) |
139 | return NULL; | |
140 | ||
52746c43 | 141 | drv = _env_driver_lookup(loc); |
c9d728dd SG |
142 | if (!drv) { |
143 | debug("%s: No environment driver for location %d\n", __func__, | |
144 | loc); | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | return drv; | |
149 | } | |
150 | ||
a69d0f60 | 151 | int env_get_char(int index) |
c9d728dd | 152 | { |
8a3a7e22 MR |
153 | struct env_driver *drv; |
154 | int prio; | |
c9d728dd | 155 | |
2d7cb5b4 | 156 | if (gd->env_valid == ENV_INVALID) |
a69d0f60 | 157 | return default_environment[index]; |
8a3a7e22 MR |
158 | |
159 | for (prio = 0; (drv = env_driver_lookup(ENVOP_GET_CHAR, prio)); prio++) { | |
160 | int ret; | |
161 | ||
162 | if (!drv->get_char) | |
163 | continue; | |
164 | ||
1d446087 MR |
165 | if (!env_has_inited(drv->location)) |
166 | continue; | |
167 | ||
8a3a7e22 MR |
168 | ret = drv->get_char(index); |
169 | if (!ret) | |
170 | return 0; | |
171 | ||
172 | debug("%s: Environment %s failed to load (err=%d)\n", __func__, | |
173 | drv->name, ret); | |
c9d728dd SG |
174 | } |
175 | ||
8a3a7e22 | 176 | return -ENODEV; |
c9d728dd SG |
177 | } |
178 | ||
179 | int env_load(void) | |
180 | { | |
8a3a7e22 MR |
181 | struct env_driver *drv; |
182 | int prio; | |
c9d728dd | 183 | |
8a3a7e22 MR |
184 | for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) { |
185 | int ret; | |
186 | ||
187 | if (!drv->load) | |
188 | continue; | |
189 | ||
1d446087 MR |
190 | if (!env_has_inited(drv->location)) |
191 | continue; | |
192 | ||
3574ba01 | 193 | printf("Loading Environment from %s... ", drv->name); |
8a3a7e22 | 194 | ret = drv->load(); |
3574ba01 MR |
195 | if (ret) |
196 | printf("Failed (%d)\n", ret); | |
197 | else | |
198 | printf("OK\n"); | |
199 | ||
8a3a7e22 MR |
200 | if (!ret) |
201 | return 0; | |
c9d728dd SG |
202 | } |
203 | ||
8a3a7e22 | 204 | return -ENODEV; |
c9d728dd SG |
205 | } |
206 | ||
207 | int env_save(void) | |
208 | { | |
8a3a7e22 MR |
209 | struct env_driver *drv; |
210 | int prio; | |
c9d728dd | 211 | |
8a3a7e22 MR |
212 | for (prio = 0; (drv = env_driver_lookup(ENVOP_SAVE, prio)); prio++) { |
213 | int ret; | |
214 | ||
215 | if (!drv->save) | |
216 | continue; | |
217 | ||
1d446087 MR |
218 | if (!env_has_inited(drv->location)) |
219 | continue; | |
220 | ||
9efac3c8 | 221 | printf("Saving Environment to %s... ", drv->name); |
8a3a7e22 | 222 | ret = drv->save(); |
3574ba01 MR |
223 | if (ret) |
224 | printf("Failed (%d)\n", ret); | |
225 | else | |
226 | printf("OK\n"); | |
227 | ||
8a3a7e22 MR |
228 | if (!ret) |
229 | return 0; | |
c9d728dd SG |
230 | } |
231 | ||
8a3a7e22 | 232 | return -ENODEV; |
c9d728dd SG |
233 | } |
234 | ||
6eeae424 | 235 | int env_init(void) |
c9d728dd | 236 | { |
8a3a7e22 | 237 | struct env_driver *drv; |
7938822a | 238 | int ret = -ENOENT; |
8a3a7e22 MR |
239 | int prio; |
240 | ||
241 | for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) { | |
1d446087 MR |
242 | if (!drv->init || !(ret = drv->init())) |
243 | env_set_inited(drv->location); | |
8a3a7e22 | 244 | |
1d446087 | 245 | debug("%s: Environment %s init done (ret=%d)\n", __func__, |
8a3a7e22 MR |
246 | drv->name, ret); |
247 | } | |
248 | ||
249 | if (!prio) | |
250 | return -ENODEV; | |
251 | ||
7938822a SG |
252 | if (ret == -ENOENT) { |
253 | gd->env_addr = (ulong)&default_environment[0]; | |
eeba55cb | 254 | gd->env_valid = ENV_VALID; |
7938822a SG |
255 | |
256 | return 0; | |
c9d728dd SG |
257 | } |
258 | ||
8a3a7e22 | 259 | return ret; |
c9d728dd | 260 | } |