cmd_flash.c 19 KB

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