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