ipmi: start implementing flashDataBlock

Change-Id: I780c0e5da77a027ce23a674bd3fdbf08b8a81c14
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/flash-ipmi.cpp b/flash-ipmi.cpp
index 4d05044..1c75246 100644
--- a/flash-ipmi.cpp
+++ b/flash-ipmi.cpp
@@ -37,3 +37,9 @@
     /* Start over! */
     return openEverything();
 }
+
+bool FlashUpdate::flashData(uint32_t offset, const std::vector<uint8_t>& bytes)
+{
+    /* TODO: implement. */
+    return false;
+}
diff --git a/flash-ipmi.hpp b/flash-ipmi.hpp
index deef582..db7765e 100644
--- a/flash-ipmi.hpp
+++ b/flash-ipmi.hpp
@@ -1,5 +1,7 @@
 #pragma once
 
+#include <vector>
+
 #include "host-ipmid/ipmid-api.h"
 
 /* Clearer way to represent the subcommand size. */
@@ -70,12 +72,21 @@
     uint32_t length; /* Maximum image length is 4GiB (little-endian) */
 } __attribute__((packed));
 
+struct ChunkHdr
+{
+    uint8_t cmd;
+    uint32_t offset; /* 0-based write offset */
+    uint8_t data[];
+} __attribute__((packed));
+
 class UpdateInterface
 {
   public:
     virtual ~UpdateInterface() = default;
 
     virtual bool start(uint32_t length) = 0;
+    virtual bool flashData(uint32_t offset,
+                           const std::vector<uint8_t>& bytes) = 0;
 };
 
 class FlashUpdate : public UpdateInterface
@@ -96,6 +107,15 @@
      */
     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;
