ipmi: start implementing flashStartHash

Change-Id: I5465bd672eac510be30d50cac8cf34b86a24ca40
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/flash-ipmi.cpp b/flash-ipmi.cpp
index c4f2aec..daaab5e 100644
--- a/flash-ipmi.cpp
+++ b/flash-ipmi.cpp
@@ -49,3 +49,9 @@
     /* TODO: implement. */
     return false;
 }
+
+bool FlashUpdate::startHash(uint32_t length)
+{
+    /* TODO: implement. */
+    return false;
+}
diff --git a/flash-ipmi.hpp b/flash-ipmi.hpp
index 56c32da..7a63fc3 100644
--- a/flash-ipmi.hpp
+++ b/flash-ipmi.hpp
@@ -106,6 +106,14 @@
      * Called to indicate the host is done sending the flash bytes.
      */
     virtual bool flashFinish() = 0;
+
+    /**
+     * Prepare to receive a BMC image signature.
+     *
+     * @param[in] length - length of signature in bytes.
+     * @return true on success, false otherwise.
+     */
+    virtual bool startHash(uint32_t length) = 0;
 };
 
 class FlashUpdate : public UpdateInterface
@@ -124,6 +132,8 @@
 
     bool flashFinish() override;
 
+    bool startHash(uint32_t length) override;
+
   private:
     /**
      * Tries to close out and delete anything staged.
diff --git a/ipmi.cpp b/ipmi.cpp
index d8e9f89..00f027a 100644
--- a/ipmi.cpp
+++ b/ipmi.cpp
@@ -25,6 +25,7 @@
     static const std::unordered_map<FlashSubCmds, size_t> minimumLengths = {
         {FlashSubCmds::flashStartTransfer, sizeof(struct StartTx)},
         {FlashSubCmds::flashDataBlock, sizeof(struct ChunkHdr) + 1},
+        {FlashSubCmds::flashStartHash, sizeof(struct StartTx)},
     };
 
     auto results = minimumLengths.find(command);
@@ -98,3 +99,19 @@
     (*dataLen) = 1;
     return IPMI_CC_OK;
 }
+
+ipmi_ret_t startHash(UpdateInterface* updater, const uint8_t* reqBuf,
+                     uint8_t* replyBuf, size_t* dataLen)
+{
+    auto request = reinterpret_cast<const struct StartTx*>(reqBuf);
+
+    if (!updater->startHash(request->length))
+    {
+        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 050f7ca..e80c68e 100644
--- a/ipmi.hpp
+++ b/ipmi.hpp
@@ -51,3 +51,16 @@
  */
 ipmi_ret_t dataFinish(UpdateInterface* updater, const uint8_t* reqBuf,
                       uint8_t* replyBuf, size_t* dataLen);
+
+/**
+ * Prepare to receive a BMC image signature.
+ *
+ * @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 startHash(UpdateInterface* updater, const uint8_t* reqBuf,
+                     uint8_t* replyBuf, size_t* dataLen);
diff --git a/main.cpp b/main.cpp
index 29117dd..d56fed5 100644
--- a/main.cpp
+++ b/main.cpp
@@ -66,6 +66,9 @@
         case FlashSubCmds::flashDataFinish:
             return dataFinish(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
                               dataLen);
+        case FlashSubCmds::flashStartHash:
+            return startHash(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
+                             dataLen);
         default:
             return IPMI_CC_INVALID;
     }
diff --git a/test/Makefile.am b/test/Makefile.am
index 8f86df7..282a824 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -13,6 +13,7 @@
 	ipmi_starttransfer_unittest \
 	ipmi_flashdata_unittest \
 	ipmi_flashfinish_unittest \
+	ipmi_starthash_unittest \
 	ipmi_validate_unittest
 
 TESTS = $(check_PROGRAMS)
@@ -26,5 +27,8 @@
 ipmi_flashfinish_unittest_SOURCES = ipmi_flashfinish_unittest.cpp
 ipmi_flashfinish_unittest_LDADD = $(top_builddir)/ipmi.o
 
+ipmi_starthash_unittest_SOURCES = ipmi_starthash_unittest.cpp
+ipmi_starthash_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_starthash_unittest.cpp b/test/ipmi_starthash_unittest.cpp
new file mode 100644
index 0000000..c8db942
--- /dev/null
+++ b/test/ipmi_starthash_unittest.cpp
@@ -0,0 +1,40 @@
+#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
+#define HASH_SIZE 512
+
+TEST(IpmiStartHashTest, ValidBoringCaseReturnsSuccess)
+{
+    // This is also mostly a pass-thru command such that the real validation is
+    // done elsewhere.
+
+    StrictMock<UpdaterMock> updater;
+
+    size_t dataLen;
+    uint8_t request[MAX_IPMI_BUFFER] = {0};
+    uint8_t reply[MAX_IPMI_BUFFER] = {0};
+
+    struct StartTx tx;
+    tx.cmd = FlashSubCmds::flashStartHash;
+    tx.length = HASH_SIZE;
+    std::memcpy(request, &tx, sizeof(tx));
+
+    dataLen = sizeof(tx);
+
+    EXPECT_CALL(updater, startHash(HASH_SIZE)).WillOnce(Return(true));
+
+    EXPECT_EQ(IPMI_CC_OK, startHash(&updater, request, reply, &dataLen));
+    EXPECT_EQ(sizeof(uint8_t), dataLen);
+    EXPECT_EQ(0, reply[0]);
+}
diff --git a/test/ipmi_validate_unittest.cpp b/test/ipmi_validate_unittest.cpp
index ee600a0..be4b128 100644
--- a/test/ipmi_validate_unittest.cpp
+++ b/test/ipmi_validate_unittest.cpp
@@ -37,3 +37,12 @@
     size_t dataLen = sizeof(struct ChunkHdr);
     EXPECT_FALSE(validateRequestLength(cmd, dataLen));
 }
+
+TEST(IpmiValidateTest, StartHashInvalidReturnsFalse)
+{
+    // Verify the request is sanity checked w.r.t length.
+
+    auto cmd = FlashSubCmds::flashStartHash;
+    size_t dataLen = sizeof(struct StartTx) - 1;
+    EXPECT_FALSE(validateRequestLength(cmd, dataLen));
+}
diff --git a/test/updater_mock.hpp b/test/updater_mock.hpp
index 732df31..561deaa 100644
--- a/test/updater_mock.hpp
+++ b/test/updater_mock.hpp
@@ -13,4 +13,5 @@
     MOCK_METHOD1(start, bool(uint32_t));
     MOCK_METHOD2(flashData, bool(uint32_t, const std::vector<uint8_t>&));
     MOCK_METHOD0(flashFinish, bool());
+    MOCK_METHOD1(startHash, bool(uint32_t));
 };