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