1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
3 * Copyright (C) 2007-2010 Øyvind Harboe <oyvind.harboe@zylin.com> *
4 * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
5 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
6 * Copyright (C) 2010 by Antonio Borneo <borneo.antonio@gmail.com> *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
25 #include <flash/common.h>
26 #include <flash/nor/core.h>
27 #include <flash/nor/imp.h>
28 #include <target/image.h>
32 * Upper level of NOR flash framework.
33 * The lower level interfaces are to drivers. These upper level ones
34 * primarily support access from Tcl scripts or from GDB.
37 static struct flash_bank
*flash_banks
;
39 int flash_driver_erase(struct flash_bank
*bank
, int first
, int last
)
43 retval
= bank
->driver
->erase(bank
, first
, last
);
44 if (retval
!= ERROR_OK
)
45 LOG_ERROR("failed erasing sectors %d to %d", first
, last
);
50 int flash_driver_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
54 /* callers may not supply illegal parameters ... */
55 if (first
< 0 || first
> last
|| last
>= bank
->num_sectors
) {
56 LOG_ERROR("illegal sector range");
60 /* force "set" to 0/1 */
65 * We must not use any cached information about protection state!!!!
67 * There are a million things that could change the protect state:
69 * the target could have reset, power cycled, been hot plugged,
70 * the application could have run, etc.
72 * Drivers only receive valid sector range.
74 retval
= bank
->driver
->protect(bank
, set
, first
, last
);
75 if (retval
!= ERROR_OK
)
76 LOG_ERROR("failed setting protection for areas %d to %d", first
, last
);
81 int flash_driver_write(struct flash_bank
*bank
,
82 uint8_t *buffer
, uint32_t offset
, uint32_t count
)
86 retval
= bank
->driver
->write(bank
, buffer
, offset
, count
);
87 if (retval
!= ERROR_OK
) {
89 "error writing to flash at address 0x%08" PRIx32
" at offset 0x%8.8" PRIx32
,
97 int flash_driver_read(struct flash_bank
*bank
,
98 uint8_t *buffer
, uint32_t offset
, uint32_t count
)
102 LOG_DEBUG("call flash_driver_read()");
104 retval
= bank
->driver
->read(bank
, buffer
, offset
, count
);
105 if (retval
!= ERROR_OK
) {
107 "error reading to flash at address 0x%08" PRIx32
" at offset 0x%8.8" PRIx32
,
115 int default_flash_read(struct flash_bank
*bank
,
116 uint8_t *buffer
, uint32_t offset
, uint32_t count
)
118 return target_read_buffer(bank
->target
, offset
+ bank
->base
, count
, buffer
);
121 void flash_bank_add(struct flash_bank
*bank
)
123 /* put flash bank in linked list */
124 unsigned bank_num
= 0;
126 /* find last flash bank */
127 struct flash_bank
*p
= flash_banks
;
128 while (NULL
!= p
->next
) {
137 bank
->bank_number
= bank_num
;
140 struct flash_bank
*flash_bank_list(void)
145 struct flash_bank
*get_flash_bank_by_num_noprobe(int num
)
147 struct flash_bank
*p
;
150 for (p
= flash_banks
; p
; p
= p
->next
) {
154 LOG_ERROR("flash bank %d does not exist", num
);
158 int flash_get_bank_count(void)
160 struct flash_bank
*p
;
162 for (p
= flash_banks
; p
; p
= p
->next
)
167 struct flash_bank
*get_flash_bank_by_name_noprobe(const char *name
)
169 unsigned requested
= get_flash_name_index(name
);
172 struct flash_bank
*bank
;
173 for (bank
= flash_banks
; NULL
!= bank
; bank
= bank
->next
) {
174 if (strcmp(bank
->name
, name
) == 0)
176 if (!flash_driver_name_matches(bank
->driver
->name
, name
))
178 if (++found
< requested
)
185 int get_flash_bank_by_name(const char *name
, struct flash_bank
**bank_result
)
187 struct flash_bank
*bank
;
190 bank
= get_flash_bank_by_name_noprobe(name
);
192 retval
= bank
->driver
->auto_probe(bank
);
194 if (retval
!= ERROR_OK
) {
195 LOG_ERROR("auto_probe failed");
204 int get_flash_bank_by_num(int num
, struct flash_bank
**bank
)
206 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(num
);
212 retval
= p
->driver
->auto_probe(p
);
214 if (retval
!= ERROR_OK
) {
215 LOG_ERROR("auto_probe failed");
222 /* lookup flash bank by address, bank not found is success, but
223 * result_bank is set to NULL. */
224 int get_flash_bank_by_addr(struct target
*target
,
227 struct flash_bank
**result_bank
)
229 struct flash_bank
*c
;
231 /* cycle through bank list */
232 for (c
= flash_banks
; c
; c
= c
->next
) {
233 if (c
->target
!= target
)
237 retval
= c
->driver
->auto_probe(c
);
239 if (retval
!= ERROR_OK
) {
240 LOG_ERROR("auto_probe failed");
243 /* check whether address belongs to this flash bank */
244 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1))) {
251 LOG_ERROR("No flash at address 0x%08" PRIx32
, addr
);
257 static int default_flash_mem_blank_check(struct flash_bank
*bank
)
259 struct target
*target
= bank
->target
;
260 const int buffer_size
= 1024;
263 int retval
= ERROR_OK
;
265 if (bank
->target
->state
!= TARGET_HALTED
) {
266 LOG_ERROR("Target not halted");
267 return ERROR_TARGET_NOT_HALTED
;
270 uint8_t *buffer
= malloc(buffer_size
);
272 for (i
= 0; i
< bank
->num_sectors
; i
++) {
274 bank
->sectors
[i
].is_erased
= 1;
276 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
) {
279 if (chunk
> (j
- bank
->sectors
[i
].size
))
280 chunk
= (j
- bank
->sectors
[i
].size
);
282 retval
= target_read_memory(target
,
283 bank
->base
+ bank
->sectors
[i
].offset
+ j
,
287 if (retval
!= ERROR_OK
)
290 for (nBytes
= 0; nBytes
< chunk
; nBytes
++) {
291 if (buffer
[nBytes
] != 0xFF) {
292 bank
->sectors
[i
].is_erased
= 0;
305 int default_flash_blank_check(struct flash_bank
*bank
)
307 struct target
*target
= bank
->target
;
313 if (bank
->target
->state
!= TARGET_HALTED
) {
314 LOG_ERROR("Target not halted");
315 return ERROR_TARGET_NOT_HALTED
;
318 for (i
= 0; i
< bank
->num_sectors
; i
++) {
319 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
320 uint32_t size
= bank
->sectors
[i
].size
;
322 retval
= target_blank_check_memory(target
, address
, size
, &blank
);
323 if (retval
!= ERROR_OK
) {
328 bank
->sectors
[i
].is_erased
= 1;
330 bank
->sectors
[i
].is_erased
= 0;
335 LOG_USER("Running slow fallback erase check - add working memory");
336 return default_flash_mem_blank_check(bank
);
342 /* Manipulate given flash region, selecting the bank according to target
343 * and address. Maps an address range to a set of sectors, and issues
344 * the callback() on that set ... e.g. to erase or unprotect its members.
346 * (Note a current bad assumption: that protection operates on the same
347 * size sectors as erase operations use.)
349 * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
350 * range must fit those sectors exactly. This is clearly safe; it can't
351 * erase data which the caller said to leave alone, for example. If it's
352 * non-NULL, rather than failing, extra data in the first and/or last
353 * sectors will be added to the range, and that reason string is used when
354 * warning about those additions.
356 static int flash_iterate_address_range_inner(struct target
*target
,
357 char *pad_reason
, uint32_t addr
, uint32_t length
,
358 int (*callback
)(struct flash_bank
*bank
, int first
, int last
))
360 struct flash_bank
*c
;
361 uint32_t last_addr
= addr
+ length
; /* first address AFTER end */
366 int retval
= get_flash_bank_by_addr(target
, addr
, true, &c
);
367 if (retval
!= ERROR_OK
)
370 if (c
->size
== 0 || c
->num_sectors
== 0) {
371 LOG_ERROR("Bank is invalid");
372 return ERROR_FLASH_BANK_INVALID
;
376 /* special case, erase whole bank when length is zero */
377 if (addr
!= c
->base
) {
378 LOG_ERROR("Whole bank access must start at beginning of bank.");
379 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
382 return callback(c
, 0, c
->num_sectors
- 1);
385 /* check whether it all fits in this bank */
386 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1) {
387 LOG_ERROR("Flash access does not fit into bank.");
388 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
391 /** @todo: handle erasures that cross into adjacent banks */
394 last_addr
-= c
->base
;
396 for (i
= 0; i
< c
->num_sectors
; i
++) {
397 struct flash_sector
*f
= c
->sectors
+ i
;
398 uint32_t end
= f
->offset
+ f
->size
;
400 /* start only on a sector boundary */
402 /* scanned past the first sector? */
403 if (addr
< f
->offset
)
406 /* is this the first sector? */
407 if (addr
== f
->offset
)
410 /* Does this need head-padding? If so, pad and warn;
411 * or else force an error.
413 * Such padding can make trouble, since *WE* can't
414 * ever know if that data was in use. The warning
415 * should help users sort out messes later.
417 else if (addr
< end
&& pad_reason
) {
418 /* FIXME say how many bytes (e.g. 80 KB) */
419 LOG_WARNING("Adding extra %s range, "
422 (unsigned) f
->offset
,
423 (unsigned) addr
- 1);
429 /* is this (also?) the last sector? */
430 if (last_addr
== end
) {
435 /* Does this need tail-padding? If so, pad and warn;
436 * or else force an error.
438 if (last_addr
< end
&& pad_reason
) {
439 /* FIXME say how many bytes (e.g. 80 KB) */
440 LOG_WARNING("Adding extra %s range, "
443 (unsigned) last_addr
,
449 /* MUST finish on a sector boundary */
450 if (last_addr
<= f
->offset
)
454 /* invalid start or end address? */
455 if (first
== -1 || last
== -1) {
456 LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
457 "is not sector-aligned",
458 (unsigned) (c
->base
+ addr
),
459 (unsigned) (c
->base
+ last_addr
- 1));
460 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
463 /* The NOR driver may trim this range down, based on what
464 * sectors are already erased/unprotected. GDB currently
465 * blocks such optimizations.
467 return callback(c
, first
, last
);
470 /* The inner fn only handles a single bank, we could be spanning
473 static int flash_iterate_address_range(struct target
*target
,
474 char *pad_reason
, uint32_t addr
, uint32_t length
,
475 int (*callback
)(struct flash_bank
*bank
, int first
, int last
))
477 struct flash_bank
*c
;
478 int retval
= ERROR_OK
;
480 /* Danger! zero-length iterations means entire bank! */
482 retval
= get_flash_bank_by_addr(target
, addr
, true, &c
);
483 if (retval
!= ERROR_OK
)
486 uint32_t cur_length
= length
;
487 /* check whether it all fits in this bank */
488 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1) {
489 LOG_DEBUG("iterating over more than one flash bank.");
490 cur_length
= c
->base
+ c
->size
- addr
;
492 retval
= flash_iterate_address_range_inner(target
,
493 pad_reason
, addr
, cur_length
,
495 if (retval
!= ERROR_OK
)
498 length
-= cur_length
;
500 } while (length
> 0);
505 int flash_erase_address_range(struct target
*target
,
506 bool pad
, uint32_t addr
, uint32_t length
)
508 return flash_iterate_address_range(target
, pad
? "erase" : NULL
,
509 addr
, length
, &flash_driver_erase
);
512 static int flash_driver_unprotect(struct flash_bank
*bank
, int first
, int last
)
514 return flash_driver_protect(bank
, 0, first
, last
);
517 int flash_unlock_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
519 /* By default, pad to sector boundaries ... the real issue here
520 * is that our (only) caller *permanently* removes protection,
521 * and doesn't restore it.
523 return flash_iterate_address_range(target
, "unprotect",
524 addr
, length
, &flash_driver_unprotect
);
527 static int compare_section(const void *a
, const void *b
)
529 struct imagesection
*b1
, *b2
;
530 b1
= *((struct imagesection
**)a
);
531 b2
= *((struct imagesection
**)b
);
533 if (b1
->base_address
== b2
->base_address
)
535 else if (b1
->base_address
> b2
->base_address
)
541 int flash_write_unlock(struct target
*target
, struct image
*image
,
542 uint32_t *written
, int erase
, bool unlock
)
544 int retval
= ERROR_OK
;
547 uint32_t section_offset
;
548 struct flash_bank
*c
;
558 /* assume all sectors need erasing - stops any problems
559 * when flash_write is called multiple times */
564 /* allocate padding array */
565 padding
= calloc(image
->num_sections
, sizeof(*padding
));
567 /* This fn requires all sections to be in ascending order of addresses,
568 * whereas an image can have sections out of order. */
569 struct imagesection
**sections
= malloc(sizeof(struct imagesection
*) *
570 image
->num_sections
);
572 for (i
= 0; i
< image
->num_sections
; i
++)
573 sections
[i
] = &image
->sections
[i
];
575 qsort(sections
, image
->num_sections
, sizeof(struct imagesection
*),
578 /* loop until we reach end of the image */
579 while (section
< image
->num_sections
) {
580 uint32_t buffer_size
;
583 uint32_t run_address
= sections
[section
]->base_address
+ section_offset
;
584 uint32_t run_size
= sections
[section
]->size
- section_offset
;
587 if (sections
[section
]->size
== 0) {
588 LOG_WARNING("empty section %d", section
);
594 /* find the corresponding flash bank */
595 retval
= get_flash_bank_by_addr(target
, run_address
, false, &c
);
596 if (retval
!= ERROR_OK
)
599 LOG_WARNING("no flash bank found for address %" PRIx32
, run_address
);
600 section
++; /* and skip it */
605 /* collect consecutive sections which fall into the same bank */
606 section_last
= section
;
607 padding
[section
] = 0;
608 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1) &&
609 (section_last
+ 1 < image
->num_sections
)) {
610 /* sections are sorted */
611 assert(sections
[section_last
+ 1]->base_address
>= c
->base
);
612 if (sections
[section_last
+ 1]->base_address
>= (c
->base
+ c
->size
)) {
613 /* Done with this bank */
617 /* FIXME This needlessly touches sectors BETWEEN the
618 * sections it's writing. Without auto erase, it just
619 * writes ones. That WILL INVALIDATE data in cases
620 * like Stellaris Tempest chips, corrupting internal
621 * ECC codes; and at least FreeScale suggests issues
622 * with that approach (in HC11 documentation).
624 * With auto erase enabled, data in those sectors will
625 * be needlessly destroyed; and some of the limited
626 * number of flash erase cycles will be wasted...
628 * In both cases, the extra writes slow things down.
631 /* if we have multiple sections within our image,
632 * flash programming could fail due to alignment issues
633 * attempt to rebuild a consecutive buffer for the flash loader */
634 pad_bytes
= (sections
[section_last
+ 1]->base_address
) - (run_address
+ run_size
);
635 padding
[section_last
] = pad_bytes
;
636 run_size
+= sections
[++section_last
]->size
;
637 run_size
+= pad_bytes
;
640 LOG_INFO("Padding image section %d with %d bytes",
645 if (run_address
+ run_size
- 1 > c
->base
+ c
->size
- 1) {
646 /* If we have more than one flash chip back to back, then we limit
647 * the current write operation to the current chip.
649 LOG_DEBUG("Truncate flash run size to the current flash chip.");
651 run_size
= c
->base
+ c
->size
- run_address
;
652 assert(run_size
> 0);
655 /* If we're applying any sector automagic, then pad this
656 * (maybe-combined) segment to the end of its last sector.
658 if (unlock
|| erase
) {
660 uint32_t offset_start
= run_address
- c
->base
;
661 uint32_t offset_end
= offset_start
+ run_size
;
662 uint32_t end
= offset_end
, delta
;
664 for (sector
= 0; sector
< c
->num_sectors
; sector
++) {
665 end
= c
->sectors
[sector
].offset
666 + c
->sectors
[sector
].size
;
667 if (offset_end
<= end
)
671 delta
= end
- offset_end
;
672 padding
[section_last
] += delta
;
676 /* allocate buffer */
677 buffer
= malloc(run_size
);
678 if (buffer
== NULL
) {
679 LOG_ERROR("Out of memory for flash bank buffer");
685 /* read sections to the buffer */
686 while (buffer_size
< run_size
) {
689 size_read
= run_size
- buffer_size
;
690 if (size_read
> sections
[section
]->size
- section_offset
)
691 size_read
= sections
[section
]->size
- section_offset
;
695 * #¤%#"%¤% we have to figure out the section # from the sorted
696 * list of pointers to sections to invoke image_read_section()...
698 intptr_t diff
= (intptr_t)sections
[section
] - (intptr_t)image
->sections
;
699 int t_section_num
= diff
/ sizeof(struct imagesection
);
701 LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, "
702 "section_offset = %d, buffer_size = %d, size_read = %d",
703 (int)section
, (int)t_section_num
, (int)section_offset
,
704 (int)buffer_size
, (int)size_read
);
705 retval
= image_read_section(image
, t_section_num
, section_offset
,
706 size_read
, buffer
+ buffer_size
, &size_read
);
707 if (retval
!= ERROR_OK
|| size_read
== 0) {
712 /* see if we need to pad the section */
713 while (padding
[section
]--)
714 (buffer
+ buffer_size
)[size_read
++] = c
->default_padded_value
;
716 buffer_size
+= size_read
;
717 section_offset
+= size_read
;
719 if (section_offset
>= sections
[section
]->size
) {
728 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
729 if (retval
== ERROR_OK
) {
731 /* calculate and erase sectors */
732 retval
= flash_erase_address_range(target
,
733 true, run_address
, run_size
);
737 if (retval
== ERROR_OK
) {
738 /* write flash sectors */
739 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
744 if (retval
!= ERROR_OK
) {
745 /* abort operation */
750 *written
+= run_size
; /* add run size to total written counter */
760 int flash_write(struct target
*target
, struct image
*image
,
761 uint32_t *written
, int erase
)
763 return flash_write_unlock(target
, image
, written
, erase
, false);
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)