flash: add error message if image is too big for flash
[openocd/dnglaze.git] / src / flash / nor / core.c
blob429bad6762dcbe606cb776f5f39e1eceefc4cfd0
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 * *
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. *
12 * *
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. *
17 * *
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 ***************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 #include <flash/common.h>
28 #include <flash/nor/core.h>
29 #include <flash/nor/imp.h>
30 #include <target/image.h>
33 /**
34 * @file
35 * Upper level of NOR flash framework.
36 * The lower level interfaces are to drivers. These upper level ones
37 * primarily support access from Tcl scripts or from GDB.
40 static struct flash_bank *flash_banks;
42 int flash_driver_erase(struct flash_bank *bank, int first, int last)
44 int retval;
46 retval = bank->driver->erase(bank, first, last);
47 if (retval != ERROR_OK)
49 LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
52 return retval;
55 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
57 int retval;
59 /* callers may not supply illegal parameters ... */
60 if (first < 0 || first > last || last >= bank->num_sectors)
62 LOG_ERROR("illegal sector range");
63 return ERROR_FAIL;
66 /* force "set" to 0/1 */
67 set = !!set;
69 /* DANGER!
71 * We must not use any cached information about protection state!!!!
73 * There are a million things that could change the protect state:
75 * the target could have reset, power cycled, been hot plugged,
76 * the application could have run, etc.
78 * Drivers only receive valid sector range.
80 retval = bank->driver->protect(bank, set, first, last);
81 if (retval != ERROR_OK)
83 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
86 return retval;
89 int flash_driver_write(struct flash_bank *bank,
90 uint8_t *buffer, uint32_t offset, uint32_t count)
92 int retval;
94 retval = bank->driver->write(bank, buffer, offset, count);
95 if (retval != ERROR_OK)
97 LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
98 bank->base, offset, retval);
101 return retval;
104 int flash_driver_read(struct flash_bank *bank,
105 uint8_t *buffer, uint32_t offset, uint32_t count)
107 int retval;
109 LOG_DEBUG("call flash_driver_read()");
111 retval = bank->driver->read(bank, buffer, offset, count);
112 if (retval != ERROR_OK)
114 LOG_ERROR("error reading to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
115 bank->base, offset, retval);
118 return retval;
121 int default_flash_read(struct flash_bank *bank,
122 uint8_t *buffer, uint32_t offset, uint32_t count)
124 return target_read_buffer(bank->target, offset + bank->base, count, buffer);
127 void flash_bank_add(struct flash_bank *bank)
129 /* put flash bank in linked list */
130 unsigned bank_num = 0;
131 if (flash_banks)
133 /* find last flash bank */
134 struct flash_bank *p = flash_banks;
135 while (NULL != p->next)
137 bank_num += 1;
138 p = p->next;
140 p->next = bank;
141 bank_num += 1;
143 else
144 flash_banks = bank;
146 bank->bank_number = bank_num;
149 struct flash_bank *flash_bank_list(void)
151 return flash_banks;
154 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
156 struct flash_bank *p;
157 int i = 0;
159 for (p = flash_banks; p; p = p->next)
161 if (i++ == num)
163 return p;
166 LOG_ERROR("flash bank %d does not exist", num);
167 return NULL;
170 int flash_get_bank_count(void)
172 struct flash_bank *p;
173 int i = 0;
174 for (p = flash_banks; p; p = p->next)
176 i++;
178 return i;
181 struct flash_bank *get_flash_bank_by_name_noprobe(const char *name)
183 unsigned requested = get_flash_name_index(name);
184 unsigned found = 0;
186 struct flash_bank *bank;
187 for (bank = flash_banks; NULL != bank; bank = bank->next)
189 if (strcmp(bank->name, name) == 0)
190 return bank;
191 if (!flash_driver_name_matches(bank->driver->name, name))
192 continue;
193 if (++found < requested)
194 continue;
195 return bank;
197 return NULL;
200 struct flash_bank *get_flash_bank_by_name(const char *name)
202 struct flash_bank *bank;
203 int retval;
205 bank = get_flash_bank_by_name_noprobe(name);
206 if (bank != NULL)
208 retval = bank->driver->auto_probe(bank);
210 if (retval != ERROR_OK)
212 LOG_ERROR("auto_probe failed %d\n", retval);
213 return NULL;
217 return bank;
220 int get_flash_bank_by_num(int num, struct flash_bank **bank)
222 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
223 int retval;
225 if (p == NULL)
227 return ERROR_FAIL;
230 retval = p->driver->auto_probe(p);
232 if (retval != ERROR_OK)
234 LOG_ERROR("auto_probe failed %d\n", retval);
235 return retval;
237 *bank = p;
238 return ERROR_OK;
241 /* lookup flash bank by address */
242 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
244 struct flash_bank *c;
246 /* cycle through bank list */
247 for (c = flash_banks; c; c = c->next)
249 int retval;
250 retval = c->driver->auto_probe(c);
252 if (retval != ERROR_OK)
254 LOG_ERROR("auto_probe failed %d\n", retval);
255 return NULL;
257 /* check whether address belongs to this flash bank */
258 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
259 return c;
261 LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
262 return NULL;
265 int default_flash_mem_blank_check(struct flash_bank *bank)
267 struct target *target = bank->target;
268 const int buffer_size = 1024;
269 int i;
270 uint32_t nBytes;
271 int retval = ERROR_OK;
273 if (bank->target->state != TARGET_HALTED)
275 LOG_ERROR("Target not halted");
276 return ERROR_TARGET_NOT_HALTED;
279 uint8_t *buffer = malloc(buffer_size);
281 for (i = 0; i < bank->num_sectors; i++)
283 uint32_t j;
284 bank->sectors[i].is_erased = 1;
286 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
288 uint32_t chunk;
289 chunk = buffer_size;
290 if (chunk > (j - bank->sectors[i].size))
292 chunk = (j - bank->sectors[i].size);
295 retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
296 if (retval != ERROR_OK)
298 goto done;
301 for (nBytes = 0; nBytes < chunk; nBytes++)
303 if (buffer[nBytes] != 0xFF)
305 bank->sectors[i].is_erased = 0;
306 break;
312 done:
313 free(buffer);
315 return retval;
318 int default_flash_blank_check(struct flash_bank *bank)
320 struct target *target = bank->target;
321 int i;
322 int retval;
323 int fast_check = 0;
324 uint32_t blank;
326 if (bank->target->state != TARGET_HALTED)
328 LOG_ERROR("Target not halted");
329 return ERROR_TARGET_NOT_HALTED;
332 for (i = 0; i < bank->num_sectors; i++)
334 uint32_t address = bank->base + bank->sectors[i].offset;
335 uint32_t size = bank->sectors[i].size;
337 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
339 fast_check = 0;
340 break;
342 if (blank == 0xFF)
343 bank->sectors[i].is_erased = 1;
344 else
345 bank->sectors[i].is_erased = 0;
346 fast_check = 1;
349 if (!fast_check)
351 LOG_USER("Running slow fallback erase check - add working memory");
352 return default_flash_mem_blank_check(bank);
355 return ERROR_OK;
358 /* Manipulate given flash region, selecting the bank according to target
359 * and address. Maps an address range to a set of sectors, and issues
360 * the callback() on that set ... e.g. to erase or unprotect its members.
362 * (Note a current bad assumption: that protection operates on the same
363 * size sectors as erase operations use.)
365 * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
366 * range must fit those sectors exactly. This is clearly safe; it can't
367 * erase data which the caller said to leave alone, for example. If it's
368 * non-NULL, rather than failing, extra data in the first and/or last
369 * sectors will be added to the range, and that reason string is used when
370 * warning about those additions.
372 static int flash_iterate_address_range(struct target *target,
373 char *pad_reason, uint32_t addr, uint32_t length,
374 int (*callback)(struct flash_bank *bank, int first, int last))
376 struct flash_bank *c;
377 uint32_t last_addr = addr + length; /* first address AFTER end */
378 int first = -1;
379 int last = -1;
380 int i;
382 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
383 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
385 if (c->size == 0 || c->num_sectors == 0)
387 LOG_ERROR("Bank is invalid");
388 return ERROR_FLASH_BANK_INVALID;
391 if (length == 0)
393 /* special case, erase whole bank when length is zero */
394 if (addr != c->base)
396 LOG_ERROR("Whole bank access must start at beginning of bank.");
397 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
400 return callback(c, 0, c->num_sectors - 1);
403 /* check whether it all fits in this bank */
404 if (addr + length - 1 > c->base + c->size - 1)
406 LOG_ERROR("Flash access does not fit into bank.");
407 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
410 /** @todo: handle erasures that cross into adjacent banks */
412 addr -= c->base;
413 last_addr -= c->base;
415 for (i = 0; i < c->num_sectors; i++)
417 struct flash_sector *f = c->sectors + i;
418 uint32_t end = f->offset + f->size;
420 /* start only on a sector boundary */
421 if (first < 0) {
422 /* scanned past the first sector? */
423 if (addr < f->offset)
424 break;
426 /* is this the first sector? */
427 if (addr == f->offset)
428 first = i;
430 /* Does this need head-padding? If so, pad and warn;
431 * or else force an error.
433 * Such padding can make trouble, since *WE* can't
434 * ever know if that data was in use. The warning
435 * should help users sort out messes later.
437 else if (addr < end && pad_reason) {
438 /* FIXME say how many bytes (e.g. 80 KB) */
439 LOG_WARNING("Adding extra %s range, "
440 "%#8.8x to %#8.8x",
441 pad_reason,
442 (unsigned) f->offset,
443 (unsigned) addr - 1);
444 first = i;
445 } else
446 continue;
449 /* is this (also?) the last sector? */
450 if (last_addr == end) {
451 last = i;
452 break;
455 /* Does this need tail-padding? If so, pad and warn;
456 * or else force an error.
458 if (last_addr < end && pad_reason) {
459 /* FIXME say how many bytes (e.g. 80 KB) */
460 LOG_WARNING("Adding extra %s range, "
461 "%#8.8x to %#8.8x",
462 pad_reason,
463 (unsigned) last_addr,
464 (unsigned) end - 1);
465 last = i;
466 break;
469 /* MUST finish on a sector boundary */
470 if (last_addr <= f->offset)
471 break;
474 /* invalid start or end address? */
475 if (first == -1 || last == -1) {
476 LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
477 "is not sector-aligned",
478 (unsigned) (c->base + addr),
479 (unsigned) (c->base + last_addr - 1));
480 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
483 /* The NOR driver may trim this range down, based on what
484 * sectors are already erased/unprotected. GDB currently
485 * blocks such optimizations.
487 return callback(c, first, last);
490 int flash_erase_address_range(struct target *target,
491 bool pad, uint32_t addr, uint32_t length)
493 return flash_iterate_address_range(target, pad ? "erase" : NULL,
494 addr, length, &flash_driver_erase);
497 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
499 return flash_driver_protect(bank, 0, first, last);
502 int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
504 /* By default, pad to sector boundaries ... the real issue here
505 * is that our (only) caller *permanently* removes protection,
506 * and doesn't restore it.
508 return flash_iterate_address_range(target, "unprotect",
509 addr, length, &flash_driver_unprotect);
512 static int compare_section (const void * a, const void * b)
514 struct imageection *b1, *b2;
515 b1=*((struct imageection **)a);
516 b2=*((struct imageection **)b);
518 if (b1->base_address == b2->base_address)
520 return 0;
521 } else if (b1->base_address > b2->base_address)
523 return 1;
524 } else
526 return -1;
531 int flash_write_unlock(struct target *target, struct image *image,
532 uint32_t *written, int erase, bool unlock)
534 int retval = ERROR_OK;
536 int section;
537 uint32_t section_offset;
538 struct flash_bank *c;
539 int *padding;
541 section = 0;
542 section_offset = 0;
544 if (written)
545 *written = 0;
547 if (erase)
549 /* assume all sectors need erasing - stops any problems
550 * when flash_write is called multiple times */
552 flash_set_dirty();
555 /* allocate padding array */
556 padding = calloc(image->num_sections, sizeof(*padding));
558 /* This fn requires all sections to be in ascending order of addresses,
559 * whereas an image can have sections out of order. */
560 struct imageection **sections = malloc(sizeof(struct imageection *) *
561 image->num_sections);
562 int i;
563 for (i = 0; i < image->num_sections; i++)
565 sections[i] = &image->sections[i];
568 qsort(sections, image->num_sections, sizeof(struct imageection *),
569 compare_section);
571 /* loop until we reach end of the image */
572 while (section < image->num_sections)
574 uint32_t buffer_size;
575 uint8_t *buffer;
576 int section_first;
577 int section_last;
578 uint32_t run_address = sections[section]->base_address + section_offset;
579 uint32_t run_size = sections[section]->size - section_offset;
580 int pad_bytes = 0;
582 if (sections[section]->size == 0)
584 LOG_WARNING("empty section %d", section);
585 section++;
586 section_offset = 0;
587 continue;
590 /* find the corresponding flash bank */
591 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
593 section++; /* and skip it */
594 section_offset = 0;
595 continue;
598 /* collect consecutive sections which fall into the same bank */
599 section_first = section;
600 section_last = section;
601 padding[section] = 0;
602 while ((run_address + run_size - 1 < c->base + c->size - 1)
603 && (section_last + 1 < image->num_sections))
605 /* sections are sorted */
606 assert(sections[section_last + 1]->base_address >= c->base);
607 if (sections[section_last + 1]->base_address >= (c->base + c->size))
609 /* Done with this bank */
610 break;
613 /* FIXME This needlessly touches sectors BETWEEN the
614 * sections it's writing. Without auto erase, it just
615 * writes ones. That WILL INVALIDATE data in cases
616 * like Stellaris Tempest chips, corrupting internal
617 * ECC codes; and at least FreeScale suggests issues
618 * with that approach (in HC11 documentation).
620 * With auto erase enabled, data in those sectors will
621 * be needlessly destroyed; and some of the limited
622 * number of flash erase cycles will be wasted...
624 * In both cases, the extra writes slow things down.
627 /* if we have multiple sections within our image,
628 * flash programming could fail due to alignment issues
629 * attempt to rebuild a consecutive buffer for the flash loader */
630 pad_bytes = (sections[section_last + 1]->base_address) - (run_address + run_size);
631 padding[section_last] = pad_bytes;
632 run_size += sections[++section_last]->size;
633 run_size += pad_bytes;
635 if (pad_bytes > 0)
636 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
639 if (run_address + run_size - 1 > c->base + c->size - 1)
641 LOG_ERROR("The image is too big for the flash");
642 return ERROR_FAIL;
645 /* If we're applying any sector automagic, then pad this
646 * (maybe-combined) segment to the end of its last sector.
648 if (unlock || erase) {
649 int sector;
650 uint32_t offset_start = run_address - c->base;
651 uint32_t offset_end = offset_start + run_size;
652 uint32_t end = offset_end, delta;
654 for (sector = 0; sector < c->num_sectors; sector++) {
655 end = c->sectors[sector].offset
656 + c->sectors[sector].size;
657 if (offset_end <= end)
658 break;
661 delta = end - offset_end;
662 padding[section_last] += delta;
663 run_size += delta;
666 /* allocate buffer */
667 buffer = malloc(run_size);
668 buffer_size = 0;
670 /* read sections to the buffer */
671 while (buffer_size < run_size)
673 size_t size_read;
675 size_read = run_size - buffer_size;
676 if (size_read > sections[section]->size - section_offset)
677 size_read = sections[section]->size - section_offset;
679 /* KLUDGE!
681 * #¤%#"%¤% we have to figure out the section # from the sorted
682 * list of pointers to sections to invoke image_read_section()...
684 intptr_t diff = (intptr_t)sections[section] - (intptr_t)image->sections;
685 int t_section_num = diff / sizeof(struct imageection);
687 LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, section_offset = %d, buffer_size = %d, size_read = %d",
688 (int)section,
689 (int)t_section_num, (int)section_offset, (int)buffer_size, (int)size_read);
690 if ((retval = image_read_section(image, t_section_num, section_offset,
691 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
693 free(buffer);
694 goto done;
697 /* see if we need to pad the section */
698 while (padding[section]--)
699 (buffer + buffer_size)[size_read++] = 0xff;
701 buffer_size += size_read;
702 section_offset += size_read;
704 if (section_offset >= sections[section]->size)
706 section++;
707 section_offset = 0;
711 retval = ERROR_OK;
713 if (unlock)
715 retval = flash_unlock_address_range(target, run_address, run_size);
717 if (retval == ERROR_OK)
719 if (erase)
721 /* calculate and erase sectors */
722 retval = flash_erase_address_range(target,
723 true, run_address, run_size);
727 if (retval == ERROR_OK)
729 /* write flash sectors */
730 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
733 free(buffer);
735 if (retval != ERROR_OK)
737 /* abort operation */
738 goto done;
741 if (written != NULL)
742 *written += run_size; /* add run size to total written counter */
746 done:
747 free(sections);
748 free(padding);
750 return retval;
753 int flash_write(struct target *target, struct image *image,
754 uint32_t *written, int erase)
756 return flash_write_unlock(target, image, written, erase, false);