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