Add smm variable power fail recovery

An error condition can occur when the smm variable index is written
to NV storage but the corresponding data is not due to a power
failure. This change adds detection and clean-up for this
condition such that the variable index is always consistent
with the data stored in the peristent storage backend.

Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: Iec88bf0e8a856edf105487354b98d456ef8a60f5
diff --git a/components/service/smm_variable/backend/variable_index.c b/components/service/smm_variable/backend/variable_index.c
index a9484dc..2dc486f 100644
--- a/components/service/smm_variable/backend/variable_index.c
+++ b/components/service/smm_variable/backend/variable_index.c
@@ -97,6 +97,13 @@
 		entry->dirty = true;
 }
 
+static struct variable_entry *containing_entry(const struct variable_info *info)
+{
+	size_t info_offset = offsetof(struct variable_entry, info);
+	struct variable_entry *entry = (struct variable_entry*)((uint8_t*)info - info_offset);
+	return entry;
+}
+
 /* Public functions */
 efi_status_t variable_index_init(
 	struct variable_index *context,
@@ -253,15 +260,11 @@
 
 void variable_index_remove(
 	struct variable_index *context,
-	const EFI_GUID *guid,
-	size_t name_size,
-	const int16_t *name)
+	const struct variable_info *info)
 {
-	int pos = find_variable(context, guid, name_size, name);
+	if (info) {
 
-	if (pos >= 0) {
-
-		struct variable_entry *entry = &context->entries[pos];
+		struct variable_entry *entry = containing_entry(info);
 		mark_dirty(entry);
 		entry->in_use = false;
 
@@ -277,9 +280,7 @@
 	if (info) {
 
 		struct variable_info *modified_info = (struct variable_info*)info;
-
-		size_t info_offset = offsetof(struct variable_entry, info);
-		struct variable_entry *entry = (struct variable_entry*)((uint8_t*)modified_info - info_offset);
+		struct variable_entry *entry = containing_entry(modified_info);
 
 		if (attributes != modified_info->attributes) {