blob: 3773412549bd66c7d2238b3e93a65bfc27154de5 [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 Roese68d7d652009-03-19 13:30:36 +010034#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +020035#include <jffs2/jffs2.h>
36
37/* parition handling routines */
38int mtdparts_init(void);
Stefan Roese68d7d652009-03-19 13:30:36 +010039int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
Wolfgang Denk700a0c62005-08-08 01:03:24 +020040int find_dev_and_part(const char *id, struct mtd_device **dev,
41 u8 *part_num, struct part_info **part);
42#endif
43
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020044#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +000045extern 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
wdenkbdccc4f2003-08-05 17:43:17 +000067abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
wdenk38a24a62002-09-18 12:49:44 +000068{
wdenkbdccc4f2003-08-05 17:43:17 +000069 flash_info_t *fp;
70 int bank, first, last;
71 char *p, *ep;
wdenk38a24a62002-09-18 12:49:44 +000072
wdenkbdccc4f2003-08-05 17:43:17 +000073 if ((p = strchr (str, ':')) == NULL)
74 return 0;
wdenk38a24a62002-09-18 12:49:44 +000075 *p++ = '\0';
76
wdenkbdccc4f2003-08-05 17:43:17 +000077 bank = simple_strtoul (str, &ep, 10);
78 if (ep == str || *ep != '\0' ||
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020079 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
wdenkbdccc4f2003-08-05 17:43:17 +000080 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
81 return -1;
wdenk38a24a62002-09-18 12:49:44 +000082
wdenkbdccc4f2003-08-05 17:43:17 +000083 str = p;
84 if ((p = strchr (str, '-')) != NULL)
85 *p++ = '\0';
wdenk38a24a62002-09-18 12:49:44 +000086
wdenkbdccc4f2003-08-05 17:43:17 +000087 first = simple_strtoul (str, &ep, 10);
88 if (ep == str || *ep != '\0' || first >= fp->sector_count)
89 return -1;
wdenk38a24a62002-09-18 12:49:44 +000090
wdenkbdccc4f2003-08-05 17:43:17 +000091 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 }
99
100 *pinfo = fp;
101 *psf = first;
102 *psl = last;
103
104 return 1;
wdenk38a24a62002-09-18 12:49:44 +0000105}
wdenkbdccc4f2003-08-05 17:43:17 +0000106
Wolfgang Denkf5301872005-07-21 11:45:50 +0200107/*
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200108 * 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;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200119 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200120 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
148/*
Wolfgang Denkf5301872005-07-21 11:45:50 +0200149 * 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')
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200153 * 2. <cmd> start +length - operate on <'start', start + length)
Wolfgang Denkf5301872005-07-21 11:45:50 +0200154 * 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;
Heiko Schocherbb741402006-04-11 14:39:21 +0200169 char len_used; /* indicates if the "start +length" form used */
Wolfgang Denk2c61f142005-08-05 11:10:31 +0200170
Wolfgang Denkf5301872005-07-21 11:45:50 +0200171 *addr_first = simple_strtoul(arg1, &ep, 16);
172 if (ep == arg1 || *ep != '\0')
173 return -1;
174
Heiko Schocherbb741402006-04-11 14:39:21 +0200175 len_used = 0;
Wolfgang Denkf5301872005-07-21 11:45:50 +0200176 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
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200199 if (flash_sect_roundb(addr_last) > 0)
Wolfgang Denkf5301872005-07-21 11:45:50 +0200200 return -1;
Wolfgang Denkf5301872005-07-21 11:45:50 +0200201 } /* "start +length" from used */
202
203 return 1;
204}
205
wdenkbdccc4f2003-08-05 17:43:17 +0000206static 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
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200217 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenkbdccc4f2003-08-05 17:43:17 +0000218 s_first[bank] = -1; /* first sector to erase */
219 s_last [bank] = -1; /* last sector to erase */
220 }
221
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200222 for (bank=0,info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200223 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
wdenkbdccc4f2003-08-05 17:43:17 +0000224 ++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 */
239
240 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
241
242 if (addr_first > end)
243 continue;
244 if (addr_last < info->start[sect])
245 continue;
246
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 {
wdenk4b9206e2004-03-23 22:14:11 +0000259 puts ("Error: end address"
wdenkbdccc4f2003-08-05 17:43:17 +0000260 " not on sector boundary\n");
261 rcode = 1;
262 break;
263 }
264 }
265 if (s_last[bank] < s_first[bank]) {
wdenk4b9206e2004-03-23 22:14:11 +0000266 puts ("Error: end sector"
wdenkbdccc4f2003-08-05 17:43:17 +0000267 " 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;
wdenke2ffd592004-12-31 09:32:47 +0000274 } 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;
wdenk2d5b5612003-10-14 19:43:55 +0000278 } else if (s_last[bank] >= 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000279 puts ("Error: cannot span across banks when they are"
wdenk2d5b5612003-10-14 19:43:55 +0000280 " mapped in reverse order\n");
281 rcode = 1;
282 break;
wdenkbdccc4f2003-08-05 17:43:17 +0000283 }
284 }
285
286 return rcode;
287}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200288#endif /* CONFIG_SYS_NO_FLASH */
wdenkbdccc4f2003-08-05 17:43:17 +0000289
wdenk38a24a62002-09-18 12:49:44 +0000290int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
291{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200292#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000293 ulong bank;
Stelian Pop880cc432008-03-26 22:52:35 +0100294#endif
wdenk38a24a62002-09-18 12:49:44 +0000295
wdenk2abbe072003-06-16 23:50:08 +0000296#ifdef CONFIG_HAS_DATAFLASH
297 dataflash_print_info();
298#endif
299
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200300#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000301 if (argc == 1) { /* print info for all FLASH banks */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200302 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000303 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);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200311 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000312 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200313 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000314 return 1;
315 }
316 printf ("\nBank # %ld: ", bank);
317 flash_print_info (&flash_info[bank-1]);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200318#endif /* CONFIG_SYS_NO_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000319 return 0;
320}
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200321
wdenk38a24a62002-09-18 12:49:44 +0000322int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
323{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200324#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000325 flash_info_t *info;
326 ulong bank, addr_first, addr_last;
327 int n, sect_first, sect_last;
Stefan Roese68d7d652009-03-19 13:30:36 +0100328#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200329 struct mtd_device *dev;
330 struct part_info *part;
331 u8 dev_type, dev_num, pnum;
332#endif
wdenk38a24a62002-09-18 12:49:44 +0000333 int rcode = 0;
334
335 if (argc < 2) {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600336 cmd_usage(cmdtp);
wdenk38a24a62002-09-18 12:49:44 +0000337 return 1;
338 }
339
340 if (strcmp(argv[1], "all") == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200341 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000342 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) {
wdenk4b9206e2004-03-23 22:14:11 +0000351 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000352 return 1;
353 }
Stefan Roesef2302d42008-08-06 14:05:38 +0200354 printf ("Erase Flash Sectors %d-%d in Bank # %zu ",
wdenk38a24a62002-09-18 12:49:44 +0000355 sect_first, sect_last, (info-flash_info)+1);
356 rcode = flash_erase(info, sect_first, sect_last);
357 return rcode;
358 }
359
Stefan Roese68d7d652009-03-19 13:30:36 +0100360#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200361 /* erase <part-id> - erase partition */
Stefan Roese68d7d652009-03-19 13:30:36 +0100362 if ((argc == 2) && (mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200363 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, "
Kim Phillips4109df62008-07-10 14:00:15 -0500372 "bank %ld, 0x%08lx - 0x%08lx ",
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200373 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
wdenk38a24a62002-09-18 12:49:44 +0000386 if (argc != 3) {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600387 cmd_usage(cmdtp);
wdenk38a24a62002-09-18 12:49:44 +0000388 return 1;
389 }
390
391 if (strcmp(argv[1], "bank") == 0) {
392 bank = simple_strtoul(argv[2], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200393 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000394 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200395 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000396 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
Wolfgang Denkf5301872005-07-21 11:45:50 +0200404 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
405 printf ("Bad address format\n");
406 return 1;
407 }
wdenk38a24a62002-09-18 12:49:44 +0000408
409 if (addr_first >= addr_last) {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600410 cmd_usage(cmdtp);
wdenk38a24a62002-09-18 12:49:44 +0000411 return 1;
412 }
413
wdenk38a24a62002-09-18 12:49:44 +0000414 rcode = flash_sect_erase(addr_first, addr_last);
415 return rcode;
Stelian Pop880cc432008-03-26 22:52:35 +0100416#else
417 return 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200418#endif /* CONFIG_SYS_NO_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000419}
420
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200421#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000422int flash_sect_erase (ulong addr_first, ulong addr_last)
423{
424 flash_info_t *info;
425 ulong bank;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200426#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];
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200428#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200429 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200430#endif
wdenkbdccc4f2003-08-05 17:43:17 +0000431 int erased = 0;
432 int planned;
wdenk38a24a62002-09-18 12:49:44 +0000433 int rcode = 0;
434
wdenkbdccc4f2003-08-05 17:43:17 +0000435 rcode = flash_fill_sect_ranges (addr_first, addr_last,
436 s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000437
wdenkbdccc4f2003-08-05 17:43:17 +0000438 if (planned && (rcode == 0)) {
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200439 for (bank=0,info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200440 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
wdenkbdccc4f2003-08-05 17:43:17 +0000441 ++bank, ++info) {
442 if (s_first[bank]>=0) {
443 erased += s_last[bank] - s_first[bank] + 1;
wdenk013dc8d2003-08-07 14:52:18 +0000444 debug ("Erase Flash from 0x%08lx to 0x%08lx "
wdenkbdccc4f2003-08-05 17:43:17 +0000445 "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]);
wdenk38a24a62002-09-18 12:49:44 +0000452 }
453 }
wdenk38a24a62002-09-18 12:49:44 +0000454 printf ("Erased %d sectors\n", erased);
wdenkbdccc4f2003-08-05 17:43:17 +0000455 } else if (rcode == 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000456 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000457 " not on sector boundary\n");
458 rcode = 1;
459 }
460 return rcode;
461}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200462#endif /* CONFIG_SYS_NO_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000463
wdenk38a24a62002-09-18 12:49:44 +0000464int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
465{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200466#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000467 flash_info_t *info;
Stelian Pop880cc432008-03-26 22:52:35 +0100468 ulong bank;
469 int i, n, sect_first, sect_last;
Wolfgang Denk5669ed42009-07-18 23:18:14 +0200470#endif /* CONFIG_SYS_NO_FLASH */
Wolfgang Denk1a4664b2009-07-19 19:32:37 +0200471#if !defined(CONFIG_SYS_NO_FLASH) || defined(CONFIG_HAS_DATAFLASH)
472 ulong addr_first, addr_last;
473#endif
Stefan Roese68d7d652009-03-19 13:30:36 +0100474#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200475 struct mtd_device *dev;
476 struct part_info *part;
477 u8 dev_type, dev_num, pnum;
478#endif
wdenk5779d8d2003-12-06 23:55:10 +0000479#ifdef CONFIG_HAS_DATAFLASH
480 int status;
481#endif
Wolfgang Denk5669ed42009-07-18 23:18:14 +0200482 int p;
483 int rcode = 0;
Stefan Roese2662b402006-04-01 13:41:03 +0200484
wdenk38a24a62002-09-18 12:49:44 +0000485 if (argc < 3) {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600486 cmd_usage(cmdtp);
wdenk38a24a62002-09-18 12:49:44 +0000487 return 1;
488 }
489
wdenkbdccc4f2003-08-05 17:43:17 +0000490 if (strcmp(argv[1], "off") == 0) {
wdenk38a24a62002-09-18 12:49:44 +0000491 p = 0;
wdenkbdccc4f2003-08-05 17:43:17 +0000492 } else if (strcmp(argv[1], "on") == 0) {
wdenk38a24a62002-09-18 12:49:44 +0000493 p = 1;
wdenkbdccc4f2003-08-05 17:43:17 +0000494 } else {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600495 cmd_usage(cmdtp);
wdenk38a24a62002-09-18 12:49:44 +0000496 return 1;
497 }
498
wdenk5779d8d2003-12-06 23:55:10 +0000499#ifdef CONFIG_HAS_DATAFLASH
500 if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
501 addr_first = simple_strtoul(argv[2], NULL, 16);
502 addr_last = simple_strtoul(argv[3], NULL, 16);
503
504 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
505 status = dataflash_real_protect(p,addr_first,addr_last);
506 if (status < 0){
wdenk4b9206e2004-03-23 22:14:11 +0000507 puts ("Bad DataFlash sector specification\n");
wdenkd4ca31c2004-01-02 14:00:00 +0000508 return 1;
509 }
510 printf("%sProtect %d DataFlash Sectors\n",
511 p ? "" : "Un-", status);
wdenk5779d8d2003-12-06 23:55:10 +0000512 return 0;
513 }
514 }
515#endif
wdenkd4ca31c2004-01-02 14:00:00 +0000516
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200517#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000518 if (strcmp(argv[2], "all") == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200519 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000520 info = &flash_info[bank-1];
521 if (info->flash_id == FLASH_UNKNOWN) {
522 continue;
523 }
524 printf ("%sProtect Flash Bank # %ld\n",
525 p ? "" : "Un-", bank);
526
527 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200528#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000529 if (flash_real_protect(info, i, p))
530 rcode = 1;
531 putc ('.');
532#else
533 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200534#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000535 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200536#if defined(CONFIG_SYS_FLASH_PROTECTION)
Heiko Schocherbb741402006-04-11 14:39:21 +0200537 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200538#endif /* CONFIG_SYS_FLASH_PROTECTION */
Heiko Schocherbb741402006-04-11 14:39:21 +0200539 }
wdenk38a24a62002-09-18 12:49:44 +0000540 return rcode;
541 }
542
543 if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
544 if (n < 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000545 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000546 return 1;
547 }
Stefan Roesef2302d42008-08-06 14:05:38 +0200548 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
wdenk38a24a62002-09-18 12:49:44 +0000549 p ? "" : "Un-", sect_first, sect_last,
550 (info-flash_info)+1);
551 for (i = sect_first; i <= sect_last; i++) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200552#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000553 if (flash_real_protect(info, i, p))
554 rcode = 1;
555 putc ('.');
556#else
557 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200558#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000559 }
560
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200561#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000562 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200563#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000564
565 return rcode;
566 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200567
Stefan Roese68d7d652009-03-19 13:30:36 +0100568#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200569 /* protect on/off <part-id> */
Stefan Roese68d7d652009-03-19 13:30:36 +0100570 if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200571 mtdparts_init();
572 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
573 if (dev->id->type == MTD_DEV_TYPE_NOR) {
574 bank = dev->id->num;
575 info = &flash_info[bank];
576 addr_first = part->offset + info->start[0];
577 addr_last = addr_first + part->size - 1;
578
579 printf ("%sProtect Flash Parition %s, "
Kim Phillips4109df62008-07-10 14:00:15 -0500580 "bank %ld, 0x%08lx - 0x%08lx\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200581 p ? "" : "Un", argv[1],
582 bank, addr_first, addr_last);
583
584 rcode = flash_sect_protect (p, addr_first, addr_last);
585 return rcode;
586 }
587
588 printf("cannot %sprotect, not a NOR device\n",
589 p ? "" : "un");
590 return 1;
591 }
592 }
593#endif
wdenk38a24a62002-09-18 12:49:44 +0000594
595 if (argc != 4) {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600596 cmd_usage(cmdtp);
wdenk38a24a62002-09-18 12:49:44 +0000597 return 1;
598 }
599
600 if (strcmp(argv[2], "bank") == 0) {
601 bank = simple_strtoul(argv[3], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200602 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000603 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200604 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000605 return 1;
606 }
607 printf ("%sProtect Flash Bank # %ld\n",
608 p ? "" : "Un-", bank);
609 info = &flash_info[bank-1];
610
611 if (info->flash_id == FLASH_UNKNOWN) {
wdenk4b9206e2004-03-23 22:14:11 +0000612 puts ("missing or unknown FLASH type\n");
wdenk38a24a62002-09-18 12:49:44 +0000613 return 1;
614 }
615 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200616#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000617 if (flash_real_protect(info, i, p))
618 rcode = 1;
619 putc ('.');
620#else
621 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200622#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000623 }
624
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200625#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000626 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200627#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000628
629 return rcode;
630 }
631
Wolfgang Denkf5301872005-07-21 11:45:50 +0200632 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
633 printf("Bad address format\n");
634 return 1;
635 }
wdenk38a24a62002-09-18 12:49:44 +0000636
637 if (addr_first >= addr_last) {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600638 cmd_usage(cmdtp);
wdenk38a24a62002-09-18 12:49:44 +0000639 return 1;
640 }
641 rcode = flash_sect_protect (p, addr_first, addr_last);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200642#endif /* CONFIG_SYS_NO_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000643 return rcode;
644}
645
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200646#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000647int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
648{
649 flash_info_t *info;
650 ulong bank;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200651#ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT
652 int s_first[CONFIG_SYS_MAX_FLASH_BANKS_DETECT], s_last[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200653#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200654 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200655#endif
wdenk38a24a62002-09-18 12:49:44 +0000656 int protected, i;
wdenkbdccc4f2003-08-05 17:43:17 +0000657 int planned;
658 int rcode;
659
660 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000661
662 protected = 0;
663
wdenkbdccc4f2003-08-05 17:43:17 +0000664 if (planned && (rcode == 0)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200665 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
wdenkbdccc4f2003-08-05 17:43:17 +0000666 if (info->flash_id == FLASH_UNKNOWN) {
wdenk38a24a62002-09-18 12:49:44 +0000667 continue;
wdenkbdccc4f2003-08-05 17:43:17 +0000668 }
wdenk38a24a62002-09-18 12:49:44 +0000669
wdenkbdccc4f2003-08-05 17:43:17 +0000670 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
stroeseabcac872003-12-09 14:58:22 +0000671 debug ("%sProtecting sectors %d..%d in bank %ld\n",
672 p ? "" : "Un-",
wdenkbdccc4f2003-08-05 17:43:17 +0000673 s_first[bank], s_last[bank], bank+1);
674 protected += s_last[bank] - s_first[bank] + 1;
675 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200676#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkbdccc4f2003-08-05 17:43:17 +0000677 if (flash_real_protect(info, i, p))
678 rcode = 1;
679 putc ('.');
wdenk38a24a62002-09-18 12:49:44 +0000680#else
wdenkbdccc4f2003-08-05 17:43:17 +0000681 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200682#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenkbdccc4f2003-08-05 17:43:17 +0000683 }
wdenk38a24a62002-09-18 12:49:44 +0000684 }
wdenkbdccc4f2003-08-05 17:43:17 +0000685 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200686#if defined(CONFIG_SYS_FLASH_PROTECTION)
Stefan Roese2662b402006-04-01 13:41:03 +0200687 puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200688#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000689
wdenk38a24a62002-09-18 12:49:44 +0000690 printf ("%sProtected %d sectors\n",
691 p ? "" : "Un-", protected);
wdenkbdccc4f2003-08-05 17:43:17 +0000692 } else if (rcode == 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000693 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000694 " not on sector boundary\n");
695 rcode = 1;
696 }
697 return rcode;
698}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200699#endif /* CONFIG_SYS_NO_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000700
wdenk8bde7f72003-06-27 21:31:46 +0000701
702/**************************************************/
Stefan Roese68d7d652009-03-19 13:30:36 +0100703#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200704# define TMP_ERASE "erase <part-id>\n - erase partition\n"
705# define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
706# define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
707#else
708# define TMP_ERASE /* empty */
709# define TMP_PROT_ON /* empty */
710# define TMP_PROT_OFF /* empty */
711#endif
wdenk8bde7f72003-06-27 21:31:46 +0000712
wdenk0d498392003-07-01 21:06:45 +0000713U_BOOT_CMD(
714 flinfo, 2, 1, do_flinfo,
Peter Tyser2fb26042009-01-27 18:03:12 -0600715 "print FLASH memory information",
wdenk8bde7f72003-06-27 21:31:46 +0000716 "\n - print information for all FLASH memory banks\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200717 "flinfo N\n - print information for FLASH memory bank # N"
wdenk8bde7f72003-06-27 21:31:46 +0000718);
719
wdenk0d498392003-07-01 21:06:45 +0000720U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200721 erase, 3, 0, do_flerase,
Peter Tyser2fb26042009-01-27 18:03:12 -0600722 "erase FLASH memory",
wdenk8bde7f72003-06-27 21:31:46 +0000723 "start end\n"
724 " - erase FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200725 "erase start +len\n"
726 " - erase FLASH from addr 'start' to the end of sect "
727 "w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000728 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
729 "erase bank N\n - erase FLASH bank # N\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200730 TMP_ERASE
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200731 "erase all\n - erase all FLASH banks"
wdenk8bde7f72003-06-27 21:31:46 +0000732);
733
wdenk0d498392003-07-01 21:06:45 +0000734U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200735 protect, 4, 0, do_protect,
Peter Tyser2fb26042009-01-27 18:03:12 -0600736 "enable or disable FLASH write protection",
wdenk8bde7f72003-06-27 21:31:46 +0000737 "on start end\n"
738 " - protect FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200739 "protect on start +len\n"
740 " - protect FLASH from addr 'start' to end of sect "
741 "w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000742 "protect on N:SF[-SL]\n"
743 " - protect sectors SF-SL in FLASH bank # N\n"
744 "protect on bank N\n - protect FLASH bank # N\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200745 TMP_PROT_ON
wdenk8bde7f72003-06-27 21:31:46 +0000746 "protect on all\n - protect all FLASH banks\n"
747 "protect off start end\n"
748 " - make FLASH from addr 'start' to addr 'end' writable\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200749 "protect off start +len\n"
750 " - make FLASH from addr 'start' to end of sect "
751 "w/addr 'start'+'len'-1 wrtable\n"
wdenk8bde7f72003-06-27 21:31:46 +0000752 "protect off N:SF[-SL]\n"
753 " - make sectors SF-SL writable in FLASH bank # N\n"
754 "protect off bank N\n - make FLASH bank # N writable\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200755 TMP_PROT_OFF
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200756 "protect off all\n - make all FLASH banks writable"
wdenk8bde7f72003-06-27 21:31:46 +0000757);
758
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200759#undef TMP_ERASE
760#undef TMP_PROT_ON
761#undef TMP_PROT_OFF