blob: b150940bf3a99396fdfdf691465a1d2b6cc27a98 [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 Denkd79de1d2013-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>
wdenk57b2d802003-06-27 21:31:46 +000013
wdenk381669a2003-06-16 23:50:08 +000014#ifdef CONFIG_HAS_DATAFLASH
15#include <dataflash.h>
16#endif
17
Alexander Stein8967edf2010-08-11 16:48:04 +020018#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk47f57792005-08-08 01:03:24 +020019#include <jffs2/jffs2.h>
20
Wolfgang Denk161bfc52009-11-17 21:27:39 +010021/* partition handling routines */
Wolfgang Denk47f57792005-08-08 01:03:24 +020022int mtdparts_init(void);
Stefan Roeseb1423dd2009-03-19 13:30:36 +010023int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
Wolfgang Denk47f57792005-08-08 01:03:24 +020024int find_dev_and_part(const char *id, struct mtd_device **dev,
25 u8 *part_num, struct part_info **part);
26#endif
27
Masahiro Yamada8cea9b52017-02-11 22:43:54 +090028#ifdef CONFIG_MTD_NOR_FLASH
Stefan Roese64b96a32010-08-31 10:04:11 +020029#include <flash.h>
30#include <mtd/cfi_flash.h>
wdenk38a24a62002-09-18 12:49:44 +000031extern flash_info_t flash_info[]; /* info for FLASH chips */
32
33/*
34 * The user interface starts numbering for Flash banks with 1
35 * for historical reasons.
36 */
37
38/*
39 * this routine looks for an abbreviated flash range specification.
40 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
41 * sector to erase, and SL is the last sector to erase (defaults to SF).
42 * bank numbers start at 1 to be consistent with other specs, sector numbers
43 * start at zero.
44 *
45 * returns: 1 - correct spec; *pinfo, *psf and *psl are
46 * set appropriately
47 * 0 - doesn't look like an abbreviated spec
48 * -1 - looks like an abbreviated spec, but got
49 * a parsing error, a number out of range,
50 * or an invalid flash bank.
51 */
52static int
wdenk0a658552003-08-05 17:43:17 +000053abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
wdenk38a24a62002-09-18 12:49:44 +000054{
wdenk0a658552003-08-05 17:43:17 +000055 flash_info_t *fp;
56 int bank, first, last;
57 char *p, *ep;
wdenk38a24a62002-09-18 12:49:44 +000058
wdenk0a658552003-08-05 17:43:17 +000059 if ((p = strchr (str, ':')) == NULL)
60 return 0;
61 *p++ = '\0';
wdenk38a24a62002-09-18 12:49:44 +000062
wdenk0a658552003-08-05 17:43:17 +000063 bank = simple_strtoul (str, &ep, 10);
64 if (ep == str || *ep != '\0' ||
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020065 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
wdenk0a658552003-08-05 17:43:17 +000066 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
67 return -1;
wdenk38a24a62002-09-18 12:49:44 +000068
wdenk0a658552003-08-05 17:43:17 +000069 str = p;
70 if ((p = strchr (str, '-')) != NULL)
71 *p++ = '\0';
72
73 first = simple_strtoul (str, &ep, 10);
74 if (ep == str || *ep != '\0' || first >= fp->sector_count)
75 return -1;
76
77 if (p != NULL) {
78 last = simple_strtoul (p, &ep, 10);
79 if (ep == p || *ep != '\0' ||
80 last < first || last >= fp->sector_count)
81 return -1;
82 } else {
83 last = first;
84 }
85
86 *pinfo = fp;
87 *psf = first;
88 *psl = last;
89
90 return 1;
91}
92
Wolfgang Denk104866b2005-07-21 11:45:50 +020093/*
Bartlomiej Sieka6c24d312008-10-01 15:26:27 +020094 * Take *addr in Flash and adjust it to fall on the end of its sector
95 */
96int flash_sect_roundb (ulong *addr)
97{
98 flash_info_t *info;
99 ulong bank, sector_end_addr;
100 char found;
101 int i;
102
103 /* find the end addr of the sector where the *addr is */
104 found = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200105 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
Bartlomiej Sieka6c24d312008-10-01 15:26:27 +0200106 info = &flash_info[bank];
107 for (i = 0; i < info->sector_count && !found; ++i) {
108 /* get the end address of the sector */
109 if (i == info->sector_count - 1) {
110 sector_end_addr = info->start[0] +
111 info->size - 1;
112 } else {
113 sector_end_addr = info->start[i+1] - 1;
114 }
115
116 if (*addr <= sector_end_addr &&
117 *addr >= info->start[i]) {
118 found = 1;
119 /* adjust *addr if necessary */
120 if (*addr < sector_end_addr)
121 *addr = sector_end_addr;
122 } /* sector */
123 } /* bank */
124 }
125 if (!found) {
Mike Williamsbf895ad2011-07-22 04:01:30 +0000126 /* error, address not in flash */
Bartlomiej Sieka6c24d312008-10-01 15:26:27 +0200127 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
128 return 1;
129 }
130
131 return 0;
132}
133
134/*
Wolfgang Denk104866b2005-07-21 11:45:50 +0200135 * This function computes the start and end addresses for both
136 * erase and protect commands. The range of the addresses on which
137 * either of the commands is to operate can be given in two forms:
138 * 1. <cmd> start end - operate on <'start', 'end')
Wolfgang Denkd06ce5d2005-08-02 17:06:17 +0200139 * 2. <cmd> start +length - operate on <'start', start + length)
Wolfgang Denk104866b2005-07-21 11:45:50 +0200140 * If the second form is used and the end address doesn't fall on the
141 * sector boundary, than it will be adjusted to the next sector boundary.
142 * If it isn't in the flash, the function will fail (return -1).
143 * Input:
144 * arg1, arg2: address specification (i.e. both command arguments)
145 * Output:
146 * addr_first, addr_last: computed address range
147 * Return:
148 * 1: success
149 * -1: failure (bad format, bad address).
150*/
151static int
152addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
153{
154 char *ep;
Heiko Schocherfd9e97c2006-04-11 14:39:21 +0200155 char len_used; /* indicates if the "start +length" form used */
Wolfgang Denkbb5c4042005-08-05 11:10:31 +0200156
Wolfgang Denk104866b2005-07-21 11:45:50 +0200157 *addr_first = simple_strtoul(arg1, &ep, 16);
158 if (ep == arg1 || *ep != '\0')
159 return -1;
160
Heiko Schocherfd9e97c2006-04-11 14:39:21 +0200161 len_used = 0;
Wolfgang Denk104866b2005-07-21 11:45:50 +0200162 if (arg2 && *arg2 == '+'){
163 len_used = 1;
164 ++arg2;
165 }
166
167 *addr_last = simple_strtoul(arg2, &ep, 16);
168 if (ep == arg2 || *ep != '\0')
169 return -1;
170
171 if (len_used){
172 /*
173 * *addr_last has the length, compute correct *addr_last
174 * XXX watch out for the integer overflow! Right now it is
175 * checked for in both the callers.
176 */
177 *addr_last = *addr_first + *addr_last - 1;
178
179 /*
180 * It may happen that *addr_last doesn't fall on the sector
181 * boundary. We want to round such an address to the next
182 * sector boundary, so that the commands don't fail later on.
183 */
184
Bartlomiej Sieka6c24d312008-10-01 15:26:27 +0200185 if (flash_sect_roundb(addr_last) > 0)
Wolfgang Denk104866b2005-07-21 11:45:50 +0200186 return -1;
Wolfgang Denk104866b2005-07-21 11:45:50 +0200187 } /* "start +length" from used */
188
189 return 1;
190}
191
wdenk0a658552003-08-05 17:43:17 +0000192static int
193flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
194 int *s_first, int *s_last,
195 int *s_count )
196{
197 flash_info_t *info;
198 ulong bank;
199 int rcode = 0;
200
201 *s_count = 0;
202
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200203 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk0a658552003-08-05 17:43:17 +0000204 s_first[bank] = -1; /* first sector to erase */
205 s_last [bank] = -1; /* last sector to erase */
206 }
207
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200208 for (bank=0,info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200209 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
wdenk0a658552003-08-05 17:43:17 +0000210 ++bank, ++info) {
211 ulong b_end;
212 int sect;
213 short s_end;
214
215 if (info->flash_id == FLASH_UNKNOWN) {
216 continue;
217 }
218
219 b_end = info->start[0] + info->size - 1; /* bank end addr */
220 s_end = info->sector_count - 1; /* last sector */
wdenk38a24a62002-09-18 12:49:44 +0000221
wdenk38a24a62002-09-18 12:49:44 +0000222
wdenk0a658552003-08-05 17:43:17 +0000223 for (sect=0; sect < info->sector_count; ++sect) {
224 ulong end; /* last address in current sect */
225
226 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
wdenk38a24a62002-09-18 12:49:44 +0000227
wdenk0a658552003-08-05 17:43:17 +0000228 if (addr_first > end)
229 continue;
230 if (addr_last < info->start[sect])
231 continue;
wdenk38a24a62002-09-18 12:49:44 +0000232
wdenk0a658552003-08-05 17:43:17 +0000233 if (addr_first == info->start[sect]) {
234 s_first[bank] = sect;
235 }
236 if (addr_last == end) {
237 s_last[bank] = sect;
238 }
239 }
240 if (s_first[bank] >= 0) {
241 if (s_last[bank] < 0) {
242 if (addr_last > b_end) {
243 s_last[bank] = s_end;
244 } else {
wdenk42c05472004-03-23 22:14:11 +0000245 puts ("Error: end address"
wdenk0a658552003-08-05 17:43:17 +0000246 " not on sector boundary\n");
247 rcode = 1;
248 break;
249 }
250 }
251 if (s_last[bank] < s_first[bank]) {
wdenk42c05472004-03-23 22:14:11 +0000252 puts ("Error: end sector"
wdenk0a658552003-08-05 17:43:17 +0000253 " precedes start sector\n");
254 rcode = 1;
255 break;
256 }
257 sect = s_last[bank];
258 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
259 (*s_count) += s_last[bank] - s_first[bank] + 1;
wdenk54070ab2004-12-31 09:32:47 +0000260 } else if (addr_first >= info->start[0] && addr_first < b_end) {
261 puts ("Error: start address not on sector boundary\n");
262 rcode = 1;
263 break;
wdenkbd1575f2003-10-14 19:43:55 +0000264 } else if (s_last[bank] >= 0) {
wdenk42c05472004-03-23 22:14:11 +0000265 puts ("Error: cannot span across banks when they are"
wdenkbd1575f2003-10-14 19:43:55 +0000266 " mapped in reverse order\n");
267 rcode = 1;
268 break;
wdenk0a658552003-08-05 17:43:17 +0000269 }
270 }
271
272 return rcode;
wdenk38a24a62002-09-18 12:49:44 +0000273}
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900274#endif /* CONFIG_MTD_NOR_FLASH */
wdenk0a658552003-08-05 17:43:17 +0000275
Kim Phillipsdc00a682012-10-29 13:34:31 +0000276static int do_flinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000277{
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900278#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000279 ulong bank;
Stelian Popa63d2eb2008-03-26 22:52:35 +0100280#endif
wdenk38a24a62002-09-18 12:49:44 +0000281
wdenk381669a2003-06-16 23:50:08 +0000282#ifdef CONFIG_HAS_DATAFLASH
283 dataflash_print_info();
284#endif
285
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900286#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000287 if (argc == 1) { /* print info for all FLASH banks */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200288 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000289 printf ("\nBank # %ld: ", bank+1);
290
291 flash_print_info (&flash_info[bank]);
292 }
293 return 0;
294 }
295
296 bank = simple_strtoul(argv[1], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200297 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000298 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200299 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000300 return 1;
301 }
302 printf ("\nBank # %ld: ", bank);
303 flash_print_info (&flash_info[bank-1]);
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900304#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000305 return 0;
306}
Wolfgang Denk47f57792005-08-08 01:03:24 +0200307
Kim Phillipsdc00a682012-10-29 13:34:31 +0000308static int do_flerase(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000309{
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900310#ifdef CONFIG_MTD_NOR_FLASH
Wolfgang Denk57e219e2011-11-05 05:13:04 +0000311 flash_info_t *info = NULL;
wdenk38a24a62002-09-18 12:49:44 +0000312 ulong bank, addr_first, addr_last;
Wolfgang Denk57e219e2011-11-05 05:13:04 +0000313 int n, sect_first = 0, sect_last = 0;
Alexander Stein8967edf2010-08-11 16:48:04 +0200314#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200315 struct mtd_device *dev;
316 struct part_info *part;
317 u8 dev_type, dev_num, pnum;
318#endif
wdenk38a24a62002-09-18 12:49:44 +0000319 int rcode = 0;
320
Wolfgang Denk3b683112010-07-17 01:06:04 +0200321 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000322 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000323
324 if (strcmp(argv[1], "all") == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200325 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000326 printf ("Erase Flash Bank # %ld ", bank);
327 info = &flash_info[bank-1];
328 rcode = flash_erase (info, 0, info->sector_count-1);
329 }
330 return rcode;
331 }
332
333 if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
334 if (n < 0) {
wdenk42c05472004-03-23 22:14:11 +0000335 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000336 return 1;
337 }
Stefan Roese37628252008-08-06 14:05:38 +0200338 printf ("Erase Flash Sectors %d-%d in Bank # %zu ",
wdenk38a24a62002-09-18 12:49:44 +0000339 sect_first, sect_last, (info-flash_info)+1);
340 rcode = flash_erase(info, sect_first, sect_last);
341 return rcode;
342 }
343
Alexander Stein8967edf2010-08-11 16:48:04 +0200344#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200345 /* erase <part-id> - erase partition */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100346 if ((argc == 2) && (mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200347 mtdparts_init();
348 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
349 if (dev->id->type == MTD_DEV_TYPE_NOR) {
350 bank = dev->id->num;
351 info = &flash_info[bank];
352 addr_first = part->offset + info->start[0];
353 addr_last = addr_first + part->size - 1;
354
Wolfgang Denk161bfc52009-11-17 21:27:39 +0100355 printf ("Erase Flash Partition %s, "
Kim Phillipsf91aa8d2008-07-10 14:00:15 -0500356 "bank %ld, 0x%08lx - 0x%08lx ",
Wolfgang Denk47f57792005-08-08 01:03:24 +0200357 argv[1], bank, addr_first,
358 addr_last);
359
360 rcode = flash_sect_erase(addr_first, addr_last);
361 return rcode;
362 }
363
364 printf("cannot erase, not a NOR device\n");
365 return 1;
366 }
367 }
368#endif
369
Wolfgang Denk3b683112010-07-17 01:06:04 +0200370 if (argc != 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000371 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000372
373 if (strcmp(argv[1], "bank") == 0) {
374 bank = simple_strtoul(argv[2], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200375 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000376 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200377 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000378 return 1;
379 }
380 printf ("Erase Flash Bank # %ld ", bank);
381 info = &flash_info[bank-1];
382 rcode = flash_erase (info, 0, info->sector_count-1);
383 return rcode;
384 }
385
Wolfgang Denk104866b2005-07-21 11:45:50 +0200386 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
387 printf ("Bad address format\n");
388 return 1;
389 }
wdenk38a24a62002-09-18 12:49:44 +0000390
Wolfgang Denk3b683112010-07-17 01:06:04 +0200391 if (addr_first >= addr_last)
Simon Glassa06dfc72011-12-10 08:44:01 +0000392 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000393
wdenk38a24a62002-09-18 12:49:44 +0000394 rcode = flash_sect_erase(addr_first, addr_last);
395 return rcode;
Stelian Popa63d2eb2008-03-26 22:52:35 +0100396#else
397 return 0;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900398#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000399}
400
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900401#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000402int flash_sect_erase (ulong addr_first, ulong addr_last)
403{
404 flash_info_t *info;
405 ulong bank;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200406 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
wdenk0a658552003-08-05 17:43:17 +0000407 int erased = 0;
408 int planned;
wdenk38a24a62002-09-18 12:49:44 +0000409 int rcode = 0;
410
wdenk0a658552003-08-05 17:43:17 +0000411 rcode = flash_fill_sect_ranges (addr_first, addr_last,
412 s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000413
wdenk0a658552003-08-05 17:43:17 +0000414 if (planned && (rcode == 0)) {
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200415 for (bank=0,info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200416 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
wdenk0a658552003-08-05 17:43:17 +0000417 ++bank, ++info) {
418 if (s_first[bank]>=0) {
419 erased += s_last[bank] - s_first[bank] + 1;
wdenk3f08e202003-08-07 14:52:18 +0000420 debug ("Erase Flash from 0x%08lx to 0x%08lx "
wdenk0a658552003-08-05 17:43:17 +0000421 "in Bank # %ld ",
422 info->start[s_first[bank]],
423 (s_last[bank] == info->sector_count) ?
424 info->start[0] + info->size - 1:
425 info->start[s_last[bank]+1] - 1,
426 bank+1);
427 rcode = flash_erase (info, s_first[bank], s_last[bank]);
wdenk38a24a62002-09-18 12:49:44 +0000428 }
wdenk38a24a62002-09-18 12:49:44 +0000429 }
Joe Hershberger497c32f2012-08-17 15:36:41 -0500430 if (rcode == 0)
431 printf("Erased %d sectors\n", erased);
wdenk0a658552003-08-05 17:43:17 +0000432 } else if (rcode == 0) {
wdenk42c05472004-03-23 22:14:11 +0000433 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000434 " not on sector boundary\n");
435 rcode = 1;
436 }
437 return rcode;
438}
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900439#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000440
Kim Phillipsdc00a682012-10-29 13:34:31 +0000441static int do_protect(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000442{
Marek Vasut4c2d8992011-09-26 02:26:02 +0200443 int rcode = 0;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900444#ifdef CONFIG_MTD_NOR_FLASH
Wolfgang Denk57e219e2011-11-05 05:13:04 +0000445 flash_info_t *info = NULL;
Stelian Popa63d2eb2008-03-26 22:52:35 +0100446 ulong bank;
Wolfgang Denk57e219e2011-11-05 05:13:04 +0000447 int i, n, sect_first = 0, sect_last = 0;
Alexander Stein8967edf2010-08-11 16:48:04 +0200448#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200449 struct mtd_device *dev;
450 struct part_info *part;
451 u8 dev_type, dev_num, pnum;
452#endif
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900453#endif /* CONFIG_MTD_NOR_FLASH */
wdenk86765902003-12-06 23:55:10 +0000454#ifdef CONFIG_HAS_DATAFLASH
455 int status;
456#endif
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900457#if defined(CONFIG_MTD_NOR_FLASH) || defined(CONFIG_HAS_DATAFLASH)
Wolfgang Denka2f30822009-07-18 23:18:14 +0200458 int p;
Marek Vasut4c2d8992011-09-26 02:26:02 +0200459 ulong addr_first, addr_last;
460#endif
Stefan Roeseefef95b2006-04-01 13:41:03 +0200461
Wolfgang Denk3b683112010-07-17 01:06:04 +0200462 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000463 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000464
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900465#if defined(CONFIG_MTD_NOR_FLASH) || defined(CONFIG_HAS_DATAFLASH)
Wolfgang Denk3b683112010-07-17 01:06:04 +0200466 if (strcmp(argv[1], "off") == 0)
wdenk38a24a62002-09-18 12:49:44 +0000467 p = 0;
Wolfgang Denk3b683112010-07-17 01:06:04 +0200468 else if (strcmp(argv[1], "on") == 0)
wdenk38a24a62002-09-18 12:49:44 +0000469 p = 1;
Wolfgang Denk3b683112010-07-17 01:06:04 +0200470 else
Simon Glassa06dfc72011-12-10 08:44:01 +0000471 return CMD_RET_USAGE;
Marek Vasut4c2d8992011-09-26 02:26:02 +0200472#endif
wdenk38a24a62002-09-18 12:49:44 +0000473
wdenk86765902003-12-06 23:55:10 +0000474#ifdef CONFIG_HAS_DATAFLASH
475 if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
476 addr_first = simple_strtoul(argv[2], NULL, 16);
477 addr_last = simple_strtoul(argv[3], NULL, 16);
478
479 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
480 status = dataflash_real_protect(p,addr_first,addr_last);
481 if (status < 0){
wdenk42c05472004-03-23 22:14:11 +0000482 puts ("Bad DataFlash sector specification\n");
wdenk1ebf41e2004-01-02 14:00:00 +0000483 return 1;
484 }
485 printf("%sProtect %d DataFlash Sectors\n",
486 p ? "" : "Un-", status);
wdenk86765902003-12-06 23:55:10 +0000487 return 0;
488 }
489 }
490#endif
wdenk1ebf41e2004-01-02 14:00:00 +0000491
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900492#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000493 if (strcmp(argv[2], "all") == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200494 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000495 info = &flash_info[bank-1];
496 if (info->flash_id == FLASH_UNKNOWN) {
497 continue;
498 }
499 printf ("%sProtect Flash Bank # %ld\n",
500 p ? "" : "Un-", bank);
501
502 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200503#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000504 if (flash_real_protect(info, i, p))
505 rcode = 1;
506 putc ('.');
507#else
508 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200509#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000510 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200511#if defined(CONFIG_SYS_FLASH_PROTECTION)
Heiko Schocherfd9e97c2006-04-11 14:39:21 +0200512 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200513#endif /* CONFIG_SYS_FLASH_PROTECTION */
Heiko Schocherfd9e97c2006-04-11 14:39:21 +0200514 }
wdenk38a24a62002-09-18 12:49:44 +0000515 return rcode;
516 }
517
518 if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
519 if (n < 0) {
wdenk42c05472004-03-23 22:14:11 +0000520 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000521 return 1;
522 }
Stefan Roese37628252008-08-06 14:05:38 +0200523 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
wdenk38a24a62002-09-18 12:49:44 +0000524 p ? "" : "Un-", sect_first, sect_last,
525 (info-flash_info)+1);
526 for (i = sect_first; i <= sect_last; i++) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200527#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000528 if (flash_real_protect(info, i, p))
529 rcode = 1;
530 putc ('.');
531#else
532 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200533#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000534 }
535
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200536#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000537 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200538#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000539
540 return rcode;
541 }
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200542
Alexander Stein8967edf2010-08-11 16:48:04 +0200543#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200544 /* protect on/off <part-id> */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100545 if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200546 mtdparts_init();
547 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
548 if (dev->id->type == MTD_DEV_TYPE_NOR) {
549 bank = dev->id->num;
550 info = &flash_info[bank];
551 addr_first = part->offset + info->start[0];
552 addr_last = addr_first + part->size - 1;
553
Wolfgang Denk161bfc52009-11-17 21:27:39 +0100554 printf ("%sProtect Flash Partition %s, "
Kim Phillipsf91aa8d2008-07-10 14:00:15 -0500555 "bank %ld, 0x%08lx - 0x%08lx\n",
Wolfgang Denk47f57792005-08-08 01:03:24 +0200556 p ? "" : "Un", argv[1],
557 bank, addr_first, addr_last);
558
559 rcode = flash_sect_protect (p, addr_first, addr_last);
560 return rcode;
561 }
562
563 printf("cannot %sprotect, not a NOR device\n",
564 p ? "" : "un");
565 return 1;
566 }
567 }
568#endif
wdenk38a24a62002-09-18 12:49:44 +0000569
Wolfgang Denk3b683112010-07-17 01:06:04 +0200570 if (argc != 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000571 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000572
573 if (strcmp(argv[2], "bank") == 0) {
574 bank = simple_strtoul(argv[3], NULL, 16);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200575 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000576 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200577 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000578 return 1;
579 }
580 printf ("%sProtect Flash Bank # %ld\n",
581 p ? "" : "Un-", bank);
582 info = &flash_info[bank-1];
583
584 if (info->flash_id == FLASH_UNKNOWN) {
wdenk42c05472004-03-23 22:14:11 +0000585 puts ("missing or unknown FLASH type\n");
wdenk38a24a62002-09-18 12:49:44 +0000586 return 1;
587 }
588 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200589#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000590 if (flash_real_protect(info, i, p))
591 rcode = 1;
592 putc ('.');
593#else
594 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200595#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000596 }
597
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200598#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000599 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200600#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000601
602 return rcode;
603 }
604
Wolfgang Denk104866b2005-07-21 11:45:50 +0200605 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
606 printf("Bad address format\n");
607 return 1;
608 }
wdenk38a24a62002-09-18 12:49:44 +0000609
Wolfgang Denk3b683112010-07-17 01:06:04 +0200610 if (addr_first >= addr_last)
Simon Glassa06dfc72011-12-10 08:44:01 +0000611 return CMD_RET_USAGE;
Wolfgang Denk3b683112010-07-17 01:06:04 +0200612
wdenk38a24a62002-09-18 12:49:44 +0000613 rcode = flash_sect_protect (p, addr_first, addr_last);
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900614#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000615 return rcode;
616}
617
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900618#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000619int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
620{
621 flash_info_t *info;
622 ulong bank;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200623 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
wdenk38a24a62002-09-18 12:49:44 +0000624 int protected, i;
wdenk0a658552003-08-05 17:43:17 +0000625 int planned;
626 int rcode;
wdenk38a24a62002-09-18 12:49:44 +0000627
wdenk0a658552003-08-05 17:43:17 +0000628 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000629
wdenk0a658552003-08-05 17:43:17 +0000630 protected = 0;
wdenk38a24a62002-09-18 12:49:44 +0000631
wdenk0a658552003-08-05 17:43:17 +0000632 if (planned && (rcode == 0)) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200633 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
wdenk0a658552003-08-05 17:43:17 +0000634 if (info->flash_id == FLASH_UNKNOWN) {
wdenk38a24a62002-09-18 12:49:44 +0000635 continue;
wdenk38a24a62002-09-18 12:49:44 +0000636 }
wdenk0a658552003-08-05 17:43:17 +0000637
638 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
stroeseeb4a4392003-12-09 14:58:22 +0000639 debug ("%sProtecting sectors %d..%d in bank %ld\n",
640 p ? "" : "Un-",
wdenk0a658552003-08-05 17:43:17 +0000641 s_first[bank], s_last[bank], bank+1);
642 protected += s_last[bank] - s_first[bank] + 1;
643 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200644#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk0a658552003-08-05 17:43:17 +0000645 if (flash_real_protect(info, i, p))
646 rcode = 1;
647 putc ('.');
wdenk38a24a62002-09-18 12:49:44 +0000648#else
wdenk0a658552003-08-05 17:43:17 +0000649 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200650#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk0a658552003-08-05 17:43:17 +0000651 }
wdenk38a24a62002-09-18 12:49:44 +0000652 }
Stefan Roeseefef95b2006-04-01 13:41:03 +0200653 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200654#if defined(CONFIG_SYS_FLASH_PROTECTION)
Stefan Roeseefef95b2006-04-01 13:41:03 +0200655 puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200656#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000657
wdenk38a24a62002-09-18 12:49:44 +0000658 printf ("%sProtected %d sectors\n",
659 p ? "" : "Un-", protected);
wdenk0a658552003-08-05 17:43:17 +0000660 } else if (rcode == 0) {
wdenk42c05472004-03-23 22:14:11 +0000661 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000662 " not on sector boundary\n");
663 rcode = 1;
664 }
665 return rcode;
666}
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900667#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000668
wdenk57b2d802003-06-27 21:31:46 +0000669
670/**************************************************/
Alexander Stein8967edf2010-08-11 16:48:04 +0200671#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk16439ee2005-08-09 17:25:22 +0200672# define TMP_ERASE "erase <part-id>\n - erase partition\n"
673# define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
674# define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
675#else
676# define TMP_ERASE /* empty */
677# define TMP_PROT_ON /* empty */
678# define TMP_PROT_OFF /* empty */
679#endif
wdenk57b2d802003-06-27 21:31:46 +0000680
wdenkf287a242003-07-01 21:06:45 +0000681U_BOOT_CMD(
682 flinfo, 2, 1, do_flinfo,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600683 "print FLASH memory information",
wdenk57b2d802003-06-27 21:31:46 +0000684 "\n - print information for all FLASH memory banks\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200685 "flinfo N\n - print information for FLASH memory bank # N"
wdenk57b2d802003-06-27 21:31:46 +0000686);
687
wdenkf287a242003-07-01 21:06:45 +0000688U_BOOT_CMD(
Detlev Zundel409a2852007-05-23 19:02:41 +0200689 erase, 3, 0, do_flerase,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600690 "erase FLASH memory",
wdenk57b2d802003-06-27 21:31:46 +0000691 "start end\n"
692 " - erase FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denk104866b2005-07-21 11:45:50 +0200693 "erase start +len\n"
694 " - erase FLASH from addr 'start' to the end of sect "
695 "w/addr 'start'+'len'-1\n"
wdenk57b2d802003-06-27 21:31:46 +0000696 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
697 "erase bank N\n - erase FLASH bank # N\n"
Wolfgang Denk16439ee2005-08-09 17:25:22 +0200698 TMP_ERASE
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200699 "erase all\n - erase all FLASH banks"
wdenk57b2d802003-06-27 21:31:46 +0000700);
701
wdenkf287a242003-07-01 21:06:45 +0000702U_BOOT_CMD(
Detlev Zundel409a2852007-05-23 19:02:41 +0200703 protect, 4, 0, do_protect,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600704 "enable or disable FLASH write protection",
wdenk57b2d802003-06-27 21:31:46 +0000705 "on start end\n"
706 " - protect FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denk104866b2005-07-21 11:45:50 +0200707 "protect on start +len\n"
708 " - protect FLASH from addr 'start' to end of sect "
709 "w/addr 'start'+'len'-1\n"
wdenk57b2d802003-06-27 21:31:46 +0000710 "protect on N:SF[-SL]\n"
711 " - protect sectors SF-SL in FLASH bank # N\n"
712 "protect on bank N\n - protect FLASH bank # N\n"
Wolfgang Denk16439ee2005-08-09 17:25:22 +0200713 TMP_PROT_ON
wdenk57b2d802003-06-27 21:31:46 +0000714 "protect on all\n - protect all FLASH banks\n"
715 "protect off start end\n"
716 " - make FLASH from addr 'start' to addr 'end' writable\n"
Wolfgang Denk104866b2005-07-21 11:45:50 +0200717 "protect off start +len\n"
718 " - make FLASH from addr 'start' to end of sect "
719 "w/addr 'start'+'len'-1 wrtable\n"
wdenk57b2d802003-06-27 21:31:46 +0000720 "protect off N:SF[-SL]\n"
721 " - make sectors SF-SL writable in FLASH bank # N\n"
722 "protect off bank N\n - make FLASH bank # N writable\n"
Wolfgang Denk16439ee2005-08-09 17:25:22 +0200723 TMP_PROT_OFF
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200724 "protect off all\n - make all FLASH banks writable"
wdenk57b2d802003-06-27 21:31:46 +0000725);
726
Wolfgang Denk16439ee2005-08-09 17:25:22 +0200727#undef TMP_ERASE
728#undef TMP_PROT_ON
729#undef TMP_PROT_OFF