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  int retval;
33 
34  retval = bank->driver->erase(bank, first, last);
35  if (retval != ERROR_OK)
36  LOG_ERROR("failed erasing sectors %u to %u", first, last);
37 
38  return retval;
39 }
40 
41 int flash_driver_protect(struct flash_bank *bank, int set, unsigned int first,
42  unsigned int last)
43 {
44  int retval;
45  unsigned int num_blocks;
46 
47  if (!bank->driver->protect) {
48  LOG_ERROR("Flash protection is not supported");
50  }
51 
52  if (bank->num_prot_blocks)
53  num_blocks = bank->num_prot_blocks;
54  else
55  num_blocks = bank->num_sectors;
56 
57 
58  /* callers may not supply illegal parameters ... */
59  if (first > last || last >= num_blocks) {
60  LOG_ERROR("illegal protection block range");
61  return ERROR_FAIL;
62  }
63 
64  /* force "set" to 0/1 */
65  set = !!set;
66 
67  /* DANGER!
68  *
69  * We must not use any cached information about protection state!!!!
70  *
71  * There are a million things that could change the protect state:
72  *
73  * the target could have reset, power cycled, been hot plugged,
74  * the application could have run, etc.
75  *
76  * Drivers only receive valid protection block range.
77  */
78  retval = bank->driver->protect(bank, set, first, last);
79  if (retval != ERROR_OK)
80  LOG_ERROR("failed setting protection for blocks %u to %u", first, last);
81 
82  return retval;
83 }
84 
86  const uint8_t *buffer, uint32_t offset, uint32_t count)
87 {
88  int retval;
89 
90  retval = bank->driver->write(bank, buffer, offset, count);
91  if (retval != ERROR_OK) {
92  LOG_ERROR(
93  "error writing to flash at address " TARGET_ADDR_FMT
94  " at offset 0x%8.8" PRIx32,
95  bank->base,
96  offset);
97  }
98 
99  return retval;
100 }
101 
103  uint8_t *buffer, uint32_t offset, uint32_t count)
104 {
105  int retval;
106 
107  LOG_DEBUG("call flash_driver_read()");
108 
109  retval = bank->driver->read(bank, buffer, offset, count);
110  if (retval != ERROR_OK) {
111  LOG_ERROR(
112  "error reading to flash at address " TARGET_ADDR_FMT
113  " at offset 0x%8.8" PRIx32,
114  bank->base,
115  offset);
116  }
117 
118  return retval;
119 }
120 
122  uint8_t *buffer, uint32_t offset, uint32_t count)
123 {
124  return target_read_buffer(bank->target, offset + bank->base, count, buffer);
125 }
126 
128  const uint8_t *buffer, uint32_t offset, uint32_t count)
129 {
130  int retval;
131 
132  if (bank->driver->verify)
133  retval = bank->driver->verify(bank, buffer, offset, count);
134  else
136  if (retval != ERROR_OK) {
137  LOG_ERROR("verify failed in bank at " TARGET_ADDR_FMT " starting at 0x%8.8" PRIx32,
138  bank->base, offset);
139  }
140 
141  return retval;
142 }
143 
145  const uint8_t *buffer, uint32_t offset, uint32_t count)
146 {
147  uint32_t target_crc, image_crc;
148  int retval;
149 
150  retval = image_calculate_checksum(buffer, count, &image_crc);
151  if (retval != ERROR_OK)
152  return retval;
153 
154  retval = target_checksum_memory(bank->target, offset + bank->base, count, &target_crc);
155  if (retval != ERROR_OK)
156  return retval;
157 
158  LOG_DEBUG("addr " TARGET_ADDR_FMT ", len 0x%08" PRIx32 ", crc 0x%08" PRIx32 " 0x%08" PRIx32,
159  offset + bank->base, count, ~image_crc, ~target_crc);
160  if (target_crc == image_crc)
161  return ERROR_OK;
162  else
163  return ERROR_FAIL;
164 }
165 
167 {
168  /* put flash bank in linked list */
169  unsigned int bank_num = 0;
170  if (flash_banks) {
171  /* find last flash bank */
172  struct flash_bank *p = flash_banks;
173  while (p->next) {
174  bank_num += 1;
175  p = p->next;
176  }
177  p->next = bank;
178  bank_num += 1;
179  } else
180  flash_banks = bank;
181 
182  bank->bank_number = bank_num;
183 }
184 
186 {
187  return flash_banks;
188 }
189 
190 struct flash_bank *get_flash_bank_by_num_noprobe(unsigned int num)
191 {
192  struct flash_bank *p;
193  unsigned int i = 0;
194 
195  for (p = flash_banks; p; p = p->next) {
196  if (i++ == num)
197  return p;
198  }
199  LOG_ERROR("flash bank %d does not exist", num);
200  return NULL;
201 }
202 
203 unsigned int flash_get_bank_count(void)
204 {
205  struct flash_bank *p;
206  unsigned int i = 0;
207  for (p = flash_banks; p; p = p->next)
208  i++;
209  return i;
210 }
211 
213 {
214  free(bank->driver_priv);
215  bank->driver_priv = NULL;
216 }
217 
219 {
220  struct flash_bank *bank = flash_banks;
221  while (bank) {
222  struct flash_bank *next = bank->next;
223  if (bank->driver->free_driver_priv)
224  bank->driver->free_driver_priv(bank);
225  else
227 
228  free(bank->sectors);
229  free(bank->prot_blocks);
230 
231  free(bank->name);
232  free(bank);
233  bank = next;
234  }
235  flash_banks = NULL;
236 }
237 
239 {
240  unsigned int requested = get_flash_name_index(name);
241  unsigned int found = 0;
242 
243  struct flash_bank *bank;
244  for (bank = flash_banks; bank; bank = bank->next) {
245  if (strcmp(bank->name, name) == 0)
246  return bank;
247  if (!flash_driver_name_matches(bank->driver->name, name))
248  continue;
249  if (++found < requested)
250  continue;
251  return bank;
252  }
253  return NULL;
254 }
255 
256 int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
257 {
258  struct flash_bank *bank;
259  int retval;
260 
262  if (bank) {
263  retval = bank->driver->auto_probe(bank);
264 
265  if (retval != ERROR_OK) {
266  LOG_ERROR("auto_probe failed");
267  return retval;
268  }
269  }
270 
271  *bank_result = bank;
272  return ERROR_OK;
273 }
274 
275 int get_flash_bank_by_num(unsigned int num, struct flash_bank **bank)
276 {
277  struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
278  int retval;
279 
280  if (!p)
281  return ERROR_FAIL;
282 
283  retval = p->driver->auto_probe(p);
284 
285  if (retval != ERROR_OK) {
286  LOG_ERROR("auto_probe failed");
287  return retval;
288  }
289  *bank = p;
290  return ERROR_OK;
291 }
292 
293 /* lookup flash bank by address, bank not found is success, but
294  * result_bank is set to NULL. */
297  bool check,
298  struct flash_bank **result_bank)
299 {
300  struct flash_bank *c;
301 
302  /* cycle through bank list */
303  for (c = flash_banks; c; c = c->next) {
304  if (c->target != target)
305  continue;
306 
307  int retval;
308  retval = c->driver->auto_probe(c);
309 
310  if (retval != ERROR_OK) {
311  LOG_ERROR("auto_probe failed");
312  return retval;
313  }
314  /* check whether address belongs to this flash bank */
315  if ((addr >= c->base) && (addr <= c->base + (c->size - 1))) {
316  *result_bank = c;
317  return ERROR_OK;
318  }
319  }
320  *result_bank = NULL;
321  if (check) {
322  LOG_ERROR("No flash at address " TARGET_ADDR_FMT, addr);
323  return ERROR_FAIL;
324  }
325  return ERROR_OK;
326 }
327 
329 {
330  struct target *target = bank->target;
331  const int buffer_size = 1024;
332  uint32_t n_bytes;
333  int retval = ERROR_OK;
334 
335  if (bank->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  uint32_t j;
348  bank->sectors[i].is_erased = 1;
349 
350  for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
351  uint32_t chunk;
352  chunk = buffer_size;
353  if (chunk > (bank->sectors[i].size - j))
354  chunk = (bank->sectors[i].size - j);
355 
356  retval = target_read_memory(target,
357  bank->base + bank->sectors[i].offset + j,
358  4,
359  chunk/4,
360  buffer);
361  if (retval != ERROR_OK)
362  goto done;
363 
364  for (n_bytes = 0; n_bytes < chunk; n_bytes++) {
365  if (buffer[n_bytes] != bank->erased_value) {
366  bank->sectors[i].is_erased = 0;
367  break;
368  }
369  }
370  }
371  }
372 
373 done:
374  free(buffer);
375 
376  return retval;
377 }
378 
380 {
381  struct target *target = bank->target;
382  int retval;
383 
384  if (bank->target->state != TARGET_HALTED) {
385  LOG_ERROR("Target not halted");
387  }
388 
389  struct target_memory_check_block *block_array;
390  block_array = malloc(bank->num_sectors * sizeof(struct target_memory_check_block));
391  if (!block_array) {
392  LOG_WARNING("Running slow fallback erase check - limited host memory available");
394  }
395 
396  for (unsigned int i = 0; i < bank->num_sectors; i++) {
397  block_array[i].address = bank->base + bank->sectors[i].offset;
398  block_array[i].size = bank->sectors[i].size;
399  block_array[i].result = UINT32_MAX; /* erase state unknown */
400  }
401 
402  bool fast_check = true;
403  for (unsigned int i = 0; i < bank->num_sectors; ) {
404  unsigned int checked;
406  block_array + i, bank->num_sectors - i,
407  bank->erased_value, &checked);
408  if (retval != ERROR_OK) {
409  /* Run slow fallback if the first run gives no result
410  * otherwise use possibly incomplete results */
411  if (i == 0)
412  fast_check = false;
413  break;
414  }
415  i += checked; /* add number of blocks done this round */
416  }
417 
418  if (fast_check) {
419  for (unsigned int i = 0; i < bank->num_sectors; i++)
420  bank->sectors[i].is_erased = block_array[i].result;
421  retval = ERROR_OK;
422  } else {
423  if (retval == ERROR_NOT_IMPLEMENTED)
424  LOG_DEBUG("Running slow fallback erase check");
425  else
426  LOG_WARNING("Running slow fallback erase check - add working memory");
427 
429  }
430  free(block_array);
431 
432  return retval;
433 }
434 
435 /* Manipulate given flash region, selecting the bank according to target
436  * and address. Maps an address range to a set of sectors, and issues
437  * the callback() on that set ... e.g. to erase or unprotect its members.
438  *
439  * Parameter iterate_protect_blocks switches iteration of protect block
440  * instead of erase sectors. If there is no protect blocks array, sectors
441  * are used in iteration, so compatibility for old flash drivers is retained.
442  *
443  * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
444  * range must fit those sectors exactly. This is clearly safe; it can't
445  * erase data which the caller said to leave alone, for example. If it's
446  * non-NULL, rather than failing, extra data in the first and/or last
447  * sectors will be added to the range, and that reason string is used when
448  * warning about those additions.
449  */
451  char *pad_reason, target_addr_t addr, uint32_t length,
452  bool iterate_protect_blocks,
453  int (*callback)(struct flash_bank *bank, unsigned int first,
454  unsigned int last))
455 {
456  struct flash_bank *c;
457  struct flash_sector *block_array;
458  target_addr_t last_addr = addr + length - 1; /* the last address of range */
459  int first = -1;
460  int last = -1;
461  int i;
462  int num_blocks;
463 
464  int retval = get_flash_bank_by_addr(target, addr, true, &c);
465  if (retval != ERROR_OK)
466  return retval;
467 
468  if (c->size == 0 || c->num_sectors == 0) {
469  LOG_ERROR("Bank is invalid");
471  }
472 
473  if (length == 0) {
474  /* special case, erase whole bank when length is zero */
475  if (addr != c->base) {
476  LOG_ERROR("Whole bank access must start at beginning of bank.");
478  }
479 
480  return callback(c, 0, c->num_sectors - 1);
481  }
482 
483  /* check whether it all fits in this bank */
484  if (last_addr > c->base + c->size - 1) {
485  LOG_ERROR("Flash access does not fit into bank.");
487  }
488 
489  if (!c->prot_blocks || c->num_prot_blocks == 0) {
490  /* flash driver does not define protect blocks, use sectors instead */
491  iterate_protect_blocks = false;
492  }
493 
494  if (iterate_protect_blocks) {
495  block_array = c->prot_blocks;
496  num_blocks = c->num_prot_blocks;
497  } else {
498  block_array = c->sectors;
499  num_blocks = c->num_sectors;
500  }
501 
502  for (i = 0; i < num_blocks; i++) {
503  struct flash_sector *f = &block_array[i];
504  target_addr_t sector_addr = c->base + f->offset;
505  target_addr_t sector_last_addr = sector_addr + f->size - 1;
506 
507  /* start only on a sector boundary */
508  if (first < 0) {
509  /* scanned past the first sector? */
510  if (addr < sector_addr)
511  break;
512 
513  /* is this the first sector? */
514  if (addr == sector_addr)
515  first = i;
516 
517  /* Does this need head-padding? If so, pad and warn;
518  * or else force an error.
519  *
520  * Such padding can make trouble, since *WE* can't
521  * ever know if that data was in use. The warning
522  * should help users sort out messes later.
523  */
524  else if (addr <= sector_last_addr && pad_reason) {
525  /* FIXME say how many bytes (e.g. 80 KB) */
526  LOG_WARNING("Adding extra %s range, "
528  pad_reason,
529  sector_addr,
530  addr - 1);
531  first = i;
532  } else
533  continue;
534  }
535 
536  /* is this (also?) the last sector? */
537  if (last_addr == sector_last_addr) {
538  last = i;
539  break;
540  }
541 
542  /* Does this need tail-padding? If so, pad and warn;
543  * or else force an error.
544  */
545  if (last_addr < sector_last_addr && pad_reason) {
546  /* FIXME say how many bytes (e.g. 80 KB) */
547  LOG_WARNING("Adding extra %s range, "
549  pad_reason,
550  last_addr + 1,
551  sector_last_addr);
552  last = i;
553  break;
554  }
555 
556  /* MUST finish on a sector boundary */
557  if (last_addr < sector_addr)
558  break;
559  }
560 
561  /* invalid start or end address? */
562  if (first == -1 || last == -1) {
563  LOG_ERROR("address range " TARGET_ADDR_FMT " .. " TARGET_ADDR_FMT
564  " is not sector-aligned",
565  addr,
566  last_addr);
568  }
569 
570  /* The NOR driver may trim this range down, based on what
571  * sectors are already erased/unprotected. GDB currently
572  * blocks such optimizations.
573  */
574  return callback(c, first, last);
575 }
576 
577 /* The inner fn only handles a single bank, we could be spanning
578  * multiple chips.
579  */
581  char *pad_reason, target_addr_t addr, uint32_t length,
582  bool iterate_protect_blocks,
583  int (*callback)(struct flash_bank *bank, unsigned int first,
584  unsigned int last))
585 {
586  struct flash_bank *c;
587  int retval = ERROR_OK;
588 
589  /* Danger! zero-length iterations means entire bank! */
590  do {
591  retval = get_flash_bank_by_addr(target, addr, true, &c);
592  if (retval != ERROR_OK)
593  return retval;
594 
595  uint32_t cur_length = length;
596  /* check whether it all fits in this bank */
597  if (addr + length - 1 > c->base + c->size - 1) {
598  LOG_DEBUG("iterating over more than one flash bank.");
599  cur_length = c->base + c->size - addr;
600  }
602  pad_reason, addr, cur_length,
603  iterate_protect_blocks,
604  callback);
605  if (retval != ERROR_OK)
606  break;
607 
608  length -= cur_length;
609  addr += cur_length;
610  } while (length > 0);
611 
612  return retval;
613 }
614 
616  bool pad, target_addr_t addr, uint32_t length)
617 {
618  return flash_iterate_address_range(target, pad ? "erase" : NULL,
619  addr, length, false, &flash_driver_erase);
620 }
621 
622 static int flash_driver_unprotect(struct flash_bank *bank, unsigned int first,
623  unsigned int last)
624 {
625  return flash_driver_protect(bank, 0, first, last);
626 }
627 
629  uint32_t length)
630 {
631  /* By default, pad to sector boundaries ... the real issue here
632  * is that our (only) caller *permanently* removes protection,
633  * and doesn't restore it.
634  */
635  return flash_iterate_address_range(target, "unprotect",
637 }
638 
639 static int compare_section(const void *a, const void *b)
640 {
641  struct imagesection *b1, *b2;
642  b1 = *((struct imagesection **)a);
643  b2 = *((struct imagesection **)b);
644 
645  if (b1->base_address == b2->base_address)
646  return 0;
647  else if (b1->base_address > b2->base_address)
648  return 1;
649  else
650  return -1;
651 }
652 
657 {
658  if (addr < bank->base || addr >= bank->base + bank->size
659  || bank->write_start_alignment <= 1)
660  return addr;
661 
662  if (bank->write_start_alignment == FLASH_WRITE_ALIGN_SECTOR) {
663  uint32_t offset = addr - bank->base;
664  uint32_t aligned = 0;
665  for (unsigned int sect = 0; sect < bank->num_sectors; sect++) {
666  if (bank->sectors[sect].offset > offset)
667  break;
668 
669  aligned = bank->sectors[sect].offset;
670  }
671  return bank->base + aligned;
672  }
673 
674  return addr & ~(bank->write_start_alignment - 1);
675 }
676 
681 {
682  if (addr < bank->base || addr >= bank->base + bank->size
683  || bank->write_end_alignment <= 1)
684  return addr;
685 
686  if (bank->write_end_alignment == FLASH_WRITE_ALIGN_SECTOR) {
687  uint32_t offset = addr - bank->base;
688  uint32_t aligned = 0;
689  for (unsigned int sect = 0; sect < bank->num_sectors; sect++) {
690  aligned = bank->sectors[sect].offset + bank->sectors[sect].size - 1;
691  if (aligned >= offset)
692  break;
693  }
694  return bank->base + aligned;
695  }
696 
697  return addr | (bank->write_end_alignment - 1);
698 }
699 
704  target_addr_t addr1, target_addr_t addr2)
705 {
706  if (bank->minimal_write_gap == FLASH_WRITE_CONTINUOUS
707  || addr1 < bank->base || addr1 >= bank->base + bank->size
708  || addr2 < bank->base || addr2 >= bank->base + bank->size)
709  return false;
710 
711  if (bank->minimal_write_gap == FLASH_WRITE_GAP_SECTOR) {
712  unsigned int sect;
713  uint32_t offset1 = addr1 - bank->base;
714  /* find the sector following the one containing addr1 */
715  for (sect = 0; sect < bank->num_sectors; sect++) {
716  if (bank->sectors[sect].offset > offset1)
717  break;
718  }
719  if (sect >= bank->num_sectors)
720  return false;
721 
722  uint32_t offset2 = addr2 - bank->base;
723  return bank->sectors[sect].offset + bank->sectors[sect].size <= offset2;
724  }
725 
726  target_addr_t aligned1 = flash_write_align_end(bank, addr1);
727  target_addr_t aligned2 = flash_write_align_start(bank, addr2);
728  return aligned1 + bank->minimal_write_gap < aligned2;
729 }
730 
731 
733  uint32_t *written, bool erase, bool unlock, bool write, bool verify)
734 {
735  int retval = ERROR_OK;
736 
737  unsigned int section;
738  uint32_t section_offset;
739  struct flash_bank *c;
740 
741  section = 0;
742  section_offset = 0;
743 
744  /* allocate padding array */
745  int *padding = calloc(image->num_sections, sizeof(*padding));
746 
747  /* This fn requires all sections to be in ascending order of addresses,
748  * whereas an image can have sections out of order. */
749  struct imagesection **sections = malloc(sizeof(struct imagesection *) *
751 
752  if (!padding || !sections) {
753  LOG_ERROR("Out of memory");
754  free(padding);
755  free(sections);
756  return ERROR_FAIL;
757  }
758 
759  if (written)
760  *written = 0;
761 
762  if (erase) {
763  /* assume all sectors need erasing - stops any problems
764  * when flash_write is called multiple times */
765 
766  flash_set_dirty();
767  }
768 
769  for (unsigned int i = 0; i < image->num_sections; i++)
770  sections[i] = &image->sections[i];
771 
772  qsort(sections, image->num_sections, sizeof(struct imagesection *),
774 
775  /* loop until we reach end of the image */
776  while (section < image->num_sections) {
777  uint32_t buffer_idx;
778  uint8_t *buffer;
779  unsigned int section_last;
780  target_addr_t run_address = sections[section]->base_address + section_offset;
781  uint32_t run_size = sections[section]->size - section_offset;
782  int pad_bytes = 0;
783 
784  if (sections[section]->size == 0) {
785  LOG_WARNING("empty section %d", section);
786  section++;
787  section_offset = 0;
788  continue;
789  }
790 
791  /* find the corresponding flash bank */
792  retval = get_flash_bank_by_addr(target, run_address, false, &c);
793  if (retval != ERROR_OK)
794  goto done;
795  if (!c) {
796  LOG_WARNING("no flash bank found for address " TARGET_ADDR_FMT, run_address);
797  section++; /* and skip it */
798  section_offset = 0;
799  continue;
800  }
801 
802  /* collect consecutive sections which fall into the same bank */
803  section_last = section;
804  padding[section] = 0;
805  while ((run_address + run_size - 1 < c->base + c->size - 1) &&
806  (section_last + 1 < image->num_sections)) {
807  /* sections are sorted */
808  assert(sections[section_last + 1]->base_address >= c->base);
809  if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
810  /* Done with this bank */
811  break;
812  }
813 
814  /* if we have multiple sections within our image,
815  * flash programming could fail due to alignment issues
816  * attempt to rebuild a consecutive buffer for the flash loader */
817  target_addr_t run_next_addr = run_address + run_size;
818  target_addr_t next_section_base = sections[section_last + 1]->base_address;
819  if (next_section_base < run_next_addr) {
820  LOG_ERROR("Section at " TARGET_ADDR_FMT
821  " overlaps section ending at " TARGET_ADDR_FMT,
822  next_section_base, run_next_addr);
823  LOG_ERROR("Flash write aborted.");
824  retval = ERROR_FAIL;
825  goto done;
826  }
827 
828  pad_bytes = next_section_base - run_next_addr;
829  if (pad_bytes) {
830  if (flash_write_check_gap(c, run_next_addr - 1, next_section_base)) {
831  LOG_INFO("Flash write discontinued at " TARGET_ADDR_FMT
832  ", next section at " TARGET_ADDR_FMT,
833  run_next_addr, next_section_base);
834  break;
835  }
836  }
837  if (pad_bytes > 0)
838  LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
839  " with %d bytes",
840  section_last, run_next_addr, pad_bytes);
841 
842  padding[section_last] = pad_bytes;
843  run_size += pad_bytes;
844  run_size += sections[++section_last]->size;
845  }
846 
847  if (run_address + run_size - 1 > c->base + c->size - 1) {
848  /* If we have more than one flash chip back to back, then we limit
849  * the current write operation to the current chip.
850  */
851  LOG_DEBUG("Truncate flash run size to the current flash chip.");
852 
853  run_size = c->base + c->size - run_address;
854  assert(run_size > 0);
855  }
856 
857  uint32_t padding_at_start = 0;
859  /* align write region according to bank requirements */
860  target_addr_t aligned_start = flash_write_align_start(c, run_address);
861  padding_at_start = run_address - aligned_start;
862  if (padding_at_start > 0) {
863  LOG_WARNING("Section start address " TARGET_ADDR_FMT
864  " breaks the required alignment of flash bank %s",
865  run_address, c->name);
866  LOG_WARNING("Padding %" PRIu32 " bytes from " TARGET_ADDR_FMT,
867  padding_at_start, aligned_start);
868 
869  run_address -= padding_at_start;
870  run_size += padding_at_start;
871  }
872 
873  target_addr_t run_end = run_address + run_size - 1;
874  target_addr_t aligned_end = flash_write_align_end(c, run_end);
875  pad_bytes = aligned_end - run_end;
876  if (pad_bytes > 0) {
877  LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
878  " with %d bytes (bank write end alignment)",
879  section_last, run_end + 1, pad_bytes);
880 
881  padding[section_last] += pad_bytes;
882  run_size += pad_bytes;
883  }
884 
885  } else if (unlock || erase) {
886  /* If we're applying any sector automagic, then pad this
887  * (maybe-combined) segment to the end of its last sector.
888  */
889  uint32_t offset_start = run_address - c->base;
890  uint32_t offset_end = offset_start + run_size;
891  uint32_t end = offset_end, delta;
892 
893  for (unsigned int sector = 0; sector < c->num_sectors; sector++) {
894  end = c->sectors[sector].offset
895  + c->sectors[sector].size;
896  if (offset_end <= end)
897  break;
898  }
899 
900  delta = end - offset_end;
901  padding[section_last] += delta;
902  run_size += delta;
903  }
904 
905  /* allocate buffer */
906  buffer = malloc(run_size);
907  if (!buffer) {
908  LOG_ERROR("Out of memory for flash bank buffer");
909  retval = ERROR_FAIL;
910  goto done;
911  }
912 
913  if (padding_at_start)
914  memset(buffer, c->default_padded_value, padding_at_start);
915 
916  buffer_idx = padding_at_start;
917 
918  /* read sections to the buffer */
919  while (buffer_idx < run_size) {
920  size_t size_read;
921 
922  size_read = run_size - buffer_idx;
923  if (size_read > sections[section]->size - section_offset)
924  size_read = sections[section]->size - section_offset;
925 
926  /* KLUDGE!
927  *
928  * #¤%#"%¤% we have to figure out the section # from the sorted
929  * list of pointers to sections to invoke image_read_section()...
930  */
931  intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
932  int t_section_num = diff / sizeof(struct imagesection);
933 
934  LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
935  "section_offset = %"PRIu32", buffer_idx = %"PRIu32", size_read = %zu",
936  section, t_section_num, section_offset,
937  buffer_idx, size_read);
938  retval = image_read_section(image, t_section_num, section_offset,
939  size_read, buffer + buffer_idx, &size_read);
940  if (retval != ERROR_OK || size_read == 0) {
941  free(buffer);
942  goto done;
943  }
944 
945  buffer_idx += size_read;
946  section_offset += size_read;
947 
948  /* see if we need to pad the section */
949  if (padding[section]) {
950  memset(buffer + buffer_idx, c->default_padded_value, padding[section]);
951  buffer_idx += padding[section];
952  }
953 
954  if (section_offset >= sections[section]->size) {
955  section++;
956  section_offset = 0;
957  }
958  }
959 
960  retval = ERROR_OK;
961 
962  if (unlock)
963  retval = flash_unlock_address_range(target, run_address, run_size);
964  if (retval == ERROR_OK) {
965  if (erase) {
966  /* calculate and erase sectors */
968  true, run_address, run_size);
969  }
970  }
971 
972  if (retval == ERROR_OK) {
973  if (write) {
974  /* write flash sectors */
975  retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
976  }
977  }
978 
979  if (retval == ERROR_OK) {
980  if (verify) {
981  /* verify flash sectors */
982  retval = flash_driver_verify(c, buffer, run_address - c->base, run_size);
983  }
984  }
985 
986  free(buffer);
987 
988  if (retval != ERROR_OK) {
989  /* abort operation */
990  goto done;
991  }
992 
993  if (written)
994  *written += run_size; /* add run size to total written counter */
995  }
996 
997 done:
998  free(sections);
999  free(padding);
1000 
1001  return retval;
1002 }
1003 
1004 int flash_write(struct target *target, struct image *image,
1005  uint32_t *written, bool erase)
1006 {
1007  return flash_write_unlock_verify(target, image, written, erase, false, true, false);
1008 }
1009 
1010 struct flash_sector *alloc_block_array(uint32_t offset, uint32_t size,
1011  unsigned int num_blocks)
1012 {
1013  struct flash_sector *array = calloc(num_blocks, sizeof(struct flash_sector));
1014  if (!array)
1015  return NULL;
1016 
1017  for (unsigned int i = 0; i < num_blocks; i++) {
1018  array[i].offset = offset;
1019  array[i].size = size;
1020  array[i].is_erased = -1;
1021  array[i].is_protected = -1;
1022  offset += size;
1023  }
1024 
1025  return array;
1026 }
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, 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)
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 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_erase_address_range(struct target *target, bool pad, target_addr_t addr, uint32_t length)
Erases length bytes in the target flash, starting at addr.
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_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
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:126
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:128
struct flash_bank * next
The next flash bank on this chip.
Definition: nor/core.h:130
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:222
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