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 <target/image.h>
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 struct flash_bank
*get_flash_bank_by_num_noprobe(int num
)
125 struct flash_bank
*p
;
128 for (p
= flash_banks
; p
; p
= p
->next
)
135 LOG_ERROR("flash bank %d does not exist", num
);
139 int flash_get_bank_count(void)
141 struct flash_bank
*p
;
143 for (p
= flash_banks
; p
; p
= p
->next
)
150 struct flash_bank
*get_flash_bank_by_name(const char *name
)
152 unsigned requested
= get_flash_name_index(name
);
155 struct flash_bank
*bank
;
156 for (bank
= flash_banks
; NULL
!= bank
; bank
= bank
->next
)
158 if (strcmp(bank
->name
, name
) == 0)
160 if (!flash_driver_name_matches(bank
->driver
->name
, name
))
162 if (++found
< requested
)
169 struct flash_bank
*get_flash_bank_by_num(int num
)
171 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(num
);
177 retval
= p
->driver
->auto_probe(p
);
179 if (retval
!= ERROR_OK
)
181 LOG_ERROR("auto_probe failed %d\n", retval
);
187 COMMAND_HELPER(flash_command_get_bank
, unsigned name_index
,
188 struct flash_bank
**bank
)
190 const char *name
= CMD_ARGV
[name_index
];
191 *bank
= get_flash_bank_by_name(name
);
196 COMMAND_PARSE_NUMBER(uint
, name
, bank_num
);
198 *bank
= get_flash_bank_by_num(bank_num
);
201 command_print(CMD_CTX
, "flash bank '%s' not found", name
);
202 return ERROR_INVALID_ARGUMENTS
;
208 COMMAND_HANDLER(handle_flash_info_command
)
210 struct flash_bank
*p
;
216 return ERROR_COMMAND_SYNTAX_ERROR
;
219 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], bank_nr
);
221 for (p
= flash_banks
; p
; p
= p
->next
, i
++)
228 /* attempt auto probe */
229 if ((retval
= p
->driver
->auto_probe(p
)) != ERROR_OK
)
232 command_print(CMD_CTX
,
233 "#%" PRIi32
" : %s at 0x%8.8" PRIx32
", size 0x%8.8" PRIx32
", buswidth %i, chipwidth %i",
240 for (j
= 0; j
< p
->num_sectors
; j
++)
244 if (p
->sectors
[j
].is_protected
== 0)
245 protect_state
= "not protected";
246 else if (p
->sectors
[j
].is_protected
== 1)
247 protect_state
= "protected";
249 protect_state
= "protection state unknown";
251 command_print(CMD_CTX
,
252 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIi32
"kB) %s",
254 p
->sectors
[j
].offset
,
256 p
->sectors
[j
].size
>> 10,
260 *buf
= '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
261 retval
= p
->driver
->info(p
, buf
, sizeof(buf
));
262 command_print(CMD_CTX
, "%s", buf
);
263 if (retval
!= ERROR_OK
)
264 LOG_ERROR("error retrieving flash info (%d)", retval
);
270 COMMAND_HANDLER(handle_flash_probe_command
)
276 return ERROR_COMMAND_SYNTAX_ERROR
;
280 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], bank_nr
);
281 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(bank_nr
);
284 if ((retval
= p
->driver
->probe(p
)) == ERROR_OK
)
286 command_print(CMD_CTX
, "flash '%s' found at 0x%8.8" PRIx32
, p
->driver
->name
, p
->base
);
288 else if (retval
== ERROR_FLASH_BANK_INVALID
)
290 command_print(CMD_CTX
, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32
,
291 CMD_ARGV
[0], p
->base
);
295 command_print(CMD_CTX
, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32
,
296 CMD_ARGV
[0], p
->base
);
301 command_print(CMD_CTX
, "flash bank '#%s' is out of bounds", CMD_ARGV
[0]);
307 COMMAND_HANDLER(handle_flash_erase_check_command
)
311 return ERROR_COMMAND_SYNTAX_ERROR
;
314 struct flash_bank
*p
;
315 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
316 if (ERROR_OK
!= retval
)
320 if ((retval
= p
->driver
->erase_check(p
)) == ERROR_OK
)
322 command_print(CMD_CTX
, "successfully checked erase state");
326 command_print(CMD_CTX
, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32
,
327 CMD_ARGV
[0], p
->base
);
330 for (j
= 0; j
< p
->num_sectors
; j
++)
334 if (p
->sectors
[j
].is_erased
== 0)
335 erase_state
= "not erased";
336 else if (p
->sectors
[j
].is_erased
== 1)
337 erase_state
= "erased";
339 erase_state
= "erase 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,
353 COMMAND_HANDLER(handle_flash_erase_address_command
)
355 struct flash_bank
*p
;
360 struct target
*target
= get_current_target(CMD_CTX
);
363 return ERROR_COMMAND_SYNTAX_ERROR
;
365 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], address
);
366 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], length
);
369 command_print(CMD_CTX
, "Length must be >0");
370 return ERROR_COMMAND_SYNTAX_ERROR
;
373 p
= get_flash_bank_by_addr(target
, address
);
379 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
382 struct duration bench
;
383 duration_start(&bench
);
385 retval
= flash_erase_address_range(target
, address
, length
);
387 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
389 command_print(CMD_CTX
, "erased address 0x%8.8x (length %i)"
390 " in %fs (%0.3f kb/s)", address
, length
,
391 duration_elapsed(&bench
), duration_kbps(&bench
, length
));
397 COMMAND_HANDLER(handle_flash_protect_check_command
)
400 return ERROR_COMMAND_SYNTAX_ERROR
;
402 struct flash_bank
*p
;
403 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
404 if (ERROR_OK
!= retval
)
407 if ((retval
= p
->driver
->protect_check(p
)) == ERROR_OK
)
409 command_print(CMD_CTX
, "successfully checked protect state");
411 else if (retval
== ERROR_FLASH_OPERATION_FAILED
)
413 command_print(CMD_CTX
, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32
, CMD_ARGV
[0], p
->base
);
417 command_print(CMD_CTX
, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32
, CMD_ARGV
[0], p
->base
);
423 static int flash_check_sector_parameters(struct command_context
*cmd_ctx
,
424 uint32_t first
, uint32_t last
, uint32_t num_sectors
)
426 if (!(first
<= last
)) {
427 command_print(cmd_ctx
, "ERROR: "
428 "first sector must be <= last sector");
432 if (!(last
<= (num_sectors
- 1))) {
433 command_print(cmd_ctx
, "ERROR: last sector must be <= %d",
434 (int) num_sectors
- 1);
441 COMMAND_HANDLER(handle_flash_erase_command
)
444 return ERROR_COMMAND_SYNTAX_ERROR
;
450 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], bank_nr
);
451 struct flash_bank
*p
= get_flash_bank_by_num(bank_nr
);
455 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], first
);
456 if (strcmp(CMD_ARGV
[2], "last") == 0)
457 last
= p
->num_sectors
- 1;
459 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], last
);
462 if ((retval
= flash_check_sector_parameters(CMD_CTX
,
463 first
, last
, p
->num_sectors
)) != ERROR_OK
)
466 struct duration bench
;
467 duration_start(&bench
);
469 retval
= flash_driver_erase(p
, first
, last
);
471 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
473 command_print(CMD_CTX
, "erased sectors %" PRIu32
" "
474 "through %" PRIu32
" on flash bank %" PRIu32
" "
475 "in %fs", first
, last
, bank_nr
, duration_elapsed(&bench
));
481 COMMAND_HANDLER(handle_flash_protect_command
)
484 return ERROR_COMMAND_SYNTAX_ERROR
;
490 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], bank_nr
);
491 struct flash_bank
*p
= get_flash_bank_by_num(bank_nr
);
495 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], first
);
496 if (strcmp(CMD_ARGV
[2], "last") == 0)
497 last
= p
->num_sectors
- 1;
499 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], last
);
502 COMMAND_PARSE_ON_OFF(CMD_ARGV
[3], set
);
505 if ((retval
= flash_check_sector_parameters(CMD_CTX
,
506 first
, last
, p
->num_sectors
)) != ERROR_OK
)
509 retval
= flash_driver_protect(p
, set
, first
, last
);
510 if (retval
== ERROR_OK
) {
511 command_print(CMD_CTX
, "%s protection for sectors %i "
512 "through %i on flash bank %i",
513 (set
) ? "set" : "cleared", (int) first
,
514 (int) last
, (int) bank_nr
);
520 COMMAND_HANDLER(handle_flash_write_image_command
)
522 struct target
*target
= get_current_target(CMD_CTX
);
531 return ERROR_COMMAND_SYNTAX_ERROR
;
534 /* flash auto-erase is disabled by default*/
536 bool auto_unlock
= false;
540 if (strcmp(CMD_ARGV
[0], "erase") == 0)
545 command_print(CMD_CTX
, "auto erase enabled");
546 } else if (strcmp(CMD_ARGV
[0], "unlock") == 0)
551 command_print(CMD_CTX
, "auto unlock enabled");
560 return ERROR_COMMAND_SYNTAX_ERROR
;
565 LOG_ERROR("no target selected");
569 struct duration bench
;
570 duration_start(&bench
);
574 image
.base_address_set
= 1;
575 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], image
.base_address
);
579 image
.base_address_set
= 0;
580 image
.base_address
= 0x0;
583 image
.start_address_set
= 0;
585 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
586 if (retval
!= ERROR_OK
)
591 retval
= flash_write_unlock(target
, &image
, &written
, auto_erase
, auto_unlock
);
592 if (retval
!= ERROR_OK
)
598 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
600 command_print(CMD_CTX
, "wrote %" PRIu32
" byte from file %s "
601 "in %fs (%0.3f kb/s)", written
, CMD_ARGV
[0],
602 duration_elapsed(&bench
), duration_kbps(&bench
, written
));
610 COMMAND_HANDLER(handle_flash_fill_command
)
617 uint32_t cur_size
= 0;
618 uint32_t chunk_count
;
619 struct target
*target
= get_current_target(CMD_CTX
);
622 int retval
= ERROR_OK
;
624 static size_t const chunksize
= 1024;
625 uint8_t *chunk
= malloc(chunksize
);
629 uint8_t *readback
= malloc(chunksize
);
630 if (readback
== NULL
)
639 retval
= ERROR_COMMAND_SYNTAX_ERROR
;
644 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
645 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], pattern
);
646 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], count
);
663 retval
= ERROR_COMMAND_SYNTAX_ERROR
;
667 chunk_count
= MIN(count
, (chunksize
/ wordsize
));
671 for (i
= 0; i
< chunk_count
; i
++)
673 target_buffer_set_u32(target
, chunk
+ i
* wordsize
, pattern
);
677 for (i
= 0; i
< chunk_count
; i
++)
679 target_buffer_set_u16(target
, chunk
+ i
* wordsize
, pattern
);
683 memset(chunk
, pattern
, chunk_count
);
686 LOG_ERROR("BUG: can't happen");
690 struct duration bench
;
691 duration_start(&bench
);
693 for (wrote
= 0; wrote
< (count
*wordsize
); wrote
+= cur_size
)
695 cur_size
= MIN((count
*wordsize
- wrote
), sizeof(chunk
));
696 struct flash_bank
*bank
;
697 bank
= get_flash_bank_by_addr(target
, address
);
703 err
= flash_driver_write(bank
, chunk
, address
- bank
->base
+ wrote
, cur_size
);
710 err
= target_read_buffer(target
, address
+ wrote
, cur_size
, readback
);
718 for (i
= 0; i
< cur_size
; i
++)
720 if (readback
[i
]!=chunk
[i
])
722 LOG_ERROR("Verfication error address 0x%08" PRIx32
", read back 0x%02x, expected 0x%02x",
723 address
+ wrote
+ i
, readback
[i
], chunk
[i
]);
730 if (duration_measure(&bench
) == ERROR_OK
)
732 command_print(CMD_CTX
, "wrote %" PRIu32
" bytes to 0x%8.8" PRIx32
733 " in %fs (%0.3f kb/s)", wrote
, address
,
734 duration_elapsed(&bench
), duration_kbps(&bench
, wrote
));
744 COMMAND_HANDLER(handle_flash_write_bank_command
)
748 struct fileio fileio
;
751 return ERROR_COMMAND_SYNTAX_ERROR
;
753 struct duration bench
;
754 duration_start(&bench
);
756 struct flash_bank
*p
;
757 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &p
);
758 if (ERROR_OK
!= retval
)
761 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], offset
);
763 if (fileio_open(&fileio
, CMD_ARGV
[1], FILEIO_READ
, FILEIO_BINARY
) != ERROR_OK
)
768 buffer
= malloc(fileio
.size
);
770 if (fileio_read(&fileio
, fileio
.size
, buffer
, &buf_cnt
) != ERROR_OK
)
773 fileio_close(&fileio
);
777 retval
= flash_driver_write(p
, buffer
, offset
, buf_cnt
);
782 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
784 command_print(CMD_CTX
, "wrote %zu byte from file %s to flash bank %u"
785 " at offset 0x%8.8" PRIx32
" in %fs (%0.3f kb/s)",
786 fileio
.size
, CMD_ARGV
[1], p
->bank_number
, offset
,
787 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
790 fileio_close(&fileio
);
795 void flash_set_dirty(void)
797 struct flash_bank
*c
;
800 /* set all flash to require erasing */
801 for (c
= flash_banks
; c
; c
= c
->next
)
803 for (i
= 0; i
< c
->num_sectors
; i
++)
805 c
->sectors
[i
].is_erased
= 0;
810 /* lookup flash bank by address */
811 struct flash_bank
*get_flash_bank_by_addr(struct target
*target
, uint32_t addr
)
813 struct flash_bank
*c
;
815 /* cycle through bank list */
816 for (c
= flash_banks
; c
; c
= c
->next
)
819 retval
= c
->driver
->auto_probe(c
);
821 if (retval
!= ERROR_OK
)
823 LOG_ERROR("auto_probe failed %d\n", retval
);
826 /* check whether address belongs to this flash bank */
827 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
830 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
834 /* erase given flash region, selects proper bank according to target and address */
835 static int flash_iterate_address_range(struct target
*target
, uint32_t addr
, uint32_t length
,
836 int (*callback
)(struct flash_bank
*bank
, int first
, int last
))
838 struct flash_bank
*c
;
843 if ((c
= get_flash_bank_by_addr(target
, addr
)) == NULL
)
844 return ERROR_FLASH_DST_OUT_OF_BANK
; /* no corresponding bank found */
846 if (c
->size
== 0 || c
->num_sectors
== 0)
848 LOG_ERROR("Bank is invalid");
849 return ERROR_FLASH_BANK_INVALID
;
854 /* special case, erase whole bank when length is zero */
856 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
858 return callback(c
, 0, c
->num_sectors
- 1);
861 /* check whether it fits */
862 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1)
863 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
867 for (i
= 0; i
< c
->num_sectors
; i
++)
869 /* check whether sector overlaps with the given range and is not yet erased */
870 if (addr
< c
->sectors
[i
].offset
+ c
->sectors
[i
].size
&& addr
+ length
> c
->sectors
[i
].offset
&& c
->sectors
[i
].is_erased
!= 1) {
871 /* if first is not set yet then this is the first sector */
874 last
= i
; /* and it is the last one so far in any case */
878 if (first
== -1 || last
== -1)
881 return callback(c
, first
, last
);
886 int flash_erase_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
888 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_erase
);
891 static int flash_driver_unprotect(struct flash_bank
*bank
, int first
, int last
)
893 return flash_driver_protect(bank
, 0, first
, last
);
896 static int flash_unlock_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
898 return flash_iterate_address_range(target
, addr
, length
, &flash_driver_unprotect
);
902 /* write (optional verify) an image to flash memory of the given target */
903 static int flash_write_unlock(struct target
*target
, struct image
*image
, uint32_t *written
, int erase
, bool unlock
)
905 int retval
= ERROR_OK
;
908 uint32_t section_offset
;
909 struct flash_bank
*c
;
920 /* assume all sectors need erasing - stops any problems
921 * when flash_write is called multiple times */
926 /* allocate padding array */
927 padding
= malloc(image
->num_sections
* sizeof(padding
));
929 /* loop until we reach end of the image */
930 while (section
< image
->num_sections
)
932 uint32_t buffer_size
;
936 uint32_t run_address
= image
->sections
[section
].base_address
+ section_offset
;
937 uint32_t run_size
= image
->sections
[section
].size
- section_offset
;
940 if (image
->sections
[section
].size
== 0)
942 LOG_WARNING("empty section %d", section
);
948 /* find the corresponding flash bank */
949 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
951 section
++; /* and skip it */
956 /* collect consecutive sections which fall into the same bank */
957 section_first
= section
;
958 section_last
= section
;
959 padding
[section
] = 0;
960 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1)
961 && (section_last
+ 1 < image
->num_sections
))
963 if (image
->sections
[section_last
+ 1].base_address
< (run_address
+ run_size
))
965 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last
+ 1);
968 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
969 * attempt to rebuild a consecutive buffer for the flash loader */
970 pad_bytes
= (image
->sections
[section_last
+ 1].base_address
) - (run_address
+ run_size
);
971 if ((run_address
+ run_size
+ pad_bytes
) > (c
->base
+ c
->size
))
973 padding
[section_last
] = pad_bytes
;
974 run_size
+= image
->sections
[++section_last
].size
;
975 run_size
+= pad_bytes
;
976 padding
[section_last
] = 0;
978 LOG_INFO("Padding image section %d with %d bytes", section_last
-1, pad_bytes
);
981 /* fit the run into bank constraints */
982 if (run_address
+ run_size
- 1 > c
->base
+ c
->size
- 1)
984 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
985 (int)(c
->base
+ c
->size
- run_address
), (int)(run_size
), (int)(c
->size
));
986 run_size
= c
->base
+ c
->size
- run_address
;
989 /* allocate buffer */
990 buffer
= malloc(run_size
);
993 /* read sections to the buffer */
994 while (buffer_size
< run_size
)
998 size_read
= run_size
- buffer_size
;
999 if (size_read
> image
->sections
[section
].size
- section_offset
)
1000 size_read
= image
->sections
[section
].size
- section_offset
;
1002 if ((retval
= image_read_section(image
, section
, section_offset
,
1003 size_read
, buffer
+ buffer_size
, &size_read
)) != ERROR_OK
|| size_read
== 0)
1010 /* see if we need to pad the section */
1011 while (padding
[section
]--)
1012 (buffer
+ buffer_size
)[size_read
++] = 0xff;
1014 buffer_size
+= size_read
;
1015 section_offset
+= size_read
;
1017 if (section_offset
>= image
->sections
[section
].size
)
1028 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
1030 if (retval
== ERROR_OK
)
1034 /* calculate and erase sectors */
1035 retval
= flash_erase_address_range(target
, run_address
, run_size
);
1039 if (retval
== ERROR_OK
)
1041 /* write flash sectors */
1042 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
1047 if (retval
!= ERROR_OK
)
1050 return retval
; /* abort operation */
1053 if (written
!= NULL
)
1054 *written
+= run_size
; /* add run size to total written counter */
1062 int flash_write(struct target
*target
, struct image
*image
, uint32_t *written
, int erase
)
1064 return flash_write_unlock(target
, image
, written
, erase
, false);
1067 int default_flash_mem_blank_check(struct flash_bank
*bank
)
1069 struct target
*target
= bank
->target
;
1070 const int buffer_size
= 1024;
1073 int retval
= ERROR_OK
;
1075 if (bank
->target
->state
!= TARGET_HALTED
)
1077 LOG_ERROR("Target not halted");
1078 return ERROR_TARGET_NOT_HALTED
;
1081 uint8_t *buffer
= malloc(buffer_size
);
1083 for (i
= 0; i
< bank
->num_sectors
; i
++)
1086 bank
->sectors
[i
].is_erased
= 1;
1088 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
1091 chunk
= buffer_size
;
1092 if (chunk
> (j
- bank
->sectors
[i
].size
))
1094 chunk
= (j
- bank
->sectors
[i
].size
);
1097 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
1098 if (retval
!= ERROR_OK
)
1103 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
1105 if (buffer
[nBytes
] != 0xFF)
1107 bank
->sectors
[i
].is_erased
= 0;
1120 int default_flash_blank_check(struct flash_bank
*bank
)
1122 struct target
*target
= bank
->target
;
1128 if (bank
->target
->state
!= TARGET_HALTED
)
1130 LOG_ERROR("Target not halted");
1131 return ERROR_TARGET_NOT_HALTED
;
1134 for (i
= 0; i
< bank
->num_sectors
; i
++)
1136 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
1137 uint32_t size
= bank
->sectors
[i
].size
;
1139 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
1145 bank
->sectors
[i
].is_erased
= 1;
1147 bank
->sectors
[i
].is_erased
= 0;
1153 LOG_USER("Running slow fallback erase check - add working memory");
1154 return default_flash_mem_blank_check(bank
);
1160 static const struct command_registration flash_exec_command_handlers
[] = {
1163 .handler
= &handle_flash_probe_command
,
1164 .mode
= COMMAND_EXEC
,
1166 .help
= "identify flash bank",
1170 .handler
= &handle_flash_info_command
,
1171 .mode
= COMMAND_EXEC
,
1173 .help
= "print bank information",
1176 .name
= "erase_check",
1177 .handler
= &handle_flash_erase_check_command
,
1178 .mode
= COMMAND_EXEC
,
1180 .help
= "check erase state of sectors",
1183 .name
= "protect_check",
1184 .handler
= &handle_flash_protect_check_command
,
1185 .mode
= COMMAND_EXEC
,
1187 .help
= "check protection state of sectors",
1190 .name
= "erase_sector",
1191 .handler
= &handle_flash_erase_command
,
1192 .mode
= COMMAND_EXEC
,
1193 .usage
= "<bank> <first> <last>",
1194 .help
= "erase sectors",
1197 .name
= "erase_address",
1198 .handler
= &handle_flash_erase_address_command
,
1199 .mode
= COMMAND_EXEC
,
1200 .usage
= "<address> <length>",
1201 .help
= "erase address range",
1206 .handler
= &handle_flash_fill_command
,
1207 .mode
= COMMAND_EXEC
,
1208 .usage
= "<bank> <address> <word_pattern> <count>",
1209 .help
= "fill with pattern (no autoerase)",
1213 .handler
= &handle_flash_fill_command
,
1214 .mode
= COMMAND_EXEC
,
1215 .usage
= "<bank> <address> <halfword_pattern> <count>",
1216 .help
= "fill with pattern",
1220 .handler
= &handle_flash_fill_command
,
1221 .mode
= COMMAND_EXEC
,
1222 .usage
= "<bank> <address> <byte_pattern> <count>",
1223 .help
= "fill with pattern",
1227 .name
= "write_bank",
1228 .handler
= &handle_flash_write_bank_command
,
1229 .mode
= COMMAND_EXEC
,
1230 .usage
= "<bank> <file> <offset>",
1231 .help
= "write binary data",
1234 .name
= "write_image",
1235 .handler
= &handle_flash_write_image_command
,
1236 .mode
= COMMAND_EXEC
,
1237 .usage
= "<bank> [erase] [unlock] <file> [offset] [type]",
1238 .help
= "write an image to flash"
1242 .handler
= &handle_flash_protect_command
,
1243 .mode
= COMMAND_EXEC
,
1244 .usage
= "<bank> <first> <last> <on | off>",
1245 .help
= "set protection of sectors",
1247 COMMAND_REGISTRATION_DONE
1250 int flash_init_drivers(struct command_context
*cmd_ctx
)
1255 struct command
*parent
= command_find_in_context(cmd_ctx
, "flash");
1256 return register_commands(cmd_ctx
, parent
, flash_exec_command_handlers
);