vpnor: implement init_flash_dev

The vpnor version needs to set the erase block size as 4K.
That's the size hostboot expects in the FFS structure.

This change also requires moving code from mboxd_flash.c to
mboxd_flash_physical.c, else there will be 3 symbols per flash function.

It should suffice to just have mboxd_flash_physical.c and
mboxd_flash_virtual.cpp.

Change-Id: I35442a0c1dbee7f66b278cbf094be78e870b6c86
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/mboxd_flash_virtual.cpp b/mboxd_flash_virtual.cpp
index 3c83f02..6e3af71 100644
--- a/mboxd_flash_virtual.cpp
+++ b/mboxd_flash_virtual.cpp
@@ -23,6 +23,7 @@
 #include <syslog.h>
 #include <sys/mman.h>
 #include <unistd.h>
+#include <sys/ioctl.h>
 
 extern "C" {
 #include "common.h"
@@ -36,10 +37,87 @@
 #include <phosphor-logging/log.hpp>
 #include <phosphor-logging/elog-errors.hpp>
 
+#include <memory>
 #include <string>
 #include <exception>
 #include <stdexcept>
 
+/** @brief unique_ptr functor to release a char* reference. */
+struct StringDeleter
+{
+    void operator()(char* ptr) const
+    {
+        free(ptr);
+    }
+};
+using StringPtr = std::unique_ptr<char, StringDeleter>;
+
+int init_flash_dev(struct mbox_context *context)
+{
+    StringPtr filename(get_dev_mtd());
+    int fd = 0;
+    int rc = 0;
+
+    if (!filename)
+    {
+        MSG_ERR("Couldn't find the flash /dev/mtd partition\n");
+        return -1;
+    }
+
+    MSG_DBG("Opening %s\n", filename.get());
+
+    fd = open(filename.get(), O_RDWR);
+    if (fd < 0)
+    {
+        MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
+                filename.get(), strerror(errno));
+        return -errno;
+    }
+
+    // Read the Flash Info
+    if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1)
+    {
+        MSG_ERR("Couldn't get information about MTD: %s\n",
+                strerror(errno));
+        close(fd);
+        return -errno;
+    }
+
+    if (context->flash_size == 0)
+    {
+        // See comment in mboxd_flash_physical.c on why
+        // this is needed.
+        context->flash_size = context->mtd_info.size;
+    }
+
+    // Hostboot requires a 4K block-size to be used in the FFS flash structure
+    context->mtd_info.erasesize = 4096;
+    context->erase_size_shift = log_2(context->mtd_info.erasesize);
+    context->flash_bmap = NULL;
+    context->fds[MTD_FD].fd = -1;
+
+    close(fd);
+    return rc;
+}
+
+void free_flash_dev(struct mbox_context *context)
+{
+    // No-op
+}
+
+int set_flash_bytemap(struct mbox_context *context, uint32_t offset,
+                      uint32_t count, uint8_t val)
+{
+    // No-op
+    return 0;
+}
+
+int erase_flash(struct mbox_context *context, uint32_t offset, uint32_t count)
+{
+    // No-op
+    return 0;
+}
+
 /*
  * copy_flash() - Copy data from the virtual pnor into a provided buffer
  * @context:    The mbox context pointer