env.c 8.0 KB

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