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