1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <common.h>
28#include <command.h>
29
30#ifdef CONFIG_HAS_DATAFLASH
31#include <dataflash.h>
32#endif
33
34#if defined(CONFIG_CMD_MTDPARTS)
35#include <jffs2/jffs2.h>
36
37
38int mtdparts_init(void);
39int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
40int find_dev_and_part(const char *id, struct mtd_device **dev,
41 u8 *part_num, struct part_info **part);
42#endif
43
44#ifndef CONFIG_SYS_NO_FLASH
45#include <flash.h>
46#include <mtd/cfi_flash.h>
47extern flash_info_t flash_info[];
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68static int
69abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
70{
71 flash_info_t *fp;
72 int bank, first, last;
73 char *p, *ep;
74
75 if ((p = strchr (str, ':')) == NULL)
76 return 0;
77 *p++ = '\0';
78
79 bank = simple_strtoul (str, &ep, 10);
80 if (ep == str || *ep != '\0' ||
81 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
82 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
83 return -1;
84
85 str = p;
86 if ((p = strchr (str, '-')) != NULL)
87 *p++ = '\0';
88
89 first = simple_strtoul (str, &ep, 10);
90 if (ep == str || *ep != '\0' || first >= fp->sector_count)
91 return -1;
92
93 if (p != NULL) {
94 last = simple_strtoul (p, &ep, 10);
95 if (ep == p || *ep != '\0' ||
96 last < first || last >= fp->sector_count)
97 return -1;
98 } else {
99 last = first;
100 }
101
102 *pinfo = fp;
103 *psf = first;
104 *psl = last;
105
106 return 1;
107}
108
109
110
111
112int flash_sect_roundb (ulong *addr)
113{
114 flash_info_t *info;
115 ulong bank, sector_end_addr;
116 char found;
117 int i;
118
119
120 found = 0;
121 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
122 info = &flash_info[bank];
123 for (i = 0; i < info->sector_count && !found; ++i) {
124
125 if (i == info->sector_count - 1) {
126 sector_end_addr = info->start[0] +
127 info->size - 1;
128 } else {
129 sector_end_addr = info->start[i+1] - 1;
130 }
131
132 if (*addr <= sector_end_addr &&
133 *addr >= info->start[i]) {
134 found = 1;
135
136 if (*addr < sector_end_addr)
137 *addr = sector_end_addr;
138 }
139 }
140 }
141 if (!found) {
142
143 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
144 return 1;
145 }
146
147 return 0;
148}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167static int
168addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
169{
170 char *ep;
171 char len_used;
172
173 *addr_first = simple_strtoul(arg1, &ep, 16);
174 if (ep == arg1 || *ep != '\0')
175 return -1;
176
177 len_used = 0;
178 if (arg2 && *arg2 == '+'){
179 len_used = 1;
180 ++arg2;
181 }
182
183 *addr_last = simple_strtoul(arg2, &ep, 16);
184 if (ep == arg2 || *ep != '\0')
185 return -1;
186
187 if (len_used){
188
189
190
191
192
193 *addr_last = *addr_first + *addr_last - 1;
194
195
196
197
198
199
200
201 if (flash_sect_roundb(addr_last) > 0)
202 return -1;
203 }
204
205 return 1;
206}
207
208static int
209flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
210 int *s_first, int *s_last,
211 int *s_count )
212{
213 flash_info_t *info;
214 ulong bank;
215 int rcode = 0;
216
217 *s_count = 0;
218
219 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
220 s_first[bank] = -1;
221 s_last [bank] = -1;
222 }
223
224 for (bank=0,info = &flash_info[0];
225 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
226 ++bank, ++info) {
227 ulong b_end;
228 int sect;
229 short s_end;
230
231 if (info->flash_id == FLASH_UNKNOWN) {
232 continue;
233 }
234
235 b_end = info->start[0] + info->size - 1;
236 s_end = info->sector_count - 1;
237
238
239 for (sect=0; sect < info->sector_count; ++sect) {
240 ulong end;
241
242 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
243
244 if (addr_first > end)
245 continue;
246 if (addr_last < info->start[sect])
247 continue;
248
249 if (addr_first == info->start[sect]) {
250 s_first[bank] = sect;
251 }
252 if (addr_last == end) {
253 s_last[bank] = sect;
254 }
255 }
256 if (s_first[bank] >= 0) {
257 if (s_last[bank] < 0) {
258 if (addr_last > b_end) {
259 s_last[bank] = s_end;
260 } else {
261 puts ("Error: end address"
262 " not on sector boundary\n");
263 rcode = 1;
264 break;
265 }
266 }
267 if (s_last[bank] < s_first[bank]) {
268 puts ("Error: end sector"
269 " precedes start sector\n");
270 rcode = 1;
271 break;
272 }
273 sect = s_last[bank];
274 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
275 (*s_count) += s_last[bank] - s_first[bank] + 1;
276 } else if (addr_first >= info->start[0] && addr_first < b_end) {
277 puts ("Error: start address not on sector boundary\n");
278 rcode = 1;
279 break;
280 } else if (s_last[bank] >= 0) {
281 puts ("Error: cannot span across banks when they are"
282 " mapped in reverse order\n");
283 rcode = 1;
284 break;
285 }
286 }
287
288 return rcode;
289}
290#endif
291
292int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
293{
294#ifndef CONFIG_SYS_NO_FLASH
295 ulong bank;
296#endif
297
298#ifdef CONFIG_HAS_DATAFLASH
299 dataflash_print_info();
300#endif
301
302#ifndef CONFIG_SYS_NO_FLASH
303 if (argc == 1) {
304 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
305 printf ("\nBank # %ld: ", bank+1);
306
307 flash_print_info (&flash_info[bank]);
308 }
309 return 0;
310 }
311
312 bank = simple_strtoul(argv[1], NULL, 16);
313 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
314 printf ("Only FLASH Banks # 1 ... # %d supported\n",
315 CONFIG_SYS_MAX_FLASH_BANKS);
316 return 1;
317 }
318 printf ("\nBank # %ld: ", bank);
319 flash_print_info (&flash_info[bank-1]);
320#endif
321 return 0;
322}
323
324int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
325{
326#ifndef CONFIG_SYS_NO_FLASH
327 flash_info_t *info;
328 ulong bank, addr_first, addr_last;
329 int n, sect_first, sect_last;
330#if defined(CONFIG_CMD_MTDPARTS)
331 struct mtd_device *dev;
332 struct part_info *part;
333 u8 dev_type, dev_num, pnum;
334#endif
335 int rcode = 0;
336
337 if (argc < 2)
338 return cmd_usage(cmdtp);
339
340 if (strcmp(argv[1], "all") == 0) {
341 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
342 printf ("Erase Flash Bank # %ld ", bank);
343 info = &flash_info[bank-1];
344 rcode = flash_erase (info, 0, info->sector_count-1);
345 }
346 return rcode;
347 }
348
349 if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) {
350 if (n < 0) {
351 puts ("Bad sector specification\n");
352 return 1;
353 }
354 printf ("Erase Flash Sectors %d-%d in Bank # %zu ",
355 sect_first, sect_last, (info-flash_info)+1);
356 rcode = flash_erase(info, sect_first, sect_last);
357 return rcode;
358 }
359
360#if defined(CONFIG_CMD_MTDPARTS)
361
362 if ((argc == 2) && (mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
363 mtdparts_init();
364 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
365 if (dev->id->type == MTD_DEV_TYPE_NOR) {
366 bank = dev->id->num;
367 info = &flash_info[bank];
368 addr_first = part->offset + info->start[0];
369 addr_last = addr_first + part->size - 1;
370
371 printf ("Erase Flash Partition %s, "
372 "bank %ld, 0x%08lx - 0x%08lx ",
373 argv[1], bank, addr_first,
374 addr_last);
375
376 rcode = flash_sect_erase(addr_first, addr_last);
377 return rcode;
378 }
379
380 printf("cannot erase, not a NOR device\n");
381 return 1;
382 }
383 }
384#endif
385
386 if (argc != 3)
387 return cmd_usage(cmdtp);
388
389 if (strcmp(argv[1], "bank") == 0) {
390 bank = simple_strtoul(argv[2], NULL, 16);
391 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
392 printf ("Only FLASH Banks # 1 ... # %d supported\n",
393 CONFIG_SYS_MAX_FLASH_BANKS);
394 return 1;
395 }
396 printf ("Erase Flash Bank # %ld ", bank);
397 info = &flash_info[bank-1];
398 rcode = flash_erase (info, 0, info->sector_count-1);
399 return rcode;
400 }
401
402 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
403 printf ("Bad address format\n");
404 return 1;
405 }
406
407 if (addr_first >= addr_last)
408 return cmd_usage(cmdtp);
409
410 rcode = flash_sect_erase(addr_first, addr_last);
411 return rcode;
412#else
413 return 0;
414#endif
415}
416
417#ifndef CONFIG_SYS_NO_FLASH
418int flash_sect_erase (ulong addr_first, ulong addr_last)
419{
420 flash_info_t *info;
421 ulong bank;
422 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
423 int erased = 0;
424 int planned;
425 int rcode = 0;
426
427 rcode = flash_fill_sect_ranges (addr_first, addr_last,
428 s_first, s_last, &planned );
429
430 if (planned && (rcode == 0)) {
431 for (bank=0,info = &flash_info[0];
432 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
433 ++bank, ++info) {
434 if (s_first[bank]>=0) {
435 erased += s_last[bank] - s_first[bank] + 1;
436 debug ("Erase Flash from 0x%08lx to 0x%08lx "
437 "in Bank # %ld ",
438 info->start[s_first[bank]],
439 (s_last[bank] == info->sector_count) ?
440 info->start[0] + info->size - 1:
441 info->start[s_last[bank]+1] - 1,
442 bank+1);
443 rcode = flash_erase (info, s_first[bank], s_last[bank]);
444 }
445 }
446 printf ("Erased %d sectors\n", erased);
447 } else if (rcode == 0) {
448 puts ("Error: start and/or end address"
449 " not on sector boundary\n");
450 rcode = 1;
451 }
452 return rcode;
453}
454#endif
455
456int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
457{
458#ifndef CONFIG_SYS_NO_FLASH
459 flash_info_t *info;
460 ulong bank;
461 int i, n, sect_first, sect_last;
462#if defined(CONFIG_CMD_MTDPARTS)
463 struct mtd_device *dev;
464 struct part_info *part;
465 u8 dev_type, dev_num, pnum;
466#endif
467#endif
468#if !defined(CONFIG_SYS_NO_FLASH) || defined(CONFIG_HAS_DATAFLASH)
469 ulong addr_first, addr_last;
470#endif
471#ifdef CONFIG_HAS_DATAFLASH
472 int status;
473#endif
474 int p;
475 int rcode = 0;
476
477 if (argc < 3)
478 return cmd_usage(cmdtp);
479
480 if (strcmp(argv[1], "off") == 0)
481 p = 0;
482 else if (strcmp(argv[1], "on") == 0)
483 p = 1;
484 else
485 return cmd_usage(cmdtp);
486
487#ifdef CONFIG_HAS_DATAFLASH
488 if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
489 addr_first = simple_strtoul(argv[2], NULL, 16);
490 addr_last = simple_strtoul(argv[3], NULL, 16);
491
492 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
493 status = dataflash_real_protect(p,addr_first,addr_last);
494 if (status < 0){
495 puts ("Bad DataFlash sector specification\n");
496 return 1;
497 }
498 printf("%sProtect %d DataFlash Sectors\n",
499 p ? "" : "Un-", status);
500 return 0;
501 }
502 }
503#endif
504
505#ifndef CONFIG_SYS_NO_FLASH
506 if (strcmp(argv[2], "all") == 0) {
507 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
508 info = &flash_info[bank-1];
509 if (info->flash_id == FLASH_UNKNOWN) {
510 continue;
511 }
512 printf ("%sProtect Flash Bank # %ld\n",
513 p ? "" : "Un-", bank);
514
515 for (i=0; i<info->sector_count; ++i) {
516#if defined(CONFIG_SYS_FLASH_PROTECTION)
517 if (flash_real_protect(info, i, p))
518 rcode = 1;
519 putc ('.');
520#else
521 info->protect[i] = p;
522#endif
523 }
524#if defined(CONFIG_SYS_FLASH_PROTECTION)
525 if (!rcode) puts (" done\n");
526#endif
527 }
528 return rcode;
529 }
530
531 if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) {
532 if (n < 0) {
533 puts ("Bad sector specification\n");
534 return 1;
535 }
536 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
537 p ? "" : "Un-", sect_first, sect_last,
538 (info-flash_info)+1);
539 for (i = sect_first; i <= sect_last; i++) {
540#if defined(CONFIG_SYS_FLASH_PROTECTION)
541 if (flash_real_protect(info, i, p))
542 rcode = 1;
543 putc ('.');
544#else
545 info->protect[i] = p;
546#endif
547 }
548
549#if defined(CONFIG_SYS_FLASH_PROTECTION)
550 if (!rcode) puts (" done\n");
551#endif
552
553 return rcode;
554 }
555
556#if defined(CONFIG_CMD_MTDPARTS)
557
558 if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
559 mtdparts_init();
560 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
561 if (dev->id->type == MTD_DEV_TYPE_NOR) {
562 bank = dev->id->num;
563 info = &flash_info[bank];
564 addr_first = part->offset + info->start[0];
565 addr_last = addr_first + part->size - 1;
566
567 printf ("%sProtect Flash Partition %s, "
568 "bank %ld, 0x%08lx - 0x%08lx\n",
569 p ? "" : "Un", argv[1],
570 bank, addr_first, addr_last);
571
572 rcode = flash_sect_protect (p, addr_first, addr_last);
573 return rcode;
574 }
575
576 printf("cannot %sprotect, not a NOR device\n",
577 p ? "" : "un");
578 return 1;
579 }
580 }
581#endif
582
583 if (argc != 4)
584 return cmd_usage(cmdtp);
585
586 if (strcmp(argv[2], "bank") == 0) {
587 bank = simple_strtoul(argv[3], NULL, 16);
588 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
589 printf ("Only FLASH Banks # 1 ... # %d supported\n",
590 CONFIG_SYS_MAX_FLASH_BANKS);
591 return 1;
592 }
593 printf ("%sProtect Flash Bank # %ld\n",
594 p ? "" : "Un-", bank);
595 info = &flash_info[bank-1];
596
597 if (info->flash_id == FLASH_UNKNOWN) {
598 puts ("missing or unknown FLASH type\n");
599 return 1;
600 }
601 for (i=0; i<info->sector_count; ++i) {
602#if defined(CONFIG_SYS_FLASH_PROTECTION)
603 if (flash_real_protect(info, i, p))
604 rcode = 1;
605 putc ('.');
606#else
607 info->protect[i] = p;
608#endif
609 }
610
611#if defined(CONFIG_SYS_FLASH_PROTECTION)
612 if (!rcode) puts (" done\n");
613#endif
614
615 return rcode;
616 }
617
618 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
619 printf("Bad address format\n");
620 return 1;
621 }
622
623 if (addr_first >= addr_last)
624 return cmd_usage(cmdtp);
625
626 rcode = flash_sect_protect (p, addr_first, addr_last);
627#endif
628 return rcode;
629}
630
631#ifndef CONFIG_SYS_NO_FLASH
632int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
633{
634 flash_info_t *info;
635 ulong bank;
636 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
637 int protected, i;
638 int planned;
639 int rcode;
640
641 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
642
643 protected = 0;
644
645 if (planned && (rcode == 0)) {
646 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
647 if (info->flash_id == FLASH_UNKNOWN) {
648 continue;
649 }
650
651 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
652 debug ("%sProtecting sectors %d..%d in bank %ld\n",
653 p ? "" : "Un-",
654 s_first[bank], s_last[bank], bank+1);
655 protected += s_last[bank] - s_first[bank] + 1;
656 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
657#if defined(CONFIG_SYS_FLASH_PROTECTION)
658 if (flash_real_protect(info, i, p))
659 rcode = 1;
660 putc ('.');
661#else
662 info->protect[i] = p;
663#endif
664 }
665 }
666 }
667#if defined(CONFIG_SYS_FLASH_PROTECTION)
668 puts (" done\n");
669#endif
670
671 printf ("%sProtected %d sectors\n",
672 p ? "" : "Un-", protected);
673 } else if (rcode == 0) {
674 puts ("Error: start and/or end address"
675 " not on sector boundary\n");
676 rcode = 1;
677 }
678 return rcode;
679}
680#endif
681
682
683
684#if defined(CONFIG_CMD_MTDPARTS)
685# define TMP_ERASE "erase <part-id>\n - erase partition\n"
686# define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
687# define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
688#else
689# define TMP_ERASE
690# define TMP_PROT_ON
691# define TMP_PROT_OFF
692#endif
693
694U_BOOT_CMD(
695 flinfo, 2, 1, do_flinfo,
696 "print FLASH memory information",
697 "\n - print information for all FLASH memory banks\n"
698 "flinfo N\n - print information for FLASH memory bank # N"
699);
700
701U_BOOT_CMD(
702 erase, 3, 0, do_flerase,
703 "erase FLASH memory",
704 "start end\n"
705 " - erase FLASH from addr 'start' to addr 'end'\n"
706 "erase start +len\n"
707 " - erase FLASH from addr 'start' to the end of sect "
708 "w/addr 'start'+'len'-1\n"
709 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
710 "erase bank N\n - erase FLASH bank # N\n"
711 TMP_ERASE
712 "erase all\n - erase all FLASH banks"
713);
714
715U_BOOT_CMD(
716 protect, 4, 0, do_protect,
717 "enable or disable FLASH write protection",
718 "on start end\n"
719 " - protect FLASH from addr 'start' to addr 'end'\n"
720 "protect on start +len\n"
721 " - protect FLASH from addr 'start' to end of sect "
722 "w/addr 'start'+'len'-1\n"
723 "protect on N:SF[-SL]\n"
724 " - protect sectors SF-SL in FLASH bank # N\n"
725 "protect on bank N\n - protect FLASH bank # N\n"
726 TMP_PROT_ON
727 "protect on all\n - protect all FLASH banks\n"
728 "protect off start end\n"
729 " - make FLASH from addr 'start' to addr 'end' writable\n"
730 "protect off start +len\n"
731 " - make FLASH from addr 'start' to end of sect "
732 "w/addr 'start'+'len'-1 wrtable\n"
733 "protect off N:SF[-SL]\n"
734 " - make sectors SF-SL writable in FLASH bank # N\n"
735 "protect off bank N\n - make FLASH bank # N writable\n"
736 TMP_PROT_OFF
737 "protect off all\n - make all FLASH banks writable"
738);
739
740#undef TMP_ERASE
741#undef TMP_PROT_ON
742#undef TMP_PROT_OFF
743