blob: a2803e8ee47d9bbdeee75a6b623f9b6d5a8cb00a [file] [log] [blame]
wdenk38a24a62002-09-18 12:49:44 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk38a24a62002-09-18 12:49:44 +00006 */
7
8/*
9 * FLASH support
10 */
11#include <common.h>
12#include <command.h>
wdenk8bde7f72003-06-27 21:31:46 +000013
Alexander Steinc0008082010-08-11 16:48:04 +020014#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +020015#include <jffs2/jffs2.h>
16
Wolfgang Denk445093d2009-11-17 21:27:39 +010017/* partition handling routines */
Wolfgang Denk700a0c62005-08-08 01:03:24 +020018int mtdparts_init(void);
Stefan Roese68d7d652009-03-19 13:30:36 +010019int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
Wolfgang Denk700a0c62005-08-08 01:03:24 +020020int find_dev_and_part(const char *id, struct mtd_device **dev,
21 u8 *part_num, struct part_info **part);
22#endif
23
Masahiro Yamadae856bdc2017-02-11 22:43:54 +090024#ifdef CONFIG_MTD_NOR_FLASH
Stefan Roese3c299752010-08-31 10:04:11 +020025#include <flash.h>
26#include <mtd/cfi_flash.h>
wdenk38a24a62002-09-18 12:49:44 +000027extern flash_info_t flash_info[]; /* info for FLASH chips */
28
29/*
30 * The user interface starts numbering for Flash banks with 1
31 * for historical reasons.
32 */
33
34/*
35 * this routine looks for an abbreviated flash range specification.
36 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
37 * sector to erase, and SL is the last sector to erase (defaults to SF).
38 * bank numbers start at 1 to be consistent with other specs, sector numbers
39 * start at zero.
40 *
41 * returns: 1 - correct spec; *pinfo, *psf and *psl are
42 * set appropriately
43 * 0 - doesn't look like an abbreviated spec
44 * -1 - looks like an abbreviated spec, but got
45 * a parsing error, a number out of range,
46 * or an invalid flash bank.
47 */
48static int
wdenkbdccc4f2003-08-05 17:43:17 +000049abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
wdenk38a24a62002-09-18 12:49:44 +000050{
wdenkbdccc4f2003-08-05 17:43:17 +000051 flash_info_t *fp;
52 int bank, first, last;
53 char *p, *ep;
wdenk38a24a62002-09-18 12:49:44 +000054
wdenkbdccc4f2003-08-05 17:43:17 +000055 if ((p = strchr (str, ':')) == NULL)
56 return 0;
wdenk38a24a62002-09-18 12:49:44 +000057 *p++ = '\0';
58
wdenkbdccc4f2003-08-05 17:43:17 +000059 bank = simple_strtoul (str, &ep, 10);
60 if (ep == str || *ep != '\0' ||
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020061 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
wdenkbdccc4f2003-08-05 17:43:17 +000062 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
63 return -1;
wdenk38a24a62002-09-18 12:49:44 +000064
wdenkbdccc4f2003-08-05 17:43:17 +000065 str = p;
66 if ((p = strchr (str, '-')) != NULL)
67 *p++ = '\0';
wdenk38a24a62002-09-18 12:49:44 +000068
wdenkbdccc4f2003-08-05 17:43:17 +000069 first = simple_strtoul (str, &ep, 10);
70 if (ep == str || *ep != '\0' || first >= fp->sector_count)
71 return -1;
wdenk38a24a62002-09-18 12:49:44 +000072
wdenkbdccc4f2003-08-05 17:43:17 +000073 if (p != NULL) {
74 last = simple_strtoul (p, &ep, 10);
75 if (ep == p || *ep != '\0' ||
76 last < first || last >= fp->sector_count)
77 return -1;
78 } else {
79 last = first;
80 }
81
82 *pinfo = fp;
83 *psf = first;
84 *psl = last;
85
86 return 1;
wdenk38a24a62002-09-18 12:49:44 +000087}
wdenkbdccc4f2003-08-05 17:43:17 +000088
Wolfgang Denkf5301872005-07-21 11:45:50 +020089/*
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +020090 * Take *addr in Flash and adjust it to fall on the end of its sector
91 */
92int flash_sect_roundb (ulong *addr)
93{
94 flash_info_t *info;
95 ulong bank, sector_end_addr;
96 char found;
97 int i;
98
99 /* find the end addr of the sector where the *addr is */
100 found = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200101 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200102 info = &flash_info[bank];
103 for (i = 0; i < info->sector_count && !found; ++i) {
104 /* get the end address of the sector */
105 if (i == info->sector_count - 1) {
106 sector_end_addr = info->start[0] +
107 info->size - 1;
108 } else {
109 sector_end_addr = info->start[i+1] - 1;
110 }
111
112 if (*addr <= sector_end_addr &&
113 *addr >= info->start[i]) {
114 found = 1;
115 /* adjust *addr if necessary */
116 if (*addr < sector_end_addr)
117 *addr = sector_end_addr;
118 } /* sector */
119 } /* bank */
120 }
121 if (!found) {
Mike Williams16263082011-07-22 04:01:30 +0000122 /* error, address not in flash */
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200123 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
124 return 1;
125 }
126
127 return 0;
128}
129
130/*
Wolfgang Denkf5301872005-07-21 11:45:50 +0200131 * This function computes the start and end addresses for both
132 * erase and protect commands. The range of the addresses on which
133 * either of the commands is to operate can be given in two forms:
134 * 1. <cmd> start end - operate on <'start', 'end')
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200135 * 2. <cmd> start +length - operate on <'start', start + length)
Wolfgang Denkf5301872005-07-21 11:45:50 +0200136 * If the second form is used and the end address doesn't fall on the
137 * sector boundary, than it will be adjusted to the next sector boundary.
138 * If it isn't in the flash, the function will fail (return -1).
139 * Input:
140 * arg1, arg2: address specification (i.e. both command arguments)
141 * Output:
142 * addr_first, addr_last: computed address range
143 * Return:
144 * 1: success
145 * -1: failure (bad format, bad address).
146*/
147static int
148addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
149{
150 char *ep;
Heiko Schocherbb741402006-04-11 14:39:21 +0200151 char len_used; /* indicates if the "start +length" form used */
Wolfgang Denk2c61f142005-08-05 11:10:31 +0200152
Wolfgang Denkf5301872005-07-21 11:45:50 +0200153 *addr_first = simple_strtoul(arg1, &ep, 16);
154 if (ep == arg1 || *ep != '\0')
155 return -1;
156
Heiko Schocherbb741402006-04-11 14:39:21 +0200157 len_used = 0;
Wolfgang Denkf5301872005-07-21 11:45:50 +0200158 if (arg2 && *arg2 == '+'){
159 len_used = 1;
160 ++arg2;
161 }
162
163 *addr_last = simple_strtoul(arg2, &ep, 16);
164 if (ep == arg2 || *ep != '\0')
165 return -1;
166
167 if (len_used){
168 /*
169 * *addr_last has the length, compute correct *addr_last
170 * XXX watch out for the integer overflow! Right now it is
171 * checked for in both the callers.
172 */
173 *addr_last = *addr_first + *addr_last - 1;
174
175 /*
176 * It may happen that *addr_last doesn't fall on the sector
177 * boundary. We want to round such an address to the next
178 * sector boundary, so that the commands don't fail later on.
179 */
180
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200181 if (flash_sect_roundb(addr_last) > 0)
Wolfgang Denkf5301872005-07-21 11:45:50 +0200182 return -1;
Wolfgang Denkf5301872005-07-21 11:45:50 +0200183 } /* "start +length" from used */
184
185 return 1;
186}
187
wdenkbdccc4f2003-08-05 17:43:17 +0000188static int
189flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
190 int *s_first, int *s_last,
191 int *s_count )
192{
193 flash_info_t *info;
194 ulong bank;
195 int rcode = 0;
196
197 *s_count = 0;
198
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200199 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenkbdccc4f2003-08-05 17:43:17 +0000200 s_first[bank] = -1; /* first sector to erase */
201 s_last [bank] = -1; /* last sector to erase */
202 }
203
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200204 for (bank=0,info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200205 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
wdenkbdccc4f2003-08-05 17:43:17 +0000206 ++bank, ++info) {
207 ulong b_end;
208 int sect;
209 short s_end;
210
211 if (info->flash_id == FLASH_UNKNOWN) {
212 continue;
213 }
214
215 b_end = info->start[0] + info->size - 1; /* bank end addr */
216 s_end = info->sector_count - 1; /* last sector */
217
218
219 for (sect=0; sect < info->sector_count; ++sect) {
220 ulong end; /* last address in current sect */
221
222 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
223
224 if (addr_first > end)
225 continue;
226 if (addr_last < info->start[sect])
227 continue;
228
229 if (addr_first == info->start[sect]) {
230 s_first[bank] = sect;
231 }
232 if (addr_last == end) {
233 s_last[bank] = sect;
234 }
235 }
236 if (s_first[bank] >= 0) {
237 if (s_last[bank] < 0) {
238 if (addr_last > b_end) {
239 s_last[bank] = s_end;
240 } else {
wdenk4b9206e2004-03-23 22:14:11 +0000241 puts ("Error: end address"
wdenkbdccc4f2003-08-05 17:43:17 +0000242 " not on sector boundary\n");
243 rcode = 1;
244 break;
245 }
246 }
247 if (s_last[bank] < s_first[bank]) {
wdenk4b9206e2004-03-23 22:14:11 +0000248 puts ("Error: end sector"
wdenkbdccc4f2003-08-05 17:43:17 +0000249 " precedes start sector\n");
250 rcode = 1;
251 break;
252 }
253 sect = s_last[bank];
254 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
255 (*s_count) += s_last[bank] - s_first[bank] + 1;
wdenke2ffd592004-12-31 09:32:47 +0000256 } else if (addr_first >= info->start[0] && addr_first < b_end) {
257 puts ("Error: start address not on sector boundary\n");
258 rcode = 1;
259 break;
wdenk2d5b5612003-10-14 19:43:55 +0000260 } else if (s_last[bank] >= 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000261 puts ("Error: cannot span across banks when they are"
wdenk2d5b5612003-10-14 19:43:55 +0000262 " mapped in reverse order\n");
263 rcode = 1;
264 break;
wdenkbdccc4f2003-08-05 17:43:17 +0000265 }
266 }
267
268 return rcode;
269}
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900270#endif /* CONFIG_MTD_NOR_FLASH */
wdenkbdccc4f2003-08-05 17:43:17 +0000271
Kim Phillips088f1b12012-10-29 13:34:31 +0000272static int do_flinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000273{
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900274#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000275 ulong bank;
Stelian Pop880cc432008-03-26 22:52:35 +0100276#endif
wdenk38a24a62002-09-18 12:49:44 +0000277
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900278#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000279 if (argc == 1) { /* print info for all FLASH banks */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200280 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000281 printf ("\nBank # %ld: ", bank+1);
282
283 flash_print_info (&flash_info[bank]);
284 }
285 return 0;
286 }
287
288 bank = simple_strtoul(argv[1], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200289 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000290 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200291 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000292 return 1;
293 }
294 printf ("\nBank # %ld: ", bank);
295 flash_print_info (&flash_info[bank-1]);
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900296#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000297 return 0;
298}
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200299
Kim Phillips088f1b12012-10-29 13:34:31 +0000300static int do_flerase(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000301{
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900302#ifdef CONFIG_MTD_NOR_FLASH
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000303 flash_info_t *info = NULL;
wdenk38a24a62002-09-18 12:49:44 +0000304 ulong bank, addr_first, addr_last;
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000305 int n, sect_first = 0, sect_last = 0;
Alexander Steinc0008082010-08-11 16:48:04 +0200306#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200307 struct mtd_device *dev;
308 struct part_info *part;
309 u8 dev_type, dev_num, pnum;
310#endif
wdenk38a24a62002-09-18 12:49:44 +0000311 int rcode = 0;
312
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200313 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000314 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000315
316 if (strcmp(argv[1], "all") == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200317 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000318 printf ("Erase Flash Bank # %ld ", bank);
319 info = &flash_info[bank-1];
320 rcode = flash_erase (info, 0, info->sector_count-1);
321 }
322 return rcode;
323 }
324
325 if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
326 if (n < 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000327 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000328 return 1;
329 }
Stefan Roesef2302d42008-08-06 14:05:38 +0200330 printf ("Erase Flash Sectors %d-%d in Bank # %zu ",
wdenk38a24a62002-09-18 12:49:44 +0000331 sect_first, sect_last, (info-flash_info)+1);
332 rcode = flash_erase(info, sect_first, sect_last);
333 return rcode;
334 }
335
Alexander Steinc0008082010-08-11 16:48:04 +0200336#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200337 /* erase <part-id> - erase partition */
Stefan Roese68d7d652009-03-19 13:30:36 +0100338 if ((argc == 2) && (mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200339 mtdparts_init();
340 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
341 if (dev->id->type == MTD_DEV_TYPE_NOR) {
342 bank = dev->id->num;
343 info = &flash_info[bank];
344 addr_first = part->offset + info->start[0];
345 addr_last = addr_first + part->size - 1;
346
Wolfgang Denk445093d2009-11-17 21:27:39 +0100347 printf ("Erase Flash Partition %s, "
Kim Phillips4109df62008-07-10 14:00:15 -0500348 "bank %ld, 0x%08lx - 0x%08lx ",
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200349 argv[1], bank, addr_first,
350 addr_last);
351
352 rcode = flash_sect_erase(addr_first, addr_last);
353 return rcode;
354 }
355
356 printf("cannot erase, not a NOR device\n");
357 return 1;
358 }
359 }
360#endif
361
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200362 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000363 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000364
365 if (strcmp(argv[1], "bank") == 0) {
366 bank = simple_strtoul(argv[2], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200367 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000368 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200369 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000370 return 1;
371 }
372 printf ("Erase Flash Bank # %ld ", bank);
373 info = &flash_info[bank-1];
374 rcode = flash_erase (info, 0, info->sector_count-1);
375 return rcode;
376 }
377
Wolfgang Denkf5301872005-07-21 11:45:50 +0200378 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
379 printf ("Bad address format\n");
380 return 1;
381 }
wdenk38a24a62002-09-18 12:49:44 +0000382
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200383 if (addr_first >= addr_last)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000384 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000385
wdenk38a24a62002-09-18 12:49:44 +0000386 rcode = flash_sect_erase(addr_first, addr_last);
387 return rcode;
Stelian Pop880cc432008-03-26 22:52:35 +0100388#else
389 return 0;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900390#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000391}
392
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900393#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000394int flash_sect_erase (ulong addr_first, ulong addr_last)
395{
396 flash_info_t *info;
397 ulong bank;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200398 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
wdenkbdccc4f2003-08-05 17:43:17 +0000399 int erased = 0;
400 int planned;
wdenk38a24a62002-09-18 12:49:44 +0000401 int rcode = 0;
402
wdenkbdccc4f2003-08-05 17:43:17 +0000403 rcode = flash_fill_sect_ranges (addr_first, addr_last,
404 s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000405
wdenkbdccc4f2003-08-05 17:43:17 +0000406 if (planned && (rcode == 0)) {
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200407 for (bank=0,info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200408 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
wdenkbdccc4f2003-08-05 17:43:17 +0000409 ++bank, ++info) {
410 if (s_first[bank]>=0) {
411 erased += s_last[bank] - s_first[bank] + 1;
wdenk013dc8d2003-08-07 14:52:18 +0000412 debug ("Erase Flash from 0x%08lx to 0x%08lx "
wdenkbdccc4f2003-08-05 17:43:17 +0000413 "in Bank # %ld ",
414 info->start[s_first[bank]],
415 (s_last[bank] == info->sector_count) ?
416 info->start[0] + info->size - 1:
417 info->start[s_last[bank]+1] - 1,
418 bank+1);
419 rcode = flash_erase (info, s_first[bank], s_last[bank]);
wdenk38a24a62002-09-18 12:49:44 +0000420 }
421 }
Joe Hershbergerde15a062012-08-17 15:36:41 -0500422 if (rcode == 0)
423 printf("Erased %d sectors\n", erased);
wdenkbdccc4f2003-08-05 17:43:17 +0000424 } else if (rcode == 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000425 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000426 " not on sector boundary\n");
427 rcode = 1;
428 }
429 return rcode;
430}
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900431#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000432
Kim Phillips088f1b12012-10-29 13:34:31 +0000433static int do_protect(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000434{
Marek Vasut5b3901d2011-09-26 02:26:02 +0200435 int rcode = 0;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900436#ifdef CONFIG_MTD_NOR_FLASH
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000437 flash_info_t *info = NULL;
Stelian Pop880cc432008-03-26 22:52:35 +0100438 ulong bank;
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000439 int i, n, sect_first = 0, sect_last = 0;
Alexander Steinc0008082010-08-11 16:48:04 +0200440#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200441 struct mtd_device *dev;
442 struct part_info *part;
443 u8 dev_type, dev_num, pnum;
444#endif
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900445#endif /* CONFIG_MTD_NOR_FLASH */
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +0300446#if defined(CONFIG_MTD_NOR_FLASH)
Wolfgang Denk5669ed42009-07-18 23:18:14 +0200447 int p;
Marek Vasut5b3901d2011-09-26 02:26:02 +0200448 ulong addr_first, addr_last;
449#endif
Stefan Roese2662b402006-04-01 13:41:03 +0200450
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200451 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000452 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000453
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +0300454#if defined(CONFIG_MTD_NOR_FLASH)
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200455 if (strcmp(argv[1], "off") == 0)
wdenk38a24a62002-09-18 12:49:44 +0000456 p = 0;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200457 else if (strcmp(argv[1], "on") == 0)
wdenk38a24a62002-09-18 12:49:44 +0000458 p = 1;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200459 else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000460 return CMD_RET_USAGE;
Marek Vasut5b3901d2011-09-26 02:26:02 +0200461#endif
wdenk38a24a62002-09-18 12:49:44 +0000462
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900463#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000464 if (strcmp(argv[2], "all") == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200465 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000466 info = &flash_info[bank-1];
467 if (info->flash_id == FLASH_UNKNOWN) {
468 continue;
469 }
470 printf ("%sProtect Flash Bank # %ld\n",
471 p ? "" : "Un-", bank);
472
473 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200474#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000475 if (flash_real_protect(info, i, p))
476 rcode = 1;
477 putc ('.');
478#else
479 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200480#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000481 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200482#if defined(CONFIG_SYS_FLASH_PROTECTION)
Heiko Schocherbb741402006-04-11 14:39:21 +0200483 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200484#endif /* CONFIG_SYS_FLASH_PROTECTION */
Heiko Schocherbb741402006-04-11 14:39:21 +0200485 }
wdenk38a24a62002-09-18 12:49:44 +0000486 return rcode;
487 }
488
489 if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
490 if (n < 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000491 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000492 return 1;
493 }
Stefan Roesef2302d42008-08-06 14:05:38 +0200494 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
wdenk38a24a62002-09-18 12:49:44 +0000495 p ? "" : "Un-", sect_first, sect_last,
496 (info-flash_info)+1);
497 for (i = sect_first; i <= sect_last; i++) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200498#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000499 if (flash_real_protect(info, i, p))
500 rcode = 1;
501 putc ('.');
502#else
503 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200504#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000505 }
506
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200507#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000508 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200509#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000510
511 return rcode;
512 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200513
Alexander Steinc0008082010-08-11 16:48:04 +0200514#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200515 /* protect on/off <part-id> */
Stefan Roese68d7d652009-03-19 13:30:36 +0100516 if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200517 mtdparts_init();
518 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
519 if (dev->id->type == MTD_DEV_TYPE_NOR) {
520 bank = dev->id->num;
521 info = &flash_info[bank];
522 addr_first = part->offset + info->start[0];
523 addr_last = addr_first + part->size - 1;
524
Wolfgang Denk445093d2009-11-17 21:27:39 +0100525 printf ("%sProtect Flash Partition %s, "
Kim Phillips4109df62008-07-10 14:00:15 -0500526 "bank %ld, 0x%08lx - 0x%08lx\n",
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200527 p ? "" : "Un", argv[1],
528 bank, addr_first, addr_last);
529
530 rcode = flash_sect_protect (p, addr_first, addr_last);
531 return rcode;
532 }
533
534 printf("cannot %sprotect, not a NOR device\n",
535 p ? "" : "un");
536 return 1;
537 }
538 }
539#endif
wdenk38a24a62002-09-18 12:49:44 +0000540
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200541 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000542 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000543
544 if (strcmp(argv[2], "bank") == 0) {
545 bank = simple_strtoul(argv[3], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200546 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000547 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200548 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000549 return 1;
550 }
551 printf ("%sProtect Flash Bank # %ld\n",
552 p ? "" : "Un-", bank);
553 info = &flash_info[bank-1];
554
555 if (info->flash_id == FLASH_UNKNOWN) {
wdenk4b9206e2004-03-23 22:14:11 +0000556 puts ("missing or unknown FLASH type\n");
wdenk38a24a62002-09-18 12:49:44 +0000557 return 1;
558 }
559 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200560#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000561 if (flash_real_protect(info, i, p))
562 rcode = 1;
563 putc ('.');
564#else
565 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200566#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000567 }
568
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200569#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000570 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200571#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000572
573 return rcode;
574 }
575
Wolfgang Denkf5301872005-07-21 11:45:50 +0200576 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
577 printf("Bad address format\n");
578 return 1;
579 }
wdenk38a24a62002-09-18 12:49:44 +0000580
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200581 if (addr_first >= addr_last)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000582 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200583
wdenk38a24a62002-09-18 12:49:44 +0000584 rcode = flash_sect_protect (p, addr_first, addr_last);
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900585#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000586 return rcode;
587}
588
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900589#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000590int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
591{
592 flash_info_t *info;
593 ulong bank;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200594 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
wdenk38a24a62002-09-18 12:49:44 +0000595 int protected, i;
wdenkbdccc4f2003-08-05 17:43:17 +0000596 int planned;
597 int rcode;
598
599 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000600
601 protected = 0;
602
wdenkbdccc4f2003-08-05 17:43:17 +0000603 if (planned && (rcode == 0)) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200604 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
wdenkbdccc4f2003-08-05 17:43:17 +0000605 if (info->flash_id == FLASH_UNKNOWN) {
wdenk38a24a62002-09-18 12:49:44 +0000606 continue;
wdenkbdccc4f2003-08-05 17:43:17 +0000607 }
wdenk38a24a62002-09-18 12:49:44 +0000608
wdenkbdccc4f2003-08-05 17:43:17 +0000609 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
stroeseabcac872003-12-09 14:58:22 +0000610 debug ("%sProtecting sectors %d..%d in bank %ld\n",
611 p ? "" : "Un-",
wdenkbdccc4f2003-08-05 17:43:17 +0000612 s_first[bank], s_last[bank], bank+1);
613 protected += s_last[bank] - s_first[bank] + 1;
614 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200615#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkbdccc4f2003-08-05 17:43:17 +0000616 if (flash_real_protect(info, i, p))
617 rcode = 1;
618 putc ('.');
wdenk38a24a62002-09-18 12:49:44 +0000619#else
wdenkbdccc4f2003-08-05 17:43:17 +0000620 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200621#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenkbdccc4f2003-08-05 17:43:17 +0000622 }
wdenk38a24a62002-09-18 12:49:44 +0000623 }
wdenkbdccc4f2003-08-05 17:43:17 +0000624 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200625#if defined(CONFIG_SYS_FLASH_PROTECTION)
Stefan Roese2662b402006-04-01 13:41:03 +0200626 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
wdenk38a24a62002-09-18 12:49:44 +0000629 printf ("%sProtected %d sectors\n",
630 p ? "" : "Un-", protected);
wdenkbdccc4f2003-08-05 17:43:17 +0000631 } else if (rcode == 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000632 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000633 " not on sector boundary\n");
634 rcode = 1;
635 }
636 return rcode;
637}
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900638#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000639
wdenk8bde7f72003-06-27 21:31:46 +0000640
641/**************************************************/
Alexander Steinc0008082010-08-11 16:48:04 +0200642#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200643# define TMP_ERASE "erase <part-id>\n - erase partition\n"
644# define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
645# define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
646#else
647# define TMP_ERASE /* empty */
648# define TMP_PROT_ON /* empty */
649# define TMP_PROT_OFF /* empty */
650#endif
wdenk8bde7f72003-06-27 21:31:46 +0000651
wdenk0d498392003-07-01 21:06:45 +0000652U_BOOT_CMD(
653 flinfo, 2, 1, do_flinfo,
Peter Tyser2fb26042009-01-27 18:03:12 -0600654 "print FLASH memory information",
wdenk8bde7f72003-06-27 21:31:46 +0000655 "\n - print information for all FLASH memory banks\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200656 "flinfo N\n - print information for FLASH memory bank # N"
wdenk8bde7f72003-06-27 21:31:46 +0000657);
658
wdenk0d498392003-07-01 21:06:45 +0000659U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200660 erase, 3, 0, do_flerase,
Peter Tyser2fb26042009-01-27 18:03:12 -0600661 "erase FLASH memory",
wdenk8bde7f72003-06-27 21:31:46 +0000662 "start end\n"
663 " - erase FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200664 "erase start +len\n"
665 " - erase FLASH from addr 'start' to the end of sect "
666 "w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000667 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
668 "erase bank N\n - erase FLASH bank # N\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200669 TMP_ERASE
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200670 "erase all\n - erase all FLASH banks"
wdenk8bde7f72003-06-27 21:31:46 +0000671);
672
wdenk0d498392003-07-01 21:06:45 +0000673U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200674 protect, 4, 0, do_protect,
Peter Tyser2fb26042009-01-27 18:03:12 -0600675 "enable or disable FLASH write protection",
wdenk8bde7f72003-06-27 21:31:46 +0000676 "on start end\n"
677 " - protect FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200678 "protect on start +len\n"
679 " - protect FLASH from addr 'start' to end of sect "
680 "w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000681 "protect on N:SF[-SL]\n"
682 " - protect sectors SF-SL in FLASH bank # N\n"
683 "protect on bank N\n - protect FLASH bank # N\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200684 TMP_PROT_ON
wdenk8bde7f72003-06-27 21:31:46 +0000685 "protect on all\n - protect all FLASH banks\n"
686 "protect off start end\n"
687 " - make FLASH from addr 'start' to addr 'end' writable\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200688 "protect off start +len\n"
689 " - make FLASH from addr 'start' to end of sect "
690 "w/addr 'start'+'len'-1 wrtable\n"
wdenk8bde7f72003-06-27 21:31:46 +0000691 "protect off N:SF[-SL]\n"
692 " - make sectors SF-SL writable in FLASH bank # N\n"
693 "protect off bank N\n - make FLASH bank # N writable\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200694 TMP_PROT_OFF
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200695 "protect off all\n - make all FLASH banks writable"
wdenk8bde7f72003-06-27 21:31:46 +0000696);
697
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200698#undef TMP_ERASE
699#undef TMP_PROT_ON
700#undef TMP_PROT_OFF