3
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5
* SPDX-License-Identifier: GPL-2.0+
14
#ifdef CONFIG_HAS_DATAFLASH
15
#include <dataflash.h>
18
#if defined(CONFIG_CMD_MTDPARTS)
19
#include <jffs2/jffs2.h>
21
/* partition handling routines */
22
int mtdparts_init(void);
23
int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
24
int find_dev_and_part(const char *id, struct mtd_device **dev,
25
u8 *part_num, struct part_info **part);
28
#ifndef CONFIG_SYS_NO_FLASH
30
#include <mtd/cfi_flash.h>
31
extern flash_info_t flash_info[]; /* info for FLASH chips */
34
* The user interface starts numbering for Flash banks with 1
35
* for historical reasons.
39
* this routine looks for an abbreviated flash range specification.
40
* the syntax is B:SF[-SL], where B is the bank number, SF is the first
41
* sector to erase, and SL is the last sector to erase (defaults to SF).
42
* bank numbers start at 1 to be consistent with other specs, sector numbers
45
* returns: 1 - correct spec; *pinfo, *psf and *psl are
47
* 0 - doesn't look like an abbreviated spec
48
* -1 - looks like an abbreviated spec, but got
49
* a parsing error, a number out of range,
50
* or an invalid flash bank.
53
abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
56
int bank, first, last;
59
if ((p = strchr (str, ':')) == NULL)
63
bank = simple_strtoul (str, &ep, 10);
64
if (ep == str || *ep != '\0' ||
65
bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
66
(fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
70
if ((p = strchr (str, '-')) != NULL)
73
first = simple_strtoul (str, &ep, 10);
74
if (ep == str || *ep != '\0' || first >= fp->sector_count)
78
last = simple_strtoul (p, &ep, 10);
79
if (ep == p || *ep != '\0' ||
80
last < first || last >= fp->sector_count)
94
* Take *addr in Flash and adjust it to fall on the end of its sector
96
int flash_sect_roundb (ulong *addr)
99
ulong bank, sector_end_addr;
103
/* find the end addr of the sector where the *addr is */
105
for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
106
info = &flash_info[bank];
107
for (i = 0; i < info->sector_count && !found; ++i) {
108
/* get the end address of the sector */
109
if (i == info->sector_count - 1) {
110
sector_end_addr = info->start[0] +
113
sector_end_addr = info->start[i+1] - 1;
116
if (*addr <= sector_end_addr &&
117
*addr >= info->start[i]) {
119
/* adjust *addr if necessary */
120
if (*addr < sector_end_addr)
121
*addr = sector_end_addr;
126
/* error, address not in flash */
127
printf("Error: end address (0x%08lx) not in flash!\n", *addr);
135
* This function computes the start and end addresses for both
136
* erase and protect commands. The range of the addresses on which
137
* either of the commands is to operate can be given in two forms:
138
* 1. <cmd> start end - operate on <'start', 'end')
139
* 2. <cmd> start +length - operate on <'start', start + length)
140
* If the second form is used and the end address doesn't fall on the
141
* sector boundary, than it will be adjusted to the next sector boundary.
142
* If it isn't in the flash, the function will fail (return -1).
144
* arg1, arg2: address specification (i.e. both command arguments)
146
* addr_first, addr_last: computed address range
149
* -1: failure (bad format, bad address).
152
addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
155
char len_used; /* indicates if the "start +length" form used */
157
*addr_first = simple_strtoul(arg1, &ep, 16);
158
if (ep == arg1 || *ep != '\0')
162
if (arg2 && *arg2 == '+'){
167
*addr_last = simple_strtoul(arg2, &ep, 16);
168
if (ep == arg2 || *ep != '\0')
173
* *addr_last has the length, compute correct *addr_last
174
* XXX watch out for the integer overflow! Right now it is
175
* checked for in both the callers.
177
*addr_last = *addr_first + *addr_last - 1;
180
* It may happen that *addr_last doesn't fall on the sector
181
* boundary. We want to round such an address to the next
182
* sector boundary, so that the commands don't fail later on.
185
if (flash_sect_roundb(addr_last) > 0)
187
} /* "start +length" from used */
193
flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
194
int *s_first, int *s_last,
203
for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
204
s_first[bank] = -1; /* first sector to erase */
205
s_last [bank] = -1; /* last sector to erase */
208
for (bank=0,info = &flash_info[0];
209
(bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
215
if (info->flash_id == FLASH_UNKNOWN) {
219
b_end = info->start[0] + info->size - 1; /* bank end addr */
220
s_end = info->sector_count - 1; /* last sector */
223
for (sect=0; sect < info->sector_count; ++sect) {
224
ulong end; /* last address in current sect */
226
end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
228
if (addr_first > end)
230
if (addr_last < info->start[sect])
233
if (addr_first == info->start[sect]) {
234
s_first[bank] = sect;
236
if (addr_last == end) {
240
if (s_first[bank] >= 0) {
241
if (s_last[bank] < 0) {
242
if (addr_last > b_end) {
243
s_last[bank] = s_end;
245
puts ("Error: end address"
246
" not on sector boundary\n");
251
if (s_last[bank] < s_first[bank]) {
252
puts ("Error: end sector"
253
" precedes start sector\n");
258
addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
259
(*s_count) += s_last[bank] - s_first[bank] + 1;
260
} else if (addr_first >= info->start[0] && addr_first < b_end) {
261
puts ("Error: start address not on sector boundary\n");
264
} else if (s_last[bank] >= 0) {
265
puts ("Error: cannot span across banks when they are"
266
" mapped in reverse order\n");
274
#endif /* CONFIG_SYS_NO_FLASH */
276
static int do_flinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
278
#ifndef CONFIG_SYS_NO_FLASH
282
#ifdef CONFIG_HAS_DATAFLASH
283
dataflash_print_info();
286
#ifndef CONFIG_SYS_NO_FLASH
287
if (argc == 1) { /* print info for all FLASH banks */
288
for (bank=0; bank <CONFIG_SYS_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 > CONFIG_SYS_MAX_FLASH_BANKS)) {
298
printf ("Only FLASH Banks # 1 ... # %d supported\n",
299
CONFIG_SYS_MAX_FLASH_BANKS);
302
printf ("\nBank # %ld: ", bank);
303
flash_print_info (&flash_info[bank-1]);
304
#endif /* CONFIG_SYS_NO_FLASH */
308
static int do_flerase(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
310
#ifndef CONFIG_SYS_NO_FLASH
311
flash_info_t *info = NULL;
312
ulong bank, addr_first, addr_last;
313
int n, sect_first = 0, sect_last = 0;
314
#if defined(CONFIG_CMD_MTDPARTS)
315
struct mtd_device *dev;
316
struct part_info *part;
317
u8 dev_type, dev_num, pnum;
322
return CMD_RET_USAGE;
324
if (strcmp(argv[1], "all") == 0) {
325
for (bank=1; bank<=CONFIG_SYS_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 # %zu ",
339
sect_first, sect_last, (info-flash_info)+1);
340
rcode = flash_erase(info, sect_first, sect_last);
344
#if defined(CONFIG_CMD_MTDPARTS)
345
/* erase <part-id> - erase partition */
346
if ((argc == 2) && (mtd_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 Partition %s, "
356
"bank %ld, 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
return CMD_RET_USAGE;
373
if (strcmp(argv[1], "bank") == 0) {
374
bank = simple_strtoul(argv[2], NULL, 16);
375
if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
376
printf ("Only FLASH Banks # 1 ... # %d supported\n",
377
CONFIG_SYS_MAX_FLASH_BANKS);
380
printf ("Erase Flash Bank # %ld ", bank);
381
info = &flash_info[bank-1];
382
rcode = flash_erase (info, 0, info->sector_count-1);
386
if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
387
printf ("Bad address format\n");
391
if (addr_first >= addr_last)
392
return CMD_RET_USAGE;
394
rcode = flash_sect_erase(addr_first, addr_last);
398
#endif /* CONFIG_SYS_NO_FLASH */
401
#ifndef CONFIG_SYS_NO_FLASH
402
int flash_sect_erase (ulong addr_first, ulong addr_last)
406
int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
411
rcode = flash_fill_sect_ranges (addr_first, addr_last,
412
s_first, s_last, &planned );
414
if (planned && (rcode == 0)) {
415
for (bank=0,info = &flash_info[0];
416
(bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
418
if (s_first[bank]>=0) {
419
erased += s_last[bank] - s_first[bank] + 1;
420
debug ("Erase Flash from 0x%08lx to 0x%08lx "
422
info->start[s_first[bank]],
423
(s_last[bank] == info->sector_count) ?
424
info->start[0] + info->size - 1:
425
info->start[s_last[bank]+1] - 1,
427
rcode = flash_erase (info, s_first[bank], s_last[bank]);
431
printf("Erased %d sectors\n", erased);
432
} else if (rcode == 0) {
433
puts ("Error: start and/or end address"
434
" not on sector boundary\n");
439
#endif /* CONFIG_SYS_NO_FLASH */
441
static int do_protect(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
444
#ifndef CONFIG_SYS_NO_FLASH
445
flash_info_t *info = NULL;
447
int i, n, sect_first = 0, sect_last = 0;
448
#if defined(CONFIG_CMD_MTDPARTS)
449
struct mtd_device *dev;
450
struct part_info *part;
451
u8 dev_type, dev_num, pnum;
453
#endif /* CONFIG_SYS_NO_FLASH */
454
#ifdef CONFIG_HAS_DATAFLASH
457
#if !defined(CONFIG_SYS_NO_FLASH) || defined(CONFIG_HAS_DATAFLASH)
459
ulong addr_first, addr_last;
463
return CMD_RET_USAGE;
465
#if !defined(CONFIG_SYS_NO_FLASH) || defined(CONFIG_HAS_DATAFLASH)
466
if (strcmp(argv[1], "off") == 0)
468
else if (strcmp(argv[1], "on") == 0)
471
return CMD_RET_USAGE;
474
#ifdef CONFIG_HAS_DATAFLASH
475
if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
476
addr_first = simple_strtoul(argv[2], NULL, 16);
477
addr_last = simple_strtoul(argv[3], NULL, 16);
479
if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
480
status = dataflash_real_protect(p,addr_first,addr_last);
482
puts ("Bad DataFlash sector specification\n");
485
printf("%sProtect %d DataFlash Sectors\n",
486
p ? "" : "Un-", status);
492
#ifndef CONFIG_SYS_NO_FLASH
493
if (strcmp(argv[2], "all") == 0) {
494
for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
495
info = &flash_info[bank-1];
496
if (info->flash_id == FLASH_UNKNOWN) {
499
printf ("%sProtect Flash Bank # %ld\n",
500
p ? "" : "Un-", bank);
502
for (i=0; i<info->sector_count; ++i) {
503
#if defined(CONFIG_SYS_FLASH_PROTECTION)
504
if (flash_real_protect(info, i, p))
508
info->protect[i] = p;
509
#endif /* CONFIG_SYS_FLASH_PROTECTION */
511
#if defined(CONFIG_SYS_FLASH_PROTECTION)
512
if (!rcode) puts (" done\n");
513
#endif /* CONFIG_SYS_FLASH_PROTECTION */
518
if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) {
520
puts ("Bad sector specification\n");
523
printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
524
p ? "" : "Un-", sect_first, sect_last,
525
(info-flash_info)+1);
526
for (i = sect_first; i <= sect_last; i++) {
527
#if defined(CONFIG_SYS_FLASH_PROTECTION)
528
if (flash_real_protect(info, i, p))
532
info->protect[i] = p;
533
#endif /* CONFIG_SYS_FLASH_PROTECTION */
536
#if defined(CONFIG_SYS_FLASH_PROTECTION)
537
if (!rcode) puts (" done\n");
538
#endif /* CONFIG_SYS_FLASH_PROTECTION */
543
#if defined(CONFIG_CMD_MTDPARTS)
544
/* protect on/off <part-id> */
545
if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
547
if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
548
if (dev->id->type == MTD_DEV_TYPE_NOR) {
550
info = &flash_info[bank];
551
addr_first = part->offset + info->start[0];
552
addr_last = addr_first + part->size - 1;
554
printf ("%sProtect Flash Partition %s, "
555
"bank %ld, 0x%08lx - 0x%08lx\n",
556
p ? "" : "Un", argv[1],
557
bank, addr_first, addr_last);
559
rcode = flash_sect_protect (p, addr_first, addr_last);
563
printf("cannot %sprotect, not a NOR device\n",
571
return CMD_RET_USAGE;
573
if (strcmp(argv[2], "bank") == 0) {
574
bank = simple_strtoul(argv[3], NULL, 16);
575
if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
576
printf ("Only FLASH Banks # 1 ... # %d supported\n",
577
CONFIG_SYS_MAX_FLASH_BANKS);
580
printf ("%sProtect Flash Bank # %ld\n",
581
p ? "" : "Un-", bank);
582
info = &flash_info[bank-1];
584
if (info->flash_id == FLASH_UNKNOWN) {
585
puts ("missing or unknown FLASH type\n");
588
for (i=0; i<info->sector_count; ++i) {
589
#if defined(CONFIG_SYS_FLASH_PROTECTION)
590
if (flash_real_protect(info, i, p))
594
info->protect[i] = p;
595
#endif /* CONFIG_SYS_FLASH_PROTECTION */
598
#if defined(CONFIG_SYS_FLASH_PROTECTION)
599
if (!rcode) puts (" done\n");
600
#endif /* CONFIG_SYS_FLASH_PROTECTION */
605
if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
606
printf("Bad address format\n");
610
if (addr_first >= addr_last)
611
return CMD_RET_USAGE;
613
rcode = flash_sect_protect (p, addr_first, addr_last);
614
#endif /* CONFIG_SYS_NO_FLASH */
618
#ifndef CONFIG_SYS_NO_FLASH
619
int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
623
int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
628
rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
632
if (planned && (rcode == 0)) {
633
for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
634
if (info->flash_id == FLASH_UNKNOWN) {
638
if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
639
debug ("%sProtecting sectors %d..%d in bank %ld\n",
641
s_first[bank], s_last[bank], bank+1);
642
protected += s_last[bank] - s_first[bank] + 1;
643
for (i=s_first[bank]; i<=s_last[bank]; ++i) {
644
#if defined(CONFIG_SYS_FLASH_PROTECTION)
645
if (flash_real_protect(info, i, p))
649
info->protect[i] = p;
650
#endif /* CONFIG_SYS_FLASH_PROTECTION */
654
#if defined(CONFIG_SYS_FLASH_PROTECTION)
656
#endif /* CONFIG_SYS_FLASH_PROTECTION */
658
printf ("%sProtected %d sectors\n",
659
p ? "" : "Un-", protected);
660
} else if (rcode == 0) {
661
puts ("Error: start and/or end address"
662
" not on sector boundary\n");
667
#endif /* CONFIG_SYS_NO_FLASH */
670
/**************************************************/
671
#if defined(CONFIG_CMD_MTDPARTS)
672
# define TMP_ERASE "erase <part-id>\n - erase partition\n"
673
# define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
674
# define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
676
# define TMP_ERASE /* empty */
677
# define TMP_PROT_ON /* empty */
678
# define TMP_PROT_OFF /* empty */
682
flinfo, 2, 1, do_flinfo,
683
"print FLASH memory information",
684
"\n - print information for all FLASH memory banks\n"
685
"flinfo N\n - print information for FLASH memory bank # N"
689
erase, 3, 0, do_flerase,
690
"erase FLASH memory",
692
" - erase FLASH from addr 'start' to addr 'end'\n"
694
" - erase FLASH from addr 'start' to the end of sect "
695
"w/addr 'start'+'len'-1\n"
696
"erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
697
"erase bank N\n - erase FLASH bank # N\n"
699
"erase all\n - erase all FLASH banks"
703
protect, 4, 0, do_protect,
704
"enable or disable FLASH write protection",
706
" - protect FLASH from addr 'start' to addr 'end'\n"
707
"protect on start +len\n"
708
" - protect FLASH from addr 'start' to end of sect "
709
"w/addr 'start'+'len'-1\n"
710
"protect on N:SF[-SL]\n"
711
" - protect sectors SF-SL in FLASH bank # N\n"
712
"protect on bank N\n - protect FLASH bank # N\n"
714
"protect on all\n - protect all FLASH banks\n"
715
"protect off start end\n"
716
" - make FLASH from addr 'start' to addr 'end' writable\n"
717
"protect off start +len\n"
718
" - make FLASH from addr 'start' to end of sect "
719
"w/addr 'start'+'len'-1 wrtable\n"
720
"protect off N:SF[-SL]\n"
721
" - make sectors SF-SL writable in FLASH bank # N\n"
722
"protect off bank N\n - make FLASH bank # N writable\n"
724
"protect off all\n - make all FLASH banks writable"