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 filename: %s", path
);
72 tmp
.id
= strndup(b
, c
- b
);
76 tmp
.path
= strdup(path
);
80 tmp
.root
= strdup(root
);
84 f
= fopen(path
, "re");
86 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
89 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
92 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
96 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
98 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
102 if (IN_SET(*strstrip(buf
), '#', '\0'))
106 r
= extract_first_word(&p
, &field
, " \t", 0);
108 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
112 log_warning("%s:%u: Bad syntax", path
, line
);
116 if (streq(field
, "title"))
117 r
= free_and_strdup(&tmp
.title
, p
);
118 else if (streq(field
, "version"))
119 r
= free_and_strdup(&tmp
.version
, p
);
120 else if (streq(field
, "machine-id"))
121 r
= free_and_strdup(&tmp
.machine_id
, p
);
122 else if (streq(field
, "architecture"))
123 r
= free_and_strdup(&tmp
.architecture
, p
);
124 else if (streq(field
, "options"))
125 r
= strv_extend(&tmp
.options
, p
);
126 else if (streq(field
, "linux"))
127 r
= free_and_strdup(&tmp
.kernel
, p
);
128 else if (streq(field
, "efi"))
129 r
= free_and_strdup(&tmp
.efi
, p
);
130 else if (streq(field
, "initrd"))
131 r
= strv_extend(&tmp
.initrd
, p
);
132 else if (streq(field
, "devicetree"))
133 r
= free_and_strdup(&tmp
.device_tree
, p
);
135 log_notice("%s:%u: Unknown line \"%s\", ignoring.", path
, line
, field
);
139 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
143 tmp
= (BootEntry
) {};
147 void boot_config_free(BootConfig
*config
) {
152 free(config
->default_pattern
);
153 free(config
->timeout
);
154 free(config
->editor
);
155 free(config
->auto_entries
);
156 free(config
->auto_firmware
);
158 free(config
->entry_oneshot
);
159 free(config
->entry_default
);
161 for (i
= 0; i
< config
->n_entries
; i
++)
162 boot_entry_free(config
->entries
+ i
);
163 free(config
->entries
);
166 static int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
167 _cleanup_fclose_
FILE *f
= NULL
;
174 f
= fopen(path
, "re");
179 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
183 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
186 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
190 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
192 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
196 if (IN_SET(*strstrip(buf
), '#', '\0'))
200 r
= extract_first_word(&p
, &field
, " \t", 0);
202 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
206 log_warning("%s:%u: Bad syntax", path
, line
);
210 if (streq(field
, "default"))
211 r
= free_and_strdup(&config
->default_pattern
, p
);
212 else if (streq(field
, "timeout"))
213 r
= free_and_strdup(&config
->timeout
, p
);
214 else if (streq(field
, "editor"))
215 r
= free_and_strdup(&config
->editor
, p
);
216 else if (streq(field
, "auto-entries"))
217 r
= free_and_strdup(&config
->auto_entries
, p
);
218 else if (streq(field
, "auto-firmware"))
219 r
= free_and_strdup(&config
->auto_firmware
, p
);
220 else if (streq(field
, "console-mode"))
221 r
= free_and_strdup(&config
->console_mode
, p
);
223 log_notice("%s:%u: Unknown line \"%s\", ignoring.", path
, line
, field
);
227 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
233 static int boot_entry_compare(const BootEntry
*a
, const BootEntry
*b
) {
234 return str_verscmp(a
->id
, b
->id
);
237 static int boot_entries_find(
243 _cleanup_strv_free_
char **files
= NULL
;
244 size_t n_allocated
= *n_entries
;
253 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
255 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
257 STRV_FOREACH(f
, files
) {
258 if (!GREEDY_REALLOC0(*entries
, n_allocated
, *n_entries
+ 1))
261 r
= boot_entry_load(root
, *f
, *entries
+ *n_entries
);
271 static int boot_entry_load_unified(
274 const char *osrelease
,
278 _cleanup_free_
char *os_pretty_name
= NULL
, *os_id
= NULL
, *version_id
= NULL
, *build_id
= NULL
;
279 _cleanup_(boot_entry_free
) BootEntry tmp
= {
280 .type
= BOOT_ENTRY_UNIFIED
,
282 _cleanup_fclose_
FILE *f
= NULL
;
290 k
= path_startswith(path
, root
);
292 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Path is not below root: %s", path
);
294 f
= fmemopen((void*) osrelease
, strlen(osrelease
), "r");
296 return log_error_errno(errno
, "Failed to open os-release buffer: %m");
298 r
= parse_env_file(f
, "os-release",
299 "PRETTY_NAME", &os_pretty_name
,
301 "VERSION_ID", &version_id
,
302 "BUILD_ID", &build_id
);
304 return log_error_errno(r
, "Failed to parse os-release data from unified kernel image %s: %m", path
);
306 if (!os_pretty_name
|| !os_id
|| !(version_id
|| build_id
))
307 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Missing fields in os-release data from unified kernel image %s, refusing.", path
);
309 tmp
.id
= strjoin(os_id
, "-", version_id
?: build_id
);
313 tmp
.path
= strdup(path
);
317 tmp
.root
= strdup(root
);
321 tmp
.kernel
= strdup(skip_leading_chars(k
, "/"));
325 tmp
.options
= strv_new(skip_leading_chars(cmdline
, WHITESPACE
));
329 delete_trailing_chars(tmp
.options
[0], WHITESPACE
);
331 tmp
.title
= TAKE_PTR(os_pretty_name
);
334 tmp
= (BootEntry
) {};
338 /* Maximum PE section we are willing to load (Note that sections we are not interested in may be larger, but
339 * the ones we do care about and we are willing to load into memory have this size limit.) */
340 #define PE_SECTION_SIZE_MAX (4U*1024U*1024U)
342 static int find_sections(
344 char **ret_osrelease
,
345 char **ret_cmdline
) {
347 _cleanup_free_
struct PeSectionHeader
*sections
= NULL
;
348 _cleanup_free_
char *osrelease
= NULL
, *cmdline
= NULL
;
349 size_t i
, n_sections
;
350 struct DosFileHeader dos
;
355 n
= pread(fd
, &dos
, sizeof(dos
), 0);
357 return log_error_errno(errno
, "Failed read DOS header: %m");
358 if (n
!= sizeof(dos
))
359 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading DOS header, refusing.");
361 if (dos
.Magic
[0] != 'M' || dos
.Magic
[1] != 'Z')
362 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "DOS executable magic missing, refusing.");
364 start
= unaligned_read_le32(&dos
.ExeHeader
);
365 n
= pread(fd
, &pe
, sizeof(pe
), start
);
367 return log_error_errno(errno
, "Failed to read PE header: %m");
369 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading PE header, refusing.");
371 if (pe
.Magic
[0] != 'P' || pe
.Magic
[1] != 'E' || pe
.Magic
[2] != 0 || pe
.Magic
[3] != 0)
372 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "PE executable magic missing, refusing.");
374 n_sections
= unaligned_read_le16(&pe
.FileHeader
.NumberOfSections
);
376 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "PE header has too many sections, refusing.");
378 sections
= new(struct PeSectionHeader
, n_sections
);
382 n
= pread(fd
, sections
,
383 n_sections
* sizeof(struct PeSectionHeader
),
384 start
+ sizeof(pe
) + unaligned_read_le16(&pe
.FileHeader
.SizeOfOptionalHeader
));
386 return log_error_errno(errno
, "Failed to read section data: %m");
387 if ((size_t) n
!= n_sections
* sizeof(struct PeSectionHeader
))
388 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading sections, refusing.");
390 for (i
= 0; i
< n_sections
; i
++) {
391 _cleanup_free_
char *k
= NULL
;
392 uint32_t offset
, size
;
395 if (strneq((char*) sections
[i
].Name
, ".osrel", sizeof(sections
[i
].Name
)))
397 else if (strneq((char*) sections
[i
].Name
, ".cmdline", sizeof(sections
[i
].Name
)))
403 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Duplicate section %s, refusing.", sections
[i
].Name
);
405 offset
= unaligned_read_le32(§ions
[i
].PointerToRawData
);
406 size
= unaligned_read_le32(§ions
[i
].VirtualSize
);
408 if (size
> PE_SECTION_SIZE_MAX
)
409 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Section %s too large, refusing.", sections
[i
].Name
);
411 k
= new(char, size
+1);
415 n
= pread(fd
, k
, size
, offset
);
417 return log_error_errno(errno
, "Failed to read section payload: %m");
419 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading section payload, refusing:");
421 /* Allow one trailing NUL byte, but nothing more. */
422 if (size
> 0 && memchr(k
, 0, size
- 1))
423 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Section contains embedded NUL byte: %m");
430 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Image lacks .osrel section, refusing.");
433 *ret_osrelease
= TAKE_PTR(osrelease
);
435 *ret_cmdline
= TAKE_PTR(cmdline
);
440 static int boot_entries_find_unified(
446 _cleanup_(closedirp
) DIR *d
= NULL
;
447 size_t n_allocated
= *n_entries
;
461 return log_error_errno(errno
, "Failed to open %s: %m", dir
);
464 FOREACH_DIRENT(de
, d
, return log_error_errno(errno
, "Failed to read %s: %m", dir
)) {
465 _cleanup_free_
char *j
= NULL
, *osrelease
= NULL
, *cmdline
= NULL
;
466 _cleanup_close_
int fd
= -1;
468 if (!dirent_is_file(de
))
471 if (!endswith_no_case(de
->d_name
, ".efi"))
474 if (!GREEDY_REALLOC0(*entries
, n_allocated
, *n_entries
+ 1))
477 fd
= openat(dirfd(d
), de
->d_name
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
479 log_warning_errno(errno
, "Failed to open %s/%s, ignoring: %m", dir
, de
->d_name
);
483 r
= fd_verify_regular(fd
);
485 log_warning_errno(r
, "File %s/%s is not regular, ignoring: %m", dir
, de
->d_name
);
489 r
= find_sections(fd
, &osrelease
, &cmdline
);
493 j
= path_join(dir
, de
->d_name
);
497 r
= boot_entry_load_unified(root
, j
, osrelease
, cmdline
, *entries
+ *n_entries
);
507 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
509 bool non_unique
= false;
511 assert(entries
|| n_entries
== 0);
512 assert(arr
|| n_entries
== 0);
514 for (i
= 0; i
< n_entries
; i
++)
517 for (i
= 0; i
< n_entries
; i
++)
518 for (j
= 0; j
< n_entries
; j
++)
519 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
520 boot_entry_title(entries
+ j
)))
521 non_unique
= arr
[i
] = arr
[j
] = true;
526 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
532 assert(entries
|| n_entries
== 0);
534 /* Find _all_ non-unique titles */
535 if (!find_nonunique(entries
, n_entries
, arr
))
538 /* Add version to non-unique titles */
539 for (i
= 0; i
< n_entries
; i
++)
540 if (arr
[i
] && entries
[i
].version
) {
541 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
545 free_and_replace(entries
[i
].show_title
, s
);
548 if (!find_nonunique(entries
, n_entries
, arr
))
551 /* Add machine-id to non-unique titles */
552 for (i
= 0; i
< n_entries
; i
++)
553 if (arr
[i
] && entries
[i
].machine_id
) {
554 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
558 free_and_replace(entries
[i
].show_title
, s
);
561 if (!find_nonunique(entries
, n_entries
, arr
))
564 /* Add file name to non-unique titles */
565 for (i
= 0; i
< n_entries
; i
++)
567 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].id
);
571 free_and_replace(entries
[i
].show_title
, s
);
577 static int boot_entries_select_default(const BootConfig
*config
) {
582 if (config
->entry_oneshot
)
583 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
584 if (streq(config
->entry_oneshot
, config
->entries
[i
].id
)) {
585 log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
586 config
->entries
[i
].id
);
590 if (config
->entry_default
)
591 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
592 if (streq(config
->entry_default
, config
->entries
[i
].id
)) {
593 log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
594 config
->entries
[i
].id
);
598 if (config
->default_pattern
)
599 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
600 if (fnmatch(config
->default_pattern
, config
->entries
[i
].id
, FNM_CASEFOLD
) == 0) {
601 log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
602 config
->entries
[i
].id
, config
->default_pattern
);
606 if (config
->n_entries
> 0)
607 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].id
);
609 log_debug("Found no default boot entry :(");
611 return config
->n_entries
- 1; /* -1 means "no default" */
614 int boot_entries_load_config(
615 const char *esp_path
,
616 const char *xbootldr_path
,
617 BootConfig
*config
) {
625 p
= strjoina(esp_path
, "/loader/loader.conf");
626 r
= boot_loader_read_conf(p
, config
);
630 p
= strjoina(esp_path
, "/loader/entries");
631 r
= boot_entries_find(esp_path
, p
, &config
->entries
, &config
->n_entries
);
635 p
= strjoina(esp_path
, "/EFI/Linux/");
636 r
= boot_entries_find_unified(esp_path
, p
, &config
->entries
, &config
->n_entries
);
642 p
= strjoina(xbootldr_path
, "/loader/entries");
643 r
= boot_entries_find(xbootldr_path
, p
, &config
->entries
, &config
->n_entries
);
647 p
= strjoina(xbootldr_path
, "/EFI/Linux/");
648 r
= boot_entries_find_unified(xbootldr_path
, p
, &config
->entries
, &config
->n_entries
);
653 typesafe_qsort(config
->entries
, config
->n_entries
, boot_entry_compare
);
655 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
657 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
660 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
661 if (r
< 0 && r
!= -ENOENT
)
662 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
664 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
665 if (r
< 0 && r
!= -ENOENT
)
666 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
669 config
->default_entry
= boot_entries_select_default(config
);
673 int boot_entries_load_config_auto(
674 const char *override_esp_path
,
675 const char *override_xbootldr_path
,
676 BootConfig
*config
) {
678 _cleanup_free_
char *esp_where
= NULL
, *xbootldr_where
= NULL
;
683 /* This function is similar to boot_entries_load_config(), however we automatically search for the
684 * ESP and the XBOOTLDR partition unless it is explicitly specified. Also, if the user did not pass
685 * an ESP or XBOOTLDR path directly, let's see if /run/boot-loader-entries/ exists. If so, let's
686 * read data from there, as if it was an ESP (i.e. loading both entries and loader.conf data from
687 * it). This allows other boot loaders to pass boot loader entry information to our tools if they
690 if (!override_esp_path
&& !override_xbootldr_path
) {
692 if (access("/run/boot-loader-entries/", F_OK
) < 0) {
694 return log_error_errno(errno
, "Failed to determine whether /run/boot-loader-entries/ exists: %m");
696 r
= boot_entries_load_config("/run/boot-loader-entries/", NULL
, config
);
704 r
= find_esp_and_warn(override_esp_path
, false, &esp_where
, NULL
, NULL
, NULL
, NULL
);
705 if (r
== -ENOKEY
) /* find_esp_and_warn() doesn't warn about this case */
706 return log_error_errno(r
, "Cannot find the ESP partition mount point.");
707 if (r
< 0) /* But it logs about all these cases, hence don't log here again */
710 r
= find_xbootldr_and_warn(override_xbootldr_path
, false, &xbootldr_where
, NULL
);
711 if (r
< 0 && r
!= -ENOKEY
)
712 return r
; /* It's fine if the XBOOTLDR partition doesn't exist, hence we ignore ENOKEY here */
714 r
= boot_entries_load_config(esp_where
, xbootldr_where
, config
);
721 int boot_entries_augment_from_loader(BootConfig
*config
, bool only_auto
) {
723 static const char * const title_table
[] = {
724 /* Pretty names for a few well-known automatically discovered entries. */
726 "auto-windows", "Windows Boot Manager",
727 "auto-efi-shell", "EFI Shell",
728 "auto-efi-default", "EFI Default Loader",
729 "auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface",
732 _cleanup_free_
char **found_by_loader
= NULL
;
739 /* Let's add the entries discovered by the boot loader to the end of our list, unless they are
740 * already included there. */
742 r
= efi_loader_get_entries(&found_by_loader
);
743 if (IN_SET(r
, -ENOENT
, -EOPNOTSUPP
))
744 return log_debug_errno(r
, "Boot loader reported no entries.");
746 return log_error_errno(r
, "Failed to determine entries reported by boot loader: %m");
748 n_allocated
= config
->n_entries
;
750 STRV_FOREACH(i
, found_by_loader
) {
751 _cleanup_free_
char *c
= NULL
, *t
= NULL
;
754 if (boot_config_has_entry(config
, *i
))
757 if (only_auto
&& !startswith(*i
, "auto-"))
764 STRV_FOREACH_PAIR(a
, b
, (char**) title_table
)
772 if (!GREEDY_REALLOC0(config
->entries
, n_allocated
, config
->n_entries
+ 1))
775 config
->entries
[config
->n_entries
++] = (BootEntry
) {
776 .type
= BOOT_ENTRY_LOADER
,
778 .title
= TAKE_PTR(t
),
785 /********************************************************************************/
787 static int verify_esp_blkid(
791 uint64_t *ret_pstart
,
793 sd_id128_t
*ret_uuid
) {
795 sd_id128_t uuid
= SD_ID128_NULL
;
796 uint64_t pstart
= 0, psize
= 0;
800 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
801 _cleanup_free_
char *node
= NULL
;
805 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
807 return log_error_errno(r
, "Failed to format major/minor device path: %m");
810 b
= blkid_new_probe_from_filename(node
);
812 return log_error_errno(errno
?: SYNTHETIC_ERRNO(ENOMEM
), "Failed to open file system \"%s\": %m", node
);
814 blkid_probe_enable_superblocks(b
, 1);
815 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
816 blkid_probe_enable_partitions(b
, 1);
817 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
820 r
= blkid_do_safeprobe(b
);
822 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" is ambiguous.", node
);
824 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" does not contain a label.", node
);
826 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system \"%s\": %m", node
);
829 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
831 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system type of \"%s\": %m", node
);
832 if (!streq(v
, "vfat"))
833 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
834 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
835 "File system \"%s\" is not FAT.", node
);
838 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
840 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition scheme of \"%s\": %m", node
);
841 if (!streq(v
, "gpt"))
842 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
843 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
844 "File system \"%s\" is not on a GPT partition table.", node
);
847 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
849 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID of \"%s\": %m", node
);
850 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"))
851 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
852 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
853 "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node
);
856 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
858 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition entry UUID of \"%s\": %m", node
);
859 r
= sd_id128_from_string(v
, &uuid
);
861 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
864 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
866 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition number of \"%s\": m", node
);
867 r
= safe_atou32(v
, &part
);
869 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
872 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
874 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition offset of \"%s\": %m", node
);
875 r
= safe_atou64(v
, &pstart
);
877 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
880 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
882 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition size of \"%s\": %m", node
);
883 r
= safe_atou64(v
, &psize
);
885 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
891 *ret_pstart
= pstart
;
900 static int verify_esp_udev(
904 uint64_t *ret_pstart
,
906 sd_id128_t
*ret_uuid
) {
908 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
909 _cleanup_free_
char *node
= NULL
;
910 sd_id128_t uuid
= SD_ID128_NULL
;
911 uint64_t pstart
= 0, psize
= 0;
916 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
918 return log_error_errno(r
, "Failed to format major/minor device path: %m");
920 r
= sd_device_new_from_devnum(&d
, 'b', devid
);
922 return log_error_errno(r
, "Failed to get device from device number: %m");
924 r
= sd_device_get_property_value(d
, "ID_FS_TYPE", &v
);
926 return log_error_errno(r
, "Failed to get device property: %m");
927 if (!streq(v
, "vfat"))
928 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
929 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
930 "File system \"%s\" is not FAT.", node
);
932 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SCHEME", &v
);
934 return log_error_errno(r
, "Failed to get device property: %m");
935 if (!streq(v
, "gpt"))
936 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
937 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
938 "File system \"%s\" is not on a GPT partition table.", node
);
940 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
942 return log_error_errno(r
, "Failed to get device property: %m");
943 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"))
944 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
945 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
946 "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node
);
948 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_UUID", &v
);
950 return log_error_errno(r
, "Failed to get device property: %m");
951 r
= sd_id128_from_string(v
, &uuid
);
953 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
955 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &v
);
957 return log_error_errno(r
, "Failed to get device property: %m");
958 r
= safe_atou32(v
, &part
);
960 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
962 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_OFFSET", &v
);
964 return log_error_errno(r
, "Failed to get device property: %m");
965 r
= safe_atou64(v
, &pstart
);
967 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
969 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SIZE", &v
);
971 return log_error_errno(r
, "Failed to get device property: %m");
972 r
= safe_atou64(v
, &psize
);
974 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
979 *ret_pstart
= pstart
;
988 static int verify_fsroot_dir(
991 bool unprivileged_mode
,
995 const char *t2
, *trigger
;
1001 /* So, the ESP and XBOOTLDR partition are commonly located on an autofs mount. stat() on the
1002 * directory won't trigger it, if it is not mounted yet. Let's hence explicitly trigger it here,
1003 * before stat()ing */
1004 trigger
= strjoina(path
, "/trigger"); /* Filename doesn't matter... */
1005 (void) access(trigger
, F_OK
);
1007 if (stat(path
, &st
) < 0)
1008 return log_full_errno((searching
&& errno
== ENOENT
) ||
1009 (unprivileged_mode
&& errno
== EACCES
) ? LOG_DEBUG
: LOG_ERR
, errno
,
1010 "Failed to determine block device node of \"%s\": %m", path
);
1012 if (major(st
.st_dev
) == 0)
1013 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1014 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
1015 "Block device node of \"%s\" is invalid.", path
);
1017 t2
= strjoina(path
, "/..");
1018 if (stat(t2
, &st2
) < 0) {
1019 if (errno
!= EACCES
)
1022 _cleanup_free_
char *parent
= NULL
;
1024 /* If going via ".." didn't work due to EACCESS, then let's determine the parent path
1025 * directly instead. It's not as good, due to symlinks and such, but we can't do
1026 * anything better here. */
1028 parent
= dirname_malloc(path
);
1032 if (stat(parent
, &st2
) < 0)
1039 return log_full_errno(unprivileged_mode
&& r
== -EACCES
? LOG_DEBUG
: LOG_ERR
, r
,
1040 "Failed to determine block device node of parent of \"%s\": %m", path
);
1043 if (st
.st_dev
== st2
.st_dev
)
1044 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1045 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
1046 "Directory \"%s\" is not the root of the file system.", path
);
1049 *ret_dev
= st
.st_dev
;
1054 static int verify_esp(
1057 bool unprivileged_mode
,
1059 uint64_t *ret_pstart
,
1060 uint64_t *ret_psize
,
1061 sd_id128_t
*ret_uuid
) {
1069 /* This logs about all errors, except:
1071 * -ENOENT → if 'searching' is set, and the dir doesn't exist
1072 * -EADDRNOTAVAIL → if 'searching' is set, and the dir doesn't look like an ESP
1073 * -EACESS → if 'unprivileged_mode' is set, and we have trouble acessing the thing
1076 relax_checks
= getenv_bool("SYSTEMD_RELAX_ESP_CHECKS") > 0;
1078 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
1079 * issues. Let's also, silence the error messages. */
1081 if (!relax_checks
) {
1084 if (statfs(p
, &sfs
) < 0)
1085 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
1086 return log_full_errno((searching
&& errno
== ENOENT
) ||
1087 (unprivileged_mode
&& errno
== EACCES
) ? LOG_DEBUG
: LOG_ERR
, errno
,
1088 "Failed to check file system type of \"%s\": %m", p
);
1090 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
))
1091 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1092 SYNTHETIC_ERRNO(searching
? EADDRNOTAVAIL
: ENODEV
),
1093 "File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
1096 r
= verify_fsroot_dir(p
, searching
, unprivileged_mode
, &devid
);
1100 /* In a container we don't have access to block devices, skip this part of the verification, we trust
1101 * the container manager set everything up correctly on its own. */
1102 if (detect_container() > 0 || relax_checks
)
1105 /* If we are unprivileged we ask udev for the metadata about the partition. If we are privileged we
1106 * use blkid instead. Why? Because this code is called from 'bootctl' which is pretty much an
1107 * emergency recovery tool that should also work when udev isn't up (i.e. from the emergency shell),
1108 * however blkid can't work if we have no privileges to access block devices directly, which is why
1109 * we use udev in that case. */
1110 if (unprivileged_mode
)
1111 return verify_esp_udev(devid
, searching
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1113 return verify_esp_blkid(devid
, searching
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1123 *ret_uuid
= SD_ID128_NULL
;
1128 int find_esp_and_warn(
1130 bool unprivileged_mode
,
1133 uint64_t *ret_pstart
,
1134 uint64_t *ret_psize
,
1135 sd_id128_t
*ret_uuid
) {
1139 /* This logs about all errors except:
1141 * -ENOKEY → when we can't find the partition
1142 * -EACCESS → when unprivileged_mode is true, and we can't access something
1146 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1153 path
= getenv("SYSTEMD_ESP_PATH");
1155 if (!path_is_valid(path
) || !path_is_absolute(path
))
1156 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1157 "$SYSTEMD_ESP_PATH does not refer to absolute path, refusing to use it: %s",
1160 /* Note: when the user explicitly configured things with an env var we won't validate the mount
1161 * point. After all we want this to be useful for testing. */
1165 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
1167 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
1170 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
1174 /* No logging here */
1191 static int verify_xbootldr_blkid(
1194 sd_id128_t
*ret_uuid
) {
1196 sd_id128_t uuid
= SD_ID128_NULL
;
1199 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
1200 _cleanup_free_
char *node
= NULL
;
1204 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
1206 return log_error_errno(r
, "Failed to format major/minor device path: %m");
1208 b
= blkid_new_probe_from_filename(node
);
1210 return log_error_errno(errno
?: SYNTHETIC_ERRNO(ENOMEM
), "Failed to open file system \"%s\": %m", node
);
1212 blkid_probe_enable_partitions(b
, 1);
1213 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
1216 r
= blkid_do_safeprobe(b
);
1218 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" is ambiguous.", node
);
1220 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "File system \"%s\" does not contain a label.", node
);
1222 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe file system \"%s\": %m", node
);
1225 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
1227 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition scheme of \"%s\": %m", node
);
1228 if (streq(v
, "gpt")) {
1231 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
1233 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition type UUID of \"%s\": %m", node
);
1234 if (!streq(v
, "bc13c2ff-59e6-4262-a352-b275fd6f7172"))
1235 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1236 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1237 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1240 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
1242 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition entry UUID of \"%s\": %m", node
);
1243 r
= sd_id128_from_string(v
, &uuid
);
1245 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
1247 } else if (streq(v
, "dos")) {
1250 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
1252 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe partition type UUID of \"%s\": %m", node
);
1253 if (!streq(v
, "0xea"))
1254 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1255 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1256 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1259 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1260 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1261 "File system \"%s\" is not on a GPT or DOS partition table.", node
);
1270 static int verify_xbootldr_udev(
1273 sd_id128_t
*ret_uuid
) {
1275 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1276 _cleanup_free_
char *node
= NULL
;
1277 sd_id128_t uuid
= SD_ID128_NULL
;
1281 r
= device_path_make_major_minor(S_IFBLK
, devid
, &node
);
1283 return log_error_errno(r
, "Failed to format major/minor device path: %m");
1285 r
= sd_device_new_from_devnum(&d
, 'b', devid
);
1287 return log_error_errno(r
, "Failed to get device from device number: %m");
1289 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_SCHEME", &v
);
1291 return log_error_errno(r
, "Failed to get device property: %m");
1293 if (streq(v
, "gpt")) {
1295 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
1297 return log_error_errno(r
, "Failed to get device property: %m");
1298 if (!streq(v
, "bc13c2ff-59e6-4262-a352-b275fd6f7172"))
1299 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1300 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1301 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1303 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_UUID", &v
);
1305 return log_error_errno(r
, "Failed to get device property: %m");
1306 r
= sd_id128_from_string(v
, &uuid
);
1308 return log_error_errno(r
, "Partition \"%s\" has invalid UUID \"%s\".", node
, v
);
1310 } else if (streq(v
, "dos")) {
1312 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &v
);
1314 return log_error_errno(r
, "Failed to get device property: %m");
1315 if (!streq(v
, "0xea"))
1316 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1317 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1318 "File system \"%s\" has wrong type for extended boot loader partition.", node
);
1320 return log_full_errno(searching
? LOG_DEBUG
: LOG_ERR
,
1321 searching
? SYNTHETIC_ERRNO(EADDRNOTAVAIL
) : SYNTHETIC_ERRNO(ENODEV
),
1322 "File system \"%s\" is not on a GPT or DOS partition table.", node
);
1330 static int verify_xbootldr(
1333 bool unprivileged_mode
,
1334 sd_id128_t
*ret_uuid
) {
1342 relax_checks
= getenv_bool("SYSTEMD_RELAX_XBOOTLDR_CHECKS") > 0;
1344 r
= verify_fsroot_dir(p
, searching
, unprivileged_mode
, &devid
);
1348 if (detect_container() > 0 || relax_checks
)
1351 if (unprivileged_mode
)
1352 return verify_xbootldr_udev(devid
, searching
, ret_uuid
);
1354 return verify_xbootldr_blkid(devid
, searching
, ret_uuid
);
1358 *ret_uuid
= SD_ID128_NULL
;
1363 int find_xbootldr_and_warn(
1365 bool unprivileged_mode
,
1367 sd_id128_t
*ret_uuid
) {
1371 /* Similar to find_esp_and_warn(), but finds the XBOOTLDR partition. Returns the same errors. */
1374 r
= verify_xbootldr(path
, false, unprivileged_mode
, ret_uuid
);
1381 path
= getenv("SYSTEMD_XBOOTLDR_PATH");
1383 if (!path_is_valid(path
) || !path_is_absolute(path
))
1384 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1385 "$SYSTEMD_XBOOTLDR_PATH does not refer to absolute path, refusing to use it: %s",
1391 r
= verify_xbootldr("/boot", true, unprivileged_mode
, ret_uuid
);
1396 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
1415 int find_default_boot_entry(
1417 const BootEntry
**e
) {
1424 r
= boot_entries_load_config_auto(NULL
, NULL
, config
);
1428 if (config
->default_entry
< 0)
1429 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
1430 "No boot loader entry suitable as default, refusing to guess.");
1432 *e
= &config
->entries
[config
->default_entry
];
1433 log_debug("Found default boot loader entry in file \"%s\"", (*e
)->path
);
1438 static const char* const boot_entry_type_table
[_BOOT_ENTRY_MAX
] = {
1439 [BOOT_ENTRY_CONF
] = "conf",
1440 [BOOT_ENTRY_UNIFIED
] = "unified",
1441 [BOOT_ENTRY_LOADER
] = "loader",
1444 DEFINE_STRING_TABLE_LOOKUP(boot_entry_type
, BootEntryType
);