cmd_flash.c 19 KB

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