Implement response encode/request decode for ReadFileIntoMemory
The ReadFileIntoMemory command is an OEM command to support large
size transfer from BMC to Host via DMA mechanism.
Change-Id: I416a1af8a4d1ea91a7dffbbb98ae58eeccf6d0ea
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/.gitignore b/.gitignore
index 6148c38..ba334df 100644
--- a/.gitignore
+++ b/.gitignore
@@ -60,3 +60,4 @@
/test-suite.log
/test/*.gcda
/test/*.gcno
+/test/libpldm_fileio_test
diff --git a/Makefile.am b/Makefile.am
index d2456ab..e2a2d96 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1 +1 @@
-SUBDIRS = libpldm
+SUBDIRS = libpldm test
diff --git a/configure.ac b/configure.ac
index 6c9895c..88c4d9b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -20,6 +20,27 @@
# For linking
LT_INIT
+# Check/set gtest specific functions.
+AX_PTHREAD([GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=1"],[GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=0"])
+AC_SUBST(GTEST_CPPFLAGS)
+AC_ARG_ENABLE([oe-sdk],
+ AS_HELP_STRING([--enable-oe-sdk], [Link testcases absolutely against OE SDK so they can be ran within it.])
+)
+AC_ARG_VAR(OECORE_TARGET_SYSROOT,
+ [Path to the OE SDK SYSROOT])
+AS_IF([test "x$enable_oe_sdk" == "xyes"],
+ AS_IF([test "x$OECORE_TARGET_SYSROOT" == "x"],
+ AC_MSG_ERROR([OECORE_TARGET_SYSROOT must be set with --enable-oe-sdk])
+ )
+ AC_MSG_NOTICE([Enabling OE-SDK at $OECORE_TARGET_SYSROOT])
+ [
+ testcase_flags="-Wl,-rpath,\${OECORE_TARGET_SYSROOT}/lib"
+ testcase_flags="${testcase_flags} -Wl,-rpath,\${OECORE_TARGET_SYSROOT}/usr/lib"
+ testcase_flags="${testcase_flags} -Wl,-dynamic-linker,`find \${OECORE_TARGET_SYSROOT}/lib/ld-*.so | sort -r -n | head -n1`"
+ ]
+ AC_SUBST([OESDK_TESTCASE_FLAGS], [$testcase_flags])
+)
+
# Create configured output
-AC_CONFIG_FILES([Makefile libpldm/Makefile])
+AC_CONFIG_FILES([Makefile libpldm/Makefile test/Makefile])
AC_OUTPUT
diff --git a/libpldm/Makefile.am b/libpldm/Makefile.am
index 59da344..d837608 100644
--- a/libpldm/Makefile.am
+++ b/libpldm/Makefile.am
@@ -1,7 +1,11 @@
+nobase_include_HEADERS = \
+ file_io.h
+
libpldmoem_LTLIBRARIES = libpldmoem.la
libpldmoemdir = ${libdir}
libpldmoem_la_SOURCES = \
- base.c
+ base.c \
+ file_io.c
libpldmoem_la_LDFLAGS = \
-version-info 1:0:0 -shared
diff --git a/libpldm/file_io.c b/libpldm/file_io.c
new file mode 100644
index 0000000..426033a
--- /dev/null
+++ b/libpldm/file_io.c
@@ -0,0 +1,53 @@
+#include "file_io.h"
+#include <endian.h>
+#include <string.h>
+
+int decode_read_file_memory_req(const uint8_t *msg, size_t payload_length,
+ uint32_t *file_handle, uint32_t *offset,
+ uint32_t *length, uint64_t *address)
+{
+ if (msg == NULL || file_handle == NULL || offset == NULL ||
+ length == NULL || address == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (payload_length != PLDM_READ_FILE_MEM_REQ_BYTES) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ *file_handle = le32toh(*((uint32_t *)msg));
+ *offset = le32toh(*((uint32_t *)(msg + sizeof(*file_handle))));
+ *length = le32toh(
+ *((uint32_t *)(msg + sizeof(*file_handle) + sizeof(*offset))));
+ *address = le64toh(*((uint64_t *)(msg + sizeof(*file_handle) +
+ sizeof(*offset) + sizeof(*length))));
+
+ return PLDM_SUCCESS;
+}
+
+int encode_read_file_memory_resp(uint8_t instance_id, uint8_t completion_code,
+ uint32_t length, struct pldm_msg *msg)
+{
+ struct pldm_header_info header = {0};
+ int rc = PLDM_SUCCESS;
+
+ uint8_t *payload = msg->payload;
+ *payload = completion_code;
+
+ header.msg_type = PLDM_RESPONSE;
+ header.instance = instance_id;
+ header.pldm_type = PLDM_IBM_OEM_TYPE;
+ header.command = PLDM_READ_FILE_INTO_MEMORY;
+
+ if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+ return rc;
+ }
+
+ if (msg->payload[0] == PLDM_SUCCESS) {
+ uint8_t *dst = msg->payload + sizeof(completion_code);
+ length = htole32(length);
+ memcpy(dst, &length, sizeof(length));
+ }
+
+ return PLDM_SUCCESS;
+}
diff --git a/libpldm/file_io.h b/libpldm/file_io.h
new file mode 100644
index 0000000..c273608
--- /dev/null
+++ b/libpldm/file_io.h
@@ -0,0 +1,64 @@
+#ifndef FILEIO_H
+#define FILEIO_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "base.h"
+
+#define PLDM_IBM_OEM_TYPE 0x3F
+
+/** @brief PLDM Commands in IBM OEM type
+ */
+enum pldm_fileio_commands { PLDM_READ_FILE_INTO_MEMORY = 0x6 };
+
+/** @brief PLDM Command specific codes
+ */
+enum pldm_fileio_completion_codes {
+ PLDM_INVALID_FILE_HANDLE = 0x80,
+ PLDM_DATA_OUT_OF_RANGE = 0x81,
+ PLDM_INVALID_READ_LENGTH = 0x82
+};
+
+#define PLDM_READ_FILE_MEM_REQ_BYTES 20
+#define PLDM_READ_FILE_MEM_RESP_BYTES 5
+
+/* ReadFileIntoMemory */
+
+/** @brief Decode ReadFileIntoMemory commands request data
+ *
+ * @param[in] msg - Pointer to PLDM request message payload
+ * @param[in] payload_length - Length of request payload
+ * @param[out] file_handle - A handle to the file
+ * @param[out] offset - Offset to the file at which the read should begin
+ * @param[out] length - Number of bytes to be read
+ * @param[out] address - Memory address where the file content has to be
+ * written to
+ * @return pldm_completion_codes
+ */
+int decode_read_file_memory_req(const uint8_t *msg, size_t payload_length,
+ uint32_t *file_handle, uint32_t *offset,
+ uint32_t *length, uint64_t *address);
+
+/** @brief Create a PLDM response for ReadFileIntoMemory
+ *
+ * @param[in] instance_id - Message's instance id
+ * @param[in] completion_code - PLDM completion code
+ * @param[in] length - Number of bytes read. This could be less than what the
+ requester asked for.
+ * @param[in,out] msg - Message will be written to this
+ * @return pldm_completion_codes
+ * @note Caller is responsible for memory alloc and dealloc of param 'msg'
+ */
+int encode_read_file_memory_resp(uint8_t instance_id, uint8_t completion_code,
+ uint32_t length, struct pldm_msg *msg);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* FILEIO_H */
diff --git a/test/Makefile.am b/test/Makefile.am
new file mode 100644
index 0000000..f854cec
--- /dev/null
+++ b/test/Makefile.am
@@ -0,0 +1,27 @@
+AM_CPPFLAGS = -I$(top_srcdir)
+
+TESTS = $(check_PROGRAMS)
+
+check_PROGRAMS = \
+ libpldm_fileio_test
+
+test_cppflags = \
+ -Igtest \
+ $(GTEST_CPPFLAGS) \
+ $(AM_CPPFLAGS)
+
+test_cxxflags = \
+ $(PTHREAD_CFLAGS)
+
+test_ldflags = \
+ -lgtest_main \
+ -lgtest \
+ $(PTHREAD_LIBS) \
+ $(OESDK_TESTCASE_FLAGS)
+
+libpldm_fileio_test_CPPFLAGS = $(test_cppflags)
+libpldm_fileio_test_CXXFLAGS = $(test_cxxflags)
+libpldm_fileio_test_LDFLAGS = $(test_ldflags)
+libpldm_fileio_test_LDADD = $(top_builddir)/libpldm/base.o \
+ $(top_builddir)/libpldm/file_io.o
+libpldm_fileio_test_SOURCES = libpldm_fileio_test.cpp
diff --git a/test/libpldm_fileio_test.cpp b/test/libpldm_fileio_test.cpp
new file mode 100644
index 0000000..4968d39
--- /dev/null
+++ b/test/libpldm_fileio_test.cpp
@@ -0,0 +1,104 @@
+#include <string.h>
+
+#include <array>
+
+#include "libpldm/base.h"
+#include "libpldm/file_io.h"
+
+#include <gtest/gtest.h>
+
+TEST(ReadFileIntoMemory, testGoodDecodeRequest)
+{
+ std::array<uint8_t, PLDM_READ_FILE_MEM_REQ_BYTES> requestMsg{};
+
+ // Random value for fileHandle, offset, length, address
+ uint32_t fileHandle = 0x12345678;
+ uint32_t offset = 0x87654321;
+ uint32_t length = 0x13245768;
+ uint64_t address = 0x124356879ACBDE0F;
+
+ memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle));
+ memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset));
+ memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length,
+ sizeof(length));
+ memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) +
+ sizeof(length),
+ &address, sizeof(address));
+
+ uint32_t retFileHandle = 0;
+ uint32_t retOffset = 0;
+ uint32_t retLength = 0;
+ uint64_t retAddress = 0;
+
+ // Invoke decode the read file memory request
+ auto rc = decode_read_file_memory_req(requestMsg.data(), requestMsg.size(),
+ &retFileHandle, &retOffset,
+ &retLength, &retAddress);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(fileHandle, retFileHandle);
+ ASSERT_EQ(offset, retOffset);
+ ASSERT_EQ(length, retLength);
+ ASSERT_EQ(address, retAddress);
+}
+
+TEST(ReadFileIntoMemory, testBadDecodeRequest)
+{
+ uint32_t fileHandle = 0;
+ uint32_t offset = 0;
+ uint32_t length = 0;
+ uint64_t address = 0;
+
+ // Request payload message is missing
+ auto rc = decode_read_file_memory_req(NULL, 0, &fileHandle, &offset,
+ &length, &address);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ std::array<uint8_t, PLDM_READ_FILE_MEM_REQ_BYTES> requestMsg{};
+
+ // Address is NULL
+ rc = decode_read_file_memory_req(requestMsg.data(), requestMsg.size(),
+ &fileHandle, &offset, &length, NULL);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ // Payload length is invalid
+ rc = decode_read_file_memory_req(requestMsg.data(), 0, &fileHandle, &offset,
+ &length, &address);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(ReadFileIntoMemory, testGoodEncodeResponse)
+{
+ std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_MEM_RESP_BYTES>
+ responseMsg{};
+ uint32_t length = 0xFF00EE11;
+ pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+ auto rc = encode_read_file_memory_resp(0, PLDM_SUCCESS, length, response);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
+ ASSERT_EQ(response->hdr.instance_id, 0);
+ ASSERT_EQ(response->hdr.type, PLDM_IBM_OEM_TYPE);
+ ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
+ ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
+ ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
+ &length, sizeof(length)));
+}
+
+TEST(ReadFileIntoMemory, testBadEncodeResponse)
+{
+ std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_MEM_RESP_BYTES>
+ responseMsg{};
+ uint32_t length = 0;
+ pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+ auto rc = encode_read_file_memory_resp(0, PLDM_ERROR, length, response);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
+ ASSERT_EQ(response->hdr.instance_id, 0);
+ ASSERT_EQ(response->hdr.type, PLDM_IBM_OEM_TYPE);
+ ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
+ ASSERT_EQ(response->payload[0], PLDM_ERROR);
+}