env.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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(ENVL_COUNT > 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. return env_locations[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. __weak int env_get_char_spec(int index)
  150. {
  151. return *(uchar *)(gd->env_addr + index);
  152. }
  153. int env_get_char(int index)
  154. {
  155. if (gd->env_valid == ENV_INVALID)
  156. return default_environment[index];
  157. else
  158. return env_get_char_spec(index);
  159. }
  160. int env_load(void)
  161. {
  162. struct env_driver *drv;
  163. int best_prio = -1;
  164. int prio;
  165. for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
  166. int ret;
  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. gd->env_load_prio = prio;
  179. return 0;
  180. } else if (ret == -ENOMSG) {
  181. /* Handle "bad CRC" case */
  182. if (best_prio == -1)
  183. best_prio = prio;
  184. } else {
  185. debug("Failed (%d)\n", ret);
  186. }
  187. }
  188. /*
  189. * In case of invalid environment, we set the 'default' env location
  190. * to the best choice, i.e.:
  191. * 1. Environment location with bad CRC, if such location was found
  192. * 2. Otherwise use the location with highest priority
  193. *
  194. * This way, next calls to env_save() will restore the environment
  195. * at the right place.
  196. */
  197. if (best_prio >= 0)
  198. debug("Selecting environment with bad CRC\n");
  199. else
  200. best_prio = 0;
  201. gd->env_load_prio = best_prio;
  202. return -ENODEV;
  203. }
  204. int env_save(void)
  205. {
  206. struct env_driver *drv;
  207. drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
  208. if (drv) {
  209. int ret;
  210. printf("Saving Environment to %s... ", drv->name);
  211. if (!drv->save) {
  212. printf("not possible\n");
  213. return -ENODEV;
  214. }
  215. if (!env_has_inited(drv->location)) {
  216. printf("not initialized\n");
  217. return -ENODEV;
  218. }
  219. ret = drv->save();
  220. if (ret)
  221. printf("Failed (%d)\n", ret);
  222. else
  223. printf("OK\n");
  224. if (!ret)
  225. return 0;
  226. }
  227. return -ENODEV;
  228. }
  229. int env_erase(void)
  230. {
  231. struct env_driver *drv;
  232. drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
  233. if (drv) {
  234. int ret;
  235. if (!drv->erase)
  236. return -ENODEV;
  237. if (!env_has_inited(drv->location))
  238. return -ENODEV;
  239. printf("Erasing Environment on %s... ", drv->name);
  240. ret = drv->erase();
  241. if (ret)
  242. printf("Failed (%d)\n", ret);
  243. else
  244. printf("OK\n");
  245. if (!ret)
  246. return 0;
  247. }
  248. return -ENODEV;
  249. }
  250. int env_init(void)
  251. {
  252. struct env_driver *drv;
  253. int ret = -ENOENT;
  254. int prio;
  255. for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
  256. if (!drv->init || !(ret = drv->init()))
  257. env_set_inited(drv->location);
  258. debug("%s: Environment %s init done (ret=%d)\n", __func__,
  259. drv->name, ret);
  260. }
  261. if (!prio)
  262. return -ENODEV;
  263. if (ret == -ENOENT) {
  264. gd->env_addr = (ulong)&default_environment[0];
  265. gd->env_valid = ENV_VALID;
  266. return 0;
  267. }
  268. return ret;
  269. }