blob: f4f85ecc7a8e0613c63e55406098a60726b879ef [file] [log] [blame]
Tom Rini83d290c2018-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 Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060013#include <uuid.h>
wdenk8bde7f72003-06-27 21:31:46 +000014
Alexander Steinc0008082010-08-11 16:48:04 +020015#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +020016#include <jffs2/jffs2.h>
17
Wolfgang Denk445093d2009-11-17 21:27:39 +010018/* partition handling routines */
Wolfgang Denk700a0c62005-08-08 01:03:24 +020019int mtdparts_init(void);
Stefan Roese68d7d652009-03-19 13:30:36 +010020int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
Wolfgang Denk700a0c62005-08-08 01:03:24 +020021int find_dev_and_part(const char *id, struct mtd_device **dev,
Patrick Delaunayc8363b12022-01-04 14:23:57 +010022 u8 *part_num, struct part_info **part);
Wolfgang Denk700a0c62005-08-08 01:03:24 +020023#endif
24
Masahiro Yamadae856bdc2017-02-11 22:43:54 +090025#ifdef CONFIG_MTD_NOR_FLASH
Stefan Roese3c299752010-08-31 10:04:11 +020026#include <flash.h>
27#include <mtd/cfi_flash.h>
wdenk38a24a62002-09-18 12:49:44 +000028
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
Patrick Delaunayc8363b12022-01-04 14:23:57 +010049abbrev_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
Patrick Delaunayc8363b12022-01-04 14:23:57 +010055 p = strchr(str, ':');
56 if (!p)
wdenkbdccc4f2003-08-05 17:43:17 +000057 return 0;
wdenk38a24a62002-09-18 12:49:44 +000058 *p++ = '\0';
59
Simon Glass0b1284e2021-07-24 09:03:30 -060060 bank = dectoul(str, &ep);
wdenkbdccc4f2003-08-05 17:43:17 +000061 if (ep == str || *ep != '\0' ||
Patrick Delaunay98150e72022-01-04 14:23:58 +010062 bank < 1 || bank > CFI_FLASH_BANKS)
Patrick Delaunayc8363b12022-01-04 14:23:57 +010063 return -1;
64
65 fp = &flash_info[bank - 1];
66 if (fp->flash_id == FLASH_UNKNOWN)
wdenkbdccc4f2003-08-05 17:43:17 +000067 return -1;
wdenk38a24a62002-09-18 12:49:44 +000068
wdenkbdccc4f2003-08-05 17:43:17 +000069 str = p;
Patrick Delaunayc8363b12022-01-04 14:23:57 +010070 p = strchr(str, '-');
71 if (p)
wdenkbdccc4f2003-08-05 17:43:17 +000072 *p++ = '\0';
wdenk38a24a62002-09-18 12:49:44 +000073
Simon Glass0b1284e2021-07-24 09:03:30 -060074 first = dectoul(str, &ep);
wdenkbdccc4f2003-08-05 17:43:17 +000075 if (ep == str || *ep != '\0' || first >= fp->sector_count)
76 return -1;
wdenk38a24a62002-09-18 12:49:44 +000077
Patrick Delaunayc8363b12022-01-04 14:23:57 +010078 if (p) {
Simon Glass0b1284e2021-07-24 09:03:30 -060079 last = dectoul(p, &ep);
wdenkbdccc4f2003-08-05 17:43:17 +000080 if (ep == p || *ep != '\0' ||
Patrick Delaunayc8363b12022-01-04 14:23:57 +010081 last < first || last >= fp->sector_count)
wdenkbdccc4f2003-08-05 17:43:17 +000082 return -1;
83 } else {
84 last = first;
85 }
86
87 *pinfo = fp;
88 *psf = first;
89 *psl = last;
90
91 return 1;
wdenk38a24a62002-09-18 12:49:44 +000092}
wdenkbdccc4f2003-08-05 17:43:17 +000093
Wolfgang Denkf5301872005-07-21 11:45:50 +020094/*
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +020095 * Take *addr in Flash and adjust it to fall on the end of its sector
96 */
Simon Glassa595a0e2020-05-10 11:39:53 -060097int flash_sect_roundb(ulong *addr)
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +020098{
99 flash_info_t *info;
100 ulong bank, sector_end_addr;
101 char found;
102 int i;
103
104 /* find the end addr of the sector where the *addr is */
105 found = 0;
Patrick Delaunay98150e72022-01-04 14:23:58 +0100106 for (bank = 0; bank < CFI_FLASH_BANKS && !found; ++bank) {
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200107 info = &flash_info[bank];
108 for (i = 0; i < info->sector_count && !found; ++i) {
109 /* get the end address of the sector */
110 if (i == info->sector_count - 1) {
111 sector_end_addr = info->start[0] +
112 info->size - 1;
113 } else {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100114 sector_end_addr = info->start[i + 1] - 1;
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200115 }
116
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100117 if (*addr <= sector_end_addr && *addr >= info->start[i]) {
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200118 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 Williams16263082011-07-22 04:01:30 +0000126 /* error, address not in flash */
Bartlomiej Sieka3f0cf512008-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 Denkf5301872005-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 Denk095b8a32005-08-02 17:06:17 +0200139 * 2. <cmd> start +length - operate on <'start', start + length)
Wolfgang Denkf5301872005-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).
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100150 */
Wolfgang Denkf5301872005-07-21 11:45:50 +0200151static int
152addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
153{
154 char *ep;
Heiko Schocherbb741402006-04-11 14:39:21 +0200155 char len_used; /* indicates if the "start +length" form used */
Wolfgang Denk2c61f142005-08-05 11:10:31 +0200156
Simon Glass7e5f4602021-07-24 09:03:29 -0600157 *addr_first = hextoul(arg1, &ep);
Wolfgang Denkf5301872005-07-21 11:45:50 +0200158 if (ep == arg1 || *ep != '\0')
159 return -1;
160
Heiko Schocherbb741402006-04-11 14:39:21 +0200161 len_used = 0;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100162 if (arg2 && *arg2 == '+') {
Wolfgang Denkf5301872005-07-21 11:45:50 +0200163 len_used = 1;
164 ++arg2;
165 }
166
Simon Glass7e5f4602021-07-24 09:03:29 -0600167 *addr_last = hextoul(arg2, &ep);
Wolfgang Denkf5301872005-07-21 11:45:50 +0200168 if (ep == arg2 || *ep != '\0')
169 return -1;
170
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100171 if (len_used) {
Wolfgang Denkf5301872005-07-21 11:45:50 +0200172 /*
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 Sieka3f0cf512008-10-01 15:26:27 +0200185 if (flash_sect_roundb(addr_last) > 0)
Wolfgang Denkf5301872005-07-21 11:45:50 +0200186 return -1;
Wolfgang Denkf5301872005-07-21 11:45:50 +0200187 } /* "start +length" from used */
188
189 return 1;
190}
191
wdenkbdccc4f2003-08-05 17:43:17 +0000192static int
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100193flash_fill_sect_ranges(ulong addr_first, ulong addr_last,
194 int *s_first, int *s_last,
195 int *s_count)
wdenkbdccc4f2003-08-05 17:43:17 +0000196{
197 flash_info_t *info;
198 ulong bank;
199 int rcode = 0;
200
201 *s_count = 0;
202
Patrick Delaunay98150e72022-01-04 14:23:58 +0100203 for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) {
wdenkbdccc4f2003-08-05 17:43:17 +0000204 s_first[bank] = -1; /* first sector to erase */
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100205 s_last[bank] = -1; /* last sector to erase */
wdenkbdccc4f2003-08-05 17:43:17 +0000206 }
207
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100208 for (bank = 0, info = &flash_info[0];
Patrick Delaunay98150e72022-01-04 14:23:58 +0100209 (bank < CFI_FLASH_BANKS) && (addr_first <= addr_last);
wdenkbdccc4f2003-08-05 17:43:17 +0000210 ++bank, ++info) {
211 ulong b_end;
212 int sect;
213 short s_end;
214
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100215 if (info->flash_id == FLASH_UNKNOWN)
wdenkbdccc4f2003-08-05 17:43:17 +0000216 continue;
wdenkbdccc4f2003-08-05 17:43:17 +0000217
218 b_end = info->start[0] + info->size - 1; /* bank end addr */
219 s_end = info->sector_count - 1; /* last sector */
220
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100221 for (sect = 0; sect < info->sector_count; ++sect) {
wdenkbdccc4f2003-08-05 17:43:17 +0000222 ulong end; /* last address in current sect */
223
224 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
225
226 if (addr_first > end)
227 continue;
228 if (addr_last < info->start[sect])
229 continue;
230
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100231 if (addr_first == info->start[sect])
wdenkbdccc4f2003-08-05 17:43:17 +0000232 s_first[bank] = sect;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100233
234 if (addr_last == end)
wdenkbdccc4f2003-08-05 17:43:17 +0000235 s_last[bank] = sect;
wdenkbdccc4f2003-08-05 17:43:17 +0000236 }
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 {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100242 puts("Error: end address not on sector boundary\n");
wdenkbdccc4f2003-08-05 17:43:17 +0000243 rcode = 1;
244 break;
245 }
246 }
247 if (s_last[bank] < s_first[bank]) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100248 puts("Error: end sector precedes start sector\n");
wdenkbdccc4f2003-08-05 17:43:17 +0000249 rcode = 1;
250 break;
251 }
252 sect = s_last[bank];
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100253 addr_first = (sect == s_end) ? b_end + 1 : info->start[sect + 1];
wdenkbdccc4f2003-08-05 17:43:17 +0000254 (*s_count) += s_last[bank] - s_first[bank] + 1;
wdenke2ffd592004-12-31 09:32:47 +0000255 } else if (addr_first >= info->start[0] && addr_first < b_end) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100256 puts("Error: start address not on sector boundary\n");
wdenke2ffd592004-12-31 09:32:47 +0000257 rcode = 1;
258 break;
wdenk2d5b5612003-10-14 19:43:55 +0000259 } else if (s_last[bank] >= 0) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100260 puts("Error: cannot span across banks when they are"
261 " mapped in reverse order\n");
wdenk2d5b5612003-10-14 19:43:55 +0000262 rcode = 1;
263 break;
wdenkbdccc4f2003-08-05 17:43:17 +0000264 }
265 }
266
267 return rcode;
268}
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900269#endif /* CONFIG_MTD_NOR_FLASH */
wdenkbdccc4f2003-08-05 17:43:17 +0000270
Simon Glass09140112020-05-10 11:40:03 -0600271static int do_flinfo(struct cmd_tbl *cmdtp, int flag, int argc,
272 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 */
Patrick Delaunay98150e72022-01-04 14:23:58 +0100280 for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100281 printf("\nBank # %ld: ", bank + 1);
wdenk38a24a62002-09-18 12:49:44 +0000282
Simon Glassa595a0e2020-05-10 11:39:53 -0600283 flash_print_info(&flash_info[bank]);
wdenk38a24a62002-09-18 12:49:44 +0000284 }
285 return 0;
286 }
287
Simon Glass7e5f4602021-07-24 09:03:29 -0600288 bank = hextoul(argv[1], NULL);
Patrick Delaunay98150e72022-01-04 14:23:58 +0100289 if (bank < 1 || bank > CFI_FLASH_BANKS) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100290 printf("Only FLASH Banks # 1 ... # %d supported\n",
Patrick Delaunay98150e72022-01-04 14:23:58 +0100291 CFI_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000292 return 1;
293 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100294 printf("\nBank # %ld: ", bank);
Simon Glassa595a0e2020-05-10 11:39:53 -0600295 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
Simon Glass09140112020-05-10 11:40:03 -0600300static int do_flerase(struct cmd_tbl *cmdtp, int flag, int argc,
301 char *const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000302{
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900303#ifdef CONFIG_MTD_NOR_FLASH
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000304 flash_info_t *info = NULL;
wdenk38a24a62002-09-18 12:49:44 +0000305 ulong bank, addr_first, addr_last;
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000306 int n, sect_first = 0, sect_last = 0;
Alexander Steinc0008082010-08-11 16:48:04 +0200307#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200308 struct mtd_device *dev;
309 struct part_info *part;
310 u8 dev_type, dev_num, pnum;
311#endif
wdenk38a24a62002-09-18 12:49:44 +0000312 int rcode = 0;
313
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200314 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000315 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000316
317 if (strcmp(argv[1], "all") == 0) {
Patrick Delaunay98150e72022-01-04 14:23:58 +0100318 for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100319 printf("Erase Flash Bank # %ld ", bank);
320 info = &flash_info[bank - 1];
Simon Glassa595a0e2020-05-10 11:39:53 -0600321 rcode = flash_erase(info, 0, info->sector_count - 1);
wdenk38a24a62002-09-18 12:49:44 +0000322 }
323 return rcode;
324 }
325
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100326 n = abbrev_spec(argv[1], &info, &sect_first, &sect_last);
327 if (n) {
wdenk38a24a62002-09-18 12:49:44 +0000328 if (n < 0) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100329 puts("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000330 return 1;
331 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100332 printf("Erase Flash Sectors %d-%d in Bank # %zu ",
333 sect_first, sect_last, (info - flash_info) + 1);
wdenk38a24a62002-09-18 12:49:44 +0000334 rcode = flash_erase(info, sect_first, sect_last);
335 return rcode;
336 }
337
Alexander Steinc0008082010-08-11 16:48:04 +0200338#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200339 /* erase <part-id> - erase partition */
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100340 if (argc == 2 && mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200341 mtdparts_init();
342 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
343 if (dev->id->type == MTD_DEV_TYPE_NOR) {
344 bank = dev->id->num;
345 info = &flash_info[bank];
346 addr_first = part->offset + info->start[0];
347 addr_last = addr_first + part->size - 1;
348
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100349 printf("Erase Flash Partition %s, bank %ld, 0x%08lx - 0x%08lx ",
350 argv[1], bank, addr_first,
351 addr_last);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200352
353 rcode = flash_sect_erase(addr_first, addr_last);
354 return rcode;
355 }
356
357 printf("cannot erase, not a NOR device\n");
358 return 1;
359 }
360 }
361#endif
362
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200363 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000364 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000365
366 if (strcmp(argv[1], "bank") == 0) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600367 bank = hextoul(argv[2], NULL);
Patrick Delaunay98150e72022-01-04 14:23:58 +0100368 if (bank < 1 || bank > CFI_FLASH_BANKS) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100369 printf("Only FLASH Banks # 1 ... # %d supported\n",
Patrick Delaunay98150e72022-01-04 14:23:58 +0100370 CFI_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000371 return 1;
372 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100373 printf("Erase Flash Bank # %ld ", bank);
374 info = &flash_info[bank - 1];
Simon Glassa595a0e2020-05-10 11:39:53 -0600375 rcode = flash_erase(info, 0, info->sector_count - 1);
wdenk38a24a62002-09-18 12:49:44 +0000376 return rcode;
377 }
378
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100379 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0) {
380 printf("Bad address format\n");
Wolfgang Denkf5301872005-07-21 11:45:50 +0200381 return 1;
382 }
wdenk38a24a62002-09-18 12:49:44 +0000383
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200384 if (addr_first >= addr_last)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000385 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000386
wdenk38a24a62002-09-18 12:49:44 +0000387 rcode = flash_sect_erase(addr_first, addr_last);
388 return rcode;
Stelian Pop880cc432008-03-26 22:52:35 +0100389#else
390 return 0;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900391#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000392}
393
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900394#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassa595a0e2020-05-10 11:39:53 -0600395int flash_sect_erase(ulong addr_first, ulong addr_last)
wdenk38a24a62002-09-18 12:49:44 +0000396{
397 flash_info_t *info;
398 ulong bank;
Patrick Delaunay98150e72022-01-04 14:23:58 +0100399 int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS];
wdenkbdccc4f2003-08-05 17:43:17 +0000400 int erased = 0;
401 int planned;
wdenk38a24a62002-09-18 12:49:44 +0000402 int rcode = 0;
403
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100404 rcode = flash_fill_sect_ranges(addr_first, addr_last, s_first, s_last, &planned);
wdenk38a24a62002-09-18 12:49:44 +0000405
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100406 if (planned && rcode == 0) {
407 for (bank = 0, info = &flash_info[0];
Patrick Delaunay98150e72022-01-04 14:23:58 +0100408 bank < CFI_FLASH_BANKS && rcode == 0;
wdenkbdccc4f2003-08-05 17:43:17 +0000409 ++bank, ++info) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100410 if (s_first[bank] >= 0) {
wdenkbdccc4f2003-08-05 17:43:17 +0000411 erased += s_last[bank] - s_first[bank] + 1;
Simon Glass3c7dded2020-05-10 11:40:04 -0600412 debug("Erase Flash from 0x%08lx to 0x%08lx in Bank # %ld ",
413 info->start[s_first[bank]],
414 (s_last[bank] == info->sector_count) ?
415 info->start[0] + info->size - 1 :
416 info->start[s_last[bank] + 1] - 1,
417 bank + 1);
Simon Glassa595a0e2020-05-10 11:39:53 -0600418 rcode = flash_erase(info, s_first[bank],
419 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) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100425 puts("Error: start and/or end address not on sector boundary\n");
wdenk38a24a62002-09-18 12:49:44 +0000426 rcode = 1;
427 }
428 return rcode;
429}
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900430#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000431
Simon Glass09140112020-05-10 11:40:03 -0600432static int do_protect(struct cmd_tbl *cmdtp, int flag, int argc,
433 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) {
Patrick Delaunay98150e72022-01-04 14:23:58 +0100465 for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100466 info = &flash_info[bank - 1];
467 if (info->flash_id == FLASH_UNKNOWN)
wdenk38a24a62002-09-18 12:49:44 +0000468 continue;
wdenk38a24a62002-09-18 12:49:44 +0000469
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100470 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;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100477 putc('.');
wdenk38a24a62002-09-18 12:49:44 +0000478#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)
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100483 if (!rcode)
484 puts(" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200485#endif /* CONFIG_SYS_FLASH_PROTECTION */
Heiko Schocherbb741402006-04-11 14:39:21 +0200486 }
wdenk38a24a62002-09-18 12:49:44 +0000487 return rcode;
488 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100489 n = abbrev_spec(argv[2], &info, &sect_first, &sect_last);
490 if (n) {
wdenk38a24a62002-09-18 12:49:44 +0000491 if (n < 0) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100492 puts("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000493 return 1;
494 }
Stefan Roesef2302d42008-08-06 14:05:38 +0200495 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100496 p ? "" : "Un-", sect_first, sect_last,
497 (info - flash_info) + 1);
wdenk38a24a62002-09-18 12:49:44 +0000498 for (i = sect_first; i <= sect_last; i++) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200499#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000500 if (flash_real_protect(info, i, p))
501 rcode = 1;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100502 putc('.');
wdenk38a24a62002-09-18 12:49:44 +0000503#else
504 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200505#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000506 }
507
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200508#if defined(CONFIG_SYS_FLASH_PROTECTION)
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100509 if (!rcode)
510 puts(" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200511#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000512
513 return rcode;
514 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200515
Alexander Steinc0008082010-08-11 16:48:04 +0200516#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200517 /* protect on/off <part-id> */
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100518 if (argc == 3 && mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200519 mtdparts_init();
520 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
521 if (dev->id->type == MTD_DEV_TYPE_NOR) {
522 bank = dev->id->num;
523 info = &flash_info[bank];
524 addr_first = part->offset + info->start[0];
525 addr_last = addr_first + part->size - 1;
526
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100527 printf("%sProtect Flash Partition %s, "
528 "bank %ld, 0x%08lx - 0x%08lx\n",
529 p ? "" : "Un", argv[1],
530 bank, addr_first, addr_last);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200531
Simon Glassa595a0e2020-05-10 11:39:53 -0600532 rcode = flash_sect_protect(p, addr_first,
533 addr_last);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200534 return rcode;
535 }
536
537 printf("cannot %sprotect, not a NOR device\n",
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100538 p ? "" : "un");
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200539 return 1;
540 }
541 }
542#endif
wdenk38a24a62002-09-18 12:49:44 +0000543
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200544 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000545 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000546
547 if (strcmp(argv[2], "bank") == 0) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600548 bank = hextoul(argv[3], NULL);
Patrick Delaunay98150e72022-01-04 14:23:58 +0100549 if (bank < 1 || bank > CFI_FLASH_BANKS) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100550 printf("Only FLASH Banks # 1 ... # %d supported\n",
Patrick Delaunay98150e72022-01-04 14:23:58 +0100551 CFI_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000552 return 1;
553 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100554 printf("%sProtect Flash Bank # %ld\n",
555 p ? "" : "Un-", bank);
556 info = &flash_info[bank - 1];
wdenk38a24a62002-09-18 12:49:44 +0000557
558 if (info->flash_id == FLASH_UNKNOWN) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100559 puts("missing or unknown FLASH type\n");
wdenk38a24a62002-09-18 12:49:44 +0000560 return 1;
561 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100562 for (i = 0; i < info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200563#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000564 if (flash_real_protect(info, i, p))
565 rcode = 1;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100566 putc('.');
wdenk38a24a62002-09-18 12:49:44 +0000567#else
568 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200569#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000570 }
571
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200572#if defined(CONFIG_SYS_FLASH_PROTECTION)
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100573 if (!rcode)
574 puts(" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200575#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000576
577 return rcode;
578 }
579
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100580 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0) {
Wolfgang Denkf5301872005-07-21 11:45:50 +0200581 printf("Bad address format\n");
582 return 1;
583 }
wdenk38a24a62002-09-18 12:49:44 +0000584
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200585 if (addr_first >= addr_last)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000586 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200587
Simon Glassa595a0e2020-05-10 11:39:53 -0600588 rcode = flash_sect_protect(p, addr_first, addr_last);
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900589#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000590 return rcode;
591}
592
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900593#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassa595a0e2020-05-10 11:39:53 -0600594int flash_sect_protect(int p, ulong addr_first, ulong addr_last)
wdenk38a24a62002-09-18 12:49:44 +0000595{
596 flash_info_t *info;
597 ulong bank;
Patrick Delaunay98150e72022-01-04 14:23:58 +0100598 int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS];
wdenk38a24a62002-09-18 12:49:44 +0000599 int protected, i;
wdenkbdccc4f2003-08-05 17:43:17 +0000600 int planned;
601 int rcode;
602
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100603 rcode = flash_fill_sect_ranges(addr_first, addr_last, s_first, s_last, &planned);
wdenk38a24a62002-09-18 12:49:44 +0000604
605 protected = 0;
606
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100607 if (planned && rcode == 0) {
608 for (bank = 0, info = &flash_info[0];
Patrick Delaunay98150e72022-01-04 14:23:58 +0100609 bank < CFI_FLASH_BANKS; ++bank, ++info) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100610 if (info->flash_id == FLASH_UNKNOWN)
wdenk38a24a62002-09-18 12:49:44 +0000611 continue;
wdenk38a24a62002-09-18 12:49:44 +0000612
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100613 if (s_first[bank] >= 0 && s_first[bank] <= s_last[bank]) {
Simon Glass3c7dded2020-05-10 11:40:04 -0600614 debug("%sProtecting sectors %d..%d in bank %ld\n",
615 p ? "" : "Un-", s_first[bank],
616 s_last[bank], bank + 1);
wdenkbdccc4f2003-08-05 17:43:17 +0000617 protected += s_last[bank] - s_first[bank] + 1;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100618 for (i = s_first[bank]; i <= s_last[bank]; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200619#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkbdccc4f2003-08-05 17:43:17 +0000620 if (flash_real_protect(info, i, p))
621 rcode = 1;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100622 putc('.');
wdenk38a24a62002-09-18 12:49:44 +0000623#else
wdenkbdccc4f2003-08-05 17:43:17 +0000624 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200625#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenkbdccc4f2003-08-05 17:43:17 +0000626 }
wdenk38a24a62002-09-18 12:49:44 +0000627 }
wdenkbdccc4f2003-08-05 17:43:17 +0000628 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200629#if defined(CONFIG_SYS_FLASH_PROTECTION)
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100630 puts(" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200631#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000632
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100633 printf("%sProtected %d sectors\n",
634 p ? "" : "Un-", protected);
wdenkbdccc4f2003-08-05 17:43:17 +0000635 } else if (rcode == 0) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100636 puts("Error: start and/or end address not on sector boundary\n");
wdenk38a24a62002-09-18 12:49:44 +0000637 rcode = 1;
638 }
639 return rcode;
640}
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900641#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000642
wdenk8bde7f72003-06-27 21:31:46 +0000643/**************************************************/
Alexander Steinc0008082010-08-11 16:48:04 +0200644#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200645# define TMP_ERASE "erase <part-id>\n - erase partition\n"
646# define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
647# define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
648#else
649# define TMP_ERASE /* empty */
650# define TMP_PROT_ON /* empty */
651# define TMP_PROT_OFF /* empty */
652#endif
wdenk8bde7f72003-06-27 21:31:46 +0000653
wdenk0d498392003-07-01 21:06:45 +0000654U_BOOT_CMD(
655 flinfo, 2, 1, do_flinfo,
Peter Tyser2fb26042009-01-27 18:03:12 -0600656 "print FLASH memory information",
wdenk8bde7f72003-06-27 21:31:46 +0000657 "\n - print information for all FLASH memory banks\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200658 "flinfo N\n - print information for FLASH memory bank # N"
wdenk8bde7f72003-06-27 21:31:46 +0000659);
660
wdenk0d498392003-07-01 21:06:45 +0000661U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200662 erase, 3, 0, do_flerase,
Peter Tyser2fb26042009-01-27 18:03:12 -0600663 "erase FLASH memory",
wdenk8bde7f72003-06-27 21:31:46 +0000664 "start end\n"
665 " - erase FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200666 "erase start +len\n"
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100667 " - erase FLASH from addr 'start' to the end of sect w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000668 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
669 "erase bank N\n - erase FLASH bank # N\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200670 TMP_ERASE
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200671 "erase all\n - erase all FLASH banks"
wdenk8bde7f72003-06-27 21:31:46 +0000672);
673
wdenk0d498392003-07-01 21:06:45 +0000674U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200675 protect, 4, 0, do_protect,
Peter Tyser2fb26042009-01-27 18:03:12 -0600676 "enable or disable FLASH write protection",
wdenk8bde7f72003-06-27 21:31:46 +0000677 "on start end\n"
678 " - protect FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200679 "protect on start +len\n"
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100680 " - protect FLASH from addr 'start' to end of sect 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"
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100689 " - make FLASH from addr 'start' to end of sect w/addr 'start'+'len'-1 wrtable\n"
wdenk8bde7f72003-06-27 21:31:46 +0000690 "protect off N:SF[-SL]\n"
691 " - make sectors SF-SL writable in FLASH bank # N\n"
692 "protect off bank N\n - make FLASH bank # N writable\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200693 TMP_PROT_OFF
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200694 "protect off all\n - make all FLASH banks writable"
wdenk8bde7f72003-06-27 21:31:46 +0000695);
696
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200697#undef TMP_ERASE
698#undef TMP_PROT_ON
699#undef TMP_PROT_OFF