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_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr);
58 extern flash_driver_t lpc2000_flash;
59 extern flash_driver_t cfi_flash;
60 extern flash_driver_t at91sam7_flash;
61 extern flash_driver_t str7x_flash;
62 extern flash_driver_t str9x_flash;
63 extern flash_driver_t stellaris_flash;
64 extern flash_driver_t str9xpec_flash;
65 extern flash_driver_t stm32x_flash;
66 extern flash_driver_t tms470_flash;
68 flash_driver_t *flash_drivers[] =
82 flash_bank_t *flash_banks;
83 static command_t *flash_cmd;
84 static int auto_erase = 0;
86 int flash_register_commands(struct command_context_s *cmd_ctx)
88 flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
90 register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, "flash_bank <driver> <base> <size> <chip_width> <bus_width> <target> [driver_options ...]");
91 register_command(cmd_ctx, flash_cmd, "auto_erase", handle_flash_auto_erase_command, COMMAND_ANY,
92 "auto erase flash sectors <on|off>");
96 int flash_init_drivers(struct command_context_s *cmd_ctx)
100 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
101 "list configured flash banks ");
102 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
103 "print info about flash bank <num>");
104 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
105 "identify flash bank <num>");
106 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
107 "check erase state of sectors in flash bank <num>");
108 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
109 "check protection state of sectors in flash bank <num>");
110 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
111 "DEPRECATED, use 'erase_sector' instead");
112 register_command(cmd_ctx, flash_cmd, "erase_sector", handle_flash_erase_command, COMMAND_EXEC,
113 "erase sectors at <bank> <first> <last>");
114 register_command(cmd_ctx, flash_cmd, "erase_address", handle_flash_erase_address_command, COMMAND_EXEC,
115 "erase address range <address> <length>");
116 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_binary_command, COMMAND_EXEC,
117 "DEPRECATED, use 'write_binary' instead");
118 register_command(cmd_ctx, flash_cmd, "write_binary", handle_flash_write_binary_command, COMMAND_EXEC,
119 "write binary <bank> <file> <offset>");
120 register_command(cmd_ctx, flash_cmd, "write_image", handle_flash_write_image_command, COMMAND_EXEC,
121 "write_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>");
129 flash_bank_t *get_flash_bank_by_num_noprobe(int num)
134 for (p = flash_banks; p; p = p->next)
145 flash_bank_t *get_flash_bank_by_num(int num)
147 flash_bank_t *p = get_flash_bank_by_num_noprobe(num);
153 retval = p->driver->auto_probe(p);
155 if (retval != ERROR_OK)
157 ERROR("auto_probe failed %d\n", retval);
163 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
171 return ERROR_COMMAND_SYNTAX_ERROR;
174 if ((target = get_target_by_num(strtoul(args[5], NULL, 0))) == NULL)
176 ERROR("target %lu not defined", strtoul(args[5], NULL, 0));
180 for (i = 0; flash_drivers[i]; i++)
182 if (strcmp(args[0], flash_drivers[i]->name) == 0)
186 /* register flash specific commands */
187 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
189 ERROR("couldn't register '%s' commands", args[0]);
193 c = malloc(sizeof(flash_bank_t));
195 c->driver = flash_drivers[i];
196 c->driver_priv = NULL;
197 c->base = strtoul(args[1], NULL, 0);
198 c->size = strtoul(args[2], NULL, 0);
199 c->chip_width = strtoul(args[3], NULL, 0);
200 c->bus_width = strtoul(args[4], NULL, 0);
205 if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
207 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
212 /* put flash bank in linked list */
215 /* find last flash bank */
216 for (p = flash_banks; p && p->next; p = p->next);
229 /* no matching flash driver found */
232 ERROR("flash driver '%s' not found", args[0]);
239 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
246 command_print(cmd_ctx, "no flash banks configured");
250 for (p = flash_banks; p; p = p->next)
252 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
253 i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
259 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
267 command_print(cmd_ctx, "usage: flash info <num>");
271 for (p = flash_banks; p; p = p->next, i++)
273 if (i == strtoul(args[0], NULL, 0))
277 /* attempt auto probe */
278 p->driver->auto_probe(p);
280 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
281 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
282 for (j = 0; j < p->num_sectors; j++)
284 char *erase_state, *protect_state;
286 if (p->sectors[j].is_erased == 0)
287 erase_state = "not erased";
288 else if (p->sectors[j].is_erased == 1)
289 erase_state = "erased";
291 erase_state = "erase state unknown";
293 if (p->sectors[j].is_protected == 0)
294 protect_state = "not protected";
295 else if (p->sectors[j].is_protected == 1)
296 protect_state = "protected";
298 protect_state = "protection state unknown";
300 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%x %ikB) %s, %s",
301 j, p->sectors[j].offset, p->sectors[j].size, p->sectors[j].size>>10,
302 erase_state, protect_state);
305 p->driver->info(p, buf, 1024);
306 command_print(cmd_ctx, "%s", buf);
313 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
320 command_print(cmd_ctx, "usage: flash probe <num>");
324 p = get_flash_bank_by_num_noprobe(strtoul(args[0], NULL, 0));
327 if ((retval = p->driver->probe(p)) == ERROR_OK)
329 command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
331 else if (retval == ERROR_FLASH_BANK_INVALID)
333 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
338 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
344 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
350 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
357 command_print(cmd_ctx, "usage: flash erase_check <num>");
361 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
364 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
366 command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
370 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
376 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
382 int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
391 target_t *target = get_current_target(cmd_ctx);
395 command_print(cmd_ctx, "usage: flash erase_address <address> <length>");
399 address = strtoul(args[0], NULL, 0);
400 length = strtoul(args[1], NULL, 0);
403 command_print(cmd_ctx, "Length must be >0");
404 return ERROR_INVALID_ARGUMENTS;
407 p = get_flash_bank_by_addr(target, address);
410 command_print(cmd_ctx, "No flash at that address");
411 return ERROR_INVALID_ARGUMENTS;
414 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
417 duration_start_measure(&duration);
419 if ((retval = flash_erase_address_range(target, address, length)) != ERROR_OK)
423 case ERROR_TARGET_NOT_HALTED:
424 command_print(cmd_ctx, "can't work with this flash while target is running");
426 case ERROR_INVALID_ARGUMENTS:
427 command_print(cmd_ctx, "usage: flash erase_address <address> <length>");
429 case ERROR_FLASH_BANK_INVALID:
430 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
432 case ERROR_FLASH_OPERATION_FAILED:
433 command_print(cmd_ctx, "flash erase error");
435 case ERROR_FLASH_SECTOR_INVALID:
436 command_print(cmd_ctx, "sector number(s) invalid");
439 command_print(cmd_ctx, "unknown error");
444 duration_stop_measure(&duration, &duration_text);
445 command_print(cmd_ctx, "erased address 0x%8.8x length %i in %s", address, length, duration_text);
452 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
459 command_print(cmd_ctx, "usage: flash protect_check <num>");
463 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
466 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
468 command_print(cmd_ctx, "successfully checked protect state");
470 else if (retval == ERROR_FLASH_OPERATION_FAILED)
472 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
476 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
481 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
487 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
491 int first = strtoul(args[1], NULL, 0);
492 int last = strtoul(args[2], NULL, 0);
494 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
498 duration_start_measure(&duration);
502 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
506 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
510 case ERROR_TARGET_NOT_HALTED:
511 command_print(cmd_ctx, "can't work with this flash while target is running");
513 case ERROR_INVALID_ARGUMENTS:
514 command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
516 case ERROR_FLASH_BANK_INVALID:
517 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
519 case ERROR_FLASH_OPERATION_FAILED:
520 command_print(cmd_ctx, "flash erase error");
522 case ERROR_FLASH_SECTOR_INVALID:
523 command_print(cmd_ctx, "sector number(s) invalid");
526 command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
529 command_print(cmd_ctx, "unknown error");
534 duration_stop_measure(&duration, &duration_text);
536 command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
542 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
548 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
552 int first = strtoul(args[1], NULL, 0);
553 int last = strtoul(args[2], NULL, 0);
556 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
559 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
563 if (strcmp(args[3], "on") == 0)
565 else if (strcmp(args[3], "off") == 0)
569 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
573 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
577 case ERROR_TARGET_NOT_HALTED:
578 command_print(cmd_ctx, "can't work with this flash while target is running");
580 case ERROR_INVALID_ARGUMENTS:
581 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
583 case ERROR_FLASH_BANK_INVALID:
584 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
586 case ERROR_FLASH_OPERATION_FAILED:
587 command_print(cmd_ctx, "flash program error");
589 case ERROR_FLASH_SECTOR_INVALID:
590 command_print(cmd_ctx, "sector number(s) invalid");
593 command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
596 command_print(cmd_ctx, "unknown error");
601 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));
606 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
612 int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
614 target_t *target = get_current_target(cmd_ctx);
630 command_print(cmd_ctx, "usage: flash %s <file> [offset] [type]", cmd);
631 return ERROR_INVALID_ARGUMENTS;
636 ERROR("no target selected");
640 duration_start_measure(&duration);
644 image.base_address_set = 1;
645 image.base_address = strtoul(args[1], NULL, 0);
649 image.base_address_set = 0;
650 image.base_address = 0x0;
653 image.start_address_set = 0;
655 retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL);
656 if (retval != ERROR_OK)
658 command_print(cmd_ctx, "image_open error: %s", image.error_str);
662 failed = malloc(sizeof(int) * image.num_sections);
666 retval = flash_write(target, &image, &written, &error_str, failed, auto_erase);
668 if (retval != ERROR_OK)
672 command_print(cmd_ctx, "failed writing image %s: %s", args[0], error_str);
680 for (i = 0; i < image.num_sections; i++)
684 command_print(cmd_ctx, "didn't write section at 0x%8.8x, size 0x%8.8x",
685 image.sections[i].base_address, image.sections[i].size);
689 duration_stop_measure(&duration, &duration_text);
690 command_print(cmd_ctx, "wrote %u byte from file %s in %s (%f kb/s)",
691 written, args[0], duration_text,
692 (float)written / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
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));
751 case ERROR_TARGET_NOT_HALTED:
752 command_print(cmd_ctx, "can't work with this flash while target is running");
754 case ERROR_INVALID_ARGUMENTS:
755 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
757 case ERROR_FLASH_BANK_INVALID:
758 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
760 case ERROR_FLASH_OPERATION_FAILED:
761 command_print(cmd_ctx, "flash program error");
763 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
764 command_print(cmd_ctx, "offset breaks required alignment");
766 case ERROR_FLASH_DST_OUT_OF_BANK:
767 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
769 case ERROR_FLASH_SECTOR_NOT_ERASED:
770 command_print(cmd_ctx, "destination sector(s) not erased");
773 command_print(cmd_ctx, "unknown error");
779 duration_stop_measure(&duration, &duration_text);
780 command_print(cmd_ctx, "wrote %"PRIi64" byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
781 fileio.size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
782 (float)fileio.size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
785 fileio_close(&fileio);
790 void flash_set_dirty(void)
795 /* set all flash to require erasing */
796 for (c = flash_banks; c; c = c->next)
798 for (i = 0; i < c->num_sectors; i++)
800 c->sectors[i].is_erased = 0;
805 /* lookup flash bank by address */
806 flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr)
810 /* cycle through bank list */
811 for (c = flash_banks; c; c = c->next)
814 retval = c->driver->auto_probe(c);
816 if (retval != ERROR_OK)
818 ERROR("auto_probe failed %d\n", retval);
821 /* check whether address belongs to this flash bank */
822 if ((addr >= c->base) && (addr < c->base + c->size) && target == c->target)
829 /* erase given flash region, selects proper bank according to target and address */
830 int flash_erase_address_range(target_t *target, u32 addr, u32 length)
837 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
838 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
840 if (c->size == 0 || c->num_sectors == 0)
841 return ERROR_FLASH_BANK_INVALID;
845 /* special case, erase whole bank when length is zero */
847 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
849 return c->driver->erase(c, 0, c->num_sectors - 1);
852 /* check whether it fits */
853 if (addr + length > c->base + c->size)
854 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
858 for (i = 0; i < c->num_sectors; i++)
860 /* check whether sector overlaps with the given range and is not yet erased */
861 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
862 /* if first is not set yet then this is the first sector */
865 last = i; /* and it is the last one so far in any case */
869 if( first == -1 || last == -1 )
872 return c->driver->erase(c, first, last);
875 /* write (optional verify) an image to flash memory of the given target */
876 int flash_write(target_t *target, image_t *image, u32 *written, char **error_str, int *failed, int erase)
892 for (i = 0; i < image->num_sections; i++)
897 /* assume all sectors need erasing - stops any problems
898 * when flash_write is called multiple times */
903 /* loop until we reach end of the image */
904 while (section < image->num_sections)
910 u32 run_address = image->sections[section].base_address + section_offset;
911 u32 run_size = image->sections[section].size - section_offset;
913 if (image->sections[section].size == 0)
915 WARNING("empty section %d", section);
921 /* find the corresponding flash bank */
922 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
926 if (error_str == NULL)
927 return ERROR_FLASH_DST_OUT_OF_BANK; /* abort operation */
928 *error_str = malloc(FLASH_MAX_ERROR_STR);
929 snprintf(*error_str, FLASH_MAX_ERROR_STR, "no flash mapped at requested address");
930 return ERROR_FLASH_DST_OUT_OF_BANK; /* abort operation */
932 failed[section] = ERROR_FLASH_DST_OUT_OF_BANK; /* mark the section as failed */
933 section++; /* and skip it */
938 /* collect consecutive sections which fall into the same bank */
939 section_first = section;
940 section_last = section;
941 while ((run_address + run_size < c->base + c->size)
942 && (section_last + 1 < image->num_sections))
944 if (image->sections[section_last + 1].base_address < (run_address + run_size))
946 DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
949 if (image->sections[section_last + 1].base_address != (run_address + run_size))
951 run_size += image->sections[++section_last].size;
954 /* fit the run into bank constraints */
955 if (run_address + run_size > c->base + c->size)
956 run_size = c->base + c->size - run_address;
958 /* allocate buffer */
959 buffer = malloc(run_size);
962 /* read sections to the buffer */
963 while (buffer_size < run_size)
967 if (buffer_size - run_size <= image->sections[section].size - section_offset)
968 size_read = buffer_size - run_size;
970 size_read = image->sections[section].size - section_offset;
972 if ((retval = image_read_section(image, section, section_offset,
973 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
977 if (error_str == NULL)
978 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
980 *error_str = malloc(FLASH_MAX_ERROR_STR);
982 /* if image_read_section returned an error there's an error string we can pass on */
983 if (retval != ERROR_OK)
984 snprintf(*error_str, FLASH_MAX_ERROR_STR, "error reading from image: %s", image->error_str);
986 snprintf(*error_str, FLASH_MAX_ERROR_STR, "error reading from image");
988 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
991 buffer_size += size_read;
992 section_offset += size_read;
994 if (section_offset >= image->sections[section].size)
1005 /* calculate and erase sectors */
1006 retval = flash_erase_address_range( target, run_address, run_size );
1009 if (retval == ERROR_OK)
1011 /* write flash sectors */
1012 retval = c->driver->write(c, buffer, run_address - c->base, run_size);
1017 if (retval != ERROR_OK)
1019 if (error_str == NULL)
1020 return retval; /* abort operation */
1022 *error_str = malloc(FLASH_MAX_ERROR_STR);
1025 case ERROR_TARGET_NOT_HALTED:
1026 snprintf(*error_str, FLASH_MAX_ERROR_STR, "can't flash image while target is running");
1028 case ERROR_INVALID_ARGUMENTS:
1029 snprintf(*error_str, FLASH_MAX_ERROR_STR, "flash driver can't fulfill request");
1031 case ERROR_FLASH_OPERATION_FAILED:
1032 snprintf(*error_str, FLASH_MAX_ERROR_STR, "flash program error");
1034 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
1035 snprintf(*error_str, FLASH_MAX_ERROR_STR, "offset breaks required alignment");
1037 case ERROR_FLASH_DST_OUT_OF_BANK:
1038 snprintf(*error_str, FLASH_MAX_ERROR_STR, "no flash mapped at requested address");
1040 case ERROR_FLASH_SECTOR_NOT_ERASED:
1041 snprintf(*error_str, FLASH_MAX_ERROR_STR, "destination sector(s) not erased");
1044 snprintf(*error_str, FLASH_MAX_ERROR_STR, "unknown error: %i", retval);
1047 return retval; /* abort operation */
1050 if (written != NULL)
1051 *written += run_size; /* add run size to total written counter */
1057 int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1061 command_print(cmd_ctx, "usage: flash auto_erase <on|off>");
1065 if (strcmp(args[0], "on") == 0)
1067 else if (strcmp(args[0], "off") == 0)