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> *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
26 #include <flash/common.h>
27 #include <flash/nor/core.h>
28 #include <flash/nor/imp.h>
29 #include <target/image.h>
34 * Upper level of NOR flash framework.
35 * The lower level interfaces are to drivers. These upper level ones
36 * primarily support access from Tcl scripts or from GDB.
39 static struct flash_bank
*flash_banks
;
41 int flash_driver_erase(struct flash_bank
*bank
, int first
, int last
)
45 retval
= bank
->driver
->erase(bank
, first
, last
);
46 if (retval
!= ERROR_OK
)
48 LOG_ERROR("failed erasing sectors %d to %d (%d)", first
, last
, retval
);
54 int flash_driver_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
58 /* callers may not supply illegal parameters ... */
59 if (first
< 0 || first
> last
|| last
>= bank
->num_sectors
)
61 LOG_ERROR("illegal sector range");
65 /* force "set" to 0/1 */
70 * We must not use any cached information about protection state!!!!
72 * There are a million things that could change the protect state:
74 * the target could have reset, power cycled, been hot plugged,
75 * the application could have run, etc.
77 * Drivers only receive valid sector range.
79 retval
= bank
->driver
->protect(bank
, set
, first
, last
);
80 if (retval
!= ERROR_OK
)
82 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first
, last
, retval
);
88 int flash_driver_write(struct flash_bank
*bank
,
89 uint8_t *buffer
, uint32_t offset
, uint32_t count
)
93 retval
= bank
->driver
->write(bank
, buffer
, offset
, count
);
94 if (retval
!= ERROR_OK
)
96 LOG_ERROR("error writing to flash at address 0x%08" PRIx32
" at offset 0x%8.8" PRIx32
" (%d)",
97 bank
->base
, offset
, retval
);
103 void flash_bank_add(struct flash_bank
*bank
)
105 /* put flash bank in linked list */
106 unsigned bank_num
= 0;
109 /* find last flash bank */
110 struct flash_bank
*p
= flash_banks
;
111 while (NULL
!= p
->next
)
122 bank
->bank_number
= bank_num
;
125 struct flash_bank
*flash_bank_list(void)
130 struct flash_bank
*get_flash_bank_by_num_noprobe(int num
)
132 struct flash_bank
*p
;
135 for (p
= flash_banks
; p
; p
= p
->next
)
142 LOG_ERROR("flash bank %d does not exist", num
);
146 int flash_get_bank_count(void)
148 struct flash_bank
*p
;
150 for (p
= flash_banks
; p
; p
= p
->next
)
157 struct flash_bank
*get_flash_bank_by_name(const char *name
)
159 unsigned requested
= get_flash_name_index(name
);
162 struct flash_bank
*bank
;
163 for (bank
= flash_banks
; NULL
!= bank
; bank
= bank
->next
)
165 if (strcmp(bank
->name
, name
) == 0)
167 if (!flash_driver_name_matches(bank
->driver
->name
, name
))
169 if (++found
< requested
)
176 int get_flash_bank_by_num(int num
, struct flash_bank
**bank
)
178 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(num
);
186 retval
= p
->driver
->auto_probe(p
);
188 if (retval
!= ERROR_OK
)
190 LOG_ERROR("auto_probe failed %d\n", retval
);
197 /* lookup flash bank by address */
198 struct flash_bank
*get_flash_bank_by_addr(struct target
*target
, uint32_t addr
)
200 struct flash_bank
*c
;
202 /* cycle through bank list */
203 for (c
= flash_banks
; c
; c
= c
->next
)
206 retval
= c
->driver
->auto_probe(c
);
208 if (retval
!= ERROR_OK
)
210 LOG_ERROR("auto_probe failed %d\n", retval
);
213 /* check whether address belongs to this flash bank */
214 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
217 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
221 int default_flash_mem_blank_check(struct flash_bank
*bank
)
223 struct target
*target
= bank
->target
;
224 const int buffer_size
= 1024;
227 int retval
= ERROR_OK
;
229 if (bank
->target
->state
!= TARGET_HALTED
)
231 LOG_ERROR("Target not halted");
232 return ERROR_TARGET_NOT_HALTED
;
235 uint8_t *buffer
= malloc(buffer_size
);
237 for (i
= 0; i
< bank
->num_sectors
; i
++)
240 bank
->sectors
[i
].is_erased
= 1;
242 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
246 if (chunk
> (j
- bank
->sectors
[i
].size
))
248 chunk
= (j
- bank
->sectors
[i
].size
);
251 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
252 if (retval
!= ERROR_OK
)
257 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
259 if (buffer
[nBytes
] != 0xFF)
261 bank
->sectors
[i
].is_erased
= 0;
274 int default_flash_blank_check(struct flash_bank
*bank
)
276 struct target
*target
= bank
->target
;
282 if (bank
->target
->state
!= TARGET_HALTED
)
284 LOG_ERROR("Target not halted");
285 return ERROR_TARGET_NOT_HALTED
;
288 for (i
= 0; i
< bank
->num_sectors
; i
++)
290 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
291 uint32_t size
= bank
->sectors
[i
].size
;
293 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
299 bank
->sectors
[i
].is_erased
= 1;
301 bank
->sectors
[i
].is_erased
= 0;
307 LOG_USER("Running slow fallback erase check - add working memory");
308 return default_flash_mem_blank_check(bank
);
314 /* Manipulate given flash region, selecting the bank according to target
315 * and address. Maps an address range to a set of sectors, and issues
316 * the callback() on that set ... e.g. to erase or unprotect its members.
318 * (Note a current bad assumption: that protection operates on the same
319 * size sectors as erase operations use.)
321 * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
322 * range must fit those sectors exactly. This is clearly safe; it can't
323 * erase data which the caller said to leave alone, for example. If it's
324 * non-NULL, rather than failing, extra data in the first and/or last
325 * sectors will be added to the range, and that reason string is used when
326 * warning about those additions.
328 static int flash_iterate_address_range(struct target
*target
,
329 char *pad_reason
, uint32_t addr
, uint32_t length
,
330 int (*callback
)(struct flash_bank
*bank
, int first
, int last
))
332 struct flash_bank
*c
;
333 uint32_t last_addr
= addr
+ length
; /* first address AFTER end */
338 if ((c
= get_flash_bank_by_addr(target
, addr
)) == NULL
)
339 return ERROR_FLASH_DST_OUT_OF_BANK
; /* no corresponding bank found */
341 if (c
->size
== 0 || c
->num_sectors
== 0)
343 LOG_ERROR("Bank is invalid");
344 return ERROR_FLASH_BANK_INVALID
;
349 /* special case, erase whole bank when length is zero */
352 LOG_ERROR("Whole bank access must start at beginning of bank.");
353 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
356 return callback(c
, 0, c
->num_sectors
- 1);
359 /* check whether it all fits in this bank */
360 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1)
362 LOG_ERROR("Flash access does not fit into bank.");
363 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
366 /** @todo: handle erasures that cross into adjacent banks */
369 last_addr
-= c
->base
;
371 for (i
= 0; i
< c
->num_sectors
; i
++)
373 struct flash_sector
*f
= c
->sectors
+ i
;
374 uint32_t end
= f
->offset
+ f
->size
;
376 /* start only on a sector boundary */
378 /* scanned past the first sector? */
379 if (addr
< f
->offset
)
382 /* is this the first sector? */
383 if (addr
== f
->offset
)
386 /* Does this need head-padding? If so, pad and warn;
387 * or else force an error.
389 * Such padding can make trouble, since *WE* can't
390 * ever know if that data was in use. The warning
391 * should help users sort out messes later.
393 else if (addr
< end
&& pad_reason
) {
394 /* FIXME say how many bytes (e.g. 80 KB) */
395 LOG_WARNING("Adding extra %s range, "
398 (unsigned) f
->offset
,
399 (unsigned) addr
- 1);
405 /* is this (also?) the last sector? */
406 if (last_addr
== end
) {
411 /* Does this need tail-padding? If so, pad and warn;
412 * or else force an error.
414 if (last_addr
< end
&& pad_reason
) {
415 /* FIXME say how many bytes (e.g. 80 KB) */
416 LOG_WARNING("Adding extra %s range, "
419 (unsigned) last_addr
,
425 /* MUST finish on a sector boundary */
426 if (last_addr
<= f
->offset
)
430 /* invalid start or end address? */
431 if (first
== -1 || last
== -1) {
432 LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
433 "is not sector-aligned",
434 (unsigned) (c
->base
+ addr
),
435 (unsigned) (c
->base
+ last_addr
- 1));
436 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
439 /* The NOR driver may trim this range down, based on what
440 * sectors are already erased/unprotected. GDB currently
441 * blocks such optimizations.
443 return callback(c
, first
, last
);
446 int flash_erase_address_range(struct target
*target
,
447 bool pad
, uint32_t addr
, uint32_t length
)
449 return flash_iterate_address_range(target
, pad
? "erase" : NULL
,
450 addr
, length
, &flash_driver_erase
);
453 static int flash_driver_unprotect(struct flash_bank
*bank
, int first
, int last
)
455 return flash_driver_protect(bank
, 0, first
, last
);
458 int flash_unlock_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
460 /* By default, pad to sector boundaries ... the real issue here
461 * is that our (only) caller *permanently* removes protection,
462 * and doesn't restore it.
464 return flash_iterate_address_range(target
, "unprotect",
465 addr
, length
, &flash_driver_unprotect
);
468 static int compare_section (const void * a
, const void * b
)
470 struct imageection
*b1
, *b2
;
471 b1
=*((struct imageection
**)a
);
472 b2
=*((struct imageection
**)b
);
474 if (b1
->base_address
== b2
->base_address
)
477 } else if (b1
->base_address
> b2
->base_address
)
487 int flash_write_unlock(struct target
*target
, struct image
*image
,
488 uint32_t *written
, int erase
, bool unlock
)
490 int retval
= ERROR_OK
;
493 uint32_t section_offset
;
494 struct flash_bank
*c
;
505 /* assume all sectors need erasing - stops any problems
506 * when flash_write is called multiple times */
511 /* allocate padding array */
512 padding
= calloc(image
->num_sections
, sizeof(*padding
));
514 /* This fn requires all sections to be in ascending order of addresses,
515 * whereas an image can have sections out of order. */
516 struct imageection
**sections
= malloc(sizeof(struct imageection
*) *
517 image
->num_sections
);
519 for (i
= 0; i
< image
->num_sections
; i
++)
521 sections
[i
] = &image
->sections
[i
];
524 qsort(sections
, image
->num_sections
, sizeof(struct imageection
*),
527 /* loop until we reach end of the image */
528 while (section
< image
->num_sections
)
530 uint32_t buffer_size
;
534 uint32_t run_address
= sections
[section
]->base_address
+ section_offset
;
535 uint32_t run_size
= sections
[section
]->size
- section_offset
;
538 if (sections
[section
]->size
== 0)
540 LOG_WARNING("empty section %d", section
);
546 /* find the corresponding flash bank */
547 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
549 section
++; /* and skip it */
554 /* collect consecutive sections which fall into the same bank */
555 section_first
= section
;
556 section_last
= section
;
557 padding
[section
] = 0;
558 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1)
559 && (section_last
+ 1 < image
->num_sections
))
561 /* sections are sorted */
562 assert(sections
[section_last
+ 1]->base_address
>= c
->base
);
563 if (sections
[section_last
+ 1]->base_address
>= (c
->base
+ c
->size
))
565 /* Done with this bank */
569 /* FIXME This needlessly touches sectors BETWEEN the
570 * sections it's writing. Without auto erase, it just
571 * writes ones. That WILL INVALIDATE data in cases
572 * like Stellaris Tempest chips, corrupting internal
573 * ECC codes; and at least FreeScale suggests issues
574 * with that approach (in HC11 documentation).
576 * With auto erase enabled, data in those sectors will
577 * be needlessly destroyed; and some of the limited
578 * number of flash erase cycles will be wasted...
580 * In both cases, the extra writes slow things down.
583 /* if we have multiple sections within our image,
584 * flash programming could fail due to alignment issues
585 * attempt to rebuild a consecutive buffer for the flash loader */
586 pad_bytes
= (sections
[section_last
+ 1]->base_address
) - (run_address
+ run_size
);
587 padding
[section_last
] = pad_bytes
;
588 run_size
+= sections
[++section_last
]->size
;
589 run_size
+= pad_bytes
;
592 LOG_INFO("Padding image section %d with %d bytes", section_last
-1, pad_bytes
);
595 assert (run_address
+ run_size
- 1 <= c
->base
+ c
->size
- 1);
597 /* If we're applying any sector automagic, then pad this
598 * (maybe-combined) segment to the end of its last sector.
600 if (unlock
|| erase
) {
602 uint32_t offset_start
= run_address
- c
->base
;
603 uint32_t offset_end
= offset_start
+ run_size
;
604 uint32_t end
= offset_end
, delta
;
606 for (sector
= 0; sector
< c
->num_sectors
; sector
++) {
607 end
= c
->sectors
[sector
].offset
608 + c
->sectors
[sector
].size
;
609 if (offset_end
<= end
)
613 delta
= end
- offset_end
;
614 padding
[section_last
] += delta
;
618 /* allocate buffer */
619 buffer
= malloc(run_size
);
622 /* read sections to the buffer */
623 while (buffer_size
< run_size
)
627 size_read
= run_size
- buffer_size
;
628 if (size_read
> sections
[section
]->size
- section_offset
)
629 size_read
= sections
[section
]->size
- section_offset
;
633 * #¤%#"%¤% we have to figure out the section # from the sorted
634 * list of pointers to sections to invoke image_read_section()...
636 intptr_t diff
= (intptr_t)sections
[section
] - (intptr_t)image
->sections
;
637 int t_section_num
= diff
/ sizeof(struct imageection
);
639 LOG_DEBUG("image_read_section: section = %d, t_section_num = %d, section_offset = %d, buffer_size = %d, size_read = %d",
641 (int)t_section_num
, (int)section_offset
, (int)buffer_size
, (int)size_read
);
642 if ((retval
= image_read_section(image
, t_section_num
, section_offset
,
643 size_read
, buffer
+ buffer_size
, &size_read
)) != ERROR_OK
|| size_read
== 0)
649 /* see if we need to pad the section */
650 while (padding
[section
]--)
651 (buffer
+ buffer_size
)[size_read
++] = 0xff;
653 buffer_size
+= size_read
;
654 section_offset
+= size_read
;
656 if (section_offset
>= sections
[section
]->size
)
667 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
669 if (retval
== ERROR_OK
)
673 /* calculate and erase sectors */
674 retval
= flash_erase_address_range(target
,
675 true, run_address
, run_size
);
679 if (retval
== ERROR_OK
)
681 /* write flash sectors */
682 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
687 if (retval
!= ERROR_OK
)
689 /* abort operation */
694 *written
+= run_size
; /* add run size to total written counter */
705 int flash_write(struct target
*target
, struct image
*image
,
706 uint32_t *written
, int erase
)
708 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)