OpenOCD
stldr.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2022-2026, STMicroelectronics *
5  * fedi.bouzazi@st.com *
6  * ahmed.haoues@st.com *
7  * tarek.bouchkati@st.com *
8  ***************************************************************************/
9 
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13 #include <helper/replacements.h>
14 
15 #ifdef HAVE_ELF_H
16 #include <elf.h>
17 #endif
18 
19 #include <helper/align.h>
20 #include <target/algorithm.h>
21 #include <helper/binarybuffer.h>
22 #include <target/breakpoints.h>
23 #include <target/cortex_m.h>
24 #include <sys/stat.h>
25 
26 #include "imp.h"
27 
28 // stldr functions return values
29 #define STLDR_FUNC_FAILURE 0
30 #define STLDR_FUNC_SUCCESS 1
31 
32 #define STLDR_FUNC_ADDR_UNKNOWN 0xFFFFFFFF
33 
34 // offsets to mimic STM32CubeProgrammer memory usage
35 #define STACK_OFFSET 0x400
36 
37 // stldr functions timeouts in milliseconds
38 #define STLDR_INIT_TIMEOUT_MS 100000
39 #define STLDR_SECTOR_ERASE_TIMEOUT_MS 1000000
40 #define STLDR_MASS_ERASE_INT_TIMEOUT_MS 10000 // for MCU internal flash
41 #define STLDR_MASS_ERASE_EXT_TIMEOUT_MS 600000 // for other (external) flash types
42 #define STLDR_READ_TIMEOUT_MS 1000
43 #define STLDR_WRITE_TIMEOUT_MS 1000
44 
45 // size and offset of StorageInfo members
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
55 
56 // size and offset of DeviceSectors members
57 #define DEVICE_SECTORS_SIZE 8
58 #define DEVICE_SECTORS_COUNT_OFFSET 0
59 #define DEVICE_SECTORS_SIZE_OFFSET 4
60 
70 };
71 
75 };
76 
77 static const struct {
78  enum stldr_func_id id;
79  const char *name;
80  enum stldr_func_type type;
81 } stldr_functions[] = {
83  { FUNC_ID_READ, "Read", STLDR_FUNC_OPTIONAL },
84  { FUNC_ID_SECTOR_ERASE, "SectorErase", STLDR_FUNC_MANDATORY },
85  { FUNC_ID_MASS_ERASE, "MassErase", STLDR_FUNC_OPTIONAL },
86  { FUNC_ID_WRITE, "Write", STLDR_FUNC_MANDATORY },
87  { FUNC_ID_CHECKSUM, "CheckSum", STLDR_FUNC_OPTIONAL },
88  { FUNC_ID_VERIFY, "Verify", STLDR_FUNC_OPTIONAL }
89 };
90 
91 struct stldr_section {
92  uint32_t addr;
93  uint32_t size;
94  uint8_t *content;
95  bool do_write;
97  struct list_head lh;
98 };
99 
100 struct stldr_loader {
101  bool parsed;
106  uint32_t return_addr;
107  uint32_t offset;
108  uint32_t size;
110  struct list_head sections;
112 };
113 
125 };
126 
128  uint32_t count;
129  uint32_t size;
130 };
131 
134  enum stldr_type type;
135  uint32_t base_addr;
136  uint32_t size;
137  uint32_t page_size;
138  uint8_t erase_value;
140  unsigned int n_sectors;
141 };
142 
144  bool probed;
145  uint32_t data_width;
146  struct stldr_loader loader;
147  struct stldr_dev_info dev_info;
148 };
149 
151  uint32_t arg_0;
152  uint32_t arg_1;
153  uint32_t arg_2;
154  uint32_t arg_3;
155  uint32_t ret;
156 };
157 
158 static inline void stldr_func_args_set(struct stldr_func_args *args, uint32_t arg_0,
159  uint32_t arg_1, uint32_t arg_2, uint32_t arg_3)
160 {
161  args->ret = STLDR_FUNC_FAILURE;
162  args->arg_0 = arg_0;
163  args->arg_1 = arg_1;
164  args->arg_2 = arg_2;
165  args->arg_3 = arg_3;
166 }
167 
168 static inline void stldr_func_args_init(struct stldr_func_args *args)
169 {
170  stldr_func_args_set(args, 0, 0, 0, 0);
171 }
172 
173 static int stldr_parse_dev_info(struct flash_bank *bank, uint8_t *data, int size)
174 {
176  LOG_ERROR("stldr: invalid StorageInfo size (%d)", size);
177  return ERROR_FAIL;
178  }
179 
180  struct stldr_flash_bank *stldr_info = bank->driver_priv;
181  struct stldr_dev_info *dev_info = &stldr_info->dev_info;
182 
183  memcpy(dev_info->name, data, STORAGE_INFO_NAME_SIZE);
184  // guarantee we have a zero-terminated string
185  dev_info->name[STORAGE_INFO_NAME_SIZE] = 0;
186  LOG_DEBUG("name %s", dev_info->name);
187 
188  dev_info->type = le_to_h_u16(data + STORAGE_INFO_TYPE_OFFSET);
189  LOG_DEBUG("flash type %x", (unsigned int)dev_info->type);
190 
192  LOG_DEBUG("base_addr %x", dev_info->base_addr);
193 
194  dev_info->size = le_to_h_u32(data + STORAGE_INFO_SIZE_OFFSET);
195  LOG_DEBUG("size %x", dev_info->size);
196 
198  LOG_DEBUG("erase value %x", dev_info->erase_value);
199 
201  LOG_DEBUG("page size %x", dev_info->page_size);
202 
204  LOG_DEBUG("n_sectors %x", dev_info->n_sectors);
205 
206  for (unsigned int i = 0; i < STORAGE_INFO_SECTORS_MAX_ITEMS; i++) {
207  const unsigned int offset = i * DEVICE_SECTORS_SIZE + STORAGE_INFO_SECTORS_OFFSET;
208  const uint32_t count = le_to_h_u32(data + offset + DEVICE_SECTORS_COUNT_OFFSET);
209  if (!count) {
210  dev_info->n_sectors = i;
211  break;
212  }
213 
214  dev_info->sectors[i].count = count;
215  dev_info->sectors[i].size = le_to_h_u32(data + offset + DEVICE_SECTORS_SIZE_OFFSET);
216  }
217 
218  return ERROR_OK;
219 }
220 
221 static void stldr_free_sections(struct stldr_flash_bank *stldr_info)
222 {
223  struct stldr_section *section, *tmp;
224 
225  if (!stldr_info->loader.sections_initialized) {
226  INIT_LIST_HEAD(&stldr_info->loader.sections);
227  stldr_info->loader.sections_initialized = true;
228  } else {
229  list_for_each_entry_safe(section, tmp, &stldr_info->loader.sections, lh) {
230  list_del(&section->lh);
231  free(section->content);
232  free(section);
233  }
234 
235  INIT_LIST_HEAD(&stldr_info->loader.sections);
236  }
237 
238  stldr_info->loader.parsed = false;
239  stldr_info->loader.offset = 0;
240  stldr_info->loader.size = 0;
241  stldr_info->loader.relocatable = false;
242 }
243 
244 static int stldr_parse(struct flash_bank *bank, const char *stldr_path)
245 {
246  struct stldr_flash_bank *stldr_info = bank->driver_priv;
247  FILE *fp = NULL;
248  char *symbol_names = NULL;
249  int retval = ERROR_FAIL;
250 
251  stldr_free_sections(stldr_info);
252 
253  // Check that path exists and is a regular file, not a directory
254  struct stat st;
255  if (stat(stldr_path, &st) != 0) {
256  LOG_ERROR("stldr: cannot access '%s'", stldr_path);
257  goto out;
258  }
259 
260  if (!S_ISREG(st.st_mode)) {
261  LOG_ERROR("stldr: '%s' is not a regular file", stldr_path);
262  goto out;
263  }
264 
265  // Open file in read mode
266  fp = fopen(stldr_path, "rb");
267  if (!fp) {
268  LOG_ERROR("cannot open '%s'", stldr_path);
269  goto out;
270  }
271 
272  // Extract the ELF header
273  Elf32_Ehdr elf_header;
274  if (fread(&elf_header, sizeof(Elf32_Ehdr), 1, fp) != 1) {
275  LOG_ERROR("stldr: failed to read ELF header");
276  goto out;
277  }
278 
279  // Basic ELF sanity checks (avoid OOB seeks/allocations on malformed files)
280  if (memcmp(elf_header.e_ident, ELFMAG, SELFMAG) != 0 ||
281  elf_header.e_ident[EI_CLASS] != ELFCLASS32 ||
282  elf_header.e_shentsize != sizeof(Elf32_Shdr) ||
283  elf_header.e_shnum == 0 ||
284  elf_header.e_shstrndx >= elf_header.e_shnum ||
285  (uint64_t)elf_header.e_shoff +
286  (uint64_t)elf_header.e_shnum * elf_header.e_shentsize >
287  (uint64_t)st.st_size) {
288  LOG_ERROR("stldr: unsupported or malformed ELF file '%s'", stldr_path);
289  goto out;
290  }
291 
292  // Extract the section header string table offset
293  Elf32_Off offset_shst = elf_header.e_shoff +
294  elf_header.e_shstrndx * sizeof(Elf32_Shdr);
295 
296  // Read the section header string table
297  if (fseek(fp, offset_shst, SEEK_SET) != 0) {
298  LOG_ERROR("stldr: failed to seek to section header string table");
299  goto out;
300  }
301 
302  Elf32_Shdr section_header;
303  if (fread(&section_header, 1, sizeof(section_header), fp) != sizeof(section_header)) {
304  LOG_ERROR("stldr: failed to read section header string table header");
305  goto out;
306  }
307 
308  // Iterate all sections and get the loader binary and functions
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);
312  goto out;
313  }
314  if (fread(&section_header, 1, sizeof(section_header), fp) != sizeof(section_header)) {
315  LOG_ERROR("stldr: failed to read section header %u", idx);
316  goto out;
317  }
318 
319  // Ignore non loadable sections
320  if ((section_header.sh_flags & SHF_ALLOC) == 0)
321  continue;
322 
323  uint8_t *content = (section_header.sh_type == 8) ?
324  calloc(1, section_header.sh_size) : malloc(section_header.sh_size);
325  if (!content) {
326  LOG_ERROR("Out of memory");
327  goto out;
328  }
329 
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");
333  free(content);
334  goto out;
335  }
336  if (fread(content, 1, section_header.sh_size, fp) != section_header.sh_size) {
337  LOG_ERROR("stldr: failed to read section content");
338  free(content);
339  goto out;
340  }
341  }
342 
343  struct stldr_section *section = calloc(1, sizeof(struct stldr_section));
344  if (!section) {
345  free(content);
346  LOG_ERROR("Out of memory");
347  goto out;
348  }
349 
350  section->addr = section_header.sh_addr;
351  section->size = section_header.sh_size;
352  section->content = content;
353  section->idx = idx;
354  // sh_flags already checked against SHF_ALLOC
355  section->do_write = (section_header.sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR;
356 
357  list_add_tail(&section->lh, &stldr_info->loader.sections);
358 
359  // check if the flash loader is relocatable
360  if ((section->addr & 0xFF000000) == 0 && section->do_write)
361  stldr_info->loader.relocatable = true;
362 
363  LOG_DEBUG("Loader section found { addr : 0x%08X , size : 0x%08X }",
364  section->addr, section->size);
365  }
366 
367  // get symtab
368  bool symtab_set = false;
369  Elf32_Shdr symtab_sh;
370  for (unsigned int idx = 0; idx < elf_header.e_shnum; idx++) {
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);
373  goto out;
374  }
375  if (fread(&section_header, 1, sizeof(section_header), fp) != sizeof(section_header)) {
376  LOG_ERROR("stldr: failed to read section header %u", idx);
377  goto out;
378  }
379 
380  if (section_header.sh_type == SHT_SYMTAB) {
381  symtab_sh = section_header;
382  symtab_set = true;
383  break;
384  }
385  }
386 
387  if (!symtab_set) {
388  LOG_ERROR("symtab not found");
389  goto out;
390  }
391 
392  // Get the symbol table string 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");
395  goto out;
396  }
397  Elf32_Shdr str_sym;
398  if (fread(&str_sym, 1, sizeof(str_sym), fp) != sizeof(str_sym)) {
399  LOG_ERROR("stldr: failed to read symbol string table header");
400  goto out;
401  }
402 
403  // Create the table of symbol names
404  symbol_names = malloc(str_sym.sh_size);
405  if (!symbol_names) {
406  LOG_ERROR("Out of memory");
407  goto out;
408  }
409  if (fseek(fp, str_sym.sh_offset, SEEK_SET) != 0) {
410  LOG_ERROR("stldr: failed to seek to symbol string table");
411  goto out;
412  }
413  if (fread(symbol_names, 1, str_sym.sh_size, fp) != str_sym.sh_size) {
414  LOG_ERROR("stldr: failed to read symbol string table");
415  goto out;
416  }
417 
418  // Iterate all symbols
419  for (unsigned int f = 0; f < FUNC_ID_COUNT; f++)
421 
422  if (symtab_sh.sh_entsize == 0) {
423  LOG_ERROR("stldr: invalid symtab entry size (0)");
424  goto out;
425  }
426 
427  const unsigned int symbol_count = symtab_sh.sh_size / symtab_sh.sh_entsize;
428  for (unsigned int j = 0; j < symbol_count; j++) {
429  Elf32_Sym symbol;
430 
431  if (fseek(fp, symtab_sh.sh_offset + j * symtab_sh.sh_entsize, SEEK_SET) != 0) {
432  LOG_ERROR("stldr: failed to seek to symbol %u", j);
433  goto out;
434  }
435  if (fread(&symbol, sizeof(symbol), 1, fp) != 1) {
436  LOG_ERROR("stldr: failed to read symbol %u", j);
437  goto out;
438  }
439 
440  if (symbol.st_name >= str_sym.sh_size)
441  continue;
442 
443  const char *symbol_name = symbol_names + symbol.st_name;
444 
445  if (!memchr(symbol_name, '\0', str_sym.sh_size - symbol.st_name))
446  continue;
447  // check for loader descriptor
448  if (strcmp("StorageInfo", symbol_name) == 0) {
449  uint8_t *storage_info = malloc(symbol.st_size);
450  if (!storage_info) {
451  LOG_ERROR("Out of memory");
452  goto out;
453  }
454 
455  struct stldr_section *tmp_sec;
456  bool found_section = false;
457 
458  list_for_each_entry(tmp_sec, &stldr_info->loader.sections, lh) {
459  LOG_DEBUG("This section starts in %x with size %x", tmp_sec->addr, tmp_sec->size);
460 
461  if ((stldr_info->loader.size > tmp_sec->size + tmp_sec->addr) && tmp_sec->do_write)
462  stldr_info->loader.size = tmp_sec->size + tmp_sec->addr;
463 
464  if (tmp_sec->idx == symbol.st_shndx) {
465  found_section = true;
466 
467  if (symbol.st_value < tmp_sec->addr ||
468  (symbol.st_value - tmp_sec->addr) + symbol.st_size > tmp_sec->size) {
469  free(storage_info);
470  LOG_ERROR("stldr: StorageInfo symbol out of section bounds");
471  goto out;
472  }
473 
474  const uint32_t sym_off = symbol.st_value - tmp_sec->addr;
475  memcpy(storage_info, tmp_sec->content + sym_off, symbol.st_size);
476 
477  retval = stldr_parse_dev_info(bank, storage_info, symbol.st_size);
478  free(storage_info);
479  if (retval != ERROR_OK)
480  goto out;
481 
482  break;
483  }
484  }
485 
486  if (!found_section) {
487  free(storage_info);
488  LOG_ERROR("stldr: StorageInfo symbol section not found");
489  goto out;
490  }
491  }
492 
493  // get loader functions
494  for (unsigned int f = 0; f < FUNC_ID_COUNT; f++) {
495  if (strcmp(stldr_functions[f].name, symbol_name) == 0) {
496  LOG_DEBUG("Loader function '%s' found at 0x%08X", symbol_name, symbol.st_value);
497  stldr_info->loader.func_addr[stldr_functions[f].id] = symbol.st_value;
498  break;
499  }
500  }
501  }
502 
503  // Check that mandatory functions are found
504  for (unsigned int f = 0; f < FUNC_ID_COUNT; f++) {
506  continue;
507  if (stldr_info->loader.func_addr[stldr_functions[f].id] == STLDR_FUNC_ADDR_UNKNOWN) {
508  LOG_ERROR("Loader function %s not found", stldr_functions[f].name);
509  goto out;
510  }
511  }
512 
513  // Compute loader size
514  {
515  struct stldr_section *section;
516  uint32_t last_section_addr = 0;
517  uint32_t count_do_write = 0;
518 
519  list_for_each_entry(section, &stldr_info->loader.sections, lh) {
520  if (!section->do_write)
521  continue;
522 
523  count_do_write++;
524  if (section->addr >= last_section_addr) {
525  last_section_addr = section->addr;
526  stldr_info->loader.size += section->size;
527  }
528  }
529 
530  if (count_do_write > 1) {
531  LOG_ERROR("FlashLoader contains multiple execute sections");
532  goto out;
533  }
534  }
535 
536  stldr_info->loader.parsed = true;
537  retval = ERROR_OK;
538 
539 out:
540  free(symbol_names);
541 
542  if (fp)
543  fclose(fp);
544 
545  if (retval != ERROR_OK)
546  stldr_free_sections(stldr_info);
547 
548  return retval;
549 }
550 
551 // flash bank stldr <base> <size> 0 0 <target#> [stldr_path]
552 
553 FLASH_BANK_COMMAND_HANDLER(stldr_flash_bank_command)
554 {
555  if (CMD_ARGC != 6 && CMD_ARGC != 7)
557 
558  struct stldr_flash_bank *stldr_info;
559  stldr_info = calloc(1, sizeof(struct stldr_flash_bank));
560  if (!stldr_info) {
561  LOG_ERROR("Out of memory");
562  return ERROR_FAIL;
563  }
564 
565  bank->driver_priv = stldr_info;
566 
567  unsigned int chip_width = bank->chip_width;
568  if (chip_width == 0) {
569  chip_width = 32;
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)");
574  }
575 
576  bank->write_start_alignment = chip_width;
577  bank->write_end_alignment = chip_width;
578 
579  stldr_info->loader.user_flash_base_addr = bank->base != 0;
580  stldr_info->loader.user_flash_size = bank->size != 0;
581  stldr_info->loader.parsed = false;
582  stldr_info->probed = false;
583 
584  if (CMD_ARGC == 7) // the stldr_path is specified
585  return stldr_parse(bank, CMD_ARGV[6]);
586 
587  return ERROR_OK;
588 }
589 
591 {
592  struct stldr_flash_bank *stldr_info = bank->driver_priv;
593 
594  if (stldr_info->loader.sections.prev || stldr_info->loader.sections.next) {
595  struct stldr_section *tmp, *section;
596  list_for_each_entry_safe(section, tmp, &stldr_info->loader.sections, lh) {
597  free(section->content);
598  free(section);
599  }
600  }
601 
602  free(bank->driver_priv);
603  bank->driver_priv = NULL;
604 }
605 
606 static int stldr_write_loader(struct flash_bank *bank)
607 {
608  struct stldr_flash_bank *stldr_info = bank->driver_priv;
609 
610  if (stldr_info->loader.relocatable) {
612  stldr_info->loader.size + 4 + STACK_OFFSET,
613  &stldr_info->loader.work_area) != ERROR_OK) {
614  LOG_ERROR("working area not large enough");
616  }
617 
618  // update loader functions offset
619  stldr_info->loader.return_addr = stldr_info->loader.work_area->address;
620  stldr_info->loader.offset = stldr_info->loader.work_area->address + 4;
621  LOG_DEBUG("loader offset 0x%08X", stldr_info->loader.offset);
622  }
623 
624  struct target *target = bank->target;
625  struct stldr_section *section;
627  uint32_t working_area_size = target->working_area_size;
628  list_for_each_entry(section, &stldr_info->loader.sections, lh) {
629  if (!section->do_write)
630  continue;
631 
632  if (!stldr_info->loader.relocatable) {
633  const uint32_t wa_start = working_area;
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");
639  return ERROR_FAIL;
640  }
641 
642  int retval = target_write_buffer(target, section->addr, section->size, section->content);
643  if (retval != ERROR_OK)
644  return retval;
645 
646  // update loader functions offset
647  stldr_info->loader.return_addr = section->addr - 4;
648  stldr_info->loader.offset = section->addr;
649  LOG_DEBUG("loader offset 0x%08X", stldr_info->loader.offset);
650  } else {
651  int retval = target_write_buffer(target, section->addr + stldr_info->loader.offset,
652  section->size, section->content);
653  if (retval != ERROR_OK) {
655  return retval;
656  }
657  }
658  }
659 
660  return ERROR_OK;
661 }
662 
663 static int stldr_exec_function(struct flash_bank *bank, enum stldr_func_id func_id,
664  unsigned int timeout_ms, struct stldr_func_args *args)
665 {
666  struct stldr_flash_bank *stldr_info = bank->driver_priv;
667 
668  struct target *target = bank->target;
669  struct armv7m_algorithm armv7m_info = {
671  .core_mode = ARM_MODE_THREAD
672  };
673 
674  if (target->state != TARGET_HALTED) {
675  LOG_ERROR("Target not halted");
677  }
678 
679  if (stldr_info->loader.func_addr[func_id] == STLDR_FUNC_ADDR_UNKNOWN) {
680  LOG_ERROR("Loader function %s not found", stldr_functions[func_id].name);
681  return ERROR_FAIL;
682  }
683 
684  const uint32_t func_addr = stldr_info->loader.func_addr[func_id] +
685  (stldr_info->loader.relocatable ? stldr_info->loader.offset : 0);
686 
687  LOG_DEBUG("Running loader function %s(0x%" PRIx32 ", 0x%" PRIx32 ", 0x%" PRIx32 ", 0x%" PRIx32 ")",
688  stldr_functions[func_id].name, args->arg_0, args->arg_1, args->arg_2, args->arg_3);
689 
690  struct reg_param reg_params[6];
691 
692  // write function parameters
693  init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT);
694  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
695  init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
696  init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
697 
698  buf_set_u32(reg_params[0].value, 0, 32, args->arg_0);
699  buf_set_u32(reg_params[1].value, 0, 32, args->arg_1);
700  buf_set_u32(reg_params[2].value, 0, 32, args->arg_2);
701  buf_set_u32(reg_params[3].value, 0, 32, args->arg_3);
702 
703  // write algo stack pointer
704  uint32_t estack;
705  if (stldr_info->loader.relocatable)
706  estack = stldr_info->loader.work_area->size + stldr_info->loader.work_area->address;
707  else
708  estack = stldr_info->loader.size + stldr_info->loader.offset + STACK_OFFSET;
709  init_reg_param(&reg_params[4], "sp", 32, PARAM_OUT);
710  buf_set_u32(reg_params[4].value, 0, 32, estack);
711  init_reg_param(&reg_params[5], "lr", 32, PARAM_OUT);
712  buf_set_u32(reg_params[5].value, 0, 32, stldr_info->loader.return_addr | 0x1);
713 
714  // execute function
716  int retval = target_run_algorithm(target,
717  0, NULL,
718  ARRAY_SIZE(reg_params), reg_params,
719  func_addr,
720  stldr_info->loader.return_addr, timeout_ms, &armv7m_info);
721 
723 
724  if (retval != ERROR_OK) {
725  LOG_ERROR("Failed to run loader function %s(...) %s",
726  stldr_functions[func_id].name,
727  retval == ERROR_TARGET_TIMEOUT ? " : timeout reached" : ""
728  );
729  goto exit;
730  }
731 
732  args->ret = buf_get_u32(reg_params[0].value, 0, 32);
733  LOG_DEBUG("Loader function %s(...) returned %d", stldr_functions[func_id].name, args->ret);
734 exit:
735  destroy_reg_param(&reg_params[0]);
736  destroy_reg_param(&reg_params[1]);
737  destroy_reg_param(&reg_params[2]);
738  destroy_reg_param(&reg_params[3]);
739  destroy_reg_param(&reg_params[4]);
740  destroy_reg_param(&reg_params[5]);
741  return retval;
742 }
743 
745 {
746  struct stldr_flash_bank *stldr_info = bank->driver_priv;
747  return target_free_working_area(bank->target, stldr_info->loader.work_area);
748 }
749 
751 {
752  struct stldr_func_args args;
753  stldr_func_args_init(&args);
754 
755  int retval = stldr_write_loader(bank);
756  if (retval != ERROR_OK)
757  return retval;
758 
760  if (retval != ERROR_OK || args.ret != STLDR_FUNC_SUCCESS)
762 
763  return ERROR_OK;
764 }
765 
767 {
768  struct stldr_flash_bank *stldr_info = bank->driver_priv;
769 
770  unsigned int timeout_ms = (stldr_info->dev_info.type == STLDR_TYPE_MCU_FLASH) ?
772 
773  struct stldr_func_args args;
774  stldr_func_args_init(&args);
775 
776  int retval = stldr_exec_function_init(bank);
777  if (retval != ERROR_OK) {
779  return retval;
780  }
781 
782  retval = stldr_exec_function(bank, FUNC_ID_MASS_ERASE, timeout_ms, &args);
783  if (retval != ERROR_OK || args.ret != STLDR_FUNC_SUCCESS) {
786  }
787 
789 }
790 
792  uint32_t start_addr, uint32_t end_addr)
793 {
794  struct stldr_func_args args;
795  stldr_func_args_set(&args, start_addr, end_addr, 0, 0);
797  if (retval != ERROR_OK || args.ret != STLDR_FUNC_SUCCESS)
799 
800  return ERROR_OK;
801 }
802 
804  uint32_t addr, uint32_t size, uint32_t buffer_addr)
805 {
806  struct stldr_func_args args;
807  stldr_func_args_set(&args, addr, size, buffer_addr, 0);
809  if (retval != ERROR_OK || args.ret != STLDR_FUNC_SUCCESS)
811 
812  return ERROR_OK;
813 }
814 
816  uint32_t addr, uint32_t size, uint32_t buffer_addr)
817 {
818  struct stldr_func_args args;
819  stldr_func_args_set(&args, addr, size, buffer_addr, 0);
821  if (retval != ERROR_OK || args.ret != STLDR_FUNC_SUCCESS)
823 
824  return ERROR_OK;
825 }
826 
827 static int stldr_erase(struct flash_bank *bank, unsigned int first,
828  unsigned int last)
829 {
830  int retval = stldr_exec_function_init(bank);
831  if (retval != ERROR_OK) {
833  return retval;
834  }
835 
837  bank->base + bank->sectors[first].offset,
838  bank->base + bank->sectors[last].offset);
840  if (retval != ERROR_OK)
841  return retval;
842 
843  return ERROR_OK;
844 }
845 
846 static int stldr_write(struct flash_bank *bank, const uint8_t *buffer,
847  uint32_t offset, uint32_t count)
848 {
849  struct stldr_flash_bank *stldr_info = bank->driver_priv;
850 
851  int retval = stldr_exec_function_init(bank);
852  if (retval != ERROR_OK) {
854  return retval;
855  }
856 
857  uint32_t buffer_size = target_get_working_area_avail(bank->target);
858  // buffer_size should be multiple of stldr_info->data_width
860 
861  if (buffer_size < 256) {
862  LOG_ERROR("Block memory writes cannot be performed because working area is not large enough");
865  }
866 
867  LOG_DEBUG("workarea size: %x", buffer_size);
868  LOG_DEBUG("workarea address: " TARGET_ADDR_FMT, bank->target->working_area_phys);
869 
870  /* The flash write must be aligned to the 'stldr_info->data_width' boundary.
871  * The flash infrastructure ensures it, do just a security check */
872  assert(offset % stldr_info->data_width == 0);
873  assert(count % stldr_info->data_width == 0);
874 
875  struct working_area *source;
877  LOG_ERROR("working area not large enough");
880  }
881 
882  uint32_t address = bank->base + offset;
883 
884  while (count > 0) {
885  if (count < buffer_size)
886  buffer_size = count;
887 
888  retval = target_write_buffer(bank->target, source->address, buffer_size, buffer);
889  if (retval != ERROR_OK)
890  goto exit_error;
891 
893  if (retval != ERROR_OK)
894  goto exit_error;
895 
896  buffer += buffer_size;
897  address += buffer_size;
898  count -= buffer_size;
899  }
902  return ERROR_OK;
903 
904 exit_error:
908 }
909 
910 static int stldr_read(struct flash_bank *bank,
911  uint8_t *buffer, uint32_t offset, uint32_t count)
912 {
913  struct stldr_flash_bank *stldr_info = bank->driver_priv;
914  struct stldr_dev_info *dev_info = &stldr_info->dev_info;
915 
916  // init is needed for default_flash_read as well
917  int retval = stldr_exec_function_init(bank);
918  if (retval != ERROR_OK) {
920  return retval;
921  }
922 
924  int ret = default_flash_read(bank, buffer, offset, count);
926  return ret;
927  }
928 
929  const uint32_t block_size = dev_info->page_size;
930 
931  struct working_area *buffer_area;
932  if (target_alloc_working_area_try(bank->target, block_size, &buffer_area) != ERROR_OK) {
933  LOG_ERROR("working area not large enough");
936  }
937 
938  uint32_t address = bank->base + offset;
939  while (count > 0) {
940  const uint32_t read_block_size = MIN(count, block_size);
941 
942  retval = stldr_exec_function_read(bank, address, read_block_size, buffer_area->address);
943  if (retval != ERROR_OK)
944  goto exit_error;
945 
946  retval = target_read_buffer(bank->target, buffer_area->address, read_block_size, buffer);
947  if (retval != ERROR_OK)
948  goto exit_error;
949 
950  buffer += read_block_size;
951  address += read_block_size;
952  count -= read_block_size;
953  }
954 
956  target_free_working_area(bank->target, buffer_area);
957  return ERROR_OK;
958 
959 exit_error:
961  target_free_working_area(bank->target, buffer_area);
963 }
964 
965 static int stldr_probe(struct flash_bank *bank)
966 {
967  struct stldr_flash_bank *stldr_info = bank->driver_priv;
968  struct stldr_dev_info *dev_info = &stldr_info->dev_info;
969 
970  if (!stldr_info->loader.parsed) {
971  LOG_ERROR("Please check if you have set correctly the loader file");
972  return ERROR_FAIL;
973  }
974 
975  free(bank->sectors);
976 
977  if (!stldr_info->loader.user_flash_base_addr)
978  bank->base = dev_info->base_addr;
979  if (!stldr_info->loader.user_flash_size)
980  bank->size = dev_info->size;
981 
982  stldr_info->data_width = bank->write_start_alignment;
983 
984  bank->num_sectors = 0;
985  for (unsigned int i = 0; i < dev_info->n_sectors; i++)
986  bank->num_sectors += dev_info->sectors[i].count;
987 
988  bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
989  if (!bank->sectors) {
990  LOG_ERROR("failed to allocate bank sectors");
991  return ERROR_FAIL;
992  }
993 
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;
998  bank->sectors[s_i].size = dev_info->sectors[i].size;
999  bank->sectors[s_i].is_erased = -1;
1000  bank->sectors[s_i].is_protected = -1;
1001 
1002  s_i++;
1003  s_offset += dev_info->sectors[i].size;
1004  }
1005  }
1006 
1007  LOG_DEBUG("Bank (%u) size is %" PRIu32 " kb, base address is " TARGET_ADDR_FMT,
1008  bank->bank_number, bank->size >> 10, bank->base);
1009 
1010  stldr_info->probed = true;
1011  return ERROR_OK;
1012 }
1013 
1014 static int stldr_auto_probe(struct flash_bank *bank)
1015 {
1016  struct stldr_flash_bank *stldr_info = bank->driver_priv;
1017 
1018  if (stldr_info->probed)
1019  return ERROR_OK;
1020 
1021  return stldr_probe(bank);
1022 }
1023 
1025 {
1026  struct stldr_flash_bank *stldr_info = bank->driver_priv;
1027  struct stldr_dev_info *dev_info = &stldr_info->dev_info;
1028 
1029  if (stldr_info->loader.parsed)
1030  command_print_sameline(cmd, "Loader file for %s ", dev_info->name);
1031  else
1032  command_print_sameline(cmd, "Please check if you have set correctly the loader file");
1033 
1034  return ERROR_OK;
1035 }
1036 
1037 COMMAND_HANDLER(stldr_handle_set_loader_command)
1038 {
1039  if (CMD_ARGC < 2)
1041 
1042  struct flash_bank *bank;
1043  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_probe_optional, 0, &bank, false);
1044  if (retval != ERROR_OK)
1045  return retval;
1046 
1047  struct stldr_flash_bank *stldr_info = bank->driver_priv;
1048  stldr_info->probed = false;
1049 
1050  retval = stldr_parse(bank, CMD_ARGV[1]);
1051  if (retval != ERROR_OK)
1052  command_print(CMD, "stldr file parsing failed");
1053 
1054  return retval;
1055 }
1056 
1057 COMMAND_HANDLER(stldr_handle_set_size_command)
1058 {
1059  if (CMD_ARGC < 2)
1061 
1062  struct flash_bank *bank;
1063  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_probe_optional, 0, &bank, false);
1064  if (retval != ERROR_OK)
1065  return retval;
1066 
1067  struct stldr_flash_bank *stldr_info = bank->driver_priv;
1068  stldr_info->probed = false;
1069 
1070  uint32_t bank_size;
1071  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], bank_size);
1072  if (bank_size == 0) {
1073  command_print(CMD, "The bank size could not be zero");
1075  }
1076 
1077  bank->size = bank_size;
1078  stldr_info->loader.user_flash_size = true;
1079 
1080  return retval;
1081 }
1082 
1083 COMMAND_HANDLER(stldr_handle_mass_erase_command)
1084 {
1085  if (CMD_ARGC < 1)
1087 
1088  struct flash_bank *bank;
1089  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1090  if (retval != ERROR_OK)
1091  return retval;
1092 
1094  if (retval != ERROR_OK)
1095  command_print(CMD, "stldr mass erase failed");
1096 
1097  return retval;
1098 }
1099 
1100 COMMAND_HANDLER(stldr_handle_init_command)
1101 {
1102  if (CMD_ARGC < 1)
1104 
1105  struct flash_bank *bank;
1106  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1107  if (retval != ERROR_OK)
1108  return retval;
1109 
1110  retval = stldr_exec_function_init(bank);
1111  if (retval != ERROR_OK)
1112  command_print(CMD, "stldr init failed");
1113 
1114  return retval;
1115 }
1116 
1117 static const struct command_registration stldr_subcommand_handlers[] = {
1118  {
1119  .name = "set_loader",
1120  .handler = stldr_handle_set_loader_command,
1121  .mode = COMMAND_ANY,
1122  .usage = "bank_id path/to/stldr",
1123  .help = "set loader path"
1124  },
1125  {
1126  .name = "set_size",
1127  .handler = stldr_handle_set_size_command,
1128  .mode = COMMAND_ANY,
1129  .usage = "bank_id size",
1130  .help = "set bank size"
1131  },
1132  {
1133  .name = "mass_erase",
1134  .handler = stldr_handle_mass_erase_command,
1135  .mode = COMMAND_EXEC,
1136  .usage = "bank_id",
1137  .help = "Erase entire flash bank"
1138  },
1139  {
1140  .name = "init",
1141  .handler = stldr_handle_init_command,
1142  .mode = COMMAND_EXEC,
1143  .usage = "bank_id",
1144  .help = "Initialize memory"
1145  },
1147 };
1148 
1149 static const struct command_registration stldr_command_handlers[] = {
1150  {
1151  .name = "stldr",
1152  .mode = COMMAND_ANY,
1153  .help = "stldr flash command group",
1154  .usage = "",
1155  .chain = stldr_subcommand_handlers,
1156  },
1158 };
1159 
1160 const struct flash_driver stldr_flash = {
1161  .name = "stldr",
1162  .commands = stldr_command_handlers,
1163  .flash_bank_command = stldr_flash_bank_command,
1164  .erase = stldr_erase,
1165  .write = stldr_write,
1166  .read = stldr_read,
1167  .probe = stldr_probe,
1168  .auto_probe = stldr_auto_probe,
1169  .erase_check = default_flash_blank_check,
1170  .info = stldr_get_info,
1171  .free_driver_priv = stldr_free_driver_priv
1172 };
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
Definition: algorithm.c:29
void destroy_reg_param(struct reg_param *param)
Definition: algorithm.c:38
@ PARAM_OUT
Definition: algorithm.h:16
@ PARAM_IN_OUT
Definition: algorithm.h:17
#define ALIGN_DOWN(x, a)
Definition: align.h:21
@ ARM_MODE_THREAD
Definition: arm.h:94
#define ARMV7M_COMMON_MAGIC
Definition: armv7m.h:229
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.
Definition: binarybuffer.h:104
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.
Definition: binarybuffer.h:34
int breakpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:363
int breakpoint_add(struct target *target, target_addr_t address, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:233
@ BKPT_SOFT
Definition: breakpoints.h:21
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#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...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t buffer_size
Size of dw_spi_program::buffer.
Definition: dw-spi-helper.h:5
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
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)
Definition: list.h:203
#define list_for_each_entry_safe(p, n, h, field)
Definition: list.h:159
#define list_for_each_entry(p, h, field)
Definition: list.h:155
static void list_del(struct list_head *entry)
Definition: list.h:88
static void INIT_LIST_HEAD(struct list_head *list)
Definition: list.h:54
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
#define fp
Definition: mips32.c:223
static uint32_t lh(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:172
#define SHF_ALLOC
Definition: replacements.h:311
#define SHT_SYMTAB
Definition: replacements.h:308
#define EI_CLASS
Definition: replacements.h:287
#define ELFCLASS32
Definition: replacements.h:288
uint32_t Elf32_Off
Definition: replacements.h:260
#define SELFMAG
Definition: replacements.h:285
#define SHF_EXECINSTR
Definition: replacements.h:312
uint16_t Elf32_Section
Definition: replacements.h:263
#define ELFMAG
Definition: replacements.h:284
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct rtt_source source
Definition: rtt/rtt.c:23
stldr_func_type
Definition: stldr.c:72
@ STLDR_FUNC_OPTIONAL
Definition: stldr.c:73
@ STLDR_FUNC_MANDATORY
Definition: stldr.c:74
#define STORAGE_INFO_NAME_SIZE
Definition: stldr.c:47
FLASH_BANK_COMMAND_HANDLER(stldr_flash_bank_command)
Definition: stldr.c:553
#define STLDR_WRITE_TIMEOUT_MS
Definition: stldr.c:43
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)
Definition: stldr.c:158
static int stldr_probe(struct flash_bank *bank)
Definition: stldr.c:965
#define STLDR_FUNC_SUCCESS
Definition: stldr.c:30
#define STACK_OFFSET
Definition: stldr.c:35
enum stldr_func_type type
Definition: stldr.c:80
#define STORAGE_INFO_SIZE_OFFSET
Definition: stldr.c:50
stldr_func_id
Definition: stldr.c:61
@ FUNC_ID_READ
Definition: stldr.c:63
@ FUNC_ID_MASS_ERASE
Definition: stldr.c:65
@ FUNC_ID_COUNT
Definition: stldr.c:69
@ FUNC_ID_INIT
Definition: stldr.c:62
@ FUNC_ID_SECTOR_ERASE
Definition: stldr.c:64
@ FUNC_ID_VERIFY
Definition: stldr.c:68
@ FUNC_ID_CHECKSUM
Definition: stldr.c:67
@ FUNC_ID_WRITE
Definition: stldr.c:66
#define DEVICE_SECTORS_SIZE
Definition: stldr.c:57
static int stldr_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stldr.c:910
const struct flash_driver stldr_flash
Definition: stldr.c:1160
static void stldr_free_sections(struct stldr_flash_bank *stldr_info)
Definition: stldr.c:221
#define STORAGE_INFO_PAGE_SIZE_OFFSET
Definition: stldr.c:51
COMMAND_HANDLER(stldr_handle_set_loader_command)
Definition: stldr.c:1037
static int stldr_auto_probe(struct flash_bank *bank)
Definition: stldr.c:1014
static int stldr_exec_function_write(struct flash_bank *bank, uint32_t addr, uint32_t size, uint32_t buffer_addr)
Definition: stldr.c:815
#define DEVICE_SECTORS_COUNT_OFFSET
Definition: stldr.c:58
#define STLDR_READ_TIMEOUT_MS
Definition: stldr.c:42
#define STORAGE_INFO_TYPE_OFFSET
Definition: stldr.c:48
static int stldr_exec_function_init(struct flash_bank *bank)
Definition: stldr.c:750
static int stldr_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stldr.c:846
stldr_type
Definition: stldr.c:114
@ STLDR_TYPE_MCU_FLASH
Definition: stldr.c:115
@ STLDR_TYPE_NAND_FLASH
Definition: stldr.c:116
@ STLDR_TYPE_SRAM
Definition: stldr.c:118
@ STLDR_TYPE_PC_CARD
Definition: stldr.c:120
@ STLDR_TYPE_SDRAM
Definition: stldr.c:123
@ STLDR_TYPE_SPI_FLASH
Definition: stldr.c:121
@ STLDR_TYPE_I2C_EEPROM
Definition: stldr.c:124
@ STLDR_TYPE_I2C_FLASH
Definition: stldr.c:122
@ STLDR_TYPE_PSRAM
Definition: stldr.c:119
@ STLDR_TYPE_NOR_FLASH
Definition: stldr.c:117
static int stldr_exec_function_sector_erase(struct flash_bank *bank, uint32_t start_addr, uint32_t end_addr)
Definition: stldr.c:791
static int stldr_parse_dev_info(struct flash_bank *bank, uint8_t *data, int size)
Definition: stldr.c:173
static int stldr_exec_function_read(struct flash_bank *bank, uint32_t addr, uint32_t size, uint32_t buffer_addr)
Definition: stldr.c:803
const char * name
Definition: stldr.c:79
#define DEVICE_SECTORS_SIZE_OFFSET
Definition: stldr.c:59
#define STORAGE_INFO_SECTORS_MAX_ITEMS
Definition: stldr.c:54
#define STORAGE_INFO_BASE_ADDR_OFFSET
Definition: stldr.c:49
#define STLDR_SECTOR_ERASE_TIMEOUT_MS
Definition: stldr.c:39
static int stldr_exec_function_mass_erase(struct flash_bank *bank)
Definition: stldr.c:766
#define STLDR_FUNC_ADDR_UNKNOWN
Definition: stldr.c:32
static const struct command_registration stldr_subcommand_handlers[]
Definition: stldr.c:1117
static int stldr_exec_function(struct flash_bank *bank, enum stldr_func_id func_id, unsigned int timeout_ms, struct stldr_func_args *args)
Definition: stldr.c:663
#define STLDR_INIT_TIMEOUT_MS
Definition: stldr.c:38
static int stldr_parse(struct flash_bank *bank, const char *stldr_path)
Definition: stldr.c:244
#define STORAGE_INFO_SECTORS_OFFSET
Definition: stldr.c:53
static const struct command_registration stldr_command_handlers[]
Definition: stldr.c:1149
enum stldr_func_id id
Definition: stldr.c:78
static int stldr_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: stldr.c:827
static void stldr_free_driver_priv(struct flash_bank *bank)
Definition: stldr.c:590
#define STLDR_FUNC_FAILURE
Definition: stldr.c:29
static void stldr_func_args_init(struct stldr_func_args *args)
Definition: stldr.c:168
static int stldr_get_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: stldr.c:1024
#define STLDR_MASS_ERASE_INT_TIMEOUT_MS
Definition: stldr.c:40
#define STORAGE_INFO_ERASE_VALUE_OFFSET
Definition: stldr.c:52
static int stldr_exec_function_deinit(struct flash_bank *bank)
Definition: stldr.c:744
static int stldr_write_loader(struct flash_bank *bank)
Definition: stldr.c:606
#define STLDR_MASS_ERASE_EXT_TIMEOUT_MS
Definition: stldr.c:41
Elf32_Off e_shoff
Definition: replacements.h:274
Elf32_Half e_shnum
Definition: replacements.h:280
Elf32_Half e_shstrndx
Definition: replacements.h:281
Elf32_Half e_shentsize
Definition: replacements.h:279
unsigned char e_ident[EI_NIDENT]
Definition: replacements.h:268
Elf32_Word sh_entsize
Definition: replacements.h:305
Elf32_Word sh_flags
Definition: replacements.h:298
Elf32_Off sh_offset
Definition: replacements.h:300
Elf32_Addr sh_addr
Definition: replacements.h:299
Elf32_Word sh_size
Definition: replacements.h:301
Elf32_Word sh_type
Definition: replacements.h:297
Elf32_Word sh_link
Definition: replacements.h:302
Elf32_Word st_size
Definition: replacements.h:317
Elf32_Word st_name
Definition: replacements.h:315
Elf32_Addr st_value
Definition: replacements.h:316
Elf32_Section st_shndx
Definition: replacements.h:320
unsigned int common_magic
Definition: armv7m.h:306
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:239
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
Describes the geometry and status of a single flash sector within a flash bank.
Definition: nor/core.h:28
Definition: list.h:41
struct list_head * next
Definition: list.h:42
struct list_head * prev
Definition: list.h:43
uint8_t * value
Definition: algorithm.h:30
uint32_t size
Definition: stldr.c:136
uint32_t page_size
Definition: stldr.c:137
enum stldr_type type
Definition: stldr.c:134
uint8_t erase_value
Definition: stldr.c:138
char name[STORAGE_INFO_NAME_SIZE+1]
Definition: stldr.c:133
struct stldr_dev_sector sectors[STORAGE_INFO_SECTORS_MAX_ITEMS]
Definition: stldr.c:139
unsigned int n_sectors
Definition: stldr.c:140
uint32_t base_addr
Definition: stldr.c:135
uint32_t size
Definition: stldr.c:129
uint32_t count
Definition: stldr.c:128
uint32_t data_width
Definition: stldr.c:145
struct stldr_dev_info dev_info
Definition: stldr.c:147
struct stldr_loader loader
Definition: stldr.c:146
uint32_t arg_1
Definition: stldr.c:152
uint32_t arg_0
Definition: stldr.c:151
uint32_t arg_2
Definition: stldr.c:153
uint32_t ret
Definition: stldr.c:155
uint32_t arg_3
Definition: stldr.c:154
uint32_t offset
Definition: stldr.c:107
uint32_t return_addr
Definition: stldr.c:106
bool user_flash_base_addr
Definition: stldr.c:103
struct working_area * work_area
Definition: stldr.c:111
bool relocatable
Definition: stldr.c:102
struct list_head sections
Definition: stldr.c:110
uint32_t func_addr[FUNC_ID_COUNT]
Definition: stldr.c:109
bool sections_initialized
Definition: stldr.c:105
bool parsed
Definition: stldr.c:101
uint32_t size
Definition: stldr.c:108
bool user_flash_size
Definition: stldr.c:104
uint8_t * content
Definition: stldr.c:94
uint32_t size
Definition: stldr.c:93
bool do_write
Definition: stldr.c:95
struct list_head lh
Definition: stldr.c:97
Elf32_Section idx
Definition: stldr.c:96
uint32_t addr
Definition: stldr.c:92
Definition: target.h:119
uint32_t working_area_size
Definition: target.h:161
enum target_state state
Definition: target.h:167
target_addr_t working_area_phys
Definition: target.h:160
uint32_t size
Definition: target.h:90
target_addr_t address
Definition: target.h:89
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2423
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2488
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.
Definition: target.c:787
uint32_t target_get_working_area_avail(struct target *target)
Definition: target.c:2233
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2187
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2031
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:818
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_TIMEOUT
Definition: target.h:817
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:822
static uint16_t le_to_h_u16(const uint8_t *buf)
Definition: types.h:122
#define TARGET_ADDR_FMT
Definition: types.h:286
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22