fwu: Mark failed bank as invalid during rollback
v2 cover fwu: Mark failed bank as invalid during rollback
This series improves the FWU (Firmware Update) subsystem's handling of boot failures by properly marking failed banks as invalid during rollback.
Patch 1 refactors fwu_state_machine_updates() to use an enum instead of a boolean, adding support for FWU_BANK_INVALID state.
Patch 2 detects rollback at boot time (when boot_idx != active_idx) and marks the failed bank as invalid to prevent future boot attempts.
Changes in v2:
- Refactors fwu_state_machine_updates() to use enum as parameter instead of adding a new fwu_invalid_bank() helper.
Kory Maincent (2):
fwu: Pass bank state enum to fwu_state_machine_updates()
fwu: Mark failed bank as invalid during rollback
include/fwu.h | 22 ++++++++-------- lib/efi_loader/efi_capsule.c | 5 ++-- lib/fwu_updates/fwu.c | 6 +++++ lib/fwu_updates/fwu_v1.c | 16 ++++++------ lib/fwu_updates/fwu_v2.c | 50 +++++++++++++++--------------------- 5 files changed, 50 insertions(+), 49 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/2 fwu: Pass bank state enum to fwu_state_machine_updates()
Change fwu_state_machine_updates() to accept an enum fwu_bank_states parameter instead of a boolean. This makes the function interface more explicit and prepares for adding FWU_BANK_INVALID support to handle boot failures on the active bank.
Convert the FWU_BANK_* defines to an enum and update all call sites accordingly.
---
Changes in v2:
- Refactors fwu_state_machine_updates() to use enum as parameter instead of adding a new fwu_invalid_bank() helper.
--- include/fwu.h | 22 ++++++++-------- lib/efi_loader/efi_capsule.c | 5 ++-- lib/fwu_updates/fwu_v1.c | 16 ++++++------ lib/fwu_updates/fwu_v2.c | 50 +++++++++++++++--------------------- 4 files changed, 44 insertions(+), 49 deletions(-)
include/fwu.h +12 -10
@@ -80,9 +80,11 @@ struct fwu_mdata_ops {#define FWU_IMAGE_ACCEPTED 0x1 -#define FWU_BANK_INVALID (uint8_t)0xFF -#define FWU_BANK_VALID (uint8_t)0xFE -#define FWU_BANK_ACCEPTED (uint8_t)0xFC +enum fwu_bank_states { +FWU_BANK_INVALID =0xFF , +FWU_BANK_VALID =0xFE , +FWU_BANK_ACCEPTED =0xFC , +};enum {PRIMARY_PART =1 ,
@@ -396,24 +398,24 @@ int fwu_get_mdata_size(uint32_t *mdata_size);/** * fwu_state_machine_updates() - Update FWU state of the platform -* @trial_state: Is platform transitioning into Trial State +* @state: FWU bank state * @update_index: Bank number to which images have been updated * -* On successful completion of updates, transition the platform to -* either Trial State or Regular State. +* FWU_BANK_VALID transition the platform to Trial state +* FWU_BANK_ACCEPTED accept the FWU bank state +* FWU_BANK_INVALID invalid the FWU bank state * * To transition the platform to Trial State, start the * TrialStateCtr counter, followed by setting the value of bank_state * field of the metadata to Valid state(applicable only in version 2 * of metadata). * -* In case, the platform is to transition directly to Regular State, -* update the bank_state field of the metadata to Accepted -* state(applicable only in version 2 of metadata). +* Saving the bank_state field of the metadata is only applicable in +* version 2 of metadata. * * Return: 0 if OK, -ve on error */ -int fwu_state_machine_updates (bool trial_state,uint32_t update_index); +int fwu_state_machine_updates (enum fwu_bank_states state,uint32_t update_index);/** * fwu_init() - FWU specific initialisations
lib/efi_loader/efi_capsule.c +3 -2
@@ -465,7 +465,7 @@ static __maybe_unused efi_status_t fwu_empty_capsule_process(log_err ("Unable to set the Accept bit for the image %pUs\n" , image_guid); - status =fwu_state_machine_updates (0 , active_idx); + status =fwu_state_machine_updates (FWU_BANK_ACCEPTED , active_idx);if (status <0 ) ret =EFI_DEVICE_ERROR ;
@@ -510,7 +510,8 @@ static __maybe_unused efi_status_t fwu_post_update_process(bool fw_accept_os)log_err ("Failed to update FWU metadata index values\n" ); }else {log_debug ("Successfully updated the active_index\n" ); - status =fwu_state_machine_updates (fw_accept_os ?1 :0 , + status =fwu_state_machine_updates (fw_accept_os ? +FWU_BANK_VALID :FWU_BANK_ACCEPTED , update_index);if (status <0 ) ret =EFI_DEVICE_ERROR ;
lib/fwu_updates/fwu_v1.c +8 -8
@@ -98,27 +98,27 @@ void fwu_populate_mdata_image_info(struct fwu_data *data)/** * fwu_state_machine_updates() - Update FWU state of the platform -* @trial_state: Is platform transitioning into Trial State +* @state: FWU bank state * @update_index: Bank number to which images have been updated * -* On successful completion of updates, transition the platform to -* either Trial State or Regular State. +* FWU_BANK_VALID transition the platform to Trial state +* FWU_BANK_ACCEPTED accept the FWU bank state +* FWU_BANK_INVALID invalid the FWU bank state * * To transition the platform to Trial State, start the * TrialStateCtr counter, followed by setting the value of bank_state * field of the metadata to Valid state(applicable only in version 2 * of metadata). * -* In case, the platform is to transition directly to Regular State, -* update the bank_state field of the metadata to Accepted -* state(applicable only in version 2 of metadata). +* Saving the bank_state field of the metadata is only applicable in +* version 2 of metadata. * * Return: 0 if OK, -ve on error */ -int fwu_state_machine_updates (bool trial_state, +int fwu_state_machine_updates (enum fwu_bank_states state,uint32_t update_index) { -return fwu_trial_state_update (trial_state, update_index); +return fwu_trial_state_update (state ==FWU_BANK_VALID , update_index); }/**
lib/fwu_updates/fwu_v2.c +21 -29
90 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 1/2] fwu: Pass bank state enum to fwu_state_machine_updates()
Thanks Kory,
This looks cleaner.
I got ont nit, I don't think you need to cast the states to a unit8.
Quoted 11 lines · Kory Maincent
Change
fwu_state_machine_updates()to accept an enumfwu_bank_statesparameter instead of a boolean. This makes the function interface more explicit and prepares for addingFWU_BANK_INVALIDsupport to handle boot failures on the active bank.Convert the FWU_BANK_* defines to an enum and update all call sites accordingly.
Signed-off-by Kory Maincent <[email protected]>---
Quoted 223 lines
Changes in v2:
- Refactorsfwu_state_machine_updates()to use enum as parameter instead of adding a newfwu_invalid_bank()helper.--- include/fwu.h | 22 ++++++++-------- lib/efi_loader/efi_capsule.c | 5 ++-- lib/fwu_updates/fwu_v1.c | 16 ++++++------ lib/fwu_updates/fwu_v2.c | 50 +++++++++++++++--------------------- 4 files changed, 44 insertions(+), 49 deletions(-)include/fwu.h +12
-10@@ -80,9 +80,11 @@ struct fwu_mdata_ops {#define FWU_IMAGE_ACCEPTED 0x1 -#define FWU_BANK_INVALID (uint8_t)0xFF -#define FWU_BANK_VALID (uint8_t)0xFE -#define FWU_BANK_ACCEPTED (uint8_t)0xFC +enum fwu_bank_states { +FWU_BANK_INVALID =0xFF , +FWU_BANK_VALID =0xFE , +FWU_BANK_ACCEPTED =0xFC , +};enum {PRIMARY_PART =1 ,@@ -396,24 +398,24 @@ int fwu_get_mdata_size(uint32_t *mdata_size);/** * fwu_state_machine_updates() - Update FWU state of the platform -* @trial_state: Is platform transitioning into Trial State +* @state: FWU bank state * @update_index: Bank number to which images have been updated * -* On successful completion of updates, transition the platform to -* either Trial State or Regular State. +* FWU_BANK_VALID transition the platform to Trial state +* FWU_BANK_ACCEPTED accept the FWU bank state +* FWU_BANK_INVALID invalid the FWU bank state * * To transition the platform to Trial State, start the * TrialStateCtr counter, followed by setting the value of bank_state * field of the metadata to Valid state(applicable only in version 2 * of metadata). * -* In case, the platform is to transition directly to Regular State, -* update the bank_state field of the metadata to Accepted -* state(applicable only in version 2 of metadata). +* Saving the bank_state field of the metadata is only applicable in +* version 2 of metadata. * * Return: 0 if OK, -ve on error */ -int fwu_state_machine_updates (bool trial_state,uint32_t update_index); +int fwu_state_machine_updates (enum fwu_bank_states state,uint32_t update_index);/** * fwu_init() - FWU specific initialisations lib/efi_loader/efi_capsule.c +3
-2@@ -465,7 +465,7 @@ static __maybe_unused efi_status_t fwu_empty_capsule_process(log_err ("Unable to set the Accept bit for the image %pUs\n" , image_guid); - status =fwu_state_machine_updates (0 , active_idx); + status =fwu_state_machine_updates (FWU_BANK_ACCEPTED , active_idx);if (status <0 ) ret =EFI_DEVICE_ERROR ;@@ -510,7 +510,8 @@ static __maybe_unused efi_status_t fwu_post_update_process(bool fw_accept_os)log_err ("Failed to update FWU metadata index values\n" ); }else {log_debug ("Successfully updated the active_index\n" ); - status =fwu_state_machine_updates (fw_accept_os ?1 :0 , + status =fwu_state_machine_updates (fw_accept_os ? +FWU_BANK_VALID :FWU_BANK_ACCEPTED , update_index);if (status <0 ) ret =EFI_DEVICE_ERROR ;lib/fwu_updates/fwu_v1.c +8
-8@@ -98,27 +98,27 @@ void fwu_populate_mdata_image_info(struct fwu_data *data)/** * fwu_state_machine_updates() - Update FWU state of the platform -* @trial_state: Is platform transitioning into Trial State +* @state: FWU bank state * @update_index: Bank number to which images have been updated * -* On successful completion of updates, transition the platform to -* either Trial State or Regular State. +* FWU_BANK_VALID transition the platform to Trial state +* FWU_BANK_ACCEPTED accept the FWU bank state +* FWU_BANK_INVALID invalid the FWU bank state * * To transition the platform to Trial State, start the * TrialStateCtr counter, followed by setting the value of bank_state * field of the metadata to Valid state(applicable only in version 2 * of metadata). * -* In case, the platform is to transition directly to Regular State, -* update the bank_state field of the metadata to Accepted -* state(applicable only in version 2 of metadata). +* Saving the bank_state field of the metadata is only applicable in +* version 2 of metadata. * * Return: 0 if OK, -ve on error */ -int fwu_state_machine_updates (bool trial_state, +int fwu_state_machine_updates (enum fwu_bank_states state,uint32_t update_index) { -return fwu_trial_state_update (trial_state, update_index); +return fwu_trial_state_update (state ==FWU_BANK_VALID , update_index); }/** lib/fwu_updates/fwu_v2.c +21
-30@@ -80,42 +80,27 @@ static int fwu_mdata_sanity_checks(void)return 0 ; } -static int fwu_bank_state_update (bool trial_state,uint32_t bank) +static int fwu_bank_state_update (enum fwu_bank_states state,uint32_t bank) {int ret;struct fwu_data *data =fwu_get_data ();struct fwu_mdata *mdata = data->fwu_mdata; -if (!trial_state && !fwu_bank_accepted (data, bank)) +if (state ==FWU_BANK_ACCEPTED && !fwu_bank_accepted (data, bank))return 0 ; - mdata->bank_state[bank] = data->bank_state[bank] = trial_state ? -FWU_BANK_VALID :FWU_BANK_ACCEPTED ; + mdata->bank_state[bank] = (uint8_t )state; + data->bank_state[bank] = (uint8_t )state; ret =fwu_sync_mdata (mdata,BOTH_PARTS );if (ret)log_err ("Unable to set bank_state for bank %u\n" , bank);else - data->trial_state = trial_state; + data->trial_state = state ==FWU_BANK_VALID ?1 :0 ;return ret; } -static int fwu_trial_state_start (uint update_index) -{ -int ret; - - ret =fwu_trial_state_ctr_start (); -if (ret) -return ret; - - ret =fwu_bank_state_update (1 , update_index); -if (ret) -return ret; - -return 0 ; -} -static bool fwu_get_mdata_mandatory (uint part) {int ret =0 ;@@ -171,27 +156,34 @@ void fwu_populate_mdata_image_info(struct fwu_data *data)/** * fwu_state_machine_updates() - Update FWU state of the platform -* @trial_state: Is platform transitioning into Trial State +* @state: FWU bank state * @update_index: Bank number to which images have been updated * -* On successful completion of updates, transition the platform to -* either Trial State or Regular State. +* FWU_BANK_VALID transition the platform to Trial state +* FWU_BANK_ACCEPTED accept the FWU bank state +* FWU_BANK_INVALID invalid the FWU bank state * * To transition the platform to Trial State, start the * TrialStateCtr counter, followed by setting the value of bank_state * field of the metadata to Valid state(applicable only in version 2 * of metadata). * -* In case, the platform is to transition directly to Regular State, -* update the bank_state field of the metadata to Accepted -* state(applicable only in version 2 of metadata). +* Saving the bank_state field of the metadata is only applicable in +* version 2 of metadata. * * Return: 0 if OK, -ve on error */ -int fwu_state_machine_updates (bool trial_state,uint32_t update_index) +int fwu_state_machine_updates (enum fwu_bank_states state,uint32_t update_index) { -return trial_state ?fwu_trial_state_start (update_index) : -fwu_bank_state_update (0 , update_index); +int ret; + +if (state ==FWU_BANK_VALID ) { + ret =fwu_trial_state_ctr_start (); +if (ret) +return ret; + } + +return fwu_bank_state_update (state, update_index); }/** -- 2.43.0
PATCH v2 2/2 fwu: Mark failed bank as invalid during rollback
In reply to Kory MaincentWhen boot_idx differs from active_idx at boot time, it indicates a rollback scenario where the firmware update failed and the system reverted to the previous working bank. In this case, mark the failed bank (active_idx) as invalid to prevent future boot attempts from that bank.
--- lib/fwu_updates/fwu.c | 6 ++++++ 1 file changed, 6 insertions(+)
lib/fwu_updates/fwu.c +6 -0
@@ -766,6 +766,12 @@ static int fwu_boottime_checks(void)if (boot_idx != active_idx) {log_info ("Boot idx %u is not matching active idx %u, changing active_idx\n" , boot_idx, active_idx); + + ret =fwu_state_machine_updates (FWU_BANK_INVALID , active_idx); +if (ret) +log_err ("Unable to set bank %u state as invalid" , + active_idx); + ret =fwu_set_active_index (boot_idx);if (!ret) boottime_check =1 ;
Signature · 1 line
2.43.0
Quoted 9 lines · Kory Maincent
When
boot_idxdiffers fromactive_idxat boot time, it indicates a rollback scenario where the firmware update failed and the system reverted to the previous working bank. In this case, mark the failed bank (active_idx) as invalid to prevent future boot attempts from that bank.Signed-off-by Kory Maincent <[email protected]>---
Quoted 23 lines
lib/fwu_updates/fwu.c | 6 ++++++ 1 file changed, 6 insertions(+)lib/fwu_updates/fwu.c +6
-1@@ -766,6 +766,12 @@ static int fwu_boottime_checks(void)if (boot_idx != active_idx) {log_info ("Boot idx %u is not matching active idx %u, changing active_idx\n" , boot_idx, active_idx); + + ret =fwu_state_machine_updates (FWU_BANK_INVALID , active_idx); +if (ret) +log_err ("Unable to set bank %u state as invalid" , + active_idx); + ret =fwu_set_active_index (boot_idx);if (!ret) boottime_check =1 ; --2.43.0
Quoted 8 lines · On Mon, 19 Jan 2026 17:07:40 +0100 Kory Maincent <[email protected]>…
This series improves the FWU (Firmware Update) subsystem's handling of boot failures by properly marking failed banks as invalid during rollback.
Patch 1 refactors
fwu_state_machine_updates()to use an enum instead of a boolean, adding support forFWU_BANK_INVALIDstate.Patch 2 detects rollback at boot time (when
boot_idx!=active_idx) and marks the failed bank as invalid to prevent future boot attempts.
Hello,
Any news on this series? Does something prevent it to be merged?
Regards,
Signature · 3 lines
Köry Maincent, Bootlin Embedded Linux and kernel engineering https://bootlin.com