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