1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
13 #if defined(CONFIG_CMD_MTDPARTS)
14 #include <jffs2/jffs2.h>
16 /* partition handling routines */
17 int mtdparts_init(void);
18 int mtd_id_parse(const char *id
, const char **ret_id
, u8
*dev_type
, u8
*dev_num
);
19 int find_dev_and_part(const char *id
, struct mtd_device
**dev
,
20 u8
*part_num
, struct part_info
**part
);
23 #ifdef CONFIG_MTD_NOR_FLASH
25 #include <mtd/cfi_flash.h>
26 extern flash_info_t flash_info
[]; /* info for FLASH chips */
29 * The user interface starts numbering for Flash banks with 1
30 * for historical reasons.
34 * this routine looks for an abbreviated flash range specification.
35 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
36 * sector to erase, and SL is the last sector to erase (defaults to SF).
37 * bank numbers start at 1 to be consistent with other specs, sector numbers
40 * returns: 1 - correct spec; *pinfo, *psf and *psl are
42 * 0 - doesn't look like an abbreviated spec
43 * -1 - looks like an abbreviated spec, but got
44 * a parsing error, a number out of range,
45 * or an invalid flash bank.
48 abbrev_spec (char *str
, flash_info_t
** pinfo
, int *psf
, int *psl
)
51 int bank
, first
, last
;
54 if ((p
= strchr (str
, ':')) == NULL
)
58 bank
= simple_strtoul (str
, &ep
, 10);
59 if (ep
== str
|| *ep
!= '\0' ||
60 bank
< 1 || bank
> CONFIG_SYS_MAX_FLASH_BANKS
||
61 (fp
= &flash_info
[bank
- 1])->flash_id
== FLASH_UNKNOWN
)
65 if ((p
= strchr (str
, '-')) != NULL
)
68 first
= simple_strtoul (str
, &ep
, 10);
69 if (ep
== str
|| *ep
!= '\0' || first
>= fp
->sector_count
)
73 last
= simple_strtoul (p
, &ep
, 10);
74 if (ep
== p
|| *ep
!= '\0' ||
75 last
< first
|| last
>= fp
->sector_count
)
89 * Take *addr in Flash and adjust it to fall on the end of its sector
91 int flash_sect_roundb (ulong
*addr
)
94 ulong bank
, sector_end_addr
;
98 /* find the end addr of the sector where the *addr is */
100 for (bank
= 0; bank
< CONFIG_SYS_MAX_FLASH_BANKS
&& !found
; ++bank
) {
101 info
= &flash_info
[bank
];
102 for (i
= 0; i
< info
->sector_count
&& !found
; ++i
) {
103 /* get the end address of the sector */
104 if (i
== info
->sector_count
- 1) {
105 sector_end_addr
= info
->start
[0] +
108 sector_end_addr
= info
->start
[i
+1] - 1;
111 if (*addr
<= sector_end_addr
&&
112 *addr
>= info
->start
[i
]) {
114 /* adjust *addr if necessary */
115 if (*addr
< sector_end_addr
)
116 *addr
= sector_end_addr
;
121 /* error, address not in flash */
122 printf("Error: end address (0x%08lx) not in flash!\n", *addr
);
130 * This function computes the start and end addresses for both
131 * erase and protect commands. The range of the addresses on which
132 * either of the commands is to operate can be given in two forms:
133 * 1. <cmd> start end - operate on <'start', 'end')
134 * 2. <cmd> start +length - operate on <'start', start + length)
135 * If the second form is used and the end address doesn't fall on the
136 * sector boundary, than it will be adjusted to the next sector boundary.
137 * If it isn't in the flash, the function will fail (return -1).
139 * arg1, arg2: address specification (i.e. both command arguments)
141 * addr_first, addr_last: computed address range
144 * -1: failure (bad format, bad address).
147 addr_spec(char *arg1
, char *arg2
, ulong
*addr_first
, ulong
*addr_last
)
150 char len_used
; /* indicates if the "start +length" form used */
152 *addr_first
= simple_strtoul(arg1
, &ep
, 16);
153 if (ep
== arg1
|| *ep
!= '\0')
157 if (arg2
&& *arg2
== '+'){
162 *addr_last
= simple_strtoul(arg2
, &ep
, 16);
163 if (ep
== arg2
|| *ep
!= '\0')
168 * *addr_last has the length, compute correct *addr_last
169 * XXX watch out for the integer overflow! Right now it is
170 * checked for in both the callers.
172 *addr_last
= *addr_first
+ *addr_last
- 1;
175 * It may happen that *addr_last doesn't fall on the sector
176 * boundary. We want to round such an address to the next
177 * sector boundary, so that the commands don't fail later on.
180 if (flash_sect_roundb(addr_last
) > 0)
182 } /* "start +length" from used */
188 flash_fill_sect_ranges (ulong addr_first
, ulong addr_last
,
189 int *s_first
, int *s_last
,
198 for (bank
=0; bank
< CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
) {
199 s_first
[bank
] = -1; /* first sector to erase */
200 s_last
[bank
] = -1; /* last sector to erase */
203 for (bank
=0,info
= &flash_info
[0];
204 (bank
< CONFIG_SYS_MAX_FLASH_BANKS
) && (addr_first
<= addr_last
);
210 if (info
->flash_id
== FLASH_UNKNOWN
) {
214 b_end
= info
->start
[0] + info
->size
- 1; /* bank end addr */
215 s_end
= info
->sector_count
- 1; /* last sector */
218 for (sect
=0; sect
< info
->sector_count
; ++sect
) {
219 ulong end
; /* last address in current sect */
221 end
= (sect
== s_end
) ? b_end
: info
->start
[sect
+ 1] - 1;
223 if (addr_first
> end
)
225 if (addr_last
< info
->start
[sect
])
228 if (addr_first
== info
->start
[sect
]) {
229 s_first
[bank
] = sect
;
231 if (addr_last
== end
) {
235 if (s_first
[bank
] >= 0) {
236 if (s_last
[bank
] < 0) {
237 if (addr_last
> b_end
) {
238 s_last
[bank
] = s_end
;
240 puts ("Error: end address"
241 " not on sector boundary\n");
246 if (s_last
[bank
] < s_first
[bank
]) {
247 puts ("Error: end sector"
248 " 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(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
273 #ifdef CONFIG_MTD_NOR_FLASH
277 #ifdef CONFIG_MTD_NOR_FLASH
278 if (argc
== 1) { /* print info for all FLASH banks */
279 for (bank
=0; bank
<CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
) {
280 printf ("\nBank # %ld: ", bank
+1);
282 flash_print_info (&flash_info
[bank
]);
287 bank
= simple_strtoul(argv
[1], NULL
, 16);
288 if ((bank
< 1) || (bank
> CONFIG_SYS_MAX_FLASH_BANKS
)) {
289 printf ("Only FLASH Banks # 1 ... # %d supported\n",
290 CONFIG_SYS_MAX_FLASH_BANKS
);
293 printf ("\nBank # %ld: ", bank
);
294 flash_print_info (&flash_info
[bank
-1]);
295 #endif /* CONFIG_MTD_NOR_FLASH */
299 static int do_flerase(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
301 #ifdef CONFIG_MTD_NOR_FLASH
302 flash_info_t
*info
= NULL
;
303 ulong bank
, addr_first
, addr_last
;
304 int n
, sect_first
= 0, sect_last
= 0;
305 #if defined(CONFIG_CMD_MTDPARTS)
306 struct mtd_device
*dev
;
307 struct part_info
*part
;
308 u8 dev_type
, dev_num
, pnum
;
313 return CMD_RET_USAGE
;
315 if (strcmp(argv
[1], "all") == 0) {
316 for (bank
=1; bank
<=CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
) {
317 printf ("Erase Flash Bank # %ld ", bank
);
318 info
= &flash_info
[bank
-1];
319 rcode
= flash_erase (info
, 0, info
->sector_count
-1);
324 if ((n
= abbrev_spec(argv
[1], &info
, §_first
, §_last
)) != 0) {
326 puts ("Bad sector specification\n");
329 printf ("Erase Flash Sectors %d-%d in Bank # %zu ",
330 sect_first
, sect_last
, (info
-flash_info
)+1);
331 rcode
= flash_erase(info
, sect_first
, sect_last
);
335 #if defined(CONFIG_CMD_MTDPARTS)
336 /* erase <part-id> - erase partition */
337 if ((argc
== 2) && (mtd_id_parse(argv
[1], NULL
, &dev_type
, &dev_num
) == 0)) {
339 if (find_dev_and_part(argv
[1], &dev
, &pnum
, &part
) == 0) {
340 if (dev
->id
->type
== MTD_DEV_TYPE_NOR
) {
342 info
= &flash_info
[bank
];
343 addr_first
= part
->offset
+ info
->start
[0];
344 addr_last
= addr_first
+ part
->size
- 1;
346 printf ("Erase Flash Partition %s, "
347 "bank %ld, 0x%08lx - 0x%08lx ",
348 argv
[1], bank
, addr_first
,
351 rcode
= flash_sect_erase(addr_first
, addr_last
);
355 printf("cannot erase, not a NOR device\n");
362 return CMD_RET_USAGE
;
364 if (strcmp(argv
[1], "bank") == 0) {
365 bank
= simple_strtoul(argv
[2], NULL
, 16);
366 if ((bank
< 1) || (bank
> CONFIG_SYS_MAX_FLASH_BANKS
)) {
367 printf ("Only FLASH Banks # 1 ... # %d supported\n",
368 CONFIG_SYS_MAX_FLASH_BANKS
);
371 printf ("Erase Flash Bank # %ld ", bank
);
372 info
= &flash_info
[bank
-1];
373 rcode
= flash_erase (info
, 0, info
->sector_count
-1);
377 if (addr_spec(argv
[1], argv
[2], &addr_first
, &addr_last
) < 0){
378 printf ("Bad address format\n");
382 if (addr_first
>= addr_last
)
383 return CMD_RET_USAGE
;
385 rcode
= flash_sect_erase(addr_first
, addr_last
);
389 #endif /* CONFIG_MTD_NOR_FLASH */
392 #ifdef CONFIG_MTD_NOR_FLASH
393 int flash_sect_erase (ulong addr_first
, ulong addr_last
)
397 int s_first
[CONFIG_SYS_MAX_FLASH_BANKS
], s_last
[CONFIG_SYS_MAX_FLASH_BANKS
];
402 rcode
= flash_fill_sect_ranges (addr_first
, addr_last
,
403 s_first
, s_last
, &planned
);
405 if (planned
&& (rcode
== 0)) {
406 for (bank
=0,info
= &flash_info
[0];
407 (bank
< CONFIG_SYS_MAX_FLASH_BANKS
) && (rcode
== 0);
409 if (s_first
[bank
]>=0) {
410 erased
+= s_last
[bank
] - s_first
[bank
] + 1;
411 debug ("Erase Flash from 0x%08lx to 0x%08lx "
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
], s_last
[bank
]);
422 printf("Erased %d sectors\n", erased
);
423 } else if (rcode
== 0) {
424 puts ("Error: start and/or end address"
425 " not on sector boundary\n");
430 #endif /* CONFIG_MTD_NOR_FLASH */
432 static int do_protect(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
435 #ifdef CONFIG_MTD_NOR_FLASH
436 flash_info_t
*info
= NULL
;
438 int i
, n
, sect_first
= 0, sect_last
= 0;
439 #if defined(CONFIG_CMD_MTDPARTS)
440 struct mtd_device
*dev
;
441 struct part_info
*part
;
442 u8 dev_type
, dev_num
, pnum
;
444 #endif /* CONFIG_MTD_NOR_FLASH */
445 #if defined(CONFIG_MTD_NOR_FLASH)
447 ulong addr_first
, addr_last
;
451 return CMD_RET_USAGE
;
453 #if defined(CONFIG_MTD_NOR_FLASH)
454 if (strcmp(argv
[1], "off") == 0)
456 else if (strcmp(argv
[1], "on") == 0)
459 return CMD_RET_USAGE
;
462 #ifdef CONFIG_MTD_NOR_FLASH
463 if (strcmp(argv
[2], "all") == 0) {
464 for (bank
=1; bank
<=CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
) {
465 info
= &flash_info
[bank
-1];
466 if (info
->flash_id
== FLASH_UNKNOWN
) {
469 printf ("%sProtect Flash Bank # %ld\n",
470 p
? "" : "Un-", bank
);
472 for (i
=0; i
<info
->sector_count
; ++i
) {
473 #if defined(CONFIG_SYS_FLASH_PROTECTION)
474 if (flash_real_protect(info
, i
, p
))
478 info
->protect
[i
] = p
;
479 #endif /* CONFIG_SYS_FLASH_PROTECTION */
481 #if defined(CONFIG_SYS_FLASH_PROTECTION)
482 if (!rcode
) puts (" done\n");
483 #endif /* CONFIG_SYS_FLASH_PROTECTION */
488 if ((n
= abbrev_spec(argv
[2], &info
, §_first
, §_last
)) != 0) {
490 puts ("Bad sector specification\n");
493 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
494 p
? "" : "Un-", sect_first
, sect_last
,
495 (info
-flash_info
)+1);
496 for (i
= sect_first
; i
<= sect_last
; i
++) {
497 #if defined(CONFIG_SYS_FLASH_PROTECTION)
498 if (flash_real_protect(info
, i
, p
))
502 info
->protect
[i
] = p
;
503 #endif /* CONFIG_SYS_FLASH_PROTECTION */
506 #if defined(CONFIG_SYS_FLASH_PROTECTION)
507 if (!rcode
) puts (" done\n");
508 #endif /* CONFIG_SYS_FLASH_PROTECTION */
513 #if defined(CONFIG_CMD_MTDPARTS)
514 /* protect on/off <part-id> */
515 if ((argc
== 3) && (mtd_id_parse(argv
[2], NULL
, &dev_type
, &dev_num
) == 0)) {
517 if (find_dev_and_part(argv
[2], &dev
, &pnum
, &part
) == 0) {
518 if (dev
->id
->type
== MTD_DEV_TYPE_NOR
) {
520 info
= &flash_info
[bank
];
521 addr_first
= part
->offset
+ info
->start
[0];
522 addr_last
= addr_first
+ part
->size
- 1;
524 printf ("%sProtect Flash Partition %s, "
525 "bank %ld, 0x%08lx - 0x%08lx\n",
526 p
? "" : "Un", argv
[1],
527 bank
, addr_first
, addr_last
);
529 rcode
= flash_sect_protect (p
, addr_first
, addr_last
);
533 printf("cannot %sprotect, not a NOR device\n",
541 return CMD_RET_USAGE
;
543 if (strcmp(argv
[2], "bank") == 0) {
544 bank
= simple_strtoul(argv
[3], NULL
, 16);
545 if ((bank
< 1) || (bank
> CONFIG_SYS_MAX_FLASH_BANKS
)) {
546 printf ("Only FLASH Banks # 1 ... # %d supported\n",
547 CONFIG_SYS_MAX_FLASH_BANKS
);
550 printf ("%sProtect Flash Bank # %ld\n",
551 p
? "" : "Un-", bank
);
552 info
= &flash_info
[bank
-1];
554 if (info
->flash_id
== FLASH_UNKNOWN
) {
555 puts ("missing or unknown FLASH type\n");
558 for (i
=0; i
<info
->sector_count
; ++i
) {
559 #if defined(CONFIG_SYS_FLASH_PROTECTION)
560 if (flash_real_protect(info
, i
, p
))
564 info
->protect
[i
] = p
;
565 #endif /* CONFIG_SYS_FLASH_PROTECTION */
568 #if defined(CONFIG_SYS_FLASH_PROTECTION)
569 if (!rcode
) puts (" done\n");
570 #endif /* CONFIG_SYS_FLASH_PROTECTION */
575 if (addr_spec(argv
[2], argv
[3], &addr_first
, &addr_last
) < 0){
576 printf("Bad address format\n");
580 if (addr_first
>= addr_last
)
581 return CMD_RET_USAGE
;
583 rcode
= flash_sect_protect (p
, addr_first
, addr_last
);
584 #endif /* CONFIG_MTD_NOR_FLASH */
588 #ifdef CONFIG_MTD_NOR_FLASH
589 int flash_sect_protect (int p
, ulong addr_first
, ulong addr_last
)
593 int s_first
[CONFIG_SYS_MAX_FLASH_BANKS
], s_last
[CONFIG_SYS_MAX_FLASH_BANKS
];
598 rcode
= flash_fill_sect_ranges( addr_first
, addr_last
, s_first
, s_last
, &planned
);
602 if (planned
&& (rcode
== 0)) {
603 for (bank
=0,info
= &flash_info
[0]; bank
< CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
, ++info
) {
604 if (info
->flash_id
== FLASH_UNKNOWN
) {
608 if (s_first
[bank
]>=0 && s_first
[bank
]<=s_last
[bank
]) {
609 debug ("%sProtecting sectors %d..%d in bank %ld\n",
611 s_first
[bank
], s_last
[bank
], bank
+1);
612 protected += s_last
[bank
] - s_first
[bank
] + 1;
613 for (i
=s_first
[bank
]; i
<=s_last
[bank
]; ++i
) {
614 #if defined(CONFIG_SYS_FLASH_PROTECTION)
615 if (flash_real_protect(info
, i
, p
))
619 info
->protect
[i
] = p
;
620 #endif /* CONFIG_SYS_FLASH_PROTECTION */
624 #if defined(CONFIG_SYS_FLASH_PROTECTION)
626 #endif /* CONFIG_SYS_FLASH_PROTECTION */
628 printf ("%sProtected %d sectors\n",
629 p
? "" : "Un-", protected);
630 } else if (rcode
== 0) {
631 puts ("Error: start and/or end address"
632 " not on sector boundary\n");
637 #endif /* CONFIG_MTD_NOR_FLASH */
640 /**************************************************/
641 #if defined(CONFIG_CMD_MTDPARTS)
642 # define TMP_ERASE "erase <part-id>\n - erase partition\n"
643 # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
644 # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
646 # define TMP_ERASE /* empty */
647 # define TMP_PROT_ON /* empty */
648 # define TMP_PROT_OFF /* empty */
652 flinfo
, 2, 1, do_flinfo
,
653 "print FLASH memory information",
654 "\n - print information for all FLASH memory banks\n"
655 "flinfo N\n - print information for FLASH memory bank # N"
659 erase
, 3, 0, do_flerase
,
660 "erase FLASH memory",
662 " - erase FLASH from addr 'start' to addr 'end'\n"
664 " - erase FLASH from addr 'start' to the end of sect "
665 "w/addr 'start'+'len'-1\n"
666 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
667 "erase bank N\n - erase FLASH bank # N\n"
669 "erase all\n - erase all FLASH banks"
673 protect
, 4, 0, do_protect
,
674 "enable or disable FLASH write protection",
676 " - protect FLASH from addr 'start' to addr 'end'\n"
677 "protect on start +len\n"
678 " - protect FLASH from addr 'start' to end of sect "
679 "w/addr 'start'+'len'-1\n"
680 "protect on N:SF[-SL]\n"
681 " - protect sectors SF-SL in FLASH bank # N\n"
682 "protect on bank N\n - protect FLASH bank # N\n"
684 "protect on all\n - protect all FLASH banks\n"
685 "protect off start end\n"
686 " - make FLASH from addr 'start' to addr 'end' writable\n"
687 "protect off start +len\n"
688 " - make FLASH from addr 'start' to end of sect "
689 "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"