OpenOCD
flash/nor/core.c File Reference

Upper level of NOR flash framework. More...

Include dependency graph for flash/nor/core.c:

Go to the source code of this file.

Functions

struct flash_sectoralloc_block_array (uint32_t offset, uint32_t size, unsigned int num_blocks)
 Allocate and fill an array of sectors or protection blocks. More...
 
static int compare_section (const void *a, const void *b)
 
int default_flash_blank_check (struct flash_bank *bank)
 Provides default erased-bank check handling. More...
 
void default_flash_free_driver_priv (struct flash_bank *bank)
 Deallocates bank->driver_priv. More...
 
static int default_flash_mem_blank_check (struct flash_bank *bank)
 
int default_flash_read (struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
 Provides default read implementation for flash memory. More...
 
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. More...
 
void flash_bank_add (struct flash_bank *bank)
 Adds a new NOR bank to the global list of banks. More...
 
struct flash_bankflash_bank_list (void)
 
int flash_driver_erase (struct flash_bank *bank, unsigned int first, unsigned int last)
 
int flash_driver_protect (struct flash_bank *bank, int set, unsigned int first, unsigned int last)
 
int flash_driver_read (struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
 
static int flash_driver_unprotect (struct flash_bank *bank, unsigned int first, unsigned int last)
 
int flash_driver_verify (struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
 
int flash_driver_write (struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
 
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. More...
 
void flash_free_all_banks (void)
 Deallocates all flash banks. More...
 
unsigned int flash_get_bank_count (void)
 
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))
 
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))
 
int flash_unlock_address_range (struct target *target, target_addr_t addr, uint32_t length)
 
int flash_write (struct target *target, struct image *image, uint32_t *written, bool erase)
 Writes image into the target flash. More...
 
target_addr_t flash_write_align_end (struct flash_bank *bank, target_addr_t addr)
 Get aligned end address of a flash write region. More...
 
target_addr_t flash_write_align_start (struct flash_bank *bank, target_addr_t addr)
 Get aligned start address of a flash write region. More...
 
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. More...
 
int flash_write_unlock_verify (struct target *target, struct image *image, uint32_t *written, bool erase, bool unlock, bool write, bool verify)
 
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. More...
 
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 the driver-specific bank number. More...
 
struct flash_bankget_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 the driver-specific bank number. More...
 
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. More...
 
struct flash_bankget_flash_bank_by_num_noprobe (unsigned int num)
 Returns the flash bank like get_flash_bank_by_num(), without probing. More...
 

Variables

static struct flash_bankflash_banks
 

Detailed Description

Upper level of NOR flash framework.

The lower level interfaces are to drivers. These upper level ones primarily support access from Tcl scripts or from GDB.

Definition in file flash/nor/core.c.

Function Documentation

◆ alloc_block_array()

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.

Parameters
offsetOffset of first block.
sizeSize of each block.
num_blocksNumber of blocks in array.
Returns
A struct flash_sector pointer or NULL when allocation failed.

Definition at line 1002 of file flash/nor/core.c.

References flash_sector::is_erased, flash_sector::is_protected, NULL, flash_sector::offset, offset, flash_sector::size, and size.

Referenced by esirisc_flash_probe(), FLASH_BANK_COMMAND_HANDLER(), kinetis_probe(), nrf5_probe(), psoc4_probe(), rp2040_flash_probe(), rsl10_probe(), samd_probe(), same5_probe(), stm32l4_probe(), and stm32x_probe().

◆ compare_section()

static int compare_section ( const void *  a,
const void *  b 
)
static

Definition at line 637 of file flash/nor/core.c.

References imagesection::base_address.

Referenced by flash_write_unlock_verify().

◆ default_flash_blank_check()

int default_flash_blank_check ( struct flash_bank bank)

Provides default erased-bank check handling.

Checks to see if the flash driver knows they are erased; if things look uncertain, this routine will call default_flash_mem_blank_check() to confirm.

Returns
ERROR_OK if successful; otherwise, an error code.

Definition at line 380 of file flash/nor/core.c.

References target_memory_check_block::address, bank, default_flash_mem_blank_check(), ERROR_NOT_IMPLEMENTED, ERROR_OK, ERROR_TARGET_NOT_HALTED, LOG_ERROR, LOG_USER, target_memory_check_block::result, target_memory_check_block::size, target_blank_check_memory(), and TARGET_HALTED.

Referenced by at91sam7_erase_check(), and nrf5_erase_page().

◆ default_flash_free_driver_priv()

void default_flash_free_driver_priv ( struct flash_bank bank)

Deallocates bank->driver_priv.

Definition at line 210 of file flash/nor/core.c.

References bank, and NULL.

◆ default_flash_mem_blank_check()

static int default_flash_mem_blank_check ( struct flash_bank bank)
static

◆ default_flash_read()

int default_flash_read ( struct flash_bank bank,
uint8_t *  buffer,
uint32_t  offset,
uint32_t  count 
)

Provides default read implementation for flash memory.

