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.

Tested: `$ sudo ./flashrom -p internal -r /tmp/bios.bin`
Tested: `$ sudo ./flashrom -p internal -l /tmp/layout -i FOO -r /tmp/foo.bin`

Change-Id: Id2addadb891c482ee3f69da806062d7a88776675
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/60430
Original-Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72288
Reviewed-by: Nico Huber <nico.h@gmx.de>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/flashrom.c b/flashrom.c
index 353a827..48477d6 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -2137,13 +2137,23 @@
 
 int do_read(struct flashctx *const flash, const char *const filename)
 {
-	if (prepare_flash_access(flash, true, false, false, false))
+	int ret;
+
+	unsigned long size = flash->chip->total_size * 1024;
+	unsigned char *buf = calloc(size, sizeof(unsigned char));
+	if (!buf) {
+		msg_gerr("Memory allocation 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 = write_buf_to_file(buf, size, filename);
 
+free_out:
+	free(buf);
 	return ret;
 }