]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemd-boot: Try harder not to add ourselves to the list
authorJan Janssen <medhefgo@web.de>
Sat, 6 Jan 2018 18:21:48 +0000 (19:21 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 7 Mar 2018 08:03:18 +0000 (09:03 +0100)
We don't need to check if we are adding ourselves to the list
if we know that it's the windows or EFI shell loaders.

If we are adding the EFI default loader, additionally try to
see if we can find the systemd-boot magic string and skip
this entry if we do.

src/boot/efi/boot.c

index e0438281e1a9746bbf186d7008dc310ab8fa3909..4768366c1ee8f8acf837a2e5bbc7d6c9f1e8174a 100644 (file)
@@ -1458,8 +1458,28 @@ static BOOLEAN config_entry_add_loader_auto(Config *config, EFI_HANDLE *device,
                 return FALSE;
 
         /* do not add an entry for ourselves */
-        if (loaded_image_path && StriCmp(loader, loaded_image_path) == 0)
-                return FALSE;
+        if (loaded_image_path) {
+                UINTN len;
+                CHAR8 *content;
+
+                if (StriCmp(loader, loaded_image_path) == 0)
+                        return FALSE;
+
+                /* look for systemd-boot magic string */
+                err = file_read(root_dir, loader, 0, 100*1024, &content, &len);
+                if (!EFI_ERROR(err)) {
+                        CHAR8 *start = content;
+                        CHAR8 *last = content + len - sizeof(magic) - 1;
+
+                        for (; start <= last; start++)
+                                if (start[0] == magic[0] && CompareMem(start, magic, sizeof(magic) - 1) == 0) {
+                                        FreePool(content);
+                                        return FALSE;
+                                }
+
+                        FreePool(content);
+                }
+        }
 
         /* check existence */
         err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, loader, EFI_FILE_MODE_READ, 0ULL);
@@ -1785,9 +1805,9 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
         config_sort_entries(&config);
 
         /* if we find some well-known loaders, add them to the end of the list */
-        config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
+        config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, NULL,
                                      L"auto-windows", 'w', L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
-        config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
+        config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, NULL,
                                      L"auto-efi-shell", 's', L"EFI Shell", L"\\shell" EFI_MACHINE_TYPE_NAME ".efi");
         config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
                                      L"auto-efi-default", '\0', L"EFI Default Loader", L"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME ".efi");