+static __maybe_unused bool fwu_empty_capsule(struct efi_capsule_header *capsule)
+{
+ return !guidcmp(&capsule->capsule_guid,
+ &fwu_guid_os_request_fw_revert) ||
+ !guidcmp(&capsule->capsule_guid,
+ &fwu_guid_os_request_fw_accept);
+}
+
+static __maybe_unused efi_status_t fwu_to_efi_error(int err)
+{
+ efi_status_t ret;
+
+ switch(err) {
+ case 0:
+ ret = EFI_SUCCESS;
+ break;
+ case -ERANGE:
+ case -EIO:
+ ret = EFI_DEVICE_ERROR;
+ break;
+ case -EINVAL:
+ ret = EFI_INVALID_PARAMETER;
+ break;
+ case -ENODEV:
+ ret = EFI_NOT_FOUND;
+ break;
+ default:
+ ret = EFI_OUT_OF_RESOURCES;
+ }
+
+ return ret;
+}
+
+static __maybe_unused efi_status_t fwu_empty_capsule_process(
+ struct efi_capsule_header *capsule)
+{
+ int status;
+ u32 active_idx;
+ efi_guid_t *image_guid;
+ efi_status_t ret = EFI_INVALID_PARAMETER;
+
+ if (!guidcmp(&capsule->capsule_guid,
+ &fwu_guid_os_request_fw_revert)) {
+ /*
+ * One of the previously updated image has
+ * failed the OS acceptance test. OS has
+ * requested to revert back to the earlier
+ * boot index
+ */
+ status = fwu_revert_boot_index();
+ ret = fwu_to_efi_error(status);
+ if (ret == EFI_SUCCESS)
+ log_debug("Reverted the FWU active_index. Recommend rebooting the system\n");
+ else
+ log_err("Failed to revert the FWU boot index\n");
+ } else if (!guidcmp(&capsule->capsule_guid,
+ &fwu_guid_os_request_fw_accept)) {
+ /*
+ * Image accepted by the OS. Set the acceptance
+ * status for the image.
+ */
+ image_guid = (void *)(char *)capsule +
+ capsule->header_size;
+
+ status = fwu_get_active_index(&active_idx);
+ ret = fwu_to_efi_error(status);
+ if (ret != EFI_SUCCESS) {
+ log_err("Unable to get the active_index from the FWU metadata\n");
+ return ret;
+ }
+
+ status = fwu_accept_image(image_guid, active_idx);
+ ret = fwu_to_efi_error(status);
+ if (ret != EFI_SUCCESS)
+ log_err("Unable to set the Accept bit for the image %pUs\n",
+ image_guid);
+ }
+
+ return ret;
+}
+
+static __maybe_unused void fwu_post_update_checks(
+ struct efi_capsule_header *capsule,
+ bool *fw_accept_os, bool *capsule_update)
+{
+ if (fwu_empty_capsule(capsule))
+ *capsule_update = false;
+ else
+ if (!*fw_accept_os)
+ *fw_accept_os =
+ capsule->flags & FW_ACCEPT_OS ? true : false;
+}
+
+static __maybe_unused efi_status_t fwu_post_update_process(bool fw_accept_os)
+{
+ int status;
+ uint update_index;
+ efi_status_t ret;
+
+ status = fwu_plat_get_update_index(&update_index);
+ if (status < 0) {
+ log_err("Failed to get the FWU update_index value\n");
+ return EFI_DEVICE_ERROR;
+ }
+
+ /*
+ * All the capsules have been updated successfully,
+ * update the FWU metadata.
+ */
+ log_debug("Update Complete. Now updating active_index to %u\n",
+ update_index);
+ status = fwu_set_active_index(update_index);
+ ret = fwu_to_efi_error(status);
+ if (ret != EFI_SUCCESS) {
+ log_err("Failed to update FWU metadata index values\n");
+ } else {
+ log_debug("Successfully updated the active_index\n");
+ if (fw_accept_os) {
+ status = fwu_trial_state_ctr_start();
+ if (status < 0)
+ ret = EFI_DEVICE_ERROR;
+ }
+ }
+
+ return ret;
+}