+ if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
+ return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
+
+ if (c->size == 0 || c->num_sectors == 0)
+ return ERROR_FLASH_BANK_INVALID;
+
+ if (length == 0)
+ {
+ /* special case, erase whole bank when length is zero */
+ if (addr != c->base)
+ return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
+
+ return c->driver->erase(c, 0, c->num_sectors - 1);
+ }
+
+ /* check whether it fits */
+ if (addr + length > c->base + c->size)
+ return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
+
+ addr -= c->base;
+
+ for (i = 0; i < c->num_sectors; i++)
+ {
+ /* check whether sector overlaps with the given range and is not yet erased */
+ if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
+ /* if first is not set yet then this is the first sector */
+ if (first == -1)
+ first = i;
+ last = i; /* and it is the last one so far in any case */
+ }
+ }
+
+ if( first == -1 || last == -1 )
+ return ERROR_OK;
+
+ return c->driver->erase(c, first, last);
+}
+
+/* write (optional verify) an image to flash memory of the given target */
+int flash_write(target_t *target, image_t *image, u32 *written, char **error_str, int *failed, int erase)
+{
+ int retval;
+ int i;
+
+ int section;
+ u32 section_offset;
+ flash_bank_t *c;
+
+ section = 0;
+ section_offset = 0;
+
+ if (written)
+ *written = 0;
+
+ if (failed != NULL)
+ for (i = 0; i < image->num_sections; i++)
+ failed[i] = 0;
+
+ if (erase)
+ {
+ /* assume all sectors need erasing - stops any problems
+ * when flash_write is called multiple times */
+
+ flash_set_dirty();
+ }
+
+ /* loop until we reach end of the image */
+ while (section < image->num_sections)
+ {
+ u32 buffer_size;
+ u8 *buffer;
+ int section_first;
+ int section_last;
+ u32 run_address = image->sections[section].base_address + section_offset;
+ u32 run_size = image->sections[section].size - section_offset;
+
+ if (image->sections[section].size == 0)
+ {
+ WARNING("empty section %d", section);
+ section++;
+ section_offset = 0;
+ continue;
+ }
+
+ /* find the corresponding flash bank */
+ if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
+ {
+ if (failed == NULL)
+ {
+ if (error_str == NULL)
+ return ERROR_FLASH_DST_OUT_OF_BANK; /* abort operation */
+ *error_str = malloc(FLASH_MAX_ERROR_STR);
+ snprintf(*error_str, FLASH_MAX_ERROR_STR, "no flash mapped at requested address");
+ return ERROR_FLASH_DST_OUT_OF_BANK; /* abort operation */
+ }
+ failed[section] = ERROR_FLASH_DST_OUT_OF_BANK; /* mark the section as failed */
+ section++; /* and skip it */
+ section_offset = 0;
+ continue;
+ }
+
+ /* collect consecutive sections which fall into the same bank */
+ section_first = section;
+ section_last = section;
+ while ((run_address + run_size < c->base + c->size)
+ && (section_last + 1 < image->num_sections))
+ {
+ if (image->sections[section_last + 1].base_address < (run_address + run_size))
+ {
+ DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
+ break;
+ }
+ if (image->sections[section_last + 1].base_address != (run_address + run_size))
+ break;
+ run_size += image->sections[++section_last].size;
+ }
+
+ /* fit the run into bank constraints */
+ if (run_address + run_size > c->base + c->size)
+ run_size = c->base + c->size - run_address;
+
+ /* allocate buffer */
+ buffer = malloc(run_size);
+ buffer_size = 0;
+
+ /* read sections to the buffer */
+ while (buffer_size < run_size)
+ {
+ u32 size_read;
+
+ if (buffer_size - run_size <= image->sections[section].size - section_offset)
+ size_read = buffer_size - run_size;
+ else
+ size_read = image->sections[section].size - section_offset;
+
+ if ((retval = image_read_section(image, section, section_offset,
+ size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
+ {
+ free(buffer);
+
+ if (error_str == NULL)
+ return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
+
+ *error_str = malloc(FLASH_MAX_ERROR_STR);
+
+ /* if image_read_section returned an error there's an error string we can pass on */
+ if (retval != ERROR_OK)
+ snprintf(*error_str, FLASH_MAX_ERROR_STR, "error reading from image: %s", image->error_str);
+ else
+ snprintf(*error_str, FLASH_MAX_ERROR_STR, "error reading from image");
+
+ return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
+ }
+
+ buffer_size += size_read;
+ section_offset += size_read;
+
+ if (section_offset >= image->sections[section].size)
+ {
+ section++;
+ section_offset = 0;
+ }
+ }
+
+ retval = ERROR_OK;
+
+ if (erase)
+ {
+ /* calculate and erase sectors */
+ retval = flash_erase_address_range( target, run_address, run_size );
+ }
+
+ if (retval == ERROR_OK)
+ {
+ /* write flash sectors */
+ retval = c->driver->write(c, buffer, run_address - c->base, run_size);
+ }
+
+ free(buffer);
+
+ if (retval != ERROR_OK)
+ {
+ if (error_str == NULL)
+ return retval; /* abort operation */
+
+ *error_str = malloc(FLASH_MAX_ERROR_STR);
+ switch (retval)
+ {
+ case ERROR_TARGET_NOT_HALTED:
+ snprintf(*error_str, FLASH_MAX_ERROR_STR, "can't flash image while target is running");
+ break;
+ case ERROR_INVALID_ARGUMENTS:
+ snprintf(*error_str, FLASH_MAX_ERROR_STR, "flash driver can't fulfill request");
+ break;
+ case ERROR_FLASH_OPERATION_FAILED:
+ snprintf(*error_str, FLASH_MAX_ERROR_STR, "flash program error");
+ break;
+ case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
+ snprintf(*error_str, FLASH_MAX_ERROR_STR, "offset breaks required alignment");
+ break;
+ case ERROR_FLASH_DST_OUT_OF_BANK:
+ snprintf(*error_str, FLASH_MAX_ERROR_STR, "no flash mapped at requested address");
+ break;
+ case ERROR_FLASH_SECTOR_NOT_ERASED:
+ snprintf(*error_str, FLASH_MAX_ERROR_STR, "destination sector(s) not erased");
+ break;
+ default:
+ snprintf(*error_str, FLASH_MAX_ERROR_STR, "unknown error: %i", retval);
+ }
+
+ return retval; /* abort operation */
+ }
+
+ if (written != NULL)
+ *written += run_size; /* add run size to total written counter */
+ }
+
+ return ERROR_OK;
+}
+
+int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+ if (argc != 1)
+ {
+ command_print(cmd_ctx, "usage: flash auto_erase <on|off>");
+ return ERROR_OK;
+ }
+
+ if (strcmp(args[0], "on") == 0)
+ auto_erase = 1;
+ else if (strcmp(args[0], "off") == 0)
+ auto_erase = 0;