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 (CONFIG_COMMANDS & CFG_CMD_FLASH)
36 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
37 #include <jffs2/jffs2.h>
39 /* parition handling routines */
40 int mtdparts_init(void);
41 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
42 int find_dev_and_part(const char *id, struct mtd_device **dev,
43 u8 *part_num, struct part_info **part);
46 extern flash_info_t flash_info[]; /* info for FLASH chips */
49 * The user interface starts numbering for Flash banks with 1
50 * for historical reasons.
54 * this routine looks for an abbreviated flash range specification.
55 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
56 * sector to erase, and SL is the last sector to erase (defaults to SF).
57 * bank numbers start at 1 to be consistent with other specs, sector numbers
60 * returns: 1 - correct spec; *pinfo, *psf and *psl are
62 * 0 - doesn't look like an abbreviated spec
63 * -1 - looks like an abbreviated spec, but got
64 * a parsing error, a number out of range,
65 * or an invalid flash bank.
68 abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
71 int bank, first, last;
74 if ((p = strchr (str, ':')) == NULL)
78 bank = simple_strtoul (str, &ep, 10);
79 if (ep == str || *ep != '\0' ||
80 bank < 1 || bank > CFG_MAX_FLASH_BANKS ||
81 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
85 if ((p = strchr (str, '-')) != NULL)
88 first = simple_strtoul (str, &ep, 10);
89 if (ep == str || *ep != '\0' || first >= fp->sector_count)
93 last = simple_strtoul (p, &ep, 10);
94 if (ep == p || *ep != '\0' ||
95 last < first || last >= fp->sector_count)
109 * This function computes the start and end addresses for both
110 * erase and protect commands. The range of the addresses on which
111 * either of the commands is to operate can be given in two forms:
112 * 1. <cmd> start end - operate on <'start', 'end')
113 * 2. <cmd> start +length - operate on <'start', start + length)
114 * If the second form is used and the end address doesn't fall on the
115 * sector boundary, than it will be adjusted to the next sector boundary.
116 * If it isn't in the flash, the function will fail (return -1).
118 * arg1, arg2: address specification (i.e. both command arguments)
120 * addr_first, addr_last: computed address range
123 * -1: failure (bad format, bad address).
126 addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
128 char len_used = 0; /* indicates if the "start +length" form used */
131 *addr_first = simple_strtoul(arg1, &ep, 16);
132 if (ep == arg1 || *ep != '\0')
135 if (arg2 && *arg2 == '+'){
140 *addr_last = simple_strtoul(arg2, &ep, 16);
141 if (ep == arg2 || *ep != '\0')
149 * *addr_last has the length, compute correct *addr_last
150 * XXX watch out for the integer overflow! Right now it is
151 * checked for in both the callers.
153 *addr_last = *addr_first + *addr_last - 1;
156 * It may happen that *addr_last doesn't fall on the sector
157 * boundary. We want to round such an address to the next
158 * sector boundary, so that the commands don't fail later on.
161 /* find the end addr of the sector where the *addr_last is */
162 for (bank = 0; bank < CFG_MAX_FLASH_BANKS && !found; ++bank){
164 flash_info_t *info = &flash_info[bank];
165 for (i = 0; i < info->sector_count && !found; ++i){
166 /* get the end address of the sector */
167 ulong sector_end_addr;
168 if (i == info->sector_count - 1){
170 info->start[0] + info->size - 1;
173 info->start[i+1] - 1;
175 if (*addr_last <= sector_end_addr &&
176 *addr_last >= info->start[i]){
179 /* adjust *addr_last if necessary */
180 if (*addr_last < sector_end_addr){
181 *addr_last = sector_end_addr;
187 /* error, addres not in flash */
188 printf("Error: end address (0x%08lx) not in flash!\n",
192 } /* "start +length" from used */
198 flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
199 int *s_first, int *s_last,
208 for (bank=0; bank < CFG_MAX_FLASH_BANKS; ++bank) {
209 s_first[bank] = -1; /* first sector to erase */
210 s_last [bank] = -1; /* last sector to erase */
213 for (bank=0,info=&flash_info[0];
214 (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last);
220 if (info->flash_id == FLASH_UNKNOWN) {
224 b_end = info->start[0] + info->size - 1; /* bank end addr */
225 s_end = info->sector_count - 1; /* last sector */
228 for (sect=0; sect < info->sector_count; ++sect) {
229 ulong end; /* last address in current sect */
231 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
233 if (addr_first > end)
235 if (addr_last < info->start[sect])
238 if (addr_first == info->start[sect]) {
239 s_first[bank] = sect;
241 if (addr_last == end) {
245 if (s_first[bank] >= 0) {
246 if (s_last[bank] < 0) {
247 if (addr_last > b_end) {
248 s_last[bank] = s_end;
250 puts ("Error: end address"
251 " not on sector boundary\n");
256 if (s_last[bank] < s_first[bank]) {
257 puts ("Error: end sector"
258 " precedes start sector\n");
263 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
264 (*s_count) += s_last[bank] - s_first[bank] + 1;
265 } else if (addr_first >= info->start[0] && addr_first < b_end) {
266 puts ("Error: start address not on sector boundary\n");
269 } else if (s_last[bank] >= 0) {
270 puts ("Error: cannot span across banks when they are"
271 " mapped in reverse order\n");
280 int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
284 #ifdef CONFIG_HAS_DATAFLASH
285 dataflash_print_info();
288 if (argc == 1) { /* print info for all FLASH banks */
289 for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
290 printf ("\nBank # %ld: ", bank+1);
292 flash_print_info (&flash_info[bank]);
297 bank = simple_strtoul(argv[1], NULL, 16);
298 if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
299 printf ("Only FLASH Banks # 1 ... # %d supported\n",
300 CFG_MAX_FLASH_BANKS);
303 printf ("\nBank # %ld: ", bank);
304 flash_print_info (&flash_info[bank-1]);
308 int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
311 ulong bank, addr_first, addr_last;
312 int n, sect_first, sect_last;
313 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
314 struct mtd_device *dev;
315 struct part_info *part;
316 u8 dev_type, dev_num, pnum;
321 printf ("Usage:\n%s\n", cmdtp->usage);
325 if (strcmp(argv[1], "all") == 0) {
326 for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
327 printf ("Erase Flash Bank # %ld ", bank);
328 info = &flash_info[bank-1];
329 rcode = flash_erase (info, 0, info->sector_count-1);
334 if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) {
336 puts ("Bad sector specification\n");
339 printf ("Erase Flash Sectors %d-%d in Bank # %d ",
340 sect_first, sect_last, (info-flash_info)+1);
341 rcode = flash_erase(info, sect_first, sect_last);
345 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
346 /* erase <part-id> - erase partition */
347 if ((argc == 2) && (id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
349 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
350 if (dev->id->type == MTD_DEV_TYPE_NOR) {
352 info = &flash_info[bank];
353 addr_first = part->offset + info->start[0];
354 addr_last = addr_first + part->size - 1;
356 printf ("Erase Flash Parition %s, "
357 "bank %d, 0x%08lx - 0x%08lx ",
358 argv[1], bank, addr_first,
361 rcode = flash_sect_erase(addr_first, addr_last);
365 printf("cannot erase, not a NOR device\n");
372 printf ("Usage:\n%s\n", cmdtp->usage);
376 if (strcmp(argv[1], "bank") == 0) {
377 bank = simple_strtoul(argv[2], NULL, 16);
378 if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
379 printf ("Only FLASH Banks # 1 ... # %d supported\n",
380 CFG_MAX_FLASH_BANKS);
383 printf ("Erase Flash Bank # %ld ", bank);
384 info = &flash_info[bank-1];
385 rcode = flash_erase (info, 0, info->sector_count-1);
389 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
390 printf ("Bad address format\n");
394 if (addr_first >= addr_last) {
395 printf ("Usage:\n%s\n", cmdtp->usage);
399 rcode = flash_sect_erase(addr_first, addr_last);
403 int flash_sect_erase (ulong addr_first, ulong addr_last)
407 #ifdef CFG_MAX_FLASH_BANKS_DETECT
408 int s_first[CFG_MAX_FLASH_BANKS_DETECT], s_last[CFG_MAX_FLASH_BANKS_DETECT];
410 int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
416 rcode = flash_fill_sect_ranges (addr_first, addr_last,
417 s_first, s_last, &planned );
419 if (planned && (rcode == 0)) {
420 for (bank=0,info=&flash_info[0];
421 (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
423 if (s_first[bank]>=0) {
424 erased += s_last[bank] - s_first[bank] + 1;
425 debug ("Erase Flash from 0x%08lx to 0x%08lx "
427 info->start[s_first[bank]],
428 (s_last[bank] == info->sector_count) ?
429 info->start[0] + info->size - 1:
430 info->start[s_last[bank]+1] - 1,
432 rcode = flash_erase (info, s_first[bank], s_last[bank]);
435 printf ("Erased %d sectors\n", erased);
436 } else if (rcode == 0) {
437 puts ("Error: start and/or end address"
438 " not on sector boundary\n");
444 int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
447 ulong bank, addr_first, addr_last;
448 int i, p, n, sect_first, sect_last;
449 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
450 struct mtd_device *dev;
451 struct part_info *part;
452 u8 dev_type, dev_num, pnum;
455 #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 */
510 #if defined(CFG_FLASH_PROTECTION)
511 if (!rcode) puts (" done\n");
512 #endif /* CFG_FLASH_PROTECTION */
517 if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) {
519 puts ("Bad sector specification\n");
522 printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
523 p ? "" : "Un-", sect_first, sect_last,
524 (info-flash_info)+1);
525 for (i = sect_first; i <= sect_last; i++) {
526 #if defined(CFG_FLASH_PROTECTION)
527 if (flash_real_protect(info, i, p))
531 info->protect[i] = p;
532 #endif /* CFG_FLASH_PROTECTION */
535 #if defined(CFG_FLASH_PROTECTION)
536 if (!rcode) puts (" done\n");
537 #endif /* CFG_FLASH_PROTECTION */
542 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
543 /* protect on/off <part-id> */
544 if ((argc == 3) && (id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
546 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
547 if (dev->id->type == MTD_DEV_TYPE_NOR) {
549 info = &flash_info[bank];
550 addr_first = part->offset + info->start[0];
551 addr_last = addr_first + part->size - 1;
553 printf ("%sProtect Flash Parition %s, "
554 "bank %d, 0x%08lx - 0x%08lx\n",
555 p ? "" : "Un", argv[1],
556 bank, addr_first, addr_last);
558 rcode = flash_sect_protect (p, addr_first, addr_last);
562 printf("cannot %sprotect, not a NOR device\n",
570 printf ("Usage:\n%s\n", cmdtp->usage);
574 if (strcmp(argv[2], "bank") == 0) {
575 bank = simple_strtoul(argv[3], NULL, 16);
576 if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
577 printf ("Only FLASH Banks # 1 ... # %d supported\n",
578 CFG_MAX_FLASH_BANKS);
581 printf ("%sProtect Flash Bank # %ld\n",
582 p ? "" : "Un-", bank);
583 info = &flash_info[bank-1];
585 if (info->flash_id == FLASH_UNKNOWN) {
586 puts ("missing or unknown FLASH type\n");
589 for (i=0; i<info->sector_count; ++i) {
590 #if defined(CFG_FLASH_PROTECTION)
591 if (flash_real_protect(info, i, p))
595 info->protect[i] = p;
596 #endif /* CFG_FLASH_PROTECTION */
599 #if defined(CFG_FLASH_PROTECTION)
600 if (!rcode) puts (" done\n");
601 #endif /* CFG_FLASH_PROTECTION */
606 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
607 printf("Bad address format\n");
611 if (addr_first >= addr_last) {
612 printf ("Usage:\n%s\n", cmdtp->usage);
615 rcode = flash_sect_protect (p, addr_first, addr_last);
620 int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
624 #ifdef CFG_MAX_FLASH_BANKS_DETECT
625 int s_first[CFG_MAX_FLASH_BANKS_DETECT], s_last[CFG_MAX_FLASH_BANKS_DETECT];
627 int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
633 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
637 if (planned && (rcode == 0)) {
638 for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
639 if (info->flash_id == FLASH_UNKNOWN) {
643 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
644 debug ("%sProtecting sectors %d..%d in bank %ld\n",
646 s_first[bank], s_last[bank], bank+1);
647 protected += s_last[bank] - s_first[bank] + 1;
648 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
649 #if defined(CFG_FLASH_PROTECTION)
650 if (flash_real_protect(info, i, p))
654 info->protect[i] = p;
655 #endif /* CFG_FLASH_PROTECTION */
658 #if defined(CFG_FLASH_PROTECTION)
659 if (!rcode) putc ('\n');
660 #endif /* CFG_FLASH_PROTECTION */
663 printf ("%sProtected %d sectors\n",
664 p ? "" : "Un-", protected);
665 } else if (rcode == 0) {
666 puts ("Error: start and/or end address"
667 " not on sector boundary\n");
674 /**************************************************/
675 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
676 # define TMP_ERASE "erase <part-id>\n - erase partition\n"
677 # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
678 # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
680 # define TMP_ERASE /* empty */
681 # define TMP_PROT_ON /* empty */
682 # define TMP_PROT_OFF /* empty */
686 flinfo, 2, 1, do_flinfo,
687 "flinfo - print FLASH memory information\n",
688 "\n - print information for all FLASH memory banks\n"
689 "flinfo N\n - print information for FLASH memory bank # N\n"
693 erase, 3, 1, do_flerase,
694 "erase - erase FLASH memory\n",
696 " - erase FLASH from addr 'start' to addr 'end'\n"
698 " - erase FLASH from addr 'start' to the end of sect "
699 "w/addr 'start'+'len'-1\n"
700 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
701 "erase bank N\n - erase FLASH bank # N\n"
703 "erase all\n - erase all FLASH banks\n"
707 protect, 4, 1, do_protect,
708 "protect - enable or disable FLASH write protection\n",
710 " - protect FLASH from addr 'start' to addr 'end'\n"
711 "protect on start +len\n"
712 " - protect FLASH from addr 'start' to end of sect "
713 "w/addr 'start'+'len'-1\n"
714 "protect on N:SF[-SL]\n"
715 " - protect sectors SF-SL in FLASH bank # N\n"
716 "protect on bank N\n - protect FLASH bank # N\n"
718 "protect on all\n - protect all FLASH banks\n"
719 "protect off start end\n"
720 " - make FLASH from addr 'start' to addr 'end' writable\n"
721 "protect off start +len\n"
722 " - make FLASH from addr 'start' to end of sect "
723 "w/addr 'start'+'len'-1 wrtable\n"
724 "protect off N:SF[-SL]\n"
725 " - make sectors SF-SL writable in FLASH bank # N\n"
726 "protect off bank N\n - make FLASH bank # N writable\n"
728 "protect off all\n - make all FLASH banks writable\n"
735 #endif /* CFG_CMD_FLASH */