Update libpldm api encode_*_req functions to have payload_length param
There were a few of the APIs in libpldm's fru and platform
that Hostboot was using downstream that did not have the
payload_length param which the Hostboot templates expect. All
encode_*_req functions should end with a payload_length parameter
and this commit moves us towards that goal.
Tested: I ran run-unit-test-docker.sh from the openbmc-build-scripts
repo with the script pointed at my pldm directory.
Change-Id: Ief825111970db2faffaf00e431bc0f43f7a11b02
Signed-off-by: Christian Geddes <crgeddes@us.ibm.com>
diff --git a/host_pdr_handler.cpp b/host_pdr_handler.cpp
index 17b0b88..dabb401 100644
--- a/host_pdr_handler.cpp
+++ b/host_pdr_handler.cpp
@@ -299,7 +299,8 @@
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
rc = encode_platform_event_message_req(
instanceId, 1, 0, PLDM_PDR_REPOSITORY_CHG_EVENT, eventDataVec.data(),
- actualSize, request);
+ actualSize, request,
+ actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
if (rc != PLDM_SUCCESS)
{
requester.markFree(mctp_eid, instanceId);
diff --git a/libpldm/fru.c b/libpldm/fru.c
index 13d14e8..242fc28 100644
--- a/libpldm/fru.c
+++ b/libpldm/fru.c
@@ -4,12 +4,17 @@
#include "fru.h"
int encode_get_fru_record_table_metadata_req(uint8_t instance_id,
- struct pldm_msg *msg)
+ struct pldm_msg *msg,
+ size_t payload_length)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
+ if (payload_length != PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
struct pldm_header_info header = {0};
header.instance = instance_id;
header.msg_type = PLDM_REQUEST;
@@ -19,6 +24,7 @@
if (PLDM_SUCCESS != rc) {
return rc;
}
+
return PLDM_SUCCESS;
}
diff --git a/libpldm/fru.h b/libpldm/fru.h
index 6addf27..4a8205e 100644
--- a/libpldm/fru.h
+++ b/libpldm/fru.h
@@ -11,10 +11,13 @@
#include "base.h"
+#define PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES 0
#define PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES 19
#define PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES 5
#define PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES 6
+#define FRU_TABLE_CHECKSUM_SIZE 4
+
/** @brief PLDM FRU commands
*/
enum pldm_fru_commands {
@@ -131,12 +134,14 @@
*
* @param[in] instance_id - Message's instance id
* @param[in,out] msg - Message will be written to this
+ * @param[in] payload_length - Length of the request message payload
* @return pldm_completion_codes
* @note Caller is responsible for memory alloc and dealloc of param
* 'msg.payload'
*/
int encode_get_fru_record_table_metadata_req(uint8_t instance_id,
- struct pldm_msg *msg);
+ struct pldm_msg *msg,
+ size_t payload_length);
/** @brief Decode GetFruRecordTable response data
*
diff --git a/libpldm/platform.c b/libpldm/platform.c
index 9e16ed2..9c696fc 100644
--- a/libpldm/platform.c
+++ b/libpldm/platform.c
@@ -620,12 +620,10 @@
return PLDM_SUCCESS;
}
-int encode_platform_event_message_req(uint8_t instance_id,
- uint8_t format_version, uint8_t tid,
- uint8_t event_class,
- const uint8_t *event_data,
- size_t event_data_length,
- struct pldm_msg *msg)
+int encode_platform_event_message_req(
+ uint8_t instance_id, uint8_t format_version, uint8_t tid,
+ uint8_t event_class, const uint8_t *event_data, size_t event_data_length,
+ struct pldm_msg *msg, size_t payload_length)
{
struct pldm_header_info header = {0};
@@ -648,6 +646,11 @@
return PLDM_ERROR_INVALID_DATA;
}
+ if (payload_length !=
+ PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + event_data_length) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
if (event_class > PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT &&
!(event_class >= 0xF0 && event_class <= 0xFE)) {
return PLDM_ERROR_INVALID_DATA;
diff --git a/libpldm/platform.h b/libpldm/platform.h
index cab1804..cafd47c 100644
--- a/libpldm/platform.h
+++ b/libpldm/platform.h
@@ -1056,12 +1056,10 @@
* @note Caller is responsible for memory alloc and dealloc of param
* 'msg.payload'
*/
-int encode_platform_event_message_req(uint8_t instance_id,
- uint8_t format_version, uint8_t tid,
- uint8_t event_class,
- const uint8_t *event_data,
- size_t event_data_length,
- struct pldm_msg *msg);
+int encode_platform_event_message_req(
+ uint8_t instance_id, uint8_t format_version, uint8_t tid,
+ uint8_t event_class, const uint8_t *event_data, size_t event_data_length,
+ struct pldm_msg *msg, size_t payload_length);
/** @brief Decode PlatformEventMessage response data
* @param[in] msg - Request message
diff --git a/libpldm/tests/libpldm_fru_test.cpp b/libpldm/tests/libpldm_fru_test.cpp
index 97177c0..d79df4b 100644
--- a/libpldm/tests/libpldm_fru_test.cpp
+++ b/libpldm/tests/libpldm_fru_test.cpp
@@ -11,7 +11,8 @@
{
std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
- auto rc = encode_get_fru_record_table_metadata_req(0, requestPtr);
+ auto rc = encode_get_fru_record_table_metadata_req(
+ 0, requestPtr, PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
ASSERT_EQ(rc, PLDM_SUCCESS);
ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
ASSERT_EQ(requestPtr->hdr.instance_id, 0u);
@@ -21,8 +22,12 @@
TEST(GetFruRecordTableMetadata, testBadEncodeRequest)
{
- auto rc = encode_get_fru_record_table_metadata_req(0, NULL);
+ auto rc = encode_get_fru_record_table_metadata_req(0, NULL, 0);
ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+ std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
+ auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ rc = encode_get_fru_record_table_metadata_req(0, requestPtr, 1);
+ ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
}
TEST(GetFruRecordTableMetadata, testGoodDecodeResponse)
diff --git a/libpldm/tests/libpldm_platform_test.cpp b/libpldm/tests/libpldm_platform_test.cpp
index 54daa33..25aee71 100644
--- a/libpldm/tests/libpldm_platform_test.cpp
+++ b/libpldm/tests/libpldm_platform_test.cpp
@@ -870,7 +870,8 @@
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
auto rc = encode_platform_event_message_req(
0, formatVersion, Tid, eventClass,
- reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), request);
+ reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), request,
+ sizeof(eventData) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
struct pldm_platform_event_message_req* req =
reinterpret_cast<struct pldm_platform_event_message_req*>(
@@ -888,24 +889,32 @@
uint8_t Tid = 0x03;
uint8_t eventClass = 0x00;
uint8_t eventData = 34;
+ size_t sz_eventData = sizeof(eventData);
+ size_t payloadLen =
+ sz_eventData + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
uint8_t formatVersion = 0x01;
std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
sizeof(eventData)>
requestMsg{};
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
auto rc = encode_platform_event_message_req(
0, formatVersion, Tid, eventClass,
- reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), nullptr);
-
+ reinterpret_cast<uint8_t*>(&eventData), sz_eventData, nullptr,
+ payloadLen);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
rc = encode_platform_event_message_req(
0, 0, Tid, eventClass, reinterpret_cast<uint8_t*>(&eventData),
- sizeof(eventData), request);
+ sz_eventData, request, payloadLen);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass,
- nullptr, 0, request);
+ nullptr, 0, request, payloadLen);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+ rc = encode_platform_event_message_req(
+ 0, formatVersion, Tid, eventClass,
+ reinterpret_cast<uint8_t*>(&eventData), sz_eventData, request, 0);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
}
TEST(PlatformEventMessage, testGoodDecodeResponse)
diff --git a/tool/pldm_fru_cmd.cpp b/tool/pldm_fru_cmd.cpp
index a5fe5a3..2075a48 100644
--- a/tool/pldm_fru_cmd.cpp
+++ b/tool/pldm_fru_cmd.cpp
@@ -35,7 +35,8 @@
std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
- auto rc = encode_get_fru_record_table_metadata_req(instanceId, request);
+ auto rc = encode_get_fru_record_table_metadata_req(
+ instanceId, request, PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
return {rc, requestMsg};
}