diff mbox series

[v2,09/21] fwu: add a function to put a bank in Trial State

Message ID 20240212074712.3657076-10-sughosh.ganu@linaro.org
State New
Headers show
Series FWU: Migrate FWU metadata to version 2 | expand

Commit Message

Sughosh Ganu Feb. 12, 2024, 7:47 a.m. UTC
The version 2 of the FWU metadata has a field in the top level
structure, called bank_state. This is used to keep the state a given
bank is in, either of valid(for trial state), invalid, or accepted.

Update this field when putting the platform in Trial State, in
addition to starting the TrialStateCtr variable by calling the
fwu_trial_state_start() function.

Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
---

Changes since V1: None

 include/fwu.h                | 12 +++++---
 lib/efi_loader/efi_capsule.c |  2 +-
 lib/fwu_updates/fwu.c        | 54 ++++++++++++++++++++++++++++++------
 3 files changed, 54 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/include/fwu.h b/include/fwu.h
index 109ceb2610..1c6a5fcda9 100644
--- a/include/fwu.h
+++ b/include/fwu.h
@@ -342,15 +342,19 @@  u8 fwu_update_checks_pass(void);
 u8 fwu_empty_capsule_checks_pass(void);
 
 /**
- * fwu_trial_state_ctr_start() - Start the Trial State counter
+ * fwu_trial_state_start() - Put the platform in Trial State
+ * @update_index: Bank number to which images have been updated
  *
- * Start the counter to identify the platform booting in the
- * Trial State. The counter is implemented as an EFI variable.
+ * Put the platform in Trial State by starting the counter to
+ * identify the platform booting in the Trial State. The
+ * counter is implemented as an EFI variable. Secondly, set
+ * the bank_state in the metadata for the updated bank to Valid
+ * state.
  *
  * Return: 0 if OK, -ve on error
  *
  */
-int fwu_trial_state_ctr_start(void);
+int fwu_trial_state_start(uint update_index);
 
 /**
  * fwu_gen_alt_info_from_mtd() - Parse dfu_alt_info from metadata in mtd
diff --git a/lib/efi_loader/efi_capsule.c b/lib/efi_loader/efi_capsule.c
index de0d49ebeb..0e6a38b441 100644
--- a/lib/efi_loader/efi_capsule.c
+++ b/lib/efi_loader/efi_capsule.c
@@ -522,7 +522,7 @@  static __maybe_unused efi_status_t fwu_post_update_process(bool fw_accept_os)
 	} else {
 		log_debug("Successfully updated the active_index\n");
 		if (fw_accept_os) {
-			status = fwu_trial_state_ctr_start();
+			status = fwu_trial_state_start(update_index);
 			if (status < 0)
 				ret = EFI_DEVICE_ERROR;
 		}
diff --git a/lib/fwu_updates/fwu.c b/lib/fwu_updates/fwu.c
index 587ca779d3..a58c42bee2 100644
--- a/lib/fwu_updates/fwu.c
+++ b/lib/fwu_updates/fwu.c
@@ -672,6 +672,36 @@  out:
 	return ret;
 }
 
+static int fwu_trial_state_ctr_start(void)
+{
+	int ret;
+	u16 trial_state_ctr;
+
+	printf("%s: starting the TrialStateCtr\n", __func__);
+	trial_state_ctr = 0;
+	ret = trial_counter_update(&trial_state_ctr);
+	if (ret)
+		log_err("Unable to initialise TrialStateCtr\n");
+
+	return ret;
+}
+
+static int fwu_set_bank_state_trial(uint update_index)
+{
+	int ret;
+	struct fwu_mdata *mdata = g_mdata;
+
+	mdata->bank_state[update_index] = FWU_BANK_VALID;
+
+	ret = fwu_sync_mdata(mdata, BOTH_PARTS);
+	if (ret) {
+		log_err("Unable to set bank_state for %d bank\n", update_index);
+		return ret;
+	}
+
+	return 0;
+}
+
 /**
  * fwu_bank_state_update() - Check and update the bank_state of the metadata
  * @update_index: Bank for which the bank_state needs to be updated
@@ -830,25 +860,31 @@  u8 fwu_empty_capsule_checks_pass(void)
 }
 
 /**
- * fwu_trial_state_ctr_start() - Start the Trial State counter
+ * fwu_trial_state_start() - Put the platform in Trial State
+ * @update_index: Bank number to which images have been updated
  *
- * Start the counter to identify the platform booting in the
- * Trial State. The counter is implemented as an EFI variable.
+ * Put the platform in Trial State by starting the counter to
+ * identify the platform booting in the Trial State. The
+ * counter is implemented as an EFI variable. Secondly, set
+ * the bank_state in the metadata for the updated bank to Valid
+ * state.
  *
  * Return: 0 if OK, -ve on error
  *
  */
-int fwu_trial_state_ctr_start(void)
+int fwu_trial_state_start(uint update_index)
 {
 	int ret;
-	u16 trial_state_ctr;
 
-	trial_state_ctr = 0;
-	ret = trial_counter_update(&trial_state_ctr);
+	ret = fwu_trial_state_ctr_start();
 	if (ret)
-		log_err("Unable to initialise TrialStateCtr\n");
+		return ret;
 
-	return ret;
+	ret = fwu_set_bank_state_trial(update_index);
+	if (ret)
+		return ret;
+
+	return 0;
 }
 
 static int fwu_boottime_checks(void)