blob: ad774f41175babe834e642a975936709fd44214a [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
81#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
82 ENVL_SPI_FLASH,
83#endif
84#ifdef CONFIG_ENV_IS_IN_UBI
85 ENVL_UBI,
86#endif
87#ifdef CONFIG_ENV_IS_NOWHERE
88 ENVL_NOWHERE,
89#endif
90};
91
Maxime Ripard2131c5e2018-01-23 21:16:59 +010092static bool env_has_inited(enum env_location location)
93{
94 return gd->env_has_init & BIT(location);
95}
96
97static void env_set_inited(enum env_location location)
98{
99 /*
100 * We're using a 32-bits bitmask stored in gd (env_has_init)
101 * using the above enum value as the bit index. We need to
102 * make sure that we're not overflowing it.
103 */
Patrick Delaunay13ac85a2020-06-24 09:27:25 +0200104 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100105
106 gd->env_has_init |= BIT(location);
107}
108
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100109/**
Marek Vasutf92cf582022-04-06 02:21:32 +0200110 * arch_env_get_location() - Returns the best env location for an arch
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100111 * @op: operations performed on the environment
112 * @prio: priority between the multiple environments, 0 being the
113 * highest priority
114 *
115 * This will return the preferred environment for the given priority.
Marek Vasutf92cf582022-04-06 02:21:32 +0200116 * This is overridable by architectures if they need to and has lower
117 * priority than board side env_get_location() override.
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100118 *
119 * All implementations are free to use the operation, the priority and
120 * any other data relevant to their choice, but must take into account
121 * the fact that the lowest prority (0) is the most important location
122 * in the system. The following locations should be returned by order
123 * of descending priorities, from the highest to the lowest priority.
124 *
125 * Returns:
126 * an enum env_location value on success, a negative error code otherwise
127 */
Marek Vasutf92cf582022-04-06 02:21:32 +0200128__weak enum env_location arch_env_get_location(enum env_operation op, int prio)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600129{
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200130 if (prio >= ARRAY_SIZE(env_locations))
131 return ENVL_UNKNOWN;
Maxime Ripard520cf802018-01-23 21:16:58 +0100132
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200133 return env_locations[prio];
Simon Glass15e5a1a2017-08-03 12:22:00 -0600134}
135
Marek Vasutf92cf582022-04-06 02:21:32 +0200136/**
137 * env_get_location() - Returns the best env location for a board
138 * @op: operations performed on the environment
139 * @prio: priority between the multiple environments, 0 being the
140 * highest priority
141 *
142 * This will return the preferred environment for the given priority.
143 * This is overridable by boards if they need to.
144 *
145 * All implementations are free to use the operation, the priority and
146 * any other data relevant to their choice, but must take into account
147 * the fact that the lowest prority (0) is the most important location
148 * in the system. The following locations should be returned by order
149 * of descending priorities, from the highest to the lowest priority.
150 *
151 * Returns:
152 * an enum env_location value on success, a negative error code otherwise
153 */
154__weak enum env_location env_get_location(enum env_operation op, int prio)
155{
156 return arch_env_get_location(op, prio);
157}
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100158
159/**
160 * env_driver_lookup() - Finds the most suited environment location
161 * @op: operations performed on the environment
162 * @prio: priority between the multiple environments, 0 being the
163 * highest priority
164 *
165 * This will try to find the available environment with the highest
166 * priority in the system.
167 *
168 * Returns:
169 * NULL on error, a pointer to a struct env_driver otherwise
170 */
171static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600172{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100173 enum env_location loc = env_get_location(op, prio);
Simon Glass15e5a1a2017-08-03 12:22:00 -0600174 struct env_driver *drv;
175
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100176 if (loc == ENVL_UNKNOWN)
177 return NULL;
178
Maxime Ripardb7da89a2018-01-23 21:16:51 +0100179 drv = _env_driver_lookup(loc);
Simon Glass15e5a1a2017-08-03 12:22:00 -0600180 if (!drv) {
181 debug("%s: No environment driver for location %d\n", __func__,
182 loc);
183 return NULL;
184 }
185
186 return drv;
187}
188
Simon Glass15e5a1a2017-08-03 12:22:00 -0600189int env_load(void)
190{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100191 struct env_driver *drv;
Sam Protsenko239f3122019-01-18 21:19:04 +0200192 int best_prio = -1;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100193 int prio;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600194
Jan Kiszka755703b2023-02-03 13:22:51 +0100195 if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
196 /*
197 * When using a list of writeable variables, the baseline comes
198 * from the built-in default env. So load this first.
199 */
200 env_set_default(NULL, 0);
201 }
202
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100203 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
204 int ret;
205
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100206 if (!env_has_inited(drv->location))
207 continue;
208
Maxime Ripard185402a2018-01-23 21:16:54 +0100209 printf("Loading Environment from %s... ", drv->name);
Sam Protsenko051dfd82018-07-30 19:19:26 +0300210 /*
211 * In error case, the error message must be printed during
212 * drv->load() in some underlying API, and it must be exactly
213 * one message.
214 */
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100215 ret = drv->load();
Sam Protsenko239f3122019-01-18 21:19:04 +0200216 if (!ret) {
Maxime Ripard185402a2018-01-23 21:16:54 +0100217 printf("OK\n");
Patrick Delaunaya639f4e2020-07-28 11:51:17 +0200218 gd->env_load_prio = prio;
219
Marek Vasuta7b82672020-07-07 20:51:38 +0200220#if !CONFIG_IS_ENABLED(ENV_APPEND)
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100221 return 0;
Marek Vasuta7b82672020-07-07 20:51:38 +0200222#endif
Sam Protsenko239f3122019-01-18 21:19:04 +0200223 } else if (ret == -ENOMSG) {
224 /* Handle "bad CRC" case */
225 if (best_prio == -1)
226 best_prio = prio;
227 } else {
228 debug("Failed (%d)\n", ret);
Sam Protsenko051dfd82018-07-30 19:19:26 +0300229 }
Simon Glass15e5a1a2017-08-03 12:22:00 -0600230 }
231
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200232 /*
233 * In case of invalid environment, we set the 'default' env location
Sam Protsenko239f3122019-01-18 21:19:04 +0200234 * to the best choice, i.e.:
235 * 1. Environment location with bad CRC, if such location was found
236 * 2. Otherwise use the location with highest priority
237 *
238 * This way, next calls to env_save() will restore the environment
239 * at the right place.
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200240 */
Sam Protsenko239f3122019-01-18 21:19:04 +0200241 if (best_prio >= 0)
242 debug("Selecting environment with bad CRC\n");
243 else
244 best_prio = 0;
Patrick Delaunaya639f4e2020-07-28 11:51:17 +0200245
246 gd->env_load_prio = best_prio;
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200247
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100248 return -ENODEV;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600249}
250
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200251int env_reload(void)
252{
253 struct env_driver *drv;
254
255 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
256 if (drv) {
257 int ret;
258
259 printf("Loading Environment from %s... ", drv->name);
260
261 if (!env_has_inited(drv->location)) {
262 printf("not initialized\n");
263 return -ENODEV;
264 }
265
266 ret = drv->load();
267 if (ret)
268 printf("Failed (%d)\n", ret);
269 else
270 printf("OK\n");
271
272 if (!ret)
273 return 0;
274 }
275
276 return -ENODEV;
277}
278
Simon Glass15e5a1a2017-08-03 12:22:00 -0600279int env_save(void)
280{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100281 struct env_driver *drv;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600282
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200283 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
284 if (drv) {
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100285 int ret;
Maxime Ripard3c0b2e52018-01-23 21:16:50 +0100286
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200287 printf("Saving Environment to %s... ", drv->name);
288 if (!drv->save) {
289 printf("not possible\n");
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200290 return -ENODEV;
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200291 }
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100292
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200293 if (!env_has_inited(drv->location)) {
294 printf("not initialized\n");
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200295 return -ENODEV;
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200296 }
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100297
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100298 ret = drv->save();
Maxime Ripard185402a2018-01-23 21:16:54 +0100299 if (ret)
300 printf("Failed (%d)\n", ret);
301 else
302 printf("OK\n");
303
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100304 if (!ret)
305 return 0;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600306 }
307
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100308 return -ENODEV;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600309}
310
Frank Wunderlich33afa932019-06-29 11:36:19 +0200311int env_erase(void)
312{
313 struct env_driver *drv;
314
315 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
316 if (drv) {
317 int ret;
318
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100319 if (!drv->erase) {
320 printf("not possible\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
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100324 if (!env_has_inited(drv->location)) {
325 printf("not initialized\n");
Frank Wunderlich33afa932019-06-29 11:36:19 +0200326 return -ENODEV;
Patrick Delaunay6d6a1d02022-12-14 16:51:32 +0100327 }
Frank Wunderlich33afa932019-06-29 11:36:19 +0200328
329 printf("Erasing Environment on %s... ", drv->name);
330 ret = drv->erase();
331 if (ret)
332 printf("Failed (%d)\n", ret);
333 else
334 printf("OK\n");
335
336 if (!ret)
337 return 0;
338 }
339
340 return -ENODEV;
341}
342
Simon Glass36d85812017-08-03 12:22:05 -0600343int env_init(void)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600344{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100345 struct env_driver *drv;
Simon Glassd40d8042017-08-03 12:22:02 -0600346 int ret = -ENOENT;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100347 int prio;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600348
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100349 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100350 if (!drv->init || !(ret = drv->init()))
351 env_set_inited(drv->location);
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100352 if (ret == -ENOENT)
353 env_set_inited(drv->location);
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100354
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100355 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100356 drv->name, ret);
Felix.Vietmeyer@jila.colorado.edud0f91062021-04-20 20:04:26 -0600357
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200358 if (gd->env_valid == ENV_INVALID)
359 ret = -ENOENT;
Felix.Vietmeyer@jila.colorado.edud0f91062021-04-20 20:04:26 -0600360 }
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100361
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200362 if (!prio)
363 return -ENODEV;
364
Simon Glassd40d8042017-08-03 12:22:02 -0600365 if (ret == -ENOENT) {
366 gd->env_addr = (ulong)&default_environment[0];
Marek Vasut8e25d0c2022-04-10 06:46:52 +0200367 gd->env_valid = ENV_VALID;
Simon Glassd40d8042017-08-03 12:22:02 -0600368
369 return 0;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600370 }
371
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100372 return ret;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600373}
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +0200374
375int env_select(const char *name)
376{
377 struct env_driver *drv;
378 const int n_ents = ll_entry_count(struct env_driver, env_driver);
379 struct env_driver *entry;
380 int prio;
381 bool found = false;
382
383 printf("Select Environment on %s: ", name);
384
385 /* search ENV driver by name */
386 drv = ll_entry_start(struct env_driver, env_driver);
387 for (entry = drv; entry != drv + n_ents; entry++) {
388 if (!strcmp(entry->name, name)) {
389 found = true;
390 break;
391 }
392 }
393
394 if (!found) {
395 printf("driver not found\n");
396 return -ENODEV;
397 }
398
399 /* search priority by driver */
400 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
401 if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
402 /* when priority change, reset the ENV flags */
403 if (gd->env_load_prio != prio) {
404 gd->env_load_prio = prio;
405 gd->env_valid = ENV_INVALID;
406 gd->flags &= ~GD_FLG_ENV_DEFAULT;
407 }
408 printf("OK\n");
409 return 0;
410 }
411 }
412 printf("priority not found\n");
413
414 return -ENODEV;
415}