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;
61 bank = dectoul(str, &ep);
62 if (ep == str || *ep != '\0' ||
63 bank < 1 || bank > CFI_FLASH_BANKS)
66 fp = &flash_info[bank - 1];
67 if (fp->flash_id == FLASH_UNKNOWN)
75 first = dectoul(str, &ep);
76 if (ep == str || *ep != '\0' || first >= fp->sector_count)
80 last = dectoul(p, &ep);
81 if (ep == p || *ep != '\0' ||
82 last < first || last >= fp->sector_count)
96 * Take *addr in Flash and adjust it to fall on the end of its sector
98 int flash_sect_roundb(ulong *addr)
101 ulong bank, sector_end_addr;
105 /* find the end addr of the sector where the *addr is */
107 for (bank = 0; bank < CFI_FLASH_BANKS && !found; ++bank) {
108 info = &flash_info[bank];
109 for (i = 0; i < info->sector_count && !found; ++i) {
110 /* get the end address of the sector */
111 if (i == info->sector_count - 1) {
112 sector_end_addr = info->start[0] +
115 sector_end_addr = info->start[i + 1] - 1;
118 if (*addr <= sector_end_addr && *addr >= info->start[i]) {
120 /* adjust *addr if necessary */
121 if (*addr < sector_end_addr)
122 *addr = sector_end_addr;
127 /* error, address not in flash */
128 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
136 * This function computes the start and end addresses for both
137 * erase and protect commands. The range of the addresses on which
138 * either of the commands is to operate can be given in two forms:
139 * 1. <cmd> start end - operate on <'start', 'end')
140 * 2. <cmd> start +length - operate on <'start', start + length)
141 * If the second form is used and the end address doesn't fall on the
142 * sector boundary, than it will be adjusted to the next sector boundary.
143 * If it isn't in the flash, the function will fail (return -1).
145 * arg1, arg2: address specification (i.e. both command arguments)
147 * addr_first, addr_last: computed address range
150 * -1: failure (bad format, bad address).
153 addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
156 char len_used; /* indicates if the "start +length" form used */
158 *addr_first = hextoul(arg1, &ep);
159 if (ep == arg1 || *ep != '\0')
163 if (arg2 && *arg2 == '+') {
168 *addr_last = hextoul(arg2, &ep);
169 if (ep == arg2 || *ep != '\0')
174 * *addr_last has the length, compute correct *addr_last
175 * XXX watch out for the integer overflow! Right now it is
176 * checked for in both the callers.
178 *addr_last = *addr_first + *addr_last - 1;
181 * It may happen that *addr_last doesn't fall on the sector
182 * boundary. We want to round such an address to the next
183 * sector boundary, so that the commands don't fail later on.
186 if (flash_sect_roundb(addr_last) > 0)
188 } /* "start +length" from used */
194 flash_fill_sect_ranges(ulong addr_first, ulong addr_last,
195 int *s_first, int *s_last,
204 for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) {
205 s_first[bank] = -1; /* first sector to erase */
206 s_last[bank] = -1; /* last sector to erase */
209 for (bank = 0, info = &flash_info[0];
210 (bank < CFI_FLASH_BANKS) && (addr_first <= addr_last);
216 if (info->flash_id == FLASH_UNKNOWN)
219 b_end = info->start[0] + info->size - 1; /* bank end addr */
220 s_end = info->sector_count - 1; /* last sector */
222 for (sect = 0; sect < info->sector_count; ++sect) {
223 ulong end; /* last address in current sect */
225 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
227 if (addr_first > end)
229 if (addr_last < info->start[sect])
232 if (addr_first == info->start[sect])
233 s_first[bank] = sect;
235 if (addr_last == end)
238 if (s_first[bank] >= 0) {
239 if (s_last[bank] < 0) {
240 if (addr_last > b_end) {
241 s_last[bank] = s_end;
243 puts("Error: end address not on sector boundary\n");
248 if (s_last[bank] < s_first[bank]) {
249 puts("Error: end sector precedes start sector\n");
254 addr_first = (sect == s_end) ? b_end + 1 : info->start[sect + 1];
255 (*s_count) += s_last[bank] - s_first[bank] + 1;
256 } else if (addr_first >= info->start[0] && addr_first < b_end) {
257 puts("Error: start address not on sector boundary\n");
260 } else if (s_last[bank] >= 0) {
261 puts("Error: cannot span across banks when they are"
262 " mapped in reverse order\n");
270 #endif /* CONFIG_MTD_NOR_FLASH */
272 static int do_flinfo(struct cmd_tbl *cmdtp, int flag, int argc,
275 #ifdef CONFIG_MTD_NOR_FLASH
279 #ifdef CONFIG_MTD_NOR_FLASH
280 if (argc == 1) { /* print info for all FLASH banks */
281 for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) {
282 printf("\nBank # %ld: ", bank + 1);
284 flash_print_info(&flash_info[bank]);
289 bank = hextoul(argv[1], NULL);
290 if (bank < 1 || bank > CFI_FLASH_BANKS) {
291 printf("Only FLASH Banks # 1 ... # %d supported\n",
295 printf("\nBank # %ld: ", bank);
296 flash_print_info(&flash_info[bank - 1]);
297 #endif /* CONFIG_MTD_NOR_FLASH */
301 static int do_flerase(struct cmd_tbl *cmdtp, int flag, int argc,
304 #ifdef CONFIG_MTD_NOR_FLASH
305 flash_info_t *info = NULL;
306 ulong bank, addr_first, addr_last;
307 int n, sect_first = 0, sect_last = 0;
308 #if defined(CONFIG_CMD_MTDPARTS)
309 struct mtd_device *dev;
310 struct part_info *part;
311 u8 dev_type, dev_num, pnum;
316 return CMD_RET_USAGE;
318 if (strcmp(argv[1], "all") == 0) {
319 for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) {
320 printf("Erase Flash Bank # %ld ", bank);
321 info = &flash_info[bank - 1];
322 rcode = flash_erase(info, 0, info->sector_count - 1);
327 n = abbrev_spec(argv[1], &info, §_first, §_last);
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, bank %ld, 0x%08lx - 0x%08lx ",
351 argv[1], bank, addr_first,
354 rcode = flash_sect_erase(addr_first, addr_last);
358 printf("cannot erase, not a NOR device\n");
365 return CMD_RET_USAGE;
367 if (strcmp(argv[1], "bank") == 0) {
368 bank = hextoul(argv[2], NULL);
369 if (bank < 1 || bank > CFI_FLASH_BANKS) {
370 printf("Only FLASH Banks # 1 ... # %d supported\n",
374 printf("Erase Flash Bank # %ld ", bank);
375 info = &flash_info[bank - 1];
376 rcode = flash_erase(info, 0, info->sector_count - 1);
380 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0) {
381 printf("Bad address format\n");
385 if (addr_first >= addr_last)
386 return CMD_RET_USAGE;
388 rcode = flash_sect_erase(addr_first, addr_last);
392 #endif /* CONFIG_MTD_NOR_FLASH */
395 #ifdef CONFIG_MTD_NOR_FLASH
396 int flash_sect_erase(ulong addr_first, ulong addr_last)
400 int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS];
405 rcode = flash_fill_sect_ranges(addr_first, addr_last, s_first, s_last, &planned);
407 if (planned && rcode == 0) {
408 for (bank = 0, info = &flash_info[0];
409 bank < CFI_FLASH_BANKS && rcode == 0;
411 if (s_first[bank] >= 0) {
412 erased += s_last[bank] - s_first[bank] + 1;
413 debug("Erase Flash from 0x%08lx to 0x%08lx in Bank # %ld ",
414 info->start[s_first[bank]],
415 (s_last[bank] == info->sector_count) ?
416 info->start[0] + info->size - 1 :
417 info->start[s_last[bank] + 1] - 1,
419 rcode = flash_erase(info, s_first[bank],
424 printf("Erased %d sectors\n", erased);
425 } else if (rcode == 0) {
426 puts("Error: start and/or end address not on sector boundary\n");
431 #endif /* CONFIG_MTD_NOR_FLASH */
433 static int do_protect(struct cmd_tbl *cmdtp, int flag, int argc,
437 #ifdef CONFIG_MTD_NOR_FLASH
438 flash_info_t *info = NULL;
440 int i, n, sect_first = 0, sect_last = 0;
441 #if defined(CONFIG_CMD_MTDPARTS)
442 struct mtd_device *dev;
443 struct part_info *part;
444 u8 dev_type, dev_num, pnum;
446 #endif /* CONFIG_MTD_NOR_FLASH */
447 #if defined(CONFIG_MTD_NOR_FLASH)
449 ulong addr_first, addr_last;
453 return CMD_RET_USAGE;
455 #if defined(CONFIG_MTD_NOR_FLASH)
456 if (strcmp(argv[1], "off") == 0)
458 else if (strcmp(argv[1], "on") == 0)
461 return CMD_RET_USAGE;
464 #ifdef CONFIG_MTD_NOR_FLASH
465 if (strcmp(argv[2], "all") == 0) {
466 for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) {
467 info = &flash_info[bank - 1];
468 if (info->flash_id == FLASH_UNKNOWN)
471 printf("%sProtect Flash Bank # %ld\n",
472 p ? "" : "Un-", bank);
474 for (i = 0; i < info->sector_count; ++i) {
475 #if defined(CONFIG_SYS_FLASH_PROTECTION)
476 if (flash_real_protect(info, i, p))
480 info->protect[i] = p;
481 #endif /* CONFIG_SYS_FLASH_PROTECTION */
483 #if defined(CONFIG_SYS_FLASH_PROTECTION)
486 #endif /* CONFIG_SYS_FLASH_PROTECTION */
490 n = abbrev_spec(argv[2], &info, §_first, §_last);
493 puts("Bad sector specification\n");
496 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
497 p ? "" : "Un-", sect_first, sect_last,
498 (info - flash_info) + 1);
499 for (i = sect_first; i <= sect_last; i++) {
500 #if defined(CONFIG_SYS_FLASH_PROTECTION)
501 if (flash_real_protect(info, i, p))
505 info->protect[i] = p;
506 #endif /* CONFIG_SYS_FLASH_PROTECTION */
509 #if defined(CONFIG_SYS_FLASH_PROTECTION)
512 #endif /* CONFIG_SYS_FLASH_PROTECTION */
517 #if defined(CONFIG_CMD_MTDPARTS)
518 /* protect on/off <part-id> */
519 if (argc == 3 && mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0) {
521 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
522 if (dev->id->type == MTD_DEV_TYPE_NOR) {
524 info = &flash_info[bank];
525 addr_first = part->offset + info->start[0];
526 addr_last = addr_first + part->size - 1;
528 printf("%sProtect Flash Partition %s, "
529 "bank %ld, 0x%08lx - 0x%08lx\n",
530 p ? "" : "Un", argv[1],
531 bank, addr_first, addr_last);
533 rcode = flash_sect_protect(p, addr_first,
538 printf("cannot %sprotect, not a NOR device\n",
546 return CMD_RET_USAGE;
548 if (strcmp(argv[2], "bank") == 0) {
549 bank = hextoul(argv[3], NULL);
550 if (bank < 1 || bank > CFI_FLASH_BANKS) {
551 printf("Only FLASH Banks # 1 ... # %d supported\n",
555 printf("%sProtect Flash Bank # %ld\n",
556 p ? "" : "Un-", bank);
557 info = &flash_info[bank - 1];
559 if (info->flash_id == FLASH_UNKNOWN) {
560 puts("missing or unknown FLASH type\n");
563 for (i = 0; i < info->sector_count; ++i) {
564 #if defined(CONFIG_SYS_FLASH_PROTECTION)
565 if (flash_real_protect(info, i, p))
569 info->protect[i] = p;
570 #endif /* CONFIG_SYS_FLASH_PROTECTION */
573 #if defined(CONFIG_SYS_FLASH_PROTECTION)
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[CFI_FLASH_BANKS], s_last[CFI_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];
610 bank < CFI_FLASH_BANKS; ++bank, ++info) {
611 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 not on sector boundary\n");
642 #endif /* CONFIG_MTD_NOR_FLASH */
644 /**************************************************/
645 #if defined(CONFIG_CMD_MTDPARTS)
646 # define TMP_ERASE "erase <part-id>\n - erase partition\n"
647 # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
648 # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
650 # define TMP_ERASE /* empty */
651 # define TMP_PROT_ON /* empty */
652 # define TMP_PROT_OFF /* empty */
656 flinfo, 2, 1, do_flinfo,
657 "print FLASH memory information",
658 "\n - print information for all FLASH memory banks\n"
659 "flinfo N\n - print information for FLASH memory bank # N"
663 erase, 3, 0, do_flerase,
664 "erase FLASH memory",
666 " - erase FLASH from addr 'start' to addr 'end'\n"
668 " - erase FLASH from addr 'start' to the end of sect w/addr 'start'+'len'-1\n"
669 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
670 "erase bank N\n - erase FLASH bank # N\n"
672 "erase all\n - erase all FLASH banks"
676 protect, 4, 0, do_protect,
677 "enable or disable FLASH write protection",
679 " - protect FLASH from addr 'start' to addr 'end'\n"
680 "protect on start +len\n"
681 " - protect FLASH from addr 'start' to end of sect w/addr 'start'+'len'-1\n"
682 "protect on N:SF[-SL]\n"
683 " - protect sectors SF-SL in FLASH bank # N\n"
684 "protect on bank N\n - protect FLASH bank # N\n"
686 "protect on all\n - protect all FLASH banks\n"
687 "protect off start end\n"
688 " - make FLASH from addr 'start' to addr 'end' writable\n"
689 "protect off start +len\n"
690 " - make FLASH from addr 'start' to end of sect w/addr 'start'+'len'-1 wrtable\n"
691 "protect off N:SF[-SL]\n"
692 " - make sectors SF-SL writable in FLASH bank # N\n"
693 "protect off bank N\n - make FLASH bank # N writable\n"
695 "protect off all\n - make all FLASH banks writable"