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, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
22 ***************************************************************************/
27 #include <flash/common.h>
28 #include <flash/nor/core.h>
29 #include <flash/nor/imp.h>
30 #include <target/image.h>
34 * Upper level of NOR flash framework.
35 * The lower level interfaces are to drivers. These upper level ones
36 * primarily support access from Tcl scripts or from GDB.
39 static struct flash_bank *flash_banks;
41 int flash_driver_erase(struct flash_bank *bank, int first, int last)
45 retval = bank->driver->erase(bank, first, last);
46 if (retval != ERROR_OK)
47 LOG_ERROR("failed erasing sectors %d to %d", first, last);
52 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
56 /* callers may not supply illegal parameters ... */
57 if (first < 0 || first > last || last >= bank->num_sectors) {
58 LOG_ERROR("illegal sector range");
62 /* force "set" to 0/1 */
67 * We must not use any cached information about protection state!!!!
69 * There are a million things that could change the protect state:
71 * the target could have reset, power cycled, been hot plugged,
72 * the application could have run, etc.
74 * Drivers only receive valid sector range.
76 retval = bank->driver->protect(bank, set, first, last);
77 if (retval != ERROR_OK)
78 LOG_ERROR("failed setting protection for areas %d to %d", first, last);
83 int flash_driver_write(struct flash_bank *bank,
84 uint8_t *buffer, uint32_t offset, uint32_t count)
88 retval = bank->driver->write(bank, buffer, offset, count);
89 if (retval != ERROR_OK) {
91 "error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32,
99 int flash_driver_read(struct flash_bank *bank,
100 uint8_t *buffer, uint32_t offset, uint32_t count)
104 LOG_DEBUG("call flash_driver_read()");
106 retval = bank->driver->read(bank, buffer, offset, count);
107 if (retval != ERROR_OK) {
109 "error reading to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32,
117 int default_flash_read(struct flash_bank *bank,
118 uint8_t *buffer, uint32_t offset, uint32_t count)
120 return target_read_buffer(bank->target, offset + bank->base, count, buffer);
123 void flash_bank_add(struct flash_bank *bank)
125 /* put flash bank in linked list */
126 unsigned bank_num = 0;
128 /* find last flash bank */
129 struct flash_bank *p = flash_banks;
130 while (NULL != p->next) {
139 bank->bank_number = bank_num;
142 struct flash_bank *flash_bank_list(void)
147 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
149 struct flash_bank *p;
152 for (p = flash_banks; p; p = p->next) {
156 LOG_ERROR("flash bank %d does not exist", num);
160 int flash_get_bank_count(void)
162 struct flash_bank *p;
164 for (p = flash_banks; p; p = p->next)
169 struct flash_bank *get_flash_bank_by_name_noprobe(const char *name)
171 unsigned requested = get_flash_name_index(name);
174 struct flash_bank *bank;
175 for (bank = flash_banks; NULL != bank; bank = bank->next) {
176 if (strcmp(bank->name, name) == 0)
178 if (!flash_driver_name_matches(bank->driver->name, name))
180 if (++found < requested)
187 int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
189 struct flash_bank *bank;
192 bank = get_flash_bank_by_name_noprobe(name);
194 retval = bank->driver->auto_probe(bank);
196 if (retval != ERROR_OK) {
197 LOG_ERROR("auto_probe failed");
206 int get_flash_bank_by_num(int num, struct flash_bank **bank)
208 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
214 retval = p->driver->auto_probe(p);
216 if (retval != ERROR_OK) {
217 LOG_ERROR("auto_probe failed");
224 /* lookup flash bank by address, bank not found is success, but
225 * result_bank is set to NULL. */
226 int get_flash_bank_by_addr(struct target *target,
229 struct flash_bank **result_bank)
231 struct flash_bank *c;
233 /* cycle through bank list */
234 for (c = flash_banks; c; c = c->next) {
235 if (c->target != target)
239 retval = c->driver->auto_probe(c);
241 if (retval != ERROR_OK) {
242 LOG_ERROR("auto_probe failed");
245 /* check whether address belongs to this flash bank */
246 if ((addr >= c->base) && (addr <= c->base + (c->size - 1))) {
253 LOG_ERROR("No flash at address 0x%08" PRIx32, addr);
259 static int default_flash_mem_blank_check(struct flash_bank *bank)
261 struct target *target = bank->target;
262 const int buffer_size = 1024;
265 int retval = ERROR_OK;
267 if (bank->target->state != TARGET_HALTED) {
268 LOG_ERROR("Target not halted");
269 return ERROR_TARGET_NOT_HALTED;
272 uint8_t *buffer = malloc(buffer_size);
274 for (i = 0; i < bank->num_sectors; i++) {
276 bank->sectors[i].is_erased = 1;
278 for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
281 if (chunk > (j - bank->sectors[i].size))
282 chunk = (j - bank->sectors[i].size);
284 retval = target_read_memory(target,
285 bank->base + bank->sectors[i].offset + j,
289 if (retval != ERROR_OK)
292 for (nBytes = 0; nBytes < chunk; nBytes++) {
293 if (buffer[nBytes] != 0xFF) {
294 bank->sectors[i].is_erased = 0;
307 int default_flash_blank_check(struct flash_bank *bank)
309 struct target *target = bank->target;
315 if (bank->target->state != TARGET_HALTED) {
316 LOG_ERROR("Target not halted");
317 return ERROR_TARGET_NOT_HALTED;
320 for (i = 0; i < bank->num_sectors; i++) {
321 uint32_t address = bank->base + bank->sectors[i].offset;
322 uint32_t size = bank->sectors[i].size;
324 retval = target_blank_check_memory(target, address, size, &blank);
325 if (retval != ERROR_OK) {
330 bank->sectors[i].is_erased = 1;
332 bank->sectors[i].is_erased = 0;
337 LOG_USER("Running slow fallback erase check - add working memory");
338 return default_flash_mem_blank_check(bank);
344 /* Manipulate given flash region, selecting the bank according to target
345 * and address. Maps an address range to a set of sectors, and issues
346 * the callback() on that set ... e.g. to erase or unprotect its members.
348 * (Note a current bad assumption: that protection operates on the same
349 * size sectors as erase operations use.)
351 * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
352 * range must fit those sectors exactly. This is clearly safe; it can't
353 * erase data which the caller said to leave alone, for example. If it's
354 * non-NULL, rather than failing, extra data in the first and/or last
355 * sectors will be added to the range, and that reason string is used when
356 * warning about those additions.
358 static int flash_iterate_address_range_inner(struct target *target,
359 char *pad_reason, uint32_t addr, uint32_t length,
360 int (*callback)(struct flash_bank *bank, int first, int last))
362 struct flash_bank *c;
363 uint32_t last_addr = addr + length; /* first address AFTER end */
368 int retval = get_flash_bank_by_addr(target, addr, true, &c);
369 if (retval != ERROR_OK)
372 if (c->size == 0 || c->num_sectors == 0) {
373 LOG_ERROR("Bank is invalid");
374 return ERROR_FLASH_BANK_INVALID;
378 /* special case, erase whole bank when length is zero */
379 if (addr != c->base) {
380 LOG_ERROR("Whole bank access must start at beginning of bank.");
381 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
384 return callback(c, 0, c->num_sectors - 1);
387 /* check whether it all fits in this bank */
388 if (addr + length - 1 > c->base + c->size - 1) {
389 LOG_ERROR("Flash access does not fit into bank.");
390 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
393 /** @todo: handle erasures that cross into adjacent banks */
396 last_addr -= c->base;
398 for (i = 0; i < c->num_sectors; i++) {
399 struct flash_sector *f = c->sectors + i;
400 uint32_t end = f->offset + f->size;
402 /* start only on a sector boundary */
404 /* scanned past the first sector? */
405 if (addr < f->offset)
408 /* is this the first sector? */
409 if (addr == f->offset)
412 /* Does this need head-padding? If so, pad and warn;
413 * or else force an error.
415 * Such padding can make trouble, since *WE* can't
416 * ever know if that data was in use. The warning
417 * should help users sort out messes later.
419 else if (addr < end && pad_reason) {
420 /* FIXME say how many bytes (e.g. 80 KB) */
421 LOG_WARNING("Adding extra %s range, "
424 (unsigned) f->offset,
425 (unsigned) addr - 1);
431 /* is this (also?) the last sector? */
432 if (last_addr == end) {
437 /* Does this need tail-padding? If so, pad and warn;
438 * or else force an error.
440 if (last_addr < end && pad_reason) {
441 /* FIXME say how many bytes (e.g. 80 KB) */
442 LOG_WARNING("Adding extra %s range, "
445 (unsigned) last_addr,
451 /* MUST finish on a sector boundary */
452 if (last_addr <= f->offset)
456 /* invalid start or end address? */
457 if (first == -1 || last == -1) {
458 LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
459 "is not sector-aligned",
460 (unsigned) (c->base + addr),
461 (unsigned) (c->base + last_addr - 1));
462 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
465 /* The NOR driver may trim this range down, based on what
466 * sectors are already erased/unprotected. GDB currently
467 * blocks such optimizations.
469 return callback(c, first, last);
472 /* The inner fn only handles a single bank, we could be spanning
475 static int flash_iterate_address_range(struct target *target,
476 char *pad_reason, uint32_t addr, uint32_t length,
477 int (*callback)(struct flash_bank *bank, int first, int last))
479 struct flash_bank *c;
480 int retval = ERROR_OK;
482 /* Danger! zero-length iterations means entire bank! */
484 retval = get_flash_bank_by_addr(target, addr, true, &c);
485 if (retval != ERROR_OK)
488 uint32_t cur_length = length;
489 /* check whether it all fits in this bank */
490 if (addr + length - 1 > c->base + c->size - 1) {
491 LOG_DEBUG("iterating over more than one flash bank.");
492 cur_length = c->base + c->size - addr;
494 retval = flash_iterate_address_range_inner(target,
495 pad_reason, addr, cur_length,
497 if (retval != ERROR_OK)
500 length -= cur_length;
502 } while (length > 0);
507 int flash_erase_address_range(struct target *target,
508 bool pad, uint32_t addr, uint32_t length)
510 return flash_iterate_address_range(target, pad ? "erase" : NULL,
511 addr, length, &flash_driver_erase);
514 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
516 return flash_driver_protect(bank, 0, first, last);
519 int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
521 /* By default, pad to sector boundaries ... the real issue here
522 * is that our (only) caller *permanently* removes protection,
523 * and doesn't restore it.
525 return flash_iterate_address_range(target, "unprotect",
526 addr, length, &flash_driver_unprotect);
529 static int compare_section(const void *a, const void *b)
531 struct imagesection *b1, *b2;
532 b1 = *((struct imagesection **)a);
533 b2 = *((struct imagesection **)b);
535 if (b1->base_address == b2->base_address)
537 else if (b1->base_address > b2->base_address)
543 int flash_write_unlock(struct target *target, struct image *image,
544 uint32_t *written, int erase, bool unlock)
546 int retval = ERROR_OK;
549 uint32_t section_offset;
550 struct flash_bank *c;
560 /* assume all sectors need erasing - stops any problems
561 * when flash_write is called multiple times */
566 /* allocate padding array */
567 padding = calloc(image->num_sections, sizeof(*padding));
569 /* This fn requires all sections to be in ascending order of addresses,
570 * whereas an image can have sections out of order. */
571 struct imagesection **sections = malloc(sizeof(struct imagesection *) *
572 image->num_sections);
574 for (i = 0; i < image->num_sections; i++)
575 sections[i] = &image->sections[i];
577 qsort(sections, image->num_sections, sizeof(struct imagesection *),
580 /* loop until we reach end of the image */
581 while (section < image->num_sections) {
582 uint32_t buffer_size;
585 uint32_t run_address = sections[section]->base_address + section_offset;
586 uint32_t run_size = sections[section]->size - section_offset;
589 if (sections[section]->size == 0) {
590 LOG_WARNING("empty section %d", section);
596 /* find the corresponding flash bank */
597 retval = get_flash_bank_by_addr(target, run_address, false, &c);
598 if (retval != ERROR_OK)
601 LOG_WARNING("no flash bank found for address %" PRIx32, run_address);
602 section++; /* and skip it */
607 /* collect consecutive sections which fall into the same bank */
608 section_last = section;
609 padding[section] = 0;
610 while ((run_address + run_size - 1 < c->base + c->size - 1) &&
611 (section_last + 1 < image->num_sections)) {
612 /* sections are sorted */
613 assert(sections[section_last + 1]->base_address >= c->base);
614 if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
615 /* Done with this bank */
619 /* FIXME This needlessly touches sectors BETWEEN the
620 * sections it's writing. Without auto erase, it just
621 * writes ones. That WILL INVALIDATE data in cases
622 * like Stellaris Tempest chips, corrupting internal
623 * ECC codes; and at least FreeScale suggests issues
624 * with that approach (in HC11 documentation).
626 * With auto erase enabled, data in those sectors will
627 * be needlessly destroyed; and some of the limited
628 * number of flash erase cycles will be wasted...
630 * In both cases, the extra writes slow things down.
633 /* if we have multiple sections within our image,
634 * flash programming could fail due to alignment issues
635 * attempt to rebuild a consecutive buffer for the flash loader */
636 pad_bytes = (sections[section_last + 1]->base_address) - (run_address + run_size);
637 padding[section_last] = pad_bytes;
638 run_size += sections[++section_last]->size;
639 run_size += pad_bytes;
642 LOG_INFO("Padding image section %d with %d bytes",
647 if (run_address + run_size - 1 > c->base + c->size - 1) {
648 /* If we have more than one flash chip back to back, then we limit
649 * the current write operation to the current chip.
651 LOG_DEBUG("Truncate flash run size to the current flash chip.");
653 run_size = c->base + c->size - run_address;
654 assert(run_size > 0);
657 /* If we're applying any sector automagic, then pad this
658 * (maybe-combined) segment to the end of its last sector.
660 if (unlock || erase) {
662 uint32_t offset_start = run_address - c->base;
663 uint32_t offset_end = offset_start + run_size;
664 uint32_t end = offset_end, delta;
666 for (sector = 0; sector < c->num_sectors; sector++) {
667 end = c->sectors[sector].offset
668 + c->sectors[sector].size;
669 if (offset_end <= end)
673 delta = end - offset_end;
674 padding[section_last] += delta;
678 /* allocate buffer */
679 buffer = malloc(run_size);
680 if (buffer == NULL) {
681 LOG_ERROR("Out of memory for flash bank buffer");
687 /* read sections to the buffer */
688 while (buffer_size < run_size) {
691 size_read = run_size - buffer_size;
692 if (size_read > sections[section]->size - section_offset)
693 size_read = sections[section]->size - section_offset;
697 * #¤%#"%¤% we have to figure out the section # from the sorted
698 * list of pointers to sections to invoke image_read_section()...
700 intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
701 int t_section_num = diff / sizeof(struct imagesection);
703 LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
704 "section_offset = %d, buffer_size = %d, size_read = %d",
705 (int)section, (int)t_section_num, (int)section_offset,
706 (int)buffer_size, (int)size_read);
707 retval = image_read_section(image, t_section_num, section_offset,
708 size_read, buffer + buffer_size, &size_read);
709 if (retval != ERROR_OK || size_read == 0) {
714 /* see if we need to pad the section */
715 while (padding[section]--)
716 (buffer + buffer_size)[size_read++] = c->default_padded_value;
718 buffer_size += size_read;
719 section_offset += size_read;
721 if (section_offset >= sections[section]->size) {
730 retval = flash_unlock_address_range(target, run_address, run_size);
731 if (retval == ERROR_OK) {
733 /* calculate and erase sectors */
734 retval = flash_erase_address_range(target,
735 true, run_address, run_size);
739 if (retval == ERROR_OK) {
740 /* write flash sectors */
741 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
746 if (retval != ERROR_OK) {
747 /* abort operation */
752 *written += run_size; /* add run size to total written counter */
762 int flash_write(struct target *target, struct image *image,
763 uint32_t *written, int erase)
765 return flash_write_unlock(target, image, written, erase, false);