efi/tpm: Extract duplicate code into independent functions
PATCH 1/1 efi/tpm: Extract duplicate code into independent functions
Part of the code logic for processing the return value of efi log_extend_event is repetitive and complicated. Extract the repetitive code into an independent function.
Signed-off-by Tianjia Zhang <[email protected]>
--- grub-core/commands/efi/tpm.c | 55 +++++++++++++++--------------------- 1 file changed, 23 insertions(+), 32 deletions(-)
grub-core/commands/efi/tpm.c +23 -32
@@ -127,6 +127,27 @@ grub_tpm_handle_find (grub_efi_handle_t *tpm_handle,return 0 ; } +static grub_err_t +grub_efi_log_event_status (grub_efi_status_t status) +{ +switch (status) + { +case GRUB_EFI_SUCCESS : +return 0 ; +case GRUB_EFI_DEVICE_ERROR : +return grub_error (GRUB_ERR_IO ,N_ ("Command failed" )); +case GRUB_EFI_INVALID_PARAMETER : +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("Invalid parameter" )); +case GRUB_EFI_BUFFER_TOO_SMALL : +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Output buffer too small" )); +case GRUB_EFI_NOT_FOUND : +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,N_ ("TPM unavailable" )); +default : +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,N_ ("Unknown TPM error" )); + } +} + static grub_err_t grub_tpm1_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
@@ -160,22 +181,7 @@ grub_tpm1_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf, algorithm, event, &eventnum, &lastevent);grub_free (event); -switch (status) - { -case GRUB_EFI_SUCCESS : -return 0 ; -case GRUB_EFI_DEVICE_ERROR : -return grub_error (GRUB_ERR_IO ,N_ ("Command failed" )); -case GRUB_EFI_INVALID_PARAMETER : -return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("Invalid parameter" )); -case GRUB_EFI_BUFFER_TOO_SMALL : -return grub_error (GRUB_ERR_BAD_ARGUMENT , -N_ ("Output buffer too small" )); -case GRUB_EFI_NOT_FOUND : -return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,N_ ("TPM unavailable" )); -default : -return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,N_ ("Unknown TPM error" )); - } +return grub_efi_log_event_status (status); } static grub_err_t
@@ -211,22 +217,7 @@ grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf, (grub_uint64_t) size, event);grub_free (event); -switch (status) - { -case GRUB_EFI_SUCCESS : -return 0 ; -case GRUB_EFI_DEVICE_ERROR : -return grub_error (GRUB_ERR_IO ,N_ ("Command failed" )); -case GRUB_EFI_INVALID_PARAMETER : -return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("Invalid parameter" )); -case GRUB_EFI_BUFFER_TOO_SMALL : -return grub_error (GRUB_ERR_BAD_ARGUMENT , -N_ ("Output buffer too small" )); -case GRUB_EFI_NOT_FOUND : -return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,N_ ("TPM unavailable" )); -default : -return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,N_ ("Unknown TPM error" )); - } +return grub_efi_log_event_status (status); } grub_err_t
Signature · 1 line
2.19.1.3.ge56e4f7
Quoted 5 lines · Tianjia Zhang
Part of the code logic for processing the return value of efi
log_extend_eventis repetitive and complicated. Extract the repetitive code into an independent function.Signed-off-by Tianjia Zhang <[email protected]>
Reviewed-by Daniel Kiper <[email protected]>
Daniel