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