env.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. __weak int env_get_char_spec(int index)
  151. {
  152. return *(uchar *)(gd->env_addr + index);
  153. }
  154. int env_get_char(int index)
  155. {
  156. if (gd->env_valid == ENV_INVALID)
  157. return default_environment[index];
  158. else
  159. return env_get_char_spec(index);
  160. }
  161. int env_load(void)
  162. {
  163. struct env_driver *drv;
  164. int best_prio = -1;
  165. int prio;
  166. for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
  167. int ret;
  168. if (!env_has_inited(drv->location))
  169. continue;
  170. printf("Loading Environment from %s... ", drv->name);
  171. /*
  172. * In error case, the error message must be printed during
  173. * drv->load() in some underlying API, and it must be exactly
  174. * one message.
  175. */
  176. ret = drv->load();
  177. if (!ret) {
  178. printf("OK\n");
  179. gd->env_load_prio = prio;
  180. #if !CONFIG_IS_ENABLED(ENV_APPEND)
  181. return 0;
  182. #endif
  183. } else if (ret == -ENOMSG) {
  184. /* Handle "bad CRC" case */
  185. if (best_prio == -1)
  186. best_prio = prio;
  187. } else {
  188. debug("Failed (%d)\n", ret);
  189. }
  190. }
  191. /*
  192. * In case of invalid environment, we set the 'default' env location
  193. * to the best choice, i.e.:
  194. * 1. Environment location with bad CRC, if such location was found
  195. * 2. Otherwise use the location with highest priority
  196. *
  197. * This way, next calls to env_save() will restore the environment
  198. * at the right place.
  199. */
  200. if (best_prio >= 0)
  201. debug("Selecting environment with bad CRC\n");
  202. else
  203. best_prio = 0;
  204. gd->env_load_prio = best_prio;
  205. return -ENODEV;
  206. }
  207. int env_reload(void)
  208. {
  209. struct env_driver *drv;
  210. drv = env_driver_lookup(ENVOP_LOAD, gd->env_load_prio);
  211. if (drv) {
  212. int ret;
  213. printf("Loading Environment from %s... ", drv->name);
  214. if (!env_has_inited(drv->location)) {
  215. printf("not initialized\n");
  216. return -ENODEV;
  217. }
  218. ret = drv->load();
  219. if (ret)
  220. printf("Failed (%d)\n", ret);
  221. else
  222. printf("OK\n");
  223. if (!ret)
  224. return 0;
  225. }
  226. return -ENODEV;
  227. }
  228. int env_save(void)
  229. {
  230. struct env_driver *drv;
  231. drv = env_driver_lookup(ENVOP_SAVE, gd->env_load_prio);
  232. if (drv) {
  233. int ret;
  234. printf("Saving Environment to %s... ", drv->name);
  235. if (!drv->save) {
  236. printf("not possible\n");
  237. return -ENODEV;
  238. }
  239. if (!env_has_inited(drv->location)) {
  240. printf("not initialized\n");
  241. return -ENODEV;
  242. }
  243. ret = drv->save();
  244. if (ret)
  245. printf("Failed (%d)\n", ret);
  246. else
  247. printf("OK\n");
  248. if (!ret)
  249. return 0;
  250. }
  251. return -ENODEV;
  252. }
  253. int env_erase(void)
  254. {
  255. struct env_driver *drv;
  256. drv = env_driver_lookup(ENVOP_ERASE, gd->env_load_prio);
  257. if (drv) {
  258. int ret;
  259. if (!drv->erase)
  260. return -ENODEV;
  261. if (!env_has_inited(drv->location))
  262. return -ENODEV;
  263. printf("Erasing Environment on %s... ", drv->name);
  264. ret = drv->erase();
  265. if (ret)
  266. printf("Failed (%d)\n", ret);
  267. else
  268. printf("OK\n");
  269. if (!ret)
  270. return 0;
  271. }
  272. return -ENODEV;
  273. }
  274. int env_init(void)
  275. {
  276. struct env_driver *drv;
  277. int ret = -ENOENT;
  278. int prio;
  279. for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
  280. if (!drv->init || !(ret = drv->init()))
  281. env_set_inited(drv->location);
  282. if (ret == -ENOENT)
  283. env_set_inited(drv->location);
  284. debug("%s: Environment %s init done (ret=%d)\n", __func__,
  285. drv->name, ret);
  286. if (gd->env_valid == ENV_INVALID)
  287. ret = -ENOENT;
  288. }
  289. if (!prio)
  290. return -ENODEV;
  291. if (ret == -ENOENT) {
  292. gd->env_addr = (ulong)&default_environment[0];
  293. gd->env_valid = ENV_VALID;
  294. return 0;
  295. }
  296. return ret;
  297. }
  298. int env_select(const char *name)
  299. {
  300. struct env_driver *drv;
  301. const int n_ents = ll_entry_count(struct env_driver, env_driver);
  302. struct env_driver *entry;
  303. int prio;
  304. bool found = false;
  305. printf("Select Environment on %s: ", name);
  306. /* search ENV driver by name */
  307. drv = ll_entry_start(struct env_driver, env_driver);
  308. for (entry = drv; entry != drv + n_ents; entry++) {
  309. if (!strcmp(entry->name, name)) {
  310. found = true;
  311. break;
  312. }
  313. }
  314. if (!found) {
  315. printf("driver not found\n");
  316. return -ENODEV;
  317. }
  318. /* search priority by driver */
  319. for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
  320. if (entry->location == env_get_location(ENVOP_LOAD, prio)) {
  321. /* when priority change, reset the ENV flags */
  322. if (gd->env_load_prio != prio) {
  323. gd->env_load_prio = prio;
  324. gd->env_valid = ENV_INVALID;
  325. gd->flags &= ~GD_FLG_ENV_DEFAULT;
  326. }
  327. printf("OK\n");
  328. return 0;
  329. }
  330. }
  331. printf("priority not found\n");
  332. return -ENODEV;
  333. }