cmd_flash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. /*
  85. * This function computes the start and end addresses for both
  86. * erase and protect commands. The range of the addresses on which
  87. * either of the commands is to operate can be given in two forms:
  88. * 1. <cmd> start end - operate on <'start', 'end')
  89. * 2. <cmd> start +length - operate on <'start', start + length)
  90. * If the second form is used and the end address doesn't fall on the
  91. * sector boundary, than it will be adjusted to the next sector boundary.
  92. * If it isn't in the flash, the function will fail (return -1).
  93. * Input:
  94. * arg1, arg2: address specification (i.e. both command arguments)
  95. * Output:
  96. * addr_first, addr_last: computed address range
  97. * Return:
  98. * 1: success
  99. * -1: failure (bad format, bad address).
  100. */
  101. static int
  102. addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
  103. {
  104. char *ep;
  105. *addr_first = simple_strtoul(arg1, &ep, 16);
  106. if (ep == arg1 || *ep != '\0')
  107. return -1;
  108. char len_used = 0; /* indicates if the "start +length" form used */
  109. if (arg2 && *arg2 == '+'){
  110. len_used = 1;
  111. ++arg2;
  112. }
  113. *addr_last = simple_strtoul(arg2, &ep, 16);
  114. if (ep == arg2 || *ep != '\0')
  115. return -1;
  116. if (len_used){
  117. /*
  118. * *addr_last has the length, compute correct *addr_last
  119. * XXX watch out for the integer overflow! Right now it is
  120. * checked for in both the callers.
  121. */
  122. *addr_last = *addr_first + *addr_last - 1;
  123. /*
  124. * It may happen that *addr_last doesn't fall on the sector
  125. * boundary. We want to round such an address to the next
  126. * sector boundary, so that the commands don't fail later on.
  127. */
  128. /* find the end addr of the sector where the *addr_last is */
  129. char found = 0;
  130. ulong bank;
  131. for (bank = 0; bank < CFG_MAX_FLASH_BANKS && !found; ++bank){
  132. int i;
  133. flash_info_t *info = &flash_info[bank];
  134. for (i = 0; i < info->sector_count && !found; ++i){
  135. /* get the end address of the sector */
  136. ulong sector_end_addr;
  137. if (i == info->sector_count - 1){
  138. sector_end_addr =
  139. info->start[0] + info->size - 1;
  140. } else {
  141. sector_end_addr =
  142. info->start[i+1] - 1;
  143. }
  144. if (*addr_last <= sector_end_addr &&
  145. *addr_last >= info->start[i]){
  146. /* sector found */
  147. found = 1;
  148. /* adjust *addr_last if necessary */
  149. if (*addr_last < sector_end_addr){
  150. *addr_last = sector_end_addr;
  151. }
  152. }
  153. } /* sector */
  154. } /* bank */
  155. if (!found){
  156. /* error, addres not in flash */
  157. printf("Error: end address (0x%08lx) not in flash!\n",
  158. *addr_last);
  159. return -1;
  160. }
  161. } /* "start +length" from used */
  162. return 1;
  163. }
  164. static int
  165. flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
  166. int *s_first, int *s_last,
  167. int *s_count )
  168. {
  169. flash_info_t *info;
  170. ulong bank;
  171. int rcode = 0;
  172. *s_count = 0;
  173. for (bank=0; bank < CFG_MAX_FLASH_BANKS; ++bank) {
  174. s_first[bank] = -1; /* first sector to erase */
  175. s_last [bank] = -1; /* last sector to erase */
  176. }
  177. for (bank=0,info=&flash_info[0];
  178. (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last);
  179. ++bank, ++info) {
  180. ulong b_end;
  181. int sect;
  182. short s_end;
  183. if (info->flash_id == FLASH_UNKNOWN) {
  184. continue;
  185. }
  186. b_end = info->start[0] + info->size - 1; /* bank end addr */
  187. s_end = info->sector_count - 1; /* last sector */
  188. for (sect=0; sect < info->sector_count; ++sect) {
  189. ulong end; /* last address in current sect */
  190. end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
  191. if (addr_first > end)
  192. continue;
  193. if (addr_last < info->start[sect])
  194. continue;
  195. if (addr_first == info->start[sect]) {
  196. s_first[bank] = sect;
  197. }
  198. if (addr_last == end) {
  199. s_last[bank] = sect;
  200. }
  201. }
  202. if (s_first[bank] >= 0) {
  203. if (s_last[bank] < 0) {
  204. if (addr_last > b_end) {
  205. s_last[bank] = s_end;
  206. } else {
  207. puts ("Error: end address"
  208. " not on sector boundary\n");
  209. rcode = 1;
  210. break;
  211. }
  212. }
  213. if (s_last[bank] < s_first[bank]) {
  214. puts ("Error: end sector"
  215. " precedes start sector\n");
  216. rcode = 1;
  217. break;
  218. }
  219. sect = s_last[bank];
  220. addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
  221. (*s_count) += s_last[bank] - s_first[bank] + 1;
  222. } else if (addr_first >= info->start[0] && addr_first < b_end) {
  223. puts ("Error: start address not on sector boundary\n");
  224. rcode = 1;
  225. break;
  226. } else if (s_last[bank] >= 0) {
  227. puts ("Error: cannot span across banks when they are"
  228. " mapped in reverse order\n");
  229. rcode = 1;
  230. break;
  231. }
  232. }
  233. return rcode;
  234. }
  235. int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  236. {
  237. ulong bank;
  238. #ifdef CONFIG_HAS_DATAFLASH
  239. dataflash_print_info();
  240. #endif
  241. if (argc == 1) { /* print info for all FLASH banks */
  242. for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
  243. printf ("\nBank # %ld: ", bank+1);
  244. flash_print_info (&flash_info[bank]);
  245. }
  246. return 0;
  247. }
  248. bank = simple_strtoul(argv[1], NULL, 16);
  249. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  250. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  251. CFG_MAX_FLASH_BANKS);
  252. return 1;
  253. }
  254. printf ("\nBank # %ld: ", bank);
  255. flash_print_info (&flash_info[bank-1]);
  256. return 0;
  257. }
  258. int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  259. {
  260. flash_info_t *info;
  261. ulong bank, addr_first, addr_last;
  262. int n, sect_first, sect_last;
  263. int rcode = 0;
  264. if (argc < 2) {
  265. printf ("Usage:\n%s\n", cmdtp->usage);
  266. return 1;
  267. }
  268. if (strcmp(argv[1], "all") == 0) {
  269. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  270. printf ("Erase Flash Bank # %ld ", bank);
  271. info = &flash_info[bank-1];
  272. rcode = flash_erase (info, 0, info->sector_count-1);
  273. }
  274. return rcode;
  275. }
  276. if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
  277. if (n < 0) {
  278. puts ("Bad sector specification\n");
  279. return 1;
  280. }
  281. printf ("Erase Flash Sectors %d-%d in Bank # %d ",
  282. sect_first, sect_last, (info-flash_info)+1);
  283. rcode = flash_erase(info, sect_first, sect_last);
  284. return rcode;
  285. }
  286. if (argc != 3) {
  287. printf ("Usage:\n%s\n", cmdtp->usage);
  288. return 1;
  289. }
  290. if (strcmp(argv[1], "bank") == 0) {
  291. bank = simple_strtoul(argv[2], NULL, 16);
  292. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  293. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  294. CFG_MAX_FLASH_BANKS);
  295. return 1;
  296. }
  297. printf ("Erase Flash Bank # %ld ", bank);
  298. info = &flash_info[bank-1];
  299. rcode = flash_erase (info, 0, info->sector_count-1);
  300. return rcode;
  301. }
  302. if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
  303. printf ("Bad address format\n");
  304. return 1;
  305. }
  306. if (addr_first >= addr_last) {
  307. printf ("Usage:\n%s\n", cmdtp->usage);
  308. return 1;
  309. }
  310. rcode = flash_sect_erase(addr_first, addr_last);
  311. return rcode;
  312. }
  313. int flash_sect_erase (ulong addr_first, ulong addr_last)
  314. {
  315. flash_info_t *info;
  316. ulong bank;
  317. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  318. int erased = 0;
  319. int planned;
  320. int rcode = 0;
  321. rcode = flash_fill_sect_ranges (addr_first, addr_last,
  322. s_first, s_last, &planned );
  323. if (planned && (rcode == 0)) {
  324. for (bank=0,info=&flash_info[0];
  325. (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
  326. ++bank, ++info) {
  327. if (s_first[bank]>=0) {
  328. erased += s_last[bank] - s_first[bank] + 1;
  329. debug ("Erase Flash from 0x%08lx to 0x%08lx "
  330. "in Bank # %ld ",
  331. info->start[s_first[bank]],
  332. (s_last[bank] == info->sector_count) ?
  333. info->start[0] + info->size - 1:
  334. info->start[s_last[bank]+1] - 1,
  335. bank+1);
  336. rcode = flash_erase (info, s_first[bank], s_last[bank]);
  337. }
  338. }
  339. printf ("Erased %d sectors\n", erased);
  340. } else if (rcode == 0) {
  341. puts ("Error: start and/or end address"
  342. " not on sector boundary\n");
  343. rcode = 1;
  344. }
  345. return rcode;
  346. }
  347. int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  348. {
  349. flash_info_t *info;
  350. ulong bank, addr_first, addr_last;
  351. int i, p, n, sect_first, sect_last;
  352. int rcode = 0;
  353. #ifdef CONFIG_HAS_DATAFLASH
  354. int status;
  355. #endif
  356. if (argc < 3) {
  357. printf ("Usage:\n%s\n", cmdtp->usage);
  358. return 1;
  359. }
  360. if (strcmp(argv[1], "off") == 0) {
  361. p = 0;
  362. } else if (strcmp(argv[1], "on") == 0) {
  363. p = 1;
  364. } else {
  365. printf ("Usage:\n%s\n", cmdtp->usage);
  366. return 1;
  367. }
  368. #ifdef CONFIG_HAS_DATAFLASH
  369. if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
  370. addr_first = simple_strtoul(argv[2], NULL, 16);
  371. addr_last = simple_strtoul(argv[3], NULL, 16);
  372. if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
  373. status = dataflash_real_protect(p,addr_first,addr_last);
  374. if (status < 0){
  375. puts ("Bad DataFlash sector specification\n");
  376. return 1;
  377. }
  378. printf("%sProtect %d DataFlash Sectors\n",
  379. p ? "" : "Un-", status);
  380. return 0;
  381. }
  382. }
  383. #endif
  384. if (strcmp(argv[2], "all") == 0) {
  385. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  386. info = &flash_info[bank-1];
  387. if (info->flash_id == FLASH_UNKNOWN) {
  388. continue;
  389. }
  390. printf ("%sProtect Flash Bank # %ld\n",
  391. p ? "" : "Un-", bank);
  392. for (i=0; i<info->sector_count; ++i) {
  393. #if defined(CFG_FLASH_PROTECTION)
  394. if (flash_real_protect(info, i, p))
  395. rcode = 1;
  396. putc ('.');
  397. #else
  398. info->protect[i] = p;
  399. #endif /* CFG_FLASH_PROTECTION */
  400. }
  401. }
  402. #if defined(CFG_FLASH_PROTECTION)
  403. if (!rcode) puts (" done\n");
  404. #endif /* CFG_FLASH_PROTECTION */
  405. return rcode;
  406. }
  407. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  408. if (n < 0) {
  409. puts ("Bad sector specification\n");
  410. return 1;
  411. }
  412. printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
  413. p ? "" : "Un-", sect_first, sect_last,
  414. (info-flash_info)+1);
  415. for (i = sect_first; i <= sect_last; i++) {
  416. #if defined(CFG_FLASH_PROTECTION)
  417. if (flash_real_protect(info, i, p))
  418. rcode = 1;
  419. putc ('.');
  420. #else
  421. info->protect[i] = p;
  422. #endif /* CFG_FLASH_PROTECTION */
  423. }
  424. #if defined(CFG_FLASH_PROTECTION)
  425. if (!rcode) puts (" done\n");
  426. #endif /* CFG_FLASH_PROTECTION */
  427. return rcode;
  428. }
  429. if (argc != 4) {
  430. printf ("Usage:\n%s\n", cmdtp->usage);
  431. return 1;
  432. }
  433. if (strcmp(argv[2], "bank") == 0) {
  434. bank = simple_strtoul(argv[3], NULL, 16);
  435. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  436. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  437. CFG_MAX_FLASH_BANKS);
  438. return 1;
  439. }
  440. printf ("%sProtect Flash Bank # %ld\n",
  441. p ? "" : "Un-", bank);
  442. info = &flash_info[bank-1];
  443. if (info->flash_id == FLASH_UNKNOWN) {
  444. puts ("missing or unknown FLASH type\n");
  445. return 1;
  446. }
  447. for (i=0; i<info->sector_count; ++i) {
  448. #if defined(CFG_FLASH_PROTECTION)
  449. if (flash_real_protect(info, i, p))
  450. rcode = 1;
  451. putc ('.');
  452. #else
  453. info->protect[i] = p;
  454. #endif /* CFG_FLASH_PROTECTION */
  455. }
  456. #if defined(CFG_FLASH_PROTECTION)
  457. if (!rcode) puts (" done\n");
  458. #endif /* CFG_FLASH_PROTECTION */
  459. return rcode;
  460. }
  461. if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
  462. printf("Bad address format\n");
  463. return 1;
  464. }
  465. if (addr_first >= addr_last) {
  466. printf ("Usage:\n%s\n", cmdtp->usage);
  467. return 1;
  468. }
  469. rcode = flash_sect_protect (p, addr_first, addr_last);
  470. return rcode;
  471. }
  472. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  473. {
  474. flash_info_t *info;
  475. ulong bank;
  476. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  477. int protected, i;
  478. int planned;
  479. int rcode;
  480. rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
  481. protected = 0;
  482. if (planned && (rcode == 0)) {
  483. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  484. if (info->flash_id == FLASH_UNKNOWN) {
  485. continue;
  486. }
  487. if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
  488. debug ("%sProtecting sectors %d..%d in bank %ld\n",
  489. p ? "" : "Un-",
  490. s_first[bank], s_last[bank], bank+1);
  491. protected += s_last[bank] - s_first[bank] + 1;
  492. for (i=s_first[bank]; i<=s_last[bank]; ++i) {
  493. #if defined(CFG_FLASH_PROTECTION)
  494. if (flash_real_protect(info, i, p))
  495. rcode = 1;
  496. putc ('.');
  497. #else
  498. info->protect[i] = p;
  499. #endif /* CFG_FLASH_PROTECTION */
  500. }
  501. }
  502. #if defined(CFG_FLASH_PROTECTION)
  503. if (!rcode) putc ('\n');
  504. #endif /* CFG_FLASH_PROTECTION */
  505. }
  506. printf ("%sProtected %d sectors\n",
  507. p ? "" : "Un-", protected);
  508. } else if (rcode == 0) {
  509. puts ("Error: start and/or end address"
  510. " not on sector boundary\n");
  511. rcode = 1;
  512. }
  513. return rcode;
  514. }
  515. /**************************************************/
  516. U_BOOT_CMD(
  517. flinfo, 2, 1, do_flinfo,
  518. "flinfo - print FLASH memory information\n",
  519. "\n - print information for all FLASH memory banks\n"
  520. "flinfo N\n - print information for FLASH memory bank # N\n"
  521. );
  522. U_BOOT_CMD(
  523. erase, 3, 1, do_flerase,
  524. "erase - erase FLASH memory\n",
  525. "start end\n"
  526. " - erase FLASH from addr 'start' to addr 'end'\n"
  527. "erase start +len\n"
  528. " - erase FLASH from addr 'start' to the end of sect "
  529. "w/addr 'start'+'len'-1\n"
  530. "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
  531. "erase bank N\n - erase FLASH bank # N\n"
  532. "erase all\n - erase all FLASH banks\n"
  533. );
  534. U_BOOT_CMD(
  535. protect, 4, 1, do_protect,
  536. "protect - enable or disable FLASH write protection\n",
  537. "on start end\n"
  538. " - protect FLASH from addr 'start' to addr 'end'\n"
  539. "protect on start +len\n"
  540. " - protect FLASH from addr 'start' to end of sect "
  541. "w/addr 'start'+'len'-1\n"
  542. "protect on N:SF[-SL]\n"
  543. " - protect sectors SF-SL in FLASH bank # N\n"
  544. "protect on bank N\n - protect FLASH bank # N\n"
  545. "protect on all\n - protect all FLASH banks\n"
  546. "protect off start end\n"
  547. " - make FLASH from addr 'start' to addr 'end' writable\n"
  548. "protect off start +len\n"
  549. " - make FLASH from addr 'start' to end of sect "
  550. "w/addr 'start'+'len'-1 wrtable\n"
  551. "protect off N:SF[-SL]\n"
  552. " - make sectors SF-SL writable in FLASH bank # N\n"
  553. "protect off bank N\n - make FLASH bank # N writable\n"
  554. "protect off all\n - make all FLASH banks writable\n"
  555. );
  556. #endif /* CFG_CMD_FLASH */