]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
7e87c7d9 ZJS |
2 | |
3 | #include <stdio.h> | |
af918182 | 4 | #include <linux/magic.h> |
ca78ad1d | 5 | #include <unistd.h> |
7e87c7d9 | 6 | |
cedb9eec | 7 | #include "sd-device.h" |
dccca82b LP |
8 | #include "sd-id128.h" |
9 | ||
7e87c7d9 | 10 | #include "alloc-util.h" |
af918182 | 11 | #include "blkid-util.h" |
7e87c7d9 ZJS |
12 | #include "bootspec.h" |
13 | #include "conf-files.h" | |
14 | #include "def.h" | |
c67f84b0 | 15 | #include "device-nodes.h" |
5e146a75 | 16 | #include "dirent-util.h" |
7e87c7d9 | 17 | #include "efivars.h" |
0bb2f0f1 | 18 | #include "efi-loader.h" |
5e146a75 | 19 | #include "env-file.h" |
8cbb7d87 | 20 | #include "env-util.h" |
7e87c7d9 ZJS |
21 | #include "fd-util.h" |
22 | #include "fileio.h" | |
af918182 | 23 | #include "parse-util.h" |
cc7a0bfa | 24 | #include "path-util.h" |
5e146a75 | 25 | #include "pe-header.h" |
760877e9 | 26 | #include "sort-util.h" |
af918182 | 27 | #include "stat-util.h" |
93f14ce2 | 28 | #include "string-table.h" |
7e87c7d9 ZJS |
29 | #include "string-util.h" |
30 | #include "strv.h" | |
5e146a75 | 31 | #include "unaligned.h" |
ca78ad1d | 32 | #include "util.h" |
af918182 | 33 | #include "virt.h" |
7e87c7d9 | 34 | |
0de2e1fd | 35 | static void boot_entry_free(BootEntry *entry) { |
4fe2ba0e | 36 | assert(entry); |
7e87c7d9 | 37 | |
12580bc3 | 38 | free(entry->id); |
15b82eec | 39 | free(entry->id_old); |
2d3bfb69 | 40 | free(entry->path); |
43b736a8 | 41 | free(entry->root); |
7e87c7d9 | 42 | free(entry->title); |
64f05708 | 43 | free(entry->show_title); |
7e87c7d9 ZJS |
44 | free(entry->version); |
45 | free(entry->machine_id); | |
46 | free(entry->architecture); | |
47 | strv_free(entry->options); | |
48 | free(entry->kernel); | |
49 | free(entry->efi); | |
50 | strv_free(entry->initrd); | |
51 | free(entry->device_tree); | |
52 | } | |
53 | ||
43b736a8 LP |
54 | static int boot_entry_load( |
55 | const char *root, | |
56 | const char *path, | |
57 | BootEntry *entry) { | |
58 | ||
93f14ce2 LP |
59 | _cleanup_(boot_entry_free) BootEntry tmp = { |
60 | .type = BOOT_ENTRY_CONF, | |
61 | }; | |
62 | ||
7e87c7d9 ZJS |
63 | _cleanup_fclose_ FILE *f = NULL; |
64 | unsigned line = 1; | |
263195c6 | 65 | char *b, *c; |
7e87c7d9 ZJS |
66 | int r; |
67 | ||
43b736a8 | 68 | assert(root); |
4fe2ba0e LP |
69 | assert(path); |
70 | assert(entry); | |
71 | ||
263195c6 | 72 | c = endswith_no_case(path, ".conf"); |
46bba8a5 | 73 | if (!c) |
eed7210a | 74 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry file suffix: %s", path); |
7e87c7d9 | 75 | |
263195c6 | 76 | b = basename(path); |
ae474efc | 77 | tmp.id = strdup(b); |
15b82eec SM |
78 | tmp.id_old = strndup(b, c - b); |
79 | if (!tmp.id || !tmp.id_old) | |
7e87c7d9 ZJS |
80 | return log_oom(); |
81 | ||
eed7210a | 82 | if (!efi_loader_entry_name_valid(tmp.id)) |
dfc22cb4 | 83 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", tmp.id); |
eed7210a | 84 | |
2d3bfb69 ZJS |
85 | tmp.path = strdup(path); |
86 | if (!tmp.path) | |
87 | return log_oom(); | |
88 | ||
43b736a8 LP |
89 | tmp.root = strdup(root); |
90 | if (!tmp.root) | |
91 | return log_oom(); | |
92 | ||
263195c6 YW |
93 | f = fopen(path, "re"); |
94 | if (!f) | |
95 | return log_error_errno(errno, "Failed to open \"%s\": %m", path); | |
96 | ||
7e87c7d9 | 97 | for (;;) { |
f99fdc3e YW |
98 | _cleanup_free_ char *buf = NULL, *field = NULL; |
99 | const char *p; | |
7e87c7d9 ZJS |
100 | |
101 | r = read_line(f, LONG_LINE_MAX, &buf); | |
102 | if (r == 0) | |
103 | break; | |
104 | if (r == -ENOBUFS) | |
105 | return log_error_errno(r, "%s:%u: Line too long", path, line); | |
106 | if (r < 0) | |
107 | return log_error_errno(r, "%s:%u: Error while reading: %m", path, line); | |
108 | ||
109 | line++; | |
110 | ||
111 | if (IN_SET(*strstrip(buf), '#', '\0')) | |
112 | continue; | |
113 | ||
f99fdc3e YW |
114 | p = buf; |
115 | r = extract_first_word(&p, &field, " \t", 0); | |
116 | if (r < 0) { | |
117 | log_error_errno(r, "Failed to parse config file %s line %u: %m", path, line); | |
118 | continue; | |
119 | } | |
120 | if (r == 0) { | |
7e87c7d9 ZJS |
121 | log_warning("%s:%u: Bad syntax", path, line); |
122 | continue; | |
123 | } | |
7e87c7d9 | 124 | |
f99fdc3e | 125 | if (streq(field, "title")) |
7e87c7d9 | 126 | r = free_and_strdup(&tmp.title, p); |
f99fdc3e | 127 | else if (streq(field, "version")) |
7e87c7d9 | 128 | r = free_and_strdup(&tmp.version, p); |
f99fdc3e | 129 | else if (streq(field, "machine-id")) |
7e87c7d9 | 130 | r = free_and_strdup(&tmp.machine_id, p); |
f99fdc3e | 131 | else if (streq(field, "architecture")) |
7e87c7d9 | 132 | r = free_and_strdup(&tmp.architecture, p); |
f99fdc3e | 133 | else if (streq(field, "options")) |
7e87c7d9 | 134 | r = strv_extend(&tmp.options, p); |
f99fdc3e | 135 | else if (streq(field, "linux")) |
7e87c7d9 | 136 | r = free_and_strdup(&tmp.kernel, p); |
f99fdc3e | 137 | else if (streq(field, "efi")) |
7e87c7d9 | 138 | r = free_and_strdup(&tmp.efi, p); |
f99fdc3e | 139 | else if (streq(field, "initrd")) |
7e87c7d9 | 140 | r = strv_extend(&tmp.initrd, p); |
f99fdc3e | 141 | else if (streq(field, "devicetree")) |
7e87c7d9 ZJS |
142 | r = free_and_strdup(&tmp.device_tree, p); |
143 | else { | |
feb41f1f | 144 | log_notice("%s:%u: Unknown line \"%s\", ignoring.", path, line, field); |
7e87c7d9 ZJS |
145 | continue; |
146 | } | |
147 | if (r < 0) | |
148 | return log_error_errno(r, "%s:%u: Error while reading: %m", path, line); | |
149 | } | |
150 | ||
151 | *entry = tmp; | |
152 | tmp = (BootEntry) {}; | |
153 | return 0; | |
154 | } | |
155 | ||
156 | void boot_config_free(BootConfig *config) { | |
da6053d0 | 157 | size_t i; |
7e87c7d9 | 158 | |
4fe2ba0e LP |
159 | assert(config); |
160 | ||
7e87c7d9 ZJS |
161 | free(config->default_pattern); |
162 | free(config->timeout); | |
163 | free(config->editor); | |
c1d4e298 JJ |
164 | free(config->auto_entries); |
165 | free(config->auto_firmware); | |
72263375 | 166 | free(config->console_mode); |
fe5a698f | 167 | free(config->random_seed_mode); |
7e87c7d9 ZJS |
168 | |
169 | free(config->entry_oneshot); | |
170 | free(config->entry_default); | |
171 | ||
172 | for (i = 0; i < config->n_entries; i++) | |
173 | boot_entry_free(config->entries + i); | |
174 | free(config->entries); | |
175 | } | |
176 | ||
0de2e1fd | 177 | static int boot_loader_read_conf(const char *path, BootConfig *config) { |
7e87c7d9 ZJS |
178 | _cleanup_fclose_ FILE *f = NULL; |
179 | unsigned line = 1; | |
180 | int r; | |
181 | ||
4fe2ba0e LP |
182 | assert(path); |
183 | assert(config); | |
184 | ||
7e87c7d9 | 185 | f = fopen(path, "re"); |
f91ed3dc LP |
186 | if (!f) { |
187 | if (errno == ENOENT) | |
188 | return 0; | |
189 | ||
7e87c7d9 | 190 | return log_error_errno(errno, "Failed to open \"%s\": %m", path); |
f91ed3dc | 191 | } |
7e87c7d9 ZJS |
192 | |
193 | for (;;) { | |
f99fdc3e YW |
194 | _cleanup_free_ char *buf = NULL, *field = NULL; |
195 | const char *p; | |
7e87c7d9 ZJS |
196 | |
197 | r = read_line(f, LONG_LINE_MAX, &buf); | |
198 | if (r == 0) | |
199 | break; | |
200 | if (r == -ENOBUFS) | |
201 | return log_error_errno(r, "%s:%u: Line too long", path, line); | |
202 | if (r < 0) | |
203 | return log_error_errno(r, "%s:%u: Error while reading: %m", path, line); | |
204 | ||
205 | line++; | |
206 | ||
207 | if (IN_SET(*strstrip(buf), '#', '\0')) | |
208 | continue; | |
209 | ||
f99fdc3e YW |
210 | p = buf; |
211 | r = extract_first_word(&p, &field, " \t", 0); | |
212 | if (r < 0) { | |
213 | log_error_errno(r, "Failed to parse config file %s line %u: %m", path, line); | |
214 | continue; | |
215 | } | |
216 | if (r == 0) { | |
7e87c7d9 ZJS |
217 | log_warning("%s:%u: Bad syntax", path, line); |
218 | continue; | |
219 | } | |
7e87c7d9 | 220 | |
f99fdc3e | 221 | if (streq(field, "default")) |
7e87c7d9 | 222 | r = free_and_strdup(&config->default_pattern, p); |
f99fdc3e | 223 | else if (streq(field, "timeout")) |
7e87c7d9 | 224 | r = free_and_strdup(&config->timeout, p); |
f99fdc3e | 225 | else if (streq(field, "editor")) |
7e87c7d9 | 226 | r = free_and_strdup(&config->editor, p); |
790f84eb | 227 | else if (streq(field, "auto-entries")) |
c1d4e298 | 228 | r = free_and_strdup(&config->auto_entries, p); |
790f84eb | 229 | else if (streq(field, "auto-firmware")) |
c1d4e298 | 230 | r = free_and_strdup(&config->auto_firmware, p); |
790f84eb | 231 | else if (streq(field, "console-mode")) |
d37b0737 | 232 | r = free_and_strdup(&config->console_mode, p); |
fe5a698f YW |
233 | else if (streq(field, "random-seed-mode")) |
234 | r = free_and_strdup(&config->random_seed_mode, p); | |
7e87c7d9 | 235 | else { |
feb41f1f | 236 | log_notice("%s:%u: Unknown line \"%s\", ignoring.", path, line, field); |
7e87c7d9 ZJS |
237 | continue; |
238 | } | |
239 | if (r < 0) | |
240 | return log_error_errno(r, "%s:%u: Error while reading: %m", path, line); | |
241 | } | |
242 | ||
f91ed3dc | 243 | return 1; |
7e87c7d9 ZJS |
244 | } |
245 | ||
93bab288 | 246 | static int boot_entry_compare(const BootEntry *a, const BootEntry *b) { |
8087644a | 247 | return strverscmp_improved(a->id, b->id); |
7e87c7d9 ZJS |
248 | } |
249 | ||
43b736a8 LP |
250 | static int boot_entries_find( |
251 | const char *root, | |
252 | const char *dir, | |
a2f8664e LP |
253 | BootEntry **entries, |
254 | size_t *n_entries) { | |
43b736a8 | 255 | |
7e87c7d9 ZJS |
256 | _cleanup_strv_free_ char **files = NULL; |
257 | char **f; | |
258 | int r; | |
7e87c7d9 | 259 | |
43b736a8 | 260 | assert(root); |
4fe2ba0e | 261 | assert(dir); |
a2f8664e LP |
262 | assert(entries); |
263 | assert(n_entries); | |
4fe2ba0e | 264 | |
36b12282 | 265 | r = conf_files_list(&files, ".conf", NULL, 0, dir); |
7e87c7d9 ZJS |
266 | if (r < 0) |
267 | return log_error_errno(r, "Failed to list files in \"%s\": %m", dir); | |
268 | ||
269 | STRV_FOREACH(f, files) { | |
319a4f4b | 270 | if (!GREEDY_REALLOC0(*entries, *n_entries + 1)) |
7e87c7d9 ZJS |
271 | return log_oom(); |
272 | ||
a2f8664e | 273 | r = boot_entry_load(root, *f, *entries + *n_entries); |
7e87c7d9 ZJS |
274 | if (r < 0) |
275 | continue; | |
276 | ||
a2f8664e | 277 | (*n_entries) ++; |
7e87c7d9 ZJS |
278 | } |
279 | ||
7e87c7d9 ZJS |
280 | return 0; |
281 | } | |
282 | ||
5e146a75 LP |
283 | static int boot_entry_load_unified( |
284 | const char *root, | |
285 | const char *path, | |
286 | const char *osrelease, | |
287 | const char *cmdline, | |
288 | BootEntry *ret) { | |
289 | ||
15b82eec | 290 | _cleanup_free_ char *os_pretty_name = NULL, *os_id = NULL, *version_id = NULL, *build_id = NULL; |
93f14ce2 LP |
291 | _cleanup_(boot_entry_free) BootEntry tmp = { |
292 | .type = BOOT_ENTRY_UNIFIED, | |
293 | }; | |
5e146a75 LP |
294 | _cleanup_fclose_ FILE *f = NULL; |
295 | const char *k; | |
ae474efc | 296 | char *b; |
5e146a75 LP |
297 | int r; |
298 | ||
299 | assert(root); | |
300 | assert(path); | |
301 | assert(osrelease); | |
302 | ||
303 | k = path_startswith(path, root); | |
304 | if (!k) | |
305 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not below root: %s", path); | |
306 | ||
673a1e6f | 307 | f = fmemopen_unlocked((void*) osrelease, strlen(osrelease), "r"); |
5e146a75 LP |
308 | if (!f) |
309 | return log_error_errno(errno, "Failed to open os-release buffer: %m"); | |
310 | ||
15b82eec SM |
311 | r = parse_env_file(f, "os-release", |
312 | "PRETTY_NAME", &os_pretty_name, | |
313 | "ID", &os_id, | |
314 | "VERSION_ID", &version_id, | |
315 | "BUILD_ID", &build_id); | |
5e146a75 LP |
316 | if (r < 0) |
317 | return log_error_errno(r, "Failed to parse os-release data from unified kernel image %s: %m", path); | |
318 | ||
15b82eec | 319 | if (!os_pretty_name || !os_id || !(version_id || build_id)) |
5e146a75 LP |
320 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Missing fields in os-release data from unified kernel image %s, refusing.", path); |
321 | ||
ae474efc SM |
322 | b = basename(path); |
323 | tmp.id = strdup(b); | |
15b82eec SM |
324 | tmp.id_old = strjoin(os_id, "-", version_id ?: build_id); |
325 | if (!tmp.id || !tmp.id_old) | |
5e146a75 LP |
326 | return log_oom(); |
327 | ||
eed7210a | 328 | if (!efi_loader_entry_name_valid(tmp.id)) |
dfc22cb4 | 329 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", tmp.id); |
eed7210a | 330 | |
5e146a75 LP |
331 | tmp.path = strdup(path); |
332 | if (!tmp.path) | |
333 | return log_oom(); | |
334 | ||
335 | tmp.root = strdup(root); | |
336 | if (!tmp.root) | |
337 | return log_oom(); | |
338 | ||
339 | tmp.kernel = strdup(skip_leading_chars(k, "/")); | |
340 | if (!tmp.kernel) | |
341 | return log_oom(); | |
342 | ||
343 | tmp.options = strv_new(skip_leading_chars(cmdline, WHITESPACE)); | |
344 | if (!tmp.options) | |
345 | return log_oom(); | |
346 | ||
347 | delete_trailing_chars(tmp.options[0], WHITESPACE); | |
348 | ||
349 | tmp.title = TAKE_PTR(os_pretty_name); | |
350 | ||
351 | *ret = tmp; | |
352 | tmp = (BootEntry) {}; | |
353 | return 0; | |
354 | } | |
355 | ||
356 | /* Maximum PE section we are willing to load (Note that sections we are not interested in may be larger, but | |
357 | * the ones we do care about and we are willing to load into memory have this size limit.) */ | |
358 | #define PE_SECTION_SIZE_MAX (4U*1024U*1024U) | |
359 | ||
360 | static int find_sections( | |
361 | int fd, | |
362 | char **ret_osrelease, | |
363 | char **ret_cmdline) { | |
364 | ||
365 | _cleanup_free_ struct PeSectionHeader *sections = NULL; | |
366 | _cleanup_free_ char *osrelease = NULL, *cmdline = NULL; | |
367 | size_t i, n_sections; | |
368 | struct DosFileHeader dos; | |
369 | struct PeHeader pe; | |
370 | uint64_t start; | |
371 | ssize_t n; | |
372 | ||
373 | n = pread(fd, &dos, sizeof(dos), 0); | |
374 | if (n < 0) | |
375 | return log_error_errno(errno, "Failed read DOS header: %m"); | |
376 | if (n != sizeof(dos)) | |
377 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading DOS header, refusing."); | |
378 | ||
379 | if (dos.Magic[0] != 'M' || dos.Magic[1] != 'Z') | |
380 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "DOS executable magic missing, refusing."); | |
381 | ||
382 | start = unaligned_read_le32(&dos.ExeHeader); | |
383 | n = pread(fd, &pe, sizeof(pe), start); | |
384 | if (n < 0) | |
385 | return log_error_errno(errno, "Failed to read PE header: %m"); | |
386 | if (n != sizeof(pe)) | |
387 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading PE header, refusing."); | |
388 | ||
389 | if (pe.Magic[0] != 'P' || pe.Magic[1] != 'E' || pe.Magic[2] != 0 || pe.Magic[3] != 0) | |
390 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PE executable magic missing, refusing."); | |
391 | ||
392 | n_sections = unaligned_read_le16(&pe.FileHeader.NumberOfSections); | |
393 | if (n_sections > 96) | |
394 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PE header has too many sections, refusing."); | |
395 | ||
396 | sections = new(struct PeSectionHeader, n_sections); | |
397 | if (!sections) | |
398 | return log_oom(); | |
399 | ||
400 | n = pread(fd, sections, | |
401 | n_sections * sizeof(struct PeSectionHeader), | |
402 | start + sizeof(pe) + unaligned_read_le16(&pe.FileHeader.SizeOfOptionalHeader)); | |
403 | if (n < 0) | |
404 | return log_error_errno(errno, "Failed to read section data: %m"); | |
405 | if ((size_t) n != n_sections * sizeof(struct PeSectionHeader)) | |
406 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading sections, refusing."); | |
407 | ||
408 | for (i = 0; i < n_sections; i++) { | |
409 | _cleanup_free_ char *k = NULL; | |
410 | uint32_t offset, size; | |
411 | char **b; | |
412 | ||
413 | if (strneq((char*) sections[i].Name, ".osrel", sizeof(sections[i].Name))) | |
414 | b = &osrelease; | |
415 | else if (strneq((char*) sections[i].Name, ".cmdline", sizeof(sections[i].Name))) | |
416 | b = &cmdline; | |
417 | else | |
418 | continue; | |
419 | ||
420 | if (*b) | |
421 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Duplicate section %s, refusing.", sections[i].Name); | |
422 | ||
423 | offset = unaligned_read_le32(§ions[i].PointerToRawData); | |
424 | size = unaligned_read_le32(§ions[i].VirtualSize); | |
425 | ||
426 | if (size > PE_SECTION_SIZE_MAX) | |
427 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Section %s too large, refusing.", sections[i].Name); | |
428 | ||
429 | k = new(char, size+1); | |
430 | if (!k) | |
431 | return log_oom(); | |
432 | ||
433 | n = pread(fd, k, size, offset); | |
434 | if (n < 0) | |
435 | return log_error_errno(errno, "Failed to read section payload: %m"); | |
a75fcef8 | 436 | if ((size_t) n != size) |
5e146a75 LP |
437 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading section payload, refusing:"); |
438 | ||
439 | /* Allow one trailing NUL byte, but nothing more. */ | |
440 | if (size > 0 && memchr(k, 0, size - 1)) | |
441 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Section contains embedded NUL byte: %m"); | |
442 | ||
443 | k[size] = 0; | |
444 | *b = TAKE_PTR(k); | |
445 | } | |
446 | ||
447 | if (!osrelease) | |
448 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Image lacks .osrel section, refusing."); | |
449 | ||
450 | if (ret_osrelease) | |
451 | *ret_osrelease = TAKE_PTR(osrelease); | |
452 | if (ret_cmdline) | |
453 | *ret_cmdline = TAKE_PTR(cmdline); | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
458 | static int boot_entries_find_unified( | |
459 | const char *root, | |
460 | const char *dir, | |
461 | BootEntry **entries, | |
462 | size_t *n_entries) { | |
463 | ||
464 | _cleanup_(closedirp) DIR *d = NULL; | |
5e146a75 LP |
465 | struct dirent *de; |
466 | int r; | |
467 | ||
468 | assert(root); | |
469 | assert(dir); | |
470 | assert(entries); | |
471 | assert(n_entries); | |
472 | ||
473 | d = opendir(dir); | |
474 | if (!d) { | |
475 | if (errno == ENOENT) | |
476 | return 0; | |
477 | ||
478 | return log_error_errno(errno, "Failed to open %s: %m", dir); | |
479 | } | |
480 | ||
481 | FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read %s: %m", dir)) { | |
482 | _cleanup_free_ char *j = NULL, *osrelease = NULL, *cmdline = NULL; | |
483 | _cleanup_close_ int fd = -1; | |
484 | ||
28e68bb2 | 485 | dirent_ensure_type(d, de); |
5e146a75 LP |
486 | if (!dirent_is_file(de)) |
487 | continue; | |
488 | ||
489 | if (!endswith_no_case(de->d_name, ".efi")) | |
490 | continue; | |
491 | ||
319a4f4b | 492 | if (!GREEDY_REALLOC0(*entries, *n_entries + 1)) |
5e146a75 LP |
493 | return log_oom(); |
494 | ||
495 | fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK); | |
496 | if (fd < 0) { | |
497 | log_warning_errno(errno, "Failed to open %s/%s, ignoring: %m", dir, de->d_name); | |
498 | continue; | |
499 | } | |
500 | ||
501 | r = fd_verify_regular(fd); | |
502 | if (r < 0) { | |
dba33c4a | 503 | log_warning_errno(r, "File %s/%s is not regular, ignoring: %m", dir, de->d_name); |
5e146a75 LP |
504 | continue; |
505 | } | |
506 | ||
507 | r = find_sections(fd, &osrelease, &cmdline); | |
508 | if (r < 0) | |
509 | continue; | |
510 | ||
511 | j = path_join(dir, de->d_name); | |
512 | if (!j) | |
513 | return log_oom(); | |
514 | ||
515 | r = boot_entry_load_unified(root, j, osrelease, cmdline, *entries + *n_entries); | |
516 | if (r < 0) | |
517 | continue; | |
518 | ||
519 | (*n_entries) ++; | |
5e146a75 LP |
520 | } |
521 | ||
5e146a75 LP |
522 | return 0; |
523 | } | |
524 | ||
64f05708 | 525 | static bool find_nonunique(BootEntry *entries, size_t n_entries, bool *arr) { |
da6053d0 | 526 | size_t i, j; |
64f05708 ZJS |
527 | bool non_unique = false; |
528 | ||
4fe2ba0e LP |
529 | assert(entries || n_entries == 0); |
530 | assert(arr || n_entries == 0); | |
531 | ||
64f05708 ZJS |
532 | for (i = 0; i < n_entries; i++) |
533 | arr[i] = false; | |
534 | ||
535 | for (i = 0; i < n_entries; i++) | |
536 | for (j = 0; j < n_entries; j++) | |
537 | if (i != j && streq(boot_entry_title(entries + i), | |
538 | boot_entry_title(entries + j))) | |
539 | non_unique = arr[i] = arr[j] = true; | |
540 | ||
541 | return non_unique; | |
542 | } | |
543 | ||
544 | static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) { | |
545 | char *s; | |
da6053d0 | 546 | size_t i; |
64f05708 ZJS |
547 | int r; |
548 | bool arr[n_entries]; | |
549 | ||
4fe2ba0e LP |
550 | assert(entries || n_entries == 0); |
551 | ||
64f05708 ZJS |
552 | /* Find _all_ non-unique titles */ |
553 | if (!find_nonunique(entries, n_entries, arr)) | |
554 | return 0; | |
555 | ||
556 | /* Add version to non-unique titles */ | |
557 | for (i = 0; i < n_entries; i++) | |
558 | if (arr[i] && entries[i].version) { | |
559 | r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].version); | |
560 | if (r < 0) | |
561 | return -ENOMEM; | |
562 | ||
563 | free_and_replace(entries[i].show_title, s); | |
564 | } | |
565 | ||
566 | if (!find_nonunique(entries, n_entries, arr)) | |
567 | return 0; | |
568 | ||
569 | /* Add machine-id to non-unique titles */ | |
570 | for (i = 0; i < n_entries; i++) | |
571 | if (arr[i] && entries[i].machine_id) { | |
572 | r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].machine_id); | |
573 | if (r < 0) | |
574 | return -ENOMEM; | |
575 | ||
576 | free_and_replace(entries[i].show_title, s); | |
577 | } | |
578 | ||
579 | if (!find_nonunique(entries, n_entries, arr)) | |
580 | return 0; | |
581 | ||
582 | /* Add file name to non-unique titles */ | |
583 | for (i = 0; i < n_entries; i++) | |
584 | if (arr[i]) { | |
12580bc3 | 585 | r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].id); |
64f05708 ZJS |
586 | if (r < 0) |
587 | return -ENOMEM; | |
588 | ||
589 | free_and_replace(entries[i].show_title, s); | |
590 | } | |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
ad1afd60 | 595 | static int boot_entries_select_default(const BootConfig *config) { |
7e87c7d9 ZJS |
596 | int i; |
597 | ||
4fe2ba0e | 598 | assert(config); |
388d2993 ZJS |
599 | assert(config->entries || config->n_entries == 0); |
600 | ||
601 | if (config->n_entries == 0) { | |
602 | log_debug("Found no default boot entry :("); | |
603 | return -1; /* -1 means "no default" */ | |
604 | } | |
4fe2ba0e | 605 | |
7e87c7d9 ZJS |
606 | if (config->entry_oneshot) |
607 | for (i = config->n_entries - 1; i >= 0; i--) | |
12580bc3 LP |
608 | if (streq(config->entry_oneshot, config->entries[i].id)) { |
609 | log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot", | |
610 | config->entries[i].id); | |
7e87c7d9 ZJS |
611 | return i; |
612 | } | |
613 | ||
614 | if (config->entry_default) | |
615 | for (i = config->n_entries - 1; i >= 0; i--) | |
12580bc3 LP |
616 | if (streq(config->entry_default, config->entries[i].id)) { |
617 | log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault", | |
618 | config->entries[i].id); | |
7e87c7d9 ZJS |
619 | return i; |
620 | } | |
621 | ||
622 | if (config->default_pattern) | |
623 | for (i = config->n_entries - 1; i >= 0; i--) | |
12580bc3 LP |
624 | if (fnmatch(config->default_pattern, config->entries[i].id, FNM_CASEFOLD) == 0) { |
625 | log_debug("Found default: id \"%s\" is matched by pattern \"%s\"", | |
626 | config->entries[i].id, config->default_pattern); | |
7e87c7d9 ZJS |
627 | return i; |
628 | } | |
629 | ||
388d2993 ZJS |
630 | log_debug("Found default: last entry \"%s\"", config->entries[config->n_entries - 1].id); |
631 | return config->n_entries - 1; | |
7e87c7d9 ZJS |
632 | } |
633 | ||
a2f8664e LP |
634 | int boot_entries_load_config( |
635 | const char *esp_path, | |
636 | const char *xbootldr_path, | |
637 | BootConfig *config) { | |
638 | ||
7e87c7d9 ZJS |
639 | const char *p; |
640 | int r; | |
641 | ||
4fe2ba0e LP |
642 | assert(config); |
643 | ||
a2f8664e LP |
644 | if (esp_path) { |
645 | p = strjoina(esp_path, "/loader/loader.conf"); | |
646 | r = boot_loader_read_conf(p, config); | |
647 | if (r < 0) | |
648 | return r; | |
7e87c7d9 | 649 | |
a2f8664e LP |
650 | p = strjoina(esp_path, "/loader/entries"); |
651 | r = boot_entries_find(esp_path, p, &config->entries, &config->n_entries); | |
652 | if (r < 0) | |
653 | return r; | |
5e146a75 LP |
654 | |
655 | p = strjoina(esp_path, "/EFI/Linux/"); | |
656 | r = boot_entries_find_unified(esp_path, p, &config->entries, &config->n_entries); | |
657 | if (r < 0) | |
658 | return r; | |
a2f8664e LP |
659 | } |
660 | ||
661 | if (xbootldr_path) { | |
662 | p = strjoina(xbootldr_path, "/loader/entries"); | |
663 | r = boot_entries_find(xbootldr_path, p, &config->entries, &config->n_entries); | |
664 | if (r < 0) | |
665 | return r; | |
5e146a75 LP |
666 | |
667 | p = strjoina(xbootldr_path, "/EFI/Linux/"); | |
668 | r = boot_entries_find_unified(xbootldr_path, p, &config->entries, &config->n_entries); | |
669 | if (r < 0) | |
670 | return r; | |
a2f8664e | 671 | } |
7e87c7d9 | 672 | |
dd2bf34c LP |
673 | typesafe_qsort(config->entries, config->n_entries, boot_entry_compare); |
674 | ||
64f05708 ZJS |
675 | r = boot_entries_uniquify(config->entries, config->n_entries); |
676 | if (r < 0) | |
677 | return log_error_errno(r, "Failed to uniquify boot entries: %m"); | |
678 | ||
9c4a6c13 LP |
679 | if (is_efi_boot()) { |
680 | r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntryOneShot", &config->entry_oneshot); | |
bd29f9de ZJS |
681 | if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA)) { |
682 | log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryOneShot\": %m"); | |
683 | if (r == -ENOMEM) | |
684 | return r; | |
685 | } | |
9c4a6c13 LP |
686 | |
687 | r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntryDefault", &config->entry_default); | |
bd29f9de ZJS |
688 | if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA)) { |
689 | log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryDefault\": %m"); | |
690 | if (r == -ENOMEM) | |
691 | return r; | |
692 | } | |
9c4a6c13 | 693 | } |
7e87c7d9 ZJS |
694 | |
695 | config->default_entry = boot_entries_select_default(config); | |
696 | return 0; | |
697 | } | |
af918182 | 698 | |
eea4ce1e LP |
699 | int boot_entries_load_config_auto( |
700 | const char *override_esp_path, | |
701 | const char *override_xbootldr_path, | |
702 | BootConfig *config) { | |
703 | ||
704 | _cleanup_free_ char *esp_where = NULL, *xbootldr_where = NULL; | |
705 | int r; | |
706 | ||
707 | assert(config); | |
708 | ||
709 | /* This function is similar to boot_entries_load_config(), however we automatically search for the | |
710 | * ESP and the XBOOTLDR partition unless it is explicitly specified. Also, if the user did not pass | |
711 | * an ESP or XBOOTLDR path directly, let's see if /run/boot-loader-entries/ exists. If so, let's | |
712 | * read data from there, as if it was an ESP (i.e. loading both entries and loader.conf data from | |
713 | * it). This allows other boot loaders to pass boot loader entry information to our tools if they | |
714 | * want to. */ | |
715 | ||
716 | if (!override_esp_path && !override_xbootldr_path) { | |
f40999f8 ZJS |
717 | if (access("/run/boot-loader-entries/", F_OK) >= 0) |
718 | return boot_entries_load_config("/run/boot-loader-entries/", NULL, config); | |
eea4ce1e | 719 | |
f40999f8 ZJS |
720 | if (errno != ENOENT) |
721 | return log_error_errno(errno, | |
722 | "Failed to determine whether /run/boot-loader-entries/ exists: %m"); | |
eea4ce1e LP |
723 | } |
724 | ||
725 | r = find_esp_and_warn(override_esp_path, false, &esp_where, NULL, NULL, NULL, NULL); | |
cc5957dc | 726 | if (r < 0) /* we don't log about ENOKEY here, but propagate it, leaving it to the caller to log */ |
eea4ce1e LP |
727 | return r; |
728 | ||
729 | r = find_xbootldr_and_warn(override_xbootldr_path, false, &xbootldr_where, NULL); | |
730 | if (r < 0 && r != -ENOKEY) | |
731 | return r; /* It's fine if the XBOOTLDR partition doesn't exist, hence we ignore ENOKEY here */ | |
732 | ||
f40999f8 | 733 | return boot_entries_load_config(esp_where, xbootldr_where, config); |
eea4ce1e LP |
734 | } |
735 | ||
d4bd786d LP |
736 | int boot_entries_augment_from_loader( |
737 | BootConfig *config, | |
738 | char **found_by_loader, | |
739 | bool only_auto) { | |
740 | ||
741 | static const char *const title_table[] = { | |
93f14ce2 LP |
742 | /* Pretty names for a few well-known automatically discovered entries. */ |
743 | "auto-osx", "macOS", | |
744 | "auto-windows", "Windows Boot Manager", | |
745 | "auto-efi-shell", "EFI Shell", | |
746 | "auto-efi-default", "EFI Default Loader", | |
747 | "auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface", | |
748 | }; | |
749 | ||
93f14ce2 | 750 | char **i; |
93f14ce2 LP |
751 | |
752 | assert(config); | |
753 | ||
754 | /* Let's add the entries discovered by the boot loader to the end of our list, unless they are | |
755 | * already included there. */ | |
756 | ||
93f14ce2 | 757 | STRV_FOREACH(i, found_by_loader) { |
ce4c4f81 | 758 | _cleanup_free_ char *c = NULL, *t = NULL, *p = NULL; |
93f14ce2 LP |
759 | char **a, **b; |
760 | ||
761 | if (boot_config_has_entry(config, *i)) | |
762 | continue; | |
763 | ||
764 | if (only_auto && !startswith(*i, "auto-")) | |
765 | continue; | |
766 | ||
767 | c = strdup(*i); | |
768 | if (!c) | |
769 | return log_oom(); | |
770 | ||
771 | STRV_FOREACH_PAIR(a, b, (char**) title_table) | |
772 | if (streq(*a, *i)) { | |
773 | t = strdup(*b); | |
774 | if (!t) | |
775 | return log_oom(); | |
776 | break; | |
777 | } | |
778 | ||
ce4c4f81 ZJS |
779 | p = efi_variable_path(EFI_VENDOR_LOADER, "LoaderEntries"); |
780 | if (!p) | |
781 | return log_oom(); | |
782 | ||
319a4f4b | 783 | if (!GREEDY_REALLOC0(config->entries, config->n_entries + 1)) |
93f14ce2 LP |
784 | return log_oom(); |
785 | ||
786 | config->entries[config->n_entries++] = (BootEntry) { | |
787 | .type = BOOT_ENTRY_LOADER, | |
788 | .id = TAKE_PTR(c), | |
789 | .title = TAKE_PTR(t), | |
ce4c4f81 | 790 | .path = TAKE_PTR(p), |
93f14ce2 LP |
791 | }; |
792 | } | |
793 | ||
794 | return 0; | |
795 | } | |
796 | ||
af918182 ZJS |
797 | /********************************************************************************/ |
798 | ||
575d4370 LP |
799 | static int verify_esp_blkid( |
800 | dev_t devid, | |
5caa3167 | 801 | bool searching, |
af918182 ZJS |
802 | uint32_t *ret_part, |
803 | uint64_t *ret_pstart, | |
804 | uint64_t *ret_psize, | |
805 | sd_id128_t *ret_uuid) { | |
575d4370 LP |
806 | |
807 | sd_id128_t uuid = SD_ID128_NULL; | |
808 | uint64_t pstart = 0, psize = 0; | |
809 | uint32_t part = 0; | |
810 | ||
4e066f7f | 811 | #if HAVE_BLKID |
8e766630 | 812 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
54b22b26 | 813 | _cleanup_free_ char *node = NULL; |
4e066f7f | 814 | const char *v; |
af918182 ZJS |
815 | int r; |
816 | ||
575d4370 | 817 | r = device_path_make_major_minor(S_IFBLK, devid, &node); |
54b22b26 LP |
818 | if (r < 0) |
819 | return log_error_errno(r, "Failed to format major/minor device path: %m"); | |
575d4370 | 820 | |
af918182 | 821 | errno = 0; |
54b22b26 | 822 | b = blkid_new_probe_from_filename(node); |
af918182 | 823 | if (!b) |
575d4370 | 824 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(ENOMEM), "Failed to open file system \"%s\": %m", node); |
af918182 ZJS |
825 | |
826 | blkid_probe_enable_superblocks(b, 1); | |
827 | blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE); | |
828 | blkid_probe_enable_partitions(b, 1); | |
829 | blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); | |
830 | ||
831 | errno = 0; | |
832 | r = blkid_do_safeprobe(b); | |
a7afbd60 | 833 | if (r == -2) |
575d4370 | 834 | return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" is ambiguous.", node); |
a7afbd60 | 835 | else if (r == 1) |
575d4370 | 836 | return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" does not contain a label.", node); |
a7afbd60 | 837 | else if (r != 0) |
575d4370 | 838 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe file system \"%s\": %m", node); |
af918182 | 839 | |
af918182 ZJS |
840 | r = blkid_probe_lookup_value(b, "TYPE", &v, NULL); |
841 | if (r != 0) | |
7ea3024b LP |
842 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, |
843 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
844 | "No filesystem found on \"%s\": %m", node); | |
a7afbd60 LP |
845 | if (!streq(v, "vfat")) |
846 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
847 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
575d4370 | 848 | "File system \"%s\" is not FAT.", node); |
af918182 | 849 | |
af918182 ZJS |
850 | r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL); |
851 | if (r != 0) | |
7ea3024b LP |
852 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, |
853 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
854 | "File system \"%s\" is not located on a partitioned block device.", node); | |
a7afbd60 LP |
855 | if (!streq(v, "gpt")) |
856 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
857 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
575d4370 | 858 | "File system \"%s\" is not on a GPT partition table.", node); |
af918182 ZJS |
859 | |
860 | errno = 0; | |
861 | r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL); | |
862 | if (r != 0) | |
575d4370 | 863 | return log_error_errno(errno ?: EIO, "Failed to probe partition type UUID of \"%s\": %m", node); |
a7afbd60 LP |
864 | if (!streq(v, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) |
865 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
866 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
575d4370 | 867 | "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node); |
af918182 ZJS |
868 | |
869 | errno = 0; | |
870 | r = blkid_probe_lookup_value(b, "PART_ENTRY_UUID", &v, NULL); | |
871 | if (r != 0) | |
575d4370 | 872 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition entry UUID of \"%s\": %m", node); |
af918182 | 873 | r = sd_id128_from_string(v, &uuid); |
a7afbd60 | 874 | if (r < 0) |
575d4370 | 875 | return log_error_errno(r, "Partition \"%s\" has invalid UUID \"%s\".", node, v); |
af918182 ZJS |
876 | |
877 | errno = 0; | |
878 | r = blkid_probe_lookup_value(b, "PART_ENTRY_NUMBER", &v, NULL); | |
879 | if (r != 0) | |
28b77ab2 | 880 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition number of \"%s\": %m", node); |
af918182 ZJS |
881 | r = safe_atou32(v, &part); |
882 | if (r < 0) | |
883 | return log_error_errno(r, "Failed to parse PART_ENTRY_NUMBER field."); | |
884 | ||
885 | errno = 0; | |
886 | r = blkid_probe_lookup_value(b, "PART_ENTRY_OFFSET", &v, NULL); | |
887 | if (r != 0) | |
575d4370 | 888 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition offset of \"%s\": %m", node); |
af918182 ZJS |
889 | r = safe_atou64(v, &pstart); |
890 | if (r < 0) | |
891 | return log_error_errno(r, "Failed to parse PART_ENTRY_OFFSET field."); | |
892 | ||
893 | errno = 0; | |
894 | r = blkid_probe_lookup_value(b, "PART_ENTRY_SIZE", &v, NULL); | |
895 | if (r != 0) | |
575d4370 | 896 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition size of \"%s\": %m", node); |
af918182 ZJS |
897 | r = safe_atou64(v, &psize); |
898 | if (r < 0) | |
899 | return log_error_errno(r, "Failed to parse PART_ENTRY_SIZE field."); | |
4e066f7f | 900 | #endif |
af918182 | 901 | |
af918182 ZJS |
902 | if (ret_part) |
903 | *ret_part = part; | |
904 | if (ret_pstart) | |
905 | *ret_pstart = pstart; | |
906 | if (ret_psize) | |
907 | *ret_psize = psize; | |
908 | if (ret_uuid) | |
909 | *ret_uuid = uuid; | |
910 | ||
911 | return 0; | |
912 | } | |
913 | ||
cedb9eec LP |
914 | static int verify_esp_udev( |
915 | dev_t devid, | |
916 | bool searching, | |
917 | uint32_t *ret_part, | |
918 | uint64_t *ret_pstart, | |
919 | uint64_t *ret_psize, | |
920 | sd_id128_t *ret_uuid) { | |
921 | ||
922 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; | |
923 | _cleanup_free_ char *node = NULL; | |
924 | sd_id128_t uuid = SD_ID128_NULL; | |
925 | uint64_t pstart = 0, psize = 0; | |
926 | uint32_t part = 0; | |
927 | const char *v; | |
928 | int r; | |
929 | ||
930 | r = device_path_make_major_minor(S_IFBLK, devid, &node); | |
931 | if (r < 0) | |
932 | return log_error_errno(r, "Failed to format major/minor device path: %m"); | |
933 | ||
934 | r = sd_device_new_from_devnum(&d, 'b', devid); | |
935 | if (r < 0) | |
936 | return log_error_errno(r, "Failed to get device from device number: %m"); | |
937 | ||
938 | r = sd_device_get_property_value(d, "ID_FS_TYPE", &v); | |
939 | if (r < 0) | |
940 | return log_error_errno(r, "Failed to get device property: %m"); | |
941 | if (!streq(v, "vfat")) | |
942 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
943 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
944 | "File system \"%s\" is not FAT.", node ); | |
945 | ||
946 | r = sd_device_get_property_value(d, "ID_PART_ENTRY_SCHEME", &v); | |
947 | if (r < 0) | |
948 | return log_error_errno(r, "Failed to get device property: %m"); | |
949 | if (!streq(v, "gpt")) | |
950 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
951 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
952 | "File system \"%s\" is not on a GPT partition table.", node); | |
953 | ||
954 | r = sd_device_get_property_value(d, "ID_PART_ENTRY_TYPE", &v); | |
955 | if (r < 0) | |
956 | return log_error_errno(r, "Failed to get device property: %m"); | |
957 | if (!streq(v, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) | |
958 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
959 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
960 | "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node); | |
961 | ||
962 | r = sd_device_get_property_value(d, "ID_PART_ENTRY_UUID", &v); | |
963 | if (r < 0) | |
964 | return log_error_errno(r, "Failed to get device property: %m"); | |
965 | r = sd_id128_from_string(v, &uuid); | |
966 | if (r < 0) | |
967 | return log_error_errno(r, "Partition \"%s\" has invalid UUID \"%s\".", node, v); | |
968 | ||
969 | r = sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &v); | |
970 | if (r < 0) | |
971 | return log_error_errno(r, "Failed to get device property: %m"); | |
972 | r = safe_atou32(v, &part); | |
973 | if (r < 0) | |
974 | return log_error_errno(r, "Failed to parse PART_ENTRY_NUMBER field."); | |
975 | ||
976 | r = sd_device_get_property_value(d, "ID_PART_ENTRY_OFFSET", &v); | |
977 | if (r < 0) | |
978 | return log_error_errno(r, "Failed to get device property: %m"); | |
979 | r = safe_atou64(v, &pstart); | |
980 | if (r < 0) | |
981 | return log_error_errno(r, "Failed to parse PART_ENTRY_OFFSET field."); | |
982 | ||
983 | r = sd_device_get_property_value(d, "ID_PART_ENTRY_SIZE", &v); | |
984 | if (r < 0) | |
985 | return log_error_errno(r, "Failed to get device property: %m"); | |
986 | r = safe_atou64(v, &psize); | |
987 | if (r < 0) | |
988 | return log_error_errno(r, "Failed to parse PART_ENTRY_SIZE field."); | |
989 | ||
990 | if (ret_part) | |
991 | *ret_part = part; | |
992 | if (ret_pstart) | |
993 | *ret_pstart = pstart; | |
994 | if (ret_psize) | |
995 | *ret_psize = psize; | |
996 | if (ret_uuid) | |
997 | *ret_uuid = uuid; | |
998 | ||
999 | return 0; | |
1000 | } | |
1001 | ||
18ae9ef1 LP |
1002 | static int verify_fsroot_dir( |
1003 | const char *path, | |
1004 | bool searching, | |
1005 | bool unprivileged_mode, | |
1006 | dev_t *ret_dev) { | |
1007 | ||
1008 | struct stat st, st2; | |
8312d694 | 1009 | const char *t2, *trigger; |
18ae9ef1 LP |
1010 | int r; |
1011 | ||
1012 | assert(path); | |
1013 | assert(ret_dev); | |
1014 | ||
8312d694 LP |
1015 | /* So, the ESP and XBOOTLDR partition are commonly located on an autofs mount. stat() on the |
1016 | * directory won't trigger it, if it is not mounted yet. Let's hence explicitly trigger it here, | |
1017 | * before stat()ing */ | |
1018 | trigger = strjoina(path, "/trigger"); /* Filename doesn't matter... */ | |
1019 | (void) access(trigger, F_OK); | |
1020 | ||
18ae9ef1 LP |
1021 | if (stat(path, &st) < 0) |
1022 | return log_full_errno((searching && errno == ENOENT) || | |
1023 | (unprivileged_mode && errno == EACCES) ? LOG_DEBUG : LOG_ERR, errno, | |
1024 | "Failed to determine block device node of \"%s\": %m", path); | |
1025 | ||
1026 | if (major(st.st_dev) == 0) | |
1027 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
1028 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
1029 | "Block device node of \"%s\" is invalid.", path); | |
1030 | ||
eceb6111 LP |
1031 | if (path_equal(path, "/")) { |
1032 | /* Let's assume that the root directory of the OS is always the root of its file system | |
1033 | * (which technically doesn't have to be the case, but it's close enough, and it's not easy | |
1034 | * to be fully correct for it, since we can't look further up than the root dir easily.) */ | |
1035 | if (ret_dev) | |
1036 | *ret_dev = st.st_dev; | |
1037 | ||
1038 | return 0; | |
1039 | } | |
1040 | ||
18ae9ef1 LP |
1041 | t2 = strjoina(path, "/.."); |
1042 | if (stat(t2, &st2) < 0) { | |
1043 | if (errno != EACCES) | |
1044 | r = -errno; | |
1045 | else { | |
1046 | _cleanup_free_ char *parent = NULL; | |
1047 | ||
1048 | /* If going via ".." didn't work due to EACCESS, then let's determine the parent path | |
1049 | * directly instead. It's not as good, due to symlinks and such, but we can't do | |
1050 | * anything better here. */ | |
1051 | ||
1052 | parent = dirname_malloc(path); | |
1053 | if (!parent) | |
1054 | return log_oom(); | |
1055 | ||
eceb6111 | 1056 | r = stat(parent, &st2) < 0 ? -errno : 0; |
18ae9ef1 LP |
1057 | } |
1058 | ||
1059 | if (r < 0) | |
1060 | return log_full_errno(unprivileged_mode && r == -EACCES ? LOG_DEBUG : LOG_ERR, r, | |
1061 | "Failed to determine block device node of parent of \"%s\": %m", path); | |
1062 | } | |
1063 | ||
1064 | if (st.st_dev == st2.st_dev) | |
1065 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
1066 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
1067 | "Directory \"%s\" is not the root of the file system.", path); | |
1068 | ||
1069 | if (ret_dev) | |
1070 | *ret_dev = st.st_dev; | |
1071 | ||
1072 | return 0; | |
1073 | } | |
1074 | ||
575d4370 LP |
1075 | static int verify_esp( |
1076 | const char *p, | |
1077 | bool searching, | |
1078 | bool unprivileged_mode, | |
1079 | uint32_t *ret_part, | |
1080 | uint64_t *ret_pstart, | |
1081 | uint64_t *ret_psize, | |
1082 | sd_id128_t *ret_uuid) { | |
1083 | ||
575d4370 | 1084 | bool relax_checks; |
18ae9ef1 | 1085 | dev_t devid; |
575d4370 LP |
1086 | int r; |
1087 | ||
1088 | assert(p); | |
1089 | ||
1090 | /* This logs about all errors, except: | |
1091 | * | |
1092 | * -ENOENT → if 'searching' is set, and the dir doesn't exist | |
1093 | * -EADDRNOTAVAIL → if 'searching' is set, and the dir doesn't look like an ESP | |
5238e957 | 1094 | * -EACESS → if 'unprivileged_mode' is set, and we have trouble accessing the thing |
575d4370 LP |
1095 | */ |
1096 | ||
1097 | relax_checks = getenv_bool("SYSTEMD_RELAX_ESP_CHECKS") > 0; | |
1098 | ||
5238e957 | 1099 | /* Non-root user can only check the status, so if an error occurred in the following, it does not cause any |
575d4370 LP |
1100 | * issues. Let's also, silence the error messages. */ |
1101 | ||
1102 | if (!relax_checks) { | |
18ae9ef1 LP |
1103 | struct statfs sfs; |
1104 | ||
575d4370 LP |
1105 | if (statfs(p, &sfs) < 0) |
1106 | /* If we are searching for the mount point, don't generate a log message if we can't find the path */ | |
1107 | return log_full_errno((searching && errno == ENOENT) || | |
1108 | (unprivileged_mode && errno == EACCES) ? LOG_DEBUG : LOG_ERR, errno, | |
1109 | "Failed to check file system type of \"%s\": %m", p); | |
1110 | ||
1111 | if (!F_TYPE_EQUAL(sfs.f_type, MSDOS_SUPER_MAGIC)) | |
1112 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
1113 | SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV), | |
1114 | "File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p); | |
1115 | } | |
1116 | ||
18ae9ef1 | 1117 | r = verify_fsroot_dir(p, searching, unprivileged_mode, &devid); |
575d4370 | 1118 | if (r < 0) |
18ae9ef1 | 1119 | return r; |
575d4370 LP |
1120 | |
1121 | /* In a container we don't have access to block devices, skip this part of the verification, we trust | |
cedb9eec LP |
1122 | * the container manager set everything up correctly on its own. */ |
1123 | if (detect_container() > 0 || relax_checks) | |
575d4370 LP |
1124 | goto finish; |
1125 | ||
cedb9eec LP |
1126 | /* If we are unprivileged we ask udev for the metadata about the partition. If we are privileged we |
1127 | * use blkid instead. Why? Because this code is called from 'bootctl' which is pretty much an | |
1128 | * emergency recovery tool that should also work when udev isn't up (i.e. from the emergency shell), | |
1129 | * however blkid can't work if we have no privileges to access block devices directly, which is why | |
1130 | * we use udev in that case. */ | |
1131 | if (unprivileged_mode) | |
18ae9ef1 | 1132 | return verify_esp_udev(devid, searching, ret_part, ret_pstart, ret_psize, ret_uuid); |
cedb9eec | 1133 | else |
18ae9ef1 | 1134 | return verify_esp_blkid(devid, searching, ret_part, ret_pstart, ret_psize, ret_uuid); |
575d4370 LP |
1135 | |
1136 | finish: | |
1137 | if (ret_part) | |
1138 | *ret_part = 0; | |
1139 | if (ret_pstart) | |
1140 | *ret_pstart = 0; | |
1141 | if (ret_psize) | |
1142 | *ret_psize = 0; | |
1143 | if (ret_uuid) | |
1144 | *ret_uuid = SD_ID128_NULL; | |
1145 | ||
1146 | return 0; | |
1147 | } | |
1148 | ||
5caa3167 LP |
1149 | int find_esp_and_warn( |
1150 | const char *path, | |
1151 | bool unprivileged_mode, | |
1152 | char **ret_path, | |
1153 | uint32_t *ret_part, | |
1154 | uint64_t *ret_pstart, | |
1155 | uint64_t *ret_psize, | |
1156 | sd_id128_t *ret_uuid) { | |
af918182 | 1157 | |
af918182 ZJS |
1158 | int r; |
1159 | ||
5caa3167 LP |
1160 | /* This logs about all errors except: |
1161 | * | |
1162 | * -ENOKEY → when we can't find the partition | |
1163 | * -EACCESS → when unprivileged_mode is true, and we can't access something | |
1164 | */ | |
af918182 | 1165 | |
5caa3167 LP |
1166 | if (path) { |
1167 | r = verify_esp(path, false, unprivileged_mode, ret_part, ret_pstart, ret_psize, ret_uuid); | |
af918182 ZJS |
1168 | if (r < 0) |
1169 | return r; | |
1170 | ||
5caa3167 LP |
1171 | goto found; |
1172 | } | |
1173 | ||
cc7a0bfa LP |
1174 | path = getenv("SYSTEMD_ESP_PATH"); |
1175 | if (path) { | |
baaa35ad ZJS |
1176 | if (!path_is_valid(path) || !path_is_absolute(path)) |
1177 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1178 | "$SYSTEMD_ESP_PATH does not refer to absolute path, refusing to use it: %s", | |
1179 | path); | |
cc7a0bfa LP |
1180 | |
1181 | /* Note: when the user explicitly configured things with an env var we won't validate the mount | |
1182 | * point. After all we want this to be useful for testing. */ | |
1183 | goto found; | |
1184 | } | |
1185 | ||
5caa3167 LP |
1186 | FOREACH_STRING(path, "/efi", "/boot", "/boot/efi") { |
1187 | ||
1188 | r = verify_esp(path, true, unprivileged_mode, ret_part, ret_pstart, ret_psize, ret_uuid); | |
1189 | if (r >= 0) | |
1190 | goto found; | |
1191 | if (!IN_SET(r, -ENOENT, -EADDRNOTAVAIL)) /* This one is not it */ | |
1192 | return r; | |
1193 | } | |
1194 | ||
1195 | /* No logging here */ | |
1196 | return -ENOKEY; | |
1197 | ||
15cb6c98 LP |
1198 | found: |
1199 | if (ret_path) { | |
1200 | char *c; | |
1201 | ||
1202 | c = strdup(path); | |
1203 | if (!c) | |
1204 | return log_oom(); | |
1205 | ||
1206 | *ret_path = c; | |
1207 | } | |
1208 | ||
1209 | return 0; | |
1210 | } | |
1211 | ||
ad95aa44 LP |
1212 | static int verify_xbootldr_blkid( |
1213 | dev_t devid, | |
15cb6c98 | 1214 | bool searching, |
15cb6c98 | 1215 | sd_id128_t *ret_uuid) { |
ad95aa44 LP |
1216 | |
1217 | sd_id128_t uuid = SD_ID128_NULL; | |
1218 | ||
15cb6c98 LP |
1219 | #if HAVE_BLKID |
1220 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; | |
1221 | _cleanup_free_ char *node = NULL; | |
1222 | const char *v; | |
15cb6c98 LP |
1223 | int r; |
1224 | ||
ad95aa44 | 1225 | r = device_path_make_major_minor(S_IFBLK, devid, &node); |
15cb6c98 LP |
1226 | if (r < 0) |
1227 | return log_error_errno(r, "Failed to format major/minor device path: %m"); | |
1228 | errno = 0; | |
1229 | b = blkid_new_probe_from_filename(node); | |
1230 | if (!b) | |
ad95aa44 | 1231 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(ENOMEM), "Failed to open file system \"%s\": %m", node); |
15cb6c98 LP |
1232 | |
1233 | blkid_probe_enable_partitions(b, 1); | |
1234 | blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); | |
1235 | ||
1236 | errno = 0; | |
1237 | r = blkid_do_safeprobe(b); | |
1238 | if (r == -2) | |
ad95aa44 | 1239 | return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" is ambiguous.", node); |
15cb6c98 | 1240 | else if (r == 1) |
ad95aa44 | 1241 | return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" does not contain a label.", node); |
15cb6c98 | 1242 | else if (r != 0) |
ad95aa44 | 1243 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe file system \"%s\": %m", node); |
15cb6c98 LP |
1244 | |
1245 | errno = 0; | |
1246 | r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL); | |
1247 | if (r != 0) | |
ad95aa44 | 1248 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition scheme of \"%s\": %m", node); |
15cb6c98 LP |
1249 | if (streq(v, "gpt")) { |
1250 | ||
1251 | errno = 0; | |
1252 | r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL); | |
1253 | if (r != 0) | |
ad95aa44 | 1254 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition type UUID of \"%s\": %m", node); |
15cb6c98 LP |
1255 | if (!streq(v, "bc13c2ff-59e6-4262-a352-b275fd6f7172")) |
1256 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
1257 | searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV), | |
ad95aa44 | 1258 | "File system \"%s\" has wrong type for extended boot loader partition.", node); |
15cb6c98 LP |
1259 | |
1260 | errno = 0; | |
1261 | r = blkid_probe_lookup_value(b, "PART_ENTRY_UUID", &v, NULL); | |
1262 | if (r != 0) | |
ad95aa44 | 1263 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition entry UUID of \"%s\": %m", node); |
15cb6c98 LP |
1264 | r = sd_id128_from_string(v, &uuid); |
1265 | if (r < 0) | |
ad95aa44 | 1266 | return log_error_errno(r, "Partition \"%s\" has invalid UUID \"%s\".", node, v); |
15cb6c98 LP |
1267 | |
1268 | } else if (streq(v, "dos")) { | |
1269 | ||
1270 | errno = 0; | |
1271 | r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL); | |
1272 | if (r != 0) | |
ad95aa44 | 1273 | return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition type UUID of \"%s\": %m", node); |
15cb6c98 LP |
1274 | if (!streq(v, "0xea")) |
1275 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
1276 | searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV), | |
ad95aa44 | 1277 | "File system \"%s\" has wrong type for extended boot loader partition.", node); |
15cb6c98 LP |
1278 | |
1279 | } else | |
1280 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
1281 | searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV), | |
ad95aa44 | 1282 | "File system \"%s\" is not on a GPT or DOS partition table.", node); |
15cb6c98 LP |
1283 | #endif |
1284 | ||
15cb6c98 LP |
1285 | if (ret_uuid) |
1286 | *ret_uuid = uuid; | |
1287 | ||
1288 | return 0; | |
1289 | } | |
1290 | ||
85d02102 LP |
1291 | static int verify_xbootldr_udev( |
1292 | dev_t devid, | |
1293 | bool searching, | |
1294 | sd_id128_t *ret_uuid) { | |
1295 | ||
1296 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; | |
1297 | _cleanup_free_ char *node = NULL; | |
1298 | sd_id128_t uuid = SD_ID128_NULL; | |
1299 | const char *v; | |
1300 | int r; | |
1301 | ||
1302 | r = device_path_make_major_minor(S_IFBLK, devid, &node); | |
1303 | if (r < 0) | |
1304 | return log_error_errno(r, "Failed to format major/minor device path: %m"); | |
1305 | ||
1306 | r = sd_device_new_from_devnum(&d, 'b', devid); | |
1307 | if (r < 0) | |
1308 | return log_error_errno(r, "Failed to get device from device number: %m"); | |
1309 | ||
1310 | r = sd_device_get_property_value(d, "ID_PART_ENTRY_SCHEME", &v); | |
1311 | if (r < 0) | |
1312 | return log_error_errno(r, "Failed to get device property: %m"); | |
1313 | ||
1314 | if (streq(v, "gpt")) { | |
1315 | ||
1316 | r = sd_device_get_property_value(d, "ID_PART_ENTRY_TYPE", &v); | |
1317 | if (r < 0) | |
1318 | return log_error_errno(r, "Failed to get device property: %m"); | |
1319 | if (!streq(v, "bc13c2ff-59e6-4262-a352-b275fd6f7172")) | |
1320 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
1321 | searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV), | |
1322 | "File system \"%s\" has wrong type for extended boot loader partition.", node); | |
1323 | ||
1324 | r = sd_device_get_property_value(d, "ID_PART_ENTRY_UUID", &v); | |
1325 | if (r < 0) | |
1326 | return log_error_errno(r, "Failed to get device property: %m"); | |
1327 | r = sd_id128_from_string(v, &uuid); | |
1328 | if (r < 0) | |
1329 | return log_error_errno(r, "Partition \"%s\" has invalid UUID \"%s\".", node, v); | |
1330 | ||
1331 | } else if (streq(v, "dos")) { | |
1332 | ||
1333 | r = sd_device_get_property_value(d, "ID_PART_ENTRY_TYPE", &v); | |
1334 | if (r < 0) | |
1335 | return log_error_errno(r, "Failed to get device property: %m"); | |
1336 | if (!streq(v, "0xea")) | |
1337 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
1338 | searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV), | |
1339 | "File system \"%s\" has wrong type for extended boot loader partition.", node); | |
1340 | } else | |
1341 | return log_full_errno(searching ? LOG_DEBUG : LOG_ERR, | |
1342 | searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV), | |
1343 | "File system \"%s\" is not on a GPT or DOS partition table.", node); | |
1344 | ||
1345 | if (ret_uuid) | |
1346 | *ret_uuid = uuid; | |
1347 | ||
1348 | return 0; | |
1349 | } | |
1350 | ||
ad95aa44 LP |
1351 | static int verify_xbootldr( |
1352 | const char *p, | |
1353 | bool searching, | |
1354 | bool unprivileged_mode, | |
1355 | sd_id128_t *ret_uuid) { | |
1356 | ||
ad95aa44 | 1357 | bool relax_checks; |
4a4994b6 | 1358 | dev_t devid; |
ad95aa44 LP |
1359 | int r; |
1360 | ||
1361 | assert(p); | |
1362 | ||
1363 | relax_checks = getenv_bool("SYSTEMD_RELAX_XBOOTLDR_CHECKS") > 0; | |
1364 | ||
4a4994b6 | 1365 | r = verify_fsroot_dir(p, searching, unprivileged_mode, &devid); |
ad95aa44 | 1366 | if (r < 0) |
4a4994b6 | 1367 | return r; |
ad95aa44 | 1368 | |
85d02102 | 1369 | if (detect_container() > 0 || relax_checks) |
ad95aa44 LP |
1370 | goto finish; |
1371 | ||
85d02102 | 1372 | if (unprivileged_mode) |
4a4994b6 | 1373 | return verify_xbootldr_udev(devid, searching, ret_uuid); |
85d02102 | 1374 | else |
4a4994b6 | 1375 | return verify_xbootldr_blkid(devid, searching, ret_uuid); |
ad95aa44 LP |
1376 | |
1377 | finish: | |
1378 | if (ret_uuid) | |
1379 | *ret_uuid = SD_ID128_NULL; | |
1380 | ||
1381 | return 0; | |
1382 | } | |
1383 | ||
15cb6c98 LP |
1384 | int find_xbootldr_and_warn( |
1385 | const char *path, | |
1386 | bool unprivileged_mode, | |
1387 | char **ret_path, | |
1388 | sd_id128_t *ret_uuid) { | |
1389 | ||
1390 | int r; | |
1391 | ||
1392 | /* Similar to find_esp_and_warn(), but finds the XBOOTLDR partition. Returns the same errors. */ | |
1393 | ||
1394 | if (path) { | |
1395 | r = verify_xbootldr(path, false, unprivileged_mode, ret_uuid); | |
1396 | if (r < 0) | |
1397 | return r; | |
1398 | ||
1399 | goto found; | |
1400 | } | |
1401 | ||
1402 | path = getenv("SYSTEMD_XBOOTLDR_PATH"); | |
1403 | if (path) { | |
1404 | if (!path_is_valid(path) || !path_is_absolute(path)) | |
1405 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1406 | "$SYSTEMD_XBOOTLDR_PATH does not refer to absolute path, refusing to use it: %s", | |
1407 | path); | |
1408 | ||
1409 | goto found; | |
1410 | } | |
1411 | ||
1412 | r = verify_xbootldr("/boot", true, unprivileged_mode, ret_uuid); | |
1413 | if (r >= 0) { | |
1414 | path = "/boot"; | |
1415 | goto found; | |
1416 | } | |
1417 | if (!IN_SET(r, -ENOENT, -EADDRNOTAVAIL)) /* This one is not it */ | |
1418 | return r; | |
1419 | ||
1420 | return -ENOKEY; | |
1421 | ||
5caa3167 LP |
1422 | found: |
1423 | if (ret_path) { | |
1424 | char *c; | |
1425 | ||
1426 | c = strdup(path); | |
1427 | if (!c) | |
af918182 ZJS |
1428 | return log_oom(); |
1429 | ||
5caa3167 | 1430 | *ret_path = c; |
af918182 ZJS |
1431 | } |
1432 | ||
5caa3167 | 1433 | return 0; |
af918182 | 1434 | } |