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->driver->protect) {
48 LOG_ERROR(
"Flash protection is not supported");
52 if (
bank->num_prot_blocks)
53 num_blocks =
bank->num_prot_blocks;
55 num_blocks =
bank->num_sectors;
59 if (first > last || last >= num_blocks) {
60 LOG_ERROR(
"illegal protection block range");
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);
227 free(
bank->prot_blocks);
239 unsigned int found = 0;
247 if (++found < requested)
261 retval =
bank->driver->auto_probe(
bank);
340 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
342 bank->sectors[i].is_erased = 1;
347 if (chunk > (
bank->sectors[i].size - j))
348 chunk = (
bank->sectors[i].size - j);
351 bank->base +
bank->sectors[i].offset + j,
358 for (n_bytes = 0; n_bytes < chunk; n_bytes++) {
360 bank->sectors[i].is_erased = 0;
388 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
390 block_array[i].
size =
bank->sectors[i].size;
391 block_array[i].
result = UINT32_MAX;
394 bool fast_check =
true;
395 for (
unsigned int i = 0; i <
bank->num_sectors; ) {
396 unsigned int checked;
398 block_array + i,
bank->num_sectors - i,
399 bank->erased_value, &checked);
411 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
412 bank->sectors[i].is_erased = block_array[i].
result;
416 LOG_USER(
"Running slow fallback erase check");
418 LOG_USER(
"Running slow fallback erase check - add working memory");
444 bool iterate_protect_blocks,
468 LOG_ERROR(
"Whole bank access must start at beginning of bank.");
476 if (last_addr > c->
base + c->
size - 1) {
477 LOG_ERROR(
"Flash access does not fit into bank.");
483 iterate_protect_blocks =
false;
486 if (iterate_protect_blocks) {
494 for (i = 0; i < num_blocks; i++) {
502 if (
addr < sector_addr)
506 if (
addr == sector_addr)
516 else if (
addr <= sector_last_addr && pad_reason) {
529 if (last_addr == sector_last_addr) {
537 if (last_addr < sector_last_addr && pad_reason) {
549 if (last_addr < sector_addr)
554 if (first == -1 || last == -1) {
556 " is not sector-aligned",
566 return callback(c, first, last);
574 bool iterate_protect_blocks,
587 uint32_t cur_length =
length;
590 LOG_DEBUG(
"iterating over more than one flash bank.");
594 pad_reason,
addr, cur_length,
595 iterate_protect_blocks,
650 if (addr < bank->base ||
addr >=
bank->base +
bank->size
651 ||
bank->write_start_alignment <= 1)
656 uint32_t aligned = 0;
657 for (
unsigned int sect = 0; sect <
bank->num_sectors; sect++) {
661 aligned =
bank->sectors[sect].offset;
663 return bank->base + aligned;
666 return addr & ~(
bank->write_start_alignment - 1);
674 if (addr < bank->base ||
addr >=
bank->base +
bank->size
675 ||
bank->write_end_alignment <= 1)
680 uint32_t aligned = 0;
681 for (
unsigned int sect = 0; sect <
bank->num_sectors; sect++) {
682 aligned =
bank->sectors[sect].offset +
bank->sectors[sect].size - 1;
686 return bank->base + aligned;
689 return addr | (
bank->write_end_alignment - 1);
699 || addr1 < bank->base || addr1 >=
bank->base +
bank->size
700 || addr2 < bank->base || addr2 >=
bank->base +
bank->size)
705 uint32_t offset1 = addr1 -
bank->base;
707 for (sect = 0; sect <
bank->num_sectors; sect++) {
708 if (
bank->sectors[sect].offset > offset1)
711 if (sect >=
bank->num_sectors)
714 uint32_t offset2 = addr2 -
bank->base;
715 return bank->sectors[sect].offset +
bank->sectors[sect].size <= offset2;
720 return aligned1 +
bank->minimal_write_gap < aligned2;
725 uint32_t *written,
bool erase,
bool unlock,
bool write,
bool verify)
729 unsigned int section;
730 uint32_t section_offset;
762 while (section < image->num_sections) {
765 unsigned int section_last;
767 uint32_t run_size = sections[section]->
size - section_offset;
770 if (sections[section]->
size == 0) {
789 section_last = section;
790 padding[section] = 0;
791 while ((run_address + run_size - 1 < c->
base + c->
size - 1) &&
805 if (next_section_base < run_next_addr) {
808 next_section_base, run_next_addr);
814 pad_bytes = next_section_base - run_next_addr;
819 run_next_addr, next_section_base);
826 section_last, run_next_addr, pad_bytes);
828 padding[section_last] = pad_bytes;
829 run_size += pad_bytes;
830 run_size += sections[++section_last]->
size;
833 if (run_address + run_size - 1 > c->
base + c->
size - 1) {
837 LOG_DEBUG(
"Truncate flash run size to the current flash chip.");
839 run_size = c->
base + c->
size - run_address;
840 assert(run_size > 0);
843 uint32_t padding_at_start = 0;
847 padding_at_start = run_address - aligned_start;
848 if (padding_at_start > 0) {
850 " breaks the required alignment of flash bank %s",
851 run_address, c->
name);
853 padding_at_start, aligned_start);
855 run_address -= padding_at_start;
856 run_size += padding_at_start;
861 pad_bytes = aligned_end - run_end;
864 " with %d bytes (bank write end alignment)",
865 section_last, run_end + 1, pad_bytes);
867 padding[section_last] += pad_bytes;
868 run_size += pad_bytes;
871 }
else if (unlock || erase) {
875 uint32_t offset_start = run_address - c->
base;
876 uint32_t offset_end = offset_start + run_size;
877 uint32_t end = offset_end, delta;
879 for (
unsigned int sector = 0; sector < c->
num_sectors; sector++) {
882 if (offset_end <= end)
886 delta = end - offset_end;
887 padding[section_last] += delta;
892 buffer = malloc(run_size);
894 LOG_ERROR(
"Out of memory for flash bank buffer");
899 if (padding_at_start)
902 buffer_idx = padding_at_start;
905 while (buffer_idx < run_size) {
908 size_read = run_size - buffer_idx;
909 if (size_read > sections[section]->
size - section_offset)
910 size_read = sections[section]->
size - section_offset;
917 intptr_t diff = (intptr_t)sections[section] - (intptr_t)
image->
sections;
920 LOG_DEBUG(
"image_read_section: section = %d, t_section_num = %d, "
921 "section_offset = %"PRIu32
", buffer_idx = %"PRIu32
", size_read = %zu",
922 section, t_section_num, section_offset,
923 buffer_idx, size_read);
925 size_read,
buffer + buffer_idx, &size_read);
926 if (retval !=
ERROR_OK || size_read == 0) {
931 buffer_idx += size_read;
932 section_offset += size_read;
935 if (padding[section]) {
937 buffer_idx += padding[section];
940 if (section_offset >= sections[section]->
size) {
954 true, run_address, run_size);
980 *written += run_size;
991 uint32_t *written,
bool erase)
997 unsigned int num_blocks)
1003 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, 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.
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, unsigned int num_blocks, uint8_t erased_value, unsigned int *checked)
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