• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <env.h>
9 #include <env_internal.h>
10 
11 DECLARE_GLOBAL_DATA_PTR;
12 
13 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
env_fix_drivers(void)14 void env_fix_drivers(void)
15 {
16 	struct env_driver *drv;
17 	const int n_ents = ll_entry_count(struct env_driver, env_driver);
18 	struct env_driver *entry;
19 
20 	drv = ll_entry_start(struct env_driver, env_driver);
21 	for (entry = drv; entry != drv + n_ents; entry++) {
22 		if (entry->name)
23 			entry->name += gd->reloc_off;
24 		if (entry->load)
25 			entry->load += gd->reloc_off;
26 		if (entry->save)
27 			entry->save += gd->reloc_off;
28 		if (entry->erase)
29 			entry->erase += gd->reloc_off;
30 		if (entry->init)
31 			entry->init += gd->reloc_off;
32 	}
33 }
34 #endif
35 
_env_driver_lookup(enum env_location loc)36 static struct env_driver *_env_driver_lookup(enum env_location loc)
37 {
38 	struct env_driver *drv;
39 	const int n_ents = ll_entry_count(struct env_driver, env_driver);
40 	struct env_driver *entry;
41 
42 	drv = ll_entry_start(struct env_driver, env_driver);
43 	for (entry = drv; entry != drv + n_ents; entry++) {
44 		if (loc == entry->location)
45 			return entry;
46 	}
47 
48 	/* Not found */
49 	return NULL;
50 }
51 
52 static enum env_location env_locations[] = {
53 #ifdef CONFIG_ENV_IS_IN_EEPROM
54 	ENVL_EEPROM,
55 #endif
56 #ifdef CONFIG_ENV_IS_IN_EXT4
57 	ENVL_EXT4,
58 #endif
59 #ifdef CONFIG_ENV_IS_IN_FAT
60 	ENVL_FAT,
61 #endif
62 #ifdef CONFIG_ENV_IS_IN_FLASH
63 	ENVL_FLASH,
64 #endif
65 #ifdef CONFIG_ENV_IS_IN_MMC
66 	ENVL_MMC,
67 #endif
68 /* Prevents the environment variables from being saved to the
69  * spinand when the system boots from the SPI NOR flash.
70  */
71 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
72 	ENVL_SPI_FLASH,
73 #endif
74 #ifdef CONFIG_ENV_IS_IN_NAND
75 	ENVL_NAND,
76 #endif
77 #ifdef CONFIG_ENV_IS_IN_NVRAM
78 	ENVL_NVRAM,
79 #endif
80 #ifdef CONFIG_ENV_IS_IN_REMOTE
81 	ENVL_REMOTE,
82 #endif
83 #ifdef CONFIG_ENV_IS_IN_SATA
84 	ENVL_ESATA,
85 #endif
86 #ifdef CONFIG_ENV_IS_IN_UBI
87 	ENVL_UBI,
88 #endif
89 #ifdef CONFIG_ENV_IS_IN_UFS
90 	ENVL_UFS,
91 #endif
92 #ifdef CONFIG_ENV_IS_NOWHERE
93 	ENVL_NOWHERE,
94 #endif
95 };
96 
env_has_inited(enum env_location location)97 static bool env_has_inited(enum env_location location)
98 {
99 	return gd->env_has_init & BIT(location);
100 }
101 
env_set_inited(enum env_location location)102 static void env_set_inited(enum env_location location)
103 {
104 	/*
105 	 * We're using a 32-bits bitmask stored in gd (env_has_init)
106 	 * using the above enum value as the bit index. We need to
107 	 * make sure that we're not overflowing it.
108 	 */
109 	BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG);
110 
111 	gd->env_has_init |= BIT(location);
112 }
113 
114 /**
115  * env_get_location() - Returns the best env location for a board
116  * @op: operations performed on the environment
117  * @prio: priority between the multiple environments, 0 being the
118  *        highest priority
119  *
120  * This will return the preferred environment for the given priority.
121  * This is overridable by boards if they need to.
122  *
123  * All implementations are free to use the operation, the priority and
124  * any other data relevant to their choice, but must take into account
125  * the fact that the lowest prority (0) is the most important location
126  * in the system. The following locations should be returned by order
127  * of descending priorities, from the highest to the lowest priority.
128  *
129  * Returns:
130  * an enum env_location value on success, a negative error code otherwise
131  */
env_get_location(enum env_operation op,int prio)132 __weak enum env_location env_get_location(enum env_operation op, int prio)
133 {
134 	if (prio >= ARRAY_SIZE(env_locations))
135 		return ENVL_UNKNOWN;
136 
137 	gd->env_load_prio = prio;
138 
139 	return env_locations[prio];
140 }
141 
142 
143 /**
144  * env_driver_lookup() - Finds the most suited environment location
145  * @op: operations performed on the environment
146  * @prio: priority between the multiple environments, 0 being the
147  *        highest priority
148  *
149  * This will try to find the available environment with the highest
150  * priority in the system.
151  *
152  * Returns:
153  * NULL on error, a pointer to a struct env_driver otherwise
154  */
env_driver_lookup(enum env_operation op,int prio)155 static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
156 {
157 	enum env_location loc = env_get_location(op, prio);
158 	struct env_driver *drv;
159 
160 	if (loc == ENVL_UNKNOWN)
161 		return NULL;
162 
163 	drv = _env_driver_lookup(loc);
164 	if (!drv) {
165 		debug("%s: No environment driver for location %d\n", __func__,
166 		      loc);
167 		return NULL;
168 	}
169 
170 	return drv;
171 }
172 
env_get_char_spec(int index)173 __weak int env_get_char_spec(int index)
174 {
175 	return *(uchar *)(gd->env_addr + index);
176 }
177 
env_get_char(int index)178 int env_get_char(int index)
179 {
180 	if (gd->env_valid == ENV_INVALID)
181 		return default_environment[index];
182 	else
183 		return env_get_char_spec(index);
184 }
185 
env_load(void)186 int env_load(void)
187 {
188 	struct env_driver *drv;
189 	int best_prio = -1;
190 	int prio;
191 
192 	for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
193 		int ret;
194 
195 		if (!drv->load)
196 			continue;
197 
198 		if (!env_has_inited(drv->location))
199 			continue;
200 
201 		printf("Loading Environment from %s... ", drv->name);
202 		/*
203 		 * In error case, the error message must be printed during
204 		 * drv->load() in some underlying API, and it must be exactly
205 		 * one message.
206 		 */
207 		ret = drv->load();
208 		if (!ret) {
209 			printf("OK\n");
210 			return 0;
211 		} else if (ret == -ENOMSG) {
212 			/* Handle "bad CRC" case */
213 			if (best_prio == -1)
214 				best_prio = prio;
215 		} else {
216 			debug("Failed (%d)\n", ret);
217 		}
218 	}
219 
220 	/*
221 	 * In case of invalid environment, we set the 'default' env location
222 	 * to the best choice, i.e.:
223 	 *   1. Environment location with bad CRC, if such location was found
224 	 *   2. Otherwise use the location with highest priority
225 	 *
226 	 * This way, next calls to env_save() will restore the environment
227 	 * at the right place.
228 	 */
229 	if (best_prio >= 0)
230 		debug("Selecting environment with bad CRC\n");
231 	else
232 		best_prio = 0;
233 	env_get_location(ENVOP_LOAD, best_prio);
234 
235 	return -ENODEV;
236 }
237 
env_save(void)238 int env_save(void)
239 {
240 	struct env_driver *drv;
241 
242 	drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
243 	if (drv) {
244 		int ret;
245 
246 		if (!drv->save)
247 			return -ENODEV;
248 
249 		if (!env_has_inited(drv->location))
250 			return -ENODEV;
251 
252 		printf("Saving Environment to %s... ", drv->name);
253 		ret = drv->save();
254 		if (ret)
255 			printf("Failed (%d)\n", ret);
256 		else
257 			printf("OK\n");
258 
259 		if (!ret)
260 			return 0;
261 	}
262 
263 	return -ENODEV;
264 }
265 
env_erase(void)266 int env_erase(void)
267 {
268 	struct env_driver *drv;
269 
270 	drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
271 	if (drv) {
272 		int ret;
273 
274 		if (!drv->erase)
275 			return -ENODEV;
276 
277 		if (!env_has_inited(drv->location))
278 			return -ENODEV;
279 
280 		printf("Erasing Environment on %s... ", drv->name);
281 		ret = drv->erase();
282 		if (ret)
283 			printf("Failed (%d)\n", ret);
284 		else
285 			printf("OK\n");
286 
287 		if (!ret)
288 			return 0;
289 	}
290 
291 	return -ENODEV;
292 }
293 
env_init(void)294 int env_init(void)
295 {
296 	struct env_driver *drv;
297 	int ret = -ENOENT;
298 	int prio;
299 
300 	for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
301 		if (!drv->init || !(ret = drv->init()))
302 			env_set_inited(drv->location);
303 
304 		debug("%s: Environment %s init done (ret=%d)\n", __func__,
305 		      drv->name, ret);
306 	}
307 
308 	if (!prio)
309 		return -ENODEV;
310 
311 	if (ret == -ENOENT) {
312 		gd->env_addr = (ulong)&default_environment[0];
313 		gd->env_valid = ENV_VALID;
314 
315 		return 0;
316 	}
317 
318 	return ret;
319 }
320