env: Initialise all the environments
[oweals/u-boot.git] / env / env.c
1 /*
2  * Copyright (C) 2017 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <environment.h>
10
11 DECLARE_GLOBAL_DATA_PTR;
12
13 static struct env_driver *_env_driver_lookup(enum env_location loc)
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 (loc == entry->location)
22                         return entry;
23         }
24
25         /* Not found */
26         return NULL;
27 }
28
29 static enum env_location env_locations[] = {
30 #ifdef CONFIG_ENV_IS_IN_EEPROM
31         ENVL_EEPROM,
32 #endif
33 #ifdef CONFIG_ENV_IS_IN_EXT4
34         ENVL_EXT4,
35 #endif
36 #ifdef CONFIG_ENV_IS_IN_FAT
37         ENVL_FAT,
38 #endif
39 #ifdef CONFIG_ENV_IS_IN_FLASH
40         ENVL_FLASH,
41 #endif
42 #ifdef CONFIG_ENV_IS_IN_MMC
43         ENVL_MMC,
44 #endif
45 #ifdef CONFIG_ENV_IS_IN_NAND
46         ENVL_NAND,
47 #endif
48 #ifdef CONFIG_ENV_IS_IN_NVRAM
49         ENVL_NVRAM,
50 #endif
51 #ifdef CONFIG_ENV_IS_IN_REMOTE
52         ENVL_REMOTE,
53 #endif
54 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
55         ENVL_SPI_FLASH,
56 #endif
57 #ifdef CONFIG_ENV_IS_IN_UBI
58         ENVL_UBI,
59 #endif
60 #ifdef CONFIG_ENV_IS_NOWHERE
61         ENVL_NOWHERE,
62 #endif
63 };
64
65 static enum env_location env_load_location = ENVL_UNKNOWN;
66
67 static bool env_has_inited(enum env_location location)
68 {
69         return gd->env_has_init & BIT(location);
70 }
71
72 static void env_set_inited(enum env_location location)
73 {
74         /*
75          * We're using a 32-bits bitmask stored in gd (env_has_init)
76          * using the above enum value as the bit index. We need to
77          * make sure that we're not overflowing it.
78          */
79         BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG);
80
81         gd->env_has_init |= BIT(location);
82 }
83
84 /**
85  * env_get_location() - Returns the best env location for a board
86  * @op: operations performed on the environment
87  * @prio: priority between the multiple environments, 0 being the
88  *        highest priority
89  *
90  * This will return the preferred environment for the given priority.
91  *
92  * All implementations are free to use the operation, the priority and
93  * any other data relevant to their choice, but must take into account
94  * the fact that the lowest prority (0) is the most important location
95  * in the system. The following locations should be returned by order
96  * of descending priorities, from the highest to the lowest priority.
97  *
98  * Returns:
99  * an enum env_location value on success, a negative error code otherwise
100  */
101 static enum env_location env_get_location(enum env_operation op, int prio)
102 {
103         switch (op) {
104         case ENVOP_GET_CHAR:
105         case ENVOP_INIT:
106         case ENVOP_LOAD:
107                 if (prio >= ARRAY_SIZE(env_locations))
108                         return ENVL_UNKNOWN;
109
110                 env_load_location = env_locations[prio];
111                 return env_load_location;
112
113         case ENVOP_SAVE:
114                 return env_load_location;
115         }
116
117         return ENVL_UNKNOWN;
118 }
119
120
121 /**
122  * env_driver_lookup() - Finds the most suited environment location
123  * @op: operations performed on the environment
124  * @prio: priority between the multiple environments, 0 being the
125  *        highest priority
126  *
127  * This will try to find the available environment with the highest
128  * priority in the system.
129  *
130  * Returns:
131  * NULL on error, a pointer to a struct env_driver otherwise
132  */
133 static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
134 {
135         enum env_location loc = env_get_location(op, prio);
136         struct env_driver *drv;
137
138         if (loc == ENVL_UNKNOWN)
139                 return NULL;
140
141         drv = _env_driver_lookup(loc);
142         if (!drv) {
143                 debug("%s: No environment driver for location %d\n", __func__,
144                       loc);
145                 return NULL;
146         }
147
148         return drv;
149 }
150
151 int env_get_char(int index)
152 {
153         struct env_driver *drv;
154         int prio;
155
156         if (gd->env_valid == ENV_INVALID)
157                 return default_environment[index];
158
159         for (prio = 0; (drv = env_driver_lookup(ENVOP_GET_CHAR, prio)); prio++) {
160                 int ret;
161
162                 if (!drv->get_char)
163                         continue;
164
165                 if (!env_has_inited(drv->location))
166                         continue;
167
168                 ret = drv->get_char(index);
169                 if (!ret)
170                         return 0;
171
172                 debug("%s: Environment %s failed to load (err=%d)\n", __func__,
173                       drv->name, ret);
174         }
175
176         return -ENODEV;
177 }
178
179 int env_load(void)
180 {
181         struct env_driver *drv;
182         int prio;
183
184         for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
185                 int ret;
186
187                 if (!drv->load)
188                         continue;
189
190                 if (!env_has_inited(drv->location))
191                         continue;
192
193                 printf("Loading Environment from %s... ", drv->name);
194                 ret = drv->load();
195                 if (ret)
196                         printf("Failed (%d)\n", ret);
197                 else
198                         printf("OK\n");
199
200                 if (!ret)
201                         return 0;
202         }
203
204         return -ENODEV;
205 }
206
207 int env_save(void)
208 {
209         struct env_driver *drv;
210         int prio;
211
212         for (prio = 0; (drv = env_driver_lookup(ENVOP_SAVE, prio)); prio++) {
213                 int ret;
214
215                 if (!drv->save)
216                         continue;
217
218                 if (!env_has_inited(drv->location))
219                         continue;
220
221                 printf("Saving Environment to %s... ", drv->name);
222                 ret = drv->save();
223                 if (ret)
224                         printf("Failed (%d)\n", ret);
225                 else
226                         printf("OK\n");
227
228                 if (!ret)
229                         return 0;
230         }
231
232         return -ENODEV;
233 }
234
235 int env_init(void)
236 {
237         struct env_driver *drv;
238         int ret = -ENOENT;
239         int prio;
240
241         for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
242                 if (!drv->init || !(ret = drv->init()))
243                         env_set_inited(drv->location);
244
245                 debug("%s: Environment %s init done (ret=%d)\n", __func__,
246                       drv->name, ret);
247         }
248
249         if (!prio)
250                 return -ENODEV;
251
252         if (ret == -ENOENT) {
253                 gd->env_addr = (ulong)&default_environment[0];
254                 gd->env_valid = ENV_VALID;
255
256                 return 0;
257         }
258
259         return ret;
260 }