blob: 4b417b90a2912c201f201514e840d644f03e45e7 [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>
8#include <environment.h>
9
10DECLARE_GLOBAL_DATA_PTR;
11
Siva Durga Prasad Paladugu7bcdf192018-04-13 07:57:21 +020012#if defined(CONFIG_NEEDS_MANUAL_RELOC)
13void env_fix_drivers(void)
14{
15 struct env_driver *drv;
16 const int n_ents = ll_entry_count(struct env_driver, env_driver);
17 struct env_driver *entry;
18
19 drv = ll_entry_start(struct env_driver, env_driver);
20 for (entry = drv; entry != drv + n_ents; entry++) {
21 if (entry->name)
22 entry->name += gd->reloc_off;
23 if (entry->load)
24 entry->load += gd->reloc_off;
25 if (entry->save)
26 entry->save += gd->reloc_off;
27 if (entry->init)
28 entry->init += gd->reloc_off;
29 }
30}
31#endif
32
Maxime Ripard52746c42018-01-23 21:16:51 +010033static struct env_driver *_env_driver_lookup(enum env_location loc)
Simon Glassc9d728d2017-08-03 12:22:00 -060034{
35 struct env_driver *drv;
36 const int n_ents = ll_entry_count(struct env_driver, env_driver);
37 struct env_driver *entry;
38
39 drv = ll_entry_start(struct env_driver, env_driver);
40 for (entry = drv; entry != drv + n_ents; entry++) {
41 if (loc == entry->location)
42 return entry;
43 }
44
45 /* Not found */
46 return NULL;
47}
48
Maxime Ripard7d714a22018-01-23 21:16:58 +010049static enum env_location env_locations[] = {
50#ifdef CONFIG_ENV_IS_IN_EEPROM
51 ENVL_EEPROM,
52#endif
53#ifdef CONFIG_ENV_IS_IN_EXT4
54 ENVL_EXT4,
55#endif
56#ifdef CONFIG_ENV_IS_IN_FAT
57 ENVL_FAT,
58#endif
59#ifdef CONFIG_ENV_IS_IN_FLASH
60 ENVL_FLASH,
61#endif
62#ifdef CONFIG_ENV_IS_IN_MMC
63 ENVL_MMC,
64#endif
65#ifdef CONFIG_ENV_IS_IN_NAND
66 ENVL_NAND,
67#endif
68#ifdef CONFIG_ENV_IS_IN_NVRAM
69 ENVL_NVRAM,
70#endif
71#ifdef CONFIG_ENV_IS_IN_REMOTE
72 ENVL_REMOTE,
73#endif
Ye Li9a6a3112019-01-04 09:34:24 +000074#ifdef CONFIG_ENV_IS_IN_SATA
75 ENVL_ESATA,
76#endif
Maxime Ripard7d714a22018-01-23 21:16:58 +010077#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
78 ENVL_SPI_FLASH,
79#endif
80#ifdef CONFIG_ENV_IS_IN_UBI
81 ENVL_UBI,
82#endif
83#ifdef CONFIG_ENV_IS_NOWHERE
84 ENVL_NOWHERE,
85#endif
86};
87
Maxime Ripard1d446082018-01-23 21:16:59 +010088static bool env_has_inited(enum env_location location)
89{
90 return gd->env_has_init & BIT(location);
91}
92
93static void env_set_inited(enum env_location location)
94{
95 /*
96 * We're using a 32-bits bitmask stored in gd (env_has_init)
97 * using the above enum value as the bit index. We need to
98 * make sure that we're not overflowing it.
99 */
100 BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG);
101
102 gd->env_has_init |= BIT(location);
103}
104
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100105/**
106 * env_get_location() - Returns the best env location for a board
107 * @op: operations performed on the environment
108 * @prio: priority between the multiple environments, 0 being the
109 * highest priority
110 *
111 * This will return the preferred environment for the given priority.
Maxime Ripard40c08a62018-01-23 21:17:02 +0100112 * This is overridable by boards if they need to.
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100113 *
114 * All implementations are free to use the operation, the priority and
115 * any other data relevant to their choice, but must take into account
116 * the fact that the lowest prority (0) is the most important location
117 * in the system. The following locations should be returned by order
118 * of descending priorities, from the highest to the lowest priority.
119 *
120 * Returns:
121 * an enum env_location value on success, a negative error code otherwise
122 */
Maxime Ripard40c08a62018-01-23 21:17:02 +0100123__weak enum env_location env_get_location(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600124{
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200125 if (prio >= ARRAY_SIZE(env_locations))
126 return ENVL_UNKNOWN;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100127
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200128 gd->env_load_prio = prio;
Maxime Ripard7d714a22018-01-23 21:16:58 +0100129
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200130 return env_locations[prio];
Simon Glassc9d728d2017-08-03 12:22:00 -0600131}
132
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100133
134/**
135 * env_driver_lookup() - Finds the most suited environment location
136 * @op: operations performed on the environment
137 * @prio: priority between the multiple environments, 0 being the
138 * highest priority
139 *
140 * This will try to find the available environment with the highest
141 * priority in the system.
142 *
143 * Returns:
144 * NULL on error, a pointer to a struct env_driver otherwise
145 */
146static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600147{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100148 enum env_location loc = env_get_location(op, prio);
Simon Glassc9d728d2017-08-03 12:22:00 -0600149 struct env_driver *drv;
150
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100151 if (loc == ENVL_UNKNOWN)
152 return NULL;
153
Maxime Ripard52746c42018-01-23 21:16:51 +0100154 drv = _env_driver_lookup(loc);
Simon Glassc9d728d2017-08-03 12:22:00 -0600155 if (!drv) {
156 debug("%s: No environment driver for location %d\n", __func__,
157 loc);
158 return NULL;
159 }
160
161 return drv;
162}
163
Goldschmidt Simonb2cdef42018-02-09 20:23:17 +0000164__weak int env_get_char_spec(int index)
165{
166 return *(uchar *)(gd->env_addr + index);
167}
168
Simon Glassa69d0f62017-08-03 12:22:06 -0600169int env_get_char(int index)
Simon Glassc9d728d2017-08-03 12:22:00 -0600170{
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600171 if (gd->env_valid == ENV_INVALID)
Simon Glassa69d0f62017-08-03 12:22:06 -0600172 return default_environment[index];
Goldschmidt Simonb2cdef42018-02-09 20:23:17 +0000173 else
174 return env_get_char_spec(index);
Simon Glassc9d728d2017-08-03 12:22:00 -0600175}
176
177int env_load(void)
178{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100179 struct env_driver *drv;
Sam Protsenko293a1722019-01-18 21:19:04 +0200180 int best_prio = -1;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100181 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600182
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100183 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
184 int ret;
185
186 if (!drv->load)
187 continue;
188
Maxime Ripard1d446082018-01-23 21:16:59 +0100189 if (!env_has_inited(drv->location))
190 continue;
191
Maxime Ripard3574ba02018-01-23 21:16:54 +0100192 printf("Loading Environment from %s... ", drv->name);
Sam Protsenko13bbfb42018-07-30 19:19:26 +0300193 /*
194 * In error case, the error message must be printed during
195 * drv->load() in some underlying API, and it must be exactly
196 * one message.
197 */
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100198 ret = drv->load();
Sam Protsenko293a1722019-01-18 21:19:04 +0200199 if (!ret) {
Maxime Ripard3574ba02018-01-23 21:16:54 +0100200 printf("OK\n");
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100201 return 0;
Sam Protsenko293a1722019-01-18 21:19:04 +0200202 } else if (ret == -ENOMSG) {
203 /* Handle "bad CRC" case */
204 if (best_prio == -1)
205 best_prio = prio;
206 } else {
207 debug("Failed (%d)\n", ret);
Sam Protsenko13bbfb42018-07-30 19:19:26 +0300208 }
Simon Glassc9d728d2017-08-03 12:22:00 -0600209 }
210
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200211 /*
212 * In case of invalid environment, we set the 'default' env location
Sam Protsenko293a1722019-01-18 21:19:04 +0200213 * to the best choice, i.e.:
214 * 1. Environment location with bad CRC, if such location was found
215 * 2. Otherwise use the location with highest priority
216 *
217 * This way, next calls to env_save() will restore the environment
218 * at the right place.
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200219 */
Sam Protsenko293a1722019-01-18 21:19:04 +0200220 if (best_prio >= 0)
221 debug("Selecting environment with bad CRC\n");
222 else
223 best_prio = 0;
224 env_get_location(ENVOP_LOAD, best_prio);
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200225
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100226 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600227}
228
229int env_save(void)
230{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100231 struct env_driver *drv;
Simon Glassc9d728d2017-08-03 12:22:00 -0600232
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200233 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
234 if (drv) {
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100235 int ret;
Maxime Ripard9c24dfb2018-01-23 21:16:50 +0100236
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100237 if (!drv->save)
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200238 return -ENODEV;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100239
Maxime Ripard1d446082018-01-23 21:16:59 +0100240 if (!env_has_inited(drv->location))
Nicholas Faustinid30ba232018-07-23 10:01:07 +0200241 return -ENODEV;
Maxime Ripard1d446082018-01-23 21:16:59 +0100242
Maxime Ripard9efac3c2018-01-23 21:16:53 +0100243 printf("Saving Environment to %s... ", drv->name);
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100244 ret = drv->save();
Maxime Ripard3574ba02018-01-23 21:16:54 +0100245 if (ret)
246 printf("Failed (%d)\n", ret);
247 else
248 printf("OK\n");
249
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100250 if (!ret)
251 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600252 }
253
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100254 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600255}
256
Simon Glass6eeae422017-08-03 12:22:05 -0600257int env_init(void)
Simon Glassc9d728d2017-08-03 12:22:00 -0600258{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100259 struct env_driver *drv;
Simon Glass79388222017-08-03 12:22:02 -0600260 int ret = -ENOENT;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100261 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600262
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100263 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard1d446082018-01-23 21:16:59 +0100264 if (!drv->init || !(ret = drv->init()))
265 env_set_inited(drv->location);
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100266
Maxime Ripard1d446082018-01-23 21:16:59 +0100267 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100268 drv->name, ret);
269 }
270
271 if (!prio)
272 return -ENODEV;
273
Simon Glass79388222017-08-03 12:22:02 -0600274 if (ret == -ENOENT) {
275 gd->env_addr = (ulong)&default_environment[0];
Tom Rinieeba55c2017-08-19 22:27:57 -0400276 gd->env_valid = ENV_VALID;
Simon Glass79388222017-08-03 12:22:02 -0600277
278 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600279 }
280
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100281 return ret;
Simon Glassc9d728d2017-08-03 12:22:00 -0600282}