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