1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/magic.h>
10 #include "alloc-util.h"
11 #include "blkid-util.h"
13 #include "conf-files.h"
15 #include "device-nodes.h"
16 #include "dirent-util.h"
18 #include "efi-loader.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "pe-header.h"
26 #include "sort-util.h"
27 #include "stat-util.h"
28 #include "string-table.h"
29 #include "string-util.h"
31 #include "unaligned.h"
35 static void boot_entry_free(BootEntry
*entry
) {
42 free(entry
->show_title
);
44 free(entry
->machine_id
);
45 free(entry
->architecture
);
46 strv_free(entry
->options
);
49 strv_free(entry
->initrd
);
50 free(entry
->device_tree
);
53 static int boot_entry_load(
58 _cleanup_(boot_entry_free
) BootEntry tmp
= {
59 .type
= BOOT_ENTRY_CONF
,
62 _cleanup_fclose_
FILE *f
= NULL
;
71 c
= endswith_no_case(path
, ".conf");
73 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid loader entry file suffix: %s", path
);
76 tmp
.id
= strndup(b
, c
- b
);
80 if (!efi_loader_entry_name_valid(tmp
.id
))
81 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid loader entry filename: %s", path
);
83 tmp
.path
= strdup(path
);
87 tmp
.root
= strdup(root
);
91 f
= fopen(path
, "re");
93 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
96 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
99 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
103 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
105 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
109 if (IN_SET(*strstrip(buf
), '#', '\0'))
113 r
= extract_first_word(&p
, &field
, " \t", 0);
115 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
119 log_warning("%s:%u: Bad syntax", path
, line
);
123 if (streq(field
, "title"))
124 r
= free_and_strdup(&tmp
.title
, p
);
125 else if (streq(field
, "version"))
126 r
= free_and_strdup(&tmp
.version
, p
);
127 else if (streq(field
, "machine-id"))
128 r
= free_and_strdup(&tmp
.machine_id
, p
);
129 else if (streq(field
, "architecture"))
130 r
= free_and_strdup(&tmp
.architecture
, p
);
131 else if (streq(field
, "options"))
132 r
= strv_extend(&tmp
.options
, p
);
133 else if (streq(field
, "linux"))
134 r
= free_and_strdup(&tmp
.kernel
, p
);
135 else if (streq(field
, "efi"))
136 r
= free_and_strdup(&tmp
.efi
, p
);
137 else if (streq(field
, "initrd"))
138 r
= strv_extend(&tmp
.initrd
, p
);
139 else if (streq(field
, "devicetree"))
140 r
= free_and_strdup(&tmp
.device_tree
, p
);
142 log_notice("%s:%u: Unknown line \"%s\", ignoring.", path
, line
, field
);
146 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
150 tmp
= (BootEntry
) {};
154 void boot_config_free(BootConfig
*config
) {
159 free(config
->default_pattern
);
160 free(config
->timeout
);
161 free(config
->editor
);
162 free(config
->auto_entries
);
163 free(config
->auto_firmware
);
164 free(config
->console_mode
);
166 free(config
->entry_oneshot
);
167 free(config
->entry_default
);
169 for (i
= 0; i
< config
->n_entries
; i
++)
170 boot_entry_free(config
->entries
+ i
);
171 free(config
->entries
);
174 static int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
175 _cleanup_fclose_
FILE *f
= NULL
;
182 f
= fopen(path
, "re");
187 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
191 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
194 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
198 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
200 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
204 if (IN_SET(*strstrip(buf
), '#', '\0'))
208 r
= extract_first_word(&p
, &field
, " \t", 0);
210 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
214 log_warning("%s:%u: Bad syntax", path
, line
);
218 if (streq(field
, "default"))
219 r
= free_and_strdup(&config
->default_pattern
, p
);
220 else if (streq(field
, "timeout"))
221 r
= free_and_strdup(&config
->timeout
, p
);
222 else if (streq(field
, "editor"))
223 r
= free_and_strdup(&config
->editor
, p
);
224 else if (streq(field
, "auto-entries"))
225 r
= free_and_strdup(&config
->auto_entries
, p
);
226 else if (streq(field
, "auto-firmware"))
227 r
= free_and_strdup(&config
->auto_firmware
, p
);
228 else if (streq(field
, "console-mode"))
229 r
= free_and_strdup(&config
->console_mode
, p
);
231 log_notice("%s:%u: Unknown line \"%s\", ignoring.", path
, line
, field
);
235 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
241 static int boot_entry_compare(const BootEntry
*a
, const BootEntry
*b
) {
242 return str_verscmp(a
->id
, b
->id
);
245 static int boot_entries_find(
251 _cleanup_strv_free_
char **files
= NULL
;
252 size_t n_allocated
= *n_entries
;
261 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
);
263 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
265 STRV_FOREACH(f
, files
) {
266 if (!GREEDY_REALLOC0(*entries
, n_allocated
, *n_entries
+ 1))
269 r
= boot_entry_load(root
, *f
, *entries
+ *n_entries
);
279 static int boot_entry_load_unified(
282 const char *osrelease
,
286 _cleanup_free_
char *os_pretty_name
= NULL
, *os_id
= NULL
, *version_id
= NULL
, *build_id
= NULL
;
287 _cleanup_(boot_entry_free
) BootEntry tmp
= {
288 .type
= BOOT_ENTRY_UNIFIED
,
290 _cleanup_fclose_
FILE *f
= NULL
;
298 k
= path_startswith(path
, root
);
300 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Path is not below root: %s", path
);
302 f
= fmemopen_unlocked((void*) osrelease
, strlen(osrelease
), "r");
304 return log_error_errno(errno
, "Failed to open os-release buffer: %m");
306 r
= parse_env_file(f
, "os-release",
307 "PRETTY_NAME", &os_pretty_name
,
309 "VERSION_ID", &version_id
,
310 "BUILD_ID", &build_id
);
312 return log_error_errno(r
, "Failed to parse os-release data from unified kernel image %s: %m", path
);
314 if (!os_pretty_name
|| !os_id
|| !(version_id
|| build_id
))
315 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Missing fields in os-release data from unified kernel image %s, refusing.", path
);
317 tmp
.id
= strjoin(os_id
, "-", version_id
?: build_id
);
321 if (!efi_loader_entry_name_valid(tmp
.id
))
322 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid loader entry: %s", tmp
.id
);
324 tmp
.path
= strdup(path
);
328 tmp
.root
= strdup(root
);
332 tmp
.kernel
= strdup(skip_leading_chars(k
, "/"));
336 tmp
.options
= strv_new(skip_leading_chars(cmdline
, WHITESPACE
));
340 delete_trailing_chars(tmp
.options
[0], WHITESPACE
);
342 tmp
.title
= TAKE_PTR(os_pretty_name
);
345 tmp
= (BootEntry
) {};
349 /* Maximum PE section we are willing to load (Note that sections we are not interested in may be larger, but
350 * the ones we do care about and we are willing to load into memory have this size limit.) */
351 #define PE_SECTION_SIZE_MAX (4U*1024U*1024U)
353 static int find_sections(
355 char **ret_osrelease
,
356 char **ret_cmdline
) {
358 _cleanup_free_
struct PeSectionHeader
*sections
= NULL
;
359 _cleanup_free_
char *osrelease
= NULL
, *cmdline
= NULL
;
360 size_t i
, n_sections
;
361 struct DosFileHeader dos
;
366 n
= pread(fd
, &dos
, sizeof(dos
), 0);
368 return log_error_errno(errno
, "Failed read DOS header: %m");
369 if (n
!= sizeof(dos
))
370 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading DOS header, refusing.");
372 if (dos
.Magic
[0] != 'M' || dos
.Magic
[1] != 'Z')
373 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "DOS executable magic missing, refusing.");
375 start
= unaligned_read_le32(&dos
.ExeHeader
);
376 n
= pread(fd
, &pe
, sizeof(pe
), start
);
378 return log_error_errno(errno
, "Failed to read PE header: %m");
380 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading PE header, refusing.");
382 if (pe
.Magic
[0] != 'P' || pe
.Magic
[1] != 'E' || pe
.Magic
[2] != 0 || pe
.Magic
[3] != 0)
383 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "PE executable magic missing, refusing.");
385 n_sections
= unaligned_read_le16(&pe
.FileHeader
.NumberOfSections
);
387 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "PE header has too many sections, refusing.");
389 sections
= new(struct PeSectionHeader
, n_sections
);
393 n
= pread(fd
, sections
,
394 n_sections
* sizeof(struct PeSectionHeader
),
395 start
+ sizeof(pe
) + unaligned_read_le16(&pe
.FileHeader
.SizeOfOptionalHeader
));
397 return log_error_errno(errno
, "Failed to read section data: %m");
398 if ((size_t) n
!= n_sections
* sizeof(struct PeSectionHeader
))
399 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading sections, refusing.");
401 for (i
= 0; i
< n_sections
; i
++) {
402 _cleanup_free_
char *k
= NULL
;
403 uint32_t offset
, size
;
406 if (strneq((char*) sections
[i
].Name
, ".osrel", sizeof(sections
[i
].Name
)))
408 else if (strneq((char*) sections
[i
].Name
, ".cmdline", sizeof(sections
[i
].Name
)))
414 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Duplicate section %s, refusing.", sections
[i
].Name
);
416 offset
= unaligned_read_le32(§ions
[i
].PointerToRawData
);
417 size
= unaligned_read_le32(§ions
[i
].VirtualSize
);
419 if (size
> PE_SECTION_SIZE_MAX
)
420 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Section %s too large, refusing.", sections
[i
].Name
);
422 k
= new(char, size
+1);
426 n
= pread(fd
, k
, size
, offset
);
428 return log_error_errno(errno
, "Failed to read section payload: %m");
429 if ((size_t) n
!= size
)
430 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading section payload, refusing:");
432 /* Allow one trailing NUL byte, but nothing more. */
433 if (size
> 0 && memchr(k
, 0, size
- 1))
434 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Section contains embedded NUL byte: %m");
441 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Image lacks .osrel section, refusing.");
444 *ret_osrelease
= TAKE_PTR(osrelease
);
446 *ret_cmdline
= TAKE_PTR(cmdline
);
451 static int boot_entries_find_unified(
457 _cleanup_(closedirp
) DIR *d
= NULL
;
458 size_t n_allocated
= *n_entries
;
472 return log_error_errno(errno
, "Failed to open %s: %m", dir
);
475 FOREACH_DIRENT(de
, d
, return log_error_errno(errno
, "Failed to read %s: %m", dir
)) {
476 _cleanup_free_
char *j
= NULL
, *osrelease
= NULL
, *cmdline
= NULL
;
477 _cleanup_close_
int fd
= -1;
479 dirent_ensure_type(d
, de
);
480 if (!dirent_is_file(de
))
483 if (!endswith_no_case(de
->d_name
, ".efi"))
486 if (!GREEDY_REALLOC0(*entries
, n_allocated
, *n_entries
+ 1))
489 fd
= openat(dirfd(d
), de
->d_name
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
491 log_warning_errno(errno
, "Failed to open %s/%s, ignoring: %m", dir
, de
->d_name
);
495 r
= fd_verify_regular(fd
);
497 log_warning_errno(r
, "File %s/%s is not regular, ignoring: %m", dir
, de
->d_name
);
501 r
= find_sections(fd
, &osrelease
, &cmdline
);
505 j
= path_join(dir
, de
->d_name
);
509 r
= boot_entry_load_unified(root
, j
, osrelease
, cmdline
, *entries
+ *n_entries
);
519 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
521 bool non_unique
= false;
523 assert(entries
|| n_entries
== 0);
524 assert(arr
|| n_entries
== 0);
526 for (i
= 0; i
< n_entries
; i
++)
529 for (i
= 0; i
< n_entries
; i
++)
530 for (j
= 0; j
< n_entries
; j
++)
531 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
532 boot_entry_title(entries
+ j
)))
533 non_unique
= arr
[i
] = arr
[j
] = true;
538 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
544 assert(entries
|| n_entries
== 0);
546 /* Find _all_ non-unique titles */
547 if (!find_nonunique(entries
, n_entries
, arr
))
550 /* Add version to non-unique titles */
551 for (i
= 0; i
< n_entries
; i
++)
552 if (arr
[i
] && entries
[i
].version
) {
553 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
557 free_and_replace(entries
[i
].show_title
, s
);
560 if (!find_nonunique(entries
, n_entries
, arr
))
563 /* Add machine-id to non-unique titles */
564 for (i
= 0; i
< n_entries
; i
++)
565 if (arr
[i
] && entries
[i
].machine_id
) {
566 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
570 free_and_replace(entries
[i
].show_title
, s
);
573 if (!find_nonunique(entries
, n_entries
, arr
))
576 /* Add file name to non-unique titles */
577 for (i
= 0; i
< n_entries
; i
++)
579 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].id
);
583 free_and_replace(entries
[i
].show_title
, s
);
589 static int boot_entries_select_default(const BootConfig
*config
) {
593 assert(config
->entries
|| config
->n_entries
== 0);
595 if (config
->n_entries
== 0) {
596 log_debug("Found no default boot entry :(");
597 return -1; /* -1 means "no default" */
600 if (config
->entry_oneshot
)
601 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
602 if (streq(config
->entry_oneshot
, config
->entries
[i
].id
)) {
603 log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
604 config
->entries
[i
].id
);
608 if (config
->entry_default
)
609 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
610 if (streq(config
->entry_default
, config
->entries
[i
].id
)) {
611 log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
612 config
->entries
[i
].id
);
616 if (config
->default_pattern
)
617 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
618 if (fnmatch(config
->default_pattern
, config
->entries
[i
].id
, FNM_CASEFOLD
) == 0) {
619 log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
620 config
->entries
[i
].id
, config
->default_pattern
);
624 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].id
);
625 return config
->n_entries
- 1;
628 int boot_entries_load_config(
629 const char *esp_path
,
630 const char *xbootldr_path
,
631 BootConfig
*config
) {
639 p
= strjoina(esp_path
, "/loader/loader.conf");
640 r
= boot_loader_read_conf(p
, config
);
644 p
= strjoina(esp_path
, "/loader/entries");
645 r
= boot_entries_find(esp_path
, p
, &config
->entries
, &config
->n_entries
);
649 p
= strjoina(esp_path
, "/EFI/Linux/");
650 r
= boot_entries_find_unified(esp_path
, p
, &config
->entries
, &config
->n_entries
);
656 p
= strjoina(xbootldr_path
, "/loader/entries");
657 r
= boot_entries_find(xbootldr_path
, p
, &config
->entries
, &config
->n_entries
);
661 p
= strjoina(xbootldr_path
, "/EFI/Linux/");
662 r
= boot_entries_find_unified(xbootldr_path
, p
, &config
->entries
, &config
->n_entries
);
667 typesafe_qsort(config
->entries
, config
->n_entries
, boot_entry_compare
);
669 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
671 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
674 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
675 if (r
< 0 && !IN_SET(r
, -ENOENT
, -ENODATA
)) {
676 log_warning_errno(r
, "Failed to read EFI variable \"LoaderEntryOneShot\": %m");
681 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
682 if (r
< 0 && !IN_SET(r
, -ENOENT
, -ENODATA
)) {
683 log_warning_errno(r
, "Failed to read EFI variable \"LoaderEntryDefault\": %m");
689 config
->default_entry
= boot_entries_select_default(config
);
693 int boot_entries_load_config_auto(
694 const char *override_esp_path
,
695 const char *override_xbootldr_path
,
696 BootConfig
*config
) {
698 _cleanup_free_
char *esp_where
= NULL
, *xbootldr_where
= NULL
;
703 /* This function is similar to boot_entries_load_config(), however we automatically search for the
704 * ESP and the XBOOTLDR partition unless it is explicitly specified. Also, if the user did not pass
705 * an ESP or XBOOTLDR path directly, let's see if /run/boot-loader-entries/ exists. If so, let's
706 * read data from there, as if it was an ESP (i.e. loading both entries and loader.conf data from
707 * it). This allows other boot loaders to pass boot loader entry information to our tools if they
710 if (!override_esp_path
&& !override_xbootldr_path
) {
711 if (access("/run/boot-loader-entries/", F_OK
) >= 0)
712 return boot_entries_load_config("/run/boot-loader-entries/", NULL
, config
);
715 return log_error_errno(errno
,
716 "Failed to determine whether /run/boot-loader-entries/ exists: %m");
719 r
= find_esp_and_warn(override_esp_path
, false, &esp_where
, NULL
, NULL
, NULL
, NULL
);
720 if (r
< 0) /* we don't log about ENOKEY here, but propagate it, leaving it to the caller to log */
723 r
= find_xbootldr_and_warn(override_xbootldr_path
, false, &xbootldr_where
, NULL
);
724 if (r
< 0 && r
!= -ENOKEY
)
725 return r
; /* It's fine if the XBOOTLDR partition doesn't exist, hence we ignore ENOKEY here */
727 return boot_entries_load_config(esp_where
, xbootldr_where
, config
);
731 int boot_entries_augment_from_loader(BootConfig
*config
, bool only_auto
) {
732 static const char * const title_table
[] = {
733 /* Pretty names for a few well-known automatically discovered entries. */
735 "auto-windows", "Windows Boot Manager",
736 "auto-efi-shell", "EFI Shell",
737 "auto-efi-default", "EFI Default Loader",
738 "auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface",
741 _cleanup_strv_free_
char **found_by_loader
= NULL
;
748 /* Let's add the entries discovered by the boot loader to the end of our list, unless they are
749 * already included there. */
751 r
= efi_loader_get_entries(&found_by_loader
);
752 if (IN_SET(r
, -ENOENT
, -EOPNOTSUPP
))
753 return log_debug_errno(r
, "Boot loader reported no entries.");
755 return log_error_errno(r
, "Failed to determine entries reported by boot loader: %m");
757 n_allocated
= config
->n_entries
;
759 STRV_FOREACH(i
, found_by_loader
) {
760 _cleanup_free_
char *c
= NULL
, *t
= NULL
, *p
= NULL
;
763 if (boot_config_has_entry(config
, *i
))
766 if (only_auto
&& !startswith(*i
, "auto-"))
773 STRV_FOREACH_PAIR(a
, b
, (char**) title_table
)
781 p
= efi_variable_path(EFI_VENDOR_LOADER
, "LoaderEntries");
785 if (!GREEDY_REALLOC0(config
->entries
, n_allocated
, config
->n_entries
+ 1))
788 config
->entries
[config
->n_entries
++] = (BootEntry
) {
789 .type
= BOOT_ENTRY_LOADER
,
791 .title
= TAKE_PTR(t
),
800 /********************************************************************************/
802 static int verify_esp_blkid(
806 uint64_t *ret_pstart
,
808 sd_id128_t
*ret_uuid
) {
810 sd_id128_t uuid
= SD_ID128_NULL
;
811 uint64_t pstart
= 0, psize
= 0;
815 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
816 _cleanup_free_
char *node
= NULL
;
820 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
822 return log_error_errno(r
, "Failed to format major/minor device path: %m");
825 b
= blkid_new_probe_from_filename(node
);
827 return log_error_errno(errno
?: SYNTHETIC_ERRNO(ENOMEM
), "Failed to open file system \"%s\": %m", node
);
829 blkid_probe_enable_superblocks(b
, 1);
830 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
831 blkid_probe_enable_partitions(b
, 1);
832 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
835 r
= blkid_do_safeprobe(b
);
837 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" is ambiguous.", node
);
839 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" does not contain a label.", node
);
841 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system \"%s\": %m", node
);
844 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
846 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system type of \"%s\": %m", node
);
847 if (!streq(v
, "vfat"))
848 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
849 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
850 "File system \"%s\" is not FAT.", node
);
853 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
855 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition scheme of \"%s\": %m", node
);
856 if (!streq(v
, "gpt"))
857 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
858 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
859 "File system \"%s\" is not on a GPT partition table.", node
);
862 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
864 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID of \"%s\": %m", node
);
865 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"))
866 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
867 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
868 "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node
);
871 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
873 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition entry UUID of \"%s\": %m", node
);
874 r
= sd_id128_from_string(v
, &uuid
);
876 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
879 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
881 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition number of \"%s\": %m", node
);
882 r
= safe_atou32(v
, &part
);
884 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
887 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
889 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition offset of \"%s\": %m", node
);
890 r
= safe_atou64(v
, &pstart
);
892 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
895 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
897 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition size of \"%s\": %m", node
);
898 r
= safe_atou64(v
, &psize
);
900 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
906 *ret_pstart
= pstart
;
915 static int verify_esp_udev(
919 uint64_t *ret_pstart
,
921 sd_id128_t
*ret_uuid
) {
923 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
924 _cleanup_free_
char *node
= NULL
;
925 sd_id128_t uuid
= SD_ID128_NULL
;
926 uint64_t pstart
= 0, psize
= 0;
931 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
933 return log_error_errno(r
, "Failed to format major/minor device path: %m");
935 r
= sd_device_new_from_devnum(&d
, 'b', devid
);
937 return log_error_errno(r
, "Failed to get device from device number: %m");
939 r
= sd_device_get_property_value(d
, "ID_FS_TYPE", &v
);
941 return log_error_errno(r
, "Failed to get device property: %m");
942 if (!streq(v
, "vfat"))
943 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
944 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
945 "File system \"%s\" is not FAT.", node
);
947 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SCHEME", &v
);
949 return log_error_errno(r
, "Failed to get device property: %m");
950 if (!streq(v
, "gpt"))
951 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
952 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
953 "File system \"%s\" is not on a GPT partition table.", node
);
955 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
957 return log_error_errno(r
, "Failed to get device property: %m");
958 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"))
959 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
960 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
961 "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node
);
963 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_UUID", &v
);
965 return log_error_errno(r
, "Failed to get device property: %m");
966 r
= sd_id128_from_string(v
, &uuid
);
968 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
970 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &v
);
972 return log_error_errno(r
, "Failed to get device property: %m");
973 r
= safe_atou32(v
, &part
);
975 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
977 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_OFFSET", &v
);
979 return log_error_errno(r
, "Failed to get device property: %m");
980 r
= safe_atou64(v
, &pstart
);
982 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
984 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SIZE", &v
);
986 return log_error_errno(r
, "Failed to get device property: %m");
987 r
= safe_atou64(v
, &psize
);
989 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
994 *ret_pstart
= pstart
;
1003 static int verify_fsroot_dir(
1006 bool unprivileged_mode
,
1009 struct stat st
, st2
;
1010 const char *t2
, *trigger
;
1016 /* So, the ESP and XBOOTLDR partition are commonly located on an autofs mount. stat() on the
1017 * directory won't trigger it, if it is not mounted yet. Let's hence explicitly trigger it here,
1018 * before stat()ing */
1019 trigger
= strjoina(path
, "/trigger"); /* Filename doesn't matter... */
1020 (void) access(trigger
, F_OK
);
1022 if (stat(path
, &st
) < 0)
1023 return log_full_errno((searching
&& errno
== ENOENT
) ||
1024 (unprivileged_mode
&& errno
== EACCES
) ? LOG_DEBUG
: LOG_ERR
, errno
,
1025 "Failed to determine block device node of \"%s\": %m", path
);
1027 if (major(st
.st_dev
) == 0)
1028 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1029 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
1030 "Block device node of \"%s\" is invalid.", path
);
1032 t2
= strjoina(path
, "/..");
1033 if (stat(t2
, &st2
) < 0) {
1034 if (errno
!= EACCES
)
1037 _cleanup_free_
char *parent
= NULL
;
1039 /* If going via ".." didn't work due to EACCESS, then let's determine the parent path
1040 * directly instead. It's not as good, due to symlinks and such, but we can't do
1041 * anything better here. */
1043 parent
= dirname_malloc(path
);
1047 if (stat(parent
, &st2
) < 0)
1054 return log_full_errno(unprivileged_mode
&& r
== -EACCES
? LOG_DEBUG
: LOG_ERR
, r
,
1055 "Failed to determine block device node of parent of \"%s\": %m", path
);
1058 if (st
.st_dev
== st2
.st_dev
)
1059 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1060 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
1061 "Directory \"%s\" is not the root of the file system.", path
);
1064 *ret_dev
= st
.st_dev
;
1069 static int verify_esp(
1072 bool unprivileged_mode
,
1074 uint64_t *ret_pstart
,
1075 uint64_t *ret_psize
,
1076 sd_id128_t
*ret_uuid
) {
1084 /* This logs about all errors, except:
1086 * -ENOENT → if 'searching' is set, and the dir doesn't exist
1087 * -EADDRNOTAVAIL → if 'searching' is set, and the dir doesn't look like an ESP
1088 * -EACESS → if 'unprivileged_mode' is set, and we have trouble accessing the thing
1091 relax_checks
= getenv_bool("SYSTEMD_RELAX_ESP_CHECKS") > 0;
1093 /* Non-root user can only check the status, so if an error occurred in the following, it does not cause any
1094 * issues. Let's also, silence the error messages. */
1096 if (!relax_checks
) {
1099 if (statfs(p
, &sfs
) < 0)
1100 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
1101 return log_full_errno((searching
&& errno
== ENOENT
) ||
1102 (unprivileged_mode
&& errno
== EACCES
) ? LOG_DEBUG
: LOG_ERR
, errno
,
1103 "Failed to check file system type of \"%s\": %m", p
);
1105 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
))
1106 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1107 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
1108 "File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
1111 r
= verify_fsroot_dir(p
, searching
, unprivileged_mode
, &devid
);
1115 /* In a container we don't have access to block devices, skip this part of the verification, we trust
1116 * the container manager set everything up correctly on its own. */
1117 if (detect_container() > 0 || relax_checks
)
1120 /* If we are unprivileged we ask udev for the metadata about the partition. If we are privileged we
1121 * use blkid instead. Why? Because this code is called from 'bootctl' which is pretty much an
1122 * emergency recovery tool that should also work when udev isn't up (i.e. from the emergency shell),
1123 * however blkid can't work if we have no privileges to access block devices directly, which is why
1124 * we use udev in that case. */
1125 if (unprivileged_mode
)
1126 return verify_esp_udev(devid
, searching
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1128 return verify_esp_blkid(devid
, searching
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1138 *ret_uuid
= SD_ID128_NULL
;
1143 int find_esp_and_warn(
1145 bool unprivileged_mode
,
1148 uint64_t *ret_pstart
,
1149 uint64_t *ret_psize
,
1150 sd_id128_t
*ret_uuid
) {
1154 /* This logs about all errors except:
1156 * -ENOKEY → when we can't find the partition
1157 * -EACCESS → when unprivileged_mode is true, and we can't access something
1161 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1168 path
= getenv("SYSTEMD_ESP_PATH");
1170 if (!path_is_valid(path
) || !path_is_absolute(path
))
1171 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1172 "$SYSTEMD_ESP_PATH does not refer to absolute path, refusing to use it: %s",
1175 /* Note: when the user explicitly configured things with an env var we won't validate the mount
1176 * point. After all we want this to be useful for testing. */
1180 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
1182 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1185 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
1189 /* No logging here */
1206 static int verify_xbootldr_blkid(
1209 sd_id128_t
*ret_uuid
) {
1211 sd_id128_t uuid
= SD_ID128_NULL
;
1214 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
1215 _cleanup_free_
char *node
= NULL
;
1219 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
1221 return log_error_errno(r
, "Failed to format major/minor device path: %m");
1223 b
= blkid_new_probe_from_filename(node
);
1225 return log_error_errno(errno
?: SYNTHETIC_ERRNO(ENOMEM
), "Failed to open file system \"%s\": %m", node
);
1227 blkid_probe_enable_partitions(b
, 1);
1228 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
1231 r
= blkid_do_safeprobe(b
);
1233 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" is ambiguous.", node
);
1235 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" does not contain a label.", node
);
1237 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system \"%s\": %m", node
);
1240 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
1242 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition scheme of \"%s\": %m", node
);
1243 if (streq(v
, "gpt")) {
1246 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
1248 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition type UUID of \"%s\": %m", node
);
1249 if (!streq(v
, "bc13c2ff-59e6-4262-a352-b275fd6f7172"))
1250 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1251 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1252 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1255 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
1257 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition entry UUID of \"%s\": %m", node
);
1258 r
= sd_id128_from_string(v
, &uuid
);
1260 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
1262 } else if (streq(v
, "dos")) {
1265 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
1267 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition type UUID of \"%s\": %m", node
);
1268 if (!streq(v
, "0xea"))
1269 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1270 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1271 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1274 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1275 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1276 "File system \"%s\" is not on a GPT or DOS partition table.", node
);
1285 static int verify_xbootldr_udev(
1288 sd_id128_t
*ret_uuid
) {
1290 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1291 _cleanup_free_
char *node
= NULL
;
1292 sd_id128_t uuid
= SD_ID128_NULL
;
1296 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
1298 return log_error_errno(r
, "Failed to format major/minor device path: %m");
1300 r
= sd_device_new_from_devnum(&d
, 'b', devid
);
1302 return log_error_errno(r
, "Failed to get device from device number: %m");
1304 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SCHEME", &v
);
1306 return log_error_errno(r
, "Failed to get device property: %m");
1308 if (streq(v
, "gpt")) {
1310 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
1312 return log_error_errno(r
, "Failed to get device property: %m");
1313 if (!streq(v
, "bc13c2ff-59e6-4262-a352-b275fd6f7172"))
1314 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1315 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1316 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1318 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_UUID", &v
);
1320 return log_error_errno(r
, "Failed to get device property: %m");
1321 r
= sd_id128_from_string(v
, &uuid
);
1323 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
1325 } else if (streq(v
, "dos")) {
1327 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
1329 return log_error_errno(r
, "Failed to get device property: %m");
1330 if (!streq(v
, "0xea"))
1331 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1332 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1333 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1335 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1336 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1337 "File system \"%s\" is not on a GPT or DOS partition table.", node
);
1345 static int verify_xbootldr(
1348 bool unprivileged_mode
,
1349 sd_id128_t
*ret_uuid
) {
1357 relax_checks
= getenv_bool("SYSTEMD_RELAX_XBOOTLDR_CHECKS") > 0;
1359 r
= verify_fsroot_dir(p
, searching
, unprivileged_mode
, &devid
);
1363 if (detect_container() > 0 || relax_checks
)
1366 if (unprivileged_mode
)
1367 return verify_xbootldr_udev(devid
, searching
, ret_uuid
);
1369 return verify_xbootldr_blkid(devid
, searching
, ret_uuid
);
1373 *ret_uuid
= SD_ID128_NULL
;
1378 int find_xbootldr_and_warn(
1380 bool unprivileged_mode
,
1382 sd_id128_t
*ret_uuid
) {
1386 /* Similar to find_esp_and_warn(), but finds the XBOOTLDR partition. Returns the same errors. */
1389 r
= verify_xbootldr(path
, false, unprivileged_mode
, ret_uuid
);
1396 path
= getenv("SYSTEMD_XBOOTLDR_PATH");
1398 if (!path_is_valid(path
) || !path_is_absolute(path
))
1399 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1400 "$SYSTEMD_XBOOTLDR_PATH does not refer to absolute path, refusing to use it: %s",
1406 r
= verify_xbootldr("/boot", true, unprivileged_mode
, ret_uuid
);
1411 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */