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