blob: 91d220c3dd1f64a32cbc05b995949615a2e10f04 [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
Ye Lia44849a2019-01-04 09:34:24 +000081#ifdef CONFIG_ENV_IS_IN_SATA
82 ENVL_ESATA,
83#endif
Maxime Ripard520cf802018-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
90#ifdef CONFIG_ENV_IS_NOWHERE
91 ENVL_NOWHERE,
92#endif
93};
94
Maxime Ripard2131c5e2018-01-23 21:16:59 +010095static bool env_has_inited(enum env_location location)
96{
97 return gd->env_has_init & BIT(location);
98}
99
100static void env_set_inited(enum env_location location)
101{
102 /*
103 * We're using a 32-bits bitmask stored in gd (env_has_init)
104 * using the above enum value as the bit index. We need to
105 * make sure that we're not overflowing it.
106 */
Patrick Delaunay13ac85a2020-06-24 09:27:25 +0200107 BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100108
109 gd->env_has_init |= BIT(location);
110}
111
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100112/**
113 * env_get_location() - Returns the best env location for a board
114 * @op: operations performed on the environment
115 * @prio: priority between the multiple environments, 0 being the
116 * highest priority
117 *
118 * This will return the preferred environment for the given priority.
Maxime Ripard89448752018-01-23 21:17:02 +0100119 * This is overridable by boards if they need to.
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100120 *
121 * All implementations are free to use the operation, the priority and
122 * any other data relevant to their choice, but must take into account
123 * the fact that the lowest prority (0) is the most important location
124 * in the system. The following locations should be returned by order
125 * of descending priorities, from the highest to the lowest priority.
126 *
127 * Returns:
128 * an enum env_location value on success, a negative error code otherwise
129 */
Maxime Ripard89448752018-01-23 21:17:02 +0100130__weak enum env_location env_get_location(enum env_operation op, int prio)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600131{
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200132 if (prio >= ARRAY_SIZE(env_locations))
133 return ENVL_UNKNOWN;
Maxime Ripard520cf802018-01-23 21:16:58 +0100134
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200135 return env_locations[prio];
Simon Glass15e5a1a2017-08-03 12:22:00 -0600136}
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
Goldschmidt Simon9b0d66b2018-02-09 20:23:17 +0000169int env_get_char(int index)
170{
Simon Glass1f39d0b2017-08-20 04:45:15 -0600171 if (gd->env_valid == ENV_INVALID)
Simon Glass10b0d7b2017-08-03 12:22:06 -0600172 return default_environment[index];
Goldschmidt Simon9b0d66b2018-02-09 20:23:17 +0000173 else
Marek Behúna88ce3d2021-10-17 17:36:27 +0200174 return *(uchar *)(gd->env_addr + index);
Simon Glass15e5a1a2017-08-03 12:22:00 -0600175}
176
177int env_load(void)
178{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100179 struct env_driver *drv;
Sam Protsenko239f3122019-01-18 21:19:04 +0200180 int best_prio = -1;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100181 int prio;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600182
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
Marek Vasuta7b82672020-07-07 20:51:38 +0200200#if !CONFIG_IS_ENABLED(ENV_APPEND)
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100201 return 0;
Marek Vasuta7b82672020-07-07 20:51:38 +0200202#endif
Sam Protsenko239f3122019-01-18 21:19:04 +0200203 } else if (ret == -ENOMSG) {
204 /* Handle "bad CRC" case */
205 if (best_prio == -1)
206 best_prio = prio;
207 } else {
208 debug("Failed (%d)\n", ret);
Sam Protsenko051dfd82018-07-30 19:19:26 +0300209 }
Simon Glass15e5a1a2017-08-03 12:22:00 -0600210 }
211
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200212 /*
213 * In case of invalid environment, we set the 'default' env location
Sam Protsenko239f3122019-01-18 21:19:04 +0200214 * to the best choice, i.e.:
215 * 1. Environment location with bad CRC, if such location was found
216 * 2. Otherwise use the location with highest priority
217 *
218 * This way, next calls to env_save() will restore the environment
219 * at the right place.
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200220 */
Sam Protsenko239f3122019-01-18 21:19:04 +0200221 if (best_prio >= 0)
222 debug("Selecting environment with bad CRC\n");
223 else
224 best_prio = 0;
Patrick Delaunaya639f4e2020-07-28 11:51:17 +0200225
226 gd->env_load_prio = best_prio;
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200227
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100228 return -ENODEV;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600229}
230
Patrick Delaunay748e42e2020-07-28 11:51:20 +0200231int env_reload(void)
232{
233 struct env_driver *drv;
234
235 drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
236 if (drv) {
237 int ret;
238
239 printf("Loading Environment from %s... ", drv->name);
240
241 if (!env_has_inited(drv->location)) {
242 printf("not initialized\n");
243 return -ENODEV;
244 }
245
246 ret = drv->load();
247 if (ret)
248 printf("Failed (%d)\n", ret);
249 else
250 printf("OK\n");
251
252 if (!ret)
253 return 0;
254 }
255
256 return -ENODEV;
257}
258
Simon Glass15e5a1a2017-08-03 12:22:00 -0600259int env_save(void)
260{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100261 struct env_driver *drv;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600262
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200263 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
264 if (drv) {
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100265 int ret;
Maxime Ripard3c0b2e52018-01-23 21:16:50 +0100266
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200267 printf("Saving Environment to %s... ", drv->name);
268 if (!drv->save) {
269 printf("not possible\n");
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200270 return -ENODEV;
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200271 }
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100272
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200273 if (!env_has_inited(drv->location)) {
274 printf("not initialized\n");
Nicholas Faustinia8a17f82018-07-23 10:01:07 +0200275 return -ENODEV;
Patrick Delaunay65bbec22020-06-24 10:17:50 +0200276 }
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100277
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100278 ret = drv->save();
Maxime Ripard185402a2018-01-23 21:16:54 +0100279 if (ret)
280 printf("Failed (%d)\n", ret);
281 else
282 printf("OK\n");
283
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100284 if (!ret)
285 return 0;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600286 }
287
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100288 return -ENODEV;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600289}
290
Frank Wunderlich33afa932019-06-29 11:36:19 +0200291int env_erase(void)
292{
293 struct env_driver *drv;
294
295 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
296 if (drv) {
297 int ret;
298
299 if (!drv->erase)
300 return -ENODEV;
301
302 if (!env_has_inited(drv->location))
303 return -ENODEV;
304
305 printf("Erasing Environment on %s... ", drv->name);
306 ret = drv->erase();
307 if (ret)
308 printf("Failed (%d)\n", ret);
309 else
310 printf("OK\n");
311
312 if (!ret)
313 return 0;
314 }
315
316 return -ENODEV;
317}
318
Simon Glass36d85812017-08-03 12:22:05 -0600319int env_init(void)
Simon Glass15e5a1a2017-08-03 12:22:00 -0600320{
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100321 struct env_driver *drv;
Simon Glassd40d8042017-08-03 12:22:02 -0600322 int ret = -ENOENT;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100323 int prio;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600324
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100325 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100326 if (!drv->init || !(ret = drv->init()))
327 env_set_inited(drv->location);
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100328 if (ret == -ENOENT)
329 env_set_inited(drv->location);
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100330
Maxime Ripard2131c5e2018-01-23 21:16:59 +0100331 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100332 drv->name, ret);
Marek Vasut447ac2c2021-01-20 15:45:16 +0100333
334 if (gd->env_valid == ENV_INVALID)
335 ret = -ENOENT;
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100336 }
337
338 if (!prio)
339 return -ENODEV;
340
Simon Glassd40d8042017-08-03 12:22:02 -0600341 if (ret == -ENOENT) {
342 gd->env_addr = (ulong)&default_environment[0];
Tom Rini8a7c44b2017-08-19 22:27:57 -0400343 gd->env_valid = ENV_VALID;
Simon Glassd40d8042017-08-03 12:22:02 -0600344
345 return 0;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600346 }
347
Maxime Ripardfd3158e2018-01-23 21:16:52 +0100348 return ret;
Simon Glass15e5a1a2017-08-03 12:22:00 -0600349}
Patrick Delaunaya59f7ec2020-07-28 11:51:21 +0200350
351int env_select(const char *name)
352{
353 struct env_driver *drv;
354 const int n_ents = ll_entry_count(struct env_driver, env_driver);
355 struct env_driver *entry;
356 int prio;
357 bool found = false;
358
359 printf("Select Environment on %s: ", name);
360
361 /* search ENV driver by name */
362 drv = ll_entry_start(struct env_driver, env_driver);
363 for (entry = drv; entry != drv + n_ents; entry++) {
364 if (!strcmp(entry->name, name)) {
365 found = true;
366 break;
367 }
368 }
369
370 if (!found) {
371 printf("driver not found\n");
372 return -ENODEV;
373 }
374
375 /* search priority by driver */
376 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
377 if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
378 /* when priority change, reset the ENV flags */
379 if (gd->env_load_prio != prio) {
380 gd->env_load_prio = prio;
381 gd->env_valid = ENV_INVALID;
382 gd->flags &= ~GD_FLG_ENV_DEFAULT;
383 }
384 printf("OK\n");
385 return 0;
386 }
387 }
388 printf("priority not found\n");
389
390 return -ENODEV;
391}