]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_flash.c
Standardize command usage messages with cmd_usage()
[people/ms/u-boot.git] / common / cmd_flash.c
CommitLineData
38a24a61
WD
1/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * FLASH support
26 */
27#include <common.h>
28#include <command.h>
8bde7f77 29
2abbe075
WD
30#ifdef CONFIG_HAS_DATAFLASH
31#include <dataflash.h>
32#endif
33
3865b1fb 34#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
700a0c64
WD
35#include <jffs2/jffs2.h>
36
37/* parition handling routines */
38int mtdparts_init(void);
39int 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
6d0f6bcf 44#ifndef CONFIG_SYS_NO_FLASH
38a24a61
WD
45extern flash_info_t flash_info[]; /* info for FLASH chips */
46
47/*
48 * The user interface starts numbering for Flash banks with 1
49 * for historical reasons.
50 */
51
52/*
53 * this routine looks for an abbreviated flash range specification.
54 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
55 * sector to erase, and SL is the last sector to erase (defaults to SF).
56 * bank numbers start at 1 to be consistent with other specs, sector numbers
57 * start at zero.
58 *
59 * returns: 1 - correct spec; *pinfo, *psf and *psl are
60 * set appropriately
61 * 0 - doesn't look like an abbreviated spec
62 * -1 - looks like an abbreviated spec, but got
63 * a parsing error, a number out of range,
64 * or an invalid flash bank.
65 */
66static int
bdccc4fe 67abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
38a24a61 68{
bdccc4fe
WD
69 flash_info_t *fp;
70 int bank, first, last;
71 char *p, *ep;
38a24a61 72
bdccc4fe
WD
73 if ((p = strchr (str, ':')) == NULL)
74 return 0;
75 *p++ = '\0';
38a24a61 76
bdccc4fe
WD
77 bank = simple_strtoul (str, &ep, 10);
78 if (ep == str || *ep != '\0' ||
6d0f6bcf 79 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
bdccc4fe
WD
80 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
81 return -1;
82
83 str = p;
84 if ((p = strchr (str, '-')) != NULL)
85 *p++ = '\0';
86
87 first = simple_strtoul (str, &ep, 10);
88 if (ep == str || *ep != '\0' || first >= fp->sector_count)
89 return -1;
90
91 if (p != NULL) {
92 last = simple_strtoul (p, &ep, 10);
93 if (ep == p || *ep != '\0' ||
94 last < first || last >= fp->sector_count)
95 return -1;
96 } else {
97 last = first;
98 }
38a24a61 99
bdccc4fe
WD
100 *pinfo = fp;
101 *psf = first;
102 *psl = last;
103
104 return 1;
105}
106
3f0cf51d
BS
107/*
108 * Take *addr in Flash and adjust it to fall on the end of its sector
109 */
110int flash_sect_roundb (ulong *addr)
111{
112 flash_info_t *info;
113 ulong bank, sector_end_addr;
114 char found;
115 int i;
116
117 /* find the end addr of the sector where the *addr is */
118 found = 0;
6d0f6bcf 119 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
3f0cf51d
BS
120 info = &flash_info[bank];
121 for (i = 0; i < info->sector_count && !found; ++i) {
122 /* get the end address of the sector */
123 if (i == info->sector_count - 1) {
124 sector_end_addr = info->start[0] +
125 info->size - 1;
126 } else {
127 sector_end_addr = info->start[i+1] - 1;
128 }
129
130 if (*addr <= sector_end_addr &&
131 *addr >= info->start[i]) {
132 found = 1;
133 /* adjust *addr if necessary */
134 if (*addr < sector_end_addr)
135 *addr = sector_end_addr;
136 } /* sector */
137 } /* bank */
138 }
139 if (!found) {
140 /* error, addres not in flash */
141 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
142 return 1;
143 }
144
145 return 0;
146}
147
f530187d
WD
148/*
149 * This function computes the start and end addresses for both
150 * erase and protect commands. The range of the addresses on which
151 * either of the commands is to operate can be given in two forms:
152 * 1. <cmd> start end - operate on <'start', 'end')
095b8a37 153 * 2. <cmd> start +length - operate on <'start', start + length)
f530187d
WD
154 * If the second form is used and the end address doesn't fall on the
155 * sector boundary, than it will be adjusted to the next sector boundary.
156 * If it isn't in the flash, the function will fail (return -1).
157 * Input:
158 * arg1, arg2: address specification (i.e. both command arguments)
159 * Output:
160 * addr_first, addr_last: computed address range
161 * Return:
162 * 1: success
163 * -1: failure (bad format, bad address).
164*/
165static int
166addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
167{
168 char *ep;
bb74140d 169 char len_used; /* indicates if the "start +length" form used */
2c61f14c 170
f530187d
WD
171 *addr_first = simple_strtoul(arg1, &ep, 16);
172 if (ep == arg1 || *ep != '\0')
173 return -1;
174
bb74140d 175 len_used = 0;
f530187d
WD
176 if (arg2 && *arg2 == '+'){
177 len_used = 1;
178 ++arg2;
179 }
180
181 *addr_last = simple_strtoul(arg2, &ep, 16);
182 if (ep == arg2 || *ep != '\0')
183 return -1;
184
185 if (len_used){
186 /*
187 * *addr_last has the length, compute correct *addr_last
188 * XXX watch out for the integer overflow! Right now it is
189 * checked for in both the callers.
190 */
191 *addr_last = *addr_first + *addr_last - 1;
192
193 /*
194 * It may happen that *addr_last doesn't fall on the sector
195 * boundary. We want to round such an address to the next
196 * sector boundary, so that the commands don't fail later on.
197 */
198
3f0cf51d 199 if (flash_sect_roundb(addr_last) > 0)
f530187d 200 return -1;
f530187d
WD
201 } /* "start +length" from used */
202
203 return 1;
204}
205
bdccc4fe
WD
206static int
207flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
208 int *s_first, int *s_last,
209 int *s_count )
210{
211 flash_info_t *info;
212 ulong bank;
213 int rcode = 0;
214
215 *s_count = 0;
216
6d0f6bcf 217 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
bdccc4fe
WD
218 s_first[bank] = -1; /* first sector to erase */
219 s_last [bank] = -1; /* last sector to erase */
220 }
221
d0ff51ba 222 for (bank=0,info = &flash_info[0];
6d0f6bcf 223 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
bdccc4fe
WD
224 ++bank, ++info) {
225 ulong b_end;
226 int sect;
227 short s_end;
228
229 if (info->flash_id == FLASH_UNKNOWN) {
230 continue;
231 }
232
233 b_end = info->start[0] + info->size - 1; /* bank end addr */
234 s_end = info->sector_count - 1; /* last sector */
235
236
237 for (sect=0; sect < info->sector_count; ++sect) {
238 ulong end; /* last address in current sect */
38a24a61 239
bdccc4fe 240 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
38a24a61 241
bdccc4fe
WD
242 if (addr_first > end)
243 continue;
244 if (addr_last < info->start[sect])
245 continue;
38a24a61 246
bdccc4fe
WD
247 if (addr_first == info->start[sect]) {
248 s_first[bank] = sect;
249 }
250 if (addr_last == end) {
251 s_last[bank] = sect;
252 }
253 }
254 if (s_first[bank] >= 0) {
255 if (s_last[bank] < 0) {
256 if (addr_last > b_end) {
257 s_last[bank] = s_end;
258 } else {
4b9206ed 259 puts ("Error: end address"
bdccc4fe
WD
260 " not on sector boundary\n");
261 rcode = 1;
262 break;
263 }
264 }
265 if (s_last[bank] < s_first[bank]) {
4b9206ed 266 puts ("Error: end sector"
bdccc4fe
WD
267 " precedes start sector\n");
268 rcode = 1;
269 break;
270 }
271 sect = s_last[bank];
272 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
273 (*s_count) += s_last[bank] - s_first[bank] + 1;
e2ffd59b
WD
274 } else if (addr_first >= info->start[0] && addr_first < b_end) {
275 puts ("Error: start address not on sector boundary\n");
276 rcode = 1;
277 break;
2d5b561e 278 } else if (s_last[bank] >= 0) {
4b9206ed 279 puts ("Error: cannot span across banks when they are"
2d5b561e
WD
280 " mapped in reverse order\n");
281 rcode = 1;
282 break;
bdccc4fe
WD
283 }
284 }
38a24a61 285
bdccc4fe 286 return rcode;
38a24a61 287}
6d0f6bcf 288#endif /* CONFIG_SYS_NO_FLASH */
bdccc4fe 289
38a24a61
WD
290int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
291{
6d0f6bcf 292#ifndef CONFIG_SYS_NO_FLASH
38a24a61 293 ulong bank;
880cc438 294#endif
38a24a61 295
2abbe075
WD
296#ifdef CONFIG_HAS_DATAFLASH
297 dataflash_print_info();
298#endif
299
6d0f6bcf 300#ifndef CONFIG_SYS_NO_FLASH
38a24a61 301 if (argc == 1) { /* print info for all FLASH banks */
6d0f6bcf 302 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
38a24a61
WD
303 printf ("\nBank # %ld: ", bank+1);
304
305 flash_print_info (&flash_info[bank]);
306 }
307 return 0;
308 }
309
310 bank = simple_strtoul(argv[1], NULL, 16);
6d0f6bcf 311 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
38a24a61 312 printf ("Only FLASH Banks # 1 ... # %d supported\n",
6d0f6bcf 313 CONFIG_SYS_MAX_FLASH_BANKS);
38a24a61
WD
314 return 1;
315 }
316 printf ("\nBank # %ld: ", bank);
317 flash_print_info (&flash_info[bank-1]);
6d0f6bcf 318#endif /* CONFIG_SYS_NO_FLASH */
38a24a61
WD
319 return 0;
320}
700a0c64 321
38a24a61
WD
322int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
323{
6d0f6bcf 324#ifndef CONFIG_SYS_NO_FLASH
38a24a61
WD
325 flash_info_t *info;
326 ulong bank, addr_first, addr_last;
327 int n, sect_first, sect_last;
baa26db4 328#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
700a0c64
WD
329 struct mtd_device *dev;
330 struct part_info *part;
331 u8 dev_type, dev_num, pnum;
332#endif
38a24a61
WD
333 int rcode = 0;
334
335 if (argc < 2) {
62c3ae7c 336 cmd_usage(cmdtp);
38a24a61
WD
337 return 1;
338 }
339
340 if (strcmp(argv[1], "all") == 0) {
6d0f6bcf 341 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
38a24a61
WD
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, &sect_first, &sect_last)) != 0) {
350 if (n < 0) {
4b9206ed 351 puts ("Bad sector specification\n");
38a24a61
WD
352 return 1;
353 }
f2302d44 354 printf ("Erase Flash Sectors %d-%d in Bank # %zu ",
38a24a61
WD
355 sect_first, sect_last, (info-flash_info)+1);
356 rcode = flash_erase(info, sect_first, sect_last);
357 return rcode;
358 }
359
baa26db4 360#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
700a0c64
WD
361 /* erase <part-id> - erase partition */
362 if ((argc == 2) && (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 Parition %s, "
4109df6f 372 "bank %ld, 0x%08lx - 0x%08lx ",
700a0c64
WD
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
38a24a61 386 if (argc != 3) {
62c3ae7c 387 cmd_usage(cmdtp);
38a24a61
WD
388 return 1;
389 }
390
391 if (strcmp(argv[1], "bank") == 0) {
392 bank = simple_strtoul(argv[2], NULL, 16);
6d0f6bcf 393 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
38a24a61 394 printf ("Only FLASH Banks # 1 ... # %d supported\n",
6d0f6bcf 395 CONFIG_SYS_MAX_FLASH_BANKS);
38a24a61
WD
396 return 1;
397 }
398 printf ("Erase Flash Bank # %ld ", bank);
399 info = &flash_info[bank-1];
400 rcode = flash_erase (info, 0, info->sector_count-1);
401 return rcode;
402 }
403
f530187d
WD
404 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
405 printf ("Bad address format\n");
406 return 1;
407 }
38a24a61
WD
408
409 if (addr_first >= addr_last) {
62c3ae7c 410 cmd_usage(cmdtp);
38a24a61
WD
411 return 1;
412 }
413
38a24a61
WD
414 rcode = flash_sect_erase(addr_first, addr_last);
415 return rcode;
880cc438
SP
416#else
417 return 0;
6d0f6bcf 418#endif /* CONFIG_SYS_NO_FLASH */
38a24a61
WD
419}
420
6d0f6bcf 421#ifndef CONFIG_SYS_NO_FLASH
38a24a61
WD
422int flash_sect_erase (ulong addr_first, ulong addr_last)
423{
424 flash_info_t *info;
425 ulong bank;
6d0f6bcf
JCPV
426#ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT
427 int s_first[CONFIG_SYS_MAX_FLASH_BANKS_DETECT], s_last[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
e6f2e902 428#else
6d0f6bcf 429 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
e6f2e902 430#endif
bdccc4fe
WD
431 int erased = 0;
432 int planned;
38a24a61
WD
433 int rcode = 0;
434
bdccc4fe
WD
435 rcode = flash_fill_sect_ranges (addr_first, addr_last,
436 s_first, s_last, &planned );
437
438 if (planned && (rcode == 0)) {
d0ff51ba 439 for (bank=0,info = &flash_info[0];
6d0f6bcf 440 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
bdccc4fe
WD
441 ++bank, ++info) {
442 if (s_first[bank]>=0) {
443 erased += s_last[bank] - s_first[bank] + 1;
013dc8d9 444 debug ("Erase Flash from 0x%08lx to 0x%08lx "
bdccc4fe
WD
445 "in Bank # %ld ",
446 info->start[s_first[bank]],
447 (s_last[bank] == info->sector_count) ?
448 info->start[0] + info->size - 1:
449 info->start[s_last[bank]+1] - 1,
450 bank+1);
451 rcode = flash_erase (info, s_first[bank], s_last[bank]);
38a24a61
WD
452 }
453 }
38a24a61 454 printf ("Erased %d sectors\n", erased);
bdccc4fe 455 } else if (rcode == 0) {
4b9206ed 456 puts ("Error: start and/or end address"
38a24a61
WD
457 " not on sector boundary\n");
458 rcode = 1;
459 }
460 return rcode;
461}
6d0f6bcf 462#endif /* CONFIG_SYS_NO_FLASH */
38a24a61 463
38a24a61
WD
464int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
465{
6d0f6bcf 466#ifndef CONFIG_SYS_NO_FLASH
38a24a61 467 flash_info_t *info;
880cc438
SP
468 ulong bank;
469 int i, n, sect_first, sect_last;
6d0f6bcf 470#endif /* CONFIG_SYS_NO_FLASH */
880cc438
SP
471 ulong addr_first, addr_last;
472 int p;
baa26db4 473#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
700a0c64
WD
474 struct mtd_device *dev;
475 struct part_info *part;
476 u8 dev_type, dev_num, pnum;
477#endif
38a24a61 478 int rcode = 0;
5779d8d9
WD
479#ifdef CONFIG_HAS_DATAFLASH
480 int status;
481#endif
2662b40c 482
38a24a61 483 if (argc < 3) {
62c3ae7c 484 cmd_usage(cmdtp);
38a24a61
WD
485 return 1;
486 }
487
bdccc4fe 488 if (strcmp(argv[1], "off") == 0) {
38a24a61 489 p = 0;
bdccc4fe 490 } else if (strcmp(argv[1], "on") == 0) {
38a24a61 491 p = 1;
bdccc4fe 492 } else {
62c3ae7c 493 cmd_usage(cmdtp);
38a24a61
WD
494 return 1;
495 }
496
5779d8d9
WD
497#ifdef CONFIG_HAS_DATAFLASH
498 if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
499 addr_first = simple_strtoul(argv[2], NULL, 16);
500 addr_last = simple_strtoul(argv[3], NULL, 16);
501
502 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
503 status = dataflash_real_protect(p,addr_first,addr_last);
504 if (status < 0){
4b9206ed 505 puts ("Bad DataFlash sector specification\n");
d4ca31c4
WD
506 return 1;
507 }
508 printf("%sProtect %d DataFlash Sectors\n",
509 p ? "" : "Un-", status);
5779d8d9
WD
510 return 0;
511 }
512 }
513#endif
d4ca31c4 514
6d0f6bcf 515#ifndef CONFIG_SYS_NO_FLASH
38a24a61 516 if (strcmp(argv[2], "all") == 0) {
6d0f6bcf 517 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
38a24a61
WD
518 info = &flash_info[bank-1];
519 if (info->flash_id == FLASH_UNKNOWN) {
520 continue;
521 }
522 printf ("%sProtect Flash Bank # %ld\n",
523 p ? "" : "Un-", bank);
524
525 for (i=0; i<info->sector_count; ++i) {
6d0f6bcf 526#if defined(CONFIG_SYS_FLASH_PROTECTION)
38a24a61
WD
527 if (flash_real_protect(info, i, p))
528 rcode = 1;
529 putc ('.');
530#else
531 info->protect[i] = p;
6d0f6bcf 532#endif /* CONFIG_SYS_FLASH_PROTECTION */
38a24a61 533 }
6d0f6bcf 534#if defined(CONFIG_SYS_FLASH_PROTECTION)
bb74140d 535 if (!rcode) puts (" done\n");
6d0f6bcf 536#endif /* CONFIG_SYS_FLASH_PROTECTION */
bb74140d 537 }
38a24a61
WD
538 return rcode;
539 }
540
541 if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
542 if (n < 0) {
4b9206ed 543 puts ("Bad sector specification\n");
38a24a61
WD
544 return 1;
545 }
f2302d44 546 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
38a24a61
WD
547 p ? "" : "Un-", sect_first, sect_last,
548 (info-flash_info)+1);
549 for (i = sect_first; i <= sect_last; i++) {
6d0f6bcf 550#if defined(CONFIG_SYS_FLASH_PROTECTION)
38a24a61
WD
551 if (flash_real_protect(info, i, p))
552 rcode = 1;
553 putc ('.');
554#else
555 info->protect[i] = p;
6d0f6bcf 556#endif /* CONFIG_SYS_FLASH_PROTECTION */
38a24a61
WD
557 }
558
6d0f6bcf 559#if defined(CONFIG_SYS_FLASH_PROTECTION)
38a24a61 560 if (!rcode) puts (" done\n");
6d0f6bcf 561#endif /* CONFIG_SYS_FLASH_PROTECTION */
38a24a61
WD
562
563 return rcode;
564 }
8f79e4c2 565
baa26db4 566#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
700a0c64
WD
567 /* protect on/off <part-id> */
568 if ((argc == 3) && (id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
569 mtdparts_init();
570 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
571 if (dev->id->type == MTD_DEV_TYPE_NOR) {
572 bank = dev->id->num;
573 info = &flash_info[bank];
574 addr_first = part->offset + info->start[0];
575 addr_last = addr_first + part->size - 1;
576
577 printf ("%sProtect Flash Parition %s, "
4109df6f 578 "bank %ld, 0x%08lx - 0x%08lx\n",
700a0c64
WD
579 p ? "" : "Un", argv[1],
580 bank, addr_first, addr_last);
581
582 rcode = flash_sect_protect (p, addr_first, addr_last);
583 return rcode;
584 }
585
586 printf("cannot %sprotect, not a NOR device\n",
587 p ? "" : "un");
588 return 1;
589 }
590 }
591#endif
38a24a61
WD
592
593 if (argc != 4) {
62c3ae7c 594 cmd_usage(cmdtp);
38a24a61
WD
595 return 1;
596 }
597
598 if (strcmp(argv[2], "bank") == 0) {
599 bank = simple_strtoul(argv[3], NULL, 16);
6d0f6bcf 600 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
38a24a61 601 printf ("Only FLASH Banks # 1 ... # %d supported\n",
6d0f6bcf 602 CONFIG_SYS_MAX_FLASH_BANKS);
38a24a61
WD
603 return 1;
604 }
605 printf ("%sProtect Flash Bank # %ld\n",
606 p ? "" : "Un-", bank);
607 info = &flash_info[bank-1];
608
609 if (info->flash_id == FLASH_UNKNOWN) {
4b9206ed 610 puts ("missing or unknown FLASH type\n");
38a24a61
WD
611 return 1;
612 }
613 for (i=0; i<info->sector_count; ++i) {
6d0f6bcf 614#if defined(CONFIG_SYS_FLASH_PROTECTION)
38a24a61
WD
615 if (flash_real_protect(info, i, p))
616 rcode = 1;
617 putc ('.');
618#else
619 info->protect[i] = p;
6d0f6bcf 620#endif /* CONFIG_SYS_FLASH_PROTECTION */
38a24a61
WD
621 }
622
6d0f6bcf 623#if defined(CONFIG_SYS_FLASH_PROTECTION)
38a24a61 624 if (!rcode) puts (" done\n");
6d0f6bcf 625#endif /* CONFIG_SYS_FLASH_PROTECTION */
38a24a61
WD
626
627 return rcode;
628 }
629
f530187d
WD
630 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
631 printf("Bad address format\n");
632 return 1;
633 }
38a24a61
WD
634
635 if (addr_first >= addr_last) {
62c3ae7c 636 cmd_usage(cmdtp);
38a24a61
WD
637 return 1;
638 }
639 rcode = flash_sect_protect (p, addr_first, addr_last);
6d0f6bcf 640#endif /* CONFIG_SYS_NO_FLASH */
38a24a61
WD
641 return rcode;
642}
643
6d0f6bcf 644#ifndef CONFIG_SYS_NO_FLASH
38a24a61
WD
645int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
646{
647 flash_info_t *info;
648 ulong bank;
6d0f6bcf
JCPV
649#ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT
650 int s_first[CONFIG_SYS_MAX_FLASH_BANKS_DETECT], s_last[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
e6f2e902 651#else
6d0f6bcf 652 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
e6f2e902 653#endif
38a24a61 654 int protected, i;
bdccc4fe
WD
655 int planned;
656 int rcode;
38a24a61 657
bdccc4fe 658 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
38a24a61 659
bdccc4fe 660 protected = 0;
38a24a61 661
bdccc4fe 662 if (planned && (rcode == 0)) {
6d0f6bcf 663 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
bdccc4fe 664 if (info->flash_id == FLASH_UNKNOWN) {
38a24a61 665 continue;
38a24a61 666 }
bdccc4fe
WD
667
668 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
abcac872
SR
669 debug ("%sProtecting sectors %d..%d in bank %ld\n",
670 p ? "" : "Un-",
bdccc4fe
WD
671 s_first[bank], s_last[bank], bank+1);
672 protected += s_last[bank] - s_first[bank] + 1;
673 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
6d0f6bcf 674#if defined(CONFIG_SYS_FLASH_PROTECTION)
bdccc4fe
WD
675 if (flash_real_protect(info, i, p))
676 rcode = 1;
677 putc ('.');
38a24a61 678#else
bdccc4fe 679 info->protect[i] = p;
6d0f6bcf 680#endif /* CONFIG_SYS_FLASH_PROTECTION */
bdccc4fe 681 }
38a24a61 682 }
2662b40c 683 }
6d0f6bcf 684#if defined(CONFIG_SYS_FLASH_PROTECTION)
2662b40c 685 puts (" done\n");
6d0f6bcf 686#endif /* CONFIG_SYS_FLASH_PROTECTION */
38a24a61 687
38a24a61
WD
688 printf ("%sProtected %d sectors\n",
689 p ? "" : "Un-", protected);
bdccc4fe 690 } else if (rcode == 0) {
4b9206ed 691 puts ("Error: start and/or end address"
38a24a61
WD
692 " not on sector boundary\n");
693 rcode = 1;
694 }
695 return rcode;
696}
6d0f6bcf 697#endif /* CONFIG_SYS_NO_FLASH */
38a24a61 698
8bde7f77
WD
699
700/**************************************************/
baa26db4 701#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
c19c3134
WD
702# define TMP_ERASE "erase <part-id>\n - erase partition\n"
703# define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
704# define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
705#else
706# define TMP_ERASE /* empty */
707# define TMP_PROT_ON /* empty */
708# define TMP_PROT_OFF /* empty */
709#endif
8bde7f77 710
0d498393
WD
711U_BOOT_CMD(
712 flinfo, 2, 1, do_flinfo,
8bde7f77
WD
713 "flinfo - print FLASH memory information\n",
714 "\n - print information for all FLASH memory banks\n"
715 "flinfo N\n - print information for FLASH memory bank # N\n"
716);
717
0d498393 718U_BOOT_CMD(
9912121f 719 erase, 3, 0, do_flerase,
8bde7f77
WD
720 "erase - erase FLASH memory\n",
721 "start end\n"
722 " - erase FLASH from addr 'start' to addr 'end'\n"
f530187d
WD
723 "erase start +len\n"
724 " - erase FLASH from addr 'start' to the end of sect "
725 "w/addr 'start'+'len'-1\n"
8bde7f77
WD
726 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
727 "erase bank N\n - erase FLASH bank # N\n"
c19c3134 728 TMP_ERASE
8bde7f77
WD
729 "erase all\n - erase all FLASH banks\n"
730);
731
0d498393 732U_BOOT_CMD(
9912121f 733 protect, 4, 0, do_protect,
8bde7f77
WD
734 "protect - enable or disable FLASH write protection\n",
735 "on start end\n"
736 " - protect FLASH from addr 'start' to addr 'end'\n"
f530187d
WD
737 "protect on start +len\n"
738 " - protect FLASH from addr 'start' to end of sect "
739 "w/addr 'start'+'len'-1\n"
8bde7f77
WD
740 "protect on N:SF[-SL]\n"
741 " - protect sectors SF-SL in FLASH bank # N\n"
742 "protect on bank N\n - protect FLASH bank # N\n"
c19c3134 743 TMP_PROT_ON
8bde7f77
WD
744 "protect on all\n - protect all FLASH banks\n"
745 "protect off start end\n"
746 " - make FLASH from addr 'start' to addr 'end' writable\n"
f530187d
WD
747 "protect off start +len\n"
748 " - make FLASH from addr 'start' to end of sect "
749 "w/addr 'start'+'len'-1 wrtable\n"
8bde7f77
WD
750 "protect off N:SF[-SL]\n"
751 " - make sectors SF-SL writable in FLASH bank # N\n"
752 "protect off bank N\n - make FLASH bank # N writable\n"
c19c3134 753 TMP_PROT_OFF
8bde7f77
WD
754 "protect off all\n - make all FLASH banks writable\n"
755);
756
c19c3134
WD
757#undef TMP_ERASE
758#undef TMP_PROT_ON
759#undef TMP_PROT_OFF