env.c 5.7 KB

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