+
   private:
     /**
      * Tries to close out and delete anything staged.
diff --git a/ipmi.cpp b/ipmi.cpp
index 65a719a..6201f0e 100644
--- a/ipmi.cpp
+++ b/ipmi.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include <cstring>
+
 #include "flash-ipmi.hpp"
 #include "ipmi.hpp"
 
@@ -38,3 +40,32 @@
     (*dataLen) = 1;
     return IPMI_CC_OK;
 }
+
+ipmi_ret_t dataBlock(UpdateInterface* updater, const uint8_t* reqBuf,
+                     uint8_t* replyBuf, size_t* dataLen)
+{
+    size_t requestLength = (*dataLen);
+    /* Require at least one byte. */
+    if (requestLength < sizeof(struct ChunkHdr) + 1)
+    {
+        return IPMI_CC_INVALID;
+    }
+
+    struct ChunkHdr hdr;
+    std::memcpy(&hdr, reqBuf, sizeof(hdr));
+
+    /* Grab the bytes from the packet. */
+    size_t bytesLength = requestLength - sizeof(struct ChunkHdr);
+    std::vector<uint8_t> bytes(bytesLength);
+    std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength);
+
+    if (!updater->flashData(hdr.offset, bytes))
+    {
+        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 3b622b0..dcaf33e 100644
--- a/ipmi.hpp
+++ b/ipmi.hpp
@@ -16,3 +16,16 @@
  */
 ipmi_ret_t startTransfer(UpdateInterface* updater, const uint8_t* reqBuf,
                          uint8_t* replyBuf, size_t* dataLen);
+
+/**
+ * Receive a flash image data block and store it.
+ *
+ * @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 dataBlock(UpdateInterface* updater, const uint8_t* reqBuf,
+                     uint8_t* replyBuf, size_t* dataLen);
diff --git a/main.cpp b/main.cpp
index a3288ef..9b78f77 100644
--- a/main.cpp
+++ b/main.cpp
@@ -54,6 +54,9 @@
         case FlashSubCmds::flashStartTransfer:
             return startTransfer(flashUpdateSingleton.get(), reqBuf,
                                  replyCmdBuf, dataLen);
+        case FlashSubCmds::flashDataBlock:
+            return dataBlock(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
+                             dataLen);
         default:
             return IPMI_CC_INVALID;
     }
diff --git a/test/Makefile.am b/test/Makefile.am
index 7da78d8..d9fd289 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -10,9 +10,13 @@
 
 # Run all 'check' test programs
 check_PROGRAMS = \
-	ipmi_starttransfer_unittest
+	ipmi_starttransfer_unittest \
+	ipmi_flashdata_unittest
 
 TESTS = $(check_PROGRAMS)
 
 ipmi_starttransfer_unittest_SOURCES = ipmi_starttransfer_unittest.cpp
 ipmi_starttransfer_unittest_LDADD = $(top_builddir)/ipmi.o
+
+ipmi_flashdata_unittest_SOURCES = ipmi_flashdata_unittest.cpp
+ipmi_flashdata_unittest_LDADD = $(top_builddir)/ipmi.o
diff --git a/test/ipmi_flashdata_unittest.cpp b/test/ipmi_flashdata_unittest.cpp
new file mode 100644
index 0000000..dcb27f6
--- /dev/null
+++ b/test/ipmi_flashdata_unittest.cpp
@@ -0,0 +1,84 @@
+#include "flash-ipmi.hpp"
+#include "ipmi.hpp"
+
+#include "updater_mock.hpp"
+
+#include <cstring>
+#include <gtest/gtest.h>
+
+using ::testing::ElementsAreArray;
+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(IpmiFlashData, InvalidRequestLengthReturnsFailure)
+{
+    // This request isn't large enough to be well-formed.
+
+    StrictMock<UpdaterMock> updater;
+
+    size_t dataLen;
+    uint8_t request[MAX_IPMI_BUFFER] = {0};
+    uint8_t reply[MAX_IPMI_BUFFER] = {0};
+
+    struct ChunkHdr tx;
+    tx.cmd = FlashSubCmds::flashDataBlock;
+    tx.offset = 0x00;
+    std::memcpy(request, &tx, sizeof(tx));
+
+    dataLen = sizeof(tx) - 1; // It's too small to be a valid packet.
+
+    EXPECT_EQ(IPMI_CC_INVALID, dataBlock(&updater, request, reply, &dataLen));
+}
+
+TEST(IpmiFlashData, NoDataReturnsFailure)
+{
+    // If the request has no data, it's invalid, returns failure.
+
+    StrictMock<UpdaterMock> updater;
+
+    size_t dataLen;
+    uint8_t request[MAX_IPMI_BUFFER] = {0};
+    uint8_t reply[MAX_IPMI_BUFFER] = {0};
+
+    struct ChunkHdr tx;
+    tx.cmd = FlashSubCmds::flashDataBlock;
+    tx.offset = 0x00;
+    std::memcpy(request, &tx, sizeof(tx));
+
+    dataLen = sizeof(tx); // It's only the header, so no data.
+
+    EXPECT_EQ(IPMI_CC_INVALID, dataBlock(&updater, request, reply, &dataLen));
+}
+
+TEST(IpmiFlashData, DataReceivedIsPassedOnOk)
+{
+    // If valid data was passed in, it'll pass it onto the update handler.
+
+    StrictMock<UpdaterMock> updater;
+
+    size_t dataLen;
+    uint8_t request[MAX_IPMI_BUFFER] = {0};
+    uint8_t reply[MAX_IPMI_BUFFER] = {0};
+
+    struct ChunkHdr tx;
+    tx.cmd = FlashSubCmds::flashDataBlock;
+    tx.offset = 0x00;
+    std::memcpy(request, &tx, sizeof(tx));
+
+    uint8_t bytes[] = {0x04, 0x05};
+    std::memcpy(&request[sizeof(struct ChunkHdr)], bytes, sizeof(bytes));
+
+    dataLen = sizeof(struct ChunkHdr) + sizeof(bytes);
+
+    EXPECT_CALL(updater,
+                flashData(0x00, ElementsAreArray(bytes, sizeof(bytes))))
+        .WillOnce(Return(true));
+
+    EXPECT_EQ(IPMI_CC_OK, dataBlock(&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 3c2b5ab..ef0094b 100644
--- a/test/updater_mock.hpp
+++ b/test/updater_mock.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <gmock/gmock.h>
+#include <vector>
 
 #include "flash-ipmi.hpp"
 
@@ -10,4 +11,5 @@
     virtual ~UpdaterMock() = default;
 
     MOCK_METHOD1(start, bool(uint32_t));
+    MOCK_METHOD2(flashData, bool(uint32_t, const std::vector<uint8_t>&));
 };