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