]>
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> | |
29 | #include <cmd_boot.h> | |
30 | #include <flash.h> | |
31 | ||
32 | #if (CONFIG_COMMANDS & CFG_CMD_FLASH) | |
33 | ||
34 | extern flash_info_t flash_info[]; /* info for FLASH chips */ | |
35 | ||
36 | /* | |
37 | * The user interface starts numbering for Flash banks with 1 | |
38 | * for historical reasons. | |
39 | */ | |
40 | ||
41 | /* | |
42 | * this routine looks for an abbreviated flash range specification. | |
43 | * the syntax is B:SF[-SL], where B is the bank number, SF is the first | |
44 | * sector to erase, and SL is the last sector to erase (defaults to SF). | |
45 | * bank numbers start at 1 to be consistent with other specs, sector numbers | |
46 | * start at zero. | |
47 | * | |
48 | * returns: 1 - correct spec; *pinfo, *psf and *psl are | |
49 | * set appropriately | |
50 | * 0 - doesn't look like an abbreviated spec | |
51 | * -1 - looks like an abbreviated spec, but got | |
52 | * a parsing error, a number out of range, | |
53 | * or an invalid flash bank. | |
54 | */ | |
55 | static int | |
56 | abbrev_spec(char *str, flash_info_t **pinfo, int *psf, int *psl) | |
57 | { | |
58 | flash_info_t *fp; | |
59 | int bank, first, last; | |
60 | char *p, *ep; | |
61 | ||
62 | if ((p = strchr(str, ':')) == NULL) | |
63 | return 0; | |
64 | *p++ = '\0'; | |
65 | ||
66 | bank = simple_strtoul(str, &ep, 10); | |
67 | if (ep == str || *ep != '\0' || | |
68 | bank < 1 || bank > CFG_MAX_FLASH_BANKS || | |
69 | (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN) | |
70 | return -1; | |
71 | ||
72 | str = p; | |
73 | if ((p = strchr(str, '-')) != NULL) | |
74 | *p++ = '\0'; | |
75 | ||
76 | first = simple_strtoul(str, &ep, 10); | |
77 | if (ep == str || *ep != '\0' || first >= fp->sector_count) | |
78 | return -1; | |
79 | ||
80 | if (p != NULL) { | |
81 | last = simple_strtoul(p, &ep, 10); | |
82 | if (ep == p || *ep != '\0' || | |
83 | last < first || last >= fp->sector_count) | |
84 | return -1; | |
85 | } | |
86 | else | |
87 | last = first; | |
88 | ||
89 | *pinfo = fp; | |
90 | *psf = first; | |
91 | *psl = last; | |
92 | ||
93 | return 1; | |
94 | } | |
95 | int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) | |
96 | { | |
97 | ulong bank; | |
98 | ||
99 | if (argc == 1) { /* print info for all FLASH banks */ | |
100 | for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) { | |
101 | printf ("\nBank # %ld: ", bank+1); | |
102 | ||
103 | flash_print_info (&flash_info[bank]); | |
104 | } | |
105 | return 0; | |
106 | } | |
107 | ||
108 | bank = simple_strtoul(argv[1], NULL, 16); | |
109 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { | |
110 | printf ("Only FLASH Banks # 1 ... # %d supported\n", | |
111 | CFG_MAX_FLASH_BANKS); | |
112 | return 1; | |
113 | } | |
114 | printf ("\nBank # %ld: ", bank); | |
115 | flash_print_info (&flash_info[bank-1]); | |
116 | return 0; | |
117 | } | |
118 | int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) | |
119 | { | |
120 | flash_info_t *info; | |
121 | ulong bank, addr_first, addr_last; | |
122 | int n, sect_first, sect_last; | |
123 | int rcode = 0; | |
124 | ||
125 | if (argc < 2) { | |
126 | printf ("Usage:\n%s\n", cmdtp->usage); | |
127 | return 1; | |
128 | } | |
129 | ||
130 | if (strcmp(argv[1], "all") == 0) { | |
131 | for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) { | |
132 | printf ("Erase Flash Bank # %ld ", bank); | |
133 | info = &flash_info[bank-1]; | |
134 | rcode = flash_erase (info, 0, info->sector_count-1); | |
135 | } | |
136 | return rcode; | |
137 | } | |
138 | ||
139 | if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) { | |
140 | if (n < 0) { | |
141 | printf("Bad sector specification\n"); | |
142 | return 1; | |
143 | } | |
144 | printf ("Erase Flash Sectors %d-%d in Bank # %d ", | |
145 | sect_first, sect_last, (info-flash_info)+1); | |
146 | rcode = flash_erase(info, sect_first, sect_last); | |
147 | return rcode; | |
148 | } | |
149 | ||
150 | if (argc != 3) { | |
151 | printf ("Usage:\n%s\n", cmdtp->usage); | |
152 | return 1; | |
153 | } | |
154 | ||
155 | if (strcmp(argv[1], "bank") == 0) { | |
156 | bank = simple_strtoul(argv[2], NULL, 16); | |
157 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { | |
158 | printf ("Only FLASH Banks # 1 ... # %d supported\n", | |
159 | CFG_MAX_FLASH_BANKS); | |
160 | return 1; | |
161 | } | |
162 | printf ("Erase Flash Bank # %ld ", bank); | |
163 | info = &flash_info[bank-1]; | |
164 | rcode = flash_erase (info, 0, info->sector_count-1); | |
165 | return rcode; | |
166 | } | |
167 | ||
168 | addr_first = simple_strtoul(argv[1], NULL, 16); | |
169 | addr_last = simple_strtoul(argv[2], NULL, 16); | |
170 | ||
171 | if (addr_first >= addr_last) { | |
172 | printf ("Usage:\n%s\n", cmdtp->usage); | |
173 | return 1; | |
174 | } | |
175 | ||
176 | printf ("Erase Flash from 0x%08lx to 0x%08lx ", addr_first, addr_last); | |
177 | rcode = flash_sect_erase(addr_first, addr_last); | |
178 | return rcode; | |
179 | } | |
180 | ||
181 | int flash_sect_erase (ulong addr_first, ulong addr_last) | |
182 | { | |
183 | flash_info_t *info; | |
184 | ulong bank; | |
185 | int s_first, s_last; | |
186 | int erased; | |
187 | int rcode = 0; | |
188 | ||
189 | erased = 0; | |
190 | ||
191 | for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) { | |
192 | ulong b_end; | |
193 | int sect; | |
194 | ||
195 | if (info->flash_id == FLASH_UNKNOWN) { | |
196 | continue; | |
197 | } | |
198 | ||
199 | b_end = info->start[0] + info->size - 1; /* bank end addr */ | |
200 | ||
201 | s_first = -1; /* first sector to erase */ | |
202 | s_last = -1; /* last sector to erase */ | |
203 | ||
204 | for (sect=0; sect < info->sector_count; ++sect) { | |
205 | ulong end; /* last address in current sect */ | |
206 | short s_end; | |
207 | ||
208 | s_end = info->sector_count - 1; | |
209 | ||
210 | end = (sect == s_end) ? b_end : info->start[sect + 1] - 1; | |
211 | ||
212 | if (addr_first > end) | |
213 | continue; | |
214 | if (addr_last < info->start[sect]) | |
215 | continue; | |
216 | ||
217 | if (addr_first == info->start[sect]) { | |
218 | s_first = sect; | |
219 | } | |
220 | if (addr_last == end) { | |
221 | s_last = sect; | |
222 | } | |
223 | } | |
224 | if (s_first>=0 && s_first<=s_last) { | |
225 | erased += s_last - s_first + 1; | |
226 | rcode = flash_erase (info, s_first, s_last); | |
227 | } | |
228 | } | |
229 | if (erased) { | |
230 | printf ("Erased %d sectors\n", erased); | |
231 | } else { | |
232 | printf ("Error: start and/or end address" | |
233 | " not on sector boundary\n"); | |
234 | rcode = 1; | |
235 | } | |
236 | return rcode; | |
237 | } | |
238 | ||
239 | ||
240 | int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) | |
241 | { | |
242 | flash_info_t *info; | |
243 | ulong bank, addr_first, addr_last; | |
244 | int i, p, n, sect_first, sect_last; | |
245 | int rcode = 0; | |
246 | ||
247 | if (argc < 3) { | |
248 | printf ("Usage:\n%s\n", cmdtp->usage); | |
249 | return 1; | |
250 | } | |
251 | ||
252 | if (strcmp(argv[1], "off") == 0) | |
253 | p = 0; | |
254 | else if (strcmp(argv[1], "on") == 0) | |
255 | p = 1; | |
256 | else { | |
257 | printf ("Usage:\n%s\n", cmdtp->usage); | |
258 | return 1; | |
259 | } | |
260 | ||
261 | if (strcmp(argv[2], "all") == 0) { | |
262 | for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) { | |
263 | info = &flash_info[bank-1]; | |
264 | if (info->flash_id == FLASH_UNKNOWN) { | |
265 | continue; | |
266 | } | |
267 | printf ("%sProtect Flash Bank # %ld\n", | |
268 | p ? "" : "Un-", bank); | |
269 | ||
270 | for (i=0; i<info->sector_count; ++i) { | |
271 | #if defined(CFG_FLASH_PROTECTION) | |
272 | if (flash_real_protect(info, i, p)) | |
273 | rcode = 1; | |
274 | putc ('.'); | |
275 | #else | |
276 | info->protect[i] = p; | |
277 | #endif /* CFG_FLASH_PROTECTION */ | |
278 | } | |
279 | } | |
280 | ||
281 | #if defined(CFG_FLASH_PROTECTION) | |
282 | if (!rcode) puts (" done\n"); | |
283 | #endif /* CFG_FLASH_PROTECTION */ | |
284 | ||
285 | return rcode; | |
286 | } | |
287 | ||
288 | if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) { | |
289 | if (n < 0) { | |
290 | printf("Bad sector specification\n"); | |
291 | return 1; | |
292 | } | |
293 | printf("%sProtect Flash Sectors %d-%d in Bank # %d\n", | |
294 | p ? "" : "Un-", sect_first, sect_last, | |
295 | (info-flash_info)+1); | |
296 | for (i = sect_first; i <= sect_last; i++) { | |
297 | #if defined(CFG_FLASH_PROTECTION) | |
298 | if (flash_real_protect(info, i, p)) | |
299 | rcode = 1; | |
300 | putc ('.'); | |
301 | #else | |
302 | info->protect[i] = p; | |
303 | #endif /* CFG_FLASH_PROTECTION */ | |
304 | } | |
305 | ||
306 | #if defined(CFG_FLASH_PROTECTION) | |
307 | if (!rcode) puts (" done\n"); | |
308 | #endif /* CFG_FLASH_PROTECTION */ | |
309 | ||
310 | return rcode; | |
311 | } | |
312 | ||
313 | if (argc != 4) { | |
314 | printf ("Usage:\n%s\n", cmdtp->usage); | |
315 | return 1; | |
316 | } | |
317 | ||
318 | if (strcmp(argv[2], "bank") == 0) { | |
319 | bank = simple_strtoul(argv[3], NULL, 16); | |
320 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { | |
321 | printf ("Only FLASH Banks # 1 ... # %d supported\n", | |
322 | CFG_MAX_FLASH_BANKS); | |
323 | return 1; | |
324 | } | |
325 | printf ("%sProtect Flash Bank # %ld\n", | |
326 | p ? "" : "Un-", bank); | |
327 | info = &flash_info[bank-1]; | |
328 | ||
329 | if (info->flash_id == FLASH_UNKNOWN) { | |
330 | printf ("missing or unknown FLASH type\n"); | |
331 | return 1; | |
332 | } | |
333 | for (i=0; i<info->sector_count; ++i) { | |
334 | #if defined(CFG_FLASH_PROTECTION) | |
335 | if (flash_real_protect(info, i, p)) | |
336 | rcode = 1; | |
337 | putc ('.'); | |
338 | #else | |
339 | info->protect[i] = p; | |
340 | #endif /* CFG_FLASH_PROTECTION */ | |
341 | } | |
342 | ||
343 | #if defined(CFG_FLASH_PROTECTION) | |
344 | if (!rcode) puts (" done\n"); | |
345 | #endif /* CFG_FLASH_PROTECTION */ | |
346 | ||
347 | return rcode; | |
348 | } | |
349 | ||
350 | addr_first = simple_strtoul(argv[2], NULL, 16); | |
351 | addr_last = simple_strtoul(argv[3], NULL, 16); | |
352 | ||
353 | if (addr_first >= addr_last) { | |
354 | printf ("Usage:\n%s\n", cmdtp->usage); | |
355 | return 1; | |
356 | } | |
357 | rcode = flash_sect_protect (p, addr_first, addr_last); | |
358 | return rcode; | |
359 | } | |
360 | ||
361 | ||
362 | int flash_sect_protect (int p, ulong addr_first, ulong addr_last) | |
363 | { | |
364 | flash_info_t *info; | |
365 | ulong bank; | |
366 | int s_first, s_last; | |
367 | int protected, i; | |
368 | int rcode = 0; | |
369 | ||
370 | protected = 0; | |
371 | ||
372 | for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) { | |
373 | ulong b_end; | |
374 | int sect; | |
375 | ||
376 | if (info->flash_id == FLASH_UNKNOWN) { | |
377 | continue; | |
378 | } | |
379 | ||
380 | b_end = info->start[0] + info->size - 1; /* bank end addr */ | |
381 | ||
382 | s_first = -1; /* first sector to erase */ | |
383 | s_last = -1; /* last sector to erase */ | |
384 | ||
385 | for (sect=0; sect < info->sector_count; ++sect) { | |
386 | ulong end; /* last address in current sect */ | |
387 | short s_end; | |
388 | ||
389 | s_end = info->sector_count - 1; | |
390 | ||
391 | end = (sect == s_end) ? b_end : info->start[sect + 1] - 1; | |
392 | ||
393 | if (addr_first > end) | |
394 | continue; | |
395 | if (addr_last < info->start[sect]) | |
396 | continue; | |
397 | ||
398 | if (addr_first == info->start[sect]) { | |
399 | s_first = sect; | |
400 | } | |
401 | if (addr_last == end) { | |
402 | s_last = sect; | |
403 | } | |
404 | } | |
405 | if (s_first>=0 && s_first<=s_last) { | |
406 | protected += s_last - s_first + 1; | |
407 | for (i=s_first; i<=s_last; ++i) { | |
408 | #if defined(CFG_FLASH_PROTECTION) | |
409 | if (flash_real_protect(info, i, p)) | |
410 | rcode = 1; | |
411 | putc ('.'); | |
412 | #else | |
413 | info->protect[i] = p; | |
414 | #endif /* CFG_FLASH_PROTECTION */ | |
415 | } | |
416 | } | |
417 | #if defined(CFG_FLASH_PROTECTION) | |
418 | if (!rcode) putc ('\n'); | |
419 | #endif /* CFG_FLASH_PROTECTION */ | |
420 | ||
421 | } | |
422 | if (protected) { | |
423 | printf ("%sProtected %d sectors\n", | |
424 | p ? "" : "Un-", protected); | |
425 | } else { | |
426 | printf ("Error: start and/or end address" | |
427 | " not on sector boundary\n"); | |
428 | rcode = 1; | |
429 | } | |
430 | return rcode; | |
431 | } | |
432 | ||
433 | #endif /* CFG_CMD_FLASH */ |