| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "env-file.h" |
| 5 |
#include "fd-util.h" |
| 6 |
#include "fs-util.h" |
| 7 |
#include "kernel-image.h" |
| 8 |
#include "log.h" |
| 9 |
#include "pe-binary.h" |
| 10 |
#include "string-table.h" |
| 11 |
#include "string-util.h" |
| 12 |
|
| 13 |
#define PE_SECTION_READ_MAX (16U*1024U) |
| 14 |
|
| 15 |
static const char * const kernel_image_type_table[_KERNEL_IMAGE_TYPE_MAX] = { |
| 16 |
[KERNEL_IMAGE_TYPE_UNKNOWN] = "unknown", |
| 17 |
[KERNEL_IMAGE_TYPE_UKI] = "uki", |
| 18 |
[KERNEL_IMAGE_TYPE_ADDON] = "addon", |
| 19 |
[KERNEL_IMAGE_TYPE_PE] = "pe", |
| 20 |
}; |
| 21 |
|
| 22 |
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_image_type, KernelImageType); |
| 23 |
|
| 24 |
static int uki_read_pretty_name( |
| 25 |
int fd, |
| 26 |
const PeHeader *pe_header, |
| 27 |
const IMAGE_SECTION_HEADER *sections, |
| 28 |
char **ret) { |
| 29 |
|
| 30 |
_cleanup_free_ char *pname = NULL, *name = NULL; |
| 31 |
_cleanup_free_ void *osrel = NULL; |
| 32 |
size_t osrel_size; |
| 33 |
int r; |
| 34 |
|
| 35 |
assert(fd >= 0); |
| 36 |
assert(pe_header); |
| 37 |
assert(sections || le16toh(pe_header->pe.NumberOfSections) == 0); |
| 38 |
assert(ret); |
| 39 |
|
| 40 |
r = pe_read_section_data_by_name( |
| 41 |
fd, |
| 42 |
pe_header, |
| 43 |
sections, |
| 44 |
".osrel", |
| 45 |
/* max_size= */ PE_SECTION_READ_MAX, |
| 46 |
&osrel, |
| 47 |
&osrel_size); |
| 48 |
if (r == -ENXIO) { /* Section not found */ |
| 49 |
*ret = NULL; |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
r = parse_env_data( |
| 54 |
osrel, osrel_size, ".osrel", |
| 55 |
"PRETTY_NAME", &pname, |
| 56 |
"NAME", &name); |
| 57 |
if (r < 0) |
| 58 |
return log_debug_errno(r, "Failed to parse embedded os-release file: %m"); |
| 59 |
|
| 60 |
/* follow the same logic as os_release_pretty_name() */ |
| 61 |
if (!isempty(pname)) |
| 62 |
*ret = TAKE_PTR(pname); |
| 63 |
else if (!isempty(name)) |
| 64 |
*ret = TAKE_PTR(name); |
| 65 |
else { |
| 66 |
char *n = strdup("Linux"); |
| 67 |
if (!n) |
| 68 |
return -ENOMEM; |
| 69 |
|
| 70 |
*ret = n; |
| 71 |
} |
| 72 |
|
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
static int inspect_uki( |
| 77 |
int fd, |
| 78 |
const PeHeader *pe_header, |
| 79 |
const IMAGE_SECTION_HEADER *sections, |
| 80 |
char **ret_cmdline, |
| 81 |
char **ret_uname, |
| 82 |
char **ret_pretty_name) { |
| 83 |
|
| 84 |
_cleanup_free_ char *cmdline = NULL, *uname = NULL, *pname = NULL; |
| 85 |
int r; |
| 86 |
|
| 87 |
assert(fd >= 0); |
| 88 |
assert(sections || le16toh(pe_header->pe.NumberOfSections) == 0); |
| 89 |
|
| 90 |
if (ret_cmdline) { |
| 91 |
r = pe_read_section_data_by_name(fd, pe_header, sections, ".cmdline", PE_SECTION_READ_MAX, (void**) &cmdline, NULL); |
| 92 |
if (r < 0 && r != -ENXIO) /* If the section doesn't exist, that's fine */ |
| 93 |
return r; |
| 94 |
} |
| 95 |
|
| 96 |
if (ret_uname) { |
| 97 |
r = pe_read_section_data_by_name(fd, pe_header, sections, ".uname", PE_SECTION_READ_MAX, (void**) &uname, NULL); |
| 98 |
if (r < 0 && r != -ENXIO) /* If the section doesn't exist, that's fine */ |
| 99 |
return r; |
| 100 |
} |
| 101 |
|
| 102 |
if (ret_pretty_name) { |
| 103 |
r = uki_read_pretty_name(fd, pe_header, sections, &pname); |
| 104 |
if (r < 0) |
| 105 |
return r; |
| 106 |
} |
| 107 |
|
| 108 |
if (ret_cmdline) |
| 109 |
*ret_cmdline = TAKE_PTR(cmdline); |
| 110 |
if (ret_uname) |
| 111 |
*ret_uname = TAKE_PTR(uname); |
| 112 |
if (ret_pretty_name) |
| 113 |
*ret_pretty_name = TAKE_PTR(pname); |
| 114 |
|
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
int inspect_kernel_full( |
| 119 |
int dir_fd, |
| 120 |
const char *filename, |
| 121 |
KernelImageType *ret_type, |
| 122 |
char **ret_cmdline, |
| 123 |
char **ret_uname, |
| 124 |
char **ret_pretty_name) { |
| 125 |
|
| 126 |
_cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL; |
| 127 |
_cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL; |
| 128 |
KernelImageType t = KERNEL_IMAGE_TYPE_UNKNOWN; |
| 129 |
_cleanup_free_ PeHeader *pe_header = NULL; |
| 130 |
_cleanup_close_ int fd = -EBADF; |
| 131 |
int r; |
| 132 |
|
| 133 |
assert(wildcard_fd_is_valid(dir_fd)); |
| 134 |
|
| 135 |
fd = xopenat(dir_fd, filename, O_RDONLY|O_CLOEXEC); |
| 136 |
if (fd < 0) |
| 137 |
return log_debug_errno(fd, "Failed to open kernel image file '%s': %m", strna(filename)); |
| 138 |
|
| 139 |
r = pe_load_headers(fd, &dos_header, &pe_header); |
| 140 |
if (r == -EBADMSG) /* not a valid PE file */ |
| 141 |
goto not_uki; |
| 142 |
if (r < 0) |
| 143 |
return log_debug_errno(r, "Failed to parse kernel image file '%s': %m", strna(filename)); |
| 144 |
|
| 145 |
r = pe_load_sections(fd, dos_header, pe_header, §ions); |
| 146 |
if (r == -EBADMSG) /* not a valid PE file */ |
| 147 |
goto not_uki; |
| 148 |
if (r < 0) |
| 149 |
return log_debug_errno(r, "Failed to load PE sections from kernel image file '%s': %m", strna(filename)); |
| 150 |
|
| 151 |
if (pe_is_uki(pe_header, sections)) { |
| 152 |
r = inspect_uki(fd, pe_header, sections, ret_cmdline, ret_uname, ret_pretty_name); |
| 153 |
if (r < 0) |
| 154 |
return r; |
| 155 |
|
| 156 |
t = KERNEL_IMAGE_TYPE_UKI; |
| 157 |
goto done; |
| 158 |
} else if (pe_is_addon(pe_header, sections)) { |
| 159 |
r = inspect_uki(fd, pe_header, sections, ret_cmdline, ret_uname, /* ret_pretty_name= */ NULL); |
| 160 |
if (r < 0) |
| 161 |
return r; |
| 162 |
|
| 163 |
if (ret_pretty_name) |
| 164 |
*ret_pretty_name = NULL; |
| 165 |
|
| 166 |
t = KERNEL_IMAGE_TYPE_ADDON; |
| 167 |
goto done; |
| 168 |
} else |
| 169 |
t = KERNEL_IMAGE_TYPE_PE; |
| 170 |
|
| 171 |
not_uki: |
| 172 |
if (ret_cmdline) |
| 173 |
*ret_cmdline = NULL; |
| 174 |
if (ret_uname) |
| 175 |
*ret_uname = NULL; |
| 176 |
if (ret_pretty_name) |
| 177 |
*ret_pretty_name = NULL; |
| 178 |
|
| 179 |
done: |
| 180 |
if (ret_type) |
| 181 |
*ret_type = t; |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |