blob: bae3f6482aea6a9abf41591d939ee2989c79a234 [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 Glass401d1c42020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glasscd93d622020-05-10 11:40:13 -060012#include <linux/bitops.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060013#include <linux/bug.h>
Simon Glassc9d728d2017-08-03 12:22:00 -060014
15DECLARE_GLOBAL_DATA_PTR;
16
Maxime Ripard52746c42018-01-23 21:16:51 +010017static struct env_driver *_env_driver_lookup(enum env_location loc)
Simon Glassc9d728d2017-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 Ripard7d714a22018-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 Ripard1d446082018-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 Delaunayd5a6a5a2020-06-24 09:27:25 +020081 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
Maxime Ripard1d446082018-01-23 21:16:59 +010082
83 gd->env_has_init |= BIT(location);
84}
85
Maxime Ripard8a3a7e22018-01-23 21:16:52 +010086/**
Marek Vasutde70e882022-04-06 02:21:32 +020087 * arch_env_get_location() - Returns the best env location for an arch
88 * @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.
93 * This is overridable by architectures if they need to and has lower
94 * priority than board side env_get_location() override.
95 *
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 */
105__weak enum env_location arch_env_get_location(enum env_operation op, int prio)
106{
107 if (prio >= ARRAY_SIZE(env_locations))
108 return ENVL_UNKNOWN;
109
110 return env_locations[prio];
111}
112
113/**
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100114 * 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.
Maxime Ripard40c08a62018-01-23 21:17:02 +0100120 * This is overridable by boards if they need to.
Maxime Ripard8a3a7e22018-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 */
Maxime Ripard40c08a62018-01-23 21:17:02 +0100131__weak enum env_location env_get_location(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600132{
Marek Vasutde70e882022-04-06 02:21:32 +0200133 return arch_env_get_location(op, prio);
Simon Glassc9d728d2017-08-03 12:22:00 -0600134}
135
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100136/**
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 Glassc9d728d2017-08-03 12:22:00 -0600149{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100150 enum env_location loc = env_get_location(op, prio);
Simon Glassc9d728d2017-08-03 12:22:00 -0600151 struct env_driver *drv;
152
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100153 if (loc == ENVL_UNKNOWN)
154 return NULL;
155
Maxime Ripard52746c42018-01-23 21:16:51 +0100156 drv = _env_driver_lookup(loc);
Simon Glassc9d728d2017-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 Glassc9d728d2017-08-03 12:22:00 -0600166int env_load(void)
167{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100168 struct env_driver *drv;
Sam Protsenko293a1722019-01-18 21:19:04 +0200169 int best_prio = -1;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100170 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600171
Jan Kiszka5ab81052023-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 Ripard8a3a7e22018-01-23 21:16:52 +0100180 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
181 int ret;
182
Maxime Ripard1d446082018-01-23 21:16:59 +0100183 if (!env_has_inited(drv->location))
184 continue;
185
Maxime Ripard3574ba02018-01-23 21:16:54 +0100186 printf("Loading Environment from %s... ", drv->name);
Sam Protsenko13bbfb42018-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 Ripard8a3a7e22018-01-23 21:16:52 +0100192 ret = drv->load();
Sam Protsenko293a1722019-01-18 21:19:04 +0200193 if (!ret) {
Maxime Ripard3574ba02018-01-23 21:16:54 +0100194 printf("OK\n");
Patrick Delaunay6d8d8402020-07-28 11:51:17 +0200195 gd->env_load_prio = prio;
196
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100197 return 0;
Sam Protsenko293a1722019-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 Protsenko13bbfb42018-07-30 19:19:26 +0300204 }
Simon Glassc9d728d2017-08-03 12:22:00 -0600205 }
206
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200207 /*
208 * In case of invalid environment, we set the 'default' env location
Sam Protsenko293a1722019-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 Faustinid30ba232018-07-23 10:01:07 +0200215 */
Sam Protsenko293a1722019-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 Delaunay6d8d8402020-07-28 11:51:17 +0200220
221 gd->env_load_prio = best_prio;
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200222
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100223 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600224}
225
Patrick Delaunay0115dd32020-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 Glassc9d728d2017-08-03 12:22:00 -0600254int env_save(void)
255{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100256 struct env_driver *drv;
Simon Glassc9d728d2017-08-03 12:22:00 -0600257
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200258 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
259 if (drv) {
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100260 int ret;
Maxime Ripard9c24dfb2018-01-23 21:16:50 +0100261
Maxime Ripard9efac3c2018-01-23 21:16:53 +0100262 printf("Saving Environment to %s... ", drv->name);
Patrick Delaunay89682882020-06-24 10:17:50 +0200263 if (!drv->save) {
264 printf("not possible\n");
265 return -ENODEV;
266 }
267
268 if (!env_has_inited(drv->location)) {
269 printf("not initialized\n");
270 return -ENODEV;
271 }
272
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100273 ret = drv->save();
Maxime Ripard3574ba02018-01-23 21:16:54 +0100274 if (ret)
275 printf("Failed (%d)\n", ret);
276 else
277 printf("OK\n");
278
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100279 if (!ret)
280 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600281 }
282
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100283 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600284}
285
Frank Wunderlichcd121bd2019-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 Delaunay1c44d102022-12-14 16:51:32 +0100294 if (!drv->erase) {
295 printf("not possible\n");
Frank Wunderlichcd121bd2019-06-29 11:36:19 +0200296 return -ENODEV;
Patrick Delaunay1c44d102022-12-14 16:51:32 +0100297 }
Frank Wunderlichcd121bd2019-06-29 11:36:19 +0200298
Patrick Delaunay1c44d102022-12-14 16:51:32 +0100299 if (!env_has_inited(drv->location)) {
300 printf("not initialized\n");
Frank Wunderlichcd121bd2019-06-29 11:36:19 +0200301 return -ENODEV;
Patrick Delaunay1c44d102022-12-14 16:51:32 +0100302 }
Frank Wunderlichcd121bd2019-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 Glass6eeae422017-08-03 12:22:05 -0600318int env_init(void)
Simon Glassc9d728d2017-08-03 12:22:00 -0600319{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100320 struct env_driver *drv;
Simon Glass79388222017-08-03 12:22:02 -0600321 int ret = -ENOENT;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100322 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600323
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100324 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard1d446082018-01-23 21:16:59 +0100325 if (!drv->init || !(ret = drv->init()))
326 env_set_inited(drv->location);
Heiko Schocher46ce9e72020-11-03 15:22:36 +0100327 if (ret == -ENOENT)
328 env_set_inited(drv->location);
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100329
Maxime Ripard1d446082018-01-23 21:16:59 +0100330 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100331 drv->name, ret);
Marek Vasut5c7399e2022-04-10 06:46:52 +0200332
333 if (gd->env_valid == ENV_INVALID)
334 ret = -ENOENT;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100335 }
336
Marek Vasut5c7399e2022-04-10 06:46:52 +0200337 if (!prio)
338 return -ENODEV;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100339
Simon Glass79388222017-08-03 12:22:02 -0600340 if (ret == -ENOENT) {
341 gd->env_addr = (ulong)&default_environment[0];
Marek Vasut5c7399e2022-04-10 06:46:52 +0200342 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}