1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 #if defined(CONFIG_CMD_MTDPARTS)
15 #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);
24 #ifdef CONFIG_MTD_NOR_FLASH
26 #include <mtd/cfi_flash.h>
27 extern flash_info_t flash_info[]; /* info for FLASH chips */
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;
55 if ((p = strchr (str, ':')) == NULL)
59 bank = simple_strtoul (str, &ep, 10);
60 if (ep == str || *ep != '\0' ||
61 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
62 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
66 if ((p = strchr (str, '-')) != NULL)
69 first = simple_strtoul (str, &ep, 10);
70 if (ep == str || *ep != '\0' || first >= fp->sector_count)
74 last = simple_strtoul (p, &ep, 10);
75 if (ep == p || *ep != '\0' ||
76 last < first || last >= fp->sector_count)
90 * Take *addr in Flash and adjust it to fall on the end of its sector
92 int flash_sect_roundb(ulong *addr)
95 ulong bank, sector_end_addr;
99 /* find the end addr of the sector where the *addr is */
101 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
102 info = &flash_info[bank];
103 for (i = 0; i < info->sector_count && !found; ++i) {
104 /* get the end address of the sector */
105 if (i == info->sector_count - 1) {
106 sector_end_addr = info->start[0] +
109 sector_end_addr = info->start[i+1] - 1;
112 if (*addr <= sector_end_addr &&
113 *addr >= info->start[i]) {
115 /* adjust *addr if necessary */
116 if (*addr < sector_end_addr)
117 *addr = sector_end_addr;
122 /* error, address not in flash */
123 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
131 * This function computes the start and end addresses for both
132 * erase and protect commands. The range of the addresses on which
133 * either of the commands is to operate can be given in two forms:
134 * 1. <cmd> start end - operate on <'start', 'end')
135 * 2. <cmd> start +length - operate on <'start', start + length)
136 * If the second form is used and the end address doesn't fall on the
137 * sector boundary, than it will be adjusted to the next sector boundary.
138 * If it isn't in the flash, the function will fail (return -1).
140 * arg1, arg2: address specification (i.e. both command arguments)
142 * addr_first, addr_last: computed address range
145 * -1: failure (bad format, bad address).
148 addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
151 char len_used; /* indicates if the "start +length" form used */
153 *addr_first = simple_strtoul(arg1, &ep, 16);
154 if (ep == arg1 || *ep != '\0')
158 if (arg2 && *arg2 == '+'){
163 *addr_last = simple_strtoul(arg2, &ep, 16);
164 if (ep == arg2 || *ep != '\0')
169 * *addr_last has the length, compute correct *addr_last
170 * XXX watch out for the integer overflow! Right now it is
171 * checked for in both the callers.
173 *addr_last = *addr_first + *addr_last - 1;
176 * It may happen that *addr_last doesn't fall on the sector
177 * boundary. We want to round such an address to the next
178 * sector boundary, so that the commands don't fail later on.
181 if (flash_sect_roundb(addr_last) > 0)
183 } /* "start +length" from used */
189 flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
190 int *s_first, int *s_last,
199 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
200 s_first[bank] = -1; /* first sector to erase */
201 s_last [bank] = -1; /* last sector to erase */
204 for (bank=0,info = &flash_info[0];
205 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
211 if (info->flash_id == FLASH_UNKNOWN) {
215 b_end = info->start[0] + info->size - 1; /* bank end addr */
216 s_end = info->sector_count - 1; /* last sector */
219 for (sect=0; sect < info->sector_count; ++sect) {
220 ulong end; /* last address in current sect */
222 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
224 if (addr_first > end)
226 if (addr_last < info->start[sect])
229 if (addr_first == info->start[sect]) {
230 s_first[bank] = sect;
232 if (addr_last == end) {
236 if (s_first[bank] >= 0) {
237 if (s_last[bank] < 0) {
238 if (addr_last > b_end) {
239 s_last[bank] = s_end;
241 puts ("Error: end address"
242 " not on sector boundary\n");
247 if (s_last[bank] < s_first[bank]) {
248 puts ("Error: end sector"
249 " 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 <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
282 printf ("\nBank # %ld: ", bank+1);
284 flash_print_info(&flash_info[bank]);
289 bank = simple_strtoul(argv[1], NULL, 16);
290 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
291 printf ("Only FLASH Banks # 1 ... # %d supported\n",
292 CONFIG_SYS_MAX_FLASH_BANKS);
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<=CONFIG_SYS_MAX_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 if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) {
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, "
350 "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 = simple_strtoul(argv[2], NULL, 16);
369 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
370 printf ("Only FLASH Banks # 1 ... # %d supported\n",
371 CONFIG_SYS_MAX_FLASH_BANKS);
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[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
405 rcode = flash_fill_sect_ranges (addr_first, addr_last,
406 s_first, s_last, &planned );
408 if (planned && (rcode == 0)) {
409 for (bank=0,info = &flash_info[0];
410 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
412 if (s_first[bank]>=0) {
413 erased += s_last[bank] - s_first[bank] + 1;
414 debug("Erase Flash from 0x%08lx to 0x%08lx in Bank # %ld ",
415 info->start[s_first[bank]],
416 (s_last[bank] == info->sector_count) ?
417 info->start[0] + info->size - 1 :
418 info->start[s_last[bank] + 1] - 1,
420 rcode = flash_erase(info, s_first[bank],
425 printf("Erased %d sectors\n", erased);
426 } else if (rcode == 0) {
427 puts ("Error: start and/or end address"
428 " not on sector boundary\n");
433 #endif /* CONFIG_MTD_NOR_FLASH */
435 static int do_protect(struct cmd_tbl *cmdtp, int flag, int argc,
439 #ifdef CONFIG_MTD_NOR_FLASH
440 flash_info_t *info = NULL;
442 int i, n, sect_first = 0, sect_last = 0;
443 #if defined(CONFIG_CMD_MTDPARTS)
444 struct mtd_device *dev;
445 struct part_info *part;
446 u8 dev_type, dev_num, pnum;
448 #endif /* CONFIG_MTD_NOR_FLASH */
449 #if defined(CONFIG_MTD_NOR_FLASH)
451 ulong addr_first, addr_last;
455 return CMD_RET_USAGE;
457 #if defined(CONFIG_MTD_NOR_FLASH)
458 if (strcmp(argv[1], "off") == 0)
460 else if (strcmp(argv[1], "on") == 0)
463 return CMD_RET_USAGE;
466 #ifdef CONFIG_MTD_NOR_FLASH
467 if (strcmp(argv[2], "all") == 0) {
468 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
469 info = &flash_info[bank-1];
470 if (info->flash_id == FLASH_UNKNOWN) {
473 printf ("%sProtect Flash Bank # %ld\n",
474 p ? "" : "Un-", bank);
476 for (i=0; i<info->sector_count; ++i) {
477 #if defined(CONFIG_SYS_FLASH_PROTECTION)
478 if (flash_real_protect(info, i, p))
482 info->protect[i] = p;
483 #endif /* CONFIG_SYS_FLASH_PROTECTION */
485 #if defined(CONFIG_SYS_FLASH_PROTECTION)
486 if (!rcode) puts (" done\n");
487 #endif /* CONFIG_SYS_FLASH_PROTECTION */
492 if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) {
494 puts ("Bad sector specification\n");
497 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
498 p ? "" : "Un-", sect_first, sect_last,
499 (info-flash_info)+1);
500 for (i = sect_first; i <= sect_last; i++) {
501 #if defined(CONFIG_SYS_FLASH_PROTECTION)
502 if (flash_real_protect(info, i, p))
506 info->protect[i] = p;
507 #endif /* CONFIG_SYS_FLASH_PROTECTION */
510 #if defined(CONFIG_SYS_FLASH_PROTECTION)
511 if (!rcode) puts (" done\n");
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 = simple_strtoul(argv[3], NULL, 16);
550 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
551 printf ("Only FLASH Banks # 1 ... # %d supported\n",
552 CONFIG_SYS_MAX_FLASH_BANKS);
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)
574 if (!rcode) puts (" done\n");
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[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_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]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
609 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"
637 " not on sector boundary\n");
642 #endif /* CONFIG_MTD_NOR_FLASH */
645 /**************************************************/
646 #if defined(CONFIG_CMD_MTDPARTS)
647 # define TMP_ERASE "erase <part-id>\n - erase partition\n"
648 # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
649 # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
651 # define TMP_ERASE /* empty */
652 # define TMP_PROT_ON /* empty */
653 # define TMP_PROT_OFF /* empty */
657 flinfo, 2, 1, do_flinfo,
658 "print FLASH memory information",
659 "\n - print information for all FLASH memory banks\n"
660 "flinfo N\n - print information for FLASH memory bank # N"
664 erase, 3, 0, do_flerase,
665 "erase FLASH memory",
667 " - erase FLASH from addr 'start' to addr 'end'\n"
669 " - erase FLASH from addr 'start' to the end of sect "
670 "w/addr 'start'+'len'-1\n"
671 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
672 "erase bank N\n - erase FLASH bank # N\n"
674 "erase all\n - erase all FLASH banks"
678 protect, 4, 0, do_protect,
679 "enable or disable FLASH write protection",
681 " - protect FLASH from addr 'start' to addr 'end'\n"
682 "protect on start +len\n"
683 " - protect FLASH from addr 'start' to end of sect "
684 "w/addr 'start'+'len'-1\n"
685 "protect on N:SF[-SL]\n"
686 " - protect sectors SF-SL in FLASH bank # N\n"
687 "protect on bank N\n - protect FLASH bank # N\n"
689 "protect on all\n - protect all FLASH banks\n"
690 "protect off start end\n"
691 " - make FLASH from addr 'start' to addr 'end' writable\n"
692 "protect off start +len\n"
693 " - make FLASH from addr 'start' to end of sect "
694 "w/addr 'start'+'len'-1 wrtable\n"
695 "protect off N:SF[-SL]\n"
696 " - make sectors SF-SL writable in FLASH bank # N\n"
697 "protect off bank N\n - make FLASH bank # N writable\n"
699 "protect off all\n - make all FLASH banks writable"