flash.c 18 KB

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