]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-boot: Check for existence of required pe sections
authorJan Janssen <medhefgo@web.de>
Tue, 26 Oct 2021 10:33:43 +0000 (12:33 +0200)
committerJan Janssen <medhefgo@web.de>
Thu, 4 Nov 2021 17:48:53 +0000 (18:48 +0100)
src/boot/efi/boot.c
src/boot/efi/stub.c

index b2b763beee1c54465df36829da0d66cf6ecf0e4f..296efdd4892a9de899c5b6cca8215e6767782af8 100644 (file)
@@ -2026,16 +2026,23 @@ static void config_entry_add_linux(
                 return;
 
         for (;;) {
+                enum {
+                        SECTION_CMDLINE,
+                        SECTION_OSREL,
+                        _SECTION_MAX,
+                };
+
+                static const CHAR8* const sections[_SECTION_MAX + 1] = {
+                        [SECTION_CMDLINE] = (const CHAR8 *) ".cmdline",
+                        [SECTION_OSREL]   = (const CHAR8 *) ".osrel",
+                        NULL,
+                };
+
                 _cleanup_freepool_ CHAR16 *os_name_pretty = NULL, *os_name = NULL, *os_id = NULL,
                         *os_version = NULL, *os_version_id = NULL, *os_build_id = NULL, *os_image_version = NULL;
                 _cleanup_freepool_ CHAR8 *content = NULL;
-                const CHAR8 *sections[] = {
-                        (CHAR8 *)".osrel",
-                        (CHAR8 *)".cmdline",
-                        NULL
-                };
-                UINTN offs[ELEMENTSOF(sections)-1] = {};
-                UINTN szs[ELEMENTSOF(sections)-1] = {};
+                UINTN offs[_SECTION_MAX] = {};
+                UINTN szs[_SECTION_MAX] = {};
                 CHAR8 *line;
                 UINTN pos = 0;
                 CHAR8 *key, *value;
@@ -2054,11 +2061,11 @@ static void config_entry_add_linux(
                         continue;
 
                 /* look for .osrel and .cmdline sections in the .efi binary */
-                err = pe_file_locate_sections(linux_dir, f->FileName, sections, offs, szs);
-                if (EFI_ERROR(err))
+                err = pe_file_locate_sections(linux_dir, f->FileName, (const CHAR8**) sections, offs, szs);
+                if (EFI_ERROR(err) || szs[SECTION_OSREL] == 0)
                         continue;
 
-                err = file_read(linux_dir, f->FileName, offs[0], szs[0], &content, NULL);
+                err = file_read(linux_dir, f->FileName, offs[SECTION_OSREL], szs[SECTION_OSREL], &content, NULL);
                 if (EFI_ERROR(err))
                         continue;
 
@@ -2122,21 +2129,24 @@ static void config_entry_add_linux(
                                         path,
                                         os_image_version ?: (os_version ?: (os_version_id ? : os_build_id)));
 
+                        config_entry_parse_tries(entry, L"\\EFI\\Linux", f->FileName, L".efi");
+
+                        if (szs[SECTION_CMDLINE] == 0)
+                                continue;
+
                         FreePool(content);
                         content = NULL;
 
                         /* read the embedded cmdline file */
-                        err = file_read(linux_dir, f->FileName, offs[1], szs[1], &content, NULL);
+                        err = file_read(linux_dir, f->FileName, offs[SECTION_CMDLINE], szs[SECTION_CMDLINE], &content, NULL);
                         if (!EFI_ERROR(err)) {
 
                                 /* chomp the newline */
-                                if (content[szs[1]-1] == '\n')
-                                        content[szs[1]-1] = '\0';
+                                if (content[szs[SECTION_CMDLINE] - 1] == '\n')
+                                        content[szs[SECTION_CMDLINE] - 1] = '\0';
 
                                 entry->options = stra_to_str(content);
                         }
-
-                        config_entry_parse_tries(entry, L"\\EFI\\Linux", f->FileName, L".efi");
                 }
         }
 }
index 256aa21827fa46723b84421a09cd5924c22dc814..52d26e7cd2b39fcf92be53b423571fbc5d4d61e9 100644 (file)
@@ -154,7 +154,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
                 _SECTION_MAX,
         };
 
-        const CHAR8* const sections[] = {
+        static const CHAR8* const sections[_SECTION_MAX + 1] = {
                 [SECTION_CMDLINE] = (const CHAR8*) ".cmdline",
                 [SECTION_LINUX]   = (const CHAR8*) ".linux",
                 [SECTION_INITRD]  = (const CHAR8*) ".initrd",
@@ -187,8 +187,11 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
                 return log_error_status_stall(err, L"Error getting a LoadedImageProtocol handle: %r", err);
 
         err = pe_memory_locate_sections(loaded_image->ImageBase, (const CHAR8**) sections, addrs, szs);
-        if (EFI_ERROR(err))
+        if (EFI_ERROR(err) || szs[SECTION_LINUX] == 0) {
+                if (!EFI_ERROR(err))
+                        err = EFI_NOT_FOUND;
                 return log_error_status_stall(err, L"Unable to locate embedded .linux section: %r", err);
+        }
 
         /* Show splash screen as early as possible */
         graphics_splash((const UINT8*) loaded_image->ImageBase + addrs[SECTION_SPLASH], szs[SECTION_SPLASH], NULL);