env.c 6.5 KB

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