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