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