env_flash.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. /* #define DEBUG */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <environment.h>
  29. #include <linux/stddef.h>
  30. #include <malloc.h>
  31. #include <search.h>
  32. #include <errno.h>
  33. DECLARE_GLOBAL_DATA_PTR;
  34. #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
  35. #define CMD_SAVEENV
  36. #elif defined(CONFIG_ENV_ADDR_REDUND)
  37. #error Cannot use CONFIG_ENV_ADDR_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
  38. #endif
  39. #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
  40. #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
  41. #endif
  42. char * env_name_spec = "Flash";
  43. #ifdef ENV_IS_EMBEDDED
  44. extern uchar environment[];
  45. env_t *env_ptr = (env_t *)(&environment[0]);
  46. static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
  47. #else /* ! ENV_IS_EMBEDDED */
  48. env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
  49. static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
  50. #endif /* ENV_IS_EMBEDDED */
  51. #if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND)
  52. /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
  53. static ulong end_addr = CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
  54. #endif
  55. #ifdef CONFIG_ENV_ADDR_REDUND
  56. static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
  57. /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
  58. static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
  59. #endif /* CONFIG_ENV_ADDR_REDUND */
  60. extern uchar default_environment[];
  61. uchar env_get_char_spec(int index)
  62. {
  63. return (*((uchar *)(gd->env_addr + index)));
  64. }
  65. #undef debug
  66. #define debug printf
  67. #ifdef CONFIG_ENV_ADDR_REDUND
  68. int env_init(void)
  69. {
  70. int crc1_ok = 0, crc2_ok = 0;
  71. uchar flag1 = flash_addr->flags;
  72. uchar flag2 = flash_addr_new->flags;
  73. ulong addr_default = (ulong)&default_environment[0];
  74. ulong addr1 = (ulong)&(flash_addr->data);
  75. ulong addr2 = (ulong)&(flash_addr_new->data);
  76. crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
  77. crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
  78. if (crc1_ok && ! crc2_ok) {
  79. gd->env_addr = addr1;
  80. gd->env_valid = 1;
  81. } else if (! crc1_ok && crc2_ok) {
  82. gd->env_addr = addr2;
  83. gd->env_valid = 1;
  84. } else if (! crc1_ok && ! crc2_ok) {
  85. gd->env_addr = addr_default;
  86. gd->env_valid = 0;
  87. } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
  88. gd->env_addr = addr1;
  89. gd->env_valid = 1;
  90. } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
  91. gd->env_addr = addr2;
  92. gd->env_valid = 1;
  93. } else if (flag1 == flag2) {
  94. gd->env_addr = addr1;
  95. gd->env_valid = 2;
  96. } else if (flag1 == 0xFF) {
  97. gd->env_addr = addr1;
  98. gd->env_valid = 2;
  99. } else if (flag2 == 0xFF) {
  100. gd->env_addr = addr2;
  101. gd->env_valid = 2;
  102. }
  103. return 0;
  104. }
  105. #ifdef CMD_SAVEENV
  106. int saveenv(void)
  107. {
  108. env_t env_new;
  109. ssize_t len;
  110. char *saved_data = NULL;
  111. char *res;
  112. int rc = 1;
  113. char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
  114. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  115. ulong up_data = 0;
  116. #endif
  117. debug("Protect off %08lX ... %08lX\n",
  118. (ulong)flash_addr, end_addr);
  119. if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) {
  120. goto done;
  121. }
  122. debug("Protect off %08lX ... %08lX\n",
  123. (ulong)flash_addr_new, end_addr_new);
  124. if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) {
  125. goto done;
  126. }
  127. res = (char *)&env_new.data;
  128. len = hexport('\0', &res, ENV_SIZE);
  129. if (len < 0) {
  130. error("Cannot export environment: errno = %d\n", errno);
  131. goto done;
  132. }
  133. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  134. env_new.flags = new_flag;
  135. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  136. up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE));
  137. debug("Data to save 0x%lX\n", up_data);
  138. if (up_data) {
  139. if ((saved_data = malloc(up_data)) == NULL) {
  140. printf("Unable to save the rest of sector (%ld)\n",
  141. up_data);
  142. goto done;
  143. }
  144. memcpy(saved_data,
  145. (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data);
  146. debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
  147. (long)flash_addr_new + CONFIG_ENV_SIZE,
  148. up_data, saved_data);
  149. }
  150. #endif
  151. puts("Erasing Flash...");
  152. debug(" %08lX ... %08lX ...",
  153. (ulong)flash_addr_new, end_addr_new);
  154. if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) {
  155. goto done;
  156. }
  157. puts("Writing to Flash... ");
  158. debug(" %08lX ... %08lX ...",
  159. (ulong)&(flash_addr_new->data),
  160. sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
  161. if ((rc = flash_write((char *)&env_new,
  162. (ulong)flash_addr_new,
  163. sizeof(env_new))) ||
  164. (rc = flash_write(&flag,
  165. (ulong)&(flash_addr->flags),
  166. sizeof(flash_addr->flags))) ) {
  167. flash_perror(rc);
  168. goto done;
  169. }
  170. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  171. if (up_data) { /* restore the rest of sector */
  172. debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
  173. (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
  174. if (flash_write(saved_data,
  175. (long)flash_addr_new + CONFIG_ENV_SIZE,
  176. up_data)) {
  177. flash_perror(rc);
  178. goto done;
  179. }
  180. }
  181. #endif
  182. puts("done\n");
  183. {
  184. env_t * etmp = flash_addr;
  185. ulong ltmp = end_addr;
  186. flash_addr = flash_addr_new;
  187. flash_addr_new = etmp;
  188. end_addr = end_addr_new;
  189. end_addr_new = ltmp;
  190. }
  191. rc = 0;
  192. done:
  193. if (saved_data)
  194. free(saved_data);
  195. /* try to re-protect */
  196. (void) flash_sect_protect(1, (ulong)flash_addr, end_addr);
  197. (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
  198. return rc;
  199. }
  200. #endif /* CMD_SAVEENV */
  201. #else /* ! CONFIG_ENV_ADDR_REDUND */
  202. int env_init(void)
  203. {
  204. if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
  205. gd->env_addr = (ulong)&(env_ptr->data);
  206. gd->env_valid = 1;
  207. return(0);
  208. }
  209. gd->env_addr = (ulong)&default_environment[0];
  210. gd->env_valid = 0;
  211. return 0;
  212. }
  213. #ifdef CMD_SAVEENV
  214. int saveenv(void)
  215. {
  216. env_t env_new;
  217. ssize_t len;
  218. int rc = 1;
  219. char *res;
  220. char *saved_data = NULL;
  221. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  222. ulong up_data = 0;
  223. up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE));
  224. debug("Data to save 0x%lx\n", up_data);
  225. if (up_data) {
  226. if ((saved_data = malloc(up_data)) == NULL) {
  227. printf("Unable to save the rest of sector (%ld)\n",
  228. up_data);
  229. goto done;
  230. }
  231. memcpy(saved_data,
  232. (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
  233. debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
  234. (ulong)flash_addr + CONFIG_ENV_SIZE,
  235. up_data,
  236. (ulong)saved_data);
  237. }
  238. #endif /* CONFIG_ENV_SECT_SIZE */
  239. debug("Protect off %08lX ... %08lX\n",
  240. (ulong)flash_addr, end_addr);
  241. if (flash_sect_protect(0, (long)flash_addr, end_addr))
  242. goto done;
  243. res = (char *)&env_new.data;
  244. len = hexport('\0', &res, ENV_SIZE);
  245. if (len < 0) {
  246. error("Cannot export environment: errno = %d\n", errno);
  247. goto done;
  248. }
  249. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  250. puts("Erasing Flash...");
  251. if (flash_sect_erase((long)flash_addr, end_addr))
  252. goto done;
  253. puts("Writing to Flash... ");
  254. rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
  255. if (rc != 0) {
  256. flash_perror(rc);
  257. goto done;
  258. }
  259. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  260. if (up_data) { /* restore the rest of sector */
  261. debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
  262. (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
  263. if (flash_write(saved_data,
  264. (long)flash_addr + CONFIG_ENV_SIZE,
  265. up_data)) {
  266. flash_perror(rc);
  267. goto done;
  268. }
  269. }
  270. #endif
  271. puts("done\n");
  272. rc = 0;
  273. done:
  274. if (saved_data)
  275. free(saved_data);
  276. /* try to re-protect */
  277. (void) flash_sect_protect(1, (long)flash_addr, end_addr);
  278. return rc;
  279. }
  280. #endif /* CMD_SAVEENV */
  281. #endif /* CONFIG_ENV_ADDR_REDUND */
  282. void env_relocate_spec(void)
  283. {
  284. #ifdef CONFIG_ENV_ADDR_REDUND
  285. if (gd->env_addr != (ulong)&(flash_addr->data)) {
  286. env_t *etmp = flash_addr;
  287. ulong ltmp = end_addr;
  288. flash_addr = flash_addr_new;
  289. flash_addr_new = etmp;
  290. end_addr = end_addr_new;
  291. end_addr_new = ltmp;
  292. }
  293. if (flash_addr_new->flags != OBSOLETE_FLAG &&
  294. crc32(0, flash_addr_new->data, ENV_SIZE) ==
  295. flash_addr_new->crc) {
  296. char flag = OBSOLETE_FLAG;
  297. gd->env_valid = 2;
  298. flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
  299. flash_write(&flag,
  300. (ulong)&(flash_addr_new->flags),
  301. sizeof(flash_addr_new->flags));
  302. flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
  303. }
  304. if (flash_addr->flags != ACTIVE_FLAG &&
  305. (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
  306. char flag = ACTIVE_FLAG;
  307. gd->env_valid = 2;
  308. flash_sect_protect(0, (ulong)flash_addr, end_addr);
  309. flash_write(&flag,
  310. (ulong)&(flash_addr->flags),
  311. sizeof(flash_addr->flags));
  312. flash_sect_protect(1, (ulong)flash_addr, end_addr);
  313. }
  314. if (gd->env_valid == 2)
  315. puts ("*** Warning - some problems detected "
  316. "reading environment; recovered successfully\n\n");
  317. #endif /* CONFIG_ENV_ADDR_REDUND */
  318. env_import((char *)flash_addr, 1);
  319. }