libpldm: Implement requester flow for SetBIOSTable

For the requester, need to encode request data and send responder,
waiting for recive responder data to decode, so need to implement
encode/decode(Requester flow only) for SetBIOSTable.

Tested: the unit tests functions have been added to check these APIs.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I410c64b8bbc8d37c9b5baa5d95bbff94f25bd7be
diff --git a/libpldm/bios.c b/libpldm/bios.c
index 151ba29..bae2d37 100644
--- a/libpldm/bios.c
+++ b/libpldm/bios.c
@@ -544,3 +544,65 @@
 
 	return PLDM_SUCCESS;
 }
+
+int encode_set_bios_table_req(uint8_t instance_id, uint32_t transfer_handle,
+			      uint8_t transfer_flag, uint8_t table_type,
+			      const uint8_t *table_data, size_t table_length,
+			      struct pldm_msg *msg, size_t payload_length)
+{
+	int rc = PLDM_SUCCESS;
+
+	if (msg == NULL || table_data == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	if (PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES + table_length !=
+	    payload_length) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_header_info header = {0};
+	header.instance = instance_id;
+	header.msg_type = PLDM_REQUEST;
+	header.pldm_type = PLDM_BIOS;
+	header.command = PLDM_SET_BIOS_TABLE;
+
+	if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_set_bios_table_req *request =
+	    (struct pldm_set_bios_table_req *)msg->payload;
+	request->transfer_handle = htole32(transfer_handle);
+	request->transfer_flag = transfer_flag;
+	request->table_type = table_type;
+	memcpy(request->table_data, table_data, table_length);
+
+	return PLDM_SUCCESS;
+}
+
+int decode_set_bios_table_resp(const struct pldm_msg *msg,
+			       size_t payload_length, uint8_t *completion_code,
+			       uint32_t *next_transfer_handle)
+{
+	if (msg == NULL || completion_code == NULL ||
+	    next_transfer_handle == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	*completion_code = msg->payload[0];
+	if (PLDM_SUCCESS != *completion_code) {
+		return PLDM_SUCCESS;
+	}
+
+	if (payload_length != PLDM_SET_BIOS_TABLE_RESP_BYTES) {
+		return PLDM_ERROR_INVALID_LENGTH;
+	}
+
+	struct pldm_set_bios_table_resp *response =
+	    (struct pldm_set_bios_table_resp *)msg->payload;
+
+	*next_transfer_handle = le32toh(response->next_transfer_handle);
+
+	return PLDM_SUCCESS;
+}
diff --git a/libpldm/bios.h b/libpldm/bios.h
index 4430053..89182e3 100644
--- a/libpldm/bios.h
+++ b/libpldm/bios.h
@@ -17,6 +17,8 @@
 
 #define PLDM_GET_BIOS_TABLE_REQ_BYTES 6
 #define PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES 6
+#define PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES 6
+#define PLDM_SET_BIOS_TABLE_RESP_BYTES 5
 #define PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES 5
 #define PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES 5
 #define PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_REQ_BYTES 7
@@ -30,6 +32,7 @@
 };
 enum pldm_bios_commands {
 	PLDM_GET_BIOS_TABLE = 0x01,
+	PLDM_SET_BIOS_TABLE = 0x02,
 	PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE = 0x07,
 	PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE = 0x08,
 	PLDM_GET_DATE_TIME = 0x0c,
@@ -176,6 +179,28 @@
 	uint32_t next_transfer_handle;
 } __attribute__((packed));
 
+/** @struct pldm_set_bios_table_req
+ *
+ *  structure representing SetBIOSTable request packet
+ *
+ */
+struct pldm_set_bios_table_req {
+	uint32_t transfer_handle;
+	uint8_t transfer_flag;
+	uint8_t table_type;
+	uint8_t table_data[1];
+} __attribute__((packed));
+
+/** @struct pldm_set_bios_table_resp
+ *
+ *  structure representing SetBIOSTable response packet
+ *
+ */
+struct pldm_set_bios_table_resp {
+	uint8_t completion_code;
+	uint32_t next_transfer_handle;
+} __attribute__((packed));
+
 /* Requester */
 
 /* GetDateTime */
@@ -255,6 +280,49 @@
     const struct pldm_msg *msg, size_t payload_length, uint8_t *completion_code,
     uint32_t *next_transfer_handle);
 
+/* SetBIOSTable */
+
+/** @brief Create a PLDM request message for SetBIOSTable
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] transfer_handle - Handle to identify a BIOS table transfer
+ *  @param[in] transfer_flag - Flag to indicate what part of the transfer
+ * 			   this request represents
+ *  @param[in] table_type - Indicates what table is being transferred
+ *             {BIOSStringTable=0x0, BIOSAttributeTable=0x1,
+ *              BIOSAttributeValueTable=0x2}
+ *  @param[in] table_data - Contains data specific to the table type
+ *  @param[in] table_length - Length of table data
+ *  @param[out] msg - Message will be written to this
+ *  @param[in] payload_length - Length of message payload
+ *  @return pldm_completion_codes
+ *  @note  Caller is responsible for memory alloc and dealloc of params
+ *         'msg.payload'
+ */
+int encode_set_bios_table_req(uint8_t instance_id, uint32_t transfer_handle,
+			      uint8_t transfer_flag, uint8_t table_type,
+			      const uint8_t *table_data, size_t table_length,
+			      struct pldm_msg *msg, size_t payload_length);
+
+/** @brief Decode a SetBIOSTable response message
+ *
+ *  Note:
+ *  * If the return value is not PLDM_SUCCESS, it represents a
+ * transport layer error.
+ *  * If the completion_code value is not PLDM_SUCCESS, it represents a
+ * protocol layer error and all the out-parameters are invalid.
+ *
+ *  @param[in] msg - Response message
+ *  @param[in] payload_length - Length of response message payload
+ *  @param[out] completion_code - Pointer to response msg's PLDM completion code
+ *  @param[out] next_transfer_handle - Pointer to a handle that identify the
+ *              next portion of the transfer
+ *  @return pldm_completion_codes
+ */
+int decode_set_bios_table_resp(const struct pldm_msg *msg,
+			       size_t payload_length, uint8_t *completion_code,
+			       uint32_t *next_transfer_handle);
+
 /* Responder */
 
 /* GetDateTime */
