1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
32 #include "time_support.h"
34 static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock);
38 extern struct flash_driver lpc2000_flash;
39 extern struct flash_driver lpc288x_flash;
40 extern struct flash_driver lpc2900_flash;
41 extern struct flash_driver cfi_flash;
42 extern struct flash_driver at91sam3_flash;
43 extern struct flash_driver at91sam7_flash;
44 extern struct flash_driver str7x_flash;
45 extern struct flash_driver str9x_flash;
46 extern struct flash_driver aduc702x_flash;
47 extern struct flash_driver stellaris_flash;
48 extern struct flash_driver str9xpec_flash;
49 extern struct flash_driver stm32x_flash;
50 extern struct flash_driver tms470_flash;
51 extern struct flash_driver ecosflash_flash;
52 extern struct flash_driver ocl_flash;
53 extern struct flash_driver pic32mx_flash;
54 extern struct flash_driver avr_flash;
55 extern struct flash_driver faux_flash;
57 struct flash_driver *flash_drivers[] = {
79 struct flash_bank *flash_banks;
80 static struct command *flash_cmd;
82 /* wafer thin wrapper for invoking the flash driver */
83 static int flash_driver_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
87 retval = bank->driver->write(bank, buffer, offset, count);
88 if (retval != ERROR_OK)
90 LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
91 bank->base, offset, retval);
97 static int flash_driver_erase(struct flash_bank *bank, int first, int last)
101 retval = bank->driver->erase(bank, first, last);
102 if (retval != ERROR_OK)
104 LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
110 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
114 retval = bank->driver->protect(bank, set, first, last);
115 if (retval != ERROR_OK)
117 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
123 static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
125 struct flash_bank *p;
128 Jim_WrongNumArgs(interp, 1, argv, "no arguments to flash_banks command");
132 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
133 for (p = flash_banks; p; p = p->next)
135 Jim_Obj *elem = Jim_NewListObj(interp, NULL, 0);
137 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "name", -1));
138 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, p->driver->name, -1));
139 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "base", -1));
140 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->base));
141 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "size", -1));
142 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->size));
143 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "bus_width", -1));
144 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->bus_width));
145 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "chip_width", -1));
146 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->chip_width));
148 Jim_ListAppendElement(interp, list, elem);
151 Jim_SetResult(interp, list);
156 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
158 struct flash_bank *p;
161 for (p = flash_banks; p; p = p->next)
168 LOG_ERROR("flash bank %d does not exist", num);
172 int flash_get_bank_count(void)
174 struct flash_bank *p;
176 for (p = flash_banks; p; p = p->next)
183 struct flash_bank *get_flash_bank_by_num(int num)
185 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
191 retval = p->driver->auto_probe(p);
193 if (retval != ERROR_OK)
195 LOG_ERROR("auto_probe failed %d\n", retval);
201 COMMAND_HELPER(flash_command_get_bank_by_num,
202 unsigned name_index, struct flash_bank **bank)
204 const char *name = CMD_ARGV[name_index];
206 COMMAND_PARSE_NUMBER(uint, name, bank_num);
208 *bank = get_flash_bank_by_num(bank_num);
211 command_print(CMD_CTX, "flash bank '%s' not found", name);
212 return ERROR_INVALID_ARGUMENTS;
218 COMMAND_HANDLER(handle_flash_bank_command)
223 struct target *target;
227 return ERROR_COMMAND_SYNTAX_ERROR;
230 if ((target = get_target(CMD_ARGV[5])) == NULL)
232 LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
236 for (i = 0; flash_drivers[i]; i++)
238 if (strcmp(CMD_ARGV[0], flash_drivers[i]->name) != 0)
241 struct flash_bank *p, *c;
243 /* register flash specific commands */
244 if (flash_drivers[i]->register_commands(CMD_CTX) != ERROR_OK)
246 LOG_ERROR("couldn't register '%s' commands", CMD_ARGV[0]);
250 c = malloc(sizeof(struct flash_bank));
252 c->driver = flash_drivers[i];
253 c->driver_priv = NULL;
254 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], c->base);
255 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
256 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
257 COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
262 retval = CALL_COMMAND_HANDLER(flash_drivers[i]->flash_bank_command, c);
263 if (ERROR_OK != retval)
265 LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32 , CMD_ARGV[0], c->base);
270 /* put flash bank in linked list */
274 /* find last flash bank */
275 for (p = flash_banks; p && p->next; p = p->next) bank_num++;
278 c->bank_number = bank_num + 1;
289 /* no matching flash driver found */
292 LOG_ERROR("flash driver '%s' not found", CMD_ARGV[0]);
299 COMMAND_HANDLER(handle_flash_info_command)
301 struct flash_bank *p;
307 return ERROR_COMMAND_SYNTAX_ERROR;
310 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
312 for (p = flash_banks; p; p = p->next, i++)
319 /* attempt auto probe */
320 if ((retval = p->driver->auto_probe(p)) != ERROR_OK)
323 command_print(CMD_CTX,
324 "#%" PRIi32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", buswidth %i, chipwidth %i",
331 for (j = 0; j < p->num_sectors; j++)
335 if (p->sectors[j].is_protected == 0)
336 protect_state = "not protected";
337 else if (p->sectors[j].is_protected == 1)
338 protect_state = "protected";
340 protect_state = "protection state unknown";
342 command_print(CMD_CTX,
343 "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
345 p->sectors[j].offset,
347 p->sectors[j].size >> 10,
351 *buf = '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
352 retval = p->driver->info(p, buf, sizeof(buf));
353 command_print(CMD_CTX, "%s", buf);
354 if (retval != ERROR_OK)
355 LOG_ERROR("error retrieving flash info (%d)", retval);
361 COMMAND_HANDLER(handle_flash_probe_command)
367 return ERROR_COMMAND_SYNTAX_ERROR;
371 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
372 struct flash_bank *p = get_flash_bank_by_num_noprobe(bank_nr);
375 if ((retval = p->driver->probe(p)) == ERROR_OK)
377 command_print(CMD_CTX, "flash '%s' found at 0x%8.8" PRIx32, p->driver->name, p->base);
379 else if (retval == ERROR_FLASH_BANK_INVALID)
381 command_print(CMD_CTX, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32,
382 CMD_ARGV[0], p->base);
386 command_print(CMD_CTX, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32,
387 CMD_ARGV[0], p->base);
392 command_print(CMD_CTX, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
398 COMMAND_HANDLER(handle_flash_erase_check_command)
402 return ERROR_COMMAND_SYNTAX_ERROR;
405 struct flash_bank *p;
406 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &p);
407 if (ERROR_OK != retval)
411 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
413 command_print(CMD_CTX, "successfully checked erase state");
417 command_print(CMD_CTX, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
418 CMD_ARGV[0], p->base);
421 for (j = 0; j < p->num_sectors; j++)
425 if (p->sectors[j].is_erased == 0)
426 erase_state = "not erased";
427 else if (p->sectors[j].is_erased == 1)
428 erase_state = "erased";
430 erase_state = "erase state unknown";
432 command_print(CMD_CTX,
433 "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
435 p->sectors[j].offset,
437 p->sectors[j].size >> 10,
444 COMMAND_HANDLER(handle_flash_erase_address_command)
446 struct flash_bank *p;
451 struct target *target = get_current_target(CMD_CTX);
454 return ERROR_COMMAND_SYNTAX_ERROR;
456 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], address);
457 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], length);
460 command_print(CMD_CTX, "Length must be >0");
461 return ERROR_COMMAND_SYNTAX_ERROR;
464 p = get_flash_bank_by_addr(target, address);
470 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
473 struct duration bench;
474 duration_start(&bench);
476 retval = flash_erase_address_range(target, address, length);
478 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
480 command_print(CMD_CTX, "erased address 0x%8.8x (length %i)"
481 " in %fs (%0.3f kb/s)", address, length,
482 duration_elapsed(&bench), duration_kbps(&bench, length));
488 COMMAND_HANDLER(handle_flash_protect_check_command)
491 return ERROR_COMMAND_SYNTAX_ERROR;
493 struct flash_bank *p;
494 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &p);
495 if (ERROR_OK != retval)
498 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
500 command_print(CMD_CTX, "successfully checked protect state");
502 else if (retval == ERROR_FLASH_OPERATION_FAILED)
504 command_print(CMD_CTX, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
508 command_print(CMD_CTX, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
514 static int flash_check_sector_parameters(struct command_context *cmd_ctx,
515 uint32_t first, uint32_t last, uint32_t num_sectors)
517 if (!(first <= last)) {
518 command_print(cmd_ctx, "ERROR: "
519 "first sector must be <= last sector");
523 if (!(last <= (num_sectors - 1))) {
524 command_print(cmd_ctx, "ERROR: last sector must be <= %d",
525 (int) num_sectors - 1);
532 COMMAND_HANDLER(handle_flash_erase_command)
535 return ERROR_COMMAND_SYNTAX_ERROR;
541 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
542 struct flash_bank *p = get_flash_bank_by_num(bank_nr);
546 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
547 if (strcmp(CMD_ARGV[2], "last") == 0)
548 last = p->num_sectors - 1;
550 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
553 if ((retval = flash_check_sector_parameters(CMD_CTX,
554 first, last, p->num_sectors)) != ERROR_OK)
557 struct duration bench;
558 duration_start(&bench);
560 retval = flash_driver_erase(p, first, last);
562 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
564 command_print(CMD_CTX, "erased sectors %" PRIu32 " "
565 "through %" PRIu32" on flash bank %" PRIu32 " "
566 "in %fs", first, last, bank_nr, duration_elapsed(&bench));
572 COMMAND_HANDLER(handle_flash_protect_command)
575 return ERROR_COMMAND_SYNTAX_ERROR;
581 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
582 struct flash_bank *p = get_flash_bank_by_num(bank_nr);
586 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
587 if (strcmp(CMD_ARGV[2], "last") == 0)
588 last = p->num_sectors - 1;
590 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
593 COMMAND_PARSE_ON_OFF(CMD_ARGV[3], set);
596 if ((retval = flash_check_sector_parameters(CMD_CTX,
597 first, last, p->num_sectors)) != ERROR_OK)
600 retval = flash_driver_protect(p, set, first, last);
601 if (retval == ERROR_OK) {
602 command_print(CMD_CTX, "%s protection for sectors %i "
603 "through %i on flash bank %i",
604 (set) ? "set" : "cleared", (int) first,
605 (int) last, (int) bank_nr);
611 COMMAND_HANDLER(handle_flash_write_image_command)
613 struct target *target = get_current_target(CMD_CTX);
622 return ERROR_COMMAND_SYNTAX_ERROR;
625 /* flash auto-erase is disabled by default*/
627 bool auto_unlock = false;
631 if (strcmp(CMD_ARGV[0], "erase") == 0)
636 command_print(CMD_CTX, "auto erase enabled");
637 } else if (strcmp(CMD_ARGV[0], "unlock") == 0)
642 command_print(CMD_CTX, "auto unlock enabled");
651 return ERROR_COMMAND_SYNTAX_ERROR;
656 LOG_ERROR("no target selected");
660 struct duration bench;
661 duration_start(&bench);
665 image.base_address_set = 1;
666 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], image.base_address);
670 image.base_address_set = 0;
671 image.base_address = 0x0;
674 image.start_address_set = 0;
676 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
677 if (retval != ERROR_OK)
682 retval = flash_write_unlock(target, &image, &written, auto_erase, auto_unlock);
683 if (retval != ERROR_OK)
689 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
691 command_print(CMD_CTX, "wrote %" PRIu32 " byte from file %s "
692 "in %fs (%0.3f kb/s)", written, CMD_ARGV[0],
693 duration_elapsed(&bench), duration_kbps(&bench, written));
701 COMMAND_HANDLER(handle_flash_fill_command)
708 uint8_t readback[1024];
710 uint32_t cur_size = 0;
711 uint32_t chunk_count;
712 struct target *target = get_current_target(CMD_CTX);
717 return ERROR_COMMAND_SYNTAX_ERROR;
719 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
720 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
721 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
738 return ERROR_COMMAND_SYNTAX_ERROR;
741 chunk_count = MIN(count, (1024 / wordsize));
745 for (i = 0; i < chunk_count; i++)
747 target_buffer_set_u32(target, chunk + i * wordsize, pattern);
751 for (i = 0; i < chunk_count; i++)
753 target_buffer_set_u16(target, chunk + i * wordsize, pattern);
757 memset(chunk, pattern, chunk_count);
760 LOG_ERROR("BUG: can't happen");
764 struct duration bench;
765 duration_start(&bench);
767 for (wrote = 0; wrote < (count*wordsize); wrote += cur_size)
769 cur_size = MIN((count*wordsize - wrote), sizeof(chunk));
770 struct flash_bank *bank;
771 bank = get_flash_bank_by_addr(target, address);
776 err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
780 err = target_read_buffer(target, address + wrote, cur_size, readback);
785 for (i = 0; i < cur_size; i++)
787 if (readback[i]!=chunk[i])
789 LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
790 address + wrote + i, readback[i], chunk[i]);
796 if (duration_measure(&bench) == ERROR_OK)
798 command_print(CMD_CTX, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32
799 " in %fs (%0.3f kb/s)", wrote, address,
800 duration_elapsed(&bench), duration_kbps(&bench, wrote));
805 COMMAND_HANDLER(handle_flash_write_bank_command)
809 struct fileio fileio;
812 return ERROR_COMMAND_SYNTAX_ERROR;
814 struct duration bench;
815 duration_start(&bench);
817 struct flash_bank *p;
818 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &p);
819 if (ERROR_OK != retval)
822 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
824 if (fileio_open(&fileio, CMD_ARGV[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
829 buffer = malloc(fileio.size);
831 if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
834 fileio_close(&fileio);
838 retval = flash_driver_write(p, buffer, offset, buf_cnt);
843 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
845 command_print(CMD_CTX, "wrote %zu byte from file %s to flash bank %u"
846 " at offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
847 fileio.size, CMD_ARGV[1], p->bank_number, offset,
848 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
851 fileio_close(&fileio);
856 void flash_set_dirty(void)
858 struct flash_bank *c;
861 /* set all flash to require erasing */
862 for (c = flash_banks; c; c = c->next)
864 for (i = 0; i < c->num_sectors; i++)
866 c->sectors[i].is_erased = 0;
871 /* lookup flash bank by address */
872 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
874 struct flash_bank *c;
876 /* cycle through bank list */
877 for (c = flash_banks; c; c = c->next)
880 retval = c->driver->auto_probe(c);
882 if (retval != ERROR_OK)
884 LOG_ERROR("auto_probe failed %d\n", retval);
887 /* check whether address belongs to this flash bank */
888 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
891 LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
895 /* erase given flash region, selects proper bank according to target and address */
896 static int flash_iterate_address_range(struct target *target, uint32_t addr, uint32_t length,
897 int (*callback)(struct flash_bank *bank, int first, int last))
899 struct flash_bank *c;
904 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
905 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
907 if (c->size == 0 || c->num_sectors == 0)
909 LOG_ERROR("Bank is invalid");
910 return ERROR_FLASH_BANK_INVALID;
915 /* special case, erase whole bank when length is zero */
917 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
919 return callback(c, 0, c->num_sectors - 1);
922 /* check whether it fits */
923 if (addr + length - 1 > c->base + c->size - 1)
924 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
928 for (i = 0; i < c->num_sectors; i++)
930 /* check whether sector overlaps with the given range and is not yet erased */
931 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
932 /* if first is not set yet then this is the first sector */
935 last = i; /* and it is the last one so far in any case */
939 if (first == -1 || last == -1)
942 return callback(c, first, last);
947 int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
949 return flash_iterate_address_range(target, addr, length, &flash_driver_erase);
952 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
954 return flash_driver_protect(bank, 0, first, last);
957 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
959 return flash_iterate_address_range(target, addr, length, &flash_driver_unprotect);
963 /* write (optional verify) an image to flash memory of the given target */
964 static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock)
966 int retval = ERROR_OK;
969 uint32_t section_offset;
970 struct flash_bank *c;
981 /* assume all sectors need erasing - stops any problems
982 * when flash_write is called multiple times */
987 /* allocate padding array */
988 padding = malloc(image->num_sections * sizeof(padding));
990 /* loop until we reach end of the image */
991 while (section < image->num_sections)
993 uint32_t buffer_size;
997 uint32_t run_address = image->sections[section].base_address + section_offset;
998 uint32_t run_size = image->sections[section].size - section_offset;
1001 if (image->sections[section].size == 0)
1003 LOG_WARNING("empty section %d", section);
1009 /* find the corresponding flash bank */
1010 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
1012 section++; /* and skip it */
1017 /* collect consecutive sections which fall into the same bank */
1018 section_first = section;
1019 section_last = section;
1020 padding[section] = 0;
1021 while ((run_address + run_size - 1 < c->base + c->size - 1)
1022 && (section_last + 1 < image->num_sections))
1024 if (image->sections[section_last + 1].base_address < (run_address + run_size))
1026 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
1029 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
1030 * attempt to rebuild a consecutive buffer for the flash loader */
1031 pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
1032 if ((run_address + run_size + pad_bytes) > (c->base + c->size))
1034 padding[section_last] = pad_bytes;
1035 run_size += image->sections[++section_last].size;
1036 run_size += pad_bytes;
1037 padding[section_last] = 0;
1039 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
1042 /* fit the run into bank constraints */
1043 if (run_address + run_size - 1 > c->base + c->size - 1)
1045 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
1046 (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
1047 run_size = c->base + c->size - run_address;
1050 /* allocate buffer */
1051 buffer = malloc(run_size);
1054 /* read sections to the buffer */
1055 while (buffer_size < run_size)
1059 size_read = run_size - buffer_size;
1060 if (size_read > image->sections[section].size - section_offset)
1061 size_read = image->sections[section].size - section_offset;
1063 if ((retval = image_read_section(image, section, section_offset,
1064 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
1071 /* see if we need to pad the section */
1072 while (padding[section]--)
1073 (buffer + buffer_size)[size_read++] = 0xff;
1075 buffer_size += size_read;
1076 section_offset += size_read;
1078 if (section_offset >= image->sections[section].size)
1089 retval = flash_unlock_address_range(target, run_address, run_size);
1091 if (retval == ERROR_OK)
1095 /* calculate and erase sectors */
1096 retval = flash_erase_address_range(target, run_address, run_size);
1100 if (retval == ERROR_OK)
1102 /* write flash sectors */
1103 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
1108 if (retval != ERROR_OK)
1111 return retval; /* abort operation */
1114 if (written != NULL)
1115 *written += run_size; /* add run size to total written counter */
1123 int flash_write(struct target *target, struct image *image, uint32_t *written, int erase)
1125 return flash_write_unlock(target, image, written, erase, false);
1128 int default_flash_mem_blank_check(struct flash_bank *bank)
1130 struct target *target = bank->target;
1131 uint8_t buffer[1024];
1132 int buffer_size = sizeof(buffer);
1136 if (bank->target->state != TARGET_HALTED)
1138 LOG_ERROR("Target not halted");
1139 return ERROR_TARGET_NOT_HALTED;
1142 for (i = 0; i < bank->num_sectors; i++)
1145 bank->sectors[i].is_erased = 1;
1147 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
1151 chunk = buffer_size;
1152 if (chunk > (j - bank->sectors[i].size))
1154 chunk = (j - bank->sectors[i].size);
1157 retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
1158 if (retval != ERROR_OK)
1161 for (nBytes = 0; nBytes < chunk; nBytes++)
1163 if (buffer[nBytes] != 0xFF)
1165 bank->sectors[i].is_erased = 0;
1175 int default_flash_blank_check(struct flash_bank *bank)
1177 struct target *target = bank->target;
1183 if (bank->target->state != TARGET_HALTED)
1185 LOG_ERROR("Target not halted");
1186 return ERROR_TARGET_NOT_HALTED;
1189 for (i = 0; i < bank->num_sectors; i++)
1191 uint32_t address = bank->base + bank->sectors[i].offset;
1192 uint32_t size = bank->sectors[i].size;
1194 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
1200 bank->sectors[i].is_erased = 1;
1202 bank->sectors[i].is_erased = 0;
1208 LOG_USER("Running slow fallback erase check - add working memory");
1209 return default_flash_mem_blank_check(bank);
1215 int flash_init_drivers(struct command_context *cmd_ctx)
1217 register_jim(cmd_ctx, "ocd_flash_banks",
1218 jim_flash_banks, "return information about the flash banks");
1223 register_command(cmd_ctx, flash_cmd, "info",
1224 handle_flash_info_command, COMMAND_EXEC,
1225 "print info about flash bank <num>");
1226 register_command(cmd_ctx, flash_cmd, "probe",
1227 handle_flash_probe_command, COMMAND_EXEC,
1228 "identify flash bank <num>");
1229 register_command(cmd_ctx, flash_cmd, "erase_check",
1230 handle_flash_erase_check_command, COMMAND_EXEC,
1231 "check erase state of sectors in flash bank <num>");
1232 register_command(cmd_ctx, flash_cmd, "protect_check",
1233 handle_flash_protect_check_command, COMMAND_EXEC,
1234 "check protection state of sectors in flash bank <num>");
1235 register_command(cmd_ctx, flash_cmd, "erase_sector",
1236 handle_flash_erase_command, COMMAND_EXEC,
1237 "erase sectors at <bank> <first> <last>");
1238 register_command(cmd_ctx, flash_cmd, "erase_address",
1239 handle_flash_erase_address_command, COMMAND_EXEC,
1240 "erase address range <address> <length>");
1242 register_command(cmd_ctx, flash_cmd, "fillw",
1243 handle_flash_fill_command, COMMAND_EXEC,
1244 "fill with pattern (no autoerase) <address> <word_pattern> <count>");
1245 register_command(cmd_ctx, flash_cmd, "fillh",
1246 handle_flash_fill_command, COMMAND_EXEC,
1247 "fill with pattern <address> <halfword_pattern> <count>");
1248 register_command(cmd_ctx, flash_cmd, "fillb",
1249 handle_flash_fill_command, COMMAND_EXEC,
1250 "fill with pattern <address> <byte_pattern> <count>");
1252 register_command(cmd_ctx, flash_cmd, "write_bank",
1253 handle_flash_write_bank_command, COMMAND_EXEC,
1254 "write binary data to <bank> <file> <offset>");
1255 register_command(cmd_ctx, flash_cmd, "write_image",
1256 handle_flash_write_image_command, COMMAND_EXEC,
1257 "write_image [erase] [unlock] <file> [offset] [type]");
1258 register_command(cmd_ctx, flash_cmd, "protect",
1259 handle_flash_protect_command, COMMAND_EXEC,
1260 "set protection of sectors at <bank> <first> <last> <on | off>");
1265 int flash_register_commands(struct command_context *cmd_ctx)
1267 flash_cmd = register_command(cmd_ctx, NULL, "flash",
1268 NULL, COMMAND_ANY, NULL);
1270 register_command(cmd_ctx, flash_cmd, "bank",
1271 handle_flash_bank_command, COMMAND_CONFIG,
1272 "flash bank <driver> <base> <size> "
1273 "<chip_width> <bus_width> <target> [driver_options ...]");