32 if (!
bank->driver->erase) {
37 return bank->driver->erase(
bank, first, last);
44 unsigned int num_blocks;
46 if (!
bank->driver->protect) {
47 LOG_ERROR(
"Flash protection is not supported");
51 if (
bank->num_prot_blocks)
52 num_blocks =
bank->num_prot_blocks;
54 num_blocks =
bank->num_sectors;
58 if (first > last || last >= num_blocks) {
59 LOG_ERROR(
"illegal protection block range");
77 retval =
bank->driver->protect(
bank, set, first, last);
79 LOG_ERROR(
"failed setting protection for blocks %u to %u", first, last);
93 " at offset 0x%8.8" PRIx32,
108 if (
bank->driver->read)
115 " at offset 0x%8.8" PRIx32,
134 if (
bank->driver->verify)
149 uint32_t target_crc, image_crc;
162 if (target_crc == image_crc)
171 unsigned int bank_num = 0;
184 bank->bank_number = bank_num;
201 LOG_ERROR(
"flash bank %d does not exist", num);
216 free(
bank->driver_priv);
225 if (
bank->driver->free_driver_priv)
226 bank->driver->free_driver_priv(
bank);
231 free(
bank->prot_blocks);
243 unsigned int found = 0;
251 if (++found < requested)
265 retval =
bank->driver->auto_probe(
bank);
346 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
347 bank->sectors[i].is_erased = 1;
352 if (chunk > (
bank->sectors[i].size - j))
353 chunk = (
bank->sectors[i].size - j);
356 bank->base +
bank->sectors[i].offset + j,
364 LOG_WARNING(
"Failed to check erase state on sector %u, "
365 "continuing on next sector", i);
366 bank->sectors[i].is_erased = -1;
370 for (uint32_t n_bytes = 0; n_bytes < chunk; n_bytes++) {
372 bank->sectors[i].is_erased = 0;
377 if (
bank->sectors[i].is_erased == 0)
394 check_block.
result = UINT32_MAX;
396 unsigned int checked;
398 bank->erased_value, &checked);
423 LOG_WARNING(
"Fast erase check failed, running slow fallback");
425 bank->is_erased = -1;
436 for (uint32_t i = 0; i < chunk_size; i++) {
459 if (
bank->num_sectors == 0)
465 LOG_WARNING(
"Running slow fallback erase check - limited host memory available");
469 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
471 block_array[i].
size =
bank->sectors[i].size;
472 block_array[i].
result = UINT32_MAX;
475 bool fast_check =
true;
476 for (
unsigned int i = 0; i <
bank->num_sectors; ) {
477 unsigned int checked;
479 block_array + i,
bank->num_sectors - i,
480 bank->erased_value, &checked);
492 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
493 bank->sectors[i].is_erased = block_array[i].
result;
497 LOG_DEBUG(
"Running slow fallback erase check");
499 LOG_WARNING(
"Running slow fallback erase check - add working memory");
525 bool iterate_protect_blocks,
552 LOG_ERROR(
"Whole bank access must start at beginning of bank.");
560 if (last_addr > c->
base + c->
size - 1) {
561 LOG_ERROR(
"Flash access does not fit into bank.");
567 iterate_protect_blocks =
false;
570 if (iterate_protect_blocks) {
578 for (i = 0; i < num_blocks; i++) {
586 if (
addr < sector_addr)
590 if (
addr == sector_addr)
600 else if (
addr <= sector_last_addr && pad_reason) {
613 if (last_addr == sector_last_addr) {
621 if (last_addr < sector_last_addr && pad_reason) {
633 if (last_addr < sector_addr)
638 if (first == -1 || last == -1) {
640 " is not sector-aligned",
650 return callback(c, first, last);
658 bool iterate_protect_blocks,
bool skip_banks_without_erase,
671 uint32_t cur_length =
length;
674 LOG_DEBUG(
"iterating over more than one flash bank.");
677 if (skip_banks_without_erase && !c->
driver->
erase) {
678 LOG_DEBUG(
"bank %s does not support erase, skipped",
682 pad_reason,
addr, cur_length,
683 iterate_protect_blocks,
742 if (addr < bank->base ||
addr >=
bank->base +
bank->size
743 ||
bank->write_start_alignment <= 1)
748 uint32_t aligned = 0;
749 for (
unsigned int sect = 0; sect <
bank->num_sectors; sect++) {
753 aligned =
bank->sectors[sect].offset;
755 return bank->base + aligned;
758 return addr & ~(
bank->write_start_alignment - 1);
766 if (addr < bank->base ||
addr >=
bank->base +
bank->size
767 ||
bank->write_end_alignment <= 1)
772 uint32_t aligned = 0;
773 for (
unsigned int sect = 0; sect <
bank->num_sectors; sect++) {
774 aligned =
bank->sectors[sect].offset +
bank->sectors[sect].size - 1;
778 return bank->base + aligned;
781 return addr | (
bank->write_end_alignment - 1);
791 || addr1 < bank->base || addr1 >=
bank->base +
bank->size
792 || addr2 < bank->base || addr2 >=
bank->base +
bank->size)
797 uint32_t offset1 = addr1 -
bank->base;
799 for (sect = 0; sect <
bank->num_sectors; sect++) {
800 if (
bank->sectors[sect].offset > offset1)
803 if (sect >=
bank->num_sectors)
806 uint32_t offset2 = addr2 -
bank->base;
807 return bank->sectors[sect].offset +
bank->sectors[sect].size <= offset2;
812 return aligned1 +
bank->minimal_write_gap < aligned2;
817 uint32_t *written,
bool erase,
bool unlock,
bool write,
bool verify)
821 unsigned int section;
822 uint32_t section_offset;
836 if (!padding || !sections) {
860 while (section < image->num_sections) {
863 unsigned int section_last;
865 uint32_t run_size = sections[section]->
size - section_offset;
868 if (sections[section]->
size == 0) {
887 section_last = section;
888 padding[section] = 0;
889 while ((run_address + run_size - 1 < c->
base + c->
size - 1) &&
903 if (next_section_base < run_next_addr) {
906 next_section_base, run_next_addr);
912 pad_bytes = next_section_base - run_next_addr;
917 run_next_addr, next_section_base);
924 section_last, run_next_addr, pad_bytes);
926 padding[section_last] = pad_bytes;
927 run_size += pad_bytes;
928 run_size += sections[++section_last]->
size;
931 if (run_address + run_size - 1 > c->
base + c->
size - 1) {
935 LOG_DEBUG(
"Truncate flash run size to the current flash chip.");
937 run_size = c->
base + c->
size - run_address;
938 assert(run_size > 0);
941 uint32_t padding_at_start = 0;
945 padding_at_start = run_address - aligned_start;
946 if (padding_at_start > 0) {
948 " breaks the required alignment of flash bank %s",
949 run_address, c->
name);
951 padding_at_start, aligned_start);
953 run_address -= padding_at_start;
954 run_size += padding_at_start;
959 pad_bytes = aligned_end - run_end;
962 " with %d bytes (bank write end alignment)",
963 section_last, run_end + 1, pad_bytes);
965 padding[section_last] += pad_bytes;
966 run_size += pad_bytes;
969 }
else if (unlock || erase) {
973 uint32_t offset_start = run_address - c->
base;
974 uint32_t offset_end = offset_start + run_size;
975 uint32_t end = offset_end, delta;
977 for (
unsigned int sector = 0; sector < c->
num_sectors; sector++) {
980 if (offset_end <= end)
984 delta = end - offset_end;
985 padding[section_last] += delta;
990 buffer = malloc(run_size);
992 LOG_ERROR(
"Out of memory for flash bank buffer");
997 if (padding_at_start)
1000 buffer_idx = padding_at_start;
1003 while (buffer_idx < run_size) {
1006 size_read = run_size - buffer_idx;
1007 if (size_read > sections[section]->
size - section_offset)
1008 size_read = sections[section]->
size - section_offset;
1015 intptr_t diff = (intptr_t)sections[section] - (intptr_t)
image->
sections;
1016 int t_section_num = diff /
sizeof(
struct imagesection);
1018 LOG_DEBUG(
"image_read_section: section = %d, t_section_num = %d, "
1019 "section_offset = %"PRIu32
", buffer_idx = %"PRIu32
", size_read = %zu",
1020 section, t_section_num, section_offset,
1021 buffer_idx, size_read);
1023 size_read,
buffer + buffer_idx, &size_read);
1024 if (retval !=
ERROR_OK || size_read == 0) {
1029 buffer_idx += size_read;
1030 section_offset += size_read;
1033 if (padding[section]) {
1035 buffer_idx += padding[section];
1038 if (section_offset >= sections[section]->
size) {
1053 run_address, run_size,
1080 *written += run_size;
1091 uint32_t *written,
bool erase)
1097 unsigned int num_blocks)
1103 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.
uint64_t buffer
Pointer to data buffer to send over SPI.
uint32_t size
Size of dw_spi_transaction::buffer.
uint32_t buffer_size
Size of dw_spi_program::buffer.
#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, bool skip_banks_without_erase, 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)
static int flash_blank_check_no_sectors(struct flash_bank *bank)
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 int flash_blank_check_no_sectors_fast(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_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_erase_address_range(struct target *target, bool pad, target_addr_t addr, uint32_t length, bool skip_banks_without_erase)
Erases length bytes in the target flash, starting at addr.
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 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.
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.
int(* erase)(struct flash_bank *bank, unsigned int first, unsigned int last)
Bank/sector erase routine (target-specific).
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, unsigned int num_blocks, uint8_t erased_value, unsigned int *checked)
#define ERROR_TARGET_NOT_HALTED