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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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) {
236 retval = c->driver->auto_probe(c);
238 if (retval != ERROR_OK) {
239 LOG_ERROR("auto_probe failed");
242 /* check whether address belongs to this flash bank */
243 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target) {
250 LOG_ERROR("No flash at address 0x%08" PRIx32, addr);
256 int default_flash_mem_blank_check(struct flash_bank *bank)
258 struct target *target = bank->target;
259 const int buffer_size = 1024;
262 int retval = ERROR_OK;
264 if (bank->target->state != TARGET_HALTED) {
265 LOG_ERROR("Target not halted");
266 return ERROR_TARGET_NOT_HALTED;
269 uint8_t *buffer = malloc(buffer_size);
271 for (i = 0; i < bank->num_sectors; i++) {
273 bank->sectors[i].is_erased = 1;
275 for (j = 0; j < bank->sectors[i].size; j += buffer_size) {
278 if (chunk > (j - bank->sectors[i].size))
279 chunk = (j - bank->sectors[i].size);
281 retval = target_read_memory(target,
282 bank->base + bank->sectors[i].offset + j,
286 if (retval != ERROR_OK)
289 for (nBytes = 0; nBytes < chunk; nBytes++) {
290 if (buffer[nBytes] != 0xFF) {
291 bank->sectors[i].is_erased = 0;
304 int default_flash_blank_check(struct flash_bank *bank)
306 struct target *target = bank->target;
312 if (bank->target->state != TARGET_HALTED) {
313 LOG_ERROR("Target not halted");
314 return ERROR_TARGET_NOT_HALTED;
317 for (i = 0; i < bank->num_sectors; i++) {
318 uint32_t address = bank->base + bank->sectors[i].offset;
319 uint32_t size = bank->sectors[i].size;
321 retval = target_blank_check_memory(target, address, size, &blank);
322 if (retval != ERROR_OK) {
327 bank->sectors[i].is_erased = 1;
329 bank->sectors[i].is_erased = 0;
334 LOG_USER("Running slow fallback erase check - add working memory");
335 return default_flash_mem_blank_check(bank);
341 /* Manipulate given flash region, selecting the bank according to target
342 * and address. Maps an address range to a set of sectors, and issues
343 * the callback() on that set ... e.g. to erase or unprotect its members.
345 * (Note a current bad assumption: that protection operates on the same
346 * size sectors as erase operations use.)
348 * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
349 * range must fit those sectors exactly. This is clearly safe; it can't
350 * erase data which the caller said to leave alone, for example. If it's
351 * non-NULL, rather than failing, extra data in the first and/or last
352 * sectors will be added to the range, and that reason string is used when
353 * warning about those additions.
355 static int flash_iterate_address_range_inner(struct target *target,
356 char *pad_reason, uint32_t addr, uint32_t length,
357 int (*callback)(struct flash_bank *bank, int first, int last))
359 struct flash_bank *c;
360 uint32_t last_addr = addr + length; /* first address AFTER end */
365 int retval = get_flash_bank_by_addr(target, addr, true, &c);
366 if (retval != ERROR_OK)
369 if (c->size == 0 || c->num_sectors == 0) {
370 LOG_ERROR("Bank is invalid");
371 return ERROR_FLASH_BANK_INVALID;
375 /* special case, erase whole bank when length is zero */
376 if (addr != c->base) {
377 LOG_ERROR("Whole bank access must start at beginning of bank.");
378 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
381 return callback(c, 0, c->num_sectors - 1);
384 /* check whether it all fits in this bank */
385 if (addr + length - 1 > c->base + c->size - 1) {
386 LOG_ERROR("Flash access does not fit into bank.");
387 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
390 /** @todo: handle erasures that cross into adjacent banks */
393 last_addr -= c->base;
395 for (i = 0; i < c->num_sectors; i++) {
396 struct flash_sector *f = c->sectors + i;
397 uint32_t end = f->offset + f->size;
399 /* start only on a sector boundary */
401 /* scanned past the first sector? */
402 if (addr < f->offset)
405 /* is this the first sector? */
406 if (addr == f->offset)
409 /* Does this need head-padding? If so, pad and warn;
410 * or else force an error.
412 * Such padding can make trouble, since *WE* can't
413 * ever know if that data was in use. The warning
414 * should help users sort out messes later.
416 else if (addr < end && pad_reason) {
417 /* FIXME say how many bytes (e.g. 80 KB) */
418 LOG_WARNING("Adding extra %s range, "
421 (unsigned) f->offset,
422 (unsigned) addr - 1);
428 /* is this (also?) the last sector? */
429 if (last_addr == end) {
434 /* Does this need tail-padding? If so, pad and warn;
435 * or else force an error.
437 if (last_addr < end && pad_reason) {
438 /* FIXME say how many bytes (e.g. 80 KB) */
439 LOG_WARNING("Adding extra %s range, "
442 (unsigned) last_addr,
448 /* MUST finish on a sector boundary */
449 if (last_addr <= f->offset)
453 /* invalid start or end address? */
454 if (first == -1 || last == -1) {
455 LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
456 "is not sector-aligned",
457 (unsigned) (c->base + addr),
458 (unsigned) (c->base + last_addr - 1));
459 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
462 /* The NOR driver may trim this range down, based on what
463 * sectors are already erased/unprotected. GDB currently
464 * blocks such optimizations.
466 return callback(c, first, last);
469 /* The inner fn only handles a single bank, we could be spanning
472 static int flash_iterate_address_range(struct target *target,
473 char *pad_reason, uint32_t addr, uint32_t length,
474 int (*callback)(struct flash_bank *bank, int first, int last))
476 struct flash_bank *c;
477 int retval = ERROR_OK;
479 /* Danger! zero-length iterations means entire bank! */
481 retval = get_flash_bank_by_addr(target, addr, true, &c);
482 if (retval != ERROR_OK)
485 uint32_t cur_length = length;
486 /* check whether it all fits in this bank */
487 if (addr + length - 1 > c->base + c->size - 1) {
488 LOG_DEBUG("iterating over more than one flash bank.");
489 cur_length = c->base + c->size - addr;
491 retval = flash_iterate_address_range_inner(target,
492 pad_reason, addr, cur_length,
494 if (retval != ERROR_OK)
497 length -= cur_length;
499 } while (length > 0);
504 int flash_erase_address_range(struct target *target,
505 bool pad, uint32_t addr, uint32_t length)
507 return flash_iterate_address_range(target, pad ? "erase" : NULL,
508 addr, length, &flash_driver_erase);
511 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
513 return flash_driver_protect(bank, 0, first, last);
516 int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
518 /* By default, pad to sector boundaries ... the real issue here
519 * is that our (only) caller *permanently* removes protection,
520 * and doesn't restore it.
522 return flash_iterate_address_range(target, "unprotect",
523 addr, length, &flash_driver_unprotect);
526 static int compare_section(const void *a, const void *b)
528 struct imagesection *b1, *b2;
529 b1 = *((struct imagesection **)a);
530 b2 = *((struct imagesection **)b);
532 if (b1->base_address == b2->base_address)
534 else if (b1->base_address > b2->base_address)
540 int flash_write_unlock(struct target *target, struct image *image,
541 uint32_t *written, int erase, bool unlock)
543 int retval = ERROR_OK;
546 uint32_t section_offset;
547 struct flash_bank *c;
557 /* assume all sectors need erasing - stops any problems
558 * when flash_write is called multiple times */
563 /* allocate padding array */
564 padding = calloc(image->num_sections, sizeof(*padding));
566 /* This fn requires all sections to be in ascending order of addresses,
567 * whereas an image can have sections out of order. */
568 struct imagesection **sections = malloc(sizeof(struct imagesection *) *
569 image->num_sections);
571 for (i = 0; i < image->num_sections; i++)
572 sections[i] = &image->sections[i];
574 qsort(sections, image->num_sections, sizeof(struct imagesection *),
577 /* loop until we reach end of the image */
578 while (section < image->num_sections) {
579 uint32_t buffer_size;
582 uint32_t run_address = sections[section]->base_address + section_offset;
583 uint32_t run_size = sections[section]->size - section_offset;
586 if (sections[section]->size == 0) {
587 LOG_WARNING("empty section %d", section);
593 /* find the corresponding flash bank */
594 retval = get_flash_bank_by_addr(target, run_address, false, &c);
595 if (retval != ERROR_OK)
598 section++; /* and skip it */
603 /* collect consecutive sections which fall into the same bank */
604 section_last = section;
605 padding[section] = 0;
606 while ((run_address + run_size - 1 < c->base + c->size - 1) &&
607 (section_last + 1 < image->num_sections)) {
608 /* sections are sorted */
609 assert(sections[section_last + 1]->base_address >= c->base);
610 if (sections[section_last + 1]->base_address >= (c->base + c->size)) {
611 /* Done with this bank */
615 /* FIXME This needlessly touches sectors BETWEEN the
616 * sections it's writing. Without auto erase, it just
617 * writes ones. That WILL INVALIDATE data in cases
618 * like Stellaris Tempest chips, corrupting internal
619 * ECC codes; and at least FreeScale suggests issues
620 * with that approach (in HC11 documentation).
622 * With auto erase enabled, data in those sectors will
623 * be needlessly destroyed; and some of the limited
624 * number of flash erase cycles will be wasted...
626 * In both cases, the extra writes slow things down.
629 /* if we have multiple sections within our image,
630 * flash programming could fail due to alignment issues
631 * attempt to rebuild a consecutive buffer for the flash loader */
632 pad_bytes = (sections[section_last + 1]->base_address) - (run_address + run_size);
633 padding[section_last] = pad_bytes;
634 run_size += sections[++section_last]->size;
635 run_size += pad_bytes;
638 LOG_INFO("Padding image section %d with %d bytes",
643 if (run_address + run_size - 1 > c->base + c->size - 1) {
644 /* If we have more than one flash chip back to back, then we limit
645 * the current write operation to the current chip.
647 LOG_DEBUG("Truncate flash run size to the current flash chip.");
649 run_size = c->base + c->size - run_address;
650 assert(run_size > 0);
653 /* If we're applying any sector automagic, then pad this
654 * (maybe-combined) segment to the end of its last sector.
656 if (unlock || erase) {
658 uint32_t offset_start = run_address - c->base;
659 uint32_t offset_end = offset_start + run_size;
660 uint32_t end = offset_end, delta;
662 for (sector = 0; sector < c->num_sectors; sector++) {
663 end = c->sectors[sector].offset
664 + c->sectors[sector].size;
665 if (offset_end <= end)
669 delta = end - offset_end;
670 padding[section_last] += delta;
674 /* allocate buffer */
675 buffer = malloc(run_size);
676 if (buffer == NULL) {
677 LOG_ERROR("Out of memory for flash bank buffer");
683 /* read sections to the buffer */
684 while (buffer_size < run_size) {
687 size_read = run_size - buffer_size;
688 if (size_read > sections[section]->size - section_offset)
689 size_read = sections[section]->size - section_offset;
693 * #¤%#"%¤% we have to figure out the section # from the sorted
694 * list of pointers to sections to invoke image_read_section()...
696 intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
697 int t_section_num = diff / sizeof(struct imagesection);
699 LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
700 "section_offset = %d, buffer_size = %d, size_read = %d",
701 (int)section, (int)t_section_num, (int)section_offset,
702 (int)buffer_size, (int)size_read);
703 retval = image_read_section(image, t_section_num, section_offset,
704 size_read, buffer + buffer_size, &size_read);
705 if (retval != ERROR_OK || size_read == 0) {
710 /* see if we need to pad the section */
711 while (padding[section]--)
712 (buffer + buffer_size)[size_read++] = 0xff;
714 buffer_size += size_read;
715 section_offset += size_read;
717 if (section_offset >= sections[section]->size) {
726 retval = flash_unlock_address_range(target, run_address, run_size);
727 if (retval == ERROR_OK) {
729 /* calculate and erase sectors */
730 retval = flash_erase_address_range(target,
731 true, run_address, run_size);
735 if (retval == ERROR_OK) {
736 /* write flash sectors */
737 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
742 if (retval != ERROR_OK) {
743 /* abort operation */
748 *written += run_size; /* add run size to total written counter */
758 int flash_write(struct target *target, struct image *image,
759 uint32_t *written, int erase)
761 return flash_write_unlock(target, image, written, erase, false);