diff --git a/src/bootctl/bootctl-util.c b/src/bootctl/bootctl-util.c index 9bd85d6461..f6667a5a93 100644 --- a/src/bootctl/bootctl-util.c +++ b/src/bootctl/bootctl-util.c @@ -142,21 +142,14 @@ int get_file_version(int fd, char **ret) { * section, i.e. a string such as "#### LoaderInfo: systemd-boot 218 ####", and returns the inner * part, e.g. "systemd-boot 218". Does not reposition the file offset (as it uses pread()). */ - _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL; + _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL; _cleanup_free_ PeHeader *pe_header = NULL; - r = pe_load_headers(fd, &dos_header, &pe_header); + r = pe_load_headers_and_sections(fd, &pe_header, §ions); if (r == -EBADMSG) return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "EFI binary is not a valid PE file, assuming no version information."); if (r < 0) return r; - _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL; - r = pe_load_sections(fd, dos_header, pe_header, §ions); - if (r == -EBADMSG) - return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "Failed to load PE section table, assuming no version information."); - if (r < 0) - return r; - _cleanup_free_ char *sdmagic = NULL; r = pe_read_section_data_by_name( fd, diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 1d6fa82ef9..4615a8755e 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -1045,36 +1045,6 @@ static int boot_entry_load_unified( return 0; } -static int pe_load_headers_and_sections( - int fd, - const char *path, - IMAGE_SECTION_HEADER **ret_sections, - PeHeader **ret_pe_header) { - - _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL; - _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL; - _cleanup_free_ PeHeader *pe_header = NULL; - int r; - - assert(fd >= 0); - assert(path); - - r = pe_load_headers(fd, &dos_header, &pe_header); - if (r < 0) - return log_error_errno(r, "Failed to parse PE file '%s': %m", path); - - r = pe_load_sections(fd, dos_header, pe_header, §ions); - if (r < 0) - return log_error_errno(r, "Failed to parse PE sections of '%s': %m", path); - - if (ret_pe_header) - *ret_pe_header = TAKE_PTR(pe_header); - if (ret_sections) - *ret_sections = TAKE_PTR(sections); - - return 0; -} - static const IMAGE_SECTION_HEADER* pe_find_profile_section_table( const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections, @@ -1178,9 +1148,9 @@ int pe_find_uki_sections( assert(path); assert(profile != UINT_MAX); - r = pe_load_headers_and_sections(fd, path, §ions, &pe_header); + r = pe_load_headers_and_sections(fd, &pe_header, §ions); if (r < 0) - return r; + return log_error_errno(r, "Failed to parse PE file '%s': %m", path); if (!pe_is_uki(pe_header, sections)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Parsed PE file '%s' is not a UKI.", path); @@ -1270,9 +1240,9 @@ static int pe_find_addon_sections( assert(path); assert(ret_cmdline); - r = pe_load_headers_and_sections(fd, path, §ions, &pe_header); + r = pe_load_headers_and_sections(fd, &pe_header, §ions); if (r < 0) - return r; + return log_error_errno(r, "Failed to parse PE file '%s': %m", path); if (!pe_is_addon(pe_header, sections)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Parse PE file '%s' is not an add-on.", path); diff --git a/src/shared/kernel-image.c b/src/shared/kernel-image.c index 406c3e89cf..afb093d66b 100644 --- a/src/shared/kernel-image.c +++ b/src/shared/kernel-image.c @@ -124,7 +124,6 @@ int inspect_kernel_full( char **ret_pretty_name) { _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL; - _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL; KernelImageType t = KERNEL_IMAGE_TYPE_UNKNOWN; _cleanup_free_ PeHeader *pe_header = NULL; _cleanup_close_ int fd = -EBADF; @@ -136,18 +135,12 @@ int inspect_kernel_full( if (fd < 0) return log_debug_errno(fd, "Failed to open kernel image file '%s': %m", strna(filename)); - r = pe_load_headers(fd, &dos_header, &pe_header); + r = pe_load_headers_and_sections(fd, &pe_header, §ions); if (r == -EBADMSG) /* not a valid PE file */ goto not_uki; if (r < 0) return log_debug_errno(r, "Failed to parse kernel image file '%s': %m", strna(filename)); - r = pe_load_sections(fd, dos_header, pe_header, §ions); - if (r == -EBADMSG) /* not a valid PE file */ - goto not_uki; - if (r < 0) - return log_debug_errno(r, "Failed to load PE sections from kernel image file '%s': %m", strna(filename)); - if (pe_is_uki(pe_header, sections)) { r = inspect_uki(fd, pe_header, sections, ret_cmdline, ret_uname, ret_pretty_name); if (r < 0) diff --git a/src/shared/pe-binary.c b/src/shared/pe-binary.c index fc6576a875..7c93f5c087 100644 --- a/src/shared/pe-binary.c +++ b/src/shared/pe-binary.c @@ -220,6 +220,27 @@ int pe_load_sections( return 0; } +int pe_load_headers_and_sections(int fd, PeHeader **ret_pe_header, IMAGE_SECTION_HEADER **ret_sections) { + _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL; + _cleanup_free_ PeHeader *pe_header = NULL; + int r; + + assert(fd >= 0); + + r = pe_load_headers(fd, &dos_header, &pe_header); + if (r < 0) + return r; + + r = pe_load_sections(fd, dos_header, pe_header, ret_sections); + if (r < 0) + return r; + + if (ret_pe_header) + *ret_pe_header = TAKE_PTR(pe_header); + + return 0; +} + int pe_read_section_data( int fd, const IMAGE_SECTION_HEADER *section, diff --git a/src/shared/pe-binary.h b/src/shared/pe-binary.h index 4b5dc243fe..8835632415 100644 --- a/src/shared/pe-binary.h +++ b/src/shared/pe-binary.h @@ -141,6 +141,7 @@ const IMAGE_SECTION_HEADER* pe_section_table_find(const IMAGE_SECTION_HEADER *se int pe_load_headers(int fd, IMAGE_DOS_HEADER **ret_dos_header, PeHeader **ret_pe_header); int pe_load_sections(int fd, const IMAGE_DOS_HEADER *dos_header, const PeHeader *pe_header, IMAGE_SECTION_HEADER **ret_sections); +int pe_load_headers_and_sections(int fd, PeHeader **ret_pe_header, IMAGE_SECTION_HEADER **ret_sections); int pe_read_section_data(int fd, const IMAGE_SECTION_HEADER *section, size_t max_size, void **ret, size_t *ret_size); int pe_read_section_data_by_name(int fd, const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections, const char *name, size_t max_size, void **ret, size_t *ret_size); diff --git a/src/shared/reboot-util.c b/src/shared/reboot-util.c index 758b7a2600..2fdccf6524 100644 --- a/src/shared/reboot-util.c +++ b/src/shared/reboot-util.c @@ -381,17 +381,12 @@ static int extract_uki(const char *path, int fd, int *ret_kernel_fd, int *ret_in assert(fd >= 0); assert(ret_kernel_fd); - _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL; + _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL; _cleanup_free_ PeHeader *pe_header = NULL; - r = pe_load_headers(fd, &dos_header, &pe_header); + r = pe_load_headers_and_sections(fd, &pe_header, §ions); if (r < 0) return log_debug_errno(r, "Not a valid PE file '%s': %m", path); - _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL; - r = pe_load_sections(fd, dos_header, pe_header, §ions); - if (r < 0) - return log_debug_errno(r, "Failed to load PE sections from '%s': %m", path); - if (!pe_is_uki(pe_header, sections)) return 0; /* Not a UKI */