29 #define STLDR_FUNC_FAILURE 0
30 #define STLDR_FUNC_SUCCESS 1
32 #define STLDR_FUNC_ADDR_UNKNOWN 0xFFFFFFFF
35 #define STACK_OFFSET 0x400
38 #define STLDR_INIT_TIMEOUT_MS 100000
39 #define STLDR_SECTOR_ERASE_TIMEOUT_MS 1000000
40 #define STLDR_MASS_ERASE_INT_TIMEOUT_MS 10000
41 #define STLDR_MASS_ERASE_EXT_TIMEOUT_MS 600000
42 #define STLDR_READ_TIMEOUT_MS 1000
43 #define STLDR_WRITE_TIMEOUT_MS 1000
46 #define STORAGE_INFO_NAME_OFFSET 0
47 #define STORAGE_INFO_NAME_SIZE 100
48 #define STORAGE_INFO_TYPE_OFFSET (STORAGE_INFO_NAME_OFFSET + STORAGE_INFO_NAME_SIZE)
49 #define STORAGE_INFO_BASE_ADDR_OFFSET (STORAGE_INFO_TYPE_OFFSET + 4)
50 #define STORAGE_INFO_SIZE_OFFSET (STORAGE_INFO_BASE_ADDR_OFFSET + 4)
51 #define STORAGE_INFO_PAGE_SIZE_OFFSET (STORAGE_INFO_SIZE_OFFSET + 4)
52 #define STORAGE_INFO_ERASE_VALUE_OFFSET (STORAGE_INFO_PAGE_SIZE_OFFSET + 4)
53 #define STORAGE_INFO_SECTORS_OFFSET (STORAGE_INFO_ERASE_VALUE_OFFSET + 4)
54 #define STORAGE_INFO_SECTORS_MAX_ITEMS 10
57 #define DEVICE_SECTORS_SIZE 8
58 #define DEVICE_SECTORS_COUNT_OFFSET 0
59 #define DEVICE_SECTORS_SIZE_OFFSET 4
159 uint32_t arg_1, uint32_t arg_2, uint32_t arg_3)
248 char *symbol_names =
NULL;
255 if (stat(stldr_path, &st) != 0) {
256 LOG_ERROR(
"stldr: cannot access '%s'", stldr_path);
260 if (!S_ISREG(st.st_mode)) {
261 LOG_ERROR(
"stldr: '%s' is not a regular file", stldr_path);
266 fp = fopen(stldr_path,
"rb");
268 LOG_ERROR(
"cannot open '%s'", stldr_path);
274 if (fread(&elf_header,
sizeof(
Elf32_Ehdr), 1,
fp) != 1) {
275 LOG_ERROR(
"stldr: failed to read ELF header");
287 (uint64_t)st.st_size) {
288 LOG_ERROR(
"stldr: unsupported or malformed ELF file '%s'", stldr_path);
297 if (fseek(
fp, offset_shst, SEEK_SET) != 0) {
298 LOG_ERROR(
"stldr: failed to seek to section header string table");
303 if (fread(§ion_header, 1,
sizeof(section_header),
fp) !=
sizeof(section_header)) {
304 LOG_ERROR(
"stldr: failed to read section header string table header");
309 for (
unsigned int idx = 0; idx < elf_header.
e_shnum; idx++) {
310 if (fseek(
fp, elf_header.
e_shoff + idx *
sizeof(section_header), SEEK_SET) != 0) {
311 LOG_ERROR(
"stldr: failed to seek to section header %u", idx);
314 if (fread(§ion_header, 1,
sizeof(section_header),
fp) !=
sizeof(section_header)) {
315 LOG_ERROR(
"stldr: failed to read section header %u", idx);
323 uint8_t *content = (section_header.
sh_type == 8) ?
324 calloc(1, section_header.
sh_size) : malloc(section_header.
sh_size);
330 if (section_header.
sh_type != 8) {
331 if (fseek(
fp, section_header.
sh_offset, SEEK_SET) != 0) {
332 LOG_ERROR(
"stldr: failed to seek to section content");
336 if (fread(content, 1, section_header.
sh_size,
fp) != section_header.
sh_size) {
337 LOG_ERROR(
"stldr: failed to read section content");
360 if ((section->
addr & 0xFF000000) == 0 && section->
do_write)
363 LOG_DEBUG(
"Loader section found { addr : 0x%08X , size : 0x%08X }",
368 bool symtab_set =
false;
371 if (fseek(
fp, elf_header.
e_shoff +
idx *
sizeof(section_header), SEEK_SET) != 0) {
372 LOG_ERROR(
"stldr: failed to seek to section header %u",
idx);
375 if (fread(§ion_header, 1,
sizeof(section_header),
fp) !=
sizeof(section_header)) {
376 LOG_ERROR(
"stldr: failed to read section header %u",
idx);
381 symtab_sh = section_header;
393 if (fseek(
fp, elf_header.
e_shoff + symtab_sh.
sh_link *
sizeof(symtab_sh), SEEK_SET) != 0) {
394 LOG_ERROR(
"stldr: failed to seek to symbol string table header");
398 if (fread(&str_sym, 1,
sizeof(str_sym),
fp) !=
sizeof(str_sym)) {
399 LOG_ERROR(
"stldr: failed to read symbol string table header");
404 symbol_names = malloc(str_sym.
sh_size);
410 LOG_ERROR(
"stldr: failed to seek to symbol string table");
414 LOG_ERROR(
"stldr: failed to read symbol string table");
423 LOG_ERROR(
"stldr: invalid symtab entry size (0)");
428 for (
unsigned int j = 0; j < symbol_count; j++) {
432 LOG_ERROR(
"stldr: failed to seek to symbol %u", j);
435 if (fread(&symbol,
sizeof(symbol), 1,
fp) != 1) {
436 LOG_ERROR(
"stldr: failed to read symbol %u", j);
443 const char *symbol_name = symbol_names + symbol.
st_name;
448 if (strcmp(
"StorageInfo", symbol_name) == 0) {
449 uint8_t *storage_info = malloc(symbol.
st_size);
456 bool found_section =
false;
459 LOG_DEBUG(
"This section starts in %x with size %x", tmp_sec->
addr, tmp_sec->
size);
465 found_section =
true;
470 LOG_ERROR(
"stldr: StorageInfo symbol out of section bounds");
474 const uint32_t sym_off = symbol.
st_value - tmp_sec->
addr;
486 if (!found_section) {
488 LOG_ERROR(
"stldr: StorageInfo symbol section not found");
516 uint32_t last_section_addr = 0;
517 uint32_t count_do_write = 0;
524 if (section->
addr >= last_section_addr) {
525 last_section_addr = section->
addr;
530 if (count_do_write > 1) {
531 LOG_ERROR(
"FlashLoader contains multiple execute sections");
565 bank->driver_priv = stldr_info;
567 unsigned int chip_width =
bank->chip_width;
568 if (chip_width == 0) {
570 LOG_DEBUG(
"The chip_width argument is set to 32");
571 }
else if (chip_width & (chip_width - 1)) {
572 LOG_ERROR(
"chip_width must be a power of two (bytes)");
576 bank->write_start_alignment = chip_width;
577 bank->write_end_alignment = chip_width;
582 stldr_info->
probed =
false;
602 free(
bank->driver_priv);
614 LOG_ERROR(
"working area not large enough");
634 const uint32_t wa_end =
working_area + working_area_size;
635 const uint32_t sec_start = section->
addr;
636 const uint32_t sec_end = section->
addr + section->
size;
637 if (sec_start < wa_end && sec_end > wa_start) {
638 LOG_ERROR(
"loader section overlaps target work area");
687 LOG_DEBUG(
"Running loader function %s(0x%" PRIx32
", 0x%" PRIx32
", 0x%" PRIx32
", 0x%" PRIx32
")",
725 LOG_ERROR(
"Failed to run loader function %s(...) %s",
792 uint32_t start_addr, uint32_t end_addr)
804 uint32_t
addr, uint32_t
size, uint32_t buffer_addr)
816 uint32_t
addr, uint32_t
size, uint32_t buffer_addr)
837 bank->base +
bank->sectors[first].offset,
838 bank->base +
bank->sectors[last].offset);
862 LOG_ERROR(
"Block memory writes cannot be performed because working area is not large enough");
877 LOG_ERROR(
"working area not large enough");
929 const uint32_t block_size = dev_info->
page_size;
933 LOG_ERROR(
"working area not large enough");
940 const uint32_t read_block_size = MIN(
count, block_size);
950 buffer += read_block_size;
952 count -= read_block_size;
971 LOG_ERROR(
"Please check if you have set correctly the loader file");
984 bank->num_sectors = 0;
985 for (
unsigned int i = 0; i < dev_info->
n_sectors; i++)
989 if (!
bank->sectors) {
990 LOG_ERROR(
"failed to allocate bank sectors");
994 uint32_t s_i = 0, s_offset = 0;
995 for (
unsigned int i = 0; i < dev_info->
n_sectors; i++) {
996 for (
unsigned int j = 0; j < dev_info->
sectors[i].
count; j++) {
997 bank->sectors[s_i].offset = s_offset;
999 bank->sectors[s_i].is_erased = -1;
1000 bank->sectors[s_i].is_protected = -1;
1010 stldr_info->
probed =
true;
1048 stldr_info->
probed =
false;
1068 stldr_info->
probed =
false;
1072 if (bank_size == 0) {
1077 bank->size = bank_size;
1119 .
name =
"set_loader",
1120 .handler = stldr_handle_set_loader_command,
1122 .usage =
"bank_id path/to/stldr",
1123 .help =
"set loader path"
1127 .handler = stldr_handle_set_size_command,
1129 .usage =
"bank_id size",
1130 .help =
"set bank size"
1133 .name =
"mass_erase",
1134 .handler = stldr_handle_mass_erase_command,
1137 .help =
"Erase entire flash bank"
1141 .handler = stldr_handle_init_command,
1144 .help =
"Initialize memory"
1153 .help =
"stldr flash command group",
1163 .flash_bank_command = stldr_flash_bank_command,
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
void destroy_reg_param(struct reg_param *param)
#define ARMV7M_COMMON_MAGIC
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
int breakpoint_remove(struct target *target, target_addr_t address)
int breakpoint_add(struct target *target, target_addr_t address, unsigned int length, enum breakpoint_type type)
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
void command_print(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_COMMAND_ARGUMENT_INVALID
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.
uint32_t address
Starting address. Sector aligned.
#define ERROR_FLASH_OPERATION_FAILED
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
static void list_add_tail(struct list_head *new, struct list_head *head)
#define list_for_each_entry_safe(p, n, h, field)
#define list_for_each_entry(p, h, field)
static void list_del(struct list_head *entry)
static void INIT_LIST_HEAD(struct list_head *list)
#define LOG_ERROR(expr ...)
#define LOG_DEBUG(expr ...)
static uint32_t lh(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
target_addr_t addr
Start address to search for the control block.
#define STORAGE_INFO_NAME_SIZE
FLASH_BANK_COMMAND_HANDLER(stldr_flash_bank_command)
#define STLDR_WRITE_TIMEOUT_MS
static const struct @17 stldr_functions[]
static void stldr_func_args_set(struct stldr_func_args *args, uint32_t arg_0, uint32_t arg_1, uint32_t arg_2, uint32_t arg_3)
static int stldr_probe(struct flash_bank *bank)
#define STLDR_FUNC_SUCCESS
enum stldr_func_type type
#define STORAGE_INFO_SIZE_OFFSET
#define DEVICE_SECTORS_SIZE
static int stldr_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
const struct flash_driver stldr_flash
static void stldr_free_sections(struct stldr_flash_bank *stldr_info)
#define STORAGE_INFO_PAGE_SIZE_OFFSET
COMMAND_HANDLER(stldr_handle_set_loader_command)
static int stldr_auto_probe(struct flash_bank *bank)
static int stldr_exec_function_write(struct flash_bank *bank, uint32_t addr, uint32_t size, uint32_t buffer_addr)
#define DEVICE_SECTORS_COUNT_OFFSET
#define STLDR_READ_TIMEOUT_MS
#define STORAGE_INFO_TYPE_OFFSET
static int stldr_exec_function_init(struct flash_bank *bank)
static int stldr_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static int stldr_exec_function_sector_erase(struct flash_bank *bank, uint32_t start_addr, uint32_t end_addr)
static int stldr_parse_dev_info(struct flash_bank *bank, uint8_t *data, int size)
static int stldr_exec_function_read(struct flash_bank *bank, uint32_t addr, uint32_t size, uint32_t buffer_addr)
#define DEVICE_SECTORS_SIZE_OFFSET
#define STORAGE_INFO_SECTORS_MAX_ITEMS
#define STORAGE_INFO_BASE_ADDR_OFFSET
#define STLDR_SECTOR_ERASE_TIMEOUT_MS
static int stldr_exec_function_mass_erase(struct flash_bank *bank)
#define STLDR_FUNC_ADDR_UNKNOWN
static const struct command_registration stldr_subcommand_handlers[]
static int stldr_exec_function(struct flash_bank *bank, enum stldr_func_id func_id, unsigned int timeout_ms, struct stldr_func_args *args)
#define STLDR_INIT_TIMEOUT_MS
static int stldr_parse(struct flash_bank *bank, const char *stldr_path)
#define STORAGE_INFO_SECTORS_OFFSET
static const struct command_registration stldr_command_handlers[]
static int stldr_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static void stldr_free_driver_priv(struct flash_bank *bank)
#define STLDR_FUNC_FAILURE
static void stldr_func_args_init(struct stldr_func_args *args)
static int stldr_get_info(struct flash_bank *bank, struct command_invocation *cmd)
#define STLDR_MASS_ERASE_INT_TIMEOUT_MS
#define STORAGE_INFO_ERASE_VALUE_OFFSET
static int stldr_exec_function_deinit(struct flash_bank *bank)
static int stldr_write_loader(struct flash_bank *bank)
#define STLDR_MASS_ERASE_EXT_TIMEOUT_MS
unsigned char e_ident[EI_NIDENT]
unsigned int common_magic
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Provides details of a flash bank, available either on-chip or through a major interface.
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Describes the geometry and status of a single flash sector within a flash bank.
char name[STORAGE_INFO_NAME_SIZE+1]
struct stldr_dev_sector sectors[STORAGE_INFO_SECTORS_MAX_ITEMS]
struct stldr_dev_info dev_info
struct stldr_loader loader
bool user_flash_base_addr
struct working_area * work_area
struct list_head sections
uint32_t func_addr[FUNC_ID_COUNT]
bool sections_initialized
uint32_t working_area_size
target_addr_t working_area_phys
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
uint32_t target_get_working_area_avail(struct target *target)
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
#define ERROR_TARGET_NOT_HALTED
#define ERROR_TARGET_TIMEOUT
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
static uint16_t le_to_h_u16(const uint8_t *buf)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
static uint32_t le_to_h_u32(const uint8_t *buf)