3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #ifdef CONFIG_HAS_DATAFLASH
31 #include <dataflash.h>
34 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
35 #include <jffs2/jffs2.h>
37 /* parition handling routines */
38 int mtdparts_init(void);
39 int id_parse(const char *id
, const char **ret_id
, u8
*dev_type
, u8
*dev_num
);
40 int find_dev_and_part(const char *id
, struct mtd_device
**dev
,
41 u8
*part_num
, struct part_info
**part
);
44 extern flash_info_t flash_info
[]; /* info for FLASH chips */
47 * The user interface starts numbering for Flash banks with 1
48 * for historical reasons.
52 * this routine looks for an abbreviated flash range specification.
53 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
54 * sector to erase, and SL is the last sector to erase (defaults to SF).
55 * bank numbers start at 1 to be consistent with other specs, sector numbers
58 * returns: 1 - correct spec; *pinfo, *psf and *psl are
60 * 0 - doesn't look like an abbreviated spec
61 * -1 - looks like an abbreviated spec, but got
62 * a parsing error, a number out of range,
63 * or an invalid flash bank.
66 abbrev_spec (char *str
, flash_info_t
** pinfo
, int *psf
, int *psl
)
69 int bank
, first
, last
;
72 if ((p
= strchr (str
, ':')) == NULL
)
76 bank
= simple_strtoul (str
, &ep
, 10);
77 if (ep
== str
|| *ep
!= '\0' ||
78 bank
< 1 || bank
> CFG_MAX_FLASH_BANKS
||
79 (fp
= &flash_info
[bank
- 1])->flash_id
== FLASH_UNKNOWN
)
83 if ((p
= strchr (str
, '-')) != NULL
)
86 first
= simple_strtoul (str
, &ep
, 10);
87 if (ep
== str
|| *ep
!= '\0' || first
>= fp
->sector_count
)
91 last
= simple_strtoul (p
, &ep
, 10);
92 if (ep
== p
|| *ep
!= '\0' ||
93 last
< first
|| last
>= fp
->sector_count
)
107 * This function computes the start and end addresses for both
108 * erase and protect commands. The range of the addresses on which
109 * either of the commands is to operate can be given in two forms:
110 * 1. <cmd> start end - operate on <'start', 'end')
111 * 2. <cmd> start +length - operate on <'start', start + length)
112 * If the second form is used and the end address doesn't fall on the
113 * sector boundary, than it will be adjusted to the next sector boundary.
114 * If it isn't in the flash, the function will fail (return -1).
116 * arg1, arg2: address specification (i.e. both command arguments)
118 * addr_first, addr_last: computed address range
121 * -1: failure (bad format, bad address).
124 addr_spec(char *arg1
, char *arg2
, ulong
*addr_first
, ulong
*addr_last
)
127 char len_used
; /* indicates if the "start +length" form used */
131 *addr_first
= simple_strtoul(arg1
, &ep
, 16);
132 if (ep
== arg1
|| *ep
!= '\0')
136 if (arg2
&& *arg2
== '+'){
141 *addr_last
= simple_strtoul(arg2
, &ep
, 16);
142 if (ep
== arg2
|| *ep
!= '\0')
147 * *addr_last has the length, compute correct *addr_last
148 * XXX watch out for the integer overflow! Right now it is
149 * checked for in both the callers.
151 *addr_last
= *addr_first
+ *addr_last
- 1;
154 * It may happen that *addr_last doesn't fall on the sector
155 * boundary. We want to round such an address to the next
156 * sector boundary, so that the commands don't fail later on.
159 /* find the end addr of the sector where the *addr_last is */
161 for (bank
= 0; bank
< CFG_MAX_FLASH_BANKS
&& !found
; ++bank
){
163 flash_info_t
*info
= &flash_info
[bank
];
164 for (i
= 0; i
< info
->sector_count
&& !found
; ++i
){
165 /* get the end address of the sector */
166 ulong sector_end_addr
;
167 if (i
== info
->sector_count
- 1){
169 info
->start
[0] + info
->size
- 1;
172 info
->start
[i
+1] - 1;
174 if (*addr_last
<= sector_end_addr
&&
175 *addr_last
>= info
->start
[i
]){
178 /* adjust *addr_last if necessary */
179 if (*addr_last
< sector_end_addr
){
180 *addr_last
= sector_end_addr
;
186 /* error, addres not in flash */
187 printf("Error: end address (0x%08lx) not in flash!\n",
191 } /* "start +length" from used */
197 flash_fill_sect_ranges (ulong addr_first
, ulong addr_last
,
198 int *s_first
, int *s_last
,
207 for (bank
=0; bank
< CFG_MAX_FLASH_BANKS
; ++bank
) {
208 s_first
[bank
] = -1; /* first sector to erase */
209 s_last
[bank
] = -1; /* last sector to erase */
212 for (bank
=0,info
=&flash_info
[0];
213 (bank
< CFG_MAX_FLASH_BANKS
) && (addr_first
<= addr_last
);
219 if (info
->flash_id
== FLASH_UNKNOWN
) {
223 b_end
= info
->start
[0] + info
->size
- 1; /* bank end addr */
224 s_end
= info
->sector_count
- 1; /* last sector */
227 for (sect
=0; sect
< info
->sector_count
; ++sect
) {
228 ulong end
; /* last address in current sect */
230 end
= (sect
== s_end
) ? b_end
: info
->start
[sect
+ 1] - 1;
232 if (addr_first
> end
)
234 if (addr_last
< info
->start
[sect
])
237 if (addr_first
== info
->start
[sect
]) {
238 s_first
[bank
] = sect
;
240 if (addr_last
== end
) {
244 if (s_first
[bank
] >= 0) {
245 if (s_last
[bank
] < 0) {
246 if (addr_last
> b_end
) {
247 s_last
[bank
] = s_end
;
249 puts ("Error: end address"
250 " not on sector boundary\n");
255 if (s_last
[bank
] < s_first
[bank
]) {
256 puts ("Error: end sector"
257 " precedes start sector\n");
262 addr_first
= (sect
== s_end
) ? b_end
+ 1: info
->start
[sect
+ 1];
263 (*s_count
) += s_last
[bank
] - s_first
[bank
] + 1;
264 } else if (addr_first
>= info
->start
[0] && addr_first
< b_end
) {
265 puts ("Error: start address not on sector boundary\n");
268 } else if (s_last
[bank
] >= 0) {
269 puts ("Error: cannot span across banks when they are"
270 " mapped in reverse order\n");
279 int do_flinfo ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
283 #ifdef CONFIG_HAS_DATAFLASH
284 dataflash_print_info();
287 if (argc
== 1) { /* print info for all FLASH banks */
288 for (bank
=0; bank
<CFG_MAX_FLASH_BANKS
; ++bank
) {
289 printf ("\nBank # %ld: ", bank
+1);
291 flash_print_info (&flash_info
[bank
]);
296 bank
= simple_strtoul(argv
[1], NULL
, 16);
297 if ((bank
< 1) || (bank
> CFG_MAX_FLASH_BANKS
)) {
298 printf ("Only FLASH Banks # 1 ... # %d supported\n",
299 CFG_MAX_FLASH_BANKS
);
302 printf ("\nBank # %ld: ", bank
);
303 flash_print_info (&flash_info
[bank
-1]);
307 int do_flerase (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
310 ulong bank
, addr_first
, addr_last
;
311 int n
, sect_first
, sect_last
;
312 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
313 struct mtd_device
*dev
;
314 struct part_info
*part
;
315 u8 dev_type
, dev_num
, pnum
;
320 printf ("Usage:\n%s\n", cmdtp
->usage
);
324 if (strcmp(argv
[1], "all") == 0) {
325 for (bank
=1; bank
<=CFG_MAX_FLASH_BANKS
; ++bank
) {
326 printf ("Erase Flash Bank # %ld ", bank
);
327 info
= &flash_info
[bank
-1];
328 rcode
= flash_erase (info
, 0, info
->sector_count
-1);
333 if ((n
= abbrev_spec(argv
[1], &info
, §_first
, §_last
)) != 0) {
335 puts ("Bad sector specification\n");
338 printf ("Erase Flash Sectors %d-%d in Bank # %d ",
339 sect_first
, sect_last
, (info
-flash_info
)+1);
340 rcode
= flash_erase(info
, sect_first
, sect_last
);
344 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
345 /* erase <part-id> - erase partition */
346 if ((argc
== 2) && (id_parse(argv
[1], NULL
, &dev_type
, &dev_num
) == 0)) {
348 if (find_dev_and_part(argv
[1], &dev
, &pnum
, &part
) == 0) {
349 if (dev
->id
->type
== MTD_DEV_TYPE_NOR
) {
351 info
= &flash_info
[bank
];
352 addr_first
= part
->offset
+ info
->start
[0];
353 addr_last
= addr_first
+ part
->size
- 1;
355 printf ("Erase Flash Parition %s, "
356 "bank %d, 0x%08lx - 0x%08lx ",
357 argv
[1], bank
, addr_first
,
360 rcode
= flash_sect_erase(addr_first
, addr_last
);
364 printf("cannot erase, not a NOR device\n");
371 printf ("Usage:\n%s\n", cmdtp
->usage
);
375 if (strcmp(argv
[1], "bank") == 0) {
376 bank
= simple_strtoul(argv
[2], NULL
, 16);
377 if ((bank
< 1) || (bank
> CFG_MAX_FLASH_BANKS
)) {
378 printf ("Only FLASH Banks # 1 ... # %d supported\n",
379 CFG_MAX_FLASH_BANKS
);
382 printf ("Erase Flash Bank # %ld ", bank
);
383 info
= &flash_info
[bank
-1];
384 rcode
= flash_erase (info
, 0, info
->sector_count
-1);
388 if (addr_spec(argv
[1], argv
[2], &addr_first
, &addr_last
) < 0){
389 printf ("Bad address format\n");
393 if (addr_first
>= addr_last
) {
394 printf ("Usage:\n%s\n", cmdtp
->usage
);
398 rcode
= flash_sect_erase(addr_first
, addr_last
);
402 int flash_sect_erase (ulong addr_first
, ulong addr_last
)
406 #ifdef CFG_MAX_FLASH_BANKS_DETECT
407 int s_first
[CFG_MAX_FLASH_BANKS_DETECT
], s_last
[CFG_MAX_FLASH_BANKS_DETECT
];
409 int s_first
[CFG_MAX_FLASH_BANKS
], s_last
[CFG_MAX_FLASH_BANKS
];
415 rcode
= flash_fill_sect_ranges (addr_first
, addr_last
,
416 s_first
, s_last
, &planned
);
418 if (planned
&& (rcode
== 0)) {
419 for (bank
=0,info
=&flash_info
[0];
420 (bank
< CFG_MAX_FLASH_BANKS
) && (rcode
== 0);
422 if (s_first
[bank
]>=0) {
423 erased
+= s_last
[bank
] - s_first
[bank
] + 1;
424 debug ("Erase Flash from 0x%08lx to 0x%08lx "
426 info
->start
[s_first
[bank
]],
427 (s_last
[bank
] == info
->sector_count
) ?
428 info
->start
[0] + info
->size
- 1:
429 info
->start
[s_last
[bank
]+1] - 1,
431 rcode
= flash_erase (info
, s_first
[bank
], s_last
[bank
]);
434 printf ("Erased %d sectors\n", erased
);
435 } else if (rcode
== 0) {
436 puts ("Error: start and/or end address"
437 " not on sector boundary\n");
443 int do_protect (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
446 ulong bank
, addr_first
, addr_last
;
447 int i
, p
, n
, sect_first
, sect_last
;
448 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
449 struct mtd_device
*dev
;
450 struct part_info
*part
;
451 u8 dev_type
, dev_num
, pnum
;
454 #ifdef CONFIG_HAS_DATAFLASH
459 printf ("Usage:\n%s\n", cmdtp
->usage
);
463 if (strcmp(argv
[1], "off") == 0) {
465 } else if (strcmp(argv
[1], "on") == 0) {
468 printf ("Usage:\n%s\n", cmdtp
->usage
);
472 #ifdef CONFIG_HAS_DATAFLASH
473 if ((strcmp(argv
[2], "all") != 0) && (strcmp(argv
[2], "bank") != 0)) {
474 addr_first
= simple_strtoul(argv
[2], NULL
, 16);
475 addr_last
= simple_strtoul(argv
[3], NULL
, 16);
477 if (addr_dataflash(addr_first
) && addr_dataflash(addr_last
)) {
478 status
= dataflash_real_protect(p
,addr_first
,addr_last
);
480 puts ("Bad DataFlash sector specification\n");
483 printf("%sProtect %d DataFlash Sectors\n",
484 p
? "" : "Un-", status
);
490 if (strcmp(argv
[2], "all") == 0) {
491 for (bank
=1; bank
<=CFG_MAX_FLASH_BANKS
; ++bank
) {
492 info
= &flash_info
[bank
-1];
493 if (info
->flash_id
== FLASH_UNKNOWN
) {
496 printf ("%sProtect Flash Bank # %ld\n",
497 p
? "" : "Un-", bank
);
499 for (i
=0; i
<info
->sector_count
; ++i
) {
500 #if defined(CFG_FLASH_PROTECTION)
501 if (flash_real_protect(info
, i
, p
))
505 info
->protect
[i
] = p
;
506 #endif /* CFG_FLASH_PROTECTION */
508 #if defined(CFG_FLASH_PROTECTION)
509 if (!rcode
) puts (" done\n");
510 #endif /* CFG_FLASH_PROTECTION */
515 if ((n
= abbrev_spec(argv
[2], &info
, §_first
, §_last
)) != 0) {
517 puts ("Bad sector specification\n");
520 printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
521 p
? "" : "Un-", sect_first
, sect_last
,
522 (info
-flash_info
)+1);
523 for (i
= sect_first
; i
<= sect_last
; i
++) {
524 #if defined(CFG_FLASH_PROTECTION)
525 if (flash_real_protect(info
, i
, p
))
529 info
->protect
[i
] = p
;
530 #endif /* CFG_FLASH_PROTECTION */
533 #if defined(CFG_FLASH_PROTECTION)
534 if (!rcode
) puts (" done\n");
535 #endif /* CFG_FLASH_PROTECTION */
540 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
541 /* protect on/off <part-id> */
542 if ((argc
== 3) && (id_parse(argv
[2], NULL
, &dev_type
, &dev_num
) == 0)) {
544 if (find_dev_and_part(argv
[2], &dev
, &pnum
, &part
) == 0) {
545 if (dev
->id
->type
== MTD_DEV_TYPE_NOR
) {
547 info
= &flash_info
[bank
];
548 addr_first
= part
->offset
+ info
->start
[0];
549 addr_last
= addr_first
+ part
->size
- 1;
551 printf ("%sProtect Flash Parition %s, "
552 "bank %d, 0x%08lx - 0x%08lx\n",
553 p
? "" : "Un", argv
[1],
554 bank
, addr_first
, addr_last
);
556 rcode
= flash_sect_protect (p
, addr_first
, addr_last
);
560 printf("cannot %sprotect, not a NOR device\n",
568 printf ("Usage:\n%s\n", cmdtp
->usage
);
572 if (strcmp(argv
[2], "bank") == 0) {
573 bank
= simple_strtoul(argv
[3], NULL
, 16);
574 if ((bank
< 1) || (bank
> CFG_MAX_FLASH_BANKS
)) {
575 printf ("Only FLASH Banks # 1 ... # %d supported\n",
576 CFG_MAX_FLASH_BANKS
);
579 printf ("%sProtect Flash Bank # %ld\n",
580 p
? "" : "Un-", bank
);
581 info
= &flash_info
[bank
-1];
583 if (info
->flash_id
== FLASH_UNKNOWN
) {
584 puts ("missing or unknown FLASH type\n");
587 for (i
=0; i
<info
->sector_count
; ++i
) {
588 #if defined(CFG_FLASH_PROTECTION)
589 if (flash_real_protect(info
, i
, p
))
593 info
->protect
[i
] = p
;
594 #endif /* CFG_FLASH_PROTECTION */
597 #if defined(CFG_FLASH_PROTECTION)
598 if (!rcode
) puts (" done\n");
599 #endif /* CFG_FLASH_PROTECTION */
604 if (addr_spec(argv
[2], argv
[3], &addr_first
, &addr_last
) < 0){
605 printf("Bad address format\n");
609 if (addr_first
>= addr_last
) {
610 printf ("Usage:\n%s\n", cmdtp
->usage
);
613 rcode
= flash_sect_protect (p
, addr_first
, addr_last
);
618 int flash_sect_protect (int p
, ulong addr_first
, ulong addr_last
)
622 #ifdef CFG_MAX_FLASH_BANKS_DETECT
623 int s_first
[CFG_MAX_FLASH_BANKS_DETECT
], s_last
[CFG_MAX_FLASH_BANKS_DETECT
];
625 int s_first
[CFG_MAX_FLASH_BANKS
], s_last
[CFG_MAX_FLASH_BANKS
];
631 rcode
= flash_fill_sect_ranges( addr_first
, addr_last
, s_first
, s_last
, &planned
);
635 if (planned
&& (rcode
== 0)) {
636 for (bank
=0,info
=&flash_info
[0]; bank
< CFG_MAX_FLASH_BANKS
; ++bank
, ++info
) {
637 if (info
->flash_id
== FLASH_UNKNOWN
) {
641 if (s_first
[bank
]>=0 && s_first
[bank
]<=s_last
[bank
]) {
642 debug ("%sProtecting sectors %d..%d in bank %ld\n",
644 s_first
[bank
], s_last
[bank
], bank
+1);
645 protected += s_last
[bank
] - s_first
[bank
] + 1;
646 for (i
=s_first
[bank
]; i
<=s_last
[bank
]; ++i
) {
647 #if defined(CFG_FLASH_PROTECTION)
648 if (flash_real_protect(info
, i
, p
))
652 info
->protect
[i
] = p
;
653 #endif /* CFG_FLASH_PROTECTION */
657 #if defined(CFG_FLASH_PROTECTION)
659 #endif /* CFG_FLASH_PROTECTION */
661 printf ("%sProtected %d sectors\n",
662 p
? "" : "Un-", protected);
663 } else if (rcode
== 0) {
664 puts ("Error: start and/or end address"
665 " not on sector boundary\n");
672 /**************************************************/
673 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
674 # define TMP_ERASE "erase <part-id>\n - erase partition\n"
675 # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
676 # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
678 # define TMP_ERASE /* empty */
679 # define TMP_PROT_ON /* empty */
680 # define TMP_PROT_OFF /* empty */
684 flinfo
, 2, 1, do_flinfo
,
685 "flinfo - print FLASH memory information\n",
686 "\n - print information for all FLASH memory banks\n"
687 "flinfo N\n - print information for FLASH memory bank # N\n"
691 erase
, 3, 0, do_flerase
,
692 "erase - erase FLASH memory\n",
694 " - erase FLASH from addr 'start' to addr 'end'\n"
696 " - erase FLASH from addr 'start' to the end of sect "
697 "w/addr 'start'+'len'-1\n"
698 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
699 "erase bank N\n - erase FLASH bank # N\n"
701 "erase all\n - erase all FLASH banks\n"
705 protect
, 4, 0, do_protect
,
706 "protect - enable or disable FLASH write protection\n",
708 " - protect FLASH from addr 'start' to addr 'end'\n"
709 "protect on start +len\n"
710 " - protect FLASH from addr 'start' to end of sect "
711 "w/addr 'start'+'len'-1\n"
712 "protect on N:SF[-SL]\n"
713 " - protect sectors SF-SL in FLASH bank # N\n"
714 "protect on bank N\n - protect FLASH bank # N\n"
716 "protect on all\n - protect all FLASH banks\n"
717 "protect off start end\n"
718 " - make FLASH from addr 'start' to addr 'end' writable\n"
719 "protect off start +len\n"
720 " - make FLASH from addr 'start' to end of sect "
721 "w/addr 'start'+'len'-1 wrtable\n"
722 "protect off N:SF[-SL]\n"
723 " - make sectors SF-SL writable in FLASH bank # N\n"
724 "protect off bank N\n - make FLASH bank # N writable\n"
726 "protect off all\n - make all FLASH banks writable\n"