ipmi: start implementing flashDataFinish

Change-Id: I627f6b788311650637221413a93ce7f1a3f0d0cd
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/flash-ipmi.cpp b/flash-ipmi.cpp
index 1c75246..c4f2aec 100644
--- a/flash-ipmi.cpp
+++ b/flash-ipmi.cpp
@@ -43,3 +43,9 @@
     /* TODO: implement. */
     return false;
 }
+
+bool FlashUpdate::flashFinish()
+{
+    /* TODO: implement. */
+    return false;
+}
diff --git a/flash-ipmi.hpp b/flash-ipmi.hpp
index db7765e..56c32da 100644
--- a/flash-ipmi.hpp
+++ b/flash-ipmi.hpp
@@ -84,9 +84,28 @@
   public:
     virtual ~UpdateInterface() = default;
 
+    /**
+     * Prepare to receive a BMC image and then a signature.
+     *
+     * @param[in] length - the size of the flash image.
+     * @return true on success, false otherwise.
+     */
     virtual bool start(uint32_t length) = 0;
+
+    /**
+     * Attempt to write the bytes at the offset.
+     *
+     * @param[in] offset - the 0-based byte offset into the flash image.
+     * @param[in] bytes - the bytes to write.
+     * @return true on success, false otherwise.
+     */
     virtual bool flashData(uint32_t offset,
                            const std::vector<uint8_t>& bytes) = 0;
+
+    /**
+     * Called to indicate the host is done sending the flash bytes.
+     */
+    virtual bool flashFinish() = 0;
 };
 
 class FlashUpdate : public UpdateInterface
@@ -99,23 +118,12 @@
     FlashUpdate(FlashUpdate&&) = default;
     FlashUpdate& operator=(FlashUpdate&&) = default;
 
-    /**
-     * Prepare to receive a BMC image and then a signature.
-     *
-     * @param[in] length - the size of the flash image.
-     * @return true on success, false otherwise.
-     */
     bool start(uint32_t length) override;
 
-    /**
-     * Attempt to write the bytes at the offset.
-     *
-     * @param[in] offset - the 0-based byte offset into the flash image.
-     * @param[in] bytes - the bytes to write.
-     * @return true on success, false otherwise.
-     */
     bool flashData(uint32_t offset, const std::vector<uint8_t>& bytes) override;
 
+    bool flashFinish() override;
+
   private:
     /**
      * Tries to close out and delete anything staged.
diff --git a/ipmi.cpp b/ipmi.cpp
index 5752682..d8e9f89 100644
--- a/ipmi.cpp
+++ b/ipmi.cpp
@@ -82,3 +82,19 @@
     (*dataLen) = 1;
     return IPMI_CC_OK;
 }
+
+ipmi_ret_t dataFinish(UpdateInterface* updater, const uint8_t* reqBuf,
+                      uint8_t* replyBuf, size_t* dataLen)
+{
+    if (!updater->flashFinish())
+    {
+        return IPMI_CC_INVALID;
+    }
+
+    /* TODO: If all commands return this on success, handle it in one place. */
+
+    /* We were successful and set the response byte to 0. */
+    replyBuf[0] = 0x00;
+    (*dataLen) = 1;
+    return IPMI_CC_OK;
+}
diff --git a/ipmi.hpp b/ipmi.hpp
index 0644ad8..050f7ca 100644
--- a/ipmi.hpp
+++ b/ipmi.hpp
@@ -38,3 +38,16 @@
  */
 ipmi_ret_t dataBlock(UpdateInterface* updater, const uint8_t* reqBuf,
                      uint8_t* replyBuf, size_t* dataLen);
+
+/**
+ * Indicate all flash data has been sent.
+ *
+ * @param[in] updater - Pointer to Updater object.
+ * @param[in] reqBuf - the IPMI packet.
+ * @param[in] replyBuf - Pointer to buffer for any response.
+ * @param[in,out] dataLen - Initially reqBuf length, set to replyBuf
+ * length when done.
+ * @return corresponding IPMI return code.
+ */
+ipmi_ret_t dataFinish(UpdateInterface* updater, const uint8_t* reqBuf,
+                      uint8_t* replyBuf, size_t* dataLen);
diff --git a/main.cpp b/main.cpp
index 7a22923..29117dd 100644
--- a/main.cpp
+++ b/main.cpp
@@ -63,6 +63,9 @@
         case FlashSubCmds::flashDataBlock:
             return dataBlock(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
                              dataLen);
+        case FlashSubCmds::flashDataFinish:
+            return dataFinish(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
+                              dataLen);
         default:
             return IPMI_CC_INVALID;
     }
diff --git a/test/Makefile.am b/test/Makefile.am
index 9df14db..8f86df7 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -12,6 +12,7 @@
 check_PROGRAMS = \
 	ipmi_starttransfer_unittest \
 	ipmi_flashdata_unittest \
+	ipmi_flashfinish_unittest \
 	ipmi_validate_unittest
 
 TESTS = $(check_PROGRAMS)
@@ -22,5 +23,8 @@
 ipmi_flashdata_unittest_SOURCES = ipmi_flashdata_unittest.cpp
 ipmi_flashdata_unittest_LDADD = $(top_builddir)/ipmi.o
 
+ipmi_flashfinish_unittest_SOURCES = ipmi_flashfinish_unittest.cpp
+ipmi_flashfinish_unittest_LDADD = $(top_builddir)/ipmi.o
+
 ipmi_validate_unittest_SOURCES = ipmi_validate_unittest.cpp
 ipmi_validate_unittest_LDADD = $(top_builddir)/ipmi.o
diff --git a/test/ipmi_flashfinish_unittest.cpp b/test/ipmi_flashfinish_unittest.cpp
new file mode 100644
index 0000000..6844776
--- /dev/null
+++ b/test/ipmi_flashfinish_unittest.cpp
@@ -0,0 +1,34 @@
+#include "flash-ipmi.hpp"
+#include "ipmi.hpp"
+
+#include "updater_mock.hpp"
+
+#include <cstring>
+#include <gtest/gtest.h>
+
+using ::testing::Return;
+using ::testing::StrictMock;
+
+// ipmid.hpp isn't installed where we can grab it and this value is per BMC
+// SoC.
+#define MAX_IPMI_BUFFER 64
+
+TEST(IpmiFlashFinish, CallPassedOn)
+{
+    // The data finish command does no validation as the input is empty, and
+    // simply relies on the flash manager to do work.
+
+    StrictMock<UpdaterMock> updater;
+
+    size_t dataLen;
+    uint8_t request[MAX_IPMI_BUFFER] = {0};
+    uint8_t reply[MAX_IPMI_BUFFER] = {0};
+
+    dataLen = 1; // request is only the command.
+    request[0] = FlashSubCmds::flashDataFinish;
+
+    EXPECT_CALL(updater, flashFinish()).WillOnce(Return(true));
+    EXPECT_EQ(IPMI_CC_OK, dataFinish(&updater, request, reply, &dataLen));
+    EXPECT_EQ(sizeof(uint8_t), dataLen);
+    EXPECT_EQ(0, reply[0]);
+}
diff --git a/test/updater_mock.hpp b/test/updater_mock.hpp
index ef0094b..732df31 100644
--- a/test/updater_mock.hpp
+++ b/test/updater_mock.hpp
@@ -12,4 +12,5 @@
 
     MOCK_METHOD1(start, bool(uint32_t));
     MOCK_METHOD2(flashData, bool(uint32_t, const std::vector<uint8_t>&));
+    MOCK_METHOD0(flashFinish, bool());
 };