aboutsummaryrefslogtreecommitdiff
path: root/src/flash
diff options
context:
space:
mode:
Diffstat (limited to 'src/flash')
-rw-r--r--src/flash/nor/at91sam3.c17
-rw-r--r--src/flash/nor/at91sam4.c17
-rw-r--r--src/flash/nor/at91sam4l.c41
-rw-r--r--src/flash/nor/at91sam7.c48
-rw-r--r--src/flash/nor/at91samd.c39
-rw-r--r--src/flash/nor/core.c235
-rw-r--r--src/flash/nor/core.h35
-rw-r--r--src/flash/nor/kinetis.c19
-rw-r--r--src/flash/nor/psoc6.c361
-rw-r--r--src/flash/nor/stm32f1x.c39
-rw-r--r--src/flash/nor/stm32f2x.c39
-rw-r--r--src/flash/nor/stm32h7x.c45
-rw-r--r--src/flash/nor/stm32l4x.c13
-rw-r--r--src/flash/nor/stm32lx.c4
-rw-r--r--src/flash/nor/tcl.c206
15 files changed, 645 insertions, 513 deletions
diff --git a/src/flash/nor/at91sam3.c b/src/flash/nor/at91sam3.c
index 1536378..d80b6fe 100644
--- a/src/flash/nor/at91sam3.c
+++ b/src/flash/nor/at91sam3.c
@@ -3117,6 +3117,22 @@ FLASH_BANK_COMMAND_HANDLER(sam3_flash_bank_command)
return ERROR_OK;
}
+/**
+ * Remove all chips from the internal list without distingushing which one
+ * is owned by this bank. This simplification works only for one shot
+ * deallocation like current flash_free_all_banks()
+ */
+void sam3_free_driver_priv(struct flash_bank *bank)
+{
+ struct sam3_chip *chip = all_sam3_chips;
+ while (chip) {
+ struct sam3_chip *next = chip->next;
+ free(chip);
+ chip = next;
+ }
+ all_sam3_chips = NULL;
+}
+
static int sam3_GetDetails(struct sam3_bank_private *pPrivate)
{
const struct sam3_chip_details *pDetails;
@@ -3771,4 +3787,5 @@ struct flash_driver at91sam3_flash = {
.auto_probe = sam3_auto_probe,
.erase_check = sam3_erase_check,
.protect_check = sam3_protect_check,
+ .free_driver_priv = sam3_free_driver_priv,
};
diff --git a/src/flash/nor/at91sam4.c b/src/flash/nor/at91sam4.c
index d101c9b..0475216 100644
--- a/src/flash/nor/at91sam4.c
+++ b/src/flash/nor/at91sam4.c
@@ -2514,6 +2514,22 @@ FLASH_BANK_COMMAND_HANDLER(sam4_flash_bank_command)
return ERROR_OK;
}
+/**
+ * Remove all chips from the internal list without distingushing which one
+ * is owned by this bank. This simplification works only for one shot
+ * deallocation like current flash_free_all_banks()
+ */
+static void sam4_free_driver_priv(struct flash_bank *bank)
+{
+ struct sam4_chip *chip = all_sam4_chips;
+ while (chip) {
+ struct sam4_chip *next = chip->next;
+ free(chip);
+ chip = next;
+ }
+ all_sam4_chips = NULL;
+}
+
static int sam4_GetDetails(struct sam4_bank_private *pPrivate)
{
const struct sam4_chip_details *pDetails;
@@ -3194,4 +3210,5 @@ struct flash_driver at91sam4_flash = {
.auto_probe = sam4_auto_probe,
.erase_check = default_flash_blank_check,
.protect_check = sam4_protect_check,
+ .free_driver_priv = sam4_free_driver_priv,
};
diff --git a/src/flash/nor/at91sam4l.c b/src/flash/nor/at91sam4l.c
index 0a605d5..794ccbb 100644
--- a/src/flash/nor/at91sam4l.c
+++ b/src/flash/nor/at91sam4l.c
@@ -129,10 +129,8 @@ struct sam4l_info {
bool probed;
struct target *target;
- struct sam4l_info *next;
};
-static struct sam4l_info *sam4l_chips;
static int sam4l_flash_wait_until_ready(struct target *target)
{
@@ -204,30 +202,6 @@ static int sam4l_flash_command(struct target *target, uint8_t cmd, int page)
FLASH_BANK_COMMAND_HANDLER(sam4l_flash_bank_command)
{
- struct sam4l_info *chip = sam4l_chips;
-
- while (chip) {
- if (chip->target == bank->target)
- break;
- chip = chip->next;
- }
-
- if (!chip) {
- /* Create a new chip */
- chip = calloc(1, sizeof(*chip));
- if (!chip)
- return ERROR_FAIL;
-
- chip->target = bank->target;
- chip->probed = false;
-
- bank->driver_priv = chip;
-
- /* Insert it into the chips list (at head) */
- chip->next = sam4l_chips;
- sam4l_chips = chip;
- }
-
if (bank->base != SAM4L_FLASH) {
LOG_ERROR("Address 0x%08" PRIx32 " invalid bank address (try 0x%08" PRIx32
"[at91sam4l series] )",
@@ -235,6 +209,18 @@ FLASH_BANK_COMMAND_HANDLER(sam4l_flash_bank_command)
return ERROR_FAIL;
}
+ struct sam4l_info *chip;
+ chip = calloc(1, sizeof(*chip));
+ if (!chip) {
+ LOG_ERROR("No memory for flash bank chip info");
+ return ERROR_FAIL;
+ }
+
+ chip->target = bank->target;
+ chip->probed = false;
+
+ bank->driver_priv = chip;
+
return ERROR_OK;
}
@@ -396,7 +382,7 @@ static int sam4l_protect_check(struct flash_bank *bank)
static int sam4l_protect(struct flash_bank *bank, int set, int first, int last)
{
- struct sam4l_info *chip = sam4l_chips;
+ struct sam4l_info *chip = (struct sam4l_info *)bank->driver_priv;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
@@ -709,4 +695,5 @@ struct flash_driver at91sam4l_flash = {
.auto_probe = sam4l_probe,
.erase_check = default_flash_blank_check,
.protect_check = sam4l_protect_check,
+ .free_driver_priv = default_flash_free_driver_priv,
};
diff --git a/src/flash/nor/at91sam7.c b/src/flash/nor/at91sam7.c
index 03f771c..9de8293 100644
--- a/src/flash/nor/at91sam7.c
+++ b/src/flash/nor/at91sam7.c
@@ -639,14 +639,6 @@ static int at91sam7_read_part_info(struct flash_bank *bank)
static int at91sam7_erase_check(struct flash_bank *bank)
{
- struct target *target = bank->target;
- uint16_t retval;
- uint32_t blank;
- uint16_t fast_check;
- uint8_t *buffer;
- uint16_t nSector;
- uint16_t nByte;
-
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
@@ -656,45 +648,7 @@ static int at91sam7_erase_check(struct flash_bank *bank)
at91sam7_read_clock_info(bank);
at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
- fast_check = 1;
- for (nSector = 0; nSector < bank->num_sectors; nSector++) {
- retval = target_blank_check_memory(target,
- bank->base + bank->sectors[nSector].offset,
- bank->sectors[nSector].size,
- &blank, bank->erased_value);
- if (retval != ERROR_OK) {
- fast_check = 0;
- break;
- }
- if (blank == 0xFF)
- bank->sectors[nSector].is_erased = 1;
- else
- bank->sectors[nSector].is_erased = 0;
- }
-
- if (fast_check)
- return ERROR_OK;
-
- LOG_USER("Running slow fallback erase check - add working memory");
-
- buffer = malloc(bank->sectors[0].size);
- for (nSector = 0; nSector < bank->num_sectors; nSector++) {
- bank->sectors[nSector].is_erased = 1;
- retval = target_read_memory(target, bank->base + bank->sectors[nSector].offset, 4,
- bank->sectors[nSector].size/4, buffer);
- if (retval != ERROR_OK)
- return retval;
-
- for (nByte = 0; nByte < bank->sectors[nSector].size; nByte++) {
- if (buffer[nByte] != 0xFF) {
- bank->sectors[nSector].is_erased = 0;
- break;
- }
- }
- }
- free(buffer);
-
- return ERROR_OK;
+ return default_flash_blank_check(bank);
}
static int at91sam7_protect_check(struct flash_bank *bank)
diff --git a/src/flash/nor/at91samd.c b/src/flash/nor/at91samd.c
index 64716d9..8553ee8 100644
--- a/src/flash/nor/at91samd.c
+++ b/src/flash/nor/at91samd.c
@@ -304,10 +304,8 @@ struct samd_info {
bool probed;
struct target *target;
- struct samd_info *next;
};
-static struct samd_info *samd_chips;
/**
* Gives the family structure to specific device id.
@@ -876,30 +874,6 @@ free_pb:
FLASH_BANK_COMMAND_HANDLER(samd_flash_bank_command)
{
- struct samd_info *chip = samd_chips;
-
- while (chip) {
- if (chip->target == bank->target)
- break;
- chip = chip->next;
- }
-
- if (!chip) {
- /* Create a new chip */
- chip = calloc(1, sizeof(*chip));
- if (!chip)
- return ERROR_FAIL;
-
- chip->target = bank->target;
- chip->probed = false;
-
- bank->driver_priv = chip;
-
- /* Insert it into the chips list (at head) */
- chip->next = samd_chips;
- samd_chips = chip;
- }
-
if (bank->base != SAMD_FLASH) {
LOG_ERROR("Address 0x%08" PRIx32 " invalid bank address (try 0x%08" PRIx32
"[at91samd series] )",
@@ -907,6 +881,18 @@ FLASH_BANK_COMMAND_HANDLER(samd_flash_bank_command)
return ERROR_FAIL;
}
+ struct samd_info *chip;
+ chip = calloc(1, sizeof(*chip));
+ if (!chip) {
+ LOG_ERROR("No memory for flash bank chip info");
+ return ERROR_FAIL;
+ }
+
+ chip->target = bank->target;
+ chip->probed = false;
+
+ bank->driver_priv = chip;
+
return ERROR_OK;
}
@@ -1281,4 +1267,5 @@ struct flash_driver at91samd_flash = {
.auto_probe = samd_probe,
.erase_check = default_flash_blank_check,
.protect_check = samd_protect_check,
+ .free_driver_priv = default_flash_free_driver_priv,
};
diff --git a/src/flash/nor/core.c b/src/flash/nor/core.c
index 636d50c..f05c68b 100644
--- a/src/flash/nor/core.c
+++ b/src/flash/nor/core.c
@@ -4,6 +4,7 @@
* Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
* Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
* Copyright (C) 2010 by Antonio Borneo <borneo.antonio@gmail.com> *
+ * Copyright (C) 2017-2018 Tomas Vanek <vanekt@fbl.cz> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
@@ -339,36 +340,49 @@ int default_flash_blank_check(struct flash_bank *bank)
struct target *target = bank->target;
int i;
int retval;
- int fast_check = 0;
- uint32_t blank;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}
+ struct target_memory_check_block *block_array;
+ block_array = malloc(bank->num_sectors * sizeof(struct target_memory_check_block));
+ if (block_array == NULL)
+ return default_flash_mem_blank_check(bank);
+
for (i = 0; i < bank->num_sectors; i++) {
- uint32_t address = bank->base + bank->sectors[i].offset;
- uint32_t size = bank->sectors[i].size;
+ block_array[i].address = bank->base + bank->sectors[i].offset;
+ block_array[i].size = bank->sectors[i].size;
+ block_array[i].result = UINT32_MAX; /* erase state unknown */
+ }
- retval = target_blank_check_memory(target, address, size, &blank, bank->erased_value);
- if (retval != ERROR_OK) {
- fast_check = 0;
+ bool fast_check = true;
+ for (i = 0; i < bank->num_sectors; ) {
+ retval = target_blank_check_memory(target,
+ block_array + i, bank->num_sectors - i,
+ bank->erased_value);
+ if (retval < 1) {
+ /* Run slow fallback if the first run gives no result
+ * otherwise use possibly incomplete results */
+ if (i == 0)
+ fast_check = false;
break;
}
- if (blank == bank->erased_value)
- bank->sectors[i].is_erased = 1;
- else
- bank->sectors[i].is_erased = 0;
- fast_check = 1;
+ i += retval; /* add number of blocks done this round */
}
- if (!fast_check) {
+ if (fast_check) {
+ for (i = 0; i < bank->num_sectors; i++)
+ bank->sectors[i].is_erased = block_array[i].result;
+ retval = ERROR_OK;
+ } else {
LOG_USER("Running slow fallback erase check - add working memory");
- return default_flash_mem_blank_check(bank);
+ retval = default_flash_mem_blank_check(bank);
}
+ free(block_array);
- return ERROR_OK;
+ return retval;
}
/* Manipulate given flash region, selecting the bank according to target
@@ -587,6 +601,87 @@ static int compare_section(const void *a, const void *b)
return -1;
}
+/**
+ * Get aligned start address of a flash write region
+ */
+target_addr_t flash_write_align_start(struct flash_bank *bank, target_addr_t addr)
+{
+ if (addr < bank->base || addr >= bank->base + bank->size
+ || bank->write_start_alignment <= 1)
+ return addr;
+
+ if (bank->write_start_alignment == FLASH_WRITE_ALIGN_SECTOR) {
+ uint32_t offset = addr - bank->base;
+ uint32_t aligned = 0;
+ int sect;
+ for (sect = 0; sect < bank->num_sectors; sect++) {
+ if (bank->sectors[sect].offset > offset)
+ break;
+
+ aligned = bank->sectors[sect].offset;
+ }
+ return bank->base + aligned;
+ }
+
+ return addr & ~(bank->write_start_alignment - 1);
+}
+
+/**
+ * Get aligned end address of a flash write region
+ */
+target_addr_t flash_write_align_end(struct flash_bank *bank, target_addr_t addr)
+{
+ if (addr < bank->base || addr >= bank->base + bank->size
+ || bank->write_end_alignment <= 1)
+ return addr;
+
+ if (bank->write_end_alignment == FLASH_WRITE_ALIGN_SECTOR) {
+ uint32_t offset = addr - bank->base;
+ uint32_t aligned = 0;
+ int sect;
+ for (sect = 0; sect < bank->num_sectors; sect++) {
+ aligned = bank->sectors[sect].offset + bank->sectors[sect].size - 1;
+ if (aligned >= offset)
+ break;
+ }
+ return bank->base + aligned;
+ }
+
+ return addr | (bank->write_end_alignment - 1);
+}
+
+/**
+ * Check if gap between sections is bigger than minimum required to discontinue flash write
+ */
+static bool flash_write_check_gap(struct flash_bank *bank,
+ target_addr_t addr1, target_addr_t addr2)
+{
+ if (bank->minimal_write_gap == FLASH_WRITE_CONTINUOUS
+ || addr1 < bank->base || addr1 >= bank->base + bank->size
+ || addr2 < bank->base || addr2 >= bank->base + bank->size)
+ return false;
+
+ if (bank->minimal_write_gap == FLASH_WRITE_GAP_SECTOR) {
+ int sect;
+ uint32_t offset1 = addr1 - bank->base;
+ /* find the sector following the one containing addr1 */
+ for (sect = 0; sect < bank->num_sectors; sect++) {
+ if (bank->sectors[sect].offset > offset1)
+ break;
+ }
+ if (sect >= bank->num_sectors)
+ return false;
+
+ uint32_t offset2 = addr2 - bank->base;
+ return bank->sectors[sect].offset + bank->sectors[sect].size <= offset2;
+ }
+
+ target_addr_t aligned1 = flash_write_align_end(bank, addr1);
+ target_addr_t aligned2 = flash_write_align_start(bank, addr2);
+ return aligned1 + bank->minimal_write_gap < aligned2;
+}
+
+
int flash_write_unlock(struct target *target, struct image *image,
uint32_t *written, int erase, bool unlock)
{
@@ -626,7 +721,7 @@ int flash_write_unlock(struct target *target, struct image *image,
/* loop until we reach end of the image */
while (section < image->num_sections) {
- uint32_t buffer_size;
+ uint32_t buffer_idx;
uint8_t *buffer;
int section_last;
target_addr_t run_address = sections[section]->base_address + section_offset;
@@ -663,43 +758,37 @@ int flash_write_unlock(struct target *target, struct image *image,
break;
}
- /* FIXME This needlessly touches sectors BETWEEN the
- * sections it's writing. Without auto erase, it just
- * writes ones. That WILL INVALIDATE data in cases
- * like Stellaris Tempest chips, corrupting internal
- * ECC codes; and at least FreeScale suggests issues
- * with that approach (in HC11 documentation).
- *
- * With auto erase enabled, data in those sectors will
- * be needlessly destroyed; and some of the limited
- * number of flash erase cycles will be wasted...
- *
- * In both cases, the extra writes slow things down.
- */
-
/* if we have multiple sections within our image,
* flash programming could fail due to alignment issues
* attempt to rebuild a consecutive buffer for the flash loader */
target_addr_t run_next_addr = run_address + run_size;
- if (sections[section_last + 1]->base_address < run_next_addr) {
+ target_addr_t next_section_base = sections[section_last + 1]->base_address;
+ if (next_section_base < run_next_addr) {
LOG_ERROR("Section at " TARGET_ADDR_FMT
" overlaps section ending at " TARGET_ADDR_FMT,
- sections[section_last + 1]->base_address,
- run_next_addr);
+ next_section_base, run_next_addr);
LOG_ERROR("Flash write aborted.");
retval = ERROR_FAIL;
goto done;
}
- pad_bytes = sections[section_last + 1]->base_address - run_next_addr;
+ pad_bytes = next_section_base - run_next_addr;
+ if (pad_bytes) {
+ if (flash_write_check_gap(c, run_next_addr - 1, next_section_base)) {
+ LOG_INFO("Flash write discontinued at " TARGET_ADDR_FMT
+ ", next section at " TARGET_ADDR_FMT,
+ run_next_addr, next_section_base);
+ break;
+ }
+ }
+ if (pad_bytes > 0)
+ LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
+ " with %d bytes",
+ section_last, run_next_addr, pad_bytes);
+
padding[section_last] = pad_bytes;
- run_size += sections[++section_last]->size;
run_size += pad_bytes;
-
- if (pad_bytes > 0)
- LOG_INFO("Padding image section %d with %d bytes",
- section_last-1,
- pad_bytes);
+ run_size += sections[++section_last]->size;
}
if (run_address + run_size - 1 > c->base + c->size - 1) {
@@ -712,10 +801,38 @@ int flash_write_unlock(struct target *target, struct image *image,
assert(run_size > 0);
}
- /* If we're applying any sector automagic, then pad this
- * (maybe-combined) segment to the end of its last sector.
- */
- if (unlock || erase) {
+ uint32_t padding_at_start = 0;
+ if (c->write_start_alignment || c->write_end_alignment) {
+ /* align write region according to bank requirements */
+ target_addr_t aligned_start = flash_write_align_start(c, run_address);
+ padding_at_start = run_address - aligned_start;
+ if (padding_at_start > 0) {
+ LOG_WARNING("Section start address " TARGET_ADDR_FMT
+ " breaks the required alignment of flash bank %s",
+ run_address, c->name);
+ LOG_WARNING("Padding %d bytes from " TARGET_ADDR_FMT,
+ padding_at_start, aligned_start);
+
+ run_address -= padding_at_start;
+ run_size += padding_at_start;
+ }
+
+ target_addr_t run_end = run_address + run_size - 1;
+ target_addr_t aligned_end = flash_write_align_end(c, run_end);
+ pad_bytes = aligned_end - run_end;
+ if (pad_bytes > 0) {
+ LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
+ " with %d bytes (bank write end alignment)",
+ section_last, run_end + 1, pad_bytes);
+
+ padding[section_last] += pad_bytes;
+ run_size += pad_bytes;
+ }
+
+ } else if (unlock || erase) {
+ /* If we're applying any sector automagic, then pad this
+ * (maybe-combined) segment to the end of its last sector.
+ */
int sector;
uint32_t offset_start = run_address - c->base;
uint32_t offset_end = offset_start + run_size;
@@ -740,13 +857,17 @@ int flash_write_unlock(struct target *target, struct image *image,
retval = ERROR_FAIL;
goto done;
}
- buffer_size = 0;
+
+ if (padding_at_start)
+ memset(buffer, c->default_padded_value, padding_at_start);
+
+ buffer_idx = padding_at_start;
/* read sections to the buffer */
- while (buffer_size < run_size) {
+ while (buffer_idx < run_size) {
size_t size_read;
- size_read = run_size - buffer_size;
+ size_read = run_size - buffer_idx;
if (size_read > sections[section]->size - section_offset)
size_read = sections[section]->size - section_offset;
@@ -759,23 +880,25 @@ int flash_write_unlock(struct target *target, struct image *image,
int t_section_num = diff / sizeof(struct imagesection);
LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
- "section_offset = %d, buffer_size = %d, size_read = %d",
- (int)section, (int)t_section_num, (int)section_offset,
- (int)buffer_size, (int)size_read);
+ "section_offset = %"PRIu32", buffer_idx = %"PRIu32", size_read = %zu",
+ section, t_section_num, section_offset,
+ buffer_idx, size_read);
retval = image_read_section(image, t_section_num, section_offset,
- size_read, buffer + buffer_size, &size_read);
+ size_read, buffer + buffer_idx, &size_read);
if (retval != ERROR_OK || size_read == 0) {
free(buffer);
goto done;
}
- /* see if we need to pad the section */
- while (padding[section]--)
- (buffer + buffer_size)[size_read++] = c->default_padded_value;
-
- buffer_size += size_read;
+ buffer_idx += size_read;
section_offset += size_read;
+ /* see if we need to pad the section */
+ if (padding[section]) {
+ memset(buffer + buffer_idx, c->default_padded_value, padding[section]);
+ buffer_idx += padding[section];
+ }
+
if (section_offset >= sections[section]->size) {
section++;
section_offset = 0;
diff --git a/src/flash/nor/core.h b/src/flash/nor/core.h
index 1bfe1ab..67de94e 100644
--- a/src/flash/nor/core.h
+++ b/src/flash/nor/core.h
@@ -65,6 +65,13 @@ struct flash_sector {
int is_protected;
};
+/** Special value for write_start_alignment and write_end_alignment field */
+#define FLASH_WRITE_ALIGN_SECTOR UINT32_MAX
+
+/** Special values for minimal_write_gap field */
+#define FLASH_WRITE_CONTINUOUS 0
+#define FLASH_WRITE_GAP_SECTOR UINT32_MAX
+
/**
* Provides details of a flash bank, available either on-chip or through
* a major interface.
@@ -97,6 +104,18 @@ struct flash_bank {
* erased value. Defaults to 0xFF. */
uint8_t default_padded_value;
+ /** Required alignment of flash write start address.
+ * Default 0, no alignment. Can be any power of two or FLASH_WRITE_ALIGN_SECTOR */
+ uint32_t write_start_alignment;
+ /** Required alignment of flash write end address.
+ * Default 0, no alignment. Can be any power of two or FLASH_WRITE_ALIGN_SECTOR */
+ uint32_t write_end_alignment;
+ /** Minimal gap between sections to discontinue flash write
+ * Default FLASH_WRITE_GAP_SECTOR splits the write if one or more untouched
+ * sectors in between.
+ * Can be size in bytes or FLASH_WRITE_CONTINUOUS */
+ uint32_t minimal_write_gap;
+
/**
* The number of sectors on this chip. This value will
* be set intially to 0, and the flash driver must set this to
@@ -136,6 +155,22 @@ int flash_unlock_address_range(struct target *target, uint32_t addr,
uint32_t length);
/**
+ * Align start address of a flash write region according to bank requirements.
+ * @param bank Pointer to bank descriptor structure
+ * @param addr Address to align
+ * @returns Aligned address
+*/
+target_addr_t flash_write_align_start(struct flash_bank *bank, target_addr_t addr);
+/**
+ * Align end address of a flash write region according to bank requirements.
+ * Note: Use address of the last byte to write, not the next after the region.
+ * @param bank Pointer to bank descriptor structure
+ * @param addr Address to align (address of the last byte to write)
+ * @returns Aligned address (address of the last byte of padded region)
+*/
+target_addr_t flash_write_align_end(struct flash_bank *bank, target_addr_t addr);
+
+/**
* Writes @a image into the @a target flash. The @a written parameter
* will contain the
* @param target The target with the flash to be programmed.
diff --git a/src/flash/nor/kinetis.c b/src/flash/nor/kinetis.c
index 48a5de4..4d665d3 100644
--- a/src/flash/nor/kinetis.c
+++ b/src/flash/nor/kinetis.c
@@ -915,6 +915,22 @@ FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
}
+static void kinetis_free_driver_priv(struct flash_bank *bank)
+{
+ struct kinetis_flash_bank *k_bank = bank->driver_priv;
+ if (k_bank == NULL)
+ return;
+
+ struct kinetis_chip *k_chip = k_bank->k_chip;
+ if (k_chip == NULL)
+ return;
+
+ k_chip->num_banks--;
+ if (k_chip->num_banks == 0)
+ free(k_chip);
+}
+
+
static int kinetis_create_missing_banks(struct kinetis_chip *k_chip)
{
unsigned bank_idx;
@@ -939,7 +955,7 @@ static int kinetis_create_missing_banks(struct kinetis_chip *k_chip)
if (k_chip->num_pflash_blocks > 1) {
/* rename first bank if numbering is needed */
snprintf(name, sizeof(name), "%s.pflash0", base_name);
- free((void *)bank->name);
+ free(bank->name);
bank->name = strdup(name);
}
}
@@ -3132,4 +3148,5 @@ struct flash_driver kinetis_flash = {
.erase_check = kinetis_blank_check,
.protect_check = kinetis_protect_check,
.info = kinetis_info,
+ .free_driver_priv = kinetis_free_driver_priv,
};
diff --git a/src/flash/nor/psoc6.c b/src/flash/nor/psoc6.c
index e5c4197..9352ad4 100644
--- a/src/flash/nor/psoc6.c
+++ b/src/flash/nor/psoc6.c
@@ -1,6 +1,6 @@
/***************************************************************************
* *
- * Copyright (C) 2017 by Bohdan Tymkiv *
+ * Copyright (C) 2018 by Bohdan Tymkiv *
* bohdan.tymkiv@cypress.com bohdan200@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
@@ -101,7 +101,7 @@ struct row_region {
size_t size;
};
-static struct row_region safe_sflash_regions[] = {
+static const struct row_region safe_sflash_regions[] = {
{0x16000800, 0x800}, /* SFLASH: User Data */
{0x16001A00, 0x200}, /* SFLASH: NAR */
{0x16005A00, 0xC00}, /* SFLASH: Public Key */
@@ -111,8 +111,12 @@ static struct row_region safe_sflash_regions[] = {
#define SFLASH_NUM_REGIONS (sizeof(safe_sflash_regions) / sizeof(safe_sflash_regions[0]))
static struct working_area *g_stack_area;
-/**************************************************************************************************
- * Initializes timeout_s structure with given timeout in milliseconds
+static struct armv7m_algorithm g_armv7m_info;
+
+/** ***********************************************************************************************
+ * @brief Initializes `struct timeout` structure with given timeout value
+ * @param to pointer to `struct timeout` structure
+ * @param timeout_ms timeout, in milliseconds
*************************************************************************************************/
static void timeout_init(struct timeout *to, long timeout_ms)
{
@@ -120,17 +124,23 @@ static void timeout_init(struct timeout *to, long timeout_ms)
to->timeout_ms = timeout_ms;
}
-/**************************************************************************************************
- * Returns true if given timeout_s object has expired
+/** ***********************************************************************************************
+ * @brief Returns true if given `struct timeout` structure has expired
+ * @param to pointer to `struct timeout` structure
+ * @return true if timeout expired
*************************************************************************************************/
static bool timeout_expired(struct timeout *to)
{
return (timeval_ms() - to->start_time) > to->timeout_ms;
}
-/**************************************************************************************************
- * Prepares PSoC6 for running pseudo flash algorithm. This function allocates Working Area for
- * the algorithm and for CPU Stack.
+/** ***********************************************************************************************
+ * @brief Starts pseudo flash algorithm and leaves it running. Function allocates working area for
+ * algorithm code and CPU stack, adjusts stack pointer, uploads and starts the algorithm.
+ * Algorithm (a basic infinite loop) runs asynchronously while driver performs Flash operations.
+ *
+ * @param target target for the algorithm
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int sromalgo_prepare(struct target *target)
{
@@ -141,88 +151,92 @@ static int sromalgo_prepare(struct target *target)
if (hr != ERROR_OK)
return hr;
+ /* Restore THUMB bit in xPSR register */
+ const struct armv7m_common *cm = target_to_armv7m(target);
+ hr = cm->store_core_reg_u32(target, ARMV7M_xPSR, 0x01000000);
+ if (hr != ERROR_OK)
+ return hr;
+
/* Allocate Working Area for Stack and Flash algorithm */
hr = target_alloc_working_area(target, RAM_STACK_WA_SIZE, &g_stack_area);
if (hr != ERROR_OK)
return hr;
- /* Restore THUMB bit in xPSR register */
- const struct armv7m_common *cm = target_to_armv7m(target);
- hr = cm->store_core_reg_u32(target, ARMV7M_xPSR, 0x01000000);
+ g_armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
+ g_armv7m_info.core_mode = ARM_MODE_THREAD;
+
+ struct reg_param reg_params;
+ init_reg_param(&reg_params, "sp", 32, PARAM_OUT);
+ buf_set_u32(reg_params.value, 0, 32, g_stack_area->address + g_stack_area->size);
+
+ /* Write basic infinite loop algorithm to target RAM */
+ hr = target_write_u32(target, g_stack_area->address, 0xFEE7FEE7);
if (hr != ERROR_OK)
- goto exit_free_wa;
+ goto destroy_rp_free_wa;
- return ERROR_OK;
+ hr = target_start_algorithm(target, 0, NULL, 1, &reg_params, g_stack_area->address,
+ 0, &g_armv7m_info);
+ if (hr != ERROR_OK)
+ goto destroy_rp_free_wa;
-exit_free_wa:
- /* Something went wrong, free allocated area */
- if (g_stack_area) {
- target_free_working_area(target, g_stack_area);
- g_stack_area = NULL;
- }
+ destroy_reg_param(&reg_params);
return hr;
-}
-/**************************************************************************************************
- * Releases working area
- *************************************************************************************************/
-static int sromalgo_release(struct target *target)
-{
- int hr = ERROR_OK;
+destroy_rp_free_wa:
+ /* Something went wrong, do some cleanup */
+ destroy_reg_param(&reg_params);
- /* Free Stack/Flash algorithm working area */
if (g_stack_area) {
- hr = target_free_working_area(target, g_stack_area);
+ target_free_working_area(target, g_stack_area);
g_stack_area = NULL;
}
return hr;
}
-/**************************************************************************************************
- * Runs pseudo flash algorithm. Algorithm itself consist of couple of NOPs followed by BKPT
- * instruction. The trick here is that NMI has already been posted to CM0 via IPC structure
- * prior to calling this function. CM0 will immediately jump to NMI handler and execute
- * SROM API code.
- * This approach is borrowed from PSoC4 Flash Driver.
+/** ***********************************************************************************************
+ * @brief Stops running flash algorithm and releases associated resources.
+ * This function is also used for cleanup in case of errors so g_stack_area may be NULL.
+ * These cases have to be handled gracefully.
+ *
+ * @param target current target
*************************************************************************************************/
-static int sromalgo_run(struct target *target)
+static void sromalgo_release(struct target *target)
{
- int hr;
-
- struct armv7m_algorithm armv7m_info;
- armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
- armv7m_info.core_mode = ARM_MODE_THREAD;
-
- struct reg_param reg_params;
- init_reg_param(&reg_params, "sp", 32, PARAM_OUT);
- buf_set_u32(reg_params.value, 0, 32, g_stack_area->address + g_stack_area->size);
-
- /* mov r8, r8; mov r8, r8 */
- hr = target_write_u32(target, g_stack_area->address + 0, 0x46C046C0);
- if (hr != ERROR_OK)
- return hr;
-
- /* mov r8, r8; bkpt #0 */
- hr = target_write_u32(target, g_stack_area->address + 4, 0xBE0046C0);
- if (hr != ERROR_OK)
- return hr;
+ int hr = ERROR_OK;
- hr = target_run_algorithm(target, 0, NULL, 1, &reg_params, g_stack_area->address,
- 0, SROMAPI_CALL_TIMEOUT_MS, &armv7m_info);
+ if (g_stack_area) {
+ /* Stop flash algorithm if it is running */
+ if (target->running_alg) {
+ hr = target_halt(target);
+ if (hr != ERROR_OK)
+ goto exit_free_wa;
- destroy_reg_param(&reg_params);
+ hr = target_wait_algorithm(target, 0, NULL, 0, NULL, 0,
+ IPC_TIMEOUT_MS, &g_armv7m_info);
+ if (hr != ERROR_OK)
+ goto exit_free_wa;
+ }
- return hr;
+exit_free_wa:
+ /* Free Stack/Flash algorithm working area */
+ target_free_working_area(target, g_stack_area);
+ g_stack_area = NULL;
+ }
}
-/**************************************************************************************************
- * Waits for expected IPC lock status.
- * PSoC6 uses IPC structures for inter-core communication. Same IPCs are used to invoke SROM API.
- * IPC structure must be locked prior to invoking any SROM API. This ensures nothing else in the
- * system will use same IPC thus corrupting our data. Locking is performed by ipc_acquire(), this
- * function ensures that IPC is actually in expected state
+/** ***********************************************************************************************
+ * @brief Waits for expected IPC lock status. PSoC6 uses IPC structures for inter-core
+ * communication. Same IPCs are used to invoke SROM API. IPC structure must be locked prior to
+ * invoking any SROM API. This ensures nothing else in the system will use same IPC thus corrupting
+ * our data. Locking is performed by ipc_acquire(), this function ensures that IPC is actually
+ * in expected state
+ *
+ * @param target current target
+ * @param ipc_id IPC index to poll. IPC #2 is dedicated for DAP access
+ * @param lock_expected expected lock status
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int ipc_poll_lock_stat(struct target *target, uint32_t ipc_id, bool lock_expected)
{
@@ -258,11 +272,15 @@ static int ipc_poll_lock_stat(struct target *target, uint32_t ipc_id, bool lock_
return ERROR_TARGET_TIMEOUT;
}
-/**************************************************************************************************
- * Acquires IPC structure
- * PSoC6 uses IPC structures for inter-core communication. Same IPCs are used to invoke SROM API.
- * IPC structure must be locked prior to invoking any SROM API. This ensures nothing else in the
- * system will use same IPC thus corrupting our data. This function locks the IPC.
+/** ***********************************************************************************************
+ * @brief Acquires IPC structure. PSoC6 uses IPC structures for inter-core communication.
+ * Same IPCs are used to invoke SROM API. IPC structure must be locked prior to invoking any SROM API.
+ * This ensures nothing else in the system will use same IPC thus corrupting our data.
+ * This function locks the IPC.
+ *
+ * @param target current target
+ * @param ipc_id ipc_id IPC index to acquire. IPC #2 is dedicated for DAP access
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int ipc_acquire(struct target *target, char ipc_id)
{
@@ -303,8 +321,14 @@ static int ipc_acquire(struct target *target, char ipc_id)
return hr;
}
-/**************************************************************************************************
- * Invokes SROM API functions which are responsible for Flash operations
+/** ***********************************************************************************************
+ * @brief Invokes SROM API functions which are responsible for Flash operations
+ *
+ * @param target current target
+ * @param req_and_params requect id of the function to invoke
+ * @param working_area address of memory buffer in target's memory space for SROM API parameters
+ * @param data_out pointer to variable which will be populated with execution status
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int call_sromapi(struct target *target,
uint32_t req_and_params,
@@ -336,10 +360,6 @@ static int call_sromapi(struct target *target,
if (hr != ERROR_OK)
return hr;
- hr = sromalgo_run(target);
- if (hr != ERROR_OK)
- return hr;
-
/* Poll lock status */
hr = ipc_poll_lock_stat(target, IPC_ID, false);
if (hr != ERROR_OK)
@@ -365,8 +385,12 @@ static int call_sromapi(struct target *target,
return ERROR_OK;
}
-/**************************************************************************************************
- * Retrieves SiliconID and Protection status of the target device
+/** ***********************************************************************************************
+ * @brief Retrieves SiliconID and Protection status of the target device
+ * @param target current target
+ * @param si_id pointer to variable, will be populated with SiliconID
+ * @param protection pointer to variable, will be populated with protection status
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int get_silicon_id(struct target *target, uint32_t *si_id, uint8_t *protection)
{
@@ -375,17 +399,17 @@ static int get_silicon_id(struct target *target, uint32_t *si_id, uint8_t *prote
hr = sromalgo_prepare(target);
if (hr != ERROR_OK)
- return hr;
+ goto exit;
/* Read FamilyID and Revision */
hr = call_sromapi(target, SROMAPI_SIID_REQ_FAMILY_REVISION, 0, &family_rev);
if (hr != ERROR_OK)
- return hr;
+ goto exit;
/* Read SiliconID and Protection */
hr = call_sromapi(target, SROMAPI_SIID_REQ_SIID_PROTECTION, 0, &siid_prot);
if (hr != ERROR_OK)
- return hr;
+ goto exit;
*si_id = (siid_prot & 0x0000FFFF) << 16;
*si_id |= (family_rev & 0x00FF0000) >> 8;
@@ -393,12 +417,15 @@ static int get_silicon_id(struct target *target, uint32_t *si_id, uint8_t *prote
*protection = (siid_prot & 0x000F0000) >> 0x10;
- hr = sromalgo_release(target);
- return hr;
+exit:
+ sromalgo_release(target);
+ return ERROR_OK;
}
-/**************************************************************************************************
- * Translates Protection status to openocd-friendly boolean value
+/** ***********************************************************************************************
+ * @brief Translates Protection status to openocd-friendly boolean value
+ * @param bank current flash bank
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int psoc6_protect_check(struct flash_bank *bank)
{
@@ -429,8 +456,9 @@ static int psoc6_protect_check(struct flash_bank *bank)
return ERROR_OK;
}
-/**************************************************************************************************
- * Life Cycle transition is not currently supported
+/** ***********************************************************************************************
+ * @brief Dummy function, Life Cycle transition is not currently supported
+ * @return ERROR_OK always
*************************************************************************************************/
static int psoc6_protect(struct flash_bank *bank, int set, int first, int last)
{
@@ -443,8 +471,10 @@ static int psoc6_protect(struct flash_bank *bank, int set, int first, int last)
return ERROR_OK;
}
-/**************************************************************************************************
- * Translates Protection status to string
+/** ***********************************************************************************************
+ * @brief Translates Protection status to string
+ * @param protection protection value
+ * @return pointer to const string describintg protection status
*************************************************************************************************/
static const char *protection_to_str(uint8_t protection)
{
@@ -468,8 +498,12 @@ static const char *protection_to_str(uint8_t protection)
}
}
-/**************************************************************************************************
- * Displays human-readable information about acquired device
+/** ***********************************************************************************************
+ * @brief psoc6_get_info Displays human-readable information about acquired device
+ * @param bank current flash bank
+ * @param buf pointer to buffer for human-readable text
+ * @param buf_size size of the buffer
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int psoc6_get_info(struct flash_bank *bank, char *buf, int buf_size)
{
@@ -494,8 +528,10 @@ static int psoc6_get_info(struct flash_bank *bank, char *buf, int buf_size)
return ERROR_OK;
}
-/**************************************************************************************************
- * Returns true if flash bank name represents Supervisory Flash
+/** ***********************************************************************************************
+ * @brief Checks if given flash bank belongs to Supervisory Flash
+ * @param bank current flash bank
+ * @return true if flash bank belongs to Supervisory Flash
*************************************************************************************************/
static bool is_sflash_bank(struct flash_bank *bank)
{
@@ -507,27 +543,33 @@ static bool is_sflash_bank(struct flash_bank *bank)
return false;
}
-/**************************************************************************************************
- * Returns true if flash bank name represents Work Flash
+/** ***********************************************************************************************
+ * @brief Checks if given flash bank belongs to Work Flash
+ * @param bank current flash bank
+ * @return true if flash bank belongs to Work Flash
*************************************************************************************************/
static inline bool is_wflash_bank(struct flash_bank *bank)
{
return (bank->base == MEM_BASE_WFLASH);
}
-/**************************************************************************************************
- * Returns true if flash bank name represents Main Flash
+/** ***********************************************************************************************
+ * @brief Checks if given flash bank belongs to Main Flash
+ * @param bank current flash bank
+ * @return true if flash bank belongs to Main Flash
*************************************************************************************************/
static inline bool is_mflash_bank(struct flash_bank *bank)
{
return (bank->base == MEM_BASE_MFLASH);
}
-/**************************************************************************************************
- * Probes the device and populates related data structures with target flash geometry data.
+/** ***********************************************************************************************
+ * @brief Probes the device and populates related data structures with target flash geometry data.
* This is done in non-intrusive way, no SROM API calls are involved so GDB can safely attach to a
- * running target.
- * Function assumes that size of Work Flash is 32kB (true for all current part numbers)
+ * running target. Function assumes that size of Work Flash is 32kB (true for all current part numbers)
+ *
+ * @param bank current flash bank
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int psoc6_probe(struct flash_bank *bank)
{
@@ -595,8 +637,10 @@ static int psoc6_probe(struct flash_bank *bank)
return hr;
}
-/**************************************************************************************************
- * Probes target device only if it hasn't been probed yet
+/** ***********************************************************************************************
+ * @brief Probes target device only if it hasn't been probed yet
+ * @param bank current flash bank
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int psoc6_auto_probe(struct flash_bank *bank)
{
@@ -611,8 +655,12 @@ static int psoc6_auto_probe(struct flash_bank *bank)
return hr;
}
-/**************************************************************************************************
- * Erases single sector (256k) on target device
+/** ***********************************************************************************************
+ * @brief Erases single sector (256k) on target device
+ * @param bank current flash bank
+ * @param wa working area for SROM API parameters
+ * @param addr starting address of the sector
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int psoc6_erase_sector(struct flash_bank *bank, struct working_area *wa, uint32_t addr)
{
@@ -636,8 +684,12 @@ static int psoc6_erase_sector(struct flash_bank *bank, struct working_area *wa,
return hr;
}
-/**************************************************************************************************
- * Erases single row (512b) on target device
+/** ***********************************************************************************************
+ * @brief Erases single row (512b) on target device
+ * @param bank current flash bank
+ * @param wa working area for SROM API parameters
+ * @param addr starting address of the flash row
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int psoc6_erase_row(struct flash_bank *bank, struct working_area *wa, uint32_t addr)
{
@@ -661,9 +713,14 @@ static int psoc6_erase_row(struct flash_bank *bank, struct working_area *wa, uin
return hr;
}
-/**************************************************************************************************
- * Performs Erase operation.
- * Function will try to use biggest erase block possible to speedup the operation
+/** ***********************************************************************************************
+ * @brief Performs Erase operation. Function will try to use biggest erase block possible to
+ * speedup the operation.
+ *
+ * @param bank current flash bank
+ * @param first first sector to erase
+ * @param last last sector to erase
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int psoc6_erase(struct flash_bank *bank, int first, int last)
{
@@ -681,7 +738,7 @@ static int psoc6_erase(struct flash_bank *bank, int first, int last)
hr = sromalgo_prepare(target);
if (hr != ERROR_OK)
- return hr;
+ goto exit;
hr = target_alloc_working_area(target, psoc6_info->row_sz + 32, &wa);
if (hr != ERROR_OK)
@@ -720,9 +777,13 @@ exit:
return hr;
}
-
-/**************************************************************************************************
- * Programs single Flash Row
+/** ***********************************************************************************************
+ * @brief Programs single Flash Row
+ * @param bank current flash bank
+ * @param addr address of the flash row
+ * @param buffer pointer to the buffer with data
+ * @param is_sflash true if current flash bank belongs to Supervisory Flash
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int psoc6_program_row(struct flash_bank *bank,
uint32_t addr,
@@ -773,9 +834,13 @@ exit:
return hr;
}
-
-/**************************************************************************************************
- * Programs set of Rows
+/** ***********************************************************************************************
+ * @brief Performs Program operation
+ * @param bank current flash bank
+ * @param buffer pointer to the buffer with data
+ * @param offset starting offset in falsh bank
+ * @param count number of bytes in buffer
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
static int psoc6_program(struct flash_bank *bank,
const uint8_t *buffer,
@@ -787,11 +852,11 @@ static int psoc6_program(struct flash_bank *bank,
const bool is_sflash = is_sflash_bank(bank);
int hr;
+ uint8_t page_buf[psoc6_info->row_sz];
+
hr = sromalgo_prepare(target);
if (hr != ERROR_OK)
- return hr;
-
- uint8_t page_buf[psoc6_info->row_sz];
+ goto exit;
while (count) {
uint32_t row_offset = offset % psoc6_info->row_sz;
@@ -804,7 +869,7 @@ static int psoc6_program(struct flash_bank *bank,
hr = psoc6_program_row(bank, aligned_addr, page_buf, is_sflash);
if (hr != ERROR_OK) {
LOG_ERROR("Failed to program Flash at address 0x%08X", aligned_addr);
- break;
+ goto exit;
}
buffer += row_bytes;
@@ -812,13 +877,15 @@ static int psoc6_program(struct flash_bank *bank,
count -= row_bytes;
}
- hr = sromalgo_release(target);
+exit:
+ sromalgo_release(target);
return hr;
}
-/**************************************************************************************************
- * Performs Mass Erase of given flash bank
- * Syntax: psoc6 mass_erase bank_id
+/** ***********************************************************************************************
+ * @brief Performs Mass Erase operation
+ * @param bank flash bank index to erase
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
COMMAND_HANDLER(psoc6_handle_mass_erase_command)
{
@@ -835,13 +902,16 @@ COMMAND_HANDLER(psoc6_handle_mass_erase_command)
return hr;
}
-/**************************************************************************************************
- * Simulates broken Vector Catch
+/** ***********************************************************************************************
+ * @brief Simulates broken Vector Catch
* Function will try to determine entry point of user application. If it succeeds it will set HW
* breakpoint at that address, issue SW Reset and remove the breakpoint afterwards.
* In case of CM0, SYSRESETREQ is used. This allows to reset all peripherals. Boot code will
* reset CM4 anyway, so using SYSRESETREQ is safe here.
* In case of CM4, VECTRESET is used instead of SYSRESETREQ to not disturb CM0 core.
+ *
+ * @param target current target
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
*************************************************************************************************/
int handle_reset_halt(struct target *target)
{
@@ -889,33 +959,42 @@ int handle_reset_halt(struct target *target)
const struct armv7m_common *cm = target_to_armv7m(target);
+ /* PSoC6 reboots immediatelly after issuing SYSRESETREQ / VECTRESET
+ * this disables SWD/JTAG pins momentarily and may break communication
+ * Ignoring return value of mem_ap_write_atomic_u32 seems to be ok here */
if (is_cm0) {
/* Reset the CM0 by asserting SYSRESETREQ. This will also reset CM4 */
LOG_INFO("psoc6.cm0: bkpt @0x%08X, issuing SYSRESETREQ", reset_addr);
- hr = mem_ap_write_atomic_u32(cm->debug_ap,
- NVIC_AIRCR,
- AIRCR_VECTKEY | AIRCR_SYSRESETREQ);
-
- /* Wait for bootcode and initialize DAP */
- usleep(3000);
- dap_dp_init(cm->debug_ap->dap);
+ mem_ap_write_atomic_u32(cm->debug_ap, NVIC_AIRCR,
+ AIRCR_VECTKEY | AIRCR_SYSRESETREQ);
} else {
LOG_INFO("psoc6.cm4: bkpt @0x%08X, issuing VECTRESET", reset_addr);
- hr = mem_ap_write_atomic_u32(cm->debug_ap,
- NVIC_AIRCR,
- AIRCR_VECTKEY | AIRCR_VECTRESET);
- if (hr != ERROR_OK)
- return hr;
+ mem_ap_write_atomic_u32(cm->debug_ap, NVIC_AIRCR,
+ AIRCR_VECTKEY | AIRCR_VECTRESET);
}
+ /* Wait 100ms for bootcode and reinitialize DAP */
+ usleep(100000);
+ dap_dp_init(cm->debug_ap->dap);
+
target_wait_state(target, TARGET_HALTED, IPC_TIMEOUT_MS);
/* Remove the break point */
breakpoint_remove(target, reset_addr);
- return hr;
+ return ERROR_OK;
}
+/** ***********************************************************************************************
+ * @brief Simulates broken Vector Catch
+ * Function will try to determine entry point of user application. If it succeeds it will set HW
+ * breakpoint at that address, issue SW Reset and remove the breakpoint afterwards.
+ * In case of CM0, SYSRESETREQ is used. This allows to reset all peripherals. Boot code will
+ * reset CM4 anyway, so using SYSRESETREQ is safe here.
+ * In case of CM4, VECTRESET is used instead of SYSRESETREQ to not disturb CM0 core.
+ *
+ * @return ERROR_OK in case of success, ERROR_XXX code otherwise
+ *************************************************************************************************/
COMMAND_HANDLER(psoc6_handle_reset_halt)
{
if (CMD_ARGC)
@@ -945,7 +1024,7 @@ static const struct command_registration psoc6_exec_command_handlers[] = {
.name = "mass_erase",
.handler = psoc6_handle_mass_erase_command,
.mode = COMMAND_EXEC,
- .usage = NULL,
+ .usage = "bank",
.help = "Erases entire Main Flash",
},
{
diff --git a/src/flash/nor/stm32f1x.c b/src/flash/nor/stm32f1x.c
index 64c9168..015988a 100644
--- a/src/flash/nor/stm32f1x.c
+++ b/src/flash/nor/stm32f1x.c
@@ -572,45 +572,8 @@ static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer,
struct armv7m_algorithm armv7m_info;
int retval = ERROR_OK;
- /* see contrib/loaders/flash/stm32f1x.S for src */
-
static const uint8_t stm32x_flash_write_code[] = {
- /* #define STM32_FLASH_SR_OFFSET 0x0C */
- /* wait_fifo: */
- 0x16, 0x68, /* ldr r6, [r2, #0] */
- 0x00, 0x2e, /* cmp r6, #0 */
- 0x18, 0xd0, /* beq exit */
- 0x55, 0x68, /* ldr r5, [r2, #4] */
- 0xb5, 0x42, /* cmp r5, r6 */
- 0xf9, 0xd0, /* beq wait_fifo */
- 0x2e, 0x88, /* ldrh r6, [r5, #0] */
- 0x26, 0x80, /* strh r6, [r4, #0] */
- 0x02, 0x35, /* adds r5, #2 */
- 0x02, 0x34, /* adds r4, #2 */
- /* busy: */
- 0xc6, 0x68, /* ldr r6, [r0, #STM32_FLASH_SR_OFFSET] */
- 0x01, 0x27, /* movs r7, #1 */
- 0x3e, 0x42, /* tst r6, r7 */
- 0xfb, 0xd1, /* bne busy */
- 0x14, 0x27, /* movs r7, #0x14 */
- 0x3e, 0x42, /* tst r6, r7 */
- 0x08, 0xd1, /* bne error */
- 0x9d, 0x42, /* cmp r5, r3 */
- 0x01, 0xd3, /* bcc no_wrap */
- 0x15, 0x46, /* mov r5, r2 */
- 0x08, 0x35, /* adds r5, #8 */
- /* no_wrap: */
- 0x55, 0x60, /* str r5, [r2, #4] */
- 0x01, 0x39, /* subs r1, r1, #1 */
- 0x00, 0x29, /* cmp r1, #0 */
- 0x02, 0xd0, /* beq exit */
- 0xe5, 0xe7, /* b wait_fifo */
- /* error: */
- 0x00, 0x20, /* movs r0, #0 */
- 0x50, 0x60, /* str r0, [r2, #4] */
- /* exit: */
- 0x30, 0x46, /* mov r0, r6 */
- 0x00, 0xbe, /* bkpt #0 */
+#include "../../../contrib/loaders/flash/stm32/stm32f1x.inc"
};
/* flash write code */
diff --git a/src/flash/nor/stm32f2x.c b/src/flash/nor/stm32f2x.c
index 8bca62e..8013e58 100644
--- a/src/flash/nor/stm32f2x.c
+++ b/src/flash/nor/stm32f2x.c
@@ -584,45 +584,8 @@ static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer,
struct armv7m_algorithm armv7m_info;
int retval = ERROR_OK;
- /* see contrib/loaders/flash/stm32f2x.S for src */
-
static const uint8_t stm32x_flash_write_code[] = {
- /* wait_fifo: */
- 0xD0, 0xF8, 0x00, 0x80, /* ldr r8, [r0, #0] */
- 0xB8, 0xF1, 0x00, 0x0F, /* cmp r8, #0 */
- 0x1A, 0xD0, /* beq exit */
- 0x47, 0x68, /* ldr r7, [r0, #4] */
- 0x47, 0x45, /* cmp r7, r8 */
- 0xF7, 0xD0, /* beq wait_fifo */
-
- 0xDF, 0xF8, 0x34, 0x60, /* ldr r6, STM32_PROG16 */
- 0x26, 0x61, /* str r6, [r4, #STM32_FLASH_CR_OFFSET] */
- 0x37, 0xF8, 0x02, 0x6B, /* ldrh r6, [r7], #0x02 */
- 0x22, 0xF8, 0x02, 0x6B, /* strh r6, [r2], #0x02 */
- 0xBF, 0xF3, 0x4F, 0x8F, /* dsb sy */
- /* busy: */
- 0xE6, 0x68, /* ldr r6, [r4, #STM32_FLASH_SR_OFFSET] */
- 0x16, 0xF4, 0x80, 0x3F, /* tst r6, #0x10000 */
- 0xFB, 0xD1, /* bne busy */
- 0x16, 0xF0, 0xF0, 0x0F, /* tst r6, #0xf0 */
- 0x07, 0xD1, /* bne error */
-
- 0x8F, 0x42, /* cmp r7, r1 */
- 0x28, 0xBF, /* it cs */
- 0x00, 0xF1, 0x08, 0x07, /* addcs r7, r0, #8 */
- 0x47, 0x60, /* str r7, [r0, #4] */
- 0x01, 0x3B, /* subs r3, r3, #1 */
- 0x13, 0xB1, /* cbz r3, exit */
- 0xDF, 0xE7, /* b wait_fifo */
- /* error: */
- 0x00, 0x21, /* movs r1, #0 */
- 0x41, 0x60, /* str r1, [r0, #4] */
- /* exit: */
- 0x30, 0x46, /* mov r0, r6 */
- 0x00, 0xBE, /* bkpt #0x00 */
-
- /* <STM32_PROG16>: */
- 0x01, 0x01, 0x00, 0x00, /* .word 0x00000101 */
+#include "../../../contrib/loaders/flash/stm32/stm32f2x.inc"
};
if (target_alloc_working_area(target, sizeof(stm32x_flash_write_code),
diff --git a/src/flash/nor/stm32h7x.c b/src/flash/nor/stm32h7x.c
index a15cd25..009eb9b 100644
--- a/src/flash/nor/stm32h7x.c
+++ b/src/flash/nor/stm32h7x.c
@@ -568,51 +568,8 @@ static int stm32x_write_block(struct flash_bank *bank, const uint8_t *buffer,
struct stm32h7x_flash_bank *stm32x_info = bank->driver_priv;
int retval = ERROR_OK;
- /* see contrib/loaders/flash/smt32h7x.S for src */
static const uint8_t stm32x_flash_write_code[] = {
- /* <code>: */
- 0x45, 0x68, /* ldr r5, [r0, #4] */
- /* <wait_fifo>: */
- 0x06, 0x68, /* ldr r6, [r0, #0] */
- 0x26, 0xb3, /* cbz r6, <exit> */
- 0x76, 0x1b, /* subs r6, r6, r5 */
- 0x42, 0xbf, /* ittt mi */
- 0x76, 0x18, /* addmi r6, r6, r1 */
- 0x36, 0x1a, /* submi r6, r6, r0 */
- 0x08, 0x3e, /* submi r6, #8 */
- 0x20, 0x2e, /* cmp r6, #32 */
- 0xf6, 0xd3, /* bcc.n <wait_fifo> */
- 0x4f, 0xf0, 0x32, 0x06, /* mov.w r6, #STM32_PROG */
- 0xe6, 0x60, /* str r6, [r4, #STM32_FLASH_CR_OFFSET] */
- 0x4f, 0xf0, 0x08, 0x07, /* mov.w r7, #8 */
- /* <write_flash>: */
- 0x55, 0xf8, 0x04, 0x6b, /* ldr.w r6, [r5], #4 */
- 0x42, 0xf8, 0x04, 0x6b, /* str.w r6, [r2], #4 */
- 0xbf, 0xf3, 0x4f, 0x8f, /* dsb sy */
- 0x8d, 0x42, /* cmp r5, r1 */
- 0x28, 0xbf, /* it cs */
- 0x00, 0xf1, 0x08, 0x05, /* addcs.w r5, r0, #8 */
- 0x01, 0x3f, /* subs r7, #1 */
- 0xf3, 0xd1, /* bne.n <write_flash> */
- /* <busy>: */
- 0x26, 0x69, /* ldr r6, [r4, #STM32_FLASH_SR_OFFSET] */
- 0x16, 0xf0, 0x01, 0x0f, /* tst.w r6, #STM32_SR_BUSY_MASK */
- 0xfb, 0xd1, /* bne.n <busy> */
- 0x05, 0x4f, /* ldr r7, [pc, #20] ; (<stm32_sr_error_mask>) */
- 0x3e, 0x42, /* tst r6, r7 */
- 0x03, 0xd1, /* bne.n <error> */
- 0x45, 0x60, /* str r5, [r0, #4] */
- 0x01, 0x3b, /* subs r3, #1 */
- 0xdb, 0xd1, /* bne.n <wait_fifo> */
- 0x01, 0xe0, /* b.n <exit> */
- /* <error>: */
- 0x00, 0x27, /* movs r7, #0 */
- 0x47, 0x60, /* str r7, [r0, #4] */
- /* <exit>: */
- 0x30, 0x46, /* mov r0, r6 */
- 0x00, 0xbe, /* bkpt 0x0000 */
- /* <stm32_sr_error_mask>: */
- 0x00, 0x00, 0xee, 0x03 /* .word 0x03ee0000 ; (STM32_SR_ERROR_MASK) */
+#include "../../../contrib/loaders/flash/stm32/stm32h7x.inc"
};
if (target_alloc_working_area(target, sizeof(stm32x_flash_write_code),
diff --git a/src/flash/nor/stm32l4x.c b/src/flash/nor/stm32l4x.c
index e2710bd..e47313c 100644
--- a/src/flash/nor/stm32l4x.c
+++ b/src/flash/nor/stm32l4x.c
@@ -461,19 +461,8 @@ static int stm32l4_write_block(struct flash_bank *bank, const uint8_t *buffer,
struct armv7m_algorithm armv7m_info;
int retval = ERROR_OK;
- /* See contrib/loaders/flash/stm32l4x.S for source and
- * hints how to generate the data!
- */
-
static const uint8_t stm32l4_flash_write_code[] = {
- 0xd0, 0xf8, 0x00, 0x80, 0xb8, 0xf1, 0x00, 0x0f, 0x21, 0xd0, 0x45, 0x68,
- 0xb8, 0xeb, 0x05, 0x06, 0x44, 0xbf, 0x76, 0x18, 0x36, 0x1a, 0x08, 0x2e,
- 0xf2, 0xd3, 0xdf, 0xf8, 0x36, 0x60, 0x66, 0x61, 0xf5, 0xe8, 0x02, 0x67,
- 0xe2, 0xe8, 0x02, 0x67, 0xbf, 0xf3, 0x4f, 0x8f, 0x26, 0x69, 0x16, 0xf4,
- 0x80, 0x3f, 0xfb, 0xd1, 0x16, 0xf0, 0xfa, 0x0f, 0x07, 0xd1, 0x8d, 0x42,
- 0x28, 0xbf, 0x00, 0xf1, 0x08, 0x05, 0x45, 0x60, 0x01, 0x3b, 0x13, 0xb1,
- 0xda, 0xe7, 0x00, 0x21, 0x41, 0x60, 0x30, 0x46, 0x00, 0xbe, 0x01, 0x00,
- 0x00, 0x00
+#include "../../../contrib/loaders/flash/stm32/stm32l4x.inc"
};
if (target_alloc_working_area(target, sizeof(stm32l4_flash_write_code),
diff --git a/src/flash/nor/stm32lx.c b/src/flash/nor/stm32lx.c
index f4dd686..c68d7c2 100644
--- a/src/flash/nor/stm32lx.c
+++ b/src/flash/nor/stm32lx.c
@@ -448,10 +448,8 @@ static int stm32lx_write_half_pages(struct flash_bank *bank, const uint8_t *buff
int retval = ERROR_OK;
- /* see contib/loaders/flash/stm32lx.S for src */
-
static const uint8_t stm32lx_flash_write_code[] = {
- 0x92, 0x00, 0x8A, 0x18, 0x01, 0xE0, 0x08, 0xC9, 0x08, 0xC0, 0x91, 0x42, 0xFB, 0xD1, 0x00, 0xBE
+#include "../../../contrib/loaders/flash/stm32/stm32lx.inc"
};
/* Make sure we're performing a half-page aligned write. */
diff --git a/src/flash/nor/tcl.c b/src/flash/nor/tcl.c
index e5e2801..34681db 100644
--- a/src/flash/nor/tcl.c
+++ b/src/flash/nor/tcl.c
@@ -3,6 +3,7 @@
* Copyright (C) 2007,2008 Øyvind Harboe <oyvind.harboe@zylin.com> *
* Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
* Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
+ * Copyright (C) 2017-2018 Tomas Vanek <vanekt@fbl.cz> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
@@ -460,42 +461,29 @@ COMMAND_HANDLER(handle_flash_write_image_command)
COMMAND_HANDLER(handle_flash_fill_command)
{
- int err = ERROR_OK;
- uint32_t address;
+ target_addr_t address;
uint32_t pattern;
uint32_t count;
- uint32_t wrote = 0;
- uint32_t cur_size = 0;
- uint32_t chunk_count;
struct target *target = get_current_target(CMD_CTX);
unsigned i;
uint32_t wordsize;
- int retval = ERROR_OK;
-
- static size_t const chunksize = 1024;
- uint8_t *chunk = NULL, *readback = NULL;
+ int retval;
- if (CMD_ARGC != 3) {
- retval = ERROR_COMMAND_SYNTAX_ERROR;
- goto done;
- }
+ if (CMD_ARGC != 3)
+ return ERROR_COMMAND_SYNTAX_ERROR;
+#if BUILD_TARGET64
+ COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], address);
+#else
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
+#endif
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
- chunk = malloc(chunksize);
- if (chunk == NULL)
- return ERROR_FAIL;
-
- readback = malloc(chunksize);
- if (readback == NULL) {
- free(chunk);
- return ERROR_FAIL;
- }
-
- if (count == 0)
- goto done;
+ struct flash_bank *bank;
+ retval = get_flash_bank_by_addr(target, address, true, &bank);
+ if (retval != ERROR_OK)
+ return retval;
switch (CMD_NAME[4]) {
case 'w':
@@ -508,73 +496,109 @@ COMMAND_HANDLER(handle_flash_fill_command)
wordsize = 1;
break;
default:
- retval = ERROR_COMMAND_SYNTAX_ERROR;
- goto done;
+ return ERROR_COMMAND_SYNTAX_ERROR;
+ }
+
+ if (count == 0)
+ return ERROR_OK;
+
+ if (address + count >= bank->base + bank->size) {
+ LOG_ERROR("Cannot cross flash bank borders");
+ return ERROR_FAIL;
+ }
+
+ uint32_t size_bytes = count * wordsize;
+ target_addr_t aligned_start = flash_write_align_start(bank, address);
+ target_addr_t end_addr = address + size_bytes - 1;
+ target_addr_t aligned_end = flash_write_align_end(bank, end_addr);
+ uint32_t aligned_size = aligned_end + 1 - aligned_start;
+ uint32_t padding_at_start = address - aligned_start;
+ uint32_t padding_at_end = aligned_end - end_addr;
+
+ uint8_t *buffer = malloc(aligned_size);
+ if (buffer == NULL)
+ return ERROR_FAIL;
+
+ if (padding_at_start) {
+ memset(buffer, bank->default_padded_value, padding_at_start);
+ LOG_WARNING("Start address " TARGET_ADDR_FMT
+ " breaks the required alignment of flash bank %s",
+ address, bank->name);
+ LOG_WARNING("Padding %" PRId32 " bytes from " TARGET_ADDR_FMT,
+ padding_at_start, aligned_start);
}
- chunk_count = MIN(count, (chunksize / wordsize));
+ uint8_t *ptr = buffer + padding_at_start;
+
switch (wordsize) {
case 4:
- for (i = 0; i < chunk_count; i++)
- target_buffer_set_u32(target, chunk + i * wordsize, pattern);
+ for (i = 0; i < count; i++, ptr += wordsize)
+ target_buffer_set_u32(target, ptr, pattern);
break;
case 2:
- for (i = 0; i < chunk_count; i++)
- target_buffer_set_u16(target, chunk + i * wordsize, pattern);
+ for (i = 0; i < count; i++, ptr += wordsize)
+ target_buffer_set_u16(target, ptr, pattern);
break;
case 1:
- memset(chunk, pattern, chunk_count);
+ memset(ptr, pattern, count);
+ ptr += count;
break;
default:
LOG_ERROR("BUG: can't happen");
exit(-1);
}
+ if (padding_at_end) {
+ memset(ptr, bank->default_padded_value, padding_at_end);
+ LOG_INFO("Padding at " TARGET_ADDR_FMT " with %" PRId32
+ " bytes (bank write end alignment)",
+ end_addr + 1, padding_at_end);
+ }
+
struct duration bench;
duration_start(&bench);
- for (wrote = 0; wrote < (count*wordsize); wrote += cur_size) {
- struct flash_bank *bank;
+ retval = flash_driver_write(bank, buffer, aligned_start - bank->base, aligned_size);
+ if (retval != ERROR_OK)
+ goto done;
- retval = get_flash_bank_by_addr(target, address, true, &bank);
- if (retval != ERROR_OK)
- goto done;
+ retval = flash_driver_read(bank, buffer, address - bank->base, size_bytes);
+ if (retval != ERROR_OK)
+ goto done;
- cur_size = MIN((count * wordsize - wrote), chunksize);
- err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
- if (err != ERROR_OK) {
- retval = err;
- goto done;
- }
+ for (i = 0, ptr = buffer; i < count; i++) {
+ uint32_t readback = 0;
- err = flash_driver_read(bank, readback, address - bank->base + wrote, cur_size);
- if (err != ERROR_OK) {
- retval = err;
- goto done;
+ switch (wordsize) {
+ case 4:
+ readback = target_buffer_get_u32(target, ptr);
+ break;
+ case 2:
+ readback = target_buffer_get_u16(target, ptr);
+ break;
+ case 1:
+ readback = *ptr;
+ break;
}
-
- for (i = 0; i < cur_size; i++) {
- if (readback[i] != chunk[i]) {
- LOG_ERROR(
- "Verification error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
- address + wrote + i,
- readback[i],
- chunk[i]);
- retval = ERROR_FAIL;
- goto done;
- }
+ if (readback != pattern) {
+ LOG_ERROR(
+ "Verification error address " TARGET_ADDR_FMT
+ ", read back 0x%02" PRIx32 ", expected 0x%02" PRIx32,
+ address + i * wordsize, readback, pattern);
+ retval = ERROR_FAIL;
+ goto done;
}
+ ptr += wordsize;
}
if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
- command_print(CMD_CTX, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32
- " in %fs (%0.3f KiB/s)", wrote, address,
- duration_elapsed(&bench), duration_kbps(&bench, wrote));
+ command_print(CMD_CTX, "wrote %" PRIu32 " bytes to " TARGET_ADDR_FMT
+ " in %fs (%0.3f KiB/s)", size_bytes, address,
+ duration_elapsed(&bench), duration_kbps(&bench, size_bytes));
}
done:
- free(readback);
- free(chunk);
+ free(buffer);
return retval;
}
@@ -592,8 +616,8 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
struct duration bench;
duration_start(&bench);
- struct flash_bank *p;
- int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
+ struct flash_bank *bank;
+ int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (ERROR_OK != retval)
return retval;
@@ -602,7 +626,7 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
if (CMD_ARGC > 2)
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
- if (offset > p->size) {
+ if (offset > bank->size) {
LOG_ERROR("Offset 0x%8.8" PRIx32 " is out of range of the flash bank",
offset);
return ERROR_COMMAND_ARGUMENT_INVALID;
@@ -618,7 +642,7 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
return retval;
}
- length = MIN(filesize, p->size - offset);
+ length = MIN(filesize, bank->size - offset);
if (!length) {
LOG_INFO("Nothing to write to flash bank");
@@ -630,14 +654,33 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
LOG_INFO("File content exceeds flash bank size. Only writing the "
"first %zu bytes of the file", length);
- buffer = malloc(length);
+ target_addr_t start_addr = bank->base + offset;
+ target_addr_t aligned_start = flash_write_align_start(bank, start_addr);
+ target_addr_t end_addr = start_addr + length - 1;
+ target_addr_t aligned_end = flash_write_align_end(bank, end_addr);
+ uint32_t aligned_size = aligned_end + 1 - aligned_start;
+ uint32_t padding_at_start = start_addr - aligned_start;
+ uint32_t padding_at_end = aligned_end - end_addr;
+
+ buffer = malloc(aligned_size);
if (buffer == NULL) {
fileio_close(fileio);
LOG_ERROR("Out of memory");
return ERROR_FAIL;
}
+
+ if (padding_at_start) {
+ memset(buffer, bank->default_padded_value, padding_at_start);
+ LOG_WARNING("Start offset 0x%08" PRIx32
+ " breaks the required alignment of flash bank %s",
+ offset, bank->name);
+ LOG_WARNING("Padding %" PRId32 " bytes from " TARGET_ADDR_FMT,
+ padding_at_start, aligned_start);
+ }
+
+ uint8_t *ptr = buffer + padding_at_start;
size_t buf_cnt;
- if (fileio_read(fileio, length, buffer, &buf_cnt) != ERROR_OK) {
+ if (fileio_read(fileio, length, ptr, &buf_cnt) != ERROR_OK) {
free(buffer);
fileio_close(fileio);
return ERROR_FAIL;
@@ -649,15 +692,23 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
return ERROR_FAIL;
}
- retval = flash_driver_write(p, buffer, offset, length);
+ ptr += length;
+
+ if (padding_at_end) {
+ memset(ptr, bank->default_padded_value, padding_at_end);
+ LOG_INFO("Padding at " TARGET_ADDR_FMT " with %" PRId32
+ " bytes (bank write end alignment)",
+ end_addr + 1, padding_at_end);
+ }
+
+ retval = flash_driver_write(bank, buffer, aligned_start - bank->base, aligned_size);
free(buffer);
- buffer = NULL;
if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
command_print(CMD_CTX, "wrote %zu bytes from file %s to flash bank %u"
" at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
- length, CMD_ARGV[1], p->bank_number, offset,
+ length, CMD_ARGV[1], bank->bank_number, offset,
duration_elapsed(&bench), duration_kbps(&bench, length));
}
@@ -1071,21 +1122,16 @@ COMMAND_HANDLER(handle_flash_bank_command)
}
}
- struct flash_bank *c = malloc(sizeof(*c));
+ struct flash_bank *c = calloc(1, sizeof(*c));
c->name = strdup(bank_name);
c->target = target;
c->driver = driver;
- c->driver_priv = NULL;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], c->base);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
c->default_padded_value = c->erased_value = 0xff;
- c->num_sectors = 0;
- c->sectors = NULL;
- c->num_prot_blocks = 0;
- c->prot_blocks = NULL;
- c->next = NULL;
+ c->minimal_write_gap = FLASH_WRITE_GAP_SECTOR;
int retval;
retval = CALL_COMMAND_HANDLER(driver->flash_bank_command, c);