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 ***************************************************************************/
33 #include "time_support.h"
35 static int flash_write_unlock(struct target
*target
, struct image
*image
, uint32_t *written
, int erase
, bool unlock
);
39 extern struct flash_driver lpc2000_flash
;
40 extern struct flash_driver lpc288x_flash
;
41 extern struct flash_driver lpc2900_flash
;
42 extern struct flash_driver cfi_flash
;
43 extern struct flash_driver at91sam3_flash
;
44 extern struct flash_driver at91sam7_flash
;
45 extern struct flash_driver str7x_flash
;
46 extern struct flash_driver str9x_flash
;
47 extern struct flash_driver aduc702x_flash
;
48 extern struct flash_driver stellaris_flash
;
49 extern struct flash_driver str9xpec_flash
;
50 extern struct flash_driver stm32x_flash
;
51 extern struct flash_driver tms470_flash
;
52 extern struct flash_driver ecosflash_flash
;
53 extern struct flash_driver ocl_flash
;
54 extern struct flash_driver pic32mx_flash
;
55 extern struct flash_driver avr_flash
;
56 extern struct flash_driver faux_flash
;
58 struct flash_driver
*flash_drivers
[] = {
80 struct flash_bank
*flash_banks
;
81 static struct command
*flash_cmd
;
83 /* wafer thin wrapper for invoking the flash driver */
84 static int flash_driver_write(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
88 retval
= bank
->driver
->write(bank
, buffer
, offset
, count
);
89 if (retval
!= ERROR_OK
)
91 LOG_ERROR("error writing to flash at address 0x%08" PRIx32
" at offset 0x%8.8" PRIx32
" (%d)",
92 bank
->base
, offset
, retval
);
98 static int flash_driver_erase(struct flash_bank
*bank
, int first
, int last
)
102 retval
= bank
->driver
->erase(bank
, first
, last
);
103 if (retval
!= ERROR_OK
)
105 LOG_ERROR("failed erasing sectors %d to %d (%d)", first
, last
, retval
);
111 int flash_driver_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
115 retval
= bank
->driver
->protect(bank
, set
, first
, last
);
116 if (retval
!= ERROR_OK
)
118 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first
, last
, retval
);
124 static int jim_flash_banks(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
126 struct flash_bank
*p
;
129 Jim_WrongNumArgs(interp
, 1, argv
, "no arguments to flash_banks command");
133 Jim_Obj
*list
= Jim_NewListObj(interp
, NULL
, 0);
134 for (p
= flash_banks
; p
; p
= p
->next
)
136 Jim_Obj
*elem
= Jim_NewListObj(interp
, NULL
, 0);
138 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, "name", -1));
139 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, p
->driver
->name
, -1));
140 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, "base", -1));
141 Jim_ListAppendElement(interp
, elem
, Jim_NewIntObj(interp
, p
->base
));
142 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, "size", -1));
143 Jim_ListAppendElement(interp
, elem
, Jim_NewIntObj(interp
, p
->size
));
144 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, "bus_width", -1));
145 Jim_ListAppendElement(interp
, elem
, Jim_NewIntObj(interp
, p
->bus_width
));
146 Jim_ListAppendElement(interp
, elem
, Jim_NewStringObj(interp
, "chip_width", -1));
147 Jim_ListAppendElement(interp
, elem
, Jim_NewIntObj(interp
, p
->chip_width
));
149 Jim_ListAppendElement(interp
, list
, elem
);
152 Jim_SetResult(interp
, list
);
157 struct flash_bank
*get_flash_bank_by_num_noprobe(int num
)
159 struct flash_bank
*p
;
162 for (p
= flash_banks
; p
; p
= p
->next
)
169 LOG_ERROR("flash bank %d does not exist", num
);
173 int flash_get_bank_count(void)
175 struct flash_bank
*p
;
177 for (p
= flash_banks
; p
; p
= p
->next
)
184 struct flash_bank
*get_flash_bank_by_name(const char *name
)
186 unsigned requested
= get_flash_name_index(name
);
189 struct flash_bank
*bank
;
190 for (bank
= flash_banks
; NULL
!= bank
; bank
= bank
->next
)
192 if (strcmp(bank
->name
, name
) == 0)
194 if (!flash_driver_name_matches(bank
->driver
->name
, name
))
196 if (++found
< requested
)
203 struct flash_bank
*get_flash_bank_by_num(int num
)
205 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(num
);
211 retval
= p
->driver
->auto_probe(p
);
213 if (retval
!= ERROR_OK
)
215 LOG_ERROR("auto_probe failed %d\n", retval
);
221 COMMAND_HELPER(flash_command_get_bank
, unsigned name_index
,
222 struct flash_bank
**bank
)
224 const char *name
= CMD_ARGV
[name_index
];
225 *bank
= get_flash_bank_by_name(name
);
230 COMMAND_PARSE_NUMBER(uint
, name
, bank_num
);
232 *bank
= get_flash_bank_by_num(bank_num
);
235 command_print(CMD_CTX
, "flash bank '%s' not found", name
);
236 return ERROR_INVALID_ARGUMENTS
;
242 COMMAND_HANDLER(handle_flash_bank_command
)
246 LOG_ERROR("usage: flash bank <name> <driver> "
247 "<base> <size> <chip_width> <bus_width>");
248 return ERROR_COMMAND_SYNTAX_ERROR
;
250 // save bank name and advance arguments for compatibility
251 const char *bank_name
= *CMD_ARGV
++;
254 struct target
*target
;
255 if ((target
= get_target(CMD_ARGV
[5])) == NULL
)
257 LOG_ERROR("target '%s' not defined", CMD_ARGV
[5]);
261 const char *driver_name
= CMD_ARGV
[0];
262 for (unsigned i
= 0; flash_drivers
[i
]; i
++)
264 if (strcmp(driver_name
, flash_drivers
[i
]->name
) != 0)
267 struct flash_bank
*p
, *c
;
269 /* register flash specific commands */
270 if (flash_drivers
[i
]->register_commands(CMD_CTX
) != ERROR_OK
)
272 LOG_ERROR("couldn't register '%s' commands", driver_name
);
276 c
= malloc(sizeof(struct flash_bank
));
277 c
->name
= strdup(bank_name
);
279 c
->driver
= flash_drivers
[i
];
280 c
->driver_priv
= NULL
;
281 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], c
->base
);
282 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], c
->size
);
283 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[3], c
->chip_width
);
284 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[4], c
->bus_width
);
290 retval
= CALL_COMMAND_HANDLER(flash_drivers
[i
]->flash_bank_command
, c
);
291 if (ERROR_OK
!= retval
)
293 LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32
,
294 driver_name
, c
->base
);
299 /* put flash bank in linked list */
303 /* find last flash bank */
304 for (p
= flash_banks
; p
&& p
->next
; p
= p
->next
) bank_num
++;
307 c
->bank_number
= bank_num
+ 1;
318 /* no matching flash driver found */
319 LOG_ERROR("flash driver '%s' not found", driver_name
);
323 COMMAND_HANDLER(handle_flash_info_command
)
325 struct flash_bank
*p
;
331 return ERROR_COMMAND_SYNTAX_ERROR
;
334 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], bank_nr
);
336 for (p
= flash_banks
; p
; p
= p
->next
, i
++)
343 /* attempt auto probe */
344 if ((retval
= p
->driver
->auto_probe(p
)) != ERROR_OK
)
347 command_print(CMD_CTX
,
348 "#%" PRIi32
" : %s at 0x%8.8" PRIx32
", size 0x%8.8" PRIx32
", buswidth %i, chipwidth %i",
355 for (j
= 0; j
< p
->num_sectors
; j
++)
359 if (p
->sectors
[j
].is_protected
== 0)
360 protect_state
= "not protected";
361 else if (p
->sectors
[j
].is_protected
== 1)
362 protect_state
= "protected";
364 protect_state
= "protection state unknown";
366 command_print(CMD_CTX
,
367 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
369 p
->sectors
[j
].offset
,
371 p
->sectors
[j
].size
>> 10,
375 *buf
= '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
376 retval
= p
->driver
->info(p
, buf
, sizeof(buf
));
377 command_print(CMD_CTX
, "%s", buf
);
378 if (retval
!= ERROR_OK
)
379 LOG_ERROR("error retrieving flash info (%d)", retval
);
385 COMMAND_HANDLER(handle_flash_probe_command
)
391 return ERROR_COMMAND_SYNTAX_ERROR
;
395 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], bank_nr
);
396 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(bank_nr
);
399 if ((retval
= p
->driver
->probe(p
)) == ERROR_OK
)
401 command_print(CMD_CTX
, "flash '%s' found at 0x%8.8" PRIx32
, p
->driver
->name
, p
->base
);
403 else if (retval
== ERROR_FLASH_BANK_INVALID
)
405 command_print(CMD_CTX
, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32
,
406 CMD_ARGV
[0], p
->base
);
410 command_print(CMD_CTX
, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32
,
411 CMD_ARGV
[0], p
->base
);
416 command_print(CMD_CTX
, "flash bank '#%s' is out of bounds", CMD_ARGV
[0]);
422 COMMAND_HANDLER(handle_flash_erase_check_command
)
426 return ERROR_COMMAND_SYNTAX_ERROR
;
429 struct flash_bank
*p
;
430 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
431 if (ERROR_OK
!= retval
)
435 if ((retval
= p
->driver
->erase_check(p
)) == ERROR_OK
)
437 command_print(CMD_CTX
, "successfully checked erase state");
441 command_print(CMD_CTX
, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32
,
442 CMD_ARGV
[0], p
->base
);
445 for (j
= 0; j
< p
->num_sectors
; j
++)
449 if (p
->sectors
[j
].is_erased
== 0)
450 erase_state
= "not erased";
451 else if (p
->sectors
[j
].is_erased
== 1)
452 erase_state
= "erased";
454 erase_state
= "erase state unknown";
456 command_print(CMD_CTX
,
457 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
459 p
->sectors
[j
].offset
,
461 p
->sectors
[j
].size
>> 10,
468 COMMAND_HANDLER(handle_flash_erase_address_command
)
470 struct flash_bank
*p
;
475 struct target
*target
= get_current_target(CMD_CTX
);
478 return ERROR_COMMAND_SYNTAX_ERROR
;
480 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], address
);
481 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], length
);
484 command_print(CMD_CTX
, "Length must be >0");
485 return ERROR_COMMAND_SYNTAX_ERROR
;
488 p
= get_flash_bank_by_addr(target
, address
);
494 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
497 struct duration bench
;
498 duration_start(&bench
);
500 retval
= flash_erase_address_range(target
, address
, length
);
502 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
504 command_print(CMD_CTX
, "erased address 0x%8.8x (length %i)"
505 " in %fs (%0.3f kb/s)", address
, length
,
506 duration_elapsed(&bench
), duration_kbps(&bench
, length
));
512 COMMAND_HANDLER(handle_flash_protect_check_command
)
515 return ERROR_COMMAND_SYNTAX_ERROR
;
517 struct flash_bank
*p
;
518 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
519 if (ERROR_OK
!= retval
)
522 if ((retval
= p
->driver
->protect_check(p
)) == ERROR_OK
)
524 command_print(CMD_CTX
, "successfully checked protect state");
526 else if (retval
== ERROR_FLASH_OPERATION_FAILED
)
528 command_print(CMD_CTX
, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32
, CMD_ARGV
[0], p
->base
);
532 command_print(CMD_CTX
, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32
, CMD_ARGV
[0], p
->base
);
538 static int flash_check_sector_parameters(struct command_context
*cmd_ctx
,
539 uint32_t first
, uint32_t last
, uint32_t num_sectors
)
541 if (!(first
<= last
)) {
542 command_print(cmd_ctx
, "ERROR: "
543 "first sector must be <= last sector");
547 if (!(last
<= (num_sectors
- 1))) {
548 command_print(cmd_ctx
, "ERROR: last sector must be <= %d",
549 (int) num_sectors
- 1);
556 COMMAND_HANDLER(handle_flash_erase_command
)
559 return ERROR_COMMAND_SYNTAX_ERROR
;
565 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], bank_nr
);
566 struct flash_bank
*p
= get_flash_bank_by_num(bank_nr
);
570 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], first
);
571 if (strcmp(CMD_ARGV
[2], "last") == 0)
572 last
= p
->num_sectors
- 1;
574 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], last
);
577 if ((retval
= flash_check_sector_parameters(CMD_CTX
,
578 first
, last
, p
->num_sectors
)) != ERROR_OK
)
581 struct duration bench
;
582 duration_start(&bench
);
584 retval
= flash_driver_erase(p
, first
, last
);
586 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
588 command_print(CMD_CTX
, "erased sectors %" PRIu32
" "
589 "through %" PRIu32
" on flash bank %" PRIu32
" "
590 "in %fs", first
, last
, bank_nr
, duration_elapsed(&bench
));
596 COMMAND_HANDLER(handle_flash_protect_command
)
599 return ERROR_COMMAND_SYNTAX_ERROR
;
605 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], bank_nr
);
606 struct flash_bank
*p
= get_flash_bank_by_num(bank_nr
);
610 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], first
);
611 if (strcmp(CMD_ARGV
[2], "last") == 0)
612 last
= p
->num_sectors
- 1;
614 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], last
);
617 COMMAND_PARSE_ON_OFF(CMD_ARGV
[3], set
);
620 if ((retval
= flash_check_sector_parameters(CMD_CTX
,
621 first
, last
, p
->num_sectors
)) != ERROR_OK
)
624 retval
= flash_driver_protect(p
, set
, first
, last
);
625 if (retval
== ERROR_OK
) {
626 command_print(CMD_CTX
, "%s protection for sectors %i "
627 "through %i on flash bank %i",
628 (set
) ? "set" : "cleared", (int) first
,
629 (int) last
, (int) bank_nr
);
635 COMMAND_HANDLER(handle_flash_write_image_command
)
637 struct target
*target
= get_current_target(CMD_CTX
);
646 return ERROR_COMMAND_SYNTAX_ERROR
;
649 /* flash auto-erase is disabled by default*/
651 bool auto_unlock
= false;
655 if (strcmp(CMD_ARGV
[0], "erase") == 0)
660 command_print(CMD_CTX
, "auto erase enabled");
661 } else if (strcmp(CMD_ARGV
[0], "unlock") == 0)
666 command_print(CMD_CTX
, "auto unlock enabled");
675 return ERROR_COMMAND_SYNTAX_ERROR
;
680 LOG_ERROR("no target selected");
684 struct duration bench
;
685 duration_start(&bench
);
689 image
.base_address_set
= 1;
690 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], image
.base_address
);
694 image
.base_address_set
= 0;
695 image
.base_address
= 0x0;
698 image
.start_address_set
= 0;
700 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
701 if (retval
!= ERROR_OK
)
706 retval
= flash_write_unlock(target
, &image
, &written
, auto_erase
, auto_unlock
);
707 if (retval
!= ERROR_OK
)
713 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
715 command_print(CMD_CTX
, "wrote %" PRIu32
" byte from file %s "
716 "in %fs (%0.3f kb/s)", written
, CMD_ARGV
[0],
717 duration_elapsed(&bench
), duration_kbps(&bench
, written
));
725 COMMAND_HANDLER(handle_flash_fill_command
)
732 uint8_t readback
[1024];
734 uint32_t cur_size
= 0;
735 uint32_t chunk_count
;
736 struct target
*target
= get_current_target(CMD_CTX
);
741 return ERROR_COMMAND_SYNTAX_ERROR
;
743 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
744 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], pattern
);
745 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], count
);
762 return ERROR_COMMAND_SYNTAX_ERROR
;
765 chunk_count
= MIN(count
, (1024 / wordsize
));
769 for (i
= 0; i
< chunk_count
; i
++)
771 target_buffer_set_u32(target
, chunk
+ i
* wordsize
, pattern
);
775 for (i
= 0; i
< chunk_count
; i
++)
777 target_buffer_set_u16(target
, chunk
+ i
* wordsize
, pattern
);
781 memset(chunk
, pattern
, chunk_count
);
784 LOG_ERROR("BUG: can't happen");
788 struct duration bench
;
789 duration_start(&bench
);
791 for (wrote
= 0; wrote
< (count
*wordsize
); wrote
+= cur_size
)
793 cur_size
= MIN((count
*wordsize
- wrote
), sizeof(chunk
));
794 struct flash_bank
*bank
;
795 bank
= get_flash_bank_by_addr(target
, address
);
800 err
= flash_driver_write(bank
, chunk
, address
- bank
->base
+ wrote
, cur_size
);
804 err
= target_read_buffer(target
, address
+ wrote
, cur_size
, readback
);
809 for (i
= 0; i
< cur_size
; i
++)
811 if (readback
[i
]!=chunk
[i
])
813 LOG_ERROR("Verfication error address 0x%08" PRIx32
", read back 0x%02x, expected 0x%02x",
814 address
+ wrote
+ i
, readback
[i
], chunk
[i
]);
820 if (duration_measure(&bench
) == ERROR_OK
)
822 command_print(CMD_CTX
, "wrote %" PRIu32
" bytes to 0x%8.8" PRIx32
823 " in %fs (%0.3f kb/s)", wrote
, address
,
824 duration_elapsed(&bench
), duration_kbps(&bench
, wrote
));
829 COMMAND_HANDLER(handle_flash_write_bank_command
)
833 struct fileio fileio
;
836 return ERROR_COMMAND_SYNTAX_ERROR
;
838 struct duration bench
;
839 duration_start(&bench
);
841 struct flash_bank
*p
;
842 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
843 if (ERROR_OK
!= retval
)
846 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], offset
);
848 if (fileio_open(&fileio
, CMD_ARGV
[1], FILEIO_READ
, FILEIO_BINARY
) != ERROR_OK
)
853 buffer
= malloc(fileio
.size
);
855 if (fileio_read(&fileio
, fileio
.size
, buffer
, &buf_cnt
) != ERROR_OK
)
858 fileio_close(&fileio
);
862 retval
= flash_driver_write(p
, buffer
, offset
, buf_cnt
);
867 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
869 command_print(CMD_CTX
, "wrote %zu byte from file %s to flash bank %u"
870 " at offset 0x%8.8" PRIx32
" in %fs (%0.3f kb/s)",
871 fileio
.size
, CMD_ARGV
[1], p
->bank_number
, offset
,
872 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
875 fileio_close(&fileio
);
880 void flash_set_dirty(void)
882 struct flash_bank
*c
;
885 /* set all flash to require erasing */
886 for (c
= flash_banks
; c
; c
= c
->next
)
888 for (i
= 0; i
< c
->num_sectors
; i
++)
890 c
->sectors
[i
].is_erased
= 0;
895 /* lookup flash bank by address */
896 struct flash_bank
*get_flash_bank_by_addr(struct target
*target
, uint32_t addr
)
898 struct flash_bank
*c
;
900 /* cycle through bank list */
901 for (c
= flash_banks
; c
; c
= c
->next
)
904 retval
= c
->driver
->auto_probe(c
);
906 if (retval
!= ERROR_OK
)
908 LOG_ERROR("auto_probe failed %d\n", retval
);
911 /* check whether address belongs to this flash bank */
912 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
915 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
919 /* erase given flash region, selects proper bank according to target and address */
920 static int flash_iterate_address_range(struct target
*target
, uint32_t addr
, uint32_t length
,
921 int (*callback
)(struct flash_bank
*bank
, int first
, int last
))
923 struct flash_bank
*c
;
928 if ((c
= get_flash_bank_by_addr(target
, addr
)) == NULL
)
929 return ERROR_FLASH_DST_OUT_OF_BANK
; /* no corresponding bank found */
931 if (c
->size
== 0 || c
->num_sectors
== 0)
933 LOG_ERROR("Bank is invalid");
934 return ERROR_FLASH_BANK_INVALID
;
939 /* special case, erase whole bank when length is zero */
941 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
943 return callback(c
, 0, c
->num_sectors
- 1);
946 /* check whether it fits */
947 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1)
948 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
952 for (i
= 0; i
< c
->num_sectors
; i
++)
954 /* check whether sector overlaps with the given range and is not yet erased */
955 if (addr
< c
->sectors
[i
].offset
+ c
->sectors
[i
].size
&& addr
+ length
> c
->sectors
[i
].offset
&& c
->sectors
[i
].is_erased
!= 1) {
956 /* if first is not set yet then this is the first sector */
959 last
= i
; /* and it is the last one so far in any case */
963 if (first
== -1 || last
== -1)
966 return callback(c
, first
, last
);
971 int flash_erase_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
973 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_erase
);
976 static int flash_driver_unprotect(struct flash_bank
*bank
, int first
, int last
)
978 return flash_driver_protect(bank
, 0, first
, last
);
981 static int flash_unlock_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
983 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_unprotect
);
987 /* write (optional verify) an image to flash memory of the given target */
988 static int flash_write_unlock(struct target
*target
, struct image
*image
, uint32_t *written
, int erase
, bool unlock
)
990 int retval
= ERROR_OK
;
993 uint32_t section_offset
;
994 struct flash_bank
*c
;
1005 /* assume all sectors need erasing - stops any problems
1006 * when flash_write is called multiple times */
1011 /* allocate padding array */
1012 padding
= malloc(image
->num_sections
* sizeof(padding
));
1014 /* loop until we reach end of the image */
1015 while (section
< image
->num_sections
)
1017 uint32_t buffer_size
;
1021 uint32_t run_address
= image
->sections
[section
].base_address
+ section_offset
;
1022 uint32_t run_size
= image
->sections
[section
].size
- section_offset
;
1025 if (image
->sections
[section
].size
== 0)
1027 LOG_WARNING("empty section %d", section
);
1033 /* find the corresponding flash bank */
1034 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
1036 section
++; /* and skip it */
1041 /* collect consecutive sections which fall into the same bank */
1042 section_first
= section
;
1043 section_last
= section
;
1044 padding
[section
] = 0;
1045 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1)
1046 && (section_last
+ 1 < image
->num_sections
))
1048 if (image
->sections
[section_last
+ 1].base_address
< (run_address
+ run_size
))
1050 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last
+ 1);
1053 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
1054 * attempt to rebuild a consecutive buffer for the flash loader */
1055 pad_bytes
= (image
->sections
[section_last
+ 1].base_address
) - (run_address
+ run_size
);
1056 if ((run_address
+ run_size
+ pad_bytes
) > (c
->base
+ c
->size
))
1058 padding
[section_last
] = pad_bytes
;
1059 run_size
+= image
->sections
[++section_last
].size
;
1060 run_size
+= pad_bytes
;
1061 padding
[section_last
] = 0;
1063 LOG_INFO("Padding image section %d with %d bytes", section_last
-1, pad_bytes
);
1066 /* fit the run into bank constraints */
1067 if (run_address
+ run_size
- 1 > c
->base
+ c
->size
- 1)
1069 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
1070 (int)(c
->base
+ c
->size
- run_address
), (int)(run_size
), (int)(c
->size
));
1071 run_size
= c
->base
+ c
->size
- run_address
;
1074 /* allocate buffer */
1075 buffer
= malloc(run_size
);
1078 /* read sections to the buffer */
1079 while (buffer_size
< run_size
)
1083 size_read
= run_size
- buffer_size
;
1084 if (size_read
> image
->sections
[section
].size
- section_offset
)
1085 size_read
= image
->sections
[section
].size
- section_offset
;
1087 if ((retval
= image_read_section(image
, section
, section_offset
,
1088 size_read
, buffer
+ buffer_size
, &size_read
)) != ERROR_OK
|| size_read
== 0)
1095 /* see if we need to pad the section */
1096 while (padding
[section
]--)
1097 (buffer
+ buffer_size
)[size_read
++] = 0xff;
1099 buffer_size
+= size_read
;
1100 section_offset
+= size_read
;
1102 if (section_offset
>= image
->sections
[section
].size
)
1113 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
1115 if (retval
== ERROR_OK
)
1119 /* calculate and erase sectors */
1120 retval
= flash_erase_address_range(target
, run_address
, run_size
);
1124 if (retval
== ERROR_OK
)
1126 /* write flash sectors */
1127 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
1132 if (retval
!= ERROR_OK
)
1135 return retval
; /* abort operation */
1138 if (written
!= NULL
)
1139 *written
+= run_size
; /* add run size to total written counter */
1147 int flash_write(struct target
*target
, struct image
*image
, uint32_t *written
, int erase
)
1149 return flash_write_unlock(target
, image
, written
, erase
, false);
1152 int default_flash_mem_blank_check(struct flash_bank
*bank
)
1154 struct target
*target
= bank
->target
;
1155 uint8_t buffer
[1024];
1156 int buffer_size
= sizeof(buffer
);
1160 if (bank
->target
->state
!= TARGET_HALTED
)
1162 LOG_ERROR("Target not halted");
1163 return ERROR_TARGET_NOT_HALTED
;
1166 for (i
= 0; i
< bank
->num_sectors
; i
++)
1169 bank
->sectors
[i
].is_erased
= 1;
1171 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
1175 chunk
= buffer_size
;
1176 if (chunk
> (j
- bank
->sectors
[i
].size
))
1178 chunk
= (j
- bank
->sectors
[i
].size
);
1181 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
1182 if (retval
!= ERROR_OK
)
1185 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
1187 if (buffer
[nBytes
] != 0xFF)
1189 bank
->sectors
[i
].is_erased
= 0;
1199 int default_flash_blank_check(struct flash_bank
*bank
)
1201 struct target
*target
= bank
->target
;
1207 if (bank
->target
->state
!= TARGET_HALTED
)
1209 LOG_ERROR("Target not halted");
1210 return ERROR_TARGET_NOT_HALTED
;
1213 for (i
= 0; i
< bank
->num_sectors
; i
++)
1215 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
1216 uint32_t size
= bank
->sectors
[i
].size
;
1218 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
1224 bank
->sectors
[i
].is_erased
= 1;
1226 bank
->sectors
[i
].is_erased
= 0;
1232 LOG_USER("Running slow fallback erase check - add working memory");
1233 return default_flash_mem_blank_check(bank
);
1239 int flash_init_drivers(struct command_context
*cmd_ctx
)
1241 register_jim(cmd_ctx
, "ocd_flash_banks",
1242 jim_flash_banks
, "return information about the flash banks");
1247 register_command(cmd_ctx
, flash_cmd
, "info",
1248 handle_flash_info_command
, COMMAND_EXEC
,
1249 "print info about flash bank <num>");
1250 register_command(cmd_ctx
, flash_cmd
, "probe",
1251 handle_flash_probe_command
, COMMAND_EXEC
,
1252 "identify flash bank <num>");
1253 register_command(cmd_ctx
, flash_cmd
, "erase_check",
1254 handle_flash_erase_check_command
, COMMAND_EXEC
,
1255 "check erase state of sectors in flash bank <num>");
1256 register_command(cmd_ctx
, flash_cmd
, "protect_check",
1257 handle_flash_protect_check_command
, COMMAND_EXEC
,
1258 "check protection state of sectors in flash bank <num>");
1259 register_command(cmd_ctx
, flash_cmd
, "erase_sector",
1260 handle_flash_erase_command
, COMMAND_EXEC
,
1261 "erase sectors at <bank> <first> <last>");
1262 register_command(cmd_ctx
, flash_cmd
, "erase_address",
1263 handle_flash_erase_address_command
, COMMAND_EXEC
,
1264 "erase address range <address> <length>");
1266 register_command(cmd_ctx
, flash_cmd
, "fillw",
1267 handle_flash_fill_command
, COMMAND_EXEC
,
1268 "fill with pattern (no autoerase) <address> <word_pattern> <count>");
1269 register_command(cmd_ctx
, flash_cmd
, "fillh",
1270 handle_flash_fill_command
, COMMAND_EXEC
,
1271 "fill with pattern <address> <halfword_pattern> <count>");
1272 register_command(cmd_ctx
, flash_cmd
, "fillb",
1273 handle_flash_fill_command
, COMMAND_EXEC
,
1274 "fill with pattern <address> <byte_pattern> <count>");
1276 register_command(cmd_ctx
, flash_cmd
, "write_bank",
1277 handle_flash_write_bank_command
, COMMAND_EXEC
,
1278 "write binary data to <bank> <file> <offset>");
1279 register_command(cmd_ctx
, flash_cmd
, "write_image",
1280 handle_flash_write_image_command
, COMMAND_EXEC
,
1281 "write_image [erase] [unlock] <file> [offset] [type]");
1282 register_command(cmd_ctx
, flash_cmd
, "protect",
1283 handle_flash_protect_command
, COMMAND_EXEC
,
1284 "set protection of sectors at <bank> <first> <last> <on | off>");
1289 int flash_register_commands(struct command_context
*cmd_ctx
)
1291 flash_cmd
= register_command(cmd_ctx
, NULL
, "flash",
1292 NULL
, COMMAND_ANY
, NULL
);
1294 register_command(cmd_ctx
, flash_cmd
, "bank",
1295 handle_flash_bank_command
, COMMAND_CONFIG
,
1296 "flash bank <driver> <base> <size> "
1297 "<chip_width> <bus_width> <target> [driver_options ...]");
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)