cmd_flash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  32. #include <jffs2/jffs2.h>
  33. /* parition handling routines */
  34. int mtdparts_init(void);
  35. int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
  36. int find_dev_and_part(const char *id, struct mtd_device **dev,
  37. u8 *part_num, struct part_info **part);
  38. #endif
  39. #ifndef CFG_NO_FLASH
  40. extern flash_info_t flash_info[]; /* info for FLASH chips */
  41. /*
  42. * The user interface starts numbering for Flash banks with 1
  43. * for historical reasons.
  44. */
  45. /*
  46. * this routine looks for an abbreviated flash range specification.
  47. * the syntax is B:SF[-SL], where B is the bank number, SF is the first
  48. * sector to erase, and SL is the last sector to erase (defaults to SF).
  49. * bank numbers start at 1 to be consistent with other specs, sector numbers
  50. * start at zero.
  51. *
  52. * returns: 1 - correct spec; *pinfo, *psf and *psl are
  53. * set appropriately
  54. * 0 - doesn't look like an abbreviated spec
  55. * -1 - looks like an abbreviated spec, but got
  56. * a parsing error, a number out of range,
  57. * or an invalid flash bank.
  58. */
  59. static int
  60. abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
  61. {
  62. flash_info_t *fp;
  63. int bank, first, last;
  64. char *p, *ep;
  65. if ((p = strchr (str, ':')) == NULL)
  66. return 0;
  67. *p++ = '\0';
  68. bank = simple_strtoul (str, &ep, 10);
  69. if (ep == str || *ep != '\0' ||
  70. bank < 1 || bank > CFG_MAX_FLASH_BANKS ||
  71. (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
  72. return -1;
  73. str = p;
  74. if ((p = strchr (str, '-')) != NULL)
  75. *p++ = '\0';
  76. first = simple_strtoul (str, &ep, 10);
  77. if (ep == str || *ep != '\0' || first >= fp->sector_count)
  78. return -1;
  79. if (p != NULL) {
  80. last = simple_strtoul (p, &ep, 10);
  81. if (ep == p || *ep != '\0' ||
  82. last < first || last >= fp->sector_count)
  83. return -1;
  84. } else {
  85. last = first;
  86. }
  87. *pinfo = fp;
  88. *psf = first;
  89. *psl = last;
  90. return 1;
  91. }
  92. /*
  93. * This function computes the start and end addresses for both
  94. * erase and protect commands. The range of the addresses on which
  95. * either of the commands is to operate can be given in two forms:
  96. * 1. <cmd> start end - operate on <'start', 'end')
  97. * 2. <cmd> start +length - operate on <'start', start + length)
  98. * If the second form is used and the end address doesn't fall on the
  99. * sector boundary, than it will be adjusted to the next sector boundary.
  100. * If it isn't in the flash, the function will fail (return -1).
  101. * Input:
  102. * arg1, arg2: address specification (i.e. both command arguments)
  103. * Output:
  104. * addr_first, addr_last: computed address range
  105. * Return:
  106. * 1: success
  107. * -1: failure (bad format, bad address).
  108. */
  109. static int
  110. addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
  111. {
  112. char *ep;
  113. char len_used; /* indicates if the "start +length" form used */
  114. char found;
  115. ulong bank;
  116. *addr_first = simple_strtoul(arg1, &ep, 16);
  117. if (ep == arg1 || *ep != '\0')
  118. return -1;
  119. len_used = 0;
  120. if (arg2 && *arg2 == '+'){
  121. len_used = 1;
  122. ++arg2;
  123. }
  124. *addr_last = simple_strtoul(arg2, &ep, 16);
  125. if (ep == arg2 || *ep != '\0')
  126. return -1;
  127. if (len_used){
  128. /*
  129. * *addr_last has the length, compute correct *addr_last
  130. * XXX watch out for the integer overflow! Right now it is
  131. * checked for in both the callers.
  132. */
  133. *addr_last = *addr_first + *addr_last - 1;
  134. /*
  135. * It may happen that *addr_last doesn't fall on the sector
  136. * boundary. We want to round such an address to the next
  137. * sector boundary, so that the commands don't fail later on.
  138. */
  139. /* find the end addr of the sector where the *addr_last is */
  140. found = 0;
  141. for (bank = 0; bank < CFG_MAX_FLASH_BANKS && !found; ++bank){
  142. int i;
  143. flash_info_t *info = &flash_info[bank];
  144. for (i = 0; i < info->sector_count && !found; ++i){
  145. /* get the end address of the sector */
  146. ulong sector_end_addr;
  147. if (i == info->sector_count - 1){
  148. sector_end_addr =
  149. info->start[0] + info->size - 1;
  150. } else {
  151. sector_end_addr =
  152. info->start[i+1] - 1;
  153. }
  154. if (*addr_last <= sector_end_addr &&
  155. *addr_last >= info->start[i]){
  156. /* sector found */
  157. found = 1;
  158. /* adjust *addr_last if necessary */
  159. if (*addr_last < sector_end_addr){
  160. *addr_last = sector_end_addr;
  161. }
  162. }
  163. } /* sector */
  164. } /* bank */
  165. if (!found){
  166. /* error, addres not in flash */
  167. printf("Error: end address (0x%08lx) not in flash!\n",
  168. *addr_last);
  169. return -1;
  170. }
  171. } /* "start +length" from used */
  172. return 1;
  173. }
  174. static int
  175. flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
  176. int *s_first, int *s_last,
  177. int *s_count )
  178. {
  179. flash_info_t *info;
  180. ulong bank;
  181. int rcode = 0;
  182. *s_count = 0;
  183. for (bank=0; bank < CFG_MAX_FLASH_BANKS; ++bank) {
  184. s_first[bank] = -1; /* first sector to erase */
  185. s_last [bank] = -1; /* last sector to erase */
  186. }
  187. for (bank=0,info=&flash_info[0];
  188. (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last);
  189. ++bank, ++info) {
  190. ulong b_end;
  191. int sect;
  192. short s_end;
  193. if (info->flash_id == FLASH_UNKNOWN) {
  194. continue;
  195. }
  196. b_end = info->start[0] + info->size - 1; /* bank end addr */
  197. s_end = info->sector_count - 1; /* last sector */
  198. for (sect=0; sect < info->sector_count; ++sect) {
  199. ulong end; /* last address in current sect */
  200. end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
  201. if (addr_first > end)
  202. continue;
  203. if (addr_last < info->start[sect])
  204. continue;
  205. if (addr_first == info->start[sect]) {
  206. s_first[bank] = sect;
  207. }
  208. if (addr_last == end) {
  209. s_last[bank] = sect;
  210. }
  211. }
  212. if (s_first[bank] >= 0) {
  213. if (s_last[bank] < 0) {
  214. if (addr_last > b_end) {
  215. s_last[bank] = s_end;
  216. } else {
  217. puts ("Error: end address"
  218. " not on sector boundary\n");
  219. rcode = 1;
  220. break;
  221. }
  222. }
  223. if (s_last[bank] < s_first[bank]) {
  224. puts ("Error: end sector"
  225. " precedes start sector\n");
  226. rcode = 1;
  227. break;
  228. }
  229. sect = s_last[bank];
  230. addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
  231. (*s_count) += s_last[bank] - s_first[bank] + 1;
  232. } else if (addr_first >= info->start[0] && addr_first < b_end) {
  233. puts ("Error: start address not on sector boundary\n");
  234. rcode = 1;
  235. break;
  236. } else if (s_last[bank] >= 0) {
  237. puts ("Error: cannot span across banks when they are"
  238. " mapped in reverse order\n");
  239. rcode = 1;
  240. break;
  241. }
  242. }
  243. return rcode;
  244. }
  245. #endif /* CFG_NO_FLASH */
  246. int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  247. {
  248. #ifndef CFG_NO_FLASH
  249. ulong bank;
  250. #endif
  251. #ifdef CONFIG_HAS_DATAFLASH
  252. dataflash_print_info();
  253. #endif
  254. #ifndef CFG_NO_FLASH
  255. if (argc == 1) { /* print info for all FLASH banks */
  256. for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
  257. printf ("\nBank # %ld: ", bank+1);
  258. flash_print_info (&flash_info[bank]);
  259. }
  260. return 0;
  261. }
  262. bank = simple_strtoul(argv[1], NULL, 16);
  263. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  264. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  265. CFG_MAX_FLASH_BANKS);
  266. return 1;
  267. }
  268. printf ("\nBank # %ld: ", bank);
  269. flash_print_info (&flash_info[bank-1]);
  270. #endif /* CFG_NO_FLASH */
  271. return 0;
  272. }
  273. int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  274. {
  275. #ifndef CFG_NO_FLASH
  276. flash_info_t *info;
  277. ulong bank, addr_first, addr_last;
  278. int n, sect_first, sect_last;
  279. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  280. struct mtd_device *dev;
  281. struct part_info *part;
  282. u8 dev_type, dev_num, pnum;
  283. #endif
  284. int rcode = 0;
  285. if (argc < 2) {
  286. printf ("Usage:\n%s\n", cmdtp->usage);
  287. return 1;
  288. }
  289. if (strcmp(argv[1], "all") == 0) {
  290. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  291. printf ("Erase Flash Bank # %ld ", bank);
  292. info = &flash_info[bank-1];
  293. rcode = flash_erase (info, 0, info->sector_count-1);
  294. }
  295. return rcode;
  296. }
  297. if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
  298. if (n < 0) {
  299. puts ("Bad sector specification\n");
  300. return 1;
  301. }
  302. printf ("Erase Flash Sectors %d-%d in Bank # %d ",
  303. sect_first, sect_last, (info-flash_info)+1);
  304. rcode = flash_erase(info, sect_first, sect_last);
  305. return rcode;
  306. }
  307. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  308. /* erase <part-id> - erase partition */
  309. if ((argc == 2) && (id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
  310. mtdparts_init();
  311. if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
  312. if (dev->id->type == MTD_DEV_TYPE_NOR) {
  313. bank = dev->id->num;
  314. info = &flash_info[bank];
  315. addr_first = part->offset + info->start[0];
  316. addr_last = addr_first + part->size - 1;
  317. printf ("Erase Flash Parition %s, "
  318. "bank %d, 0x%08lx - 0x%08lx ",
  319. argv[1], bank, addr_first,
  320. addr_last);
  321. rcode = flash_sect_erase(addr_first, addr_last);
  322. return rcode;
  323. }
  324. printf("cannot erase, not a NOR device\n");
  325. return 1;
  326. }
  327. }
  328. #endif
  329. if (argc != 3) {
  330. printf ("Usage:\n%s\n", cmdtp->usage);
  331. return 1;
  332. }
  333. if (strcmp(argv[1], "bank") == 0) {
  334. bank = simple_strtoul(argv[2], NULL, 16);
  335. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  336. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  337. CFG_MAX_FLASH_BANKS);
  338. return 1;
  339. }
  340. printf ("Erase Flash Bank # %ld ", bank);
  341. info = &flash_info[bank-1];
  342. rcode = flash_erase (info, 0, info->sector_count-1);
  343. return rcode;
  344. }
  345. if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
  346. printf ("Bad address format\n");
  347. return 1;
  348. }
  349. if (addr_first >= addr_last) {
  350. printf ("Usage:\n%s\n", cmdtp->usage);
  351. return 1;
  352. }
  353. rcode = flash_sect_erase(addr_first, addr_last);
  354. return rcode;
  355. #else
  356. return 0;
  357. #endif /* CFG_NO_FLASH */
  358. }
  359. #ifndef CFG_NO_FLASH
  360. int flash_sect_erase (ulong addr_first, ulong addr_last)
  361. {
  362. flash_info_t *info;
  363. ulong bank;
  364. #ifdef CFG_MAX_FLASH_BANKS_DETECT
  365. int s_first[CFG_MAX_FLASH_BANKS_DETECT], s_last[CFG_MAX_FLASH_BANKS_DETECT];
  366. #else
  367. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  368. #endif
  369. int erased = 0;
  370. int planned;
  371. int rcode = 0;
  372. rcode = flash_fill_sect_ranges (addr_first, addr_last,
  373. s_first, s_last, &planned );
  374. if (planned && (rcode == 0)) {
  375. for (bank=0,info=&flash_info[0];
  376. (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
  377. ++bank, ++info) {
  378. if (s_first[bank]>=0) {
  379. erased += s_last[bank] - s_first[bank] + 1;
  380. debug ("Erase Flash from 0x%08lx to 0x%08lx "
  381. "in Bank # %ld ",
  382. info->start[s_first[bank]],
  383. (s_last[bank] == info->sector_count) ?
  384. info->start[0] + info->size - 1:
  385. info->start[s_last[bank]+1] - 1,
  386. bank+1);
  387. rcode = flash_erase (info, s_first[bank], s_last[bank]);
  388. }
  389. }
  390. printf ("Erased %d sectors\n", erased);
  391. } else if (rcode == 0) {
  392. puts ("Error: start and/or end address"
  393. " not on sector boundary\n");
  394. rcode = 1;
  395. }
  396. return rcode;
  397. }
  398. #endif /* CFG_NO_FLASH */
  399. int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  400. {
  401. #ifndef CFG_NO_FLASH
  402. flash_info_t *info;
  403. ulong bank;
  404. int i, n, sect_first, sect_last;
  405. #endif /* CFG_NO_FLASH */
  406. ulong addr_first, addr_last;
  407. int p;
  408. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  409. struct mtd_device *dev;
  410. struct part_info *part;
  411. u8 dev_type, dev_num, pnum;
  412. #endif
  413. int rcode = 0;
  414. #ifdef CONFIG_HAS_DATAFLASH
  415. int status;
  416. #endif
  417. if (argc < 3) {
  418. printf ("Usage:\n%s\n", cmdtp->usage);
  419. return 1;
  420. }
  421. if (strcmp(argv[1], "off") == 0) {
  422. p = 0;
  423. } else if (strcmp(argv[1], "on") == 0) {
  424. p = 1;
  425. } else {
  426. printf ("Usage:\n%s\n", cmdtp->usage);
  427. return 1;
  428. }
  429. #ifdef CONFIG_HAS_DATAFLASH
  430. if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
  431. addr_first = simple_strtoul(argv[2], NULL, 16);
  432. addr_last = simple_strtoul(argv[3], NULL, 16);
  433. if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
  434. status = dataflash_real_protect(p,addr_first,addr_last);
  435. if (status < 0){
  436. puts ("Bad DataFlash sector specification\n");
  437. return 1;
  438. }
  439. printf("%sProtect %d DataFlash Sectors\n",
  440. p ? "" : "Un-", status);
  441. return 0;
  442. }
  443. }
  444. #endif
  445. #ifndef CFG_NO_FLASH
  446. if (strcmp(argv[2], "all") == 0) {
  447. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  448. info = &flash_info[bank-1];
  449. if (info->flash_id == FLASH_UNKNOWN) {
  450. continue;
  451. }
  452. printf ("%sProtect Flash Bank # %ld\n",
  453. p ? "" : "Un-", bank);
  454. for (i=0; i<info->sector_count; ++i) {
  455. #if defined(CFG_FLASH_PROTECTION)
  456. if (flash_real_protect(info, i, p))
  457. rcode = 1;
  458. putc ('.');
  459. #else
  460. info->protect[i] = p;
  461. #endif /* CFG_FLASH_PROTECTION */
  462. }
  463. #if defined(CFG_FLASH_PROTECTION)
  464. if (!rcode) puts (" done\n");
  465. #endif /* CFG_FLASH_PROTECTION */
  466. }
  467. return rcode;
  468. }
  469. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  470. if (n < 0) {
  471. puts ("Bad sector specification\n");
  472. return 1;
  473. }
  474. printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
  475. p ? "" : "Un-", sect_first, sect_last,
  476. (info-flash_info)+1);
  477. for (i = sect_first; i <= sect_last; i++) {
  478. #if defined(CFG_FLASH_PROTECTION)
  479. if (flash_real_protect(info, i, p))
  480. rcode = 1;
  481. putc ('.');
  482. #else
  483. info->protect[i] = p;
  484. #endif /* CFG_FLASH_PROTECTION */
  485. }
  486. #if defined(CFG_FLASH_PROTECTION)
  487. if (!rcode) puts (" done\n");
  488. #endif /* CFG_FLASH_PROTECTION */
  489. return rcode;
  490. }
  491. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  492. /* protect on/off <part-id> */
  493. if ((argc == 3) && (id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
  494. mtdparts_init();
  495. if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
  496. if (dev->id->type == MTD_DEV_TYPE_NOR) {
  497. bank = dev->id->num;
  498. info = &flash_info[bank];
  499. addr_first = part->offset + info->start[0];
  500. addr_last = addr_first + part->size - 1;
  501. printf ("%sProtect Flash Parition %s, "
  502. "bank %d, 0x%08lx - 0x%08lx\n",
  503. p ? "" : "Un", argv[1],
  504. bank, addr_first, addr_last);
  505. rcode = flash_sect_protect (p, addr_first, addr_last);
  506. return rcode;
  507. }
  508. printf("cannot %sprotect, not a NOR device\n",
  509. p ? "" : "un");
  510. return 1;
  511. }
  512. }
  513. #endif
  514. if (argc != 4) {
  515. printf ("Usage:\n%s\n", cmdtp->usage);
  516. return 1;
  517. }
  518. if (strcmp(argv[2], "bank") == 0) {
  519. bank = simple_strtoul(argv[3], NULL, 16);
  520. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  521. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  522. CFG_MAX_FLASH_BANKS);
  523. return 1;
  524. }
  525. printf ("%sProtect Flash Bank # %ld\n",
  526. p ? "" : "Un-", bank);
  527. info = &flash_info[bank-1];
  528. if (info->flash_id == FLASH_UNKNOWN) {
  529. puts ("missing or unknown FLASH type\n");
  530. return 1;
  531. }
  532. for (i=0; i<info->sector_count; ++i) {
  533. #if defined(CFG_FLASH_PROTECTION)
  534. if (flash_real_protect(info, i, p))
  535. rcode = 1;
  536. putc ('.');
  537. #else
  538. info->protect[i] = p;
  539. #endif /* CFG_FLASH_PROTECTION */
  540. }
  541. #if defined(CFG_FLASH_PROTECTION)
  542. if (!rcode) puts (" done\n");
  543. #endif /* CFG_FLASH_PROTECTION */
  544. return rcode;
  545. }
  546. if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
  547. printf("Bad address format\n");
  548. return 1;
  549. }
  550. if (addr_first >= addr_last) {
  551. printf ("Usage:\n%s\n", cmdtp->usage);
  552. return 1;
  553. }
  554. rcode = flash_sect_protect (p, addr_first, addr_last);
  555. #endif /* CFG_NO_FLASH */
  556. return rcode;
  557. }
  558. #ifndef CFG_NO_FLASH
  559. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  560. {
  561. flash_info_t *info;
  562. ulong bank;
  563. #ifdef CFG_MAX_FLASH_BANKS_DETECT
  564. int s_first[CFG_MAX_FLASH_BANKS_DETECT], s_last[CFG_MAX_FLASH_BANKS_DETECT];
  565. #else
  566. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  567. #endif
  568. int protected, i;
  569. int planned;
  570. int rcode;
  571. rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
  572. protected = 0;
  573. if (planned && (rcode == 0)) {
  574. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  575. if (info->flash_id == FLASH_UNKNOWN) {
  576. continue;
  577. }
  578. if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
  579. debug ("%sProtecting sectors %d..%d in bank %ld\n",
  580. p ? "" : "Un-",
  581. s_first[bank], s_last[bank], bank+1);
  582. protected += s_last[bank] - s_first[bank] + 1;
  583. for (i=s_first[bank]; i<=s_last[bank]; ++i) {
  584. #if defined(CFG_FLASH_PROTECTION)
  585. if (flash_real_protect(info, i, p))
  586. rcode = 1;
  587. putc ('.');
  588. #else
  589. info->protect[i] = p;
  590. #endif /* CFG_FLASH_PROTECTION */
  591. }
  592. }
  593. }
  594. #if defined(CFG_FLASH_PROTECTION)
  595. puts (" done\n");
  596. #endif /* CFG_FLASH_PROTECTION */
  597. printf ("%sProtected %d sectors\n",
  598. p ? "" : "Un-", protected);
  599. } else if (rcode == 0) {
  600. puts ("Error: start and/or end address"
  601. " not on sector boundary\n");
  602. rcode = 1;
  603. }
  604. return rcode;
  605. }
  606. #endif /* CFG_NO_FLASH */
  607. /**************************************************/
  608. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  609. # define TMP_ERASE "erase <part-id>\n - erase partition\n"
  610. # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
  611. # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
  612. #else
  613. # define TMP_ERASE /* empty */
  614. # define TMP_PROT_ON /* empty */
  615. # define TMP_PROT_OFF /* empty */
  616. #endif
  617. U_BOOT_CMD(
  618. flinfo, 2, 1, do_flinfo,
  619. "flinfo - print FLASH memory information\n",
  620. "\n - print information for all FLASH memory banks\n"
  621. "flinfo N\n - print information for FLASH memory bank # N\n"
  622. );
  623. U_BOOT_CMD(
  624. erase, 3, 0, do_flerase,
  625. "erase - erase FLASH memory\n",
  626. "start end\n"
  627. " - erase FLASH from addr 'start' to addr 'end'\n"
  628. "erase start +len\n"
  629. " - erase FLASH from addr 'start' to the end of sect "
  630. "w/addr 'start'+'len'-1\n"
  631. "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
  632. "erase bank N\n - erase FLASH bank # N\n"
  633. TMP_ERASE
  634. "erase all\n - erase all FLASH banks\n"
  635. );
  636. U_BOOT_CMD(
  637. protect, 4, 0, do_protect,
  638. "protect - enable or disable FLASH write protection\n",
  639. "on start end\n"
  640. " - protect FLASH from addr 'start' to addr 'end'\n"
  641. "protect on start +len\n"
  642. " - protect FLASH from addr 'start' to end of sect "
  643. "w/addr 'start'+'len'-1\n"
  644. "protect on N:SF[-SL]\n"
  645. " - protect sectors SF-SL in FLASH bank # N\n"
  646. "protect on bank N\n - protect FLASH bank # N\n"
  647. TMP_PROT_ON
  648. "protect on all\n - protect all FLASH banks\n"
  649. "protect off start end\n"
  650. " - make FLASH from addr 'start' to addr 'end' writable\n"
  651. "protect off start +len\n"
  652. " - make FLASH from addr 'start' to end of sect "
  653. "w/addr 'start'+'len'-1 wrtable\n"
  654. "protect off N:SF[-SL]\n"
  655. " - make sectors SF-SL writable in FLASH bank # N\n"
  656. "protect off bank N\n - make FLASH bank # N writable\n"
  657. TMP_PROT_OFF
  658. "protect off all\n - make all FLASH banks writable\n"
  659. );
  660. #undef TMP_ERASE
  661. #undef TMP_PROT_ON
  662. #undef TMP_PROT_OFF