blob: 3795dbc24e2bcbb80b066146021f8ea085867a32 [file] [log] [blame]
Simon Glassc9d728d2017-08-03 12:22:00 -06001/*
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
Maxime Ripard52746c42018-01-23 21:16:51 +010013static struct env_driver *_env_driver_lookup(enum env_location loc)
Simon Glassc9d728d2017-08-03 12:22:00 -060014{
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
Maxime Ripard7d714a22018-01-23 21:16:58 +010029static 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
Maxime Ripard1d446082018-01-23 21:16:59 +010065static bool env_has_inited(enum env_location location)
66{
67 return gd->env_has_init & BIT(location);
68}
69
70static void env_set_inited(enum env_location location)
71{
72 /*
73 * We're using a 32-bits bitmask stored in gd (env_has_init)
74 * using the above enum value as the bit index. We need to
75 * make sure that we're not overflowing it.
76 */
77 BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG);
78
79 gd->env_has_init |= BIT(location);
80}
81
Maxime Ripard8a3a7e22018-01-23 21:16:52 +010082/**
83 * env_get_location() - Returns the best env location for a board
84 * @op: operations performed on the environment
85 * @prio: priority between the multiple environments, 0 being the
86 * highest priority
87 *
88 * This will return the preferred environment for the given priority.
Maxime Ripard40c08a62018-01-23 21:17:02 +010089 * This is overridable by boards if they need to.
Maxime Ripard8a3a7e22018-01-23 21:16:52 +010090 *
91 * All implementations are free to use the operation, the priority and
92 * any other data relevant to their choice, but must take into account
93 * the fact that the lowest prority (0) is the most important location
94 * in the system. The following locations should be returned by order
95 * of descending priorities, from the highest to the lowest priority.
96 *
97 * Returns:
98 * an enum env_location value on success, a negative error code otherwise
99 */
Maxime Ripard40c08a62018-01-23 21:17:02 +0100100__weak enum env_location env_get_location(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600101{
Maxime Ripard7d714a22018-01-23 21:16:58 +0100102 switch (op) {
103 case ENVOP_GET_CHAR:
104 case ENVOP_INIT:
105 case ENVOP_LOAD:
106 if (prio >= ARRAY_SIZE(env_locations))
107 return ENVL_UNKNOWN;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100108
York Sune1caa582018-02-07 14:17:11 -0800109 gd->env_load_location = env_locations[prio];
110 return gd->env_load_location;
Maxime Ripard7d714a22018-01-23 21:16:58 +0100111
112 case ENVOP_SAVE:
York Sune1caa582018-02-07 14:17:11 -0800113 return gd->env_load_location;
Maxime Ripard7d714a22018-01-23 21:16:58 +0100114 }
115
116 return ENVL_UNKNOWN;
Simon Glassc9d728d2017-08-03 12:22:00 -0600117}
118
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100119
120/**
121 * env_driver_lookup() - Finds the most suited environment location
122 * @op: operations performed on the environment
123 * @prio: priority between the multiple environments, 0 being the
124 * highest priority
125 *
126 * This will try to find the available environment with the highest
127 * priority in the system.
128 *
129 * Returns:
130 * NULL on error, a pointer to a struct env_driver otherwise
131 */
132static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
Simon Glassc9d728d2017-08-03 12:22:00 -0600133{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100134 enum env_location loc = env_get_location(op, prio);
Simon Glassc9d728d2017-08-03 12:22:00 -0600135 struct env_driver *drv;
136
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100137 if (loc == ENVL_UNKNOWN)
138 return NULL;
139
Maxime Ripard52746c42018-01-23 21:16:51 +0100140 drv = _env_driver_lookup(loc);
Simon Glassc9d728d2017-08-03 12:22:00 -0600141 if (!drv) {
142 debug("%s: No environment driver for location %d\n", __func__,
143 loc);
144 return NULL;
145 }
146
147 return drv;
148}
149
Goldschmidt Simonb2cdef42018-02-09 20:23:17 +0000150__weak int env_get_char_spec(int index)
151{
152 return *(uchar *)(gd->env_addr + index);
153}
154
Simon Glassa69d0f62017-08-03 12:22:06 -0600155int env_get_char(int index)
Simon Glassc9d728d2017-08-03 12:22:00 -0600156{
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600157 if (gd->env_valid == ENV_INVALID)
Simon Glassa69d0f62017-08-03 12:22:06 -0600158 return default_environment[index];
Goldschmidt Simonb2cdef42018-02-09 20:23:17 +0000159 else
160 return env_get_char_spec(index);
Simon Glassc9d728d2017-08-03 12:22:00 -0600161}
162
163int env_load(void)
164{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100165 struct env_driver *drv;
166 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600167
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100168 for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
169 int ret;
170
171 if (!drv->load)
172 continue;
173
Maxime Ripard1d446082018-01-23 21:16:59 +0100174 if (!env_has_inited(drv->location))
175 continue;
176
Maxime Ripard3574ba02018-01-23 21:16:54 +0100177 printf("Loading Environment from %s... ", drv->name);
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100178 ret = drv->load();
Maxime Ripard3574ba02018-01-23 21:16:54 +0100179 if (ret)
180 printf("Failed (%d)\n", ret);
181 else
182 printf("OK\n");
183
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100184 if (!ret)
185 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600186 }
187
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100188 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600189}
190
191int env_save(void)
192{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100193 struct env_driver *drv;
194 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600195
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100196 for (prio = 0; (drv = env_driver_lookup(ENVOP_SAVE, prio)); prio++) {
197 int ret;
Maxime Ripard9c24dfb2018-01-23 21:16:50 +0100198
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100199 if (!drv->save)
200 continue;
201
Maxime Ripard1d446082018-01-23 21:16:59 +0100202 if (!env_has_inited(drv->location))
203 continue;
204
Maxime Ripard9efac3c2018-01-23 21:16:53 +0100205 printf("Saving Environment to %s... ", drv->name);
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100206 ret = drv->save();
Maxime Ripard3574ba02018-01-23 21:16:54 +0100207 if (ret)
208 printf("Failed (%d)\n", ret);
209 else
210 printf("OK\n");
211
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100212 if (!ret)
213 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600214 }
215
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100216 return -ENODEV;
Simon Glassc9d728d2017-08-03 12:22:00 -0600217}
218
Simon Glass6eeae422017-08-03 12:22:05 -0600219int env_init(void)
Simon Glassc9d728d2017-08-03 12:22:00 -0600220{
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100221 struct env_driver *drv;
Simon Glass79388222017-08-03 12:22:02 -0600222 int ret = -ENOENT;
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100223 int prio;
Simon Glassc9d728d2017-08-03 12:22:00 -0600224
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100225 for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
Maxime Ripard1d446082018-01-23 21:16:59 +0100226 if (!drv->init || !(ret = drv->init()))
227 env_set_inited(drv->location);
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100228
Maxime Ripard1d446082018-01-23 21:16:59 +0100229 debug("%s: Environment %s init done (ret=%d)\n", __func__,
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100230 drv->name, ret);
231 }
232
233 if (!prio)
234 return -ENODEV;
235
Simon Glass79388222017-08-03 12:22:02 -0600236 if (ret == -ENOENT) {
237 gd->env_addr = (ulong)&default_environment[0];
Tom Rinieeba55c2017-08-19 22:27:57 -0400238 gd->env_valid = ENV_VALID;
Simon Glass79388222017-08-03 12:22:02 -0600239
240 return 0;
Simon Glassc9d728d2017-08-03 12:22:00 -0600241 }
242
Maxime Ripard8a3a7e22018-01-23 21:16:52 +0100243 return ret;
Simon Glassc9d728d2017-08-03 12:22:00 -0600244}