1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/magic.h>
9 #include "alloc-util.h"
10 #include "blkid-util.h"
12 #include "conf-files.h"
14 #include "device-nodes.h"
15 #include "dirent-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "pe-header.h"
24 #include "stat-util.h"
25 #include "string-util.h"
27 #include "unaligned.h"
30 static void boot_entry_free(BootEntry
*entry
) {
37 free(entry
->show_title
);
39 free(entry
->machine_id
);
40 free(entry
->architecture
);
41 strv_free(entry
->options
);
44 strv_free(entry
->initrd
);
45 free(entry
->device_tree
);
48 static int boot_entry_load(
53 _cleanup_(boot_entry_free
) BootEntry tmp
= {};
54 _cleanup_fclose_
FILE *f
= NULL
;
63 c
= endswith_no_case(path
, ".conf");
65 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid loader entry filename: %s", path
);
68 tmp
.id
= strndup(b
, c
- b
);
72 tmp
.path
= strdup(path
);
76 tmp
.root
= strdup(root
);
80 f
= fopen(path
, "re");
82 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
85 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
88 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
92 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
94 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
98 if (IN_SET(*strstrip(buf
), '#', '\0'))
102 r
= extract_first_word(&p
, &field
, " \t", 0);
104 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
108 log_warning("%s:%u: Bad syntax", path
, line
);
112 if (streq(field
, "title"))
113 r
= free_and_strdup(&tmp
.title
, p
);
114 else if (streq(field
, "version"))
115 r
= free_and_strdup(&tmp
.version
, p
);
116 else if (streq(field
, "machine-id"))
117 r
= free_and_strdup(&tmp
.machine_id
, p
);
118 else if (streq(field
, "architecture"))
119 r
= free_and_strdup(&tmp
.architecture
, p
);
120 else if (streq(field
, "options"))
121 r
= strv_extend(&tmp
.options
, p
);
122 else if (streq(field
, "linux"))
123 r
= free_and_strdup(&tmp
.kernel
, p
);
124 else if (streq(field
, "efi"))
125 r
= free_and_strdup(&tmp
.efi
, p
);
126 else if (streq(field
, "initrd"))
127 r
= strv_extend(&tmp
.initrd
, p
);
128 else if (streq(field
, "devicetree"))
129 r
= free_and_strdup(&tmp
.device_tree
, p
);
131 log_notice("%s:%u: Unknown line \"%s\", ignoring.", path
, line
, field
);
135 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
139 tmp
= (BootEntry
) {};
143 void boot_config_free(BootConfig
*config
) {
148 free(config
->default_pattern
);
149 free(config
->timeout
);
150 free(config
->editor
);
151 free(config
->auto_entries
);
152 free(config
->auto_firmware
);
154 free(config
->entry_oneshot
);
155 free(config
->entry_default
);
157 for (i
= 0; i
< config
->n_entries
; i
++)
158 boot_entry_free(config
->entries
+ i
);
159 free(config
->entries
);
162 static int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
163 _cleanup_fclose_
FILE *f
= NULL
;
170 f
= fopen(path
, "re");
175 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
179 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
182 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
186 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
188 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
192 if (IN_SET(*strstrip(buf
), '#', '\0'))
196 r
= extract_first_word(&p
, &field
, " \t", 0);
198 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
202 log_warning("%s:%u: Bad syntax", path
, line
);
206 if (streq(field
, "default"))
207 r
= free_and_strdup(&config
->default_pattern
, p
);
208 else if (streq(field
, "timeout"))
209 r
= free_and_strdup(&config
->timeout
, p
);
210 else if (streq(field
, "editor"))
211 r
= free_and_strdup(&config
->editor
, p
);
212 else if (streq(field
, "auto-entries"))
213 r
= free_and_strdup(&config
->auto_entries
, p
);
214 else if (streq(field
, "auto-firmware"))
215 r
= free_and_strdup(&config
->auto_firmware
, p
);
216 else if (streq(field
, "console-mode"))
217 r
= free_and_strdup(&config
->console_mode
, p
);
219 log_notice("%s:%u: Unknown line \"%s\", ignoring.", path
, line
, field
);
223 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
229 static int boot_entry_compare(const BootEntry
*a
, const BootEntry
*b
) {
230 return str_verscmp(a
->id
, b
->id
);
233 static int boot_entries_find(
239 _cleanup_strv_free_
char **files
= NULL
;
240 size_t n_allocated
= *n_entries
;
249 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
251 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
253 STRV_FOREACH(f
, files
) {
254 if (!GREEDY_REALLOC0(*entries
, n_allocated
, *n_entries
+ 1))
257 r
= boot_entry_load(root
, *f
, *entries
+ *n_entries
);
267 static int boot_entry_load_unified(
270 const char *osrelease
,
274 _cleanup_free_
char *os_pretty_name
= NULL
, *os_id
= NULL
, *version_id
= NULL
, *build_id
= NULL
;
275 _cleanup_(boot_entry_free
) BootEntry tmp
= {};
276 _cleanup_fclose_
FILE *f
= NULL
;
284 k
= path_startswith(path
, root
);
286 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Path is not below root: %s", path
);
288 f
= fmemopen((void*) osrelease
, strlen(osrelease
), "r");
290 return log_error_errno(errno
, "Failed to open os-release buffer: %m");
292 r
= parse_env_file(f
, "os-release",
293 "PRETTY_NAME", &os_pretty_name
,
295 "VERSION_ID", &version_id
,
296 "BUILD_ID", &build_id
);
298 return log_error_errno(r
, "Failed to parse os-release data from unified kernel image %s: %m", path
);
300 if (!os_pretty_name
|| !os_id
|| !(version_id
|| build_id
))
301 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Missing fields in os-release data from unified kernel image %s, refusing.", path
);
303 tmp
.id
= strjoin(os_id
, "-", version_id
?: build_id
);
307 tmp
.path
= strdup(path
);
311 tmp
.root
= strdup(root
);
315 tmp
.kernel
= strdup(skip_leading_chars(k
, "/"));
319 tmp
.options
= strv_new(skip_leading_chars(cmdline
, WHITESPACE
));
323 delete_trailing_chars(tmp
.options
[0], WHITESPACE
);
325 tmp
.title
= TAKE_PTR(os_pretty_name
);
328 tmp
= (BootEntry
) {};
332 /* Maximum PE section we are willing to load (Note that sections we are not interested in may be larger, but
333 * the ones we do care about and we are willing to load into memory have this size limit.) */
334 #define PE_SECTION_SIZE_MAX (4U*1024U*1024U)
336 static int find_sections(
338 char **ret_osrelease
,
339 char **ret_cmdline
) {
341 _cleanup_free_
struct PeSectionHeader
*sections
= NULL
;
342 _cleanup_free_
char *osrelease
= NULL
, *cmdline
= NULL
;
343 size_t i
, n_sections
;
344 struct DosFileHeader dos
;
349 n
= pread(fd
, &dos
, sizeof(dos
), 0);
351 return log_error_errno(errno
, "Failed read DOS header: %m");
352 if (n
!= sizeof(dos
))
353 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading DOS header, refusing.");
355 if (dos
.Magic
[0] != 'M' || dos
.Magic
[1] != 'Z')
356 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "DOS executable magic missing, refusing.");
358 start
= unaligned_read_le32(&dos
.ExeHeader
);
359 n
= pread(fd
, &pe
, sizeof(pe
), start
);
361 return log_error_errno(errno
, "Failed to read PE header: %m");
363 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading PE header, refusing.");
365 if (pe
.Magic
[0] != 'P' || pe
.Magic
[1] != 'E' || pe
.Magic
[2] != 0 || pe
.Magic
[3] != 0)
366 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "PE executable magic missing, refusing.");
368 n_sections
= unaligned_read_le16(&pe
.FileHeader
.NumberOfSections
);
370 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "PE header has too many sections, refusing.");
372 sections
= new(struct PeSectionHeader
, n_sections
);
376 n
= pread(fd
, sections
,
377 n_sections
* sizeof(struct PeSectionHeader
),
378 start
+ sizeof(pe
) + unaligned_read_le16(&pe
.FileHeader
.SizeOfOptionalHeader
));
380 return log_error_errno(errno
, "Failed to read section data: %m");
381 if ((size_t) n
!= n_sections
* sizeof(struct PeSectionHeader
))
382 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading sections, refusing.");
384 for (i
= 0; i
< n_sections
; i
++) {
385 _cleanup_free_
char *k
= NULL
;
386 uint32_t offset
, size
;
389 if (strneq((char*) sections
[i
].Name
, ".osrel", sizeof(sections
[i
].Name
)))
391 else if (strneq((char*) sections
[i
].Name
, ".cmdline", sizeof(sections
[i
].Name
)))
397 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Duplicate section %s, refusing.", sections
[i
].Name
);
399 offset
= unaligned_read_le32(§ions
[i
].PointerToRawData
);
400 size
= unaligned_read_le32(§ions
[i
].VirtualSize
);
402 if (size
> PE_SECTION_SIZE_MAX
)
403 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Section %s too large, refusing.", sections
[i
].Name
);
405 k
= new(char, size
+1);
409 n
= pread(fd
, k
, size
, offset
);
411 return log_error_errno(errno
, "Failed to read section payload: %m");
413 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading section payload, refusing:");
415 /* Allow one trailing NUL byte, but nothing more. */
416 if (size
> 0 && memchr(k
, 0, size
- 1))
417 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Section contains embedded NUL byte: %m");
424 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Image lacks .osrel section, refusing.");
427 *ret_osrelease
= TAKE_PTR(osrelease
);
429 *ret_cmdline
= TAKE_PTR(cmdline
);
434 static int boot_entries_find_unified(
440 _cleanup_(closedirp
) DIR *d
= NULL
;
441 size_t n_allocated
= *n_entries
;
455 return log_error_errno(errno
, "Failed to open %s: %m", dir
);
458 FOREACH_DIRENT(de
, d
, return log_error_errno(errno
, "Failed to read %s: %m", dir
)) {
459 _cleanup_free_
char *j
= NULL
, *osrelease
= NULL
, *cmdline
= NULL
;
460 _cleanup_close_
int fd
= -1;
462 if (!dirent_is_file(de
))
465 if (!endswith_no_case(de
->d_name
, ".efi"))
468 if (!GREEDY_REALLOC0(*entries
, n_allocated
, *n_entries
+ 1))
471 fd
= openat(dirfd(d
), de
->d_name
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
473 log_warning_errno(errno
, "Failed to open %s/%s, ignoring: %m", dir
, de
->d_name
);
477 r
= fd_verify_regular(fd
);
479 log_warning_errno(errno
, "File %s/%s is not regular, ignoring: %m", dir
, de
->d_name
);
483 r
= find_sections(fd
, &osrelease
, &cmdline
);
487 j
= path_join(dir
, de
->d_name
);
491 r
= boot_entry_load_unified(root
, j
, osrelease
, cmdline
, *entries
+ *n_entries
);
501 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
503 bool non_unique
= false;
505 assert(entries
|| n_entries
== 0);
506 assert(arr
|| n_entries
== 0);
508 for (i
= 0; i
< n_entries
; i
++)
511 for (i
= 0; i
< n_entries
; i
++)
512 for (j
= 0; j
< n_entries
; j
++)
513 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
514 boot_entry_title(entries
+ j
)))
515 non_unique
= arr
[i
] = arr
[j
] = true;
520 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
526 assert(entries
|| n_entries
== 0);
528 /* Find _all_ non-unique titles */
529 if (!find_nonunique(entries
, n_entries
, arr
))
532 /* Add version to non-unique titles */
533 for (i
= 0; i
< n_entries
; i
++)
534 if (arr
[i
] && entries
[i
].version
) {
535 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
539 free_and_replace(entries
[i
].show_title
, s
);
542 if (!find_nonunique(entries
, n_entries
, arr
))
545 /* Add machine-id to non-unique titles */
546 for (i
= 0; i
< n_entries
; i
++)
547 if (arr
[i
] && entries
[i
].machine_id
) {
548 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
552 free_and_replace(entries
[i
].show_title
, s
);
555 if (!find_nonunique(entries
, n_entries
, arr
))
558 /* Add file name to non-unique titles */
559 for (i
= 0; i
< n_entries
; i
++)
561 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].id
);
565 free_and_replace(entries
[i
].show_title
, s
);
571 static int boot_entries_select_default(const BootConfig
*config
) {
576 if (config
->entry_oneshot
)
577 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
578 if (streq(config
->entry_oneshot
, config
->entries
[i
].id
)) {
579 log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
580 config
->entries
[i
].id
);
584 if (config
->entry_default
)
585 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
586 if (streq(config
->entry_default
, config
->entries
[i
].id
)) {
587 log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
588 config
->entries
[i
].id
);
592 if (config
->default_pattern
)
593 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
594 if (fnmatch(config
->default_pattern
, config
->entries
[i
].id
, FNM_CASEFOLD
) == 0) {
595 log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
596 config
->entries
[i
].id
, config
->default_pattern
);
600 if (config
->n_entries
> 0)
601 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].id
);
603 log_debug("Found no default boot entry :(");
605 return config
->n_entries
- 1; /* -1 means "no default" */
608 int boot_entries_load_config(
609 const char *esp_path
,
610 const char *xbootldr_path
,
611 BootConfig
*config
) {
619 p
= strjoina(esp_path
, "/loader/loader.conf");
620 r
= boot_loader_read_conf(p
, config
);
624 p
= strjoina(esp_path
, "/loader/entries");
625 r
= boot_entries_find(esp_path
, p
, &config
->entries
, &config
->n_entries
);
629 p
= strjoina(esp_path
, "/EFI/Linux/");
630 r
= boot_entries_find_unified(esp_path
, p
, &config
->entries
, &config
->n_entries
);
636 p
= strjoina(xbootldr_path
, "/loader/entries");
637 r
= boot_entries_find(xbootldr_path
, p
, &config
->entries
, &config
->n_entries
);
641 p
= strjoina(xbootldr_path
, "/EFI/Linux/");
642 r
= boot_entries_find_unified(xbootldr_path
, p
, &config
->entries
, &config
->n_entries
);
647 typesafe_qsort(config
->entries
, config
->n_entries
, boot_entry_compare
);
649 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
651 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
654 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
655 if (r
< 0 && r
!= -ENOENT
)
656 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
658 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
659 if (r
< 0 && r
!= -ENOENT
)
660 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
663 config
->default_entry
= boot_entries_select_default(config
);
667 /********************************************************************************/
669 static int verify_esp_blkid(
673 uint64_t *ret_pstart
,
675 sd_id128_t
*ret_uuid
) {
677 sd_id128_t uuid
= SD_ID128_NULL
;
678 uint64_t pstart
= 0, psize
= 0;
682 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
683 _cleanup_free_
char *node
= NULL
;
687 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
689 return log_error_errno(r
, "Failed to format major/minor device path: %m");
692 b
= blkid_new_probe_from_filename(node
);
694 return log_error_errno(errno
?: SYNTHETIC_ERRNO(ENOMEM
), "Failed to open file system \"%s\": %m", node
);
696 blkid_probe_enable_superblocks(b
, 1);
697 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
698 blkid_probe_enable_partitions(b
, 1);
699 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
702 r
= blkid_do_safeprobe(b
);
704 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" is ambiguous.", node
);
706 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" does not contain a label.", node
);
708 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system \"%s\": %m", node
);
711 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
713 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system type of \"%s\": %m", node
);
714 if (!streq(v
, "vfat"))
715 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
716 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
717 "File system \"%s\" is not FAT.", node
);
720 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
722 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition scheme of \"%s\": %m", node
);
723 if (!streq(v
, "gpt"))
724 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
725 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
726 "File system \"%s\" is not on a GPT partition table.", node
);
729 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
731 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID of \"%s\": %m", node
);
732 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"))
733 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
734 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
735 "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node
);
738 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
740 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition entry UUID of \"%s\": %m", node
);
741 r
= sd_id128_from_string(v
, &uuid
);
743 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
746 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
748 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition number of \"%s\": m", node
);
749 r
= safe_atou32(v
, &part
);
751 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
754 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
756 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition offset of \"%s\": %m", node
);
757 r
= safe_atou64(v
, &pstart
);
759 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
762 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
764 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition size of \"%s\": %m", node
);
765 r
= safe_atou64(v
, &psize
);
767 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
773 *ret_pstart
= pstart
;
782 static int verify_esp_udev(
786 uint64_t *ret_pstart
,
788 sd_id128_t
*ret_uuid
) {
790 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
791 _cleanup_free_
char *node
= NULL
;
792 sd_id128_t uuid
= SD_ID128_NULL
;
793 uint64_t pstart
= 0, psize
= 0;
798 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
800 return log_error_errno(r
, "Failed to format major/minor device path: %m");
802 r
= sd_device_new_from_devnum(&d
, 'b', devid
);
804 return log_error_errno(r
, "Failed to get device from device number: %m");
806 r
= sd_device_get_property_value(d
, "ID_FS_TYPE", &v
);
808 return log_error_errno(r
, "Failed to get device property: %m");
809 if (!streq(v
, "vfat"))
810 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
811 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
812 "File system \"%s\" is not FAT.", node
);
814 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SCHEME", &v
);
816 return log_error_errno(r
, "Failed to get device property: %m");
817 if (!streq(v
, "gpt"))
818 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
819 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
820 "File system \"%s\" is not on a GPT partition table.", node
);
822 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
824 return log_error_errno(r
, "Failed to get device property: %m");
825 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"))
826 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
827 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
828 "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node
);
830 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_UUID", &v
);
832 return log_error_errno(r
, "Failed to get device property: %m");
833 r
= sd_id128_from_string(v
, &uuid
);
835 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
837 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &v
);
839 return log_error_errno(r
, "Failed to get device property: %m");
840 r
= safe_atou32(v
, &part
);
842 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
844 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_OFFSET", &v
);
846 return log_error_errno(r
, "Failed to get device property: %m");
847 r
= safe_atou64(v
, &pstart
);
849 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
851 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SIZE", &v
);
853 return log_error_errno(r
, "Failed to get device property: %m");
854 r
= safe_atou64(v
, &psize
);
856 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
861 *ret_pstart
= pstart
;
870 static int verify_fsroot_dir(
873 bool unprivileged_mode
,
877 const char *t2
, *trigger
;
883 /* So, the ESP and XBOOTLDR partition are commonly located on an autofs mount. stat() on the
884 * directory won't trigger it, if it is not mounted yet. Let's hence explicitly trigger it here,
885 * before stat()ing */
886 trigger
= strjoina(path
, "/trigger"); /* Filename doesn't matter... */
887 (void) access(trigger
, F_OK
);
889 if (stat(path
, &st
) < 0)
890 return log_full_errno((searching
&& errno
== ENOENT
) ||
891 (unprivileged_mode
&& errno
== EACCES
) ? LOG_DEBUG
: LOG_ERR
, errno
,
892 "Failed to determine block device node of \"%s\": %m", path
);
894 if (major(st
.st_dev
) == 0)
895 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
896 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
897 "Block device node of \"%s\" is invalid.", path
);
899 t2
= strjoina(path
, "/..");
900 if (stat(t2
, &st2
) < 0) {
904 _cleanup_free_
char *parent
= NULL
;
906 /* If going via ".." didn't work due to EACCESS, then let's determine the parent path
907 * directly instead. It's not as good, due to symlinks and such, but we can't do
908 * anything better here. */
910 parent
= dirname_malloc(path
);
914 if (stat(parent
, &st2
) < 0)
921 return log_full_errno(unprivileged_mode
&& r
== -EACCES
? LOG_DEBUG
: LOG_ERR
, r
,
922 "Failed to determine block device node of parent of \"%s\": %m", path
);
925 if (st
.st_dev
== st2
.st_dev
)
926 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
927 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
928 "Directory \"%s\" is not the root of the file system.", path
);
931 *ret_dev
= st
.st_dev
;
936 static int verify_esp(
939 bool unprivileged_mode
,
941 uint64_t *ret_pstart
,
943 sd_id128_t
*ret_uuid
) {
951 /* This logs about all errors, except:
953 * -ENOENT → if 'searching' is set, and the dir doesn't exist
954 * -EADDRNOTAVAIL → if 'searching' is set, and the dir doesn't look like an ESP
955 * -EACESS → if 'unprivileged_mode' is set, and we have trouble acessing the thing
958 relax_checks
= getenv_bool("SYSTEMD_RELAX_ESP_CHECKS") > 0;
960 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
961 * issues. Let's also, silence the error messages. */
966 if (statfs(p
, &sfs
) < 0)
967 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
968 return log_full_errno((searching
&& errno
== ENOENT
) ||
969 (unprivileged_mode
&& errno
== EACCES
) ? LOG_DEBUG
: LOG_ERR
, errno
,
970 "Failed to check file system type of \"%s\": %m", p
);
972 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
))
973 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
974 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
975 "File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
978 r
= verify_fsroot_dir(p
, searching
, unprivileged_mode
, &devid
);
982 /* In a container we don't have access to block devices, skip this part of the verification, we trust
983 * the container manager set everything up correctly on its own. */
984 if (detect_container() > 0 || relax_checks
)
987 /* If we are unprivileged we ask udev for the metadata about the partition. If we are privileged we
988 * use blkid instead. Why? Because this code is called from 'bootctl' which is pretty much an
989 * emergency recovery tool that should also work when udev isn't up (i.e. from the emergency shell),
990 * however blkid can't work if we have no privileges to access block devices directly, which is why
991 * we use udev in that case. */
992 if (unprivileged_mode
)
993 return verify_esp_udev(devid
, searching
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
995 return verify_esp_blkid(devid
, searching
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1005 *ret_uuid
= SD_ID128_NULL
;
1010 int find_esp_and_warn(
1012 bool unprivileged_mode
,
1015 uint64_t *ret_pstart
,
1016 uint64_t *ret_psize
,
1017 sd_id128_t
*ret_uuid
) {
1021 /* This logs about all errors except:
1023 * -ENOKEY → when we can't find the partition
1024 * -EACCESS → when unprivileged_mode is true, and we can't access something
1028 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1035 path
= getenv("SYSTEMD_ESP_PATH");
1037 if (!path_is_valid(path
) || !path_is_absolute(path
))
1038 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1039 "$SYSTEMD_ESP_PATH does not refer to absolute path, refusing to use it: %s",
1042 /* Note: when the user explicitly configured things with an env var we won't validate the mount
1043 * point. After all we want this to be useful for testing. */
1047 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
1049 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1052 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
1056 /* No logging here */
1073 static int verify_xbootldr_blkid(
1076 sd_id128_t
*ret_uuid
) {
1078 sd_id128_t uuid
= SD_ID128_NULL
;
1081 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
1082 _cleanup_free_
char *node
= NULL
;
1086 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
1088 return log_error_errno(r
, "Failed to format major/minor device path: %m");
1090 b
= blkid_new_probe_from_filename(node
);
1092 return log_error_errno(errno
?: SYNTHETIC_ERRNO(ENOMEM
), "Failed to open file system \"%s\": %m", node
);
1094 blkid_probe_enable_partitions(b
, 1);
1095 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
1098 r
= blkid_do_safeprobe(b
);
1100 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" is ambiguous.", node
);
1102 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" does not contain a label.", node
);
1104 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system \"%s\": %m", node
);
1107 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
1109 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition scheme of \"%s\": %m", node
);
1110 if (streq(v
, "gpt")) {
1113 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
1115 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition type UUID of \"%s\": %m", node
);
1116 if (!streq(v
, "bc13c2ff-59e6-4262-a352-b275fd6f7172"))
1117 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1118 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1119 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1122 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
1124 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition entry UUID of \"%s\": %m", node
);
1125 r
= sd_id128_from_string(v
, &uuid
);
1127 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
1129 } else if (streq(v
, "dos")) {
1132 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
1134 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition type UUID of \"%s\": %m", node
);
1135 if (!streq(v
, "0xea"))
1136 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1137 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1138 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1141 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1142 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1143 "File system \"%s\" is not on a GPT or DOS partition table.", node
);
1152 static int verify_xbootldr_udev(
1155 sd_id128_t
*ret_uuid
) {
1157 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1158 _cleanup_free_
char *node
= NULL
;
1159 sd_id128_t uuid
= SD_ID128_NULL
;
1163 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
1165 return log_error_errno(r
, "Failed to format major/minor device path: %m");
1167 r
= sd_device_new_from_devnum(&d
, 'b', devid
);
1169 return log_error_errno(r
, "Failed to get device from device number: %m");
1171 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SCHEME", &v
);
1173 return log_error_errno(r
, "Failed to get device property: %m");
1175 if (streq(v
, "gpt")) {
1177 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
1179 return log_error_errno(r
, "Failed to get device property: %m");
1180 if (!streq(v
, "bc13c2ff-59e6-4262-a352-b275fd6f7172"))
1181 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1182 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1183 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1185 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_UUID", &v
);
1187 return log_error_errno(r
, "Failed to get device property: %m");
1188 r
= sd_id128_from_string(v
, &uuid
);
1190 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
1192 } else if (streq(v
, "dos")) {
1194 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
1196 return log_error_errno(r
, "Failed to get device property: %m");
1197 if (!streq(v
, "0xea"))
1198 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1199 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1200 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1202 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1203 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1204 "File system \"%s\" is not on a GPT or DOS partition table.", node
);
1212 static int verify_xbootldr(
1215 bool unprivileged_mode
,
1216 sd_id128_t
*ret_uuid
) {
1224 relax_checks
= getenv_bool("SYSTEMD_RELAX_XBOOTLDR_CHECKS") > 0;
1226 r
= verify_fsroot_dir(p
, searching
, unprivileged_mode
, &devid
);
1230 if (detect_container() > 0 || relax_checks
)
1233 if (unprivileged_mode
)
1234 return verify_xbootldr_udev(devid
, searching
, ret_uuid
);
1236 return verify_xbootldr_blkid(devid
, searching
, ret_uuid
);
1240 *ret_uuid
= SD_ID128_NULL
;
1245 int find_xbootldr_and_warn(
1247 bool unprivileged_mode
,
1249 sd_id128_t
*ret_uuid
) {
1253 /* Similar to find_esp_and_warn(), but finds the XBOOTLDR partition. Returns the same errors. */
1256 r
= verify_xbootldr(path
, false, unprivileged_mode
, ret_uuid
);
1263 path
= getenv("SYSTEMD_XBOOTLDR_PATH");
1265 if (!path_is_valid(path
) || !path_is_absolute(path
))
1266 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1267 "$SYSTEMD_XBOOTLDR_PATH does not refer to absolute path, refusing to use it: %s",
1273 r
= verify_xbootldr("/boot", true, unprivileged_mode
, ret_uuid
);
1278 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
1297 int find_default_boot_entry(
1298 const char *esp_path
,
1299 const char *xbootldr_path
,
1301 const BootEntry
**e
) {
1303 _cleanup_free_
char *esp_where
= NULL
, *xbootldr_where
= NULL
;
1309 r
= find_esp_and_warn(esp_path
, false, &esp_where
, NULL
, NULL
, NULL
, NULL
);
1310 if (r
== -ENOKEY
) /* find_esp_and_warn() doesn't warn about this case */
1311 return log_error_errno(r
, "Cannot find the ESP partition mount point.");
1312 if (r
< 0) /* But it logs about all these cases, hence don't log here again */
1315 r
= find_xbootldr_and_warn(xbootldr_path
, false, &xbootldr_where
, NULL
);
1316 if (r
< 0 && r
!= -ENOKEY
)
1319 r
= boot_entries_load_config(esp_where
, xbootldr_where
, config
);
1321 return log_error_errno(r
, "Failed to load boot loader entries: %m");
1323 if (config
->default_entry
< 0)
1324 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
1325 "No boot loader entry suitable as default, refusing to guess.");
1327 *e
= &config
->entries
[config
->default_entry
];
1328 log_debug("Found default boot loader entry in file \"%s\"", (*e
)->path
);