Tom Rini | 83d290c | 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> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | b79fdc7 | 2020-05-10 11:39:54 -0600 | [diff] [blame] | 13 | #include <uuid.h> |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 14 | |
Alexander Stein | c000808 | 2010-08-11 16:48:04 +0200 | [diff] [blame] | 15 | #if defined(CONFIG_CMD_MTDPARTS) |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 16 | #include <jffs2/jffs2.h> |
| 17 | |
Wolfgang Denk | 445093d | 2009-11-17 21:27:39 +0100 | [diff] [blame] | 18 | /* partition handling routines */ |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 19 | int mtdparts_init(void); |
Stefan Roese | 68d7d65 | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 20 | 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] | 21 | int find_dev_and_part(const char *id, struct mtd_device **dev, |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 22 | u8 *part_num, struct part_info **part); |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 23 | #endif |
| 24 | |
Masahiro Yamada | e856bdc | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 25 | #ifdef CONFIG_MTD_NOR_FLASH |
Stefan Roese | 3c29975 | 2010-08-31 10:04:11 +0200 | [diff] [blame] | 26 | #include <flash.h> |
| 27 | #include <mtd/cfi_flash.h> |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 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 |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [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 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 55 | p = strchr(str, ':'); |
| 56 | if (!p) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 57 | return 0; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 58 | *p++ = '\0'; |
| 59 | |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 60 | bank = dectoul(str, &ep); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 61 | if (ep == str || *ep != '\0' || |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 62 | bank < 1 || bank > CFI_FLASH_BANKS) |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 63 | return -1; |
| 64 | |
| 65 | fp = &flash_info[bank - 1]; |
| 66 | if (fp->flash_id == FLASH_UNKNOWN) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 67 | return -1; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 68 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 69 | str = p; |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 70 | p = strchr(str, '-'); |
| 71 | if (p) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 72 | *p++ = '\0'; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 73 | |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 74 | first = dectoul(str, &ep); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 75 | if (ep == str || *ep != '\0' || first >= fp->sector_count) |
| 76 | return -1; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 77 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 78 | if (p) { |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 79 | last = dectoul(p, &ep); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 80 | if (ep == p || *ep != '\0' || |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 81 | last < first || last >= fp->sector_count) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 82 | return -1; |
| 83 | } else { |
| 84 | last = first; |
| 85 | } |
| 86 | |
| 87 | *pinfo = fp; |
| 88 | *psf = first; |
| 89 | *psl = last; |
| 90 | |
| 91 | return 1; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 92 | } |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 93 | |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 94 | /* |
Bartlomiej Sieka | 3f0cf51 | 2008-10-01 15:26:27 +0200 | [diff] [blame] | 95 | * Take *addr in Flash and adjust it to fall on the end of its sector |
| 96 | */ |
Simon Glass | a595a0e | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 97 | int flash_sect_roundb(ulong *addr) |
Bartlomiej Sieka | 3f0cf51 | 2008-10-01 15:26:27 +0200 | [diff] [blame] | 98 | { |
| 99 | flash_info_t *info; |
| 100 | ulong bank, sector_end_addr; |
| 101 | char found; |
| 102 | int i; |
| 103 | |
| 104 | /* find the end addr of the sector where the *addr is */ |
| 105 | found = 0; |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 106 | for (bank = 0; bank < CFI_FLASH_BANKS && !found; ++bank) { |
Bartlomiej Sieka | 3f0cf51 | 2008-10-01 15:26:27 +0200 | [diff] [blame] | 107 | info = &flash_info[bank]; |
| 108 | for (i = 0; i < info->sector_count && !found; ++i) { |
| 109 | /* get the end address of the sector */ |
| 110 | if (i == info->sector_count - 1) { |
| 111 | sector_end_addr = info->start[0] + |
| 112 | info->size - 1; |
| 113 | } else { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 114 | sector_end_addr = info->start[i + 1] - 1; |
Bartlomiej Sieka | 3f0cf51 | 2008-10-01 15:26:27 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 117 | if (*addr <= sector_end_addr && *addr >= info->start[i]) { |
Bartlomiej Sieka | 3f0cf51 | 2008-10-01 15:26:27 +0200 | [diff] [blame] | 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 | 1626308 | 2011-07-22 04:01:30 +0000 | [diff] [blame] | 126 | /* error, address not in flash */ |
Bartlomiej Sieka | 3f0cf51 | 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 | f530187 | 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 | 095b8a3 | 2005-08-02 17:06:17 +0200 | [diff] [blame] | 139 | * 2. <cmd> start +length - operate on <'start', start + length) |
Wolfgang Denk | f530187 | 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). |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 150 | */ |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 151 | static int |
| 152 | addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last) |
| 153 | { |
| 154 | char *ep; |
Heiko Schocher | bb74140 | 2006-04-11 14:39:21 +0200 | [diff] [blame] | 155 | char len_used; /* indicates if the "start +length" form used */ |
Wolfgang Denk | 2c61f14 | 2005-08-05 11:10:31 +0200 | [diff] [blame] | 156 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 157 | *addr_first = hextoul(arg1, &ep); |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 158 | if (ep == arg1 || *ep != '\0') |
| 159 | return -1; |
| 160 | |
Heiko Schocher | bb74140 | 2006-04-11 14:39:21 +0200 | [diff] [blame] | 161 | len_used = 0; |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 162 | if (arg2 && *arg2 == '+') { |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 163 | len_used = 1; |
| 164 | ++arg2; |
| 165 | } |
| 166 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 167 | *addr_last = hextoul(arg2, &ep); |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 168 | if (ep == arg2 || *ep != '\0') |
| 169 | return -1; |
| 170 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 171 | if (len_used) { |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 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 | 3f0cf51 | 2008-10-01 15:26:27 +0200 | [diff] [blame] | 185 | if (flash_sect_roundb(addr_last) > 0) |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 186 | return -1; |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 187 | } /* "start +length" from used */ |
| 188 | |
| 189 | return 1; |
| 190 | } |
| 191 | |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 192 | static int |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 193 | flash_fill_sect_ranges(ulong addr_first, ulong addr_last, |
| 194 | int *s_first, int *s_last, |
| 195 | int *s_count) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 196 | { |
| 197 | flash_info_t *info; |
| 198 | ulong bank; |
| 199 | int rcode = 0; |
| 200 | |
| 201 | *s_count = 0; |
| 202 | |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 203 | for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) { |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 204 | s_first[bank] = -1; /* first sector to erase */ |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 205 | s_last[bank] = -1; /* last sector to erase */ |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 208 | for (bank = 0, info = &flash_info[0]; |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 209 | (bank < CFI_FLASH_BANKS) && (addr_first <= addr_last); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 210 | ++bank, ++info) { |
| 211 | ulong b_end; |
| 212 | int sect; |
| 213 | short s_end; |
| 214 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 215 | if (info->flash_id == FLASH_UNKNOWN) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 216 | continue; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 217 | |
| 218 | b_end = info->start[0] + info->size - 1; /* bank end addr */ |
| 219 | s_end = info->sector_count - 1; /* last sector */ |
| 220 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 221 | for (sect = 0; sect < info->sector_count; ++sect) { |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 222 | ulong end; /* last address in current sect */ |
| 223 | |
| 224 | end = (sect == s_end) ? b_end : info->start[sect + 1] - 1; |
| 225 | |
| 226 | if (addr_first > end) |
| 227 | continue; |
| 228 | if (addr_last < info->start[sect]) |
| 229 | continue; |
| 230 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 231 | if (addr_first == info->start[sect]) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 232 | s_first[bank] = sect; |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 233 | |
| 234 | if (addr_last == end) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 235 | s_last[bank] = sect; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 236 | } |
| 237 | if (s_first[bank] >= 0) { |
| 238 | if (s_last[bank] < 0) { |
| 239 | if (addr_last > b_end) { |
| 240 | s_last[bank] = s_end; |
| 241 | } else { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 242 | puts("Error: end address not on sector boundary\n"); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 243 | rcode = 1; |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | if (s_last[bank] < s_first[bank]) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 248 | puts("Error: end sector precedes start sector\n"); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 249 | rcode = 1; |
| 250 | break; |
| 251 | } |
| 252 | sect = s_last[bank]; |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 253 | addr_first = (sect == s_end) ? b_end + 1 : info->start[sect + 1]; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 254 | (*s_count) += s_last[bank] - s_first[bank] + 1; |
wdenk | e2ffd59 | 2004-12-31 09:32:47 +0000 | [diff] [blame] | 255 | } else if (addr_first >= info->start[0] && addr_first < b_end) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 256 | puts("Error: start address not on sector boundary\n"); |
wdenk | e2ffd59 | 2004-12-31 09:32:47 +0000 | [diff] [blame] | 257 | rcode = 1; |
| 258 | break; |
wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 259 | } else if (s_last[bank] >= 0) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 260 | puts("Error: cannot span across banks when they are" |
| 261 | " mapped in reverse order\n"); |
wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 262 | rcode = 1; |
| 263 | break; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | return rcode; |
| 268 | } |
Masahiro Yamada | e856bdc | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 269 | #endif /* CONFIG_MTD_NOR_FLASH */ |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 270 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 271 | static int do_flinfo(struct cmd_tbl *cmdtp, int flag, int argc, |
| 272 | 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 */ |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 280 | for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 281 | printf("\nBank # %ld: ", bank + 1); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 282 | |
Simon Glass | a595a0e | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 283 | flash_print_info(&flash_info[bank]); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 284 | } |
| 285 | return 0; |
| 286 | } |
| 287 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 288 | bank = hextoul(argv[1], NULL); |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 289 | if (bank < 1 || bank > CFI_FLASH_BANKS) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 290 | printf("Only FLASH Banks # 1 ... # %d supported\n", |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 291 | CFI_FLASH_BANKS); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 292 | return 1; |
| 293 | } |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 294 | printf("\nBank # %ld: ", bank); |
Simon Glass | a595a0e | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 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 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 300 | static int do_flerase(struct cmd_tbl *cmdtp, int flag, int argc, |
| 301 | char *const argv[]) |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 302 | { |
Masahiro Yamada | e856bdc | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 303 | #ifdef CONFIG_MTD_NOR_FLASH |
Wolfgang Denk | 4f5710f | 2011-11-05 05:13:04 +0000 | [diff] [blame] | 304 | flash_info_t *info = NULL; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 305 | ulong bank, addr_first, addr_last; |
Wolfgang Denk | 4f5710f | 2011-11-05 05:13:04 +0000 | [diff] [blame] | 306 | int n, sect_first = 0, sect_last = 0; |
Alexander Stein | c000808 | 2010-08-11 16:48:04 +0200 | [diff] [blame] | 307 | #if defined(CONFIG_CMD_MTDPARTS) |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 308 | struct mtd_device *dev; |
| 309 | struct part_info *part; |
| 310 | u8 dev_type, dev_num, pnum; |
| 311 | #endif |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 312 | int rcode = 0; |
| 313 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 314 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 315 | return CMD_RET_USAGE; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 316 | |
| 317 | if (strcmp(argv[1], "all") == 0) { |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 318 | for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 319 | printf("Erase Flash Bank # %ld ", bank); |
| 320 | info = &flash_info[bank - 1]; |
Simon Glass | a595a0e | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 321 | rcode = flash_erase(info, 0, info->sector_count - 1); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 322 | } |
| 323 | return rcode; |
| 324 | } |
| 325 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 326 | n = abbrev_spec(argv[1], &info, §_first, §_last); |
| 327 | if (n) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 328 | if (n < 0) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 329 | puts("Bad sector specification\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 330 | return 1; |
| 331 | } |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 332 | printf("Erase Flash Sectors %d-%d in Bank # %zu ", |
| 333 | sect_first, sect_last, (info - flash_info) + 1); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 334 | rcode = flash_erase(info, sect_first, sect_last); |
| 335 | return rcode; |
| 336 | } |
| 337 | |
Alexander Stein | c000808 | 2010-08-11 16:48:04 +0200 | [diff] [blame] | 338 | #if defined(CONFIG_CMD_MTDPARTS) |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 339 | /* erase <part-id> - erase partition */ |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 340 | 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] | 341 | mtdparts_init(); |
| 342 | if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) { |
| 343 | if (dev->id->type == MTD_DEV_TYPE_NOR) { |
| 344 | bank = dev->id->num; |
| 345 | info = &flash_info[bank]; |
| 346 | addr_first = part->offset + info->start[0]; |
| 347 | addr_last = addr_first + part->size - 1; |
| 348 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 349 | printf("Erase Flash Partition %s, bank %ld, 0x%08lx - 0x%08lx ", |
| 350 | argv[1], bank, addr_first, |
| 351 | addr_last); |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 352 | |
| 353 | rcode = flash_sect_erase(addr_first, addr_last); |
| 354 | return rcode; |
| 355 | } |
| 356 | |
| 357 | printf("cannot erase, not a NOR device\n"); |
| 358 | return 1; |
| 359 | } |
| 360 | } |
| 361 | #endif |
| 362 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 363 | if (argc != 3) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 364 | return CMD_RET_USAGE; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 365 | |
| 366 | if (strcmp(argv[1], "bank") == 0) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 367 | bank = hextoul(argv[2], NULL); |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 368 | if (bank < 1 || bank > CFI_FLASH_BANKS) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 369 | printf("Only FLASH Banks # 1 ... # %d supported\n", |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 370 | CFI_FLASH_BANKS); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 371 | return 1; |
| 372 | } |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 373 | printf("Erase Flash Bank # %ld ", bank); |
| 374 | info = &flash_info[bank - 1]; |
Simon Glass | a595a0e | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 375 | rcode = flash_erase(info, 0, info->sector_count - 1); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 376 | return rcode; |
| 377 | } |
| 378 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 379 | if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0) { |
| 380 | printf("Bad address format\n"); |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 381 | return 1; |
| 382 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 383 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 384 | if (addr_first >= addr_last) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 385 | return CMD_RET_USAGE; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 386 | |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 387 | rcode = flash_sect_erase(addr_first, addr_last); |
| 388 | return rcode; |
Stelian Pop | 880cc43 | 2008-03-26 22:52:35 +0100 | [diff] [blame] | 389 | #else |
| 390 | return 0; |
Masahiro Yamada | e856bdc | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 391 | #endif /* CONFIG_MTD_NOR_FLASH */ |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Masahiro Yamada | e856bdc | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 394 | #ifdef CONFIG_MTD_NOR_FLASH |
Simon Glass | a595a0e | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 395 | int flash_sect_erase(ulong addr_first, ulong addr_last) |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 396 | { |
| 397 | flash_info_t *info; |
| 398 | ulong bank; |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 399 | int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS]; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 400 | int erased = 0; |
| 401 | int planned; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 402 | int rcode = 0; |
| 403 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 404 | 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] | 405 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 406 | if (planned && rcode == 0) { |
| 407 | for (bank = 0, info = &flash_info[0]; |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 408 | bank < CFI_FLASH_BANKS && rcode == 0; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 409 | ++bank, ++info) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 410 | if (s_first[bank] >= 0) { |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 411 | erased += s_last[bank] - s_first[bank] + 1; |
Simon Glass | 3c7dded | 2020-05-10 11:40:04 -0600 | [diff] [blame] | 412 | debug("Erase Flash from 0x%08lx to 0x%08lx 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); |
Simon Glass | a595a0e | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 418 | rcode = flash_erase(info, s_first[bank], |
| 419 | 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) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 425 | puts("Error: start and/or end address not on sector boundary\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 426 | rcode = 1; |
| 427 | } |
| 428 | return rcode; |
| 429 | } |
Masahiro Yamada | e856bdc | 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 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 432 | static int do_protect(struct cmd_tbl *cmdtp, int flag, int argc, |
| 433 | 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) { |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 465 | for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 466 | info = &flash_info[bank - 1]; |
| 467 | if (info->flash_id == FLASH_UNKNOWN) |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 468 | continue; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 469 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 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; |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 477 | putc('.'); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 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) |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 483 | if (!rcode) |
| 484 | puts(" done\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 485 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
Heiko Schocher | bb74140 | 2006-04-11 14:39:21 +0200 | [diff] [blame] | 486 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 487 | return rcode; |
| 488 | } |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 489 | n = abbrev_spec(argv[2], &info, §_first, §_last); |
| 490 | if (n) { |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 491 | if (n < 0) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 492 | puts("Bad sector specification\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 493 | return 1; |
| 494 | } |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 495 | printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n", |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 496 | p ? "" : "Un-", sect_first, sect_last, |
| 497 | (info - flash_info) + 1); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 498 | for (i = sect_first; i <= sect_last; i++) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 499 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 500 | if (flash_real_protect(info, i, p)) |
| 501 | rcode = 1; |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 502 | putc('.'); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 503 | #else |
| 504 | info->protect[i] = p; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 505 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 508 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 509 | if (!rcode) |
| 510 | puts(" done\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 511 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 512 | |
| 513 | return rcode; |
| 514 | } |
Wolfgang Denk | 8f79e4c | 2005-08-10 15:14:32 +0200 | [diff] [blame] | 515 | |
Alexander Stein | c000808 | 2010-08-11 16:48:04 +0200 | [diff] [blame] | 516 | #if defined(CONFIG_CMD_MTDPARTS) |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 517 | /* protect on/off <part-id> */ |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 518 | 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] | 519 | mtdparts_init(); |
| 520 | if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) { |
| 521 | if (dev->id->type == MTD_DEV_TYPE_NOR) { |
| 522 | bank = dev->id->num; |
| 523 | info = &flash_info[bank]; |
| 524 | addr_first = part->offset + info->start[0]; |
| 525 | addr_last = addr_first + part->size - 1; |
| 526 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 527 | printf("%sProtect Flash Partition %s, " |
| 528 | "bank %ld, 0x%08lx - 0x%08lx\n", |
| 529 | p ? "" : "Un", argv[1], |
| 530 | bank, addr_first, addr_last); |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 531 | |
Simon Glass | a595a0e | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 532 | rcode = flash_sect_protect(p, addr_first, |
| 533 | addr_last); |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 534 | return rcode; |
| 535 | } |
| 536 | |
| 537 | printf("cannot %sprotect, not a NOR device\n", |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 538 | p ? "" : "un"); |
Wolfgang Denk | 700a0c6 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 539 | return 1; |
| 540 | } |
| 541 | } |
| 542 | #endif |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 543 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 544 | if (argc != 4) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 545 | return CMD_RET_USAGE; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 546 | |
| 547 | if (strcmp(argv[2], "bank") == 0) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 548 | bank = hextoul(argv[3], NULL); |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 549 | if (bank < 1 || bank > CFI_FLASH_BANKS) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 550 | printf("Only FLASH Banks # 1 ... # %d supported\n", |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 551 | CFI_FLASH_BANKS); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 552 | return 1; |
| 553 | } |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 554 | printf("%sProtect Flash Bank # %ld\n", |
| 555 | p ? "" : "Un-", bank); |
| 556 | info = &flash_info[bank - 1]; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 557 | |
| 558 | if (info->flash_id == FLASH_UNKNOWN) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 559 | puts("missing or unknown FLASH type\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 560 | return 1; |
| 561 | } |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 562 | for (i = 0; i < info->sector_count; ++i) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 563 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 564 | if (flash_real_protect(info, i, p)) |
| 565 | rcode = 1; |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 566 | putc('.'); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 567 | #else |
| 568 | info->protect[i] = p; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 569 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 572 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 573 | if (!rcode) |
| 574 | puts(" done\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 575 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 576 | |
| 577 | return rcode; |
| 578 | } |
| 579 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 580 | if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0) { |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 581 | printf("Bad address format\n"); |
| 582 | return 1; |
| 583 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 584 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 585 | if (addr_first >= addr_last) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 586 | return CMD_RET_USAGE; |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 587 | |
Simon Glass | a595a0e | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 588 | rcode = flash_sect_protect(p, addr_first, addr_last); |
Masahiro Yamada | e856bdc | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 589 | #endif /* CONFIG_MTD_NOR_FLASH */ |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 590 | return rcode; |
| 591 | } |
| 592 | |
Masahiro Yamada | e856bdc | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 593 | #ifdef CONFIG_MTD_NOR_FLASH |
Simon Glass | a595a0e | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 594 | int flash_sect_protect(int p, ulong addr_first, ulong addr_last) |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 595 | { |
| 596 | flash_info_t *info; |
| 597 | ulong bank; |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 598 | int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS]; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 599 | int protected, i; |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 600 | int planned; |
| 601 | int rcode; |
| 602 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 603 | 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] | 604 | |
| 605 | protected = 0; |
| 606 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 607 | if (planned && rcode == 0) { |
| 608 | for (bank = 0, info = &flash_info[0]; |
Patrick Delaunay | 98150e7 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 609 | bank < CFI_FLASH_BANKS; ++bank, ++info) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 610 | if (info->flash_id == FLASH_UNKNOWN) |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 611 | continue; |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 612 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 613 | if (s_first[bank] >= 0 && s_first[bank] <= s_last[bank]) { |
Simon Glass | 3c7dded | 2020-05-10 11:40:04 -0600 | [diff] [blame] | 614 | debug("%sProtecting sectors %d..%d in bank %ld\n", |
| 615 | p ? "" : "Un-", s_first[bank], |
| 616 | s_last[bank], bank + 1); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 617 | protected += s_last[bank] - s_first[bank] + 1; |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 618 | for (i = s_first[bank]; i <= s_last[bank]; ++i) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 619 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 620 | if (flash_real_protect(info, i, p)) |
| 621 | rcode = 1; |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 622 | putc('.'); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 623 | #else |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 624 | info->protect[i] = p; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 625 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 626 | } |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 627 | } |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 628 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 629 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 630 | puts(" done\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 631 | #endif /* CONFIG_SYS_FLASH_PROTECTION */ |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 632 | |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 633 | printf("%sProtected %d sectors\n", |
| 634 | p ? "" : "Un-", protected); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 635 | } else if (rcode == 0) { |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 636 | puts("Error: start and/or end address not on sector boundary\n"); |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 637 | rcode = 1; |
| 638 | } |
| 639 | return rcode; |
| 640 | } |
Masahiro Yamada | e856bdc | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 641 | #endif /* CONFIG_MTD_NOR_FLASH */ |
wdenk | 38a24a6 | 2002-09-18 12:49:44 +0000 | [diff] [blame] | 642 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 643 | /**************************************************/ |
Alexander Stein | c000808 | 2010-08-11 16:48:04 +0200 | [diff] [blame] | 644 | #if defined(CONFIG_CMD_MTDPARTS) |
Wolfgang Denk | c19c313 | 2005-08-09 17:25:22 +0200 | [diff] [blame] | 645 | # define TMP_ERASE "erase <part-id>\n - erase partition\n" |
| 646 | # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n" |
| 647 | # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n" |
| 648 | #else |
| 649 | # define TMP_ERASE /* empty */ |
| 650 | # define TMP_PROT_ON /* empty */ |
| 651 | # define TMP_PROT_OFF /* empty */ |
| 652 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 653 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 654 | U_BOOT_CMD( |
| 655 | flinfo, 2, 1, do_flinfo, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 656 | "print FLASH memory information", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 657 | "\n - print information for all FLASH memory banks\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 658 | "flinfo N\n - print information for FLASH memory bank # N" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 659 | ); |
| 660 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 661 | U_BOOT_CMD( |
Detlev Zundel | 9912121 | 2007-05-23 19:02:41 +0200 | [diff] [blame] | 662 | erase, 3, 0, do_flerase, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 663 | "erase FLASH memory", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 664 | "start end\n" |
| 665 | " - erase FLASH from addr 'start' to addr 'end'\n" |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 666 | "erase start +len\n" |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 667 | " - erase FLASH from addr 'start' to the end of sect w/addr 'start'+'len'-1\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 668 | "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n" |
| 669 | "erase bank N\n - erase FLASH bank # N\n" |
Wolfgang Denk | c19c313 | 2005-08-09 17:25:22 +0200 | [diff] [blame] | 670 | TMP_ERASE |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 671 | "erase all\n - erase all FLASH banks" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 672 | ); |
| 673 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 674 | U_BOOT_CMD( |
Detlev Zundel | 9912121 | 2007-05-23 19:02:41 +0200 | [diff] [blame] | 675 | protect, 4, 0, do_protect, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 676 | "enable or disable FLASH write protection", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 677 | "on start end\n" |
| 678 | " - protect FLASH from addr 'start' to addr 'end'\n" |
Wolfgang Denk | f530187 | 2005-07-21 11:45:50 +0200 | [diff] [blame] | 679 | "protect on start +len\n" |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 680 | " - protect FLASH from addr 'start' to end of sect 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" |
Patrick Delaunay | c8363b1 | 2022-01-04 14:23:57 +0100 | [diff] [blame] | 689 | " - make FLASH from addr 'start' to end of sect w/addr 'start'+'len'-1 wrtable\n" |
wdenk | 8bde7f7 | 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 | c19c313 | 2005-08-09 17:25:22 +0200 | [diff] [blame] | 693 | TMP_PROT_OFF |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 694 | "protect off all\n - make all FLASH banks writable" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 695 | ); |
| 696 | |
Wolfgang Denk | c19c313 | 2005-08-09 17:25:22 +0200 | [diff] [blame] | 697 | #undef TMP_ERASE |
| 698 | #undef TMP_PROT_ON |
| 699 | #undef TMP_PROT_OFF |