Parameters
bankThe bank to read.
bufferThe data bytes read.
offsetThe offset into the chip to read.
countThe number of bytes to read.
Returns
ERROR_OK if successful; otherwise, an error code.

Definition at line 121 of file flash/nor/core.c.

References bank, buffer, count, offset, and target_read_buffer().

Referenced by mdr_read().

◆ default_flash_verify()

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.

Parameters
bankThe bank to verify.
bufferThe data bytes to verify.
offsetThe offset into the chip to verify.
countThe number of bytes to verify.
Returns
ERROR_OK if successful; otherwise, an error code.

Definition at line 142 of file flash/nor/core.c.

References bank, buffer, count, ERROR_FAIL, ERROR_OK, image_calculate_checksum(), LOG_DEBUG, offset, TARGET_ADDR_FMT, and target_checksum_memory().

Referenced by flash_driver_verify().

◆ flash_bank_add()

void flash_bank_add ( struct flash_bank bank)

Adds a new NOR bank to the global list of banks.

Parameters
bankThe bank that should be added.

Definition at line 164 of file flash/nor/core.c.

References bank, flash_banks, and flash_bank::next.

Referenced by COMMAND_HANDLER(), kinetis_create_missing_banks(), and msp432_probe().

◆ flash_bank_list()

struct flash_bank* flash_bank_list ( void  )
Returns
The first bank in the global list.

Definition at line 183 of file flash/nor/core.c.

References flash_banks.

Referenced by COMMAND_HANDLER(), FLASH_BANK_COMMAND_HANDLER(), flash_init_drivers(), flash_set_dirty(), jim_flash_list(), kinetis_get_chip(), nrf5_get_chip(), and rsl10_get_chip().

◆ flash_driver_erase()

int flash_driver_erase ( struct flash_bank bank,
unsigned int  first,
unsigned int  last 
)

Definition at line 29 of file flash/nor/core.c.

References bank, ERROR_OK, and LOG_ERROR.

Referenced by COMMAND_HANDLER(), and flash_erase_address_range().

◆ flash_driver_protect()

int flash_driver_protect ( struct flash_bank bank,
int  set,
unsigned int  first,
unsigned int  last 
)

◆ flash_driver_read()

int flash_driver_read ( struct flash_bank bank,
uint8_t *  buffer,
uint32_t  offset,
uint32_t  count 
)

Definition at line 102 of file flash/nor/core.c.

References bank, buffer, count, ERROR_OK, LOG_DEBUG, LOG_ERROR, offset, and TARGET_ADDR_FMT.

Referenced by COMMAND_HANDLER().

◆ flash_driver_unprotect()

static int flash_driver_unprotect ( struct flash_bank bank,
unsigned int  first,
unsigned int  last 
)
static

Definition at line 620 of file flash/nor/core.c.

References bank, and flash_driver_protect().

Referenced by flash_unlock_address_range().

◆ flash_driver_verify()

int flash_driver_verify ( struct flash_bank bank,
const uint8_t *  buffer,
uint32_t  offset,
uint32_t  count 
)

◆ flash_driver_write()

int flash_driver_write ( struct flash_bank bank,
const uint8_t *  buffer,
uint32_t  offset,
uint32_t  count 
)

Definition at line 85 of file flash/nor/core.c.

References bank, buffer, count, ERROR_OK, LOG_ERROR, offset, and TARGET_ADDR_FMT.

Referenced by COMMAND_HANDLER(), and flash_write_unlock_verify().

◆ flash_erase_address_range()

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.

The range addr to addr + length - 1 must be strictly sector aligned, unless pad is true. Setting pad true extends the range, at beginning and/or end, if needed for sector alignment.

Returns
ERROR_OK if successful; otherwise, an error code.

Definition at line 613 of file flash/nor/core.c.

References addr, flash_driver_erase(), flash_iterate_address_range(), length, and NULL.

Referenced by COMMAND_HANDLER(), flash_write_unlock_verify(), and gdb_v_packet().

◆ flash_free_all_banks()

void flash_free_all_banks ( void  )

Deallocates all flash banks.

Definition at line 216 of file flash/nor/core.c.

References bank, flash_banks, LOG_WARNING, flash_bank::next, and NULL.

Referenced by openocd_main().

◆ flash_get_bank_count()

unsigned int flash_get_bank_count ( void  )
Returns
The number of flash banks currently defined.

Definition at line 201 of file flash/nor/core.c.

References flash_banks, and flash_bank::next.

Referenced by gdb_memory_map(), gdb_new_connection(), and gdb_query_packet().

◆ flash_iterate_address_range()

static int flash_iterate_address_range ( struct target target,
char *  pad_reason,
target_addr_t  addr,
uint32_t  length,
bool  iterate_protect_blocks,
int(*)(struct flash_bank *bank, unsigned int first, unsigned int last)  callback 
)
static

◆ flash_iterate_address_range_inner()

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(*)(struct flash_bank *bank, unsigned int first, unsigned int last)  callback 
)
static

◆ flash_unlock_address_range()

