env.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <env.h>
  8. #include <env_internal.h>
  9. #include <log.h>
  10. #include <asm/global_data.h>
  11. #include <linux/bitops.h>
  12. #include <linux/bug.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static struct env_driver *_env_driver_lookup(enum env_location loc)
  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. 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. /* Not found */
  25. return NULL;
  26. }
  27. static enum env_location env_locations[] = {
  28. #ifdef CONFIG_ENV_IS_IN_EEPROM
  29. ENVL_EEPROM,
  30. #endif
  31. #ifdef CONFIG_ENV_IS_IN_EXT4
  32. ENVL_EXT4,
  33. #endif
  34. #ifdef CONFIG_ENV_IS_IN_FAT
  35. ENVL_FAT,
  36. #endif
  37. #ifdef CONFIG_ENV_IS_IN_FLASH
  38. ENVL_FLASH,
  39. #endif
  40. #ifdef CONFIG_ENV_IS_IN_MMC
  41. ENVL_MMC,
  42. #endif
  43. #ifdef CONFIG_ENV_IS_IN_NAND
  44. ENVL_NAND,
  45. #endif
  46. #ifdef CONFIG_ENV_IS_IN_NVRAM
  47. ENVL_NVRAM,
  48. #endif
  49. #ifdef CONFIG_ENV_IS_IN_REMOTE
  50. ENVL_REMOTE,
  51. #endif
  52. #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
  53. ENVL_SPI_FLASH,
  54. #endif
  55. #ifdef CONFIG_ENV_IS_IN_UBI
  56. ENVL_UBI,
  57. #endif
  58. #ifdef CONFIG_ENV_IS_NOWHERE
  59. ENVL_NOWHERE,
  60. #endif
  61. };
  62. static bool env_has_inited(enum env_location location)
  63. {
  64. return gd->env_has_init & BIT(location);
  65. }
  66. static void env_set_inited(enum env_location location)
  67. {
  68. /*
  69. * We're using a 32-bits bitmask stored in gd (env_has_init)
  70. * using the above enum value as the bit index. We need to
  71. * make sure that we're not overflowing it.
  72. */
  73. BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
  74. gd->env_has_init |= BIT(location);
  75. }
  76. /**
  77. * arch_env_get_location() - Returns the best env location for an arch
  78. * @op: operations performed on the environment
  79. * @prio: priority between the multiple environments, 0 being the
  80. * highest priority
  81. *
  82. * This will return the preferred environment for the given priority.
  83. * This is overridable by architectures if they need to and has lower
  84. * priority than board side env_get_location() override.
  85. *
  86. * All implementations are free to use the operation, the priority and
  87. * any other data relevant to their choice, but must take into account
  88. * the fact that the lowest prority (0) is the most important location
  89. * in the system. The following locations should be returned by order
  90. * of descending priorities, from the highest to the lowest priority.
  91. *
  92. * Returns:
  93. * an enum env_location value on success, a negative error code otherwise
  94. */
  95. __weak enum env_location arch_env_get_location(enum env_operation op, int prio)
  96. {
  97. if (prio >= ARRAY_SIZE(env_locations))
  98. return ENVL_UNKNOWN;
  99. return env_locations[prio];
  100. }
  101. /**
  102. * env_get_location() - Returns the best env location for a board
  103. * @op: operations performed on the environment
  104. * @prio: priority between the multiple environments, 0 being the
  105. * highest priority
  106. *
  107. * This will return the preferred environment for the given priority.
  108. * This is overridable by boards if they need to.
  109. *
  110. * All implementations are free to use the operation, the priority and
  111. * any other data relevant to their choice, but must take into account
  112. * the fact that the lowest prority (0) is the most important location
  113. * in the system. The following locations should be returned by order
  114. * of descending priorities, from the highest to the lowest priority.
  115. *
  116. * Returns:
  117. * an enum env_location value on success, a negative error code otherwise
  118. */
  119. __weak enum env_location env_get_location(enum env_operation op, int prio)
  120. {
  121. return arch_env_get_location(op, prio);
  122. }
  123. /**
  124. * env_driver_lookup() - Finds the most suited environment location
  125. * @op: operations performed on the environment
  126. * @prio: priority between the multiple environments, 0 being the
  127. * highest priority
  128. *
  129. * This will try to find the available environment with the highest
  130. * priority in the system.
  131. *
  132. * Returns:
  133. * NULL on error, a pointer to a struct env_driver otherwise
  134. */
  135. static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
  136. {
  137. enum env_location loc = env_get_location(op, prio);
  138. struct env_driver *drv;
  139. if (loc == ENVL_UNKNOWN)
  140. return NULL;
  141. drv = _env_driver_lookup(loc);
  142. if (!drv) {
  143. debug("%s: No environment driver for location %d\n", __func__,
  144. loc);
  145. return NULL;
  146. }
  147. return drv;
  148. }
  149. int env_load(void)
  150. {
  151. struct env_driver *drv;
  152. int best_prio = -1;
  153. int prio;
  154. if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
  155. /*
  156. * When using a list of writeable variables, the baseline comes
  157. * from the built-in default env. So load this first.
  158. */
  159. env_set_default(NULL, 0);
  160. }
  161. for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
  162. int ret;
  163. if (!env_has_inited(drv->location))
  164. continue;
  165. printf("Loading Environment from %s... ", drv->name);
  166. /*
  167. * In error case, the error message must be printed during
  168. * drv->load() in some underlying API, and it must be exactly
  169. * one message.
  170. */
  171. ret = drv->load();
  172. if (!ret) {
  173. printf("OK\n");
  174. gd->env_load_prio = prio;
  175. return 0;
  176. } else if (ret == -ENOMSG) {
  177. /* Handle "bad CRC" case */
  178. if (best_prio == -1)
  179. best_prio = prio;
  180. } else {
  181. debug("Failed (%d)\n", ret);
  182. }
  183. }
  184. /*
  185. * In case of invalid environment, we set the 'default' env location
  186. * to the best choice, i.e.:
  187. * 1. Environment location with bad CRC, if such location was found
  188. * 2. Otherwise use the location with highest priority
  189. *
  190. * This way, next calls to env_save() will restore the environment
  191. * at the right place.
  192. */
  193. if (best_prio >= 0)
  194. debug("Selecting environment with bad CRC\n");
  195. else
  196. best_prio = 0;
  197. gd->env_load_prio = best_prio;
  198. return -ENODEV;
  199. }
  200. int env_reload(void)
  201. {
  202. struct env_driver *drv;
  203. drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
  204. if (drv) {
  205. int ret;
  206. printf("Loading Environment from %s... ", drv->name);
  207. if (!env_has_inited(drv->location)) {
  208. printf("not initialized\n");
  209. return -ENODEV;
  210. }
  211. ret = drv->load();
  212. if (ret)
  213. printf("Failed (%d)\n", ret);
  214. else
  215. printf("OK\n");
  216. if (!ret)
  217. return 0;
  218. }
  219. return -ENODEV;
  220. }
  221. int env_save(void)
  222. {
  223. struct env_driver *drv;
  224. drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
  225. if (drv) {
  226. int ret;
  227. printf("Saving Environment to %s... ", drv->name);
  228. if (!drv->save) {
  229. printf("not possible\n");
  230. return -ENODEV;
  231. }
  232. if (!env_has_inited(drv->location)) {
  233. printf("not initialized\n");
  234. return -ENODEV;
  235. }
  236. ret = drv->save();
  237. if (ret)
  238. printf("Failed (%d)\n", ret);
  239. else
  240. printf("OK\n");
  241. if (!ret)
  242. return 0;
  243. }
  244. return -ENODEV;
  245. }
  246. int env_erase(void)
  247. {
  248. struct env_driver *drv;
  249. drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
  250. if (drv) {
  251. int ret;
  252. if (!drv->erase) {
  253. printf("not possible\n");
  254. return -ENODEV;
  255. }
  256. if (!env_has_inited(drv->location)) {
  257. printf("not initialized\n");
  258. return -ENODEV;
  259. }
  260. printf("Erasing Environment on %s... ", drv->name);
  261. ret = drv->erase();
  262. if (ret)
  263. printf("Failed (%d)\n", ret);
  264. else
  265. printf("OK\n");
  266. if (!ret)
  267. return 0;
  268. }
  269. return -ENODEV;
  270. }
  271. int env_init(void)
  272. {
  273. struct env_driver *drv;
  274. int ret = -ENOENT;
  275. int prio;
  276. for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
  277. if (!drv->init || !(ret = drv->init()))
  278. env_set_inited(drv->location);
  279. if (ret == -ENOENT)
  280. env_set_inited(drv->location);
  281. debug("%s: Environment %s init done (ret=%d)\n", __func__,
  282. drv->name, ret);
  283. if (gd->env_valid == ENV_INVALID)
  284. ret = -ENOENT;
  285. }
  286. if (!prio)
  287. return -ENODEV;
  288. if (ret == -ENOENT) {
  289. gd->env_addr = (ulong)&default_environment[0];
  290. gd->env_valid = ENV_VALID;
  291. return 0;
  292. }
  293. return ret;
  294. }
  295. int env_select(const char *name)
  296. {
  297. struct env_driver *drv;
  298. const int n_ents = ll_entry_count(struct env_driver, env_driver);
  299. struct env_driver *entry;
  300. int prio;
  301. bool found = false;
  302. printf("Select Environment on %s: ", name);
  303. /* search ENV driver by name */
  304. drv = ll_entry_start(struct env_driver, env_driver);
  305. for (entry = drv; entry != drv + n_ents; entry++) {
  306. if (!strcmp(entry->name, name)) {
  307. found = true;
  308. break;
  309. }
  310. }
  311. if (!found) {
  312. printf("driver not found\n");
  313. return -ENODEV;
  314. }
  315. /* search priority by driver */
  316. for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
  317. if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
  318. /* when priority change, reset the ENV flags */
  319. if (gd->env_load_prio != prio) {
  320. gd->env_load_prio = prio;
  321. gd->env_valid = ENV_INVALID;
  322. gd->flags &= ~GD_FLG_ENV_DEFAULT;
  323. }
  324. printf("OK\n");
  325. return 0;
  326. }
  327. }
  328. printf("priority not found\n");
  329. return -ENODEV;
  330. }