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