blob: bc93e984bca6e9f3eca0c6a0e6a05243f5f13e20 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38a24a62002-09-18 12:49:44 +00002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38a24a62002-09-18 12:49:44 +00005 */
6
7/*
8 * FLASH support
9 */
10#include <common.h>
11#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass8e201882020-05-10 11:39:54 -060013#include <uuid.h>
wdenk57b2d802003-06-27 21:31:46 +000014
Alexander Stein8967edf2010-08-11 16:48:04 +020015#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk47f57792005-08-08 01:03:24 +020016#include <jffs2/jffs2.h>
17
Wolfgang Denk161bfc52009-11-17 21:27:39 +010018/* partition handling routines */
Wolfgang Denk47f57792005-08-08 01:03:24 +020019int mtdparts_init(void);
Stefan Roeseb1423dd2009-03-19 13:30:36 +010020int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
Wolfgang Denk47f57792005-08-08 01:03:24 +020021int find_dev_and_part(const char *id, struct mtd_device **dev,
22 u8 *part_num, struct part_info **part);
23#endif
24
Masahiro Yamada8cea9b52017-02-11 22:43:54 +090025#ifdef CONFIG_MTD_NOR_FLASH
Stefan Roese64b96a32010-08-31 10:04:11 +020026#include <flash.h>
27#include <mtd/cfi_flash.h>
wdenk38a24a62002-09-18 12:49:44 +000028extern flash_info_t flash_info[]; /* info for FLASH chips */
29
30/*
31 * The user interface starts numbering for Flash banks with 1
32 * for historical reasons.
33 */
34
35/*
36 * this routine looks for an abbreviated flash range specification.
37 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
38 * sector to erase, and SL is the last sector to erase (defaults to SF).
39 * bank numbers start at 1 to be consistent with other specs, sector numbers
40 * start at zero.
41 *
42 * returns: 1 - correct spec; *pinfo, *psf and *psl are
43 * set appropriately
44 * 0 - doesn't look like an abbreviated spec
45 * -1 - looks like an abbreviated spec, but got
46 * a parsing error, a number out of range,
47 * or an invalid flash bank.
48 */
49static int
wdenk0a658552003-08-05 17:43:17 +000050abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
wdenk38a24a62002-09-18 12:49:44 +000051{
wdenk0a658552003-08-05 17:43:17 +000052 flash_info_t *fp;
53 int bank, first, last;
54 char *p, *ep;
wdenk38a24a62002-09-18 12:49:44 +000055
wdenk0a658552003-08-05 17:43:17 +000056 if ((p = strchr (str, ':')) == NULL)
57 return 0;
58 *p++ = '\0';
wdenk38a24a62002-09-18 12:49:44 +000059
wdenk0a658552003-08-05 17:43:17 +000060 bank = simple_strtoul (str, &ep, 10);
61 if (ep == str || *ep != '\0' ||
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020062 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
wdenk0a658552003-08-05 17:43:17 +000063 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
64 return -1;
wdenk38a24a62002-09-18 12:49:44 +000065
wdenk0a658552003-08-05 17:43:17 +000066 str = p;
67 if ((p = strchr (str, '-')) != NULL)
68 *p++ = '\0';
69
70 first = simple_strtoul (str, &ep, 10);
71 if (ep == str || *ep != '\0' || first >= fp->sector_count)
72 return -1;
73
74 if (p != NULL) {
75 last = simple_strtoul (p, &ep, 10);
76 if (ep == p || *ep != '\0' ||
77 last < first || last >= fp->sector_count)
78 return -1;
79 } else {
80 last = first;
81 }
82
83 *pinfo = fp;
84 *psf = first;
85 *psl = last;
86
87 return 1;
88}
89
Wolfgang Denk104866b2005-07-21 11:45:50 +020090/*
Bartlomiej Sieka6c24d312008-10-01 15:26:27 +020091 * Take *addr in Flash and adjust it to fall on the end of its sector
92 */
Simon Glassc7443162020-05-10 11:39:53 -060093int flash_sect_roundb(ulong *addr)
Bartlomiej Sieka6c24d312008-10-01 15:26:27 +020094{
95 flash_info_t *info;
96 ulong bank, sector_end_addr;
97 char found;
98 int i;
99
100 /* find the end addr of the sector where the *addr is */
101 found = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200102 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
Bartlomiej Sieka6c24d312008-10-01 15:26:27 +0200103 info = &flash_info[bank];
104 for (i = 0; i < info->sector_count && !found; ++i) {
105 /* get the end address of the sector */
106 if (i == info->sector_count - 1) {
107 sector_end_addr = info->start[0] +
108 info->size - 1;
109 } else {
110 sector_end_addr = info->start[i+1] - 1;
111 }
112
113 if (*addr <= sector_end_addr &&
114 *addr >= info->start[i]) {
115 found = 1;
116 /* adjust *addr if necessary */
117 if (*addr < sector_end_addr)
118 *addr = sector_end_addr;
119 } /* sector */
120 } /* bank */
121 }
122 if (!found) {
Mike Williamsbf895ad2011-07-22 04:01:30 +0000123 /* error, address not in flash */
Bartlomiej Sieka6c24d312008-10-01 15:26:27 +0200124 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
125 return 1;
126 }
127
128 return 0;
129}
130
131/*
Wolfgang Denk104866b2005-07-21 11:45:50 +0200132 * This function computes the start and end addresses for both
133 * erase and protect commands. The range of the addresses on which
134 * either of the commands is to operate can be given in two forms:
135 * 1. <cmd> start end - operate on <'start', 'end')
Wolfgang Denkd06ce5d2005-08-02 17:06:17 +0200136 * 2. <cmd> start +length - operate on <'start', start + length)
Wolfgang Denk104866b2005-07-21 11:45:50 +0200137 * If the second form is used and the end address doesn't fall on the
138 * sector boundary, than it will be adjusted to the next sector boundary.
139 * If it isn't in the flash, the function will fail (return -1).
140 * Input:
141 * arg1, arg2: address specification (i.e. both command arguments)
142 * Output:
143 * addr_first, addr_last: computed address range
144 * Return:
145 * 1: success
146 * -1: failure (bad format, bad address).
147*/
148static int
149addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
150{
151 char *ep;
Heiko Schocherfd9e97c2006-04-11 14:39:21 +0200152 char len_used; /* indicates if the "start +length" form used */
Wolfgang Denkbb5c4042005-08-05 11:10:31 +0200153
Simon Glass3ff49ec2021-07-24 09:03:29 -0600154 *addr_first = hextoul(arg1, &ep);
Wolfgang Denk104866b2005-07-21 11:45:50 +0200155 if (ep == arg1 || *ep != '\0')
156 return -1;
157
Heiko Schocherfd9e97c2006-04-11 14:39:21 +0200158 len_used = 0;
Wolfgang Denk104866b2005-07-21 11:45:50 +0200159 if (arg2 && *arg2 == '+'){
160 len_used = 1;
161 ++arg2;
162 }
163
Simon Glass3ff49ec2021-07-24 09:03:29 -0600164 *addr_last = hextoul(arg2, &ep);
Wolfgang Denk104866b2005-07-21 11:45:50 +0200165 if (ep == arg2 || *ep != '\0')
166 return -1;
167
168 if (len_used){
169 /*
170 * *addr_last has the length, compute correct *addr_last
171 * XXX watch out for the integer overflow! Right now it is
172 * checked for in both the callers.
173 */
174 *addr_last = *addr_first + *addr_last - 1;
175
176 /*
177 * It may happen that *addr_last doesn't fall on the sector
178 * boundary. We want to round such an address to the next
179 * sector boundary, so that the commands don't fail later on.
180 */
181
Bartlomiej Sieka6c24d312008-10-01 15:26:27 +0200182 if (flash_sect_roundb(addr_last) > 0)
Wolfgang Denk104866b2005-07-21 11:45:50 +0200183 return -1;
Wolfgang Denk104866b2005-07-21 11:45:50 +0200184 } /* "start +length" from used */
185
186 return 1;
187}
188
wdenk0a658552003-08-05 17:43:17 +0000189static int
190flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
191 int *s_first, int *s_last,
192 int *s_count )
193{
194 flash_info_t *info;
195 ulong bank;
196 int rcode = 0;
197
198 *s_count = 0;
199
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200200 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk0a658552003-08-05 17:43:17 +0000201 s_first[bank] = -1; /* first sector to erase */
202 s_last [bank] = -1; /* last sector to erase */
203 }
204
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200205 for (bank=0,info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200206 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
wdenk0a658552003-08-05 17:43:17 +0000207 ++bank, ++info) {
208 ulong b_end;
209 int sect;
210 short s_end;
211
212 if (info->flash_id == FLASH_UNKNOWN) {
213 continue;
214 }
215
216 b_end = info->start[0] + info->size - 1; /* bank end addr */
217 s_end = info->sector_count - 1; /* last sector */
wdenk38a24a62002-09-18 12:49:44 +0000218
wdenk38a24a62002-09-18 12:49:44 +0000219
wdenk0a658552003-08-05 17:43:17 +0000220 for (sect=0; sect < info->sector_count; ++sect) {
221 ulong end; /* last address in current sect */
222
223 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
wdenk38a24a62002-09-18 12:49:44 +0000224
wdenk0a658552003-08-05 17:43:17 +0000225 if (addr_first > end)
226 continue;
227 if (addr_last < info->start[sect])
228 continue;
wdenk38a24a62002-09-18 12:49:44 +0000229
wdenk0a658552003-08-05 17:43:17 +0000230 if (addr_first == info->start[sect]) {
231 s_first[bank] = sect;
232 }
233 if (addr_last == end) {
234 s_last[bank] = sect;
235 }
236 }
237 if (s_first[bank] >= 0) {
238 if (s_last[bank] < 0) {
239 if (addr_last > b_end) {
240 s_last[bank] = s_end;
241 } else {
wdenk42c05472004-03-23 22:14:11 +0000242 puts ("Error: end address"
wdenk0a658552003-08-05 17:43:17 +0000243 " not on sector boundary\n");
244 rcode = 1;
245 break;
246 }
247 }
248 if (s_last[bank] < s_first[bank]) {
wdenk42c05472004-03-23 22:14:11 +0000249 puts ("Error: end sector"
wdenk0a658552003-08-05 17:43:17 +0000250 " precedes start sector\n");
251 rcode = 1;
252 break;
253 }
254 sect = s_last[bank];
255 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
256 (*s_count) += s_last[bank] - s_first[bank] + 1;
wdenk54070ab2004-12-31 09:32:47 +0000257 } else if (addr_first >= info->start[0] && addr_first < b_end) {
258 puts ("Error: start address not on sector boundary\n");
259 rcode = 1;
260 break;
wdenkbd1575f2003-10-14 19:43:55 +0000261 } else if (s_last[bank] >= 0) {
wdenk42c05472004-03-23 22:14:11 +0000262 puts ("Error: cannot span across banks when they are"
wdenkbd1575f2003-10-14 19:43:55 +0000263 " mapped in reverse order\n");
264 rcode = 1;
265 break;
wdenk0a658552003-08-05 17:43:17 +0000266 }
267 }
268
269 return rcode;
wdenk38a24a62002-09-18 12:49:44 +0000270}
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900271#endif /* CONFIG_MTD_NOR_FLASH */
wdenk0a658552003-08-05 17:43:17 +0000272
Simon Glassed38aef2020-05-10 11:40:03 -0600273static int do_flinfo(struct cmd_tbl *cmdtp, int flag, int argc,
274 char *const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000275{
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900276#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000277 ulong bank;
Stelian Popa63d2eb2008-03-26 22:52:35 +0100278#endif
wdenk38a24a62002-09-18 12:49:44 +0000279
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900280#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000281 if (argc == 1) { /* print info for all FLASH banks */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200282 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000283 printf ("\nBank # %ld: ", bank+1);
284
Simon Glassc7443162020-05-10 11:39:53 -0600285 flash_print_info(&flash_info[bank]);
wdenk38a24a62002-09-18 12:49:44 +0000286 }
287 return 0;
288 }
289
Simon Glass3ff49ec2021-07-24 09:03:29 -0600290 bank = hextoul(argv[1], NULL);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200291 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000292 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200293 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000294 return 1;
295 }
296 printf ("\nBank # %ld: ", bank);
Simon Glassc7443162020-05-10 11:39:53 -0600297 flash_print_info(&flash_info[bank - 1]);
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900298#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000299 return 0;
300}
Wolfgang Denk47f57792005-08-08 01:03:24 +0200301
Simon Glassed38aef2020-05-10 11:40:03 -0600302static int do_flerase(struct cmd_tbl *cmdtp, int flag, int argc,
303 char *const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000304{
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900305#ifdef CONFIG_MTD_NOR_FLASH
Wolfgang Denk57e219e2011-11-05 05:13:04 +0000306 flash_info_t *info = NULL;
wdenk38a24a62002-09-18 12:49:44 +0000307 ulong bank, addr_first, addr_last;
Wolfgang Denk57e219e2011-11-05 05:13:04 +0000308 int n, sect_first = 0, sect_last = 0;
Alexander Stein8967edf2010-08-11 16:48:04 +0200309#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200310 struct mtd_device *dev;
311 struct part_info *part;
312 u8 dev_type, dev_num, pnum;
313#endif
wdenk38a24a62002-09-18 12:49:44 +0000314 int rcode = 0;
315
Wolfgang Denk3b683112010-07-17 01:06:04 +0200316 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000317 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000318
319 if (strcmp(argv[1], "all") == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200320 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000321 printf ("Erase Flash Bank # %ld ", bank);
322 info = &flash_info[bank-1];
Simon Glassc7443162020-05-10 11:39:53 -0600323 rcode = flash_erase(info, 0, info->sector_count - 1);
wdenk38a24a62002-09-18 12:49:44 +0000324 }
325 return rcode;
326 }
327
328 if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
329 if (n < 0) {
wdenk42c05472004-03-23 22:14:11 +0000330 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000331 return 1;
332 }
Stefan Roese37628252008-08-06 14:05:38 +0200333 printf ("Erase Flash Sectors %d-%d in Bank # %zu ",
wdenk38a24a62002-09-18 12:49:44 +0000334 sect_first, sect_last, (info-flash_info)+1);
335 rcode = flash_erase(info, sect_first, sect_last);
336 return rcode;
337 }
338
Alexander Stein8967edf2010-08-11 16:48:04 +0200339#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200340 /* erase <part-id> - erase partition */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100341 if ((argc == 2) && (mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200342 mtdparts_init();
343 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
344 if (dev->id->type == MTD_DEV_TYPE_NOR) {
345 bank = dev->id->num;
346 info = &flash_info[bank];
347 addr_first = part->offset + info->start[0];
348 addr_last = addr_first + part->size - 1;
349
Wolfgang Denk161bfc52009-11-17 21:27:39 +0100350 printf ("Erase Flash Partition %s, "
Kim Phillipsf91aa8d2008-07-10 14:00:15 -0500351 "bank %ld, 0x%08lx - 0x%08lx ",
Wolfgang Denk47f57792005-08-08 01:03:24 +0200352 argv[1], bank, addr_first,
353 addr_last);
354
355 rcode = flash_sect_erase(addr_first, addr_last);
356 return rcode;
357 }
358
359 printf("cannot erase, not a NOR device\n");
360 return 1;
361 }
362 }
363#endif
364
Wolfgang Denk3b683112010-07-17 01:06:04 +0200365 if (argc != 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000366 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000367
368 if (strcmp(argv[1], "bank") == 0) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600369 bank = hextoul(argv[2], NULL);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200370 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000371 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200372 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000373 return 1;
374 }
375 printf ("Erase Flash Bank # %ld ", bank);
376 info = &flash_info[bank-1];
Simon Glassc7443162020-05-10 11:39:53 -0600377 rcode = flash_erase(info, 0, info->sector_count - 1);
wdenk38a24a62002-09-18 12:49:44 +0000378 return rcode;
379 }
380
Wolfgang Denk104866b2005-07-21 11:45:50 +0200381 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
382 printf ("Bad address format\n");
383 return 1;
384 }
wdenk38a24a62002-09-18 12:49:44 +0000385
Wolfgang Denk3b683112010-07-17 01:06:04 +0200386 if (addr_first >= addr_last)
Simon Glassa06dfc72011-12-10 08:44:01 +0000387 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000388
wdenk38a24a62002-09-18 12:49:44 +0000389 rcode = flash_sect_erase(addr_first, addr_last);
390 return rcode;
Stelian Popa63d2eb2008-03-26 22:52:35 +0100391#else
392 return 0;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900393#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000394}
395
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900396#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassc7443162020-05-10 11:39:53 -0600397int flash_sect_erase(ulong addr_first, ulong addr_last)
wdenk38a24a62002-09-18 12:49:44 +0000398{
399 flash_info_t *info;
400 ulong bank;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200401 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
wdenk0a658552003-08-05 17:43:17 +0000402 int erased = 0;
403 int planned;
wdenk38a24a62002-09-18 12:49:44 +0000404 int rcode = 0;
405
wdenk0a658552003-08-05 17:43:17 +0000406 rcode = flash_fill_sect_ranges (addr_first, addr_last,
407 s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000408
wdenk0a658552003-08-05 17:43:17 +0000409 if (planned && (rcode == 0)) {
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200410 for (bank=0,info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200411 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
wdenk0a658552003-08-05 17:43:17 +0000412 ++bank, ++info) {
413 if (s_first[bank]>=0) {
414 erased += s_last[bank] - s_first[bank] + 1;
Simon Glass8f055af2020-05-10 11:40:04 -0600415 debug("Erase Flash from 0x%08lx to 0x%08lx in Bank # %ld ",
416 info->start[s_first[bank]],
417 (s_last[bank] == info->sector_count) ?
418 info->start[0] + info->size - 1 :
419 info->start[s_last[bank] + 1] - 1,
420 bank + 1);
Simon Glassc7443162020-05-10 11:39:53 -0600421 rcode = flash_erase(info, s_first[bank],
422 s_last[bank]);
wdenk38a24a62002-09-18 12:49:44 +0000423 }
wdenk38a24a62002-09-18 12:49:44 +0000424 }
Joe Hershberger497c32f2012-08-17 15:36:41 -0500425 if (rcode == 0)
426 printf("Erased %d sectors\n", erased);
wdenk0a658552003-08-05 17:43:17 +0000427 } else if (rcode == 0) {
wdenk42c05472004-03-23 22:14:11 +0000428 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000429 " not on sector boundary\n");
430 rcode = 1;
431 }
432 return rcode;
433}
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900434#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000435
Simon Glassed38aef2020-05-10 11:40:03 -0600436static int do_protect(struct cmd_tbl *cmdtp, int flag, int argc,
437 char *const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000438{
Marek Vasut4c2d8992011-09-26 02:26:02 +0200439 int rcode = 0;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900440#ifdef CONFIG_MTD_NOR_FLASH
Wolfgang Denk57e219e2011-11-05 05:13:04 +0000441 flash_info_t *info = NULL;
Stelian Popa63d2eb2008-03-26 22:52:35 +0100442 ulong bank;
Wolfgang Denk57e219e2011-11-05 05:13:04 +0000443 int i, n, sect_first = 0, sect_last = 0;
Alexander Stein8967edf2010-08-11 16:48:04 +0200444#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200445 struct mtd_device *dev;
446 struct part_info *part;
447 u8 dev_type, dev_num, pnum;
448#endif
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900449#endif /* CONFIG_MTD_NOR_FLASH */
Tuomas Tynkkynen1b725202017-10-10 21:59:42 +0300450#if defined(CONFIG_MTD_NOR_FLASH)
Wolfgang Denka2f30822009-07-18 23:18:14 +0200451 int p;
Marek Vasut4c2d8992011-09-26 02:26:02 +0200452 ulong addr_first, addr_last;
453#endif
Stefan Roeseefef95b2006-04-01 13:41:03 +0200454
Wolfgang Denk3b683112010-07-17 01:06:04 +0200455 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000456 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000457
Tuomas Tynkkynen1b725202017-10-10 21:59:42 +0300458#if defined(CONFIG_MTD_NOR_FLASH)
Wolfgang Denk3b683112010-07-17 01:06:04 +0200459 if (strcmp(argv[1], "off") == 0)
wdenk38a24a62002-09-18 12:49:44 +0000460 p = 0;
Wolfgang Denk3b683112010-07-17 01:06:04 +0200461 else if (strcmp(argv[1], "on") == 0)
wdenk38a24a62002-09-18 12:49:44 +0000462 p = 1;
Wolfgang Denk3b683112010-07-17 01:06:04 +0200463 else
Simon Glassa06dfc72011-12-10 08:44:01 +0000464 return CMD_RET_USAGE;
Marek Vasut4c2d8992011-09-26 02:26:02 +0200465#endif
wdenk38a24a62002-09-18 12:49:44 +0000466
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900467#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000468 if (strcmp(argv[2], "all") == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200469 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
wdenk38a24a62002-09-18 12:49:44 +0000470 info = &flash_info[bank-1];
471 if (info->flash_id == FLASH_UNKNOWN) {
472 continue;
473 }
474 printf ("%sProtect Flash Bank # %ld\n",
475 p ? "" : "Un-", bank);
476
477 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200478#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000479 if (flash_real_protect(info, i, p))
480 rcode = 1;
481 putc ('.');
482#else
483 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200484#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000485 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200486#if defined(CONFIG_SYS_FLASH_PROTECTION)
Heiko Schocherfd9e97c2006-04-11 14:39:21 +0200487 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200488#endif /* CONFIG_SYS_FLASH_PROTECTION */
Heiko Schocherfd9e97c2006-04-11 14:39:21 +0200489 }
wdenk38a24a62002-09-18 12:49:44 +0000490 return rcode;
491 }
492
493 if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
494 if (n < 0) {
wdenk42c05472004-03-23 22:14:11 +0000495 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000496 return 1;
497 }
Stefan Roese37628252008-08-06 14:05:38 +0200498 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
wdenk38a24a62002-09-18 12:49:44 +0000499 p ? "" : "Un-", sect_first, sect_last,
500 (info-flash_info)+1);
501 for (i = sect_first; i <= sect_last; i++) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200502#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000503 if (flash_real_protect(info, i, p))
504 rcode = 1;
505 putc ('.');
506#else
507 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200508#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000509 }
510
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200511#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000512 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200513#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000514
515 return rcode;
516 }
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200517
Alexander Stein8967edf2010-08-11 16:48:04 +0200518#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200519 /* protect on/off <part-id> */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100520 if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200521 mtdparts_init();
522 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
523 if (dev->id->type == MTD_DEV_TYPE_NOR) {
524 bank = dev->id->num;
525 info = &flash_info[bank];
526 addr_first = part->offset + info->start[0];
527 addr_last = addr_first + part->size - 1;
528
Wolfgang Denk161bfc52009-11-17 21:27:39 +0100529 printf ("%sProtect Flash Partition %s, "
Kim Phillipsf91aa8d2008-07-10 14:00:15 -0500530 "bank %ld, 0x%08lx - 0x%08lx\n",
Wolfgang Denk47f57792005-08-08 01:03:24 +0200531 p ? "" : "Un", argv[1],
532 bank, addr_first, addr_last);
533
Simon Glassc7443162020-05-10 11:39:53 -0600534 rcode = flash_sect_protect(p, addr_first,
535 addr_last);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200536 return rcode;
537 }
538
539 printf("cannot %sprotect, not a NOR device\n",
540 p ? "" : "un");
541 return 1;
542 }
543 }
544#endif
wdenk38a24a62002-09-18 12:49:44 +0000545
Wolfgang Denk3b683112010-07-17 01:06:04 +0200546 if (argc != 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000547 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000548
549 if (strcmp(argv[2], "bank") == 0) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600550 bank = hextoul(argv[3], NULL);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200551 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
wdenk38a24a62002-09-18 12:49:44 +0000552 printf ("Only FLASH Banks # 1 ... # %d supported\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200553 CONFIG_SYS_MAX_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000554 return 1;
555 }
556 printf ("%sProtect Flash Bank # %ld\n",
557 p ? "" : "Un-", bank);
558 info = &flash_info[bank-1];
559
560 if (info->flash_id == FLASH_UNKNOWN) {
wdenk42c05472004-03-23 22:14:11 +0000561 puts ("missing or unknown FLASH type\n");
wdenk38a24a62002-09-18 12:49:44 +0000562 return 1;
563 }
564 for (i=0; i<info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200565#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000566 if (flash_real_protect(info, i, p))
567 rcode = 1;
568 putc ('.');
569#else
570 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200571#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000572 }
573
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200574#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000575 if (!rcode) puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200576#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000577
578 return rcode;
579 }
580
Wolfgang Denk104866b2005-07-21 11:45:50 +0200581 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
582 printf("Bad address format\n");
583 return 1;
584 }
wdenk38a24a62002-09-18 12:49:44 +0000585
Wolfgang Denk3b683112010-07-17 01:06:04 +0200586 if (addr_first >= addr_last)
Simon Glassa06dfc72011-12-10 08:44:01 +0000587 return CMD_RET_USAGE;
Wolfgang Denk3b683112010-07-17 01:06:04 +0200588
Simon Glassc7443162020-05-10 11:39:53 -0600589 rcode = flash_sect_protect(p, addr_first, addr_last);
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900590#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000591 return rcode;
592}
593
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900594#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassc7443162020-05-10 11:39:53 -0600595int flash_sect_protect(int p, ulong addr_first, ulong addr_last)
wdenk38a24a62002-09-18 12:49:44 +0000596{
597 flash_info_t *info;
598 ulong bank;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200599 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
wdenk38a24a62002-09-18 12:49:44 +0000600 int protected, i;
wdenk0a658552003-08-05 17:43:17 +0000601 int planned;
602 int rcode;
wdenk38a24a62002-09-18 12:49:44 +0000603
wdenk0a658552003-08-05 17:43:17 +0000604 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000605
wdenk0a658552003-08-05 17:43:17 +0000606 protected = 0;
wdenk38a24a62002-09-18 12:49:44 +0000607
wdenk0a658552003-08-05 17:43:17 +0000608 if (planned && (rcode == 0)) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200609 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
wdenk0a658552003-08-05 17:43:17 +0000610 if (info->flash_id == FLASH_UNKNOWN) {
wdenk38a24a62002-09-18 12:49:44 +0000611 continue;
wdenk38a24a62002-09-18 12:49:44 +0000612 }
wdenk0a658552003-08-05 17:43:17 +0000613
614 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
Simon Glass8f055af2020-05-10 11:40:04 -0600615 debug("%sProtecting sectors %d..%d in bank %ld\n",
616 p ? "" : "Un-", s_first[bank],
617 s_last[bank], bank + 1);
wdenk0a658552003-08-05 17:43:17 +0000618 protected += s_last[bank] - s_first[bank] + 1;
619 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200620#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk0a658552003-08-05 17:43:17 +0000621 if (flash_real_protect(info, i, p))
622 rcode = 1;
623 putc ('.');
wdenk38a24a62002-09-18 12:49:44 +0000624#else
wdenk0a658552003-08-05 17:43:17 +0000625 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200626#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk0a658552003-08-05 17:43:17 +0000627 }
wdenk38a24a62002-09-18 12:49:44 +0000628 }
Stefan Roeseefef95b2006-04-01 13:41:03 +0200629 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200630#if defined(CONFIG_SYS_FLASH_PROTECTION)
Stefan Roeseefef95b2006-04-01 13:41:03 +0200631 puts (" done\n");
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200632#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000633
wdenk38a24a62002-09-18 12:49:44 +0000634 printf ("%sProtected %d sectors\n",
635 p ? "" : "Un-", protected);
wdenk0a658552003-08-05 17:43:17 +0000636 } else if (rcode == 0) {
wdenk42c05472004-03-23 22:14:11 +0000637 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000638 " not on sector boundary\n");
639 rcode = 1;
640 }
641 return rcode;
642}
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900643#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000644
wdenk57b2d802003-06-27 21:31:46 +0000645
646/**************************************************/
Alexander Stein8967edf2010-08-11 16:48:04 +0200647#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk16439ee2005-08-09 17:25:22 +0200648# define TMP_ERASE "erase <part-id>\n - erase partition\n"
649# define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
650# define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
651#else
652# define TMP_ERASE /* empty */
653# define TMP_PROT_ON /* empty */
654# define TMP_PROT_OFF /* empty */
655#endif
wdenk57b2d802003-06-27 21:31:46 +0000656
wdenkf287a242003-07-01 21:06:45 +0000657U_BOOT_CMD(
658 flinfo, 2, 1, do_flinfo,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600659 "print FLASH memory information",
wdenk57b2d802003-06-27 21:31:46 +0000660 "\n - print information for all FLASH memory banks\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200661 "flinfo N\n - print information for FLASH memory bank # N"
wdenk57b2d802003-06-27 21:31:46 +0000662);
663
wdenkf287a242003-07-01 21:06:45 +0000664U_BOOT_CMD(
Detlev Zundel409a2852007-05-23 19:02:41 +0200665 erase, 3, 0, do_flerase,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600666 "erase FLASH memory",
wdenk57b2d802003-06-27 21:31:46 +0000667 "start end\n"
668 " - erase FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denk104866b2005-07-21 11:45:50 +0200669 "erase start +len\n"
670 " - erase FLASH from addr 'start' to the end of sect "
671 "w/addr 'start'+'len'-1\n"
wdenk57b2d802003-06-27 21:31:46 +0000672 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
673 "erase bank N\n - erase FLASH bank # N\n"
Wolfgang Denk16439ee2005-08-09 17:25:22 +0200674 TMP_ERASE
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200675 "erase all\n - erase all FLASH banks"
wdenk57b2d802003-06-27 21:31:46 +0000676);
677
wdenkf287a242003-07-01 21:06:45 +0000678U_BOOT_CMD(
Detlev Zundel409a2852007-05-23 19:02:41 +0200679 protect, 4, 0, do_protect,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600680 "enable or disable FLASH write protection",
wdenk57b2d802003-06-27 21:31:46 +0000681 "on start end\n"
682 " - protect FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denk104866b2005-07-21 11:45:50 +0200683 "protect on start +len\n"
684 " - protect FLASH from addr 'start' to end of sect "
685 "w/addr 'start'+'len'-1\n"
wdenk57b2d802003-06-27 21:31:46 +0000686 "protect on N:SF[-SL]\n"
687 " - protect sectors SF-SL in FLASH bank # N\n"
688 "protect on bank N\n - protect FLASH bank # N\n"
Wolfgang Denk16439ee2005-08-09 17:25:22 +0200689 TMP_PROT_ON
wdenk57b2d802003-06-27 21:31:46 +0000690 "protect on all\n - protect all FLASH banks\n"
691 "protect off start end\n"
692 " - make FLASH from addr 'start' to addr 'end' writable\n"
Wolfgang Denk104866b2005-07-21 11:45:50 +0200693 "protect off start +len\n"
694 " - make FLASH from addr 'start' to end of sect "
695 "w/addr 'start'+'len'-1 wrtable\n"
wdenk57b2d802003-06-27 21:31:46 +0000696 "protect off N:SF[-SL]\n"
697 " - make sectors SF-SL writable in FLASH bank # N\n"
698 "protect off bank N\n - make FLASH bank # N writable\n"
Wolfgang Denk16439ee2005-08-09 17:25:22 +0200699 TMP_PROT_OFF
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200700 "protect off all\n - make all FLASH banks writable"
wdenk57b2d802003-06-27 21:31:46 +0000701);
702
Wolfgang Denk16439ee2005-08-09 17:25:22 +0200703#undef TMP_ERASE
704#undef TMP_PROT_ON
705#undef TMP_PROT_OFF