bios: Use crc implemented in libpldm
Use the crc function implemented in libpldm,
and fixed the endianness of checksum.
Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: Id799da93fdf55ce9fad31dc7c1cfca7f832cf6f1
diff --git a/libpldm/bios_table.c b/libpldm/bios_table.c
index 2512f5e..d59c48e 100644
--- a/libpldm/bios_table.c
+++ b/libpldm/bios_table.c
@@ -7,6 +7,7 @@
#include "bios.h"
#include "bios_table.h"
+#include "utils.h"
#define POINTER_CHECK(pointer) \
do { \
@@ -636,6 +637,50 @@
return PLDM_SUCCESS;
}
+static size_t pad_size_get(size_t size_without_pad)
+{
+ return ((size_without_pad % 4) ? (4 - size_without_pad % 4) : 0);
+}
+
+static uint8_t *pad_append(uint8_t *table_end, size_t pad_size)
+{
+ while (pad_size--)
+ *table_end++ = 0;
+
+ return table_end;
+}
+
+static uint8_t *checksum_append(uint8_t *table_end, uint32_t checksum)
+{
+ checksum = htole32(checksum);
+ memcpy(table_end, &checksum, sizeof(checksum));
+
+ return table_end + sizeof(checksum);
+}
+
+size_t pldm_bios_table_pad_checksum_size(size_t size_without_pad)
+{
+ size_t size = pad_size_get(size_without_pad) +
+ sizeof(uint32_t) /*sizeof(checksum)*/;
+ return size;
+}
+
+void pldm_bios_table_append_pad_checksum(void *table, size_t size,
+ size_t size_without_pad)
+{
+
+ size_t pad_checksum_size =
+ pldm_bios_table_pad_checksum_size(size_without_pad);
+ assert(size >= (size_without_pad + pad_checksum_size));
+
+ uint8_t *table_end = (uint8_t *)table + size_without_pad;
+ size_t pad_size = pad_size_get(size_without_pad);
+ table_end = pad_append(table_end, pad_size);
+
+ uint32_t checksum = crc32(table, size_without_pad + pad_size);
+ checksum_append(table_end, checksum);
+}
+
struct pldm_bios_table_iter {
const uint8_t *table_data;
size_t table_len;
diff --git a/libpldm/bios_table.h b/libpldm/bios_table.h
index bd42690..ad5891d 100644
--- a/libpldm/bios_table.h
+++ b/libpldm/bios_table.h
@@ -490,6 +490,21 @@
uint16_t attr_handle,
uint8_t attr_type,
uint64_t cv);
+
+/** @brief Get the size of pad and checksum
+ * @param[in] size_without_pad - Table size without pad
+ * @return The size of pad and checksum
+ */
+size_t pldm_bios_table_pad_checksum_size(size_t size_without_pad);
+
+/** @brief Append pad and checksum at the end of the table
+ * @param[in,out] table - Pointer to a buffer of a bios table
+ * @param[in] size - Size of the buffer of a bios table
+ * @param[in] size_without_pad - Table size without pad and checksum
+ */
+void pldm_bios_table_append_pad_checksum(void *table, size_t size,
+ size_t size_without_pad);
+
#ifdef __cplusplus
}
#endif
diff --git a/libpldmresponder/bios.cpp b/libpldmresponder/bios.cpp
index 47bfc0a..1c48f1a 100644
--- a/libpldmresponder/bios.cpp
+++ b/libpldmresponder/bios.cpp
@@ -57,21 +57,17 @@
size_t getTableTotalsize(size_t sizeWithoutPad)
{
- auto padSize = getNumPadBytes(sizeWithoutPad);
- return sizeWithoutPad + padSize + sizeof(uint32_t) /* checksum */;
+ return sizeWithoutPad + pldm_bios_table_pad_checksum_size(sizeWithoutPad);
}
void padAndChecksum(Table& table)
{
- auto padSize = getNumPadBytes(table.size());
- table.insert(table.end(), padSize, 0);
+ auto sizeWithoutPad = table.size();
+ auto padAndChecksumSize = pldm_bios_table_pad_checksum_size(sizeWithoutPad);
+ table.resize(table.size() + padAndChecksumSize);
- boost::crc_32_type result;
- size_t size = table.size();
- result.process_bytes(table.data(), size);
- uint32_t checkSum = result.checksum();
- uint8_t* checkSumPtr = reinterpret_cast<uint8_t*>(&checkSum);
- table.insert(table.end(), checkSumPtr, checkSumPtr + sizeof(checkSum));
+ pldm_bios_table_append_pad_checksum(table.data(), table.size(),
+ sizeWithoutPad);
}
} // namespace utils
diff --git a/test/libpldm_bios_table_test.cpp b/test/libpldm_bios_table_test.cpp
index 84468d3..cbed6d6 100644
--- a/test/libpldm_bios_table_test.cpp
+++ b/test/libpldm_bios_table_test.cpp
@@ -685,3 +685,25 @@
EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL");
pldm_bios_table_iter_free(iter);
}
+
+TEST(PadAndChecksum, PadAndChecksum)
+{
+ EXPECT_EQ(4, pldm_bios_table_pad_checksum_size(0));
+ EXPECT_EQ(7, pldm_bios_table_pad_checksum_size(1));
+ EXPECT_EQ(6, pldm_bios_table_pad_checksum_size(2));
+ EXPECT_EQ(5, pldm_bios_table_pad_checksum_size(3));
+ EXPECT_EQ(4, pldm_bios_table_pad_checksum_size(4));
+
+ // The table is borrowed from
+ // https://github.com/openbmc/pldm/commit/69d3e7fb2d9935773f4fbf44326c33f3fc0a3c38
+ // refer to the commit message
+ Table attrValTable = {0x09, 0x00, 0x01, 0x02, 0x00, 0x65, 0x66};
+ auto sizeWithoutPad = attrValTable.size();
+ attrValTable.resize(sizeWithoutPad +
+ pldm_bios_table_pad_checksum_size(sizeWithoutPad));
+ pldm_bios_table_append_pad_checksum(attrValTable.data(),
+ attrValTable.size(), sizeWithoutPad);
+ Table expectedTable = {0x09, 0x00, 0x01, 0x02, 0x00, 0x65,
+ 0x66, 0x00, 0x6d, 0x81, 0x4a, 0xb6};
+ EXPECT_EQ(attrValTable, expectedTable);
+}