env.c 6.6 KB

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