ipmi: start implementing flashHashData
Change-Id: Ib8c57c0f482515ed64867b9f1c98942d39c8a10f
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/flash-ipmi.cpp b/flash-ipmi.cpp
index daaab5e..ab294c3 100644
--- a/flash-ipmi.cpp
+++ b/flash-ipmi.cpp
@@ -55,3 +55,9 @@
/* TODO: implement. */
return false;
}
+
+bool FlashUpdate::hashData(uint32_t offset, const std::vector<uint8_t>& bytes)
+{
+ /* TODO: implement. */
+ return false;
+}
diff --git a/flash-ipmi.hpp b/flash-ipmi.hpp
index 7a63fc3..ddcfa14 100644
--- a/flash-ipmi.hpp
+++ b/flash-ipmi.hpp
@@ -114,6 +114,16 @@
* @return true on success, false otherwise.
*/
virtual bool startHash(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 hashData(uint32_t offset,
+ const std::vector<uint8_t>& bytes) = 0;
};
class FlashUpdate : public UpdateInterface
@@ -127,12 +137,11 @@
FlashUpdate& operator=(FlashUpdate&&) = default;
bool start(uint32_t length) override;
-
bool flashData(uint32_t offset, const std::vector<uint8_t>& bytes) override;
-
bool flashFinish() override;
bool startHash(uint32_t length) override;
+ bool hashData(uint32_t offset, const std::vector<uint8_t>& bytes) override;
private:
/**
diff --git a/ipmi.cpp b/ipmi.cpp
index 00f027a..50208d3 100644
--- a/ipmi.cpp
+++ b/ipmi.cpp
@@ -26,6 +26,7 @@
{FlashSubCmds::flashStartTransfer, sizeof(struct StartTx)},
{FlashSubCmds::flashDataBlock, sizeof(struct ChunkHdr) + 1},
{FlashSubCmds::flashStartHash, sizeof(struct StartTx)},
+ {FlashSubCmds::flashHashData, sizeof(struct ChunkHdr) + 1},
};
auto results = minimumLengths.find(command);
@@ -66,10 +67,10 @@
struct ChunkHdr hdr;
std::memcpy(&hdr, reqBuf, sizeof(hdr));
- size_t requestLength = (*dataLen);
+ auto requestLength = *dataLen;
/* Grab the bytes from the packet. */
- size_t bytesLength = requestLength - sizeof(struct ChunkHdr);
+ auto bytesLength = requestLength - sizeof(struct ChunkHdr);
std::vector<uint8_t> bytes(bytesLength);
std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength);
@@ -115,3 +116,29 @@
(*dataLen) = 1;
return IPMI_CC_OK;
}
+
+ipmi_ret_t hashBlock(UpdateInterface* updater, const uint8_t* reqBuf,
+ uint8_t* replyBuf, size_t* dataLen)
+{
+ struct ChunkHdr hdr;
+ std::memcpy(&hdr, reqBuf, sizeof(hdr));
+
+ auto requestLength = *dataLen;
+
+ /* Grab the bytes from the packet. */
+ auto bytesLength = requestLength - sizeof(struct ChunkHdr);
+ std::vector<uint8_t> bytes(bytesLength);
+ std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength);
+
+ /* TODO: Refactor this and dataBlock for re-use. */
+
+ if (!updater->hashData(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 e80c68e..1010b6f 100644
--- a/ipmi.hpp
+++ b/ipmi.hpp
@@ -64,3 +64,16 @@
*/
ipmi_ret_t startHash(UpdateInterface* updater, const uint8_t* reqBuf,
uint8_t* replyBuf, size_t* dataLen);
+
+/**
+ * Receive a flash hash 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 hashBlock(UpdateInterface* updater, const uint8_t* reqBuf,
+ uint8_t* replyBuf, size_t* dataLen);
diff --git a/main.cpp b/main.cpp
index d56fed5..cb86fcd 100644
--- a/main.cpp
+++ b/main.cpp
@@ -69,6 +69,9 @@
case FlashSubCmds::flashStartHash:
return startHash(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
dataLen);
+ case FlashSubCmds::flashHashData:
+ return hashBlock(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
+ dataLen);
default:
return IPMI_CC_INVALID;
}
diff --git a/test/Makefile.am b/test/Makefile.am
index 282a824..392db5c 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -14,6 +14,7 @@
ipmi_flashdata_unittest \
ipmi_flashfinish_unittest \
ipmi_starthash_unittest \
+ ipmi_hashdata_unittest \
ipmi_validate_unittest
TESTS = $(check_PROGRAMS)
@@ -30,5 +31,8 @@
ipmi_starthash_unittest_SOURCES = ipmi_starthash_unittest.cpp
ipmi_starthash_unittest_LDADD = $(top_builddir)/ipmi.o
+ipmi_hashdata_unittest_SOURCES = ipmi_hashdata_unittest.cpp
+ipmi_hashdata_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_hashdata_unittest.cpp b/test/ipmi_hashdata_unittest.cpp
new file mode 100644
index 0000000..17de66c
--- /dev/null
+++ b/test/ipmi_hashdata_unittest.cpp
@@ -0,0 +1,43 @@
+#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(IpmiHashData, 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::flashHashData;
+ 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, hashData(0x00, ElementsAreArray(bytes, sizeof(bytes))))
+ .WillOnce(Return(true));
+
+ EXPECT_EQ(IPMI_CC_OK, hashBlock(&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 be4b128..14ada91 100644
--- a/test/ipmi_validate_unittest.cpp
+++ b/test/ipmi_validate_unittest.cpp
@@ -41,8 +41,15 @@
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));
}
+
+TEST(IpmiValidateTest, InvalidLengthHashBlockReturnsFalse)
+{
+ // This request isn't large enough to be well-formed.
+ auto cmd = FlashSubCmds::flashHashData;
+ size_t dataLen = sizeof(struct ChunkHdr) - 1;
+ EXPECT_FALSE(validateRequestLength(cmd, dataLen));
+}
diff --git a/test/updater_mock.hpp b/test/updater_mock.hpp
index 561deaa..1e03a31 100644
--- a/test/updater_mock.hpp
+++ b/test/updater_mock.hpp
@@ -14,4 +14,5 @@
MOCK_METHOD2(flashData, bool(uint32_t, const std::vector<uint8_t>&));
MOCK_METHOD0(flashFinish, bool());
MOCK_METHOD1(startHash, bool(uint32_t));
+ MOCK_METHOD2(hashData, bool(uint32_t, const std::vector<uint8_t>&));
};