1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
3 * Copyright (C) 2007,2008 Ø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 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 retval
= bank
->driver
->protect(bank
, set
, first
, last
);
59 if (retval
!= ERROR_OK
)
61 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first
, last
, retval
);
67 int flash_driver_write(struct flash_bank
*bank
,
68 uint8_t *buffer
, uint32_t offset
, uint32_t count
)
72 retval
= bank
->driver
->write(bank
, buffer
, offset
, count
);
73 if (retval
!= ERROR_OK
)
75 LOG_ERROR("error writing to flash at address 0x%08" PRIx32
" at offset 0x%8.8" PRIx32
" (%d)",
76 bank
->base
, offset
, retval
);
82 void flash_bank_add(struct flash_bank
*bank
)
84 /* put flash bank in linked list */
85 unsigned bank_num
= 0;
88 /* find last flash bank */
89 struct flash_bank
*p
= flash_banks
;
90 while (NULL
!= p
->next
)
101 bank
->bank_number
= bank_num
;
104 struct flash_bank
*flash_bank_list(void)
109 struct flash_bank
*get_flash_bank_by_num_noprobe(int num
)
111 struct flash_bank
*p
;
114 for (p
= flash_banks
; p
; p
= p
->next
)
121 LOG_ERROR("flash bank %d does not exist", num
);
125 int flash_get_bank_count(void)
127 struct flash_bank
*p
;
129 for (p
= flash_banks
; p
; p
= p
->next
)
136 struct flash_bank
*get_flash_bank_by_name(const char *name
)
138 unsigned requested
= get_flash_name_index(name
);
141 struct flash_bank
*bank
;
142 for (bank
= flash_banks
; NULL
!= bank
; bank
= bank
->next
)
144 if (strcmp(bank
->name
, name
) == 0)
146 if (!flash_driver_name_matches(bank
->driver
->name
, name
))
148 if (++found
< requested
)
155 struct flash_bank
*get_flash_bank_by_num(int num
)
157 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(num
);
163 retval
= p
->driver
->auto_probe(p
);
165 if (retval
!= ERROR_OK
)
167 LOG_ERROR("auto_probe failed %d\n", retval
);
173 /* lookup flash bank by address */
174 struct flash_bank
*get_flash_bank_by_addr(struct target
*target
, uint32_t addr
)
176 struct flash_bank
*c
;
178 /* cycle through bank list */
179 for (c
= flash_banks
; c
; c
= c
->next
)
182 retval
= c
->driver
->auto_probe(c
);
184 if (retval
!= ERROR_OK
)
186 LOG_ERROR("auto_probe failed %d\n", retval
);
189 /* check whether address belongs to this flash bank */
190 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
193 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
197 int default_flash_mem_blank_check(struct flash_bank
*bank
)
199 struct target
*target
= bank
->target
;
200 const int buffer_size
= 1024;
203 int retval
= ERROR_OK
;
205 if (bank
->target
->state
!= TARGET_HALTED
)
207 LOG_ERROR("Target not halted");
208 return ERROR_TARGET_NOT_HALTED
;
211 uint8_t *buffer
= malloc(buffer_size
);
213 for (i
= 0; i
< bank
->num_sectors
; i
++)
216 bank
->sectors
[i
].is_erased
= 1;
218 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
222 if (chunk
> (j
- bank
->sectors
[i
].size
))
224 chunk
= (j
- bank
->sectors
[i
].size
);
227 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
228 if (retval
!= ERROR_OK
)
233 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
235 if (buffer
[nBytes
] != 0xFF)
237 bank
->sectors
[i
].is_erased
= 0;
250 int default_flash_blank_check(struct flash_bank
*bank
)
252 struct target
*target
= bank
->target
;
258 if (bank
->target
->state
!= TARGET_HALTED
)
260 LOG_ERROR("Target not halted");
261 return ERROR_TARGET_NOT_HALTED
;
264 for (i
= 0; i
< bank
->num_sectors
; i
++)
266 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
267 uint32_t size
= bank
->sectors
[i
].size
;
269 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
275 bank
->sectors
[i
].is_erased
= 1;
277 bank
->sectors
[i
].is_erased
= 0;
283 LOG_USER("Running slow fallback erase check - add working memory");
284 return default_flash_mem_blank_check(bank
);
290 /* Manipulate given flash region, selecting the bank according to target
291 * and address. Maps an address range to a set of sectors, and issues
292 * the callback() on that set ... e.g. to erase or unprotect its members.
294 * (Note a current bad assumption: that protection operates on the same
295 * size sectors as erase operations use.)
297 * The "pad_reason" parameter is a kind of boolean: when it's NULL, the
298 * range must fit those sectors exactly. This is clearly safe; it can't
299 * erase data which the caller said to leave alone, for example. If it's
300 * non-NULL, rather than failing, extra data in the first and/or last
301 * sectors will be added to the range, and that reason string is used when
302 * warning about those additions.
304 static int flash_iterate_address_range(struct target
*target
,
305 char *pad_reason
, uint32_t addr
, uint32_t length
,
306 int (*callback
)(struct flash_bank
*bank
, int first
, int last
))
308 struct flash_bank
*c
;
309 uint32_t last_addr
= addr
+ length
; /* first address AFTER end */
314 if ((c
= get_flash_bank_by_addr(target
, addr
)) == NULL
)
315 return ERROR_FLASH_DST_OUT_OF_BANK
; /* no corresponding bank found */
317 if (c
->size
== 0 || c
->num_sectors
== 0)
319 LOG_ERROR("Bank is invalid");
320 return ERROR_FLASH_BANK_INVALID
;
325 /* special case, erase whole bank when length is zero */
328 LOG_ERROR("Whole bank access must start at beginning of bank.");
329 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
332 return callback(c
, 0, c
->num_sectors
- 1);
335 /* check whether it all fits in this bank */
336 if (addr
+ length
- 1 > c
->base
+ c
->size
- 1)
338 LOG_ERROR("Flash access does not fit into bank.");
339 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
342 /** @todo: handle erasures that cross into adjacent banks */
345 last_addr
-= c
->base
;
347 for (i
= 0; i
< c
->num_sectors
; i
++)
349 struct flash_sector
*f
= c
->sectors
+ i
;
350 uint32_t end
= f
->offset
+ f
->size
;
352 /* start only on a sector boundary */
354 /* scanned past the first sector? */
355 if (addr
< f
->offset
)
358 /* is this the first sector? */
359 if (addr
== f
->offset
)
362 /* Does this need head-padding? If so, pad and warn;
363 * or else force an error.
365 * Such padding can make trouble, since *WE* can't
366 * ever know if that data was in use. The warning
367 * should help users sort out messes later.
369 else if (addr
< end
&& pad_reason
) {
370 /* FIXME say how many bytes (e.g. 80 KB) */
371 LOG_WARNING("Adding extra %s range, "
374 (unsigned) f
->offset
,
375 (unsigned) addr
- 1);
381 /* is this (also?) the last sector? */
382 if (last_addr
== end
) {
387 /* Does this need tail-padding? If so, pad and warn;
388 * or else force an error.
390 if (last_addr
< end
&& pad_reason
) {
391 /* FIXME say how many bytes (e.g. 80 KB) */
392 LOG_WARNING("Adding extra %s range, "
395 (unsigned) last_addr
,
401 /* MUST finish on a sector boundary */
402 if (last_addr
<= f
->offset
)
406 /* invalid start or end address? */
407 if (first
== -1 || last
== -1) {
408 LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
409 "is not sector-aligned",
410 (unsigned) (c
->base
+ addr
),
411 (unsigned) (last_addr
- 1));
412 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
415 /* The NOR driver may trim this range down, based on what
416 * sectors are already erased/unprotected. GDB currently
417 * blocks such optimizations.
419 return callback(c
, first
, last
);
422 int flash_erase_address_range(struct target
*target
,
423 bool pad
, uint32_t addr
, uint32_t length
)
425 return flash_iterate_address_range(target
, pad
? "erase" : NULL
,
426 addr
, length
, &flash_driver_erase
);
429 static int flash_driver_unprotect(struct flash_bank
*bank
, int first
, int last
)
431 return flash_driver_protect(bank
, 0, first
, last
);
434 static int flash_unlock_address_range(struct target
*target
, uint32_t addr
, uint32_t length
)
436 /* By default, pad to sector boundaries ... the real issue here
437 * is that our (only) caller *permanently* removes protection,
438 * and doesn't restore it.
440 return flash_iterate_address_range(target
, "unprotect",
441 addr
, length
, &flash_driver_unprotect
);
444 int flash_write_unlock(struct target
*target
, struct image
*image
,
445 uint32_t *written
, int erase
, bool unlock
)
447 int retval
= ERROR_OK
;
450 uint32_t section_offset
;
451 struct flash_bank
*c
;
454 /* REVISIT do_pad should perhaps just be another parameter.
455 * GDB wouldn't ever need it, since it erases separately.
456 * But "flash write_image" commands might want that option.
468 /* assume all sectors need erasing - stops any problems
469 * when flash_write is called multiple times */
474 /* allocate padding array */
475 padding
= calloc(image
->num_sections
, sizeof(*padding
));
477 /* loop until we reach end of the image */
478 while (section
< image
->num_sections
)
480 uint32_t buffer_size
;
484 uint32_t run_address
= image
->sections
[section
].base_address
+ section_offset
;
485 uint32_t run_size
= image
->sections
[section
].size
- section_offset
;
488 if (image
->sections
[section
].size
== 0)
490 LOG_WARNING("empty section %d", section
);
496 /* find the corresponding flash bank */
497 if ((c
= get_flash_bank_by_addr(target
, run_address
)) == NULL
)
499 section
++; /* and skip it */
504 /* collect consecutive sections which fall into the same bank */
505 section_first
= section
;
506 section_last
= section
;
507 padding
[section
] = 0;
508 while ((run_address
+ run_size
- 1 < c
->base
+ c
->size
- 1)
509 && (section_last
+ 1 < image
->num_sections
))
511 if (image
->sections
[section_last
+ 1].base_address
< (run_address
+ run_size
))
513 LOG_DEBUG("section %d out of order "
514 "(surprising, but supported)",
516 /* REVISIT this can break with autoerase ...
517 * clobbering data after it's written.
522 /* FIXME This needlessly touches sectors BETWEEN the
523 * sections it's writing. Without auto erase, it just
524 * writes ones. That WILL INVALIDATE data in cases
525 * like Stellaris Tempest chips, corrupting internal
526 * ECC codes; and at least FreeScale suggests issues
527 * with that approach (in HC11 documentation).
529 * With auto erase enabled, data in those sectors will
530 * be needlessly destroyed; and some of the limited
531 * number of flash erase cycles will be wasted...
533 * In both cases, the extra writes slow things down.
536 /* if we have multiple sections within our image,
537 * flash programming could fail due to alignment issues
538 * attempt to rebuild a consecutive buffer for the flash loader */
539 pad_bytes
= (image
->sections
[section_last
+ 1].base_address
) - (run_address
+ run_size
);
540 if ((run_address
+ run_size
+ pad_bytes
) > (c
->base
+ c
->size
))
542 padding
[section_last
] = pad_bytes
;
543 run_size
+= image
->sections
[++section_last
].size
;
544 run_size
+= pad_bytes
;
546 LOG_INFO("Padding image section %d with %d bytes", section_last
-1, pad_bytes
);
549 /* fit the run into bank constraints */
550 if (run_address
+ run_size
- 1 > c
->base
+ c
->size
- 1)
552 /* REVISIT isn't this superfluous, given the while()
553 * loop conditions above??
555 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
556 (int)(c
->base
+ c
->size
- run_address
), (int)(run_size
), (int)(c
->size
));
557 run_size
= c
->base
+ c
->size
- run_address
;
560 /* If we're applying any sector automagic, then pad this
561 * (maybe-combined) segment to the end of its last sector.
563 if (unlock
|| erase
) {
565 uint32_t offset_start
= run_address
- c
->base
;
566 uint32_t offset_end
= offset_start
+ run_size
;
567 uint32_t end
= offset_end
, delta
;
569 for (sector
= 0; sector
< c
->num_sectors
; sector
++) {
570 end
= c
->sectors
[sector
].offset
571 + c
->sectors
[sector
].size
;
572 if (offset_end
<= end
)
576 delta
= end
- offset_end
;
577 padding
[section_last
] += delta
;
581 /* allocate buffer */
582 buffer
= malloc(run_size
);
585 /* read sections to the buffer */
586 while (buffer_size
< run_size
)
590 size_read
= run_size
- buffer_size
;
591 if (size_read
> image
->sections
[section
].size
- section_offset
)
592 size_read
= image
->sections
[section
].size
- section_offset
;
594 if ((retval
= image_read_section(image
, section
, section_offset
,
595 size_read
, buffer
+ buffer_size
, &size_read
)) != ERROR_OK
|| size_read
== 0)
602 /* see if we need to pad the section */
603 while (padding
[section
]--)
604 (buffer
+ buffer_size
)[size_read
++] = 0xff;
606 buffer_size
+= size_read
;
607 section_offset
+= size_read
;
609 if (section_offset
>= image
->sections
[section
].size
)
620 retval
= flash_unlock_address_range(target
, run_address
, run_size
);
622 if (retval
== ERROR_OK
)
626 /* calculate and erase sectors */
627 retval
= flash_erase_address_range(target
,
628 do_pad
, run_address
, run_size
);
632 if (retval
== ERROR_OK
)
634 /* write flash sectors */
635 retval
= flash_driver_write(c
, buffer
, run_address
- c
->base
, run_size
);
640 if (retval
!= ERROR_OK
)
643 return retval
; /* abort operation */
647 *written
+= run_size
; /* add run size to total written counter */
655 int flash_write(struct target
*target
, struct image
*image
,
656 uint32_t *written
, int erase
)
658 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)