dsp: file: Add encode req & decode resp for DfOpen command
Added encode/decode APIs for DfOpen command(0x01)
which is defined in DSP0242 Version 1.0.0 Section: 9.2.
Change-Id: I5a975a7ae2bbd4115898486984ad96016590b04b
Signed-off-by: Chau Ly <chaul@amperecomputing.com>
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a3afd66..f3327ff 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -41,6 +41,8 @@
- platform: Add decode_pldm_file_descriptor_pdr() and
decode_pldm_file_descriptor_pdr_names()
+- file: Add encode req & decode resp for DfOpen command.
+
### Changed
- dsp: firmware_update: Expand "params" in symbol names
diff --git a/include/libpldm/base.h b/include/libpldm/base.h
index f74404e..9088d80 100644
--- a/include/libpldm/base.h
+++ b/include/libpldm/base.h
@@ -28,6 +28,7 @@
PLDM_FRU = 0x04,
PLDM_FWUP = 0x05,
PLDM_RDE = 0x06,
+ PLDM_FILE = 0x07,
PLDM_OEM = 0x3f,
};
diff --git a/include/libpldm/file.h b/include/libpldm/file.h
new file mode 100644
index 0000000..0c0eb0e
--- /dev/null
+++ b/include/libpldm/file.h
@@ -0,0 +1,101 @@
+/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
+#ifndef LIBPLDM_FILE_H
+#define LIBPLDM_FILE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <libpldm/pldm_types.h>
+
+#include <asm/byteorder.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#define PLDM_DF_OPEN_REQ_BYTES 4
+#define PLDM_DF_OPEN_RESP_BYTES 3
+
+/** @brief PLDM File Transfer Completion Code */
+enum pldm_file_cc {
+ PLDM_FILE_CC_INVALID_FILE_DESCRIPTOR = 0x80,
+ PLDM_FILE_CC_INVALID_DF_ATTRIBUTE = 0x81,
+ PLDM_FILE_CC_ZEROLENGTH_NOT_ALLOWED = 0x82,
+ PLDM_FILE_CC_EXCLUSIVE_OWNERSHIP_NOT_ESTABLISHED = 0x83,
+ PLDM_FILE_CC_EXCLUSIVE_OWNERSHIP_NOT_ALLOWED = 0x84,
+ PLDM_FILE_CC_EXCLUSIVE_OWNERSHIP_NOT_AVAILABLE = 0x85,
+ PLDM_FILE_CC_INVALID_FILE_IDENTIFIER = 0x86,
+ PLDM_FILE_CC_DFOPEN_DIR_NOT_ALLOWED = 0x87,
+ PLDM_FILE_CC_MAX_NUM_FDS_EXCEEDED = 0x88,
+ PLDM_FILE_CC_FILE_OPEN = 0x89,
+ PLDM_FILE_CC_UNABLE_TO_OPEN_FILE = 0x8A,
+};
+
+/** @brief PLDM File Transfer Command */
+enum pldm_file_cmd {
+ PLDM_FILE_CMD_DF_OPEN = 0x01,
+ PLDM_FILE_CMD_DF_CLOSE = 0x02,
+ PLDM_FILE_CMD_DF_HEARTBEAT = 0x03,
+ PLDM_FILE_CMD_DF_PROPERTIES = 0x10,
+ PLDM_FILE_CMD_DF_GET_FILE_ATTRIBUTE = 0x11,
+ PLDM_FILE_CMD_DF_SET_FILE_ATTRIBUTE = 0x12,
+ PLDM_FILE_CMD_DF_READ = 0x20,
+ PLDM_FILE_CMD_DF_FIFO_SEND = 0x21,
+};
+
+/** @struct pldm_file_df_open_req
+ *
+ * Structure representing PLDM File DfOpen request.
+ */
+struct pldm_file_df_open_req {
+ uint16_t file_identifier;
+ bitfield16_t file_attribute;
+};
+
+/** @struct pldm_file_df_open_resp
+ *
+ * Structure representing PLDM File DfOpen response.
+ */
+struct pldm_file_df_open_resp {
+ uint8_t completion_code;
+ uint16_t file_descriptor;
+};
+
+/** @brief Create a PLDM request message for DFOpen
+ *
+ * @param[in] instance_id - Message's instance id
+ * @param[in] req - The pointer to the request message to be encoded
+ * @param[in,out] msg - Message will be written to this
+ * @param[in] payload_length - Length of the request message payload
+ * @return 0 on success
+ * -EINVAL if the input parameters' memory are not allocated,
+ * or message type or instance in request header is invalid
+ * -ENOMSG if the PLDM type in the request header is invalid
+ * -EOVERFLOW if the input message length is invalid
+ * @note Caller is responsible for memory alloc and dealloc of param
+ * 'msg.payload'
+ */
+int encode_pldm_file_df_open_req(uint8_t instance_id,
+ const struct pldm_file_df_open_req *req,
+ struct pldm_msg *msg, size_t payload_length);
+
+/** @brief Decode DFOpen response data
+ *
+ * @param[in] msg - Response message
+ * @param[in] payload_length - Length of response message payload
+ * @param[out] resp - pointer to the decoded response message
+ * @return 0 on success
+ * -EINVAL if the input parameters' memory are not allocated
+ * -EOVERFLOW if the input message buffer is too short for the output
+ * response struct
+ * -EBADMSG if the input message buffer is too large for the output
+ * response struct.
+ */
+int decode_pldm_file_df_open_resp(const struct pldm_msg *msg,
+ size_t payload_length,
+ struct pldm_file_df_open_resp *resp);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/include/libpldm/meson.build b/include/libpldm/meson.build
index 3a8c708..23785ff 100644
--- a/include/libpldm/meson.build
+++ b/include/libpldm/meson.build
@@ -6,6 +6,7 @@
'compiler.h',
'control.h',
'entity.h',
+ 'file.h',
'firmware_fd.h',
'firmware_update.h',
'fru.h',
diff --git a/src/dsp/file.c b/src/dsp/file.c
new file mode 100644
index 0000000..d9c2a21
--- /dev/null
+++ b/src/dsp/file.c
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
+#include "dsp/base.h"
+#include "msgbuf.h"
+
+#include <libpldm/base.h>
+#include <libpldm/file.h>
+#include <libpldm/utils.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+
+LIBPLDM_ABI_TESTING
+int encode_pldm_file_df_open_req(uint8_t instance_id,
+ const struct pldm_file_df_open_req *req,
+ struct pldm_msg *msg, size_t payload_length)
+{
+ PLDM_MSGBUF_DEFINE_P(buf);
+ int rc;
+
+ if (req == NULL || msg == NULL) {
+ return -EINVAL;
+ }
+
+ struct pldm_header_info header = { 0 };
+ header.instance = instance_id;
+ header.msg_type = PLDM_REQUEST;
+ header.pldm_type = PLDM_FILE;
+ header.command = PLDM_FILE_CMD_DF_OPEN;
+
+ rc = pack_pldm_header_errno(&header, &(msg->hdr));
+ if (rc) {
+ return rc;
+ }
+
+ rc = pldm_msgbuf_init_errno(buf, PLDM_DF_OPEN_REQ_BYTES, msg->payload,
+ payload_length);
+ if (rc) {
+ return rc;
+ }
+
+ pldm_msgbuf_insert(buf, req->file_identifier);
+ pldm_msgbuf_insert(buf, req->file_attribute.value);
+
+ return pldm_msgbuf_complete(buf);
+}
+
+LIBPLDM_ABI_TESTING
+int decode_pldm_file_df_open_resp(const struct pldm_msg *msg,
+ size_t payload_length,
+ struct pldm_file_df_open_resp *resp)
+{
+ PLDM_MSGBUF_DEFINE_P(buf);
+ int rc;
+
+ if (!msg || !resp) {
+ return -EINVAL;
+ }
+
+ rc = pldm_msg_has_error(msg, payload_length);
+ if (rc) {
+ resp->completion_code = rc;
+ return 0;
+ }
+
+ rc = pldm_msgbuf_init_errno(buf, PLDM_DF_OPEN_RESP_BYTES, msg->payload,
+ payload_length);
+ if (rc) {
+ return rc;
+ }
+
+ pldm_msgbuf_extract(buf, resp->completion_code);
+ pldm_msgbuf_extract(buf, resp->file_descriptor);
+
+ return pldm_msgbuf_complete_consumed(buf);
+}
diff --git a/src/dsp/meson.build b/src/dsp/meson.build
index b81e533..4a5bc41 100644
--- a/src/dsp/meson.build
+++ b/src/dsp/meson.build
@@ -2,6 +2,7 @@
'base.c',
'bios.c',
'bios_table.c',
+ 'file.c',
'firmware_update.c',
'fru.c',
'pdr.c',
diff --git a/tests/dsp/file.cpp b/tests/dsp/file.cpp
new file mode 100644
index 0000000..f738377
--- /dev/null
+++ b/tests/dsp/file.cpp
@@ -0,0 +1,166 @@
+#include <libpldm/file.h>
+#include <libpldm/pldm_types.h>
+
+#include <array>
+#include <cstdint>
+#include <cstring>
+#include <vector>
+
+#include "msgbuf.h"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#ifdef LIBPLDM_API_TESTING
+TEST(EncodeDfOpenReq, GoodTest)
+{
+ uint8_t instance_id = 0;
+ uint16_t file_identifier = 0x0100;
+ bitfield16_t file_attribute;
+ file_attribute.value = 0x0400;
+ std::array<uint8_t, PLDM_DF_OPEN_REQ_BYTES> requestMsg = {0x00, 0x01, 0x00,
+ 0x04};
+
+ const struct pldm_file_df_open_req req_data = {file_identifier,
+ file_attribute};
+
+ PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_OPEN_REQ_BYTES);
+ auto rc = encode_pldm_file_df_open_req(instance_id, &req_data, requestPtr,
+ PLDM_DF_OPEN_REQ_BYTES);
+
+ ASSERT_EQ(rc, 0);
+ EXPECT_EQ(
+ 0, memcmp(requestPtr->payload, requestMsg.data(), sizeof(requestMsg)));
+}
+#endif
+
+#ifdef LIBPLDM_API_TESTING
+TEST(EncodeDfOpenReq, BadTestUnAllocatedPtrParams)
+{
+ uint8_t instance_id = 0;
+ uint16_t file_identifier = 0x0100;
+ bitfield16_t file_attribute;
+ file_attribute.value = 0x0400;
+ int rc;
+
+ const struct pldm_file_df_open_req req_data = {file_identifier,
+ file_attribute};
+
+ PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_OPEN_REQ_BYTES);
+ rc = encode_pldm_file_df_open_req(instance_id, &req_data, nullptr,
+ PLDM_DF_OPEN_REQ_BYTES);
+ EXPECT_EQ(rc, -EINVAL);
+
+ rc = encode_pldm_file_df_open_req(instance_id, nullptr, requestPtr,
+ PLDM_DF_OPEN_REQ_BYTES);
+ EXPECT_EQ(rc, -EINVAL);
+}
+#endif
+
+#ifdef LIBPLDM_API_TESTING
+TEST(EncodeDfOpenReq, BadTestInvalidExpectedOutputMsgLength)
+{
+ uint8_t instance_id = 0;
+ uint16_t file_identifier = 0x0100;
+ bitfield16_t file_attribute;
+ file_attribute.value = 0x0400;
+ int rc;
+
+ const struct pldm_file_df_open_req req_data = {file_identifier,
+ file_attribute};
+
+ PLDM_MSG_DEFINE_P(requestPtr, PLDM_DF_OPEN_REQ_BYTES);
+ rc = encode_pldm_file_df_open_req(instance_id, &req_data, requestPtr, 1);
+ EXPECT_EQ(rc, -EOVERFLOW);
+}
+#endif
+
+#ifdef LIBPLDM_API_TESTING
+TEST(DecodeDfOpenResp, GoodTest)
+{
+ uint8_t completion_code = PLDM_SUCCESS;
+ uint16_t file_descriptor = 20;
+
+ struct pldm_file_df_open_resp resp_data = {};
+
+ PLDM_MSGBUF_DEFINE_P(buf);
+ int rc;
+
+ static constexpr const size_t payload_length = PLDM_DF_OPEN_RESP_BYTES;
+
+ PLDM_MSG_DEFINE_P(responseMsg, payload_length);
+
+ rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length);
+ ASSERT_EQ(rc, 0);
+
+ pldm_msgbuf_insert_uint8(buf, completion_code);
+ pldm_msgbuf_insert_uint16(buf, file_descriptor);
+
+ ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0);
+
+ rc = decode_pldm_file_df_open_resp(responseMsg, payload_length, &resp_data);
+
+ ASSERT_EQ(rc, 0);
+ EXPECT_EQ(resp_data.completion_code, completion_code);
+ EXPECT_EQ(resp_data.file_descriptor, file_descriptor);
+}
+#endif
+
+#ifdef LIBPLDM_API_TESTING
+TEST(DecodeDfOpenResp, BadTestUnAllocatedPtrParams)
+{
+ uint8_t completion_code = PLDM_SUCCESS;
+ uint16_t file_descriptor = 20;
+
+ struct pldm_file_df_open_resp resp_data = {};
+
+ PLDM_MSGBUF_DEFINE_P(buf);
+ int rc;
+
+ static constexpr const size_t payload_length = PLDM_DF_OPEN_RESP_BYTES;
+
+ PLDM_MSG_DEFINE_P(responseMsg, payload_length);
+
+ rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length);
+ ASSERT_EQ(rc, 0);
+
+ pldm_msgbuf_insert_uint8(buf, completion_code);
+ pldm_msgbuf_insert_uint16(buf, file_descriptor);
+
+ ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0);
+
+ rc = decode_pldm_file_df_open_resp(nullptr, payload_length, &resp_data);
+ EXPECT_EQ(rc, -EINVAL);
+
+ rc = decode_pldm_file_df_open_resp(responseMsg, payload_length, nullptr);
+ EXPECT_EQ(rc, -EINVAL);
+}
+#endif
+
+#ifdef LIBPLDM_API_TESTING
+TEST(DecodeDfOpenResp, BadTestInvalidExpectedInputMsgLength)
+{
+ uint8_t completion_code = PLDM_SUCCESS;
+ uint16_t file_descriptor = 20;
+
+ struct pldm_file_df_open_resp resp_data = {};
+
+ PLDM_MSGBUF_DEFINE_P(buf);
+ int rc;
+
+ static constexpr const size_t payload_length = PLDM_DF_OPEN_RESP_BYTES;
+
+ PLDM_MSG_DEFINE_P(responseMsg, payload_length);
+
+ rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length);
+ ASSERT_EQ(rc, 0);
+
+ pldm_msgbuf_insert_uint8(buf, completion_code);
+ pldm_msgbuf_insert_uint16(buf, file_descriptor);
+
+ ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0);
+
+ rc = decode_pldm_file_df_open_resp(responseMsg, 0, &resp_data);
+ EXPECT_EQ(rc, -EOVERFLOW);
+}
+#endif
diff --git a/tests/dsp/meson.build b/tests/dsp/meson.build
index dd2b5b0..64acb94 100644
--- a/tests/dsp/meson.build
+++ b/tests/dsp/meson.build
@@ -2,6 +2,7 @@
'dsp/base',
'dsp/bios_table',
'dsp/bios',
+ 'dsp/file',
'dsp/firmware_update',
'dsp/fru',
'dsp/pdr',