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