1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 #if defined(CONFIG_CMD_MTDPARTS)
16 #include <jffs2/jffs2.h>
18 /* partition handling routines */
19 int mtdparts_init(void);
20 int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
21 int find_dev_and_part(const char *id, struct mtd_device **dev,
22 u8 *part_num, struct part_info **part);
25 #ifdef CONFIG_MTD_NOR_FLASH
27 #include <mtd/cfi_flash.h>
28 extern flash_info_t flash_info[]; /* info for FLASH chips */
31 * The user interface starts numbering for Flash banks with 1
32 * for historical reasons.
36 * this routine looks for an abbreviated flash range specification.
37 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
38 * sector to erase, and SL is the last sector to erase (defaults to SF).
39 * bank numbers start at 1 to be consistent with other specs, sector numbers
42 * returns: 1 - correct spec; *pinfo, *psf and *psl are
44 * 0 - doesn't look like an abbreviated spec
45 * -1 - looks like an abbreviated spec, but got
46 * a parsing error, a number out of range,
47 * or an invalid flash bank.
50 abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
53 int bank, first, last;
56 if ((p = strchr (str, ':')) == NULL)
60 bank = simple_strtoul (str, &ep, 10);
61 if (ep == str || *ep != '\0' ||
62 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
63 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
67 if ((p = strchr (str, '-')) != NULL)
70 first = simple_strtoul (str, &ep, 10);
71 if (ep == str || *ep != '\0' || first >= fp->sector_count)
75 last = simple_strtoul (p, &ep, 10);
76 if (ep == p || *ep != '\0' ||
77 last < first || last >= fp->sector_count)
91 * Take *addr in Flash and adjust it to fall on the end of its sector
93 int flash_sect_roundb(ulong *addr)
96 ulong bank, sector_end_addr;
100 /* find the end addr of the sector where the *addr is */
102 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
103 info = &flash_info[bank];
104 for (i = 0; i < info->sector_count && !found; ++i) {
105 /* get the end address of the sector */
106 if (i == info->sector_count - 1) {
107 sector_end_addr = info->start[0] +
110 sector_end_addr = info->start[i+1] - 1;
113 if (*addr <= sector_end_addr &&
114 *addr >= info->start[i]) {
116 /* adjust *addr if necessary */
117 if (*addr < sector_end_addr)
118 *addr = sector_end_addr;
123 /* error, address not in flash */
124 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
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).
141 * arg1, arg2: address specification (i.e. both command arguments)
143 * addr_first, addr_last: computed address range
146 * -1: failure (bad format, bad address).
149 addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
152 char len_used; /* indicates if the "start +length" form used */
154 *addr_first = simple_strtoul(arg1, &ep, 16);
155 if (ep == arg1 || *ep != '\0')
159 if (arg2 && *arg2 == '+'){
164 *addr_last = simple_strtoul(arg2, &ep, 16);
165 if (ep == arg2 || *ep != '\0')
170 * *addr_last has the length, compute correct *addr_last
171 * XXX watch out for the integer overflow! Right now it is
172 * checked for in both the callers.
174 *addr_last = *addr_first + *addr_last - 1;
177 * It may happen that *addr_last doesn't fall on the sector
178 * boundary. We want to round such an address to the next
179 * sector boundary, so that the commands don't fail later on.
182 if (flash_sect_roundb(addr_last) > 0)
184 } /* "start +length" from used */
190 flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
191 int *s_first, int *s_last,
200 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
201 s_first[bank] = -1; /* first sector to erase */
202 s_last [bank] = -1; /* last sector to erase */
205 for (bank=0,info = &flash_info[0];
206 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
212 if (info->flash_id == FLASH_UNKNOWN) {
216 b_end = info->start[0] + info->size - 1; /* bank end addr */
217 s_end = info->sector_count - 1; /* last sector */
220 for (sect=0; sect < info->sector_count; ++sect) {
221 ulong end; /* last address in current sect */
223 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
225 if (addr_first > end)
227 if (addr_last < info->start[sect])
230 if (addr_first == info->start[sect]) {
231 s_first[bank] = sect;
233 if (addr_last == end) {
237 if (s_first[bank] >= 0) {
238 if (s_last[bank] < 0) {
239 if (addr_last > b_end) {
240 s_last[bank] = s_end;
242 puts ("Error: end address"
243 " not on sector boundary\n");
248 if (s_last[bank] < s_first[bank]) {
249 puts ("Error: end sector"
250 " precedes start sector\n");
255 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
256 (*s_count) += s_last[bank] - s_first[bank] + 1;
257 } else if (addr_first >= info->start[0] && addr_first < b_end) {
258 puts ("Error: start address not on sector boundary\n");
261 } else if (s_last[bank] >= 0) {
262 puts ("Error: cannot span across banks when they are"
263 " mapped in reverse order\n");
271 #endif /* CONFIG_MTD_NOR_FLASH */
273 static int do_flinfo(struct cmd_tbl *cmdtp, int flag, int argc,
276 #ifdef CONFIG_MTD_NOR_FLASH
280 #ifdef CONFIG_MTD_NOR_FLASH
281 if (argc == 1) { /* print info for all FLASH banks */
282 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
283 printf ("\nBank # %ld: ", bank+1);
285 flash_print_info(&flash_info[bank]);
290 bank = simple_strtoul(argv[1], NULL, 16);
291 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
292 printf ("Only FLASH Banks # 1 ... # %d supported\n",
293 CONFIG_SYS_MAX_FLASH_BANKS);
296 printf ("\nBank # %ld: ", bank);
297 flash_print_info(&flash_info[bank - 1]);
298 #endif /* CONFIG_MTD_NOR_FLASH */
302 static int do_flerase(struct cmd_tbl *cmdtp, int flag, int argc,
305 #ifdef CONFIG_MTD_NOR_FLASH
306 flash_info_t *info = NULL;
307 ulong bank, addr_first, addr_last;
308 int n, sect_first = 0, sect_last = 0;
309 #if defined(CONFIG_CMD_MTDPARTS)
310 struct mtd_device *dev;
311 struct part_info *part;
312 u8 dev_type, dev_num, pnum;
317 return CMD_RET_USAGE;
319 if (strcmp(argv[1], "all") == 0) {
320 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
321 printf ("Erase Flash Bank # %ld ", bank);
322 info = &flash_info[bank-1];
323 rcode = flash_erase(info, 0, info->sector_count - 1);
328 if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) {
330 puts ("Bad sector specification\n");
333 printf ("Erase Flash Sectors %d-%d in Bank # %zu ",
334 sect_first, sect_last, (info-flash_info)+1);
335 rcode = flash_erase(info, sect_first, sect_last);
339 #if defined(CONFIG_CMD_MTDPARTS)
340 /* erase <part-id> - erase partition */
341 if ((argc == 2) && (mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
343 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
344 if (dev->id->type == MTD_DEV_TYPE_NOR) {
346 info = &flash_info[bank];
347 addr_first = part->offset + info->start[0];
348 addr_last = addr_first + part->size - 1;
350 printf ("Erase Flash Partition %s, "
351 "bank %ld, 0x%08lx - 0x%08lx ",
352 argv[1], bank, addr_first,
355 rcode = flash_sect_erase(addr_first, addr_last);
359 printf("cannot erase, not a NOR device\n");
366 return CMD_RET_USAGE;
368 if (strcmp(argv[1], "bank") == 0) {
369 bank = simple_strtoul(argv[2], NULL, 16);
370 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
371 printf ("Only FLASH Banks # 1 ... # %d supported\n",
372 CONFIG_SYS_MAX_FLASH_BANKS);
375 printf ("Erase Flash Bank # %ld ", bank);
376 info = &flash_info[bank-1];
377 rcode = flash_erase(info, 0, info->sector_count - 1);
381 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
382 printf ("Bad address format\n");
386 if (addr_first >= addr_last)
387 return CMD_RET_USAGE;
389 rcode = flash_sect_erase(addr_first, addr_last);
393 #endif /* CONFIG_MTD_NOR_FLASH */
396 #ifdef CONFIG_MTD_NOR_FLASH
397 int flash_sect_erase(ulong addr_first, ulong addr_last)
401 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
406 rcode = flash_fill_sect_ranges (addr_first, addr_last,
407 s_first, s_last, &planned );
409 if (planned && (rcode == 0)) {
410 for (bank=0,info = &flash_info[0];
411 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
413 if (s_first[bank]>=0) {
414 erased += s_last[bank] - s_first[bank] + 1;
415 debug("Erase Flash from 0x%08lx to 0x%08lx in Bank # %ld ",
416 info->start[s_first[bank]],
417 (s_last[bank] == info->sector_count) ?
418 info->start[0] + info->size - 1 :
419 info->start[s_last[bank] + 1] - 1,
421 rcode = flash_erase(info, s_first[bank],
426 printf("Erased %d sectors\n", erased);
427 } else if (rcode == 0) {
428 puts ("Error: start and/or end address"
429 " not on sector boundary\n");
434 #endif /* CONFIG_MTD_NOR_FLASH */
436 static int do_protect(struct cmd_tbl *cmdtp, int flag, int argc,
440 #ifdef CONFIG_MTD_NOR_FLASH
441 flash_info_t *info = NULL;
443 int i, n, sect_first = 0, sect_last = 0;
444 #if defined(CONFIG_CMD_MTDPARTS)
445 struct mtd_device *dev;
446 struct part_info *part;
447 u8 dev_type, dev_num, pnum;
449 #endif /* CONFIG_MTD_NOR_FLASH */
450 #if defined(CONFIG_MTD_NOR_FLASH)
452 ulong addr_first, addr_last;
456 return CMD_RET_USAGE;
458 #if defined(CONFIG_MTD_NOR_FLASH)
459 if (strcmp(argv[1], "off") == 0)
461 else if (strcmp(argv[1], "on") == 0)
464 return CMD_RET_USAGE;
467 #ifdef CONFIG_MTD_NOR_FLASH
468 if (strcmp(argv[2], "all") == 0) {
469 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
470 info = &flash_info[bank-1];
471 if (info->flash_id == FLASH_UNKNOWN) {
474 printf ("%sProtect Flash Bank # %ld\n",
475 p ? "" : "Un-", bank);
477 for (i=0; i<info->sector_count; ++i) {
478 #if defined(CONFIG_SYS_FLASH_PROTECTION)
479 if (flash_real_protect(info, i, p))
483 info->protect[i] = p;
484 #endif /* CONFIG_SYS_FLASH_PROTECTION */
486 #if defined(CONFIG_SYS_FLASH_PROTECTION)
487 if (!rcode) puts (" done\n");
488 #endif /* CONFIG_SYS_FLASH_PROTECTION */
493 if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) {
495 puts ("Bad sector specification\n");
498 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
499 p ? "" : "Un-", sect_first, sect_last,
500 (info-flash_info)+1);
501 for (i = sect_first; i <= sect_last; i++) {
502 #if defined(CONFIG_SYS_FLASH_PROTECTION)
503 if (flash_real_protect(info, i, p))
507 info->protect[i] = p;
508 #endif /* CONFIG_SYS_FLASH_PROTECTION */
511 #if defined(CONFIG_SYS_FLASH_PROTECTION)
512 if (!rcode) puts (" done\n");
513 #endif /* CONFIG_SYS_FLASH_PROTECTION */
518 #if defined(CONFIG_CMD_MTDPARTS)
519 /* protect on/off <part-id> */
520 if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
522 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
523 if (dev->id->type == MTD_DEV_TYPE_NOR) {
525 info = &flash_info[bank];
526 addr_first = part->offset + info->start[0];
527 addr_last = addr_first + part->size - 1;
529 printf ("%sProtect Flash Partition %s, "
530 "bank %ld, 0x%08lx - 0x%08lx\n",
531 p ? "" : "Un", argv[1],
532 bank, addr_first, addr_last);
534 rcode = flash_sect_protect(p, addr_first,
539 printf("cannot %sprotect, not a NOR device\n",
547 return CMD_RET_USAGE;
549 if (strcmp(argv[2], "bank") == 0) {
550 bank = simple_strtoul(argv[3], NULL, 16);
551 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
552 printf ("Only FLASH Banks # 1 ... # %d supported\n",
553 CONFIG_SYS_MAX_FLASH_BANKS);
556 printf ("%sProtect Flash Bank # %ld\n",
557 p ? "" : "Un-", bank);
558 info = &flash_info[bank-1];
560 if (info->flash_id == FLASH_UNKNOWN) {
561 puts ("missing or unknown FLASH type\n");
564 for (i=0; i<info->sector_count; ++i) {
565 #if defined(CONFIG_SYS_FLASH_PROTECTION)
566 if (flash_real_protect(info, i, p))
570 info->protect[i] = p;
571 #endif /* CONFIG_SYS_FLASH_PROTECTION */
574 #if defined(CONFIG_SYS_FLASH_PROTECTION)
575 if (!rcode) puts (" done\n");
576 #endif /* CONFIG_SYS_FLASH_PROTECTION */
581 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
582 printf("Bad address format\n");
586 if (addr_first >= addr_last)
587 return CMD_RET_USAGE;
589 rcode = flash_sect_protect(p, addr_first, addr_last);
590 #endif /* CONFIG_MTD_NOR_FLASH */
594 #ifdef CONFIG_MTD_NOR_FLASH
595 int flash_sect_protect(int p, ulong addr_first, ulong addr_last)
599 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
604 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
608 if (planned && (rcode == 0)) {
609 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
610 if (info->flash_id == FLASH_UNKNOWN) {
614 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
615 debug("%sProtecting sectors %d..%d in bank %ld\n",
616 p ? "" : "Un-", s_first[bank],
617 s_last[bank], bank + 1);
618 protected += s_last[bank] - s_first[bank] + 1;
619 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
620 #if defined(CONFIG_SYS_FLASH_PROTECTION)
621 if (flash_real_protect(info, i, p))
625 info->protect[i] = p;
626 #endif /* CONFIG_SYS_FLASH_PROTECTION */
630 #if defined(CONFIG_SYS_FLASH_PROTECTION)
632 #endif /* CONFIG_SYS_FLASH_PROTECTION */
634 printf ("%sProtected %d sectors\n",
635 p ? "" : "Un-", protected);
636 } else if (rcode == 0) {
637 puts ("Error: start and/or end address"
638 " not on sector boundary\n");
643 #endif /* CONFIG_MTD_NOR_FLASH */
646 /**************************************************/
647 #if defined(CONFIG_CMD_MTDPARTS)
648 # define TMP_ERASE "erase <part-id>\n - erase partition\n"
649 # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
650 # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
652 # define TMP_ERASE /* empty */
653 # define TMP_PROT_ON /* empty */
654 # define TMP_PROT_OFF /* empty */
658 flinfo, 2, 1, do_flinfo,
659 "print FLASH memory information",
660 "\n - print information for all FLASH memory banks\n"
661 "flinfo N\n - print information for FLASH memory bank # N"
665 erase, 3, 0, do_flerase,
666 "erase FLASH memory",
668 " - erase FLASH from addr 'start' to addr 'end'\n"
670 " - erase FLASH from addr 'start' to the end of sect "
671 "w/addr 'start'+'len'-1\n"
672 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
673 "erase bank N\n - erase FLASH bank # N\n"
675 "erase all\n - erase all FLASH banks"
679 protect, 4, 0, do_protect,
680 "enable or disable FLASH write protection",
682 " - protect FLASH from addr 'start' to addr 'end'\n"
683 "protect on start +len\n"
684 " - protect FLASH from addr 'start' to end of sect "
685 "w/addr 'start'+'len'-1\n"
686 "protect on N:SF[-SL]\n"
687 " - protect sectors SF-SL in FLASH bank # N\n"
688 "protect on bank N\n - protect FLASH bank # N\n"
690 "protect on all\n - protect all FLASH banks\n"
691 "protect off start end\n"
692 " - make FLASH from addr 'start' to addr 'end' writable\n"
693 "protect off start +len\n"
694 " - make FLASH from addr 'start' to end of sect "
695 "w/addr 'start'+'len'-1 wrtable\n"
696 "protect off N:SF[-SL]\n"
697 " - make sectors SF-SL writable in FLASH bank # N\n"
698 "protect off bank N\n - make FLASH bank # N writable\n"
700 "protect off all\n - make all FLASH banks writable"