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