ipmi: start implementing flashHashFinish

Change-Id: I354b346bcd2d0dd141fd21a84e57cd7c06eadc1a
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/flash-ipmi.cpp b/flash-ipmi.cpp
index ab294c3..35f8ad2 100644
--- a/flash-ipmi.cpp
+++ b/flash-ipmi.cpp
@@ -61,3 +61,9 @@
     /* TODO: implement. */
     return false;
 }
+
+bool FlashUpdate::hashFinish()
+{
+    /* TODO: implement. */
+    return false;
+}
diff --git a/flash-ipmi.hpp b/flash-ipmi.hpp
index ddcfa14..baf4c7f 100644
--- a/flash-ipmi.hpp
+++ b/flash-ipmi.hpp
@@ -124,6 +124,11 @@
      */
     virtual bool hashData(uint32_t offset,
                           const std::vector<uint8_t>& bytes) = 0;
+
+    /**
+     * Called to indicate the host is done sending the hash bytes.
+     */
+    virtual bool hashFinish() = 0;
 };
 
 class FlashUpdate : public UpdateInterface
@@ -142,6 +147,7 @@
 
     bool startHash(uint32_t length) override;
     bool hashData(uint32_t offset, const std::vector<uint8_t>& bytes) override;
+    bool hashFinish() override;
 
   private:
     /**
diff --git a/ipmi.cpp b/ipmi.cpp
index 50208d3..b9a8943 100644
--- a/ipmi.cpp
+++ b/ipmi.cpp
@@ -142,3 +142,17 @@
     (*dataLen) = 1;
     return IPMI_CC_OK;
 }
+
+ipmi_ret_t hashFinish(UpdateInterface* updater, const uint8_t* reqBuf,
+                      uint8_t* replyBuf, size_t* dataLen)
+{
+    if (!updater->hashFinish())
+    {
+        return IPMI_CC_INVALID;
+    }
+
+    /* 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 1010b6f..5401108 100644
--- a/ipmi.hpp
+++ b/ipmi.hpp
@@ -77,3 +77,16 @@
  */
 ipmi_ret_t hashBlock(UpdateInterface* updater, const uint8_t* reqBuf,
                      uint8_t* replyBuf, size_t* dataLen);
+
+/**
+ * Indicate all hash 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 hashFinish(UpdateInterface* updater, const uint8_t* reqBuf,
+                      uint8_t* replyBuf, size_t* dataLen);
diff --git a/main.cpp b/main.cpp
index cb86fcd..4d2e4bf 100644
--- a/main.cpp
+++ b/main.cpp
@@ -72,6 +72,9 @@
         case FlashSubCmds::flashHashData:
             return hashBlock(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
                              dataLen);
+        case FlashSubCmds::flashHashFinish:
+            return hashFinish(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
+                              dataLen);
         default:
             return IPMI_CC_INVALID;
     }
diff --git a/test/Makefile.am b/test/Makefile.am
index 392db5c..350c3a8 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -15,6 +15,7 @@
 	ipmi_flashfinish_unittest \
 	ipmi_starthash_unittest \
 	ipmi_hashdata_unittest \
+	ipmi_hashfinish_unittest \
 	ipmi_validate_unittest
 
 TESTS = $(check_PROGRAMS)
@@ -34,5 +35,8 @@
 ipmi_hashdata_unittest_SOURCES = ipmi_hashdata_unittest.cpp
 ipmi_hashdata_unittest_LDADD = $(top_builddir)/ipmi.o
 
+ipmi_hashfinish_unittest_SOURCES = ipmi_hashfinish_unittest.cpp
+ipmi_hashfinish_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_hashfinish_unittest.cpp b/test/ipmi_hashfinish_unittest.cpp
new file mode 100644
index 0000000..006525d
--- /dev/null
+++ b/test/ipmi_hashfinish_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(IpmiHashFinish, 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::flashHashFinish;
+
+    EXPECT_CALL(updater, hashFinish()).WillOnce(Return(true));
+    EXPECT_EQ(IPMI_CC_OK, hashFinish(&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 1e03a31..fa912ea 100644
--- a/test/updater_mock.hpp
+++ b/test/updater_mock.hpp
@@ -15,4 +15,5 @@
     MOCK_METHOD0(flashFinish, bool());
     MOCK_METHOD1(startHash, bool(uint32_t));
     MOCK_METHOD2(hashData, bool(uint32_t, const std::vector<uint8_t>&));
+    MOCK_METHOD0(hashFinish, bool());
 };