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