blob: 06078c7f3744178524211ae46873ffcf311a3b42 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass15e5a1a2017-08-03 12:22:00 -06002/*
3 * Copyright (C) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass15e5a1a2017-08-03 12:22:00 -06005 */
6
7#include <common.h>
Simon Glass79fd2142019-08-01 09:46:43 -06008#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -06009#include <env_internal.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060013#include <linux/bug.h>
Simon Glass15e5a1a2017-08-03 12:22:00 -060014
15DECLARE_GLOBAL_DATA_PTR;
16
Siva Durga Prasad Paladugu75c65792018-04-13 07:57:21 +020017#if defined(CONFIG_NEEDS_MANUAL_RELOC)
18void env_fix_drivers(void)
19{
20 struct env_driver *drv;
21 const int n_ents = ll_entry_count(struct env_driver, env_driver);
22 struct env_driver *entry;
23
24 drv = ll_entry_start(struct env_driver, env_driver);
25 for (entry = drv; entry != drv + n_ents; entry++) {
26 if (entry->name)
27 entry->name += gd->reloc_off;
28 if (entry->load)
29 entry->load += gd->reloc_off;
30 if (entry->save)
31 entry->save += gd->reloc_off;
Frank Wunderlich33afa932019-06-29 11:36:19 +020032 if (entry->erase)
33 entry->erase += gd->reloc_off;
Siva Durga Prasad Paladugu75c65792018-04-13 07:57:21 +020034 if (entry->init)
35 entry->init += gd->reloc_off;
36 }
37}
38#endif
39
Maxime Ripardb7da89a2018-01-23 21:16:51 +010040static struct env_driver *_env_driver_lookup(enum env_location loc)
Simon Glass15e5a1a2017-08-03 12:22:00 -060041{
42 struct env_driver *drv;
43 const int n_ents = ll_entry_count(struct env_driver, env_driver);
44 struct env_driver *entry;
45
46 drv = ll_entry_start(struct env_driver, env_driver);
47 for (entry = drv; entry != drv + n_ents; entry++) {
48 if (loc == entry->location)
49 return entry;
50 }
51
52 /* Not found */
53 return NULL;
54}
55
Maxime Ripard520cf802018-01-23 21:16:58 +010056static enum env_location env_locations[] = {
57#ifdef CONFIG_ENV_IS_IN_EEPROM
58 ENVL_EEPROM,
59#endif
60#ifdef CONFIG_ENV_IS_IN_EXT4
61 ENVL_EXT4,
62#endif
63#ifdef CONFIG_ENV_IS_IN_FAT
64 ENVL_FAT,
65#endif
66#ifdef CONFIG_ENV_IS_IN_FLASH
67 ENVL_FLASH,
68#endif
69#ifdef CONFIG_ENV_IS_IN_MMC
70 ENVL_MMC,
71#endif
72#ifdef CONFIG_ENV_IS_IN_NAND
73 ENVL_NAND,
74#endif
75#ifdef CONFIG_ENV_IS_IN_NVRAM
76 ENVL_NVRAM,
77#endif
78#ifdef CONFIG_ENV_IS_IN_REMOTE
79 ENVL_REMOTE,
80#endif
Ye Lia44849a2019-01-04 09:34:24 +000081#ifdef CONFIG_ENV_IS_IN_SATA
82 ENVL_ESATA,
83#endif
Maxime Ripard520cf802018-01-23 21:16:58 +010084#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
85 ENVL_SPI_FLASH,
86#endif
87#ifdef CONFIG_ENV_IS_IN_UBI
88 ENVL_UBI,
89#endif
90#ifdef CONFIG_ENV_IS_NOWHERE
91 ENVL_NOWHERE,
92#endif
93};
94
Maxime Ripard2131c5e2018-01-23 21:16:59 +010095static bool env_has_inited(enum env_location location)
96{
97 return gd->env_has_init & BIT(location);
98}
99
100static void env_set_inited(enum env_location location)
101{
102 /*
103 * We're using a 32-bits bitmask stored in gd (env_has_init)
104 * using the above enum value as the bit index. We need to
105 * make sure that we're not overflowing it.
106 */
Patrick Delaunay13ac85a2020-06-24 09:27:25 +0200107 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100108
109 gd->env_has_init |= BIT(location);
110}
111
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100112/**
Marek Vasutf92cf582022-04-06 02:21:32 +0200113 * arch_env_get_location() - Returns the best env location for an arch
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100114 * @op: operations performed on the environment
115 * @prio: priority between the multiple environments, 0 being the
116 * highest priority
117 *
118 * This will return the preferred environment for the given priority.
Marek Vasutf92cf582022-04-06 02:21:32 +0200119 * This is overridable by architectures if they need to and has lower
120 * priority than board side env_get_location() override.
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100121 *
122 * All implementations are free to use the operation, the priority and
123 * any other data relevant to their choice, but must take into account
124 * the fact that the lowest prority (0) is the most important location
125 * in the system. The following locations should be returned by order
126 * of descending priorities, from the highest to the lowest priority.
127 *
128 * Returns:
129 * an enum env_location value on success, a negative error code otherwise
130 */
Marek Vasutf92cf582022-04-06 02:21:32 +0200131__weak enum env_location arch_env_get_location(enum env_operation op, int prio)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600132{
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200133 if (prio >= ARRAY_SIZE(env_locations))
134 return ENVL_UNKNOWN;
Maxime Ripard520cf802018-01-23 21:16:58 +0100135
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200136 return env_locations[prio];
Simon Glass15e5a1a2017-08-03 12:22:00 -0600137}
138
Marek Vasutf92cf582022-04-06 02:21:32 +0200139/**
140 * env_get_location() - Returns the best env location for a board
141 * @op: operations performed on the environment
142 * @prio: priority between the multiple environments, 0 being the
143 * highest priority
144 *
145 * This will return the preferred environment for the given priority.
146 * This is overridable by boards if they need to.
147 *
148 * All implementations are free to use the operation, the priority and
149 * any other data relevant to their choice, but must take into account
150 * the fact that the lowest prority (0) is the most important location
151 * in the system. The following locations should be returned by order
152 * of descending priorities, from the highest to the lowest priority.
153 *
154 * Returns:
155 * an enum env_location value on success, a negative error code otherwise
156 */
157__weak enum env_location env_get_location(enum env_operation op, int prio)
158{
159 return arch_env_get_location(op, prio);
160}
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100161
162/**
163 * env_driver_lookup() - Finds the most suited environment location
164 * @op: operations performed on the environment
165 * @prio: priority between the multiple environments, 0 being the
166 * highest priority
167 *
168 * This will try to find the available environment with the highest
169 * priority in the system.
170 *
171 * Returns:
172 * NULL on error, a pointer to a struct env_driver otherwise
173 */
174static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600175{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100176 enum env_location loc = env_get_location(op, prio);
Simon Glass15e5a1a2017-08-03 12:22:00 -0600177 struct env_driver *drv;
178
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100179 if (loc == ENVL_UNKNOWN)
180 return NULL;
181
Maxime Ripardb7da89a2018-01-23 21:16:51 +0100182 drv = _env_driver_lookup(loc);
Simon Glass15e5a1a2017-08-03 12:22:00 -0600183 if (!drv) {
184 debug("%s: No environment driver for location %d\n", __func__,
185 loc);
186 return NULL;
187 }
188
189 return drv;
190}
191
Simon Glass15e5a1a2017-08-03 12:22:00 -0600192int env_load(void)
193{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100194 struct env_driver *drv;
Sam Protsenko239f3122019-01-18 21:19:04 +0200195 int best_prio = -1;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100196 int prio;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600197
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100198 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
199 int ret;
200
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100201 if (!env_has_inited(drv->location))
202 continue;
203
Maxime Ripard185402a2018-01-23 21:16:54 +0100204 printf("Loading Environment from %s... ", drv->name);
Sam Protsenko051dfd82018-07-30 19:19:26 +0300205 /*
206 * In error case, the error message must be printed during
207 * drv->load() in some underlying API, and it must be exactly
208 * one message.
209 */
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100210 ret = drv->load();
Sam Protsenko239f3122019-01-18 21:19:04 +0200211 if (!ret) {
Maxime Ripard185402a2018-01-23 21:16:54 +0100212 printf("OK\n");
Patrick Delaunaya639f4e2020-07-28 11:51:17 +0200213 gd->env_load_prio = prio;
214
Marek Vasuta7b82672020-07-07 20:51:38 +0200215#if !CONFIG_IS_ENABLED(ENV_APPEND)
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100216 return 0;
Marek Vasuta7b82672020-07-07 20:51:38 +0200217#endif
Sam Protsenko239f3122019-01-18 21:19:04 +0200218 } else if (ret == -ENOMSG) {
219 /* Handle "bad CRC" case */
220 if (best_prio == -1)
221 best_prio = prio;
222 } else {
223 debug("Failed (%d)\n", ret);
Sam Protsenko051dfd82018-07-30 19:19:26 +0300224 }
Simon Glass15e5a1a2017-08-03 12:22:00 -0600225 }
226
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200227 /*
228 * In case of invalid environment, we set the 'default' env location
Sam Protsenko239f3122019-01-18 21:19:04 +0200229 * to the best choice, i.e.:
230 * 1. Environment location with bad CRC, if such location was found
231 * 2. Otherwise use the location with highest priority
232 *
233 * This way, next calls to env_save() will restore the environment
234 * at the right place.
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200235 */
Sam Protsenko239f3122019-01-18 21:19:04 +0200236 if (best_prio >= 0)
237 debug("Selecting environment with bad CRC\n");
238 else
239 best_prio = 0;
Patrick Delaunaya639f4e2020-07-28 11:51:17 +0200240
241 gd->env_load_prio = best_prio;
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200242
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100243 return -ENODEV;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600244}
245
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200246int env_reload(void)
247{
248 struct env_driver *drv;
249
250 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
251 if (drv) {
252 int ret;
253
254 printf("Loading Environment from %s... ", drv->name);
255
256 if (!env_has_inited(drv->location)) {
257 printf("not initialized\n");
258 return -ENODEV;
259 }
260
261 ret = drv->load();
262 if (ret)
263 printf("Failed (%d)\n", ret);
264 else
265 printf("OK\n");
266
267 if (!ret)
268 return 0;
269 }
270
271 return -ENODEV;
272}
273
Simon Glass15e5a1a2017-08-03 12:22:00 -0600274int env_save(void)
275{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100276 struct env_driver *drv;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600277
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200278 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
279 if (drv) {
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100280 int ret;
Maxime Ripard3c0b2e52018-01-23 21:16:50 +0100281
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200282 printf("Saving Environment to %s... ", drv->name);
283 if (!drv->save) {
284 printf("not possible\n");
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200285 return -ENODEV;
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200286 }
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100287
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200288 if (!env_has_inited(drv->location)) {
289 printf("not initialized\n");
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200290 return -ENODEV;
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200291 }
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100292
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100293 ret = drv->save();
Maxime Ripard185402a2018-01-23 21:16:54 +0100294 if (ret)
295 printf("Failed (%d)\n", ret);
296 else
297 printf("OK\n");
298
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100299 if (!ret)
300 return 0;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600301 }
302
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100303 return -ENODEV;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600304}
305
Frank Wunderlich33afa932019-06-29 11:36:19 +0200306int env_erase(void)
307{
308 struct env_driver *drv;
309
310 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
311 if (drv) {
312 int ret;
313
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100314 if (!drv->erase) {
315 printf("not possible\n");
Frank Wunderlich33afa932019-06-29 11:36:19 +0200316 return -ENODEV;
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100317 }
Frank Wunderlich33afa932019-06-29 11:36:19 +0200318
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100319 if (!env_has_inited(drv->location)) {
320 printf("not initialized\n");
Frank Wunderlich33afa932019-06-29 11:36:19 +0200321 return -ENODEV;
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100322 }
Frank Wunderlich33afa932019-06-29 11:36:19 +0200323
324 printf("Erasing Environment on %s... ", drv->name);
325 ret = drv->erase();
326 if (ret)
327 printf("Failed (%d)\n", ret);
328 else
329 printf("OK\n");
330
331 if (!ret)
332 return 0;
333 }
334
335 return -ENODEV;
336}
337
Simon Glass36d85812017-08-03 12:22:05 -0600338int env_init(void)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600339{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100340 struct env_driver *drv;
Simon Glassd40d8042017-08-03 12:22:02 -0600341 int ret = -ENOENT;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100342 int prio;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600343
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100344 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100345 if (!drv->init || !(ret = drv->init()))
346 env_set_inited(drv->location);
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100347 if (ret == -ENOENT)
348 env_set_inited(drv->location);
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100349
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100350 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100351 drv->name, ret);
Felix.Vietmeyer@jila.colorado.edud0f91062021-04-20 20:04:26 -0600352
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200353 if (gd->env_valid == ENV_INVALID)
354 ret = -ENOENT;
Felix.Vietmeyer@jila.colorado.edud0f91062021-04-20 20:04:26 -0600355 }
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100356
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200357 if (!prio)
358 return -ENODEV;
359
Simon Glassd40d8042017-08-03 12:22:02 -0600360 if (ret == -ENOENT) {
361 gd->env_addr = (ulong)&default_environment[0];
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200362 gd->env_valid = ENV_VALID;
Simon Glassd40d8042017-08-03 12:22:02 -0600363
364 return 0;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600365 }
366
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100367 return ret;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600368}
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +0200369
370int env_select(const char *name)
371{
372 struct env_driver *drv;
373 const int n_ents = ll_entry_count(struct env_driver, env_driver);
374 struct env_driver *entry;
375 int prio;
376 bool found = false;
377
378 printf("Select Environment on %s: ", name);
379
380 /* search ENV driver by name */
381 drv = ll_entry_start(struct env_driver, env_driver);
382 for (entry = drv; entry != drv + n_ents; entry++) {
383 if (!strcmp(entry->name, name)) {
384 found = true;
385 break;
386 }
387 }
388
389 if (!found) {
390 printf("driver not found\n");
391 return -ENODEV;
392 }
393
394 /* search priority by driver */
395 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
396 if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
397 /* when priority change, reset the ENV flags */
398 if (gd->env_load_prio != prio) {
399 gd->env_load_prio = prio;
400 gd->env_valid = ENV_INVALID;
401 gd->flags &= ~GD_FLG_ENV_DEFAULT;
402 }
403 printf("OK\n");
404 return 0;
405 }
406 }
407 printf("priority not found\n");
408
409 return -ENODEV;
410}