Edward O'Callaghan has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/70025 )
Change subject: flashrom.c: Factor out setup_curcontents() from flashrom_image_write() ......................................................................
flashrom.c: Factor out setup_curcontents() from flashrom_image_write()
The CrOS tree factored this functionality into its own function, seems reasonable.
Change-Id: I4baf09700d7efcbca3a6ba0cfda3231837e0a34a Signed-off-by: Edward O'Callaghan quasisec@google.com --- M flashrom.c 1 file changed, 54 insertions(+), 25 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/25/70025/1
diff --git a/flashrom.c b/flashrom.c index 51a6b64..48ac687 100644 --- a/flashrom.c +++ b/flashrom.c @@ -1800,6 +1800,44 @@ memcpy(newcontents + start, oldcontents + start, copy_len); }
+static int setup_curcontents(struct flashctx *const flashctx, + void *curcontents, const void *const refcontents) +{ + const size_t flash_size = flashctx->chip->total_size * 1024; + const bool verify_all = flashctx->flags.verify_whole_chip; + + memset(curcontents, UNERASED_VALUE(flashctx), flash_size); + + /* If given, assume flash chip contains same data as `refcontents`. */ + if (refcontents) { + msg_cinfo("Assuming old flash chip contents as ref-file...\n"); + memcpy(curcontents, refcontents, flash_size); + } else { + /* + * Read the whole chip to be able to check whether regions need to be + * erased and to give better diagnostics in case write fails. + * The alternative is to read only the regions which are to be + * preserved, but in that case we might perform unneeded erase which + * takes time as well. + */ + msg_cinfo("Reading old flash chip contents... "); + if (verify_all) { + if (read_flash(flashctx, curcontents, 0, flash_size)) { + msg_cinfo("FAILED.\n"); + return -1; + } + } else { + if (read_by_layout(flashctx, curcontents)) { + msg_cinfo("FAILED.\n"); + return -1; + } + } + msg_cinfo("done.\n"); + } + + return 0; +} + int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len, const void *const refbuffer) { @@ -1840,34 +1878,12 @@ if (prepare_flash_access(flashctx, false, true, false, verify)) goto _free_ret;
- /* If given, assume flash chip contains same data as `refcontents`. */ + if (setup_curcontents(flashctx, curcontents, refcontents) < 0) + goto _finalize_ret; + if (refcontents) { - msg_cinfo("Assuming old flash chip contents as ref-file...\n"); - memcpy(curcontents, refcontents, flash_size); if (oldcontents) memcpy(oldcontents, refcontents, flash_size); - } else { - /* - * Read the whole chip to be able to check whether regions need to be - * erased and to give better diagnostics in case write fails. - * The alternative is to read only the regions which are to be - * preserved, but in that case we might perform unneeded erase which - * takes time as well. - */ - msg_cinfo("Reading old flash chip contents... "); - if (verify_all) { - if (read_flash(flashctx, oldcontents, 0, flash_size)) { - msg_cinfo("FAILED.\n"); - goto _finalize_ret; - } - memcpy(curcontents, oldcontents, flash_size); - } else { - if (read_by_layout(flashctx, curcontents)) { - msg_cinfo("FAILED.\n"); - goto _finalize_ret; - } - } - msg_cinfo("done.\n"); }
if (write_by_layout(flashctx, curcontents, newcontents)) {