1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
3 * Copyright (C) 2007-2010 Øyvind Harboe <oyvind.harboe@zylin.com> *
4 * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
5 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
6 * Copyright (C) 2010 by Antonio Borneo <borneo.antonio@gmail.com> *
7 * Copyright (C) 2017-2018 Tomas Vanek <vanekt@fbl.cz> *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
21 ***************************************************************************/
26 #include <flash/common.h>
27 #include <flash/nor/core.h>
28 #include <flash/nor/imp.h>
29 #include <target/image.h>
33 * Upper level of NOR flash framework.
34 * The lower level interfaces are to drivers. These upper level ones
35 * primarily support access from Tcl scripts or from GDB.
38 static struct flash_bank *flash_banks;
40 int flash_driver_erase(struct flash_bank *bank, int first, int last)
44 retval = bank->driver->erase(bank, first, last);
45 if (retval != ERROR_OK)
46 LOG_ERROR("failed erasing sectors %d to %d", first, last);
51 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
56 if (bank->num_prot_blocks)
57 num_blocks = bank->num_prot_blocks;
59 num_blocks = bank->num_sectors;
62 /* callers may not supply illegal parameters ... */
63 if (first < 0 || first > last || last >= num_blocks) {
64 LOG_ERROR("illegal protection block range");
68 /* force "set" to 0/1 */
73 * We must not use any cached information about protection state!!!!
75 * There are a million things that could change the protect state:
77 * the target could have reset, power cycled, been hot plugged,
78 * the application could have run, etc.
80 * Drivers only receive valid protection block range.
82 retval = bank->driver->protect(bank, set, first, last);
83 if (retval != ERROR_OK)
84 LOG_ERROR("failed setting protection for blocks %d to %d", first, last);
89 int flash_driver_write(struct flash_bank *bank,
90 uint8_t *buffer, uint32_t offset, uint32_t count)
94 retval = bank->driver->write(bank, buffer, offset, count);
95 if (retval != ERROR_OK) {
97 "error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32,
105 int flash_driver_read(struct flash_bank *bank,
106 uint8_t *buffer, uint32_t offset, uint32_t count)
110 LOG_DEBUG("call flash_driver_read()");
112 retval = bank->driver->read(bank, buffer, offset, count);
113 if (retval != ERROR_OK) {
115 "error reading to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32,
123 int default_flash_read(struct flash_bank *bank,
124 uint8_t *buffer, uint32_t offset, uint32_t count)
126 return target_read_buffer(bank->target, offset + bank->base, count, buffer);
129 void flash_bank_add(struct flash_bank *bank)
131 /* put flash bank in linked list */
132 unsigned bank_num = 0;
134 /* find last flash bank */
135 struct flash_bank *p = flash_banks;
136 while (NULL != p->next) {
145 bank->bank_number = bank_num;
148 struct flash_bank *flash_bank_list(void)
153 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
155 struct flash_bank *p;
158 for (p = flash_banks; p; p = p->next) {
162 LOG_ERROR("flash bank %d does not exist", num);
166 int flash_get_bank_count(void)
168 struct flash_bank *p;
170 for (p = flash_banks; p; p = p->next)
175 void default_flash_free_driver_priv(struct flash_bank *bank)
177 free(bank->driver_priv);
178 bank->driver_priv = NULL;
181 void flash_free_all_banks(void)
183 struct flash_bank *bank = flash_banks;
185 struct flash_bank *next = bank->next;
186 if (bank->driver->free_driver_priv)
187 bank->driver->free_driver_priv(bank);
189 LOG_WARNING("Flash driver of %s does not support free_driver_priv()", bank->name);
193 free(bank->prot_blocks);
200 struct flash_bank *get_flash_bank_by_name_noprobe(const char *name)
202 unsigned requested = get_flash_name_index(name);
205 struct flash_bank *bank;
206 for (bank = flash_banks; NULL != bank; bank = bank->next) {
207 if (strcmp(bank->name, name) == 0)
209 if (!flash_driver_name_matches(bank->driver->name, name))
211 if (++found < requested)
218 int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
220 struct flash_bank *bank;
223 bank = get_flash_bank_by_name_noprobe(name);
225 retval = bank->driver->auto_probe(bank);
227 if (retval != ERROR_OK) {
228 LOG_ERROR("auto_probe failed");
237 int get_flash_bank_by_num(int num, struct flash_bank **bank)
239 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
245 retval = p->driver->auto_probe(p);
247 if (retval != ERROR_OK) {
248 LOG_ERROR("auto_probe failed");
255 /* lookup flash bank by address, bank not found is success, but
256 * result_bank is set to NULL. */
257 int get_flash_bank_by_addr(struct target *target,
260 struct flash_bank **result_bank)
262 struct flash_bank *c;
264 /* cycle through bank list */
265 for (c = flash_banks; c; c = c->next) {
266 if (c->target != target)
270 retval = c->driver->auto_probe(c);
272 if (retval != ERROR_OK) {
273 LOG_ERROR("auto_probe failed");
276 /* check whether address belongs to this flash bank */
277 if ((addr >= c->base) && (addr <= c->base + (c->size - 1))) {
284 LOG_ERROR("No flash at address 0x%08" PRIx32, addr);
290 static int default_flash_mem_blank_check(struct flash_bank *bank)
292 struct target *target = bank->target;
293 const int buffer_size = 1024;
296 int retval = ERROR_OK;
298 if (bank->target->state != TARGET_HALTED) {
299 LOG_ERROR("Target not halted");
300 return ERROR_TARGET_NOT_HALTED;
303 uint8_t *buffer = malloc(buffer_size);
305 for (i = 0; i < bank->num_sectors; i++) {
307 bank->sectors[i].is_erased = 1;
309 for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
312 if (chunk > (j - bank->sectors[i].size))
313 chunk = (j - bank->sectors[i].size);
315 retval = target_read_memory(target,
316 bank->base + bank->sectors[i].offset + j,
320 if (retval != ERROR_OK)
323 for (nBytes = 0; nBytes < chunk; nBytes++) {
324 if (buffer[nBytes] != bank->erased_value) {
325 bank->sectors[i].is_erased = 0;
338 int default_flash_blank_check(struct flash_bank *bank)
340 struct target *target = bank->target;
344 if (bank->target->state != TARGET_HALTED) {
345 LOG_ERROR("Target not halted");
346 return ERROR_TARGET_NOT_HALTED;
349 struct target_memory_check_block *block_array;
350 block_array = malloc(bank->num_sectors * sizeof(struct target_memory_check_block));
351 if (block_array == NULL)
352 return default_flash_mem_blank_check(bank);
354 for (i = 0; i < bank->num_sectors; i++) {
355 block_array[i].address = bank->base + bank->sectors[i].offset;
356 block_array[i].size = bank->sectors[i].size;
357 block_array[i].result = UINT32_MAX; /* erase state unknown */
360 bool fast_check = true;
361 for (i = 0; i < bank->num_sectors; ) {
362 retval = target_blank_check_memory(target,
363 block_array + i, bank->num_sectors - i,
366 /* Run slow fallback if the first run gives no result
367 * otherwise use possibly incomplete results */
372 i += retval; /* add number of blocks done this round */
376 for (i = 0; i < bank->num_sectors; i++)
377 bank->sectors[i].is_erased = block_array[i].result;
380 LOG_USER("Running slow fallback erase check - add working memory");
381 retval = default_flash_mem_blank_check(bank);
388 /* Manipulate given flash region, selecting the bank according to target
389 * and address. Maps an address range to a set of sectors, and issues
390 * the callback() on that set ... e.g. to erase or unprotect its members.
392 * Parameter iterate_protect_blocks switches iteration of protect block
393 * instead of erase sectors. If there is no protect blocks array, sectors
394 * are used in iteration, so compatibility for old flash drivers is retained.
396 * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
397 * range must fit those sectors exactly. This is clearly safe; it can't
398 * erase data which the caller said to leave alone, for example. If it's
399 * non-NULL, rather than failing, extra data in the first and/or last
400 * sectors will be added to the range, and that reason string is used when
401 * warning about those additions.
403 static int flash_iterate_address_range_inner(struct target *target,
404 char *pad_reason, uint32_t addr, uint32_t length,
405 bool iterate_protect_blocks,
406 int (*callback)(struct flash_bank *bank, int first, int last))
408 struct flash_bank *c;
409 struct flash_sector *block_array;
410 uint32_t last_addr = addr + length; /* first address AFTER end */
416 int retval = get_flash_bank_by_addr(target, addr, true, &c);
417 if (retval != ERROR_OK)
420 if (c->size == 0 || c->num_sectors == 0) {
421 LOG_ERROR("Bank is invalid");
422 return ERROR_FLASH_BANK_INVALID;
426 /* special case, erase whole bank when length is zero */
427 if (addr != c->base) {
428 LOG_ERROR("Whole bank access must start at beginning of bank.");
429 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
432 return callback(c, 0, c->num_sectors - 1);
435 /* check whether it all fits in this bank */
436 if (addr + length - 1 > c->base + c->size - 1) {
437 LOG_ERROR("Flash access does not fit into bank.");
438 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
441 if (c->prot_blocks == NULL || c->num_prot_blocks == 0) {
442 /* flash driver does not define protect blocks, use sectors instead */
443 iterate_protect_blocks = false;
446 if (iterate_protect_blocks) {
447 block_array = c->prot_blocks;
448 num_blocks = c->num_prot_blocks;
450 block_array = c->sectors;
451 num_blocks = c->num_sectors;
455 last_addr -= c->base;
457 for (i = 0; i < num_blocks; i++) {
458 struct flash_sector *f = &block_array[i];
459 uint32_t end = f->offset + f->size;
461 /* start only on a sector boundary */
463 /* scanned past the first sector? */
464 if (addr < f->offset)
467 /* is this the first sector? */
468 if (addr == f->offset)
471 /* Does this need head-padding? If so, pad and warn;
472 * or else force an error.
474 * Such padding can make trouble, since *WE* can't
475 * ever know if that data was in use. The warning
476 * should help users sort out messes later.
478 else if (addr < end && pad_reason) {
479 /* FIXME say how many bytes (e.g. 80 KB) */
480 LOG_WARNING("Adding extra %s range, "
483 (unsigned) f->offset,
484 (unsigned) addr - 1);
490 /* is this (also?) the last sector? */
491 if (last_addr == end) {
496 /* Does this need tail-padding? If so, pad and warn;
497 * or else force an error.
499 if (last_addr < end && pad_reason) {
500 /* FIXME say how many bytes (e.g. 80 KB) */
501 LOG_WARNING("Adding extra %s range, "
504 (unsigned) last_addr,
510 /* MUST finish on a sector boundary */
511 if (last_addr <= f->offset)
515 /* invalid start or end address? */
516 if (first == -1 || last == -1) {
517 LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
518 "is not sector-aligned",
519 (unsigned) (c->base + addr),
520 (unsigned) (c->base + last_addr - 1));
521 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
524 /* The NOR driver may trim this range down, based on what
525 * sectors are already erased/unprotected. GDB currently
526 * blocks such optimizations.
528 return callback(c, first, last);
531 /* The inner fn only handles a single bank, we could be spanning
534 static int flash_iterate_address_range(struct target *target,
535 char *pad_reason, uint32_t addr, uint32_t length,
536 bool iterate_protect_blocks,
537 int (*callback)(struct flash_bank *bank, int first, int last))
539 struct flash_bank *c;
540 int retval = ERROR_OK;
542 /* Danger! zero-length iterations means entire bank! */
544 retval = get_flash_bank_by_addr(target, addr, true, &c);
545 if (retval != ERROR_OK)
548 uint32_t cur_length = length;
549 /* check whether it all fits in this bank */
550 if (addr + length - 1 > c->base + c->size - 1) {
551 LOG_DEBUG("iterating over more than one flash bank.");
552 cur_length = c->base + c->size - addr;
554 retval = flash_iterate_address_range_inner(target,
555 pad_reason, addr, cur_length,
556 iterate_protect_blocks,
558 if (retval != ERROR_OK)
561 length -= cur_length;
563 } while (length > 0);
568 int flash_erase_address_range(struct target *target,
569 bool pad, uint32_t addr, uint32_t length)
571 return flash_iterate_address_range(target, pad ? "erase" : NULL,
572 addr, length, false, &flash_driver_erase);
575 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
577 return flash_driver_protect(bank, 0, first, last);
580 int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
582 /* By default, pad to sector boundaries ... the real issue here
583 * is that our (only) caller *permanently* removes protection,
584 * and doesn't restore it.
586 return flash_iterate_address_range(target, "unprotect",
587 addr, length, true, &flash_driver_unprotect);
590 static int compare_section(const void *a, const void *b)
592 struct imagesection *b1, *b2;
593 b1 = *((struct imagesection **)a);
594 b2 = *((struct imagesection **)b);
596 if (b1->base_address == b2->base_address)
598 else if (b1->base_address > b2->base_address)
605 * Get aligned start address of a flash write region
607 target_addr_t flash_write_align_start(struct flash_bank *bank, target_addr_t addr)
609 if (addr < bank->base || addr >= bank->base + bank->size
610 || bank->write_start_alignment <= 1)
613 if (bank->write_start_alignment == FLASH_WRITE_ALIGN_SECTOR) {
614 uint32_t offset = addr - bank->base;
615 uint32_t aligned = 0;
617 for (sect = 0; sect < bank->num_sectors; sect++) {
618 if (bank->sectors[sect].offset > offset)
621 aligned = bank->sectors[sect].offset;
623 return bank->base + aligned;
626 return addr & ~(bank->write_start_alignment - 1);
630 * Get aligned end address of a flash write region
632 target_addr_t flash_write_align_end(struct flash_bank *bank, target_addr_t addr)
634 if (addr < bank->base || addr >= bank->base + bank->size
635 || bank->write_end_alignment <= 1)
638 if (bank->write_end_alignment == FLASH_WRITE_ALIGN_SECTOR) {
639 uint32_t offset = addr - bank->base;
640 uint32_t aligned = 0;
642 for (sect = 0; sect < bank->num_sectors; sect++) {
643 aligned = bank->sectors[sect].offset + bank->sectors[sect].size - 1;
644 if (aligned >= offset)
647 return bank->base + aligned;
650 return addr | (bank->write_end_alignment - 1);
654 * Check if gap between sections is bigger than minimum required to discontinue flash write
656 static bool flash_write_check_gap(struct flash_bank *bank,
657 target_addr_t addr1, target_addr_t addr2)
659 if (bank->minimal_write_gap == FLASH_WRITE_CONTINUOUS
660 || addr1 < bank->base || addr1 >= bank->base + bank->size
661 || addr2 < bank->base || addr2 >= bank->base + bank->size)
664 if (bank->minimal_write_gap == FLASH_WRITE_GAP_SECTOR) {
666 uint32_t offset1 = addr1 - bank->base;
667 /* find the sector following the one containing addr1 */
668 for (sect = 0; sect < bank->num_sectors; sect++) {
669 if (bank->sectors[sect].offset > offset1)
672 if (sect >= bank->num_sectors)
675 uint32_t offset2 = addr2 - bank->base;
676 return bank->sectors[sect].offset + bank->sectors[sect].size <= offset2;
679 target_addr_t aligned1 = flash_write_align_end(bank, addr1);
680 target_addr_t aligned2 = flash_write_align_start(bank, addr2);
681 return aligned1 + bank->minimal_write_gap < aligned2;
685 int flash_write_unlock(struct target *target, struct image *image,
686 uint32_t *written, int erase, bool unlock)
688 int retval = ERROR_OK;
691 uint32_t section_offset;
692 struct flash_bank *c;
702 /* assume all sectors need erasing - stops any problems
703 * when flash_write is called multiple times */
708 /* allocate padding array */
709 padding = calloc(image->num_sections, sizeof(*padding));
711 /* This fn requires all sections to be in ascending order of addresses,
712 * whereas an image can have sections out of order. */
713 struct imagesection **sections = malloc(sizeof(struct imagesection *) *
714 image->num_sections);
716 for (i = 0; i < image->num_sections; i++)
717 sections[i] = &image->sections[i];
719 qsort(sections, image->num_sections, sizeof(struct imagesection *),
722 /* loop until we reach end of the image */
723 while (section < image->num_sections) {
727 target_addr_t run_address = sections[section]->base_address + section_offset;
728 uint32_t run_size = sections[section]->size - section_offset;
731 if (sections[section]->size == 0) {
732 LOG_WARNING("empty section %d", section);
738 /* find the corresponding flash bank */
739 retval = get_flash_bank_by_addr(target, run_address, false, &c);
740 if (retval != ERROR_OK)
743 LOG_WARNING("no flash bank found for address " TARGET_ADDR_FMT, run_address);
744 section++; /* and skip it */
749 /* collect consecutive sections which fall into the same bank */
750 section_last = section;
751 padding[section] = 0;
752 while ((run_address + run_size - 1 < c->base + c->size - 1) &&
753 (section_last + 1 < image->num_sections)) {
754 /* sections are sorted */
755 assert(sections[section_last + 1]->base_address >= c->base);
756 if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
757 /* Done with this bank */
761 /* if we have multiple sections within our image,
762 * flash programming could fail due to alignment issues
763 * attempt to rebuild a consecutive buffer for the flash loader */
764 target_addr_t run_next_addr = run_address + run_size;
765 target_addr_t next_section_base = sections[section_last + 1]->base_address;
766 if (next_section_base < run_next_addr) {
767 LOG_ERROR("Section at " TARGET_ADDR_FMT
768 " overlaps section ending at " TARGET_ADDR_FMT,
769 next_section_base, run_next_addr);
770 LOG_ERROR("Flash write aborted.");
775 pad_bytes = next_section_base - run_next_addr;
777 if (flash_write_check_gap(c, run_next_addr - 1, next_section_base)) {
778 LOG_INFO("Flash write discontinued at " TARGET_ADDR_FMT
779 ", next section at " TARGET_ADDR_FMT,
780 run_next_addr, next_section_base);
785 LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
787 section_last, run_next_addr, pad_bytes);
789 padding[section_last] = pad_bytes;
790 run_size += pad_bytes;
791 run_size += sections[++section_last]->size;
794 if (run_address + run_size - 1 > c->base + c->size - 1) {
795 /* If we have more than one flash chip back to back, then we limit
796 * the current write operation to the current chip.
798 LOG_DEBUG("Truncate flash run size to the current flash chip.");
800 run_size = c->base + c->size - run_address;
801 assert(run_size > 0);
804 uint32_t padding_at_start = 0;
805 if (c->write_start_alignment || c->write_end_alignment) {
806 /* align write region according to bank requirements */
807 target_addr_t aligned_start = flash_write_align_start(c, run_address);
808 padding_at_start = run_address - aligned_start;
809 if (padding_at_start > 0) {
810 LOG_WARNING("Section start address " TARGET_ADDR_FMT
811 " breaks the required alignment of flash bank %s",
812 run_address, c->name);
813 LOG_WARNING("Padding %d bytes from " TARGET_ADDR_FMT,
814 padding_at_start, aligned_start);
816 run_address -= padding_at_start;
817 run_size += padding_at_start;
820 target_addr_t run_end = run_address + run_size - 1;
821 target_addr_t aligned_end = flash_write_align_end(c, run_end);
822 pad_bytes = aligned_end - run_end;
824 LOG_INFO("Padding image section %d at " TARGET_ADDR_FMT
825 " with %d bytes (bank write end alignment)",
826 section_last, run_end + 1, pad_bytes);
828 padding[section_last] += pad_bytes;
829 run_size += pad_bytes;
832 } else if (unlock || erase) {
833 /* If we're applying any sector automagic, then pad this
834 * (maybe-combined) segment to the end of its last sector.
837 uint32_t offset_start = run_address - c->base;
838 uint32_t offset_end = offset_start + run_size;
839 uint32_t end = offset_end, delta;
841 for (sector = 0; sector < c->num_sectors; sector++) {
842 end = c->sectors[sector].offset
843 + c->sectors[sector].size;
844 if (offset_end <= end)
848 delta = end - offset_end;
849 padding[section_last] += delta;
853 /* allocate buffer */
854 buffer = malloc(run_size);
855 if (buffer == NULL) {
856 LOG_ERROR("Out of memory for flash bank buffer");
861 if (padding_at_start)
862 memset(buffer, c->default_padded_value, padding_at_start);
864 buffer_idx = padding_at_start;
866 /* read sections to the buffer */
867 while (buffer_idx < run_size) {
870 size_read = run_size - buffer_idx;
871 if (size_read > sections[section]->size - section_offset)
872 size_read = sections[section]->size - section_offset;
876 * #¤%#"%¤% we have to figure out the section # from the sorted
877 * list of pointers to sections to invoke image_read_section()...
879 intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
880 int t_section_num = diff / sizeof(struct imagesection);
882 LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
883 "section_offset = %"PRIu32", buffer_idx = %"PRIu32", size_read = %zu",
884 section, t_section_num, section_offset,
885 buffer_idx, size_read);
886 retval = image_read_section(image, t_section_num, section_offset,
887 size_read, buffer + buffer_idx, &size_read);
888 if (retval != ERROR_OK || size_read == 0) {
893 buffer_idx += size_read;
894 section_offset += size_read;
896 /* see if we need to pad the section */
897 if (padding[section]) {
898 memset(buffer + buffer_idx, c->default_padded_value, padding[section]);
899 buffer_idx += padding[section];
902 if (section_offset >= sections[section]->size) {
911 retval = flash_unlock_address_range(target, run_address, run_size);
912 if (retval == ERROR_OK) {
914 /* calculate and erase sectors */
915 retval = flash_erase_address_range(target,
916 true, run_address, run_size);
920 if (retval == ERROR_OK) {
921 /* write flash sectors */
922 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
927 if (retval != ERROR_OK) {
928 /* abort operation */
933 *written += run_size; /* add run size to total written counter */
943 int flash_write(struct target *target, struct image *image,
944 uint32_t *written, int erase)
946 return flash_write_unlock(target, image, written, erase, false);
949 struct flash_sector *alloc_block_array(uint32_t offset, uint32_t size, int num_blocks)
953 struct flash_sector *array = calloc(num_blocks, sizeof(struct flash_sector));
957 for (i = 0; i < num_blocks; i++) {
958 array[i].offset = offset;
959 array[i].size = size;
960 array[i].is_erased = -1;
961 array[i].is_protected = -1;