cmd_flash.c 20 KB

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