Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <environment.h> |
| 10 | |
| 11 | DECLARE_GLOBAL_DATA_PTR; |
| 12 | |
Maxime Ripard | 52746c4 | 2018-01-23 21:16:51 +0100 | [diff] [blame] | 13 | static struct env_driver *_env_driver_lookup(enum env_location loc) |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 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 (loc == entry->location) |
| 22 | return entry; |
| 23 | } |
| 24 | |
| 25 | /* Not found */ |
| 26 | return NULL; |
| 27 | } |
| 28 | |
Maxime Ripard | 7d714a2 | 2018-01-23 21:16:58 +0100 | [diff] [blame] | 29 | static enum env_location env_locations[] = { |
| 30 | #ifdef CONFIG_ENV_IS_IN_EEPROM |
| 31 | ENVL_EEPROM, |
| 32 | #endif |
| 33 | #ifdef CONFIG_ENV_IS_IN_EXT4 |
| 34 | ENVL_EXT4, |
| 35 | #endif |
| 36 | #ifdef CONFIG_ENV_IS_IN_FAT |
| 37 | ENVL_FAT, |
| 38 | #endif |
| 39 | #ifdef CONFIG_ENV_IS_IN_FLASH |
| 40 | ENVL_FLASH, |
| 41 | #endif |
| 42 | #ifdef CONFIG_ENV_IS_IN_MMC |
| 43 | ENVL_MMC, |
| 44 | #endif |
| 45 | #ifdef CONFIG_ENV_IS_IN_NAND |
| 46 | ENVL_NAND, |
| 47 | #endif |
| 48 | #ifdef CONFIG_ENV_IS_IN_NVRAM |
| 49 | ENVL_NVRAM, |
| 50 | #endif |
| 51 | #ifdef CONFIG_ENV_IS_IN_REMOTE |
| 52 | ENVL_REMOTE, |
| 53 | #endif |
| 54 | #ifdef CONFIG_ENV_IS_IN_SPI_FLASH |
| 55 | ENVL_SPI_FLASH, |
| 56 | #endif |
| 57 | #ifdef CONFIG_ENV_IS_IN_UBI |
| 58 | ENVL_UBI, |
| 59 | #endif |
| 60 | #ifdef CONFIG_ENV_IS_NOWHERE |
| 61 | ENVL_NOWHERE, |
| 62 | #endif |
| 63 | }; |
| 64 | |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 65 | static bool env_has_inited(enum env_location location) |
| 66 | { |
| 67 | return gd->env_has_init & BIT(location); |
| 68 | } |
| 69 | |
| 70 | static void env_set_inited(enum env_location location) |
| 71 | { |
| 72 | /* |
| 73 | * We're using a 32-bits bitmask stored in gd (env_has_init) |
| 74 | * using the above enum value as the bit index. We need to |
| 75 | * make sure that we're not overflowing it. |
| 76 | */ |
| 77 | BUILD_BUG_ON(ARRAY_SIZE(env_locations) > BITS_PER_LONG); |
| 78 | |
| 79 | gd->env_has_init |= BIT(location); |
| 80 | } |
| 81 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 82 | /** |
| 83 | * env_get_location() - Returns the best env location for a board |
| 84 | * @op: operations performed on the environment |
| 85 | * @prio: priority between the multiple environments, 0 being the |
| 86 | * highest priority |
| 87 | * |
| 88 | * This will return the preferred environment for the given priority. |
Maxime Ripard | 40c08a6 | 2018-01-23 21:17:02 +0100 | [diff] [blame] | 89 | * This is overridable by boards if they need to. |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 90 | * |
| 91 | * All implementations are free to use the operation, the priority and |
| 92 | * any other data relevant to their choice, but must take into account |
| 93 | * the fact that the lowest prority (0) is the most important location |
| 94 | * in the system. The following locations should be returned by order |
| 95 | * of descending priorities, from the highest to the lowest priority. |
| 96 | * |
| 97 | * Returns: |
| 98 | * an enum env_location value on success, a negative error code otherwise |
| 99 | */ |
Maxime Ripard | 40c08a6 | 2018-01-23 21:17:02 +0100 | [diff] [blame] | 100 | __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] | 101 | { |
Maxime Ripard | 7d714a2 | 2018-01-23 21:16:58 +0100 | [diff] [blame] | 102 | switch (op) { |
| 103 | case ENVOP_GET_CHAR: |
| 104 | case ENVOP_INIT: |
| 105 | case ENVOP_LOAD: |
| 106 | if (prio >= ARRAY_SIZE(env_locations)) |
| 107 | return ENVL_UNKNOWN; |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 108 | |
York Sun | e1caa58 | 2018-02-07 14:17:11 -0800 | [diff] [blame^] | 109 | gd->env_load_location = env_locations[prio]; |
| 110 | return gd->env_load_location; |
Maxime Ripard | 7d714a2 | 2018-01-23 21:16:58 +0100 | [diff] [blame] | 111 | |
| 112 | case ENVOP_SAVE: |
York Sun | e1caa58 | 2018-02-07 14:17:11 -0800 | [diff] [blame^] | 113 | return gd->env_load_location; |
Maxime Ripard | 7d714a2 | 2018-01-23 21:16:58 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return ENVL_UNKNOWN; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 117 | } |
| 118 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * env_driver_lookup() - Finds the most suited environment location |
| 122 | * @op: operations performed on the environment |
| 123 | * @prio: priority between the multiple environments, 0 being the |
| 124 | * highest priority |
| 125 | * |
| 126 | * This will try to find the available environment with the highest |
| 127 | * priority in the system. |
| 128 | * |
| 129 | * Returns: |
| 130 | * NULL on error, a pointer to a struct env_driver otherwise |
| 131 | */ |
| 132 | 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] | 133 | { |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 134 | enum env_location loc = env_get_location(op, prio); |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 135 | struct env_driver *drv; |
| 136 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 137 | if (loc == ENVL_UNKNOWN) |
| 138 | return NULL; |
| 139 | |
Maxime Ripard | 52746c4 | 2018-01-23 21:16:51 +0100 | [diff] [blame] | 140 | drv = _env_driver_lookup(loc); |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 141 | if (!drv) { |
| 142 | debug("%s: No environment driver for location %d\n", __func__, |
| 143 | loc); |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | return drv; |
| 148 | } |
| 149 | |
Simon Glass | a69d0f6 | 2017-08-03 12:22:06 -0600 | [diff] [blame] | 150 | int env_get_char(int index) |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 151 | { |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 152 | struct env_driver *drv; |
| 153 | int prio; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 154 | |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 155 | if (gd->env_valid == ENV_INVALID) |
Simon Glass | a69d0f6 | 2017-08-03 12:22:06 -0600 | [diff] [blame] | 156 | return default_environment[index]; |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 157 | |
| 158 | for (prio = 0; (drv = env_driver_lookup(ENVOP_GET_CHAR, prio)); prio++) { |
| 159 | int ret; |
| 160 | |
| 161 | if (!drv->get_char) |
| 162 | continue; |
| 163 | |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 164 | if (!env_has_inited(drv->location)) |
| 165 | continue; |
| 166 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 167 | ret = drv->get_char(index); |
| 168 | if (!ret) |
| 169 | return 0; |
| 170 | |
| 171 | debug("%s: Environment %s failed to load (err=%d)\n", __func__, |
| 172 | drv->name, ret); |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 173 | } |
| 174 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 175 | return -ENODEV; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int env_load(void) |
| 179 | { |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 180 | struct env_driver *drv; |
| 181 | int prio; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 182 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 183 | for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) { |
| 184 | int ret; |
| 185 | |
| 186 | if (!drv->load) |
| 187 | continue; |
| 188 | |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 189 | if (!env_has_inited(drv->location)) |
| 190 | continue; |
| 191 | |
Maxime Ripard | 3574ba0 | 2018-01-23 21:16:54 +0100 | [diff] [blame] | 192 | printf("Loading Environment from %s... ", drv->name); |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 193 | ret = drv->load(); |
Maxime Ripard | 3574ba0 | 2018-01-23 21:16:54 +0100 | [diff] [blame] | 194 | if (ret) |
| 195 | printf("Failed (%d)\n", ret); |
| 196 | else |
| 197 | printf("OK\n"); |
| 198 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 199 | if (!ret) |
| 200 | return 0; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 203 | return -ENODEV; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | int env_save(void) |
| 207 | { |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 208 | struct env_driver *drv; |
| 209 | int prio; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 210 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 211 | for (prio = 0; (drv = env_driver_lookup(ENVOP_SAVE, prio)); prio++) { |
| 212 | int ret; |
Maxime Ripard | 9c24dfb | 2018-01-23 21:16:50 +0100 | [diff] [blame] | 213 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 214 | if (!drv->save) |
| 215 | continue; |
| 216 | |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 217 | if (!env_has_inited(drv->location)) |
| 218 | continue; |
| 219 | |
Maxime Ripard | 9efac3c | 2018-01-23 21:16:53 +0100 | [diff] [blame] | 220 | printf("Saving Environment to %s... ", drv->name); |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 221 | ret = drv->save(); |
Maxime Ripard | 3574ba0 | 2018-01-23 21:16:54 +0100 | [diff] [blame] | 222 | if (ret) |
| 223 | printf("Failed (%d)\n", ret); |
| 224 | else |
| 225 | printf("OK\n"); |
| 226 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 227 | if (!ret) |
| 228 | return 0; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 229 | } |
| 230 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 231 | return -ENODEV; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 232 | } |
| 233 | |
Simon Glass | 6eeae42 | 2017-08-03 12:22:05 -0600 | [diff] [blame] | 234 | int env_init(void) |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 235 | { |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 236 | struct env_driver *drv; |
Simon Glass | 7938822 | 2017-08-03 12:22:02 -0600 | [diff] [blame] | 237 | int ret = -ENOENT; |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 238 | int prio; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 239 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 240 | for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) { |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 241 | if (!drv->init || !(ret = drv->init())) |
| 242 | env_set_inited(drv->location); |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 243 | |
Maxime Ripard | 1d44608 | 2018-01-23 21:16:59 +0100 | [diff] [blame] | 244 | debug("%s: Environment %s init done (ret=%d)\n", __func__, |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 245 | drv->name, ret); |
| 246 | } |
| 247 | |
| 248 | if (!prio) |
| 249 | return -ENODEV; |
| 250 | |
Simon Glass | 7938822 | 2017-08-03 12:22:02 -0600 | [diff] [blame] | 251 | if (ret == -ENOENT) { |
| 252 | gd->env_addr = (ulong)&default_environment[0]; |
Tom Rini | eeba55c | 2017-08-19 22:27:57 -0400 | [diff] [blame] | 253 | gd->env_valid = ENV_VALID; |
Simon Glass | 7938822 | 2017-08-03 12:22:02 -0600 | [diff] [blame] | 254 | |
| 255 | return 0; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 256 | } |
| 257 | |
Maxime Ripard | 8a3a7e2 | 2018-01-23 21:16:52 +0100 | [diff] [blame] | 258 | return ret; |
Simon Glass | c9d728d | 2017-08-03 12:22:00 -0600 | [diff] [blame] | 259 | } |