cmd_flash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. static 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. static 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 = NULL;
  284. ulong bank, addr_first, addr_last;
  285. int n, sect_first = 0, sect_last = 0;
  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_RET_USAGE;
  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_RET_USAGE;
  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_RET_USAGE;
  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. if (rcode == 0)
  388. printf("Erased %d sectors\n", erased);
  389. } else if (rcode == 0) {
  390. puts ("Error: start and/or end address"
  391. " not on sector boundary\n");
  392. rcode = 1;
  393. }
  394. return rcode;
  395. }
  396. #endif /* CONFIG_SYS_NO_FLASH */
  397. static int do_protect(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  398. {
  399. int rcode = 0;
  400. #ifndef CONFIG_SYS_NO_FLASH
  401. flash_info_t *info = NULL;
  402. ulong bank;
  403. int i, n, sect_first = 0, sect_last = 0;
  404. #if defined(CONFIG_CMD_MTDPARTS)
  405. struct mtd_device *dev;
  406. struct part_info *part;
  407. u8 dev_type, dev_num, pnum;
  408. #endif
  409. #endif /* CONFIG_SYS_NO_FLASH */
  410. #ifdef CONFIG_HAS_DATAFLASH
  411. int status;
  412. #endif
  413. #if !defined(CONFIG_SYS_NO_FLASH) || defined(CONFIG_HAS_DATAFLASH)
  414. int p;
  415. ulong addr_first, addr_last;
  416. #endif
  417. if (argc < 3)
  418. return CMD_RET_USAGE;
  419. #if !defined(CONFIG_SYS_NO_FLASH) || defined(CONFIG_HAS_DATAFLASH)
  420. if (strcmp(argv[1], "off") == 0)
  421. p = 0;
  422. else if (strcmp(argv[1], "on") == 0)
  423. p = 1;
  424. else
  425. return CMD_RET_USAGE;
  426. #endif
  427. #ifdef CONFIG_HAS_DATAFLASH
  428. if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
  429. addr_first = simple_strtoul(argv[2], NULL, 16);
  430. addr_last = simple_strtoul(argv[3], NULL, 16);
  431. if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
  432. status = dataflash_real_protect(p,addr_first,addr_last);
  433. if (status < 0){
  434. puts ("Bad DataFlash sector specification\n");
  435. return 1;
  436. }
  437. printf("%sProtect %d DataFlash Sectors\n",
  438. p ? "" : "Un-", status);
  439. return 0;
  440. }
  441. }
  442. #endif
  443. #ifndef CONFIG_SYS_NO_FLASH
  444. if (strcmp(argv[2], "all") == 0) {
  445. for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
  446. info = &flash_info[bank-1];
  447. if (info->flash_id == FLASH_UNKNOWN) {
  448. continue;
  449. }
  450. printf ("%sProtect Flash Bank # %ld\n",
  451. p ? "" : "Un-", bank);
  452. for (i=0; i<info->sector_count; ++i) {
  453. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  454. if (flash_real_protect(info, i, p))
  455. rcode = 1;
  456. putc ('.');
  457. #else
  458. info->protect[i] = p;
  459. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  460. }
  461. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  462. if (!rcode) puts (" done\n");
  463. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  464. }
  465. return rcode;
  466. }
  467. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  468. if (n < 0) {
  469. puts ("Bad sector specification\n");
  470. return 1;
  471. }
  472. printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
  473. p ? "" : "Un-", sect_first, sect_last,
  474. (info-flash_info)+1);
  475. for (i = sect_first; i <= sect_last; i++) {
  476. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  477. if (flash_real_protect(info, i, p))
  478. rcode = 1;
  479. putc ('.');
  480. #else
  481. info->protect[i] = p;
  482. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  483. }
  484. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  485. if (!rcode) puts (" done\n");
  486. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  487. return rcode;
  488. }
  489. #if defined(CONFIG_CMD_MTDPARTS)
  490. /* protect on/off <part-id> */
  491. if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
  492. mtdparts_init();
  493. if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
  494. if (dev->id->type == MTD_DEV_TYPE_NOR) {
  495. bank = dev->id->num;
  496. info = &flash_info[bank];
  497. addr_first = part->offset + info->start[0];
  498. addr_last = addr_first + part->size - 1;
  499. printf ("%sProtect Flash Partition %s, "
  500. "bank %ld, 0x%08lx - 0x%08lx\n",
  501. p ? "" : "Un", argv[1],
  502. bank, addr_first, addr_last);
  503. rcode = flash_sect_protect (p, addr_first, addr_last);
  504. return rcode;
  505. }
  506. printf("cannot %sprotect, not a NOR device\n",
  507. p ? "" : "un");
  508. return 1;
  509. }
  510. }
  511. #endif
  512. if (argc != 4)
  513. return CMD_RET_USAGE;
  514. if (strcmp(argv[2], "bank") == 0) {
  515. bank = simple_strtoul(argv[3], NULL, 16);
  516. if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
  517. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  518. CONFIG_SYS_MAX_FLASH_BANKS);
  519. return 1;
  520. }
  521. printf ("%sProtect Flash Bank # %ld\n",
  522. p ? "" : "Un-", bank);
  523. info = &flash_info[bank-1];
  524. if (info->flash_id == FLASH_UNKNOWN) {
  525. puts ("missing or unknown FLASH type\n");
  526. return 1;
  527. }
  528. for (i=0; i<info->sector_count; ++i) {
  529. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  530. if (flash_real_protect(info, i, p))
  531. rcode = 1;
  532. putc ('.');
  533. #else
  534. info->protect[i] = p;
  535. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  536. }
  537. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  538. if (!rcode) puts (" done\n");
  539. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  540. return rcode;
  541. }
  542. if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
  543. printf("Bad address format\n");
  544. return 1;
  545. }
  546. if (addr_first >= addr_last)
  547. return CMD_RET_USAGE;
  548. rcode = flash_sect_protect (p, addr_first, addr_last);
  549. #endif /* CONFIG_SYS_NO_FLASH */
  550. return rcode;
  551. }
  552. #ifndef CONFIG_SYS_NO_FLASH
  553. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  554. {
  555. flash_info_t *info;
  556. ulong bank;
  557. int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
  558. int protected, i;
  559. int planned;
  560. int rcode;
  561. rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
  562. protected = 0;
  563. if (planned && (rcode == 0)) {
  564. for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
  565. if (info->flash_id == FLASH_UNKNOWN) {
  566. continue;
  567. }
  568. if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
  569. debug ("%sProtecting sectors %d..%d in bank %ld\n",
  570. p ? "" : "Un-",
  571. s_first[bank], s_last[bank], bank+1);
  572. protected += s_last[bank] - s_first[bank] + 1;
  573. for (i=s_first[bank]; i<=s_last[bank]; ++i) {
  574. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  575. if (flash_real_protect(info, i, p))
  576. rcode = 1;
  577. putc ('.');
  578. #else
  579. info->protect[i] = p;
  580. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  581. }
  582. }
  583. }
  584. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  585. puts (" done\n");
  586. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  587. printf ("%sProtected %d sectors\n",
  588. p ? "" : "Un-", protected);
  589. } else if (rcode == 0) {
  590. puts ("Error: start and/or end address"
  591. " not on sector boundary\n");
  592. rcode = 1;
  593. }
  594. return rcode;
  595. }
  596. #endif /* CONFIG_SYS_NO_FLASH */
  597. /**************************************************/
  598. #if defined(CONFIG_CMD_MTDPARTS)
  599. # define TMP_ERASE "erase <part-id>\n - erase partition\n"
  600. # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
  601. # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
  602. #else
  603. # define TMP_ERASE /* empty */
  604. # define TMP_PROT_ON /* empty */
  605. # define TMP_PROT_OFF /* empty */
  606. #endif
  607. U_BOOT_CMD(
  608. flinfo, 2, 1, do_flinfo,
  609. "print FLASH memory information",
  610. "\n - print information for all FLASH memory banks\n"
  611. "flinfo N\n - print information for FLASH memory bank # N"
  612. );
  613. U_BOOT_CMD(
  614. erase, 3, 0, do_flerase,
  615. "erase FLASH memory",
  616. "start end\n"
  617. " - erase FLASH from addr 'start' to addr 'end'\n"
  618. "erase start +len\n"
  619. " - erase FLASH from addr 'start' to the end of sect "
  620. "w/addr 'start'+'len'-1\n"
  621. "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
  622. "erase bank N\n - erase FLASH bank # N\n"
  623. TMP_ERASE
  624. "erase all\n - erase all FLASH banks"
  625. );
  626. U_BOOT_CMD(
  627. protect, 4, 0, do_protect,
  628. "enable or disable FLASH write protection",
  629. "on start end\n"
  630. " - protect FLASH from addr 'start' to addr 'end'\n"
  631. "protect on start +len\n"
  632. " - protect FLASH from addr 'start' to end of sect "
  633. "w/addr 'start'+'len'-1\n"
  634. "protect on N:SF[-SL]\n"
  635. " - protect sectors SF-SL in FLASH bank # N\n"
  636. "protect on bank N\n - protect FLASH bank # N\n"
  637. TMP_PROT_ON
  638. "protect on all\n - protect all FLASH banks\n"
  639. "protect off start end\n"
  640. " - make FLASH from addr 'start' to addr 'end' writable\n"
  641. "protect off start +len\n"
  642. " - make FLASH from addr 'start' to end of sect "
  643. "w/addr 'start'+'len'-1 wrtable\n"
  644. "protect off N:SF[-SL]\n"
  645. " - make sectors SF-SL writable in FLASH bank # N\n"
  646. "protect off bank N\n - make FLASH bank # N writable\n"
  647. TMP_PROT_OFF
  648. "protect off all\n - make all FLASH banks writable"
  649. );
  650. #undef TMP_ERASE
  651. #undef TMP_PROT_ON
  652. #undef TMP_PROT_OFF