env_flash.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * (C) Copyright 2000-2010
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  6. * Andreas Heppel <aheppel@sysgo.de>
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. /* #define DEBUG */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <environment.h>
  13. #include <linux/stddef.h>
  14. #include <malloc.h>
  15. #include <search.h>
  16. #include <errno.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
  19. #define CMD_SAVEENV
  20. #elif defined(CONFIG_ENV_ADDR_REDUND)
  21. #error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
  22. #endif
  23. #if defined(CONFIG_ENV_SIZE_REDUND) && \
  24. (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
  25. #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
  26. #endif
  27. char *env_name_spec = "Flash";
  28. #ifdef ENV_IS_EMBEDDED
  29. env_t *env_ptr = &environment;
  30. static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
  31. #else /* ! ENV_IS_EMBEDDED */
  32. env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
  33. static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
  34. #endif /* ENV_IS_EMBEDDED */
  35. #if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND)
  36. /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
  37. static ulong end_addr = CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
  38. #endif
  39. #ifdef CONFIG_ENV_ADDR_REDUND
  40. static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
  41. /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
  42. static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
  43. #endif /* CONFIG_ENV_ADDR_REDUND */
  44. #ifdef CONFIG_ENV_ADDR_REDUND
  45. int env_init(void)
  46. {
  47. int crc1_ok = 0, crc2_ok = 0;
  48. uchar flag1 = flash_addr->flags;
  49. uchar flag2 = flash_addr_new->flags;
  50. ulong addr_default = (ulong)&default_environment[0];
  51. ulong addr1 = (ulong)&(flash_addr->data);
  52. ulong addr2 = (ulong)&(flash_addr_new->data);
  53. crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
  54. crc2_ok =
  55. crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
  56. if (crc1_ok && !crc2_ok) {
  57. gd->env_addr = addr1;
  58. gd->env_valid = 1;
  59. } else if (!crc1_ok && crc2_ok) {
  60. gd->env_addr = addr2;
  61. gd->env_valid = 1;
  62. } else if (!crc1_ok && !crc2_ok) {
  63. gd->env_addr = addr_default;
  64. gd->env_valid = 0;
  65. } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
  66. gd->env_addr = addr1;
  67. gd->env_valid = 1;
  68. } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
  69. gd->env_addr = addr2;
  70. gd->env_valid = 1;
  71. } else if (flag1 == flag2) {
  72. gd->env_addr = addr1;
  73. gd->env_valid = 2;
  74. } else if (flag1 == 0xFF) {
  75. gd->env_addr = addr1;
  76. gd->env_valid = 2;
  77. } else if (flag2 == 0xFF) {
  78. gd->env_addr = addr2;
  79. gd->env_valid = 2;
  80. }
  81. return 0;
  82. }
  83. #ifdef CMD_SAVEENV
  84. int saveenv(void)
  85. {
  86. env_t env_new;
  87. char *saved_data = NULL;
  88. char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
  89. int rc = 1;
  90. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  91. ulong up_data = 0;
  92. #endif
  93. debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
  94. if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
  95. goto done;
  96. debug("Protect off %08lX ... %08lX\n",
  97. (ulong)flash_addr_new, end_addr_new);
  98. if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
  99. goto done;
  100. rc = env_export(&env_new);
  101. if (rc)
  102. return rc;
  103. env_new.flags = new_flag;
  104. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  105. up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
  106. debug("Data to save 0x%lX\n", up_data);
  107. if (up_data) {
  108. saved_data = malloc(up_data);
  109. if (saved_data == NULL) {
  110. printf("Unable to save the rest of sector (%ld)\n",
  111. up_data);
  112. goto done;
  113. }
  114. memcpy(saved_data,
  115. (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
  116. up_data);
  117. debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
  118. (long)flash_addr_new + CONFIG_ENV_SIZE,
  119. up_data, saved_data);
  120. }
  121. #endif
  122. puts("Erasing Flash...");
  123. debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
  124. if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
  125. goto done;
  126. puts("Writing to Flash... ");
  127. debug(" %08lX ... %08lX ...",
  128. (ulong)&(flash_addr_new->data),
  129. sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
  130. rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
  131. sizeof(env_new));
  132. if (rc)
  133. goto perror;
  134. rc = flash_write(&flag, (ulong)&(flash_addr->flags),
  135. sizeof(flash_addr->flags));
  136. if (rc)
  137. goto perror;
  138. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  139. if (up_data) { /* restore the rest of sector */
  140. debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
  141. (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
  142. if (flash_write(saved_data,
  143. (long)flash_addr_new + CONFIG_ENV_SIZE,
  144. up_data))
  145. goto perror;
  146. }
  147. #endif
  148. puts("done\n");
  149. {
  150. env_t *etmp = flash_addr;
  151. ulong ltmp = end_addr;
  152. flash_addr = flash_addr_new;
  153. flash_addr_new = etmp;
  154. end_addr = end_addr_new;
  155. end_addr_new = ltmp;
  156. }
  157. rc = 0;
  158. goto done;
  159. perror:
  160. flash_perror(rc);
  161. done:
  162. if (saved_data)
  163. free(saved_data);
  164. /* try to re-protect */
  165. flash_sect_protect(1, (ulong)flash_addr, end_addr);
  166. flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
  167. return rc;
  168. }
  169. #endif /* CMD_SAVEENV */
  170. #else /* ! CONFIG_ENV_ADDR_REDUND */
  171. int env_init(void)
  172. {
  173. if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
  174. gd->env_addr = (ulong)&(env_ptr->data);
  175. gd->env_valid = 1;
  176. return 0;
  177. }
  178. gd->env_addr = (ulong)&default_environment[0];
  179. gd->env_valid = 0;
  180. return 0;
  181. }
  182. #ifdef CMD_SAVEENV
  183. int saveenv(void)
  184. {
  185. env_t env_new;
  186. int rc = 1;
  187. char *saved_data = NULL;
  188. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  189. ulong up_data = 0;
  190. up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
  191. debug("Data to save 0x%lx\n", up_data);
  192. if (up_data) {
  193. saved_data = malloc(up_data);
  194. if (saved_data == NULL) {
  195. printf("Unable to save the rest of sector (%ld)\n",
  196. up_data);
  197. goto done;
  198. }
  199. memcpy(saved_data,
  200. (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
  201. debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
  202. (ulong)flash_addr + CONFIG_ENV_SIZE,
  203. up_data,
  204. (ulong)saved_data);
  205. }
  206. #endif /* CONFIG_ENV_SECT_SIZE */
  207. debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
  208. if (flash_sect_protect(0, (long)flash_addr, end_addr))
  209. goto done;
  210. rc = env_export(&env_new);
  211. if (rc)
  212. goto done;
  213. puts("Erasing Flash...");
  214. if (flash_sect_erase((long)flash_addr, end_addr))
  215. goto done;
  216. puts("Writing to Flash... ");
  217. rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
  218. if (rc != 0)
  219. goto perror;
  220. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  221. if (up_data) { /* restore the rest of sector */
  222. debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
  223. (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
  224. if (flash_write(saved_data,
  225. (long)flash_addr + CONFIG_ENV_SIZE,
  226. up_data))
  227. goto perror;
  228. }
  229. #endif
  230. puts("done\n");
  231. rc = 0;
  232. goto done;
  233. perror:
  234. flash_perror(rc);
  235. done:
  236. if (saved_data)
  237. free(saved_data);
  238. /* try to re-protect */
  239. flash_sect_protect(1, (long)flash_addr, end_addr);
  240. return rc;
  241. }
  242. #endif /* CMD_SAVEENV */
  243. #endif /* CONFIG_ENV_ADDR_REDUND */
  244. void env_relocate_spec(void)
  245. {
  246. #ifdef CONFIG_ENV_ADDR_REDUND
  247. if (gd->env_addr != (ulong)&(flash_addr->data)) {
  248. env_t *etmp = flash_addr;
  249. ulong ltmp = end_addr;
  250. flash_addr = flash_addr_new;
  251. flash_addr_new = etmp;
  252. end_addr = end_addr_new;
  253. end_addr_new = ltmp;
  254. }
  255. if (flash_addr_new->flags != OBSOLETE_FLAG &&
  256. crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
  257. char flag = OBSOLETE_FLAG;
  258. gd->env_valid = 2;
  259. flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
  260. flash_write(&flag,
  261. (ulong)&(flash_addr_new->flags),
  262. sizeof(flash_addr_new->flags));
  263. flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
  264. }
  265. if (flash_addr->flags != ACTIVE_FLAG &&
  266. (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
  267. char flag = ACTIVE_FLAG;
  268. gd->env_valid = 2;
  269. flash_sect_protect(0, (ulong)flash_addr, end_addr);
  270. flash_write(&flag,
  271. (ulong)&(flash_addr->flags),
  272. sizeof(flash_addr->flags));
  273. flash_sect_protect(1, (ulong)flash_addr, end_addr);
  274. }
  275. if (gd->env_valid == 2)
  276. puts("*** Warning - some problems detected "
  277. "reading environment; recovered successfully\n\n");
  278. #endif /* CONFIG_ENV_ADDR_REDUND */
  279. env_import((char *)flash_addr, 1);
  280. }