OpenOCD
flash/nor/core.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
5  * Copyright (C) 2007-2010 Øyvind Harboe <oyvind.harboe@zylin.com> *
6  * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
7  * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
8  * Copyright (C) 2010 by Antonio Borneo <borneo.antonio@gmail.com> *
9  * Copyright (C) 2017-2018 Tomas Vanek <vanekt@fbl.cz> *
10  ***************************************************************************/
11 
12 #ifdef HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15 #include <flash/common.h>
16 #include <flash/nor/core.h>
17 #include <flash/nor/imp.h>
18 #include <target/image.h>
19 
27 static struct flash_bank *flash_banks;
28 
29 int flash_driver_erase(struct flash_bank *bank, unsigned int first,
30  unsigned int last)
31 {
32  if (!bank->driver->erase) {
33  // do not log error here to allow silently skip auto-erase
35  }
36 
37  return bank->driver->erase(bank, first, last);
38 }
39 
40 int flash_driver_protect(struct flash_bank *bank, int set, unsigned int first,
41  unsigned int last)
42 {
43  int retval;
44  unsigned int num_blocks;
45 
46  if (!bank->driver->protect) {
47  LOG_ERROR("Flash protection is not supported");
49  }
50 
51  if (bank->num_prot_blocks)
52  num_blocks = bank->num_prot_blocks;
53  else
54  num_blocks = bank->num_sectors;
55 
56 
57  /* callers may not supply illegal parameters ... */
58  if (first > last || last >= num_blocks) {
59  LOG_ERROR("illegal protection block range");
60  return ERROR_FAIL;
61  }
62 
63  /* force "set" to 0/1 */
64  set = !!set;
65 
66  /* DANGER!
67  *
68  * We must not use any cached information about protection state!!!!
69  *
70  * There are a million things that could change the protect state:
71  *
72  * the target could have reset, power cycled, been hot plugged,
73  * the application could have run, etc.
74  *
75  * Drivers only receive valid protection block range.
76  */
77  retval = bank->driver->protect(bank, set, first, last);
78  if (retval != ERROR_OK)
79  LOG_ERROR("failed setting protection for blocks %u to %u", first, last);
80 
81  return retval;
82 }
83 
85  const uint8_t *buffer, uint32_t offset, uint32_t count)
86 {
87  int retval;
88 
89  retval = bank->driver->write(bank, buffer, offset, count);
90  if (retval != ERROR_OK) {
91  LOG_ERROR(
92  "error writing to flash at address " TARGET_ADDR_FMT
93  " at offset 0x%8.8" PRIx32,
94  bank->base,
95  offset);
96  }
97 
98  return retval;
99 }
100 
102  uint8_t *buffer, uint32_t offset, uint32_t count)
103 {
104  int retval;
105 
106  LOG_DEBUG("call flash_driver_read()");
107 
108  if (bank->driver->read)
109  retval = bank->driver->read(bank, buffer, offset, count);
110  else
112  if (retval != ERROR_OK) {
113  LOG_ERROR(
114  "error reading to flash at address " TARGET_ADDR_FMT
115  " at offset 0x%8.8" PRIx32,
116  bank->base,
117  offset);
118  }
119 
120  return retval;
121 }
122 
124  uint8_t *buffer, uint32_t offset, uint32_t count)
125 {
126  return target_read_buffer(bank->target, offset + bank->base, count, buffer);
127 }
128 
130  const uint8_t *buffer, uint32_t offset, uint32_t count)
131 {
132  int retval;
133 
134  if (bank->driver->verify)
135  retval = bank->driver->verify(bank, buffer, offset, count);
136  else
138  if (retval != ERROR_OK) {
139  LOG_ERROR("verify failed in bank at " TARGET_ADDR_FMT " starting at 0x%8.8" PRIx32,
140  bank->base, offset);
141  }
142 
143  return retval;
144 }
145 
147  const uint8_t *buffer, uint32_t offset, uint32_t count)
148 {
149  uint32_t target_crc, image_crc;
150  int retval;
151 
152  retval = image_calculate_checksum(buffer, count, &image_crc);
153  if (retval != ERROR_OK)
154  return retval;
155 
156  retval = target_checksum_memory(bank->target, offset + bank->base, count, &target_crc);
157  if (retval != ERROR_OK)
158  return retval;
159 
160  LOG_DEBUG("addr " TARGET_ADDR_FMT ", len 0x%08" PRIx32 ", crc 0x%08" PRIx32 " 0x%08" PRIx32,
161  offset + bank->base, count, ~image_crc, ~target_crc);
162  if (target_crc == image_crc)
163  return ERROR_OK;
164  else
165  return ERROR_FAIL;
166 }
167 
169 {
170  /* put flash bank in linked list */
171  unsigned int bank_num = 0;
172  if (flash_banks) {
173  /* find last flash bank */
174  struct flash_bank *p = flash_banks;
175  while (p->next) {
176  bank_num += 1;
177  p = p->next;
178  }
179  p->next = bank;
180  bank_num += 1;
181  } else
182  flash_banks = bank;
183 
184  bank->bank_number = bank_num;
185 }
186 
188 {
189  return flash_banks;
190 }
191 
192 struct flash_bank *get_flash_bank_by_num_noprobe(unsigned int num)
193 {
194  struct flash_bank *p;
195  unsigned int i = 0;
196 
197  for (p = flash_banks; p; p = p->next) {
198  if (i++ == num)
199  return p;
200  }
201  LOG_ERROR("flash bank %d does not exist", num);
202  return NULL;
203 }
204 
205 unsigned int flash_get_bank_count(void)
206 {
207  struct flash_bank *p;
208  unsigned int i = 0;
209  for (p = flash_banks; p; p = p->next)
210  i++;
211  return i;
212 }
213 
215 {
216  free(bank->driver_priv);
217  bank->driver_priv = NULL;
218 }
219 
221 {
222  struct flash_bank *bank = flash_banks;
223  while (bank) {
224  struct flash_bank *next = bank->next;
225  if (bank->driver->free_driver_priv)
226  bank->driver->free_driver_priv(bank);
227  else
229 
230  free(bank->sectors);
231  free(bank->prot_blocks);
232 
233  free(bank->name);
234  free(bank);
235  bank = next;
236  }
237  flash_banks = NULL;
238 }
239 
241 {
242  unsigned int requested = get_flash_name_index(name);
243  unsigned int found = 0;
244 
245  struct flash_bank *bank;
246  for (bank = flash_banks; bank; bank = bank->next) {
247  if (strcmp(bank->name, name) == 0)
248  return bank;
249  if (!flash_driver_name_matches(bank->driver->name, name))
250  continue;
251  if (++found < requested)
252  continue;
253  return bank;
254  }
255  return NULL;
256 }
257 
258 int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
259 {
260  struct flash_bank *bank;
261  int retval;
262 
264  if (bank) {
265  retval = bank->driver->auto_probe(bank);
266 
267  if (retval != ERROR_OK) {
268  LOG_ERROR("auto_probe failed");
269  return retval;
270  }
271  }
272 
273  *bank_result = bank;
274  return ERROR_OK;
275 }
276 
277 int get_flash_bank_by_num(unsigned int num, struct flash_bank **bank)
278 {
279  struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
280  int retval;
281 
282  if (!p)
283  return ERROR_FAIL;
284 
285  retval = p->driver->auto_probe(p);
286 
287  if (retval != ERROR_OK) {
288  LOG_ERROR("auto_probe failed");
289  return retval;
290  }
291  *bank = p;
292  return ERROR_OK;
293 }
294 
295 /* lookup flash bank by address, bank not found is success, but
296  * result_bank is set to NULL. */
299  bool check,
300  struct flash_bank **result_bank)
301 {
302  struct flash_bank *c;
303 
304  /* cycle through bank list */
305  for (c = flash_banks; c; c = c->next) {
306  if (c->target != target)
307  continue;
308 
309  int retval;
310  retval = c->driver->auto_probe(c);
311 
312  if (retval != ERROR_OK) {
313  LOG_ERROR("auto_probe failed");
314  return retval;
315  }
316  /* check whether address belongs to this flash bank */
317  if ((addr >= c->base) && (addr <= c->base + (c->size - 1))) {
318  *result_bank = c;
319  return ERROR_OK;
320  }
321  }
322  *result_bank = NULL;
323  if (check) {
324  LOG_ERROR("No flash at address " TARGET_ADDR_FMT, addr);
325  return ERROR_FAIL;
326  }
327  return ERROR_OK;
328 }
329 
331 {
332  struct target *target = bank->target;
333  const int buffer_size = 1024;
334  uint32_t n_bytes;
335  int retval = ERROR_OK;
336 
337  if (bank->target->state != TARGET_HALTED) {
338  LOG_ERROR("Target not halted");
340  }
341 
342  uint8_t *buffer = malloc(buffer_size);
343  if (!buffer) {
344  LOG_ERROR("Out of memory");
345  return ERROR_FAIL;
346  }
347 
348  for (unsigned int i = 0; i < bank->num_sectors; i++) {
349  uint32_t j;
350  bank->sectors[i].is_erased = 1;
351 
352  for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
353  uint32_t chunk;
354  chunk = buffer_size;
355  if (chunk > (bank->sectors[i].size - j))
356  chunk = (bank->sectors[i].size - j);
357 
358  retval = target_read_memory(target,
359  bank->base + bank->sectors[i].offset + j,
360  4,
361  chunk/4,
362  buffer);
363  if (retval != ERROR_OK)
364  goto done;
365 
366  for (n_bytes = 0; n_bytes < chunk; n_bytes++) {
367  if (buffer[n_bytes] != bank->erased_value) {
368  bank->sectors[i].is_erased = 0;
369  break;
370  }
371  }
372  }
373  }
374 
375 done:
376  free(buffer);
377 
378  return retval;
379 }
380 
382 {
383  struct target_memory_check_block check_block;
384 
385  check_block.address = bank->base;
386  check_block.size = bank->size;
387  // Erase state unknown.
388  check_block.result = UINT32_MAX;
389 
390  unsigned int checked;
391  int retval = target_blank_check_memory(bank->target, &check_block, 1,
392  bank->erased_value, &checked);
393 
394  if (retval != ERROR_OK)
395  return retval;
396 
397  bank->is_erased = check_block.result;
398 
399  return ERROR_OK;
400 }
401 
403 {
404  struct target *target = bank->target;
405  const uint32_t buffer_size = 1024;
406  uint8_t buffer[buffer_size];
407 
408  if (bank->target->state != TARGET_HALTED) {
409  LOG_ERROR("Target not halted");
411  }
412 
414  if (retval == ERROR_OK)
415  return retval;
416 
417  LOG_WARNING("Fast erase check failed, running slow fallback");
418 
419  bank->is_erased = -1;
420 
421  for (uint32_t offset = 0; offset < bank->size; offset += buffer_size) {
422  uint32_t chunk_size = MIN(buffer_size, bank->size - offset);
423 
424  retval = target_read_buffer(target, bank->base + offset,
425  chunk_size, buffer);
426 
427  if (retval != ERROR_OK)
428  return retval;
429 
430  for (uint32_t i = 0; i < chunk_size; i++) {
431  if (buffer[i] != bank->erased_value) {
432  bank->is_erased = 0;
433  return ERROR_OK;
434  }
435  }
436  }
437 
438  bank->is_erased = 1;
439 
440  return ERROR_OK;
441 }
442 
444 {
445  struct target *target = bank->target;
446  int retval;
447 
448  if (bank->target->state != TARGET_HALTED) {
449  LOG_ERROR("Target not halted");
451  }
452 
453  if (bank->num_sectors == 0)
455 
456  struct target_memory_check_block *block_array;
457  block_array = malloc(bank->num_sectors * sizeof(struct target_memory_check_block));
458  if (!block_array) {
459  LOG_WARNING("Running slow fallback erase check - limited host memory available");
461  }
462 
463  for (unsigned int i = 0; i < bank->num_sectors; i++) {
464  block_array[i].address = bank->base + bank->sectors[i].offset;
465  block_array[i].size = bank->sectors[i].size;
466  block_array[i].result = UINT32_MAX; /* erase state unknown */
467  }
468 
469  bool fast_check = true;
470  for (unsigned int i = 0; i < bank->num_sectors; ) {
471  unsigned int checked;
473  block_array + i, bank->num_sectors - i,
474  bank->erased_value, &checked);
475  if (retval != ERROR_OK) {
476  /* Run slow fallback if the first run gives no result
477  * otherwise use possibly incomplete results */
478  if (i == 0)
479  fast_check = false;
480  break;
481  }
482  i += checked; /* add number of blocks done this round */
483  }
484 
485  if (fast_check) {
486  for (unsigned int i = 0; i < bank->num_sectors; i++)
487  bank->sectors[i].is_erased = block_array[i].result;
488  retval = ERROR_OK;
489  } else {
490  if (retval == ERROR_NOT_IMPLEMENTED)
491  LOG_DEBUG("Running slow fallback erase check");
492  else
493  LOG_WARNING("Running slow fallback erase check - add working memory");
494 
496  }
497  free(block_array);
498 
499  return retval;
500 }
501 
502 /* Manipulate given flash region, selecting the bank according to target
503  * and address. Maps an address range to a set of sectors, and issues
504  * the callback() on that set ... e.g. to erase or unprotect its members.
505  *
506  * Parameter iterate_protect_blocks switches iteration of protect block
507  * instead of erase sectors. If there is no protect blocks array, sectors
508  * are used in iteration, so compatibility for old flash drivers is retained.
509  *
510  * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
511  * range must fit those sectors exactly. This is clearly safe; it can't
512  * erase data which the caller said to leave alone, for example. If it's
513  * non-NULL, rather than failing, extra data in the first and/or last
514  * sectors will be added to the range, and that reason string is used when
515  * warning about those additions.
516  */
518  char *pad_reason, target_addr_t addr, uint32_t length,
519  bool iterate_protect_blocks,
520  int (*callback)(struct flash_bank *bank, unsigned int first,
521  unsigned int last))
522 {
523  struct flash_bank *c;
524  struct flash_sector *block_array;
525  target_addr_t last_addr = addr + length - 1; /* the last address of range */
526  int first = -1;
527  int last = -1;
528  int i;
529  int num_blocks;
530 
531  int retval = get_flash_bank_by_addr(target, addr, true, &c);
532  if (retval != ERROR_OK)
533  return retval;
534 
535  if (c->size == 0) {
536  LOG_ERROR("Bank is invalid");
538  }
539 
540  if (length == 0 || c->sectors == 0) {
541  /*
542  * Special case, erase whole bank when length is zero or the
543  * bank has no sectors.
544  */
545  if (addr != c->base) {
546  LOG_ERROR("Whole bank access must start at beginning of bank.");
548  }
549 
550  return callback(c, 0, (c->num_sectors == 0) ? 0 : c->num_sectors - 1);
551  }
552 
553  /* check whether it all fits in this bank */
554  if (last_addr > c->base + c->size - 1) {
555  LOG_ERROR("Flash access does not fit into bank.");
557  }
558 
559  if (!c->prot_blocks || c->num_prot_blocks == 0) {
560  /* flash driver does not define protect blocks, use sectors instead */
561  iterate_protect_blocks = false;
562  }
563 
564  if (iterate_protect_blocks) {
565  block_array = c->prot_blocks;
566  num_blocks = c->num_prot_blocks;
567  } else {
568  block_array = c->sectors;
569  num_blocks = c->num_sectors;
570  }
571 
572  for (i = 0; i < num_blocks; i++) {
573  struct flash_sector *f = &block_array[i];
574  target_addr_t sector_addr = c->base + f->offset;
575  target_addr_t sector_last_addr = sector_addr + f->size - 1;
576 
577  /* start only on a sector boundary */
578  if (first < 0) {
579  /* scanned past the first sector? */
580  if (addr < sector_addr)
581  break;
582 
583  /* is this the first sector? */
584  if (addr == sector_addr)
585  first = i;
586 
587  /* Does this need head-padding? If so, pad and warn;
588  * or else force an error.
589  *
590  * Such padding can make trouble, since *WE* can't
591  * ever know if that data was in use. The warning
592  * should help users sort out messes later.
593  */
594  else if (addr <= sector_last_addr && pad_reason) {
595  /* FIXME say how many bytes (e.g. 80 KB) */
596  LOG_WARNING("Adding extra %s range, "
598  pad_reason,
599  sector_addr,
600  addr - 1);
601  first = i;
602  } else
603  continue;
604  }
605 
606  /* is this (also?) the last sector? */
607  if (last_addr == sector_last_addr) {
608  last = i;
609  break;
610  }
611 
612  /* Does this need tail-padding? If so, pad and warn;
613  * or else force an error.
614  */
615  if (last_addr < sector_last_addr && pad_reason) {
616  /* FIXME say how many bytes (e.g. 80 KB) */
617  LOG_WARNING("Adding extra %s range, "
619  pad_reason,
620  last_addr + 1,
621  sector_last_addr);
622  last = i;
623  break;
624  }
625 
626  /* MUST finish on a sector boundary */
627  if (last_addr < sector_addr)
628  break;
629  }
630 
631  /* invalid start or end address? */
632  if (first == -1 || last == -1) {
633  LOG_ERROR("address range " TARGET_ADDR_FMT " .. " TARGET_ADDR_FMT
634  " is not sector-aligned",
635  addr,
636  last_addr);
638  }
639 
640  /* The NOR driver may trim this range down, based on what
641  * sectors are already erased/unprotected. GDB currently
642  * blocks such optimizations.
643  */
644  return callback(c, first, last);
645 }
646 
647 /* The inner fn only handles a single bank, we could be spanning
648  * multiple chips.
649  */
651  char *pad_reason, target_addr_t addr, uint32_t length,
652  bool iterate_protect_blocks, bool skip_banks_without_erase,
653  int (*callback)(struct flash_bank *bank, unsigned int first,
654  unsigned int last))
655 {
656  struct flash_bank *c;
657  int retval = ERROR_OK;
658 
659  /* Danger! zero-length iterations means entire bank! */
660  do {
661  retval = get_flash_bank_by_addr(target, addr, true, &c);
662  if (retval != ERROR_OK)
663  return retval;
664 
665  uint32_t cur_length = length;
666  /* check whether it all fits in this bank */
667  if (addr + length - 1 > c->base + c->size - 1) {
668  LOG_DEBUG("iterating over more than one flash bank.");
669  cur_length = c->base + c->size - addr;
670  }
671  if (skip_banks_without_erase && !c->driver->erase) {
672  LOG_DEBUG("bank %s does not support erase, skipped",
673  c->name);
674  } else {
676  pad_reason, addr, cur_length,
677  iterate_protect_blocks,
678  callback);
679  if (retval != ERROR_OK)
680  break;
681  }
682 
683  length -= cur_length;
684  addr += cur_length;
685  } while (length > 0);
686 
687  return retval;
688 }
689 
691  bool pad, target_addr_t addr, uint32_t length, bool skip_banks_without_erase)
692 {
693  return flash_iterate_address_range(target, pad ? "erase" : NULL,
694  addr, length, false /* iterate_protect_blocks */,
695  skip_banks_without_erase, &flash_driver_erase);
696 }
697 
698 static int flash_driver_unprotect(struct flash_bank *bank, unsigned int first,
699  unsigned int last)
700 {
701  return flash_driver_protect(bank, 0, first, last);
702 }
703 
705  uint32_t length)
706 {
707  /* By default, pad to sector boundaries ... the real issue here
708  * is that our (only) caller *permanently* removes protection,
709  * and doesn't restore it.
710  */
711  return flash_iterate_address_range(target, "unprotect",
712  addr, length, true /* iterate_protect_blocks */,
713  false /* skip_banks_without_erase */,
715 }
716 
717 static int compare_section(const void *a, const void *b)
718 {
719  struct imagesection *b1, *b2;
720  b1 = *((struct imagesection **)a);
721  b2 = *((struct imagesection **)b);
722 
723  if (b1->base_address == b2->base_address)
724  return 0;
725  else if (b1->base_address > b2->base_address)
726  return 1;
727  else
728  return -1;
729 }
730 
735 {
736  if (addr < bank->base || addr >= bank->base + bank->size
737  || bank->write_start_alignment <= 1)
738  return addr;
739 
740  if (bank->write_start_alignment == FLASH_WRITE_ALIGN_SECTOR) {
741  uint32_t offset = addr - bank->base;
742  uint32_t aligned = 0;
743  for (unsigned int sect = 0; sect < bank->num_sectors; sect++) {
744  if (bank->sectors[sect].offset > offset)
745  break;
746 
747  aligned = bank->sectors[sect].offset;
748  }
749  return bank->base + aligned;
750  }
751 
752  return addr & ~(bank->write_start_alignment - 1);
753 }
754 
759 {
760  if (addr < bank->base || addr >= bank->base + bank->size
761  || bank->write_end_alignment <= 1)
762  return addr;
763 
764  if (bank->write_end_alignment == FLASH_WRITE_ALIGN_SECTOR) {
765  uint32_t offset = addr - bank->base;
766  uint32_t aligned = 0;
767  for (unsigned int sect = 0; sect < bank->num_sectors; sect++) {
768  aligned = bank->sectors[sect].offset + bank->sectors[sect].size - 1;
769  if (aligned >= offset)
770  break;
771  }
772  return bank->base + aligned;
773  }
774 
775  return addr | (bank->write_end_alignment - 1);
776 }
777 
782  target_addr_t addr1, target_addr_t addr2)
783 {
784  if (bank->minimal_write_gap == FLASH_WRITE_CONTINUOUS
785  || addr1 < bank->base || addr1 >= bank->base + bank->size
786  || addr2 < bank->base || addr2 >= bank->base + bank->size)
787  return false;
788 
789  if (bank->minimal_write_gap == FLASH_WRITE_GAP_SECTOR) {
790  unsigned int sect;
791  uint32_t offset1 = addr1 - bank->base;
792  /* find the sector following the one containing addr1 */
793  for (sect = 0; sect < bank->num_sectors; sect++) {
794  if (bank->sectors[sect].offset > offset1)
795  break;
796  }
797  if (sect >= bank->num_sectors)
798  return false;
799 
800  uint32_t offset2 = addr2 - bank->base;
801  return bank->sectors[sect].offset + bank->sectors[sect].size <= offset2;
802  }
803 
804  target_addr_t aligned1 = flash_write_align_end(bank, addr1);
805  target_addr_t aligned2 = flash_write_align_start(bank, addr2);
806  return aligned1 + bank->minimal_write_gap < aligned2;
807 }
808 
809 
811  uint32_t *written, bool erase, bool unlock, bool write, bool verify)
812 {
813  int retval = ERROR_OK;
814 
815  unsigned int section;
816  uint32_t section_offset;
817  struct flash_bank *c;
818 
819  section = 0;
820  section_offset = 0;
821 
822  /* allocate padding array */
823  int *padding = calloc(image->num_sections, sizeof(*padding));
824 
825  /* This fn requires all sections to be in ascending order of addresses,
826  * whereas an image can have sections out of order. */
827  struct imagesection **sections = malloc(sizeof(struct imagesection *) *
829 
830  if (!padding || !sections) {
831  LOG_ERROR("Out of memory");
832  free(padding);
833  free(sections);
834  return ERROR_FAIL;
835  }
836 
837  if (written)
838  *written = 0;
839 
840  if (erase) {
841  /* assume all sectors need erasing - stops any problems
842  * when flash_write is called multiple times */
843 
844  flash_set_dirty();
845  }
846 
847  for (unsigned int i = 0; i < image->num_sections; i++)
848  sections[i] = &image->sections[i];
849 
850  qsort(sections, image->num_sections, sizeof(struct imagesection *),
852 
853  /* loop until we reach end of the image */
854  while (section < image->num_sections) {
855  uint32_t buffer_idx;
856  uint8_t *buffer;
857  unsigned int section_last;
858  target_addr_t run_address = sections[section]->base_address + section_offset;
859  uint32_t run_size = sections[section]->size - section_offset;
860  int pad_bytes = 0;
861 
862  if (sections[section]->size == 0) {
863  LOG_WARNING("empty section %d", section);
864  section++;
865  section_offset = 0;
866  continue;
867  }
868 
869  /* find the corresponding flash bank */
870  retval = get_flash_bank_by_addr(target, run_address, false, &c);
871  if (retval != ERROR_OK)
872  goto done;
873  if (!c) {
874  LOG_WARNING("no flash bank found for address " TARGET_ADDR_FMT, run_address);
875  section++; /* and skip it */
876  section_offset = 0;
877  continue;
878  }
879 
880  /* collect consecutive sections which fall into the same bank */
881  section_last = section;
882  padding[section] = 0;
883  while ((run_address + run_size - 1 < c->base + c->size - 1) &&
884  (section_last + 1 < image->num_sections)) {
885  /* sections are sorted */
886  assert(sections[section_last + 1]->base_address >= c->base);
887  if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
888  /* Done with this bank */
889  break;
890  }
891 
892  /* if we have multiple sections within our image,
893  * flash programming could fail due to alignment issues
894  * attempt to rebuild a consecutive buffer for the flash loader */
895  target_addr_t run_next_addr = run_address + run_size;
896  target_addr_t next_section_base = sections[section_last + 1]->base_address;
897  if (next_section_base < run_next_addr) {
898  LOG_ERROR("Section at " TARGET_ADDR_FMT
899  " overlaps section ending at " TARGET_ADDR_FMT,
900  next_section_base, run_next_addr);
901  LOG_ERROR("Flash write aborted.");
902  retval = ERROR_FAIL;
903  goto done;
904  }
905 
906  pad_bytes = next_section_base - run_next_addr;
907  if (pad_bytes) {
908  if (flash_write_check_gap(c, run_next_addr - 1, next_section_base)) {
909  LOG_INFO("Flash write discontinued at " TARGET_ADDR_FMT
910  ", next section at " TARGET_ADDR_FMT,
911  run_next_addr, next_section_base);
912  break;
913  }
914  }
915  if (pad_bytes > 0)
916  LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
917  " with %d bytes",
918  section_last, run_next_addr, pad_bytes);
919 
920  padding[section_last] = pad_bytes;
921  run_size += pad_bytes;
922  run_size += sections[++section_last]->size;
923  }
924 
925  if (run_address + run_size - 1 > c->base + c->size - 1) {
926  /* If we have more than one flash chip back to back, then we limit
927  * the current write operation to the current chip.
928  */
929  LOG_DEBUG("Truncate flash run size to the current flash chip.");
930 
931  run_size = c->base + c->size - run_address;
932  assert(run_size > 0);
933  }
934 
935  uint32_t padding_at_start = 0;
937  /* align write region according to bank requirements */
938  target_addr_t aligned_start = flash_write_align_start(c, run_address);
939  padding_at_start = run_address - aligned_start;
940  if (padding_at_start > 0) {
941  LOG_WARNING("Section start address " TARGET_ADDR_FMT
942  " breaks the required alignment of flash bank %s",
943  run_address, c->name);
944  LOG_WARNING("Padding %" PRIu32 " bytes from " TARGET_ADDR_FMT,
945  padding_at_start, aligned_start);
946 
947  run_address -= padding_at_start;
948  run_size += padding_at_start;
949  }
950 
951  target_addr_t run_end = run_address + run_size - 1;
952  target_addr_t aligned_end = flash_write_align_end(c, run_end);
953  pad_bytes = aligned_end - run_end;
954  if (pad_bytes > 0) {
955  LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
956  " with %d bytes (bank write end alignment)",
957  section_last, run_end + 1, pad_bytes);
958 
959  padding[section_last] += pad_bytes;
960  run_size += pad_bytes;
961  }
962 
963  } else if (unlock || erase) {
964  /* If we're applying any sector automagic, then pad this
965  * (maybe-combined) segment to the end of its last sector.
966  */
967  uint32_t offset_start = run_address - c->base;
968  uint32_t offset_end = offset_start + run_size;
969  uint32_t end = offset_end, delta;
970 
971  for (unsigned int sector = 0; sector < c->num_sectors; sector++) {
972  end = c->sectors[sector].offset
973  + c->sectors[sector].size;
974  if (offset_end <= end)
975  break;
976  }
977 
978  delta = end - offset_end;
979  padding[section_last] += delta;
980  run_size += delta;
981  }
982 
983  /* allocate buffer */
984  buffer = malloc(run_size);
985  if (!buffer) {
986  LOG_ERROR("Out of memory for flash bank buffer");
987  retval = ERROR_FAIL;
988  goto done;
989  }
990 
991  if (padding_at_start)
992  memset(buffer, c->default_padded_value, padding_at_start);
993 
994  buffer_idx = padding_at_start;
995 
996  /* read sections to the buffer */
997  while (buffer_idx < run_size) {
998  size_t size_read;
999 
1000  size_read = run_size - buffer_idx;
1001  if (size_read > sections[section]->size - section_offset)
1002  size_read = sections[section]->size - section_offset;
1003 
1004  /* KLUDGE!
1005  *
1006  * #¤%#"%¤% we have to figure out the section # from the sorted
1007  * list of pointers to sections to invoke image_read_section()...
1008  */
1009  intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
1010  int t_section_num = diff / sizeof(struct imagesection);
1011 
1012  LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
1013  "section_offset = %"PRIu32", buffer_idx = %"PRIu32", size_read = %zu",
1014  section, t_section_num, section_offset,
1015  buffer_idx, size_read);
1016  retval = image_read_section(image, t_section_num, section_offset,
1017  size_read, buffer + buffer_idx, &size_read);
1018  if (retval != ERROR_OK || size_read == 0) {
1019  free(buffer);
1020  goto done;
1021  }
1022 
1023  buffer_idx += size_read;
1024  section_offset += size_read;
1025 
1026  /* see if we need to pad the section */
1027  if (padding[section]) {
1028  memset(buffer + buffer_idx, c->default_padded_value, padding[section]);
1029  buffer_idx += padding[section];
1030  }
1031 
1032  if (section_offset >= sections[section]->size) {
1033  section++;
1034  section_offset = 0;
1035  }
1036  }
1037 
1038  retval = ERROR_OK;
1039 
1040  if (unlock)
1041  retval = flash_unlock_address_range(target, run_address, run_size);
1042  if (retval == ERROR_OK) {
1043  if (erase) {
1044  /* calculate and erase sectors */
1046  true /* pad */,
1047  run_address, run_size,
1048  true /* skip_banks_without_erase */);
1049  }
1050  }
1051 
1052  if (retval == ERROR_OK) {
1053  if (write) {
1054  /* write flash sectors */
1055  retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
1056  }
1057  }
1058 
1059  if (retval == ERROR_OK) {
1060  if (verify) {
1061  /* verify flash sectors */
1062  retval = flash_driver_verify(c, buffer, run_address - c->base, run_size);
1063  }
1064  }
1065 
1066  free(buffer);
1067 
1068  if (retval != ERROR_OK) {
1069  /* abort operation */
1070  goto done;
1071  }
1072 
1073  if (written)
1074  *written += run_size; /* add run size to total written counter */
1075  }
1076 
1077 done:
1078  free(sections);
1079  free(padding);
1080 
1081  return retval;
1082 }
1083 
1084 int flash_write(struct target *target, struct image *image,
1085  uint32_t *written, bool erase)
1086 {
1087  return flash_write_unlock_verify(target, image, written, erase, false, true, false);
1088 }
1089 
1090 struct flash_sector *alloc_block_array(uint32_t offset, uint32_t size,
1091  unsigned int num_blocks)
1092 {
1093  struct flash_sector *array = calloc(num_blocks, sizeof(struct flash_sector));
1094  if (!array)
1095  return NULL;
1096 
1097  for (unsigned int i = 0; i < num_blocks; i++) {
1098  array[i].offset = offset;
1099  array[i].size = size;
1100  array[i].is_erased = -1;
1101  array[i].is_protected = -1;
1102  offset += size;
1103  }
1104 
1105  return array;
1106 }
const char * name
Definition: am13e230x.c:167
bool flash_driver_name_matches(const char *name, const char *expected)
Attempt to match the expected name with the name of a driver.
Definition: common.c:27
unsigned int get_flash_name_index(const char *name)
Parses the optional '.index' portion of a flash bank identifier.
Definition: common.c:14
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
uint8_t bank
Definition: esirisc.c:135
uint8_t length
Definition: esp_usb_jtag.c:1
#define ERROR_FLASH_OPER_UNSUPPORTED
Definition: flash/common.h:36
#define ERROR_FLASH_BANK_INVALID
Definition: flash/common.h:28
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
Definition: flash/common.h:32
target_addr_t flash_write_align_start(struct flash_bank *bank, target_addr_t addr)
Get aligned start address of a flash write region.
int default_flash_verify(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default verify implementation for flash memory.
int flash_driver_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
int flash_driver_verify(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
struct flash_bank * get_flash_bank_by_name_noprobe(const char *name)
Returns the flash bank specified by name, which matches the driver name and a suffix (option) specify...
static int flash_driver_unprotect(struct flash_bank *bank, unsigned int first, unsigned int last)
static int flash_iterate_address_range(struct target *target, char *pad_reason, target_addr_t addr, uint32_t length, bool iterate_protect_blocks, bool skip_banks_without_erase, int(*callback)(struct flash_bank *bank, unsigned int first, unsigned int last))
struct flash_sector * alloc_block_array(uint32_t offset, uint32_t size, unsigned int num_blocks)
Allocate and fill an array of sectors or protection blocks.
target_addr_t flash_write_align_end(struct flash_bank *bank, target_addr_t addr)
Get aligned end address of a flash write region.
static bool flash_write_check_gap(struct flash_bank *bank, target_addr_t addr1, target_addr_t addr2)
Check if gap between sections is bigger than minimum required to discontinue flash write.
int flash_driver_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
static int compare_section(const void *a, const void *b)
static int flash_blank_check_no_sectors(struct flash_bank *bank)
int flash_driver_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
void flash_bank_add(struct flash_bank *bank)
Adds a new NOR bank to the global list of banks.
int flash_unlock_address_range(struct target *target, target_addr_t addr, uint32_t length)
struct flash_bank * get_flash_bank_by_num_noprobe(unsigned int num)
Returns the flash bank like get_flash_bank_by_num(), without probing.
static int flash_iterate_address_range_inner(struct target *target, char *pad_reason, target_addr_t addr, uint32_t length, bool iterate_protect_blocks, int(*callback)(struct flash_bank *bank, unsigned int first, unsigned int last))
static int default_flash_mem_blank_check(struct flash_bank *bank)
static int flash_blank_check_no_sectors_fast(struct flash_bank *bank)
static struct flash_bank * flash_banks
void flash_free_all_banks(void)
Deallocates all flash banks.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
unsigned int flash_get_bank_count(void)
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
int get_flash_bank_by_addr(struct target *target, target_addr_t addr, bool check, struct flash_bank **result_bank)
Returns the flash bank located at a specified address.
int flash_write(struct target *target, struct image *image, uint32_t *written, bool erase)
Writes image into the target flash.
int flash_driver_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
Returns the flash bank specified by name, which matches the driver name and a suffix (option) specify...
struct flash_bank * flash_bank_list(void)
int get_flash_bank_by_num(unsigned int num, struct flash_bank **bank)
Returns the flash bank like get_flash_bank_by_name(), without probing.
int flash_erase_address_range(struct target *target, bool pad, target_addr_t addr, uint32_t length, bool skip_banks_without_erase)
Erases length bytes in the target flash, starting at addr.
int flash_write_unlock_verify(struct target *target, struct image *image, uint32_t *written, bool erase, bool unlock, bool write, bool verify)
int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
Definition: image.c:1078
int image_calculate_checksum(const uint8_t *buffer, uint32_t nbytes, uint32_t *checksum)
Definition: image.c:1267
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:192
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
Upper level NOR flash interfaces.
#define FLASH_WRITE_GAP_SECTOR
Definition: nor/core.h:63
#define FLASH_WRITE_ALIGN_SECTOR
Special value for write_start_alignment and write_end_alignment field.
Definition: nor/core.h:59
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.
Definition: nor/core.h:62
#define MIN(a, b)
Definition: replacements.h:22
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
uint32_t write_end_alignment
Required alignment of flash write end address.
Definition: nor/core.h:104
unsigned int num_prot_blocks
The number of protection blocks in this bank.
Definition: nor/core.h:137
struct flash_sector * sectors
Array of sectors, allocated and initialized by the flash driver.
Definition: nor/core.h:118
uint8_t default_padded_value
Default padded value used, normally this matches the flash erased value.
Definition: nor/core.h:97
const struct flash_driver * driver
Driver for this bank.
Definition: nor/core.h:80
target_addr_t base
The base address of this bank.
Definition: nor/core.h:84
uint32_t size
The size of this chip bank, in bytes.
Definition: nor/core.h:85
unsigned int num_sectors
The number of sectors on this chip.
Definition: nor/core.h:116
uint32_t write_start_alignment
Required alignment of flash write start address.
Definition: nor/core.h:101
struct flash_sector * prot_blocks
Array of protection blocks, allocated and initialized by the flash driver.
Definition: nor/core.h:139
struct flash_bank * next
The next flash bank on this chip.
Definition: nor/core.h:141
struct target * target
Target to which this bank belongs.
Definition: nor/core.h:78
char * name
Definition: nor/core.h:76
int(* auto_probe)(struct flash_bank *bank)
A more gentle flavor of flash_driver::probe, performing setup with less noise.
Definition: nor/driver.h:231
int(* erase)(struct flash_bank *bank, unsigned int first, unsigned int last)
Bank/sector erase routine (target-specific).
Definition: nor/driver.h:99
Describes the geometry and status of a single flash sector within a flash bank.
Definition: nor/core.h:28
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
Definition: nor/core.h:42
uint32_t offset
Bus offset from start of the flash chip (in bytes).
Definition: nor/core.h:30
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown.
Definition: nor/core.h:55
uint32_t size
Number of bytes in this flash sector.
Definition: nor/core.h:32
Definition: image.h:48
unsigned int num_sections
Definition: image.h:51
struct imagesection * sections
Definition: image.h:52
target_addr_t base_address
Definition: image.h:42
uint32_t size
Definition: image.h:43
target_addr_t address
Definition: target.h:348
Definition: target.h:119
int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
Definition: target.c:2535
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2475
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)
Definition: target.c:2566
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.
Definition: target.c:1261
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:818
@ TARGET_HALTED
Definition: target.h:58
#define TARGET_ADDR_FMT
Definition: types.h:286
uint64_t target_addr_t
Definition: types.h:279
#define NULL
Definition: usb.h:16
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22