blob: db4bb2529c6a60df3829811cbd2e8129af4c0d42 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38a24a62002-09-18 12:49:44 +00002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38a24a62002-09-18 12:49:44 +00005 */
6
7/*
8 * FLASH support
9 */
10#include <common.h>
11#include <command.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060013#include <uuid.h>
wdenk8bde7f72003-06-27 21:31:46 +000014
Alexander Steinc0008082010-08-11 16:48:04 +020015#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +020016#include <jffs2/jffs2.h>
17
Wolfgang Denk445093d2009-11-17 21:27:39 +010018/* partition handling routines */
Wolfgang Denk700a0c62005-08-08 01:03:24 +020019int mtdparts_init(void);
Stefan Roese68d7d652009-03-19 13:30:36 +010020int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
Wolfgang Denk700a0c62005-08-08 01:03:24 +020021int find_dev_and_part(const char *id, struct mtd_device **dev,
Patrick Delaunayc8363b12022-01-04 14:23:57 +010022 u8 *part_num, struct part_info **part);
Wolfgang Denk700a0c62005-08-08 01:03:24 +020023#endif
24
Masahiro Yamadae856bdc2017-02-11 22:43:54 +090025#ifdef CONFIG_MTD_NOR_FLASH
Stefan Roese3c299752010-08-31 10:04:11 +020026#include <flash.h>
27#include <mtd/cfi_flash.h>
wdenk38a24a62002-09-18 12:49:44 +000028extern flash_info_t flash_info[]; /* info for FLASH chips */
29
30/*
31 * The user interface starts numbering for Flash banks with 1
32 * for historical reasons.
33 */
34
35/*
36 * this routine looks for an abbreviated flash range specification.
37 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
38 * sector to erase, and SL is the last sector to erase (defaults to SF).
39 * bank numbers start at 1 to be consistent with other specs, sector numbers
40 * start at zero.
41 *
42 * returns: 1 - correct spec; *pinfo, *psf and *psl are
43 * set appropriately
44 * 0 - doesn't look like an abbreviated spec
45 * -1 - looks like an abbreviated spec, but got
46 * a parsing error, a number out of range,
47 * or an invalid flash bank.
48 */
49static int
Patrick Delaunayc8363b12022-01-04 14:23:57 +010050abbrev_spec(char *str, flash_info_t **pinfo, int *psf, int *psl)
wdenk38a24a62002-09-18 12:49:44 +000051{
wdenkbdccc4f2003-08-05 17:43:17 +000052 flash_info_t *fp;
53 int bank, first, last;
54 char *p, *ep;
wdenk38a24a62002-09-18 12:49:44 +000055
Patrick Delaunayc8363b12022-01-04 14:23:57 +010056 p = strchr(str, ':');
57 if (!p)
wdenkbdccc4f2003-08-05 17:43:17 +000058 return 0;
wdenk38a24a62002-09-18 12:49:44 +000059 *p++ = '\0';
60
Simon Glass0b1284e2021-07-24 09:03:30 -060061 bank = dectoul(str, &ep);
wdenkbdccc4f2003-08-05 17:43:17 +000062 if (ep == str || *ep != '\0' ||
Patrick Delaunay98150e72022-01-04 14:23:58 +010063 bank < 1 || bank > CFI_FLASH_BANKS)
Patrick Delaunayc8363b12022-01-04 14:23:57 +010064 return -1;
65
66 fp = &flash_info[bank - 1];
67 if (fp->flash_id == FLASH_UNKNOWN)
wdenkbdccc4f2003-08-05 17:43:17 +000068 return -1;
wdenk38a24a62002-09-18 12:49:44 +000069
wdenkbdccc4f2003-08-05 17:43:17 +000070 str = p;
Patrick Delaunayc8363b12022-01-04 14:23:57 +010071 p = strchr(str, '-');
72 if (p)
wdenkbdccc4f2003-08-05 17:43:17 +000073 *p++ = '\0';
wdenk38a24a62002-09-18 12:49:44 +000074
Simon Glass0b1284e2021-07-24 09:03:30 -060075 first = dectoul(str, &ep);
wdenkbdccc4f2003-08-05 17:43:17 +000076 if (ep == str || *ep != '\0' || first >= fp->sector_count)
77 return -1;
wdenk38a24a62002-09-18 12:49:44 +000078
Patrick Delaunayc8363b12022-01-04 14:23:57 +010079 if (p) {
Simon Glass0b1284e2021-07-24 09:03:30 -060080 last = dectoul(p, &ep);
wdenkbdccc4f2003-08-05 17:43:17 +000081 if (ep == p || *ep != '\0' ||
Patrick Delaunayc8363b12022-01-04 14:23:57 +010082 last < first || last >= fp->sector_count)
wdenkbdccc4f2003-08-05 17:43:17 +000083 return -1;
84 } else {
85 last = first;
86 }
87
88 *pinfo = fp;
89 *psf = first;
90 *psl = last;
91
92 return 1;
wdenk38a24a62002-09-18 12:49:44 +000093}
wdenkbdccc4f2003-08-05 17:43:17 +000094
Wolfgang Denkf5301872005-07-21 11:45:50 +020095/*
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +020096 * Take *addr in Flash and adjust it to fall on the end of its sector
97 */
Simon Glassa595a0e2020-05-10 11:39:53 -060098int flash_sect_roundb(ulong *addr)
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +020099{
100 flash_info_t *info;
101 ulong bank, sector_end_addr;
102 char found;
103 int i;
104
105 /* find the end addr of the sector where the *addr is */
106 found = 0;
Patrick Delaunay98150e72022-01-04 14:23:58 +0100107 for (bank = 0; bank < CFI_FLASH_BANKS && !found; ++bank) {
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200108 info = &flash_info[bank];
109 for (i = 0; i < info->sector_count && !found; ++i) {
110 /* get the end address of the sector */
111 if (i == info->sector_count - 1) {
112 sector_end_addr = info->start[0] +
113 info->size - 1;
114 } else {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100115 sector_end_addr = info->start[i + 1] - 1;
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200116 }
117
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100118 if (*addr <= sector_end_addr && *addr >= info->start[i]) {
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200119 found = 1;
120 /* adjust *addr if necessary */
121 if (*addr < sector_end_addr)
122 *addr = sector_end_addr;
123 } /* sector */
124 } /* bank */
125 }
126 if (!found) {
Mike Williams16263082011-07-22 04:01:30 +0000127 /* error, address not in flash */
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200128 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
129 return 1;
130 }
131
132 return 0;
133}
134
135/*
Wolfgang Denkf5301872005-07-21 11:45:50 +0200136 * This function computes the start and end addresses for both
137 * erase and protect commands. The range of the addresses on which
138 * either of the commands is to operate can be given in two forms:
139 * 1. <cmd> start end - operate on <'start', 'end')
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200140 * 2. <cmd> start +length - operate on <'start', start + length)
Wolfgang Denkf5301872005-07-21 11:45:50 +0200141 * If the second form is used and the end address doesn't fall on the
142 * sector boundary, than it will be adjusted to the next sector boundary.
143 * If it isn't in the flash, the function will fail (return -1).
144 * Input:
145 * arg1, arg2: address specification (i.e. both command arguments)
146 * Output:
147 * addr_first, addr_last: computed address range
148 * Return:
149 * 1: success
150 * -1: failure (bad format, bad address).
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100151 */
Wolfgang Denkf5301872005-07-21 11:45:50 +0200152static int
153addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
154{
155 char *ep;
Heiko Schocherbb741402006-04-11 14:39:21 +0200156 char len_used; /* indicates if the "start +length" form used */
Wolfgang Denk2c61f142005-08-05 11:10:31 +0200157
Simon Glass7e5f4602021-07-24 09:03:29 -0600158 *addr_first = hextoul(arg1, &ep);
Wolfgang Denkf5301872005-07-21 11:45:50 +0200159 if (ep == arg1 || *ep != '\0')
160 return -1;
161
Heiko Schocherbb741402006-04-11 14:39:21 +0200162 len_used = 0;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100163 if (arg2 && *arg2 == '+') {
Wolfgang Denkf5301872005-07-21 11:45:50 +0200164 len_used = 1;
165 ++arg2;
166 }
167
Simon Glass7e5f4602021-07-24 09:03:29 -0600168 *addr_last = hextoul(arg2, &ep);
Wolfgang Denkf5301872005-07-21 11:45:50 +0200169 if (ep == arg2 || *ep != '\0')
170 return -1;
171
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100172 if (len_used) {
Wolfgang Denkf5301872005-07-21 11:45:50 +0200173 /*
174 * *addr_last has the length, compute correct *addr_last
175 * XXX watch out for the integer overflow! Right now it is
176 * checked for in both the callers.
177 */
178 *addr_last = *addr_first + *addr_last - 1;
179
180 /*
181 * It may happen that *addr_last doesn't fall on the sector
182 * boundary. We want to round such an address to the next
183 * sector boundary, so that the commands don't fail later on.
184 */
185
Bartlomiej Sieka3f0cf512008-10-01 15:26:27 +0200186 if (flash_sect_roundb(addr_last) > 0)
Wolfgang Denkf5301872005-07-21 11:45:50 +0200187 return -1;
Wolfgang Denkf5301872005-07-21 11:45:50 +0200188 } /* "start +length" from used */
189
190 return 1;
191}
192
wdenkbdccc4f2003-08-05 17:43:17 +0000193static int
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100194flash_fill_sect_ranges(ulong addr_first, ulong addr_last,
195 int *s_first, int *s_last,
196 int *s_count)
wdenkbdccc4f2003-08-05 17:43:17 +0000197{
198 flash_info_t *info;
199 ulong bank;
200 int rcode = 0;
201
202 *s_count = 0;
203
Patrick Delaunay98150e72022-01-04 14:23:58 +0100204 for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) {
wdenkbdccc4f2003-08-05 17:43:17 +0000205 s_first[bank] = -1; /* first sector to erase */
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100206 s_last[bank] = -1; /* last sector to erase */
wdenkbdccc4f2003-08-05 17:43:17 +0000207 }
208
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100209 for (bank = 0, info = &flash_info[0];
Patrick Delaunay98150e72022-01-04 14:23:58 +0100210 (bank < CFI_FLASH_BANKS) && (addr_first <= addr_last);
wdenkbdccc4f2003-08-05 17:43:17 +0000211 ++bank, ++info) {
212 ulong b_end;
213 int sect;
214 short s_end;
215
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100216 if (info->flash_id == FLASH_UNKNOWN)
wdenkbdccc4f2003-08-05 17:43:17 +0000217 continue;
wdenkbdccc4f2003-08-05 17:43:17 +0000218
219 b_end = info->start[0] + info->size - 1; /* bank end addr */
220 s_end = info->sector_count - 1; /* last sector */
221
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100222 for (sect = 0; sect < info->sector_count; ++sect) {
wdenkbdccc4f2003-08-05 17:43:17 +0000223 ulong end; /* last address in current sect */
224
225 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
226
227 if (addr_first > end)
228 continue;
229 if (addr_last < info->start[sect])
230 continue;
231
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100232 if (addr_first == info->start[sect])
wdenkbdccc4f2003-08-05 17:43:17 +0000233 s_first[bank] = sect;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100234
235 if (addr_last == end)
wdenkbdccc4f2003-08-05 17:43:17 +0000236 s_last[bank] = sect;
wdenkbdccc4f2003-08-05 17:43:17 +0000237 }
238 if (s_first[bank] >= 0) {
239 if (s_last[bank] < 0) {
240 if (addr_last > b_end) {
241 s_last[bank] = s_end;
242 } else {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100243 puts("Error: end address not on sector boundary\n");
wdenkbdccc4f2003-08-05 17:43:17 +0000244 rcode = 1;
245 break;
246 }
247 }
248 if (s_last[bank] < s_first[bank]) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100249 puts("Error: end sector precedes start sector\n");
wdenkbdccc4f2003-08-05 17:43:17 +0000250 rcode = 1;
251 break;
252 }
253 sect = s_last[bank];
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100254 addr_first = (sect == s_end) ? b_end + 1 : info->start[sect + 1];
wdenkbdccc4f2003-08-05 17:43:17 +0000255 (*s_count) += s_last[bank] - s_first[bank] + 1;
wdenke2ffd592004-12-31 09:32:47 +0000256 } else if (addr_first >= info->start[0] && addr_first < b_end) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100257 puts("Error: start address not on sector boundary\n");
wdenke2ffd592004-12-31 09:32:47 +0000258 rcode = 1;
259 break;
wdenk2d5b5612003-10-14 19:43:55 +0000260 } else if (s_last[bank] >= 0) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100261 puts("Error: cannot span across banks when they are"
262 " mapped in reverse order\n");
wdenk2d5b5612003-10-14 19:43:55 +0000263 rcode = 1;
264 break;
wdenkbdccc4f2003-08-05 17:43:17 +0000265 }
266 }
267
268 return rcode;
269}
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900270#endif /* CONFIG_MTD_NOR_FLASH */
wdenkbdccc4f2003-08-05 17:43:17 +0000271
Simon Glass09140112020-05-10 11:40:03 -0600272static int do_flinfo(struct cmd_tbl *cmdtp, int flag, int argc,
273 char *const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000274{
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900275#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000276 ulong bank;
Stelian Pop880cc432008-03-26 22:52:35 +0100277#endif
wdenk38a24a62002-09-18 12:49:44 +0000278
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900279#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000280 if (argc == 1) { /* print info for all FLASH banks */
Patrick Delaunay98150e72022-01-04 14:23:58 +0100281 for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100282 printf("\nBank # %ld: ", bank + 1);
wdenk38a24a62002-09-18 12:49:44 +0000283
Simon Glassa595a0e2020-05-10 11:39:53 -0600284 flash_print_info(&flash_info[bank]);
wdenk38a24a62002-09-18 12:49:44 +0000285 }
286 return 0;
287 }
288
Simon Glass7e5f4602021-07-24 09:03:29 -0600289 bank = hextoul(argv[1], NULL);
Patrick Delaunay98150e72022-01-04 14:23:58 +0100290 if (bank < 1 || bank > CFI_FLASH_BANKS) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100291 printf("Only FLASH Banks # 1 ... # %d supported\n",
Patrick Delaunay98150e72022-01-04 14:23:58 +0100292 CFI_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000293 return 1;
294 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100295 printf("\nBank # %ld: ", bank);
Simon Glassa595a0e2020-05-10 11:39:53 -0600296 flash_print_info(&flash_info[bank - 1]);
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900297#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000298 return 0;
299}
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200300
Simon Glass09140112020-05-10 11:40:03 -0600301static int do_flerase(struct cmd_tbl *cmdtp, int flag, int argc,
302 char *const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000303{
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900304#ifdef CONFIG_MTD_NOR_FLASH
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000305 flash_info_t *info = NULL;
wdenk38a24a62002-09-18 12:49:44 +0000306 ulong bank, addr_first, addr_last;
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000307 int n, sect_first = 0, sect_last = 0;
Alexander Steinc0008082010-08-11 16:48:04 +0200308#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200309 struct mtd_device *dev;
310 struct part_info *part;
311 u8 dev_type, dev_num, pnum;
312#endif
wdenk38a24a62002-09-18 12:49:44 +0000313 int rcode = 0;
314
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200315 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000316 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000317
318 if (strcmp(argv[1], "all") == 0) {
Patrick Delaunay98150e72022-01-04 14:23:58 +0100319 for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100320 printf("Erase Flash Bank # %ld ", bank);
321 info = &flash_info[bank - 1];
Simon Glassa595a0e2020-05-10 11:39:53 -0600322 rcode = flash_erase(info, 0, info->sector_count - 1);
wdenk38a24a62002-09-18 12:49:44 +0000323 }
324 return rcode;
325 }
326
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100327 n = abbrev_spec(argv[1], &info, &sect_first, &sect_last);
328 if (n) {
wdenk38a24a62002-09-18 12:49:44 +0000329 if (n < 0) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100330 puts("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000331 return 1;
332 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100333 printf("Erase Flash Sectors %d-%d in Bank # %zu ",
334 sect_first, sect_last, (info - flash_info) + 1);
wdenk38a24a62002-09-18 12:49:44 +0000335 rcode = flash_erase(info, sect_first, sect_last);
336 return rcode;
337 }
338
Alexander Steinc0008082010-08-11 16:48:04 +0200339#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200340 /* erase <part-id> - erase partition */
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100341 if (argc == 2 && mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200342 mtdparts_init();
343 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
344 if (dev->id->type == MTD_DEV_TYPE_NOR) {
345 bank = dev->id->num;
346 info = &flash_info[bank];
347 addr_first = part->offset + info->start[0];
348 addr_last = addr_first + part->size - 1;
349
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100350 printf("Erase Flash Partition %s, bank %ld, 0x%08lx - 0x%08lx ",
351 argv[1], bank, addr_first,
352 addr_last);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200353
354 rcode = flash_sect_erase(addr_first, addr_last);
355 return rcode;
356 }
357
358 printf("cannot erase, not a NOR device\n");
359 return 1;
360 }
361 }
362#endif
363
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200364 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000365 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000366
367 if (strcmp(argv[1], "bank") == 0) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600368 bank = hextoul(argv[2], NULL);
Patrick Delaunay98150e72022-01-04 14:23:58 +0100369 if (bank < 1 || bank > CFI_FLASH_BANKS) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100370 printf("Only FLASH Banks # 1 ... # %d supported\n",
Patrick Delaunay98150e72022-01-04 14:23:58 +0100371 CFI_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000372 return 1;
373 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100374 printf("Erase Flash Bank # %ld ", bank);
375 info = &flash_info[bank - 1];
Simon Glassa595a0e2020-05-10 11:39:53 -0600376 rcode = flash_erase(info, 0, info->sector_count - 1);
wdenk38a24a62002-09-18 12:49:44 +0000377 return rcode;
378 }
379
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100380 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0) {
381 printf("Bad address format\n");
Wolfgang Denkf5301872005-07-21 11:45:50 +0200382 return 1;
383 }
wdenk38a24a62002-09-18 12:49:44 +0000384
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200385 if (addr_first >= addr_last)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000386 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000387
wdenk38a24a62002-09-18 12:49:44 +0000388 rcode = flash_sect_erase(addr_first, addr_last);
389 return rcode;
Stelian Pop880cc432008-03-26 22:52:35 +0100390#else
391 return 0;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900392#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000393}
394
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900395#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassa595a0e2020-05-10 11:39:53 -0600396int flash_sect_erase(ulong addr_first, ulong addr_last)
wdenk38a24a62002-09-18 12:49:44 +0000397{
398 flash_info_t *info;
399 ulong bank;
Patrick Delaunay98150e72022-01-04 14:23:58 +0100400 int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS];
wdenkbdccc4f2003-08-05 17:43:17 +0000401 int erased = 0;
402 int planned;
wdenk38a24a62002-09-18 12:49:44 +0000403 int rcode = 0;
404
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100405 rcode = flash_fill_sect_ranges(addr_first, addr_last, s_first, s_last, &planned);
wdenk38a24a62002-09-18 12:49:44 +0000406
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100407 if (planned && rcode == 0) {
408 for (bank = 0, info = &flash_info[0];
Patrick Delaunay98150e72022-01-04 14:23:58 +0100409 bank < CFI_FLASH_BANKS && rcode == 0;
wdenkbdccc4f2003-08-05 17:43:17 +0000410 ++bank, ++info) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100411 if (s_first[bank] >= 0) {
wdenkbdccc4f2003-08-05 17:43:17 +0000412 erased += s_last[bank] - s_first[bank] + 1;
Simon Glass3c7dded2020-05-10 11:40:04 -0600413 debug("Erase Flash from 0x%08lx to 0x%08lx 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);
Simon Glassa595a0e2020-05-10 11:39:53 -0600419 rcode = flash_erase(info, s_first[bank],
420 s_last[bank]);
wdenk38a24a62002-09-18 12:49:44 +0000421 }
422 }
Joe Hershbergerde15a062012-08-17 15:36:41 -0500423 if (rcode == 0)
424 printf("Erased %d sectors\n", erased);
wdenkbdccc4f2003-08-05 17:43:17 +0000425 } else if (rcode == 0) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100426 puts("Error: start and/or end address not on sector boundary\n");
wdenk38a24a62002-09-18 12:49:44 +0000427 rcode = 1;
428 }
429 return rcode;
430}
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900431#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000432
Simon Glass09140112020-05-10 11:40:03 -0600433static int do_protect(struct cmd_tbl *cmdtp, int flag, int argc,
434 char *const argv[])
wdenk38a24a62002-09-18 12:49:44 +0000435{
Marek Vasut5b3901d2011-09-26 02:26:02 +0200436 int rcode = 0;
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900437#ifdef CONFIG_MTD_NOR_FLASH
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000438 flash_info_t *info = NULL;
Stelian Pop880cc432008-03-26 22:52:35 +0100439 ulong bank;
Wolfgang Denk4f5710f2011-11-05 05:13:04 +0000440 int i, n, sect_first = 0, sect_last = 0;
Alexander Steinc0008082010-08-11 16:48:04 +0200441#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200442 struct mtd_device *dev;
443 struct part_info *part;
444 u8 dev_type, dev_num, pnum;
445#endif
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900446#endif /* CONFIG_MTD_NOR_FLASH */
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +0300447#if defined(CONFIG_MTD_NOR_FLASH)
Wolfgang Denk5669ed42009-07-18 23:18:14 +0200448 int p;
Marek Vasut5b3901d2011-09-26 02:26:02 +0200449 ulong addr_first, addr_last;
450#endif
Stefan Roese2662b402006-04-01 13:41:03 +0200451
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200452 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000453 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000454
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +0300455#if defined(CONFIG_MTD_NOR_FLASH)
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200456 if (strcmp(argv[1], "off") == 0)
wdenk38a24a62002-09-18 12:49:44 +0000457 p = 0;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200458 else if (strcmp(argv[1], "on") == 0)
wdenk38a24a62002-09-18 12:49:44 +0000459 p = 1;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200460 else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000461 return CMD_RET_USAGE;
Marek Vasut5b3901d2011-09-26 02:26:02 +0200462#endif
wdenk38a24a62002-09-18 12:49:44 +0000463
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900464#ifdef CONFIG_MTD_NOR_FLASH
wdenk38a24a62002-09-18 12:49:44 +0000465 if (strcmp(argv[2], "all") == 0) {
Patrick Delaunay98150e72022-01-04 14:23:58 +0100466 for (bank = 1; bank <= CFI_FLASH_BANKS; ++bank) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100467 info = &flash_info[bank - 1];
468 if (info->flash_id == FLASH_UNKNOWN)
wdenk38a24a62002-09-18 12:49:44 +0000469 continue;
wdenk38a24a62002-09-18 12:49:44 +0000470
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100471 printf("%sProtect Flash Bank # %ld\n",
472 p ? "" : "Un-", bank);
473
474 for (i = 0; i < info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200475#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000476 if (flash_real_protect(info, i, p))
477 rcode = 1;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100478 putc('.');
wdenk38a24a62002-09-18 12:49:44 +0000479#else
480 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200481#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000482 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200483#if defined(CONFIG_SYS_FLASH_PROTECTION)
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100484 if (!rcode)
485 puts(" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200486#endif /* CONFIG_SYS_FLASH_PROTECTION */
Heiko Schocherbb741402006-04-11 14:39:21 +0200487 }
wdenk38a24a62002-09-18 12:49:44 +0000488 return rcode;
489 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100490 n = abbrev_spec(argv[2], &info, &sect_first, &sect_last);
491 if (n) {
wdenk38a24a62002-09-18 12:49:44 +0000492 if (n < 0) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100493 puts("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000494 return 1;
495 }
Stefan Roesef2302d42008-08-06 14:05:38 +0200496 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100497 p ? "" : "Un-", sect_first, sect_last,
498 (info - flash_info) + 1);
wdenk38a24a62002-09-18 12:49:44 +0000499 for (i = sect_first; i <= sect_last; i++) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200500#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000501 if (flash_real_protect(info, i, p))
502 rcode = 1;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100503 putc('.');
wdenk38a24a62002-09-18 12:49:44 +0000504#else
505 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200506#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000507 }
508
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200509#if defined(CONFIG_SYS_FLASH_PROTECTION)
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100510 if (!rcode)
511 puts(" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200512#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000513
514 return rcode;
515 }
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200516
Alexander Steinc0008082010-08-11 16:48:04 +0200517#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200518 /* protect on/off <part-id> */
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100519 if (argc == 3 && mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200520 mtdparts_init();
521 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
522 if (dev->id->type == MTD_DEV_TYPE_NOR) {
523 bank = dev->id->num;
524 info = &flash_info[bank];
525 addr_first = part->offset + info->start[0];
526 addr_last = addr_first + part->size - 1;
527
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100528 printf("%sProtect Flash Partition %s, "
529 "bank %ld, 0x%08lx - 0x%08lx\n",
530 p ? "" : "Un", argv[1],
531 bank, addr_first, addr_last);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200532
Simon Glassa595a0e2020-05-10 11:39:53 -0600533 rcode = flash_sect_protect(p, addr_first,
534 addr_last);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200535 return rcode;
536 }
537
538 printf("cannot %sprotect, not a NOR device\n",
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100539 p ? "" : "un");
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200540 return 1;
541 }
542 }
543#endif
wdenk38a24a62002-09-18 12:49:44 +0000544
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200545 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000546 return CMD_RET_USAGE;
wdenk38a24a62002-09-18 12:49:44 +0000547
548 if (strcmp(argv[2], "bank") == 0) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600549 bank = hextoul(argv[3], NULL);
Patrick Delaunay98150e72022-01-04 14:23:58 +0100550 if (bank < 1 || bank > CFI_FLASH_BANKS) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100551 printf("Only FLASH Banks # 1 ... # %d supported\n",
Patrick Delaunay98150e72022-01-04 14:23:58 +0100552 CFI_FLASH_BANKS);
wdenk38a24a62002-09-18 12:49:44 +0000553 return 1;
554 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100555 printf("%sProtect Flash Bank # %ld\n",
556 p ? "" : "Un-", bank);
557 info = &flash_info[bank - 1];
wdenk38a24a62002-09-18 12:49:44 +0000558
559 if (info->flash_id == FLASH_UNKNOWN) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100560 puts("missing or unknown FLASH type\n");
wdenk38a24a62002-09-18 12:49:44 +0000561 return 1;
562 }
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100563 for (i = 0; i < info->sector_count; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200564#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenk38a24a62002-09-18 12:49:44 +0000565 if (flash_real_protect(info, i, p))
566 rcode = 1;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100567 putc('.');
wdenk38a24a62002-09-18 12:49:44 +0000568#else
569 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200570#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000571 }
572
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200573#if defined(CONFIG_SYS_FLASH_PROTECTION)
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100574 if (!rcode)
575 puts(" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200576#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000577
578 return rcode;
579 }
580
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100581 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0) {
Wolfgang Denkf5301872005-07-21 11:45:50 +0200582 printf("Bad address format\n");
583 return 1;
584 }
wdenk38a24a62002-09-18 12:49:44 +0000585
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200586 if (addr_first >= addr_last)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000587 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200588
Simon Glassa595a0e2020-05-10 11:39:53 -0600589 rcode = flash_sect_protect(p, addr_first, addr_last);
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900590#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000591 return rcode;
592}
593
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900594#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassa595a0e2020-05-10 11:39:53 -0600595int flash_sect_protect(int p, ulong addr_first, ulong addr_last)
wdenk38a24a62002-09-18 12:49:44 +0000596{
597 flash_info_t *info;
598 ulong bank;
Patrick Delaunay98150e72022-01-04 14:23:58 +0100599 int s_first[CFI_FLASH_BANKS], s_last[CFI_FLASH_BANKS];
wdenk38a24a62002-09-18 12:49:44 +0000600 int protected, i;
wdenkbdccc4f2003-08-05 17:43:17 +0000601 int planned;
602 int rcode;
603
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100604 rcode = flash_fill_sect_ranges(addr_first, addr_last, s_first, s_last, &planned);
wdenk38a24a62002-09-18 12:49:44 +0000605
606 protected = 0;
607
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100608 if (planned && rcode == 0) {
609 for (bank = 0, info = &flash_info[0];
Patrick Delaunay98150e72022-01-04 14:23:58 +0100610 bank < CFI_FLASH_BANKS; ++bank, ++info) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100611 if (info->flash_id == FLASH_UNKNOWN)
wdenk38a24a62002-09-18 12:49:44 +0000612 continue;
wdenk38a24a62002-09-18 12:49:44 +0000613
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100614 if (s_first[bank] >= 0 && s_first[bank] <= s_last[bank]) {
Simon Glass3c7dded2020-05-10 11:40:04 -0600615 debug("%sProtecting sectors %d..%d in bank %ld\n",
616 p ? "" : "Un-", s_first[bank],
617 s_last[bank], bank + 1);
wdenkbdccc4f2003-08-05 17:43:17 +0000618 protected += s_last[bank] - s_first[bank] + 1;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100619 for (i = s_first[bank]; i <= s_last[bank]; ++i) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200620#if defined(CONFIG_SYS_FLASH_PROTECTION)
wdenkbdccc4f2003-08-05 17:43:17 +0000621 if (flash_real_protect(info, i, p))
622 rcode = 1;
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100623 putc('.');
wdenk38a24a62002-09-18 12:49:44 +0000624#else
wdenkbdccc4f2003-08-05 17:43:17 +0000625 info->protect[i] = p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200626#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenkbdccc4f2003-08-05 17:43:17 +0000627 }
wdenk38a24a62002-09-18 12:49:44 +0000628 }
wdenkbdccc4f2003-08-05 17:43:17 +0000629 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200630#if defined(CONFIG_SYS_FLASH_PROTECTION)
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100631 puts(" done\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200632#endif /* CONFIG_SYS_FLASH_PROTECTION */
wdenk38a24a62002-09-18 12:49:44 +0000633
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100634 printf("%sProtected %d sectors\n",
635 p ? "" : "Un-", protected);
wdenkbdccc4f2003-08-05 17:43:17 +0000636 } else if (rcode == 0) {
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100637 puts("Error: start and/or end address not on sector boundary\n");
wdenk38a24a62002-09-18 12:49:44 +0000638 rcode = 1;
639 }
640 return rcode;
641}
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900642#endif /* CONFIG_MTD_NOR_FLASH */
wdenk38a24a62002-09-18 12:49:44 +0000643
wdenk8bde7f72003-06-27 21:31:46 +0000644/**************************************************/
Alexander Steinc0008082010-08-11 16:48:04 +0200645#if defined(CONFIG_CMD_MTDPARTS)
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200646# define TMP_ERASE "erase <part-id>\n - erase partition\n"
647# define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
648# define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
649#else
650# define TMP_ERASE /* empty */
651# define TMP_PROT_ON /* empty */
652# define TMP_PROT_OFF /* empty */
653#endif
wdenk8bde7f72003-06-27 21:31:46 +0000654
wdenk0d498392003-07-01 21:06:45 +0000655U_BOOT_CMD(
656 flinfo, 2, 1, do_flinfo,
Peter Tyser2fb26042009-01-27 18:03:12 -0600657 "print FLASH memory information",
wdenk8bde7f72003-06-27 21:31:46 +0000658 "\n - print information for all FLASH memory banks\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200659 "flinfo N\n - print information for FLASH memory bank # N"
wdenk8bde7f72003-06-27 21:31:46 +0000660);
661
wdenk0d498392003-07-01 21:06:45 +0000662U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200663 erase, 3, 0, do_flerase,
Peter Tyser2fb26042009-01-27 18:03:12 -0600664 "erase FLASH memory",
wdenk8bde7f72003-06-27 21:31:46 +0000665 "start end\n"
666 " - erase FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200667 "erase start +len\n"
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100668 " - erase FLASH from addr 'start' to the end of sect w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000669 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
670 "erase bank N\n - erase FLASH bank # N\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200671 TMP_ERASE
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200672 "erase all\n - erase all FLASH banks"
wdenk8bde7f72003-06-27 21:31:46 +0000673);
674
wdenk0d498392003-07-01 21:06:45 +0000675U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +0200676 protect, 4, 0, do_protect,
Peter Tyser2fb26042009-01-27 18:03:12 -0600677 "enable or disable FLASH write protection",
wdenk8bde7f72003-06-27 21:31:46 +0000678 "on start end\n"
679 " - protect FLASH from addr 'start' to addr 'end'\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200680 "protect on start +len\n"
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100681 " - protect FLASH from addr 'start' to end of sect w/addr 'start'+'len'-1\n"
wdenk8bde7f72003-06-27 21:31:46 +0000682 "protect on N:SF[-SL]\n"
683 " - protect sectors SF-SL in FLASH bank # N\n"
684 "protect on bank N\n - protect FLASH bank # N\n"
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200685 TMP_PROT_ON
wdenk8bde7f72003-06-27 21:31:46 +0000686 "protect on all\n - protect all FLASH banks\n"
687 "protect off start end\n"
688 " - make FLASH from addr 'start' to addr 'end' writable\n"
Wolfgang Denkf5301872005-07-21 11:45:50 +0200689 "protect off start +len\n"
Patrick Delaunayc8363b12022-01-04 14:23:57 +0100690 " - make FLASH from addr 'start' to end of sect w/addr 'start'+'len'-1 wrtable\n"
wdenk8bde7f72003-06-27 21:31:46 +0000691 "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 Denkc19c3132005-08-09 17:25:22 +0200694 TMP_PROT_OFF
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200695 "protect off all\n - make all FLASH banks writable"
wdenk8bde7f72003-06-27 21:31:46 +0000696);
697
Wolfgang Denkc19c3132005-08-09 17:25:22 +0200698#undef TMP_ERASE
699#undef TMP_PROT_ON
700#undef TMP_PROT_OFF