oem-ibm: libpldm: add en/decoders for R/WFileByType
Read/write operations on files of a specific type will use functions
defined in this commit for the responder and requester flow of PLDM.
Signed-off-by: vkaverap <vkaverap@in.ibm.com>
Change-Id: I3df2618d901bd82c8e552286a77195151f73bf55
diff --git a/oem/ibm/libpldm/file_io.c b/oem/ibm/libpldm/file_io.c
index 8b42ce0..4c833f1 100644
--- a/oem/ibm/libpldm/file_io.c
+++ b/oem/ibm/libpldm/file_io.c
@@ -587,3 +587,118 @@
return PLDM_SUCCESS;
}
+
+int decode_rw_file_by_type_req(const struct pldm_msg *msg,
+ size_t payload_length, uint16_t *file_type,
+ uint32_t *file_handle, uint32_t *offset,
+ uint32_t *length)
+{
+ if (msg == NULL || file_type == NULL || file_handle == NULL ||
+ offset == NULL || length == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (payload_length != PLDM_RW_FILE_BY_TYPE_REQ_BYTES) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ struct pldm_read_write_file_by_type_req *request =
+ (struct pldm_read_write_file_by_type_req *)msg->payload;
+ *file_type = le16toh(request->file_type);
+ *file_handle = le32toh(request->file_handle);
+ *offset = le32toh(request->offset);
+ *length = le32toh(request->length);
+
+ return PLDM_SUCCESS;
+}
+
+int encode_rw_file_by_type_resp(uint8_t instance_id, uint8_t command,
+ uint8_t completion_code, uint32_t length,
+ struct pldm_msg *msg)
+{
+ struct pldm_header_info header = {0};
+ int rc = PLDM_SUCCESS;
+
+ if (msg == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+ if (command != PLDM_READ_FILE_BY_TYPE &&
+ command != PLDM_WRITE_FILE_BY_TYPE) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ header.msg_type = PLDM_RESPONSE;
+ header.instance = instance_id;
+ header.pldm_type = PLDM_OEM;
+ header.command = command;
+
+ if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+ return rc;
+ }
+
+ struct pldm_read_write_file_by_type_resp *response =
+ (struct pldm_read_write_file_by_type_resp *)msg->payload;
+ response->completion_code = completion_code;
+ if (response->completion_code == PLDM_SUCCESS) {
+ response->length = htole32(length);
+ }
+
+ return PLDM_SUCCESS;
+}
+
+int encode_rw_file_by_type_req(uint8_t instance_id, uint8_t command,
+ uint16_t file_type, uint32_t file_handle,
+ uint32_t offset, uint32_t length,
+ struct pldm_msg *msg)
+{
+ struct pldm_header_info header = {0};
+ int rc = PLDM_SUCCESS;
+
+ if (msg == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+ if (command != PLDM_READ_FILE_BY_TYPE &&
+ command != PLDM_WRITE_FILE_BY_TYPE) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ header.msg_type = PLDM_REQUEST;
+ header.instance = instance_id;
+ header.pldm_type = PLDM_OEM;
+ header.command = command;
+
+ if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+ return rc;
+ }
+
+ struct pldm_read_write_file_by_type_req *req =
+ (struct pldm_read_write_file_by_type_req *)msg->payload;
+ req->file_type = htole16(file_type);
+ req->file_handle = htole32(file_handle);
+ req->offset = htole32(offset);
+ req->length = htole32(length);
+
+ return PLDM_SUCCESS;
+}
+
+int decode_rw_file_by_type_resp(const struct pldm_msg *msg,
+ size_t payload_length, uint8_t *completion_code,
+ uint32_t *length)
+{
+ if (msg == NULL || length == NULL || completion_code == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (payload_length != PLDM_RW_FILE_BY_TYPE_RESP_BYTES) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ struct pldm_read_write_file_by_type_resp *response =
+ (struct pldm_read_write_file_by_type_resp *)msg->payload;
+ *completion_code = response->completion_code;
+ if (*completion_code == PLDM_SUCCESS) {
+ *length = le32toh(response->length);
+ }
+
+ return PLDM_SUCCESS;
+}
diff --git a/oem/ibm/libpldm/file_io.h b/oem/ibm/libpldm/file_io.h
index d6c9b6a..c4ab935 100644
--- a/oem/ibm/libpldm/file_io.h
+++ b/oem/ibm/libpldm/file_io.h
@@ -21,6 +21,8 @@
PLDM_READ_FILE_BY_TYPE_INTO_MEMORY = 0x8,
PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY = 0x9,
PLDM_NEW_FILE_AVAILABLE = 0xA,
+ PLDM_READ_FILE_BY_TYPE = 0xB,
+ PLDM_WRITE_FILE_BY_TYPE = 0xC
};
/** @brief PLDM Command specific codes
@@ -60,6 +62,8 @@
#define PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES 5
#define PLDM_NEW_FILE_REQ_BYTES 10
#define PLDM_NEW_FILE_RESP_BYTES 1
+#define PLDM_RW_FILE_BY_TYPE_REQ_BYTES 14
+#define PLDM_RW_FILE_BY_TYPE_RESP_BYTES 5
/** @struct pldm_read_write_file_memory_req
*
@@ -510,6 +514,92 @@
int decode_new_file_resp(const struct pldm_msg *msg, size_t payload_length,
uint8_t *completion_code);
+/** @struct pldm_read_write_file_by_type_req
+ *
+ * Structure representing ReadFileByType and
+ * WriteFileByType request
+ */
+struct pldm_read_write_file_by_type_req {
+ uint16_t file_type; //!< Type of file
+ uint32_t file_handle; //!< Handle to file
+ uint32_t offset; //!< Offset to file where read/write starts
+ uint32_t length; //!< Bytes to be read
+} __attribute__((packed));
+
+/** @struct pldm_read_write_file_by_type_resp
+ *
+ * Structure representing ReadFileByType and
+ * WriteFileByType response
+ */
+struct pldm_read_write_file_by_type_resp {
+ uint8_t completion_code; //!< Completion code
+ uint32_t length; //!< Number of bytes read
+} __attribute__((packed));
+
+/** @brief Decode ReadFileByType and WriteFileByType
+ * commands request data
+ *
+ * @param[in] msg - Pointer to PLDM request message
+ * @param[in] payload_length - Length of request payload
+ * @param[in] file_type - Type of the file
+ * @param[out] file_handle - A handle to the file
+ * @param[out] offset - Offset to the file at which the read/write should begin
+ * @param[out] length - Number of bytes to be read/written
+ * @return pldm_completion_codes
+ */
+int decode_rw_file_by_type_req(const struct pldm_msg *msg,
+ size_t payload_length, uint16_t *file_type,
+ uint32_t *file_handle, uint32_t *offset,
+ uint32_t *length);
+
+/** @brief Create a PLDM response for ReadFileByType and
+ * WriteFileByType
+ *
+ * @param[in] instance_id - Message's instance id
+ * @param[in] command - PLDM command
+ * @param[in] completion_code - PLDM completion code
+ * @param[in] length - Number of bytes read/written. 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'
+ * @note File content has to be copied directly by the caller.
+ */
+int encode_rw_file_by_type_resp(uint8_t instance_id, uint8_t command,
+ uint8_t completion_code, uint32_t length,
+ struct pldm_msg *msg);
+
+/** @brief Encode ReadFileByType and WriteFileByType
+ * commands request data
+ *
+ * @param[in] instance_id - Message's instance id
+ * @param[in] command - PLDM command
+ * @param[in] file_type - Type of the file
+ * @param[in] file_handle - A handle to the file
+ * @param[in] offset - Offset to the file at which the read should begin
+ * @param[in] length - Number of bytes to be read/written
+ * @param[out] msg - Message will be written to this
+ * @return pldm_completion_codes
+ * @note File content has to be read directly by the caller.
+ */
+int encode_rw_file_by_type_req(uint8_t instance_id, uint8_t command,
+ uint16_t file_type, uint32_t file_handle,
+ uint32_t offset, uint32_t length,
+ struct pldm_msg *msg);
+
+/** @brief Decode ReadFileByType and WriteFileByType
+ * commands response data
+ *
+ * @param[in] msg - pointer to PLDM response message
+ * @param[in] payload_length - Length of response payload
+ * @param[out] completion_code - PLDM completion code
+ * @param[out] length - Number of bytes to be read/written
+ * @return pldm_completion_codes
+ */
+int decode_rw_file_by_type_resp(const struct pldm_msg *msg,
+ size_t payload_length, uint8_t *completion_code,
+ uint32_t *length);
+
#ifdef __cplusplus
}
#endif
diff --git a/oem/ibm/test/libpldm_fileio_test.cpp b/oem/ibm/test/libpldm_fileio_test.cpp
index a796bfe..6b3020f 100644
--- a/oem/ibm/test/libpldm_fileio_test.cpp
+++ b/oem/ibm/test/libpldm_fileio_test.cpp
@@ -1118,3 +1118,214 @@
ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
}
+
+TEST(ReadWriteFileByType, testGoodDecodeRequest)
+{
+ std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_REQ_BYTES + sizeof(pldm_msg_hdr)>
+ requestMsg{};
+
+ auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
+ auto request = reinterpret_cast<pldm_read_write_file_by_type_req*>(
+ requestPtr->payload);
+
+ // Random value for fileHandle, offset and length
+ uint16_t fileType = 0;
+ uint32_t fileHandle = 0x12345678;
+ uint32_t offset = 0x87654321;
+ uint32_t length = 0x13245768;
+
+ request->file_type = fileType;
+ request->file_handle = fileHandle;
+ request->offset = offset;
+ request->length = length;
+
+ uint16_t retFileType = 0x1;
+ uint32_t retFileHandle = 0;
+ uint32_t retOffset = 0;
+ uint32_t retLength = 0;
+
+ // Invoke decode the read file request
+ auto rc =
+ decode_rw_file_by_type_req(requestPtr, payload_length, &retFileType,
+ &retFileHandle, &retOffset, &retLength);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(fileType, retFileType);
+ ASSERT_EQ(fileHandle, retFileHandle);
+ ASSERT_EQ(offset, retOffset);
+ ASSERT_EQ(length, retLength);
+}
+
+TEST(ReadWriteFileByType, testGoodDecodeResponse)
+{
+ std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_RESP_BYTES + sizeof(pldm_msg_hdr)>
+ responseMsg{};
+
+ auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
+ size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
+ auto response = reinterpret_cast<pldm_read_write_file_by_type_resp*>(
+ responsePtr->payload);
+
+ // Random value for completion code and length
+ uint8_t completionCode = 0x0;
+ uint32_t length = 0x13245768;
+
+ response->completion_code = completionCode;
+ response->length = length;
+
+ uint8_t retCompletionCode = 0x1;
+ uint32_t retLength = 0;
+
+ // Invoke decode the read/write file response
+ auto rc = decode_rw_file_by_type_resp(responsePtr, payload_length,
+ &retCompletionCode, &retLength);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(completionCode, retCompletionCode);
+ ASSERT_EQ(length, retLength);
+}
+
+TEST(ReadWriteFileByType, testBadDecodeRequest)
+{
+ uint16_t fileType = 0;
+ uint32_t fileHandle = 0;
+ uint32_t offset = 0;
+ uint32_t length = 0;
+
+ // Request payload message is missing
+ auto rc = decode_rw_file_by_type_req(NULL, 0, &fileType, &fileHandle,
+ &offset, &length);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_REQ_BYTES + sizeof(pldm_msg_hdr)>
+ requestMsg{};
+
+ auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+ // Payload length is invalid
+ rc = decode_rw_file_by_type_req(requestPtr, 0, &fileType, &fileHandle,
+ &offset, &length);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(ReadWriteFileByType, testBadDecodeResponse)
+{
+ uint32_t length = 0;
+ uint8_t completionCode = 0;
+
+ // Request payload message is missing
+ auto rc = decode_rw_file_by_type_resp(NULL, 0, &completionCode, &length);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_RESP_BYTES + sizeof(pldm_msg_hdr)>
+ responseMsg{};
+
+ auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+ // Length is NULL
+ rc = decode_rw_file_by_type_resp(responsePtr, responseMsg.size() - hdrSize,
+ &completionCode, NULL);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ // Payload length is invalid
+ rc = decode_rw_file_by_type_resp(responsePtr, 0, &completionCode, &length);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(ReadWriteFileByType, testGoodEncodeRequest)
+{
+ std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_REQ_BYTES>
+ requestMsg{};
+
+ uint16_t fileType = 0;
+ uint32_t fileHandle = 0x12345678;
+ uint32_t offset = 0x87654321;
+ uint32_t length = 0x13245768;
+
+ pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+ auto rc = encode_rw_file_by_type_req(0, PLDM_READ_FILE_BY_TYPE, fileType,
+ fileHandle, offset, length, request);
+
+ ASSERT_EQ(rc, PLDM_SUCCESS);
+ ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
+ ASSERT_EQ(request->hdr.instance_id, 0);
+ ASSERT_EQ(request->hdr.type, PLDM_OEM);
+ ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_BY_TYPE);
+
+ ASSERT_EQ(0, memcmp(request->payload, &fileType, sizeof(fileType)));
+ ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType), &fileHandle,
+ sizeof(fileHandle)));
+
+ ASSERT_EQ(0,
+ memcmp(request->payload + sizeof(fileType) + sizeof(fileHandle),
+ &offset, sizeof(offset)));
+ ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType) +
+ sizeof(fileHandle) + sizeof(offset),
+ &length, sizeof(length)));
+}
+
+TEST(ReadWriteFileByType, testGoodEncodeResponse)
+{
+ std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES>
+ responseMsg{};
+
+ uint32_t length = 0x13245768;
+ uint8_t completionCode = 0x0;
+
+ pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+ auto rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE,
+ completionCode, 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_OEM);
+ ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE);
+
+ ASSERT_EQ(
+ 0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
+ ASSERT_EQ(0, memcmp(response->payload + sizeof(completionCode), &length,
+ sizeof(length)));
+}
+
+TEST(ReadWriteFileByType, testBadEncodeResponse)
+{
+ std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES>
+ responseMsg{};
+ uint32_t length = 0;
+ pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
+
+ // completion code is PLDM_ERROR
+ auto rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, 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_OEM);
+ ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE);
+ ASSERT_EQ(response->payload[0], PLDM_ERROR);
+
+ // response is NULL pointer
+ rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, PLDM_SUCCESS,
+ length, NULL);
+
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(ReadWriteFileByType, testBadEncodeRequest)
+{
+ uint8_t fileType = 0;
+ uint32_t fileHandle = 0;
+ uint32_t offset = 0;
+ uint32_t length = 0;
+
+ // request is NULL pointer
+ auto rc = encode_rw_file_by_type_req(0, PLDM_READ_FILE_BY_TYPE, fileType,
+ fileHandle, offset, length, NULL);
+
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}