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 flash_driver_t lpc2000_flash
;
39 extern flash_driver_t lpc288x_flash
;
40 extern flash_driver_t lpc2900_flash
;
41 extern flash_driver_t cfi_flash
;
42 extern flash_driver_t at91sam3_flash
;
43 extern flash_driver_t at91sam7_flash
;
44 extern flash_driver_t str7x_flash
;
45 extern flash_driver_t str9x_flash
;
46 extern flash_driver_t aduc702x_flash
;
47 extern flash_driver_t stellaris_flash
;
48 extern flash_driver_t str9xpec_flash
;
49 extern flash_driver_t stm32x_flash
;
50 extern flash_driver_t tms470_flash
;
51 extern flash_driver_t ecosflash_flash
;
52 extern flash_driver_t ocl_flash
;
53 extern flash_driver_t pic32mx_flash
;
54 extern flash_driver_t avr_flash
;
55 extern flash_driver_t faux_flash
;
57 flash_driver_t
*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 if ((retval
= flash_drivers
[i
]->flash_bank_command(cmd_ctx
, cmd
, args
, argc
, c
)) != ERROR_OK
)
264 LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32
, args
[0], c
->base
);
269 /* put flash bank in linked list */
273 /* find last flash bank */
274 for (p
= flash_banks
; p
&& p
->next
; p
= p
->next
) bank_num
++;
277 c
->bank_number
= bank_num
+ 1;
288 /* no matching flash driver found */
291 LOG_ERROR("flash driver '%s' not found", args
[0]);
298 COMMAND_HANDLER(handle_flash_info_command
)
306 return ERROR_COMMAND_SYNTAX_ERROR
;
309 COMMAND_PARSE_NUMBER(uint
, args
[0], bank_nr
);
311 for (p
= flash_banks
; p
; p
= p
->next
, i
++)
318 /* attempt auto probe */
319 if ((retval
= p
->driver
->auto_probe(p
)) != ERROR_OK
)
322 command_print(cmd_ctx
,
323 "#%" PRIi32
" : %s at 0x%8.8" PRIx32
", size 0x%8.8" PRIx32
", buswidth %i, chipwidth %i",
330 for (j
= 0; j
< p
->num_sectors
; j
++)
334 if (p
->sectors
[j
].is_protected
== 0)
335 protect_state
= "not protected";
336 else if (p
->sectors
[j
].is_protected
== 1)
337 protect_state
= "protected";
339 protect_state
= "protection state unknown";
341 command_print(cmd_ctx
,
342 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
344 p
->sectors
[j
].offset
,
346 p
->sectors
[j
].size
>> 10,
350 *buf
= '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
351 retval
= p
->driver
->info(p
, buf
, sizeof(buf
));
352 command_print(cmd_ctx
, "%s", buf
);
353 if (retval
!= ERROR_OK
)
354 LOG_ERROR("error retrieving flash info (%d)", retval
);
360 COMMAND_HANDLER(handle_flash_probe_command
)
366 return ERROR_COMMAND_SYNTAX_ERROR
;
370 COMMAND_PARSE_NUMBER(uint
, args
[0], bank_nr
);
371 flash_bank_t
*p
= get_flash_bank_by_num_noprobe(bank_nr
);
374 if ((retval
= p
->driver
->probe(p
)) == ERROR_OK
)
376 command_print(cmd_ctx
, "flash '%s' found at 0x%8.8" PRIx32
, p
->driver
->name
, p
->base
);
378 else if (retval
== ERROR_FLASH_BANK_INVALID
)
380 command_print(cmd_ctx
, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32
,
385 command_print(cmd_ctx
, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32
,
391 command_print(cmd_ctx
, "flash bank '#%s' is out of bounds", args
[0]);
397 COMMAND_HANDLER(handle_flash_erase_check_command
)
401 return ERROR_COMMAND_SYNTAX_ERROR
;
405 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
406 if (ERROR_OK
!= retval
)
410 if ((retval
= p
->driver
->erase_check(p
)) == ERROR_OK
)
412 command_print(cmd_ctx
, "successfully checked erase state");
416 command_print(cmd_ctx
, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32
,
420 for (j
= 0; j
< p
->num_sectors
; j
++)
424 if (p
->sectors
[j
].is_erased
== 0)
425 erase_state
= "not erased";
426 else if (p
->sectors
[j
].is_erased
== 1)
427 erase_state
= "erased";
429 erase_state
= "erase state unknown";
431 command_print(cmd_ctx
,
432 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
434 p
->sectors
[j
].offset
,
436 p
->sectors
[j
].size
>> 10,
443 COMMAND_HANDLER(handle_flash_erase_address_command
)
450 target_t
*target
= get_current_target(cmd_ctx
);
453 return ERROR_COMMAND_SYNTAX_ERROR
;
455 COMMAND_PARSE_NUMBER(int, args
[0], address
);
456 COMMAND_PARSE_NUMBER(int, args
[1], length
);
459 command_print(cmd_ctx
, "Length must be >0");
460 return ERROR_COMMAND_SYNTAX_ERROR
;
463 p
= get_flash_bank_by_addr(target
, address
);
469 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
472 struct duration bench
;
473 duration_start(&bench
);
475 retval
= flash_erase_address_range(target
, address
, length
);
477 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
479 command_print(cmd_ctx
, "erased address 0x%8.8x (length %i)"
480 " in %fs (%0.3f kb/s)", address
, length
,
481 duration_elapsed(&bench
), duration_kbps(&bench
, length
));
487 COMMAND_HANDLER(handle_flash_protect_check_command
)
490 return ERROR_COMMAND_SYNTAX_ERROR
;
493 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
494 if (ERROR_OK
!= retval
)
497 if ((retval
= p
->driver
->protect_check(p
)) == ERROR_OK
)
499 command_print(cmd_ctx
, "successfully checked protect state");
501 else if (retval
== ERROR_FLASH_OPERATION_FAILED
)
503 command_print(cmd_ctx
, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32
, args
[0], p
->base
);
507 command_print(cmd_ctx
, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32
, args
[0], p
->base
);
513 static int flash_check_sector_parameters(struct command_context_s
*cmd_ctx
,
514 uint32_t first
, uint32_t last
, uint32_t num_sectors
)
516 if (!(first
<= last
)) {
517 command_print(cmd_ctx
, "ERROR: "
518 "first sector must be <= last sector");
522 if (!(last
<= (num_sectors
- 1))) {
523 command_print(cmd_ctx
, "ERROR: last sector must be <= %d",
524 (int) num_sectors
- 1);
531 COMMAND_HANDLER(handle_flash_erase_command
)
534 return ERROR_COMMAND_SYNTAX_ERROR
;
540 COMMAND_PARSE_NUMBER(u32
, args
[0], bank_nr
);
541 flash_bank_t
*p
= get_flash_bank_by_num(bank_nr
);
545 COMMAND_PARSE_NUMBER(u32
, args
[1], first
);
546 if (strcmp(args
[2], "last") == 0)
547 last
= p
->num_sectors
- 1;
549 COMMAND_PARSE_NUMBER(u32
, args
[2], last
);
552 if ((retval
= flash_check_sector_parameters(cmd_ctx
,
553 first
, last
, p
->num_sectors
)) != ERROR_OK
)
556 struct duration bench
;
557 duration_start(&bench
);
559 retval
= flash_driver_erase(p
, first
, last
);
561 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
563 command_print(cmd_ctx
, "erased sectors %" PRIu32
" "
564 "through %" PRIu32
" on flash bank %" PRIu32
" "
565 "in %fs", first
, last
, bank_nr
, duration_elapsed(&bench
));
571 COMMAND_HANDLER(handle_flash_protect_command
)
574 return ERROR_COMMAND_SYNTAX_ERROR
;
581 COMMAND_PARSE_NUMBER(u32
, args
[0], bank_nr
);
582 flash_bank_t
*p
= get_flash_bank_by_num(bank_nr
);
586 COMMAND_PARSE_NUMBER(u32
, args
[1], first
);
587 if (strcmp(args
[2], "last") == 0)
588 last
= p
->num_sectors
- 1;
590 COMMAND_PARSE_NUMBER(u32
, args
[2], last
);
592 if (strcmp(args
[3], "on") == 0)
594 else if (strcmp(args
[3], "off") == 0)
597 return ERROR_COMMAND_SYNTAX_ERROR
;
600 if ((retval
= flash_check_sector_parameters(cmd_ctx
,
601 first
, last
, p
->num_sectors
)) != ERROR_OK
)
604 retval
= flash_driver_protect(p
, set
, first
, last
);
605 if (retval
== ERROR_OK
) {
606 command_print(cmd_ctx
, "%s protection for sectors %i "
607 "through %i on flash bank %i",
608 (set
) ? "set" : "cleared", (int) first
,
609 (int) last
, (int) bank_nr
);
615 COMMAND_HANDLER(handle_flash_write_image_command
)
617 target_t
*target
= get_current_target(cmd_ctx
);
626 return ERROR_COMMAND_SYNTAX_ERROR
;
629 /* flash auto-erase is disabled by default*/
631 bool auto_unlock
= false;
635 if (strcmp(args
[0], "erase") == 0)
640 command_print(cmd_ctx
, "auto erase enabled");
641 } else if (strcmp(args
[0], "unlock") == 0)
646 command_print(cmd_ctx
, "auto unlock enabled");
655 return ERROR_COMMAND_SYNTAX_ERROR
;
660 LOG_ERROR("no target selected");
664 struct duration bench
;
665 duration_start(&bench
);
669 image
.base_address_set
= 1;
670 COMMAND_PARSE_NUMBER(int, args
[1], image
.base_address
);
674 image
.base_address_set
= 0;
675 image
.base_address
= 0x0;
678 image
.start_address_set
= 0;
680 retval
= image_open(&image
, args
[0], (argc
== 3) ? args
[2] : NULL
);
681 if (retval
!= ERROR_OK
)
686 retval
= flash_write_unlock(target
, &image
, &written
, auto_erase
, auto_unlock
);
687 if (retval
!= ERROR_OK
)
693 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
695 command_print(cmd_ctx
, "wrote %" PRIu32
" byte from file %s "
696 "in %fs (%0.3f kb/s)", written
, args
[0],
697 duration_elapsed(&bench
), duration_kbps(&bench
, written
));
705 COMMAND_HANDLER(handle_flash_fill_command
)
712 uint8_t readback
[1024];
714 uint32_t cur_size
= 0;
715 uint32_t chunk_count
;
716 target_t
*target
= get_current_target(cmd_ctx
);
721 return ERROR_COMMAND_SYNTAX_ERROR
;
723 COMMAND_PARSE_NUMBER(u32
, args
[0], address
);
724 COMMAND_PARSE_NUMBER(u32
, args
[1], pattern
);
725 COMMAND_PARSE_NUMBER(u32
, args
[2], count
);
742 return ERROR_COMMAND_SYNTAX_ERROR
;
745 chunk_count
= MIN(count
, (1024 / wordsize
));
749 for (i
= 0; i
< chunk_count
; i
++)
751 target_buffer_set_u32(target
, chunk
+ i
* wordsize
, pattern
);
755 for (i
= 0; i
< chunk_count
; i
++)
757 target_buffer_set_u16(target
, chunk
+ i
* wordsize
, pattern
);
761 memset(chunk
, pattern
, chunk_count
);
764 LOG_ERROR("BUG: can't happen");
768 struct duration bench
;
769 duration_start(&bench
);
771 for (wrote
= 0; wrote
< (count
*wordsize
); wrote
+= cur_size
)
773 cur_size
= MIN((count
*wordsize
- wrote
), sizeof(chunk
));
775 bank
= get_flash_bank_by_addr(target
, address
);
780 err
= flash_driver_write(bank
, chunk
, address
- bank
->base
+ wrote
, cur_size
);
784 err
= target_read_buffer(target
, address
+ wrote
, cur_size
, readback
);
789 for (i
= 0; i
< cur_size
; i
++)
791 if (readback
[i
]!=chunk
[i
])
793 LOG_ERROR("Verfication error address 0x%08" PRIx32
", read back 0x%02x, expected 0x%02x",
794 address
+ wrote
+ i
, readback
[i
], chunk
[i
]);
800 if (duration_measure(&bench
) == ERROR_OK
)
802 command_print(cmd_ctx
, "wrote %" PRIu32
" bytes to 0x%8.8" PRIx32
803 " in %fs (%0.3f kb/s)", wrote
, address
,
804 duration_elapsed(&bench
), duration_kbps(&bench
, wrote
));
809 COMMAND_HANDLER(handle_flash_write_bank_command
)
817 return ERROR_COMMAND_SYNTAX_ERROR
;
819 struct duration bench
;
820 duration_start(&bench
);
823 int retval
= flash_command_get_bank_by_num(cmd_ctx
, args
[0], &p
);
824 if (ERROR_OK
!= retval
)
827 COMMAND_PARSE_NUMBER(u32
, args
[2], offset
);
829 if (fileio_open(&fileio
, args
[1], FILEIO_READ
, FILEIO_BINARY
) != ERROR_OK
)
834 buffer
= malloc(fileio
.size
);
835 if (fileio_read(&fileio
, fileio
.size
, buffer
, &buf_cnt
) != ERROR_OK
)
838 fileio_close(&fileio
);
842 retval
= flash_driver_write(p
, buffer
, offset
, buf_cnt
);
847 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
849 command_print(cmd_ctx
, "wrote %lld byte from file %s to flash bank %u"
850 " at offset 0x%8.8" PRIx32
" in %fs (%0.3f kb/s)",
851 fileio
.size
, args
[1], p
->bank_number
, offset
,
852 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
855 fileio_close(&fileio
);
860 void flash_set_dirty(void)
865 /* set all flash to require erasing */
866 for (c
= flash_banks
; c
; c
= c
->next
)
868 for (i
= 0; i
< c
->num_sectors
; i
++)
870 c
->sectors
[i
].is_erased
= 0;
875 /* lookup flash bank by address */
876 flash_bank_t
*get_flash_bank_by_addr(target_t
*target
, uint32_t addr
)
880 /* cycle through bank list */
881 for (c
= flash_banks
; c
; c
= c
->next
)
884 retval
= c
->driver
->auto_probe(c
);
886 if (retval
!= ERROR_OK
)
888 LOG_ERROR("auto_probe failed %d\n", retval
);
891 /* check whether address belongs to this flash bank */
892 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
895 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
899 /* erase given flash region, selects proper bank according to target and address */
900 static int flash_iterate_address_range(target_t
*target
, uint32_t addr
, uint32_t length
,
901 int (*callback
)(struct flash_bank_s
*bank
, int first
, int last
))
908 if ((c
= get_flash_bank_by_addr(target
, addr
)) == NULL
)
909 return ERROR_FLASH_DST_OUT_OF_BANK
; /* no corresponding bank found */
911 if (c
->size
== 0 || c
->num_sectors
== 0)
913 LOG_ERROR("Bank is invalid");
914 return ERROR_FLASH_BANK_INVALID
;
919 /* special case, erase whole bank when length is zero */
921 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
923 return callback(c
, 0, c
->num_sectors
- 1);
926 /* check whether it fits */
927 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1)
928 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
932 for (i
= 0; i
< c
->num_sectors
; i
++)
934 /* check whether sector overlaps with the given range and is not yet erased */
935 if (addr
< c
->sectors
[i
].offset
+ c
->sectors
[i
].size
&& addr
+ length
> c
->sectors
[i
].offset
&& c
->sectors
[i
].is_erased
!= 1) {
936 /* if first is not set yet then this is the first sector */
939 last
= i
; /* and it is the last one so far in any case */
943 if (first
== -1 || last
== -1)
946 return callback(c
, first
, last
);
951 int flash_erase_address_range(target_t
*target
, uint32_t addr
, uint32_t length
)
953 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_erase
);
956 static int flash_driver_unprotect(struct flash_bank_s
*bank
, int first
, int last
)
958 return flash_driver_protect(bank
, 0, first
, last
);
961 static int flash_unlock_address_range(target_t
*target
, uint32_t addr
, uint32_t length
)
963 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_unprotect
);
967 /* write (optional verify) an image to flash memory of the given target */
968 static int flash_write_unlock(target_t
*target
, image_t
*image
, uint32_t *written
, int erase
, bool unlock
)
970 int retval
= ERROR_OK
;
973 uint32_t section_offset
;
985 /* assume all sectors need erasing - stops any problems
986 * when flash_write is called multiple times */
991 /* allocate padding array */
992 padding
= malloc(image
->num_sections
* sizeof(padding
));
994 /* loop until we reach end of the image */
995 while (section
< image
->num_sections
)
997 uint32_t buffer_size
;
1001 uint32_t run_address
= image
->sections
[section
].base_address
+ section_offset
;
1002 uint32_t run_size
= image
->sections
[section
].size
- section_offset
;
1005 if (image
->sections
[section
].size
== 0)
1007 LOG_WARNING("empty section %d", section
);
1013 /* find the corresponding flash bank */
1014 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
1016 section
++; /* and skip it */
1021 /* collect consecutive sections which fall into the same bank */
1022 section_first
= section
;
1023 section_last
= section
;
1024 padding
[section
] = 0;
1025 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1)
1026 && (section_last
+ 1 < image
->num_sections
))
1028 if (image
->sections
[section_last
+ 1].base_address
< (run_address
+ run_size
))
1030 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last
+ 1);
1033 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
1034 * attempt to rebuild a consecutive buffer for the flash loader */
1035 pad_bytes
= (image
->sections
[section_last
+ 1].base_address
) - (run_address
+ run_size
);
1036 if ((run_address
+ run_size
+ pad_bytes
) > (c
->base
+ c
->size
))
1038 padding
[section_last
] = pad_bytes
;
1039 run_size
+= image
->sections
[++section_last
].size
;
1040 run_size
+= pad_bytes
;
1041 padding
[section_last
] = 0;
1043 LOG_INFO("Padding image section %d with %d bytes", section_last
-1, pad_bytes
);
1046 /* fit the run into bank constraints */
1047 if (run_address
+ run_size
- 1 > c
->base
+ c
->size
- 1)
1049 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
1050 (int)(c
->base
+ c
->size
- run_address
), (int)(run_size
), (int)(c
->size
));
1051 run_size
= c
->base
+ c
->size
- run_address
;
1054 /* allocate buffer */
1055 buffer
= malloc(run_size
);
1058 /* read sections to the buffer */
1059 while (buffer_size
< run_size
)
1063 size_read
= run_size
- buffer_size
;
1064 if (size_read
> image
->sections
[section
].size
- section_offset
)
1065 size_read
= image
->sections
[section
].size
- section_offset
;
1067 if ((retval
= image_read_section(image
, section
, section_offset
,
1068 size_read
, buffer
+ buffer_size
, &size_read
)) != ERROR_OK
|| size_read
== 0)
1075 /* see if we need to pad the section */
1076 while (padding
[section
]--)
1077 (buffer
+ buffer_size
)[size_read
++] = 0xff;
1079 buffer_size
+= size_read
;
1080 section_offset
+= size_read
;
1082 if (section_offset
>= image
->sections
[section
].size
)
1093 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
1095 if (retval
== ERROR_OK
)
1099 /* calculate and erase sectors */
1100 retval
= flash_erase_address_range(target
, run_address
, run_size
);
1104 if (retval
== ERROR_OK
)
1106 /* write flash sectors */
1107 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
1112 if (retval
!= ERROR_OK
)
1115 return retval
; /* abort operation */
1118 if (written
!= NULL
)
1119 *written
+= run_size
; /* add run size to total written counter */
1127 int flash_write(target_t
*target
, image_t
*image
, uint32_t *written
, int erase
)
1129 return flash_write_unlock(target
, image
, written
, erase
, false);
1132 int default_flash_mem_blank_check(struct flash_bank_s
*bank
)
1134 target_t
*target
= bank
->target
;
1135 uint8_t buffer
[1024];
1136 int buffer_size
= sizeof(buffer
);
1140 if (bank
->target
->state
!= TARGET_HALTED
)
1142 LOG_ERROR("Target not halted");
1143 return ERROR_TARGET_NOT_HALTED
;
1146 for (i
= 0; i
< bank
->num_sectors
; i
++)
1149 bank
->sectors
[i
].is_erased
= 1;
1151 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
1155 chunk
= buffer_size
;
1156 if (chunk
> (j
- bank
->sectors
[i
].size
))
1158 chunk
= (j
- bank
->sectors
[i
].size
);
1161 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
1162 if (retval
!= ERROR_OK
)
1165 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
1167 if (buffer
[nBytes
] != 0xFF)
1169 bank
->sectors
[i
].is_erased
= 0;
1179 int default_flash_blank_check(struct flash_bank_s
*bank
)
1181 target_t
*target
= bank
->target
;
1187 if (bank
->target
->state
!= TARGET_HALTED
)
1189 LOG_ERROR("Target not halted");
1190 return ERROR_TARGET_NOT_HALTED
;
1193 for (i
= 0; i
< bank
->num_sectors
; i
++)
1195 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
1196 uint32_t size
= bank
->sectors
[i
].size
;
1198 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
1204 bank
->sectors
[i
].is_erased
= 1;
1206 bank
->sectors
[i
].is_erased
= 0;
1212 LOG_USER("Running slow fallback erase check - add working memory");
1213 return default_flash_mem_blank_check(bank
);
1219 int flash_init_drivers(struct command_context_s
*cmd_ctx
)
1221 register_jim(cmd_ctx
, "ocd_flash_banks",
1222 jim_flash_banks
, "return information about the flash banks");
1227 register_command(cmd_ctx
, flash_cmd
, "info",
1228 handle_flash_info_command
, COMMAND_EXEC
,
1229 "print info about flash bank <num>");
1230 register_command(cmd_ctx
, flash_cmd
, "probe",
1231 handle_flash_probe_command
, COMMAND_EXEC
,
1232 "identify flash bank <num>");
1233 register_command(cmd_ctx
, flash_cmd
, "erase_check",
1234 handle_flash_erase_check_command
, COMMAND_EXEC
,
1235 "check erase state of sectors in flash bank <num>");
1236 register_command(cmd_ctx
, flash_cmd
, "protect_check",
1237 handle_flash_protect_check_command
, COMMAND_EXEC
,
1238 "check protection state of sectors in flash bank <num>");
1239 register_command(cmd_ctx
, flash_cmd
, "erase_sector",
1240 handle_flash_erase_command
, COMMAND_EXEC
,
1241 "erase sectors at <bank> <first> <last>");
1242 register_command(cmd_ctx
, flash_cmd
, "erase_address",
1243 handle_flash_erase_address_command
, COMMAND_EXEC
,
1244 "erase address range <address> <length>");
1246 register_command(cmd_ctx
, flash_cmd
, "fillw",
1247 handle_flash_fill_command
, COMMAND_EXEC
,
1248 "fill with pattern (no autoerase) <address> <word_pattern> <count>");
1249 register_command(cmd_ctx
, flash_cmd
, "fillh",
1250 handle_flash_fill_command
, COMMAND_EXEC
,
1251 "fill with pattern <address> <halfword_pattern> <count>");
1252 register_command(cmd_ctx
, flash_cmd
, "fillb",
1253 handle_flash_fill_command
, COMMAND_EXEC
,
1254 "fill with pattern <address> <byte_pattern> <count>");
1256 register_command(cmd_ctx
, flash_cmd
, "write_bank",
1257 handle_flash_write_bank_command
, COMMAND_EXEC
,
1258 "write binary data to <bank> <file> <offset>");
1259 register_command(cmd_ctx
, flash_cmd
, "write_image",
1260 handle_flash_write_image_command
, COMMAND_EXEC
,
1261 "write_image [erase] [unlock] <file> [offset] [type]");
1262 register_command(cmd_ctx
, flash_cmd
, "protect",
1263 handle_flash_protect_command
, COMMAND_EXEC
,
1264 "set protection of sectors at <bank> <first> <last> <on | off>");
1269 int flash_register_commands(struct command_context_s
*cmd_ctx
)
1271 flash_cmd
= register_command(cmd_ctx
, NULL
, "flash",
1272 NULL
, COMMAND_ANY
, NULL
);
1274 register_command(cmd_ctx
, flash_cmd
, "bank",
1275 handle_flash_bank_command
, COMMAND_CONFIG
,
1276 "flash bank <driver> <base> <size> "
1277 "<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)