struct fileio: improve member types
[openocd/cmsis-dap.git] / src / flash / flash.c
blob03d4547d407df8307e6585479e991108da2ad1c1
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
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. *
15 * *
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. *
20 * *
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 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include "flash.h"
31 #include "image.h"
32 #include "time_support.h"
34 static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock);
36 /* flash drivers
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[] = {
58 &lpc2000_flash,
59 &lpc288x_flash,
60 &lpc2900_flash,
61 &cfi_flash,
62 &at91sam7_flash,
63 &at91sam3_flash,
64 &str7x_flash,
65 &str9x_flash,
66 &aduc702x_flash,
67 &stellaris_flash,
68 &str9xpec_flash,
69 &stm32x_flash,
70 &tms470_flash,
71 &ecosflash_flash,
72 &ocl_flash,
73 &pic32mx_flash,
74 &avr_flash,
75 &faux_flash,
76 NULL,
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)
85 int retval;
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);
94 return retval;
97 static int flash_driver_erase(struct flash_bank *bank, int first, int last)
99 int retval;
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);
107 return retval;
110 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
112 int retval;
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);
120 return retval;
123 static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
125 struct flash_bank *p;
127 if (argc != 1) {
128 Jim_WrongNumArgs(interp, 1, argv, "no arguments to flash_banks command");
129 return JIM_ERR;
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);
153 return JIM_OK;
156 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
158 struct flash_bank *p;
159 int i = 0;
161 for (p = flash_banks; p; p = p->next)
163 if (i++ == num)
165 return p;
168 LOG_ERROR("flash bank %d does not exist", num);
169 return NULL;
172 int flash_get_bank_count(void)
174 struct flash_bank *p;
175 int i = 0;
176 for (p = flash_banks; p; p = p->next)
178 i++;
180 return i;
183 struct flash_bank *get_flash_bank_by_num(int num)
185 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
186 int retval;
188 if (p == NULL)
189 return NULL;
191 retval = p->driver->auto_probe(p);
193 if (retval != ERROR_OK)
195 LOG_ERROR("auto_probe failed %d\n", retval);
196 return NULL;
198 return p;
201 COMMAND_HELPER(flash_command_get_bank_by_num,
202 unsigned name_index, struct flash_bank **bank)
204 unsigned bank_num;
205 COMMAND_PARSE_NUMBER(uint, args[name_index], bank_num);
207 *bank = get_flash_bank_by_num(bank_num);
208 if (!*bank)
210 command_print(cmd_ctx,
211 "flash bank '#%u' not found", bank_num);
212 return ERROR_INVALID_ARGUMENTS;
214 return ERROR_OK;
218 COMMAND_HANDLER(handle_flash_bank_command)
220 int retval;
221 int i;
222 int found = 0;
223 struct target *target;
225 if (argc < 6)
227 return ERROR_COMMAND_SYNTAX_ERROR;
230 if ((target = get_target(args[5])) == NULL)
232 LOG_ERROR("target '%s' not defined", args[5]);
233 return ERROR_FAIL;
236 for (i = 0; flash_drivers[i]; i++)
238 if (strcmp(args[0], flash_drivers[i]->name) != 0)
239 continue;
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", args[0]);
247 return ERROR_FAIL;
250 c = malloc(sizeof(struct flash_bank));
251 c->target = target;
252 c->driver = flash_drivers[i];
253 c->driver_priv = NULL;
254 COMMAND_PARSE_NUMBER(u32, args[1], c->base);
255 COMMAND_PARSE_NUMBER(u32, args[2], c->size);
256 COMMAND_PARSE_NUMBER(int, args[3], c->chip_width);
257 COMMAND_PARSE_NUMBER(int, args[4], c->bus_width);
258 c->num_sectors = 0;
259 c->sectors = NULL;
260 c->next = NULL;
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 , args[0], c->base);
266 free(c);
267 return retval;
270 /* put flash bank in linked list */
271 if (flash_banks)
273 int bank_num = 0;
274 /* find last flash bank */
275 for (p = flash_banks; p && p->next; p = p->next) bank_num++;
276 if (p)
277 p->next = c;
278 c->bank_number = bank_num + 1;
280 else
282 flash_banks = c;
283 c->bank_number = 0;
286 found = 1;
289 /* no matching flash driver found */
290 if (!found)
292 LOG_ERROR("flash driver '%s' not found", args[0]);
293 return ERROR_FAIL;
296 return ERROR_OK;
299 COMMAND_HANDLER(handle_flash_info_command)
301 struct flash_bank *p;
302 uint32_t i = 0;
303 int j = 0;
304 int retval;
306 if (argc != 1)
307 return ERROR_COMMAND_SYNTAX_ERROR;
309 unsigned bank_nr;
310 COMMAND_PARSE_NUMBER(uint, args[0], bank_nr);
312 for (p = flash_banks; p; p = p->next, i++)
314 if (i != bank_nr)
315 continue;
317 char buf[1024];
319 /* attempt auto probe */
320 if ((retval = p->driver->auto_probe(p)) != ERROR_OK)
321 return retval;
323 command_print(cmd_ctx,
324 "#%" PRIi32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", buswidth %i, chipwidth %i",
326 p->driver->name,
327 p->base,
328 p->size,
329 p->bus_width,
330 p->chip_width);
331 for (j = 0; j < p->num_sectors; j++)
333 char *protect_state;
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";
339 else
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,
346 p->sectors[j].size,
347 p->sectors[j].size >> 10,
348 protect_state);
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);
358 return ERROR_OK;
361 COMMAND_HANDLER(handle_flash_probe_command)
363 int retval;
365 if (argc != 1)
367 return ERROR_COMMAND_SYNTAX_ERROR;
370 unsigned bank_nr;
371 COMMAND_PARSE_NUMBER(uint, args[0], bank_nr);
372 struct flash_bank *p = get_flash_bank_by_num_noprobe(bank_nr);
373 if (p)
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 args[0], p->base);
384 else
386 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32,
387 args[0], p->base);
390 else
392 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
395 return ERROR_OK;
398 COMMAND_HANDLER(handle_flash_erase_check_command)
400 if (argc != 1)
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)
408 return retval;
410 int j;
411 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
413 command_print(cmd_ctx, "successfully checked erase state");
415 else
417 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
418 args[0], p->base);
421 for (j = 0; j < p->num_sectors; j++)
423 char *erase_state;
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";
429 else
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,
436 p->sectors[j].size,
437 p->sectors[j].size >> 10,
438 erase_state);
441 return ERROR_OK;
444 COMMAND_HANDLER(handle_flash_erase_address_command)
446 struct flash_bank *p;
447 int retval;
448 int address;
449 int length;
451 struct target *target = get_current_target(cmd_ctx);
453 if (argc != 2)
454 return ERROR_COMMAND_SYNTAX_ERROR;
456 COMMAND_PARSE_NUMBER(int, args[0], address);
457 COMMAND_PARSE_NUMBER(int, args[1], length);
458 if (length <= 0)
460 command_print(cmd_ctx, "Length must be >0");
461 return ERROR_COMMAND_SYNTAX_ERROR;
464 p = get_flash_bank_by_addr(target, address);
465 if (p == NULL)
467 return ERROR_FAIL;
470 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
471 flash_set_dirty();
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));
485 return retval;
488 COMMAND_HANDLER(handle_flash_protect_check_command)
490 if (argc != 1)
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)
496 return 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, args[0], p->base);
506 else
508 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32, args[0], p->base);
511 return ERROR_OK;
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");
520 return ERROR_FAIL;
523 if (!(last <= (num_sectors - 1))) {
524 command_print(cmd_ctx, "ERROR: last sector must be <= %d",
525 (int) num_sectors - 1);
526 return ERROR_FAIL;
529 return ERROR_OK;
532 COMMAND_HANDLER(handle_flash_erase_command)
534 if (argc != 2)
535 return ERROR_COMMAND_SYNTAX_ERROR;
537 uint32_t bank_nr;
538 uint32_t first;
539 uint32_t last;
541 COMMAND_PARSE_NUMBER(u32, args[0], bank_nr);
542 struct flash_bank *p = get_flash_bank_by_num(bank_nr);
543 if (!p)
544 return ERROR_OK;
546 COMMAND_PARSE_NUMBER(u32, args[1], first);
547 if (strcmp(args[2], "last") == 0)
548 last = p->num_sectors - 1;
549 else
550 COMMAND_PARSE_NUMBER(u32, args[2], last);
552 int retval;
553 if ((retval = flash_check_sector_parameters(cmd_ctx,
554 first, last, p->num_sectors)) != ERROR_OK)
555 return retval;
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));
569 return ERROR_OK;
572 COMMAND_HANDLER(handle_flash_protect_command)
574 if (argc != 3)
575 return ERROR_COMMAND_SYNTAX_ERROR;
577 uint32_t bank_nr;
578 uint32_t first;
579 uint32_t last;
580 int set;
582 COMMAND_PARSE_NUMBER(u32, args[0], bank_nr);
583 struct flash_bank *p = get_flash_bank_by_num(bank_nr);
584 if (!p)
585 return ERROR_OK;
587 COMMAND_PARSE_NUMBER(u32, args[1], first);
588 if (strcmp(args[2], "last") == 0)
589 last = p->num_sectors - 1;
590 else
591 COMMAND_PARSE_NUMBER(u32, args[2], last);
593 if (strcmp(args[3], "on") == 0)
594 set = 1;
595 else if (strcmp(args[3], "off") == 0)
596 set = 0;
597 else
598 return ERROR_COMMAND_SYNTAX_ERROR;
600 int retval;
601 if ((retval = flash_check_sector_parameters(cmd_ctx,
602 first, last, p->num_sectors)) != ERROR_OK)
603 return retval;
605 retval = flash_driver_protect(p, set, first, last);
606 if (retval == ERROR_OK) {
607 command_print(cmd_ctx, "%s protection for sectors %i "
608 "through %i on flash bank %i",
609 (set) ? "set" : "cleared", (int) first,
610 (int) last, (int) bank_nr);
613 return ERROR_OK;
616 COMMAND_HANDLER(handle_flash_write_image_command)
618 struct target *target = get_current_target(cmd_ctx);
620 struct image image;
621 uint32_t written;
623 int retval;
625 if (argc < 1)
627 return ERROR_COMMAND_SYNTAX_ERROR;
630 /* flash auto-erase is disabled by default*/
631 int auto_erase = 0;
632 bool auto_unlock = false;
634 for (;;)
636 if (strcmp(args[0], "erase") == 0)
638 auto_erase = 1;
639 args++;
640 argc--;
641 command_print(cmd_ctx, "auto erase enabled");
642 } else if (strcmp(args[0], "unlock") == 0)
644 auto_unlock = true;
645 args++;
646 argc--;
647 command_print(cmd_ctx, "auto unlock enabled");
648 } else
650 break;
654 if (argc < 1)
656 return ERROR_COMMAND_SYNTAX_ERROR;
659 if (!target)
661 LOG_ERROR("no target selected");
662 return ERROR_FAIL;
665 struct duration bench;
666 duration_start(&bench);
668 if (argc >= 2)
670 image.base_address_set = 1;
671 COMMAND_PARSE_NUMBER(int, args[1], image.base_address);
673 else
675 image.base_address_set = 0;
676 image.base_address = 0x0;
679 image.start_address_set = 0;
681 retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL);
682 if (retval != ERROR_OK)
684 return retval;
687 retval = flash_write_unlock(target, &image, &written, auto_erase, auto_unlock);
688 if (retval != ERROR_OK)
690 image_close(&image);
691 return retval;
694 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
696 command_print(cmd_ctx, "wrote %" PRIu32 " byte from file %s "
697 "in %fs (%0.3f kb/s)", written, args[0],
698 duration_elapsed(&bench), duration_kbps(&bench, written));
701 image_close(&image);
703 return retval;
706 COMMAND_HANDLER(handle_flash_fill_command)
708 int err = ERROR_OK;
709 uint32_t address;
710 uint32_t pattern;
711 uint32_t count;
712 uint8_t chunk[1024];
713 uint8_t readback[1024];
714 uint32_t wrote = 0;
715 uint32_t cur_size = 0;
716 uint32_t chunk_count;
717 struct target *target = get_current_target(cmd_ctx);
718 uint32_t i;
719 uint32_t wordsize;
721 if (argc != 3)
722 return ERROR_COMMAND_SYNTAX_ERROR;
724 COMMAND_PARSE_NUMBER(u32, args[0], address);
725 COMMAND_PARSE_NUMBER(u32, args[1], pattern);
726 COMMAND_PARSE_NUMBER(u32, args[2], count);
728 if (count == 0)
729 return ERROR_OK;
731 switch (CMD_NAME[4])
733 case 'w':
734 wordsize = 4;
735 break;
736 case 'h':
737 wordsize = 2;
738 break;
739 case 'b':
740 wordsize = 1;
741 break;
742 default:
743 return ERROR_COMMAND_SYNTAX_ERROR;
746 chunk_count = MIN(count, (1024 / wordsize));
747 switch (wordsize)
749 case 4:
750 for (i = 0; i < chunk_count; i++)
752 target_buffer_set_u32(target, chunk + i * wordsize, pattern);
754 break;
755 case 2:
756 for (i = 0; i < chunk_count; i++)
758 target_buffer_set_u16(target, chunk + i * wordsize, pattern);
760 break;
761 case 1:
762 memset(chunk, pattern, chunk_count);
763 break;
764 default:
765 LOG_ERROR("BUG: can't happen");
766 exit(-1);
769 struct duration bench;
770 duration_start(&bench);
772 for (wrote = 0; wrote < (count*wordsize); wrote += cur_size)
774 cur_size = MIN((count*wordsize - wrote), sizeof(chunk));
775 struct flash_bank *bank;
776 bank = get_flash_bank_by_addr(target, address);
777 if (bank == NULL)
779 return ERROR_FAIL;
781 err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
782 if (err != ERROR_OK)
783 return err;
785 err = target_read_buffer(target, address + wrote, cur_size, readback);
786 if (err != ERROR_OK)
787 return err;
789 unsigned i;
790 for (i = 0; i < cur_size; i++)
792 if (readback[i]!=chunk[i])
794 LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
795 address + wrote + i, readback[i], chunk[i]);
796 return ERROR_FAIL;
801 if (duration_measure(&bench) == ERROR_OK)
803 command_print(cmd_ctx, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32
804 " in %fs (%0.3f kb/s)", wrote, address,
805 duration_elapsed(&bench), duration_kbps(&bench, wrote));
807 return ERROR_OK;
810 COMMAND_HANDLER(handle_flash_write_bank_command)
812 uint32_t offset;
813 uint8_t *buffer;
814 uint32_t buf_cnt;
815 struct fileio fileio;
817 if (argc != 3)
818 return ERROR_COMMAND_SYNTAX_ERROR;
820 struct duration bench;
821 duration_start(&bench);
823 struct flash_bank *p;
824 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &p);
825 if (ERROR_OK != retval)
826 return retval;
828 COMMAND_PARSE_NUMBER(u32, args[2], offset);
830 if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
832 return ERROR_OK;
835 buffer = malloc(fileio.size);
836 if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
838 free(buffer);
839 fileio_close(&fileio);
840 return ERROR_OK;
843 retval = flash_driver_write(p, buffer, offset, buf_cnt);
845 free(buffer);
846 buffer = NULL;
848 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
850 command_print(cmd_ctx, "wrote %zu byte from file %s to flash bank %u"
851 " at offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
852 fileio.size, args[1], p->bank_number, offset,
853 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
856 fileio_close(&fileio);
858 return retval;
861 void flash_set_dirty(void)
863 struct flash_bank *c;
864 int i;
866 /* set all flash to require erasing */
867 for (c = flash_banks; c; c = c->next)
869 for (i = 0; i < c->num_sectors; i++)
871 c->sectors[i].is_erased = 0;
876 /* lookup flash bank by address */
877 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
879 struct flash_bank *c;
881 /* cycle through bank list */
882 for (c = flash_banks; c; c = c->next)
884 int retval;
885 retval = c->driver->auto_probe(c);
887 if (retval != ERROR_OK)
889 LOG_ERROR("auto_probe failed %d\n", retval);
890 return NULL;
892 /* check whether address belongs to this flash bank */
893 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
894 return c;
896 LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
897 return NULL;
900 /* erase given flash region, selects proper bank according to target and address */
901 static int flash_iterate_address_range(struct target *target, uint32_t addr, uint32_t length,
902 int (*callback)(struct flash_bank *bank, int first, int last))
904 struct flash_bank *c;
905 int first = -1;
906 int last = -1;
907 int i;
909 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
910 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
912 if (c->size == 0 || c->num_sectors == 0)
914 LOG_ERROR("Bank is invalid");
915 return ERROR_FLASH_BANK_INVALID;
918 if (length == 0)
920 /* special case, erase whole bank when length is zero */
921 if (addr != c->base)
922 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
924 return callback(c, 0, c->num_sectors - 1);
927 /* check whether it fits */
928 if (addr + length - 1 > c->base + c->size - 1)
929 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
931 addr -= c->base;
933 for (i = 0; i < c->num_sectors; i++)
935 /* check whether sector overlaps with the given range and is not yet erased */
936 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
937 /* if first is not set yet then this is the first sector */
938 if (first == -1)
939 first = i;
940 last = i; /* and it is the last one so far in any case */
944 if (first == -1 || last == -1)
945 return ERROR_OK;
947 return callback(c, first, last);
952 int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
954 return flash_iterate_address_range(target, addr, length, &flash_driver_erase);
957 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
959 return flash_driver_protect(bank, 0, first, last);
962 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
964 return flash_iterate_address_range(target, addr, length, &flash_driver_unprotect);
968 /* write (optional verify) an image to flash memory of the given target */
969 static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock)
971 int retval = ERROR_OK;
973 int section;
974 uint32_t section_offset;
975 struct flash_bank *c;
976 int *padding;
978 section = 0;
979 section_offset = 0;
981 if (written)
982 *written = 0;
984 if (erase)
986 /* assume all sectors need erasing - stops any problems
987 * when flash_write is called multiple times */
989 flash_set_dirty();
992 /* allocate padding array */
993 padding = malloc(image->num_sections * sizeof(padding));
995 /* loop until we reach end of the image */
996 while (section < image->num_sections)
998 uint32_t buffer_size;
999 uint8_t *buffer;
1000 int section_first;
1001 int section_last;
1002 uint32_t run_address = image->sections[section].base_address + section_offset;
1003 uint32_t run_size = image->sections[section].size - section_offset;
1004 int pad_bytes = 0;
1006 if (image->sections[section].size == 0)
1008 LOG_WARNING("empty section %d", section);
1009 section++;
1010 section_offset = 0;
1011 continue;
1014 /* find the corresponding flash bank */
1015 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
1017 section++; /* and skip it */
1018 section_offset = 0;
1019 continue;
1022 /* collect consecutive sections which fall into the same bank */
1023 section_first = section;
1024 section_last = section;
1025 padding[section] = 0;
1026 while ((run_address + run_size - 1 < c->base + c->size - 1)
1027 && (section_last + 1 < image->num_sections))
1029 if (image->sections[section_last + 1].base_address < (run_address + run_size))
1031 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
1032 break;
1034 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
1035 * attempt to rebuild a consecutive buffer for the flash loader */
1036 pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
1037 if ((run_address + run_size + pad_bytes) > (c->base + c->size))
1038 break;
1039 padding[section_last] = pad_bytes;
1040 run_size += image->sections[++section_last].size;
1041 run_size += pad_bytes;
1042 padding[section_last] = 0;
1044 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
1047 /* fit the run into bank constraints */
1048 if (run_address + run_size - 1 > c->base + c->size - 1)
1050 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
1051 (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
1052 run_size = c->base + c->size - run_address;
1055 /* allocate buffer */
1056 buffer = malloc(run_size);
1057 buffer_size = 0;
1059 /* read sections to the buffer */
1060 while (buffer_size < run_size)
1062 uint32_t size_read;
1064 size_read = run_size - buffer_size;
1065 if (size_read > image->sections[section].size - section_offset)
1066 size_read = image->sections[section].size - section_offset;
1068 if ((retval = image_read_section(image, section, section_offset,
1069 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
1071 free(buffer);
1072 free(padding);
1073 return retval;
1076 /* see if we need to pad the section */
1077 while (padding[section]--)
1078 (buffer + buffer_size)[size_read++] = 0xff;
1080 buffer_size += size_read;
1081 section_offset += size_read;
1083 if (section_offset >= image->sections[section].size)
1085 section++;
1086 section_offset = 0;
1090 retval = ERROR_OK;
1092 if (unlock)
1094 retval = flash_unlock_address_range(target, run_address, run_size);
1096 if (retval == ERROR_OK)
1098 if (erase)
1100 /* calculate and erase sectors */
1101 retval = flash_erase_address_range(target, run_address, run_size);
1105 if (retval == ERROR_OK)
1107 /* write flash sectors */
1108 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
1111 free(buffer);
1113 if (retval != ERROR_OK)
1115 free(padding);
1116 return retval; /* abort operation */
1119 if (written != NULL)
1120 *written += run_size; /* add run size to total written counter */
1123 free(padding);
1125 return retval;
1128 int flash_write(struct target *target, struct image *image, uint32_t *written, int erase)
1130 return flash_write_unlock(target, image, written, erase, false);
1133 int default_flash_mem_blank_check(struct flash_bank *bank)
1135 struct target *target = bank->target;
1136 uint8_t buffer[1024];
1137 int buffer_size = sizeof(buffer);
1138 int i;
1139 uint32_t nBytes;
1141 if (bank->target->state != TARGET_HALTED)
1143 LOG_ERROR("Target not halted");
1144 return ERROR_TARGET_NOT_HALTED;
1147 for (i = 0; i < bank->num_sectors; i++)
1149 uint32_t j;
1150 bank->sectors[i].is_erased = 1;
1152 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
1154 uint32_t chunk;
1155 int retval;
1156 chunk = buffer_size;
1157 if (chunk > (j - bank->sectors[i].size))
1159 chunk = (j - bank->sectors[i].size);
1162 retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
1163 if (retval != ERROR_OK)
1164 return retval;
1166 for (nBytes = 0; nBytes < chunk; nBytes++)
1168 if (buffer[nBytes] != 0xFF)
1170 bank->sectors[i].is_erased = 0;
1171 break;
1177 return ERROR_OK;
1180 int default_flash_blank_check(struct flash_bank *bank)
1182 struct target *target = bank->target;
1183 int i;
1184 int retval;
1185 int fast_check = 0;
1186 uint32_t blank;
1188 if (bank->target->state != TARGET_HALTED)
1190 LOG_ERROR("Target not halted");
1191 return ERROR_TARGET_NOT_HALTED;
1194 for (i = 0; i < bank->num_sectors; i++)
1196 uint32_t address = bank->base + bank->sectors[i].offset;
1197 uint32_t size = bank->sectors[i].size;
1199 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
1201 fast_check = 0;
1202 break;
1204 if (blank == 0xFF)
1205 bank->sectors[i].is_erased = 1;
1206 else
1207 bank->sectors[i].is_erased = 0;
1208 fast_check = 1;
1211 if (!fast_check)
1213 LOG_USER("Running slow fallback erase check - add working memory");
1214 return default_flash_mem_blank_check(bank);
1217 return ERROR_OK;
1220 int flash_init_drivers(struct command_context *cmd_ctx)
1222 register_jim(cmd_ctx, "ocd_flash_banks",
1223 jim_flash_banks, "return information about the flash banks");
1225 if (!flash_banks)
1226 return ERROR_OK;
1228 register_command(cmd_ctx, flash_cmd, "info",
1229 handle_flash_info_command, COMMAND_EXEC,
1230 "print info about flash bank <num>");
1231 register_command(cmd_ctx, flash_cmd, "probe",
1232 handle_flash_probe_command, COMMAND_EXEC,
1233 "identify flash bank <num>");
1234 register_command(cmd_ctx, flash_cmd, "erase_check",
1235 handle_flash_erase_check_command, COMMAND_EXEC,
1236 "check erase state of sectors in flash bank <num>");
1237 register_command(cmd_ctx, flash_cmd, "protect_check",
1238 handle_flash_protect_check_command, COMMAND_EXEC,
1239 "check protection state of sectors in flash bank <num>");
1240 register_command(cmd_ctx, flash_cmd, "erase_sector",
1241 handle_flash_erase_command, COMMAND_EXEC,
1242 "erase sectors at <bank> <first> <last>");
1243 register_command(cmd_ctx, flash_cmd, "erase_address",
1244 handle_flash_erase_address_command, COMMAND_EXEC,
1245 "erase address range <address> <length>");
1247 register_command(cmd_ctx, flash_cmd, "fillw",
1248 handle_flash_fill_command, COMMAND_EXEC,
1249 "fill with pattern (no autoerase) <address> <word_pattern> <count>");
1250 register_command(cmd_ctx, flash_cmd, "fillh",
1251 handle_flash_fill_command, COMMAND_EXEC,
1252 "fill with pattern <address> <halfword_pattern> <count>");
1253 register_command(cmd_ctx, flash_cmd, "fillb",
1254 handle_flash_fill_command, COMMAND_EXEC,
1255 "fill with pattern <address> <byte_pattern> <count>");
1257 register_command(cmd_ctx, flash_cmd, "write_bank",
1258 handle_flash_write_bank_command, COMMAND_EXEC,
1259 "write binary data to <bank> <file> <offset>");
1260 register_command(cmd_ctx, flash_cmd, "write_image",
1261 handle_flash_write_image_command, COMMAND_EXEC,
1262 "write_image [erase] [unlock] <file> [offset] [type]");
1263 register_command(cmd_ctx, flash_cmd, "protect",
1264 handle_flash_protect_command, COMMAND_EXEC,
1265 "set protection of sectors at <bank> <first> <last> <on | off>");
1267 return ERROR_OK;
1270 int flash_register_commands(struct command_context *cmd_ctx)
1272 flash_cmd = register_command(cmd_ctx, NULL, "flash",
1273 NULL, COMMAND_ANY, NULL);
1275 register_command(cmd_ctx, flash_cmd, "bank",
1276 handle_flash_bank_command, COMMAND_CONFIG,
1277 "flash bank <driver> <base> <size> "
1278 "<chip_width> <bus_width> <target> [driver_options ...]");
1279 return ERROR_OK;