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  const int buffer_size = 1024;
333  struct target *target = bank->target;
334 
335  if (target->state != TARGET_HALTED) {
336  LOG_ERROR("Target not halted");
338  }
339 
340  uint8_t *buffer = malloc(buffer_size);
341  if (!buffer) {
342  LOG_ERROR("Out of memory");
343  return ERROR_FAIL;
344  }
345 
346  for (unsigned int i = 0; i < bank->num_sectors; i++) {
347  bank->sectors[i].is_erased = 1;
348 
349  for (uint32_t j = 0; j < bank->sectors[i].size; j += buffer_size) {
350  uint32_t chunk;
351  chunk = buffer_size;
352  if (chunk > (bank->sectors[i].size - j))
353  chunk = (bank->sectors[i].size - j);
354 
355  int retval = target_read_buffer(target,
356  bank->base + bank->sectors[i].offset + j,
357  chunk, buffer);
358 
359  /* A sector that cannot be read cannot be classified.
360  * Mark it 'unknown' and continue with the next sector
361  * rather than failing the whole check.
362  */
363  if (retval != ERROR_OK) {
364  LOG_WARNING("Failed to check erase state on sector %u, "
365  "continuing on next sector", i);
366  bank->sectors[i].is_erased = -1;
367  break;
368  }
369 
370  for (uint32_t n_bytes = 0; n_bytes < chunk; n_bytes++) {
371  if (buffer[n_bytes] != bank->erased_value) {
372  bank->sectors[i].is_erased = 0;
373  break;
374  }
375  }
376 
377  if (bank->sectors[i].is_erased == 0)
378  break;
379  }
380  }
381 
382  free(buffer);
383 
384  return ERROR_OK;
385 }
386 
388 {
389  struct target_memory_check_block check_block;
390 
391  check_block.address = bank->base;
392  check_block.size = bank->size;
393  // Erase state unknown.
394  check_block.result = UINT32_MAX;
395 
396  unsigned int checked;
397  int retval = target_blank_check_memory(bank->target, &check_block, 1,
398  bank->erased_value, &checked);
399 
400  if (retval != ERROR_OK)
401  return retval;
402 
403  bank->is_erased = check_block.result;
404 
405  return ERROR_OK;
406 }
407 
409 {
410  struct target *target = bank->target;
411  const uint32_t buffer_size = 1024;
412  uint8_t buffer[buffer_size];
413 
414  if (bank->target->state != TARGET_HALTED) {
415  LOG_ERROR("Target not halted");
417  }
418 
420  if (retval == ERROR_OK)
421  return retval;
422 
423  LOG_WARNING("Fast erase check failed, running slow fallback");
424 
425  bank->is_erased = -1;
426 
427  for (uint32_t offset = 0; offset < bank->size; offset += buffer_size) {
428  uint32_t chunk_size = MIN(buffer_size, bank->size - offset);
429 
430  retval = target_read_buffer(target, bank->base + offset,
431  chunk_size, buffer);
432 
433  if (retval != ERROR_OK)
434  return retval;
435 
436  for (uint32_t i = 0; i < chunk_size; i++) {
437  if (buffer[i] != bank->erased_value) {
438  bank->is_erased = 0;
439  return ERROR_OK;
440  }
441  }
442  }
443 
444  bank->is_erased = 1;
445 
446  return ERROR_OK;
447 }
448 
450 {
451  struct target *target = bank->target;
452  int retval;
453 
454  if (bank->target->state != TARGET_HALTED) {
455  LOG_ERROR("Target not halted");
457  }
458 
459  if (bank->num_sectors == 0)
461 
462  struct target_memory_check_block *block_array;
463  block_array = malloc(bank->num_sectors * sizeof(struct target_memory_check_block));
464  if (!block_array) {
465  LOG_WARNING("Running slow fallback erase check - limited host memory available");
467  }
468 
469  for (unsigned int i = 0; i < bank->num_sectors; i++) {
470  block_array[i].address = bank->base + bank->sectors[i].offset;
471  block_array[i].size = bank->sectors[i].size;
472  block_array[i].result = UINT32_MAX; /* erase state unknown */
473  }
474 
475  bool fast_check = true;
476  for (unsigned int i = 0; i < bank->num_sectors; ) {
477  unsigned int checked;
479  block_array + i, bank->num_sectors - i,
480  bank->erased_value, &checked);
481  if (retval != ERROR_OK) {
482  /* Run slow fallback if the first run gives no result
483  * otherwise use possibly incomplete results */
484  if (i == 0)
485  fast_check = false;
486  break;
487  }
488  i += checked; /* add number of blocks done this round */
489  }
490 
491  if (fast_check) {
492  for (unsigned int i = 0; i < bank->num_sectors; i++)
493  bank->sectors[i].is_erased = block_array[i].result;
494  retval = ERROR_OK;
495  } else {
496  if (retval == ERROR_NOT_IMPLEMENTED)
497  LOG_DEBUG("Running slow fallback erase check");
498  else
499  LOG_WARNING("Running slow fallback erase check - add working memory");
500 
502  }
503  free(block_array);
504 
505  return retval;
506 }
507 
508 /* Manipulate given flash region, selecting the bank according to target
509  * and address. Maps an address range to a set of sectors, and issues
510  * the callback() on that set ... e.g. to erase or unprotect its members.
511  *
512  * Parameter iterate_protect_blocks switches iteration of protect block
513  * instead of erase sectors. If there is no protect blocks array, sectors
514  * are used in iteration, so compatibility for old flash drivers is retained.
515  *
516  * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
517  * range must fit those sectors exactly. This is clearly safe; it can't
518  * erase data which the caller said to leave alone, for example. If it's
519  * non-NULL, rather than failing, extra data in the first and/or last
520  * sectors will be added to the range, and that reason string is used when
521  * warning about those additions.
522  */
524  char *pad_reason, target_addr_t addr, uint32_t length,
525  bool iterate_protect_blocks,
526  int (*callback)(struct flash_bank *bank, unsigned int first,
527  unsigned int last))
528 {
529  struct flash_bank *c;
530  struct flash_sector *block_array;
531  target_addr_t last_addr = addr + length - 1; /* the last address of range */
532  int first = -1;
533  int last = -1;
534  int i;
535  int num_blocks;
536 
537  int retval = get_flash_bank_by_addr(target, addr, true, &c);
538  if (retval != ERROR_OK)
539  return retval;
540 
541  if (c->size == 0) {
542  LOG_ERROR("Bank is invalid");
544  }
545 
546  if (length == 0 || c->sectors == 0) {
547  /*
548  * Special case, erase whole bank when length is zero or the
549  * bank has no sectors.
550  */
551  if (addr != c->base) {
552  LOG_ERROR("Whole bank access must start at beginning of bank.");
554  }
555 
556  return callback(c, 0, (c->num_sectors == 0) ? 0 : c->num_sectors - 1);
557  }
558 
559  /* check whether it all fits in this bank */
560  if (last_addr > c->base + c->size - 1) {
561  LOG_ERROR("Flash access does not fit into bank.");
563  }
564 
565  if (!c->prot_blocks || c->num_prot_blocks == 0) {
566  /* flash driver does not define protect blocks, use sectors instead */
567  iterate_protect_blocks = false;
568  }
569 
570  if (iterate_protect_blocks) {
571  block_array = c->prot_blocks;
572  num_blocks = c->num_prot_blocks;
573  } else {
574  block_array = c->sectors;
575  num_blocks = c->num_sectors;
576  }
577 
578  for (i = 0; i < num_blocks; i++) {
579  struct flash_sector *f = &block_array[i];
580  target_addr_t sector_addr = c->base + f->offset;
581  target_addr_t sector_last_addr = sector_addr + f->size - 1;
582 
583  /* start only on a sector boundary */
584  if (first < 0) {
585  /* scanned past the first sector? */
586  if (addr < sector_addr)
587  break;
588 
589  /* is this the first sector? */
590  if (addr == sector_addr)
591  first = i;
592 
593  /* Does this need head-padding? If so, pad and warn;
594  * or else force an error.
595  *
596  * Such padding can make trouble, since *WE* can't
597  * ever know if that data was in use. The warning
598  * should help users sort out messes later.
599  */
600  else if (addr <= sector_last_addr && pad_reason) {
601  /* FIXME say how many bytes (e.g. 80 KB) */
602  LOG_WARNING("Adding extra %s range, "
604  pad_reason,
605  sector_addr,
606  addr - 1);
607  first = i;
608  } else
609  continue;
610  }
611 
612  /* is this (also?) the last sector? */
613  if (last_addr == sector_last_addr) {
614  last = i;
615  break;
616  }
617 
618  /* Does this need tail-padding? If so, pad and warn;
619  * or else force an error.
620  */
621  if (last_addr < sector_last_addr && pad_reason) {
622  /* FIXME say how many bytes (e.g. 80 KB) */
623  LOG_WARNING("Adding extra %s range, "
625  pad_reason,
626  last_addr + 1,
627  sector_last_addr);
628  last = i;
629  break;
630  }
631 
632  /* MUST finish on a sector boundary */
633  if (last_addr < sector_addr)
634  break;
635  }
636 
637  /* invalid start or end address? */
638  if (first == -1 || last == -1) {
639  LOG_ERROR("address range " TARGET_ADDR_FMT " .. " TARGET_ADDR_FMT
640  " is not sector-aligned",
641  addr,
642  last_addr);
644  }
645 
646  /* The NOR driver may trim this range down, based on what
647  * sectors are already erased/unprotected. GDB currently
648  * blocks such optimizations.
649  */
650  return callback(c, first, last);
651 }
652 
653 /* The inner fn only handles a single bank, we could be spanning
654  * multiple chips.
655  */
657  char *pad_reason, target_addr_t addr, uint32_t length,
658  bool iterate_protect_blocks, bool skip_banks_without_erase,
659  int (*callback)(struct flash_bank *bank, unsigned int first,
660  unsigned int last))
661 {
662  struct flash_bank *c;
663  int retval = ERROR_OK;
664 
665  /* Danger! zero-length iterations means entire bank! */
666  do {
667  retval = get_flash_bank_by_addr(target, addr, true, &c);
668  if (retval != ERROR_OK)
669  return retval;
670 
671  uint32_t cur_length = length;
672  /* check whether it all fits in this bank */
673  if (addr + length - 1 > c->base + c->size - 1) {
674  LOG_DEBUG("iterating over more than one flash bank.");
675  cur_length = c->base + c->size - addr;
676  }
677  if (skip_banks_without_erase && !c->driver->erase) {
678  LOG_DEBUG("bank %s does not support erase, skipped",
679  c->name);
680  } else {
682  pad_reason, addr, cur_length,
683  iterate_protect_blocks,
684  callback);
685  if (retval != ERROR_OK)
686  break;
687  }
688 
689  length -= cur_length;
690  addr += cur_length;
691  } while (length > 0);
692 
693  return retval;
694 }
695 
697  bool pad, target_addr_t addr, uint32_t length, bool skip_banks_without_erase)
698 {
699  return flash_iterate_address_range(target, pad ? "erase" : NULL,
700  addr, length, false /* iterate_protect_blocks */,
701  skip_banks_without_erase, &flash_driver_erase);
702 }
703 
704 static int flash_driver_unprotect(struct flash_bank *bank, unsigned int first,
705  unsigned int last)
706 {
707  return flash_driver_protect(bank, 0, first, last);
708 }
709 
711  uint32_t length)
712 {
713  /* By default, pad to sector boundaries ... the real issue here
714  * is that our (only) caller *permanently* removes protection,
715  * and doesn't restore it.
716  */
717  return flash_iterate_address_range(target, "unprotect",
718  addr, length, true /* iterate_protect_blocks */,
719  false /* skip_banks_without_erase */,
721 }
722 
723 static int compare_section(const void *a, const void *b)
724 {
725  struct imagesection *b1, *b2;
726  b1 = *((struct imagesection **)a);
727  b2 = *((struct imagesection **)b);
728 
729  if (b1->base_address == b2->base_address)
730  return 0;
731  else if (b1->base_address > b2->base_address)
732  return 1;
733  else
734  return -1;
735 }
736 
741 {
742  if (addr < bank->base || addr >= bank->base + bank->size
743  || bank->write_start_alignment <= 1)
744  return addr;
745 
746  if (bank->write_start_alignment == FLASH_WRITE_ALIGN_SECTOR) {
747  uint32_t offset = addr - bank->base;
748  uint32_t aligned = 0;
749  for (unsigned int sect = 0; sect < bank->num_sectors; sect++) {
750  if (bank->sectors[sect].offset > offset)
751  break;
752 
753  aligned = bank->sectors[sect].offset;
754  }
755  return bank->base + aligned;
756  }
757 
758  return addr & ~(bank->write_start_alignment - 1);
759 }
760 
765 {
766  if (addr < bank->base || addr >= bank->base + bank->size
767  || bank->write_end_alignment <= 1)
768  return addr;
769 
770  if (bank->write_end_alignment == FLASH_WRITE_ALIGN_SECTOR) {
771  uint32_t offset = addr - bank->base;
772  uint32_t aligned = 0;
773  for (unsigned int sect = 0; sect < bank->num_sectors; sect++) {
774  aligned = bank->sectors[sect].offset + bank->sectors[sect].size - 1;
775  if (aligned >= offset)
776  break;
777  }
778  return bank->base + aligned;
779  }
780 
781  return addr | (bank->write_end_alignment - 1);
782 }
783 
788  target_addr_t addr1, target_addr_t addr2)
789 {
790  if (bank->minimal_write_gap == FLASH_WRITE_CONTINUOUS
791  || addr1 < bank->base || addr1 >= bank->base + bank->size
792  || addr2 < bank->base || addr2 >= bank->base + bank->size)
793  return false;
794 
795  if (bank->minimal_write_gap == FLASH_WRITE_GAP_SECTOR) {
796  unsigned int sect;
797  uint32_t offset1 = addr1 - bank->base;
798  /* find the sector following the one containing addr1 */
799  for (sect = 0; sect < bank->num_sectors; sect++) {
800  if (bank->sectors[sect].offset > offset1)
801  break;
802  }
803  if (sect >= bank->num_sectors)
804  return false;
805 
806  uint32_t offset2 = addr2 - bank->base;
807  return bank->sectors[sect].offset + bank->sectors[sect].size <= offset2;
808  }
809 
810  target_addr_t aligned1 = flash_write_align_end(bank, addr1);
811  target_addr_t aligned2 = flash_write_align_start(bank, addr2);
812  return aligned1 + bank->minimal_write_gap < aligned2;
813 }
814 
815 
817  uint32_t *written, bool erase, bool unlock, bool write, bool verify)
818 {
819  int retval = ERROR_OK;
820 
821  unsigned int section;
822  uint32_t section_offset;
823  struct flash_bank *c;
824 
825  section = 0;
826  section_offset = 0;
827 
828  /* allocate padding array */
829  int *padding = calloc(image->num_sections, sizeof(*padding));
830 
831  /* This fn requires all sections to be in ascending order of addresses,
832  * whereas an image can have sections out of order. */
833  struct imagesection **sections = malloc(sizeof(struct imagesection *) *
835 
836  if (!padding || !sections) {
837  LOG_ERROR("Out of memory");
838  free(padding);
839  free(sections);
840  return ERROR_FAIL;
841  }
842 
843  if (written)
844  *written = 0;
845 
846  if (erase) {
847  /* assume all sectors need erasing - stops any problems
848  * when flash_write is called multiple times */
849 
850  flash_set_dirty();
851  }
852 
853  for (unsigned int i = 0; i < image->num_sections; i++)
854  sections[i] = &image->sections[i];
855 
856  qsort(sections, image->num_sections, sizeof(struct imagesection *),
858 
859  /* loop until we reach end of the image */
860  while (section < image->num_sections) {
861  uint32_t buffer_idx;
862  uint8_t *buffer;
863  unsigned int section_last;
864  target_addr_t run_address = sections[section]->base_address + section_offset;
865  uint32_t run_size = sections[section]->size - section_offset;
866  int pad_bytes = 0;
867 
868  if (sections[section]->size == 0) {
869  LOG_WARNING("empty section %d", section);
870  section++;
871  section_offset = 0;
872  continue;
873  }
874 
875  /* find the corresponding flash bank */
876  retval = get_flash_bank_by_addr(target, run_address, false, &c);
877  if (retval != ERROR_OK)
878  goto done;
879  if (!c) {
880  LOG_WARNING("no flash bank found for address " TARGET_ADDR_FMT, run_address);
881  section++; /* and skip it */
882  section_offset = 0;
883  continue;
884  }
885 
886  /* collect consecutive sections which fall into the same bank */
887  section_last = section;
888  padding[section] = 0;
889  while ((run_address + run_size - 1 < c->base + c->size - 1) &&
890  (section_last + 1 < image->num_sections)) {
891  /* sections are sorted */
892  assert(sections[section_last + 1]->base_address >= c->base);
893  if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
894  /* Done with this bank */
895  break;
896  }
897 
898  /* if we have multiple sections within our image,
899  * flash programming could fail due to alignment issues
900  * attempt to rebuild a consecutive buffer for the flash loader */
901  target_addr_t run_next_addr = run_address + run_size;
902  target_addr_t next_section_base = sections[section_last + 1]->base_address;
903  if (next_section_base < run_next_addr) {
904  LOG_ERROR("Section at " TARGET_ADDR_FMT
905  " overlaps section ending at " TARGET_ADDR_FMT,
906  next_section_base, run_next_addr);
907  LOG_ERROR("Flash write aborted.");
908  retval = ERROR_FAIL;
909  goto done;
910  }
911 
912  pad_bytes = next_section_base - run_next_addr;
913  if (pad_bytes) {
914  if (flash_write_check_gap(c, run_next_addr - 1, next_section_base)) {
915  LOG_INFO("Flash write discontinued at " TARGET_ADDR_FMT
916  ", next section at " TARGET_ADDR_FMT,
917  run_next_addr, next_section_base);
918  break;
919  }
920  }
921  if (pad_bytes > 0)
922  LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
923  " with %d bytes",
924  section_last, run_next_addr, pad_bytes);
925 
926  padding[section_last] = pad_bytes;
927  run_size += pad_bytes;
928  run_size += sections[++section_last]->size;
929  }
930 
931  if (run_address + run_size - 1 > c->base + c->size - 1) {
932  /* If we have more than one flash chip back to back, then we limit
933  * the current write operation to the current chip.
934  */
935  LOG_DEBUG("Truncate flash run size to the current flash chip.");
936 
937  run_size = c->base + c->size - run_address;
938  assert(run_size > 0);
939  }
940 
941  uint32_t padding_at_start = 0;
943  /* align write region according to bank requirements */
944  target_addr_t aligned_start = flash_write_align_start(c, run_address);
945  padding_at_start = run_address - aligned_start;
946  if (padding_at_start > 0) {
947  LOG_WARNING("Section start address " TARGET_ADDR_FMT
948  " breaks the required alignment of flash bank %s",
949  run_address, c->name);
950  LOG_WARNING("Padding %" PRIu32 " bytes from " TARGET_ADDR_FMT,
951  padding_at_start, aligned_start);
952 
953  run_address -= padding_at_start;
954  run_size += padding_at_start;
955  }
956 
957  target_addr_t run_end = run_address + run_size - 1;
958  target_addr_t aligned_end = flash_write_align_end(c, run_end);
959  pad_bytes = aligned_end - run_end;
960  if (pad_bytes > 0) {
961  LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
962  " with %d bytes (bank write end alignment)",
963  section_last, run_end + 1, pad_bytes);
964 
965  padding[section_last] += pad_bytes;
966  run_size += pad_bytes;
967  }
968 
969  } else if (unlock || erase) {
970  /* If we're applying any sector automagic, then pad this
971  * (maybe-combined) segment to the end of its last sector.
972  */
973  uint32_t offset_start = run_address - c->base;
974  uint32_t offset_end = offset_start + run_size;
975  uint32_t end = offset_end, delta;
976 
977  for (unsigned int sector = 0; sector < c->num_sectors; sector++) {
978  end = c->sectors[sector].offset
979  + c->sectors[sector].size;
980  if (offset_end <= end)
981  break;
982  }
983 
984  delta = end - offset_end;
985  padding[section_last] += delta;
986  run_size += delta;
987  }
988 
989  /* allocate buffer */
990  buffer = malloc(run_size);
991  if (!buffer) {
992  LOG_ERROR("Out of memory for flash bank buffer");
993  retval = ERROR_FAIL;
994  goto done;
995  }
996 
997  if (padding_at_start)
998  memset(buffer, c->default_padded_value, padding_at_start);
999 
1000  buffer_idx = padding_at_start;
1001 
1002  /* read sections to the buffer */
1003  while (buffer_idx < run_size) {
1004  size_t size_read;
1005 
1006  size_read = run_size - buffer_idx;
1007  if (size_read > sections[section]->size - section_offset)
1008  size_read = sections[section]->size - section_offset;
1009 
1010  /* KLUDGE!
1011  *
1012  * #¤%#"%¤% we have to figure out the section # from the sorted
1013  * list of pointers to sections to invoke image_read_section()...
1014  */
1015  intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
1016  int t_section_num = diff / sizeof(struct imagesection);
1017 
1018  LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
1019  "section_offset = %"PRIu32", buffer_idx = %"PRIu32", size_read = %zu",
1020  section, t_section_num, section_offset,
1021  buffer_idx, size_read);
1022  retval = image_read_section(image, t_section_num, section_offset,
1023  size_read, buffer + buffer_idx, &size_read);
1024  if (retval != ERROR_OK || size_read == 0) {
1025  free(buffer);
1026  goto done;
1027  }
1028 
1029  buffer_idx += size_read;
1030  section_offset += size_read;
1031 
1032  /* see if we need to pad the section */
1033  if (padding[section]) {
1034  memset(buffer + buffer_idx, c->default_padded_value, padding[section]);
1035  buffer_idx += padding[section];
1036  }
1037 
1038  if (section_offset >= sections[section]->size) {
1039  section++;
1040  section_offset = 0;
1041  }
1042  }
1043 
1044  retval = ERROR_OK;
1045 
1046  if (unlock)
1047  retval = flash_unlock_address_range(target, run_address, run_size);
1048  if (retval == ERROR_OK) {
1049  if (erase) {
1050  /* calculate and erase sectors */
1052  true /* pad */,
1053  run_address, run_size,
1054  true /* skip_banks_without_erase */);
1055  }
1056  }
1057 
1058  if (retval == ERROR_OK) {
1059  if (write) {
1060  /* write flash sectors */
1061  retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
1062  }
1063  }
1064 
1065  if (retval == ERROR_OK) {
1066  if (verify) {
1067  /* verify flash sectors */
1068  retval = flash_driver_verify(c, buffer, run_address - c->base, run_size);
1069  }
1070  }
1071 
1072  free(buffer);
1073 
1074  if (retval != ERROR_OK) {
1075  /* abort operation */
1076  goto done;
1077  }
1078 
1079  if (written)
1080  *written += run_size; /* add run size to total written counter */
1081  }
1082 
1083 done:
1084  free(sections);
1085  free(padding);
1086 
1087  return retval;
1088 }
1089 
1090 int flash_write(struct target *target, struct image *image,
1091  uint32_t *written, bool erase)
1092 {
1093  return flash_write_unlock_verify(target, image, written, erase, false, true, false);
1094 }
1095 
1096 struct flash_sector *alloc_block_array(uint32_t offset, uint32_t size,
1097  unsigned int num_blocks)
1098 {
1099  struct flash_sector *array = calloc(num_blocks, sizeof(struct flash_sector));
1100  if (!array)
1101  return NULL;
1102 
1103  for (unsigned int i = 0; i < num_blocks; i++) {
1104  array[i].offset = offset;
1105  array[i].size = size;
1106  array[i].is_erased = -1;
1107  array[i].is_protected = -1;
1108  offset += size;
1109  }
1110 
1111  return array;
1112 }
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
enum target_state state
Definition: target.h:167
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
#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