diff --git a/libpldm/tests/libpldm_bios_test.cpp b/libpldm/tests/libpldm_bios_test.cpp
index d8f8e73..bfaefc8 100644
--- a/libpldm/tests/libpldm_bios_test.cpp
+++ b/libpldm/tests/libpldm_bios_test.cpp
@@ -897,4 +897,104 @@
     EXPECT_EQ(sizeof(attributeData), retAttributeData.length);
     EXPECT_EQ(
         0, memcmp(retAttributeData.ptr, &attributeData, sizeof(attributeData)));
-}
\ No newline at end of file
+}
+
+TEST(SetBIOSTable, testGoodEncodeRequest)
+{
+    uint8_t instanceId = 10;
+    uint32_t transferHandle = 32;
+    uint8_t transferFlag = PLDM_START_AND_END;
+    uint8_t tableType = PLDM_BIOS_STRING_TABLE;
+    uint32_t tableData = 44;
+    std::array<uint8_t,
+               hdrSize + PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES + sizeof(tableData)>
+        requestMsg{};
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    auto rc = encode_set_bios_table_req(
+        instanceId, transferHandle, transferFlag, tableType,
+        reinterpret_cast<uint8_t*>(&tableData), sizeof(tableData), request,
+        requestMsg.size() - hdrSize);
+
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+
+    struct pldm_set_bios_table_req* req =
+        reinterpret_cast<struct pldm_set_bios_table_req*>(request->payload);
+
+    EXPECT_EQ(htole32(transferHandle), req->transfer_handle);
+    EXPECT_EQ(transferFlag, req->transfer_flag);
+    EXPECT_EQ(tableType, req->table_type);
+    EXPECT_EQ(0, memcmp(&tableData, req->table_data, sizeof(tableData)));
+}
+
+TEST(SetBIOSTable, testBadEncodeRequest)
+{
+    uint8_t instanceId = 10;
+    uint32_t transferHandle = 32;
+    uint8_t transferFlag = PLDM_START_AND_END;
+    uint8_t tableType = PLDM_BIOS_STRING_TABLE;
+    uint32_t tableData = 44;
+    std::array<uint8_t,
+               hdrSize + PLDM_SET_BIOS_TABLE_MIN_REQ_BYTES + sizeof(tableData)>
+        requestMsg{};
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+    auto rc = encode_set_bios_table_req(
+        instanceId, transferHandle, transferFlag, tableType, NULL,
+        sizeof(tableData), request, requestMsg.size() - hdrSize);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    rc = encode_set_bios_table_req(
+        instanceId, transferHandle, transferFlag, tableType,
+        reinterpret_cast<uint8_t*>(&tableData), sizeof(tableData), request,
+        requestMsg.size() - hdrSize + 1);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(SetBIOSTable, testGoodDecodeResponse)
+{
+    uint32_t nextTransferHandle = 32;
+    uint8_t completionCode = PLDM_SUCCESS;
+    std::array<uint8_t, hdrSize + PLDM_SET_BIOS_TABLE_RESP_BYTES> responseMsg{};
+    struct pldm_msg* response =
+        reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+    struct pldm_set_bios_table_resp* resp =
+        reinterpret_cast<struct pldm_set_bios_table_resp*>(response->payload);
+
+    resp->completion_code = completionCode;
+    resp->next_transfer_handle = htole32(nextTransferHandle);
+
+    uint8_t retCompletionCode;
+    uint32_t retNextTransferHandle;
+    auto rc =
+        decode_set_bios_table_resp(response, responseMsg.size() - hdrSize,
+                                   &retCompletionCode, &retNextTransferHandle);
+
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    EXPECT_EQ(completionCode, retCompletionCode);
+    EXPECT_EQ(nextTransferHandle, retNextTransferHandle);
+}
+
+TEST(SetBIOSTable, testBadDecodeResponse)
+{
+    uint32_t nextTransferHandle = 32;
+    uint8_t completionCode = PLDM_SUCCESS;
+    std::array<uint8_t, hdrSize + PLDM_SET_BIOS_TABLE_RESP_BYTES> responseMsg{};
+    struct pldm_msg* response =
+        reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+    struct pldm_set_bios_table_resp* resp =
+        reinterpret_cast<struct pldm_set_bios_table_resp*>(response->payload);
+
+    resp->completion_code = completionCode;
+    resp->next_transfer_handle = htole32(nextTransferHandle);
+
+    uint8_t retCompletionCode;
+    uint32_t retNextTransferHandle;
+
+    auto rc = decode_set_bios_table_resp(NULL, responseMsg.size() - hdrSize,
+                                         NULL, NULL);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+    rc = decode_set_bios_table_resp(response, responseMsg.size() - hdrSize + 1,
+                                    &retCompletionCode, &retNextTransferHandle);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}