blob: dbaeedc3c3b7f174bf66ed1fcb49fdc8cc57a7d0 [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
Simon Glass79fd2142019-08-01 09:46:43 -06007#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -06008#include <env_internal.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060012#include <linux/bug.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060013#include <linux/errno.h>
Simon Glass15e5a1a2017-08-03 12:22:00 -060014
15DECLARE_GLOBAL_DATA_PTR;
16
Maxime Ripardb7da89a2018-01-23 21:16:51 +010017static struct env_driver *_env_driver_lookup(enum env_location loc)
Simon Glass15e5a1a2017-08-03 12:22:00 -060018{
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 (loc == entry->location)
26 return entry;
27 }
28
29 /* Not found */
30 return NULL;
31}
32
Maxime Ripard520cf802018-01-23 21:16:58 +010033static enum env_location env_locations[] = {
34#ifdef CONFIG_ENV_IS_IN_EEPROM
35 ENVL_EEPROM,
36#endif
37#ifdef CONFIG_ENV_IS_IN_EXT4
38 ENVL_EXT4,
39#endif
40#ifdef CONFIG_ENV_IS_IN_FAT
41 ENVL_FAT,
42#endif
43#ifdef CONFIG_ENV_IS_IN_FLASH
44 ENVL_FLASH,
45#endif
46#ifdef CONFIG_ENV_IS_IN_MMC
47 ENVL_MMC,
48#endif
49#ifdef CONFIG_ENV_IS_IN_NAND
50 ENVL_NAND,
51#endif
52#ifdef CONFIG_ENV_IS_IN_NVRAM
53 ENVL_NVRAM,
54#endif
55#ifdef CONFIG_ENV_IS_IN_REMOTE
56 ENVL_REMOTE,
57#endif
58#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
59 ENVL_SPI_FLASH,
60#endif
Christian Marangif13466e2025-04-07 20:59:51 +020061#ifdef CONFIG_ENV_IS_IN_MTD
62 ENVL_MTD,
63#endif
Maxime Ripard520cf802018-01-23 21:16:58 +010064#ifdef CONFIG_ENV_IS_IN_UBI
65 ENVL_UBI,
66#endif
67#ifdef CONFIG_ENV_IS_NOWHERE
68 ENVL_NOWHERE,
69#endif
70};
71
Maxime Ripard2131c5e2018-01-23 21:16:59 +010072static bool env_has_inited(enum env_location location)
73{
74 return gd->env_has_init & BIT(location);
75}
76
77static void env_set_inited(enum env_location location)
78{
79 /*
80 * We're using a 32-bits bitmask stored in gd (env_has_init)
81 * using the above enum value as the bit index. We need to
82 * make sure that we're not overflowing it.
83 */
Patrick Delaunay13ac85a2020-06-24 09:27:25 +020084 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
Maxime Ripard2131c5e2018-01-23 21:16:59 +010085
86 gd->env_has_init |= BIT(location);
87}
88
Maxime Ripardfd3158e2018-01-23 21:16:52 +010089/**
Marek Vasutf92cf582022-04-06 02:21:32 +020090 * arch_env_get_location() - Returns the best env location for an arch
Maxime Ripardfd3158e2018-01-23 21:16:52 +010091 * @op: operations performed on the environment
92 * @prio: priority between the multiple environments, 0 being the
93 * highest priority
94 *
95 * This will return the preferred environment for the given priority.
Marek Vasutf92cf582022-04-06 02:21:32 +020096 * This is overridable by architectures if they need to and has lower
97 * priority than board side env_get_location() override.
Maxime Ripardfd3158e2018-01-23 21:16:52 +010098 *
99 * All implementations are free to use the operation, the priority and
100 * any other data relevant to their choice, but must take into account
101 * the fact that the lowest prority (0) is the most important location
102 * in the system. The following locations should be returned by order
103 * of descending priorities, from the highest to the lowest priority.
104 *
105 * Returns:
106 * an enum env_location value on success, a negative error code otherwise
107 */
Marek Vasutf92cf582022-04-06 02:21:32 +0200108__weak enum env_location arch_env_get_location(enum env_operation op, int prio)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600109{
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200110 if (prio >= ARRAY_SIZE(env_locations))
111 return ENVL_UNKNOWN;
Maxime Ripard520cf802018-01-23 21:16:58 +0100112
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200113 return env_locations[prio];
Simon Glass15e5a1a2017-08-03 12:22:00 -0600114}
115
Marek Vasutf92cf582022-04-06 02:21:32 +0200116/**
117 * env_get_location() - Returns the best env location for a board
118 * @op: operations performed on the environment
119 * @prio: priority between the multiple environments, 0 being the
120 * highest priority
121 *
122 * This will return the preferred environment for the given priority.
123 * This is overridable by boards if they need to.
124 *
125 * All implementations are free to use the operation, the priority and
126 * any other data relevant to their choice, but must take into account
127 * the fact that the lowest prority (0) is the most important location
128 * in the system. The following locations should be returned by order
129 * of descending priorities, from the highest to the lowest priority.
130 *
131 * Returns:
132 * an enum env_location value on success, a negative error code otherwise
133 */
134__weak enum env_location env_get_location(enum env_operation op, int prio)
135{
136 return arch_env_get_location(op, prio);
137}
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100138
139/**
140 * env_driver_lookup() - Finds the most suited environment location
141 * @op: operations performed on the environment
142 * @prio: priority between the multiple environments, 0 being the
143 * highest priority
144 *
145 * This will try to find the available environment with the highest
146 * priority in the system.
147 *
148 * Returns:
149 * NULL on error, a pointer to a struct env_driver otherwise
150 */
151static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600152{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100153 enum env_location loc = env_get_location(op, prio);
Simon Glass15e5a1a2017-08-03 12:22:00 -0600154 struct env_driver *drv;
155
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100156 if (loc == ENVL_UNKNOWN)
157 return NULL;
158
Maxime Ripardb7da89a2018-01-23 21:16:51 +0100159 drv = _env_driver_lookup(loc);
Simon Glass15e5a1a2017-08-03 12:22:00 -0600160 if (!drv) {
161 debug("%s: No environment driver for location %d\n", __func__,
162 loc);
163 return NULL;
164 }
165
166 return drv;
167}
168
Simon Glass15e5a1a2017-08-03 12:22:00 -0600169int env_load(void)
170{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100171 struct env_driver *drv;
Sam Protsenko239f3122019-01-18 21:19:04 +0200172 int best_prio = -1;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100173 int prio;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600174
Jan Kiszka755703b2023-02-03 13:22:51 +0100175 if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
176 /*
177 * When using a list of writeable variables, the baseline comes
178 * from the built-in default env. So load this first.
179 */
180 env_set_default(NULL, 0);
181 }
182
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100183 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
184 int ret;
185
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100186 if (!env_has_inited(drv->location))
187 continue;
188
Maxime Ripard185402a2018-01-23 21:16:54 +0100189 printf("Loading Environment from %s... ", drv->name);
Sam Protsenko051dfd82018-07-30 19:19:26 +0300190 /*
191 * In error case, the error message must be printed during
192 * drv->load() in some underlying API, and it must be exactly
193 * one message.
194 */
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100195 ret = drv->load();
Sam Protsenko239f3122019-01-18 21:19:04 +0200196 if (!ret) {
Maxime Ripard185402a2018-01-23 21:16:54 +0100197 printf("OK\n");
Patrick Delaunaya639f4e2020-07-28 11:51:17 +0200198 gd->env_load_prio = prio;
199
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100200 return 0;
Sam Protsenko239f3122019-01-18 21:19:04 +0200201 } else if (ret == -ENOMSG) {
202 /* Handle "bad CRC" case */
203 if (best_prio == -1)
204 best_prio = prio;
205 } else {
206 debug("Failed (%d)\n", ret);
Sam Protsenko051dfd82018-07-30 19:19:26 +0300207 }
Simon Glass15e5a1a2017-08-03 12:22:00 -0600208 }
209
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200210 /*
211 * In case of invalid environment, we set the 'default' env location
Sam Protsenko239f3122019-01-18 21:19:04 +0200212 * to the best choice, i.e.:
213 * 1. Environment location with bad CRC, if such location was found
214 * 2. Otherwise use the location with highest priority
215 *
216 * This way, next calls to env_save() will restore the environment
217 * at the right place.
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200218 */
Sam Protsenko239f3122019-01-18 21:19:04 +0200219 if (best_prio >= 0)
220 debug("Selecting environment with bad CRC\n");
221 else
222 best_prio = 0;
Patrick Delaunaya639f4e2020-07-28 11:51:17 +0200223
224 gd->env_load_prio = best_prio;
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200225
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100226 return -ENODEV;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600227}
228
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200229int env_reload(void)
230{
231 struct env_driver *drv;
232
233 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
234 if (drv) {
235 int ret;
236
237 printf("Loading Environment from %s... ", drv->name);
238
239 if (!env_has_inited(drv->location)) {
240 printf("not initialized\n");
241 return -ENODEV;
242 }
243
244 ret = drv->load();
245 if (ret)
246 printf("Failed (%d)\n", ret);
247 else
248 printf("OK\n");
249
250 if (!ret)
251 return 0;
252 }
253
254 return -ENODEV;
255}
256
Simon Glass15e5a1a2017-08-03 12:22:00 -0600257int env_save(void)
258{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100259 struct env_driver *drv;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600260
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200261 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
262 if (drv) {
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100263 int ret;
Maxime Ripard3c0b2e52018-01-23 21:16:50 +0100264
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200265 printf("Saving Environment to %s... ", drv->name);
266 if (!drv->save) {
267 printf("not possible\n");
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200268 return -ENODEV;
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200269 }
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100270
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200271 if (!env_has_inited(drv->location)) {
272 printf("not initialized\n");
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200273 return -ENODEV;
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200274 }
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100275
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100276 ret = drv->save();
Maxime Ripard185402a2018-01-23 21:16:54 +0100277 if (ret)
278 printf("Failed (%d)\n", ret);
279 else
280 printf("OK\n");
281
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100282 if (!ret)
283 return 0;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600284 }
285
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100286 return -ENODEV;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600287}
288
Frank Wunderlich33afa932019-06-29 11:36:19 +0200289int env_erase(void)
290{
291 struct env_driver *drv;
292
293 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
294 if (drv) {
295 int ret;
296
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100297 if (!drv->erase) {
298 printf("not possible\n");
Frank Wunderlich33afa932019-06-29 11:36:19 +0200299 return -ENODEV;
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100300 }
Frank Wunderlich33afa932019-06-29 11:36:19 +0200301
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100302 if (!env_has_inited(drv->location)) {
303 printf("not initialized\n");
Frank Wunderlich33afa932019-06-29 11:36:19 +0200304 return -ENODEV;
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100305 }
Frank Wunderlich33afa932019-06-29 11:36:19 +0200306
307 printf("Erasing Environment on %s... ", drv->name);
308 ret = drv->erase();
309 if (ret)
310 printf("Failed (%d)\n", ret);
311 else
312 printf("OK\n");
313
314 if (!ret)
315 return 0;
316 }
317
318 return -ENODEV;
319}
320
Simon Glass36d85812017-08-03 12:22:05 -0600321int env_init(void)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600322{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100323 struct env_driver *drv;
Simon Glassd40d8042017-08-03 12:22:02 -0600324 int ret = -ENOENT;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100325 int prio;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600326
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100327 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100328 if (!drv->init || !(ret = drv->init()))
329 env_set_inited(drv->location);
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100330 if (ret == -ENOENT)
331 env_set_inited(drv->location);
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100332
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100333 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100334 drv->name, ret);
Felix.Vietmeyer@jila.colorado.edud0f91062021-04-20 20:04:26 -0600335
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200336 if (gd->env_valid == ENV_INVALID)
337 ret = -ENOENT;
Felix.Vietmeyer@jila.colorado.edud0f91062021-04-20 20:04:26 -0600338 }
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100339
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200340 if (!prio)
341 return -ENODEV;
342
Simon Glassd40d8042017-08-03 12:22:02 -0600343 if (ret == -ENOENT) {
344 gd->env_addr = (ulong)&default_environment[0];
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200345 gd->env_valid = ENV_VALID;
Simon Glassd40d8042017-08-03 12:22:02 -0600346
347 return 0;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600348 }
349
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100350 return ret;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600351}
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +0200352
353int env_select(const char *name)
354{
355 struct env_driver *drv;
356 const int n_ents = ll_entry_count(struct env_driver, env_driver);
357 struct env_driver *entry;
358 int prio;
359 bool found = false;
360
361 printf("Select Environment on %s: ", name);
362
363 /* search ENV driver by name */
364 drv = ll_entry_start(struct env_driver, env_driver);
365 for (entry = drv; entry != drv + n_ents; entry++) {
366 if (!strcmp(entry->name, name)) {
367 found = true;
368 break;
369 }
370 }
371
372 if (!found) {
373 printf("driver not found\n");
374 return -ENODEV;
375 }
376
377 /* search priority by driver */
378 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
379 if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
380 /* when priority change, reset the ENV flags */
381 if (gd->env_load_prio != prio) {
382 gd->env_load_prio = prio;
383 gd->env_valid = ENV_INVALID;
384 gd->flags &= ~GD_FLG_ENV_DEFAULT;
385 }
386 printf("OK\n");
387 return 0;
388 }
389 }
390 printf("priority not found\n");
391
392 return -ENODEV;
393}