On Tuesday 22 December 2009, Yegor Yefremov wrote: > flash write_image erase image.bin 0x1000 bin > auto erase enabled > address range 0x00001000 .. 0x000135d3 is not sector-aligned > Command handler execution failed
The following patch should help in at least some cases; it doesn't insert sector pre-padding but does update the existing post-pad logic. - Dave ========== CUT HERE From: David Brownell <dbrown...@users.sourceforge.net> Subject: NOR: make flash_write_unlock() pad to sector end Resolve a regression when using new automagic "write_image" modes, by always padding to the end of affected sectors. Also: - Allocate the right amount of memory on 64-bit hosts (previous code could corrupt server memory), switching to calloc() to simplify review and initialization. - Document issues associated with the newish automagic options, adding some code comments too. - Fix syntax error: wrote N "bytes"; writing a single byte is an unusual case, not the normal one. We might need similar padding at the *beginning* of some sectors, but this is a minimalist fix for the problems which have currently been reported (plus that bug on 64-bit hosts, and doc fixes). --- doc/openocd.texi | 25 +++++++++++++++++++++++-- src/flash/nor/core.c | 46 +++++++++++++++++++++++++++++++++++++++++++--- src/flash/nor/tcl.c | 4 ++-- 3 files changed, 68 insertions(+), 7 deletions(-) --- a/doc/openocd.texi +++ b/doc/openocd.texi @@ -3797,8 +3797,29 @@ explicitly as @option{bin} (binary), @op The relevant flash sectors will be erased prior to programming if the @option{erase} parameter is given. If @option{unlock} is provided, then the flash banks are unlocked before erase and -program. The flash bank to use is inferred from the @var{address} of -each image segment. +program. The flash bank to use is inferred from the address of +each image section. + +...@quotation Warning +Be careful using the @option{erase} flag when the flash is holding +data you want to preserve. +Portions of the flash outside those described in the image's +sections might be erased with no notice. +...@itemize +...@item +When a section of the image being written does not fill out all the +sectors it uses, the unwritten parts of those sectors are necessarily +also erased, because sectors can't be partially erased. +...@item +Data stored in sector "holes" between image sections is also affected. +For example, "@command{flash write_image erase ...}" of an image with +one byte at the beginning of a flash bank and one byte at the end +erases the entire bank -- not just the two sectors being written. +...@end itemize +Also, when flash protection is important, you must re-apply it after +it has been removed by the @option{unlock} flag. +...@end quotation + @end deffn @section Other Flash commands --- a/src/flash/nor/core.c +++ b/src/flash/nor/core.c @@ -401,7 +401,7 @@ int flash_write_unlock(struct target *ta } /* allocate padding array */ - padding = malloc(image->num_sections * sizeof(padding)); + padding = calloc(image->num_sections, sizeof(*padding)); /* loop until we reach end of the image */ while (section < image->num_sections) @@ -439,9 +439,26 @@ int flash_write_unlock(struct target *ta { if (image->sections[section_last + 1].base_address < (run_address + run_size)) { - LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1); + LOG_DEBUG("section %d out of order " + "(surprising, but supported)", + section_last + 1); + /* REVISIT this can break with autoerase ... + * clobbering data after it's written. + */ break; } + + /* REVISIT This needlessly touches sectors BETWEEN the + * sections it's writing. Without auto erase, it just + * writes ones; unlikely to destroy data. + * + * With auto erase enabled, data in those sectors will + * be needlessly destroyed; and some of the limited + * number of flash erase cycles will be wasted. + * + * In both cases, the extra writes slow things down. + */ + /* if we have multiple sections within our image, flash programming could fail due to alignment issues * attempt to rebuild a consecutive buffer for the flash loader */ pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size); @@ -450,7 +467,6 @@ int flash_write_unlock(struct target *ta padding[section_last] = pad_bytes; run_size += image->sections[++section_last].size; run_size += pad_bytes; - padding[section_last] = 0; LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes); } @@ -458,11 +474,35 @@ int flash_write_unlock(struct target *ta /* fit the run into bank constraints */ if (run_address + run_size - 1 > c->base + c->size - 1) { + /* REVISIT isn't this superfluous, given the while() + * loop conditions above?? + */ LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \ (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size)); run_size = c->base + c->size - run_address; } + /* If we're applying any sector automagic, then pad this + * (maybe-combined) segment to the end of its last sector. + */ + if (unlock || erase) { + int sector; + uint32_t offset_start = run_address - c->base; + uint32_t offset_end = offset_start + run_size; + uint32_t end = offset_end, delta; + + for (sector = 0; sector < c->num_sectors; sector++) { + end = c->sectors[sector].offset + + c->sectors[sector].size; + if (offset_end <= end) + break; + } + + delta = end - offset_end; + padding[section_last] += delta; + run_size += delta; + } + /* allocate buffer */ buffer = malloc(run_size); buffer_size = 0; --- a/src/flash/nor/tcl.c +++ b/src/flash/nor/tcl.c @@ -439,7 +439,7 @@ COMMAND_HANDLER(handle_flash_write_image if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) { - command_print(CMD_CTX, "wrote %" PRIu32 " byte from file %s " + command_print(CMD_CTX, "wrote %" PRIu32 " bytes from file %s " "in %fs (%0.3f kb/s)", written, CMD_ARGV[0], duration_elapsed(&bench), duration_kbps(&bench, written)); } @@ -625,7 +625,7 @@ COMMAND_HANDLER(handle_flash_write_bank_ if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) { - command_print(CMD_CTX, "wrote %zu byte from file %s to flash bank %u" + command_print(CMD_CTX, "wrote %zu bytes from file %s to flash bank %u" " at offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)", fileio.size, CMD_ARGV[1], p->bank_number, offset, duration_elapsed(&bench), duration_kbps(&bench, fileio.size)); _______________________________________________ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development