env.c 5.2 KB

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