env.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. switch (op) {
  111. case ENVOP_GET_CHAR:
  112. case ENVOP_INIT:
  113. case ENVOP_LOAD:
  114. if (prio >= ARRAY_SIZE(env_locations))
  115. return ENVL_UNKNOWN;
  116. gd->env_load_location = env_locations[prio];
  117. return gd->env_load_location;
  118. case ENVOP_SAVE:
  119. return gd->env_load_location;
  120. }
  121. return ENVL_UNKNOWN;
  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 prio;
  164. for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
  165. int ret;
  166. if (!drv->load)
  167. continue;
  168. if (!env_has_inited(drv->location))
  169. continue;
  170. printf("Loading Environment from %s... ", drv->name);
  171. ret = drv->load();
  172. if (ret)
  173. printf("Failed (%d)\n", ret);
  174. else
  175. printf("OK\n");
  176. if (!ret)
  177. return 0;
  178. }
  179. return -ENODEV;
  180. }
  181. int env_save(void)
  182. {
  183. struct env_driver *drv;
  184. int prio;
  185. for (prio = 0; (drv = env_driver_lookup(ENVOP_SAVE, prio)); prio++) {
  186. int ret;
  187. if (!drv->save)
  188. continue;
  189. if (!env_has_inited(drv->location))
  190. continue;
  191. printf("Saving Environment to %s... ", drv->name);
  192. ret = drv->save();
  193. if (ret)
  194. printf("Failed (%d)\n", ret);
  195. else
  196. printf("OK\n");
  197. if (!ret)
  198. return 0;
  199. }
  200. return -ENODEV;
  201. }
  202. int env_init(void)
  203. {
  204. struct env_driver *drv;
  205. int ret = -ENOENT;
  206. int prio;
  207. for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
  208. if (!drv->init || !(ret = drv->init()))
  209. env_set_inited(drv->location);
  210. debug("%s: Environment %s init done (ret=%d)\n", __func__,
  211. drv->name, ret);
  212. }
  213. if (!prio)
  214. return -ENODEV;
  215. if (ret == -ENOENT) {
  216. gd->env_addr = (ulong)&default_environment[0];
  217. gd->env_valid = ENV_VALID;
  218. return 0;
  219. }
  220. return ret;
  221. }