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 static int flash_write_unlock(target_t
*target
, image_t
*image
, uint32_t *written
, int erase
, bool unlock
);
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
[] = {
79 flash_bank_t
*flash_banks
;
80 static command_t
*flash_cmd
;
82 /* wafer thin wrapper for invoking the flash driver */
83 static int flash_driver_write(struct flash_bank_s
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
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
);
97 static int flash_driver_erase(struct flash_bank_s
*bank
, int first
, int last
)
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
);
110 int flash_driver_protect(struct flash_bank_s
*bank
, int set
, int first
, int last
)
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
);
123 static int jim_flash_banks(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
128 Jim_WrongNumArgs(interp
, 1, argv
, "no arguments to flash_banks command");
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
);
156 flash_bank_t
*get_flash_bank_by_num_noprobe(int num
)
161 for (p
= flash_banks
; p
; p
= p
->next
)
168 LOG_ERROR("flash bank %d does not exist", num
);
172 int flash_get_bank_count(void)
176 for (p
= flash_banks
; p
; p
= p
->next
)
183 flash_bank_t
*get_flash_bank_by_num(int num
)
185 flash_bank_t
*p
= get_flash_bank_by_num_noprobe(num
);
191 retval
= p
->driver
->auto_probe(p
);
193 if (retval
!= ERROR_OK
)
195 LOG_ERROR("auto_probe failed %d\n", retval
);
201 int flash_command_get_bank_by_num(
202 struct command_context_s
*cmd_ctx
, const char *str
, flash_bank_t
**bank
)
205 COMMAND_PARSE_NUMBER(uint
, str
, bank_num
);
207 *bank
= get_flash_bank_by_num(bank_num
);
210 command_print(cmd_ctx
,
211 "flash bank '#%u' not found", bank_num
);
212 return ERROR_INVALID_ARGUMENTS
;
218 COMMAND_HANDLER(handle_flash_bank_command
)
227 return ERROR_COMMAND_SYNTAX_ERROR
;
230 if ((target
= get_target(args
[5])) == NULL
)
232 LOG_ERROR("target '%s' not defined", args
[5]);
236 for (i
= 0; flash_drivers
[i
]; i
++)
238 if (strcmp(args
[0], flash_drivers
[i
]->name
) != 0)
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]);
250 c
= malloc(sizeof(flash_bank_t
));
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
);
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
);
270 /* put flash bank in linked list */
274 /* find last flash bank */
275 for (p
= flash_banks
; p
&& p
->next
; p
= p
->next
) bank_num
++;
278 c
->bank_number
= bank_num
+ 1;
289 /* no matching flash driver found */
292 LOG_ERROR("flash driver '%s' not found", args
[0]);
299 COMMAND_HANDLER(handle_flash_info_command
)
307 return ERROR_COMMAND_SYNTAX_ERROR
;
310 COMMAND_PARSE_NUMBER(uint
, args
[0], bank_nr
);
312 for (p
= flash_banks
; p
; p
= p
->next
, i
++)
319 /* attempt auto probe */
320 if ((retval
= p
->driver
->auto_probe(p
)) != ERROR_OK
)
323 command_print(cmd_ctx
,
324 "#%" PRIi32
" : %s at 0x%8.8" PRIx32
", size 0x%8.8" PRIx32
", buswidth %i, chipwidth %i",
331 for (j
= 0; j
< p
->num_sectors
; j
++)
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";
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
,
347 p
->sectors
[j
].size
>> 10,
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
);
361 COMMAND_HANDLER(handle_flash_probe_command
)
367 return ERROR_COMMAND_SYNTAX_ERROR
;
371 COMMAND_PARSE_NUMBER(uint
, args
[0], bank_nr
);
372 flash_bank_t
*p
= get_flash_bank_by_num_noprobe(bank_nr
);
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
,
386 command_print(cmd_ctx
, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32
,
392 command_print(cmd_ctx
, "flash bank '#%s' is out of bounds", args
[0]);
398 COMMAND_HANDLER(handle_flash_erase_check_command
)
402 return ERROR_COMMAND_SYNTAX_ERROR
;
406 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
407 if (ERROR_OK
!= retval
)
411 if ((retval
= p
->driver
->erase_check(p
)) == ERROR_OK
)
413 command_print(cmd_ctx
, "successfully checked erase state");
417 command_print(cmd_ctx
, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32
,
421 for (j
= 0; j
< p
->num_sectors
; j
++)
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";
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
,
437 p
->sectors
[j
].size
>> 10,
444 COMMAND_HANDLER(handle_flash_erase_address_command
)
451 target_t
*target
= get_current_target(cmd_ctx
);
454 return ERROR_COMMAND_SYNTAX_ERROR
;
456 COMMAND_PARSE_NUMBER(int, args
[0], address
);
457 COMMAND_PARSE_NUMBER(int, args
[1], length
);
460 command_print(cmd_ctx
, "Length must be >0");
461 return ERROR_COMMAND_SYNTAX_ERROR
;
464 p
= get_flash_bank_by_addr(target
, address
);
470 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
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
));
488 COMMAND_HANDLER(handle_flash_protect_check_command
)
491 return ERROR_COMMAND_SYNTAX_ERROR
;
494 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
495 if (ERROR_OK
!= 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
);
508 command_print(cmd_ctx
, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32
, args
[0], p
->base
);
514 static int flash_check_sector_parameters(struct command_context_s
*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");
523 if (!(last
<= (num_sectors
- 1))) {
524 command_print(cmd_ctx
, "ERROR: last sector must be <= %d",
525 (int) num_sectors
- 1);
532 COMMAND_HANDLER(handle_flash_erase_command
)
535 return ERROR_COMMAND_SYNTAX_ERROR
;
541 COMMAND_PARSE_NUMBER(u32
, args
[0], bank_nr
);
542 flash_bank_t
*p
= get_flash_bank_by_num(bank_nr
);
546 COMMAND_PARSE_NUMBER(u32
, args
[1], first
);
547 if (strcmp(args
[2], "last") == 0)
548 last
= p
->num_sectors
- 1;
550 COMMAND_PARSE_NUMBER(u32
, args
[2], last
);
553 if ((retval
= flash_check_sector_parameters(cmd_ctx
,
554 first
, last
, p
->num_sectors
)) != ERROR_OK
)
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
));
572 COMMAND_HANDLER(handle_flash_protect_command
)
575 return ERROR_COMMAND_SYNTAX_ERROR
;
582 COMMAND_PARSE_NUMBER(u32
, args
[0], bank_nr
);
583 flash_bank_t
*p
= get_flash_bank_by_num(bank_nr
);
587 COMMAND_PARSE_NUMBER(u32
, args
[1], first
);
588 if (strcmp(args
[2], "last") == 0)
589 last
= p
->num_sectors
- 1;
591 COMMAND_PARSE_NUMBER(u32
, args
[2], last
);
593 if (strcmp(args
[3], "on") == 0)
595 else if (strcmp(args
[3], "off") == 0)
598 return ERROR_COMMAND_SYNTAX_ERROR
;
601 if ((retval
= flash_check_sector_parameters(cmd_ctx
,
602 first
, last
, p
->num_sectors
)) != ERROR_OK
)
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
);
616 COMMAND_HANDLER(handle_flash_write_image_command
)
618 target_t
*target
= get_current_target(cmd_ctx
);
627 return ERROR_COMMAND_SYNTAX_ERROR
;
630 /* flash auto-erase is disabled by default*/
632 bool auto_unlock
= false;
636 if (strcmp(args
[0], "erase") == 0)
641 command_print(cmd_ctx
, "auto erase enabled");
642 } else if (strcmp(args
[0], "unlock") == 0)
647 command_print(cmd_ctx
, "auto unlock enabled");
656 return ERROR_COMMAND_SYNTAX_ERROR
;
661 LOG_ERROR("no target selected");
665 struct duration bench
;
666 duration_start(&bench
);
670 image
.base_address_set
= 1;
671 COMMAND_PARSE_NUMBER(int, args
[1], image
.base_address
);
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
)
687 retval
= flash_write_unlock(target
, &image
, &written
, auto_erase
, auto_unlock
);
688 if (retval
!= ERROR_OK
)
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
));
706 COMMAND_HANDLER(handle_flash_fill_command
)
713 uint8_t readback
[1024];
715 uint32_t cur_size
= 0;
716 uint32_t chunk_count
;
717 target_t
*target
= get_current_target(cmd_ctx
);
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
);
743 return ERROR_COMMAND_SYNTAX_ERROR
;
746 chunk_count
= MIN(count
, (1024 / wordsize
));
750 for (i
= 0; i
< chunk_count
; i
++)
752 target_buffer_set_u32(target
, chunk
+ i
* wordsize
, pattern
);
756 for (i
= 0; i
< chunk_count
; i
++)
758 target_buffer_set_u16(target
, chunk
+ i
* wordsize
, pattern
);
762 memset(chunk
, pattern
, chunk_count
);
765 LOG_ERROR("BUG: can't happen");
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
));
776 bank
= get_flash_bank_by_addr(target
, address
);
781 err
= flash_driver_write(bank
, chunk
, address
- bank
->base
+ wrote
, cur_size
);
785 err
= target_read_buffer(target
, address
+ wrote
, cur_size
, readback
);
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
]);
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
));
810 COMMAND_HANDLER(handle_flash_write_bank_command
)
815 struct fileio fileio
;
818 return ERROR_COMMAND_SYNTAX_ERROR
;
820 struct duration bench
;
821 duration_start(&bench
);
824 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
825 if (ERROR_OK
!= retval
)
828 COMMAND_PARSE_NUMBER(u32
, args
[2], offset
);
830 if (fileio_open(&fileio
, args
[1], FILEIO_READ
, FILEIO_BINARY
) != ERROR_OK
)
835 buffer
= malloc(fileio
.size
);
836 if (fileio_read(&fileio
, fileio
.size
, buffer
, &buf_cnt
) != ERROR_OK
)
839 fileio_close(&fileio
);
843 retval
= flash_driver_write(p
, buffer
, offset
, buf_cnt
);
848 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
850 command_print(cmd_ctx
, "wrote %lld 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
);
861 void flash_set_dirty(void)
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 flash_bank_t
*get_flash_bank_by_addr(target_t
*target
, uint32_t addr
)
881 /* cycle through bank list */
882 for (c
= flash_banks
; c
; c
= c
->next
)
885 retval
= c
->driver
->auto_probe(c
);
887 if (retval
!= ERROR_OK
)
889 LOG_ERROR("auto_probe failed %d\n", retval
);
892 /* check whether address belongs to this flash bank */
893 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
896 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
900 /* erase given flash region, selects proper bank according to target and address */
901 static int flash_iterate_address_range(target_t
*target
, uint32_t addr
, uint32_t length
,
902 int (*callback
)(struct flash_bank_s
*bank
, int first
, int last
))
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
;
920 /* special case, erase whole bank when length is zero */
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
;
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 */
940 last
= i
; /* and it is the last one so far in any case */
944 if (first
== -1 || last
== -1)
947 return callback(c
, first
, last
);
952 int flash_erase_address_range(target_t
*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_s
*bank
, int first
, int last
)
959 return flash_driver_protect(bank
, 0, first
, last
);
962 static int flash_unlock_address_range(target_t
*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(target_t
*target
, image_t
*image
, uint32_t *written
, int erase
, bool unlock
)
971 int retval
= ERROR_OK
;
974 uint32_t section_offset
;
986 /* assume all sectors need erasing - stops any problems
987 * when flash_write is called multiple times */
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
;
1002 uint32_t run_address
= image
->sections
[section
].base_address
+ section_offset
;
1003 uint32_t run_size
= image
->sections
[section
].size
- section_offset
;
1006 if (image
->sections
[section
].size
== 0)
1008 LOG_WARNING("empty section %d", section
);
1014 /* find the corresponding flash bank */
1015 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
1017 section
++; /* and skip it */
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);
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
))
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
);
1059 /* read sections to the buffer */
1060 while (buffer_size
< run_size
)
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)
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
)
1094 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
1096 if (retval
== ERROR_OK
)
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
);
1113 if (retval
!= ERROR_OK
)
1116 return retval
; /* abort operation */
1119 if (written
!= NULL
)
1120 *written
+= run_size
; /* add run size to total written counter */
1128 int flash_write(target_t
*target
, image_t
*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_s
*bank
)
1135 target_t
*target
= bank
->target
;
1136 uint8_t buffer
[1024];
1137 int buffer_size
= sizeof(buffer
);
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
++)
1150 bank
->sectors
[i
].is_erased
= 1;
1152 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
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
)
1166 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
1168 if (buffer
[nBytes
] != 0xFF)
1170 bank
->sectors
[i
].is_erased
= 0;
1180 int default_flash_blank_check(struct flash_bank_s
*bank
)
1182 target_t
*target
= bank
->target
;
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
)
1205 bank
->sectors
[i
].is_erased
= 1;
1207 bank
->sectors
[i
].is_erased
= 0;
1213 LOG_USER("Running slow fallback erase check - add working memory");
1214 return default_flash_mem_blank_check(bank
);
1220 int flash_init_drivers(struct command_context_s
*cmd_ctx
)
1222 register_jim(cmd_ctx
, "ocd_flash_banks",
1223 jim_flash_banks
, "return information about the flash banks");
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>");
1270 int flash_register_commands(struct command_context_s
*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 ...]");
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)