blob: f56443e25e3a80ef03b0a46a17369d55a1c0665f [file] [log] [blame]
wdenk38a24a62002-09-18 12:49:44 +00001/*
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>
wdenk8bde7f72003-06-27 21:31:46 +000029
wdenk2abbe072003-06-16 23:50:08 +000030#ifdef CONFIG_HAS_DATAFLASH
31#include <dataflash.h>
32#endif
33
Stefan Roese3865b1f2007-07-11 12:13:53 +020034#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Wolfgang Denk700a0c62005-08-08 01:03:24 +020035#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
wdenk38a24a62002-09-18 12:49:44 +000044extern flash_info_t flash_info[]; /* info for FLASH chips */
45
46/*
47 * The user interface starts numbering for Flash banks with 1
48 * for historical reasons.
49 */
50
51/*
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
56 * start at zero.
57 *
58 * returns: 1 - correct spec; *pinfo, *psf and *psl are
59 * set appropriately
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.
64 */
65static int
wdenkbdccc4f2003-08-05 17:43:17 +000066abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
wdenk38a24a62002-09-18 12:49:44 +000067{
wdenkbdccc4f2003-08-05 17:43:17 +000068 flash_info_t *fp;
69 int bank, first, last;
70 char *p, *ep;
wdenk38a24a62002-09-18 12:49:44 +000071
wdenkbdccc4f2003-08-05 17:43:17 +000072 if ((p = strchr (str, ':')) == NULL)
73 return 0;
wdenk38a24a62002-09-18 12:49:44 +000074 *p++ = '\0';
75
wdenkbdccc4f2003-08-05 17:43:17 +000076 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)
80 return -1;
wdenk38a24a62002-09-18 12:49:44 +000081
wdenkbdccc4f2003-08-05 17:43:17 +000082 str = p;
83 if ((p = strchr (str, '-')) != NULL)
84 *p++ = '\0';
wdenk38a24a62002-09-18 12:49:44 +000085
wdenkbdccc4f2003-08-05 17:43:17 +000086 first = simple_strtoul (str, &ep, 10);
87 if (ep == str || *ep != '\0' || first >= fp->sector_count)
88 return -1;
wdenk38a24a62002-09-18 12:49:44 +000089
wdenkbdccc4f2003-08-05 17:43:17 +000090 if (p != NULL) {
91 last = simple_strtoul (p, &ep, 10);
92 if (ep == p || *ep != '\0' ||
93 last < first || last >= fp->sector_count)
94 return -1;
95 } else {
96 last = first;
97 }
98
99 *pinfo = fp;
100 *psf = first;
101 *psl = last;
102
103 return 1;
wdenk38a24a62002-09-18 12:49:44 +0000104}
wdenkbdccc4f2003-08-05 17:43:17 +0000105
Wolfgang Denkf5301872005-07-21 11:45:50 +0200106/*
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')
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200111 * 2. <cmd> start +length - operate on <'start', start + length)
Wolfgang Denkf5301872005-07-21 11:45:50 +0200112 * 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).
115 * Input:
116 * arg1, arg2: address specification (i.e. both command arguments)
117 * Output:
118 * addr_first, addr_last: computed address range
119 * Return:
120 * 1: success
121 * -1: failure (bad format, bad address).
122*/
123static int
124addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
125{
126 char *ep;
Heiko Schocherbb741402006-04-11 14:39:21 +0200127 char len_used; /* indicates if the "start +length" form used */
128 char found;
129 ulong bank;
Wolfgang Denk2c61f142005-08-05 11:10:31 +0200130
Wolfgang Denkf5301872005-07-21 11:45:50 +0200131 *addr_first = simple_strtoul(arg1, &ep, 16);
132 if (ep == arg1 || *ep != '\0')
133 return -1;
134
Heiko Schocherbb741402006-04-11 14:39:21 +0200135 len_used = 0;
Wolfgang Denkf5301872005-07-21 11:45:50 +0200136 if (arg2 && *arg2 == '+'){
137 len_used = 1;
138 ++arg2;
139 }
140
141 *addr_last = simple_strtoul(arg2, &ep, 16);
142 if (ep == arg2 || *ep != '\0')
143 return -1;
144
145 if (len_used){
146 /*
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.
150 */
151 *addr_last = *addr_first + *addr_last - 1;
152
153 /*
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.
157 */
158
159 /* find the end addr of the sector where the *addr_last is */
Heiko Schocherbb741402006-04-11 14:39:21 +0200160 found = 0;
Wolfgang Denkf5301872005-07-21 11:45:50 +0200161 for (bank = 0; bank < CFG_MAX_FLASH_BANKS && !found; ++bank){
162 int i;
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){
168 sector_end_addr =
169 info->start[0] + info->size - 1;
170 } else {
171 sector_end_addr =
172 info->start[i+1] - 1;
173 }
174 if (*addr_last <= sector_end_addr &&
175 *addr_last >= info->start[i]){
176 /* sector found */
177 found = 1;
178 /* adjust *addr_last if necessary */
179 if (*addr_last < sector_end_addr){
180 *addr_last = sector_end_addr;
181 }
182 }
183 } /* sector */
184 } /* bank */
185 if (!found){
186 /* error, addres not in flash */
187 printf("Error: end address (0x%08lx) not in flash!\n",
188 *addr_last);
189 return -1;
190 }
191 } /* "start +length" from used */
192
193 return 1;
194}
195
wdenkbdccc4f2003-08-05 17:43:17 +0000196static int
197flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
198 int *s_first, int *s_last,
199 int *s_count )
200{
201 flash_info_t *info;
202 ulong bank;
203 int rcode = 0;
204
205 *s_count = 0;
206
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 */
210 }
211
212 for (bank=0,info=&flash_info[0];
213 (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last);
214 ++bank, ++info) {
215 ulong b_end;
216 int sect;
217 short s_end;
218
219 if (info->flash_id == FLASH_UNKNOWN) {
220 continue;
221 }
222
223 b_end = info->start[0] + info->size - 1; /* bank end addr */
224 s_end = info->sector_count - 1; /* last sector */
225
226
227 for (sect=0; sect < info->sector_count; ++sect) {
228 ulong end; /* last address in current sect */
229
230 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
231
232 if (addr_first > end)
233 continue;
234 if (addr_last < info->start[sect])
235 continue;
236
237 if (addr_first == info->start[sect]) {
238 s_first[bank] = sect;
239 }
240 if (addr_last == end) {
241 s_last[bank] = sect;
242 }
243 }
244 if (s_first[bank] >= 0) {
245 if (s_last[bank] < 0) {
246 if (addr_last > b_end) {
247 s_last[bank] = s_end;
248 } else {
wdenk4b9206e2004-03-23 22:14:11 +0000249 puts ("Error: end address"
wdenkbdccc4f2003-08-05 17:43:17 +0000250 " not on sector boundary\n");
251 rcode = 1;
252 break;
253 }
254 }
255 if (s_last[bank] < s_first[bank]) {
wdenk4b9206e2004-03-23 22:14:11 +0000256 puts ("Error: end sector"
wdenkbdccc4f2003-08-05 17:43:17 +0000257 " precedes start sector\n");
258 rcode = 1;
259 break;
260 }
261 sect = s_last[bank];
262 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
263 (*s_count) += s_last[bank] - s_first[bank] + 1;
wdenke2ffd592004-12-31 09:32:47 +0000264 } else if (addr_first >= info->start[0] && addr_first < b_end) {
265 puts ("Error: start address not on sector boundary\n");
266 rcode = 1;
267 break;
wdenk2d5b5612003-10-14 19:43:55 +0000268 } else if (s_last[bank] >= 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000269 puts ("Error: cannot span across banks when they are"
wdenk2d5b5612003-10-14 19:43:55 +0000270 " mapped in reverse order\n");
271 rcode = 1;
272 break;
wdenkbdccc4f2003-08-05 17:43:17 +0000273 }
274 }
275
276 return rcode;
277}
278
wdenk38a24a62002-09-18 12:49:44 +0000279int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
280{
281 ulong bank;
282
wdenk2abbe072003-06-16 23:50:08 +0000283#ifdef CONFIG_HAS_DATAFLASH
284 dataflash_print_info();
285#endif
286
wdenk38a24a62002-09-18 12:49:44 +0000287 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);
290
291 flash_print_info (&flash_info[bank]);
292 }
293 return 0;
294 }
295
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);
300 return 1;
301 }
302 printf ("\nBank # %ld: ", bank);
303 flash_print_info (&flash_info[bank-1]);
304 return 0;
305}
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200306
wdenk38a24a62002-09-18 12:49:44 +0000307int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
308{
309 flash_info_t *info;
310 ulong bank, addr_first, addr_last;
311 int n, sect_first, sect_last;
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500312#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200313 struct mtd_device *dev;
314 struct part_info *part;
315 u8 dev_type, dev_num, pnum;
316#endif
wdenk38a24a62002-09-18 12:49:44 +0000317 int rcode = 0;
318
319 if (argc < 2) {
320 printf ("Usage:\n%s\n", cmdtp->usage);
321 return 1;
322 }
323
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);
329 }
330 return rcode;
331 }
332
333 if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
334 if (n < 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000335 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000336 return 1;
337 }
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);
341 return rcode;
342 }
343
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500344#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200345 /* erase <part-id> - erase partition */
346 if ((argc == 2) && (id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
347 mtdparts_init();
348 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
349 if (dev->id->type == MTD_DEV_TYPE_NOR) {
350 bank = dev->id->num;
351 info = &flash_info[bank];
352 addr_first = part->offset + info->start[0];
353 addr_last = addr_first + part->size - 1;
354
355 printf ("Erase Flash Parition %s, "
356 "bank %d, 0x%08lx - 0x%08lx ",
357 argv[1], bank, addr_first,
358 addr_last);
359
360 rcode = flash_sect_erase(addr_first, addr_last);
361 return rcode;
362 }
363
364 printf("cannot erase, not a NOR device\n");
365 return 1;
366 }
367 }
368#endif
369
wdenk38a24a62002-09-18 12:49:44 +0000370 if (argc != 3) {
371 printf ("Usage:\n%s\n", cmdtp->usage);
372 return 1;
373 }
374
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);
380 return 1;
381 }
382 printf ("Erase Flash Bank # %ld ", bank);
383 info = &flash_info[bank-1];
384 rcode = flash_erase (info, 0, info->sector_count-1);
385 return rcode;
386 }
387
Wolfgang Denkf5301872005-07-21 11:45:50 +0200388 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
389 printf ("Bad address format\n");
390 return 1;
391 }
wdenk38a24a62002-09-18 12:49:44 +0000392
393 if (addr_first >= addr_last) {
394 printf ("Usage:\n%s\n", cmdtp->usage);
395 return 1;
396 }
397
wdenk38a24a62002-09-18 12:49:44 +0000398 rcode = flash_sect_erase(addr_first, addr_last);
399 return rcode;
400}
401
402int flash_sect_erase (ulong addr_first, ulong addr_last)
403{
404 flash_info_t *info;
405 ulong bank;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200406#ifdef CFG_MAX_FLASH_BANKS_DETECT
407 int s_first[CFG_MAX_FLASH_BANKS_DETECT], s_last[CFG_MAX_FLASH_BANKS_DETECT];
408#else
wdenkbdccc4f2003-08-05 17:43:17 +0000409 int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200410#endif
wdenkbdccc4f2003-08-05 17:43:17 +0000411 int erased = 0;
412 int planned;
wdenk38a24a62002-09-18 12:49:44 +0000413 int rcode = 0;
414
wdenkbdccc4f2003-08-05 17:43:17 +0000415 rcode = flash_fill_sect_ranges (addr_first, addr_last,
416 s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000417
wdenkbdccc4f2003-08-05 17:43:17 +0000418 if (planned && (rcode == 0)) {
419 for (bank=0,info=&flash_info[0];
420 (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
421 ++bank, ++info) {
422 if (s_first[bank]>=0) {
423 erased += s_last[bank] - s_first[bank] + 1;
wdenk013dc8d2003-08-07 14:52:18 +0000424 debug ("Erase Flash from 0x%08lx to 0x%08lx "
wdenkbdccc4f2003-08-05 17:43:17 +0000425 "in Bank # %ld ",
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,
430 bank+1);
431 rcode = flash_erase (info, s_first[bank], s_last[bank]);
wdenk38a24a62002-09-18 12:49:44 +0000432 }
433 }
wdenk38a24a62002-09-18 12:49:44 +0000434 printf ("Erased %d sectors\n", erased);
wdenkbdccc4f2003-08-05 17:43:17 +0000435 } else if (rcode == 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000436 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000437 " not on sector boundary\n");
438 rcode = 1;
439 }
440 return rcode;
441}
442
wdenk38a24a62002-09-18 12:49:44 +0000443int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
444{
445 flash_info_t *info;
446 ulong bank, addr_first, addr_last;
447 int i, p, n, sect_first, sect_last;
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500448#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200449 struct mtd_device *dev;
450 struct part_info *part;
451 u8 dev_type, dev_num, pnum;
452#endif
wdenk38a24a62002-09-18 12:49:44 +0000453 int rcode = 0;
wdenk5779d8d2003-12-06 23:55:10 +0000454#ifdef CONFIG_HAS_DATAFLASH
455 int status;
456#endif
Stefan Roese2662b402006-04-01 13:41:03 +0200457
wdenk38a24a62002-09-18 12:49:44 +0000458 if (argc < 3) {
459 printf ("Usage:\n%s\n", cmdtp->usage);
460 return 1;
461 }
462
wdenkbdccc4f2003-08-05 17:43:17 +0000463 if (strcmp(argv[1], "off") == 0) {
wdenk38a24a62002-09-18 12:49:44 +0000464 p = 0;
wdenkbdccc4f2003-08-05 17:43:17 +0000465 } else if (strcmp(argv[1], "on") == 0) {
wdenk38a24a62002-09-18 12:49:44 +0000466 p = 1;
wdenkbdccc4f2003-08-05 17:43:17 +0000467 } else {
wdenk38a24a62002-09-18 12:49:44 +0000468 printf ("Usage:\n%s\n", cmdtp->usage);
469 return 1;
470 }
471
wdenk5779d8d2003-12-06 23:55:10 +0000472#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);
476
477 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
478 status = dataflash_real_protect(p,addr_first,addr_last);
479 if (status < 0){
wdenk4b9206e2004-03-23 22:14:11 +0000480 puts ("Bad DataFlash sector specification\n");
wdenkd4ca31c2004-01-02 14:00:00 +0000481 return 1;
482 }
483 printf("%sProtect %d DataFlash Sectors\n",
484 p ? "" : "Un-", status);
wdenk5779d8d2003-12-06 23:55:10 +0000485 return 0;
486 }
487 }
488#endif
wdenkd4ca31c2004-01-02 14:00:00 +0000489
wdenk38a24a62002-09-18 12:49:44 +0000490 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) {
494 continue;
495 }
496 printf ("%sProtect Flash Bank # %ld\n",
497 p ? "" : "Un-", bank);
498
499 for (i=0; i<info->sector_count; ++i) {
500#if defined(CFG_FLASH_PROTECTION)
501 if (flash_real_protect(info, i, p))
502 rcode = 1;
503 putc ('.');
504#else
505 info->protect[i] = p;
506#endif /* CFG_FLASH_PROTECTION */
507 }
wdenk38a24a62002-09-18 12:49:44 +0000508#if defined(CFG_FLASH_PROTECTION)
Heiko Schocherbb741402006-04-11 14:39:21 +0200509 if (!rcode) puts (" done\n");
wdenk38a24a62002-09-18 12:49:44 +0000510#endif /* CFG_FLASH_PROTECTION */
Heiko Schocherbb741402006-04-11 14:39:21 +0200511 }
wdenk38a24a62002-09-18 12:49:44 +0000512 return rcode;
513 }
514
515 if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
516 if (n < 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000517 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000518 return 1;
519 }
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))
526 rcode = 1;
527 putc ('.');
528#else
529 info->protect[i] = p;
530#endif /* CFG_FLASH_PROTECTION */
531 }
532
533#if defined(CFG_FLASH_PROTECTION)
534 if (!rcode) puts (" done\n");
535#endif /* CFG_FLASH_PROTECTION */
536
537 return rcode;
538 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200539
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500540#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200541 /* protect on/off <part-id> */
542 if ((argc == 3) && (id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
543 mtdparts_init();
544 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
545 if (dev->id->type == MTD_DEV_TYPE_NOR) {
546 bank = dev->id->num;
547 info = &flash_info[bank];
548 addr_first = part->offset + info->start[0];
549 addr_last = addr_first + part->size - 1;
550
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);
555
556 rcode = flash_sect_protect (p, addr_first, addr_last);
557 return rcode;
558 }
559
560 printf("cannot %sprotect, not a NOR device\n",
561 p ? "" : "un");
562 return 1;
563 }
564 }
565#endif
wdenk38a24a62002-09-18 12:49:44 +0000566
567 if (argc != 4) {
568 printf ("Usage:\n%s\n", cmdtp->usage);
569 return 1;
570 }
571
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);
577 return 1;
578 }
579 printf ("%sProtect Flash Bank # %ld\n",
580 p ? "" : "Un-", bank);
581 info = &flash_info[bank-1];
582
583 if (info->flash_id == FLASH_UNKNOWN) {
wdenk4b9206e2004-03-23 22:14:11 +0000584 puts ("missing or unknown FLASH type\n");
wdenk38a24a62002-09-18 12:49:44 +0000585 return 1;
586 }
587 for (i=0; i<info->sector_count; ++i) {
588#if defined(CFG_FLASH_PROTECTION)
589 if (flash_real_protect(info, i, p))
590 rcode = 1;
591 putc ('.');
592#else
593 info->protect[i] = p;
594#endif /* CFG_FLASH_PROTECTION */
595 }
596
597#if defined(CFG_FLASH_PROTECTION)
598 if (!rcode) puts (" done\n");
599#endif /* CFG_FLASH_PROTECTION */
600
601 return rcode;
602 }
603
Wolfgang Denkf5301872005-07-21 11:45:50 +0200604 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
605 printf("Bad address format\n");
606 return 1;
607 }
wdenk38a24a62002-09-18 12:49:44 +0000608
609 if (addr_first >= addr_last) {
610 printf ("Usage:\n%s\n", cmdtp->usage);
611 return 1;
612 }
613 rcode = flash_sect_protect (p, addr_first, addr_last);
614 return rcode;
615}
616
617
618int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
619{
620 flash_info_t *info;
621 ulong bank;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200622#ifdef CFG_MAX_FLASH_BANKS_DETECT
623 int s_first[CFG_MAX_FLASH_BANKS_DETECT], s_last[CFG_MAX_FLASH_BANKS_DETECT];
624#else
wdenkbdccc4f2003-08-05 17:43:17 +0000625 int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200626#endif
wdenk38a24a62002-09-18 12:49:44 +0000627 int protected, i;
wdenkbdccc4f2003-08-05 17:43:17 +0000628 int planned;
629 int rcode;
630
631 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000632
633 protected = 0;
634
wdenkbdccc4f2003-08-05 17:43:17 +0000635 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) {
wdenk38a24a62002-09-18 12:49:44 +0000638 continue;
wdenkbdccc4f2003-08-05 17:43:17 +0000639 }
wdenk38a24a62002-09-18 12:49:44 +0000640
wdenkbdccc4f2003-08-05 17:43:17 +0000641 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
stroeseabcac872003-12-09 14:58:22 +0000642 debug ("%sProtecting sectors %d..%d in bank %ld\n",
643 p ? "" : "Un-",
wdenkbdccc4f2003-08-05 17:43:17 +0000644 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) {
wdenk38a24a62002-09-18 12:49:44 +0000647#if defined(CFG_FLASH_PROTECTION)
wdenkbdccc4f2003-08-05 17:43:17 +0000648 if (flash_real_protect(info, i, p))
649 rcode = 1;
650 putc ('.');
wdenk38a24a62002-09-18 12:49:44 +0000651#else
wdenkbdccc4f2003-08-05 17:43:17 +0000652 info->protect[i] = p;
wdenk38a24a62002-09-18 12:49:44 +0000653#endif /* CFG_FLASH_PROTECTION */
wdenkbdccc4f2003-08-05 17:43:17 +0000654 }
wdenk38a24a62002-09-18 12:49:44 +0000655 }
wdenkbdccc4f2003-08-05 17:43:17 +0000656 }
Stefan Roese2662b402006-04-01 13:41:03 +0200657#if defined(CFG_FLASH_PROTECTION)
658 puts (" done\n");
659#endif /* CFG_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000660
wdenk38a24a62002-09-18 12:49:44 +0000661 printf ("%sProtected %d sectors\n",
662 p ? "" : "Un-", protected);
wdenkbdccc4f2003-08-05 17:43:17 +0000663 } else if (rcode == 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000664 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000665 " not on sector boundary\n");
666 rcode = 1;
667 }
668 return rcode;
669}
670
wdenk8bde7f72003-06-27 21:31:46 +0000671
672/**************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500673#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200674# 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"
677#else
678# define TMP_ERASE /* empty */
679# define TMP_PROT_ON /* empty */
680# define TMP_PROT_OFF /* empty */
681#endif
wdenk8bde7f72003-06-27 21:31:46 +0000682
wdenk0d498392003-07-01 21:06:45 +0000683U_BOOT_CMD(
684 flinfo, 2, 1, do_flinfo,
wdenk8bde7f72003-06-27 21:31:46 +0000685 "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"
688);
689
wdenk0d498392003-07-01 21:06:45 +0000690U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200691 erase, 3, 0, do_flerase,
wdenk8bde7f72003-06-27 21:31:46 +0000692 "erase - erase FLASH memory\n",
693 "start end\n"
694 " - erase FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200695 "erase start +len\n"
696 " - erase FLASH from addr 'start' to the end of sect "
697 "w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000698 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
699 "erase bank N\n - erase FLASH bank # N\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200700 TMP_ERASE
wdenk8bde7f72003-06-27 21:31:46 +0000701 "erase all\n - erase all FLASH banks\n"
702);
703
wdenk0d498392003-07-01 21:06:45 +0000704U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200705 protect, 4, 0, do_protect,
wdenk8bde7f72003-06-27 21:31:46 +0000706 "protect - enable or disable FLASH write protection\n",
707 "on start end\n"
708 " - protect FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200709 "protect on start +len\n"
710 " - protect FLASH from addr 'start' to end of sect "
711 "w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000712 "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"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200715 TMP_PROT_ON
wdenk8bde7f72003-06-27 21:31:46 +0000716 "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"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200719 "protect off start +len\n"
720 " - make FLASH from addr 'start' to end of sect "
721 "w/addr 'start'+'len'-1 wrtable\n"
wdenk8bde7f72003-06-27 21:31:46 +0000722 "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"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200725 TMP_PROT_OFF
wdenk8bde7f72003-06-27 21:31:46 +0000726 "protect off all\n - make all FLASH banks writable\n"
727);
728
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200729#undef TMP_ERASE
730#undef TMP_PROT_ON
731#undef TMP_PROT_OFF