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