int flash_unlock_address_range ( struct target target,
target_addr_t  addr,
uint32_t  length 
)

◆ flash_write()

int flash_write ( struct target target,
struct image image,
uint32_t *  written,
bool  erase 
)

Writes image into the target flash.

The written parameter will contain the

Parameters
targetThe target with the flash to be programmed.
imageThe image that will be programmed to flash.
writtenOn return, contains the number of bytes written.
eraseIndicates whether the flash driver should first erase the corresponding banks or sectors before programming.
Returns
ERROR_OK if successful; otherwise, an error code.

Definition at line 996 of file flash/nor/core.c.

References flash_write_unlock_verify().

Referenced by gdb_v_packet().

◆ flash_write_align_end()

target_addr_t flash_write_align_end ( struct flash_bank bank,
target_addr_t  addr 
)

Get aligned end address of a flash write region.

Align end address of a flash write region according to bank requirements.

Definition at line 678 of file flash/nor/core.c.

References addr, bank, FLASH_WRITE_ALIGN_SECTOR, and offset.

Referenced by COMMAND_HANDLER(), flash_write_check_gap(), and flash_write_unlock_verify().

◆ flash_write_align_start()

target_addr_t flash_write_align_start ( struct flash_bank bank,
target_addr_t  addr 
)

Get aligned start address of a flash write region.

Align start address of a flash write region according to bank requirements.

Definition at line 654 of file flash/nor/core.c.

References addr, bank, FLASH_WRITE_ALIGN_SECTOR, and offset.

Referenced by COMMAND_HANDLER(), flash_write_check_gap(), and flash_write_unlock_verify().

◆ flash_write_check_gap()

static bool flash_write_check_gap ( struct flash_bank bank,
target_addr_t  addr1,
target_addr_t  addr2 
)
static

Check if gap between sections is bigger than minimum required to discontinue flash write.

Definition at line 701 of file flash/nor/core.c.

References bank, flash_write_align_end(), flash_write_align_start(), FLASH_WRITE_CONTINUOUS, and FLASH_WRITE_GAP_SECTOR.

Referenced by flash_write_unlock_verify().

◆ flash_write_unlock_verify()

◆ get_flash_bank_by_addr()

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.

Parameters
targetThe target, presumed to contain one or more banks.
addrAn address that is within the range of the bank.
checkreturn ERROR_OK and result_bank NULL if the bank does not exist
result_bankThe struct flash_bank located at addr, or NULL.
Returns
ERROR_OK on success, or an error indicating the problem.

Definition at line 300 of file flash/nor/core.c.

References addr, flash_driver::auto_probe, flash_bank::base, flash_bank::driver, ERROR_FAIL, ERROR_OK, flash_banks, LOG_ERROR, flash_bank::next, NULL, flash_bank::size, flash_bank::target, and TARGET_ADDR_FMT.

Referenced by COMMAND_HANDLER(), flash_iterate_address_range(), flash_iterate_address_range_inner(), and flash_write_unlock_verify().

◆ get_flash_bank_by_name()

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 the driver-specific bank number.

The suffix consists of the '.' and the driver-specific bank number: when two str9x banks are defined, then 'str9x.1' refers to the second.

Definition at line 261 of file flash/nor/core.c.

References bank, ERROR_OK, get_flash_bank_by_name_noprobe(), LOG_ERROR, and name.

Referenced by COMMAND_HELPER().

◆ get_flash_bank_by_name_noprobe()

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 the driver-specific bank number.

The suffix consists of the '.' and the driver-specific bank number: when two str9x banks are defined, then 'str9x.1' refers to the second.

Definition at line 243 of file flash/nor/core.c.

References bank, flash_banks, flash_driver_name_matches(), get_flash_name_index(), name, and NULL.

Referenced by COMMAND_HANDLER(), COMMAND_HELPER(), FLASH_BANK_COMMAND_HANDLER(), get_flash_bank_by_name(), and virtual_get_master_bank().

◆ get_flash_bank_by_num()

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.

Parameters
numThe flash bank number.
bankreturned bank if fn returns ERROR_OK
Returns
ERROR_OK if successful

Definition at line 280 of file flash/nor/core.c.

References flash_driver::auto_probe, bank, flash_bank::driver, ERROR_FAIL, ERROR_OK, get_flash_bank_by_num_noprobe(), and LOG_ERROR.

Referenced by COMMAND_HELPER(), gdb_memory_map(), and gdb_new_connection().

◆ get_flash_bank_by_num_noprobe()

struct flash_bank* get_flash_bank_by_num_noprobe ( unsigned int  num)

Returns the flash bank like get_flash_bank_by_num(), without probing.

Parameters
numThe flash bank number.
Returns
A struct flash_bank for flash bank num, or NULL.

Definition at line 188 of file flash/nor/core.c.

References flash_banks, LOG_ERROR, flash_bank::next, and NULL.

Referenced by COMMAND_HANDLER(), COMMAND_HELPER(), gdb_memory_map(), gdb_new_connection(), and get_flash_bank_by_num().

Variable Documentation

◆ flash_banks