]> git.ipfire.org Git - people/ms/u-boot.git/blame - env/env.c
env: Support multiple environments
[people/ms/u-boot.git] / env / env.c
CommitLineData
c9d728dd
SG
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
11DECLARE_GLOBAL_DATA_PTR;
12
52746c43 13static struct env_driver *_env_driver_lookup(enum env_location loc)
c9d728dd
SG
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
7d714a24
MR
29static 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
65static enum env_location env_load_location = ENVL_UNKNOWN;
66
8a3a7e22
MR
67/**
68 * env_get_location() - Returns the best env location for a board
69 * @op: operations performed on the environment
70 * @prio: priority between the multiple environments, 0 being the
71 * highest priority
72 *
73 * This will return the preferred environment for the given priority.
74 *
75 * All implementations are free to use the operation, the priority and
76 * any other data relevant to their choice, but must take into account
77 * the fact that the lowest prority (0) is the most important location
78 * in the system. The following locations should be returned by order
79 * of descending priorities, from the highest to the lowest priority.
80 *
81 * Returns:
82 * an enum env_location value on success, a negative error code otherwise
83 */
84static enum env_location env_get_location(enum env_operation op, int prio)
c9d728dd 85{
7d714a24
MR
86 switch (op) {
87 case ENVOP_GET_CHAR:
88 case ENVOP_INIT:
89 case ENVOP_LOAD:
90 if (prio >= ARRAY_SIZE(env_locations))
91 return ENVL_UNKNOWN;
92
93 env_load_location = env_locations[prio];
94 return env_load_location;
95
96 case ENVOP_SAVE:
97 return env_load_location;
98 }
8a3a7e22 99
7d714a24 100 return ENVL_UNKNOWN;
c9d728dd
SG
101}
102
8a3a7e22
MR
103
104/**
105 * env_driver_lookup() - Finds the most suited environment location
106 * @op: operations performed on the environment
107 * @prio: priority between the multiple environments, 0 being the
108 * highest priority
109 *
110 * This will try to find the available environment with the highest
111 * priority in the system.
112 *
113 * Returns:
114 * NULL on error, a pointer to a struct env_driver otherwise
115 */
116static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
c9d728dd 117{
8a3a7e22 118 enum env_location loc = env_get_location(op, prio);
c9d728dd
SG
119 struct env_driver *drv;
120
8a3a7e22
MR
121 if (loc == ENVL_UNKNOWN)
122 return NULL;
123
52746c43 124 drv = _env_driver_lookup(loc);
c9d728dd
SG
125 if (!drv) {
126 debug("%s: No environment driver for location %d\n", __func__,
127 loc);
128 return NULL;
129 }
130
131 return drv;
132}
133
a69d0f60 134int env_get_char(int index)
c9d728dd 135{
8a3a7e22
MR
136 struct env_driver *drv;
137 int prio;
c9d728dd 138
2d7cb5b4 139 if (gd->env_valid == ENV_INVALID)
a69d0f60 140 return default_environment[index];
8a3a7e22
MR
141
142 for (prio = 0; (drv = env_driver_lookup(ENVOP_GET_CHAR, prio)); prio++) {
143 int ret;
144
145 if (!drv->get_char)
146 continue;
147
148 ret = drv->get_char(index);
149 if (!ret)
150 return 0;
151
152 debug("%s: Environment %s failed to load (err=%d)\n", __func__,
153 drv->name, ret);
c9d728dd
SG
154 }
155
8a3a7e22 156 return -ENODEV;
c9d728dd
SG
157}
158
159int env_load(void)
160{
8a3a7e22
MR
161 struct env_driver *drv;
162 int prio;
c9d728dd 163
8a3a7e22
MR
164 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
165 int ret;
166
167 if (!drv->load)
168 continue;
169
3574ba01 170 printf("Loading Environment from %s... ", drv->name);
8a3a7e22 171 ret = drv->load();
3574ba01
MR
172 if (ret)
173 printf("Failed (%d)\n", ret);
174 else
175 printf("OK\n");
176
8a3a7e22
MR
177 if (!ret)
178 return 0;
c9d728dd
SG
179 }
180
8a3a7e22 181 return -ENODEV;
c9d728dd
SG
182}
183
184int env_save(void)
185{
8a3a7e22
MR
186 struct env_driver *drv;
187 int prio;
c9d728dd 188
8a3a7e22
MR
189 for (prio = 0; (drv = env_driver_lookup(ENVOP_SAVE, prio)); prio++) {
190 int ret;
191
192 if (!drv->save)
193 continue;
194
9efac3c8 195 printf("Saving Environment to %s... ", drv->name);
8a3a7e22 196 ret = drv->save();
3574ba01
MR
197 if (ret)
198 printf("Failed (%d)\n", ret);
199 else
200 printf("OK\n");
201
8a3a7e22
MR
202 if (!ret)
203 return 0;
c9d728dd
SG
204 }
205
8a3a7e22 206 return -ENODEV;
c9d728dd
SG
207}
208
6eeae424 209int env_init(void)
c9d728dd 210{
8a3a7e22 211 struct env_driver *drv;
7938822a 212 int ret = -ENOENT;
8a3a7e22
MR
213 int prio;
214
215 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
216 if (!drv->init)
217 continue;
c9d728dd 218
7938822a 219 ret = drv->init();
8a3a7e22
MR
220 if (!ret)
221 return 0;
222
223 debug("%s: Environment %s failed to init (err=%d)\n", __func__,
224 drv->name, ret);
225 }
226
227 if (!prio)
228 return -ENODEV;
229
7938822a
SG
230 if (ret == -ENOENT) {
231 gd->env_addr = (ulong)&default_environment[0];
eeba55cb 232 gd->env_valid = ENV_VALID;
7938822a
SG
233
234 return 0;
c9d728dd
SG
235 }
236
8a3a7e22 237 return ret;
c9d728dd 238}