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>
30 * The user interface starts numbering for Flash banks with 1
31 * for historical reasons.
35 * this routine looks for an abbreviated flash range specification.
36 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
37 * sector to erase, and SL is the last sector to erase (defaults to SF).
38 * bank numbers start at 1 to be consistent with other specs, sector numbers
41 * returns: 1 - correct spec; *pinfo, *psf and *psl are
43 * 0 - doesn't look like an abbreviated spec
44 * -1 - looks like an abbreviated spec, but got
45 * a parsing error, a number out of range,
46 * or an invalid flash bank.
49 abbrev_spec(char *str, flash_info_t **pinfo, int *psf, int *psl)
52 int bank, first, last;
60 bank = dectoul(str, &ep);
61 if (ep == str || *ep != '\0' ||
62 bank < 1 || bank > CFI_FLASH_BANKS)
65 fp = &flash_info[bank - 1];
66 if (fp->flash_id == FLASH_UNKNOWN)
74 first = dectoul(str, &ep);
75 if (ep == str || *ep != '\0' || first >= fp->sector_count)
79 last = dectoul(p, &ep);
80 if (ep == p || *ep != '\0' ||
81 last < first || last >= fp->sector_count)
95 * Take *addr in Flash and adjust it to fall on the end of its sector
97 int flash_sect_roundb(ulong *addr)
100 ulong bank, sector_end_addr;
104 /* find the end addr of the sector where the *addr is */
106 for (bank = 0; bank < CFI_FLASH_BANKS && !found; ++bank) {
107 info = &flash_info[bank];
108 for (i = 0; i < info->sector_count && !found; ++i) {
109 /* get the end address of the sector */
110 if (i == info->sector_count - 1) {
111 sector_end_addr = info->start[0] +
114 sector_end_addr = info->start[i + 1] - 1;
117 if (*addr <= sector_end_addr && *addr >= info->start[i]) {
119 /* adjust *addr if necessary */
120 if (*addr < sector_end_addr)
121 *addr = sector_end_addr;
126 /* error, address not in flash */
127 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
135 * This function computes the start and end addresses for both
136 * erase and protect commands. The range of the addresses on which
137 * either of the commands is to operate can be given in two forms:
138 * 1. <cmd> start end - operate on <'start', 'end')
139 * 2. <cmd> start +length - operate on <'start', start + length)
140 * If the second form is used and the end address doesn't fall on the
141 * sector boundary, than it will be adjusted to the next sector boundary.
142 * If it isn't in the flash, the function will fail (return -1).
144 * arg1, arg2: address specification (i.e. both command arguments)
146 * addr_first, addr_last: computed address range
149 * -1: failure (bad format, bad address).
152 addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
155 char len_used; /* indicates if the "start +length" form used */
157 *addr_first = hextoul(arg1, &ep);
158 if (ep == arg1 || *ep != '\0')
162 if (arg2 && *arg2 == '+') {
167 *addr_last = hextoul(arg2, &ep);
168 if (ep == arg2 || *ep != '\0')
173 * *addr_last has the length, compute correct *addr_last
174 * XXX watch out for the integer overflow! Right now it is
175 * checked for in both the callers.
177 *addr_last = *addr_first + *addr_last - 1;
180 * It may happen that *addr_last doesn't fall on the sector
181 * boundary. We want to round such an address to the next
182 * sector boundary, so that the commands don't fail later on.
185 if (flash_sect_roundb(addr_last) > 0)
187 } /* "start +length" from used */
193 flash_fill_sect_ranges(ulong addr_first, ulong addr_last,
194 int *s_first, int *s_last,
203 for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) {
204 s_first[bank] = -1; /* first sector to erase */
205 s_last[bank] = -1; /* last sector to erase */
208 for (bank = 0, info = &flash_info[0];
209 (bank < CFI_FLASH_BANKS) && (addr_first <= addr_last);
215 if (info->flash_id == FLASH_UNKNOWN)
218 b_end = info->start[0] + info->size - 1; /* bank end addr */
219 s_end = info->sector_count - 1; /* last sector */
221 for (sect = 0; sect < info->sector_count; ++sect) {
222 ulong end; /* last address in current sect */
224 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
226 if (addr_first > end)
228 if (addr_last < info->start[sect])
231 if (addr_first == info->start[sect])
232 s_first[bank] = sect;
234 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 not on sector boundary\n");
247 if (s_last[bank] < s_first[bank]) {
248 puts("Error: end sector precedes start sector\n");
253 addr_first = (sect == s_end) ? b_end + 1 : info->start[sect + 1];
254 (*s_count) += s_last[bank] - s_first[bank] + 1;
255 } else if (addr_first >= info->start[0] && addr_first < b_end) {
256 puts("Error: start address not on sector boundary\n");
259 } else if (s_last[bank] >= 0) {
260 puts("Error: cannot span across banks when they are"
261 " mapped in reverse order\n");
269 #endif /* CONFIG_MTD_NOR_FLASH */
271 static int do_flinfo(struct cmd_tbl *cmdtp, int flag, int argc,
274 #ifdef CONFIG_MTD_NOR_FLASH
278 #ifdef CONFIG_MTD_NOR_FLASH
279 if (argc == 1) { /* print info for all FLASH banks */
280 for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) {
281 printf("\nBank # %ld: ", bank + 1);
283 flash_print_info(&flash_info[bank]);
288 bank = hextoul(argv[1], NULL);
289 if (bank < 1 || bank > CFI_FLASH_BANKS) {
290 printf("Only FLASH Banks # 1 ... # %d supported\n",
294 printf("\nBank # %ld: ", bank);
295 flash_print_info(&flash_info[bank - 1]);
296 #endif /* CONFIG_MTD_NOR_FLASH */
300 static int do_flerase(struct cmd_tbl *cmdtp, int flag, int argc,
303 #ifdef CONFIG_MTD_NOR_FLASH
304 flash_info_t *info = NULL;
305 ulong bank, addr_first, addr_last;
306 int n, sect_first = 0, sect_last = 0;
307 #if defined(CONFIG_CMD_MTDPARTS)
308 struct mtd_device *dev;
309 struct part_info *part;
310 u8 dev_type, dev_num, pnum;
315 return CMD_RET_USAGE;
317 if (strcmp(argv[1], "all") == 0) {
318 for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) {
319 printf("Erase Flash Bank # %ld ", bank);
320 info = &flash_info[bank - 1];
321 rcode = flash_erase(info, 0, info->sector_count - 1);
326 n = abbrev_spec(argv[1], &info, §_first, §_last);
329 puts("Bad sector specification\n");
332 printf("Erase Flash Sectors %d-%d in Bank # %zu ",
333 sect_first, sect_last, (info - flash_info) + 1);
334 rcode = flash_erase(info, sect_first, sect_last);
338 #if defined(CONFIG_CMD_MTDPARTS)
339 /* erase <part-id> - erase partition */
340 if (argc == 2 && mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0) {
342 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
343 if (dev->id->type == MTD_DEV_TYPE_NOR) {
345 info = &flash_info[bank];
346 addr_first = part->offset + info->start[0];
347 addr_last = addr_first + part->size - 1;
349 printf("Erase Flash Partition %s, bank %ld, 0x%08lx - 0x%08lx ",
350 argv[1], bank, addr_first,
353 rcode = flash_sect_erase(addr_first, addr_last);
357 printf("cannot erase, not a NOR device\n");
364 return CMD_RET_USAGE;
366 if (strcmp(argv[1], "bank") == 0) {
367 bank = hextoul(argv[2], NULL);
368 if (bank < 1 || bank > CFI_FLASH_BANKS) {
369 printf("Only FLASH Banks # 1 ... # %d supported\n",
373 printf("Erase Flash Bank # %ld ", bank);
374 info = &flash_info[bank - 1];
375 rcode = flash_erase(info, 0, info->sector_count - 1);
379 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0) {
380 printf("Bad address format\n");
384 if (addr_first >= addr_last)
385 return CMD_RET_USAGE;
387 rcode = flash_sect_erase(addr_first, addr_last);
391 #endif /* CONFIG_MTD_NOR_FLASH */
394 #ifdef CONFIG_MTD_NOR_FLASH
395 int flash_sect_erase(ulong addr_first, ulong addr_last)
399 int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS];
404 rcode = flash_fill_sect_ranges(addr_first, addr_last, s_first, s_last, &planned);
406 if (planned && rcode == 0) {
407 for (bank = 0, info = &flash_info[0];
408 bank < CFI_FLASH_BANKS && rcode == 0;
410 if (s_first[bank] >= 0) {
411 erased += s_last[bank] - s_first[bank] + 1;
412 debug("Erase Flash from 0x%08lx to 0x%08lx in Bank # %ld ",
413 info->start[s_first[bank]],
414 (s_last[bank] == info->sector_count) ?
415 info->start[0] + info->size - 1 :
416 info->start[s_last[bank] + 1] - 1,
418 rcode = flash_erase(info, s_first[bank],
423 printf("Erased %d sectors\n", erased);
424 } else if (rcode == 0) {
425 puts("Error: start and/or end address not on sector boundary\n");
430 #endif /* CONFIG_MTD_NOR_FLASH */
432 static int do_protect(struct cmd_tbl *cmdtp, int flag, int argc,
436 #ifdef CONFIG_MTD_NOR_FLASH
437 flash_info_t *info = NULL;
439 int i, n, sect_first = 0, sect_last = 0;
440 #if defined(CONFIG_CMD_MTDPARTS)
441 struct mtd_device *dev;
442 struct part_info *part;
443 u8 dev_type, dev_num, pnum;
445 #endif /* CONFIG_MTD_NOR_FLASH */
446 #if defined(CONFIG_MTD_NOR_FLASH)
448 ulong addr_first, addr_last;
452 return CMD_RET_USAGE;
454 #if defined(CONFIG_MTD_NOR_FLASH)
455 if (strcmp(argv[1], "off") == 0)
457 else if (strcmp(argv[1], "on") == 0)
460 return CMD_RET_USAGE;
463 #ifdef CONFIG_MTD_NOR_FLASH
464 if (strcmp(argv[2], "all") == 0) {
465 for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) {
466 info = &flash_info[bank - 1];
467 if (info->flash_id == FLASH_UNKNOWN)
470 printf("%sProtect Flash Bank # %ld\n",
471 p ? "" : "Un-", bank);
473 for (i = 0; i < info->sector_count; ++i) {
474 #if defined(CONFIG_SYS_FLASH_PROTECTION)
475 if (flash_real_protect(info, i, p))
479 info->protect[i] = p;
480 #endif /* CONFIG_SYS_FLASH_PROTECTION */
482 #if defined(CONFIG_SYS_FLASH_PROTECTION)
485 #endif /* CONFIG_SYS_FLASH_PROTECTION */
489 n = abbrev_spec(argv[2], &info, §_first, §_last);
492 puts("Bad sector specification\n");
495 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
496 p ? "" : "Un-", sect_first, sect_last,
497 (info - flash_info) + 1);
498 for (i = sect_first; i <= sect_last; i++) {
499 #if defined(CONFIG_SYS_FLASH_PROTECTION)
500 if (flash_real_protect(info, i, p))
504 info->protect[i] = p;
505 #endif /* CONFIG_SYS_FLASH_PROTECTION */
508 #if defined(CONFIG_SYS_FLASH_PROTECTION)
511 #endif /* CONFIG_SYS_FLASH_PROTECTION */
516 #if defined(CONFIG_CMD_MTDPARTS)
517 /* protect on/off <part-id> */
518 if (argc == 3 && mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0) {
520 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
521 if (dev->id->type == MTD_DEV_TYPE_NOR) {
523 info = &flash_info[bank];
524 addr_first = part->offset + info->start[0];
525 addr_last = addr_first + part->size - 1;
527 printf("%sProtect Flash Partition %s, "
528 "bank %ld, 0x%08lx - 0x%08lx\n",
529 p ? "" : "Un", argv[1],
530 bank, addr_first, addr_last);
532 rcode = flash_sect_protect(p, addr_first,
537 printf("cannot %sprotect, not a NOR device\n",
545 return CMD_RET_USAGE;
547 if (strcmp(argv[2], "bank") == 0) {
548 bank = hextoul(argv[3], NULL);
549 if (bank < 1 || bank > CFI_FLASH_BANKS) {
550 printf("Only FLASH Banks # 1 ... # %d supported\n",
554 printf("%sProtect Flash Bank # %ld\n",
555 p ? "" : "Un-", bank);
556 info = &flash_info[bank - 1];
558 if (info->flash_id == FLASH_UNKNOWN) {
559 puts("missing or unknown FLASH type\n");
562 for (i = 0; i < info->sector_count; ++i) {
563 #if defined(CONFIG_SYS_FLASH_PROTECTION)
564 if (flash_real_protect(info, i, p))
568 info->protect[i] = p;
569 #endif /* CONFIG_SYS_FLASH_PROTECTION */
572 #if defined(CONFIG_SYS_FLASH_PROTECTION)
575 #endif /* CONFIG_SYS_FLASH_PROTECTION */
580 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0) {
581 printf("Bad address format\n");
585 if (addr_first >= addr_last)
586 return CMD_RET_USAGE;
588 rcode = flash_sect_protect(p, addr_first, addr_last);
589 #endif /* CONFIG_MTD_NOR_FLASH */
593 #ifdef CONFIG_MTD_NOR_FLASH
594 int flash_sect_protect(int p, ulong addr_first, ulong addr_last)
598 int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS];
603 rcode = flash_fill_sect_ranges(addr_first, addr_last, s_first, s_last, &planned);
607 if (planned && rcode == 0) {
608 for (bank = 0, info = &flash_info[0];
609 bank < CFI_FLASH_BANKS; ++bank, ++info) {
610 if (info->flash_id == FLASH_UNKNOWN)
613 if (s_first[bank] >= 0 && s_first[bank] <= s_last[bank]) {
614 debug("%sProtecting sectors %d..%d in bank %ld\n",
615 p ? "" : "Un-", s_first[bank],
616 s_last[bank], bank + 1);
617 protected += s_last[bank] - s_first[bank] + 1;
618 for (i = s_first[bank]; i <= s_last[bank]; ++i) {
619 #if defined(CONFIG_SYS_FLASH_PROTECTION)
620 if (flash_real_protect(info, i, p))
624 info->protect[i] = p;
625 #endif /* CONFIG_SYS_FLASH_PROTECTION */
629 #if defined(CONFIG_SYS_FLASH_PROTECTION)
631 #endif /* CONFIG_SYS_FLASH_PROTECTION */
633 printf("%sProtected %d sectors\n",
634 p ? "" : "Un-", protected);
635 } else if (rcode == 0) {
636 puts("Error: start and/or end address not on sector boundary\n");
641 #endif /* CONFIG_MTD_NOR_FLASH */
643 /**************************************************/
644 #if defined(CONFIG_CMD_MTDPARTS)
645 # define TMP_ERASE "erase <part-id>\n - erase partition\n"
646 # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
647 # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
649 # define TMP_ERASE /* empty */
650 # define TMP_PROT_ON /* empty */
651 # define TMP_PROT_OFF /* empty */
655 flinfo, 2, 1, do_flinfo,
656 "print FLASH memory information",
657 "\n - print information for all FLASH memory banks\n"
658 "flinfo N\n - print information for FLASH memory bank # N"
662 erase, 3, 0, do_flerase,
663 "erase FLASH memory",
665 " - erase FLASH from addr 'start' to addr 'end'\n"
667 " - erase FLASH from addr 'start' to the end of sect w/addr 'start'+'len'-1\n"
668 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
669 "erase bank N\n - erase FLASH bank # N\n"
671 "erase all\n - erase all FLASH banks"
675 protect, 4, 0, do_protect,
676 "enable or disable FLASH write protection",
678 " - protect FLASH from addr 'start' to addr 'end'\n"
679 "protect on start +len\n"
680 " - protect FLASH from addr 'start' to end of sect w/addr 'start'+'len'-1\n"
681 "protect on N:SF[-SL]\n"
682 " - protect sectors SF-SL in FLASH bank # N\n"
683 "protect on bank N\n - protect FLASH bank # N\n"
685 "protect on all\n - protect all FLASH banks\n"
686 "protect off start end\n"
687 " - make FLASH from addr 'start' to addr 'end' writable\n"
688 "protect off start +len\n"
689 " - make FLASH from addr 'start' to end of sect w/addr 'start'+'len'-1 wrtable\n"
690 "protect off N:SF[-SL]\n"
691 " - make sectors SF-SL writable in FLASH bank # N\n"
692 "protect off bank N\n - make FLASH bank # N writable\n"
694 "protect off all\n - make all FLASH banks writable"