env.c 9.1 KB

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