bios: Add en/decodes for integer attribute

Add encode/decode functions for integer attribute to
construct attribute table and attribute value table

Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: I1b61e33c169ea9f78864f9919af0f09fb5ffe5e0
diff --git a/libpldm/bios_table.c b/libpldm/bios_table.c
index 8782a16..271d179 100644
--- a/libpldm/bios_table.c
+++ b/libpldm/bios_table.c
@@ -343,6 +343,64 @@
 	return pldm_bios_table_attr_entry_string_encode_length(def_str_len);
 }
 
+struct attr_table_integer_entry_fields {
+	uint64_t lower_bound;
+	uint64_t upper_bound;
+	uint32_t scalar_increment;
+	uint64_t default_value;
+} __attribute__((packed));
+
+size_t pldm_bios_table_attr_entry_integer_encode_length()
+{
+	return sizeof(struct pldm_bios_attr_table_entry) - 1 +
+	       sizeof(struct attr_table_integer_entry_fields);
+}
+
+void pldm_bios_table_attr_entry_integer_encode(
+    void *entry, size_t entry_length,
+    const struct pldm_bios_table_attr_entry_integer_info *info)
+{
+	size_t length = pldm_bios_table_attr_entry_integer_encode_length();
+	assert(length <= entry_length);
+	uint8_t attr_type =
+	    info->read_only ? PLDM_BIOS_INTEGER_READ_ONLY : PLDM_BIOS_INTEGER;
+	attr_table_entry_encode_header(entry, entry_length, attr_type,
+				       info->name_handle);
+	struct pldm_bios_attr_table_entry *attr_entry = entry;
+	struct attr_table_integer_entry_fields *attr_fields =
+	    (struct attr_table_integer_entry_fields *)attr_entry->metadata;
+	attr_fields->lower_bound = htole64(info->lower_bound);
+	attr_fields->upper_bound = htole64(info->upper_bound);
+	attr_fields->scalar_increment = htole32(info->scalar_increment);
+	attr_fields->default_value = htole64(info->default_value);
+}
+
+int pldm_bios_table_attr_entry_integer_encode_check(
+    void *entry, size_t entry_length,
+    const struct pldm_bios_table_attr_entry_integer_info *info)
+{
+	POINTER_CHECK(entry);
+	POINTER_CHECK(info);
+	size_t length = pldm_bios_table_attr_entry_integer_encode_length();
+	BUFFER_SIZE_EXPECT(entry_length, length);
+	if (info->lower_bound > info->upper_bound ||
+	    info->default_value > info->upper_bound ||
+	    info->default_value < info->lower_bound ||
+	    (info->default_value - info->lower_bound) %
+		    info->scalar_increment !=
+		0)
+		return PLDM_ERROR_INVALID_DATA;
+	pldm_bios_table_attr_entry_integer_encode(entry, entry_length, info);
+	return PLDM_SUCCESS;
+}
+
+static size_t
+attr_table_entry_length_integer(const struct pldm_bios_attr_table_entry *entry)
+{
+	(void)entry;
+	return pldm_bios_table_attr_entry_integer_encode_length();
+}
+
 struct attr_table_entry {
 	uint8_t attr_type;
 	size_t (*entry_length_handler)(
@@ -358,6 +416,10 @@
      .entry_length_handler = attr_table_entry_length_string},
     {.attr_type = PLDM_BIOS_STRING_READ_ONLY,
      .entry_length_handler = attr_table_entry_length_string},
+    {.attr_type = PLDM_BIOS_INTEGER,
+     .entry_length_handler = attr_table_entry_length_integer},
+    {.attr_type = PLDM_BIOS_INTEGER_READ_ONLY,
+     .entry_length_handler = attr_table_entry_length_integer},
 };
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
@@ -462,6 +524,44 @@
 	return PLDM_SUCCESS;
 }
 
+size_t pldm_bios_table_attr_value_entry_encode_integer_length()
+{
+	return sizeof(struct pldm_bios_attr_val_table_entry) - 1 +
+	       sizeof(uint64_t);
+}
+void pldm_bios_table_attr_value_entry_encode_integer(void *entry,
+						     size_t entry_length,
+						     uint16_t attr_handle,
+						     uint8_t attr_type,
+						     uint64_t cv)
+{
+	size_t length =
+	    pldm_bios_table_attr_value_entry_encode_integer_length();
+	assert(length <= entry_length);
+
+	struct pldm_bios_attr_val_table_entry *table_entry = entry;
+	table_entry->attr_handle = htole16(attr_handle);
+	table_entry->attr_type = attr_type;
+	cv = htole64(cv);
+	memcpy(table_entry->value, &cv, sizeof(uint64_t));
+}
+
+int pldm_bios_table_attr_value_entry_encode_integer_check(void *entry,
+							  size_t entry_length,
+							  uint16_t attr_handle,
+							  uint8_t attr_type,
+							  uint64_t cv)
+{
+	POINTER_CHECK(entry);
+	size_t length =
+	    pldm_bios_table_attr_value_entry_encode_integer_length();
+	ATTR_TYPE_EXPECT(attr_type, PLDM_BIOS_INTEGER);
+	BUFFER_SIZE_EXPECT(entry_length, length);
+	pldm_bios_table_attr_value_entry_encode_integer(
+	    entry, entry_length, attr_handle, attr_type, cv);
+	return PLDM_SUCCESS;
+}
+
 struct pldm_bios_table_iter {
 	const uint8_t *table_data;
 	size_t table_len;