blob: 687eb68db162878c7fcb67243179f3c14d39c47c [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
Alexander Steinc0008082010-08-11 16:48:04 +020034#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +020035#include <jffs2/jffs2.h>
36
Wolfgang Denk445093d2009-11-17 21:27:39 +010037/* partition handling routines */
Wolfgang Denk700a0c62005-08-08 01:03:24 +020038int 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
Stefan Roese3c299752010-08-31 10:04:11 +020045#include <flash.h>
46#include <mtd/cfi_flash.h>
wdenk38a24a62002-09-18 12:49:44 +000047extern flash_info_t flash_info[]; /* info for FLASH chips */
48
49/*
50 * The user interface starts numbering for Flash banks with 1
51 * for historical reasons.
52 */
53
54/*
55 * this routine looks for an abbreviated flash range specification.
56 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
57 * sector to erase, and SL is the last sector to erase (defaults to SF).
58 * bank numbers start at 1 to be consistent with other specs, sector numbers
59 * start at zero.
60 *
61 * returns: 1 - correct spec; *pinfo, *psf and *psl are
62 * set appropriately
63 * 0 - doesn't look like an abbreviated spec
64 * -1 - looks like an abbreviated spec, but got
65 * a parsing error, a number out of range,
66 * or an invalid flash bank.
67 */
68static int
wdenkbdccc4f2003-08-05 17:43:17 +000069abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
wdenk38a24a62002-09-18 12:49:44 +000070{
wdenkbdccc4f2003-08-05 17:43:17 +000071 flash_info_t *fp;
72 int bank, first, last;
73 char *p, *ep;
wdenk38a24a62002-09-18 12:49:44 +000074
wdenkbdccc4f2003-08-05 17:43:17 +000075 if ((p = strchr (str, ':')) == NULL)
76 return 0;
wdenk38a24a62002-09-18 12:49:44 +000077 *p++ = '\0';
78
wdenkbdccc4f2003-08-05 17:43:17 +000079 bank = simple_strtoul (str, &ep, 10);
80 if (ep == str || *ep != '\0' ||
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020081 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
wdenkbdccc4f2003-08-05 17:43:17 +000082 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
83 return -1;
wdenk38a24a62002-09-18 12:49:44 +000084
wdenkbdccc4f2003-08-05 17:43:17 +000085 str = p;
86 if ((p = strchr (str, '-')) != NULL)
87 *p++ = '\0';
wdenk38a24a62002-09-18 12:49:44 +000088
wdenkbdccc4f2003-08-05 17:43:17 +000089 first = simple_strtoul (str, &ep, 10);
90 if (ep == str || *ep != '\0' || first >= fp->sector_count)
91 return -1;
wdenk38a24a62002-09-18 12:49:44 +000092
wdenkbdccc4f2003-08-05 17:43:17 +000093 if (p != NULL) {
94 last = simple_strtoul (p, &ep, 10);
95 if (ep == p || *ep != '\0' ||
96 last < first || last >= fp->sector_count)
97 return -1;
98 } else {
99 last = first;
100 }
101
102 *pinfo = fp;
103 *psf = first;
104 *psl = last;
105
106 return 1;
wdenk38a24a62002-09-18 12:49:44 +0000107}
wdenkbdccc4f2003-08-05 17:43:17 +0000108
Wolfgang Denkf5301872005-07-21 11:45:50 +0200109/*
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200110 * Take *addr in Flash and adjust it to fall on the end of its sector
111 */
112int flash_sect_roundb (ulong *addr)
113{
114 flash_info_t *info;
115 ulong bank, sector_end_addr;
116 char found;
117 int i;
118
119 /* find the end addr of the sector where the *addr is */
120 found = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200121 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200122 info = &flash_info[bank];
123 for (i = 0; i < info->sector_count && !found; ++i) {
124 /* get the end address of the sector */
125 if (i == info->sector_count - 1) {
126 sector_end_addr = info->start[0] +
127 info->size - 1;
128 } else {
129 sector_end_addr = info->start[i+1] - 1;
130 }
131
132 if (*addr <= sector_end_addr &&
133 *addr >= info->start[i]) {
134 found = 1;
135 /* adjust *addr if necessary */
136 if (*addr < sector_end_addr)
137 *addr = sector_end_addr;
138 } /* sector */
139 } /* bank */
140 }
141 if (!found) {
Mike Williams16263082011-07-22 04:01:30 +0000142 /* error, address not in flash */
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200143 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
144 return 1;
145 }
146
147 return 0;
148}
149
150/*
Wolfgang Denkf5301872005-07-21 11:45:50 +0200151 * This function computes the start and end addresses for both
152 * erase and protect commands. The range of the addresses on which
153 * either of the commands is to operate can be given in two forms:
154 * 1. <cmd> start end - operate on <'start', 'end')
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200155 * 2. <cmd> start +length - operate on <'start', start + length)
Wolfgang Denkf5301872005-07-21 11:45:50 +0200156 * If the second form is used and the end address doesn't fall on the
157 * sector boundary, than it will be adjusted to the next sector boundary.
158 * If it isn't in the flash, the function will fail (return -1).
159 * Input:
160 * arg1, arg2: address specification (i.e. both command arguments)
161 * Output:
162 * addr_first, addr_last: computed address range
163 * Return:
164 * 1: success
165 * -1: failure (bad format, bad address).
166*/
167static int
168addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
169{
170 char *ep;
Heiko Schocherbb741402006-04-11 14:39:21 +0200171 char len_used; /* indicates if the "start +length" form used */
Wolfgang Denk2c61f142005-08-05 11:10:31 +0200172
Wolfgang Denkf5301872005-07-21 11:45:50 +0200173 *addr_first = simple_strtoul(arg1, &ep, 16);
174 if (ep == arg1 || *ep != '\0')
175 return -1;
176
Heiko Schocherbb741402006-04-11 14:39:21 +0200177 len_used = 0;
Wolfgang Denkf5301872005-07-21 11:45:50 +0200178 if (arg2 && *arg2 == '+'){
179 len_used = 1;
180 ++arg2;
181 }
182
183 *addr_last = simple_strtoul(arg2, &ep, 16);
184 if (ep == arg2 || *ep != '\0')
185 return -1;
186
187 if (len_used){
188 /*
189 * *addr_last has the length, compute correct *addr_last
190 * XXX watch out for the integer overflow! Right now it is
191 * checked for in both the callers.
192 */
193 *addr_last = *addr_first + *addr_last - 1;
194
195 /*
196 * It may happen that *addr_last doesn't fall on the sector
197 * boundary. We want to round such an address to the next
198 * sector boundary, so that the commands don't fail later on.
199 */
200
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200201 if (flash_sect_roundb(addr_last) > 0)
Wolfgang Denkf5301872005-07-21 11:45:50 +0200202 return -1;
Wolfgang Denkf5301872005-07-21 11:45:50 +0200203 } /* "start +length" from used */
204
205 return 1;
206}
207
wdenkbdccc4f2003-08-05 17:43:17 +0000208static int
209flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
210 int *s_first, int *s_last,
211 int *s_count )
212{
213 flash_info_t *info;
214 ulong bank;
215 int rcode = 0;
216
217 *s_count = 0;
218
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200219 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenkbdccc4f2003-08-05 17:43:17 +0000220 s_first[bank] = -1; /* first sector to erase */
221 s_last [bank] = -1; /* last sector to erase */
222 }
223
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200224 for (bank=0,info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200225 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
wdenkbdccc4f2003-08-05 17:43:17 +0000226 ++bank, ++info) {
227 ulong b_end;
228 int sect;
229 short s_end;
230
231 if (info->flash_id == FLASH_UNKNOWN) {
232 continue;
233 }
234
235 b_end = info->start[0] + info->size - 1; /* bank end addr */
236 s_end = info->sector_count - 1; /* last sector */
237
238
239 for (sect=0; sect < info->sector_count; ++sect) {
240 ulong end; /* last address in current sect */
241
242 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
243
244 if (addr_first > end)
245 continue;
246 if (addr_last < info->start[sect])
247 continue;
248
249 if (addr_first == info->start[sect]) {
250 s_first[bank] = sect;
251 }
252 if (addr_last == end) {
253 s_last[bank] = sect;
254 }
255 }
256 if (s_first[bank] >= 0) {
257 if (s_last[bank] < 0) {
258 if (addr_last > b_end) {
259 s_last[bank] = s_end;
260 } else {
wdenk4b9206e2004-03-23 22:14:11 +0000261 puts ("Error: end address"
wdenkbdccc4f2003-08-05 17:43:17 +0000262 " not on sector boundary\n");
263 rcode = 1;
264 break;
265 }
266 }
267 if (s_last[bank] < s_first[bank]) {
wdenk4b9206e2004-03-23 22:14:11 +0000268 puts ("Error: end sector"
wdenkbdccc4f2003-08-05 17:43:17 +0000269 " precedes start sector\n");
270 rcode = 1;
271 break;
272 }
273 sect = s_last[bank];
274 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
275 (*s_count) += s_last[bank] - s_first[bank] + 1;
wdenke2ffd592004-12-31 09:32:47 +0000276 } else if (addr_first >= info->start[0] && addr_first < b_end) {
277 puts ("Error: start address not on sector boundary\n");
278 rcode = 1;
279 break;
wdenk2d5b5612003-10-14 19:43:55 +0000280 } else if (s_last[bank] >= 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000281 puts ("Error: cannot span across banks when they are"
wdenk2d5b5612003-10-14 19:43:55 +0000282 " mapped in reverse order\n");
283 rcode = 1;
284 break;
wdenkbdccc4f2003-08-05 17:43:17 +0000285 }
286 }
287
288 return rcode;
289}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200290#endif /* CONFIG_SYS_NO_FLASH */
wdenkbdccc4f2003-08-05 17:43:17 +0000291
Kim Phillips088f1b12012-10-29 13:34:31 +0000292static int do_flinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000293{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200294#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000295 ulong bank;
Stelian Pop880cc432008-03-26 22:52:35 +0100296#endif
wdenk38a24a62002-09-18 12:49:44 +0000297
wdenk2abbe072003-06-16 23:50:08 +0000298#ifdef CONFIG_HAS_DATAFLASH
299 dataflash_print_info();
300#endif
301
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200302#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000303 if (argc == 1) { /* print info for all FLASH banks */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200304 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000305 printf ("\nBank # %ld: ", bank+1);
306
307 flash_print_info (&flash_info[bank]);
308 }
309 return 0;
310 }
311
312 bank = simple_strtoul(argv[1], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200313 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000314 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200315 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000316 return 1;
317 }
318 printf ("\nBank # %ld: ", bank);
319 flash_print_info (&flash_info[bank-1]);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200320#endif /* CONFIG_SYS_NO_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000321 return 0;
322}
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200323
Kim Phillips088f1b12012-10-29 13:34:31 +0000324static int do_flerase(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000325{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200326#ifndef CONFIG_SYS_NO_FLASH
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000327 flash_info_t *info = NULL;
wdenk38a24a62002-09-18 12:49:44 +0000328 ulong bank, addr_first, addr_last;
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000329 int n, sect_first = 0, sect_last = 0;
Alexander Steinc0008082010-08-11 16:48:04 +0200330#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200331 struct mtd_device *dev;
332 struct part_info *part;
333 u8 dev_type, dev_num, pnum;
334#endif
wdenk38a24a62002-09-18 12:49:44 +0000335 int rcode = 0;
336
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200337 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000338 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000339
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
Alexander Steinc0008082010-08-11 16:48:04 +0200360#if 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
Wolfgang Denk445093d2009-11-17 21:27:39 +0100371 printf ("Erase Flash Partition %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
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200386 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000387 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000388
389 if (strcmp(argv[1], "bank") == 0) {
390 bank = simple_strtoul(argv[2], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200391 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000392 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200393 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000394 return 1;
395 }
396 printf ("Erase Flash Bank # %ld ", bank);
397 info = &flash_info[bank-1];
398 rcode = flash_erase (info, 0, info->sector_count-1);
399 return rcode;
400 }
401
Wolfgang Denkf5301872005-07-21 11:45:50 +0200402 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
403 printf ("Bad address format\n");
404 return 1;
405 }
wdenk38a24a62002-09-18 12:49:44 +0000406
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200407 if (addr_first >= addr_last)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000408 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000409
wdenk38a24a62002-09-18 12:49:44 +0000410 rcode = flash_sect_erase(addr_first, addr_last);
411 return rcode;
Stelian Pop880cc432008-03-26 22:52:35 +0100412#else
413 return 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200414#endif /* CONFIG_SYS_NO_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000415}
416
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200417#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000418int flash_sect_erase (ulong addr_first, ulong addr_last)
419{
420 flash_info_t *info;
421 ulong bank;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200422 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
wdenkbdccc4f2003-08-05 17:43:17 +0000423 int erased = 0;
424 int planned;
wdenk38a24a62002-09-18 12:49:44 +0000425 int rcode = 0;
426
wdenkbdccc4f2003-08-05 17:43:17 +0000427 rcode = flash_fill_sect_ranges (addr_first, addr_last,
428 s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000429
wdenkbdccc4f2003-08-05 17:43:17 +0000430 if (planned && (rcode == 0)) {
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200431 for (bank=0,info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200432 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
wdenkbdccc4f2003-08-05 17:43:17 +0000433 ++bank, ++info) {
434 if (s_first[bank]>=0) {
435 erased += s_last[bank] - s_first[bank] + 1;
wdenk013dc8d2003-08-07 14:52:18 +0000436 debug ("Erase Flash from 0x%08lx to 0x%08lx "
wdenkbdccc4f2003-08-05 17:43:17 +0000437 "in Bank # %ld ",
438 info->start[s_first[bank]],
439 (s_last[bank] == info->sector_count) ?
440 info->start[0] + info->size - 1:
441 info->start[s_last[bank]+1] - 1,
442 bank+1);
443 rcode = flash_erase (info, s_first[bank], s_last[bank]);
wdenk38a24a62002-09-18 12:49:44 +0000444 }
445 }
Joe Hershbergerde15a062012-08-17 15:36:41 -0500446 if (rcode == 0)
447 printf("Erased %d sectors\n", erased);
wdenkbdccc4f2003-08-05 17:43:17 +0000448 } else if (rcode == 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000449 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000450 " not on sector boundary\n");
451 rcode = 1;
452 }
453 return rcode;
454}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200455#endif /* CONFIG_SYS_NO_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000456
Kim Phillips088f1b12012-10-29 13:34:31 +0000457static int do_protect(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000458{
Marek Vasut5b3901d2011-09-26 02:26:02 +0200459 int rcode = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200460#ifndef CONFIG_SYS_NO_FLASH
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000461 flash_info_t *info = NULL;
Stelian Pop880cc432008-03-26 22:52:35 +0100462 ulong bank;
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000463 int i, n, sect_first = 0, sect_last = 0;
Alexander Steinc0008082010-08-11 16:48:04 +0200464#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200465 struct mtd_device *dev;
466 struct part_info *part;
467 u8 dev_type, dev_num, pnum;
468#endif
Remy Bohmer3a26c432011-02-19 19:56:28 +0100469#endif /* CONFIG_SYS_NO_FLASH */
wdenk5779d8d2003-12-06 23:55:10 +0000470#ifdef CONFIG_HAS_DATAFLASH
471 int status;
472#endif
Marek Vasut5b3901d2011-09-26 02:26:02 +0200473#if !defined(CONFIG_SYS_NO_FLASH) || defined(CONFIG_HAS_DATAFLASH)
Wolfgang Denk5669ed42009-07-18 23:18:14 +0200474 int p;
Marek Vasut5b3901d2011-09-26 02:26:02 +0200475 ulong addr_first, addr_last;
476#endif
Stefan Roese2662b402006-04-01 13:41:03 +0200477
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200478 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000479 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000480
Marek Vasut5b3901d2011-09-26 02:26:02 +0200481#if !defined(CONFIG_SYS_NO_FLASH) || defined(CONFIG_HAS_DATAFLASH)
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200482 if (strcmp(argv[1], "off") == 0)
wdenk38a24a62002-09-18 12:49:44 +0000483 p = 0;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200484 else if (strcmp(argv[1], "on") == 0)
wdenk38a24a62002-09-18 12:49:44 +0000485 p = 1;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200486 else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000487 return CMD_RET_USAGE;
Marek Vasut5b3901d2011-09-26 02:26:02 +0200488#endif
wdenk38a24a62002-09-18 12:49:44 +0000489
wdenk5779d8d2003-12-06 23:55:10 +0000490#ifdef CONFIG_HAS_DATAFLASH
491 if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
492 addr_first = simple_strtoul(argv[2], NULL, 16);
493 addr_last = simple_strtoul(argv[3], NULL, 16);
494
495 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
496 status = dataflash_real_protect(p,addr_first,addr_last);
497 if (status < 0){
wdenk4b9206e2004-03-23 22:14:11 +0000498 puts ("Bad DataFlash sector specification\n");
wdenkd4ca31c2004-01-02 14:00:00 +0000499 return 1;
500 }
501 printf("%sProtect %d DataFlash Sectors\n",
502 p ? "" : "Un-", status);
wdenk5779d8d2003-12-06 23:55:10 +0000503 return 0;
504 }
505 }
506#endif
wdenkd4ca31c2004-01-02 14:00:00 +0000507
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200508#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000509 if (strcmp(argv[2], "all") == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200510 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000511 info = &flash_info[bank-1];
512 if (info->flash_id == FLASH_UNKNOWN) {
513 continue;
514 }
515 printf ("%sProtect Flash Bank # %ld\n",
516 p ? "" : "Un-", bank);
517
518 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200519#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000520 if (flash_real_protect(info, i, p))
521 rcode = 1;
522 putc ('.');
523#else
524 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200525#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000526 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200527#if defined(CONFIG_SYS_FLASH_PROTECTION)
Heiko Schocherbb741402006-04-11 14:39:21 +0200528 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200529#endif /* CONFIG_SYS_FLASH_PROTECTION */
Heiko Schocherbb741402006-04-11 14:39:21 +0200530 }
wdenk38a24a62002-09-18 12:49:44 +0000531 return rcode;
532 }
533
534 if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
535 if (n < 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000536 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000537 return 1;
538 }
Stefan Roesef2302d42008-08-06 14:05:38 +0200539 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
wdenk38a24a62002-09-18 12:49:44 +0000540 p ? "" : "Un-", sect_first, sect_last,
541 (info-flash_info)+1);
542 for (i = sect_first; i <= sect_last; i++) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200543#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000544 if (flash_real_protect(info, i, p))
545 rcode = 1;
546 putc ('.');
547#else
548 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200549#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000550 }
551
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200552#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000553 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200554#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000555
556 return rcode;
557 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200558
Alexander Steinc0008082010-08-11 16:48:04 +0200559#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200560 /* protect on/off <part-id> */
Stefan Roese68d7d652009-03-19 13:30:36 +0100561 if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200562 mtdparts_init();
563 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
564 if (dev->id->type == MTD_DEV_TYPE_NOR) {
565 bank = dev->id->num;
566 info = &flash_info[bank];
567 addr_first = part->offset + info->start[0];
568 addr_last = addr_first + part->size - 1;
569
Wolfgang Denk445093d2009-11-17 21:27:39 +0100570 printf ("%sProtect Flash Partition %s, "
Kim Phillips4109df62008-07-10 14:00:15 -0500571 "bank %ld, 0x%08lx - 0x%08lx\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200572 p ? "" : "Un", argv[1],
573 bank, addr_first, addr_last);
574
575 rcode = flash_sect_protect (p, addr_first, addr_last);
576 return rcode;
577 }
578
579 printf("cannot %sprotect, not a NOR device\n",
580 p ? "" : "un");
581 return 1;
582 }
583 }
584#endif
wdenk38a24a62002-09-18 12:49:44 +0000585
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200586 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000587 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000588
589 if (strcmp(argv[2], "bank") == 0) {
590 bank = simple_strtoul(argv[3], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200591 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000592 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200593 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000594 return 1;
595 }
596 printf ("%sProtect Flash Bank # %ld\n",
597 p ? "" : "Un-", bank);
598 info = &flash_info[bank-1];
599
600 if (info->flash_id == FLASH_UNKNOWN) {
wdenk4b9206e2004-03-23 22:14:11 +0000601 puts ("missing or unknown FLASH type\n");
wdenk38a24a62002-09-18 12:49:44 +0000602 return 1;
603 }
604 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200605#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000606 if (flash_real_protect(info, i, p))
607 rcode = 1;
608 putc ('.');
609#else
610 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200611#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000612 }
613
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200614#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000615 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200616#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000617
618 return rcode;
619 }
620
Wolfgang Denkf5301872005-07-21 11:45:50 +0200621 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
622 printf("Bad address format\n");
623 return 1;
624 }
wdenk38a24a62002-09-18 12:49:44 +0000625
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200626 if (addr_first >= addr_last)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000627 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200628
wdenk38a24a62002-09-18 12:49:44 +0000629 rcode = flash_sect_protect (p, addr_first, addr_last);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200630#endif /* CONFIG_SYS_NO_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000631 return rcode;
632}
633
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200634#ifndef CONFIG_SYS_NO_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000635int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
636{
637 flash_info_t *info;
638 ulong bank;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200639 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
wdenk38a24a62002-09-18 12:49:44 +0000640 int protected, i;
wdenkbdccc4f2003-08-05 17:43:17 +0000641 int planned;
642 int rcode;
643
644 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000645
646 protected = 0;
647
wdenkbdccc4f2003-08-05 17:43:17 +0000648 if (planned && (rcode == 0)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200649 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
wdenkbdccc4f2003-08-05 17:43:17 +0000650 if (info->flash_id == FLASH_UNKNOWN) {
wdenk38a24a62002-09-18 12:49:44 +0000651 continue;
wdenkbdccc4f2003-08-05 17:43:17 +0000652 }
wdenk38a24a62002-09-18 12:49:44 +0000653
wdenkbdccc4f2003-08-05 17:43:17 +0000654 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
stroeseabcac872003-12-09 14:58:22 +0000655 debug ("%sProtecting sectors %d..%d in bank %ld\n",
656 p ? "" : "Un-",
wdenkbdccc4f2003-08-05 17:43:17 +0000657 s_first[bank], s_last[bank], bank+1);
658 protected += s_last[bank] - s_first[bank] + 1;
659 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200660#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkbdccc4f2003-08-05 17:43:17 +0000661 if (flash_real_protect(info, i, p))
662 rcode = 1;
663 putc ('.');
wdenk38a24a62002-09-18 12:49:44 +0000664#else
wdenkbdccc4f2003-08-05 17:43:17 +0000665 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200666#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenkbdccc4f2003-08-05 17:43:17 +0000667 }
wdenk38a24a62002-09-18 12:49:44 +0000668 }
wdenkbdccc4f2003-08-05 17:43:17 +0000669 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200670#if defined(CONFIG_SYS_FLASH_PROTECTION)
Stefan Roese2662b402006-04-01 13:41:03 +0200671 puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200672#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000673
wdenk38a24a62002-09-18 12:49:44 +0000674 printf ("%sProtected %d sectors\n",
675 p ? "" : "Un-", protected);
wdenkbdccc4f2003-08-05 17:43:17 +0000676 } else if (rcode == 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000677 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000678 " not on sector boundary\n");
679 rcode = 1;
680 }
681 return rcode;
682}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200683#endif /* CONFIG_SYS_NO_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000684
wdenk8bde7f72003-06-27 21:31:46 +0000685
686/**************************************************/
Alexander Steinc0008082010-08-11 16:48:04 +0200687#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200688# define TMP_ERASE "erase <part-id>\n - erase partition\n"
689# define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
690# define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
691#else
692# define TMP_ERASE /* empty */
693# define TMP_PROT_ON /* empty */
694# define TMP_PROT_OFF /* empty */
695#endif
wdenk8bde7f72003-06-27 21:31:46 +0000696
wdenk0d498392003-07-01 21:06:45 +0000697U_BOOT_CMD(
698 flinfo, 2, 1, do_flinfo,
Peter Tyser2fb26042009-01-27 18:03:12 -0600699 "print FLASH memory information",
wdenk8bde7f72003-06-27 21:31:46 +0000700 "\n - print information for all FLASH memory banks\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200701 "flinfo N\n - print information for FLASH memory bank # N"
wdenk8bde7f72003-06-27 21:31:46 +0000702);
703
wdenk0d498392003-07-01 21:06:45 +0000704U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200705 erase, 3, 0, do_flerase,
Peter Tyser2fb26042009-01-27 18:03:12 -0600706 "erase FLASH memory",
wdenk8bde7f72003-06-27 21:31:46 +0000707 "start end\n"
708 " - erase FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200709 "erase start +len\n"
710 " - erase FLASH from addr 'start' to the end of sect "
711 "w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000712 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
713 "erase bank N\n - erase FLASH bank # N\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200714 TMP_ERASE
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200715 "erase all\n - erase all FLASH banks"
wdenk8bde7f72003-06-27 21:31:46 +0000716);
717
wdenk0d498392003-07-01 21:06:45 +0000718U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200719 protect, 4, 0, do_protect,
Peter Tyser2fb26042009-01-27 18:03:12 -0600720 "enable or disable FLASH write protection",
wdenk8bde7f72003-06-27 21:31:46 +0000721 "on start end\n"
722 " - protect FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200723 "protect on start +len\n"
724 " - protect FLASH from addr 'start' to end of sect "
725 "w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000726 "protect on N:SF[-SL]\n"
727 " - protect sectors SF-SL in FLASH bank # N\n"
728 "protect on bank N\n - protect FLASH bank # N\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200729 TMP_PROT_ON
wdenk8bde7f72003-06-27 21:31:46 +0000730 "protect on all\n - protect all FLASH banks\n"
731 "protect off start end\n"
732 " - make FLASH from addr 'start' to addr 'end' writable\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200733 "protect off start +len\n"
734 " - make FLASH from addr 'start' to end of sect "
735 "w/addr 'start'+'len'-1 wrtable\n"
wdenk8bde7f72003-06-27 21:31:46 +0000736 "protect off N:SF[-SL]\n"
737 " - make sectors SF-SL writable in FLASH bank # N\n"
738 "protect off bank N\n - make FLASH bank # N writable\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200739 TMP_PROT_OFF
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200740 "protect off all\n - make all FLASH banks writable"
wdenk8bde7f72003-06-27 21:31:46 +0000741);
742
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200743#undef TMP_ERASE
744#undef TMP_PROT_ON
745#undef TMP_PROT_OFF