]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Unify protocol opening
authorJan Janssen <medhefgo@web.de>
Mon, 12 Jun 2023 13:12:05 +0000 (15:12 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 12 Jun 2023 20:12:11 +0000 (21:12 +0100)
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
src/boot/efi/initrd.c
src/boot/efi/stub.c

index 65ad6f751a7bd081b00f7e5d845eee5bc42f3886..cda6f5642681a38e89070058120988c806c41bed 100644 (file)
@@ -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);
index e8af9995086ea78f15bb9aace04ee8d9317fccc6..527b05f5dbd39d37a6174412c3e8b21baae429ba 100644 (file)
@@ -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),
index c315a4d51aef17d8792953871eddb3a9da7557b3..93a364142433e6874febdf4f4c0d49f468509997 100644 (file)
@@ -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");