]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
38a24a61 WD |
2 | /* |
3 | * (C) Copyright 2000 | |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
38a24a61 WD |
5 | */ |
6 | ||
7 | /* | |
8 | * FLASH support | |
9 | */ | |
10 | #include <common.h> | |
11 | #include <command.h> | |
f7ae49fc | 12 | #include <log.h> |
b79fdc76 | 13 | #include <uuid.h> |
8bde7f77 | 14 | |
c000808b | 15 | #if defined(CONFIG_CMD_MTDPARTS) |
700a0c64 WD |
16 | #include <jffs2/jffs2.h> |
17 | ||
445093d1 | 18 | /* partition handling routines */ |
700a0c64 | 19 | int mtdparts_init(void); |
68d7d651 | 20 | int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num); |
700a0c64 | 21 | int find_dev_and_part(const char *id, struct mtd_device **dev, |
c8363b12 | 22 | u8 *part_num, struct part_info **part); |
700a0c64 WD |
23 | #endif |
24 | ||
e856bdcf | 25 | #ifdef CONFIG_MTD_NOR_FLASH |
3c29975e SR |
26 | #include <flash.h> |
27 | #include <mtd/cfi_flash.h> | |
38a24a61 WD |
28 | extern 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 | */ | |
49 | static int | |
c8363b12 | 50 | abbrev_spec(char *str, flash_info_t **pinfo, int *psf, int *psl) |
38a24a61 | 51 | { |
bdccc4fe WD |
52 | flash_info_t *fp; |
53 | int bank, first, last; | |
54 | char *p, *ep; | |
38a24a61 | 55 | |
c8363b12 PD |
56 | p = strchr(str, ':'); |
57 | if (!p) | |
bdccc4fe WD |
58 | return 0; |
59 | *p++ = '\0'; | |
38a24a61 | 60 | |
0b1284eb | 61 | bank = dectoul(str, &ep); |
bdccc4fe | 62 | if (ep == str || *ep != '\0' || |
98150e7e | 63 | bank < 1 || bank > CFI_FLASH_BANKS) |
c8363b12 PD |
64 | return -1; |
65 | ||
66 | fp = &flash_info[bank - 1]; | |
67 | if (fp->flash_id == FLASH_UNKNOWN) | |
bdccc4fe WD |
68 | return -1; |
69 | ||
70 | str = p; | |
c8363b12 PD |
71 | p = strchr(str, '-'); |
72 | if (p) | |
bdccc4fe WD |
73 | *p++ = '\0'; |
74 | ||
0b1284eb | 75 | first = dectoul(str, &ep); |
bdccc4fe WD |
76 | if (ep == str || *ep != '\0' || first >= fp->sector_count) |
77 | return -1; | |
78 | ||
c8363b12 | 79 | if (p) { |
0b1284eb | 80 | last = dectoul(p, &ep); |
bdccc4fe | 81 | if (ep == p || *ep != '\0' || |
c8363b12 | 82 | last < first || last >= fp->sector_count) |
bdccc4fe WD |
83 | return -1; |
84 | } else { | |
85 | last = first; | |
86 | } | |
38a24a61 | 87 | |
bdccc4fe WD |
88 | *pinfo = fp; |
89 | *psf = first; | |
90 | *psl = last; | |
91 | ||
92 | return 1; | |
93 | } | |
94 | ||
3f0cf51d BS |
95 | /* |
96 | * Take *addr in Flash and adjust it to fall on the end of its sector | |
97 | */ | |
a595a0e9 | 98 | int flash_sect_roundb(ulong *addr) |
3f0cf51d BS |
99 | { |
100 | flash_info_t *info; | |
101 | ulong bank, sector_end_addr; | |
102 | char found; | |
103 | int i; | |
104 | ||
105 | /* find the end addr of the sector where the *addr is */ | |
106 | found = 0; | |
98150e7e | 107 | for (bank = 0; bank < CFI_FLASH_BANKS && !found; ++bank) { |
3f0cf51d BS |
108 | info = &flash_info[bank]; |
109 | for (i = 0; i < info->sector_count && !found; ++i) { | |
110 | /* get the end address of the sector */ | |
111 | if (i == info->sector_count - 1) { | |
112 | sector_end_addr = info->start[0] + | |
113 | info->size - 1; | |
114 | } else { | |
c8363b12 | 115 | sector_end_addr = info->start[i + 1] - 1; |
3f0cf51d BS |
116 | } |
117 | ||
c8363b12 | 118 | if (*addr <= sector_end_addr && *addr >= info->start[i]) { |
3f0cf51d BS |
119 | found = 1; |
120 | /* adjust *addr if necessary */ | |
121 | if (*addr < sector_end_addr) | |
122 | *addr = sector_end_addr; | |
123 | } /* sector */ | |
124 | } /* bank */ | |
125 | } | |
126 | if (!found) { | |
16263087 | 127 | /* error, address not in flash */ |
3f0cf51d BS |
128 | printf("Error: end address (0x%08lx) not in flash!\n", *addr); |
129 | return 1; | |
130 | } | |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
f530187d WD |
135 | /* |
136 | * This function computes the start and end addresses for both | |
137 | * erase and protect commands. The range of the addresses on which | |
138 | * either of the commands is to operate can be given in two forms: | |
139 | * 1. <cmd> start end - operate on <'start', 'end') | |
095b8a37 | 140 | * 2. <cmd> start +length - operate on <'start', start + length) |
f530187d WD |
141 | * If the second form is used and the end address doesn't fall on the |
142 | * sector boundary, than it will be adjusted to the next sector boundary. | |
143 | * If it isn't in the flash, the function will fail (return -1). | |
144 | * Input: | |
145 | * arg1, arg2: address specification (i.e. both command arguments) | |
146 | * Output: | |
147 | * addr_first, addr_last: computed address range | |
148 | * Return: | |
149 | * 1: success | |
150 | * -1: failure (bad format, bad address). | |
c8363b12 | 151 | */ |
f530187d WD |
152 | static int |
153 | addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last) | |
154 | { | |
155 | char *ep; | |
bb74140d | 156 | char len_used; /* indicates if the "start +length" form used */ |
2c61f14c | 157 | |
7e5f460e | 158 | *addr_first = hextoul(arg1, &ep); |
f530187d WD |
159 | if (ep == arg1 || *ep != '\0') |
160 | return -1; | |
161 | ||
bb74140d | 162 | len_used = 0; |
c8363b12 | 163 | if (arg2 && *arg2 == '+') { |
f530187d WD |
164 | len_used = 1; |
165 | ++arg2; | |
166 | } | |
167 | ||
7e5f460e | 168 | *addr_last = hextoul(arg2, &ep); |
f530187d WD |
169 | if (ep == arg2 || *ep != '\0') |
170 | return -1; | |
171 | ||
c8363b12 | 172 | if (len_used) { |
f530187d WD |
173 | /* |
174 | * *addr_last has the length, compute correct *addr_last | |
175 | * XXX watch out for the integer overflow! Right now it is | |
176 | * checked for in both the callers. | |
177 | */ | |
178 | *addr_last = *addr_first + *addr_last - 1; | |
179 | ||
180 | /* | |
181 | * It may happen that *addr_last doesn't fall on the sector | |
182 | * boundary. We want to round such an address to the next | |
183 | * sector boundary, so that the commands don't fail later on. | |
184 | */ | |
185 | ||
3f0cf51d | 186 | if (flash_sect_roundb(addr_last) > 0) |
f530187d | 187 | return -1; |
f530187d WD |
188 | } /* "start +length" from used */ |
189 | ||
190 | return 1; | |
191 | } | |
192 | ||
bdccc4fe | 193 | static int |
c8363b12 PD |
194 | flash_fill_sect_ranges(ulong addr_first, ulong addr_last, |
195 | int *s_first, int *s_last, | |
196 | int *s_count) | |
bdccc4fe WD |
197 | { |
198 | flash_info_t *info; | |
199 | ulong bank; | |
200 | int rcode = 0; | |
201 | ||
202 | *s_count = 0; | |
203 | ||
98150e7e | 204 | for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) { |
bdccc4fe | 205 | s_first[bank] = -1; /* first sector to erase */ |
c8363b12 | 206 | s_last[bank] = -1; /* last sector to erase */ |
bdccc4fe WD |
207 | } |
208 | ||
c8363b12 | 209 | for (bank = 0, info = &flash_info[0]; |
98150e7e | 210 | (bank < CFI_FLASH_BANKS) && (addr_first <= addr_last); |
bdccc4fe WD |
211 | ++bank, ++info) { |
212 | ulong b_end; | |
213 | int sect; | |
214 | short s_end; | |
215 | ||
c8363b12 | 216 | if (info->flash_id == FLASH_UNKNOWN) |
bdccc4fe | 217 | continue; |
bdccc4fe WD |
218 | |
219 | b_end = info->start[0] + info->size - 1; /* bank end addr */ | |
220 | s_end = info->sector_count - 1; /* last sector */ | |
221 | ||
c8363b12 | 222 | for (sect = 0; sect < info->sector_count; ++sect) { |
bdccc4fe | 223 | ulong end; /* last address in current sect */ |
38a24a61 | 224 | |
bdccc4fe | 225 | end = (sect == s_end) ? b_end : info->start[sect + 1] - 1; |
38a24a61 | 226 | |
bdccc4fe WD |
227 | if (addr_first > end) |
228 | continue; | |
229 | if (addr_last < info->start[sect]) | |
230 | continue; | |
38a24a61 | 231 | |
c8363b12 | 232 | if (addr_first == info->start[sect]) |
bdccc4fe | 233 | s_first[bank] = sect; |
c8363b12 PD |
234 | |
235 | if (addr_last == end) | |
bdccc4fe | 236 | s_last[bank] = sect; |
bdccc4fe WD |
237 | } |
238 | if (s_first[bank] >= 0) { | |
239 | if (s_last[bank] < 0) { | |
240 | if (addr_last > b_end) { | |
241 | s_last[bank] = s_end; | |
242 | } else { | |
c8363b12 | 243 | puts("Error: end address not on sector boundary\n"); |
bdccc4fe WD |
244 | rcode = 1; |
245 | break; | |
246 | } | |
247 | } | |
248 | if (s_last[bank] < s_first[bank]) { | |
c8363b12 | 249 | puts("Error: end sector precedes start sector\n"); |
bdccc4fe WD |
250 | rcode = 1; |
251 | break; | |
252 | } | |
253 | sect = s_last[bank]; | |
c8363b12 | 254 | addr_first = (sect == s_end) ? b_end + 1 : info->start[sect + 1]; |
bdccc4fe | 255 | (*s_count) += s_last[bank] - s_first[bank] + 1; |
e2ffd59b | 256 | } else if (addr_first >= info->start[0] && addr_first < b_end) { |
c8363b12 | 257 | puts("Error: start address not on sector boundary\n"); |
e2ffd59b WD |
258 | rcode = 1; |
259 | break; | |
2d5b561e | 260 | } else if (s_last[bank] >= 0) { |
c8363b12 PD |
261 | puts("Error: cannot span across banks when they are" |
262 | " mapped in reverse order\n"); | |
2d5b561e WD |
263 | rcode = 1; |
264 | break; | |
bdccc4fe WD |
265 | } |
266 | } | |
38a24a61 | 267 | |
bdccc4fe | 268 | return rcode; |
38a24a61 | 269 | } |
e856bdcf | 270 | #endif /* CONFIG_MTD_NOR_FLASH */ |
bdccc4fe | 271 | |
09140113 SG |
272 | static int do_flinfo(struct cmd_tbl *cmdtp, int flag, int argc, |
273 | char *const argv[]) | |
38a24a61 | 274 | { |
e856bdcf | 275 | #ifdef CONFIG_MTD_NOR_FLASH |
38a24a61 | 276 | ulong bank; |
880cc438 | 277 | #endif |
38a24a61 | 278 | |
e856bdcf | 279 | #ifdef CONFIG_MTD_NOR_FLASH |
38a24a61 | 280 | if (argc == 1) { /* print info for all FLASH banks */ |
98150e7e | 281 | for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) { |
c8363b12 | 282 | printf("\nBank # %ld: ", bank + 1); |
38a24a61 | 283 | |
a595a0e9 | 284 | flash_print_info(&flash_info[bank]); |
38a24a61 WD |
285 | } |
286 | return 0; | |
287 | } | |
288 | ||
7e5f460e | 289 | bank = hextoul(argv[1], NULL); |
98150e7e | 290 | if (bank < 1 || bank > CFI_FLASH_BANKS) { |
c8363b12 | 291 | printf("Only FLASH Banks # 1 ... # %d supported\n", |
98150e7e | 292 | CFI_FLASH_BANKS); |
38a24a61 WD |
293 | return 1; |
294 | } | |
c8363b12 | 295 | printf("\nBank # %ld: ", bank); |
a595a0e9 | 296 | flash_print_info(&flash_info[bank - 1]); |
e856bdcf | 297 | #endif /* CONFIG_MTD_NOR_FLASH */ |
38a24a61 WD |
298 | return 0; |
299 | } | |
700a0c64 | 300 | |
09140113 SG |
301 | static int do_flerase(struct cmd_tbl *cmdtp, int flag, int argc, |
302 | char *const argv[]) | |
38a24a61 | 303 | { |
e856bdcf | 304 | #ifdef CONFIG_MTD_NOR_FLASH |
4f5710f7 | 305 | flash_info_t *info = NULL; |
38a24a61 | 306 | ulong bank, addr_first, addr_last; |
4f5710f7 | 307 | int n, sect_first = 0, sect_last = 0; |
c000808b | 308 | #if defined(CONFIG_CMD_MTDPARTS) |
700a0c64 WD |
309 | struct mtd_device *dev; |
310 | struct part_info *part; | |
311 | u8 dev_type, dev_num, pnum; | |
312 | #endif | |
38a24a61 WD |
313 | int rcode = 0; |
314 | ||
47e26b1b | 315 | if (argc < 2) |
4c12eeb8 | 316 | return CMD_RET_USAGE; |
38a24a61 WD |
317 | |
318 | if (strcmp(argv[1], "all") == 0) { | |
98150e7e | 319 | for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) { |
c8363b12 PD |
320 | printf("Erase Flash Bank # %ld ", bank); |
321 | info = &flash_info[bank - 1]; | |
a595a0e9 | 322 | rcode = flash_erase(info, 0, info->sector_count - 1); |
38a24a61 WD |
323 | } |
324 | return rcode; | |
325 | } | |
326 | ||
c8363b12 PD |
327 | n = abbrev_spec(argv[1], &info, §_first, §_last); |
328 | if (n) { | |
38a24a61 | 329 | if (n < 0) { |
c8363b12 | 330 | puts("Bad sector specification\n"); |
38a24a61 WD |
331 | return 1; |
332 | } | |
c8363b12 PD |
333 | printf("Erase Flash Sectors %d-%d in Bank # %zu ", |
334 | sect_first, sect_last, (info - flash_info) + 1); | |
38a24a61 WD |
335 | rcode = flash_erase(info, sect_first, sect_last); |
336 | return rcode; | |
337 | } | |
338 | ||
c000808b | 339 | #if defined(CONFIG_CMD_MTDPARTS) |
700a0c64 | 340 | /* erase <part-id> - erase partition */ |
c8363b12 | 341 | if (argc == 2 && mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0) { |
700a0c64 WD |
342 | 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 | ||
c8363b12 PD |
350 | printf("Erase Flash Partition %s, bank %ld, 0x%08lx - 0x%08lx ", |
351 | argv[1], bank, addr_first, | |
352 | addr_last); | |
700a0c64 WD |
353 | |
354 | rcode = flash_sect_erase(addr_first, addr_last); | |
355 | return rcode; | |
356 | } | |
357 | ||
358 | printf("cannot erase, not a NOR device\n"); | |
359 | return 1; | |
360 | } | |
361 | } | |
362 | #endif | |
363 | ||
47e26b1b | 364 | if (argc != 3) |
4c12eeb8 | 365 | return CMD_RET_USAGE; |
38a24a61 WD |
366 | |
367 | if (strcmp(argv[1], "bank") == 0) { | |
7e5f460e | 368 | bank = hextoul(argv[2], NULL); |
98150e7e | 369 | if (bank < 1 || bank > CFI_FLASH_BANKS) { |
c8363b12 | 370 | printf("Only FLASH Banks # 1 ... # %d supported\n", |
98150e7e | 371 | CFI_FLASH_BANKS); |
38a24a61 WD |
372 | return 1; |
373 | } | |
c8363b12 PD |
374 | printf("Erase Flash Bank # %ld ", bank); |
375 | info = &flash_info[bank - 1]; | |
a595a0e9 | 376 | rcode = flash_erase(info, 0, info->sector_count - 1); |
38a24a61 WD |
377 | return rcode; |
378 | } | |
379 | ||
c8363b12 PD |
380 | if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0) { |
381 | printf("Bad address format\n"); | |
f530187d WD |
382 | return 1; |
383 | } | |
38a24a61 | 384 | |
47e26b1b | 385 | if (addr_first >= addr_last) |
4c12eeb8 | 386 | return CMD_RET_USAGE; |
38a24a61 | 387 | |
38a24a61 WD |
388 | rcode = flash_sect_erase(addr_first, addr_last); |
389 | return rcode; | |
880cc438 SP |
390 | #else |
391 | return 0; | |
e856bdcf | 392 | #endif /* CONFIG_MTD_NOR_FLASH */ |
38a24a61 WD |
393 | } |
394 | ||
e856bdcf | 395 | #ifdef CONFIG_MTD_NOR_FLASH |
a595a0e9 | 396 | int flash_sect_erase(ulong addr_first, ulong addr_last) |
38a24a61 WD |
397 | { |
398 | flash_info_t *info; | |
399 | ulong bank; | |
98150e7e | 400 | int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS]; |
bdccc4fe WD |
401 | int erased = 0; |
402 | int planned; | |
38a24a61 WD |
403 | int rcode = 0; |
404 | ||
c8363b12 | 405 | rcode = flash_fill_sect_ranges(addr_first, addr_last, s_first, s_last, &planned); |
bdccc4fe | 406 | |
c8363b12 PD |
407 | if (planned && rcode == 0) { |
408 | for (bank = 0, info = &flash_info[0]; | |
98150e7e | 409 | bank < CFI_FLASH_BANKS && rcode == 0; |
bdccc4fe | 410 | ++bank, ++info) { |
c8363b12 | 411 | if (s_first[bank] >= 0) { |
bdccc4fe | 412 | erased += s_last[bank] - s_first[bank] + 1; |
3c7dded8 SG |
413 | debug("Erase Flash from 0x%08lx to 0x%08lx in Bank # %ld ", |
414 | info->start[s_first[bank]], | |
415 | (s_last[bank] == info->sector_count) ? | |
416 | info->start[0] + info->size - 1 : | |
417 | info->start[s_last[bank] + 1] - 1, | |
418 | bank + 1); | |
a595a0e9 SG |
419 | rcode = flash_erase(info, s_first[bank], |
420 | s_last[bank]); | |
38a24a61 WD |
421 | } |
422 | } | |
de15a06a JH |
423 | if (rcode == 0) |
424 | printf("Erased %d sectors\n", erased); | |
bdccc4fe | 425 | } else if (rcode == 0) { |
c8363b12 | 426 | puts("Error: start and/or end address not on sector boundary\n"); |
38a24a61 WD |
427 | rcode = 1; |
428 | } | |
429 | return rcode; | |
430 | } | |
e856bdcf | 431 | #endif /* CONFIG_MTD_NOR_FLASH */ |
38a24a61 | 432 | |
09140113 SG |
433 | static int do_protect(struct cmd_tbl *cmdtp, int flag, int argc, |
434 | char *const argv[]) | |
38a24a61 | 435 | { |
5b3901d9 | 436 | int rcode = 0; |
e856bdcf | 437 | #ifdef CONFIG_MTD_NOR_FLASH |
4f5710f7 | 438 | flash_info_t *info = NULL; |
880cc438 | 439 | ulong bank; |
4f5710f7 | 440 | int i, n, sect_first = 0, sect_last = 0; |
c000808b | 441 | #if defined(CONFIG_CMD_MTDPARTS) |
700a0c64 WD |
442 | struct mtd_device *dev; |
443 | struct part_info *part; | |
444 | u8 dev_type, dev_num, pnum; | |
445 | #endif | |
e856bdcf | 446 | #endif /* CONFIG_MTD_NOR_FLASH */ |
c68c03f5 | 447 | #if defined(CONFIG_MTD_NOR_FLASH) |
5669ed45 | 448 | int p; |
5b3901d9 MV |
449 | ulong addr_first, addr_last; |
450 | #endif | |
2662b40c | 451 | |
47e26b1b | 452 | if (argc < 3) |
4c12eeb8 | 453 | return CMD_RET_USAGE; |
38a24a61 | 454 | |
c68c03f5 | 455 | #if defined(CONFIG_MTD_NOR_FLASH) |
47e26b1b | 456 | if (strcmp(argv[1], "off") == 0) |
38a24a61 | 457 | p = 0; |
47e26b1b | 458 | else if (strcmp(argv[1], "on") == 0) |
38a24a61 | 459 | p = 1; |
47e26b1b | 460 | else |
4c12eeb8 | 461 | return CMD_RET_USAGE; |
5b3901d9 | 462 | #endif |
38a24a61 | 463 | |
e856bdcf | 464 | #ifdef CONFIG_MTD_NOR_FLASH |
38a24a61 | 465 | if (strcmp(argv[2], "all") == 0) { |
98150e7e | 466 | for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) { |
c8363b12 PD |
467 | info = &flash_info[bank - 1]; |
468 | if (info->flash_id == FLASH_UNKNOWN) | |
38a24a61 | 469 | continue; |
38a24a61 | 470 | |
c8363b12 PD |
471 | printf("%sProtect Flash Bank # %ld\n", |
472 | p ? "" : "Un-", bank); | |
473 | ||
474 | for (i = 0; i < info->sector_count; ++i) { | |
6d0f6bcf | 475 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
38a24a61 WD |
476 | if (flash_real_protect(info, i, p)) |
477 | rcode = 1; | |
c8363b12 | 478 | putc('.'); |
38a24a61 WD |
479 | #else |
480 | info->protect[i] = p; | |
6d0f6bcf | 481 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
38a24a61 | 482 | } |
6d0f6bcf | 483 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
c8363b12 PD |
484 | if (!rcode) |
485 | puts(" done\n"); | |
6d0f6bcf | 486 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
bb74140d | 487 | } |
38a24a61 WD |
488 | return rcode; |
489 | } | |
c8363b12 PD |
490 | n = abbrev_spec(argv[2], &info, §_first, §_last); |
491 | if (n) { | |
38a24a61 | 492 | if (n < 0) { |
c8363b12 | 493 | puts("Bad sector specification\n"); |
38a24a61 WD |
494 | return 1; |
495 | } | |
f2302d44 | 496 | printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n", |
c8363b12 PD |
497 | p ? "" : "Un-", sect_first, sect_last, |
498 | (info - flash_info) + 1); | |
38a24a61 | 499 | for (i = sect_first; i <= sect_last; i++) { |
6d0f6bcf | 500 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
38a24a61 WD |
501 | if (flash_real_protect(info, i, p)) |
502 | rcode = 1; | |
c8363b12 | 503 | putc('.'); |
38a24a61 WD |
504 | #else |
505 | info->protect[i] = p; | |
6d0f6bcf | 506 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
38a24a61 WD |
507 | } |
508 | ||
6d0f6bcf | 509 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
c8363b12 PD |
510 | if (!rcode) |
511 | puts(" done\n"); | |
6d0f6bcf | 512 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
38a24a61 WD |
513 | |
514 | return rcode; | |
515 | } | |
8f79e4c2 | 516 | |
c000808b | 517 | #if defined(CONFIG_CMD_MTDPARTS) |
700a0c64 | 518 | /* protect on/off <part-id> */ |
c8363b12 | 519 | if (argc == 3 && mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0) { |
700a0c64 WD |
520 | mtdparts_init(); |
521 | if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) { | |
522 | if (dev->id->type == MTD_DEV_TYPE_NOR) { | |
523 | bank = dev->id->num; | |
524 | info = &flash_info[bank]; | |
525 | addr_first = part->offset + info->start[0]; | |
526 | addr_last = addr_first + part->size - 1; | |
527 | ||
c8363b12 PD |
528 | printf("%sProtect Flash Partition %s, " |
529 | "bank %ld, 0x%08lx - 0x%08lx\n", | |
530 | p ? "" : "Un", argv[1], | |
531 | bank, addr_first, addr_last); | |
700a0c64 | 532 | |
a595a0e9 SG |
533 | rcode = flash_sect_protect(p, addr_first, |
534 | addr_last); | |
700a0c64 WD |
535 | return rcode; |
536 | } | |
537 | ||
538 | printf("cannot %sprotect, not a NOR device\n", | |
c8363b12 | 539 | p ? "" : "un"); |
700a0c64 WD |
540 | return 1; |
541 | } | |
542 | } | |
543 | #endif | |
38a24a61 | 544 | |
47e26b1b | 545 | if (argc != 4) |
4c12eeb8 | 546 | return CMD_RET_USAGE; |
38a24a61 WD |
547 | |
548 | if (strcmp(argv[2], "bank") == 0) { | |
7e5f460e | 549 | bank = hextoul(argv[3], NULL); |
98150e7e | 550 | if (bank < 1 || bank > CFI_FLASH_BANKS) { |
c8363b12 | 551 | printf("Only FLASH Banks # 1 ... # %d supported\n", |
98150e7e | 552 | CFI_FLASH_BANKS); |
38a24a61 WD |
553 | return 1; |
554 | } | |
c8363b12 PD |
555 | printf("%sProtect Flash Bank # %ld\n", |
556 | p ? "" : "Un-", bank); | |
557 | info = &flash_info[bank - 1]; | |
38a24a61 WD |
558 | |
559 | if (info->flash_id == FLASH_UNKNOWN) { | |
c8363b12 | 560 | puts("missing or unknown FLASH type\n"); |
38a24a61 WD |
561 | return 1; |
562 | } | |
c8363b12 | 563 | for (i = 0; i < info->sector_count; ++i) { |
6d0f6bcf | 564 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
38a24a61 WD |
565 | if (flash_real_protect(info, i, p)) |
566 | rcode = 1; | |
c8363b12 | 567 | putc('.'); |
38a24a61 WD |
568 | #else |
569 | info->protect[i] = p; | |
6d0f6bcf | 570 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
38a24a61 WD |
571 | } |
572 | ||
6d0f6bcf | 573 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
c8363b12 PD |
574 | if (!rcode) |
575 | puts(" done\n"); | |
6d0f6bcf | 576 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
38a24a61 WD |
577 | |
578 | return rcode; | |
579 | } | |
580 | ||
c8363b12 | 581 | if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0) { |
f530187d WD |
582 | printf("Bad address format\n"); |
583 | return 1; | |
584 | } | |
38a24a61 | 585 | |
47e26b1b | 586 | if (addr_first >= addr_last) |
4c12eeb8 | 587 | return CMD_RET_USAGE; |
47e26b1b | 588 | |
a595a0e9 | 589 | rcode = flash_sect_protect(p, addr_first, addr_last); |
e856bdcf | 590 | #endif /* CONFIG_MTD_NOR_FLASH */ |
38a24a61 WD |
591 | return rcode; |
592 | } | |
593 | ||
e856bdcf | 594 | #ifdef CONFIG_MTD_NOR_FLASH |
a595a0e9 | 595 | int flash_sect_protect(int p, ulong addr_first, ulong addr_last) |
38a24a61 WD |
596 | { |
597 | flash_info_t *info; | |
598 | ulong bank; | |
98150e7e | 599 | int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS]; |
38a24a61 | 600 | int protected, i; |
bdccc4fe WD |
601 | int planned; |
602 | int rcode; | |
38a24a61 | 603 | |
c8363b12 | 604 | rcode = flash_fill_sect_ranges(addr_first, addr_last, s_first, s_last, &planned); |
38a24a61 | 605 | |
bdccc4fe | 606 | protected = 0; |
38a24a61 | 607 | |
c8363b12 PD |
608 | if (planned && rcode == 0) { |
609 | for (bank = 0, info = &flash_info[0]; | |
98150e7e | 610 | bank < CFI_FLASH_BANKS; ++bank, ++info) { |
c8363b12 | 611 | if (info->flash_id == FLASH_UNKNOWN) |
38a24a61 | 612 | continue; |
bdccc4fe | 613 | |
c8363b12 | 614 | if (s_first[bank] >= 0 && s_first[bank] <= s_last[bank]) { |
3c7dded8 SG |
615 | debug("%sProtecting sectors %d..%d in bank %ld\n", |
616 | p ? "" : "Un-", s_first[bank], | |
617 | s_last[bank], bank + 1); | |
bdccc4fe | 618 | protected += s_last[bank] - s_first[bank] + 1; |
c8363b12 | 619 | for (i = s_first[bank]; i <= s_last[bank]; ++i) { |
6d0f6bcf | 620 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
bdccc4fe WD |
621 | if (flash_real_protect(info, i, p)) |
622 | rcode = 1; | |
c8363b12 | 623 | putc('.'); |
38a24a61 | 624 | #else |
bdccc4fe | 625 | info->protect[i] = p; |
6d0f6bcf | 626 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
bdccc4fe | 627 | } |
38a24a61 | 628 | } |
2662b40c | 629 | } |
6d0f6bcf | 630 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
c8363b12 | 631 | puts(" done\n"); |
6d0f6bcf | 632 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
38a24a61 | 633 | |
c8363b12 PD |
634 | printf("%sProtected %d sectors\n", |
635 | p ? "" : "Un-", protected); | |
bdccc4fe | 636 | } else if (rcode == 0) { |
c8363b12 | 637 | puts("Error: start and/or end address not on sector boundary\n"); |
38a24a61 WD |
638 | rcode = 1; |
639 | } | |
640 | return rcode; | |
641 | } | |
e856bdcf | 642 | #endif /* CONFIG_MTD_NOR_FLASH */ |
38a24a61 | 643 | |
8bde7f77 | 644 | /**************************************************/ |
c000808b | 645 | #if defined(CONFIG_CMD_MTDPARTS) |
c19c3134 WD |
646 | # define TMP_ERASE "erase <part-id>\n - erase partition\n" |
647 | # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n" | |
648 | # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n" | |
649 | #else | |
650 | # define TMP_ERASE /* empty */ | |
651 | # define TMP_PROT_ON /* empty */ | |
652 | # define TMP_PROT_OFF /* empty */ | |
653 | #endif | |
8bde7f77 | 654 | |
0d498393 WD |
655 | U_BOOT_CMD( |
656 | flinfo, 2, 1, do_flinfo, | |
2fb2604d | 657 | "print FLASH memory information", |
8bde7f77 | 658 | "\n - print information for all FLASH memory banks\n" |
a89c33db | 659 | "flinfo N\n - print information for FLASH memory bank # N" |
8bde7f77 WD |
660 | ); |
661 | ||
0d498393 | 662 | U_BOOT_CMD( |
9912121f | 663 | erase, 3, 0, do_flerase, |
2fb2604d | 664 | "erase FLASH memory", |
8bde7f77 WD |
665 | "start end\n" |
666 | " - erase FLASH from addr 'start' to addr 'end'\n" | |
f530187d | 667 | "erase start +len\n" |
c8363b12 | 668 | " - erase FLASH from addr 'start' to the end of sect w/addr 'start'+'len'-1\n" |
8bde7f77 WD |
669 | "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n" |
670 | "erase bank N\n - erase FLASH bank # N\n" | |
c19c3134 | 671 | TMP_ERASE |
a89c33db | 672 | "erase all\n - erase all FLASH banks" |
8bde7f77 WD |
673 | ); |
674 | ||
0d498393 | 675 | U_BOOT_CMD( |
9912121f | 676 | protect, 4, 0, do_protect, |
2fb2604d | 677 | "enable or disable FLASH write protection", |
8bde7f77 WD |
678 | "on start end\n" |
679 | " - protect FLASH from addr 'start' to addr 'end'\n" | |
f530187d | 680 | "protect on start +len\n" |
c8363b12 | 681 | " - protect FLASH from addr 'start' to end of sect w/addr 'start'+'len'-1\n" |
8bde7f77 WD |
682 | "protect on N:SF[-SL]\n" |
683 | " - protect sectors SF-SL in FLASH bank # N\n" | |
684 | "protect on bank N\n - protect FLASH bank # N\n" | |
c19c3134 | 685 | TMP_PROT_ON |
8bde7f77 WD |
686 | "protect on all\n - protect all FLASH banks\n" |
687 | "protect off start end\n" | |
688 | " - make FLASH from addr 'start' to addr 'end' writable\n" | |
f530187d | 689 | "protect off start +len\n" |
c8363b12 | 690 | " - make FLASH from addr 'start' to end of sect w/addr 'start'+'len'-1 wrtable\n" |
8bde7f77 WD |
691 | "protect off N:SF[-SL]\n" |
692 | " - make sectors SF-SL writable in FLASH bank # N\n" | |
693 | "protect off bank N\n - make FLASH bank # N writable\n" | |
c19c3134 | 694 | TMP_PROT_OFF |
a89c33db | 695 | "protect off all\n - make all FLASH banks writable" |
8bde7f77 WD |
696 | ); |
697 | ||
c19c3134 WD |
698 | #undef TMP_ERASE |
699 | #undef TMP_PROT_ON | |
700 | #undef TMP_PROT_OFF |