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 | { |
Wolfgang Denk | 2c61f14 | 2005-08-05 11:10:31 +0200 | [diff] [blame] | 128 | char len_used = 0; /* indicates if the "start +length" form used */ |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 129 | char *ep; |
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 | |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 135 | if (arg2 && *arg2 == '+'){ |
| 136 | len_used = 1; |
| 137 | ++arg2; |
| 138 | } |
| 139 | |
| 140 | *addr_last = simple_strtoul(arg2, &ep, 16); |
| 141 | if (ep == arg2 || *ep != '\0') |
| 142 | return -1; |
| 143 | |
| 144 | if (len_used){ |
Wolfgang Denk | 2c61f14 | 2005-08-05 11:10:31 +0200 | [diff] [blame] | 145 | char found = 0; |
| 146 | ulong bank; |
| 147 | |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 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 */ |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 162 | for (bank = 0; bank < CFG_MAX_FLASH_BANKS && !found; ++bank){ |
| 163 | int i; |
| 164 | flash_info_t *info = &flash_info[bank]; |
| 165 | for (i = 0; i < info->sector_count && !found; ++i){ |
| 166 | /* get the end address of the sector */ |
| 167 | ulong sector_end_addr; |
| 168 | if (i == info->sector_count - 1){ |
| 169 | sector_end_addr = |
| 170 | info->start[0] + info->size - 1; |
| 171 | } else { |
| 172 | sector_end_addr = |
| 173 | info->start[i+1] - 1; |
| 174 | } |
| 175 | if (*addr_last <= sector_end_addr && |
| 176 | *addr_last >= info->start[i]){ |
| 177 | /* sector found */ |
| 178 | found = 1; |
| 179 | /* adjust *addr_last if necessary */ |
| 180 | if (*addr_last < sector_end_addr){ |
| 181 | *addr_last = sector_end_addr; |
| 182 | } |
| 183 | } |
| 184 | } /* sector */ |
| 185 | } /* bank */ |
| 186 | if (!found){ |
| 187 | /* error, addres not in flash */ |
| 188 | printf("Error: end address (0x%08lx) not in flash!\n", |
| 189 | *addr_last); |
| 190 | return -1; |
| 191 | } |
| 192 | } /* "start +length" from used */ |
| 193 | |
| 194 | return 1; |
| 195 | } |
| 196 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 197 | static int |
| 198 | flash_fill_sect_ranges (ulong addr_first, ulong addr_last, |
| 199 | int *s_first, int *s_last, |
| 200 | int *s_count ) |
| 201 | { |
| 202 | flash_info_t *info; |
| 203 | ulong bank; |
| 204 | int rcode = 0; |
| 205 | |
| 206 | *s_count = 0; |
| 207 | |
| 208 | for (bank=0; bank < CFG_MAX_FLASH_BANKS; ++bank) { |
| 209 | s_first[bank] = -1; /* first sector to erase */ |
| 210 | s_last [bank] = -1; /* last sector to erase */ |
| 211 | } |
| 212 | |
| 213 | for (bank=0,info=&flash_info[0]; |
| 214 | (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last); |
| 215 | ++bank, ++info) { |
| 216 | ulong b_end; |
| 217 | int sect; |
| 218 | short s_end; |
| 219 | |
| 220 | if (info->flash_id == FLASH_UNKNOWN) { |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | b_end = info->start[0] + info->size - 1; /* bank end addr */ |
| 225 | s_end = info->sector_count - 1; /* last sector */ |
| 226 | |
| 227 | |
| 228 | for (sect=0; sect < info->sector_count; ++sect) { |
| 229 | ulong end; /* last address in current sect */ |
| 230 | |
| 231 | end = (sect == s_end) ? b_end : info->start[sect + 1] - 1; |
| 232 | |
| 233 | if (addr_first > end) |
| 234 | continue; |
| 235 | if (addr_last < info->start[sect]) |
| 236 | continue; |
| 237 | |
| 238 | if (addr_first == info->start[sect]) { |
| 239 | s_first[bank] = sect; |
| 240 | } |
| 241 | if (addr_last == end) { |
| 242 | s_last[bank] = sect; |
| 243 | } |
| 244 | } |
| 245 | if (s_first[bank] >= 0) { |
| 246 | if (s_last[bank] < 0) { |
| 247 | if (addr_last > b_end) { |
| 248 | s_last[bank] = s_end; |
| 249 | } else { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 250 | puts ("Error: end address" |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 251 | " not on sector boundary\n"); |
| 252 | rcode = 1; |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | if (s_last[bank] < s_first[bank]) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 257 | puts ("Error: end sector" |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 258 | " precedes start sector\n"); |
| 259 | rcode = 1; |
| 260 | break; |
| 261 | } |
| 262 | sect = s_last[bank]; |
| 263 | addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1]; |
| 264 | (*s_count) += s_last[bank] - s_first[bank] + 1; |
wdenk | e2ffd59 | 2004-12-31 09:32:47 +0000 | [diff] [blame] | 265 | } else if (addr_first >= info->start[0] && addr_first < b_end) { |
| 266 | puts ("Error: start address not on sector boundary\n"); |
| 267 | rcode = 1; |
| 268 | break; |
wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 269 | } else if (s_last[bank] >= 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 270 | puts ("Error: cannot span across banks when they are" |
wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 271 | " mapped in reverse order\n"); |
| 272 | rcode = 1; |
| 273 | break; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | return rcode; |
| 278 | } |
| 279 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 280 | int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 281 | { |
| 282 | ulong bank; |
| 283 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 284 | #ifdef CONFIG_HAS_DATAFLASH |
| 285 | dataflash_print_info(); |
| 286 | #endif |
| 287 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 288 | if (argc == 1) { /* print info for all FLASH banks */ |
| 289 | for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) { |
| 290 | printf ("\nBank # %ld: ", bank+1); |
| 291 | |
| 292 | flash_print_info (&flash_info[bank]); |
| 293 | } |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | bank = simple_strtoul(argv[1], NULL, 16); |
| 298 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { |
| 299 | printf ("Only FLASH Banks # 1 ... # %d supported\n", |
| 300 | CFG_MAX_FLASH_BANKS); |
| 301 | return 1; |
| 302 | } |
| 303 | printf ("\nBank # %ld: ", bank); |
| 304 | flash_print_info (&flash_info[bank-1]); |
| 305 | return 0; |
| 306 | } |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 307 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 308 | int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 309 | { |
| 310 | flash_info_t *info; |
| 311 | ulong bank, addr_first, addr_last; |
| 312 | int n, sect_first, sect_last; |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 313 | #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE) |
| 314 | struct mtd_device *dev; |
| 315 | struct part_info *part; |
| 316 | u8 dev_type, dev_num, pnum; |
| 317 | #endif |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 318 | int rcode = 0; |
| 319 | |
| 320 | if (argc < 2) { |
| 321 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | if (strcmp(argv[1], "all") == 0) { |
| 326 | for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) { |
| 327 | printf ("Erase Flash Bank # %ld ", bank); |
| 328 | info = &flash_info[bank-1]; |
| 329 | rcode = flash_erase (info, 0, info->sector_count-1); |
| 330 | } |
| 331 | return rcode; |
| 332 | } |
| 333 | |
| 334 | if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) { |
| 335 | if (n < 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 336 | puts ("Bad sector specification\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 337 | return 1; |
| 338 | } |
| 339 | printf ("Erase Flash Sectors %d-%d in Bank # %d ", |
| 340 | sect_first, sect_last, (info-flash_info)+1); |
| 341 | rcode = flash_erase(info, sect_first, sect_last); |
| 342 | return rcode; |
| 343 | } |
| 344 | |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 345 | #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE) |
| 346 | /* erase <part-id> - erase partition */ |
| 347 | if ((argc == 2) && (id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) { |
| 348 | mtdparts_init(); |
| 349 | if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) { |
| 350 | if (dev->id->type == MTD_DEV_TYPE_NOR) { |
| 351 | bank = dev->id->num; |
| 352 | info = &flash_info[bank]; |
| 353 | addr_first = part->offset + info->start[0]; |
| 354 | addr_last = addr_first + part->size - 1; |
| 355 | |
| 356 | printf ("Erase Flash Parition %s, " |
| 357 | "bank %d, 0x%08lx - 0x%08lx ", |
| 358 | argv[1], bank, addr_first, |
| 359 | addr_last); |
| 360 | |
| 361 | rcode = flash_sect_erase(addr_first, addr_last); |
| 362 | return rcode; |
| 363 | } |
| 364 | |
| 365 | printf("cannot erase, not a NOR device\n"); |
| 366 | return 1; |
| 367 | } |
| 368 | } |
| 369 | #endif |
| 370 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 371 | if (argc != 3) { |
| 372 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 373 | return 1; |
| 374 | } |
| 375 | |
| 376 | if (strcmp(argv[1], "bank") == 0) { |
| 377 | bank = simple_strtoul(argv[2], NULL, 16); |
| 378 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { |
| 379 | printf ("Only FLASH Banks # 1 ... # %d supported\n", |
| 380 | CFG_MAX_FLASH_BANKS); |
| 381 | return 1; |
| 382 | } |
| 383 | printf ("Erase Flash Bank # %ld ", bank); |
| 384 | info = &flash_info[bank-1]; |
| 385 | rcode = flash_erase (info, 0, info->sector_count-1); |
| 386 | return rcode; |
| 387 | } |
| 388 | |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 389 | if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){ |
| 390 | printf ("Bad address format\n"); |
| 391 | return 1; |
| 392 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 393 | |
| 394 | if (addr_first >= addr_last) { |
| 395 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 396 | return 1; |
| 397 | } |
| 398 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 399 | rcode = flash_sect_erase(addr_first, addr_last); |
| 400 | return rcode; |
| 401 | } |
| 402 | |
| 403 | int flash_sect_erase (ulong addr_first, ulong addr_last) |
| 404 | { |
| 405 | flash_info_t *info; |
| 406 | ulong bank; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 407 | int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS]; |
| 408 | int erased = 0; |
| 409 | int planned; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 410 | int rcode = 0; |
| 411 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 412 | rcode = flash_fill_sect_ranges (addr_first, addr_last, |
| 413 | s_first, s_last, &planned ); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 414 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 415 | if (planned && (rcode == 0)) { |
| 416 | for (bank=0,info=&flash_info[0]; |
| 417 | (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0); |
| 418 | ++bank, ++info) { |
| 419 | if (s_first[bank]>=0) { |
| 420 | erased += s_last[bank] - s_first[bank] + 1; |
wdenk | 013dc8d | 2003-08-07 14:52:18 +0000 | [diff] [blame] | 421 | debug ("Erase Flash from 0x%08lx to 0x%08lx " |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 422 | "in Bank # %ld ", |
| 423 | info->start[s_first[bank]], |
| 424 | (s_last[bank] == info->sector_count) ? |
| 425 | info->start[0] + info->size - 1: |
| 426 | info->start[s_last[bank]+1] - 1, |
| 427 | bank+1); |
| 428 | rcode = flash_erase (info, s_first[bank], s_last[bank]); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 431 | printf ("Erased %d sectors\n", erased); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 432 | } else if (rcode == 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 433 | puts ("Error: start and/or end address" |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 434 | " not on sector boundary\n"); |
| 435 | rcode = 1; |
| 436 | } |
| 437 | return rcode; |
| 438 | } |
| 439 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 440 | int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 441 | { |
| 442 | flash_info_t *info; |
| 443 | ulong bank, addr_first, addr_last; |
| 444 | int i, p, n, sect_first, sect_last; |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 445 | #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE) |
| 446 | struct mtd_device *dev; |
| 447 | struct part_info *part; |
| 448 | u8 dev_type, dev_num, pnum; |
| 449 | #endif |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 450 | int rcode = 0; |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 451 | #ifdef CONFIG_HAS_DATAFLASH |
| 452 | int status; |
| 453 | #endif |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 454 | if (argc < 3) { |
| 455 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 456 | return 1; |
| 457 | } |
| 458 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 459 | if (strcmp(argv[1], "off") == 0) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 460 | p = 0; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 461 | } else if (strcmp(argv[1], "on") == 0) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 462 | p = 1; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 463 | } else { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 464 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 465 | return 1; |
| 466 | } |
| 467 | |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 468 | #ifdef CONFIG_HAS_DATAFLASH |
| 469 | if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) { |
| 470 | addr_first = simple_strtoul(argv[2], NULL, 16); |
| 471 | addr_last = simple_strtoul(argv[3], NULL, 16); |
| 472 | |
| 473 | if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) { |
| 474 | status = dataflash_real_protect(p,addr_first,addr_last); |
| 475 | if (status < 0){ |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 476 | puts ("Bad DataFlash sector specification\n"); |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 477 | return 1; |
| 478 | } |
| 479 | printf("%sProtect %d DataFlash Sectors\n", |
| 480 | p ? "" : "Un-", status); |
wdenk | 5779d8d | 2003-12-06 23:55:10 +0000 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | } |
| 484 | #endif |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 485 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 486 | if (strcmp(argv[2], "all") == 0) { |
| 487 | for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) { |
| 488 | info = &flash_info[bank-1]; |
| 489 | if (info->flash_id == FLASH_UNKNOWN) { |
| 490 | continue; |
| 491 | } |
| 492 | printf ("%sProtect Flash Bank # %ld\n", |
| 493 | p ? "" : "Un-", bank); |
| 494 | |
| 495 | for (i=0; i<info->sector_count; ++i) { |
| 496 | #if defined(CFG_FLASH_PROTECTION) |
| 497 | if (flash_real_protect(info, i, p)) |
| 498 | rcode = 1; |
| 499 | putc ('.'); |
| 500 | #else |
| 501 | info->protect[i] = p; |
| 502 | #endif /* CFG_FLASH_PROTECTION */ |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | #if defined(CFG_FLASH_PROTECTION) |
| 507 | if (!rcode) puts (" done\n"); |
| 508 | #endif /* CFG_FLASH_PROTECTION */ |
| 509 | |
| 510 | return rcode; |
| 511 | } |
| 512 | |
| 513 | if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) { |
| 514 | if (n < 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 515 | puts ("Bad sector specification\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 516 | return 1; |
| 517 | } |
| 518 | printf("%sProtect Flash Sectors %d-%d in Bank # %d\n", |
| 519 | p ? "" : "Un-", sect_first, sect_last, |
| 520 | (info-flash_info)+1); |
| 521 | for (i = sect_first; i <= sect_last; i++) { |
| 522 | #if defined(CFG_FLASH_PROTECTION) |
| 523 | if (flash_real_protect(info, i, p)) |
| 524 | rcode = 1; |
| 525 | putc ('.'); |
| 526 | #else |
| 527 | info->protect[i] = p; |
| 528 | #endif /* CFG_FLASH_PROTECTION */ |
| 529 | } |
| 530 | |
| 531 | #if defined(CFG_FLASH_PROTECTION) |
| 532 | if (!rcode) puts (" done\n"); |
| 533 | #endif /* CFG_FLASH_PROTECTION */ |
| 534 | |
| 535 | return rcode; |
| 536 | } |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 537 | |
| 538 | #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE) |
| 539 | /* protect on/off <part-id> */ |
| 540 | if ((argc == 3) && (id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) { |
| 541 | mtdparts_init(); |
| 542 | if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) { |
| 543 | if (dev->id->type == MTD_DEV_TYPE_NOR) { |
| 544 | bank = dev->id->num; |
| 545 | info = &flash_info[bank]; |
| 546 | addr_first = part->offset + info->start[0]; |
| 547 | addr_last = addr_first + part->size - 1; |
| 548 | |
| 549 | printf ("%sProtect Flash Parition %s, " |
| 550 | "bank %d, 0x%08lx - 0x%08lx\n", |
| 551 | p ? "" : "Un", argv[1], |
| 552 | bank, addr_first, addr_last); |
| 553 | |
| 554 | rcode = flash_sect_protect (p, addr_first, addr_last); |
| 555 | return rcode; |
| 556 | } |
| 557 | |
| 558 | printf("cannot %sprotect, not a NOR device\n", |
| 559 | p ? "" : "un"); |
| 560 | return 1; |
| 561 | } |
| 562 | } |
| 563 | #endif |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 564 | |
| 565 | if (argc != 4) { |
| 566 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 567 | return 1; |
| 568 | } |
| 569 | |
| 570 | if (strcmp(argv[2], "bank") == 0) { |
| 571 | bank = simple_strtoul(argv[3], NULL, 16); |
| 572 | if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) { |
| 573 | printf ("Only FLASH Banks # 1 ... # %d supported\n", |
| 574 | CFG_MAX_FLASH_BANKS); |
| 575 | return 1; |
| 576 | } |
| 577 | printf ("%sProtect Flash Bank # %ld\n", |
| 578 | p ? "" : "Un-", bank); |
| 579 | info = &flash_info[bank-1]; |
| 580 | |
| 581 | if (info->flash_id == FLASH_UNKNOWN) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 582 | puts ("missing or unknown FLASH type\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 583 | return 1; |
| 584 | } |
| 585 | for (i=0; i<info->sector_count; ++i) { |
| 586 | #if defined(CFG_FLASH_PROTECTION) |
| 587 | if (flash_real_protect(info, i, p)) |
| 588 | rcode = 1; |
| 589 | putc ('.'); |
| 590 | #else |
| 591 | info->protect[i] = p; |
| 592 | #endif /* CFG_FLASH_PROTECTION */ |
| 593 | } |
| 594 | |
| 595 | #if defined(CFG_FLASH_PROTECTION) |
| 596 | if (!rcode) puts (" done\n"); |
| 597 | #endif /* CFG_FLASH_PROTECTION */ |
| 598 | |
| 599 | return rcode; |
| 600 | } |
| 601 | |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 602 | if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){ |
| 603 | printf("Bad address format\n"); |
| 604 | return 1; |
| 605 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 606 | |
| 607 | if (addr_first >= addr_last) { |
| 608 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 609 | return 1; |
| 610 | } |
| 611 | rcode = flash_sect_protect (p, addr_first, addr_last); |
| 612 | return rcode; |
| 613 | } |
| 614 | |
| 615 | |
| 616 | int flash_sect_protect (int p, ulong addr_first, ulong addr_last) |
| 617 | { |
| 618 | flash_info_t *info; |
| 619 | ulong bank; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 620 | int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS]; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 621 | int protected, i; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 622 | int planned; |
| 623 | int rcode; |
| 624 | |
| 625 | 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] | 626 | |
| 627 | protected = 0; |
| 628 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 629 | if (planned && (rcode == 0)) { |
| 630 | for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) { |
| 631 | if (info->flash_id == FLASH_UNKNOWN) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 632 | continue; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 633 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 634 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 635 | if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) { |
stroese | abcac87 | 2003-12-09 14:58:22 +0000 | [diff] [blame] | 636 | debug ("%sProtecting sectors %d..%d in bank %ld\n", |
| 637 | p ? "" : "Un-", |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 638 | s_first[bank], s_last[bank], bank+1); |
| 639 | protected += s_last[bank] - s_first[bank] + 1; |
| 640 | for (i=s_first[bank]; i<=s_last[bank]; ++i) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 641 | #if defined(CFG_FLASH_PROTECTION) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 642 | if (flash_real_protect(info, i, p)) |
| 643 | rcode = 1; |
| 644 | putc ('.'); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 645 | #else |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 646 | info->protect[i] = p; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 647 | #endif /* CFG_FLASH_PROTECTION */ |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 648 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 649 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 650 | #if defined(CFG_FLASH_PROTECTION) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 651 | if (!rcode) putc ('\n'); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 652 | #endif /* CFG_FLASH_PROTECTION */ |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 653 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 654 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 655 | printf ("%sProtected %d sectors\n", |
| 656 | p ? "" : "Un-", protected); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 657 | } else if (rcode == 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 658 | puts ("Error: start and/or end address" |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 659 | " not on sector boundary\n"); |
| 660 | rcode = 1; |
| 661 | } |
| 662 | return rcode; |
| 663 | } |
| 664 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 665 | |
| 666 | /**************************************************/ |
Wolfgang Denk | c19c313 | 2005-08-09 17:25:22 +0200 | [diff] [blame^] | 667 | #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE) |
| 668 | # define TMP_ERASE "erase <part-id>\n - erase partition\n" |
| 669 | # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n" |
| 670 | # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n" |
| 671 | #else |
| 672 | # define TMP_ERASE /* empty */ |
| 673 | # define TMP_PROT_ON /* empty */ |
| 674 | # define TMP_PROT_OFF /* empty */ |
| 675 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 676 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 677 | U_BOOT_CMD( |
| 678 | flinfo, 2, 1, do_flinfo, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 679 | "flinfo - print FLASH memory information\n", |
| 680 | "\n - print information for all FLASH memory banks\n" |
| 681 | "flinfo N\n - print information for FLASH memory bank # N\n" |
| 682 | ); |
| 683 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 684 | U_BOOT_CMD( |
| 685 | erase, 3, 1, do_flerase, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 686 | "erase - erase FLASH memory\n", |
| 687 | "start end\n" |
| 688 | " - erase FLASH from addr 'start' to addr 'end'\n" |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 689 | "erase start +len\n" |
| 690 | " - erase FLASH from addr 'start' to the end of sect " |
| 691 | "w/addr 'start'+'len'-1\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 692 | "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n" |
| 693 | "erase bank N\n - erase FLASH bank # N\n" |
Wolfgang Denk | c19c313 | 2005-08-09 17:25:22 +0200 | [diff] [blame^] | 694 | TMP_ERASE |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 695 | "erase all\n - erase all FLASH banks\n" |
| 696 | ); |
| 697 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 698 | U_BOOT_CMD( |
| 699 | protect, 4, 1, do_protect, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 700 | "protect - enable or disable FLASH write protection\n", |
| 701 | "on start end\n" |
| 702 | " - protect FLASH from addr 'start' to addr 'end'\n" |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 703 | "protect on start +len\n" |
| 704 | " - protect FLASH from addr 'start' to end of sect " |
| 705 | "w/addr 'start'+'len'-1\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 706 | "protect on N:SF[-SL]\n" |
| 707 | " - protect sectors SF-SL in FLASH bank # N\n" |
| 708 | "protect on bank N\n - protect FLASH bank # N\n" |
Wolfgang Denk | c19c313 | 2005-08-09 17:25:22 +0200 | [diff] [blame^] | 709 | TMP_PROT_ON |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 710 | "protect on all\n - protect all FLASH banks\n" |
| 711 | "protect off start end\n" |
| 712 | " - make FLASH from addr 'start' to addr 'end' writable\n" |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 713 | "protect off start +len\n" |
| 714 | " - make FLASH from addr 'start' to end of sect " |
| 715 | "w/addr 'start'+'len'-1 wrtable\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 716 | "protect off N:SF[-SL]\n" |
| 717 | " - make sectors SF-SL writable in FLASH bank # N\n" |
| 718 | "protect off bank N\n - make FLASH bank # N writable\n" |
Wolfgang Denk | c19c313 | 2005-08-09 17:25:22 +0200 | [diff] [blame^] | 719 | TMP_PROT_OFF |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 720 | "protect off all\n - make all FLASH banks writable\n" |
| 721 | ); |
| 722 | |
Wolfgang Denk | c19c313 | 2005-08-09 17:25:22 +0200 | [diff] [blame^] | 723 | #undef TMP_ERASE |
| 724 | #undef TMP_PROT_ON |
| 725 | #undef TMP_PROT_OFF |
| 726 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 727 | #endif /* CFG_CMD_FLASH */ |