1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
32 #include "time_support.h"
34 static int flash_write_unlock(target_t *target, image_t *image, uint32_t *written, int erase, bool unlock);
38 extern flash_driver_t lpc2000_flash;
39 extern flash_driver_t lpc288x_flash;
40 extern flash_driver_t lpc2900_flash;
41 extern flash_driver_t cfi_flash;
42 extern flash_driver_t at91sam3_flash;
43 extern flash_driver_t at91sam7_flash;
44 extern flash_driver_t str7x_flash;
45 extern flash_driver_t str9x_flash;
46 extern flash_driver_t aduc702x_flash;
47 extern flash_driver_t stellaris_flash;
48 extern flash_driver_t str9xpec_flash;
49 extern flash_driver_t stm32x_flash;
50 extern flash_driver_t tms470_flash;
51 extern flash_driver_t ecosflash_flash;
52 extern flash_driver_t ocl_flash;
53 extern flash_driver_t pic32mx_flash;
54 extern flash_driver_t avr_flash;
55 extern flash_driver_t faux_flash;
57 flash_driver_t *flash_drivers[] = {
79 flash_bank_t *flash_banks;
80 static command_t *flash_cmd;
82 /* wafer thin wrapper for invoking the flash driver */
83 static int flash_driver_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
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);
97 static int flash_driver_erase(struct flash_bank_s *bank, int first, int last)
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);
110 int flash_driver_protect(struct flash_bank_s *bank, int set, int first, int last)
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);
123 static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
128 Jim_WrongNumArgs(interp, 1, argv, "no arguments to flash_banks command");
132 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
133 for (p = flash_banks; p; p = p->next)
135 Jim_Obj *elem = Jim_NewListObj(interp, NULL, 0);
137 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "name", -1));
138 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, p->driver->name, -1));
139 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "base", -1));
140 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->base));
141 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "size", -1));
142 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->size));
143 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "bus_width", -1));
144 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->bus_width));
145 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "chip_width", -1));
146 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->chip_width));
148 Jim_ListAppendElement(interp, list, elem);
151 Jim_SetResult(interp, list);
156 flash_bank_t *get_flash_bank_by_num_noprobe(int num)
161 for (p = flash_banks; p; p = p->next)
168 LOG_ERROR("flash bank %d does not exist", num);
172 int flash_get_bank_count(void)
176 for (p = flash_banks; p; p = p->next)
183 flash_bank_t *get_flash_bank_by_num(int num)
185 flash_bank_t *p = get_flash_bank_by_num_noprobe(num);
191 retval = p->driver->auto_probe(p);
193 if (retval != ERROR_OK)
195 LOG_ERROR("auto_probe failed %d\n", retval);
201 int flash_command_get_bank_by_num(
202 struct command_context_s *cmd_ctx, char *str, flash_bank_t **bank)
205 COMMAND_PARSE_NUMBER(uint, str, bank_num);
207 *bank = get_flash_bank_by_num(bank_num);
210 command_print(cmd_ctx,
211 "flash bank '#%u' not found", bank_num);
212 return ERROR_INVALID_ARGUMENTS;
218 static int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
227 return ERROR_COMMAND_SYNTAX_ERROR;
230 if ((target = get_target(args[5])) == NULL)
232 LOG_ERROR("target '%s' not defined", args[5]);
236 for (i = 0; flash_drivers[i]; i++)
238 if (strcmp(args[0], flash_drivers[i]->name) != 0)
243 /* register flash specific commands */
244 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
246 LOG_ERROR("couldn't register '%s' commands", args[0]);
250 c = malloc(sizeof(flash_bank_t));
252 c->driver = flash_drivers[i];
253 c->driver_priv = NULL;
254 COMMAND_PARSE_NUMBER(u32, args[1], c->base);
255 COMMAND_PARSE_NUMBER(u32, args[2], c->size);
256 COMMAND_PARSE_NUMBER(int, args[3], c->chip_width);
257 COMMAND_PARSE_NUMBER(int, args[4], c->bus_width);
262 if ((retval = flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c)) != ERROR_OK)
264 LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32 , args[0], c->base);
269 /* put flash bank in linked list */
273 /* find last flash bank */
274 for (p = flash_banks; p && p->next; p = p->next) bank_num++;
277 c->bank_number = bank_num + 1;
288 /* no matching flash driver found */
291 LOG_ERROR("flash driver '%s' not found", args[0]);
298 static int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
306 return ERROR_COMMAND_SYNTAX_ERROR;
309 COMMAND_PARSE_NUMBER(uint, args[0], bank_nr);
311 for (p = flash_banks; p; p = p->next, i++)
318 /* attempt auto probe */
319 if ((retval = p->driver->auto_probe(p)) != ERROR_OK)
322 command_print(cmd_ctx,
323 "#%" PRIi32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", buswidth %i, chipwidth %i",
330 for (j = 0; j < p->num_sectors; j++)
334 if (p->sectors[j].is_protected == 0)
335 protect_state = "not protected";
336 else if (p->sectors[j].is_protected == 1)
337 protect_state = "protected";
339 protect_state = "protection state unknown";
341 command_print(cmd_ctx,
342 "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
344 p->sectors[j].offset,
346 p->sectors[j].size >> 10,
350 *buf = '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
351 retval = p->driver->info(p, buf, sizeof(buf));
352 command_print(cmd_ctx, "%s", buf);
353 if (retval != ERROR_OK)
354 LOG_ERROR("error retrieving flash info (%d)", retval);
360 static int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
366 return ERROR_COMMAND_SYNTAX_ERROR;
370 COMMAND_PARSE_NUMBER(uint, args[0], bank_nr);
371 flash_bank_t *p = get_flash_bank_by_num_noprobe(bank_nr);
374 if ((retval = p->driver->probe(p)) == ERROR_OK)
376 command_print(cmd_ctx, "flash '%s' found at 0x%8.8" PRIx32, p->driver->name, p->base);
378 else if (retval == ERROR_FLASH_BANK_INVALID)
380 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32,
385 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32,
391 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
397 static int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
401 return ERROR_COMMAND_SYNTAX_ERROR;
405 int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &p);
406 if (ERROR_OK != retval)
410 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
412 command_print(cmd_ctx, "successfully checked erase state");
416 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
420 for (j = 0; j < p->num_sectors; j++)
424 if (p->sectors[j].is_erased == 0)
425 erase_state = "not erased";
426 else if (p->sectors[j].is_erased == 1)
427 erase_state = "erased";
429 erase_state = "erase state unknown";
431 command_print(cmd_ctx,
432 "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
434 p->sectors[j].offset,
436 p->sectors[j].size >> 10,
443 static int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
450 target_t *target = get_current_target(cmd_ctx);
453 return ERROR_COMMAND_SYNTAX_ERROR;
455 COMMAND_PARSE_NUMBER(int, args[0], address);
456 COMMAND_PARSE_NUMBER(int, args[1], length);
459 command_print(cmd_ctx, "Length must be >0");
460 return ERROR_COMMAND_SYNTAX_ERROR;
463 p = get_flash_bank_by_addr(target, address);
469 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
472 struct duration bench;
473 duration_start(&bench);
475 retval = flash_erase_address_range(target, address, length);
477 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
479 command_print(cmd_ctx, "erased address 0x%8.8x (length %i)"
480 " in %fs (%0.3f kb/s)", address, length,
481 duration_elapsed(&bench), duration_kbps(&bench, length));
487 static int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
490 return ERROR_COMMAND_SYNTAX_ERROR;
493 int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &p);
494 if (ERROR_OK != retval)
497 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
499 command_print(cmd_ctx, "successfully checked protect state");
501 else if (retval == ERROR_FLASH_OPERATION_FAILED)
503 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32, args[0], p->base);
507 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32, args[0], p->base);
513 static int flash_check_sector_parameters(struct command_context_s *cmd_ctx,
514 uint32_t first, uint32_t last, uint32_t num_sectors)
516 if (!(first <= last)) {
517 command_print(cmd_ctx, "ERROR: "
518 "first sector must be <= last sector");
522 if (!(last <= (num_sectors - 1))) {
523 command_print(cmd_ctx, "ERROR: last sector must be <= %d",
524 (int) num_sectors - 1);
531 static int handle_flash_erase_command(struct command_context_s *cmd_ctx,
532 char *cmd, char **args, int argc)
535 return ERROR_COMMAND_SYNTAX_ERROR;
541 COMMAND_PARSE_NUMBER(u32, args[0], bank_nr);
542 flash_bank_t *p = get_flash_bank_by_num(bank_nr);
546 COMMAND_PARSE_NUMBER(u32, args[1], first);
547 if (strcmp(args[2], "last") == 0)
548 last = p->num_sectors - 1;
550 COMMAND_PARSE_NUMBER(u32, args[2], last);
553 if ((retval = flash_check_sector_parameters(cmd_ctx,
554 first, last, p->num_sectors)) != ERROR_OK)
557 struct duration bench;
558 duration_start(&bench);
560 retval = flash_driver_erase(p, first, last);
562 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
564 command_print(cmd_ctx, "erased sectors %" PRIu32 " "
565 "through %" PRIu32" on flash bank %" PRIu32 " "
566 "in %fs", first, last, bank_nr, duration_elapsed(&bench));
572 static int handle_flash_protect_command(struct command_context_s *cmd_ctx,
573 char *cmd, char **args, int argc)
576 return ERROR_COMMAND_SYNTAX_ERROR;
583 COMMAND_PARSE_NUMBER(u32, args[0], bank_nr);
584 flash_bank_t *p = get_flash_bank_by_num(bank_nr);
588 COMMAND_PARSE_NUMBER(u32, args[1], first);
589 if (strcmp(args[2], "last") == 0)
590 last = p->num_sectors - 1;
592 COMMAND_PARSE_NUMBER(u32, args[2], last);
594 if (strcmp(args[3], "on") == 0)
596 else if (strcmp(args[3], "off") == 0)
599 return ERROR_COMMAND_SYNTAX_ERROR;
602 if ((retval = flash_check_sector_parameters(cmd_ctx,
603 first, last, p->num_sectors)) != ERROR_OK)
606 retval = flash_driver_protect(p, set, first, last);
607 if (retval == ERROR_OK) {
608 command_print(cmd_ctx, "%s protection for sectors %i "
609 "through %i on flash bank %i",
610 (set) ? "set" : "cleared", (int) first,
611 (int) last, (int) bank_nr);
617 static int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
619 target_t *target = get_current_target(cmd_ctx);
628 return ERROR_COMMAND_SYNTAX_ERROR;
631 /* flash auto-erase is disabled by default*/
633 bool auto_unlock = false;
637 if (strcmp(args[0], "erase") == 0)
642 command_print(cmd_ctx, "auto erase enabled");
643 } else if (strcmp(args[0], "unlock") == 0)
648 command_print(cmd_ctx, "auto unlock enabled");
657 return ERROR_COMMAND_SYNTAX_ERROR;
662 LOG_ERROR("no target selected");
666 struct duration bench;
667 duration_start(&bench);
671 image.base_address_set = 1;
672 COMMAND_PARSE_NUMBER(int, args[1], image.base_address);
676 image.base_address_set = 0;
677 image.base_address = 0x0;
680 image.start_address_set = 0;
682 retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL);
683 if (retval != ERROR_OK)
688 retval = flash_write_unlock(target, &image, &written, auto_erase, auto_unlock);
689 if (retval != ERROR_OK)
695 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
697 command_print(cmd_ctx, "wrote %" PRIu32 " byte from file %s "
698 "in %fs (%0.3f kb/s)", written, args[0],
699 duration_elapsed(&bench), duration_kbps(&bench, written));
707 static int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
714 uint8_t readback[1024];
716 uint32_t cur_size = 0;
717 uint32_t chunk_count;
718 target_t *target = get_current_target(cmd_ctx);
723 return ERROR_COMMAND_SYNTAX_ERROR;
725 COMMAND_PARSE_NUMBER(u32, args[0], address);
726 COMMAND_PARSE_NUMBER(u32, args[1], pattern);
727 COMMAND_PARSE_NUMBER(u32, args[2], count);
744 return ERROR_COMMAND_SYNTAX_ERROR;
747 chunk_count = MIN(count, (1024 / wordsize));
751 for (i = 0; i < chunk_count; i++)
753 target_buffer_set_u32(target, chunk + i * wordsize, pattern);
757 for (i = 0; i < chunk_count; i++)
759 target_buffer_set_u16(target, chunk + i * wordsize, pattern);
763 memset(chunk, pattern, chunk_count);
766 LOG_ERROR("BUG: can't happen");
770 struct duration bench;
771 duration_start(&bench);
773 for (wrote = 0; wrote < (count*wordsize); wrote += cur_size)
775 cur_size = MIN((count*wordsize - wrote), sizeof(chunk));
777 bank = get_flash_bank_by_addr(target, address);
782 err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
786 err = target_read_buffer(target, address + wrote, cur_size, readback);
791 for (i = 0; i < cur_size; i++)
793 if (readback[i]!=chunk[i])
795 LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
796 address + wrote + i, readback[i], chunk[i]);
802 if (duration_measure(&bench) == ERROR_OK)
804 command_print(cmd_ctx, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32
805 " in %fs (%0.3f kb/s)", wrote, address,
806 duration_elapsed(&bench), duration_kbps(&bench, wrote));
811 static int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
819 return ERROR_COMMAND_SYNTAX_ERROR;
821 struct duration bench;
822 duration_start(&bench);
825 int retval = flash_command_get_bank_by_num(cmd_ctx, args[0], &p);
826 if (ERROR_OK != retval)
829 COMMAND_PARSE_NUMBER(u32, args[2], offset);
831 if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
836 buffer = malloc(fileio.size);
837 if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
840 fileio_close(&fileio);
844 retval = flash_driver_write(p, buffer, offset, buf_cnt);
849 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
851 command_print(cmd_ctx, "wrote %lld byte from file %s to flash bank %u"
852 " at offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
853 fileio.size, args[1], p->bank_number, offset,
854 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
857 fileio_close(&fileio);
862 void flash_set_dirty(void)
867 /* set all flash to require erasing */
868 for (c = flash_banks; c; c = c->next)
870 for (i = 0; i < c->num_sectors; i++)
872 c->sectors[i].is_erased = 0;
877 /* lookup flash bank by address */
878 flash_bank_t *get_flash_bank_by_addr(target_t *target, uint32_t addr)
882 /* cycle through bank list */
883 for (c = flash_banks; c; c = c->next)
886 retval = c->driver->auto_probe(c);
888 if (retval != ERROR_OK)
890 LOG_ERROR("auto_probe failed %d\n", retval);
893 /* check whether address belongs to this flash bank */
894 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
897 LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
901 /* erase given flash region, selects proper bank according to target and address */
902 static int flash_iterate_address_range(target_t *target, uint32_t addr, uint32_t length,
903 int (*callback)(struct flash_bank_s *bank, int first, int last))
910 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
911 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
913 if (c->size == 0 || c->num_sectors == 0)
915 LOG_ERROR("Bank is invalid");
916 return ERROR_FLASH_BANK_INVALID;
921 /* special case, erase whole bank when length is zero */
923 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
925 return callback(c, 0, c->num_sectors - 1);
928 /* check whether it fits */
929 if (addr + length - 1 > c->base + c->size - 1)
930 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
934 for (i = 0; i < c->num_sectors; i++)
936 /* check whether sector overlaps with the given range and is not yet erased */
937 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
938 /* if first is not set yet then this is the first sector */
941 last = i; /* and it is the last one so far in any case */
945 if (first == -1 || last == -1)
948 return callback(c, first, last);
953 int flash_erase_address_range(target_t *target, uint32_t addr, uint32_t length)
955 return flash_iterate_address_range(target, addr, length, &flash_driver_erase);
958 static int flash_driver_unprotect(struct flash_bank_s *bank, int first, int last)
960 return flash_driver_protect(bank, 0, first, last);
963 static int flash_unlock_address_range(target_t *target, uint32_t addr, uint32_t length)
965 return flash_iterate_address_range(target, addr, length, &flash_driver_unprotect);
969 /* write (optional verify) an image to flash memory of the given target */
970 static int flash_write_unlock(target_t *target, image_t *image, uint32_t *written, int erase, bool unlock)
972 int retval = ERROR_OK;
975 uint32_t section_offset;
987 /* assume all sectors need erasing - stops any problems
988 * when flash_write is called multiple times */
993 /* allocate padding array */
994 padding = malloc(image->num_sections * sizeof(padding));
996 /* loop until we reach end of the image */
997 while (section < image->num_sections)
999 uint32_t buffer_size;
1003 uint32_t run_address = image->sections[section].base_address + section_offset;
1004 uint32_t run_size = image->sections[section].size - section_offset;
1007 if (image->sections[section].size == 0)
1009 LOG_WARNING("empty section %d", section);
1015 /* find the corresponding flash bank */
1016 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
1018 section++; /* and skip it */
1023 /* collect consecutive sections which fall into the same bank */
1024 section_first = section;
1025 section_last = section;
1026 padding[section] = 0;
1027 while ((run_address + run_size - 1 < c->base + c->size - 1)
1028 && (section_last + 1 < image->num_sections))
1030 if (image->sections[section_last + 1].base_address < (run_address + run_size))
1032 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
1035 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
1036 * attempt to rebuild a consecutive buffer for the flash loader */
1037 pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
1038 if ((run_address + run_size + pad_bytes) > (c->base + c->size))
1040 padding[section_last] = pad_bytes;
1041 run_size += image->sections[++section_last].size;
1042 run_size += pad_bytes;
1043 padding[section_last] = 0;
1045 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
1048 /* fit the run into bank constraints */
1049 if (run_address + run_size - 1 > c->base + c->size - 1)
1051 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
1052 (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
1053 run_size = c->base + c->size - run_address;
1056 /* allocate buffer */
1057 buffer = malloc(run_size);
1060 /* read sections to the buffer */
1061 while (buffer_size < run_size)
1065 size_read = run_size - buffer_size;
1066 if (size_read > image->sections[section].size - section_offset)
1067 size_read = image->sections[section].size - section_offset;
1069 if ((retval = image_read_section(image, section, section_offset,
1070 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
1077 /* see if we need to pad the section */
1078 while (padding[section]--)
1079 (buffer + buffer_size)[size_read++] = 0xff;
1081 buffer_size += size_read;
1082 section_offset += size_read;
1084 if (section_offset >= image->sections[section].size)
1095 retval = flash_unlock_address_range(target, run_address, run_size);
1097 if (retval == ERROR_OK)
1101 /* calculate and erase sectors */
1102 retval = flash_erase_address_range(target, run_address, run_size);
1106 if (retval == ERROR_OK)
1108 /* write flash sectors */
1109 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
1114 if (retval != ERROR_OK)
1117 return retval; /* abort operation */
1120 if (written != NULL)
1121 *written += run_size; /* add run size to total written counter */
1129 int flash_write(target_t *target, image_t *image, uint32_t *written, int erase)
1131 return flash_write_unlock(target, image, written, erase, false);
1134 int default_flash_mem_blank_check(struct flash_bank_s *bank)
1136 target_t *target = bank->target;
1137 uint8_t buffer[1024];
1138 int buffer_size = sizeof(buffer);
1142 if (bank->target->state != TARGET_HALTED)
1144 LOG_ERROR("Target not halted");
1145 return ERROR_TARGET_NOT_HALTED;
1148 for (i = 0; i < bank->num_sectors; i++)
1151 bank->sectors[i].is_erased = 1;
1153 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
1157 chunk = buffer_size;
1158 if (chunk > (j - bank->sectors[i].size))
1160 chunk = (j - bank->sectors[i].size);
1163 retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
1164 if (retval != ERROR_OK)
1167 for (nBytes = 0; nBytes < chunk; nBytes++)
1169 if (buffer[nBytes] != 0xFF)
1171 bank->sectors[i].is_erased = 0;
1181 int default_flash_blank_check(struct flash_bank_s *bank)
1183 target_t *target = bank->target;
1189 if (bank->target->state != TARGET_HALTED)
1191 LOG_ERROR("Target not halted");
1192 return ERROR_TARGET_NOT_HALTED;
1195 for (i = 0; i < bank->num_sectors; i++)
1197 uint32_t address = bank->base + bank->sectors[i].offset;
1198 uint32_t size = bank->sectors[i].size;
1200 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
1206 bank->sectors[i].is_erased = 1;
1208 bank->sectors[i].is_erased = 0;
1214 LOG_USER("Running slow fallback erase check - add working memory");
1215 return default_flash_mem_blank_check(bank);
1221 int flash_init_drivers(struct command_context_s *cmd_ctx)
1223 register_jim(cmd_ctx, "ocd_flash_banks",
1224 jim_flash_banks, "return information about the flash banks");
1229 register_command(cmd_ctx, flash_cmd, "info",
1230 handle_flash_info_command, COMMAND_EXEC,
1231 "print info about flash bank <num>");
1232 register_command(cmd_ctx, flash_cmd, "probe",
1233 handle_flash_probe_command, COMMAND_EXEC,
1234 "identify flash bank <num>");
1235 register_command(cmd_ctx, flash_cmd, "erase_check",
1236 handle_flash_erase_check_command, COMMAND_EXEC,
1237 "check erase state of sectors in flash bank <num>");
1238 register_command(cmd_ctx, flash_cmd, "protect_check",
1239 handle_flash_protect_check_command, COMMAND_EXEC,
1240 "check protection state of sectors in flash bank <num>");
1241 register_command(cmd_ctx, flash_cmd, "erase_sector",
1242 handle_flash_erase_command, COMMAND_EXEC,
1243 "erase sectors at <bank> <first> <last>");
1244 register_command(cmd_ctx, flash_cmd, "erase_address",
1245 handle_flash_erase_address_command, COMMAND_EXEC,
1246 "erase address range <address> <length>");
1248 register_command(cmd_ctx, flash_cmd, "fillw",
1249 handle_flash_fill_command, COMMAND_EXEC,
1250 "fill with pattern (no autoerase) <address> <word_pattern> <count>");
1251 register_command(cmd_ctx, flash_cmd, "fillh",
1252 handle_flash_fill_command, COMMAND_EXEC,
1253 "fill with pattern <address> <halfword_pattern> <count>");
1254 register_command(cmd_ctx, flash_cmd, "fillb",
1255 handle_flash_fill_command, COMMAND_EXEC,
1256 "fill with pattern <address> <byte_pattern> <count>");
1258 register_command(cmd_ctx, flash_cmd, "write_bank",
1259 handle_flash_write_bank_command, COMMAND_EXEC,
1260 "write binary data to <bank> <file> <offset>");
1261 register_command(cmd_ctx, flash_cmd, "write_image",
1262 handle_flash_write_image_command, COMMAND_EXEC,
1263 "write_image [erase] [unlock] <file> [offset] [type]");
1264 register_command(cmd_ctx, flash_cmd, "protect",
1265 handle_flash_protect_command, COMMAND_EXEC,
1266 "set protection of sectors at <bank> <first> <last> <on | off>");
1271 int flash_register_commands(struct command_context_s *cmd_ctx)
1273 flash_cmd = register_command(cmd_ctx, NULL, "flash",
1274 NULL, COMMAND_ANY, NULL);
1276 register_command(cmd_ctx, flash_cmd, "bank",
1277 handle_flash_bank_command, COMMAND_CONFIG,
1278 "flash bank <driver> <base> <size> "
1279 "<chip_width> <bus_width> <target> [driver_options ...]");