bios: Implement BIOSConfig

Load the parsed json configs into memory.
And provid APIs to get/set on bios tables.

Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: Ida1fedc923d31afc61dd2d4aec70d81bb6a90ae9
diff --git a/libpldmresponder/bios_table.cpp b/libpldmresponder/bios_table.cpp
index e68884b..4a190f0 100644
--- a/libpldmresponder/bios_table.cpp
+++ b/libpldmresponder/bios_table.cpp
@@ -82,6 +82,16 @@
 namespace table
 {
 
+void appendPadAndChecksum(Table& table)
+{
+    auto sizeWithoutPad = table.size();
+    auto padAndChecksumSize = pldm_bios_table_pad_checksum_size(sizeWithoutPad);
+    table.resize(table.size() + padAndChecksumSize);
+
+    pldm_bios_table_append_pad_checksum(table.data(), table.size(),
+                                        sizeWithoutPad);
+}
+
 namespace string
 {
 
@@ -98,6 +108,17 @@
                                                buffer.size());
     return std::string(buffer.data(), buffer.data() + strLength);
 }
+const pldm_bios_string_table_entry* constructEntry(Table& table,
+                                                   const std::string& str)
+{
+    auto tableSize = table.size();
+    auto entryLength = pldm_bios_table_string_entry_encode_length(str.length());
+    table.resize(tableSize + entryLength);
+    pldm_bios_table_string_entry_encode(table.data() + tableSize, entryLength,
+                                        str.c_str(), str.length());
+    return reinterpret_cast<pldm_bios_string_table_entry*>(table.data() +
+                                                           tableSize);
+}
 
 } // namespace string
 
@@ -112,6 +133,13 @@
     return {attrHandle, attrType, stringHandle};
 }
 
+const pldm_bios_attr_table_entry* findByHandle(const Table& table,
+                                               uint16_t handle)
+{
+    return pldm_bios_table_attr_find_by_handle(table.data(), table.size(),
+                                               handle);
+}
+
 const pldm_bios_attr_table_entry*
     constructStringEntry(Table& table,
                          pldm_bios_table_attr_entry_string_info* info)
@@ -179,6 +207,30 @@
     return reinterpret_cast<pldm_bios_attr_val_table_entry*>(table.data() +
                                                              tableSize);
 }
+std::optional<Table> updateTable(const Table& table, const void* entry,
+                                 size_t size)
+{
+    // Replace the old attribute with the new attribute, the size of table will
+    // change:
+    //   sizeof(newTableBuffer) = srcTableSize + sizeof(newAttribute) -
+    //                      sizeof(oldAttribute) + pad(4-byte alignment, max =
+    //                      3)
+    // For simplicity, we use
+    //   sizeof(newTableBuffer) = srcTableSize + sizeof(newAttribute) + 3
+    size_t destBufferLength = table.size() + size + 3;
+    Table destTable(destBufferLength);
+
+    auto rc = pldm_bios_table_attr_value_copy_and_update(
+        table.data(), table.size(), destTable.data(), &destBufferLength, entry,
+        size);
+    if (rc != PLDM_SUCCESS)
+    {
+        return std::nullopt;
+    }
+    destTable.resize(destBufferLength);
+
+    return destTable;
+}
 
 } // namespace attribute_value