blob: 42c7d8155e1a5ffebf2663399cbd8eabaca29b10 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc9d728d2017-08-03 12:22:00 -06002/*
3 * Copyright (C) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassc9d728d2017-08-03 12:22:00 -06005 */
6
7#include <common.h>
Simon Glass4bfd1f52019-08-01 09:46:43 -06008#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -06009#include <env_internal.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glasscd93d622020-05-10 11:40:13 -060011#include <linux/bitops.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060012#include <linux/bug.h>
Simon Glassc9d728d2017-08-03 12:22:00 -060013
14DECLARE_GLOBAL_DATA_PTR;
15
Siva Durga Prasad Paladugu7bcdf192018-04-13 07:57:21 +020016#if defined(CONFIG_NEEDS_MANUAL_RELOC)
17void env_fix_drivers(void)
18{
19 struct env_driver *drv;
20 const int n_ents = ll_entry_count(struct env_driver, env_driver);
21 struct env_driver *entry;
22
23 drv = ll_entry_start(struct env_driver, env_driver);
24 for (entry = drv; entry != drv + n_ents; entry++) {
25 if (entry->name)
26 entry->name += gd->reloc_off;
27 if (entry->load)
28 entry->load += gd->reloc_off;
29 if (entry->save)
30 entry->save += gd->reloc_off;
Frank Wunderlichcd121bd2019-06-29 11:36:19 +020031 if (entry->erase)
32 entry->erase += gd->reloc_off;
Siva Durga Prasad Paladugu7bcdf192018-04-13 07:57:21 +020033 if (entry->init)
34 entry->init += gd->reloc_off;
35 }
36}
37#endif
38
Maxime Ripard52746c42018-01-23 21:16:51 +010039static struct env_driver *_env_driver_lookup(enum env_location loc)
Simon Glassc9d728d2017-08-03 12:22:00 -060040{
41 struct env_driver *drv;
42 const int n_ents = ll_entry_count(struct env_driver, env_driver);
43 struct env_driver *entry;
44
45 drv = ll_entry_start(struct env_driver, env_driver);
46 for (entry = drv; entry != drv + n_ents; entry++) {
47 if (loc == entry->location)
48 return entry;
49 }
50
51 /* Not found */
52 return NULL;
53}
54
Maxime Ripard7d714a22018-01-23 21:16:58 +010055static enum env_location env_locations[] = {
56#ifdef CONFIG_ENV_IS_IN_EEPROM
57 ENVL_EEPROM,
58#endif
59#ifdef CONFIG_ENV_IS_IN_EXT4
60 ENVL_EXT4,
61#endif
62#ifdef CONFIG_ENV_IS_IN_FAT
63 ENVL_FAT,
64#endif
65#ifdef CONFIG_ENV_IS_IN_FLASH
66 ENVL_FLASH,
67#endif
68#ifdef CONFIG_ENV_IS_IN_MMC
69 ENVL_MMC,
70#endif
71#ifdef CONFIG_ENV_IS_IN_NAND
72 ENVL_NAND,
73#endif
74#ifdef CONFIG_ENV_IS_IN_NVRAM
75 ENVL_NVRAM,
76#endif
77#ifdef CONFIG_ENV_IS_IN_REMOTE
78 ENVL_REMOTE,
79#endif
Ye Li9a6a3112019-01-04 09:34:24 +000080#ifdef CONFIG_ENV_IS_IN_SATA
81 ENVL_ESATA,
82#endif
Maxime Ripard7d714a22018-01-23 21:16:58 +010083#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
84 ENVL_SPI_FLASH,
85#endif
86#ifdef CONFIG_ENV_IS_IN_UBI
87 ENVL_UBI,
88#endif
89#ifdef CONFIG_ENV_IS_NOWHERE
90 ENVL_NOWHERE,
91#endif
92};
93
Maxime Ripard1d446082018-01-23 21:16:59 +010094static bool env_has_inited(enum env_location location)
95{
96 return gd->env_has_init & BIT(location);
97}
98
99static void env_set_inited(enum env_location location)
100{
101 /*
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.
105 */
Patrick Delaunayd5a6a5a2020-06-24 09:27:25 +0200106 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
Maxime Ripard1d446082018-01-23 21:16:59 +0100107
108 gd->env_has_init |= BIT(location);
109}
110
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100111/**
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
115 * highest priority
116 *
117 * This will return the preferred environment for the given priority.
Maxime Ripard40c08a62018-01-23 21:17:02 +0100118 * This is overridable by boards if they need to.
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100119 *
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.
125 *
126 * Returns:
127 * an enum env_location value on success, a negative error code otherwise
128 */
Maxime Ripard40c08a62018-01-23 21:17:02 +0100129__weak enum env_location env_get_location(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600130{
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200131 if (prio >= ARRAY_SIZE(env_locations))
132 return ENVL_UNKNOWN;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100133
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200134 return env_locations[prio];
Simon Glassc9d728d2017-08-03 12:22:00 -0600135}
136
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100137
138/**
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
142 * highest priority
143 *
144 * This will try to find the available environment with the highest
145 * priority in the system.
146 *
147 * Returns:
148 * NULL on error, a pointer to a struct env_driver otherwise
149 */
150static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600151{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100152 enum env_location loc = env_get_location(op, prio);
Simon Glassc9d728d2017-08-03 12:22:00 -0600153 struct env_driver *drv;
154
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100155 if (loc == ENVL_UNKNOWN)
156 return NULL;
157
Maxime Ripard52746c42018-01-23 21:16:51 +0100158 drv = _env_driver_lookup(loc);
Simon Glassc9d728d2017-08-03 12:22:00 -0600159 if (!drv) {
160 debug("%s: No environment driver for location %d\n", __func__,
161 loc);
162 return NULL;
163 }
164
165 return drv;
166}
167
Goldschmidt Simonb2cdef42018-02-09 20:23:17 +0000168__weak int env_get_char_spec(int index)
169{
170 return *(uchar *)(gd->env_addr + index);
171}
172
Simon Glassa69d0f62017-08-03 12:22:06 -0600173int env_get_char(int index)
Simon Glassc9d728d2017-08-03 12:22:00 -0600174{
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600175 if (gd->env_valid == ENV_INVALID)
Simon Glassa69d0f62017-08-03 12:22:06 -0600176 return default_environment[index];
Goldschmidt Simonb2cdef42018-02-09 20:23:17 +0000177 else
178 return env_get_char_spec(index);
Simon Glassc9d728d2017-08-03 12:22:00 -0600179}
180
181int env_load(void)
182{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100183 struct env_driver *drv;
Sam Protsenko293a1722019-01-18 21:19:04 +0200184 int best_prio = -1;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100185 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600186
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100187 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
188 int ret;
189
Maxime Ripard1d446082018-01-23 21:16:59 +0100190 if (!env_has_inited(drv->location))
191 continue;
192
Maxime Ripard3574ba02018-01-23 21:16:54 +0100193 printf("Loading Environment from %s... ", drv->name);
Sam Protsenko13bbfb42018-07-30 19:19:26 +0300194 /*
195 * In error case, the error message must be printed during
196 * drv->load() in some underlying API, and it must be exactly
197 * one message.
198 */
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100199 ret = drv->load();
Sam Protsenko293a1722019-01-18 21:19:04 +0200200 if (!ret) {
Maxime Ripard3574ba02018-01-23 21:16:54 +0100201 printf("OK\n");
Patrick Delaunay6d8d8402020-07-28 11:51:17 +0200202 gd->env_load_prio = prio;
203
Marek Vasut47f3b1f2020-07-07 20:51:38 +0200204#if !CONFIG_IS_ENABLED(ENV_APPEND)
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100205 return 0;
Marek Vasut47f3b1f2020-07-07 20:51:38 +0200206#endif
Sam Protsenko293a1722019-01-18 21:19:04 +0200207 } else if (ret == -ENOMSG) {
208 /* Handle "bad CRC" case */
209 if (best_prio == -1)
210 best_prio = prio;
211 } else {
212 debug("Failed (%d)\n", ret);
Sam Protsenko13bbfb42018-07-30 19:19:26 +0300213 }
Simon Glassc9d728d2017-08-03 12:22:00 -0600214 }
215
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200216 /*
217 * In case of invalid environment, we set the 'default' env location
Sam Protsenko293a1722019-01-18 21:19:04 +0200218 * 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
221 *
222 * This way, next calls to env_save() will restore the environment
223 * at the right place.
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200224 */
Sam Protsenko293a1722019-01-18 21:19:04 +0200225 if (best_prio >= 0)
226 debug("Selecting environment with bad CRC\n");
227 else
228 best_prio = 0;
Patrick Delaunay6d8d8402020-07-28 11:51:17 +0200229
230 gd->env_load_prio = best_prio;
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200231
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100232 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600233}
234
Patrick Delaunay0115dd32020-07-28 11:51:20 +0200235int env_reload(void)
236{
237 struct env_driver *drv;
238
239 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
240 if (drv) {
241 int ret;
242
243 printf("Loading Environment from %s... ", drv->name);
244
245 if (!env_has_inited(drv->location)) {
246 printf("not initialized\n");
247 return -ENODEV;
248 }
249
250 ret = drv->load();
251 if (ret)
252 printf("Failed (%d)\n", ret);
253 else
254 printf("OK\n");
255
256 if (!ret)
257 return 0;
258 }
259
260 return -ENODEV;
261}
262
Simon Glassc9d728d2017-08-03 12:22:00 -0600263int env_save(void)
264{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100265 struct env_driver *drv;
Simon Glassc9d728d2017-08-03 12:22:00 -0600266
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200267 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
268 if (drv) {
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100269 int ret;
Maxime Ripard9c24dfb2018-01-23 21:16:50 +0100270
Maxime Ripard9efac3c2018-01-23 21:16:53 +0100271 printf("Saving Environment to %s... ", drv->name);
Patrick Delaunay89682882020-06-24 10:17:50 +0200272 if (!drv->save) {
273 printf("not possible\n");
274 return -ENODEV;
275 }
276
277 if (!env_has_inited(drv->location)) {
278 printf("not initialized\n");
279 return -ENODEV;
280 }
281
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100282 ret = drv->save();
Maxime Ripard3574ba02018-01-23 21:16:54 +0100283 if (ret)
284 printf("Failed (%d)\n", ret);
285 else
286 printf("OK\n");
287
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100288 if (!ret)
289 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600290 }
291
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100292 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600293}
294
Frank Wunderlichcd121bd2019-06-29 11:36:19 +0200295int env_erase(void)
296{
297 struct env_driver *drv;
298
299 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
300 if (drv) {
301 int ret;
302
303 if (!drv->erase)
304 return -ENODEV;
305
306 if (!env_has_inited(drv->location))
307 return -ENODEV;
308
309 printf("Erasing Environment on %s... ", drv->name);
310 ret = drv->erase();
311 if (ret)
312 printf("Failed (%d)\n", ret);
313 else
314 printf("OK\n");
315
316 if (!ret)
317 return 0;
318 }
319
320 return -ENODEV;
321}
322
Simon Glass6eeae422017-08-03 12:22:05 -0600323int env_init(void)
Simon Glassc9d728d2017-08-03 12:22:00 -0600324{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100325 struct env_driver *drv;
Simon Glass79388222017-08-03 12:22:02 -0600326 int ret = -ENOENT;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100327 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600328
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100329 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard1d446082018-01-23 21:16:59 +0100330 if (!drv->init || !(ret = drv->init()))
331 env_set_inited(drv->location);
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100332
Maxime Ripard1d446082018-01-23 21:16:59 +0100333 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100334 drv->name, ret);
335 }
336
337 if (!prio)
338 return -ENODEV;
339
Simon Glass79388222017-08-03 12:22:02 -0600340 if (ret == -ENOENT) {
341 gd->env_addr = (ulong)&default_environment[0];
Tom Rinieeba55c2017-08-19 22:27:57 -0400342 gd->env_valid = ENV_VALID;
Simon Glass79388222017-08-03 12:22:02 -0600343
344 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600345 }
346
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100347 return ret;
Simon Glassc9d728d2017-08-03 12:22:00 -0600348}
Patrick Delaunaya97d22e2020-07-28 11:51:21 +0200349
350int env_select(const char *name)
351{
352 struct env_driver *drv;
353 const int n_ents = ll_entry_count(struct env_driver, env_driver);
354 struct env_driver *entry;
355 int prio;
356 bool found = false;
357
358 printf("Select Environment on %s: ", name);
359
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)) {
364 found = true;
365 break;
366 }
367 }
368
369 if (!found) {
370 printf("driver not found\n");
371 return -ENODEV;
372 }
373
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;
382 }
383 printf("OK\n");
384 return 0;
385 }
386 }
387 printf("priority not found\n");
388
389 return -ENODEV;
390}