1
2
3
4
5
6
7#include <common.h>
8#include <environment.h>
9
10DECLARE_GLOBAL_DATA_PTR;
11
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
33static struct env_driver *_env_driver_lookup(enum env_location loc)
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
46 return NULL;
47}
48
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
74#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
75 ENVL_SPI_FLASH,
76#endif
77#ifdef CONFIG_ENV_IS_IN_UBI
78 ENVL_UBI,
79#endif
80#ifdef CONFIG_ENV_IS_NOWHERE
81 ENVL_NOWHERE,
82#endif
83};
84
85static bool env_has_inited(enum env_location location)
86{
87 return gd->env_has_init & BIT(location);
88}
89
90static void env_set_inited(enum env_location location)
91{
92
93
94
95
96
97 BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG);
98
99 gd->env_has_init |= BIT(location);
100}
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120__weak enum env_location env_get_location(enum env_operation op, int prio)
121{
122 if (prio >= ARRAY_SIZE(env_locations))
123 return ENVL_UNKNOWN;
124
125 gd->env_load_prio = prio;
126
127 return env_locations[prio];
128}
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
144{
145 enum env_location loc = env_get_location(op, prio);
146 struct env_driver *drv;
147
148 if (loc == ENVL_UNKNOWN)
149 return NULL;
150
151 drv = _env_driver_lookup(loc);
152 if (!drv) {
153 debug("%s: No environment driver for location %d\n", __func__,
154 loc);
155 return NULL;
156 }
157
158 return drv;
159}
160
161__weak int env_get_char_spec(int index)
162{
163 return *(uchar *)(gd->env_addr + index);
164}
165
166int env_get_char(int index)
167{
168 if (gd->env_valid == ENV_INVALID)
169 return default_environment[index];
170 else
171 return env_get_char_spec(index);
172}
173
174int env_load(void)
175{
176 struct env_driver *drv;
177 int prio;
178
179 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
180 int ret;
181
182 if (!drv->load)
183 continue;
184
185 if (!env_has_inited(drv->location))
186 continue;
187
188 printf("Loading Environment from %s... ", drv->name);
189
190
191
192
193
194 ret = drv->load();
195 if (ret) {
196 debug("Failed (%d)\n", ret);
197 } else {
198 printf("OK\n");
199 return 0;
200 }
201 }
202
203
204
205
206
207
208 env_get_location(ENVOP_LOAD, 0);
209
210 return -ENODEV;
211}
212
213int env_save(void)
214{
215 struct env_driver *drv;
216
217 drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
218 if (drv) {
219 int ret;
220
221 if (!drv->save)
222 return -ENODEV;
223
224 if (!env_has_inited(drv->location))
225 return -ENODEV;
226
227 printf("Saving Environment to %s... ", drv->name);
228 ret = drv->save();
229 if (ret)
230 printf("Failed (%d)\n", ret);
231 else
232 printf("OK\n");
233
234 if (!ret)
235 return 0;
236 }
237
238 return -ENODEV;
239}
240
241int env_init(void)
242{
243 struct env_driver *drv;
244 int ret = -ENOENT;
245 int prio;
246
247 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
248 if (!drv->init || !(ret = drv->init()))
249 env_set_inited(drv->location);
250
251 debug("%s: Environment %s init done (ret=%d)\n", __func__,
252 drv->name, ret);
253 }
254
255 if (!prio)
256 return -ENODEV;
257
258 if (ret == -ENOENT) {
259 gd->env_addr = (ulong)&default_environment[0];
260 gd->env_valid = ENV_VALID;
261
262 return 0;
263 }
264
265 return ret;
266}
267