env.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_SPI_FLASH
  74. ENVL_SPI_FLASH,
  75. #endif
  76. #ifdef CONFIG_ENV_IS_IN_SPI_NAND_FLASH
  77. ENVL_SPI_NAND_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. * arch_env_get_location() - Returns the best env location for an arch
  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 architectures if they need to and has lower
  108. * priority than board side env_get_location() override.
  109. *
  110. * All implementations are free to use the operation, the priority and
  111. * any other data relevant to their choice, but must take into account
  112. * the fact that the lowest prority (0) is the most important location
  113. * in the system. The following locations should be returned by order
  114. * of descending priorities, from the highest to the lowest priority.
  115. *
  116. * Returns:
  117. * an enum env_location value on success, a negative error code otherwise
  118. */
  119. __weak enum env_location arch_env_get_location(enum env_operation op, int prio)
  120. {
  121. if (prio >= ARRAY_SIZE(env_locations))
  122. return ENVL_UNKNOWN;
  123. return env_locations[prio];
  124. }
  125. /**
  126. * env_get_location() - Returns the best env location for a board
  127. * @op: operations performed on the environment
  128. * @prio: priority between the multiple environments, 0 being the
  129. * highest priority
  130. *
  131. * This will return the preferred environment for the given priority.
  132. * This is overridable by boards if they need to.
  133. *
  134. * All implementations are free to use the operation, the priority and
  135. * any other data relevant to their choice, but must take into account
  136. * the fact that the lowest prority (0) is the most important location
  137. * in the system. The following locations should be returned by order
  138. * of descending priorities, from the highest to the lowest priority.
  139. *
  140. * Returns:
  141. * an enum env_location value on success, a negative error code otherwise
  142. */
  143. __weak enum env_location env_get_location(enum env_operation op, int prio)
  144. {
  145. return arch_env_get_location(op, prio);
  146. }
  147. /**
  148. * env_driver_lookup() - Finds the most suited environment location
  149. * @op: operations performed on the environment
  150. * @prio: priority between the multiple environments, 0 being the
  151. * highest priority
  152. *
  153. * This will try to find the available environment with the highest
  154. * priority in the system.
  155. *
  156. * Returns:
  157. * NULL on error, a pointer to a struct env_driver otherwise
  158. */
  159. static struct env_driver *env_driver_lookup(enum env_operation op, int prio)
  160. {
  161. enum env_location loc = env_get_location(op, prio);
  162. struct env_driver *drv;
  163. if (loc == ENVL_UNKNOWN)
  164. return NULL;
  165. drv = _env_driver_lookup(loc);
  166. if (!drv) {
  167. debug("%s: No environment driver for location %d\n", __func__,
  168. loc);
  169. return NULL;
  170. }
  171. return drv;
  172. }
  173. int env_load(void)
  174. {
  175. struct env_driver *drv;
  176. int best_prio = -1;
  177. int prio;
  178. if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
  179. /*
  180. * When using a list of writeable variables, the baseline comes
  181. * from the built-in default env. So load this first.
  182. */
  183. env_set_default(NULL, 0);
  184. }
  185. for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
  186. int ret;
  187. if (!env_has_inited(drv->location))
  188. continue;
  189. printf("Loading Environment from %s... ", drv->name);
  190. /*
  191. * In error case, the error message must be printed during
  192. * drv->load() in some underlying API, and it must be exactly
  193. * one message.
  194. */
  195. ret = drv->load();
  196. if (!ret) {
  197. printf("OK\n");
  198. gd->env_load_prio = prio;
  199. return 0;
  200. } else if (ret == -ENOMSG) {
  201. /* Handle "bad CRC" case */
  202. if (best_prio == -1)
  203. best_prio = prio;
  204. } else {
  205. debug("Failed (%d)\n", ret);
  206. }
  207. }
  208. /*
  209. * In case of invalid environment, we set the 'default' env location
  210. * to the best choice, i.e.:
  211. * 1. Environment location with bad CRC, if such location was found
  212. * 2. Otherwise use the location with highest priority
  213. *
  214. * This way, next calls to env_save() will restore the environment
  215. * at the right place.
  216. */
  217. if (best_prio >= 0)
  218. debug("Selecting environment with bad CRC\n");
  219. else
  220. best_prio = 0;
  221. gd->env_load_prio = best_prio;
  222. return -ENODEV;
  223. }
  224. int env_reload(void)
  225. {
  226. struct env_driver *drv;
  227. drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
  228. if (drv) {
  229. int ret;
  230. printf("Loading Environment from %s... ", drv->name);
  231. if (!env_has_inited(drv->location)) {
  232. printf("not initialized\n");
  233. return -ENODEV;
  234. }
  235. ret = drv->load();
  236. if (ret)
  237. printf("Failed (%d)\n", ret);
  238. else
  239. printf("OK\n");
  240. if (!ret)
  241. return 0;
  242. }
  243. return -ENODEV;
  244. }
  245. int env_save(void)
  246. {
  247. struct env_driver *drv;
  248. drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
  249. if (drv) {
  250. int ret;
  251. printf("Saving Environment to %s... ", drv->name);
  252. if (!drv->save) {
  253. printf("not possible\n");
  254. return -ENODEV;
  255. }
  256. if (!env_has_inited(drv->location)) {
  257. printf("not initialized\n");
  258. return -ENODEV;
  259. }
  260. ret = drv->save();
  261. if (ret)
  262. printf("Failed (%d)\n", ret);
  263. else
  264. printf("OK\n");
  265. if (!ret)
  266. return 0;
  267. }
  268. return -ENODEV;
  269. }
  270. int env_erase(void)
  271. {
  272. struct env_driver *drv;
  273. drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
  274. if (drv) {
  275. int ret;
  276. if (!drv->erase) {
  277. printf("not possible\n");
  278. return -ENODEV;
  279. }
  280. if (!env_has_inited(drv->location)) {
  281. printf("not initialized\n");
  282. return -ENODEV;
  283. }
  284. printf("Erasing Environment on %s... ", drv->name);
  285. ret = drv->erase();
  286. if (ret)
  287. printf("Failed (%d)\n", ret);
  288. else
  289. printf("OK\n");
  290. if (!ret)
  291. return 0;
  292. }
  293. return -ENODEV;
  294. }
  295. int env_init(void)
  296. {
  297. struct env_driver *drv;
  298. int ret = -ENOENT;
  299. int prio;
  300. for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
  301. if (!drv->init || !(ret = drv->init()))
  302. env_set_inited(drv->location);
  303. if (ret == -ENOENT)
  304. env_set_inited(drv->location);
  305. debug("%s: Environment %s init done (ret=%d)\n", __func__,
  306. drv->name, ret);
  307. if (gd->env_valid == ENV_INVALID)
  308. ret = -ENOENT;
  309. }
  310. if (!prio)
  311. return -ENODEV;
  312. if (ret == -ENOENT) {
  313. gd->env_addr = (ulong)&default_environment[0];
  314. gd->env_valid = ENV_VALID;
  315. return 0;
  316. }
  317. return ret;
  318. }
  319. int env_select(const char *name)
  320. {
  321. struct env_driver *drv;
  322. const int n_ents = ll_entry_count(struct env_driver, env_driver);
  323. struct env_driver *entry;
  324. int prio;
  325. bool found = false;
  326. printf("Select Environment on %s: ", name);
  327. /* search ENV driver by name */
  328. drv = ll_entry_start(struct env_driver, env_driver);
  329. for (entry = drv; entry != drv + n_ents; entry++) {
  330. if (!strcmp(entry->name, name)) {
  331. found = true;
  332. break;
  333. }
  334. }
  335. if (!found) {
  336. printf("driver not found\n");
  337. return -ENODEV;
  338. }
  339. /* search priority by driver */
  340. for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
  341. if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
  342. /* when priority change, reset the ENV flags */
  343. if (gd->env_load_prio != prio) {
  344. gd->env_load_prio = prio;
  345. gd->env_valid = ENV_INVALID;
  346. gd->flags &= ~GD_FLG_ENV_DEFAULT;
  347. }
  348. printf("OK\n");
  349. return 0;
  350. }
  351. }
  352. printf("priority not found\n");
  353. return -ENODEV;
  354. }