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