[PATCH 2/4] efi_loader: firmware: add last attempt tracking for RAW capsules

Balaji Selvanathan via U-Boot <[email protected]>
Newsgroups org.u-boot-project.lists.u-boot
Message-ID <20260813-efi-esrt-last-attempt-v1-2-3be172f47d9d@oss.qualcomm.com>
Now that the tracking helpers exist, call them from the RAW capsule
path. efi_firmware_raw_set_image() records the attempted version and
the mapped status at each point it can bail out (verification failure,
strdup/env_set errors, and a failed dfu_write_by_alt), and records
success once the write completes.

Either way it then writes the FMP state variable, so a query of ESRT
reflects the outcome of the last attempt instead of stale boot-time
values.

Signed-off-by: Balaji Selvanathan <[email protected]>
---
 lib/efi_loader/efi_firmware.c | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/lib/efi_loader/efi_firmware.c b/lib/efi_loader/efi_firmware.c
index cbe8bcd7c64..455d79a19ca 100644
--- a/lib/efi_loader/efi_firmware.c
+++ b/lib/efi_loader/efi_firmware.c
@@ -824,8 +824,17 @@ efi_status_t EFIAPI efi_firmware_raw_set_image(
 
 	status = efi_firmware_verify_image(&image, &image_size, image_index,
 					   &state);
-	if (status != EFI_SUCCESS)
+	if (status != EFI_SUCCESS) {
+		/* Set last attempt information for failed verification */
+		efi_firmware_set_last_attempt(&state, state.fw_version,
+					      efi_firmware_map_error_to_status(status));
+		efi_firmware_set_fmp_state_var(&state, image_index);
 		return EFI_EXIT(status);
+	}
+
+	/* Set last attempt version before starting the update */
+	efi_firmware_set_last_attempt(&state, state.fw_version,
+				      LAST_ATTEMPT_STATUS_SUCCESS);
 
 	/*
 	 * dfu_alt_num is assigned from 0 while image_index starts from 1.
@@ -850,13 +859,21 @@ efi_status_t EFIAPI efi_firmware_raw_set_image(
 		orig_dfu_env = strdup(orig_dfu_env);
 		if (!orig_dfu_env) {
 			log_err("strdup() failed!\n");
-			return EFI_EXIT(EFI_OUT_OF_RESOURCES);
+			status = EFI_OUT_OF_RESOURCES;
+			efi_firmware_set_last_attempt(&state, state.fw_version,
+						      efi_firmware_map_error_to_status(status));
+			efi_firmware_set_fmp_state_var(&state, image_index);
+			return EFI_EXIT(status);
 		}
 	}
 	if (env_set("dfu_alt_info", update_info.dfu_string)) {
 		log_err("Unable to set env variable \"dfu_alt_info\"!\n");
 		free(orig_dfu_env);
-		return EFI_EXIT(EFI_DEVICE_ERROR);
+		status = EFI_DEVICE_ERROR;
+		efi_firmware_set_last_attempt(&state, state.fw_version,
+					      efi_firmware_map_error_to_status(status));
+		efi_firmware_set_fmp_state_var(&state, image_index);
+		return EFI_EXIT(status);
 	}
 
 	ret = dfu_write_by_alt(dfu_alt_num, (void *)image, image_size,
@@ -867,9 +884,17 @@ efi_status_t EFIAPI efi_firmware_raw_set_image(
 
 	free(orig_dfu_env);
 
-	if (ret)
-		return EFI_EXIT(EFI_DEVICE_ERROR);
+	if (ret) {
+		status = EFI_DEVICE_ERROR;
+		efi_firmware_set_last_attempt(&state, state.fw_version,
+					      efi_firmware_map_error_to_status(status));
+		efi_firmware_set_fmp_state_var(&state, image_index);
+		return EFI_EXIT(status);
+	}
 
+	/* Update successful - set success status */
+	efi_firmware_set_last_attempt(&state, state.fw_version,
+				      LAST_ATTEMPT_STATUS_SUCCESS);
 	efi_firmware_set_fmp_state_var(&state, image_index);
 
 	return EFI_EXIT(EFI_SUCCESS);

-- 
2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.