- case ERROR_TARGET_NOT_HALTED:
- command_print(cmd_ctx, "can't work with this flash while target is running");
- break;
- case ERROR_INVALID_ARGUMENTS:
- command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
- break;
- case ERROR_FLASH_BANK_INVALID:
- command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
- break;
- case ERROR_FLASH_OPERATION_FAILED:
- command_print(cmd_ctx, "flash program error");
- break;
- case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
- command_print(cmd_ctx, "offset breaks required alignment");
- break;
- case ERROR_FLASH_DST_OUT_OF_BANK:
- command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
+ ERROR("auto_probe failed %d\n", retval);
+ return NULL;
+ }
+ /* check whether address belongs to this flash bank */
+ if ((addr >= c->base) && (addr < c->base + c->size) && target == c->target)
+ return c;
+ }
+ ERROR("No flash at address 0x%08x\n", addr);
+ return NULL;
+}
+
+/* erase given flash region, selects proper bank according to target and address */
+int flash_erase_address_range(target_t *target, u32 addr, u32 length)
+{
+ flash_bank_t *c;
+ int first = -1;
+ int last = -1;
+ int i;
+
+ 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 flash_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 flash_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, int erase)
+{
+ int retval;
+
+ int section;
+ u32 section_offset;
+ flash_bank_t *c;
+
+ section = 0;
+ section_offset = 0;
+
+ if (written)
+ *written = 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)
+ {
+ 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);