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> *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
25 #include <flash/common.h>
26 #include <flash/nor/core.h>
27 #include <flash/nor/imp.h>
28 #include <target/image.h>
32 * Upper level of NOR flash framework.
33 * The lower level interfaces are to drivers. These upper level ones
34 * primarily support access from Tcl scripts or from GDB.
37 static struct flash_bank *flash_banks;
39 int flash_driver_erase(struct flash_bank *bank, int first, int last)
43 retval = bank->driver->erase(bank, first, last);
44 if (retval != ERROR_OK)
45 LOG_ERROR("failed erasing sectors %d to %d", first, last);
50 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
55 if (bank->num_prot_blocks)
56 num_blocks = bank->num_prot_blocks;
58 num_blocks = bank->num_sectors;
61 /* callers may not supply illegal parameters ... */
62 if (first < 0 || first > last || last >= num_blocks) {
63 LOG_ERROR("illegal protection block range");
67 /* force "set" to 0/1 */
72 * We must not use any cached information about protection state!!!!
74 * There are a million things that could change the protect state:
76 * the target could have reset, power cycled, been hot plugged,
77 * the application could have run, etc.
79 * Drivers only receive valid protection block range.
81 retval = bank->driver->protect(bank, set, first, last);
82 if (retval != ERROR_OK)
83 LOG_ERROR("failed setting protection for blocks %d to %d", first, last);
88 int flash_driver_write(struct flash_bank *bank,
89 uint8_t *buffer, uint32_t offset, uint32_t count)
93 retval = bank->driver->write(bank, buffer, offset, count);
94 if (retval != ERROR_OK) {
96 "error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32,
104 int flash_driver_read(struct flash_bank *bank,
105 uint8_t *buffer, uint32_t offset, uint32_t count)
109 LOG_DEBUG("call flash_driver_read()");
111 retval = bank->driver->read(bank, buffer, offset, count);
112 if (retval != ERROR_OK) {
114 "error reading to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32,
122 int default_flash_read(struct flash_bank *bank,
123 uint8_t *buffer, uint32_t offset, uint32_t count)
125 return target_read_buffer(bank->target, offset + bank->base, count, buffer);
128 void flash_bank_add(struct flash_bank *bank)
130 /* put flash bank in linked list */
131 unsigned bank_num = 0;
133 /* find last flash bank */
134 struct flash_bank *p = flash_banks;
135 while (NULL != p->next) {
144 bank->bank_number = bank_num;
147 struct flash_bank *flash_bank_list(void)
152 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
154 struct flash_bank *p;
157 for (p = flash_banks; p; p = p->next) {
161 LOG_ERROR("flash bank %d does not exist", num);
165 int flash_get_bank_count(void)
167 struct flash_bank *p;
169 for (p = flash_banks; p; p = p->next)
174 void default_flash_free_driver_priv(struct flash_bank *bank)
176 free(bank->driver_priv);
177 bank->driver_priv = NULL;
180 void flash_free_all_banks(void)
182 struct flash_bank *bank = flash_banks;
184 struct flash_bank *next = bank->next;
185 if (bank->driver->free_driver_priv)
186 bank->driver->free_driver_priv(bank);
188 LOG_WARNING("Flash driver of %s does not support free_driver_priv()", bank->name);
192 free(bank->prot_blocks);
199 struct flash_bank *get_flash_bank_by_name_noprobe(const char *name)
201 unsigned requested = get_flash_name_index(name);
204 struct flash_bank *bank;
205 for (bank = flash_banks; NULL != bank; bank = bank->next) {
206 if (strcmp(bank->name, name) == 0)
208 if (!flash_driver_name_matches(bank->driver->name, name))
210 if (++found < requested)
217 int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
219 struct flash_bank *bank;
222 bank = get_flash_bank_by_name_noprobe(name);
224 retval = bank->driver->auto_probe(bank);
226 if (retval != ERROR_OK) {
227 LOG_ERROR("auto_probe failed");
236 int get_flash_bank_by_num(int num, struct flash_bank **bank)
238 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
244 retval = p->driver->auto_probe(p);
246 if (retval != ERROR_OK) {
247 LOG_ERROR("auto_probe failed");
254 /* lookup flash bank by address, bank not found is success, but
255 * result_bank is set to NULL. */
256 int get_flash_bank_by_addr(struct target *target,
259 struct flash_bank **result_bank)
261 struct flash_bank *c;
263 /* cycle through bank list */
264 for (c = flash_banks; c; c = c->next) {
265 if (c->target != target)
269 retval = c->driver->auto_probe(c);
271 if (retval != ERROR_OK) {
272 LOG_ERROR("auto_probe failed");
275 /* check whether address belongs to this flash bank */
276 if ((addr >= c->base) && (addr <= c->base + (c->size - 1))) {
283 LOG_ERROR("No flash at address 0x%08" PRIx32, addr);
289 static int default_flash_mem_blank_check(struct flash_bank *bank)
291 struct target *target = bank->target;
292 const int buffer_size = 1024;
295 int retval = ERROR_OK;
297 if (bank->target->state != TARGET_HALTED) {
298 LOG_ERROR("Target not halted");
299 return ERROR_TARGET_NOT_HALTED;
302 uint8_t *buffer = malloc(buffer_size);
304 for (i = 0; i < bank->num_sectors; i++) {
306 bank->sectors[i].is_erased = 1;
308 for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
311 if (chunk > (j - bank->sectors[i].size))
312 chunk = (j - bank->sectors[i].size);
314 retval = target_read_memory(target,
315 bank->base + bank->sectors[i].offset + j,
319 if (retval != ERROR_OK)
322 for (nBytes = 0; nBytes < chunk; nBytes++) {
323 if (buffer[nBytes] != bank->erased_value) {
324 bank->sectors[i].is_erased = 0;
337 int default_flash_blank_check(struct flash_bank *bank)
339 struct target *target = bank->target;
345 if (bank->target->state != TARGET_HALTED) {
346 LOG_ERROR("Target not halted");
347 return ERROR_TARGET_NOT_HALTED;
350 for (i = 0; i < bank->num_sectors; i++) {
351 uint32_t address = bank->base + bank->sectors[i].offset;
352 uint32_t size = bank->sectors[i].size;
354 retval = target_blank_check_memory(target, address, size, &blank, bank->erased_value);
355 if (retval != ERROR_OK) {
359 if (blank == bank->erased_value)
360 bank->sectors[i].is_erased = 1;
362 bank->sectors[i].is_erased = 0;
367 LOG_USER("Running slow fallback erase check - add working memory");
368 return default_flash_mem_blank_check(bank);
374 /* Manipulate given flash region, selecting the bank according to target
375 * and address. Maps an address range to a set of sectors, and issues
376 * the callback() on that set ... e.g. to erase or unprotect its members.
378 * Parameter iterate_protect_blocks switches iteration of protect block
379 * instead of erase sectors. If there is no protect blocks array, sectors
380 * are used in iteration, so compatibility for old flash drivers is retained.
382 * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
383 * range must fit those sectors exactly. This is clearly safe; it can't
384 * erase data which the caller said to leave alone, for example. If it's
385 * non-NULL, rather than failing, extra data in the first and/or last
386 * sectors will be added to the range, and that reason string is used when
387 * warning about those additions.
389 static int flash_iterate_address_range_inner(struct target *target,
390 char *pad_reason, uint32_t addr, uint32_t length,
391 bool iterate_protect_blocks,
392 int (*callback)(struct flash_bank *bank, int first, int last))
394 struct flash_bank *c;
395 struct flash_sector *block_array;
396 uint32_t last_addr = addr + length; /* first address AFTER end */
402 int retval = get_flash_bank_by_addr(target, addr, true, &c);
403 if (retval != ERROR_OK)
406 if (c->size == 0 || c->num_sectors == 0) {
407 LOG_ERROR("Bank is invalid");
408 return ERROR_FLASH_BANK_INVALID;
412 /* special case, erase whole bank when length is zero */
413 if (addr != c->base) {
414 LOG_ERROR("Whole bank access must start at beginning of bank.");
415 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
418 return callback(c, 0, c->num_sectors - 1);
421 /* check whether it all fits in this bank */
422 if (addr + length - 1 > c->base + c->size - 1) {
423 LOG_ERROR("Flash access does not fit into bank.");
424 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
427 if (c->prot_blocks == NULL || c->num_prot_blocks == 0) {
428 /* flash driver does not define protect blocks, use sectors instead */
429 iterate_protect_blocks = false;
432 if (iterate_protect_blocks) {
433 block_array = c->prot_blocks;
434 num_blocks = c->num_prot_blocks;
436 block_array = c->sectors;
437 num_blocks = c->num_sectors;
441 last_addr -= c->base;
443 for (i = 0; i < num_blocks; i++) {
444 struct flash_sector *f = &block_array[i];
445 uint32_t end = f->offset + f->size;
447 /* start only on a sector boundary */
449 /* scanned past the first sector? */
450 if (addr < f->offset)
453 /* is this the first sector? */
454 if (addr == f->offset)
457 /* Does this need head-padding? If so, pad and warn;
458 * or else force an error.
460 * Such padding can make trouble, since *WE* can't
461 * ever know if that data was in use. The warning
462 * should help users sort out messes later.
464 else if (addr < end && pad_reason) {
465 /* FIXME say how many bytes (e.g. 80 KB) */
466 LOG_WARNING("Adding extra %s range, "
469 (unsigned) f->offset,
470 (unsigned) addr - 1);
476 /* is this (also?) the last sector? */
477 if (last_addr == end) {
482 /* Does this need tail-padding? If so, pad and warn;
483 * or else force an error.
485 if (last_addr < end && pad_reason) {
486 /* FIXME say how many bytes (e.g. 80 KB) */
487 LOG_WARNING("Adding extra %s range, "
490 (unsigned) last_addr,
496 /* MUST finish on a sector boundary */
497 if (last_addr <= f->offset)
501 /* invalid start or end address? */
502 if (first == -1 || last == -1) {
503 LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
504 "is not sector-aligned",
505 (unsigned) (c->base + addr),
506 (unsigned) (c->base + last_addr - 1));
507 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
510 /* The NOR driver may trim this range down, based on what
511 * sectors are already erased/unprotected. GDB currently
512 * blocks such optimizations.
514 return callback(c, first, last);
517 /* The inner fn only handles a single bank, we could be spanning
520 static int flash_iterate_address_range(struct target *target,
521 char *pad_reason, uint32_t addr, uint32_t length,
522 bool iterate_protect_blocks,
523 int (*callback)(struct flash_bank *bank, int first, int last))
525 struct flash_bank *c;
526 int retval = ERROR_OK;
528 /* Danger! zero-length iterations means entire bank! */
530 retval = get_flash_bank_by_addr(target, addr, true, &c);
531 if (retval != ERROR_OK)
534 uint32_t cur_length = length;
535 /* check whether it all fits in this bank */
536 if (addr + length - 1 > c->base + c->size - 1) {
537 LOG_DEBUG("iterating over more than one flash bank.");
538 cur_length = c->base + c->size - addr;
540 retval = flash_iterate_address_range_inner(target,
541 pad_reason, addr, cur_length,
542 iterate_protect_blocks,
544 if (retval != ERROR_OK)
547 length -= cur_length;
549 } while (length > 0);
554 int flash_erase_address_range(struct target *target,
555 bool pad, uint32_t addr, uint32_t length)
557 return flash_iterate_address_range(target, pad ? "erase" : NULL,
558 addr, length, false, &flash_driver_erase);
561 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
563 return flash_driver_protect(bank, 0, first, last);
566 int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
568 /* By default, pad to sector boundaries ... the real issue here
569 * is that our (only) caller *permanently* removes protection,
570 * and doesn't restore it.
572 return flash_iterate_address_range(target, "unprotect",
573 addr, length, true, &flash_driver_unprotect);
576 static int compare_section(const void *a, const void *b)
578 struct imagesection *b1, *b2;
579 b1 = *((struct imagesection **)a);
580 b2 = *((struct imagesection **)b);
582 if (b1->base_address == b2->base_address)
584 else if (b1->base_address > b2->base_address)
590 int flash_write_unlock(struct target *target, struct image *image,
591 uint32_t *written, int erase, bool unlock)
593 int retval = ERROR_OK;
596 uint32_t section_offset;
597 struct flash_bank *c;
607 /* assume all sectors need erasing - stops any problems
608 * when flash_write is called multiple times */
613 /* allocate padding array */
614 padding = calloc(image->num_sections, sizeof(*padding));
616 /* This fn requires all sections to be in ascending order of addresses,
617 * whereas an image can have sections out of order. */
618 struct imagesection **sections = malloc(sizeof(struct imagesection *) *
619 image->num_sections);
621 for (i = 0; i < image->num_sections; i++)
622 sections[i] = &image->sections[i];
624 qsort(sections, image->num_sections, sizeof(struct imagesection *),
627 /* loop until we reach end of the image */
628 while (section < image->num_sections) {
629 uint32_t buffer_size;
632 target_addr_t run_address = sections[section]->base_address + section_offset;
633 uint32_t run_size = sections[section]->size - section_offset;
636 if (sections[section]->size == 0) {
637 LOG_WARNING("empty section %d", section);
643 /* find the corresponding flash bank */
644 retval = get_flash_bank_by_addr(target, run_address, false, &c);
645 if (retval != ERROR_OK)
648 LOG_WARNING("no flash bank found for address " TARGET_ADDR_FMT, run_address);
649 section++; /* and skip it */
654 /* collect consecutive sections which fall into the same bank */
655 section_last = section;
656 padding[section] = 0;
657 while ((run_address + run_size - 1 < c->base + c->size - 1) &&
658 (section_last + 1 < image->num_sections)) {
659 /* sections are sorted */
660 assert(sections[section_last + 1]->base_address >= c->base);
661 if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
662 /* Done with this bank */
666 /* FIXME This needlessly touches sectors BETWEEN the
667 * sections it's writing. Without auto erase, it just
668 * writes ones. That WILL INVALIDATE data in cases
669 * like Stellaris Tempest chips, corrupting internal
670 * ECC codes; and at least FreeScale suggests issues
671 * with that approach (in HC11 documentation).
673 * With auto erase enabled, data in those sectors will
674 * be needlessly destroyed; and some of the limited
675 * number of flash erase cycles will be wasted...
677 * In both cases, the extra writes slow things down.
680 /* if we have multiple sections within our image,
681 * flash programming could fail due to alignment issues
682 * attempt to rebuild a consecutive buffer for the flash loader */
683 target_addr_t run_next_addr = run_address + run_size;
684 if (sections[section_last + 1]->base_address < run_next_addr) {
685 LOG_ERROR("Section at " TARGET_ADDR_FMT
686 " overlaps section ending at " TARGET_ADDR_FMT,
687 sections[section_last + 1]->base_address,
689 LOG_ERROR("Flash write aborted.");
694 pad_bytes = sections[section_last + 1]->base_address - run_next_addr;
695 padding[section_last] = pad_bytes;
696 run_size += sections[++section_last]->size;
697 run_size += pad_bytes;
700 LOG_INFO("Padding image section %d with %d bytes",
705 if (run_address + run_size - 1 > c->base + c->size - 1) {
706 /* If we have more than one flash chip back to back, then we limit
707 * the current write operation to the current chip.
709 LOG_DEBUG("Truncate flash run size to the current flash chip.");
711 run_size = c->base + c->size - run_address;
712 assert(run_size > 0);
715 /* If we're applying any sector automagic, then pad this
716 * (maybe-combined) segment to the end of its last sector.
718 if (unlock || erase) {
720 uint32_t offset_start = run_address - c->base;
721 uint32_t offset_end = offset_start + run_size;
722 uint32_t end = offset_end, delta;
724 for (sector = 0; sector < c->num_sectors; sector++) {
725 end = c->sectors[sector].offset
726 + c->sectors[sector].size;
727 if (offset_end <= end)
731 delta = end - offset_end;
732 padding[section_last] += delta;
736 /* allocate buffer */
737 buffer = malloc(run_size);
738 if (buffer == NULL) {
739 LOG_ERROR("Out of memory for flash bank buffer");
745 /* read sections to the buffer */
746 while (buffer_size < run_size) {
749 size_read = run_size - buffer_size;
750 if (size_read > sections[section]->size - section_offset)
751 size_read = sections[section]->size - section_offset;
755 * #¤%#"%¤% we have to figure out the section # from the sorted
756 * list of pointers to sections to invoke image_read_section()...
758 intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
759 int t_section_num = diff / sizeof(struct imagesection);
761 LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
762 "section_offset = %d, buffer_size = %d, size_read = %d",
763 (int)section, (int)t_section_num, (int)section_offset,
764 (int)buffer_size, (int)size_read);
765 retval = image_read_section(image, t_section_num, section_offset,
766 size_read, buffer + buffer_size, &size_read);
767 if (retval != ERROR_OK || size_read == 0) {
772 /* see if we need to pad the section */
773 while (padding[section]--)
774 (buffer + buffer_size)[size_read++] = c->default_padded_value;
776 buffer_size += size_read;
777 section_offset += size_read;
779 if (section_offset >= sections[section]->size) {
788 retval = flash_unlock_address_range(target, run_address, run_size);
789 if (retval == ERROR_OK) {
791 /* calculate and erase sectors */
792 retval = flash_erase_address_range(target,
793 true, run_address, run_size);
797 if (retval == ERROR_OK) {
798 /* write flash sectors */
799 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
804 if (retval != ERROR_OK) {
805 /* abort operation */
810 *written += run_size; /* add run size to total written counter */
820 int flash_write(struct target *target, struct image *image,
821 uint32_t *written, int erase)
823 return flash_write_unlock(target, image, written, erase, false);
826 struct flash_sector *alloc_block_array(uint32_t offset, uint32_t size, int num_blocks)
830 struct flash_sector *array = calloc(num_blocks, sizeof(struct flash_sector));
834 for (i = 0; i < num_blocks; i++) {
835 array[i].offset = offset;
836 array[i].size = size;
837 array[i].is_erased = -1;
838 array[i].is_protected = -1;