blob: fce8e5982e2c650bf04bc6ec1c16092eba8f482f [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
Siva Durga Prasad Paladugu7bcdf192018-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 Wunderlichcd121bd2019-06-29 11:36:19 +020032 if (entry->erase)
33 entry->erase += gd->reloc_off;
Siva Durga Prasad Paladugu7bcdf192018-04-13 07:57:21 +020034 if (entry->init)
35 entry->init += gd->reloc_off;
36 }
37}
38#endif
39
Maxime Ripard52746c42018-01-23 21:16:51 +010040static struct env_driver *_env_driver_lookup(enum env_location loc)
Simon Glassc9d728d2017-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 Ripard7d714a22018-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 Li9a6a3112019-01-04 09:34:24 +000081#ifdef CONFIG_ENV_IS_IN_SATA
82 ENVL_ESATA,
83#endif
Maxime Ripard7d714a22018-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
Bo Lv4a465022023-02-28 05:51:47 +000090#ifdef CONFIG_ENV_IS_IN_STORAGE
91 ENVL_STORAGE,
92#endif
Maxime Ripard7d714a22018-01-23 21:16:58 +010093#ifdef CONFIG_ENV_IS_NOWHERE
94 ENVL_NOWHERE,
95#endif
96};
97
Maxime Ripard1d446082018-01-23 21:16:59 +010098static bool env_has_inited(enum env_location location)
99{
100 return gd->env_has_init & BIT(location);
101}
102
103static void env_set_inited(enum env_location location)
104{
105 /*
106 * We're using a 32-bits bitmask stored in gd (env_has_init)
107 * using the above enum value as the bit index. We need to
108 * make sure that we're not overflowing it.
109 */
Patrick Delaunayd5a6a5a2020-06-24 09:27:25 +0200110 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
Maxime Ripard1d446082018-01-23 21:16:59 +0100111
112 gd->env_has_init |= BIT(location);
113}
114
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100115/**
Marek Vasutde70e882022-04-06 02:21:32 +0200116 * arch_env_get_location() - Returns the best env location for an arch
117 * @op: operations performed on the environment
118 * @prio: priority between the multiple environments, 0 being the
119 * highest priority
120 *
121 * This will return the preferred environment for the given priority.
122 * This is overridable by architectures if they need to and has lower
123 * priority than board side env_get_location() override.
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 arch_env_get_location(enum env_operation op, int prio)
135{
136 if (prio >= ARRAY_SIZE(env_locations))
137 return ENVL_UNKNOWN;
138
139 return env_locations[prio];
140}
141
142/**
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100143 * env_get_location() - Returns the best env location for a board
144 * @op: operations performed on the environment
145 * @prio: priority between the multiple environments, 0 being the
146 * highest priority
147 *
148 * This will return the preferred environment for the given priority.
Maxime Ripard40c08a62018-01-23 21:17:02 +0100149 * This is overridable by boards if they need to.
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100150 *
151 * All implementations are free to use the operation, the priority and
152 * any other data relevant to their choice, but must take into account
153 * the fact that the lowest prority (0) is the most important location
154 * in the system. The following locations should be returned by order
155 * of descending priorities, from the highest to the lowest priority.
156 *
157 * Returns:
158 * an enum env_location value on success, a negative error code otherwise
159 */
Maxime Ripard40c08a62018-01-23 21:17:02 +0100160__weak enum env_location env_get_location(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600161{
Marek Vasutde70e882022-04-06 02:21:32 +0200162 return arch_env_get_location(op, prio);
Simon Glassc9d728d2017-08-03 12:22:00 -0600163}
164
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100165/**
166 * env_driver_lookup() - Finds the most suited environment location
167 * @op: operations performed on the environment
168 * @prio: priority between the multiple environments, 0 being the
169 * highest priority
170 *
171 * This will try to find the available environment with the highest
172 * priority in the system.
173 *
174 * Returns:
175 * NULL on error, a pointer to a struct env_driver otherwise
176 */
177static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600178{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100179 enum env_location loc = env_get_location(op, prio);
Simon Glassc9d728d2017-08-03 12:22:00 -0600180 struct env_driver *drv;
181
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100182 if (loc == ENVL_UNKNOWN)
183 return NULL;
184
Maxime Ripard52746c42018-01-23 21:16:51 +0100185 drv = _env_driver_lookup(loc);
Simon Glassc9d728d2017-08-03 12:22:00 -0600186 if (!drv) {
187 debug("%s: No environment driver for location %d\n", __func__,
188 loc);
189 return NULL;
190 }
191
192 return drv;
193}
194
Simon Glassc9d728d2017-08-03 12:22:00 -0600195int env_load(void)
196{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100197 struct env_driver *drv;
Sam Protsenko293a1722019-01-18 21:19:04 +0200198 int best_prio = -1;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100199 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600200
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100201 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
202 int ret;
203
Maxime Ripard1d446082018-01-23 21:16:59 +0100204 if (!env_has_inited(drv->location))
205 continue;
206
Maxime Ripard3574ba02018-01-23 21:16:54 +0100207 printf("Loading Environment from %s... ", drv->name);
Sam Protsenko13bbfb42018-07-30 19:19:26 +0300208 /*
209 * In error case, the error message must be printed during
210 * drv->load() in some underlying API, and it must be exactly
211 * one message.
212 */
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100213 ret = drv->load();
Sam Protsenko293a1722019-01-18 21:19:04 +0200214 if (!ret) {
Maxime Ripard3574ba02018-01-23 21:16:54 +0100215 printf("OK\n");
Patrick Delaunay6d8d8402020-07-28 11:51:17 +0200216 gd->env_load_prio = prio;
217
Marek Vasut47f3b1f2020-07-07 20:51:38 +0200218#if !CONFIG_IS_ENABLED(ENV_APPEND)
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100219 return 0;
Marek Vasut47f3b1f2020-07-07 20:51:38 +0200220#endif
Sam Protsenko293a1722019-01-18 21:19:04 +0200221 } else if (ret == -ENOMSG) {
222 /* Handle "bad CRC" case */
223 if (best_prio == -1)
224 best_prio = prio;
225 } else {
226 debug("Failed (%d)\n", ret);
Sam Protsenko13bbfb42018-07-30 19:19:26 +0300227 }
Simon Glassc9d728d2017-08-03 12:22:00 -0600228 }
229
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200230 /*
231 * In case of invalid environment, we set the 'default' env location
Sam Protsenko293a1722019-01-18 21:19:04 +0200232 * to the best choice, i.e.:
233 * 1. Environment location with bad CRC, if such location was found
234 * 2. Otherwise use the location with highest priority
235 *
236 * This way, next calls to env_save() will restore the environment
237 * at the right place.
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200238 */
Sam Protsenko293a1722019-01-18 21:19:04 +0200239 if (best_prio >= 0)
240 debug("Selecting environment with bad CRC\n");
241 else
242 best_prio = 0;
Patrick Delaunay6d8d8402020-07-28 11:51:17 +0200243
244 gd->env_load_prio = best_prio;
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200245
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100246 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600247}
248
Patrick Delaunay0115dd32020-07-28 11:51:20 +0200249int env_reload(void)
250{
251 struct env_driver *drv;
252
253 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
254 if (drv) {
255 int ret;
256
257 printf("Loading Environment from %s... ", drv->name);
258
259 if (!env_has_inited(drv->location)) {
260 printf("not initialized\n");
261 return -ENODEV;
262 }
263
264 ret = drv->load();
265 if (ret)
266 printf("Failed (%d)\n", ret);
267 else
268 printf("OK\n");
269
270 if (!ret)
271 return 0;
272 }
273
274 return -ENODEV;
275}
276
Simon Glassc9d728d2017-08-03 12:22:00 -0600277int env_save(void)
278{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100279 struct env_driver *drv;
Simon Glassc9d728d2017-08-03 12:22:00 -0600280
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200281 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
282 if (drv) {
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100283 int ret;
Maxime Ripard9c24dfb2018-01-23 21:16:50 +0100284
Maxime Ripard9efac3c2018-01-23 21:16:53 +0100285 printf("Saving Environment to %s... ", drv->name);
Patrick Delaunay89682882020-06-24 10:17:50 +0200286 if (!drv->save) {
287 printf("not possible\n");
288 return -ENODEV;
289 }
290
291 if (!env_has_inited(drv->location)) {
292 printf("not initialized\n");
293 return -ENODEV;
294 }
295
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100296 ret = drv->save();
Maxime Ripard3574ba02018-01-23 21:16:54 +0100297 if (ret)
298 printf("Failed (%d)\n", ret);
299 else
300 printf("OK\n");
301
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100302 if (!ret)
303 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600304 }
305
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100306 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600307}
308
Frank Wunderlichcd121bd2019-06-29 11:36:19 +0200309int env_erase(void)
310{
311 struct env_driver *drv;
312
313 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
314 if (drv) {
315 int ret;
316
317 if (!drv->erase)
318 return -ENODEV;
319
320 if (!env_has_inited(drv->location))
321 return -ENODEV;
322
323 printf("Erasing Environment on %s... ", drv->name);
324 ret = drv->erase();
325 if (ret)
326 printf("Failed (%d)\n", ret);
327 else
328 printf("OK\n");
329
330 if (!ret)
331 return 0;
332 }
333
334 return -ENODEV;
335}
336
Simon Glass6eeae422017-08-03 12:22:05 -0600337int env_init(void)
Simon Glassc9d728d2017-08-03 12:22:00 -0600338{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100339 struct env_driver *drv;
Simon Glass79388222017-08-03 12:22:02 -0600340 int ret = -ENOENT;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100341 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600342
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100343 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard1d446082018-01-23 21:16:59 +0100344 if (!drv->init || !(ret = drv->init()))
345 env_set_inited(drv->location);
Heiko Schocher46ce9e72020-11-03 15:22:36 +0100346 if (ret == -ENOENT)
347 env_set_inited(drv->location);
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100348
Maxime Ripard1d446082018-01-23 21:16:59 +0100349 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100350 drv->name, ret);
Marek Vasut5c7399e2022-04-10 06:46:52 +0200351
352 if (gd->env_valid == ENV_INVALID)
353 ret = -ENOENT;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100354 }
355
Marek Vasut5c7399e2022-04-10 06:46:52 +0200356 if (!prio)
357 return -ENODEV;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100358
Simon Glass79388222017-08-03 12:22:02 -0600359 if (ret == -ENOENT) {
360 gd->env_addr = (ulong)&default_environment[0];
Marek Vasut5c7399e2022-04-10 06:46:52 +0200361 gd->env_valid = ENV_VALID;
Simon Glass79388222017-08-03 12:22:02 -0600362
363 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600364 }
365
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100366 return ret;
Simon Glassc9d728d2017-08-03 12:22:00 -0600367}
Patrick Delaunaya97d22e2020-07-28 11:51:21 +0200368
369int env_select(const char *name)
370{
371 struct env_driver *drv;
372 const int n_ents = ll_entry_count(struct env_driver, env_driver);
373 struct env_driver *entry;
374 int prio;
375 bool found = false;
376
377 printf("Select Environment on %s: ", name);
378
379 /* search ENV driver by name */
380 drv = ll_entry_start(struct env_driver, env_driver);
381 for (entry = drv; entry != drv + n_ents; entry++) {
382 if (!strcmp(entry->name, name)) {
383 found = true;
384 break;
385 }
386 }
387
388 if (!found) {
389 printf("driver not found\n");
390 return -ENODEV;
391 }
392
393 /* search priority by driver */
394 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
395 if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
396 /* when priority change, reset the ENV flags */
397 if (gd->env_load_prio != prio) {
398 gd->env_load_prio = prio;
399 gd->env_valid = ENV_INVALID;
400 gd->flags &= ~GD_FLG_ENV_DEFAULT;
401 }
402 printf("OK\n");
403 return 0;
404 }
405 }
406 printf("priority not found\n");
407
408 return -ENODEV;
409}