env.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <asm/global_data.h>
  11. #include <linux/bitops.h>
  12. #include <linux/bug.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  15. void env_fix_drivers(void)
  16. {
  17. struct env_driver *drv;
  18. const int n_ents = ll_entry_count(struct env_driver, env_driver);
  19. struct env_driver *entry;
  20. drv = ll_entry_start(struct env_driver, env_driver);
  21. for (entry = drv; entry != drv + n_ents; entry++) {
  22. if (entry->name)
  23. entry->name += gd->reloc_off;
  24. if (entry->load)
  25. entry->load += gd->reloc_off;
  26. if (entry->save)
  27. entry->save += gd->reloc_off;
  28. if (entry->erase)
  29. entry->erase += gd->reloc_off;
  30. if (entry->init)
  31. entry->init += gd->reloc_off;
  32. }
  33. }
  34. #endif
  35. static struct env_driver *_env_driver_lookup(enum env_location loc)
  36. {
  37. struct env_driver *drv;
  38. const int n_ents = ll_entry_count(struct env_driver, env_driver);
  39. struct env_driver *entry;
  40. drv = ll_entry_start(struct env_driver, env_driver);
  41. for (entry = drv; entry != drv + n_ents; entry++) {
  42. if (loc == entry->location)
  43. return entry;
  44. }
  45. /* Not found */
  46. return NULL;
  47. }
  48. static enum env_location env_locations[] = {
  49. #ifdef CONFIG_ENV_IS_IN_EEPROM
  50. ENVL_EEPROM,
  51. #endif
  52. #ifdef CONFIG_ENV_IS_IN_EXT4
  53. ENVL_EXT4,
  54. #endif
  55. #ifdef CONFIG_ENV_IS_IN_FAT
  56. ENVL_FAT,
  57. #endif
  58. #ifdef CONFIG_ENV_IS_IN_FLASH
  59. ENVL_FLASH,
  60. #endif
  61. #ifdef CONFIG_ENV_IS_IN_MMC
  62. ENVL_MMC,
  63. #endif
  64. #ifdef CONFIG_ENV_IS_IN_NAND
  65. ENVL_NAND,
  66. #endif
  67. #ifdef CONFIG_ENV_IS_IN_NVRAM
  68. ENVL_NVRAM,
  69. #endif
  70. #ifdef CONFIG_ENV_IS_IN_REMOTE
  71. ENVL_REMOTE,
  72. #endif
  73. #ifdef CONFIG_ENV_IS_IN_SATA
  74. ENVL_ESATA,
  75. #endif
  76. #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
  77. ENVL_SPI_FLASH,
  78. #endif
  79. #ifdef CONFIG_ENV_IS_IN_UBI
  80. ENVL_UBI,
  81. #endif
  82. #ifdef CONFIG_ENV_IS_NOWHERE
  83. ENVL_NOWHERE,
  84. #endif
  85. };
  86. static bool env_has_inited(enum env_location location)
  87. {
  88. return gd->env_has_init & BIT(location);
  89. }
  90. static void env_set_inited(enum env_location location)
  91. {
  92. /*
  93. * We're using a 32-bits bitmask stored in gd (env_has_init)
  94. * using the above enum value as the bit index. We need to
  95. * make sure that we're not overflowing it.
  96. */
  97. BUILD_BUG_ON(ENVL_COUNT > BITS_PER_LONG);
  98. gd->env_has_init |= BIT(location);
  99. }
  100. /**
  101. * env_get_location() - Returns the best env location for a board
  102. * @op: operations performed on the environment
  103. * @prio: priority between the multiple environments, 0 being the
  104. * highest priority
  105. *
  106. * This will return the preferred environment for the given priority.
  107. * This is overridable by boards if they need to.
  108. *
  109. * All implementations are free to use the operation, the priority and
  110. * any other data relevant to their choice, but must take into account
  111. * the fact that the lowest prority (0) is the most important location
  112. * in the system. The following locations should be returned by order
  113. * of descending priorities, from the highest to the lowest priority.
  114. *
  115. * Returns:
  116. * an enum env_location value on success, a negative error code otherwise
  117. */
  118. __weak enum env_location env_get_location(enum env_operation op, int prio)
  119. {
  120. if (prio >= ARRAY_SIZE(env_locations))
  121. return ENVL_UNKNOWN;
  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. int env_load(void)
  151. {
  152. struct env_driver *drv;
  153. int best_prio = -1;
  154. int prio;
  155. for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
  156. int ret;
  157. if (!env_has_inited(drv->location))
  158. continue;
  159. printf("Loading Environment from %s... ", drv->name);
  160. /*
  161. * In error case, the error message must be printed during
  162. * drv->load() in some underlying API, and it must be exactly
  163. * one message.
  164. */
  165. ret = drv->load();
  166. if (!ret) {
  167. printf("OK\n");
  168. gd->env_load_prio = prio;
  169. #if !CONFIG_IS_ENABLED(ENV_APPEND)
  170. return 0;
  171. #endif
  172. } else if (ret == -ENOMSG) {
  173. /* Handle "bad CRC" case */
  174. if (best_prio == -1)
  175. best_prio = prio;
  176. } else {
  177. debug("Failed (%d)\n", ret);
  178. }
  179. }
  180. /*
  181. * In case of invalid environment, we set the 'default' env location
  182. * to the best choice, i.e.:
  183. * 1. Environment location with bad CRC, if such location was found
  184. * 2. Otherwise use the location with highest priority
  185. *
  186. * This way, next calls to env_save() will restore the environment
  187. * at the right place.
  188. */
  189. if (best_prio >= 0)
  190. debug("Selecting environment with bad CRC\n");
  191. else
  192. best_prio = 0;
  193. gd->env_load_prio = best_prio;
  194. return -ENODEV;
  195. }
  196. int env_reload(void)
  197. {
  198. struct env_driver *drv;
  199. drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
  200. if (drv) {
  201. int ret;
  202. printf("Loading Environment from %s... ", drv->name);
  203. if (!env_has_inited(drv->location)) {
  204. printf("not initialized\n");
  205. return -ENODEV;
  206. }
  207. ret = drv->load();
  208. if (ret)
  209. printf("Failed (%d)\n", ret);
  210. else
  211. printf("OK\n");
  212. if (!ret)
  213. return 0;
  214. }
  215. return -ENODEV;
  216. }
  217. int env_save(void)
  218. {
  219. struct env_driver *drv;
  220. drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
  221. if (drv) {
  222. int ret;
  223. printf("Saving Environment to %s... ", drv->name);
  224. if (!drv->save) {
  225. printf("not possible\n");
  226. return -ENODEV;
  227. }
  228. if (!env_has_inited(drv->location)) {
  229. printf("not initialized\n");
  230. return -ENODEV;
  231. }
  232. ret = drv->save();
  233. if (ret)
  234. printf("Failed (%d)\n", ret);
  235. else
  236. printf("OK\n");
  237. if (!ret)
  238. return 0;
  239. }
  240. return -ENODEV;
  241. }
  242. int env_erase(void)
  243. {
  244. struct env_driver *drv;
  245. drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
  246. if (drv) {
  247. int ret;
  248. if (!drv->erase)
  249. return -ENODEV;
  250. if (!env_has_inited(drv->location))
  251. return -ENODEV;
  252. printf("Erasing Environment on %s... ", drv->name);
  253. ret = drv->erase();
  254. if (ret)
  255. printf("Failed (%d)\n", ret);
  256. else
  257. printf("OK\n");
  258. if (!ret)
  259. return 0;
  260. }
  261. return -ENODEV;
  262. }
  263. int env_init(void)
  264. {
  265. struct env_driver *drv;
  266. int ret = -ENOENT;
  267. int prio;
  268. for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
  269. if (!drv->init || !(ret = drv->init()))
  270. env_set_inited(drv->location);
  271. if (ret == -ENOENT)
  272. env_set_inited(drv->location);
  273. debug("%s: Environment %s init done (ret=%d)\n", __func__,
  274. drv->name, ret);
  275. if (gd->env_valid == ENV_INVALID)
  276. ret = -ENOENT;
  277. }
  278. if (!prio)
  279. return -ENODEV;
  280. if (ret == -ENOENT) {
  281. gd->env_addr = (ulong)&default_environment[0];
  282. gd->env_valid = ENV_VALID;
  283. return 0;
  284. }
  285. return ret;
  286. }
  287. int env_select(const char *name)
  288. {
  289. struct env_driver *drv;
  290. const int n_ents = ll_entry_count(struct env_driver, env_driver);
  291. struct env_driver *entry;
  292. int prio;
  293. bool found = false;
  294. printf("Select Environment on %s: ", name);
  295. /* search ENV driver by name */
  296. drv = ll_entry_start(struct env_driver, env_driver);
  297. for (entry = drv; entry != drv + n_ents; entry++) {
  298. if (!strcmp(entry->name, name)) {
  299. found = true;
  300. break;
  301. }
  302. }
  303. if (!found) {
  304. printf("driver not found\n");
  305. return -ENODEV;
  306. }
  307. /* search priority by driver */
  308. for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
  309. if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
  310. /* when priority change, reset the ENV flags */
  311. if (gd->env_load_prio != prio) {
  312. gd->env_load_prio = prio;
  313. gd->env_valid = ENV_INVALID;
  314. gd->flags &= ~GD_FLG_ENV_DEFAULT;
  315. }
  316. printf("OK\n");
  317. return 0;
  318. }
  319. }
  320. printf("priority not found\n");
  321. return -ENODEV;
  322. }