#endif
/* magic string to find in the binary image */
-static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot " GIT_VERSION " ####";
+static const char _used_ _section_(".sdmagic") magic[] = "#### LoaderInfo: systemd-boot " GIT_VERSION " ####";
enum loader_type {
LOADER_UNDEFINED,
return entry;
}
+static BOOLEAN is_sd_boot(EFI_FILE *root_dir, const CHAR16 *loader_path) {
+ EFI_STATUS err;
+ const CHAR8 *sections[] = {
+ (CHAR8 *)".sdmagic",
+ NULL
+ };
+ UINTN offset = 0, size = 0, read;
+ _cleanup_freepool_ CHAR8 *content = NULL;
+
+ assert(root_dir);
+ assert(loader_path);
+
+ err = pe_file_locate_sections(root_dir, loader_path, sections, &offset, &size);
+ if (EFI_ERROR(err) || size != sizeof(magic))
+ return FALSE;
+
+ err = file_read(root_dir, loader_path, offset, size, &content, &read);
+ if (EFI_ERROR(err) || size != read)
+ return FALSE;
+
+ return CompareMem(content, magic, sizeof(magic)) == 0;
+}
+
static BOOLEAN config_entry_add_loader_auto(
Config *config,
EFI_HANDLE *device,
assert(root_dir);
assert(id);
assert(title);
- assert(loader);
+ assert(loader || loaded_image_path);
if (!config->auto_entries)
return FALSE;
- /* do not add an entry for ourselves */
if (loaded_image_path) {
- UINTN len;
- _cleanup_freepool_ CHAR8 *content = NULL;
-
- if (StriCmp(loader, loaded_image_path) == 0)
+ loader = L"\\EFI\\BOOT\\BOOT" EFI_MACHINE_TYPE_NAME ".efi";
+
+ /* We are trying to add the default EFI loader here,
+ * but we do not want to do that if that would be us.
+ *
+ * If the default loader is not us, it might be shim. It would
+ * chainload GRUBX64.EFI in that case, which might be us.*/
+ if (StriCmp(loader, loaded_image_path) == 0 ||
+ is_sd_boot(root_dir, loader) ||
+ is_sd_boot(root_dir, L"\\EFI\\BOOT\\GRUB" EFI_MACHINE_TYPE_NAME L".EFI"))
return FALSE;
-
- /* look for systemd-boot magic string */
- err = file_read(root_dir, loader, 0, 100*1024, &content, &len);
- if (!EFI_ERROR(err))
- for (CHAR8 *start = content; start <= content + len - sizeof(magic) - 1; start++)
- if (start[0] == magic[0] && CompareMem(start, magic, sizeof(magic) - 1) == 0)
- return FALSE;
}
/* check existence */
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");
+ L"auto-efi-default", '\0', L"EFI Default Loader", NULL);
config_entry_add_osx(&config);
if (config.auto_firmware && efivar_get_uint64_le(EFI_GLOBAL_GUID, L"OsIndicationsSupported", &osind) == EFI_SUCCESS) {