blob: 9237bb9c742a5242940cd6248751bc5d4eef86fa [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 Glassc9d728d2017-08-03 12:22:00 -060010
11DECLARE_GLOBAL_DATA_PTR;
12
Siva Durga Prasad Paladugu7bcdf192018-04-13 07:57:21 +020013#if defined(CONFIG_NEEDS_MANUAL_RELOC)
14void env_fix_drivers(void)
15{
16 struct env_driver *drv;
17 const int n_ents = ll_entry_count(struct env_driver, env_driver);
18 struct env_driver *entry;
19
20 drv = ll_entry_start(struct env_driver, env_driver);
21 for (entry = drv; entry != drv + n_ents; entry++) {
22 if (entry->name)
23 entry->name += gd->reloc_off;
24 if (entry->load)
25 entry->load += gd->reloc_off;
26 if (entry->save)
27 entry->save += gd->reloc_off;
Frank Wunderlichcd121bd2019-06-29 11:36:19 +020028 if (entry->erase)
29 entry->erase += gd->reloc_off;
Siva Durga Prasad Paladugu7bcdf192018-04-13 07:57:21 +020030 if (entry->init)
31 entry->init += gd->reloc_off;
32 }
33}
34#endif
35
Maxime Ripard52746c42018-01-23 21:16:51 +010036static struct env_driver *_env_driver_lookup(enum env_location loc)
Simon Glassc9d728d2017-08-03 12:22:00 -060037{
38 struct env_driver *drv;
39 const int n_ents = ll_entry_count(struct env_driver, env_driver);
40 struct env_driver *entry;
41
42 drv = ll_entry_start(struct env_driver, env_driver);
43 for (entry = drv; entry != drv + n_ents; entry++) {
44 if (loc == entry->location)
45 return entry;
46 }
47
48 /* Not found */
49 return NULL;
50}
51
Maxime Ripard7d714a22018-01-23 21:16:58 +010052static enum env_location env_locations[] = {
53#ifdef CONFIG_ENV_IS_IN_EEPROM
54 ENVL_EEPROM,
55#endif
56#ifdef CONFIG_ENV_IS_IN_EXT4
57 ENVL_EXT4,
58#endif
59#ifdef CONFIG_ENV_IS_IN_FAT
60 ENVL_FAT,
61#endif
62#ifdef CONFIG_ENV_IS_IN_FLASH
63 ENVL_FLASH,
64#endif
65#ifdef CONFIG_ENV_IS_IN_MMC
66 ENVL_MMC,
67#endif
68#ifdef CONFIG_ENV_IS_IN_NAND
69 ENVL_NAND,
70#endif
71#ifdef CONFIG_ENV_IS_IN_NVRAM
72 ENVL_NVRAM,
73#endif
74#ifdef CONFIG_ENV_IS_IN_REMOTE
75 ENVL_REMOTE,
76#endif
Ye Li9a6a3112019-01-04 09:34:24 +000077#ifdef CONFIG_ENV_IS_IN_SATA
78 ENVL_ESATA,
79#endif
Maxime Ripard7d714a22018-01-23 21:16:58 +010080#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
81 ENVL_SPI_FLASH,
82#endif
83#ifdef CONFIG_ENV_IS_IN_UBI
84 ENVL_UBI,
85#endif
86#ifdef CONFIG_ENV_IS_NOWHERE
87 ENVL_NOWHERE,
88#endif
89};
90
Maxime Ripard1d446082018-01-23 21:16:59 +010091static bool env_has_inited(enum env_location location)
92{
93 return gd->env_has_init & BIT(location);
94}
95
96static void env_set_inited(enum env_location location)
97{
98 /*
99 * We're using a 32-bits bitmask stored in gd (env_has_init)
100 * using the above enum value as the bit index. We need to
101 * make sure that we're not overflowing it.
102 */
103 BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG);
104
105 gd->env_has_init |= BIT(location);
106}
107
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100108/**
109 * env_get_location() - Returns the best env location for a board
110 * @op: operations performed on the environment
111 * @prio: priority between the multiple environments, 0 being the
112 * highest priority
113 *
114 * This will return the preferred environment for the given priority.
Maxime Ripard40c08a62018-01-23 21:17:02 +0100115 * This is overridable by boards if they need to.
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100116 *
117 * All implementations are free to use the operation, the priority and
118 * any other data relevant to their choice, but must take into account
119 * the fact that the lowest prority (0) is the most important location
120 * in the system. The following locations should be returned by order
121 * of descending priorities, from the highest to the lowest priority.
122 *
123 * Returns:
124 * an enum env_location value on success, a negative error code otherwise
125 */
Maxime Ripard40c08a62018-01-23 21:17:02 +0100126__weak enum env_location env_get_location(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600127{
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200128 if (prio >= ARRAY_SIZE(env_locations))
129 return ENVL_UNKNOWN;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100130
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200131 gd->env_load_prio = prio;
Maxime Ripard7d714a22018-01-23 21:16:58 +0100132
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200133 return env_locations[prio];
Simon Glassc9d728d2017-08-03 12:22:00 -0600134}
135
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100136
137/**
138 * env_driver_lookup() - Finds the most suited environment location
139 * @op: operations performed on the environment
140 * @prio: priority between the multiple environments, 0 being the
141 * highest priority
142 *
143 * This will try to find the available environment with the highest
144 * priority in the system.
145 *
146 * Returns:
147 * NULL on error, a pointer to a struct env_driver otherwise
148 */
149static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600150{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100151 enum env_location loc = env_get_location(op, prio);
Simon Glassc9d728d2017-08-03 12:22:00 -0600152 struct env_driver *drv;
153
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100154 if (loc == ENVL_UNKNOWN)
155 return NULL;
156
Maxime Ripard52746c42018-01-23 21:16:51 +0100157 drv = _env_driver_lookup(loc);
Simon Glassc9d728d2017-08-03 12:22:00 -0600158 if (!drv) {
159 debug("%s: No environment driver for location %d\n", __func__,
160 loc);
161 return NULL;
162 }
163
164 return drv;
165}
166
Goldschmidt Simonb2cdef42018-02-09 20:23:17 +0000167__weak int env_get_char_spec(int index)
168{
169 return *(uchar *)(gd->env_addr + index);
170}
171
Simon Glassa69d0f62017-08-03 12:22:06 -0600172int env_get_char(int index)
Simon Glassc9d728d2017-08-03 12:22:00 -0600173{
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600174 if (gd->env_valid == ENV_INVALID)
Simon Glassa69d0f62017-08-03 12:22:06 -0600175 return default_environment[index];
Goldschmidt Simonb2cdef42018-02-09 20:23:17 +0000176 else
177 return env_get_char_spec(index);
Simon Glassc9d728d2017-08-03 12:22:00 -0600178}
179
180int env_load(void)
181{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100182 struct env_driver *drv;
Sam Protsenko293a1722019-01-18 21:19:04 +0200183 int best_prio = -1;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100184 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600185
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100186 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
187 int ret;
188
189 if (!drv->load)
190 continue;
191
Maxime Ripard1d446082018-01-23 21:16:59 +0100192 if (!env_has_inited(drv->location))
193 continue;
194
Maxime Ripard3574ba02018-01-23 21:16:54 +0100195 printf("Loading Environment from %s... ", drv->name);
Sam Protsenko13bbfb42018-07-30 19:19:26 +0300196 /*
197 * In error case, the error message must be printed during
198 * drv->load() in some underlying API, and it must be exactly
199 * one message.
200 */
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100201 ret = drv->load();
Sam Protsenko293a1722019-01-18 21:19:04 +0200202 if (!ret) {
Maxime Ripard3574ba02018-01-23 21:16:54 +0100203 printf("OK\n");
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100204 return 0;
Sam Protsenko293a1722019-01-18 21:19:04 +0200205 } else if (ret == -ENOMSG) {
206 /* Handle "bad CRC" case */
207 if (best_prio == -1)
208 best_prio = prio;
209 } else {
210 debug("Failed (%d)\n", ret);
Sam Protsenko13bbfb42018-07-30 19:19:26 +0300211 }
Simon Glassc9d728d2017-08-03 12:22:00 -0600212 }
213
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200214 /*
215 * In case of invalid environment, we set the 'default' env location
Sam Protsenko293a1722019-01-18 21:19:04 +0200216 * to the best choice, i.e.:
217 * 1. Environment location with bad CRC, if such location was found
218 * 2. Otherwise use the location with highest priority
219 *
220 * This way, next calls to env_save() will restore the environment
221 * at the right place.
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200222 */
Sam Protsenko293a1722019-01-18 21:19:04 +0200223 if (best_prio >= 0)
224 debug("Selecting environment with bad CRC\n");
225 else
226 best_prio = 0;
227 env_get_location(ENVOP_LOAD, best_prio);
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200228
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100229 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600230}
231
232int env_save(void)
233{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100234 struct env_driver *drv;
Simon Glassc9d728d2017-08-03 12:22:00 -0600235
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200236 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
237 if (drv) {
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100238 int ret;
Maxime Ripard9c24dfb2018-01-23 21:16:50 +0100239
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100240 if (!drv->save)
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200241 return -ENODEV;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100242
Maxime Ripard1d446082018-01-23 21:16:59 +0100243 if (!env_has_inited(drv->location))
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200244 return -ENODEV;
Maxime Ripard1d446082018-01-23 21:16:59 +0100245
Maxime Ripard9efac3c2018-01-23 21:16:53 +0100246 printf("Saving Environment to %s... ", drv->name);
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100247 ret = drv->save();
Maxime Ripard3574ba02018-01-23 21:16:54 +0100248 if (ret)
249 printf("Failed (%d)\n", ret);
250 else
251 printf("OK\n");
252
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100253 if (!ret)
254 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600255 }
256
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100257 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600258}
259
Frank Wunderlichcd121bd2019-06-29 11:36:19 +0200260int env_erase(void)
261{
262 struct env_driver *drv;
263
264 drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
265 if (drv) {
266 int ret;
267
268 if (!drv->erase)
269 return -ENODEV;
270
271 if (!env_has_inited(drv->location))
272 return -ENODEV;
273
274 printf("Erasing Environment on %s... ", drv->name);
275 ret = drv->erase();
276 if (ret)
277 printf("Failed (%d)\n", ret);
278 else
279 printf("OK\n");
280
281 if (!ret)
282 return 0;
283 }
284
285 return -ENODEV;
286}
287
Simon Glass6eeae422017-08-03 12:22:05 -0600288int env_init(void)
Simon Glassc9d728d2017-08-03 12:22:00 -0600289{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100290 struct env_driver *drv;
Simon Glass79388222017-08-03 12:22:02 -0600291 int ret = -ENOENT;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100292 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600293
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100294 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard1d446082018-01-23 21:16:59 +0100295 if (!drv->init || !(ret = drv->init()))
296 env_set_inited(drv->location);
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100297
Maxime Ripard1d446082018-01-23 21:16:59 +0100298 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100299 drv->name, ret);
300 }
301
302 if (!prio)
303 return -ENODEV;
304
Simon Glass79388222017-08-03 12:22:02 -0600305 if (ret == -ENOENT) {
306 gd->env_addr = (ulong)&default_environment[0];
Tom Rinieeba55c2017-08-19 22:27:57 -0400307 gd->env_valid = ENV_VALID;
Simon Glass79388222017-08-03 12:22:02 -0600308
309 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600310 }
311
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100312 return ret;
Simon Glassc9d728d2017-08-03 12:22:00 -0600313}