add flash/nor/{tcl.c,imp.h} from flash/flash.c
[openocd/ztw.git] / src / flash / flash.c
blobb21838cfaaad3086d196de71bd8e52f6fb838fb5
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include "flash.h"
31 #include "common.h"
32 #include <target/image.h>
33 #include <helper/time_support.h>
35 static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock);
37 /* flash drivers
39 extern struct flash_driver lpc2000_flash;
40 extern struct flash_driver lpc288x_flash;
41 extern struct flash_driver lpc2900_flash;
42 extern struct flash_driver cfi_flash;
43 extern struct flash_driver at91sam3_flash;
44 extern struct flash_driver at91sam7_flash;
45 extern struct flash_driver str7x_flash;
46 extern struct flash_driver str9x_flash;
47 extern struct flash_driver aduc702x_flash;
48 extern struct flash_driver stellaris_flash;
49 extern struct flash_driver str9xpec_flash;
50 extern struct flash_driver stm32x_flash;
51 extern struct flash_driver tms470_flash;
52 extern struct flash_driver ecosflash_flash;
53 extern struct flash_driver ocl_flash;
54 extern struct flash_driver pic32mx_flash;
55 extern struct flash_driver avr_flash;
56 extern struct flash_driver faux_flash;
58 struct flash_driver *flash_drivers[] = {
59 &lpc2000_flash,
60 &lpc288x_flash,
61 &lpc2900_flash,
62 &cfi_flash,
63 &at91sam7_flash,
64 &at91sam3_flash,
65 &str7x_flash,
66 &str9x_flash,
67 &aduc702x_flash,
68 &stellaris_flash,
69 &str9xpec_flash,
70 &stm32x_flash,
71 &tms470_flash,
72 &ecosflash_flash,
73 &ocl_flash,
74 &pic32mx_flash,
75 &avr_flash,
76 &faux_flash,
77 NULL,
80 struct flash_bank *flash_banks;
82 /* wafer thin wrapper for invoking the flash driver */
83 static int flash_driver_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
85 int retval;
87 retval = bank->driver->write(bank, buffer, offset, count);
88 if (retval != ERROR_OK)
90 LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
91 bank->base, offset, retval);
94 return retval;
97 static int flash_driver_erase(struct flash_bank *bank, int first, int last)
99 int retval;
101 retval = bank->driver->erase(bank, first, last);
102 if (retval != ERROR_OK)
104 LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
107 return retval;
110 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
112 int retval;
114 retval = bank->driver->protect(bank, set, first, last);
115 if (retval != ERROR_OK)
117 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
120 return retval;
123 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
125 struct flash_bank *p;
126 int i = 0;
128 for (p = flash_banks; p; p = p->next)
130 if (i++ == num)
132 return p;
135 LOG_ERROR("flash bank %d does not exist", num);
136 return NULL;
139 int flash_get_bank_count(void)
141 struct flash_bank *p;
142 int i = 0;
143 for (p = flash_banks; p; p = p->next)
145 i++;
147 return i;
150 struct flash_bank *get_flash_bank_by_name(const char *name)
152 unsigned requested = get_flash_name_index(name);
153 unsigned found = 0;
155 struct flash_bank *bank;
156 for (bank = flash_banks; NULL != bank; bank = bank->next)
158 if (strcmp(bank->name, name) == 0)
159 return bank;
160 if (!flash_driver_name_matches(bank->driver->name, name))
161 continue;
162 if (++found < requested)
163 continue;
164 return bank;
166 return NULL;
169 struct flash_bank *get_flash_bank_by_num(int num)
171 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
172 int retval;
174 if (p == NULL)
175 return NULL;
177 retval = p->driver->auto_probe(p);
179 if (retval != ERROR_OK)
181 LOG_ERROR("auto_probe failed %d\n", retval);
182 return NULL;
184 return p;
187 COMMAND_HELPER(flash_command_get_bank, unsigned name_index,
188 struct flash_bank **bank)
190 const char *name = CMD_ARGV[name_index];
191 *bank = get_flash_bank_by_name(name);
192 if (*bank)
193 return ERROR_OK;
195 unsigned bank_num;
196 COMMAND_PARSE_NUMBER(uint, name, bank_num);
198 *bank = get_flash_bank_by_num(bank_num);
199 if (!*bank)
201 command_print(CMD_CTX, "flash bank '%s' not found", name);
202 return ERROR_INVALID_ARGUMENTS;
204 return ERROR_OK;
208 COMMAND_HANDLER(handle_flash_info_command)
210 struct flash_bank *p;
211 uint32_t i = 0;
212 int j = 0;
213 int retval;
215 if (CMD_ARGC != 1)
216 return ERROR_COMMAND_SYNTAX_ERROR;
218 unsigned bank_nr;
219 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
221 for (p = flash_banks; p; p = p->next, i++)
223 if (i != bank_nr)
224 continue;
226 char buf[1024];
228 /* attempt auto probe */
229 if ((retval = p->driver->auto_probe(p)) != ERROR_OK)
230 return retval;
232 command_print(CMD_CTX,
233 "#%" PRIi32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", buswidth %i, chipwidth %i",
235 p->driver->name,
236 p->base,
237 p->size,
238 p->bus_width,
239 p->chip_width);
240 for (j = 0; j < p->num_sectors; j++)
242 char *protect_state;
244 if (p->sectors[j].is_protected == 0)
245 protect_state = "not protected";
246 else if (p->sectors[j].is_protected == 1)
247 protect_state = "protected";
248 else
249 protect_state = "protection state unknown";
251 command_print(CMD_CTX,
252 "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
254 p->sectors[j].offset,
255 p->sectors[j].size,
256 p->sectors[j].size >> 10,
257 protect_state);
260 *buf = '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
261 retval = p->driver->info(p, buf, sizeof(buf));
262 command_print(CMD_CTX, "%s", buf);
263 if (retval != ERROR_OK)
264 LOG_ERROR("error retrieving flash info (%d)", retval);
267 return ERROR_OK;
270 COMMAND_HANDLER(handle_flash_probe_command)
272 int retval;
274 if (CMD_ARGC != 1)
276 return ERROR_COMMAND_SYNTAX_ERROR;
279 unsigned bank_nr;
280 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
281 struct flash_bank *p = get_flash_bank_by_num_noprobe(bank_nr);
282 if (p)
284 if ((retval = p->driver->probe(p)) == ERROR_OK)
286 command_print(CMD_CTX, "flash '%s' found at 0x%8.8" PRIx32, p->driver->name, p->base);
288 else if (retval == ERROR_FLASH_BANK_INVALID)
290 command_print(CMD_CTX, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32,
291 CMD_ARGV[0], p->base);
293 else
295 command_print(CMD_CTX, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32,
296 CMD_ARGV[0], p->base);
299 else
301 command_print(CMD_CTX, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
304 return ERROR_OK;
307 COMMAND_HANDLER(handle_flash_erase_check_command)
309 if (CMD_ARGC != 1)
311 return ERROR_COMMAND_SYNTAX_ERROR;
314 struct flash_bank *p;
315 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
316 if (ERROR_OK != retval)
317 return retval;
319 int j;
320 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
322 command_print(CMD_CTX, "successfully checked erase state");
324 else
326 command_print(CMD_CTX, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
327 CMD_ARGV[0], p->base);
330 for (j = 0; j < p->num_sectors; j++)
332 char *erase_state;
334 if (p->sectors[j].is_erased == 0)
335 erase_state = "not erased";
336 else if (p->sectors[j].is_erased == 1)
337 erase_state = "erased";
338 else
339 erase_state = "erase state unknown";
341 command_print(CMD_CTX,
342 "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
344 p->sectors[j].offset,
345 p->sectors[j].size,
346 p->sectors[j].size >> 10,
347 erase_state);
350 return ERROR_OK;
353 COMMAND_HANDLER(handle_flash_erase_address_command)
355 struct flash_bank *p;
356 int retval;
357 int address;
358 int length;
360 struct target *target = get_current_target(CMD_CTX);
362 if (CMD_ARGC != 2)
363 return ERROR_COMMAND_SYNTAX_ERROR;
365 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], address);
366 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], length);
367 if (length <= 0)
369 command_print(CMD_CTX, "Length must be >0");
370 return ERROR_COMMAND_SYNTAX_ERROR;
373 p = get_flash_bank_by_addr(target, address);
374 if (p == NULL)
376 return ERROR_FAIL;
379 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
380 flash_set_dirty();
382 struct duration bench;
383 duration_start(&bench);
385 retval = flash_erase_address_range(target, address, length);
387 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
389 command_print(CMD_CTX, "erased address 0x%8.8x (length %i)"
390 " in %fs (%0.3f kb/s)", address, length,
391 duration_elapsed(&bench), duration_kbps(&bench, length));
394 return retval;
397 COMMAND_HANDLER(handle_flash_protect_check_command)
399 if (CMD_ARGC != 1)
400 return ERROR_COMMAND_SYNTAX_ERROR;
402 struct flash_bank *p;
403 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
404 if (ERROR_OK != retval)
405 return retval;
407 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
409 command_print(CMD_CTX, "successfully checked protect state");
411 else if (retval == ERROR_FLASH_OPERATION_FAILED)
413 command_print(CMD_CTX, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
415 else
417 command_print(CMD_CTX, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
420 return ERROR_OK;
423 static int flash_check_sector_parameters(struct command_context *cmd_ctx,
424 uint32_t first, uint32_t last, uint32_t num_sectors)
426 if (!(first <= last)) {
427 command_print(cmd_ctx, "ERROR: "
428 "first sector must be <= last sector");
429 return ERROR_FAIL;
432 if (!(last <= (num_sectors - 1))) {
433 command_print(cmd_ctx, "ERROR: last sector must be <= %d",
434 (int) num_sectors - 1);
435 return ERROR_FAIL;
438 return ERROR_OK;
441 COMMAND_HANDLER(handle_flash_erase_command)
443 if (CMD_ARGC != 3)
444 return ERROR_COMMAND_SYNTAX_ERROR;
446 uint32_t bank_nr;
447 uint32_t first;
448 uint32_t last;
450 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
451 struct flash_bank *p = get_flash_bank_by_num(bank_nr);
452 if (!p)
453 return ERROR_OK;
455 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
456 if (strcmp(CMD_ARGV[2], "last") == 0)
457 last = p->num_sectors - 1;
458 else
459 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
461 int retval;
462 if ((retval = flash_check_sector_parameters(CMD_CTX,
463 first, last, p->num_sectors)) != ERROR_OK)
464 return retval;
466 struct duration bench;
467 duration_start(&bench);
469 retval = flash_driver_erase(p, first, last);
471 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
473 command_print(CMD_CTX, "erased sectors %" PRIu32 " "
474 "through %" PRIu32" on flash bank %" PRIu32 " "
475 "in %fs", first, last, bank_nr, duration_elapsed(&bench));
478 return ERROR_OK;
481 COMMAND_HANDLER(handle_flash_protect_command)
483 if (CMD_ARGC != 4)
484 return ERROR_COMMAND_SYNTAX_ERROR;
486 uint32_t bank_nr;
487 uint32_t first;
488 uint32_t last;
490 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
491 struct flash_bank *p = get_flash_bank_by_num(bank_nr);
492 if (!p)
493 return ERROR_OK;
495 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
496 if (strcmp(CMD_ARGV[2], "last") == 0)
497 last = p->num_sectors - 1;
498 else
499 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
501 bool set;
502 COMMAND_PARSE_ON_OFF(CMD_ARGV[3], set);
504 int retval;
505 if ((retval = flash_check_sector_parameters(CMD_CTX,
506 first, last, p->num_sectors)) != ERROR_OK)
507 return retval;
509 retval = flash_driver_protect(p, set, first, last);
510 if (retval == ERROR_OK) {
511 command_print(CMD_CTX, "%s protection for sectors %i "
512 "through %i on flash bank %i",
513 (set) ? "set" : "cleared", (int) first,
514 (int) last, (int) bank_nr);
517 return ERROR_OK;
520 COMMAND_HANDLER(handle_flash_write_image_command)
522 struct target *target = get_current_target(CMD_CTX);
524 struct image image;
525 uint32_t written;
527 int retval;
529 if (CMD_ARGC < 1)
531 return ERROR_COMMAND_SYNTAX_ERROR;
534 /* flash auto-erase is disabled by default*/
535 int auto_erase = 0;
536 bool auto_unlock = false;
538 for (;;)
540 if (strcmp(CMD_ARGV[0], "erase") == 0)
542 auto_erase = 1;
543 CMD_ARGV++;
544 CMD_ARGC--;
545 command_print(CMD_CTX, "auto erase enabled");
546 } else if (strcmp(CMD_ARGV[0], "unlock") == 0)
548 auto_unlock = true;
549 CMD_ARGV++;
550 CMD_ARGC--;
551 command_print(CMD_CTX, "auto unlock enabled");
552 } else
554 break;
558 if (CMD_ARGC < 1)
560 return ERROR_COMMAND_SYNTAX_ERROR;
563 if (!target)
565 LOG_ERROR("no target selected");
566 return ERROR_FAIL;
569 struct duration bench;
570 duration_start(&bench);
572 if (CMD_ARGC >= 2)
574 image.base_address_set = 1;
575 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], image.base_address);
577 else
579 image.base_address_set = 0;
580 image.base_address = 0x0;
583 image.start_address_set = 0;
585 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
586 if (retval != ERROR_OK)
588 return retval;
591 retval = flash_write_unlock(target, &image, &written, auto_erase, auto_unlock);
592 if (retval != ERROR_OK)
594 image_close(&image);
595 return retval;
598 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
600 command_print(CMD_CTX, "wrote %" PRIu32 " byte from file %s "
601 "in %fs (%0.3f kb/s)", written, CMD_ARGV[0],
602 duration_elapsed(&bench), duration_kbps(&bench, written));
605 image_close(&image);
607 return retval;
610 COMMAND_HANDLER(handle_flash_fill_command)
612 int err = ERROR_OK;
613 uint32_t address;
614 uint32_t pattern;
615 uint32_t count;
616 uint32_t wrote = 0;
617 uint32_t cur_size = 0;
618 uint32_t chunk_count;
619 struct target *target = get_current_target(CMD_CTX);
620 uint32_t i;
621 uint32_t wordsize;
622 int retval = ERROR_OK;
624 static size_t const chunksize = 1024;
625 uint8_t *chunk = malloc(chunksize);
626 if (chunk == NULL)
627 return ERROR_FAIL;
629 uint8_t *readback = malloc(chunksize);
630 if (readback == NULL)
632 free(chunk);
633 return ERROR_FAIL;
637 if (CMD_ARGC != 3)
639 retval = ERROR_COMMAND_SYNTAX_ERROR;
640 goto done;
644 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
645 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
646 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
648 if (count == 0)
649 goto done;
651 switch (CMD_NAME[4])
653 case 'w':
654 wordsize = 4;
655 break;
656 case 'h':
657 wordsize = 2;
658 break;
659 case 'b':
660 wordsize = 1;
661 break;
662 default:
663 retval = ERROR_COMMAND_SYNTAX_ERROR;
664 goto done;
667 chunk_count = MIN(count, (chunksize / wordsize));
668 switch (wordsize)
670 case 4:
671 for (i = 0; i < chunk_count; i++)
673 target_buffer_set_u32(target, chunk + i * wordsize, pattern);
675 break;
676 case 2:
677 for (i = 0; i < chunk_count; i++)
679 target_buffer_set_u16(target, chunk + i * wordsize, pattern);
681 break;
682 case 1:
683 memset(chunk, pattern, chunk_count);
684 break;
685 default:
686 LOG_ERROR("BUG: can't happen");
687 exit(-1);
690 struct duration bench;
691 duration_start(&bench);
693 for (wrote = 0; wrote < (count*wordsize); wrote += cur_size)
695 cur_size = MIN((count*wordsize - wrote), sizeof(chunk));
696 struct flash_bank *bank;
697 bank = get_flash_bank_by_addr(target, address);
698 if (bank == NULL)
700 retval = ERROR_FAIL;
701 goto done;
703 err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
704 if (err != ERROR_OK)
706 retval = err;
707 goto done;
710 err = target_read_buffer(target, address + wrote, cur_size, readback);
711 if (err != ERROR_OK)
713 retval = err;
714 goto done;
717 unsigned i;
718 for (i = 0; i < cur_size; i++)
720 if (readback[i]!=chunk[i])
722 LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
723 address + wrote + i, readback[i], chunk[i]);
724 retval = ERROR_FAIL;
725 goto done;
730 if (duration_measure(&bench) == ERROR_OK)
732 command_print(CMD_CTX, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32
733 " in %fs (%0.3f kb/s)", wrote, address,
734 duration_elapsed(&bench), duration_kbps(&bench, wrote));
737 done:
738 free(readback);
739 free(chunk);
741 return retval;
744 COMMAND_HANDLER(handle_flash_write_bank_command)
746 uint32_t offset;
747 uint8_t *buffer;
748 struct fileio fileio;
750 if (CMD_ARGC != 3)
751 return ERROR_COMMAND_SYNTAX_ERROR;
753 struct duration bench;
754 duration_start(&bench);
756 struct flash_bank *p;
757 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
758 if (ERROR_OK != retval)
759 return retval;
761 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
763 if (fileio_open(&fileio, CMD_ARGV[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
765 return ERROR_OK;
768 buffer = malloc(fileio.size);
769 size_t buf_cnt;
770 if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
772 free(buffer);
773 fileio_close(&fileio);
774 return ERROR_OK;
777 retval = flash_driver_write(p, buffer, offset, buf_cnt);
779 free(buffer);
780 buffer = NULL;
782 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
784 command_print(CMD_CTX, "wrote %zu byte from file %s to flash bank %u"
785 " at offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
786 fileio.size, CMD_ARGV[1], p->bank_number, offset,
787 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
790 fileio_close(&fileio);
792 return retval;
795 void flash_set_dirty(void)
797 struct flash_bank *c;
798 int i;
800 /* set all flash to require erasing */
801 for (c = flash_banks; c; c = c->next)
803 for (i = 0; i < c->num_sectors; i++)
805 c->sectors[i].is_erased = 0;
810 /* lookup flash bank by address */
811 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
813 struct flash_bank *c;
815 /* cycle through bank list */
816 for (c = flash_banks; c; c = c->next)
818 int retval;
819 retval = c->driver->auto_probe(c);
821 if (retval != ERROR_OK)
823 LOG_ERROR("auto_probe failed %d\n", retval);
824 return NULL;
826 /* check whether address belongs to this flash bank */
827 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
828 return c;
830 LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
831 return NULL;
834 /* erase given flash region, selects proper bank according to target and address */
835 static int flash_iterate_address_range(struct target *target, uint32_t addr, uint32_t length,
836 int (*callback)(struct flash_bank *bank, int first, int last))
838 struct flash_bank *c;
839 int first = -1;
840 int last = -1;
841 int i;
843 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
844 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
846 if (c->size == 0 || c->num_sectors == 0)
848 LOG_ERROR("Bank is invalid");
849 return ERROR_FLASH_BANK_INVALID;
852 if (length == 0)
854 /* special case, erase whole bank when length is zero */
855 if (addr != c->base)
856 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
858 return callback(c, 0, c->num_sectors - 1);
861 /* check whether it fits */
862 if (addr + length - 1 > c->base + c->size - 1)
863 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
865 addr -= c->base;
867 for (i = 0; i < c->num_sectors; i++)
869 /* check whether sector overlaps with the given range and is not yet erased */
870 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
871 /* if first is not set yet then this is the first sector */
872 if (first == -1)
873 first = i;
874 last = i; /* and it is the last one so far in any case */
878 if (first == -1 || last == -1)
879 return ERROR_OK;
881 return callback(c, first, last);
886 int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
888 return flash_iterate_address_range(target, addr, length, &flash_driver_erase);
891 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
893 return flash_driver_protect(bank, 0, first, last);
896 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
898 return flash_iterate_address_range(target, addr, length, &flash_driver_unprotect);
902 /* write (optional verify) an image to flash memory of the given target */
903 static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock)
905 int retval = ERROR_OK;
907 int section;
908 uint32_t section_offset;
909 struct flash_bank *c;
910 int *padding;
912 section = 0;
913 section_offset = 0;
915 if (written)
916 *written = 0;
918 if (erase)
920 /* assume all sectors need erasing - stops any problems
921 * when flash_write is called multiple times */
923 flash_set_dirty();
926 /* allocate padding array */
927 padding = malloc(image->num_sections * sizeof(padding));
929 /* loop until we reach end of the image */
930 while (section < image->num_sections)
932 uint32_t buffer_size;
933 uint8_t *buffer;
934 int section_first;
935 int section_last;
936 uint32_t run_address = image->sections[section].base_address + section_offset;
937 uint32_t run_size = image->sections[section].size - section_offset;
938 int pad_bytes = 0;
940 if (image->sections[section].size == 0)
942 LOG_WARNING("empty section %d", section);
943 section++;
944 section_offset = 0;
945 continue;
948 /* find the corresponding flash bank */
949 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
951 section++; /* and skip it */
952 section_offset = 0;
953 continue;
956 /* collect consecutive sections which fall into the same bank */
957 section_first = section;
958 section_last = section;
959 padding[section] = 0;
960 while ((run_address + run_size - 1 < c->base + c->size - 1)
961 && (section_last + 1 < image->num_sections))
963 if (image->sections[section_last + 1].base_address < (run_address + run_size))
965 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
966 break;
968 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
969 * attempt to rebuild a consecutive buffer for the flash loader */
970 pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
971 if ((run_address + run_size + pad_bytes) > (c->base + c->size))
972 break;
973 padding[section_last] = pad_bytes;
974 run_size += image->sections[++section_last].size;
975 run_size += pad_bytes;
976 padding[section_last] = 0;
978 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
981 /* fit the run into bank constraints */
982 if (run_address + run_size - 1 > c->base + c->size - 1)
984 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
985 (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
986 run_size = c->base + c->size - run_address;
989 /* allocate buffer */
990 buffer = malloc(run_size);
991 buffer_size = 0;
993 /* read sections to the buffer */
994 while (buffer_size < run_size)
996 size_t size_read;
998 size_read = run_size - buffer_size;
999 if (size_read > image->sections[section].size - section_offset)
1000 size_read = image->sections[section].size - section_offset;
1002 if ((retval = image_read_section(image, section, section_offset,
1003 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
1005 free(buffer);
1006 free(padding);
1007 return retval;
1010 /* see if we need to pad the section */
1011 while (padding[section]--)
1012 (buffer + buffer_size)[size_read++] = 0xff;
1014 buffer_size += size_read;
1015 section_offset += size_read;
1017 if (section_offset >= image->sections[section].size)
1019 section++;
1020 section_offset = 0;
1024 retval = ERROR_OK;
1026 if (unlock)
1028 retval = flash_unlock_address_range(target, run_address, run_size);
1030 if (retval == ERROR_OK)
1032 if (erase)
1034 /* calculate and erase sectors */
1035 retval = flash_erase_address_range(target, run_address, run_size);
1039 if (retval == ERROR_OK)
1041 /* write flash sectors */
1042 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
1045 free(buffer);
1047 if (retval != ERROR_OK)
1049 free(padding);
1050 return retval; /* abort operation */
1053 if (written != NULL)
1054 *written += run_size; /* add run size to total written counter */
1057 free(padding);
1059 return retval;
1062 int flash_write(struct target *target, struct image *image, uint32_t *written, int erase)
1064 return flash_write_unlock(target, image, written, erase, false);
1067 int default_flash_mem_blank_check(struct flash_bank *bank)
1069 struct target *target = bank->target;
1070 const int buffer_size = 1024;
1071 int i;
1072 uint32_t nBytes;
1073 int retval = ERROR_OK;
1075 if (bank->target->state != TARGET_HALTED)
1077 LOG_ERROR("Target not halted");
1078 return ERROR_TARGET_NOT_HALTED;
1081 uint8_t *buffer = malloc(buffer_size);
1083 for (i = 0; i < bank->num_sectors; i++)
1085 uint32_t j;
1086 bank->sectors[i].is_erased = 1;
1088 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
1090 uint32_t chunk;
1091 chunk = buffer_size;
1092 if (chunk > (j - bank->sectors[i].size))
1094 chunk = (j - bank->sectors[i].size);
1097 retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
1098 if (retval != ERROR_OK)
1100 goto done;
1103 for (nBytes = 0; nBytes < chunk; nBytes++)
1105 if (buffer[nBytes] != 0xFF)
1107 bank->sectors[i].is_erased = 0;
1108 break;
1114 done:
1115 free(buffer);
1117 return retval;
1120 int default_flash_blank_check(struct flash_bank *bank)
1122 struct target *target = bank->target;
1123 int i;
1124 int retval;
1125 int fast_check = 0;
1126 uint32_t blank;
1128 if (bank->target->state != TARGET_HALTED)
1130 LOG_ERROR("Target not halted");
1131 return ERROR_TARGET_NOT_HALTED;
1134 for (i = 0; i < bank->num_sectors; i++)
1136 uint32_t address = bank->base + bank->sectors[i].offset;
1137 uint32_t size = bank->sectors[i].size;
1139 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
1141 fast_check = 0;
1142 break;
1144 if (blank == 0xFF)
1145 bank->sectors[i].is_erased = 1;
1146 else
1147 bank->sectors[i].is_erased = 0;
1148 fast_check = 1;
1151 if (!fast_check)
1153 LOG_USER("Running slow fallback erase check - add working memory");
1154 return default_flash_mem_blank_check(bank);
1157 return ERROR_OK;
1160 static const struct command_registration flash_exec_command_handlers[] = {
1162 .name = "probe",
1163 .handler = &handle_flash_probe_command,
1164 .mode = COMMAND_EXEC,
1165 .usage = "<bank>",
1166 .help = "identify flash bank",
1169 .name = "info",
1170 .handler = &handle_flash_info_command,
1171 .mode = COMMAND_EXEC,
1172 .usage = "<bank>",
1173 .help = "print bank information",
1176 .name = "erase_check",
1177 .handler = &handle_flash_erase_check_command,
1178 .mode = COMMAND_EXEC,
1179 .usage = "<bank>",
1180 .help = "check erase state of sectors",
1183 .name = "protect_check",
1184 .handler = &handle_flash_protect_check_command,
1185 .mode = COMMAND_EXEC,
1186 .usage = "<bank>",
1187 .help = "check protection state of sectors",
1190 .name = "erase_sector",
1191 .handler = &handle_flash_erase_command,
1192 .mode = COMMAND_EXEC,
1193 .usage = "<bank> <first> <last>",
1194 .help = "erase sectors",
1197 .name = "erase_address",
1198 .handler = &handle_flash_erase_address_command,
1199 .mode = COMMAND_EXEC,
1200 .usage = "<address> <length>",
1201 .help = "erase address range",
1205 .name = "fillw",
1206 .handler = &handle_flash_fill_command,
1207 .mode = COMMAND_EXEC,
1208 .usage = "<bank> <address> <word_pattern> <count>",
1209 .help = "fill with pattern (no autoerase)",
1212 .name = "fillh",
1213 .handler = &handle_flash_fill_command,
1214 .mode = COMMAND_EXEC,
1215 .usage = "<bank> <address> <halfword_pattern> <count>",
1216 .help = "fill with pattern",
1219 .name = "fillb",
1220 .handler = &handle_flash_fill_command,
1221 .mode = COMMAND_EXEC,
1222 .usage = "<bank> <address> <byte_pattern> <count>",
1223 .help = "fill with pattern",
1227 .name = "write_bank",
1228 .handler = &handle_flash_write_bank_command,
1229 .mode = COMMAND_EXEC,
1230 .usage = "<bank> <file> <offset>",
1231 .help = "write binary data",
1234 .name = "write_image",
1235 .handler = &handle_flash_write_image_command,
1236 .mode = COMMAND_EXEC,
1237 .usage = "<bank> [erase] [unlock] <file> [offset] [type]",
1238 .help = "write an image to flash"
1241 .name = "protect",
1242 .handler = &handle_flash_protect_command,
1243 .mode = COMMAND_EXEC,
1244 .usage = "<bank> <first> <last> <on | off>",
1245 .help = "set protection of sectors",
1247 COMMAND_REGISTRATION_DONE
1250 int flash_init_drivers(struct command_context *cmd_ctx)
1252 if (!flash_banks)
1253 return ERROR_OK;
1255 struct command *parent = command_find_in_context(cmd_ctx, "flash");
1256 return register_commands(cmd_ctx, parent, flash_exec_command_handlers);