cmd_flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * FLASH support
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #ifdef CONFIG_HAS_DATAFLASH
  29. #include <dataflash.h>
  30. #endif
  31. #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
  32. extern flash_info_t flash_info[]; /* info for FLASH chips */
  33. /*
  34. * The user interface starts numbering for Flash banks with 1
  35. * for historical reasons.
  36. */
  37. /*
  38. * this routine looks for an abbreviated flash range specification.
  39. * the syntax is B:SF[-SL], where B is the bank number, SF is the first
  40. * sector to erase, and SL is the last sector to erase (defaults to SF).
  41. * bank numbers start at 1 to be consistent with other specs, sector numbers
  42. * start at zero.
  43. *
  44. * returns: 1 - correct spec; *pinfo, *psf and *psl are
  45. * set appropriately
  46. * 0 - doesn't look like an abbreviated spec
  47. * -1 - looks like an abbreviated spec, but got
  48. * a parsing error, a number out of range,
  49. * or an invalid flash bank.
  50. */
  51. static int
  52. abbrev_spec(char *str, flash_info_t **pinfo, int *psf, int *psl)
  53. {
  54. flash_info_t *fp;
  55. int bank, first, last;
  56. char *p, *ep;
  57. if ((p = strchr(str, ':')) == NULL)
  58. return 0;
  59. *p++ = '\0';
  60. bank = simple_strtoul(str, &ep, 10);
  61. if (ep == str || *ep != '\0' ||
  62. bank < 1 || bank > CFG_MAX_FLASH_BANKS ||
  63. (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
  64. return -1;
  65. str = p;
  66. if ((p = strchr(str, '-')) != NULL)
  67. *p++ = '\0';
  68. first = simple_strtoul(str, &ep, 10);
  69. if (ep == str || *ep != '\0' || first >= fp->sector_count)
  70. return -1;
  71. if (p != NULL) {
  72. last = simple_strtoul(p, &ep, 10);
  73. if (ep == p || *ep != '\0' ||
  74. last < first || last >= fp->sector_count)
  75. return -1;
  76. }
  77. else
  78. last = first;
  79. *pinfo = fp;
  80. *psf = first;
  81. *psl = last;
  82. return 1;
  83. }
  84. int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  85. {
  86. ulong bank;
  87. #ifdef CONFIG_HAS_DATAFLASH
  88. dataflash_print_info();
  89. #endif
  90. if (argc == 1) { /* print info for all FLASH banks */
  91. for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
  92. printf ("\nBank # %ld: ", bank+1);
  93. flash_print_info (&flash_info[bank]);
  94. }
  95. return 0;
  96. }
  97. bank = simple_strtoul(argv[1], NULL, 16);
  98. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  99. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  100. CFG_MAX_FLASH_BANKS);
  101. return 1;
  102. }
  103. printf ("\nBank # %ld: ", bank);
  104. flash_print_info (&flash_info[bank-1]);
  105. return 0;
  106. }
  107. int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  108. {
  109. flash_info_t *info;
  110. ulong bank, addr_first, addr_last;
  111. int n, sect_first, sect_last;
  112. int rcode = 0;
  113. if (argc < 2) {
  114. printf ("Usage:\n%s\n", cmdtp->usage);
  115. return 1;
  116. }
  117. if (strcmp(argv[1], "all") == 0) {
  118. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  119. printf ("Erase Flash Bank # %ld ", bank);
  120. info = &flash_info[bank-1];
  121. rcode = flash_erase (info, 0, info->sector_count-1);
  122. }
  123. return rcode;
  124. }
  125. if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
  126. if (n < 0) {
  127. printf("Bad sector specification\n");
  128. return 1;
  129. }
  130. printf ("Erase Flash Sectors %d-%d in Bank # %d ",
  131. sect_first, sect_last, (info-flash_info)+1);
  132. rcode = flash_erase(info, sect_first, sect_last);
  133. return rcode;
  134. }
  135. if (argc != 3) {
  136. printf ("Usage:\n%s\n", cmdtp->usage);
  137. return 1;
  138. }
  139. if (strcmp(argv[1], "bank") == 0) {
  140. bank = simple_strtoul(argv[2], NULL, 16);
  141. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  142. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  143. CFG_MAX_FLASH_BANKS);
  144. return 1;
  145. }
  146. printf ("Erase Flash Bank # %ld ", bank);
  147. info = &flash_info[bank-1];
  148. rcode = flash_erase (info, 0, info->sector_count-1);
  149. return rcode;
  150. }
  151. addr_first = simple_strtoul(argv[1], NULL, 16);
  152. addr_last = simple_strtoul(argv[2], NULL, 16);
  153. if (addr_first >= addr_last) {
  154. printf ("Usage:\n%s\n", cmdtp->usage);
  155. return 1;
  156. }
  157. printf ("Erase Flash from 0x%08lx to 0x%08lx ", addr_first, addr_last);
  158. rcode = flash_sect_erase(addr_first, addr_last);
  159. return rcode;
  160. }
  161. int flash_sect_erase (ulong addr_first, ulong addr_last)
  162. {
  163. flash_info_t *info;
  164. ulong bank;
  165. int s_first, s_last;
  166. int erased;
  167. int rcode = 0;
  168. erased = 0;
  169. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  170. ulong b_end;
  171. int sect;
  172. if (info->flash_id == FLASH_UNKNOWN) {
  173. continue;
  174. }
  175. b_end = info->start[0] + info->size - 1; /* bank end addr */
  176. s_first = -1; /* first sector to erase */
  177. s_last = -1; /* last sector to erase */
  178. for (sect=0; sect < info->sector_count; ++sect) {
  179. ulong end; /* last address in current sect */
  180. short s_end;
  181. s_end = info->sector_count - 1;
  182. end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
  183. if (addr_first > end)
  184. continue;
  185. if (addr_last < info->start[sect])
  186. continue;
  187. if (addr_first == info->start[sect]) {
  188. s_first = sect;
  189. }
  190. if (addr_last == end) {
  191. s_last = sect;
  192. }
  193. }
  194. if (s_first>=0 && s_first<=s_last) {
  195. erased += s_last - s_first + 1;
  196. rcode = flash_erase (info, s_first, s_last);
  197. }
  198. }
  199. if (erased) {
  200. printf ("Erased %d sectors\n", erased);
  201. } else {
  202. printf ("Error: start and/or end address"
  203. " not on sector boundary\n");
  204. rcode = 1;
  205. }
  206. return rcode;
  207. }
  208. int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  209. {
  210. flash_info_t *info;
  211. ulong bank, addr_first, addr_last;
  212. int i, p, n, sect_first, sect_last;
  213. int rcode = 0;
  214. if (argc < 3) {
  215. printf ("Usage:\n%s\n", cmdtp->usage);
  216. return 1;
  217. }
  218. if (strcmp(argv[1], "off") == 0)
  219. p = 0;
  220. else if (strcmp(argv[1], "on") == 0)
  221. p = 1;
  222. else {
  223. printf ("Usage:\n%s\n", cmdtp->usage);
  224. return 1;
  225. }
  226. if (strcmp(argv[2], "all") == 0) {
  227. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  228. info = &flash_info[bank-1];
  229. if (info->flash_id == FLASH_UNKNOWN) {
  230. continue;
  231. }
  232. printf ("%sProtect Flash Bank # %ld\n",
  233. p ? "" : "Un-", bank);
  234. for (i=0; i<info->sector_count; ++i) {
  235. #if defined(CFG_FLASH_PROTECTION)
  236. if (flash_real_protect(info, i, p))
  237. rcode = 1;
  238. putc ('.');
  239. #else
  240. info->protect[i] = p;
  241. #endif /* CFG_FLASH_PROTECTION */
  242. }
  243. }
  244. #if defined(CFG_FLASH_PROTECTION)
  245. if (!rcode) puts (" done\n");
  246. #endif /* CFG_FLASH_PROTECTION */
  247. return rcode;
  248. }
  249. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  250. if (n < 0) {
  251. printf("Bad sector specification\n");
  252. return 1;
  253. }
  254. printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
  255. p ? "" : "Un-", sect_first, sect_last,
  256. (info-flash_info)+1);
  257. for (i = sect_first; i <= sect_last; i++) {
  258. #if defined(CFG_FLASH_PROTECTION)
  259. if (flash_real_protect(info, i, p))
  260. rcode = 1;
  261. putc ('.');
  262. #else
  263. info->protect[i] = p;
  264. #endif /* CFG_FLASH_PROTECTION */
  265. }
  266. #if defined(CFG_FLASH_PROTECTION)
  267. if (!rcode) puts (" done\n");
  268. #endif /* CFG_FLASH_PROTECTION */
  269. return rcode;
  270. }
  271. if (argc != 4) {
  272. printf ("Usage:\n%s\n", cmdtp->usage);
  273. return 1;
  274. }
  275. if (strcmp(argv[2], "bank") == 0) {
  276. bank = simple_strtoul(argv[3], NULL, 16);
  277. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  278. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  279. CFG_MAX_FLASH_BANKS);
  280. return 1;
  281. }
  282. printf ("%sProtect Flash Bank # %ld\n",
  283. p ? "" : "Un-", bank);
  284. info = &flash_info[bank-1];
  285. if (info->flash_id == FLASH_UNKNOWN) {
  286. printf ("missing or unknown FLASH type\n");
  287. return 1;
  288. }
  289. for (i=0; i<info->sector_count; ++i) {
  290. #if defined(CFG_FLASH_PROTECTION)
  291. if (flash_real_protect(info, i, p))
  292. rcode = 1;
  293. putc ('.');
  294. #else
  295. info->protect[i] = p;
  296. #endif /* CFG_FLASH_PROTECTION */
  297. }
  298. #if defined(CFG_FLASH_PROTECTION)
  299. if (!rcode) puts (" done\n");
  300. #endif /* CFG_FLASH_PROTECTION */
  301. return rcode;
  302. }
  303. addr_first = simple_strtoul(argv[2], NULL, 16);
  304. addr_last = simple_strtoul(argv[3], NULL, 16);
  305. if (addr_first >= addr_last) {
  306. printf ("Usage:\n%s\n", cmdtp->usage);
  307. return 1;
  308. }
  309. rcode = flash_sect_protect (p, addr_first, addr_last);
  310. return rcode;
  311. }
  312. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  313. {
  314. flash_info_t *info;
  315. ulong bank;
  316. int s_first, s_last;
  317. int protected, i;
  318. int rcode = 0;
  319. protected = 0;
  320. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  321. ulong b_end;
  322. int sect;
  323. if (info->flash_id == FLASH_UNKNOWN) {
  324. continue;
  325. }
  326. b_end = info->start[0] + info->size - 1; /* bank end addr */
  327. s_first = -1; /* first sector to erase */
  328. s_last = -1; /* last sector to erase */
  329. for (sect=0; sect < info->sector_count; ++sect) {
  330. ulong end; /* last address in current sect */
  331. short s_end;
  332. s_end = info->sector_count - 1;
  333. end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
  334. if (addr_first > end)
  335. continue;
  336. if (addr_last < info->start[sect])
  337. continue;
  338. if (addr_first == info->start[sect]) {
  339. s_first = sect;
  340. }
  341. if (addr_last == end) {
  342. s_last = sect;
  343. }
  344. }
  345. if (s_first>=0 && s_first<=s_last) {
  346. protected += s_last - s_first + 1;
  347. for (i=s_first; i<=s_last; ++i) {
  348. #if defined(CFG_FLASH_PROTECTION)
  349. if (flash_real_protect(info, i, p))
  350. rcode = 1;
  351. putc ('.');
  352. #else
  353. info->protect[i] = p;
  354. #endif /* CFG_FLASH_PROTECTION */
  355. }
  356. }
  357. #if defined(CFG_FLASH_PROTECTION)
  358. if (!rcode) putc ('\n');
  359. #endif /* CFG_FLASH_PROTECTION */
  360. }
  361. if (protected) {
  362. printf ("%sProtected %d sectors\n",
  363. p ? "" : "Un-", protected);
  364. } else {
  365. printf ("Error: start and/or end address"
  366. " not on sector boundary\n");
  367. rcode = 1;
  368. }
  369. return rcode;
  370. }
  371. /**************************************************/
  372. U_BOOT_CMD(
  373. flinfo, 2, 1, do_flinfo,
  374. "flinfo - print FLASH memory information\n",
  375. "\n - print information for all FLASH memory banks\n"
  376. "flinfo N\n - print information for FLASH memory bank # N\n"
  377. );
  378. U_BOOT_CMD(
  379. erase, 3, 1, do_flerase,
  380. "erase - erase FLASH memory\n",
  381. "start end\n"
  382. " - erase FLASH from addr 'start' to addr 'end'\n"
  383. "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
  384. "erase bank N\n - erase FLASH bank # N\n"
  385. "erase all\n - erase all FLASH banks\n"
  386. );
  387. U_BOOT_CMD(
  388. protect, 4, 1, do_protect,
  389. "protect - enable or disable FLASH write protection\n",
  390. "on start end\n"
  391. " - protect FLASH from addr 'start' to addr 'end'\n"
  392. "protect on N:SF[-SL]\n"
  393. " - protect sectors SF-SL in FLASH bank # N\n"
  394. "protect on bank N\n - protect FLASH bank # N\n"
  395. "protect on all\n - protect all FLASH banks\n"
  396. "protect off start end\n"
  397. " - make FLASH from addr 'start' to addr 'end' writable\n"
  398. "protect off N:SF[-SL]\n"
  399. " - make sectors SF-SL writable in FLASH bank # N\n"
  400. "protect off bank N\n - make FLASH bank # N writable\n"
  401. "protect off all\n - make all FLASH banks writable\n"
  402. );
  403. #endif /* CFG_CMD_FLASH */