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 <helper/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 /* register flash specific commands */
267 if (NULL
!= flash_drivers
[i
]->commands
)
269 int retval
= register_commands(CMD_CTX
, NULL
,
270 flash_drivers
[i
]->commands
);
271 if (ERROR_OK
!= retval
)
273 LOG_ERROR("couldn't register '%s' commands",
279 struct flash_bank
*p
, *c
;
280 c
= malloc(sizeof(struct flash_bank
));
281 c
->name
= strdup(bank_name
);
283 c
->driver
= flash_drivers
[i
];
284 c
->driver_priv
= NULL
;
285 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], c
->base
);
286 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], c
->size
);
287 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[3], c
->chip_width
);
288 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[4], c
->bus_width
);
294 retval
= CALL_COMMAND_HANDLER(flash_drivers
[i
]->flash_bank_command
, c
);
295 if (ERROR_OK
!= retval
)
297 LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32
,
298 driver_name
, c
->base
);
303 /* put flash bank in linked list */
307 /* find last flash bank */
308 for (p
= flash_banks
; p
&& p
->next
; p
= p
->next
) bank_num
++;
311 c
->bank_number
= bank_num
+ 1;
322 /* no matching flash driver found */
323 LOG_ERROR("flash driver '%s' not found", driver_name
);
327 COMMAND_HANDLER(handle_flash_info_command
)
329 struct flash_bank
*p
;
335 return ERROR_COMMAND_SYNTAX_ERROR
;
338 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], bank_nr
);
340 for (p
= flash_banks
; p
; p
= p
->next
, i
++)
347 /* attempt auto probe */
348 if ((retval
= p
->driver
->auto_probe(p
)) != ERROR_OK
)
351 command_print(CMD_CTX
,
352 "#%" PRIi32
" : %s at 0x%8.8" PRIx32
", size 0x%8.8" PRIx32
", buswidth %i, chipwidth %i",
359 for (j
= 0; j
< p
->num_sectors
; j
++)
363 if (p
->sectors
[j
].is_protected
== 0)
364 protect_state
= "not protected";
365 else if (p
->sectors
[j
].is_protected
== 1)
366 protect_state
= "protected";
368 protect_state
= "protection state unknown";
370 command_print(CMD_CTX
,
371 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
373 p
->sectors
[j
].offset
,
375 p
->sectors
[j
].size
>> 10,
379 *buf
= '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
380 retval
= p
->driver
->info(p
, buf
, sizeof(buf
));
381 command_print(CMD_CTX
, "%s", buf
);
382 if (retval
!= ERROR_OK
)
383 LOG_ERROR("error retrieving flash info (%d)", retval
);
389 COMMAND_HANDLER(handle_flash_probe_command
)
395 return ERROR_COMMAND_SYNTAX_ERROR
;
399 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], bank_nr
);
400 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(bank_nr
);
403 if ((retval
= p
->driver
->probe(p
)) == ERROR_OK
)
405 command_print(CMD_CTX
, "flash '%s' found at 0x%8.8" PRIx32
, p
->driver
->name
, p
->base
);
407 else if (retval
== ERROR_FLASH_BANK_INVALID
)
409 command_print(CMD_CTX
, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32
,
410 CMD_ARGV
[0], p
->base
);
414 command_print(CMD_CTX
, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32
,
415 CMD_ARGV
[0], p
->base
);
420 command_print(CMD_CTX
, "flash bank '#%s' is out of bounds", CMD_ARGV
[0]);
426 COMMAND_HANDLER(handle_flash_erase_check_command
)
430 return ERROR_COMMAND_SYNTAX_ERROR
;
433 struct flash_bank
*p
;
434 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
435 if (ERROR_OK
!= retval
)
439 if ((retval
= p
->driver
->erase_check(p
)) == ERROR_OK
)
441 command_print(CMD_CTX
, "successfully checked erase state");
445 command_print(CMD_CTX
, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32
,
446 CMD_ARGV
[0], p
->base
);
449 for (j
= 0; j
< p
->num_sectors
; j
++)
453 if (p
->sectors
[j
].is_erased
== 0)
454 erase_state
= "not erased";
455 else if (p
->sectors
[j
].is_erased
== 1)
456 erase_state
= "erased";
458 erase_state
= "erase state unknown";
460 command_print(CMD_CTX
,
461 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
463 p
->sectors
[j
].offset
,
465 p
->sectors
[j
].size
>> 10,
472 COMMAND_HANDLER(handle_flash_erase_address_command
)
474 struct flash_bank
*p
;
479 struct target
*target
= get_current_target(CMD_CTX
);
482 return ERROR_COMMAND_SYNTAX_ERROR
;
484 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], address
);
485 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], length
);
488 command_print(CMD_CTX
, "Length must be >0");
489 return ERROR_COMMAND_SYNTAX_ERROR
;
492 p
= get_flash_bank_by_addr(target
, address
);
498 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
501 struct duration bench
;
502 duration_start(&bench
);
504 retval
= flash_erase_address_range(target
, address
, length
);
506 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
508 command_print(CMD_CTX
, "erased address 0x%8.8x (length %i)"
509 " in %fs (%0.3f kb/s)", address
, length
,
510 duration_elapsed(&bench
), duration_kbps(&bench
, length
));
516 COMMAND_HANDLER(handle_flash_protect_check_command
)
519 return ERROR_COMMAND_SYNTAX_ERROR
;
521 struct flash_bank
*p
;
522 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
523 if (ERROR_OK
!= retval
)
526 if ((retval
= p
->driver
->protect_check(p
)) == ERROR_OK
)
528 command_print(CMD_CTX
, "successfully checked protect state");
530 else if (retval
== ERROR_FLASH_OPERATION_FAILED
)
532 command_print(CMD_CTX
, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32
, CMD_ARGV
[0], p
->base
);
536 command_print(CMD_CTX
, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32
, CMD_ARGV
[0], p
->base
);
542 static int flash_check_sector_parameters(struct command_context
*cmd_ctx
,
543 uint32_t first
, uint32_t last
, uint32_t num_sectors
)
545 if (!(first
<= last
)) {
546 command_print(cmd_ctx
, "ERROR: "
547 "first sector must be <= last sector");
551 if (!(last
<= (num_sectors
- 1))) {
552 command_print(cmd_ctx
, "ERROR: last sector must be <= %d",
553 (int) num_sectors
- 1);
560 COMMAND_HANDLER(handle_flash_erase_command
)
563 return ERROR_COMMAND_SYNTAX_ERROR
;
569 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], bank_nr
);
570 struct flash_bank
*p
= get_flash_bank_by_num(bank_nr
);
574 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], first
);
575 if (strcmp(CMD_ARGV
[2], "last") == 0)
576 last
= p
->num_sectors
- 1;
578 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], last
);
581 if ((retval
= flash_check_sector_parameters(CMD_CTX
,
582 first
, last
, p
->num_sectors
)) != ERROR_OK
)
585 struct duration bench
;
586 duration_start(&bench
);
588 retval
= flash_driver_erase(p
, first
, last
);
590 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
592 command_print(CMD_CTX
, "erased sectors %" PRIu32
" "
593 "through %" PRIu32
" on flash bank %" PRIu32
" "
594 "in %fs", first
, last
, bank_nr
, duration_elapsed(&bench
));
600 COMMAND_HANDLER(handle_flash_protect_command
)
603 return ERROR_COMMAND_SYNTAX_ERROR
;
609 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], bank_nr
);
610 struct flash_bank
*p
= get_flash_bank_by_num(bank_nr
);
614 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], first
);
615 if (strcmp(CMD_ARGV
[2], "last") == 0)
616 last
= p
->num_sectors
- 1;
618 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], last
);
621 COMMAND_PARSE_ON_OFF(CMD_ARGV
[3], set
);
624 if ((retval
= flash_check_sector_parameters(CMD_CTX
,
625 first
, last
, p
->num_sectors
)) != ERROR_OK
)
628 retval
= flash_driver_protect(p
, set
, first
, last
);
629 if (retval
== ERROR_OK
) {
630 command_print(CMD_CTX
, "%s protection for sectors %i "
631 "through %i on flash bank %i",
632 (set
) ? "set" : "cleared", (int) first
,
633 (int) last
, (int) bank_nr
);
639 COMMAND_HANDLER(handle_flash_write_image_command
)
641 struct target
*target
= get_current_target(CMD_CTX
);
650 return ERROR_COMMAND_SYNTAX_ERROR
;
653 /* flash auto-erase is disabled by default*/
655 bool auto_unlock
= false;
659 if (strcmp(CMD_ARGV
[0], "erase") == 0)
664 command_print(CMD_CTX
, "auto erase enabled");
665 } else if (strcmp(CMD_ARGV
[0], "unlock") == 0)
670 command_print(CMD_CTX
, "auto unlock enabled");
679 return ERROR_COMMAND_SYNTAX_ERROR
;
684 LOG_ERROR("no target selected");
688 struct duration bench
;
689 duration_start(&bench
);
693 image
.base_address_set
= 1;
694 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], image
.base_address
);
698 image
.base_address_set
= 0;
699 image
.base_address
= 0x0;
702 image
.start_address_set
= 0;
704 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
705 if (retval
!= ERROR_OK
)
710 retval
= flash_write_unlock(target
, &image
, &written
, auto_erase
, auto_unlock
);
711 if (retval
!= ERROR_OK
)
717 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
719 command_print(CMD_CTX
, "wrote %" PRIu32
" byte from file %s "
720 "in %fs (%0.3f kb/s)", written
, CMD_ARGV
[0],
721 duration_elapsed(&bench
), duration_kbps(&bench
, written
));
729 COMMAND_HANDLER(handle_flash_fill_command
)
736 uint32_t cur_size
= 0;
737 uint32_t chunk_count
;
738 struct target
*target
= get_current_target(CMD_CTX
);
741 int retval
= ERROR_OK
;
743 static size_t const chunksize
= 1024;
744 uint8_t *chunk
= malloc(chunksize
);
748 uint8_t *readback
= malloc(chunksize
);
749 if (readback
== NULL
)
758 retval
= ERROR_COMMAND_SYNTAX_ERROR
;
763 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
764 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], pattern
);
765 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], count
);
782 retval
= ERROR_COMMAND_SYNTAX_ERROR
;
786 chunk_count
= MIN(count
, (chunksize
/ wordsize
));
790 for (i
= 0; i
< chunk_count
; i
++)
792 target_buffer_set_u32(target
, chunk
+ i
* wordsize
, pattern
);
796 for (i
= 0; i
< chunk_count
; i
++)
798 target_buffer_set_u16(target
, chunk
+ i
* wordsize
, pattern
);
802 memset(chunk
, pattern
, chunk_count
);
805 LOG_ERROR("BUG: can't happen");
809 struct duration bench
;
810 duration_start(&bench
);
812 for (wrote
= 0; wrote
< (count
*wordsize
); wrote
+= cur_size
)
814 cur_size
= MIN((count
*wordsize
- wrote
), sizeof(chunk
));
815 struct flash_bank
*bank
;
816 bank
= get_flash_bank_by_addr(target
, address
);
822 err
= flash_driver_write(bank
, chunk
, address
- bank
->base
+ wrote
, cur_size
);
829 err
= target_read_buffer(target
, address
+ wrote
, cur_size
, readback
);
837 for (i
= 0; i
< cur_size
; i
++)
839 if (readback
[i
]!=chunk
[i
])
841 LOG_ERROR("Verfication error address 0x%08" PRIx32
", read back 0x%02x, expected 0x%02x",
842 address
+ wrote
+ i
, readback
[i
], chunk
[i
]);
849 if (duration_measure(&bench
) == ERROR_OK
)
851 command_print(CMD_CTX
, "wrote %" PRIu32
" bytes to 0x%8.8" PRIx32
852 " in %fs (%0.3f kb/s)", wrote
, address
,
853 duration_elapsed(&bench
), duration_kbps(&bench
, wrote
));
863 COMMAND_HANDLER(handle_flash_write_bank_command
)
867 struct fileio fileio
;
870 return ERROR_COMMAND_SYNTAX_ERROR
;
872 struct duration bench
;
873 duration_start(&bench
);
875 struct flash_bank
*p
;
876 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
877 if (ERROR_OK
!= retval
)
880 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], offset
);
882 if (fileio_open(&fileio
, CMD_ARGV
[1], FILEIO_READ
, FILEIO_BINARY
) != ERROR_OK
)
887 buffer
= malloc(fileio
.size
);
889 if (fileio_read(&fileio
, fileio
.size
, buffer
, &buf_cnt
) != ERROR_OK
)
892 fileio_close(&fileio
);
896 retval
= flash_driver_write(p
, buffer
, offset
, buf_cnt
);
901 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
903 command_print(CMD_CTX
, "wrote %zu byte from file %s to flash bank %u"
904 " at offset 0x%8.8" PRIx32
" in %fs (%0.3f kb/s)",
905 fileio
.size
, CMD_ARGV
[1], p
->bank_number
, offset
,
906 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
909 fileio_close(&fileio
);
914 void flash_set_dirty(void)
916 struct flash_bank
*c
;
919 /* set all flash to require erasing */
920 for (c
= flash_banks
; c
; c
= c
->next
)
922 for (i
= 0; i
< c
->num_sectors
; i
++)
924 c
->sectors
[i
].is_erased
= 0;
929 /* lookup flash bank by address */
930 struct flash_bank
*get_flash_bank_by_addr(struct target
*target
, uint32_t addr
)
932 struct flash_bank
*c
;
934 /* cycle through bank list */
935 for (c
= flash_banks
; c
; c
= c
->next
)
938 retval
= c
->driver
->auto_probe(c
);
940 if (retval
!= ERROR_OK
)
942 LOG_ERROR("auto_probe failed %d\n", retval
);
945 /* check whether address belongs to this flash bank */
946 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
949 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
953 /* erase given flash region, selects proper bank according to target and address */
954 static int flash_iterate_address_range(struct target
*target
, uint32_t addr
, uint32_t length
,
955 int (*callback
)(struct flash_bank
*bank
, int first
, int last
))
957 struct flash_bank
*c
;
962 if ((c
= get_flash_bank_by_addr(target
, addr
)) == NULL
)
963 return ERROR_FLASH_DST_OUT_OF_BANK
; /* no corresponding bank found */
965 if (c
->size
== 0 || c
->num_sectors
== 0)
967 LOG_ERROR("Bank is invalid");
968 return ERROR_FLASH_BANK_INVALID
;
973 /* special case, erase whole bank when length is zero */
975 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
977 return callback(c
, 0, c
->num_sectors
- 1);
980 /* check whether it fits */
981 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1)
982 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
986 for (i
= 0; i
< c
->num_sectors
; i
++)
988 /* check whether sector overlaps with the given range and is not yet erased */
989 if (addr
< c
->sectors
[i
].offset
+ c
->sectors
[i
].size
&& addr
+ length
> c
->sectors
[i
].offset
&& c
->sectors
[i
].is_erased
!= 1) {
990 /* if first is not set yet then this is the first sector */
993 last
= i
; /* and it is the last one so far in any case */
997 if (first
== -1 || last
== -1)
1000 return callback(c
, first
, last
);
1005 int flash_erase_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
1007 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_erase
);
1010 static int flash_driver_unprotect(struct flash_bank
*bank
, int first
, int last
)
1012 return flash_driver_protect(bank
, 0, first
, last
);
1015 static int flash_unlock_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
1017 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_unprotect
);
1021 /* write (optional verify) an image to flash memory of the given target */
1022 static int flash_write_unlock(struct target
*target
, struct image
*image
, uint32_t *written
, int erase
, bool unlock
)
1024 int retval
= ERROR_OK
;
1027 uint32_t section_offset
;
1028 struct flash_bank
*c
;
1039 /* assume all sectors need erasing - stops any problems
1040 * when flash_write is called multiple times */
1045 /* allocate padding array */
1046 padding
= malloc(image
->num_sections
* sizeof(padding
));
1048 /* loop until we reach end of the image */
1049 while (section
< image
->num_sections
)
1051 uint32_t buffer_size
;
1055 uint32_t run_address
= image
->sections
[section
].base_address
+ section_offset
;
1056 uint32_t run_size
= image
->sections
[section
].size
- section_offset
;
1059 if (image
->sections
[section
].size
== 0)
1061 LOG_WARNING("empty section %d", section
);
1067 /* find the corresponding flash bank */
1068 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
1070 section
++; /* and skip it */
1075 /* collect consecutive sections which fall into the same bank */
1076 section_first
= section
;
1077 section_last
= section
;
1078 padding
[section
] = 0;
1079 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1)
1080 && (section_last
+ 1 < image
->num_sections
))
1082 if (image
->sections
[section_last
+ 1].base_address
< (run_address
+ run_size
))
1084 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last
+ 1);
1087 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
1088 * attempt to rebuild a consecutive buffer for the flash loader */
1089 pad_bytes
= (image
->sections
[section_last
+ 1].base_address
) - (run_address
+ run_size
);
1090 if ((run_address
+ run_size
+ pad_bytes
) > (c
->base
+ c
->size
))
1092 padding
[section_last
] = pad_bytes
;
1093 run_size
+= image
->sections
[++section_last
].size
;
1094 run_size
+= pad_bytes
;
1095 padding
[section_last
] = 0;
1097 LOG_INFO("Padding image section %d with %d bytes", section_last
-1, pad_bytes
);
1100 /* fit the run into bank constraints */
1101 if (run_address
+ run_size
- 1 > c
->base
+ c
->size
- 1)
1103 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
1104 (int)(c
->base
+ c
->size
- run_address
), (int)(run_size
), (int)(c
->size
));
1105 run_size
= c
->base
+ c
->size
- run_address
;
1108 /* allocate buffer */
1109 buffer
= malloc(run_size
);
1112 /* read sections to the buffer */
1113 while (buffer_size
< run_size
)
1117 size_read
= run_size
- buffer_size
;
1118 if (size_read
> image
->sections
[section
].size
- section_offset
)
1119 size_read
= image
->sections
[section
].size
- section_offset
;
1121 if ((retval
= image_read_section(image
, section
, section_offset
,
1122 size_read
, buffer
+ buffer_size
, &size_read
)) != ERROR_OK
|| size_read
== 0)
1129 /* see if we need to pad the section */
1130 while (padding
[section
]--)
1131 (buffer
+ buffer_size
)[size_read
++] = 0xff;
1133 buffer_size
+= size_read
;
1134 section_offset
+= size_read
;
1136 if (section_offset
>= image
->sections
[section
].size
)
1147 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
1149 if (retval
== ERROR_OK
)
1153 /* calculate and erase sectors */
1154 retval
= flash_erase_address_range(target
, run_address
, run_size
);
1158 if (retval
== ERROR_OK
)
1160 /* write flash sectors */
1161 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
1166 if (retval
!= ERROR_OK
)
1169 return retval
; /* abort operation */
1172 if (written
!= NULL
)
1173 *written
+= run_size
; /* add run size to total written counter */
1181 int flash_write(struct target
*target
, struct image
*image
, uint32_t *written
, int erase
)
1183 return flash_write_unlock(target
, image
, written
, erase
, false);
1186 int default_flash_mem_blank_check(struct flash_bank
*bank
)
1188 struct target
*target
= bank
->target
;
1189 const int buffer_size
= 1024;
1192 int retval
= ERROR_OK
;
1194 if (bank
->target
->state
!= TARGET_HALTED
)
1196 LOG_ERROR("Target not halted");
1197 return ERROR_TARGET_NOT_HALTED
;
1200 uint8_t *buffer
= malloc(buffer_size
);
1202 for (i
= 0; i
< bank
->num_sectors
; i
++)
1205 bank
->sectors
[i
].is_erased
= 1;
1207 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
1210 chunk
= buffer_size
;
1211 if (chunk
> (j
- bank
->sectors
[i
].size
))
1213 chunk
= (j
- bank
->sectors
[i
].size
);
1216 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
1217 if (retval
!= ERROR_OK
)
1222 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
1224 if (buffer
[nBytes
] != 0xFF)
1226 bank
->sectors
[i
].is_erased
= 0;
1239 int default_flash_blank_check(struct flash_bank
*bank
)
1241 struct target
*target
= bank
->target
;
1247 if (bank
->target
->state
!= TARGET_HALTED
)
1249 LOG_ERROR("Target not halted");
1250 return ERROR_TARGET_NOT_HALTED
;
1253 for (i
= 0; i
< bank
->num_sectors
; i
++)
1255 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
1256 uint32_t size
= bank
->sectors
[i
].size
;
1258 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
1264 bank
->sectors
[i
].is_erased
= 1;
1266 bank
->sectors
[i
].is_erased
= 0;
1272 LOG_USER("Running slow fallback erase check - add working memory");
1273 return default_flash_mem_blank_check(bank
);
1279 static const struct command_registration flash_exec_command_handlers
[] = {
1282 .handler
= &handle_flash_probe_command
,
1283 .mode
= COMMAND_EXEC
,
1285 .help
= "identify flash bank",
1289 .handler
= &handle_flash_info_command
,
1290 .mode
= COMMAND_EXEC
,
1292 .help
= "print bank information",
1295 .name
= "erase_check",
1296 .handler
= &handle_flash_erase_check_command
,
1297 .mode
= COMMAND_EXEC
,
1299 .help
= "check erase state of sectors",
1302 .name
= "protect_check",
1303 .handler
= &handle_flash_protect_check_command
,
1304 .mode
= COMMAND_EXEC
,
1306 .help
= "check protection state of sectors",
1309 .name
= "erase_sector",
1310 .handler
= &handle_flash_erase_command
,
1311 .mode
= COMMAND_EXEC
,
1312 .usage
= "<bank> <first> <last>",
1313 .help
= "erase sectors",
1316 .name
= "erase_address",
1317 .handler
= &handle_flash_erase_address_command
,
1318 .mode
= COMMAND_EXEC
,
1319 .usage
= "<address> <length>",
1320 .help
= "erase address range",
1325 .handler
= &handle_flash_fill_command
,
1326 .mode
= COMMAND_EXEC
,
1327 .usage
= "<bank> <address> <word_pattern> <count>",
1328 .help
= "fill with pattern (no autoerase)",
1332 .handler
= &handle_flash_fill_command
,
1333 .mode
= COMMAND_EXEC
,
1334 .usage
= "<bank> <address> <halfword_pattern> <count>",
1335 .help
= "fill with pattern",
1339 .handler
= &handle_flash_fill_command
,
1340 .mode
= COMMAND_EXEC
,
1341 .usage
= "<bank> <address> <byte_pattern> <count>",
1342 .help
= "fill with pattern",
1346 .name
= "write_bank",
1347 .handler
= &handle_flash_write_bank_command
,
1348 .mode
= COMMAND_EXEC
,
1349 .usage
= "<bank> <file> <offset>",
1350 .help
= "write binary data",
1353 .name
= "write_image",
1354 .handler
= &handle_flash_write_image_command
,
1355 .mode
= COMMAND_EXEC
,
1356 .usage
= "<bank> [erase] [unlock] <file> [offset] [type]",
1357 .help
= "write an image to flash"
1361 .handler
= &handle_flash_protect_command
,
1362 .mode
= COMMAND_EXEC
,
1363 .usage
= "<bank> <first> <last> <on | off>",
1364 .help
= "set protection of sectors",
1366 COMMAND_REGISTRATION_DONE
1369 int flash_init_drivers(struct command_context
*cmd_ctx
)
1374 struct command
*parent
= command_find_in_context(cmd_ctx
, "flash");
1375 return register_commands(cmd_ctx
, parent
, flash_exec_command_handlers
);
1378 COMMAND_HANDLER(handle_flash_init_command
)
1381 return ERROR_COMMAND_SYNTAX_ERROR
;
1383 static bool flash_initialized
= false;
1384 if (flash_initialized
)
1386 LOG_INFO("'flash init' has already been called");
1389 flash_initialized
= true;
1391 LOG_DEBUG("Initializing flash devices...");
1392 return flash_init_drivers(CMD_CTX
);
1395 static const struct command_registration flash_config_command_handlers
[] = {
1398 .handler
= &handle_flash_bank_command
,
1399 .mode
= COMMAND_CONFIG
,
1400 .usage
= "<name> <driver> <base> <size> "
1401 "<chip_width> <bus_width> <target> "
1402 "[driver_options ...]",
1403 .help
= "Define a new bank with the given name, "
1404 "using the specified NOR flash driver.",
1408 .mode
= COMMAND_CONFIG
,
1409 .handler
= &handle_flash_init_command
,
1410 .help
= "initialize flash devices",
1414 .mode
= COMMAND_ANY
,
1415 .jim_handler
= &jim_flash_banks
,
1416 .help
= "return information about the flash banks",
1418 COMMAND_REGISTRATION_DONE
1420 static const struct command_registration flash_command_handlers
[] = {
1423 .mode
= COMMAND_ANY
,
1424 .help
= "NOR flash command group",
1425 .chain
= flash_config_command_handlers
,
1427 COMMAND_REGISTRATION_DONE
1430 int flash_register_commands(struct command_context
*cmd_ctx
)
1432 return register_commands(cmd_ctx
, NULL
, flash_command_handlers
);