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 /* command handlers */
35 static int handle_flash_bank_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
36 static int handle_flash_info_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
37 static int handle_flash_probe_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
38 static int handle_flash_erase_check_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
39 static int handle_flash_erase_address_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
40 static int handle_flash_protect_check_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
41 static int handle_flash_erase_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
42 static int handle_flash_write_bank_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
43 static int handle_flash_write_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
44 static int handle_flash_fill_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
45 static int handle_flash_protect_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
46 static int flash_write_unlock(target_t
*target
, image_t
*image
, uint32_t *written
, int erase
, bool unlock
);
50 extern flash_driver_t lpc2000_flash
;
51 extern flash_driver_t lpc288x_flash
;
52 extern flash_driver_t lpc2900_flash
;
53 extern flash_driver_t cfi_flash
;
54 extern flash_driver_t at91sam3_flash
;
55 extern flash_driver_t at91sam7_flash
;
56 extern flash_driver_t str7x_flash
;
57 extern flash_driver_t str9x_flash
;
58 extern flash_driver_t aduc702x_flash
;
59 extern flash_driver_t stellaris_flash
;
60 extern flash_driver_t str9xpec_flash
;
61 extern flash_driver_t stm32x_flash
;
62 extern flash_driver_t tms470_flash
;
63 extern flash_driver_t ecosflash_flash
;
64 extern flash_driver_t ocl_flash
;
65 extern flash_driver_t pic32mx_flash
;
66 extern flash_driver_t avr_flash
;
67 extern flash_driver_t faux_flash
;
69 flash_driver_t
*flash_drivers
[] = {
91 flash_bank_t
*flash_banks
;
92 static command_t
*flash_cmd
;
94 /* wafer thin wrapper for invoking the flash driver */
95 static int flash_driver_write(struct flash_bank_s
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
99 retval
= bank
->driver
->write(bank
, buffer
, offset
, count
);
100 if (retval
!= ERROR_OK
)
102 LOG_ERROR("error writing to flash at address 0x%08" PRIx32
" at offset 0x%8.8" PRIx32
" (%d)",
103 bank
->base
, offset
, retval
);
109 static int flash_driver_erase(struct flash_bank_s
*bank
, int first
, int last
)
113 retval
= bank
->driver
->erase(bank
, first
, last
);
114 if (retval
!= ERROR_OK
)
116 LOG_ERROR("failed erasing sectors %d to %d (%d)", first
, last
, retval
);
122 int flash_driver_protect(struct flash_bank_s
*bank
, int set
, int first
, int last
)
126 retval
= bank
->driver
->protect(bank
, set
, first
, last
);
127 if (retval
!= ERROR_OK
)
129 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first
, last
, retval
);
135 int flash_register_commands(struct command_context_s
*cmd_ctx
)
137 flash_cmd
= register_command(cmd_ctx
, NULL
, "flash", NULL
, COMMAND_ANY
, NULL
);
139 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 ...]");
143 static int jim_flash_banks(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
148 Jim_WrongNumArgs(interp
, 1, argv
, "no arguments to flash_banks command");
152 Jim_Obj
*list
= Jim_NewListObj(interp
, NULL
, 0);
153 for (p
= flash_banks
; p
; p
= p
->next
)
155 Jim_Obj
*elem
= Jim_NewListObj(interp
, NULL
, 0);
157 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, "name", -1));
158 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, p
->driver
->name
, -1));
159 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, "base", -1));
160 Jim_ListAppendElement(interp
, elem
, Jim_NewIntObj(interp
, p
->base
));
161 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, "size", -1));
162 Jim_ListAppendElement(interp
, elem
, Jim_NewIntObj(interp
, p
->size
));
163 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, "bus_width", -1));
164 Jim_ListAppendElement(interp
, elem
, Jim_NewIntObj(interp
, p
->bus_width
));
165 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, "chip_width", -1));
166 Jim_ListAppendElement(interp
, elem
, Jim_NewIntObj(interp
, p
->chip_width
));
168 Jim_ListAppendElement(interp
, list
, elem
);
171 Jim_SetResult(interp
, list
);
176 int flash_init_drivers(struct command_context_s
*cmd_ctx
)
178 register_jim(cmd_ctx
, "ocd_flash_banks", jim_flash_banks
, "return information about the flash banks");
183 register_command(cmd_ctx
, flash_cmd
, "info", handle_flash_info_command
, COMMAND_EXEC
,
184 "print info about flash bank <num>");
185 register_command(cmd_ctx
, flash_cmd
, "probe", handle_flash_probe_command
, COMMAND_EXEC
,
186 "identify flash bank <num>");
187 register_command(cmd_ctx
, flash_cmd
, "erase_check", handle_flash_erase_check_command
, COMMAND_EXEC
,
188 "check erase state of sectors in flash bank <num>");
189 register_command(cmd_ctx
, flash_cmd
, "protect_check", handle_flash_protect_check_command
, COMMAND_EXEC
,
190 "check protection state of sectors in flash bank <num>");
191 register_command(cmd_ctx
, flash_cmd
, "erase_sector", handle_flash_erase_command
, COMMAND_EXEC
,
192 "erase sectors at <bank> <first> <last>");
193 register_command(cmd_ctx
, flash_cmd
, "erase_address", handle_flash_erase_address_command
, COMMAND_EXEC
,
194 "erase address range <address> <length>");
196 register_command(cmd_ctx
, flash_cmd
, "fillw", handle_flash_fill_command
, COMMAND_EXEC
,
197 "fill with pattern (no autoerase) <address> <word_pattern> <count>");
198 register_command(cmd_ctx
, flash_cmd
, "fillh", handle_flash_fill_command
, COMMAND_EXEC
,
199 "fill with pattern <address> <halfword_pattern> <count>");
200 register_command(cmd_ctx
, flash_cmd
, "fillb", handle_flash_fill_command
, COMMAND_EXEC
,
201 "fill with pattern <address> <byte_pattern> <count>");
203 register_command(cmd_ctx
, flash_cmd
, "write_bank", handle_flash_write_bank_command
, COMMAND_EXEC
,
204 "write binary data to <bank> <file> <offset>");
205 register_command(cmd_ctx
, flash_cmd
, "write_image", handle_flash_write_image_command
, COMMAND_EXEC
,
206 "write_image [erase] [unlock] <file> [offset] [type]");
207 register_command(cmd_ctx
, flash_cmd
, "protect", handle_flash_protect_command
, COMMAND_EXEC
,
208 "set protection of sectors at <bank> <first> <last> <on | off>");
213 flash_bank_t
*get_flash_bank_by_num_noprobe(int num
)
218 for (p
= flash_banks
; p
; p
= p
->next
)
225 LOG_ERROR("flash bank %d does not exist", num
);
229 int flash_get_bank_count(void)
233 for (p
= flash_banks
; p
; p
= p
->next
)
240 flash_bank_t
*get_flash_bank_by_num(int num
)
242 flash_bank_t
*p
= get_flash_bank_by_num_noprobe(num
);
248 retval
= p
->driver
->auto_probe(p
);
250 if (retval
!= ERROR_OK
)
252 LOG_ERROR("auto_probe failed %d\n", retval
);
258 int flash_command_get_bank_by_num(
259 struct command_context_s
*cmd_ctx
, char *str
, flash_bank_t
**bank
)
262 COMMAND_PARSE_NUMBER(uint
, str
, bank_num
);
264 *bank
= get_flash_bank_by_num(bank_num
);
267 command_print(cmd_ctx
,
268 "flash bank '#%u' not found", bank_num
);
269 return ERROR_INVALID_ARGUMENTS
;
275 static int handle_flash_bank_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
284 return ERROR_COMMAND_SYNTAX_ERROR
;
287 if ((target
= get_target(args
[5])) == NULL
)
289 LOG_ERROR("target '%s' not defined", args
[5]);
293 for (i
= 0; flash_drivers
[i
]; i
++)
295 if (strcmp(args
[0], flash_drivers
[i
]->name
) != 0)
300 /* register flash specific commands */
301 if (flash_drivers
[i
]->register_commands(cmd_ctx
) != ERROR_OK
)
303 LOG_ERROR("couldn't register '%s' commands", args
[0]);
307 c
= malloc(sizeof(flash_bank_t
));
309 c
->driver
= flash_drivers
[i
];
310 c
->driver_priv
= NULL
;
311 COMMAND_PARSE_NUMBER(u32
, args
[1], c
->base
);
312 COMMAND_PARSE_NUMBER(u32
, args
[2], c
->size
);
313 COMMAND_PARSE_NUMBER(int, args
[3], c
->chip_width
);
314 COMMAND_PARSE_NUMBER(int, args
[4], c
->bus_width
);
319 if ((retval
= flash_drivers
[i
]->flash_bank_command(cmd_ctx
, cmd
, args
, argc
, c
)) != ERROR_OK
)
321 LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32
, args
[0], c
->base
);
326 /* put flash bank in linked list */
330 /* find last flash bank */
331 for (p
= flash_banks
; p
&& p
->next
; p
= p
->next
) bank_num
++;
334 c
->bank_number
= bank_num
+ 1;
345 /* no matching flash driver found */
348 LOG_ERROR("flash driver '%s' not found", args
[0]);
355 static int handle_flash_info_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
363 return ERROR_COMMAND_SYNTAX_ERROR
;
366 COMMAND_PARSE_NUMBER(uint
, args
[0], bank_nr
);
368 for (p
= flash_banks
; p
; p
= p
->next
, i
++)
375 /* attempt auto probe */
376 if ((retval
= p
->driver
->auto_probe(p
)) != ERROR_OK
)
379 command_print(cmd_ctx
,
380 "#%" PRIi32
" : %s at 0x%8.8" PRIx32
", size 0x%8.8" PRIx32
", buswidth %i, chipwidth %i",
387 for (j
= 0; j
< p
->num_sectors
; j
++)
391 if (p
->sectors
[j
].is_protected
== 0)
392 protect_state
= "not protected";
393 else if (p
->sectors
[j
].is_protected
== 1)
394 protect_state
= "protected";
396 protect_state
= "protection state unknown";
398 command_print(cmd_ctx
,
399 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
401 p
->sectors
[j
].offset
,
403 p
->sectors
[j
].size
>> 10,
407 *buf
= '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
408 retval
= p
->driver
->info(p
, buf
, sizeof(buf
));
409 command_print(cmd_ctx
, "%s", buf
);
410 if (retval
!= ERROR_OK
)
411 LOG_ERROR("error retrieving flash info (%d)", retval
);
417 static int handle_flash_probe_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
423 return ERROR_COMMAND_SYNTAX_ERROR
;
427 COMMAND_PARSE_NUMBER(uint
, args
[0], bank_nr
);
428 flash_bank_t
*p
= get_flash_bank_by_num_noprobe(bank_nr
);
431 if ((retval
= p
->driver
->probe(p
)) == ERROR_OK
)
433 command_print(cmd_ctx
, "flash '%s' found at 0x%8.8" PRIx32
, p
->driver
->name
, p
->base
);
435 else if (retval
== ERROR_FLASH_BANK_INVALID
)
437 command_print(cmd_ctx
, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32
,
442 command_print(cmd_ctx
, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32
,
448 command_print(cmd_ctx
, "flash bank '#%s' is out of bounds", args
[0]);
454 static int handle_flash_erase_check_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
458 return ERROR_COMMAND_SYNTAX_ERROR
;
462 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
463 if (ERROR_OK
!= retval
)
467 if ((retval
= p
->driver
->erase_check(p
)) == ERROR_OK
)
469 command_print(cmd_ctx
, "successfully checked erase state");
473 command_print(cmd_ctx
, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32
,
477 for (j
= 0; j
< p
->num_sectors
; j
++)
481 if (p
->sectors
[j
].is_erased
== 0)
482 erase_state
= "not erased";
483 else if (p
->sectors
[j
].is_erased
== 1)
484 erase_state
= "erased";
486 erase_state
= "erase state unknown";
488 command_print(cmd_ctx
,
489 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
491 p
->sectors
[j
].offset
,
493 p
->sectors
[j
].size
>> 10,
500 static int handle_flash_erase_address_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
507 target_t
*target
= get_current_target(cmd_ctx
);
510 return ERROR_COMMAND_SYNTAX_ERROR
;
512 COMMAND_PARSE_NUMBER(int, args
[0], address
);
513 COMMAND_PARSE_NUMBER(int, args
[1], length
);
516 command_print(cmd_ctx
, "Length must be >0");
517 return ERROR_COMMAND_SYNTAX_ERROR
;
520 p
= get_flash_bank_by_addr(target
, address
);
526 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
529 struct duration bench
;
530 duration_start(&bench
);
532 retval
= flash_erase_address_range(target
, address
, length
);
534 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
536 command_print(cmd_ctx
, "erased address 0x%8.8x (length %i)"
537 " in %fs (%0.3f kb/s)", address
, length
,
538 duration_elapsed(&bench
), duration_kbps(&bench
, length
));
544 static int handle_flash_protect_check_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
547 return ERROR_COMMAND_SYNTAX_ERROR
;
550 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
551 if (ERROR_OK
!= retval
)
554 if ((retval
= p
->driver
->protect_check(p
)) == ERROR_OK
)
556 command_print(cmd_ctx
, "successfully checked protect state");
558 else if (retval
== ERROR_FLASH_OPERATION_FAILED
)
560 command_print(cmd_ctx
, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32
, args
[0], p
->base
);
564 command_print(cmd_ctx
, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32
, args
[0], p
->base
);
570 static int flash_check_sector_parameters(struct command_context_s
*cmd_ctx
,
571 uint32_t first
, uint32_t last
, uint32_t num_sectors
)
573 if (!(first
<= last
)) {
574 command_print(cmd_ctx
, "ERROR: "
575 "first sector must be <= last sector");
579 if (!(last
<= (num_sectors
- 1))) {
580 command_print(cmd_ctx
, "ERROR: last sector must be <= %d",
581 (int) num_sectors
- 1);
588 static int handle_flash_erase_command(struct command_context_s
*cmd_ctx
,
589 char *cmd
, char **args
, int argc
)
592 return ERROR_COMMAND_SYNTAX_ERROR
;
598 COMMAND_PARSE_NUMBER(u32
, args
[0], bank_nr
);
599 flash_bank_t
*p
= get_flash_bank_by_num(bank_nr
);
603 COMMAND_PARSE_NUMBER(u32
, args
[1], first
);
604 if (strcmp(args
[2], "last") == 0)
605 last
= p
->num_sectors
- 1;
607 COMMAND_PARSE_NUMBER(u32
, args
[2], last
);
610 if ((retval
= flash_check_sector_parameters(cmd_ctx
,
611 first
, last
, p
->num_sectors
)) != ERROR_OK
)
614 struct duration bench
;
615 duration_start(&bench
);
617 retval
= flash_driver_erase(p
, first
, last
);
619 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
621 command_print(cmd_ctx
, "erased sectors %" PRIu32
" "
622 "through %" PRIu32
" on flash bank %" PRIu32
" "
623 "in %fs", first
, last
, bank_nr
, duration_elapsed(&bench
));
629 static int handle_flash_protect_command(struct command_context_s
*cmd_ctx
,
630 char *cmd
, char **args
, int argc
)
633 return ERROR_COMMAND_SYNTAX_ERROR
;
640 COMMAND_PARSE_NUMBER(u32
, args
[0], bank_nr
);
641 flash_bank_t
*p
= get_flash_bank_by_num(bank_nr
);
645 COMMAND_PARSE_NUMBER(u32
, args
[1], first
);
646 if (strcmp(args
[2], "last") == 0)
647 last
= p
->num_sectors
- 1;
649 COMMAND_PARSE_NUMBER(u32
, args
[2], last
);
651 if (strcmp(args
[3], "on") == 0)
653 else if (strcmp(args
[3], "off") == 0)
656 return ERROR_COMMAND_SYNTAX_ERROR
;
659 if ((retval
= flash_check_sector_parameters(cmd_ctx
,
660 first
, last
, p
->num_sectors
)) != ERROR_OK
)
663 retval
= flash_driver_protect(p
, set
, first
, last
);
664 if (retval
== ERROR_OK
) {
665 command_print(cmd_ctx
, "%s protection for sectors %i "
666 "through %i on flash bank %i",
667 (set
) ? "set" : "cleared", (int) first
,
668 (int) last
, (int) bank_nr
);
674 static int handle_flash_write_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
676 target_t
*target
= get_current_target(cmd_ctx
);
685 return ERROR_COMMAND_SYNTAX_ERROR
;
688 /* flash auto-erase is disabled by default*/
690 bool auto_unlock
= false;
694 if (strcmp(args
[0], "erase") == 0)
699 command_print(cmd_ctx
, "auto erase enabled");
700 } else if (strcmp(args
[0], "unlock") == 0)
705 command_print(cmd_ctx
, "auto unlock enabled");
714 return ERROR_COMMAND_SYNTAX_ERROR
;
719 LOG_ERROR("no target selected");
723 struct duration bench
;
724 duration_start(&bench
);
728 image
.base_address_set
= 1;
729 COMMAND_PARSE_NUMBER(int, args
[1], image
.base_address
);
733 image
.base_address_set
= 0;
734 image
.base_address
= 0x0;
737 image
.start_address_set
= 0;
739 retval
= image_open(&image
, args
[0], (argc
== 3) ? args
[2] : NULL
);
740 if (retval
!= ERROR_OK
)
745 retval
= flash_write_unlock(target
, &image
, &written
, auto_erase
, auto_unlock
);
746 if (retval
!= ERROR_OK
)
752 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
754 command_print(cmd_ctx
, "wrote %" PRIu32
" byte from file %s "
755 "in %fs (%0.3f kb/s)", written
, args
[0],
756 duration_elapsed(&bench
), duration_kbps(&bench
, written
));
764 static int handle_flash_fill_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
771 uint8_t readback
[1024];
773 uint32_t cur_size
= 0;
774 uint32_t chunk_count
;
775 target_t
*target
= get_current_target(cmd_ctx
);
780 return ERROR_COMMAND_SYNTAX_ERROR
;
782 COMMAND_PARSE_NUMBER(u32
, args
[0], address
);
783 COMMAND_PARSE_NUMBER(u32
, args
[1], pattern
);
784 COMMAND_PARSE_NUMBER(u32
, args
[2], count
);
801 return ERROR_COMMAND_SYNTAX_ERROR
;
804 chunk_count
= MIN(count
, (1024 / wordsize
));
808 for (i
= 0; i
< chunk_count
; i
++)
810 target_buffer_set_u32(target
, chunk
+ i
* wordsize
, pattern
);
814 for (i
= 0; i
< chunk_count
; i
++)
816 target_buffer_set_u16(target
, chunk
+ i
* wordsize
, pattern
);
820 memset(chunk
, pattern
, chunk_count
);
823 LOG_ERROR("BUG: can't happen");
827 struct duration bench
;
828 duration_start(&bench
);
830 for (wrote
= 0; wrote
< (count
*wordsize
); wrote
+= cur_size
)
832 cur_size
= MIN((count
*wordsize
- wrote
), sizeof(chunk
));
834 bank
= get_flash_bank_by_addr(target
, address
);
839 err
= flash_driver_write(bank
, chunk
, address
- bank
->base
+ wrote
, cur_size
);
843 err
= target_read_buffer(target
, address
+ wrote
, cur_size
, readback
);
848 for (i
= 0; i
< cur_size
; i
++)
850 if (readback
[i
]!=chunk
[i
])
852 LOG_ERROR("Verfication error address 0x%08" PRIx32
", read back 0x%02x, expected 0x%02x",
853 address
+ wrote
+ i
, readback
[i
], chunk
[i
]);
859 if (duration_measure(&bench
) == ERROR_OK
)
861 command_print(cmd_ctx
, "wrote %" PRIu32
" bytes to 0x%8.8" PRIx32
862 " in %fs (%0.3f kb/s)", wrote
, address
,
863 duration_elapsed(&bench
), duration_kbps(&bench
, wrote
));
868 static int handle_flash_write_bank_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
876 return ERROR_COMMAND_SYNTAX_ERROR
;
878 struct duration bench
;
879 duration_start(&bench
);
882 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
883 if (ERROR_OK
!= retval
)
886 COMMAND_PARSE_NUMBER(u32
, args
[2], offset
);
888 if (fileio_open(&fileio
, args
[1], FILEIO_READ
, FILEIO_BINARY
) != ERROR_OK
)
893 buffer
= malloc(fileio
.size
);
894 if (fileio_read(&fileio
, fileio
.size
, buffer
, &buf_cnt
) != ERROR_OK
)
897 fileio_close(&fileio
);
901 retval
= flash_driver_write(p
, buffer
, offset
, buf_cnt
);
906 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
908 command_print(cmd_ctx
, "wrote %lld byte from file %s to flash bank %u"
909 " at offset 0x%8.8" PRIx32
" in %fs (%0.3f kb/s)",
910 fileio
.size
, args
[1], p
->bank_number
, offset
,
911 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
914 fileio_close(&fileio
);
919 void flash_set_dirty(void)
924 /* set all flash to require erasing */
925 for (c
= flash_banks
; c
; c
= c
->next
)
927 for (i
= 0; i
< c
->num_sectors
; i
++)
929 c
->sectors
[i
].is_erased
= 0;
934 /* lookup flash bank by address */
935 flash_bank_t
*get_flash_bank_by_addr(target_t
*target
, uint32_t addr
)
939 /* cycle through bank list */
940 for (c
= flash_banks
; c
; c
= c
->next
)
943 retval
= c
->driver
->auto_probe(c
);
945 if (retval
!= ERROR_OK
)
947 LOG_ERROR("auto_probe failed %d\n", retval
);
950 /* check whether address belongs to this flash bank */
951 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
954 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
958 /* erase given flash region, selects proper bank according to target and address */
959 static int flash_iterate_address_range(target_t
*target
, uint32_t addr
, uint32_t length
,
960 int (*callback
)(struct flash_bank_s
*bank
, int first
, int last
))
967 if ((c
= get_flash_bank_by_addr(target
, addr
)) == NULL
)
968 return ERROR_FLASH_DST_OUT_OF_BANK
; /* no corresponding bank found */
970 if (c
->size
== 0 || c
->num_sectors
== 0)
972 LOG_ERROR("Bank is invalid");
973 return ERROR_FLASH_BANK_INVALID
;
978 /* special case, erase whole bank when length is zero */
980 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
982 return callback(c
, 0, c
->num_sectors
- 1);
985 /* check whether it fits */
986 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1)
987 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
991 for (i
= 0; i
< c
->num_sectors
; i
++)
993 /* check whether sector overlaps with the given range and is not yet erased */
994 if (addr
< c
->sectors
[i
].offset
+ c
->sectors
[i
].size
&& addr
+ length
> c
->sectors
[i
].offset
&& c
->sectors
[i
].is_erased
!= 1) {
995 /* if first is not set yet then this is the first sector */
998 last
= i
; /* and it is the last one so far in any case */
1002 if (first
== -1 || last
== -1)
1005 return callback(c
, first
, last
);
1010 int flash_erase_address_range(target_t
*target
, uint32_t addr
, uint32_t length
)
1012 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_erase
);
1015 static int flash_driver_unprotect(struct flash_bank_s
*bank
, int first
, int last
)
1017 return flash_driver_protect(bank
, 0, first
, last
);
1020 static int flash_unlock_address_range(target_t
*target
, uint32_t addr
, uint32_t length
)
1022 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_unprotect
);
1026 /* write (optional verify) an image to flash memory of the given target */
1027 static int flash_write_unlock(target_t
*target
, image_t
*image
, uint32_t *written
, int erase
, bool unlock
)
1029 int retval
= ERROR_OK
;
1032 uint32_t section_offset
;
1044 /* assume all sectors need erasing - stops any problems
1045 * when flash_write is called multiple times */
1050 /* allocate padding array */
1051 padding
= malloc(image
->num_sections
* sizeof(padding
));
1053 /* loop until we reach end of the image */
1054 while (section
< image
->num_sections
)
1056 uint32_t buffer_size
;
1060 uint32_t run_address
= image
->sections
[section
].base_address
+ section_offset
;
1061 uint32_t run_size
= image
->sections
[section
].size
- section_offset
;
1064 if (image
->sections
[section
].size
== 0)
1066 LOG_WARNING("empty section %d", section
);
1072 /* find the corresponding flash bank */
1073 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
1075 section
++; /* and skip it */
1080 /* collect consecutive sections which fall into the same bank */
1081 section_first
= section
;
1082 section_last
= section
;
1083 padding
[section
] = 0;
1084 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1)
1085 && (section_last
+ 1 < image
->num_sections
))
1087 if (image
->sections
[section_last
+ 1].base_address
< (run_address
+ run_size
))
1089 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last
+ 1);
1092 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
1093 * attempt to rebuild a consecutive buffer for the flash loader */
1094 pad_bytes
= (image
->sections
[section_last
+ 1].base_address
) - (run_address
+ run_size
);
1095 if ((run_address
+ run_size
+ pad_bytes
) > (c
->base
+ c
->size
))
1097 padding
[section_last
] = pad_bytes
;
1098 run_size
+= image
->sections
[++section_last
].size
;
1099 run_size
+= pad_bytes
;
1100 padding
[section_last
] = 0;
1102 LOG_INFO("Padding image section %d with %d bytes", section_last
-1, pad_bytes
);
1105 /* fit the run into bank constraints */
1106 if (run_address
+ run_size
- 1 > c
->base
+ c
->size
- 1)
1108 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
1109 (int)(c
->base
+ c
->size
- run_address
), (int)(run_size
), (int)(c
->size
));
1110 run_size
= c
->base
+ c
->size
- run_address
;
1113 /* allocate buffer */
1114 buffer
= malloc(run_size
);
1117 /* read sections to the buffer */
1118 while (buffer_size
< run_size
)
1122 size_read
= run_size
- buffer_size
;
1123 if (size_read
> image
->sections
[section
].size
- section_offset
)
1124 size_read
= image
->sections
[section
].size
- section_offset
;
1126 if ((retval
= image_read_section(image
, section
, section_offset
,
1127 size_read
, buffer
+ buffer_size
, &size_read
)) != ERROR_OK
|| size_read
== 0)
1134 /* see if we need to pad the section */
1135 while (padding
[section
]--)
1136 (buffer
+ buffer_size
)[size_read
++] = 0xff;
1138 buffer_size
+= size_read
;
1139 section_offset
+= size_read
;
1141 if (section_offset
>= image
->sections
[section
].size
)
1152 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
1154 if (retval
== ERROR_OK
)
1158 /* calculate and erase sectors */
1159 retval
= flash_erase_address_range(target
, run_address
, run_size
);
1163 if (retval
== ERROR_OK
)
1165 /* write flash sectors */
1166 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
1171 if (retval
!= ERROR_OK
)
1174 return retval
; /* abort operation */
1177 if (written
!= NULL
)
1178 *written
+= run_size
; /* add run size to total written counter */
1186 int flash_write(target_t
*target
, image_t
*image
, uint32_t *written
, int erase
)
1188 return flash_write_unlock(target
, image
, written
, erase
, false);
1191 int default_flash_mem_blank_check(struct flash_bank_s
*bank
)
1193 target_t
*target
= bank
->target
;
1194 uint8_t buffer
[1024];
1195 int buffer_size
= sizeof(buffer
);
1199 if (bank
->target
->state
!= TARGET_HALTED
)
1201 LOG_ERROR("Target not halted");
1202 return ERROR_TARGET_NOT_HALTED
;
1205 for (i
= 0; i
< bank
->num_sectors
; i
++)
1208 bank
->sectors
[i
].is_erased
= 1;
1210 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
1214 chunk
= buffer_size
;
1215 if (chunk
> (j
- bank
->sectors
[i
].size
))
1217 chunk
= (j
- bank
->sectors
[i
].size
);
1220 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
1221 if (retval
!= ERROR_OK
)
1224 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
1226 if (buffer
[nBytes
] != 0xFF)
1228 bank
->sectors
[i
].is_erased
= 0;
1238 int default_flash_blank_check(struct flash_bank_s
*bank
)
1240 target_t
*target
= bank
->target
;
1246 if (bank
->target
->state
!= TARGET_HALTED
)
1248 LOG_ERROR("Target not halted");
1249 return ERROR_TARGET_NOT_HALTED
;
1252 for (i
= 0; i
< bank
->num_sectors
; i
++)
1254 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
1255 uint32_t size
= bank
->sectors
[i
].size
;
1257 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
1263 bank
->sectors
[i
].is_erased
= 1;
1265 bank
->sectors
[i
].is_erased
= 0;
1271 LOG_USER("Running slow fallback erase check - add working memory");
1272 return default_flash_mem_blank_check(bank
);
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)