]> git.ipfire.org Git - thirdparty/u-boot.git/blame - env/env.c
Merge git://git.denx.de/u-boot-mpc83xx
[thirdparty/u-boot.git] / env / env.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
c9d728dd
SG
2/*
3 * Copyright (C) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
c9d728dd
SG
5 */
6
7#include <common.h>
8#include <environment.h>
9
10DECLARE_GLOBAL_DATA_PTR;
11
7bcdf195
SDPP
12#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
52746c43 33static struct env_driver *_env_driver_lookup(enum env_location loc)
c9d728dd
SG
34{
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
7d714a24
MR
49static 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
9a6a311d
YL
74#ifdef CONFIG_ENV_IS_IN_SATA
75 ENVL_ESATA,
76#endif
7d714a24
MR
77#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
1d446087
MR
88static 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
8a3a7e22
MR
105/**
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.
40c08a68 112 * This is overridable by boards if they need to.
8a3a7e22
MR
113 *
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 */
40c08a68 123__weak enum env_location env_get_location(enum env_operation op, int prio)
c9d728dd 124{
d30ba231
NF
125 if (prio >= ARRAY_SIZE(env_locations))
126 return ENVL_UNKNOWN;
127
128 gd->env_load_prio = prio;
8a3a7e22 129
d30ba231 130 return env_locations[prio];
c9d728dd
SG
131}
132
8a3a7e22
MR
133
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)
c9d728dd 147{
8a3a7e22 148 enum env_location loc = env_get_location(op, prio);
c9d728dd
SG
149 struct env_driver *drv;
150
8a3a7e22
MR
151 if (loc == ENVL_UNKNOWN)
152 return NULL;
153
52746c43 154 drv = _env_driver_lookup(loc);
c9d728dd
SG
155 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
b2cdef48 164__weak int env_get_char_spec(int index)
c9d728dd 165{
b2cdef48
GS
166 return *(uchar *)(gd->env_addr + index);
167}
c9d728dd 168
b2cdef48
GS
169int env_get_char(int index)
170{
2d7cb5b4 171 if (gd->env_valid == ENV_INVALID)
a69d0f60 172 return default_environment[index];
b2cdef48
GS
173 else
174 return env_get_char_spec(index);
c9d728dd
SG
175}
176
177int env_load(void)
178{
8a3a7e22
MR
179 struct env_driver *drv;
180 int prio;
c9d728dd 181
8a3a7e22
MR
182 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
183 int ret;
184
185 if (!drv->load)
186 continue;
187
1d446087
MR
188 if (!env_has_inited(drv->location))
189 continue;
190
3574ba01 191 printf("Loading Environment from %s... ", drv->name);
13bbfb4a
SP
192 /*
193 * In error case, the error message must be printed during
194 * drv->load() in some underlying API, and it must be exactly
195 * one message.
196 */
8a3a7e22 197 ret = drv->load();
13bbfb4a
SP
198 if (ret) {
199 debug("Failed (%d)\n", ret);
200 } else {
3574ba01 201 printf("OK\n");
8a3a7e22 202 return 0;
13bbfb4a 203 }
c9d728dd
SG
204 }
205
d30ba231
NF
206 /*
207 * In case of invalid environment, we set the 'default' env location
208 * to the highest priority. In this way, next calls to env_save()
209 * will restore the environment at the right place.
210 */
211 env_get_location(ENVOP_LOAD, 0);
212
8a3a7e22 213 return -ENODEV;
c9d728dd
SG
214}
215
216int env_save(void)
217{
8a3a7e22 218 struct env_driver *drv;
c9d728dd 219
d30ba231
NF
220 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
221 if (drv) {
8a3a7e22
MR
222 int ret;
223
224 if (!drv->save)
d30ba231 225 return -ENODEV;
8a3a7e22 226
1d446087 227 if (!env_has_inited(drv->location))
d30ba231 228 return -ENODEV;
1d446087 229
9efac3c8 230 printf("Saving Environment to %s... ", drv->name);
8a3a7e22 231 ret = drv->save();
3574ba01
MR
232 if (ret)
233 printf("Failed (%d)\n", ret);
234 else
235 printf("OK\n");
236
8a3a7e22
MR
237 if (!ret)
238 return 0;
c9d728dd
SG
239 }
240
8a3a7e22 241 return -ENODEV;
c9d728dd
SG
242}
243
6eeae424 244int env_init(void)
c9d728dd 245{
8a3a7e22 246 struct env_driver *drv;
7938822a 247 int ret = -ENOENT;
8a3a7e22
MR
248 int prio;
249
250 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
1d446087
MR
251 if (!drv->init || !(ret = drv->init()))
252 env_set_inited(drv->location);
8a3a7e22 253
1d446087 254 debug("%s: Environment %s init done (ret=%d)\n", __func__,
8a3a7e22
MR
255 drv->name, ret);
256 }
257
258 if (!prio)
259 return -ENODEV;
260
7938822a
SG
261 if (ret == -ENOENT) {
262 gd->env_addr = (ulong)&default_environment[0];
eeba55cb 263 gd->env_valid = ENV_VALID;
7938822a
SG
264
265 return 0;
c9d728dd
SG
266 }
267
8a3a7e22 268 return ret;
c9d728dd 269}