cmd_flash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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_FLASH)
  32. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  33. #include <jffs2/jffs2.h>
  34. /* parition handling routines */
  35. int mtdparts_init(void);
  36. int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
  37. int find_dev_and_part(const char *id, struct mtd_device **dev,
  38. u8 *part_num, struct part_info **part);
  39. #endif
  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. int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  246. {
  247. ulong bank;
  248. #ifdef CONFIG_HAS_DATAFLASH
  249. dataflash_print_info();
  250. #endif
  251. if (argc == 1) { /* print info for all FLASH banks */
  252. for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
  253. printf ("\nBank # %ld: ", bank+1);
  254. flash_print_info (&flash_info[bank]);
  255. }
  256. return 0;
  257. }
  258. bank = simple_strtoul(argv[1], NULL, 16);
  259. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  260. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  261. CFG_MAX_FLASH_BANKS);
  262. return 1;
  263. }
  264. printf ("\nBank # %ld: ", bank);
  265. flash_print_info (&flash_info[bank-1]);
  266. return 0;
  267. }
  268. int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  269. {
  270. flash_info_t *info;
  271. ulong bank, addr_first, addr_last;
  272. int n, sect_first, sect_last;
  273. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  274. struct mtd_device *dev;
  275. struct part_info *part;
  276. u8 dev_type, dev_num, pnum;
  277. #endif
  278. int rcode = 0;
  279. if (argc < 2) {
  280. printf ("Usage:\n%s\n", cmdtp->usage);
  281. return 1;
  282. }
  283. if (strcmp(argv[1], "all") == 0) {
  284. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  285. printf ("Erase Flash Bank # %ld ", bank);
  286. info = &flash_info[bank-1];
  287. rcode = flash_erase (info, 0, info->sector_count-1);
  288. }
  289. return rcode;
  290. }
  291. if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
  292. if (n < 0) {
  293. puts ("Bad sector specification\n");
  294. return 1;
  295. }
  296. printf ("Erase Flash Sectors %d-%d in Bank # %d ",
  297. sect_first, sect_last, (info-flash_info)+1);
  298. rcode = flash_erase(info, sect_first, sect_last);
  299. return rcode;
  300. }
  301. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  302. /* erase <part-id> - erase partition */
  303. if ((argc == 2) && (id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
  304. mtdparts_init();
  305. if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
  306. if (dev->id->type == MTD_DEV_TYPE_NOR) {
  307. bank = dev->id->num;
  308. info = &flash_info[bank];
  309. addr_first = part->offset + info->start[0];
  310. addr_last = addr_first + part->size - 1;
  311. printf ("Erase Flash Parition %s, "
  312. "bank %d, 0x%08lx - 0x%08lx ",
  313. argv[1], bank, addr_first,
  314. addr_last);
  315. rcode = flash_sect_erase(addr_first, addr_last);
  316. return rcode;
  317. }
  318. printf("cannot erase, not a NOR device\n");
  319. return 1;
  320. }
  321. }
  322. #endif
  323. if (argc != 3) {
  324. printf ("Usage:\n%s\n", cmdtp->usage);
  325. return 1;
  326. }
  327. if (strcmp(argv[1], "bank") == 0) {
  328. bank = simple_strtoul(argv[2], NULL, 16);
  329. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  330. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  331. CFG_MAX_FLASH_BANKS);
  332. return 1;
  333. }
  334. printf ("Erase Flash Bank # %ld ", bank);
  335. info = &flash_info[bank-1];
  336. rcode = flash_erase (info, 0, info->sector_count-1);
  337. return rcode;
  338. }
  339. if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
  340. printf ("Bad address format\n");
  341. return 1;
  342. }
  343. if (addr_first >= addr_last) {
  344. printf ("Usage:\n%s\n", cmdtp->usage);
  345. return 1;
  346. }
  347. rcode = flash_sect_erase(addr_first, addr_last);
  348. return rcode;
  349. }
  350. int flash_sect_erase (ulong addr_first, ulong addr_last)
  351. {
  352. flash_info_t *info;
  353. ulong bank;
  354. #ifdef CFG_MAX_FLASH_BANKS_DETECT
  355. int s_first[CFG_MAX_FLASH_BANKS_DETECT], s_last[CFG_MAX_FLASH_BANKS_DETECT];
  356. #else
  357. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  358. #endif
  359. int erased = 0;
  360. int planned;
  361. int rcode = 0;
  362. rcode = flash_fill_sect_ranges (addr_first, addr_last,
  363. s_first, s_last, &planned );
  364. if (planned && (rcode == 0)) {
  365. for (bank=0,info=&flash_info[0];
  366. (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
  367. ++bank, ++info) {
  368. if (s_first[bank]>=0) {
  369. erased += s_last[bank] - s_first[bank] + 1;
  370. debug ("Erase Flash from 0x%08lx to 0x%08lx "
  371. "in Bank # %ld ",
  372. info->start[s_first[bank]],
  373. (s_last[bank] == info->sector_count) ?
  374. info->start[0] + info->size - 1:
  375. info->start[s_last[bank]+1] - 1,
  376. bank+1);
  377. rcode = flash_erase (info, s_first[bank], s_last[bank]);
  378. }
  379. }
  380. printf ("Erased %d sectors\n", erased);
  381. } else if (rcode == 0) {
  382. puts ("Error: start and/or end address"
  383. " not on sector boundary\n");
  384. rcode = 1;
  385. }
  386. return rcode;
  387. }
  388. int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  389. {
  390. flash_info_t *info;
  391. ulong bank, addr_first, addr_last;
  392. int i, p, n, sect_first, sect_last;
  393. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  394. struct mtd_device *dev;
  395. struct part_info *part;
  396. u8 dev_type, dev_num, pnum;
  397. #endif
  398. int rcode = 0;
  399. #ifdef CONFIG_HAS_DATAFLASH
  400. int status;
  401. #endif
  402. if (argc < 3) {
  403. printf ("Usage:\n%s\n", cmdtp->usage);
  404. return 1;
  405. }
  406. if (strcmp(argv[1], "off") == 0) {
  407. p = 0;
  408. } else if (strcmp(argv[1], "on") == 0) {
  409. p = 1;
  410. } else {
  411. printf ("Usage:\n%s\n", cmdtp->usage);
  412. return 1;
  413. }
  414. #ifdef CONFIG_HAS_DATAFLASH
  415. if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
  416. addr_first = simple_strtoul(argv[2], NULL, 16);
  417. addr_last = simple_strtoul(argv[3], NULL, 16);
  418. if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
  419. status = dataflash_real_protect(p,addr_first,addr_last);
  420. if (status < 0){
  421. puts ("Bad DataFlash sector specification\n");
  422. return 1;
  423. }
  424. printf("%sProtect %d DataFlash Sectors\n",
  425. p ? "" : "Un-", status);
  426. return 0;
  427. }
  428. }
  429. #endif
  430. if (strcmp(argv[2], "all") == 0) {
  431. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  432. info = &flash_info[bank-1];
  433. if (info->flash_id == FLASH_UNKNOWN) {
  434. continue;
  435. }
  436. printf ("%sProtect Flash Bank # %ld\n",
  437. p ? "" : "Un-", bank);
  438. for (i=0; i<info->sector_count; ++i) {
  439. #if defined(CFG_FLASH_PROTECTION)
  440. if (flash_real_protect(info, i, p))
  441. rcode = 1;
  442. putc ('.');
  443. #else
  444. info->protect[i] = p;
  445. #endif /* CFG_FLASH_PROTECTION */
  446. }
  447. #if defined(CFG_FLASH_PROTECTION)
  448. if (!rcode) puts (" done\n");
  449. #endif /* CFG_FLASH_PROTECTION */
  450. }
  451. return rcode;
  452. }
  453. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  454. if (n < 0) {
  455. puts ("Bad sector specification\n");
  456. return 1;
  457. }
  458. printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
  459. p ? "" : "Un-", sect_first, sect_last,
  460. (info-flash_info)+1);
  461. for (i = sect_first; i <= sect_last; i++) {
  462. #if defined(CFG_FLASH_PROTECTION)
  463. if (flash_real_protect(info, i, p))
  464. rcode = 1;
  465. putc ('.');
  466. #else
  467. info->protect[i] = p;
  468. #endif /* CFG_FLASH_PROTECTION */
  469. }
  470. #if defined(CFG_FLASH_PROTECTION)
  471. if (!rcode) puts (" done\n");
  472. #endif /* CFG_FLASH_PROTECTION */
  473. return rcode;
  474. }
  475. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  476. /* protect on/off <part-id> */
  477. if ((argc == 3) && (id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
  478. mtdparts_init();
  479. if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
  480. if (dev->id->type == MTD_DEV_TYPE_NOR) {
  481. bank = dev->id->num;
  482. info = &flash_info[bank];
  483. addr_first = part->offset + info->start[0];
  484. addr_last = addr_first + part->size - 1;
  485. printf ("%sProtect Flash Parition %s, "
  486. "bank %d, 0x%08lx - 0x%08lx\n",
  487. p ? "" : "Un", argv[1],
  488. bank, addr_first, addr_last);
  489. rcode = flash_sect_protect (p, addr_first, addr_last);
  490. return rcode;
  491. }
  492. printf("cannot %sprotect, not a NOR device\n",
  493. p ? "" : "un");
  494. return 1;
  495. }
  496. }
  497. #endif
  498. if (argc != 4) {
  499. printf ("Usage:\n%s\n", cmdtp->usage);
  500. return 1;
  501. }
  502. if (strcmp(argv[2], "bank") == 0) {
  503. bank = simple_strtoul(argv[3], NULL, 16);
  504. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  505. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  506. CFG_MAX_FLASH_BANKS);
  507. return 1;
  508. }
  509. printf ("%sProtect Flash Bank # %ld\n",
  510. p ? "" : "Un-", bank);
  511. info = &flash_info[bank-1];
  512. if (info->flash_id == FLASH_UNKNOWN) {
  513. puts ("missing or unknown FLASH type\n");
  514. return 1;
  515. }
  516. for (i=0; i<info->sector_count; ++i) {
  517. #if defined(CFG_FLASH_PROTECTION)
  518. if (flash_real_protect(info, i, p))
  519. rcode = 1;
  520. putc ('.');
  521. #else
  522. info->protect[i] = p;
  523. #endif /* CFG_FLASH_PROTECTION */
  524. }
  525. #if defined(CFG_FLASH_PROTECTION)
  526. if (!rcode) puts (" done\n");
  527. #endif /* CFG_FLASH_PROTECTION */
  528. return rcode;
  529. }
  530. if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
  531. printf("Bad address format\n");
  532. return 1;
  533. }
  534. if (addr_first >= addr_last) {
  535. printf ("Usage:\n%s\n", cmdtp->usage);
  536. return 1;
  537. }
  538. rcode = flash_sect_protect (p, addr_first, addr_last);
  539. return rcode;
  540. }
  541. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  542. {
  543. flash_info_t *info;
  544. ulong bank;
  545. #ifdef CFG_MAX_FLASH_BANKS_DETECT
  546. int s_first[CFG_MAX_FLASH_BANKS_DETECT], s_last[CFG_MAX_FLASH_BANKS_DETECT];
  547. #else
  548. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  549. #endif
  550. int protected, i;
  551. int planned;
  552. int rcode;
  553. rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
  554. protected = 0;
  555. if (planned && (rcode == 0)) {
  556. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  557. if (info->flash_id == FLASH_UNKNOWN) {
  558. continue;
  559. }
  560. if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
  561. debug ("%sProtecting sectors %d..%d in bank %ld\n",
  562. p ? "" : "Un-",
  563. s_first[bank], s_last[bank], bank+1);
  564. protected += s_last[bank] - s_first[bank] + 1;
  565. for (i=s_first[bank]; i<=s_last[bank]; ++i) {
  566. #if defined(CFG_FLASH_PROTECTION)
  567. if (flash_real_protect(info, i, p))
  568. rcode = 1;
  569. putc ('.');
  570. #else
  571. info->protect[i] = p;
  572. #endif /* CFG_FLASH_PROTECTION */
  573. }
  574. }
  575. }
  576. #if defined(CFG_FLASH_PROTECTION)
  577. puts (" done\n");
  578. #endif /* CFG_FLASH_PROTECTION */
  579. printf ("%sProtected %d sectors\n",
  580. p ? "" : "Un-", protected);
  581. } else if (rcode == 0) {
  582. puts ("Error: start and/or end address"
  583. " not on sector boundary\n");
  584. rcode = 1;
  585. }
  586. return rcode;
  587. }
  588. /**************************************************/
  589. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  590. # define TMP_ERASE "erase <part-id>\n - erase partition\n"
  591. # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
  592. # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
  593. #else
  594. # define TMP_ERASE /* empty */
  595. # define TMP_PROT_ON /* empty */
  596. # define TMP_PROT_OFF /* empty */
  597. #endif
  598. U_BOOT_CMD(
  599. flinfo, 2, 1, do_flinfo,
  600. "flinfo - print FLASH memory information\n",
  601. "\n - print information for all FLASH memory banks\n"
  602. "flinfo N\n - print information for FLASH memory bank # N\n"
  603. );
  604. U_BOOT_CMD(
  605. erase, 3, 0, do_flerase,
  606. "erase - erase FLASH memory\n",
  607. "start end\n"
  608. " - erase FLASH from addr 'start' to addr 'end'\n"
  609. "erase start +len\n"
  610. " - erase FLASH from addr 'start' to the end of sect "
  611. "w/addr 'start'+'len'-1\n"
  612. "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
  613. "erase bank N\n - erase FLASH bank # N\n"
  614. TMP_ERASE
  615. "erase all\n - erase all FLASH banks\n"
  616. );
  617. U_BOOT_CMD(
  618. protect, 4, 0, do_protect,
  619. "protect - enable or disable FLASH write protection\n",
  620. "on start end\n"
  621. " - protect FLASH from addr 'start' to addr 'end'\n"
  622. "protect on start +len\n"
  623. " - protect FLASH from addr 'start' to end of sect "
  624. "w/addr 'start'+'len'-1\n"
  625. "protect on N:SF[-SL]\n"
  626. " - protect sectors SF-SL in FLASH bank # N\n"
  627. "protect on bank N\n - protect FLASH bank # N\n"
  628. TMP_PROT_ON
  629. "protect on all\n - protect all FLASH banks\n"
  630. "protect off start end\n"
  631. " - make FLASH from addr 'start' to addr 'end' writable\n"
  632. "protect off start +len\n"
  633. " - make FLASH from addr 'start' to end of sect "
  634. "w/addr 'start'+'len'-1 wrtable\n"
  635. "protect off N:SF[-SL]\n"
  636. " - make sectors SF-SL writable in FLASH bank # N\n"
  637. "protect off bank N\n - make FLASH bank # N writable\n"
  638. TMP_PROT_OFF
  639. "protect off all\n - make all FLASH banks writable\n"
  640. );
  641. #undef TMP_ERASE
  642. #undef TMP_PROT_ON
  643. #undef TMP_PROT_OFF
  644. #endif