1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
27 #include "time_support.h"
35 #include <sys/types.h>
40 /* command handlers */
41 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
42 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
43 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
44 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
45 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50 int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 int handle_flash_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
55 flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr);
59 extern flash_driver_t lpc2000_flash;
60 extern flash_driver_t cfi_flash;
61 extern flash_driver_t at91sam7_flash;
62 extern flash_driver_t str7x_flash;
63 extern flash_driver_t str9x_flash;
64 extern flash_driver_t stellaris_flash;
65 extern flash_driver_t str9xpec_flash;
66 extern flash_driver_t stm32x_flash;
68 flash_driver_t *flash_drivers[] =
81 flash_bank_t *flash_banks;
82 static command_t *flash_cmd;
83 static int auto_erase = 0;
85 int flash_register_commands(struct command_context_s *cmd_ctx)
87 flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
89 register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
94 int flash_init(struct command_context_s *cmd_ctx)
98 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
99 "list configured flash banks ");
100 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
101 "print info about flash bank <num>");
102 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
103 "identify flash bank <num>");
104 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
105 "check erase state of sectors in flash bank <num>");
106 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
107 "check protection state of sectors in flash bank <num>");
108 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
109 "DEPRECATED, use 'erase_sector' instead");
110 register_command(cmd_ctx, flash_cmd, "erase_sector", handle_flash_erase_command, COMMAND_EXEC,
111 "erase sectors at <bank> <first> <last>");
112 register_command(cmd_ctx, flash_cmd, "erase_address", handle_flash_erase_address_command, COMMAND_EXEC,
113 "erase address range <address> <length>");
114 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_binary_command, COMMAND_EXEC,
115 "DEPRECATED, use 'write_binary' instead");
116 register_command(cmd_ctx, flash_cmd, "write_binary", handle_flash_write_binary_command, COMMAND_EXEC,
117 "write binary <bank> <file> <offset>");
118 register_command(cmd_ctx, flash_cmd, "write_image", handle_flash_write_image_command, COMMAND_EXEC,
119 "write_image <file> [offset] [type]");
120 register_command(cmd_ctx, flash_cmd, "verify_image", handle_flash_verify_image_command, COMMAND_EXEC,
121 "verify_image <file> [offset] [type]");
122 register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
123 "set protection of sectors at <bank> <first> <last> <on|off>");
124 register_command(cmd_ctx, flash_cmd, "auto_erase", handle_flash_auto_erase_command, COMMAND_EXEC,
125 "auto erase flash sectors <on|off>");
131 flash_bank_t *get_flash_bank_by_num(int num)
136 for (p = flash_banks; p; p = p->next)
147 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> <target> [driver_options ...]
149 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
157 WARNING("incomplete flash_bank configuration");
158 WARNING("flash_bank <driver> <base> <size> <chip_width> <bus_width> <target> [driver_options ...]");
162 if ((target = get_target_by_num(strtoul(args[5], NULL, 0))) == NULL)
164 ERROR("target %lu not defined", strtoul(args[5], NULL, 0));
168 for (i = 0; flash_drivers[i]; i++)
170 if (strcmp(args[0], flash_drivers[i]->name) == 0)
174 /* register flash specific commands */
175 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
177 ERROR("couldn't register '%s' commands", args[0]);
181 c = malloc(sizeof(flash_bank_t));
183 c->driver = flash_drivers[i];
184 c->driver_priv = NULL;
185 c->base = strtoul(args[1], NULL, 0);
186 c->size = strtoul(args[2], NULL, 0);
187 c->chip_width = strtoul(args[3], NULL, 0);
188 c->bus_width = strtoul(args[4], NULL, 0);
193 if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
195 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
200 /* put flash bank in linked list */
203 /* find last flash bank */
204 for (p = flash_banks; p && p->next; p = p->next);
217 /* no matching flash driver found */
220 ERROR("flash driver '%s' not found", args[0]);
227 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
234 command_print(cmd_ctx, "no flash banks configured");
238 for (p = flash_banks; p; p = p->next)
240 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
241 i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
247 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
255 command_print(cmd_ctx, "usage: flash info <num>");
259 for (p = flash_banks; p; p = p->next, i++)
261 if (i == strtoul(args[0], NULL, 0))
265 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
266 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
267 for (j = 0; j < p->num_sectors; j++)
269 char *erase_state, *protect_state;
271 if (p->sectors[j].is_erased == 0)
272 erase_state = "not erased";
273 else if (p->sectors[j].is_erased == 1)
274 erase_state = "erased";
276 erase_state = "erase state unknown";
278 if (p->sectors[j].is_protected == 0)
279 protect_state = "not protected";
280 else if (p->sectors[j].is_protected == 1)
281 protect_state = "protected";
283 protect_state = "protection state unknown";
285 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%x %ikB) %s, %s",
286 j, p->sectors[j].offset, p->sectors[j].size, p->sectors[j].size>>10,
287 erase_state, protect_state);
290 p->driver->info(p, buf, 1024);
291 command_print(cmd_ctx, "%s", buf);
298 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
305 command_print(cmd_ctx, "usage: flash probe <num>");
309 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
312 if ((retval = p->driver->probe(p)) == ERROR_OK)
314 command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
316 else if (retval == ERROR_FLASH_BANK_INVALID)
318 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
323 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
329 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
335 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
342 command_print(cmd_ctx, "usage: flash erase_check <num>");
346 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
349 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
351 command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
355 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
361 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
367 static void printError(struct command_context_s *cmd_ctx, flash_bank_t *p, int retval)
369 if (retval==ERROR_OK)
373 case ERROR_TARGET_NOT_HALTED:
374 command_print(cmd_ctx, "can't work with this flash while target is running");
376 case ERROR_INVALID_ARGUMENTS:
377 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
379 case ERROR_FLASH_BANK_INVALID:
380 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
382 case ERROR_FLASH_OPERATION_FAILED:
383 command_print(cmd_ctx, "flash program error");
385 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
386 command_print(cmd_ctx, "offset breaks required alignment");
388 case ERROR_FLASH_DST_OUT_OF_BANK:
389 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
391 case ERROR_FLASH_SECTOR_NOT_ERASED:
392 command_print(cmd_ctx, "destination sector(s) not erased");
395 command_print(cmd_ctx, "unknown error");
400 int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
404 target_t *target = get_current_target(cmd_ctx);
408 command_print(cmd_ctx, "usage: flash erase_address <address> <range>");
412 int address=strtoul(args[0], NULL, 0);
413 int length=strtoul(args[1], NULL, 0);
416 command_print(cmd_ctx, "Length must be >0");
417 return ERROR_INVALID_ARGUMENTS;
420 p = get_flash_bank_by_addr(target, address);
423 command_print(cmd_ctx, "No flash at that address");
424 return ERROR_INVALID_ARGUMENTS;
426 int retval=flash_erase(target, address, length);
427 printError(cmd_ctx, p, retval);
432 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
439 command_print(cmd_ctx, "usage: flash protect_check <num>");
443 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
446 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
448 command_print(cmd_ctx, "successfully checked protect state");
450 else if (retval == ERROR_FLASH_OPERATION_FAILED)
452 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
456 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
461 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
467 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
471 int first = strtoul(args[1], NULL, 0);
472 int last = strtoul(args[2], NULL, 0);
474 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
478 duration_start_measure(&duration);
482 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
486 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
490 case ERROR_TARGET_NOT_HALTED:
491 command_print(cmd_ctx, "can't work with this flash while target is running");
493 case ERROR_INVALID_ARGUMENTS:
494 command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
496 case ERROR_FLASH_BANK_INVALID:
497 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
499 case ERROR_FLASH_OPERATION_FAILED:
500 command_print(cmd_ctx, "flash erase error");
502 case ERROR_FLASH_SECTOR_INVALID:
503 command_print(cmd_ctx, "sector number(s) invalid");
506 command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
509 command_print(cmd_ctx, "unknown error");
514 duration_stop_measure(&duration, &duration_text);
516 command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
522 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
528 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
532 int first = strtoul(args[1], NULL, 0);
533 int last = strtoul(args[2], NULL, 0);
536 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
539 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
543 if (strcmp(args[3], "on") == 0)
545 else if (strcmp(args[3], "off") == 0)
549 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
553 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
557 case ERROR_TARGET_NOT_HALTED:
558 command_print(cmd_ctx, "can't work with this flash while target is running");
560 case ERROR_INVALID_ARGUMENTS:
561 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
563 case ERROR_FLASH_BANK_INVALID:
564 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
566 case ERROR_FLASH_OPERATION_FAILED:
567 command_print(cmd_ctx, "flash program error");
569 case ERROR_FLASH_SECTOR_INVALID:
570 command_print(cmd_ctx, "sector number(s) invalid");
573 command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
576 command_print(cmd_ctx, "unknown error");
581 command_print(cmd_ctx, "%s protection for sectors %i through %i on flash bank %i", (set) ? "set" : "cleared", first, last, strtoul(args[0], 0, 0));
586 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
592 int handle_flash_write_image_command_core(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, enum flash_image_op op)
594 target_t *target = get_current_target(cmd_ctx);
610 command_print(cmd_ctx, "usage: flash %s <file> [offset] [type]", cmd);
611 return ERROR_INVALID_ARGUMENTS;
616 ERROR("no target selected");
620 duration_start_measure(&duration);
624 image.base_address_set = 1;
625 image.base_address = strtoul(args[1], NULL, 0);
629 image.base_address_set = 0;
630 image.base_address = 0x0;
633 image.start_address_set = 0;
635 retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL);
636 if (retval != ERROR_OK)
638 command_print(cmd_ctx, "image_open error: %s", image.error_str);
642 failed = malloc(sizeof(int) * image.num_sections);
647 if ((op==flash_image_op_write)&&auto_erase)
649 retval = flash_image_operation(target, &image, &written, &error_str, failed, flash_image_op_erase);
651 if (retval == ERROR_OK)
653 retval = flash_image_operation(target, &image, &written, &error_str, failed, op);
656 if (retval != ERROR_OK)
660 command_print(cmd_ctx, "failed writing image %s: %s", args[0], error_str);
668 for (i = 0; i < image.num_sections; i++)
672 command_print(cmd_ctx, "didn't write section at 0x%8.8x, size 0x%8.8x",
673 image.sections[i].base_address, image.sections[i].size);
677 duration_stop_measure(&duration, &duration_text);
678 command_print(cmd_ctx, "%s %u byte from file %s in %s (%f kb/s)",
679 (op==flash_image_op_write)?"wrote":"verified",
680 written, args[0], duration_text,
681 (float)written / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
692 int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
694 return handle_flash_write_image_command_core(cmd_ctx, cmd, args, argc, flash_image_op_write);
696 int handle_flash_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
698 return handle_flash_write_image_command_core(cmd_ctx, cmd, args, argc, flash_image_op_verify);
701 int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
717 command_print(cmd_ctx, "usage: flash write_binary <bank> <file> <offset>");
721 duration_start_measure(&duration);
723 offset = strtoul(args[2], NULL, 0);
724 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
727 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
731 if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
733 command_print(cmd_ctx, "flash write_binary error: %s", fileio.error_str);
737 buffer = malloc(fileio.size);
738 if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
740 command_print(cmd_ctx, "flash write_binary error: %s", fileio.error_str);
744 if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
746 command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
747 args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
748 printError(cmd_ctx, p, retval);
753 duration_stop_measure(&duration, &duration_text);
754 command_print(cmd_ctx, "wrote %"PRIi64" byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
755 fileio.size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
756 (float)fileio.size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
759 fileio_close(&fileio);
764 /* lookup flash bank by address */
765 flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr)
769 /* cycle through bank list */
770 for (c = flash_banks; c; c = c->next)
772 /* check whether address belongs to this flash bank */
773 if ((addr >= c->base) && (addr < c->base + c->size) && target == c->target)
780 /* erase given flash region, selects proper bank according to target and address */
781 int flash_erase(target_t *target, u32 addr, u32 length)
788 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
789 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
791 if (c->size == 0 || c->num_sectors == 0)
792 return ERROR_FLASH_BANK_INVALID;
796 /* special case, erase whole bank when length is zero */
798 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
800 return c->driver->erase(c, 0, c->num_sectors - 1);
803 /* check whether it fits */
804 if (addr + length > c->base + c->size)
805 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
809 for (i = 0; i < c->num_sectors; i++)
811 /* check whether sector overlaps with the given range and is not yet erased */
812 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
813 /* if first is not set yet then this is the first sector */
816 last = i; /* and it is the last one so far in any case */
820 if( first == -1 || last == -1 )
823 return c->driver->erase(c, first, last);
826 /* perform an operation on flash using an image: verify, erase or write. */
827 int flash_image_operation(target_t *target, image_t *image, u32 *written, char **error_str, int *failed, enum flash_image_op op)
842 for (i = 0; i < image->num_sections; i++)
845 /* loop until we reach end of the image */
846 while (section < image->num_sections)
853 u32 run_address = image->sections[section].base_address+section_offset;
854 u32 run_size = image->sections[section].size-section_offset;
856 if (image->sections[section].size == 0)
858 WARNING("empty section %d", section);
864 /* find the corresponding flash bank */
865 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
869 if (error_str == NULL)
870 return ERROR_FLASH_DST_OUT_OF_BANK; /* abort operation */
871 *error_str = malloc(FLASH_MAX_ERROR_STR);
872 snprintf(*error_str, FLASH_MAX_ERROR_STR, "no flash mapped at requested address");
873 return ERROR_FLASH_DST_OUT_OF_BANK; /* abort operation */
875 failed[section] = ERROR_FLASH_DST_OUT_OF_BANK; /* mark the section as failed */
876 section++; /* and skip it */
881 /* collect consecutive sections which fall into the same bank */
882 section_first = section;
883 section_last = section;
884 while ((run_address + run_size < c->base + c->size)
885 && (section_last + 1 < image->num_sections))
887 if (image->sections[section_last + 1].base_address < (run_address + run_size))
889 DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
892 if (image->sections[section_last + 1].base_address != (run_address + run_size))
894 run_size += image->sections[++section_last].size;
897 /* fit the run into bank constraints */
898 if (run_address + run_size > c->base + c->size)
899 run_size = c->base + c->size - run_address;
901 /* allocate buffer */
902 buffer = malloc(run_size);
905 /* read sections to the buffer */
906 while (buffer_size < run_size)
910 if (buffer_size - run_size <= image->sections[section].size - section_offset)
911 size_read = buffer_size - run_size;
913 size_read = image->sections[section].size - section_offset;
915 if ((retval = image_read_section(image, section, section_offset,
916 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
920 if (error_str == NULL)
921 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
923 *error_str = malloc(FLASH_MAX_ERROR_STR);
925 /* if image_read_section returned an error there's an error string we can pass on */
926 if (retval != ERROR_OK)
927 snprintf(*error_str, FLASH_MAX_ERROR_STR, "error reading from image: %s", image->error_str);
929 snprintf(*error_str, FLASH_MAX_ERROR_STR, "error reading from image");
931 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
934 buffer_size += size_read;
935 section_offset += size_read;
937 if (section_offset >= image->sections[section].size)
948 case flash_image_op_erase:
949 /* calculate and erase sectors */
950 retval = flash_erase( target, run_address, run_size );
953 case flash_image_op_write:
954 /* write flash sectors */
955 retval = c->driver->write(c, buffer, run_address - c->base, run_size);
958 case flash_image_op_verify:
962 data=(u8 *)malloc(run_size);
964 retval = ERROR_INVALID_ARGUMENTS; // exception handling would be nice...
966 // Can we use long word accesses?
974 retval = target->type->read_memory(target, run_address, size, count, data);
976 if (retval == ERROR_OK)
979 for (i=0; i<run_size; i++)
981 if (data[i]!=buffer[i])
983 ERROR("Verify operation failed address 0x%08x. Was %02x instead of %02x\n", i+run_address, data[i], buffer[i]);
984 retval=ERROR_FLASH_OPERATION_FAILED;
1001 if (retval != ERROR_OK)
1003 if (error_str == NULL)
1004 return retval; /* abort operation */
1006 *error_str = malloc(FLASH_MAX_ERROR_STR);
1009 case ERROR_TARGET_NOT_HALTED:
1010 snprintf(*error_str, FLASH_MAX_ERROR_STR, "can't flash image while target is running");
1012 case ERROR_INVALID_ARGUMENTS:
1013 snprintf(*error_str, FLASH_MAX_ERROR_STR, "flash driver can't fulfill request");
1015 case ERROR_FLASH_OPERATION_FAILED:
1016 snprintf(*error_str, FLASH_MAX_ERROR_STR, "flash program error");
1018 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
1019 snprintf(*error_str, FLASH_MAX_ERROR_STR, "offset breaks required alignment");
1021 case ERROR_FLASH_DST_OUT_OF_BANK:
1022 snprintf(*error_str, FLASH_MAX_ERROR_STR, "no flash mapped at requested address");
1024 case ERROR_FLASH_SECTOR_NOT_ERASED:
1025 snprintf(*error_str, FLASH_MAX_ERROR_STR, "destination sector(s) not erased");
1028 snprintf(*error_str, FLASH_MAX_ERROR_STR, "unknown error: %i", retval);
1031 return retval; /* abort operation */
1034 if (written != NULL)
1035 *written += run_size; /* add run size to total written counter */
1041 int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1045 command_print(cmd_ctx, "usage: flash auto_erase <on|off>");
1049 if (strcmp(args[0], "on") == 0)
1051 else if (strcmp(args[0], "off") == 0)