cmd_flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. } else {
  77. last = first;
  78. }
  79. *pinfo = fp;
  80. *psf = first;
  81. *psl = last;
  82. return 1;
  83. }
  84. static int
  85. flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
  86. int *s_first, int *s_last,
  87. int *s_count )
  88. {
  89. flash_info_t *info;
  90. ulong bank;
  91. int rcode = 0;
  92. *s_count = 0;
  93. for (bank=0; bank < CFG_MAX_FLASH_BANKS; ++bank) {
  94. s_first[bank] = -1; /* first sector to erase */
  95. s_last [bank] = -1; /* last sector to erase */
  96. }
  97. for (bank=0,info=&flash_info[0];
  98. (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last);
  99. ++bank, ++info) {
  100. ulong b_end;
  101. int sect;
  102. short s_end;
  103. if (info->flash_id == FLASH_UNKNOWN) {
  104. continue;
  105. }
  106. b_end = info->start[0] + info->size - 1; /* bank end addr */
  107. s_end = info->sector_count - 1; /* last sector */
  108. for (sect=0; sect < info->sector_count; ++sect) {
  109. ulong end; /* last address in current sect */
  110. end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
  111. if (addr_first > end)
  112. continue;
  113. if (addr_last < info->start[sect])
  114. continue;
  115. if (addr_first == info->start[sect]) {
  116. s_first[bank] = sect;
  117. }
  118. if (addr_last == end) {
  119. s_last[bank] = sect;
  120. }
  121. }
  122. if (s_first[bank] >= 0) {
  123. if (s_last[bank] < 0) {
  124. if (addr_last > b_end) {
  125. s_last[bank] = s_end;
  126. } else {
  127. printf ("Error: end address"
  128. " not on sector boundary\n");
  129. rcode = 1;
  130. break;
  131. }
  132. }
  133. if (s_last[bank] < s_first[bank]) {
  134. printf ("Error: end sector"
  135. " precedes start sector\n");
  136. rcode = 1;
  137. break;
  138. }
  139. sect = s_last[bank];
  140. addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
  141. (*s_count) += s_last[bank] - s_first[bank] + 1;
  142. }
  143. }
  144. return rcode;
  145. }
  146. int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  147. {
  148. ulong bank;
  149. #ifdef CONFIG_HAS_DATAFLASH
  150. dataflash_print_info();
  151. #endif
  152. if (argc == 1) { /* print info for all FLASH banks */
  153. for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
  154. printf ("\nBank # %ld: ", bank+1);
  155. flash_print_info (&flash_info[bank]);
  156. }
  157. return 0;
  158. }
  159. bank = simple_strtoul(argv[1], NULL, 16);
  160. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  161. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  162. CFG_MAX_FLASH_BANKS);
  163. return 1;
  164. }
  165. printf ("\nBank # %ld: ", bank);
  166. flash_print_info (&flash_info[bank-1]);
  167. return 0;
  168. }
  169. int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  170. {
  171. flash_info_t *info;
  172. ulong bank, addr_first, addr_last;
  173. int n, sect_first, sect_last;
  174. int rcode = 0;
  175. if (argc < 2) {
  176. printf ("Usage:\n%s\n", cmdtp->usage);
  177. return 1;
  178. }
  179. if (strcmp(argv[1], "all") == 0) {
  180. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  181. printf ("Erase Flash Bank # %ld ", bank);
  182. info = &flash_info[bank-1];
  183. rcode = flash_erase (info, 0, info->sector_count-1);
  184. }
  185. return rcode;
  186. }
  187. if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
  188. if (n < 0) {
  189. printf("Bad sector specification\n");
  190. return 1;
  191. }
  192. printf ("Erase Flash Sectors %d-%d in Bank # %d ",
  193. sect_first, sect_last, (info-flash_info)+1);
  194. rcode = flash_erase(info, sect_first, sect_last);
  195. return rcode;
  196. }
  197. if (argc != 3) {
  198. printf ("Usage:\n%s\n", cmdtp->usage);
  199. return 1;
  200. }
  201. if (strcmp(argv[1], "bank") == 0) {
  202. bank = simple_strtoul(argv[2], NULL, 16);
  203. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  204. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  205. CFG_MAX_FLASH_BANKS);
  206. return 1;
  207. }
  208. printf ("Erase Flash Bank # %ld ", bank);
  209. info = &flash_info[bank-1];
  210. rcode = flash_erase (info, 0, info->sector_count-1);
  211. return rcode;
  212. }
  213. addr_first = simple_strtoul(argv[1], NULL, 16);
  214. addr_last = simple_strtoul(argv[2], NULL, 16);
  215. if (addr_first >= addr_last) {
  216. printf ("Usage:\n%s\n", cmdtp->usage);
  217. return 1;
  218. }
  219. rcode = flash_sect_erase(addr_first, addr_last);
  220. return rcode;
  221. }
  222. int flash_sect_erase (ulong addr_first, ulong addr_last)
  223. {
  224. flash_info_t *info;
  225. ulong bank;
  226. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  227. int erased = 0;
  228. int planned;
  229. int rcode = 0;
  230. rcode = flash_fill_sect_ranges (addr_first, addr_last,
  231. s_first, s_last, &planned );
  232. if (planned && (rcode == 0)) {
  233. for (bank=0,info=&flash_info[0];
  234. (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
  235. ++bank, ++info) {
  236. if (s_first[bank]>=0) {
  237. erased += s_last[bank] - s_first[bank] + 1;
  238. debug ("Erase Flash from 0x%08lx to 0x%08lx "
  239. "in Bank # %ld ",
  240. info->start[s_first[bank]],
  241. (s_last[bank] == info->sector_count) ?
  242. info->start[0] + info->size - 1:
  243. info->start[s_last[bank]+1] - 1,
  244. bank+1);
  245. rcode = flash_erase (info, s_first[bank], s_last[bank]);
  246. }
  247. }
  248. printf ("Erased %d sectors\n", erased);
  249. } else if (rcode == 0) {
  250. printf ("Error: start and/or end address"
  251. " not on sector boundary\n");
  252. rcode = 1;
  253. }
  254. return rcode;
  255. }
  256. int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  257. {
  258. flash_info_t *info;
  259. ulong bank, addr_first, addr_last;
  260. int i, p, n, sect_first, sect_last;
  261. int rcode = 0;
  262. if (argc < 3) {
  263. printf ("Usage:\n%s\n", cmdtp->usage);
  264. return 1;
  265. }
  266. if (strcmp(argv[1], "off") == 0) {
  267. p = 0;
  268. } else if (strcmp(argv[1], "on") == 0) {
  269. p = 1;
  270. } else {
  271. printf ("Usage:\n%s\n", cmdtp->usage);
  272. return 1;
  273. }
  274. if (strcmp(argv[2], "all") == 0) {
  275. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  276. info = &flash_info[bank-1];
  277. if (info->flash_id == FLASH_UNKNOWN) {
  278. continue;
  279. }
  280. printf ("%sProtect Flash Bank # %ld\n",
  281. p ? "" : "Un-", bank);
  282. for (i=0; i<info->sector_count; ++i) {
  283. #if defined(CFG_FLASH_PROTECTION)
  284. if (flash_real_protect(info, i, p))
  285. rcode = 1;
  286. putc ('.');
  287. #else
  288. info->protect[i] = p;
  289. #endif /* CFG_FLASH_PROTECTION */
  290. }
  291. }
  292. #if defined(CFG_FLASH_PROTECTION)
  293. if (!rcode) puts (" done\n");
  294. #endif /* CFG_FLASH_PROTECTION */
  295. return rcode;
  296. }
  297. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  298. if (n < 0) {
  299. printf("Bad sector specification\n");
  300. return 1;
  301. }
  302. printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
  303. p ? "" : "Un-", sect_first, sect_last,
  304. (info-flash_info)+1);
  305. for (i = sect_first; i <= sect_last; i++) {
  306. #if defined(CFG_FLASH_PROTECTION)
  307. if (flash_real_protect(info, i, p))
  308. rcode = 1;
  309. putc ('.');
  310. #else
  311. info->protect[i] = p;
  312. #endif /* CFG_FLASH_PROTECTION */
  313. }
  314. #if defined(CFG_FLASH_PROTECTION)
  315. if (!rcode) puts (" done\n");
  316. #endif /* CFG_FLASH_PROTECTION */
  317. return rcode;
  318. }
  319. if (argc != 4) {
  320. printf ("Usage:\n%s\n", cmdtp->usage);
  321. return 1;
  322. }
  323. if (strcmp(argv[2], "bank") == 0) {
  324. bank = simple_strtoul(argv[3], NULL, 16);
  325. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  326. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  327. CFG_MAX_FLASH_BANKS);
  328. return 1;
  329. }
  330. printf ("%sProtect Flash Bank # %ld\n",
  331. p ? "" : "Un-", bank);
  332. info = &flash_info[bank-1];
  333. if (info->flash_id == FLASH_UNKNOWN) {
  334. printf ("missing or unknown FLASH type\n");
  335. return 1;
  336. }
  337. for (i=0; i<info->sector_count; ++i) {
  338. #if defined(CFG_FLASH_PROTECTION)
  339. if (flash_real_protect(info, i, p))
  340. rcode = 1;
  341. putc ('.');
  342. #else
  343. info->protect[i] = p;
  344. #endif /* CFG_FLASH_PROTECTION */
  345. }
  346. #if defined(CFG_FLASH_PROTECTION)
  347. if (!rcode) puts (" done\n");
  348. #endif /* CFG_FLASH_PROTECTION */
  349. return rcode;
  350. }
  351. addr_first = simple_strtoul(argv[2], NULL, 16);
  352. addr_last = simple_strtoul(argv[3], NULL, 16);
  353. if (addr_first >= addr_last) {
  354. printf ("Usage:\n%s\n", cmdtp->usage);
  355. return 1;
  356. }
  357. rcode = flash_sect_protect (p, addr_first, addr_last);
  358. return rcode;
  359. }
  360. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  361. {
  362. flash_info_t *info;
  363. ulong bank;
  364. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  365. int protected, i;
  366. int planned;
  367. int rcode;
  368. rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
  369. protected = 0;
  370. if (planned && (rcode == 0)) {
  371. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  372. if (info->flash_id == FLASH_UNKNOWN) {
  373. continue;
  374. }
  375. if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
  376. debug ("Protecting sectors %d..%d in bank %ld\n",
  377. s_first[bank], s_last[bank], bank+1);
  378. protected += s_last[bank] - s_first[bank] + 1;
  379. for (i=s_first[bank]; i<=s_last[bank]; ++i) {
  380. #if defined(CFG_FLASH_PROTECTION)
  381. if (flash_real_protect(info, i, p))
  382. rcode = 1;
  383. putc ('.');
  384. #else
  385. info->protect[i] = p;
  386. #endif /* CFG_FLASH_PROTECTION */
  387. }
  388. }
  389. #if defined(CFG_FLASH_PROTECTION)
  390. if (!rcode) putc ('\n');
  391. #endif /* CFG_FLASH_PROTECTION */
  392. }
  393. printf ("%sProtected %d sectors\n",
  394. p ? "" : "Un-", protected);
  395. } else if (rcode == 0) {
  396. printf ("Error: start and/or end address"
  397. " not on sector boundary\n");
  398. rcode = 1;
  399. }
  400. return rcode;
  401. }
  402. /**************************************************/
  403. U_BOOT_CMD(
  404. flinfo, 2, 1, do_flinfo,
  405. "flinfo - print FLASH memory information\n",
  406. "\n - print information for all FLASH memory banks\n"
  407. "flinfo N\n - print information for FLASH memory bank # N\n"
  408. );
  409. U_BOOT_CMD(
  410. erase, 3, 1, do_flerase,
  411. "erase - erase FLASH memory\n",
  412. "start end\n"
  413. " - erase FLASH from addr 'start' to addr 'end'\n"
  414. "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
  415. "erase bank N\n - erase FLASH bank # N\n"
  416. "erase all\n - erase all FLASH banks\n"
  417. );
  418. U_BOOT_CMD(
  419. protect, 4, 1, do_protect,
  420. "protect - enable or disable FLASH write protection\n",
  421. "on start end\n"
  422. " - protect FLASH from addr 'start' to addr 'end'\n"
  423. "protect on N:SF[-SL]\n"
  424. " - protect sectors SF-SL in FLASH bank # N\n"
  425. "protect on bank N\n - protect FLASH bank # N\n"
  426. "protect on all\n - protect all FLASH banks\n"
  427. "protect off start end\n"
  428. " - make FLASH from addr 'start' to addr 'end' writable\n"
  429. "protect off N:SF[-SL]\n"
  430. " - make sectors SF-SL writable in FLASH bank # N\n"
  431. "protect off bank N\n - make FLASH bank # N writable\n"
  432. "protect off all\n - make all FLASH banks writable\n"
  433. );
  434. #endif /* CFG_CMD_FLASH */