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
)
509 target_t
*target
= get_current_target(cmd_ctx
);
512 return ERROR_COMMAND_SYNTAX_ERROR
;
514 COMMAND_PARSE_NUMBER(int, args
[0], address
);
515 COMMAND_PARSE_NUMBER(int, args
[1], length
);
518 command_print(cmd_ctx
, "Length must be >0");
519 return ERROR_COMMAND_SYNTAX_ERROR
;
522 p
= get_flash_bank_by_addr(target
, address
);
528 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
531 duration_start_measure(&duration
);
533 if ((retval
= flash_erase_address_range(target
, address
, length
)) == ERROR_OK
)
535 if ((retval
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
539 command_print(cmd_ctx
, "erased address 0x%8.8x length %i in %s", address
, length
, duration_text
);
546 static int handle_flash_protect_check_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
549 return ERROR_COMMAND_SYNTAX_ERROR
;
552 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
553 if (ERROR_OK
!= retval
)
556 if ((retval
= p
->driver
->protect_check(p
)) == ERROR_OK
)
558 command_print(cmd_ctx
, "successfully checked protect state");
560 else if (retval
== ERROR_FLASH_OPERATION_FAILED
)
562 command_print(cmd_ctx
, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32
, args
[0], p
->base
);
566 command_print(cmd_ctx
, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32
, args
[0], p
->base
);
572 static int flash_check_sector_parameters(struct command_context_s
*cmd_ctx
,
573 uint32_t first
, uint32_t last
, uint32_t num_sectors
)
575 if (!(first
<= last
)) {
576 command_print(cmd_ctx
, "ERROR: "
577 "first sector must be <= last sector");
581 if (!(last
<= (num_sectors
- 1))) {
582 command_print(cmd_ctx
, "ERROR: last sector must be <= %d",
583 (int) num_sectors
- 1);
590 static int handle_flash_erase_command(struct command_context_s
*cmd_ctx
,
591 char *cmd
, char **args
, int argc
)
594 return ERROR_COMMAND_SYNTAX_ERROR
;
600 COMMAND_PARSE_NUMBER(u32
, args
[0], bank_nr
);
601 flash_bank_t
*p
= get_flash_bank_by_num(bank_nr
);
605 COMMAND_PARSE_NUMBER(u32
, args
[1], first
);
606 if (strcmp(args
[2], "last") == 0)
607 last
= p
->num_sectors
- 1;
609 COMMAND_PARSE_NUMBER(u32
, args
[2], last
);
612 if ((retval
= flash_check_sector_parameters(cmd_ctx
,
613 first
, last
, p
->num_sectors
)) != ERROR_OK
)
618 duration_start_measure(&duration
);
620 if ((retval
= flash_driver_erase(p
, first
, last
)) == ERROR_OK
) {
621 if ((retval
= duration_stop_measure(&duration
,
622 &duration_text
)) != ERROR_OK
)
624 command_print(cmd_ctx
, "erased sectors %i through %i "
625 "on flash bank %i in %s",
626 (int) first
, (int) last
, (int) bank_nr
,
634 static int handle_flash_protect_command(struct command_context_s
*cmd_ctx
,
635 char *cmd
, char **args
, int argc
)
638 return ERROR_COMMAND_SYNTAX_ERROR
;
645 COMMAND_PARSE_NUMBER(u32
, args
[0], bank_nr
);
646 flash_bank_t
*p
= get_flash_bank_by_num(bank_nr
);
650 COMMAND_PARSE_NUMBER(u32
, args
[1], first
);
651 if (strcmp(args
[2], "last") == 0)
652 last
= p
->num_sectors
- 1;
654 COMMAND_PARSE_NUMBER(u32
, args
[2], last
);
656 if (strcmp(args
[3], "on") == 0)
658 else if (strcmp(args
[3], "off") == 0)
661 return ERROR_COMMAND_SYNTAX_ERROR
;
664 if ((retval
= flash_check_sector_parameters(cmd_ctx
,
665 first
, last
, p
->num_sectors
)) != ERROR_OK
)
668 retval
= flash_driver_protect(p
, set
, first
, last
);
669 if (retval
== ERROR_OK
) {
670 command_print(cmd_ctx
, "%s protection for sectors %i "
671 "through %i on flash bank %i",
672 (set
) ? "set" : "cleared", (int) first
,
673 (int) last
, (int) bank_nr
);
679 static int handle_flash_write_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
681 target_t
*target
= get_current_target(cmd_ctx
);
689 int retval
, retvaltemp
;
693 return ERROR_COMMAND_SYNTAX_ERROR
;
696 /* flash auto-erase is disabled by default*/
698 bool auto_unlock
= false;
702 if (strcmp(args
[0], "erase") == 0)
707 command_print(cmd_ctx
, "auto erase enabled");
708 } else if (strcmp(args
[0], "unlock") == 0)
713 command_print(cmd_ctx
, "auto unlock enabled");
722 return ERROR_COMMAND_SYNTAX_ERROR
;
727 LOG_ERROR("no target selected");
731 duration_start_measure(&duration
);
735 image
.base_address_set
= 1;
736 COMMAND_PARSE_NUMBER(int, args
[1], image
.base_address
);
740 image
.base_address_set
= 0;
741 image
.base_address
= 0x0;
744 image
.start_address_set
= 0;
746 retval
= image_open(&image
, args
[0], (argc
== 3) ? args
[2] : NULL
);
747 if (retval
!= ERROR_OK
)
752 retval
= flash_write_unlock(target
, &image
, &written
, auto_erase
, auto_unlock
);
753 if (retval
!= ERROR_OK
)
759 if ((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
767 speed
= written
/ 1024.0;
768 speed
/= ((float)duration
.duration
.tv_sec
769 + ((float)duration
.duration
.tv_usec
/ 1000000.0));
770 command_print(cmd_ctx
,
771 "wrote %" PRIu32
" byte from file %s in %s (%f kb/s)",
772 written
, args
[0], duration_text
, speed
);
781 static int handle_flash_fill_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
783 int err
= ERROR_OK
, retval
;
788 uint8_t readback
[1024];
790 uint32_t cur_size
= 0;
791 uint32_t chunk_count
;
794 target_t
*target
= get_current_target(cmd_ctx
);
799 return ERROR_COMMAND_SYNTAX_ERROR
;
801 COMMAND_PARSE_NUMBER(u32
, args
[0], address
);
802 COMMAND_PARSE_NUMBER(u32
, args
[1], pattern
);
803 COMMAND_PARSE_NUMBER(u32
, args
[2], count
);
820 return ERROR_COMMAND_SYNTAX_ERROR
;
823 chunk_count
= MIN(count
, (1024 / wordsize
));
827 for (i
= 0; i
< chunk_count
; i
++)
829 target_buffer_set_u32(target
, chunk
+ i
* wordsize
, pattern
);
833 for (i
= 0; i
< chunk_count
; i
++)
835 target_buffer_set_u16(target
, chunk
+ i
* wordsize
, pattern
);
839 memset(chunk
, pattern
, chunk_count
);
842 LOG_ERROR("BUG: can't happen");
846 duration_start_measure(&duration
);
848 for (wrote
= 0; wrote
< (count
*wordsize
); wrote
+= cur_size
)
850 cur_size
= MIN((count
*wordsize
- wrote
), sizeof(chunk
));
852 bank
= get_flash_bank_by_addr(target
, address
);
857 err
= flash_driver_write(bank
, chunk
, address
- bank
->base
+ wrote
, cur_size
);
861 err
= target_read_buffer(target
, address
+ wrote
, cur_size
, readback
);
866 for (i
= 0; i
< cur_size
; i
++)
868 if (readback
[i
]!=chunk
[i
])
870 LOG_ERROR("Verfication error address 0x%08" PRIx32
", read back 0x%02x, expected 0x%02x",
871 address
+ wrote
+ i
, readback
[i
], chunk
[i
]);
878 if ((retval
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
885 speed
= wrote
/ 1024.0;
886 speed
/= ((float)duration
.duration
.tv_sec
887 + ((float)duration
.duration
.tv_usec
/ 1000000.0));
888 command_print(cmd_ctx
,
889 "wrote %" PRIu32
" bytes to 0x%8.8" PRIx32
" in %s (%f kb/s)",
890 wrote
, address
, duration_text
, speed
);
896 static int handle_flash_write_bank_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
909 return ERROR_COMMAND_SYNTAX_ERROR
;
911 duration_start_measure(&duration
);
914 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
915 if (ERROR_OK
!= retval
)
918 COMMAND_PARSE_NUMBER(u32
, args
[2], offset
);
920 if (fileio_open(&fileio
, args
[1], FILEIO_READ
, FILEIO_BINARY
) != ERROR_OK
)
925 buffer
= malloc(fileio
.size
);
926 if (fileio_read(&fileio
, fileio
.size
, buffer
, &buf_cnt
) != ERROR_OK
)
929 fileio_close(&fileio
);
933 retval
= flash_driver_write(p
, buffer
, offset
, buf_cnt
);
939 if ((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
941 fileio_close(&fileio
);
944 if (retval
== ERROR_OK
)
946 float elapsed
= (float)duration
.duration
.tv_sec
;
947 elapsed
+= (float)duration
.duration
.tv_usec
/ 1000000.0;
948 float speed
= (float)fileio
.size
/ elapsed
;
949 command_print(cmd_ctx
,
950 "wrote %lld byte from file %s to flash bank %u "
951 "at offset 0x%8.8" PRIx32
" in %s (%f kb/s)",
952 fileio
.size
, args
[1], p
->bank_number
, offset
,
953 duration_text
, speed
/ 1024);
957 fileio_close(&fileio
);
962 void flash_set_dirty(void)
967 /* set all flash to require erasing */
968 for (c
= flash_banks
; c
; c
= c
->next
)
970 for (i
= 0; i
< c
->num_sectors
; i
++)
972 c
->sectors
[i
].is_erased
= 0;
977 /* lookup flash bank by address */
978 flash_bank_t
*get_flash_bank_by_addr(target_t
*target
, uint32_t addr
)
982 /* cycle through bank list */
983 for (c
= flash_banks
; c
; c
= c
->next
)
986 retval
= c
->driver
->auto_probe(c
);
988 if (retval
!= ERROR_OK
)
990 LOG_ERROR("auto_probe failed %d\n", retval
);
993 /* check whether address belongs to this flash bank */
994 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
997 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
1001 /* erase given flash region, selects proper bank according to target and address */
1002 static int flash_iterate_address_range(target_t
*target
, uint32_t addr
, uint32_t length
,
1003 int (*callback
)(struct flash_bank_s
*bank
, int first
, int last
))
1010 if ((c
= get_flash_bank_by_addr(target
, addr
)) == NULL
)
1011 return ERROR_FLASH_DST_OUT_OF_BANK
; /* no corresponding bank found */
1013 if (c
->size
== 0 || c
->num_sectors
== 0)
1015 LOG_ERROR("Bank is invalid");
1016 return ERROR_FLASH_BANK_INVALID
;
1021 /* special case, erase whole bank when length is zero */
1022 if (addr
!= c
->base
)
1023 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
1025 return callback(c
, 0, c
->num_sectors
- 1);
1028 /* check whether it fits */
1029 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1)
1030 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
1034 for (i
= 0; i
< c
->num_sectors
; i
++)
1036 /* check whether sector overlaps with the given range and is not yet erased */
1037 if (addr
< c
->sectors
[i
].offset
+ c
->sectors
[i
].size
&& addr
+ length
> c
->sectors
[i
].offset
&& c
->sectors
[i
].is_erased
!= 1) {
1038 /* if first is not set yet then this is the first sector */
1041 last
= i
; /* and it is the last one so far in any case */
1045 if (first
== -1 || last
== -1)
1048 return callback(c
, first
, last
);
1053 int flash_erase_address_range(target_t
*target
, uint32_t addr
, uint32_t length
)
1055 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_erase
);
1058 static int flash_driver_unprotect(struct flash_bank_s
*bank
, int first
, int last
)
1060 return flash_driver_protect(bank
, 0, first
, last
);
1063 static int flash_unlock_address_range(target_t
*target
, uint32_t addr
, uint32_t length
)
1065 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_unprotect
);
1069 /* write (optional verify) an image to flash memory of the given target */
1070 static int flash_write_unlock(target_t
*target
, image_t
*image
, uint32_t *written
, int erase
, bool unlock
)
1072 int retval
= ERROR_OK
;
1075 uint32_t section_offset
;
1087 /* assume all sectors need erasing - stops any problems
1088 * when flash_write is called multiple times */
1093 /* allocate padding array */
1094 padding
= malloc(image
->num_sections
* sizeof(padding
));
1096 /* loop until we reach end of the image */
1097 while (section
< image
->num_sections
)
1099 uint32_t buffer_size
;
1103 uint32_t run_address
= image
->sections
[section
].base_address
+ section_offset
;
1104 uint32_t run_size
= image
->sections
[section
].size
- section_offset
;
1107 if (image
->sections
[section
].size
== 0)
1109 LOG_WARNING("empty section %d", section
);
1115 /* find the corresponding flash bank */
1116 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
1118 section
++; /* and skip it */
1123 /* collect consecutive sections which fall into the same bank */
1124 section_first
= section
;
1125 section_last
= section
;
1126 padding
[section
] = 0;
1127 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1)
1128 && (section_last
+ 1 < image
->num_sections
))
1130 if (image
->sections
[section_last
+ 1].base_address
< (run_address
+ run_size
))
1132 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last
+ 1);
1135 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
1136 * attempt to rebuild a consecutive buffer for the flash loader */
1137 pad_bytes
= (image
->sections
[section_last
+ 1].base_address
) - (run_address
+ run_size
);
1138 if ((run_address
+ run_size
+ pad_bytes
) > (c
->base
+ c
->size
))
1140 padding
[section_last
] = pad_bytes
;
1141 run_size
+= image
->sections
[++section_last
].size
;
1142 run_size
+= pad_bytes
;
1143 padding
[section_last
] = 0;
1145 LOG_INFO("Padding image section %d with %d bytes", section_last
-1, pad_bytes
);
1148 /* fit the run into bank constraints */
1149 if (run_address
+ run_size
- 1 > c
->base
+ c
->size
- 1)
1151 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
1152 (int)(c
->base
+ c
->size
- run_address
), (int)(run_size
), (int)(c
->size
));
1153 run_size
= c
->base
+ c
->size
- run_address
;
1156 /* allocate buffer */
1157 buffer
= malloc(run_size
);
1160 /* read sections to the buffer */
1161 while (buffer_size
< run_size
)
1165 size_read
= run_size
- buffer_size
;
1166 if (size_read
> image
->sections
[section
].size
- section_offset
)
1167 size_read
= image
->sections
[section
].size
- section_offset
;
1169 if ((retval
= image_read_section(image
, section
, section_offset
,
1170 size_read
, buffer
+ buffer_size
, &size_read
)) != ERROR_OK
|| size_read
== 0)
1177 /* see if we need to pad the section */
1178 while (padding
[section
]--)
1179 (buffer
+ buffer_size
)[size_read
++] = 0xff;
1181 buffer_size
+= size_read
;
1182 section_offset
+= size_read
;
1184 if (section_offset
>= image
->sections
[section
].size
)
1195 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
1197 if (retval
== ERROR_OK
)
1201 /* calculate and erase sectors */
1202 retval
= flash_erase_address_range(target
, run_address
, run_size
);
1206 if (retval
== ERROR_OK
)
1208 /* write flash sectors */
1209 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
1214 if (retval
!= ERROR_OK
)
1217 return retval
; /* abort operation */
1220 if (written
!= NULL
)
1221 *written
+= run_size
; /* add run size to total written counter */
1229 int flash_write(target_t
*target
, image_t
*image
, uint32_t *written
, int erase
)
1231 return flash_write_unlock(target
, image
, written
, erase
, false);
1234 int default_flash_mem_blank_check(struct flash_bank_s
*bank
)
1236 target_t
*target
= bank
->target
;
1237 uint8_t buffer
[1024];
1238 int buffer_size
= sizeof(buffer
);
1242 if (bank
->target
->state
!= TARGET_HALTED
)
1244 LOG_ERROR("Target not halted");
1245 return ERROR_TARGET_NOT_HALTED
;
1248 for (i
= 0; i
< bank
->num_sectors
; i
++)
1251 bank
->sectors
[i
].is_erased
= 1;
1253 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
1257 chunk
= buffer_size
;
1258 if (chunk
> (j
- bank
->sectors
[i
].size
))
1260 chunk
= (j
- bank
->sectors
[i
].size
);
1263 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
1264 if (retval
!= ERROR_OK
)
1267 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
1269 if (buffer
[nBytes
] != 0xFF)
1271 bank
->sectors
[i
].is_erased
= 0;
1281 int default_flash_blank_check(struct flash_bank_s
*bank
)
1283 target_t
*target
= bank
->target
;
1289 if (bank
->target
->state
!= TARGET_HALTED
)
1291 LOG_ERROR("Target not halted");
1292 return ERROR_TARGET_NOT_HALTED
;
1295 for (i
= 0; i
< bank
->num_sectors
; i
++)
1297 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
1298 uint32_t size
= bank
->sectors
[i
].size
;
1300 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
1306 bank
->sectors
[i
].is_erased
= 1;
1308 bank
->sectors
[i
].is_erased
= 0;
1314 LOG_USER("Running slow fallback erase check - add working memory");
1315 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)