1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/magic.h>
8 #include "alloc-util.h"
9 #include "blkid-util.h"
11 #include "conf-files.h"
13 #include "device-nodes.h"
18 #include "parse-util.h"
19 #include "path-util.h"
20 #include "stat-util.h"
21 #include "string-util.h"
25 static void boot_entry_free(BootEntry
*entry
) {
31 free(entry
->show_title
);
33 free(entry
->machine_id
);
34 free(entry
->architecture
);
35 strv_free(entry
->options
);
38 strv_free(entry
->initrd
);
39 free(entry
->device_tree
);
42 static int boot_entry_load(const char *path
, BootEntry
*entry
) {
43 _cleanup_(boot_entry_free
) BootEntry tmp
= {};
44 _cleanup_fclose_
FILE *f
= NULL
;
52 c
= endswith_no_case(path
, ".conf");
54 log_error("Invalid loader entry filename: %s", path
);
59 tmp
.id
= strndup(b
, c
- b
);
63 tmp
.path
= strdup(path
);
67 f
= fopen(path
, "re");
69 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
72 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
75 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
79 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
81 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
85 if (IN_SET(*strstrip(buf
), '#', '\0'))
89 r
= extract_first_word(&p
, &field
, " \t", 0);
91 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
95 log_warning("%s:%u: Bad syntax", path
, line
);
99 if (streq(field
, "title"))
100 r
= free_and_strdup(&tmp
.title
, p
);
101 else if (streq(field
, "version"))
102 r
= free_and_strdup(&tmp
.version
, p
);
103 else if (streq(field
, "machine-id"))
104 r
= free_and_strdup(&tmp
.machine_id
, p
);
105 else if (streq(field
, "architecture"))
106 r
= free_and_strdup(&tmp
.architecture
, p
);
107 else if (streq(field
, "options"))
108 r
= strv_extend(&tmp
.options
, p
);
109 else if (streq(field
, "linux"))
110 r
= free_and_strdup(&tmp
.kernel
, p
);
111 else if (streq(field
, "efi"))
112 r
= free_and_strdup(&tmp
.efi
, p
);
113 else if (streq(field
, "initrd"))
114 r
= strv_extend(&tmp
.initrd
, p
);
115 else if (streq(field
, "devicetree"))
116 r
= free_and_strdup(&tmp
.device_tree
, p
);
118 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
122 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
126 tmp
= (BootEntry
) {};
130 void boot_config_free(BootConfig
*config
) {
135 free(config
->default_pattern
);
136 free(config
->timeout
);
137 free(config
->editor
);
138 free(config
->auto_entries
);
139 free(config
->auto_firmware
);
141 free(config
->entry_oneshot
);
142 free(config
->entry_default
);
144 for (i
= 0; i
< config
->n_entries
; i
++)
145 boot_entry_free(config
->entries
+ i
);
146 free(config
->entries
);
149 static int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
150 _cleanup_fclose_
FILE *f
= NULL
;
157 f
= fopen(path
, "re");
162 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
166 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
169 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
173 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
175 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
179 if (IN_SET(*strstrip(buf
), '#', '\0'))
183 r
= extract_first_word(&p
, &field
, " \t", 0);
185 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
189 log_warning("%s:%u: Bad syntax", path
, line
);
193 if (streq(field
, "default"))
194 r
= free_and_strdup(&config
->default_pattern
, p
);
195 else if (streq(field
, "timeout"))
196 r
= free_and_strdup(&config
->timeout
, p
);
197 else if (streq(field
, "editor"))
198 r
= free_and_strdup(&config
->editor
, p
);
199 else if (streq(field
, "auto-entries"))
200 r
= free_and_strdup(&config
->auto_entries
, p
);
201 else if (streq(field
, "auto-firmware"))
202 r
= free_and_strdup(&config
->auto_firmware
, p
);
203 else if (streq(field
, "console-mode"))
204 r
= free_and_strdup(&config
->console_mode
, p
);
206 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
210 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
216 static int boot_entry_compare(const BootEntry
*a
, const BootEntry
*b
) {
217 return str_verscmp(a
->id
, b
->id
);
220 static int boot_entries_find(const char *dir
, BootEntry
**ret_entries
, size_t *ret_n_entries
) {
221 _cleanup_strv_free_
char **files
= NULL
;
224 BootEntry
*array
= NULL
;
225 size_t n_allocated
= 0, n
= 0;
229 assert(ret_n_entries
);
231 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
233 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
235 STRV_FOREACH(f
, files
) {
236 if (!GREEDY_REALLOC0(array
, n_allocated
, n
+ 1))
239 r
= boot_entry_load(*f
, array
+ n
);
246 typesafe_qsort(array
, n
, boot_entry_compare
);
248 *ret_entries
= array
;
254 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
256 bool non_unique
= false;
258 assert(entries
|| n_entries
== 0);
259 assert(arr
|| n_entries
== 0);
261 for (i
= 0; i
< n_entries
; i
++)
264 for (i
= 0; i
< n_entries
; i
++)
265 for (j
= 0; j
< n_entries
; j
++)
266 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
267 boot_entry_title(entries
+ j
)))
268 non_unique
= arr
[i
] = arr
[j
] = true;
273 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
279 assert(entries
|| n_entries
== 0);
281 /* Find _all_ non-unique titles */
282 if (!find_nonunique(entries
, n_entries
, arr
))
285 /* Add version to non-unique titles */
286 for (i
= 0; i
< n_entries
; i
++)
287 if (arr
[i
] && entries
[i
].version
) {
288 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
292 free_and_replace(entries
[i
].show_title
, s
);
295 if (!find_nonunique(entries
, n_entries
, arr
))
298 /* Add machine-id to non-unique titles */
299 for (i
= 0; i
< n_entries
; i
++)
300 if (arr
[i
] && entries
[i
].machine_id
) {
301 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
305 free_and_replace(entries
[i
].show_title
, s
);
308 if (!find_nonunique(entries
, n_entries
, arr
))
311 /* Add file name to non-unique titles */
312 for (i
= 0; i
< n_entries
; i
++)
314 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].id
);
318 free_and_replace(entries
[i
].show_title
, s
);
324 static int boot_entries_select_default(const BootConfig
*config
) {
329 if (config
->entry_oneshot
)
330 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
331 if (streq(config
->entry_oneshot
, config
->entries
[i
].id
)) {
332 log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
333 config
->entries
[i
].id
);
337 if (config
->entry_default
)
338 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
339 if (streq(config
->entry_default
, config
->entries
[i
].id
)) {
340 log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
341 config
->entries
[i
].id
);
345 if (config
->default_pattern
)
346 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
347 if (fnmatch(config
->default_pattern
, config
->entries
[i
].id
, FNM_CASEFOLD
) == 0) {
348 log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
349 config
->entries
[i
].id
, config
->default_pattern
);
353 if (config
->n_entries
> 0)
354 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].id
);
356 log_debug("Found no default boot entry :(");
358 return config
->n_entries
- 1; /* -1 means "no default" */
361 int boot_entries_load_config(const char *esp_path
, BootConfig
*config
) {
368 p
= strjoina(esp_path
, "/loader/loader.conf");
369 r
= boot_loader_read_conf(p
, config
);
373 p
= strjoina(esp_path
, "/loader/entries");
374 r
= boot_entries_find(p
, &config
->entries
, &config
->n_entries
);
378 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
380 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
383 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
384 if (r
< 0 && r
!= -ENOENT
)
385 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
387 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
388 if (r
< 0 && r
!= -ENOENT
)
389 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
392 config
->default_entry
= boot_entries_select_default(config
);
396 /********************************************************************************/
398 static int verify_esp(
401 bool unprivileged_mode
,
403 uint64_t *ret_pstart
,
405 sd_id128_t
*ret_uuid
) {
407 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
408 _cleanup_free_
char *node
= NULL
;
411 uint64_t pstart
= 0, psize
= 0;
415 sd_id128_t uuid
= SD_ID128_NULL
;
422 relax_checks
= getenv_bool("SYSTEMD_RELAX_ESP_CHECKS") > 0;
424 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
425 * issues. Let's also, silence the error messages. */
428 if (statfs(p
, &sfs
) < 0) {
429 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
430 if (errno
== ENOENT
&& searching
)
433 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
434 "Failed to check file system type of \"%s\": %m", p
);
437 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
)) {
439 return -EADDRNOTAVAIL
;
441 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
446 if (stat(p
, &st
) < 0)
447 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
448 "Failed to determine block device node of \"%s\": %m", p
);
450 if (major(st
.st_dev
) == 0) {
451 log_error("Block device node of %p is invalid.", p
);
455 t2
= strjoina(p
, "/..");
458 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
459 "Failed to determine block device node of parent of \"%s\": %m", p
);
461 if (st
.st_dev
== st2
.st_dev
) {
462 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p
);
466 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
467 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
468 if (detect_container() > 0 || unprivileged_mode
|| relax_checks
)
472 r
= device_path_make_major_minor(S_IFBLK
, st
.st_dev
, &node
);
474 return log_error_errno(r
, "Failed to format major/minor device path: %m");
476 b
= blkid_new_probe_from_filename(node
);
478 return log_error_errno(errno
?: ENOMEM
, "Failed to open file system \"%s\": %m", p
);
480 blkid_probe_enable_superblocks(b
, 1);
481 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
482 blkid_probe_enable_partitions(b
, 1);
483 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
486 r
= blkid_do_safeprobe(b
);
488 log_error("File system \"%s\" is ambiguous.", p
);
491 log_error("File system \"%s\" does not contain a label.", p
);
494 return log_error_errno(errno
?: EIO
, "Failed to probe file system \"%s\": %m", p
);
497 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
499 return log_error_errno(errno
?: EIO
, "Failed to probe file system type \"%s\": %m", p
);
500 if (!streq(v
, "vfat")) {
501 log_error("File system \"%s\" is not FAT.", p
);
506 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
508 return log_error_errno(errno
?: EIO
, "Failed to probe partition scheme \"%s\": %m", p
);
509 if (!streq(v
, "gpt")) {
510 log_error("File system \"%s\" is not on a GPT partition table.", p
);
515 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
517 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID \"%s\": %m", p
);
518 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
519 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p
);
524 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
526 return log_error_errno(errno
?: EIO
, "Failed to probe partition entry UUID \"%s\": %m", p
);
527 r
= sd_id128_from_string(v
, &uuid
);
529 log_error("Partition \"%s\" has invalid UUID \"%s\".", p
, v
);
534 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
536 return log_error_errno(errno
?: EIO
, "Failed to probe partition number \"%s\": m", p
);
537 r
= safe_atou32(v
, &part
);
539 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
542 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
544 return log_error_errno(errno
?: EIO
, "Failed to probe partition offset \"%s\": %m", p
);
545 r
= safe_atou64(v
, &pstart
);
547 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
550 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
552 return log_error_errno(errno
?: EIO
, "Failed to probe partition size \"%s\": %m", p
);
553 r
= safe_atou64(v
, &psize
);
555 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
562 *ret_pstart
= pstart
;
571 int find_esp_and_warn(
573 bool unprivileged_mode
,
576 uint64_t *ret_pstart
,
578 sd_id128_t
*ret_uuid
) {
582 /* This logs about all errors except:
584 * -ENOKEY → when we can't find the partition
585 * -EACCESS → when unprivileged_mode is true, and we can't access something
589 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
596 path
= getenv("SYSTEMD_ESP_PATH");
598 if (!path_is_valid(path
) || !path_is_absolute(path
))
599 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
600 "$SYSTEMD_ESP_PATH does not refer to absolute path, refusing to use it: %s",
603 /* Note: when the user explicitly configured things with an env var we won't validate the mount
604 * point. After all we want this to be useful for testing. */
608 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
610 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
613 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
617 /* No logging here */
634 int find_default_boot_entry(
635 const char *esp_path
,
638 const BootEntry
**e
) {
640 _cleanup_free_
char *where
= NULL
;
646 r
= find_esp_and_warn(esp_path
, false, &where
, NULL
, NULL
, NULL
, NULL
);
650 r
= boot_entries_load_config(where
, config
);
652 return log_error_errno(r
, "Failed to load bootspec config from \"%s/loader\": %m", where
);
654 if (config
->default_entry
< 0)
655 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
656 "No entry suitable as default, refusing to guess.");
658 *e
= &config
->entries
[config
->default_entry
];
659 log_debug("Found default boot entry in file \"%s\"", (*e
)->path
);
662 *esp_where
= TAKE_PTR(where
);