Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <environment.h> |
| 9 | |
| 10 | DECLARE_GLOBAL_DATA_PTR; |
| 11 | |
Siva Durga Prasad Paladugu | 7bcdf19 | 2018-04-13 07:57:21 +0200 | [diff] [blame] | 12 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 13 | void 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; |
Frank Wunderlich | cd121bd | 2019-06-29 11:36:19 +0200 | [diff] [blame] | 27 | if (entry->erase) |
| 28 | entry->erase += gd->reloc_off; |
Siva Durga Prasad Paladugu | 7bcdf19 | 2018-04-13 07:57:21 +0200 | [diff] [blame] | 29 | if (entry->init) |
| 30 | entry->init += gd->reloc_off; |
| 31 | } |
| 32 | } |
| 33 | #endif |
| 34 | |
Maxime Ripard | 52746c4 | 2018-01-23 21:16:51 +0100 | [diff] [blame] | 35 | static struct env_driver *_env_driver_lookup(enum env_location loc) |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 36 | { |
| 37 | struct env_driver *drv; |
| 38 | const int n_ents = ll_entry_count(struct env_driver, env_driver); |
| 39 | struct env_driver *entry; |
| 40 | |
| 41 | drv = ll_entry_start(struct env_driver, env_driver); |
| 42 | for (entry = drv; entry != drv + n_ents; entry++) { |
| 43 | if (loc == entry->location) |
| 44 | return entry; |
| 45 | } |
| 46 | |
| 47 | /* Not found */ |
| 48 | return NULL; |
| 49 | } |
| 50 | |
Maxime Ripard | 7d714a2 | 2018-01-23 21:16:58 +0100 | [diff] [blame] | 51 | static enum env_location env_locations[] = { |
| 52 | #ifdef CONFIG_ENV_IS_IN_EEPROM |
| 53 | ENVL_EEPROM, |
| 54 | #endif |
| 55 | #ifdef CONFIG_ENV_IS_IN_EXT4 |
| 56 | ENVL_EXT4, |
| 57 | #endif |
| 58 | #ifdef CONFIG_ENV_IS_IN_FAT |
| 59 | ENVL_FAT, |
| 60 | #endif |
| 61 | #ifdef CONFIG_ENV_IS_IN_FLASH |
| 62 | ENVL_FLASH, |
| 63 | #endif |
| 64 | #ifdef CONFIG_ENV_IS_IN_MMC |
| 65 | ENVL_MMC, |
| 66 | #endif |
| 67 | #ifdef CONFIG_ENV_IS_IN_NAND |
| 68 | ENVL_NAND, |
| 69 | #endif |
| 70 | #ifdef CONFIG_ENV_IS_IN_NVRAM |
| 71 | ENVL_NVRAM, |
| 72 | #endif |
| 73 | #ifdef CONFIG_ENV_IS_IN_REMOTE |
| 74 | ENVL_REMOTE, |
| 75 | #endif |
Ye Li | 9a6a311 | 2019-01-04 09:34:24 +0000 | [diff] [blame] | 76 | #ifdef CONFIG_ENV_IS_IN_SATA |
| 77 | ENVL_ESATA, |
| 78 | #endif |
Maxime Ripard | 7d714a2 | 2018-01-23 21:16:58 +0100 | [diff] [blame] | 79 | #ifdef CONFIG_ENV_IS_IN_SPI_FLASH |
| 80 | ENVL_SPI_FLASH, |
| 81 | #endif |
| 82 | #ifdef CONFIG_ENV_IS_IN_UBI |
| 83 | ENVL_UBI, |
| 84 | #endif |
| 85 | #ifdef CONFIG_ENV_IS_NOWHERE |
| 86 | ENVL_NOWHERE, |
| 87 | #endif |
| 88 | }; |
| 89 | |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 90 | static bool env_has_inited(enum env_location location) |
| 91 | { |
| 92 | return gd->env_has_init & BIT(location); |
| 93 | } |
| 94 | |
| 95 | static void env_set_inited(enum env_location location) |
| 96 | { |
| 97 | /* |
| 98 | * We're using a 32-bits bitmask stored in gd (env_has_init) |
| 99 | * using the above enum value as the bit index. We need to |
| 100 | * make sure that we're not overflowing it. |
| 101 | */ |
| 102 | BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG); |
| 103 | |
| 104 | gd->env_has_init |= BIT(location); |
| 105 | } |
| 106 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 107 | /** |
| 108 | * env_get_location() - Returns the best env location for a board |
| 109 | * @op: operations performed on the environment |
| 110 | * @prio: priority between the multiple environments, 0 being the |
| 111 | * highest priority |
| 112 | * |
| 113 | * This will return the preferred environment for the given priority. |
Maxime Ripard | 40c08a6 | 2018-01-23 21:17:02 +0100 | [diff] [blame] | 114 | * This is overridable by boards if they need to. |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 115 | * |
| 116 | * All implementations are free to use the operation, the priority and |
| 117 | * any other data relevant to their choice, but must take into account |
| 118 | * the fact that the lowest prority (0) is the most important location |
| 119 | * in the system. The following locations should be returned by order |
| 120 | * of descending priorities, from the highest to the lowest priority. |
| 121 | * |
| 122 | * Returns: |
| 123 | * an enum env_location value on success, a negative error code otherwise |
| 124 | */ |
Maxime Ripard | 40c08a6 | 2018-01-23 21:17:02 +0100 | [diff] [blame] | 125 | __weak enum env_location env_get_location(enum env_operation op, int prio) |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 126 | { |
Nicholas Faustini | d30ba23 | 2018-07-23 10:01:07 +0200 | [diff] [blame] | 127 | if (prio >= ARRAY_SIZE(env_locations)) |
| 128 | return ENVL_UNKNOWN; |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 129 | |
Nicholas Faustini | d30ba23 | 2018-07-23 10:01:07 +0200 | [diff] [blame] | 130 | gd->env_load_prio = prio; |
Maxime Ripard | 7d714a2 | 2018-01-23 21:16:58 +0100 | [diff] [blame] | 131 | |
Nicholas Faustini | d30ba23 | 2018-07-23 10:01:07 +0200 | [diff] [blame] | 132 | return env_locations[prio]; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 133 | } |
| 134 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * env_driver_lookup() - Finds the most suited environment location |
| 138 | * @op: operations performed on the environment |
| 139 | * @prio: priority between the multiple environments, 0 being the |
| 140 | * highest priority |
| 141 | * |
| 142 | * This will try to find the available environment with the highest |
| 143 | * priority in the system. |
| 144 | * |
| 145 | * Returns: |
| 146 | * NULL on error, a pointer to a struct env_driver otherwise |
| 147 | */ |
| 148 | static struct env_driver *env_driver_lookup(enum env_operation op, int prio) |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 149 | { |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 150 | enum env_location loc = env_get_location(op, prio); |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 151 | struct env_driver *drv; |
| 152 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 153 | if (loc == ENVL_UNKNOWN) |
| 154 | return NULL; |
| 155 | |
Maxime Ripard | 52746c4 | 2018-01-23 21:16:51 +0100 | [diff] [blame] | 156 | drv = _env_driver_lookup(loc); |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 157 | if (!drv) { |
| 158 | debug("%s: No environment driver for location %d\n", __func__, |
| 159 | loc); |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | return drv; |
| 164 | } |
| 165 | |
Goldschmidt Simon | b2cdef4 | 2018-02-09 20:23:17 +0000 | [diff] [blame] | 166 | __weak int env_get_char_spec(int index) |
| 167 | { |
| 168 | return *(uchar *)(gd->env_addr + index); |
| 169 | } |
| 170 | |
Simon Glass | a69d0f6 | 2017-08-03 12:22:06 -0600 | [diff] [blame] | 171 | int env_get_char(int index) |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 172 | { |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 173 | if (gd->env_valid == ENV_INVALID) |
Simon Glass | a69d0f6 | 2017-08-03 12:22:06 -0600 | [diff] [blame] | 174 | return default_environment[index]; |
Goldschmidt Simon | b2cdef4 | 2018-02-09 20:23:17 +0000 | [diff] [blame] | 175 | else |
| 176 | return env_get_char_spec(index); |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | int env_load(void) |
| 180 | { |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 181 | struct env_driver *drv; |
Sam Protsenko | 293a172 | 2019-01-18 21:19:04 +0200 | [diff] [blame] | 182 | int best_prio = -1; |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 183 | int prio; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 184 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 185 | for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) { |
| 186 | int ret; |
| 187 | |
| 188 | if (!drv->load) |
| 189 | continue; |
| 190 | |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 191 | if (!env_has_inited(drv->location)) |
| 192 | continue; |
| 193 | |
Maxime Ripard | 3574ba0 | 2018-01-23 21:16:54 +0100 | [diff] [blame] | 194 | printf("Loading Environment from %s... ", drv->name); |
Sam Protsenko | 13bbfb4 | 2018-07-30 19:19:26 +0300 | [diff] [blame] | 195 | /* |
| 196 | * In error case, the error message must be printed during |
| 197 | * drv->load() in some underlying API, and it must be exactly |
| 198 | * one message. |
| 199 | */ |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 200 | ret = drv->load(); |
Sam Protsenko | 293a172 | 2019-01-18 21:19:04 +0200 | [diff] [blame] | 201 | if (!ret) { |
Maxime Ripard | 3574ba0 | 2018-01-23 21:16:54 +0100 | [diff] [blame] | 202 | printf("OK\n"); |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 203 | return 0; |
Sam Protsenko | 293a172 | 2019-01-18 21:19:04 +0200 | [diff] [blame] | 204 | } else if (ret == -ENOMSG) { |
| 205 | /* Handle "bad CRC" case */ |
| 206 | if (best_prio == -1) |
| 207 | best_prio = prio; |
| 208 | } else { |
| 209 | debug("Failed (%d)\n", ret); |
Sam Protsenko | 13bbfb4 | 2018-07-30 19:19:26 +0300 | [diff] [blame] | 210 | } |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 211 | } |
| 212 | |
Nicholas Faustini | d30ba23 | 2018-07-23 10:01:07 +0200 | [diff] [blame] | 213 | /* |
| 214 | * In case of invalid environment, we set the 'default' env location |
Sam Protsenko | 293a172 | 2019-01-18 21:19:04 +0200 | [diff] [blame] | 215 | * to the best choice, i.e.: |
| 216 | * 1. Environment location with bad CRC, if such location was found |
| 217 | * 2. Otherwise use the location with highest priority |
| 218 | * |
| 219 | * This way, next calls to env_save() will restore the environment |
| 220 | * at the right place. |
Nicholas Faustini | d30ba23 | 2018-07-23 10:01:07 +0200 | [diff] [blame] | 221 | */ |
Sam Protsenko | 293a172 | 2019-01-18 21:19:04 +0200 | [diff] [blame] | 222 | if (best_prio >= 0) |
| 223 | debug("Selecting environment with bad CRC\n"); |
| 224 | else |
| 225 | best_prio = 0; |
| 226 | env_get_location(ENVOP_LOAD, best_prio); |
Nicholas Faustini | d30ba23 | 2018-07-23 10:01:07 +0200 | [diff] [blame] | 227 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 228 | return -ENODEV; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | int env_save(void) |
| 232 | { |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 233 | struct env_driver *drv; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 234 | |
Nicholas Faustini | d30ba23 | 2018-07-23 10:01:07 +0200 | [diff] [blame] | 235 | drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio); |
| 236 | if (drv) { |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 237 | int ret; |
Maxime Ripard | 9c24dfb | 2018-01-23 21:16:50 +0100 | [diff] [blame] | 238 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 239 | if (!drv->save) |
Nicholas Faustini | d30ba23 | 2018-07-23 10:01:07 +0200 | [diff] [blame] | 240 | return -ENODEV; |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 241 | |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 242 | if (!env_has_inited(drv->location)) |
Nicholas Faustini | d30ba23 | 2018-07-23 10:01:07 +0200 | [diff] [blame] | 243 | return -ENODEV; |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 244 | |
Maxime Ripard | 9efac3c | 2018-01-23 21:16:53 +0100 | [diff] [blame] | 245 | printf("Saving Environment to %s... ", drv->name); |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 246 | ret = drv->save(); |
Maxime Ripard | 3574ba0 | 2018-01-23 21:16:54 +0100 | [diff] [blame] | 247 | if (ret) |
| 248 | printf("Failed (%d)\n", ret); |
| 249 | else |
| 250 | printf("OK\n"); |
| 251 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 252 | if (!ret) |
| 253 | return 0; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 254 | } |
| 255 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 256 | return -ENODEV; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 257 | } |
| 258 | |
Frank Wunderlich | cd121bd | 2019-06-29 11:36:19 +0200 | [diff] [blame] | 259 | int env_erase(void) |
| 260 | { |
| 261 | struct env_driver *drv; |
| 262 | |
| 263 | drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio); |
| 264 | if (drv) { |
| 265 | int ret; |
| 266 | |
| 267 | if (!drv->erase) |
| 268 | return -ENODEV; |
| 269 | |
| 270 | if (!env_has_inited(drv->location)) |
| 271 | return -ENODEV; |
| 272 | |
| 273 | printf("Erasing Environment on %s... ", drv->name); |
| 274 | ret = drv->erase(); |
| 275 | if (ret) |
| 276 | printf("Failed (%d)\n", ret); |
| 277 | else |
| 278 | printf("OK\n"); |
| 279 | |
| 280 | if (!ret) |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | return -ENODEV; |
| 285 | } |
| 286 | |
Simon Glass | 6eeae42 | 2017-08-03 12:22:05 -0600 | [diff] [blame] | 287 | int env_init(void) |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 288 | { |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 289 | struct env_driver *drv; |
Simon Glass | 7938822 | 2017-08-03 12:22:02 -0600 | [diff] [blame] | 290 | int ret = -ENOENT; |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 291 | int prio; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 292 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 293 | for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) { |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 294 | if (!drv->init || !(ret = drv->init())) |
| 295 | env_set_inited(drv->location); |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 296 | |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 297 | debug("%s: Environment %s init done (ret=%d)\n", __func__, |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 298 | drv->name, ret); |
| 299 | } |
| 300 | |
| 301 | if (!prio) |
| 302 | return -ENODEV; |
| 303 | |
Simon Glass | 7938822 | 2017-08-03 12:22:02 -0600 | [diff] [blame] | 304 | if (ret == -ENOENT) { |
| 305 | gd->env_addr = (ulong)&default_environment[0]; |
Tom Rini | eeba55c | 2017-08-19 22:27:57 -0400 | [diff] [blame] | 306 | gd->env_valid = ENV_VALID; |
Simon Glass | 7938822 | 2017-08-03 12:22:02 -0600 | [diff] [blame] | 307 | |
| 308 | return 0; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 309 | } |
| 310 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 311 | return ret; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 312 | } |