copy_flash: update window size

When a pnor partition is copied to a window, update the window size with
the actual number of blocks copied. This is required in the response of
the V2 Read Window Command.

Change-Id: I2c158df1bd261a4e62b9cbb2765e7623a7fb3dc9
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/mboxd_flash_virtual.cpp b/mboxd_flash_virtual.cpp
index 6e3af71..182278b 100644
--- a/mboxd_flash_virtual.cpp
+++ b/mboxd_flash_virtual.cpp
@@ -24,6 +24,7 @@
 #include <sys/mman.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
+#include <algorithm>
 
 extern "C" {
 #include "common.h"
@@ -124,17 +125,18 @@
  * @offset:     The pnor offset to copy from (bytes)
  * @mem:        The buffer to copy into (must be of atleast 'size' bytes)
  * @size:       The number of bytes to copy
- *
- * Return:      0 on success otherwise negative error code
+ * Return:      Number of bytes copied on success, otherwise negative error
+ *              code. copy_flash will copy at most 'size' bytes, but it may
+ *              copy less.
  */
-int copy_flash(struct mbox_context* context, uint32_t offset, void* mem,
-               uint32_t size)
+int64_t copy_flash(struct mbox_context* context, uint32_t offset, void* mem,
+                   uint32_t size)
 {
     using namespace phosphor::logging;
     using namespace sdbusplus::xyz::openbmc_project::Common::Error;
     using namespace std::string_literals;
 
-    int rc = 0;
+    int rc = size;
 
     MSG_DBG("Copy virtual pnor to %p for size 0x%.8x from offset 0x%.8x\n",
             mem, size, offset);
@@ -151,9 +153,8 @@
         {
             const struct pnor_partition_table* table =
                 vpnor_get_partition_table(context);
-            memcpy(mem,
-                   ((uint8_t*)table) + offset,
-                   min_u32(sz - offset, size));
+            rc = std::min(sz - offset, static_cast<size_t>(size));
+            memcpy(mem, ((uint8_t*)table) + offset, rc);
         }
         else
         {