blob: bae3f6482aea6a9abf41591d939ee2989c79a234 [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
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
61#ifdef CONFIG_ENV_IS_IN_UBI
62 ENVL_UBI,
63#endif
64#ifdef CONFIG_ENV_IS_NOWHERE
65 ENVL_NOWHERE,
66#endif
67};
68
Maxime Ripard2131c5e2018-01-23 21:16:59 +010069static bool env_has_inited(enum env_location location)
70{
71 return gd->env_has_init & BIT(location);
72}
73
74static void env_set_inited(enum env_location location)
75{
76 /*
77 * We're using a 32-bits bitmask stored in gd (env_has_init)
78 * using the above enum value as the bit index. We need to
79 * make sure that we're not overflowing it.
80 */
Patrick Delaunay13ac85a2020-06-24 09:27:25 +020081 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
Maxime Ripard2131c5e2018-01-23 21:16:59 +010082
83 gd->env_has_init |= BIT(location);
84}
85
Maxime Ripardfd3158e2018-01-23 21:16:52 +010086/**
Marek Vasutf92cf582022-04-06 02:21:32 +020087 * arch_env_get_location() - Returns the best env location for an arch
Maxime Ripardfd3158e2018-01-23 21:16:52 +010088 * @op: operations performed on the environment
89 * @prio: priority between the multiple environments, 0 being the
90 * highest priority
91 *
92 * This will return the preferred environment for the given priority.
Marek Vasutf92cf582022-04-06 02:21:32 +020093 * This is overridable by architectures if they need to and has lower
94 * priority than board side env_get_location() override.
Maxime Ripardfd3158e2018-01-23 21:16:52 +010095 *
96 * All implementations are free to use the operation, the priority and
97 * any other data relevant to their choice, but must take into account
98 * the fact that the lowest prority (0) is the most important location
99 * in the system. The following locations should be returned by order
100 * of descending priorities, from the highest to the lowest priority.
101 *
102 * Returns:
103 * an enum env_location value on success, a negative error code otherwise
104 */
Marek Vasutf92cf582022-04-06 02:21:32 +0200105__weak enum env_location arch_env_get_location(enum env_operation op, int prio)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600106{
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200107 if (prio >= ARRAY_SIZE(env_locations))
108 return ENVL_UNKNOWN;
Maxime Ripard520cf802018-01-23 21:16:58 +0100109
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200110 return env_locations[prio];
Simon Glass15e5a1a2017-08-03 12:22:00 -0600111}
112
Marek Vasutf92cf582022-04-06 02:21:32 +0200113/**
114 * env_get_location() - Returns the best env location for a board
115 * @op: operations performed on the environment
116 * @prio: priority between the multiple environments, 0 being the
117 * highest priority
118 *
119 * This will return the preferred environment for the given priority.
120 * This is overridable by boards if they need to.
121 *
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 */
131__weak enum env_location env_get_location(enum env_operation op, int prio)
132{
133 return arch_env_get_location(op, prio);
134}
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100135
136/**
137 * env_driver_lookup() - Finds the most suited environment location
138 * @op: operations performed on the environment
139 * @prio: priority between the multiple environments, 0 being the
140 * highest priority
141 *
142 * This will try to find the available environment with the highest
143 * priority in the system.
144 *
145 * Returns:
146 * NULL on error, a pointer to a struct env_driver otherwise
147 */
148static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600149{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100150 enum env_location loc = env_get_location(op, prio);
Simon Glass15e5a1a2017-08-03 12:22:00 -0600151 struct env_driver *drv;
152
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100153 if (loc == ENVL_UNKNOWN)
154 return NULL;
155
Maxime Ripardb7da89a2018-01-23 21:16:51 +0100156 drv = _env_driver_lookup(loc);
Simon Glass15e5a1a2017-08-03 12:22:00 -0600157 if (!drv) {
158 debug("%s: No environment driver for location %d\n", __func__,
159 loc);
160 return NULL;
161 }
162
163 return drv;
164}
165
Simon Glass15e5a1a2017-08-03 12:22:00 -0600166int env_load(void)
167{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100168 struct env_driver *drv;
Sam Protsenko239f3122019-01-18 21:19:04 +0200169 int best_prio = -1;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100170 int prio;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600171
Jan Kiszka755703b2023-02-03 13:22:51 +0100172 if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
173 /*
174 * When using a list of writeable variables, the baseline comes
175 * from the built-in default env. So load this first.
176 */
177 env_set_default(NULL, 0);
178 }
179
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100180 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
181 int ret;
182
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100183 if (!env_has_inited(drv->location))
184 continue;
185
Maxime Ripard185402a2018-01-23 21:16:54 +0100186 printf("Loading Environment from %s... ", drv->name);
Sam Protsenko051dfd82018-07-30 19:19:26 +0300187 /*
188 * In error case, the error message must be printed during
189 * drv->load() in some underlying API, and it must be exactly
190 * one message.
191 */
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100192 ret = drv->load();
Sam Protsenko239f3122019-01-18 21:19:04 +0200193 if (!ret) {
Maxime Ripard185402a2018-01-23 21:16:54 +0100194 printf("OK\n");
Patrick Delaunaya639f4e2020-07-28 11:51:17 +0200195 gd->env_load_prio = prio;
196
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100197 return 0;
Sam Protsenko239f3122019-01-18 21:19:04 +0200198 } else if (ret == -ENOMSG) {
199 /* Handle "bad CRC" case */
200 if (best_prio == -1)
201 best_prio = prio;
202 } else {
203 debug("Failed (%d)\n", ret);
Sam Protsenko051dfd82018-07-30 19:19:26 +0300204 }
Simon Glass15e5a1a2017-08-03 12:22:00 -0600205 }
206
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200207 /*
208 * In case of invalid environment, we set the 'default' env location
Sam Protsenko239f3122019-01-18 21:19:04 +0200209 * to the best choice, i.e.:
210 * 1. Environment location with bad CRC, if such location was found
211 * 2. Otherwise use the location with highest priority
212 *
213 * This way, next calls to env_save() will restore the environment
214 * at the right place.
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200215 */
Sam Protsenko239f3122019-01-18 21:19:04 +0200216 if (best_prio >= 0)
217 debug("Selecting environment with bad CRC\n");
218 else
219 best_prio = 0;
Patrick Delaunaya639f4e2020-07-28 11:51:17 +0200220
221 gd->env_load_prio = best_prio;
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200222
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100223 return -ENODEV;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600224}
225
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200226int env_reload(void)
227{
228 struct env_driver *drv;
229
230 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
231 if (drv) {
232 int ret;
233
234 printf("Loading Environment from %s... ", drv->name);
235
236 if (!env_has_inited(drv->location)) {
237 printf("not initialized\n");
238 return -ENODEV;
239 }
240
241 ret = drv->load();
242 if (ret)
243 printf("Failed (%d)\n", ret);
244 else
245 printf("OK\n");
246
247 if (!ret)
248 return 0;
249 }
250
251 return -ENODEV;
252}
253
Simon Glass15e5a1a2017-08-03 12:22:00 -0600254int env_save(void)
255{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100256 struct env_driver *drv;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600257
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200258 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
259 if (drv) {
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100260 int ret;
Maxime Ripard3c0b2e52018-01-23 21:16:50 +0100261
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200262 printf("Saving Environment to %s... ", drv->name);
263 if (!drv->save) {
264 printf("not possible\n");
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200265 return -ENODEV;
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200266 }
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100267
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200268 if (!env_has_inited(drv->location)) {
269 printf("not initialized\n");
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200270 return -ENODEV;
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200271 }
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100272
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100273 ret = drv->save();
Maxime Ripard185402a2018-01-23 21:16:54 +0100274 if (ret)
275 printf("Failed (%d)\n", ret);
276 else
277 printf("OK\n");
278
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100279 if (!ret)
280 return 0;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600281 }
282
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100283 return -ENODEV;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600284}
285
Frank Wunderlich33afa932019-06-29 11:36:19 +0200286int env_erase(void)
287{
288 struct env_driver *drv;
289
290 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
291 if (drv) {
292 int ret;
293
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100294 if (!drv->erase) {
295 printf("not possible\n");
Frank Wunderlich33afa932019-06-29 11:36:19 +0200296 return -ENODEV;
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100297 }
Frank Wunderlich33afa932019-06-29 11:36:19 +0200298
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100299 if (!env_has_inited(drv->location)) {
300 printf("not initialized\n");
Frank Wunderlich33afa932019-06-29 11:36:19 +0200301 return -ENODEV;
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100302 }
Frank Wunderlich33afa932019-06-29 11:36:19 +0200303
304 printf("Erasing Environment on %s... ", drv->name);
305 ret = drv->erase();
306 if (ret)
307 printf("Failed (%d)\n", ret);
308 else
309 printf("OK\n");
310
311 if (!ret)
312 return 0;
313 }
314
315 return -ENODEV;
316}
317
Simon Glass36d85812017-08-03 12:22:05 -0600318int env_init(void)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600319{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100320 struct env_driver *drv;
Simon Glassd40d8042017-08-03 12:22:02 -0600321 int ret = -ENOENT;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100322 int prio;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600323
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100324 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100325 if (!drv->init || !(ret = drv->init()))
326 env_set_inited(drv->location);
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100327 if (ret == -ENOENT)
328 env_set_inited(drv->location);
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100329
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100330 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100331 drv->name, ret);
Felix.Vietmeyer@jila.colorado.edud0f91062021-04-20 20:04:26 -0600332
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200333 if (gd->env_valid == ENV_INVALID)
334 ret = -ENOENT;
Felix.Vietmeyer@jila.colorado.edud0f91062021-04-20 20:04:26 -0600335 }
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100336
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200337 if (!prio)
338 return -ENODEV;
339
Simon Glassd40d8042017-08-03 12:22:02 -0600340 if (ret == -ENOENT) {
341 gd->env_addr = (ulong)&default_environment[0];
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200342 gd->env_valid = ENV_VALID;
Simon Glassd40d8042017-08-03 12:22:02 -0600343
344 return 0;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600345 }
346
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100347 return ret;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600348}
Patrick Delaunaya59f7ec2020-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}