From b4eb2de7e3ec01cd9fb2b8cda8793da64a6ef027 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Mon, 12 Jun 2023 15:12:05 +0200 Subject: [PATCH] boot: Unify protocol opening We are using HandleProtocol everywhere except in these few cases. This unifies on HandleProtocol as it is simpler to use and equivalent to OpenProtocol. The only difference between the two is that OpenProtocol attributes the opened protocol to a firmware-owned handle instead of our image handle. This has no real use for regular UEFI applications as any protocols opened via BY_HANDLE or GET_PROTOCOL is not required to be closed. In fact, when a protocol is uninstalled it will do nothing more than reduce the open count for these. --- src/boot/efi/boot.c | 24 +++++++----------------- src/boot/efi/initrd.c | 11 +---------- src/boot/efi/stub.c | 8 +------- 3 files changed, 9 insertions(+), 34 deletions(-) diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c index 65ad6f751a7..cda6f564268 100644 --- a/src/boot/efi/boot.c +++ b/src/boot/efi/boot.c @@ -2647,13 +2647,7 @@ static EFI_STATUS run(EFI_HANDLE image) { * By default, Shim uninstalls its protocol when calling StartImage(). */ shim_retain_protocol(); - err = BS->OpenProtocol( - image, - MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), - (void **) &loaded_image, - image, - NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); + err = BS->HandleProtocol(image, MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), (void **) &loaded_image); if (err != EFI_SUCCESS) return log_error_status(err, "Error getting a LoadedImageProtocol handle: %m"); @@ -2669,10 +2663,10 @@ static EFI_STATUS run(EFI_HANDLE image) { config_load_all_entries(&config, loaded_image, loaded_image_path, root_dir); - if (config.entry_count == 0) { - log_error("No loader found. Configuration files in \\loader\\entries\\*.conf are needed."); - goto out; - } + if (config.entry_count == 0) + return log_error_status( + EFI_NOT_FOUND, + "No loader found. Configuration files in \\loader\\entries\\*.conf are needed."); /* select entry or show menu when key is pressed or timeout is set */ if (config.force_menu || config.timeout_sec > 0) @@ -2699,7 +2693,7 @@ static EFI_STATUS run(EFI_HANDLE image) { if (menu) { efivar_set_time_usec(MAKE_GUID_PTR(LOADER), u"LoaderTimeMenuUSec", 0); if (!menu_run(&config, &entry, loaded_image_path)) - break; + return EFI_SUCCESS; } /* if auto enrollment is activated, we try to load keys for the given entry. */ @@ -2725,15 +2719,11 @@ static EFI_STATUS run(EFI_HANDLE image) { err = image_start(image, entry); if (err != EFI_SUCCESS) - goto out; + return err; menu = true; config.timeout_sec = 0; } - err = EFI_SUCCESS; -out: - BS->CloseProtocol(image, MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), image, NULL); - return err; } DEFINE_EFI_MAIN_FUNCTION(run, "systemd-boot", /*wait_for_debugger=*/false); diff --git a/src/boot/efi/initrd.c b/src/boot/efi/initrd.c index e8af9995086..527b05f5dbd 100644 --- a/src/boot/efi/initrd.c +++ b/src/boot/efi/initrd.c @@ -117,19 +117,10 @@ EFI_STATUS initrd_unregister(EFI_HANDLE initrd_handle) { return EFI_SUCCESS; /* get the LoadFile2 protocol that we allocated earlier */ - err = BS->OpenProtocol( - initrd_handle, - MAKE_GUID_PTR(EFI_LOAD_FILE2_PROTOCOL), - (void **) &loader, - NULL, - NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); + err = BS->HandleProtocol(initrd_handle, MAKE_GUID_PTR(EFI_LOAD_FILE2_PROTOCOL), (void **) &loader); if (err != EFI_SUCCESS) return err; - /* close the handle */ - (void) BS->CloseProtocol(initrd_handle, MAKE_GUID_PTR(EFI_LOAD_FILE2_PROTOCOL), NULL, NULL); - /* uninstall all protocols thus destroying the handle */ err = BS->UninstallMultipleProtocolInterfaces( initrd_handle, MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), diff --git a/src/boot/efi/stub.c b/src/boot/efi/stub.c index c315a4d51ae..93a36414243 100644 --- a/src/boot/efi/stub.c +++ b/src/boot/efi/stub.c @@ -379,13 +379,7 @@ static EFI_STATUS run(EFI_HANDLE image) { uint64_t loader_features = 0; EFI_STATUS err; - err = BS->OpenProtocol( - image, - MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), - (void **) &loaded_image, - image, - NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); + err = BS->HandleProtocol(image, MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), (void **) &loaded_image); if (err != EFI_SUCCESS) return log_error_status(err, "Error getting a LoadedImageProtocol handle: %m");