env_flash.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #ifdef CONFIG_ENV_ADDR_REDUND
  66. int env_init(void)
  67. {
  68. int crc1_ok = 0, crc2_ok = 0;
  69. uchar flag1 = flash_addr->flags;
  70. uchar flag2 = flash_addr_new->flags;
  71. ulong addr_default = (ulong)&default_environment[0];
  72. ulong addr1 = (ulong)&(flash_addr->data);
  73. ulong addr2 = (ulong)&(flash_addr_new->data);
  74. crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
  75. crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
  76. if (crc1_ok && ! crc2_ok) {
  77. gd->env_addr = addr1;
  78. gd->env_valid = 1;
  79. } else if (! crc1_ok && crc2_ok) {
  80. gd->env_addr = addr2;
  81. gd->env_valid = 1;
  82. } else if (! crc1_ok && ! crc2_ok) {
  83. gd->env_addr = addr_default;
  84. gd->env_valid = 0;
  85. } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
  86. gd->env_addr = addr1;
  87. gd->env_valid = 1;
  88. } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
  89. gd->env_addr = addr2;
  90. gd->env_valid = 1;
  91. } else if (flag1 == flag2) {
  92. gd->env_addr = addr1;
  93. gd->env_valid = 2;
  94. } else if (flag1 == 0xFF) {
  95. gd->env_addr = addr1;
  96. gd->env_valid = 2;
  97. } else if (flag2 == 0xFF) {
  98. gd->env_addr = addr2;
  99. gd->env_valid = 2;
  100. }
  101. return 0;
  102. }
  103. #ifdef CMD_SAVEENV
  104. int saveenv(void)
  105. {
  106. env_t env_new;
  107. ssize_t len;
  108. char *saved_data = NULL;
  109. char *res;
  110. int rc = 1;
  111. char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
  112. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  113. ulong up_data = 0;
  114. #endif
  115. debug("Protect off %08lX ... %08lX\n",
  116. (ulong)flash_addr, end_addr);
  117. if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) {
  118. goto done;
  119. }
  120. debug("Protect off %08lX ... %08lX\n",
  121. (ulong)flash_addr_new, end_addr_new);
  122. if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) {
  123. goto done;
  124. }
  125. res = (char *)&env_new.data;
  126. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
  127. if (len < 0) {
  128. error("Cannot export environment: errno = %d\n", errno);
  129. goto done;
  130. }
  131. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  132. env_new.flags = new_flag;
  133. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  134. up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE));
  135. debug("Data to save 0x%lX\n", up_data);
  136. if (up_data) {
  137. if ((saved_data = malloc(up_data)) == NULL) {
  138. printf("Unable to save the rest of sector (%ld)\n",
  139. up_data);
  140. goto done;
  141. }
  142. memcpy(saved_data,
  143. (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data);
  144. debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
  145. (long)flash_addr_new + CONFIG_ENV_SIZE,
  146. up_data, saved_data);
  147. }
  148. #endif
  149. puts("Erasing Flash...");
  150. debug(" %08lX ... %08lX ...",
  151. (ulong)flash_addr_new, end_addr_new);
  152. if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) {
  153. goto done;
  154. }
  155. puts("Writing to Flash... ");
  156. debug(" %08lX ... %08lX ...",
  157. (ulong)&(flash_addr_new->data),
  158. sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
  159. if ((rc = flash_write((char *)&env_new,
  160. (ulong)flash_addr_new,
  161. sizeof(env_new))) ||
  162. (rc = flash_write(&flag,
  163. (ulong)&(flash_addr->flags),
  164. sizeof(flash_addr->flags))) ) {
  165. flash_perror(rc);
  166. goto done;
  167. }
  168. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  169. if (up_data) { /* restore the rest of sector */
  170. debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
  171. (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
  172. if (flash_write(saved_data,
  173. (long)flash_addr_new + CONFIG_ENV_SIZE,
  174. up_data)) {
  175. flash_perror(rc);
  176. goto done;
  177. }
  178. }
  179. #endif
  180. puts("done\n");
  181. {
  182. env_t * etmp = flash_addr;
  183. ulong ltmp = end_addr;
  184. flash_addr = flash_addr_new;
  185. flash_addr_new = etmp;
  186. end_addr = end_addr_new;
  187. end_addr_new = ltmp;
  188. }
  189. rc = 0;
  190. done:
  191. if (saved_data)
  192. free(saved_data);
  193. /* try to re-protect */
  194. (void) flash_sect_protect(1, (ulong)flash_addr, end_addr);
  195. (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
  196. return rc;
  197. }
  198. #endif /* CMD_SAVEENV */
  199. #else /* ! CONFIG_ENV_ADDR_REDUND */
  200. int env_init(void)
  201. {
  202. if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
  203. gd->env_addr = (ulong)&(env_ptr->data);
  204. gd->env_valid = 1;
  205. return(0);
  206. }
  207. gd->env_addr = (ulong)&default_environment[0];
  208. gd->env_valid = 0;
  209. return 0;
  210. }
  211. #ifdef CMD_SAVEENV
  212. int saveenv(void)
  213. {
  214. env_t env_new;
  215. ssize_t len;
  216. int rc = 1;
  217. char *res;
  218. char *saved_data = NULL;
  219. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  220. ulong up_data = 0;
  221. up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE));
  222. debug("Data to save 0x%lx\n", up_data);
  223. if (up_data) {
  224. if ((saved_data = malloc(up_data)) == NULL) {
  225. printf("Unable to save the rest of sector (%ld)\n",
  226. up_data);
  227. goto done;
  228. }
  229. memcpy(saved_data,
  230. (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
  231. debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
  232. (ulong)flash_addr + CONFIG_ENV_SIZE,
  233. up_data,
  234. (ulong)saved_data);
  235. }
  236. #endif /* CONFIG_ENV_SECT_SIZE */
  237. debug("Protect off %08lX ... %08lX\n",
  238. (ulong)flash_addr, end_addr);
  239. if (flash_sect_protect(0, (long)flash_addr, end_addr))
  240. goto done;
  241. res = (char *)&env_new.data;
  242. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
  243. if (len < 0) {
  244. error("Cannot export environment: errno = %d\n", errno);
  245. goto done;
  246. }
  247. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  248. puts("Erasing Flash...");
  249. if (flash_sect_erase((long)flash_addr, end_addr))
  250. goto done;
  251. puts("Writing to Flash... ");
  252. rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
  253. if (rc != 0) {
  254. flash_perror(rc);
  255. goto done;
  256. }
  257. #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
  258. if (up_data) { /* restore the rest of sector */
  259. debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
  260. (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
  261. if (flash_write(saved_data,
  262. (long)flash_addr + CONFIG_ENV_SIZE,
  263. up_data)) {
  264. flash_perror(rc);
  265. goto done;
  266. }
  267. }
  268. #endif
  269. puts("done\n");
  270. rc = 0;
  271. done:
  272. if (saved_data)
  273. free(saved_data);
  274. /* try to re-protect */
  275. (void) flash_sect_protect(1, (long)flash_addr, end_addr);
  276. return rc;
  277. }
  278. #endif /* CMD_SAVEENV */
  279. #endif /* CONFIG_ENV_ADDR_REDUND */
  280. void env_relocate_spec(void)
  281. {
  282. #ifdef CONFIG_ENV_ADDR_REDUND
  283. if (gd->env_addr != (ulong)&(flash_addr->data)) {
  284. env_t *etmp = flash_addr;
  285. ulong ltmp = end_addr;
  286. flash_addr = flash_addr_new;
  287. flash_addr_new = etmp;
  288. end_addr = end_addr_new;
  289. end_addr_new = ltmp;
  290. }
  291. if (flash_addr_new->flags != OBSOLETE_FLAG &&
  292. crc32(0, flash_addr_new->data, ENV_SIZE) ==
  293. flash_addr_new->crc) {
  294. char flag = OBSOLETE_FLAG;
  295. gd->env_valid = 2;
  296. flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
  297. flash_write(&flag,
  298. (ulong)&(flash_addr_new->flags),
  299. sizeof(flash_addr_new->flags));
  300. flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
  301. }
  302. if (flash_addr->flags != ACTIVE_FLAG &&
  303. (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
  304. char flag = ACTIVE_FLAG;
  305. gd->env_valid = 2;
  306. flash_sect_protect(0, (ulong)flash_addr, end_addr);
  307. flash_write(&flag,
  308. (ulong)&(flash_addr->flags),
  309. sizeof(flash_addr->flags));
  310. flash_sect_protect(1, (ulong)flash_addr, end_addr);
  311. }
  312. if (gd->env_valid == 2)
  313. puts ("*** Warning - some problems detected "
  314. "reading environment; recovered successfully\n\n");
  315. #endif /* CONFIG_ENV_ADDR_REDUND */
  316. env_import((char *)flash_addr, 1);
  317. }