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
;
82 /* wafer thin wrapper for invoking the flash driver */
83 static int flash_driver_write(struct flash_bank
*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
*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
*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
)
125 struct flash_bank
*p
;
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 struct flash_bank
*get_flash_bank_by_num_noprobe(int num
)
158 struct flash_bank
*p
;
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)
174 struct flash_bank
*p
;
176 for (p
= flash_banks
; p
; p
= p
->next
)
183 struct flash_bank
*get_flash_bank_by_name(const char *name
)
185 unsigned requested
= get_flash_name_index(name
);
188 struct flash_bank
*bank
;
189 for (bank
= flash_banks
; NULL
!= bank
; bank
= bank
->next
)
191 if (strcmp(bank
->name
, name
) == 0)
193 if (!flash_driver_name_matches(bank
->driver
->name
, name
))
195 if (++found
< requested
)
202 struct flash_bank
*get_flash_bank_by_num(int num
)
204 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(num
);
210 retval
= p
->driver
->auto_probe(p
);
212 if (retval
!= ERROR_OK
)
214 LOG_ERROR("auto_probe failed %d\n", retval
);
220 COMMAND_HELPER(flash_command_get_bank
, unsigned name_index
,
221 struct flash_bank
**bank
)
223 const char *name
= CMD_ARGV
[name_index
];
224 *bank
= get_flash_bank_by_name(name
);
229 COMMAND_PARSE_NUMBER(uint
, name
, bank_num
);
231 *bank
= get_flash_bank_by_num(bank_num
);
234 command_print(CMD_CTX
, "flash bank '%s' not found", name
);
235 return ERROR_INVALID_ARGUMENTS
;
241 COMMAND_HANDLER(handle_flash_bank_command
)
245 LOG_ERROR("usage: flash bank <name> <driver> "
246 "<base> <size> <chip_width> <bus_width>");
247 return ERROR_COMMAND_SYNTAX_ERROR
;
249 // save bank name and advance arguments for compatibility
250 const char *bank_name
= *CMD_ARGV
++;
253 struct target
*target
;
254 if ((target
= get_target(CMD_ARGV
[5])) == NULL
)
256 LOG_ERROR("target '%s' not defined", CMD_ARGV
[5]);
260 const char *driver_name
= CMD_ARGV
[0];
261 for (unsigned i
= 0; flash_drivers
[i
]; i
++)
263 if (strcmp(driver_name
, flash_drivers
[i
]->name
) != 0)
266 struct flash_bank
*p
, *c
;
268 /* register flash specific commands */
269 if (flash_drivers
[i
]->register_commands(CMD_CTX
) != ERROR_OK
)
271 LOG_ERROR("couldn't register '%s' commands", driver_name
);
275 c
= malloc(sizeof(struct flash_bank
));
276 c
->name
= strdup(bank_name
);
278 c
->driver
= flash_drivers
[i
];
279 c
->driver_priv
= NULL
;
280 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], c
->base
);
281 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], c
->size
);
282 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[3], c
->chip_width
);
283 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[4], c
->bus_width
);
289 retval
= CALL_COMMAND_HANDLER(flash_drivers
[i
]->flash_bank_command
, c
);
290 if (ERROR_OK
!= retval
)
292 LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32
,
293 driver_name
, c
->base
);
298 /* put flash bank in linked list */
302 /* find last flash bank */
303 for (p
= flash_banks
; p
&& p
->next
; p
= p
->next
) bank_num
++;
306 c
->bank_number
= bank_num
+ 1;
317 /* no matching flash driver found */
318 LOG_ERROR("flash driver '%s' not found", driver_name
);
322 COMMAND_HANDLER(handle_flash_info_command
)
324 struct flash_bank
*p
;
330 return ERROR_COMMAND_SYNTAX_ERROR
;
333 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], bank_nr
);
335 for (p
= flash_banks
; p
; p
= p
->next
, i
++)
342 /* attempt auto probe */
343 if ((retval
= p
->driver
->auto_probe(p
)) != ERROR_OK
)
346 command_print(CMD_CTX
,
347 "#%" PRIi32
" : %s at 0x%8.8" PRIx32
", size 0x%8.8" PRIx32
", buswidth %i, chipwidth %i",
354 for (j
= 0; j
< p
->num_sectors
; j
++)
358 if (p
->sectors
[j
].is_protected
== 0)
359 protect_state
= "not protected";
360 else if (p
->sectors
[j
].is_protected
== 1)
361 protect_state
= "protected";
363 protect_state
= "protection state unknown";
365 command_print(CMD_CTX
,
366 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
368 p
->sectors
[j
].offset
,
370 p
->sectors
[j
].size
>> 10,
374 *buf
= '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
375 retval
= p
->driver
->info(p
, buf
, sizeof(buf
));
376 command_print(CMD_CTX
, "%s", buf
);
377 if (retval
!= ERROR_OK
)
378 LOG_ERROR("error retrieving flash info (%d)", retval
);
384 COMMAND_HANDLER(handle_flash_probe_command
)
390 return ERROR_COMMAND_SYNTAX_ERROR
;
394 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], bank_nr
);
395 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(bank_nr
);
398 if ((retval
= p
->driver
->probe(p
)) == ERROR_OK
)
400 command_print(CMD_CTX
, "flash '%s' found at 0x%8.8" PRIx32
, p
->driver
->name
, p
->base
);
402 else if (retval
== ERROR_FLASH_BANK_INVALID
)
404 command_print(CMD_CTX
, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32
,
405 CMD_ARGV
[0], p
->base
);
409 command_print(CMD_CTX
, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32
,
410 CMD_ARGV
[0], p
->base
);
415 command_print(CMD_CTX
, "flash bank '#%s' is out of bounds", CMD_ARGV
[0]);
421 COMMAND_HANDLER(handle_flash_erase_check_command
)
425 return ERROR_COMMAND_SYNTAX_ERROR
;
428 struct flash_bank
*p
;
429 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
430 if (ERROR_OK
!= retval
)
434 if ((retval
= p
->driver
->erase_check(p
)) == ERROR_OK
)
436 command_print(CMD_CTX
, "successfully checked erase state");
440 command_print(CMD_CTX
, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32
,
441 CMD_ARGV
[0], p
->base
);
444 for (j
= 0; j
< p
->num_sectors
; j
++)
448 if (p
->sectors
[j
].is_erased
== 0)
449 erase_state
= "not erased";
450 else if (p
->sectors
[j
].is_erased
== 1)
451 erase_state
= "erased";
453 erase_state
= "erase state unknown";
455 command_print(CMD_CTX
,
456 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
458 p
->sectors
[j
].offset
,
460 p
->sectors
[j
].size
>> 10,
467 COMMAND_HANDLER(handle_flash_erase_address_command
)
469 struct flash_bank
*p
;
474 struct target
*target
= get_current_target(CMD_CTX
);
477 return ERROR_COMMAND_SYNTAX_ERROR
;
479 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], address
);
480 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], length
);
483 command_print(CMD_CTX
, "Length must be >0");
484 return ERROR_COMMAND_SYNTAX_ERROR
;
487 p
= get_flash_bank_by_addr(target
, address
);
493 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
496 struct duration bench
;
497 duration_start(&bench
);
499 retval
= flash_erase_address_range(target
, address
, length
);
501 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
503 command_print(CMD_CTX
, "erased address 0x%8.8x (length %i)"
504 " in %fs (%0.3f kb/s)", address
, length
,
505 duration_elapsed(&bench
), duration_kbps(&bench
, length
));
511 COMMAND_HANDLER(handle_flash_protect_check_command
)
514 return ERROR_COMMAND_SYNTAX_ERROR
;
516 struct flash_bank
*p
;
517 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
518 if (ERROR_OK
!= retval
)
521 if ((retval
= p
->driver
->protect_check(p
)) == ERROR_OK
)
523 command_print(CMD_CTX
, "successfully checked protect state");
525 else if (retval
== ERROR_FLASH_OPERATION_FAILED
)
527 command_print(CMD_CTX
, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32
, CMD_ARGV
[0], p
->base
);
531 command_print(CMD_CTX
, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32
, CMD_ARGV
[0], p
->base
);
537 static int flash_check_sector_parameters(struct command_context
*cmd_ctx
,
538 uint32_t first
, uint32_t last
, uint32_t num_sectors
)
540 if (!(first
<= last
)) {
541 command_print(cmd_ctx
, "ERROR: "
542 "first sector must be <= last sector");
546 if (!(last
<= (num_sectors
- 1))) {
547 command_print(cmd_ctx
, "ERROR: last sector must be <= %d",
548 (int) num_sectors
- 1);
555 COMMAND_HANDLER(handle_flash_erase_command
)
558 return ERROR_COMMAND_SYNTAX_ERROR
;
564 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], bank_nr
);
565 struct flash_bank
*p
= get_flash_bank_by_num(bank_nr
);
569 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], first
);
570 if (strcmp(CMD_ARGV
[2], "last") == 0)
571 last
= p
->num_sectors
- 1;
573 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], last
);
576 if ((retval
= flash_check_sector_parameters(CMD_CTX
,
577 first
, last
, p
->num_sectors
)) != ERROR_OK
)
580 struct duration bench
;
581 duration_start(&bench
);
583 retval
= flash_driver_erase(p
, first
, last
);
585 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
587 command_print(CMD_CTX
, "erased sectors %" PRIu32
" "
588 "through %" PRIu32
" on flash bank %" PRIu32
" "
589 "in %fs", first
, last
, bank_nr
, duration_elapsed(&bench
));
595 COMMAND_HANDLER(handle_flash_protect_command
)
598 return ERROR_COMMAND_SYNTAX_ERROR
;
604 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], bank_nr
);
605 struct flash_bank
*p
= get_flash_bank_by_num(bank_nr
);
609 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], first
);
610 if (strcmp(CMD_ARGV
[2], "last") == 0)
611 last
= p
->num_sectors
- 1;
613 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], last
);
616 COMMAND_PARSE_ON_OFF(CMD_ARGV
[3], set
);
619 if ((retval
= flash_check_sector_parameters(CMD_CTX
,
620 first
, last
, p
->num_sectors
)) != ERROR_OK
)
623 retval
= flash_driver_protect(p
, set
, first
, last
);
624 if (retval
== ERROR_OK
) {
625 command_print(CMD_CTX
, "%s protection for sectors %i "
626 "through %i on flash bank %i",
627 (set
) ? "set" : "cleared", (int) first
,
628 (int) last
, (int) bank_nr
);
634 COMMAND_HANDLER(handle_flash_write_image_command
)
636 struct target
*target
= get_current_target(CMD_CTX
);
645 return ERROR_COMMAND_SYNTAX_ERROR
;
648 /* flash auto-erase is disabled by default*/
650 bool auto_unlock
= false;
654 if (strcmp(CMD_ARGV
[0], "erase") == 0)
659 command_print(CMD_CTX
, "auto erase enabled");
660 } else if (strcmp(CMD_ARGV
[0], "unlock") == 0)
665 command_print(CMD_CTX
, "auto unlock enabled");
674 return ERROR_COMMAND_SYNTAX_ERROR
;
679 LOG_ERROR("no target selected");
683 struct duration bench
;
684 duration_start(&bench
);
688 image
.base_address_set
= 1;
689 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], image
.base_address
);
693 image
.base_address_set
= 0;
694 image
.base_address
= 0x0;
697 image
.start_address_set
= 0;
699 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
700 if (retval
!= ERROR_OK
)
705 retval
= flash_write_unlock(target
, &image
, &written
, auto_erase
, auto_unlock
);
706 if (retval
!= ERROR_OK
)
712 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
714 command_print(CMD_CTX
, "wrote %" PRIu32
" byte from file %s "
715 "in %fs (%0.3f kb/s)", written
, CMD_ARGV
[0],
716 duration_elapsed(&bench
), duration_kbps(&bench
, written
));
724 COMMAND_HANDLER(handle_flash_fill_command
)
731 uint32_t cur_size
= 0;
732 uint32_t chunk_count
;
733 struct target
*target
= get_current_target(CMD_CTX
);
736 int retval
= ERROR_OK
;
738 static size_t const chunksize
= 1024;
739 uint8_t *chunk
= malloc(chunksize
);
743 uint8_t *readback
= malloc(chunksize
);
744 if (readback
== NULL
)
753 retval
= ERROR_COMMAND_SYNTAX_ERROR
;
758 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
759 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], pattern
);
760 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], count
);
777 retval
= ERROR_COMMAND_SYNTAX_ERROR
;
781 chunk_count
= MIN(count
, (chunksize
/ wordsize
));
785 for (i
= 0; i
< chunk_count
; i
++)
787 target_buffer_set_u32(target
, chunk
+ i
* wordsize
, pattern
);
791 for (i
= 0; i
< chunk_count
; i
++)
793 target_buffer_set_u16(target
, chunk
+ i
* wordsize
, pattern
);
797 memset(chunk
, pattern
, chunk_count
);
800 LOG_ERROR("BUG: can't happen");
804 struct duration bench
;
805 duration_start(&bench
);
807 for (wrote
= 0; wrote
< (count
*wordsize
); wrote
+= cur_size
)
809 cur_size
= MIN((count
*wordsize
- wrote
), sizeof(chunk
));
810 struct flash_bank
*bank
;
811 bank
= get_flash_bank_by_addr(target
, address
);
817 err
= flash_driver_write(bank
, chunk
, address
- bank
->base
+ wrote
, cur_size
);
824 err
= target_read_buffer(target
, address
+ wrote
, cur_size
, readback
);
832 for (i
= 0; i
< cur_size
; i
++)
834 if (readback
[i
]!=chunk
[i
])
836 LOG_ERROR("Verfication error address 0x%08" PRIx32
", read back 0x%02x, expected 0x%02x",
837 address
+ wrote
+ i
, readback
[i
], chunk
[i
]);
844 if (duration_measure(&bench
) == ERROR_OK
)
846 command_print(CMD_CTX
, "wrote %" PRIu32
" bytes to 0x%8.8" PRIx32
847 " in %fs (%0.3f kb/s)", wrote
, address
,
848 duration_elapsed(&bench
), duration_kbps(&bench
, wrote
));
858 COMMAND_HANDLER(handle_flash_write_bank_command
)
862 struct fileio fileio
;
865 return ERROR_COMMAND_SYNTAX_ERROR
;
867 struct duration bench
;
868 duration_start(&bench
);
870 struct flash_bank
*p
;
871 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
872 if (ERROR_OK
!= retval
)
875 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], offset
);
877 if (fileio_open(&fileio
, CMD_ARGV
[1], FILEIO_READ
, FILEIO_BINARY
) != ERROR_OK
)
882 buffer
= malloc(fileio
.size
);
884 if (fileio_read(&fileio
, fileio
.size
, buffer
, &buf_cnt
) != ERROR_OK
)
887 fileio_close(&fileio
);
891 retval
= flash_driver_write(p
, buffer
, offset
, buf_cnt
);
896 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
898 command_print(CMD_CTX
, "wrote %zu byte from file %s to flash bank %u"
899 " at offset 0x%8.8" PRIx32
" in %fs (%0.3f kb/s)",
900 fileio
.size
, CMD_ARGV
[1], p
->bank_number
, offset
,
901 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
904 fileio_close(&fileio
);
909 void flash_set_dirty(void)
911 struct flash_bank
*c
;
914 /* set all flash to require erasing */
915 for (c
= flash_banks
; c
; c
= c
->next
)
917 for (i
= 0; i
< c
->num_sectors
; i
++)
919 c
->sectors
[i
].is_erased
= 0;
924 /* lookup flash bank by address */
925 struct flash_bank
*get_flash_bank_by_addr(struct target
*target
, uint32_t addr
)
927 struct flash_bank
*c
;
929 /* cycle through bank list */
930 for (c
= flash_banks
; c
; c
= c
->next
)
933 retval
= c
->driver
->auto_probe(c
);
935 if (retval
!= ERROR_OK
)
937 LOG_ERROR("auto_probe failed %d\n", retval
);
940 /* check whether address belongs to this flash bank */
941 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
944 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
948 /* erase given flash region, selects proper bank according to target and address */
949 static int flash_iterate_address_range(struct target
*target
, uint32_t addr
, uint32_t length
,
950 int (*callback
)(struct flash_bank
*bank
, int first
, int last
))
952 struct flash_bank
*c
;
957 if ((c
= get_flash_bank_by_addr(target
, addr
)) == NULL
)
958 return ERROR_FLASH_DST_OUT_OF_BANK
; /* no corresponding bank found */
960 if (c
->size
== 0 || c
->num_sectors
== 0)
962 LOG_ERROR("Bank is invalid");
963 return ERROR_FLASH_BANK_INVALID
;
968 /* special case, erase whole bank when length is zero */
970 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
972 return callback(c
, 0, c
->num_sectors
- 1);
975 /* check whether it fits */
976 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1)
977 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
981 for (i
= 0; i
< c
->num_sectors
; i
++)
983 /* check whether sector overlaps with the given range and is not yet erased */
984 if (addr
< c
->sectors
[i
].offset
+ c
->sectors
[i
].size
&& addr
+ length
> c
->sectors
[i
].offset
&& c
->sectors
[i
].is_erased
!= 1) {
985 /* if first is not set yet then this is the first sector */
988 last
= i
; /* and it is the last one so far in any case */
992 if (first
== -1 || last
== -1)
995 return callback(c
, first
, last
);
1000 int flash_erase_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
1002 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_erase
);
1005 static int flash_driver_unprotect(struct flash_bank
*bank
, int first
, int last
)
1007 return flash_driver_protect(bank
, 0, first
, last
);
1010 static int flash_unlock_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
1012 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_unprotect
);
1016 /* write (optional verify) an image to flash memory of the given target */
1017 static int flash_write_unlock(struct target
*target
, struct image
*image
, uint32_t *written
, int erase
, bool unlock
)
1019 int retval
= ERROR_OK
;
1022 uint32_t section_offset
;
1023 struct flash_bank
*c
;
1034 /* assume all sectors need erasing - stops any problems
1035 * when flash_write is called multiple times */
1040 /* allocate padding array */
1041 padding
= malloc(image
->num_sections
* sizeof(padding
));
1043 /* loop until we reach end of the image */
1044 while (section
< image
->num_sections
)
1046 uint32_t buffer_size
;
1050 uint32_t run_address
= image
->sections
[section
].base_address
+ section_offset
;
1051 uint32_t run_size
= image
->sections
[section
].size
- section_offset
;
1054 if (image
->sections
[section
].size
== 0)
1056 LOG_WARNING("empty section %d", section
);
1062 /* find the corresponding flash bank */
1063 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
1065 section
++; /* and skip it */
1070 /* collect consecutive sections which fall into the same bank */
1071 section_first
= section
;
1072 section_last
= section
;
1073 padding
[section
] = 0;
1074 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1)
1075 && (section_last
+ 1 < image
->num_sections
))
1077 if (image
->sections
[section_last
+ 1].base_address
< (run_address
+ run_size
))
1079 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last
+ 1);
1082 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
1083 * attempt to rebuild a consecutive buffer for the flash loader */
1084 pad_bytes
= (image
->sections
[section_last
+ 1].base_address
) - (run_address
+ run_size
);
1085 if ((run_address
+ run_size
+ pad_bytes
) > (c
->base
+ c
->size
))
1087 padding
[section_last
] = pad_bytes
;
1088 run_size
+= image
->sections
[++section_last
].size
;
1089 run_size
+= pad_bytes
;
1090 padding
[section_last
] = 0;
1092 LOG_INFO("Padding image section %d with %d bytes", section_last
-1, pad_bytes
);
1095 /* fit the run into bank constraints */
1096 if (run_address
+ run_size
- 1 > c
->base
+ c
->size
- 1)
1098 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
1099 (int)(c
->base
+ c
->size
- run_address
), (int)(run_size
), (int)(c
->size
));
1100 run_size
= c
->base
+ c
->size
- run_address
;
1103 /* allocate buffer */
1104 buffer
= malloc(run_size
);
1107 /* read sections to the buffer */
1108 while (buffer_size
< run_size
)
1112 size_read
= run_size
- buffer_size
;
1113 if (size_read
> image
->sections
[section
].size
- section_offset
)
1114 size_read
= image
->sections
[section
].size
- section_offset
;
1116 if ((retval
= image_read_section(image
, section
, section_offset
,
1117 size_read
, buffer
+ buffer_size
, &size_read
)) != ERROR_OK
|| size_read
== 0)
1124 /* see if we need to pad the section */
1125 while (padding
[section
]--)
1126 (buffer
+ buffer_size
)[size_read
++] = 0xff;
1128 buffer_size
+= size_read
;
1129 section_offset
+= size_read
;
1131 if (section_offset
>= image
->sections
[section
].size
)
1142 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
1144 if (retval
== ERROR_OK
)
1148 /* calculate and erase sectors */
1149 retval
= flash_erase_address_range(target
, run_address
, run_size
);
1153 if (retval
== ERROR_OK
)
1155 /* write flash sectors */
1156 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
1161 if (retval
!= ERROR_OK
)
1164 return retval
; /* abort operation */
1167 if (written
!= NULL
)
1168 *written
+= run_size
; /* add run size to total written counter */
1176 int flash_write(struct target
*target
, struct image
*image
, uint32_t *written
, int erase
)
1178 return flash_write_unlock(target
, image
, written
, erase
, false);
1181 int default_flash_mem_blank_check(struct flash_bank
*bank
)
1183 struct target
*target
= bank
->target
;
1184 const int buffer_size
= 1024;
1187 int retval
= ERROR_OK
;
1189 if (bank
->target
->state
!= TARGET_HALTED
)
1191 LOG_ERROR("Target not halted");
1192 return ERROR_TARGET_NOT_HALTED
;
1195 uint8_t *buffer
= malloc(buffer_size
);
1197 for (i
= 0; i
< bank
->num_sectors
; i
++)
1200 bank
->sectors
[i
].is_erased
= 1;
1202 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
1205 chunk
= buffer_size
;
1206 if (chunk
> (j
- bank
->sectors
[i
].size
))
1208 chunk
= (j
- bank
->sectors
[i
].size
);
1211 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
1212 if (retval
!= ERROR_OK
)
1217 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
1219 if (buffer
[nBytes
] != 0xFF)
1221 bank
->sectors
[i
].is_erased
= 0;
1234 int default_flash_blank_check(struct flash_bank
*bank
)
1236 struct target
*target
= bank
->target
;
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
++)
1250 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
1251 uint32_t size
= bank
->sectors
[i
].size
;
1253 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
1259 bank
->sectors
[i
].is_erased
= 1;
1261 bank
->sectors
[i
].is_erased
= 0;
1267 LOG_USER("Running slow fallback erase check - add working memory");
1268 return default_flash_mem_blank_check(bank
);
1274 static const struct command_registration flash_exec_command_handlers
[] = {
1277 .handler
= &handle_flash_probe_command
,
1278 .mode
= COMMAND_EXEC
,
1280 .help
= "identify flash bank",
1284 .handler
= &handle_flash_info_command
,
1285 .mode
= COMMAND_EXEC
,
1287 .help
= "print bank information",
1290 .name
= "erase_check",
1291 .handler
= &handle_flash_erase_check_command
,
1292 .mode
= COMMAND_EXEC
,
1294 .help
= "check erase state of sectors",
1297 .name
= "protect_check",
1298 .handler
= &handle_flash_protect_check_command
,
1299 .mode
= COMMAND_EXEC
,
1301 .help
= "check protection state of sectors",
1304 .name
= "erase_sector",
1305 .handler
= &handle_flash_erase_command
,
1306 .mode
= COMMAND_EXEC
,
1307 .usage
= "<bank> <first> <last>",
1308 .help
= "erase sectors",
1311 .name
= "erase_address",
1312 .handler
= &handle_flash_erase_address_command
,
1313 .mode
= COMMAND_EXEC
,
1314 .usage
= "<bank> <address> <length>",
1315 .help
= "erase address range",
1320 .handler
= &handle_flash_fill_command
,
1321 .mode
= COMMAND_EXEC
,
1322 .usage
= "<bank> <address> <word_pattern> <count>",
1323 .help
= "fill with pattern (no autoerase)",
1327 .handler
= &handle_flash_fill_command
,
1328 .mode
= COMMAND_EXEC
,
1329 .usage
= "<bank> <address> <halfword_pattern> <count>",
1330 .help
= "fill with pattern",
1334 .handler
= &handle_flash_fill_command
,
1335 .mode
= COMMAND_EXEC
,
1336 .usage
= "<bank> <address> <byte_pattern> <count>",
1337 .help
= "fill with pattern",
1341 .name
= "write_bank",
1342 .handler
= &handle_flash_write_bank_command
,
1343 .mode
= COMMAND_EXEC
,
1344 .usage
= "<bank> <file> <offset>",
1345 .help
= "write binary data",
1348 .name
= "write_image",
1349 .handler
= &handle_flash_write_image_command
,
1350 .mode
= COMMAND_EXEC
,
1351 .usage
= "<bank> [erase] [unlock] <file> [offset] [type]",
1352 .help
= "write an image to flash"
1356 .handler
= &handle_flash_protect_command
,
1357 .mode
= COMMAND_EXEC
,
1358 .usage
= "<bank> <first> <last> <on | off>",
1359 .help
= "set protection of sectors",
1361 COMMAND_REGISTRATION_DONE
1364 int flash_init_drivers(struct command_context
*cmd_ctx
)
1366 register_jim(cmd_ctx
, "ocd_flash_banks",
1367 jim_flash_banks
, "return information about the flash banks");
1371 struct command
*parent
= command_find_in_context(cmd_ctx
, "flash");
1372 return register_commands(cmd_ctx
, parent
, flash_exec_command_handlers
);
1376 static const struct command_registration flash_config_command_handlers
[] = {
1379 .handler
= &handle_flash_bank_command
,
1380 .mode
= COMMAND_CONFIG
,
1381 .usage
= "<name> <driver> <base> <size> "
1382 "<chip_width> <bus_width> <target> "
1383 "[driver_options ...]",
1384 .help
= "Define a new bank with the given name, "
1385 "using the specified NOR flash driver.",
1387 COMMAND_REGISTRATION_DONE
1389 static const struct command_registration flash_command_handlers
[] = {
1392 .mode
= COMMAND_ANY
,
1393 .help
= "NOR flash command group",
1394 .chain
= flash_config_command_handlers
,
1396 COMMAND_REGISTRATION_DONE
1399 int flash_register_commands(struct command_context
*cmd_ctx
)
1401 return register_commands(cmd_ctx
, NULL
, flash_command_handlers
);
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)