From: Jan Janssen Date: Mon, 7 Feb 2022 12:05:56 +0000 (+0100) Subject: boot: Use cleanup handler to unload image X-Git-Tag: v252-rc1~946^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f386daa05466b532ff7dcd7b79b55566460e362c;p=thirdparty%2Fsystemd.git boot: Use cleanup handler to unload image This also moves the message about failed image execution into image_start() as we would otherwise show two error messages if any of the preparatory steps failed. --- diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c index fb08b99333b..8ad2ebe5263 100644 --- a/src/boot/efi/boot.c +++ b/src/boot/efi/boot.c @@ -2322,7 +2322,7 @@ static EFI_STATUS image_start( const ConfigEntry *entry) { _cleanup_(devicetree_cleanup) struct devicetree_state dtstate = {}; - EFI_HANDLE image; + _cleanup_(unload_imagep) EFI_HANDLE image = NULL; _cleanup_freepool_ EFI_DEVICE_PATH *path = NULL; CHAR16 *options; EFI_STATUS err; @@ -2363,10 +2363,8 @@ static EFI_STATUS image_start( err = BS->OpenProtocol(image, &LoadedImageProtocol, (void **)&loaded_image, parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (EFI_ERROR(err)) { - log_error_stall(L"Error getting LoadedImageProtocol handle: %r", err); - goto out_unload; - } + if (EFI_ERROR(err)) + return log_error_status_stall(err, L"Error getting LoadedImageProtocol handle: %r", err); loaded_image->LoadOptions = options; loaded_image->LoadOptionsSize = StrSize(loaded_image->LoadOptions); @@ -2376,9 +2374,11 @@ static EFI_STATUS image_start( efivar_set_time_usec(LOADER_GUID, L"LoaderTimeExecUSec", 0); err = BS->StartImage(image, NULL, NULL); -out_unload: - BS->UnloadImage(image); - return err; + graphics_mode(FALSE); + if (err != EFI_SUCCESS) + return log_error_status_stall(err, L"Failed to execute %s (%s): %r", entry->title_show, entry->loader, err); + + return EFI_SUCCESS; } static void config_free(Config *config) { @@ -2623,11 +2623,10 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) { (void) process_random_seed(root_dir, config.random_seed_mode); err = image_start(image, &config, entry); - if (EFI_ERROR(err)) { - graphics_mode(FALSE); - log_error_stall(L"Failed to execute %s (%s): %r", entry->title_show, entry->loader, err); + if (err != EFI_SUCCESS) + /* Not using EFI_ERROR here because positive values are also errors like with any + * other (userspace) program. */ goto out; - } menu = TRUE; config.timeout_sec = 0; diff --git a/src/boot/efi/drivers.c b/src/boot/efi/drivers.c index 2ff87261dab..65c74c461d5 100644 --- a/src/boot/efi/drivers.c +++ b/src/boot/efi/drivers.c @@ -6,17 +6,12 @@ #include "drivers.h" #include "util.h" -static void efi_unload_image(EFI_HANDLE *h) { - if (*h) - (void) BS->UnloadImage(*h); -} - static EFI_STATUS load_one_driver( EFI_HANDLE parent_image, EFI_LOADED_IMAGE *loaded_image, const CHAR16 *fname) { - _cleanup_(efi_unload_image) EFI_HANDLE image = NULL; + _cleanup_(unload_imagep) EFI_HANDLE image = NULL; _cleanup_freepool_ EFI_DEVICE_PATH *path = NULL; _cleanup_freepool_ CHAR16 *spath = NULL; EFI_STATUS err; diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index c45374a08eb..af204e9ab57 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -86,6 +86,11 @@ static inline void file_closep(EFI_FILE **handle) { (*handle)->Close(*handle); } +static inline void unload_imagep(EFI_HANDLE *image) { + if (*image) + (void) BS->UnloadImage(*image); +} + /* * Allocated random UUID, intended to be shared across tools that implement * the (ESP)\loader\entries\-.conf convention and the