Edward O'Callaghan has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/59291 )
Change subject: flashrom: Convert do_read() into a libflashrom user ......................................................................
flashrom: Convert do_read() into a libflashrom user
Aspire towards a goal of making cli_classic more of just a user of libflashrom than having quasi-parallel paths in flashrom.c
This converts the do_read() provider wrapper into a pure libflashrom user. Also fix the case where the cli detects a missing read() callback while the libflashrom interface does not.
BUG=none TEST=builds
Change-Id: I4b690b688acf9d5deb46e8642a252a2132ea8c73 Signed-off-by: Edward O'Callaghan quasisec@google.com --- M cli_classic.c M flash.h M flashrom.c 3 files changed, 27 insertions(+), 27 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/91/59291/1
diff --git a/cli_classic.c b/cli_classic.c index d69b798..101d272 100644 --- a/cli_classic.c +++ b/cli_classic.c @@ -638,7 +638,7 @@ goto out_shutdown; } msg_cinfo("Please note that forced reads most likely contain garbage.\n"); - ret = read_flash_to_file(&flashes[0], filename); + ret = do_read(&flashes[0], filename); unmap_flash(&flashes[0]); free(flashes[0].chip); goto out_shutdown; diff --git a/flash.h b/flash.h index 391a2d4..8c24c0c 100644 --- a/flash.h +++ b/flash.h @@ -341,7 +341,6 @@ int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len); int erase_flash(struct flashctx *flash); int probe_flash(struct registered_master *mst, int startchip, struct flashctx *fill_flash, int force); -int read_flash_to_file(struct flashctx *flash, const char *filename); int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len); int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len, enum write_granularity gran, const uint8_t erased_value); void print_version(void); diff --git a/flashrom.c b/flashrom.c index 48d953b..05abda7 100644 --- a/flashrom.c +++ b/flashrom.c @@ -1073,36 +1073,19 @@ return 0; }
-int read_flash_to_file(struct flashctx *flash, const char *filename) +static int read_flash_to_file(struct flashctx *flash, const char *filename, + const char *buf, size_t size) { - unsigned long size = flash->chip->total_size * 1024; - unsigned char *buf = calloc(size, sizeof(unsigned char)); int ret = 0;
- msg_cinfo("Reading flash... "); - if (!buf) { - msg_gerr("Memory allocation failed!\n"); - msg_cinfo("FAILED.\n"); - return 1; - } - if (!flash->chip->read) { - msg_cerr("No read function available for this flash chip.\n"); - ret = 1; - goto out_free; - } - if (read_by_layout(flash, buf)) { - msg_cerr("Read operation failed!\n"); - ret = 1; - goto out_free; - } if (write_buf_to_include_args(flash, buf)) { ret = 1; - goto out_free; + goto out; }
ret = write_buf_to_file(buf, size, filename); -out_free: - free(buf); + +out: msg_cinfo("%s.\n", ret ? "FAILED" : "done"); return ret; } @@ -1963,6 +1946,11 @@ if (flash_size > buffer_len) return 2;
+ if (!flash->chip->read) { + msg_cerr("No read function available for this flash chip.\n"); + ret = 1; + } + if (prepare_flash_access(flashctx, true, false, false, false)) return 1;
@@ -2193,13 +2181,26 @@
int do_read(struct flashctx *const flash, const char *const filename) { - if (prepare_flash_access(flash, true, false, false, false)) + int ret; + void *const buffer; + const size_t buffer_len; + + unsigned long size = flash->chip->total_size * 1024; + unsigned char *buf = calloc(size, sizeof(unsigned char)); + if (!buf) { + msg_gerr("Memory allocation failed!\n"); + msg_cinfo("FAILED.\n"); return 1; + }
- const int ret = read_flash_to_file(flash, filename); + ret = flashrom_image_read(flash, buf, size); + if (ret > 0) + goto free_out;
- finalize_flash_access(flash); + ret = read_flash_to_file(flash, filename, buf, size);
+free_out: + free(buf); return ret; }