bios_table: Introduce pldm_bios_table_append_pad_checksum_check()
pldm_bios_table_append_pad_checksum() was unable to indicate an error to
the caller and so resorted to using assert(). Introduce
pldm_bios_table_append_pad_checksum_check() which returns an error code
instead handling errors with assert(). From there, implement
pldm_bios_table_append_pad_checksum() in terms of
pldm_bios_table_append_pad_checksum_check().
Users of pldm_bios_table_append_pad_checksum() should prefer
pldm_bios_table_append_pad_checksum_check().
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I24e05c09023a9fcf7eee9e79668c552f08b4b6dd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 72821b2..e5652cb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,10 @@
## [Unreleased]
+### Added
+
+1. bios_table: Introduce pldm_bios_table_append_pad_checksum_check()
+
### Changed
1. requester: Mark pldm_close() as LIBPLDM_ABI_TESTING
diff --git a/include/libpldm/bios_table.h b/include/libpldm/bios_table.h
index b042a0e..db3aeef 100644
--- a/include/libpldm/bios_table.h
+++ b/include/libpldm/bios_table.h
@@ -651,12 +651,25 @@
/** @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
+ * @param[in] capacity - Size of the buffer of a bios table
+ * @param[in] size - Table size without pad and checksum
* @return Total size of the table
*/
-size_t pldm_bios_table_append_pad_checksum(void *table, size_t size,
- size_t size_without_pad);
+size_t pldm_bios_table_append_pad_checksum(void *table, size_t capacity,
+ size_t size);
+
+/** @brief Append pad and checksum at the end of the table or return an error
+ * @param[in,out] table - Pointer to a buffer of a bios table
+ * @param[in] capacity - Size of the buffer of a bios table
+ * @param[in,out] size - On input, the table size without pad and checksum, on output, the table
+ * with the padding and checksum appended
+ * @return PLDM_SUCCESS on success, PLDM_INVALID_DATA if table or size are NULL, or
+ * PLDM_ERROR_INVALID_LENGTH if size lacks capacity to encode the padded checksum in the
+ * buffer provided by table. The appropriate buffer capacity can be determined with the
+ * help of @ref pldm_bios_table_pad_checksum_size
+ */
+int pldm_bios_table_append_pad_checksum_check(void *table, size_t capacity,
+ size_t *size);
/** @brief Build a new table and update an entry
* @param[in] src_table - Pointer to the source table
diff --git a/src/bios_table.c b/src/bios_table.c
index ff54d40..2605e40 100644
--- a/src/bios_table.c
+++ b/src/bios_table.c
@@ -911,22 +911,40 @@
}
LIBPLDM_ABI_STABLE
-size_t pldm_bios_table_append_pad_checksum(void *table, size_t size,
- size_t size_without_pad)
+size_t pldm_bios_table_append_pad_checksum(void *table, size_t capacity,
+ size_t size)
{
- size_t pad_checksum_size =
- pldm_bios_table_pad_checksum_size(size_without_pad);
- size_t total_length = size_without_pad + pad_checksum_size;
- assert(size >= total_length);
+ int rc = pldm_bios_table_append_pad_checksum_check(table, capacity,
+ &size);
+ (void)rc;
+ assert(rc == PLDM_SUCCESS);
+ return size;
+}
- uint8_t *table_end = (uint8_t *)table + size_without_pad;
- size_t pad_size = pad_size_get(size_without_pad);
+LIBPLDM_ABI_TESTING
+int pldm_bios_table_append_pad_checksum_check(void *table, size_t capacity,
+ size_t *size)
+{
+ if (!table || !size) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ size_t pad_checksum_size = pldm_bios_table_pad_checksum_size(*size);
+ size_t total_length = *size + pad_checksum_size;
+ assert(capacity >= total_length);
+ if (capacity < total_length) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ uint8_t *table_end = (uint8_t *)table + *size;
+ size_t pad_size = pad_size_get(*size);
table_end = pad_append(table_end, pad_size);
- uint32_t checksum = crc32(table, size_without_pad + pad_size);
+ uint32_t checksum = crc32(table, *size + pad_size);
checksum_append(table_end, checksum);
+ *size = total_length;
- return total_length;
+ return PLDM_SUCCESS;
}
struct pldm_bios_table_iter {