UEFI variable: fix variable index alternation

Change variable index UID handling to switch banks on each write instead
of switching per boot, to properly implement failure safety.

Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I69a41b122e281adee8876ae77a651dd2316ec6a3
diff --git a/components/service/uefi/smm_variable/backend/uefi_variable_store.c b/components/service/uefi/smm_variable/backend/uefi_variable_store.c
index f9f854f..48b81ff 100644
--- a/components/service/uefi/smm_variable/backend/uefi_variable_store.c
+++ b/components/service/uefi/smm_variable/backend/uefi_variable_store.c
@@ -832,6 +832,9 @@
 				remaining_data_len -= data_of_this_iteration;
 
 			} while (remaining_data_len);
+
+			variable_index_confirm_write(&context->variable_index);
+			context->active_variable_index_uid = next_index_uid;
 		} else {
 			EMSG("Syncing variable index failed, store backend is not accessible");
 			return EFI_LOAD_ERROR;
diff --git a/components/service/uefi/smm_variable/backend/variable_index.c b/components/service/uefi/smm_variable/backend/variable_index.c
index 31e6448..9023042 100644
--- a/components/service/uefi/smm_variable/backend/variable_index.c
+++ b/components/service/uefi/smm_variable/backend/variable_index.c
@@ -344,6 +344,11 @@
 	return EFI_SUCCESS;
 }
 
+void variable_index_confirm_write(struct variable_index *context)
+{
+	context->counter++;
+}
+
 size_t variable_index_restore(struct variable_index *context, size_t data_len,
 			      const uint8_t *buffer)
 {
diff --git a/components/service/uefi/smm_variable/backend/variable_index.h b/components/service/uefi/smm_variable/backend/variable_index.h
index 720cbed..592dddc 100644
--- a/components/service/uefi/smm_variable/backend/variable_index.h
+++ b/components/service/uefi/smm_variable/backend/variable_index.h
@@ -210,6 +210,14 @@
 				 uint8_t *buffer, size_t *data_len, bool *any_dirty);
 
 /**
+ * @brief     Confirms the successful write of the variable index into the storage
+ *            by stepping the counter.
+ *
+ * @param[in] context variable_index
+ */
+void variable_index_confirm_write(struct variable_index *context);
+
+/**
  * @brief      Restore the serialized index contents
  *
  * Should be called straight after the variable index is initialized to