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 struct flash_bank *get_flash_bank_by_name_noprobe(const char *name)
176 unsigned requested = get_flash_name_index(name);
179 struct flash_bank *bank;
180 for (bank = flash_banks; NULL != bank; bank = bank->next) {
181 if (strcmp(bank->name, name) == 0)
183 if (!flash_driver_name_matches(bank->driver->name, name))
185 if (++found < requested)
192 int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
194 struct flash_bank *bank;
197 bank = get_flash_bank_by_name_noprobe(name);
199 retval = bank->driver->auto_probe(bank);
201 if (retval != ERROR_OK) {
202 LOG_ERROR("auto_probe failed");
211 int get_flash_bank_by_num(int num, struct flash_bank **bank)
213 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
219 retval = p->driver->auto_probe(p);
221 if (retval != ERROR_OK) {
222 LOG_ERROR("auto_probe failed");
229 /* lookup flash bank by address, bank not found is success, but
230 * result_bank is set to NULL. */
231 int get_flash_bank_by_addr(struct target *target,
234 struct flash_bank **result_bank)
236 struct flash_bank *c;
238 /* cycle through bank list */
239 for (c = flash_banks; c; c = c->next) {
240 if (c->target != target)
244 retval = c->driver->auto_probe(c);
246 if (retval != ERROR_OK) {
247 LOG_ERROR("auto_probe failed");
250 /* check whether address belongs to this flash bank */
251 if ((addr >= c->base) && (addr <= c->base + (c->size - 1))) {
258 LOG_ERROR("No flash at address 0x%08" PRIx32, addr);
264 static int default_flash_mem_blank_check(struct flash_bank *bank)
266 struct target *target = bank->target;
267 const int buffer_size = 1024;
270 int retval = ERROR_OK;
272 if (bank->target->state != TARGET_HALTED) {
273 LOG_ERROR("Target not halted");
274 return ERROR_TARGET_NOT_HALTED;
277 uint8_t *buffer = malloc(buffer_size);
279 for (i = 0; i < bank->num_sectors; i++) {
281 bank->sectors[i].is_erased = 1;
283 for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
286 if (chunk > (j - bank->sectors[i].size))
287 chunk = (j - bank->sectors[i].size);
289 retval = target_read_memory(target,
290 bank->base + bank->sectors[i].offset + j,
294 if (retval != ERROR_OK)
297 for (nBytes = 0; nBytes < chunk; nBytes++) {
298 if (buffer[nBytes] != bank->erased_value) {
299 bank->sectors[i].is_erased = 0;
312 int default_flash_blank_check(struct flash_bank *bank)
314 struct target *target = bank->target;
320 if (bank->target->state != TARGET_HALTED) {
321 LOG_ERROR("Target not halted");
322 return ERROR_TARGET_NOT_HALTED;
325 for (i = 0; i < bank->num_sectors; i++) {
326 uint32_t address = bank->base + bank->sectors[i].offset;
327 uint32_t size = bank->sectors[i].size;
329 retval = target_blank_check_memory(target, address, size, &blank, bank->erased_value);
330 if (retval != ERROR_OK) {
334 if (blank == bank->erased_value)
335 bank->sectors[i].is_erased = 1;
337 bank->sectors[i].is_erased = 0;
342 LOG_USER("Running slow fallback erase check - add working memory");
343 return default_flash_mem_blank_check(bank);
349 /* Manipulate given flash region, selecting the bank according to target
350 * and address. Maps an address range to a set of sectors, and issues
351 * the callback() on that set ... e.g. to erase or unprotect its members.
353 * Parameter iterate_protect_blocks switches iteration of protect block
354 * instead of erase sectors. If there is no protect blocks array, sectors
355 * are used in iteration, so compatibility for old flash drivers is retained.
357 * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
358 * range must fit those sectors exactly. This is clearly safe; it can't
359 * erase data which the caller said to leave alone, for example. If it's
360 * non-NULL, rather than failing, extra data in the first and/or last
361 * sectors will be added to the range, and that reason string is used when
362 * warning about those additions.
364 static int flash_iterate_address_range_inner(struct target *target,
365 char *pad_reason, uint32_t addr, uint32_t length,
366 bool iterate_protect_blocks,
367 int (*callback)(struct flash_bank *bank, int first, int last))
369 struct flash_bank *c;
370 struct flash_sector *block_array;
371 uint32_t last_addr = addr + length; /* first address AFTER end */
377 int retval = get_flash_bank_by_addr(target, addr, true, &c);
378 if (retval != ERROR_OK)
381 if (c->size == 0 || c->num_sectors == 0) {
382 LOG_ERROR("Bank is invalid");
383 return ERROR_FLASH_BANK_INVALID;
387 /* special case, erase whole bank when length is zero */
388 if (addr != c->base) {
389 LOG_ERROR("Whole bank access must start at beginning of bank.");
390 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
393 return callback(c, 0, c->num_sectors - 1);
396 /* check whether it all fits in this bank */
397 if (addr + length - 1 > c->base + c->size - 1) {
398 LOG_ERROR("Flash access does not fit into bank.");
399 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
403 last_addr -= c->base;
405 if (iterate_protect_blocks && c->prot_blocks && c->num_prot_blocks) {
406 block_array = c->prot_blocks;
407 num_blocks = c->num_prot_blocks;
409 block_array = c->sectors;
410 num_blocks = c->num_sectors;
411 iterate_protect_blocks = false;
415 for (i = 0; i < num_blocks; i++) {
416 struct flash_sector *f = &block_array[i];
417 uint32_t end = f->offset + f->size;
419 /* start only on a sector boundary */
421 /* scanned past the first sector? */
422 if (addr < f->offset)
425 /* is this the first sector? */
426 if (addr == f->offset)
429 /* Does this need head-padding? If so, pad and warn;
430 * or else force an error.
432 * Such padding can make trouble, since *WE* can't
433 * ever know if that data was in use. The warning
434 * should help users sort out messes later.
436 else if (addr < end && pad_reason) {
437 /* FIXME say how many bytes (e.g. 80 KB) */
438 LOG_WARNING("Adding extra %s range, "
441 (unsigned) f->offset,
442 (unsigned) addr - 1);
448 /* is this (also?) the last sector? */
449 if (last_addr == end) {
454 /* Does this need tail-padding? If so, pad and warn;
455 * or else force an error.
457 if (last_addr < end && pad_reason) {
458 /* FIXME say how many bytes (e.g. 80 KB) */
459 LOG_WARNING("Adding extra %s range, "
462 (unsigned) last_addr,
468 /* MUST finish on a sector boundary */
469 if (last_addr <= f->offset)
473 /* invalid start or end address? */
474 if (first == -1 || last == -1) {
475 LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
476 "is not sector-aligned",
477 (unsigned) (c->base + addr),
478 (unsigned) (c->base + last_addr - 1));
479 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
482 /* The NOR driver may trim this range down, based on what
483 * sectors are already erased/unprotected. GDB currently
484 * blocks such optimizations.
486 return callback(c, first, last);
489 /* The inner fn only handles a single bank, we could be spanning
492 static int flash_iterate_address_range(struct target *target,
493 char *pad_reason, uint32_t addr, uint32_t length,
494 bool iterate_protect_blocks,
495 int (*callback)(struct flash_bank *bank, int first, int last))
497 struct flash_bank *c;
498 int retval = ERROR_OK;
500 /* Danger! zero-length iterations means entire bank! */
502 retval = get_flash_bank_by_addr(target, addr, true, &c);
503 if (retval != ERROR_OK)
506 uint32_t cur_length = length;
507 /* check whether it all fits in this bank */
508 if (addr + length - 1 > c->base + c->size - 1) {
509 LOG_DEBUG("iterating over more than one flash bank.");
510 cur_length = c->base + c->size - addr;
512 retval = flash_iterate_address_range_inner(target,
513 pad_reason, addr, cur_length,
514 iterate_protect_blocks,
516 if (retval != ERROR_OK)
519 length -= cur_length;
521 } while (length > 0);
526 int flash_erase_address_range(struct target *target,
527 bool pad, uint32_t addr, uint32_t length)
529 return flash_iterate_address_range(target, pad ? "erase" : NULL,
530 addr, length, false, &flash_driver_erase);
533 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
535 return flash_driver_protect(bank, 0, first, last);
538 int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
540 /* By default, pad to sector boundaries ... the real issue here
541 * is that our (only) caller *permanently* removes protection,
542 * and doesn't restore it.
544 return flash_iterate_address_range(target, "unprotect",
545 addr, length, true, &flash_driver_unprotect);
548 static int compare_section(const void *a, const void *b)
550 struct imagesection *b1, *b2;
551 b1 = *((struct imagesection **)a);
552 b2 = *((struct imagesection **)b);
554 if (b1->base_address == b2->base_address)
556 else if (b1->base_address > b2->base_address)
562 int flash_write_unlock(struct target *target, struct image *image,
563 uint32_t *written, int erase, bool unlock)
565 int retval = ERROR_OK;
568 uint32_t section_offset;
569 struct flash_bank *c;
579 /* assume all sectors need erasing - stops any problems
580 * when flash_write is called multiple times */
585 /* allocate padding array */
586 padding = calloc(image->num_sections, sizeof(*padding));
588 /* This fn requires all sections to be in ascending order of addresses,
589 * whereas an image can have sections out of order. */
590 struct imagesection **sections = malloc(sizeof(struct imagesection *) *
591 image->num_sections);
593 for (i = 0; i < image->num_sections; i++)
594 sections[i] = &image->sections[i];
596 qsort(sections, image->num_sections, sizeof(struct imagesection *),
599 /* loop until we reach end of the image */
600 while (section < image->num_sections) {
601 uint32_t buffer_size;
604 target_addr_t run_address = sections[section]->base_address + section_offset;
605 uint32_t run_size = sections[section]->size - section_offset;
608 if (sections[section]->size == 0) {
609 LOG_WARNING("empty section %d", section);
615 /* find the corresponding flash bank */
616 retval = get_flash_bank_by_addr(target, run_address, false, &c);
617 if (retval != ERROR_OK)
620 LOG_WARNING("no flash bank found for address " TARGET_ADDR_FMT, run_address);
621 section++; /* and skip it */
626 /* collect consecutive sections which fall into the same bank */
627 section_last = section;
628 padding[section] = 0;
629 while ((run_address + run_size - 1 < c->base + c->size - 1) &&
630 (section_last + 1 < image->num_sections)) {
631 /* sections are sorted */
632 assert(sections[section_last + 1]->base_address >= c->base);
633 if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
634 /* Done with this bank */
638 /* FIXME This needlessly touches sectors BETWEEN the
639 * sections it's writing. Without auto erase, it just
640 * writes ones. That WILL INVALIDATE data in cases
641 * like Stellaris Tempest chips, corrupting internal
642 * ECC codes; and at least FreeScale suggests issues
643 * with that approach (in HC11 documentation).
645 * With auto erase enabled, data in those sectors will
646 * be needlessly destroyed; and some of the limited
647 * number of flash erase cycles will be wasted...
649 * In both cases, the extra writes slow things down.
652 /* if we have multiple sections within our image,
653 * flash programming could fail due to alignment issues
654 * attempt to rebuild a consecutive buffer for the flash loader */
655 target_addr_t run_next_addr = run_address + run_size;
656 if (sections[section_last + 1]->base_address < run_next_addr) {
657 LOG_ERROR("Section at " TARGET_ADDR_FMT
658 " overlaps section ending at " TARGET_ADDR_FMT,
659 sections[section_last + 1]->base_address,
661 LOG_ERROR("Flash write aborted.");
666 pad_bytes = sections[section_last + 1]->base_address - run_next_addr;
667 padding[section_last] = pad_bytes;
668 run_size += sections[++section_last]->size;
669 run_size += pad_bytes;
672 LOG_INFO("Padding image section %d with %d bytes",
677 if (run_address + run_size - 1 > c->base + c->size - 1) {
678 /* If we have more than one flash chip back to back, then we limit
679 * the current write operation to the current chip.
681 LOG_DEBUG("Truncate flash run size to the current flash chip.");
683 run_size = c->base + c->size - run_address;
684 assert(run_size > 0);
687 /* If we're applying any sector automagic, then pad this
688 * (maybe-combined) segment to the end of its last sector.
690 if (unlock || erase) {
692 uint32_t offset_start = run_address - c->base;
693 uint32_t offset_end = offset_start + run_size;
694 uint32_t end = offset_end, delta;
696 for (sector = 0; sector < c->num_sectors; sector++) {
697 end = c->sectors[sector].offset
698 + c->sectors[sector].size;
699 if (offset_end <= end)
703 delta = end - offset_end;
704 padding[section_last] += delta;
708 /* allocate buffer */
709 buffer = malloc(run_size);
710 if (buffer == NULL) {
711 LOG_ERROR("Out of memory for flash bank buffer");
717 /* read sections to the buffer */
718 while (buffer_size < run_size) {
721 size_read = run_size - buffer_size;
722 if (size_read > sections[section]->size - section_offset)
723 size_read = sections[section]->size - section_offset;
727 * #¤%#"%¤% we have to figure out the section # from the sorted
728 * list of pointers to sections to invoke image_read_section()...
730 intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
731 int t_section_num = diff / sizeof(struct imagesection);
733 LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
734 "section_offset = %d, buffer_size = %d, size_read = %d",
735 (int)section, (int)t_section_num, (int)section_offset,
736 (int)buffer_size, (int)size_read);
737 retval = image_read_section(image, t_section_num, section_offset,
738 size_read, buffer + buffer_size, &size_read);
739 if (retval != ERROR_OK || size_read == 0) {
744 /* see if we need to pad the section */
745 while (padding[section]--)
746 (buffer + buffer_size)[size_read++] = c->default_padded_value;
748 buffer_size += size_read;
749 section_offset += size_read;
751 if (section_offset >= sections[section]->size) {
760 retval = flash_unlock_address_range(target, run_address, run_size);
761 if (retval == ERROR_OK) {
763 /* calculate and erase sectors */
764 retval = flash_erase_address_range(target,
765 true, run_address, run_size);
769 if (retval == ERROR_OK) {
770 /* write flash sectors */
771 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
776 if (retval != ERROR_OK) {
777 /* abort operation */
782 *written += run_size; /* add run size to total written counter */
792 int flash_write(struct target *target, struct image *image,
793 uint32_t *written, int erase)
795 return flash_write_unlock(target, image, written, erase, false);
798 struct flash_sector *alloc_block_array(uint32_t offset, uint32_t size, int num_blocks)
802 struct flash_sector *array = calloc(num_blocks, sizeof(struct flash_sector));
806 for (i = 0; i < num_blocks; i++) {
807 array[i].offset = offset;
808 array[i].size = size;
809 array[i].is_erased = -1;
810 array[i].is_protected = -1;