Factor out read_firmware_device()

For sharing code between boot stub and recovery firmware

BUG=None
TEST=MAKEALL successfully

Review URL: http://codereview.chromium.org/6650024

Change-Id: I1293d246cc7e440e3a8cbdcd29389741fc30c862
diff --git a/common/cmd_cros_bootstub.c b/common/cmd_cros_bootstub.c
index 5360c95..68813fb 100644
--- a/common/cmd_cros_bootstub.c
+++ b/common/cmd_cros_bootstub.c
@@ -34,42 +34,6 @@
 #define FIRMWARE_B		2
 
 /*
- * Read <count> bytes, starting from <offset>, from firmware storage
- * device <file> into buffer <buf>.
- *
- * Return 0 on success, non-zero on error.
- */
-int read_firmware_device(firmware_storage_t *file, off_t offset, void *buf,
-		size_t count)
-{
-	ssize_t size;
-
-	if (file->seek(file->context, offset, SEEK_SET) < 0) {
-		debug(PREFIX "seek to address 0x%08x fail\n", offset);
-		return -1;
-	}
-
-	size = 0;
-	while (count > 0 &&
-			(size = file->read(file->context, buf, count)) > 0) {
-		count -= size;
-		buf += size;
-	}
-
-	if (size < 0) {
-		debug(PREFIX "an error occur when read firmware: %d\n", size);
-		return -1;
-	}
-
-	if (count > 0) {
-		debug(PREFIX "cannot read all data: %d\n", count);
-		return -1;
-	}
-
-	return 0;
-}
-
-/*
  * Read verification block on <vblock_offset> from <file>.
  *
  * The pointer to verification block is stored in <vblock_ptr>, and the size of
diff --git a/include/chromeos/firmware_storage.h b/include/chromeos/firmware_storage.h
index 672b395..3349b1b 100644
--- a/include/chromeos/firmware_storage.h
+++ b/include/chromeos/firmware_storage.h
@@ -72,4 +72,13 @@
 /* Dispose fields that are used for communicating with GetFirmwareBody(). */
 void GetFirmwareBody_dispose(firmware_storage_t *f);
 
+/*
+ * Read <count> bytes, starting from <offset>, from firmware storage
+ * device <file> into buffer <buf>.
+ *
+ * Return 0 on success, non-zero on error.
+ */
+int read_firmware_device(firmware_storage_t *file, off_t offset, void *buf,
+		size_t count);
+
 #endif /* __FIRMWARE_STORAGE_H_ */
diff --git a/lib/chromeos/load_firmware_fw.c b/lib/chromeos/load_firmware_fw.c
index b5d271b..18d2e34 100644
--- a/lib/chromeos/load_firmware_fw.c
+++ b/lib/chromeos/load_firmware_fw.c
@@ -37,6 +37,36 @@
 			free(f->firmware_body[i]);
 }
 
+int read_firmware_device(firmware_storage_t *file, off_t offset, void *buf,
+		size_t count)
+{
+	ssize_t size;
+
+	if (file->seek(file->context, offset, SEEK_SET) < 0) {
+		debug(PREFIX "seek to address 0x%08x fail\n", offset);
+		return -1;
+	}
+
+	size = 0;
+	while (count > 0 &&
+			(size = file->read(file->context, buf, count)) > 0) {
+		count -= size;
+		buf += size;
+	}
+
+	if (size < 0) {
+		debug(PREFIX "an error occur when read firmware: %d\n", size);
+		return -1;
+	}
+
+	if (count > 0) {
+		debug(PREFIX "cannot read all data: %d\n", count);
+		return -1;
+	}
+
+	return 0;
+}
+
 /*
  * See vboot_reference/firmware/include/load_firmware_fw.h for documentation of
  * this function.