From: Christian Storm <[email protected]>

While the InitializeLib() call in efi_main(), the global
  ST (SystemTable),
  BS (BootServices), and
  RT (RuntimeServices)
pointers as well as the global
  EFI_HANDLE LibImageHandle
are initialized.
Hence, there's no need to pass along the this_image
handle as GNU EFI's already initialized LibImageHandle
can be used instead.

Signed-off-by: Christian Storm <[email protected]>
---
 bootguard.c         |  2 --
 include/bootguard.h |  2 --
 main.c              | 28 +++++++++++++++++++---------
 utils.c             |  8 +++++++-
 4 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/bootguard.c b/bootguard.c
index 55811dd..4e83399 100644
--- a/bootguard.c
+++ b/bootguard.c
@@ -15,8 +15,6 @@
 #include "bootguard.h"
 #include "utils.h"
 
-EFI_HANDLE this_image;
-
 VOLUME_DESC *volumes = NULL;
 UINTN volume_count = 128;
 CHAR16 *boot_medium_path;
diff --git a/include/bootguard.h b/include/bootguard.h
index 99d8fe4..2d484aa 100644
--- a/include/bootguard.h
+++ b/include/bootguard.h
@@ -33,8 +33,6 @@ typedef int BG_STATUS;
 
 #define ENV_FILE_NAME L"BGENV.DAT"
 
-extern EFI_HANDLE this_image;
-
 typedef struct _BG_LOADER_PARAMS {
        CHAR16 *payload_path;
        CHAR16 *payload_options;
diff --git a/main.c b/main.c
index 320e4dc..517ad13 100644
--- a/main.c
+++ b/main.c
@@ -27,6 +27,12 @@ extern const unsigned long init_array_start[];
 extern const unsigned long init_array_end[];
 extern CHAR16 *boot_medium_path;
 
+/*
+ * NOTE: LibImageHandle is a global initialized while efi_main()'s
+ *       InitializeLib() call.
+ */
+extern EFI_HANDLE LibImageHandle;
+
 static EFI_STATUS probe_watchdog(EFI_LOADED_IMAGE *loaded_image,
                                 EFI_PCI_IO *pci_io, UINT16 pci_vendor_id,
                                 UINT16 pci_device_id, UINTN timeout)
@@ -72,7 +78,7 @@ static EFI_STATUS scan_devices(EFI_LOADED_IMAGE 
*loaded_image, UINTN timeout)
 
                status = uefi_call_wrapper(BS->OpenProtocol, 6, device,
                                           &PciIoProtocol, (VOID **)&pci_io,
-                                          this_image, NULL,
+                                          LibImageHandle, NULL,
                                           EFI_OPEN_PROTOCOL_GET_PROTOCOL);
                if (EFI_ERROR(status)) {
                        error_exit(L"Could not open PciIoProtocol while "
@@ -93,13 +99,13 @@ static EFI_STATUS scan_devices(EFI_LOADED_IMAGE 
*loaded_image, UINTN timeout)
                                        value >> 16, timeout);
 
                uefi_call_wrapper(BS->CloseProtocol, 4, device, &PciIoProtocol,
-                                 this_image, NULL);
+                                 LibImageHandle, NULL);
        } while (status != EFI_SUCCESS && count > 0);
 
        return status;
 }
 
-EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
+EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
 {
        EFI_DEVICE_PATH *payload_dev_path;
        EFI_LOADED_IMAGE *loaded_image;
@@ -111,15 +117,19 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, 
EFI_SYSTEM_TABLE *system_table)
 
        ZeroMem(&bg_loader_params, sizeof(bg_loader_params));
 
-       this_image = image_handle;
-       InitializeLib(this_image, system_table);
+       /*
+        * NOTE: InitializeLib() initializes the global ST (SystemTable),
+        *       BS (BootServices), and RT (RuntimeServices) pointers as
+        *       well as LibImageHandle from efi_main()'s parameters.
+        */
+       InitializeLib(ImageHandle, SystemTable);
 
        PrintC(EFI_CYAN, L"\nEFI Boot Guard %s\n", L"" EFIBOOTGUARD_VERSION);
 
        status =
-           uefi_call_wrapper(BS->OpenProtocol, 6, this_image,
+           uefi_call_wrapper(BS->OpenProtocol, 6, LibImageHandle,
                              &LoadedImageProtocol, (VOID **)&loaded_image,
-                             this_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+                             LibImageHandle, NULL, 
EFI_OPEN_PROTOCOL_GET_PROTOCOL);
        if (EFI_ERROR(status)) {
                error_exit(L"Could not open LoadedImageProtocol to get image "
                           L"information.",
@@ -177,7 +187,7 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, 
EFI_SYSTEM_TABLE *system_table)
        }
 
        /* Load and start image */
-       status = uefi_call_wrapper(BS->LoadImage, 6, TRUE, this_image,
+       status = uefi_call_wrapper(BS->LoadImage, 6, TRUE, LibImageHandle,
                                   payload_dev_path, NULL, 0, &payload_handle);
        if (EFI_ERROR(status)) {
                error_exit(L"Could not load specified kernel image.", status);
@@ -189,7 +199,7 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, 
EFI_SYSTEM_TABLE *system_table)
        status =
            uefi_call_wrapper(BS->OpenProtocol, 6, payload_handle,
                              &LoadedImageProtocol, (VOID **)&loaded_image,
-                             this_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+                             LibImageHandle, NULL, 
EFI_OPEN_PROTOCOL_GET_PROTOCOL);
        if (EFI_ERROR(status)) {
                error_exit(L"Could not open LoadedImageProtocol to set kernel "
                           L"load options.",
diff --git a/utils.c b/utils.c
index 422356f..64918ae 100644
--- a/utils.c
+++ b/utils.c
@@ -16,6 +16,12 @@
 #include <bootguard.h>
 #include <utils.h>
 
+/*
+ * NOTE: LibImageHandle is a global initialized while efi_main()'s
+ *       InitializeLib() call.
+ */
+extern EFI_HANDLE LibImageHandle;
+
 VOID PrintC(const UINT8 color, const CHAR16 *fmt, ...)
 {
        INT32 attr = ST->ConOut->Mode->Attribute;
@@ -59,7 +65,7 @@ void __attribute__((noreturn)) error_exit(CHAR16 *message, 
EFI_STATUS status)
 {
        ERROR(L"%s ( %r )\n", message, status);
        uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
-       uefi_call_wrapper(BS->Exit, 4, this_image, status, 0, NULL);
+       uefi_call_wrapper(BS->Exit, 4, LibImageHandle, status, 0, NULL);
        __builtin_unreachable();
 }
 
-- 
2.30.0

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/20210108144342.24785-6-christian.storm%40siemens.com.

Reply via email to