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-table.h"
26 #include "string-util.h"
28 #include "unaligned.h"
31 static void boot_entry_free(BootEntry
*entry
) {
38 free(entry
->show_title
);
40 free(entry
->machine_id
);
41 free(entry
->architecture
);
42 strv_free(entry
->options
);
45 strv_free(entry
->initrd
);
46 free(entry
->device_tree
);
49 static int boot_entry_load(
54 _cleanup_(boot_entry_free
) BootEntry tmp
= {
55 .type
= BOOT_ENTRY_CONF
,
58 _cleanup_fclose_
FILE *f
= NULL
;
67 c
= endswith_no_case(path
, ".conf");
69 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid loader entry file suffix: %s", path
);
72 tmp
.id
= strndup(b
, c
- b
);
76 if (!efi_loader_entry_name_valid(tmp
.id
))
77 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid loader entry filename: %s", path
);
79 tmp
.path
= strdup(path
);
83 tmp
.root
= strdup(root
);
87 f
= fopen(path
, "re");
89 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
92 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
95 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
99 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
101 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
105 if (IN_SET(*strstrip(buf
), '#', '\0'))
109 r
= extract_first_word(&p
, &field
, " \t", 0);
111 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
115 log_warning("%s:%u: Bad syntax", path
, line
);
119 if (streq(field
, "title"))
120 r
= free_and_strdup(&tmp
.title
, p
);
121 else if (streq(field
, "version"))
122 r
= free_and_strdup(&tmp
.version
, p
);
123 else if (streq(field
, "machine-id"))
124 r
= free_and_strdup(&tmp
.machine_id
, p
);
125 else if (streq(field
, "architecture"))
126 r
= free_and_strdup(&tmp
.architecture
, p
);
127 else if (streq(field
, "options"))
128 r
= strv_extend(&tmp
.options
, p
);
129 else if (streq(field
, "linux"))
130 r
= free_and_strdup(&tmp
.kernel
, p
);
131 else if (streq(field
, "efi"))
132 r
= free_and_strdup(&tmp
.efi
, p
);
133 else if (streq(field
, "initrd"))
134 r
= strv_extend(&tmp
.initrd
, p
);
135 else if (streq(field
, "devicetree"))
136 r
= free_and_strdup(&tmp
.device_tree
, p
);
138 log_notice("%s:%u: Unknown line \"%s\", ignoring.", path
, line
, field
);
142 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
146 tmp
= (BootEntry
) {};
150 void boot_config_free(BootConfig
*config
) {
155 free(config
->default_pattern
);
156 free(config
->timeout
);
157 free(config
->editor
);
158 free(config
->auto_entries
);
159 free(config
->auto_firmware
);
161 free(config
->entry_oneshot
);
162 free(config
->entry_default
);
164 for (i
= 0; i
< config
->n_entries
; i
++)
165 boot_entry_free(config
->entries
+ i
);
166 free(config
->entries
);
169 static int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
170 _cleanup_fclose_
FILE *f
= NULL
;
177 f
= fopen(path
, "re");
182 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
186 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
189 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
193 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
195 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
199 if (IN_SET(*strstrip(buf
), '#', '\0'))
203 r
= extract_first_word(&p
, &field
, " \t", 0);
205 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
209 log_warning("%s:%u: Bad syntax", path
, line
);
213 if (streq(field
, "default"))
214 r
= free_and_strdup(&config
->default_pattern
, p
);
215 else if (streq(field
, "timeout"))
216 r
= free_and_strdup(&config
->timeout
, p
);
217 else if (streq(field
, "editor"))
218 r
= free_and_strdup(&config
->editor
, p
);
219 else if (streq(field
, "auto-entries"))
220 r
= free_and_strdup(&config
->auto_entries
, p
);
221 else if (streq(field
, "auto-firmware"))
222 r
= free_and_strdup(&config
->auto_firmware
, p
);
223 else if (streq(field
, "console-mode"))
224 r
= free_and_strdup(&config
->console_mode
, p
);
226 log_notice("%s:%u: Unknown line \"%s\", ignoring.", path
, line
, field
);
230 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
236 static int boot_entry_compare(const BootEntry
*a
, const BootEntry
*b
) {
237 return str_verscmp(a
->id
, b
->id
);
240 static int boot_entries_find(
246 _cleanup_strv_free_
char **files
= NULL
;
247 size_t n_allocated
= *n_entries
;
256 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
258 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
260 STRV_FOREACH(f
, files
) {
261 if (!GREEDY_REALLOC0(*entries
, n_allocated
, *n_entries
+ 1))
264 r
= boot_entry_load(root
, *f
, *entries
+ *n_entries
);
274 static int boot_entry_load_unified(
277 const char *osrelease
,
281 _cleanup_free_
char *os_pretty_name
= NULL
, *os_id
= NULL
, *version_id
= NULL
, *build_id
= NULL
;
282 _cleanup_(boot_entry_free
) BootEntry tmp
= {
283 .type
= BOOT_ENTRY_UNIFIED
,
285 _cleanup_fclose_
FILE *f
= NULL
;
293 k
= path_startswith(path
, root
);
295 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Path is not below root: %s", path
);
297 f
= fmemopen((void*) osrelease
, strlen(osrelease
), "r");
299 return log_error_errno(errno
, "Failed to open os-release buffer: %m");
301 r
= parse_env_file(f
, "os-release",
302 "PRETTY_NAME", &os_pretty_name
,
304 "VERSION_ID", &version_id
,
305 "BUILD_ID", &build_id
);
307 return log_error_errno(r
, "Failed to parse os-release data from unified kernel image %s: %m", path
);
309 if (!os_pretty_name
|| !os_id
|| !(version_id
|| build_id
))
310 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Missing fields in os-release data from unified kernel image %s, refusing.", path
);
312 tmp
.id
= strjoin(os_id
, "-", version_id
?: build_id
);
316 if (!efi_loader_entry_name_valid(tmp
.id
))
317 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid loader entry: %s", tmp
.id
);
319 tmp
.path
= strdup(path
);
323 tmp
.root
= strdup(root
);
327 tmp
.kernel
= strdup(skip_leading_chars(k
, "/"));
331 tmp
.options
= strv_new(skip_leading_chars(cmdline
, WHITESPACE
));
335 delete_trailing_chars(tmp
.options
[0], WHITESPACE
);
337 tmp
.title
= TAKE_PTR(os_pretty_name
);
340 tmp
= (BootEntry
) {};
344 /* Maximum PE section we are willing to load (Note that sections we are not interested in may be larger, but
345 * the ones we do care about and we are willing to load into memory have this size limit.) */
346 #define PE_SECTION_SIZE_MAX (4U*1024U*1024U)
348 static int find_sections(
350 char **ret_osrelease
,
351 char **ret_cmdline
) {
353 _cleanup_free_
struct PeSectionHeader
*sections
= NULL
;
354 _cleanup_free_
char *osrelease
= NULL
, *cmdline
= NULL
;
355 size_t i
, n_sections
;
356 struct DosFileHeader dos
;
361 n
= pread(fd
, &dos
, sizeof(dos
), 0);
363 return log_error_errno(errno
, "Failed read DOS header: %m");
364 if (n
!= sizeof(dos
))
365 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading DOS header, refusing.");
367 if (dos
.Magic
[0] != 'M' || dos
.Magic
[1] != 'Z')
368 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "DOS executable magic missing, refusing.");
370 start
= unaligned_read_le32(&dos
.ExeHeader
);
371 n
= pread(fd
, &pe
, sizeof(pe
), start
);
373 return log_error_errno(errno
, "Failed to read PE header: %m");
375 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading PE header, refusing.");
377 if (pe
.Magic
[0] != 'P' || pe
.Magic
[1] != 'E' || pe
.Magic
[2] != 0 || pe
.Magic
[3] != 0)
378 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "PE executable magic missing, refusing.");
380 n_sections
= unaligned_read_le16(&pe
.FileHeader
.NumberOfSections
);
382 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "PE header has too many sections, refusing.");
384 sections
= new(struct PeSectionHeader
, n_sections
);
388 n
= pread(fd
, sections
,
389 n_sections
* sizeof(struct PeSectionHeader
),
390 start
+ sizeof(pe
) + unaligned_read_le16(&pe
.FileHeader
.SizeOfOptionalHeader
));
392 return log_error_errno(errno
, "Failed to read section data: %m");
393 if ((size_t) n
!= n_sections
* sizeof(struct PeSectionHeader
))
394 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading sections, refusing.");
396 for (i
= 0; i
< n_sections
; i
++) {
397 _cleanup_free_
char *k
= NULL
;
398 uint32_t offset
, size
;
401 if (strneq((char*) sections
[i
].Name
, ".osrel", sizeof(sections
[i
].Name
)))
403 else if (strneq((char*) sections
[i
].Name
, ".cmdline", sizeof(sections
[i
].Name
)))
409 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Duplicate section %s, refusing.", sections
[i
].Name
);
411 offset
= unaligned_read_le32(§ions
[i
].PointerToRawData
);
412 size
= unaligned_read_le32(§ions
[i
].VirtualSize
);
414 if (size
> PE_SECTION_SIZE_MAX
)
415 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Section %s too large, refusing.", sections
[i
].Name
);
417 k
= new(char, size
+1);
421 n
= pread(fd
, k
, size
, offset
);
423 return log_error_errno(errno
, "Failed to read section payload: %m");
425 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading section payload, refusing:");
427 /* Allow one trailing NUL byte, but nothing more. */
428 if (size
> 0 && memchr(k
, 0, size
- 1))
429 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Section contains embedded NUL byte: %m");
436 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Image lacks .osrel section, refusing.");
439 *ret_osrelease
= TAKE_PTR(osrelease
);
441 *ret_cmdline
= TAKE_PTR(cmdline
);
446 static int boot_entries_find_unified(
452 _cleanup_(closedirp
) DIR *d
= NULL
;
453 size_t n_allocated
= *n_entries
;
467 return log_error_errno(errno
, "Failed to open %s: %m", dir
);
470 FOREACH_DIRENT(de
, d
, return log_error_errno(errno
, "Failed to read %s: %m", dir
)) {
471 _cleanup_free_
char *j
= NULL
, *osrelease
= NULL
, *cmdline
= NULL
;
472 _cleanup_close_
int fd
= -1;
474 if (!dirent_is_file(de
))
477 if (!endswith_no_case(de
->d_name
, ".efi"))
480 if (!GREEDY_REALLOC0(*entries
, n_allocated
, *n_entries
+ 1))
483 fd
= openat(dirfd(d
), de
->d_name
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
485 log_warning_errno(errno
, "Failed to open %s/%s, ignoring: %m", dir
, de
->d_name
);
489 r
= fd_verify_regular(fd
);
491 log_warning_errno(r
, "File %s/%s is not regular, ignoring: %m", dir
, de
->d_name
);
495 r
= find_sections(fd
, &osrelease
, &cmdline
);
499 j
= path_join(dir
, de
->d_name
);
503 r
= boot_entry_load_unified(root
, j
, osrelease
, cmdline
, *entries
+ *n_entries
);
513 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
515 bool non_unique
= false;
517 assert(entries
|| n_entries
== 0);
518 assert(arr
|| n_entries
== 0);
520 for (i
= 0; i
< n_entries
; i
++)
523 for (i
= 0; i
< n_entries
; i
++)
524 for (j
= 0; j
< n_entries
; j
++)
525 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
526 boot_entry_title(entries
+ j
)))
527 non_unique
= arr
[i
] = arr
[j
] = true;
532 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
538 assert(entries
|| n_entries
== 0);
540 /* Find _all_ non-unique titles */
541 if (!find_nonunique(entries
, n_entries
, arr
))
544 /* Add version to non-unique titles */
545 for (i
= 0; i
< n_entries
; i
++)
546 if (arr
[i
] && entries
[i
].version
) {
547 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
551 free_and_replace(entries
[i
].show_title
, s
);
554 if (!find_nonunique(entries
, n_entries
, arr
))
557 /* Add machine-id to non-unique titles */
558 for (i
= 0; i
< n_entries
; i
++)
559 if (arr
[i
] && entries
[i
].machine_id
) {
560 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
564 free_and_replace(entries
[i
].show_title
, s
);
567 if (!find_nonunique(entries
, n_entries
, arr
))
570 /* Add file name to non-unique titles */
571 for (i
= 0; i
< n_entries
; i
++)
573 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].id
);
577 free_and_replace(entries
[i
].show_title
, s
);
583 static int boot_entries_select_default(const BootConfig
*config
) {
587 assert(config
->entries
|| config
->n_entries
== 0);
589 if (config
->n_entries
== 0) {
590 log_debug("Found no default boot entry :(");
591 return -1; /* -1 means "no default" */
594 if (config
->entry_oneshot
)
595 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
596 if (streq(config
->entry_oneshot
, config
->entries
[i
].id
)) {
597 log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
598 config
->entries
[i
].id
);
602 if (config
->entry_default
)
603 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
604 if (streq(config
->entry_default
, config
->entries
[i
].id
)) {
605 log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
606 config
->entries
[i
].id
);
610 if (config
->default_pattern
)
611 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
612 if (fnmatch(config
->default_pattern
, config
->entries
[i
].id
, FNM_CASEFOLD
) == 0) {
613 log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
614 config
->entries
[i
].id
, config
->default_pattern
);
618 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].id
);
619 return config
->n_entries
- 1;
622 int boot_entries_load_config(
623 const char *esp_path
,
624 const char *xbootldr_path
,
625 BootConfig
*config
) {
633 p
= strjoina(esp_path
, "/loader/loader.conf");
634 r
= boot_loader_read_conf(p
, config
);
638 p
= strjoina(esp_path
, "/loader/entries");
639 r
= boot_entries_find(esp_path
, p
, &config
->entries
, &config
->n_entries
);
643 p
= strjoina(esp_path
, "/EFI/Linux/");
644 r
= boot_entries_find_unified(esp_path
, p
, &config
->entries
, &config
->n_entries
);
650 p
= strjoina(xbootldr_path
, "/loader/entries");
651 r
= boot_entries_find(xbootldr_path
, p
, &config
->entries
, &config
->n_entries
);
655 p
= strjoina(xbootldr_path
, "/EFI/Linux/");
656 r
= boot_entries_find_unified(xbootldr_path
, p
, &config
->entries
, &config
->n_entries
);
661 typesafe_qsort(config
->entries
, config
->n_entries
, boot_entry_compare
);
663 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
665 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
668 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
669 if (r
< 0 && !IN_SET(r
, -ENOENT
, -ENODATA
)) {
670 log_warning_errno(r
, "Failed to read EFI variable \"LoaderEntryOneShot\": %m");
675 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
676 if (r
< 0 && !IN_SET(r
, -ENOENT
, -ENODATA
)) {
677 log_warning_errno(r
, "Failed to read EFI variable \"LoaderEntryDefault\": %m");
683 config
->default_entry
= boot_entries_select_default(config
);
687 int boot_entries_load_config_auto(
688 const char *override_esp_path
,
689 const char *override_xbootldr_path
,
690 BootConfig
*config
) {
692 _cleanup_free_
char *esp_where
= NULL
, *xbootldr_where
= NULL
;
697 /* This function is similar to boot_entries_load_config(), however we automatically search for the
698 * ESP and the XBOOTLDR partition unless it is explicitly specified. Also, if the user did not pass
699 * an ESP or XBOOTLDR path directly, let's see if /run/boot-loader-entries/ exists. If so, let's
700 * read data from there, as if it was an ESP (i.e. loading both entries and loader.conf data from
701 * it). This allows other boot loaders to pass boot loader entry information to our tools if they
704 if (!override_esp_path
&& !override_xbootldr_path
) {
705 if (access("/run/boot-loader-entries/", F_OK
) >= 0)
706 return boot_entries_load_config("/run/boot-loader-entries/", NULL
, config
);
709 return log_error_errno(errno
,
710 "Failed to determine whether /run/boot-loader-entries/ exists: %m");
713 r
= find_esp_and_warn(override_esp_path
, false, &esp_where
, NULL
, NULL
, NULL
, NULL
);
714 if (r
== -ENOKEY
) /* find_esp_and_warn() doesn't warn about this case */
715 return log_error_errno(r
, "Cannot find the ESP partition mount point.");
716 if (r
< 0) /* But it logs about all these cases, hence don't log here again */
719 r
= find_xbootldr_and_warn(override_xbootldr_path
, false, &xbootldr_where
, NULL
);
720 if (r
< 0 && r
!= -ENOKEY
)
721 return r
; /* It's fine if the XBOOTLDR partition doesn't exist, hence we ignore ENOKEY here */
723 return boot_entries_load_config(esp_where
, xbootldr_where
, config
);
726 int boot_entries_augment_from_loader(BootConfig
*config
, bool only_auto
) {
728 static const char * const title_table
[] = {
729 /* Pretty names for a few well-known automatically discovered entries. */
731 "auto-windows", "Windows Boot Manager",
732 "auto-efi-shell", "EFI Shell",
733 "auto-efi-default", "EFI Default Loader",
734 "auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface",
737 _cleanup_free_
char **found_by_loader
= NULL
;
744 /* Let's add the entries discovered by the boot loader to the end of our list, unless they are
745 * already included there. */
747 r
= efi_loader_get_entries(&found_by_loader
);
748 if (IN_SET(r
, -ENOENT
, -EOPNOTSUPP
))
749 return log_debug_errno(r
, "Boot loader reported no entries.");
751 return log_error_errno(r
, "Failed to determine entries reported by boot loader: %m");
753 n_allocated
= config
->n_entries
;
755 STRV_FOREACH(i
, found_by_loader
) {
756 _cleanup_free_
char *c
= NULL
, *t
= NULL
;
759 if (boot_config_has_entry(config
, *i
))
762 if (only_auto
&& !startswith(*i
, "auto-"))
769 STRV_FOREACH_PAIR(a
, b
, (char**) title_table
)
777 if (!GREEDY_REALLOC0(config
->entries
, n_allocated
, config
->n_entries
+ 1))
780 config
->entries
[config
->n_entries
++] = (BootEntry
) {
781 .type
= BOOT_ENTRY_LOADER
,
783 .title
= TAKE_PTR(t
),
790 /********************************************************************************/
792 static int verify_esp_blkid(
796 uint64_t *ret_pstart
,
798 sd_id128_t
*ret_uuid
) {
800 sd_id128_t uuid
= SD_ID128_NULL
;
801 uint64_t pstart
= 0, psize
= 0;
805 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
806 _cleanup_free_
char *node
= NULL
;
810 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
812 return log_error_errno(r
, "Failed to format major/minor device path: %m");
815 b
= blkid_new_probe_from_filename(node
);
817 return log_error_errno(errno
?: SYNTHETIC_ERRNO(ENOMEM
), "Failed to open file system \"%s\": %m", node
);
819 blkid_probe_enable_superblocks(b
, 1);
820 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
821 blkid_probe_enable_partitions(b
, 1);
822 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
825 r
= blkid_do_safeprobe(b
);
827 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" is ambiguous.", node
);
829 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" does not contain a label.", node
);
831 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system \"%s\": %m", node
);
834 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
836 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system type of \"%s\": %m", node
);
837 if (!streq(v
, "vfat"))
838 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
839 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
840 "File system \"%s\" is not FAT.", node
);
843 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
845 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition scheme of \"%s\": %m", node
);
846 if (!streq(v
, "gpt"))
847 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
848 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
849 "File system \"%s\" is not on a GPT partition table.", node
);
852 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
854 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID of \"%s\": %m", node
);
855 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"))
856 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
857 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
858 "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node
);
861 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
863 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition entry UUID of \"%s\": %m", node
);
864 r
= sd_id128_from_string(v
, &uuid
);
866 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
869 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
871 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition number of \"%s\": m", node
);
872 r
= safe_atou32(v
, &part
);
874 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
877 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
879 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition offset of \"%s\": %m", node
);
880 r
= safe_atou64(v
, &pstart
);
882 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
885 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
887 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition size of \"%s\": %m", node
);
888 r
= safe_atou64(v
, &psize
);
890 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
896 *ret_pstart
= pstart
;
905 static int verify_esp_udev(
909 uint64_t *ret_pstart
,
911 sd_id128_t
*ret_uuid
) {
913 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
914 _cleanup_free_
char *node
= NULL
;
915 sd_id128_t uuid
= SD_ID128_NULL
;
916 uint64_t pstart
= 0, psize
= 0;
921 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
923 return log_error_errno(r
, "Failed to format major/minor device path: %m");
925 r
= sd_device_new_from_devnum(&d
, 'b', devid
);
927 return log_error_errno(r
, "Failed to get device from device number: %m");
929 r
= sd_device_get_property_value(d
, "ID_FS_TYPE", &v
);
931 return log_error_errno(r
, "Failed to get device property: %m");
932 if (!streq(v
, "vfat"))
933 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
934 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
935 "File system \"%s\" is not FAT.", node
);
937 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SCHEME", &v
);
939 return log_error_errno(r
, "Failed to get device property: %m");
940 if (!streq(v
, "gpt"))
941 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
942 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
943 "File system \"%s\" is not on a GPT partition table.", node
);
945 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
947 return log_error_errno(r
, "Failed to get device property: %m");
948 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"))
949 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
950 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
951 "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node
);
953 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_UUID", &v
);
955 return log_error_errno(r
, "Failed to get device property: %m");
956 r
= sd_id128_from_string(v
, &uuid
);
958 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
960 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &v
);
962 return log_error_errno(r
, "Failed to get device property: %m");
963 r
= safe_atou32(v
, &part
);
965 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
967 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_OFFSET", &v
);
969 return log_error_errno(r
, "Failed to get device property: %m");
970 r
= safe_atou64(v
, &pstart
);
972 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
974 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SIZE", &v
);
976 return log_error_errno(r
, "Failed to get device property: %m");
977 r
= safe_atou64(v
, &psize
);
979 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
984 *ret_pstart
= pstart
;
993 static int verify_fsroot_dir(
996 bool unprivileged_mode
,
1000 const char *t2
, *trigger
;
1006 /* So, the ESP and XBOOTLDR partition are commonly located on an autofs mount. stat() on the
1007 * directory won't trigger it, if it is not mounted yet. Let's hence explicitly trigger it here,
1008 * before stat()ing */
1009 trigger
= strjoina(path
, "/trigger"); /* Filename doesn't matter... */
1010 (void) access(trigger
, F_OK
);
1012 if (stat(path
, &st
) < 0)
1013 return log_full_errno((searching
&& errno
== ENOENT
) ||
1014 (unprivileged_mode
&& errno
== EACCES
) ? LOG_DEBUG
: LOG_ERR
, errno
,
1015 "Failed to determine block device node of \"%s\": %m", path
);
1017 if (major(st
.st_dev
) == 0)
1018 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1019 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
1020 "Block device node of \"%s\" is invalid.", path
);
1022 t2
= strjoina(path
, "/..");
1023 if (stat(t2
, &st2
) < 0) {
1024 if (errno
!= EACCES
)
1027 _cleanup_free_
char *parent
= NULL
;
1029 /* If going via ".." didn't work due to EACCESS, then let's determine the parent path
1030 * directly instead. It's not as good, due to symlinks and such, but we can't do
1031 * anything better here. */
1033 parent
= dirname_malloc(path
);
1037 if (stat(parent
, &st2
) < 0)
1044 return log_full_errno(unprivileged_mode
&& r
== -EACCES
? LOG_DEBUG
: LOG_ERR
, r
,
1045 "Failed to determine block device node of parent of \"%s\": %m", path
);
1048 if (st
.st_dev
== st2
.st_dev
)
1049 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1050 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
1051 "Directory \"%s\" is not the root of the file system.", path
);
1054 *ret_dev
= st
.st_dev
;
1059 static int verify_esp(
1062 bool unprivileged_mode
,
1064 uint64_t *ret_pstart
,
1065 uint64_t *ret_psize
,
1066 sd_id128_t
*ret_uuid
) {
1074 /* This logs about all errors, except:
1076 * -ENOENT → if 'searching' is set, and the dir doesn't exist
1077 * -EADDRNOTAVAIL → if 'searching' is set, and the dir doesn't look like an ESP
1078 * -EACESS → if 'unprivileged_mode' is set, and we have trouble acessing the thing
1081 relax_checks
= getenv_bool("SYSTEMD_RELAX_ESP_CHECKS") > 0;
1083 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
1084 * issues. Let's also, silence the error messages. */
1086 if (!relax_checks
) {
1089 if (statfs(p
, &sfs
) < 0)
1090 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
1091 return log_full_errno((searching
&& errno
== ENOENT
) ||
1092 (unprivileged_mode
&& errno
== EACCES
) ? LOG_DEBUG
: LOG_ERR
, errno
,
1093 "Failed to check file system type of \"%s\": %m", p
);
1095 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
))
1096 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1097 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
1098 "File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
1101 r
= verify_fsroot_dir(p
, searching
, unprivileged_mode
, &devid
);
1105 /* In a container we don't have access to block devices, skip this part of the verification, we trust
1106 * the container manager set everything up correctly on its own. */
1107 if (detect_container() > 0 || relax_checks
)
1110 /* If we are unprivileged we ask udev for the metadata about the partition. If we are privileged we
1111 * use blkid instead. Why? Because this code is called from 'bootctl' which is pretty much an
1112 * emergency recovery tool that should also work when udev isn't up (i.e. from the emergency shell),
1113 * however blkid can't work if we have no privileges to access block devices directly, which is why
1114 * we use udev in that case. */
1115 if (unprivileged_mode
)
1116 return verify_esp_udev(devid
, searching
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1118 return verify_esp_blkid(devid
, searching
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1128 *ret_uuid
= SD_ID128_NULL
;
1133 int find_esp_and_warn(
1135 bool unprivileged_mode
,
1138 uint64_t *ret_pstart
,
1139 uint64_t *ret_psize
,
1140 sd_id128_t
*ret_uuid
) {
1144 /* This logs about all errors except:
1146 * -ENOKEY → when we can't find the partition
1147 * -EACCESS → when unprivileged_mode is true, and we can't access something
1151 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1158 path
= getenv("SYSTEMD_ESP_PATH");
1160 if (!path_is_valid(path
) || !path_is_absolute(path
))
1161 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1162 "$SYSTEMD_ESP_PATH does not refer to absolute path, refusing to use it: %s",
1165 /* Note: when the user explicitly configured things with an env var we won't validate the mount
1166 * point. After all we want this to be useful for testing. */
1170 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
1172 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1175 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
1179 /* No logging here */
1196 static int verify_xbootldr_blkid(
1199 sd_id128_t
*ret_uuid
) {
1201 sd_id128_t uuid
= SD_ID128_NULL
;
1204 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
1205 _cleanup_free_
char *node
= NULL
;
1209 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
1211 return log_error_errno(r
, "Failed to format major/minor device path: %m");
1213 b
= blkid_new_probe_from_filename(node
);
1215 return log_error_errno(errno
?: SYNTHETIC_ERRNO(ENOMEM
), "Failed to open file system \"%s\": %m", node
);
1217 blkid_probe_enable_partitions(b
, 1);
1218 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
1221 r
= blkid_do_safeprobe(b
);
1223 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" is ambiguous.", node
);
1225 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" does not contain a label.", node
);
1227 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system \"%s\": %m", node
);
1230 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
1232 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition scheme of \"%s\": %m", node
);
1233 if (streq(v
, "gpt")) {
1236 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
1238 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition type UUID of \"%s\": %m", node
);
1239 if (!streq(v
, "bc13c2ff-59e6-4262-a352-b275fd6f7172"))
1240 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1241 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1242 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1245 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
1247 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition entry UUID of \"%s\": %m", node
);
1248 r
= sd_id128_from_string(v
, &uuid
);
1250 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
1252 } else if (streq(v
, "dos")) {
1255 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
1257 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition type UUID of \"%s\": %m", node
);
1258 if (!streq(v
, "0xea"))
1259 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1260 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1261 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1264 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1265 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1266 "File system \"%s\" is not on a GPT or DOS partition table.", node
);
1275 static int verify_xbootldr_udev(
1278 sd_id128_t
*ret_uuid
) {
1280 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1281 _cleanup_free_
char *node
= NULL
;
1282 sd_id128_t uuid
= SD_ID128_NULL
;
1286 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
1288 return log_error_errno(r
, "Failed to format major/minor device path: %m");
1290 r
= sd_device_new_from_devnum(&d
, 'b', devid
);
1292 return log_error_errno(r
, "Failed to get device from device number: %m");
1294 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SCHEME", &v
);
1296 return log_error_errno(r
, "Failed to get device property: %m");
1298 if (streq(v
, "gpt")) {
1300 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
1302 return log_error_errno(r
, "Failed to get device property: %m");
1303 if (!streq(v
, "bc13c2ff-59e6-4262-a352-b275fd6f7172"))
1304 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1305 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1306 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1308 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_UUID", &v
);
1310 return log_error_errno(r
, "Failed to get device property: %m");
1311 r
= sd_id128_from_string(v
, &uuid
);
1313 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
1315 } else if (streq(v
, "dos")) {
1317 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
1319 return log_error_errno(r
, "Failed to get device property: %m");
1320 if (!streq(v
, "0xea"))
1321 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1322 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1323 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1325 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1326 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1327 "File system \"%s\" is not on a GPT or DOS partition table.", node
);
1335 static int verify_xbootldr(
1338 bool unprivileged_mode
,
1339 sd_id128_t
*ret_uuid
) {
1347 relax_checks
= getenv_bool("SYSTEMD_RELAX_XBOOTLDR_CHECKS") > 0;
1349 r
= verify_fsroot_dir(p
, searching
, unprivileged_mode
, &devid
);
1353 if (detect_container() > 0 || relax_checks
)
1356 if (unprivileged_mode
)
1357 return verify_xbootldr_udev(devid
, searching
, ret_uuid
);
1359 return verify_xbootldr_blkid(devid
, searching
, ret_uuid
);
1363 *ret_uuid
= SD_ID128_NULL
;
1368 int find_xbootldr_and_warn(
1370 bool unprivileged_mode
,
1372 sd_id128_t
*ret_uuid
) {
1376 /* Similar to find_esp_and_warn(), but finds the XBOOTLDR partition. Returns the same errors. */
1379 r
= verify_xbootldr(path
, false, unprivileged_mode
, ret_uuid
);
1386 path
= getenv("SYSTEMD_XBOOTLDR_PATH");
1388 if (!path_is_valid(path
) || !path_is_absolute(path
))
1389 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1390 "$SYSTEMD_XBOOTLDR_PATH does not refer to absolute path, refusing to use it: %s",
1396 r
= verify_xbootldr("/boot", true, unprivileged_mode
, ret_uuid
);
1401 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
1420 static const char* const boot_entry_type_table
[_BOOT_ENTRY_MAX
] = {
1421 [BOOT_ENTRY_CONF
] = "conf",
1422 [BOOT_ENTRY_UNIFIED
] = "unified",
1423 [BOOT_ENTRY_LOADER
] = "loader",
1426 DEFINE_STRING_TABLE_LOOKUP(boot_entry_type
, BootEntryType
);