Add encode_get_bios_table() function in PLDM BIOS

Signed-off-by: Sridevi Ramesh <sridevra@in.ibm.com>
Change-Id: Id0acee69d3ba13e6c39a569f37c005f1e94a5603
diff --git a/libpldm/bios.c b/libpldm/bios.c
index 017ae4a..abf7df4 100644
--- a/libpldm/bios.c
+++ b/libpldm/bios.c
@@ -1,8 +1,7 @@
+#include "bios.h"
 #include <endian.h>
 #include <string.h>
 
-#include "bios.h"
-
 int encode_get_date_time_req(uint8_t instance_id, struct pldm_msg *msg)
 {
 	struct pldm_header_info header = {0};
@@ -127,6 +126,31 @@
 	return PLDM_SUCCESS;
 }
 
+int encode_get_bios_table_req(uint8_t instance_id, uint32_t transfer_handle,
+			      uint8_t transfer_op_flag, uint8_t table_type,
+			      struct pldm_msg *msg)
+{
+	struct pldm_header_info header = {0};
+
+	if (msg == NULL) {
+		return PLDM_ERROR_INVALID_DATA;
+	}
+
+	header.msg_type = PLDM_REQUEST;
+	header.instance = instance_id;
+	header.pldm_type = PLDM_BIOS;
+	header.command = PLDM_GET_BIOS_TABLE;
+	pack_pldm_header(&header, &(msg->hdr));
+
+	struct pldm_get_bios_table_req *request =
+	    (struct pldm_get_bios_table_req *)msg->payload;
+
+	request->transfer_handle = htole32(transfer_handle);
+	request->transfer_op_flag = transfer_op_flag;
+	request->table_type = table_type;
+	return PLDM_SUCCESS;
+}
+
 int decode_get_bios_table_req(const struct pldm_msg *msg, size_t payload_length,
 			      uint32_t *transfer_handle,
 			      uint8_t *transfer_op_flag, uint8_t *table_type)
diff --git a/libpldm/bios.h b/libpldm/bios.h
index 1856b8d..270413c 100644
--- a/libpldm/bios.h
+++ b/libpldm/bios.h
@@ -262,6 +262,20 @@
 			       uint8_t transfer_flag, uint8_t *table_data,
 			       size_t payload_length, struct pldm_msg *msg);
 
+/** @brief Encode  GetBIOSTable request packet
+ *
+ *  @param[in] instance_id - Message's instance id
+ *  @param[in] transfer_handle - Handle to identify a BIOS table transfer
+ *  @param[in] transfer_op_flag - Flag to indicate the start of a multipart
+ *                                 transfer
+ *  @param[in] table_type - BIOS table type
+ *  @param[out] msg - Message will be written to this
+ *  @return pldm_completion_codes
+ */
+int encode_get_bios_table_req(uint8_t instance_id, uint32_t transfer_handle,
+			      uint8_t transfer_op_flag, uint8_t table_type,
+			      struct pldm_msg *msg);
+
 /** @brief Decode GetBIOSTable request packet
  *
  *  @param[in] msg - Request message
diff --git a/test/libpldm_bios_test.cpp b/test/libpldm_bios_test.cpp
index 46a75ed..d841da6 100644
--- a/test/libpldm_bios_test.cpp
+++ b/test/libpldm_bios_test.cpp
@@ -152,6 +152,39 @@
     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
 }
 
+TEST(GetBIOSTable, testGoodEncodeRequest)
+{
+    std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES>
+        requestMsg{};
+    uint32_t transferHandle = 0x0;
+    uint8_t transferOpFlag = 0x01;
+    uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
+
+    auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    auto rc = encode_get_bios_table_req(0, transferHandle, transferOpFlag,
+                                        tableType, request);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+
+    struct pldm_get_bios_table_req* req =
+        reinterpret_cast<struct pldm_get_bios_table_req*>(request->payload);
+    ASSERT_EQ(transferHandle, le32toh(req->transfer_handle));
+    ASSERT_EQ(transferOpFlag, req->transfer_op_flag);
+    ASSERT_EQ(tableType, req->table_type);
+}
+
+TEST(GetBIOSTable, testBadEncodeRequest)
+{
+    uint32_t transferHandle = 0x0;
+    uint8_t transferOpFlag = 0x01;
+    uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
+
+    auto rc = encode_get_bios_table_req(0, transferHandle, transferOpFlag,
+                                        tableType, nullptr);
+
+    ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
 TEST(GetBIOSTable, testGoodDecodeRequest)
 {
     const auto hdr_size = sizeof(pldm_msg_hdr);
@@ -180,7 +213,35 @@
     ASSERT_EQ(transferOpFlag, retTransferOpFlag);
     ASSERT_EQ(tableType, retTableType);
 }
+TEST(GetBIOSTable, testBadDecodeRequest)
+{
+    const auto hdr_size = sizeof(pldm_msg_hdr);
+    std::array<uint8_t, hdr_size + PLDM_GET_BIOS_TABLE_REQ_BYTES> requestMsg{};
+    uint32_t transferHandle = 31;
+    uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
+    uint8_t tableType = PLDM_BIOS_ATTR_TABLE;
+    uint32_t retTransferHandle = 0;
+    uint8_t retTransferOpFlag = 0;
+    uint8_t retTableType = 0;
 
+    auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
+    struct pldm_get_bios_table_req* request =
+        reinterpret_cast<struct pldm_get_bios_table_req*>(req->payload);
+
+    request->transfer_handle = transferHandle;
+    request->transfer_op_flag = transferOpFlag;
+    request->table_type = tableType;
+
+    auto rc = decode_get_bios_table_req(req, requestMsg.size() - hdr_size,
+                                        &retTransferHandle, &retTransferOpFlag,
+                                        &retTableType);
+
+    ASSERT_EQ(rc, PLDM_SUCCESS);
+    ASSERT_EQ(transferHandle, retTransferHandle);
+    ASSERT_EQ(transferOpFlag, retTransferOpFlag);
+    ASSERT_EQ(tableType, retTableType);
+}
+/*
 TEST(GetBIOSTable, testBadDecodeRequest)
 {
     const auto hdr_size = sizeof(pldm_msg_hdr);
@@ -205,7 +266,7 @@
                                         &retTableType);
 
     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
-}
+}*/
 
 TEST(GetBIOSAttributeCurrentValueByHandle, testGoodDecodeRequest)
 {