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