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