34 retval =
bank->driver->erase(
bank, first, last);
36 LOG_ERROR(
"failed erasing sectors %u to %u", first, last);
45 unsigned int num_blocks;
47 if (
bank->num_prot_blocks)
48 num_blocks =
bank->num_prot_blocks;
50 num_blocks =
bank->num_sectors;
54 if (first > last || last >= num_blocks) {
55 LOG_ERROR(
"illegal protection block range");
62 if (!
bank->driver->protect) {
63 LOG_ERROR(
"Flash protection is not supported.");
78 retval =
bank->driver->protect(
bank, set, first, last);
80 LOG_ERROR(
"failed setting protection for blocks %u to %u", first, last);
94 " at offset 0x%8.8" PRIx32,
113 " at offset 0x%8.8" PRIx32,
145 uint32_t target_crc, image_crc;
158 if (target_crc == image_crc)
167 unsigned int bank_num = 0;
180 bank->bank_number = bank_num;
197 LOG_ERROR(
"flash bank %d does not exist", num);
212 free(
bank->driver_priv);
221 if (
bank->driver->free_driver_priv)
222 bank->driver->free_driver_priv(
bank);
224 LOG_WARNING(
"Flash driver of %s does not support free_driver_priv()",
bank->name);
231 if (strcmp(
bank->driver->name,
"virtual") != 0) {
233 free(
bank->prot_blocks);
246 unsigned int found = 0;
254 if (++found < requested)
268 retval =
bank->driver->auto_probe(
bank);
336 const int buffer_size = 1024;
345 uint8_t *
buffer = malloc(buffer_size);
347 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
349 bank->sectors[i].is_erased = 1;
351 for (j = 0; j <
bank->sectors[i].size; j += buffer_size) {
354 if (chunk > (
bank->sectors[i].size - j))
355 chunk = (
bank->sectors[i].size - j);
358 bank->base +
bank->sectors[i].offset + j,
365 for (n_bytes = 0; n_bytes < chunk; n_bytes++) {
367 bank->sectors[i].is_erased = 0;
395 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
397 block_array[i].
size =
bank->sectors[i].size;
398 block_array[i].
result = UINT32_MAX;
401 bool fast_check =
true;
402 for (
unsigned int i = 0; i <
bank->num_sectors; ) {
404 block_array + i,
bank->num_sectors - i,
417 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
418 bank->sectors[i].is_erased = block_array[i].
result;
422 LOG_USER(
"Running slow fallback erase check");
424 LOG_USER(
"Running slow fallback erase check - add working memory");
450 bool iterate_protect_blocks,
474 LOG_ERROR(
"Whole bank access must start at beginning of bank.");
482 if (last_addr > c->
base + c->
size - 1) {
483 LOG_ERROR(
"Flash access does not fit into bank.");
489 iterate_protect_blocks =
false;
492 if (iterate_protect_blocks) {
500 for (i = 0; i < num_blocks; i++) {
508 if (
addr < sector_addr)
512 if (
addr == sector_addr)
522 else if (
addr <= sector_last_addr && pad_reason) {
535 if (last_addr == sector_last_addr) {
543 if (last_addr < sector_last_addr && pad_reason) {
555 if (last_addr < sector_addr)
560 if (first == -1 || last == -1) {
562 " is not sector-aligned",
572 return callback(c, first, last);
580 bool iterate_protect_blocks,
593 uint32_t cur_length =
length;
596 LOG_DEBUG(
"iterating over more than one flash bank.");
600 pad_reason,
addr, cur_length,
601 iterate_protect_blocks,
656 if (addr < bank->base ||
addr >=
bank->base +
bank->size
657 ||
bank->write_start_alignment <= 1)
662 uint32_t aligned = 0;
663 for (
unsigned int sect = 0; sect <
bank->num_sectors; sect++) {
667 aligned =
bank->sectors[sect].offset;
669 return bank->base + aligned;
672 return addr & ~(
bank->write_start_alignment - 1);
680 if (addr < bank->base ||
addr >=
bank->base +
bank->size
681 ||
bank->write_end_alignment <= 1)
686 uint32_t aligned = 0;
687 for (
unsigned int sect = 0; sect <
bank->num_sectors; sect++) {
688 aligned =
bank->sectors[sect].offset +
bank->sectors[sect].size - 1;
692 return bank->base + aligned;
695 return addr | (
bank->write_end_alignment - 1);
705 || addr1 < bank->base || addr1 >=
bank->base +
bank->size
706 || addr2 < bank->base || addr2 >=
bank->base +
bank->size)
711 uint32_t offset1 = addr1 -
bank->base;
713 for (sect = 0; sect <
bank->num_sectors; sect++) {
714 if (
bank->sectors[sect].offset > offset1)
717 if (sect >=
bank->num_sectors)
720 uint32_t offset2 = addr2 -
bank->base;
721 return bank->sectors[sect].offset +
bank->sectors[sect].size <= offset2;
726 return aligned1 +
bank->minimal_write_gap < aligned2;
731 uint32_t *written,
bool erase,
bool unlock,
bool write,
bool verify)
735 unsigned int section;
736 uint32_t section_offset;
768 while (section < image->num_sections) {
771 unsigned int section_last;
773 uint32_t run_size = sections[section]->
size - section_offset;
776 if (sections[section]->
size == 0) {
795 section_last = section;
796 padding[section] = 0;
797 while ((run_address + run_size - 1 < c->
base + c->
size - 1) &&
811 if (next_section_base < run_next_addr) {
814 next_section_base, run_next_addr);
820 pad_bytes = next_section_base - run_next_addr;
825 run_next_addr, next_section_base);
832 section_last, run_next_addr, pad_bytes);
834 padding[section_last] = pad_bytes;
835 run_size += pad_bytes;
836 run_size += sections[++section_last]->
size;
839 if (run_address + run_size - 1 > c->
base + c->
size - 1) {
843 LOG_DEBUG(
"Truncate flash run size to the current flash chip.");
845 run_size = c->
base + c->
size - run_address;
846 assert(run_size > 0);
849 uint32_t padding_at_start = 0;
853 padding_at_start = run_address - aligned_start;
854 if (padding_at_start > 0) {
856 " breaks the required alignment of flash bank %s",
857 run_address, c->
name);
859 padding_at_start, aligned_start);
861 run_address -= padding_at_start;
862 run_size += padding_at_start;
867 pad_bytes = aligned_end - run_end;
870 " with %d bytes (bank write end alignment)",
871 section_last, run_end + 1, pad_bytes);
873 padding[section_last] += pad_bytes;
874 run_size += pad_bytes;
877 }
else if (unlock || erase) {
881 uint32_t offset_start = run_address - c->
base;
882 uint32_t offset_end = offset_start + run_size;
883 uint32_t end = offset_end, delta;
885 for (
unsigned int sector = 0; sector < c->
num_sectors; sector++) {
888 if (offset_end <= end)
892 delta = end - offset_end;
893 padding[section_last] += delta;
898 buffer = malloc(run_size);
900 LOG_ERROR(
"Out of memory for flash bank buffer");
905 if (padding_at_start)
908 buffer_idx = padding_at_start;
911 while (buffer_idx < run_size) {
914 size_read = run_size - buffer_idx;
915 if (size_read > sections[section]->
size - section_offset)
916 size_read = sections[section]->
size - section_offset;
923 intptr_t diff = (intptr_t)sections[section] - (intptr_t)
image->
sections;
926 LOG_DEBUG(
"image_read_section: section = %d, t_section_num = %d, "
927 "section_offset = %"PRIu32
", buffer_idx = %"PRIu32
", size_read = %zu",
928 section, t_section_num, section_offset,
929 buffer_idx, size_read);
931 size_read,
buffer + buffer_idx, &size_read);
932 if (retval !=
ERROR_OK || size_read == 0) {
937 buffer_idx += size_read;
938 section_offset += size_read;
941 if (padding[section]) {
943 buffer_idx += padding[section];
946 if (section_offset >= sections[section]->
size) {
960 true, run_address, run_size);
986 *written += run_size;
997 uint32_t *written,
bool erase)
1003 unsigned int num_blocks)
1009 for (
unsigned int i = 0; i < num_blocks; i++) {
bool flash_driver_name_matches(const char *name, const char *expected)
Attempt to match the expected name with the name of a driver.
unsigned int get_flash_name_index(const char *name)
Parses the optional '.index' portion of a flash bank identifier.
#define ERROR_FLASH_OPER_UNSUPPORTED
#define ERROR_FLASH_BANK_INVALID
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
target_addr_t flash_write_align_start(struct flash_bank *bank, target_addr_t addr)
Get aligned start address of a flash write region.
int default_flash_verify(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default verify implementation for flash memory.
int flash_driver_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
int flash_driver_verify(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
struct flash_bank * get_flash_bank_by_name_noprobe(const char *name)
Returns the flash bank specified by name, which matches the driver name and a suffix (option) specify...
static int flash_driver_unprotect(struct flash_bank *bank, unsigned int first, unsigned int last)
static int flash_iterate_address_range(struct target *target, char *pad_reason, target_addr_t addr, uint32_t length, bool iterate_protect_blocks, int(*callback)(struct flash_bank *bank, unsigned int first, unsigned int last))
struct flash_sector * alloc_block_array(uint32_t offset, uint32_t size, unsigned int num_blocks)
Allocate and fill an array of sectors or protection blocks.
target_addr_t flash_write_align_end(struct flash_bank *bank, target_addr_t addr)
Get aligned end address of a flash write region.
static bool flash_write_check_gap(struct flash_bank *bank, target_addr_t addr1, target_addr_t addr2)
Check if gap between sections is bigger than minimum required to discontinue flash write.
int flash_driver_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
static int compare_section(const void *a, const void *b)
int flash_driver_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
void flash_bank_add(struct flash_bank *bank)
Adds a new NOR bank to the global list of banks.
int flash_unlock_address_range(struct target *target, target_addr_t addr, uint32_t length)
struct flash_bank * get_flash_bank_by_num_noprobe(unsigned int num)
Returns the flash bank like get_flash_bank_by_num(), without probing.
static int flash_iterate_address_range_inner(struct target *target, char *pad_reason, target_addr_t addr, uint32_t length, bool iterate_protect_blocks, int(*callback)(struct flash_bank *bank, unsigned int first, unsigned int last))
static int default_flash_mem_blank_check(struct flash_bank *bank)
static struct flash_bank * flash_banks
void flash_free_all_banks(void)
Deallocates all flash banks.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
unsigned int flash_get_bank_count(void)
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
int get_flash_bank_by_addr(struct target *target, target_addr_t addr, bool check, struct flash_bank **result_bank)
Returns the flash bank located at a specified address.
int flash_erase_address_range(struct target *target, bool pad, target_addr_t addr, uint32_t length)
Erases length bytes in the target flash, starting at addr.
int flash_write(struct target *target, struct image *image, uint32_t *written, bool erase)
Writes image into the target flash.
int flash_driver_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
Returns the flash bank specified by name, which matches the driver name and a suffix (option) specify...
struct flash_bank * flash_bank_list(void)
int get_flash_bank_by_num(unsigned int num, struct flash_bank **bank)
Returns the flash bank like get_flash_bank_by_name(), without probing.
int flash_write_unlock_verify(struct target *target, struct image *image, uint32_t *written, bool erase, bool unlock, bool write, bool verify)
int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
int image_calculate_checksum(const uint8_t *buffer, uint32_t nbytes, uint32_t *checksum)
#define LOG_USER(expr ...)
#define ERROR_NOT_IMPLEMENTED
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
Upper level NOR flash interfaces.
#define FLASH_WRITE_GAP_SECTOR
#define FLASH_WRITE_ALIGN_SECTOR
Special value for write_start_alignment and write_end_alignment field.
void flash_set_dirty(void)
Forces targets to re-examine their erase/protection state.
#define FLASH_WRITE_CONTINUOUS
Special values for minimal_write_gap field.
target_addr_t addr
Start address to search for the control block.
size_t size
Size of the control block search area.
Provides details of a flash bank, available either on-chip or through a major interface.
uint32_t write_end_alignment
Required alignment of flash write end address.
unsigned int num_prot_blocks
The number of protection blocks in this bank.
struct flash_sector * sectors
Array of sectors, allocated and initialized by the flash driver.
uint8_t default_padded_value
Default padded value used, normally this matches the flash erased value.
const struct flash_driver * driver
Driver for this bank.
target_addr_t base
The base address of this bank.
uint32_t size
The size of this chip bank, in bytes.
unsigned int num_sectors
The number of sectors on this chip.
uint32_t write_start_alignment
Required alignment of flash write start address.
struct flash_sector * prot_blocks
Array of protection blocks, allocated and initialized by the flash driver.
struct flash_bank * next
The next flash bank on this chip.
struct target * target
Target to which this bank belongs.
int(* auto_probe)(struct flash_bank *bank)
A more gentle flavor of flash_driver::probe, performing setup with less noise.
Describes the geometry and status of a single flash sector within a flash bank.
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
uint32_t offset
Bus offset from start of the flash chip (in bytes).
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown.
uint32_t size
Number of bytes in this flash sector.
unsigned int num_sections
struct imagesection * sections
target_addr_t base_address
int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
int target_blank_check_memory(struct target *target, struct target_memory_check_block *blocks, int num_blocks, uint8_t erased_value)
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
#define ERROR_TARGET_NOT_HALTED