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 "stat-util.h"
20 #include "string-util.h"
24 static void boot_entry_free(BootEntry
*entry
) {
30 free(entry
->show_title
);
32 free(entry
->machine_id
);
33 free(entry
->architecture
);
34 strv_free(entry
->options
);
37 strv_free(entry
->initrd
);
38 free(entry
->device_tree
);
41 static int boot_entry_load(const char *path
, BootEntry
*entry
) {
42 _cleanup_(boot_entry_free
) BootEntry tmp
= {};
43 _cleanup_fclose_
FILE *f
= NULL
;
51 c
= endswith_no_case(path
, ".conf");
53 log_error("Invalid loader entry filename: %s", path
);
58 tmp
.id
= strndup(b
, c
- b
);
62 tmp
.path
= strdup(path
);
66 f
= fopen(path
, "re");
68 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
71 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
74 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
78 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
80 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
84 if (IN_SET(*strstrip(buf
), '#', '\0'))
88 r
= extract_first_word(&p
, &field
, " \t", 0);
90 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
94 log_warning("%s:%u: Bad syntax", path
, line
);
98 if (streq(field
, "title"))
99 r
= free_and_strdup(&tmp
.title
, p
);
100 else if (streq(field
, "version"))
101 r
= free_and_strdup(&tmp
.version
, p
);
102 else if (streq(field
, "machine-id"))
103 r
= free_and_strdup(&tmp
.machine_id
, p
);
104 else if (streq(field
, "architecture"))
105 r
= free_and_strdup(&tmp
.architecture
, p
);
106 else if (streq(field
, "options"))
107 r
= strv_extend(&tmp
.options
, p
);
108 else if (streq(field
, "linux"))
109 r
= free_and_strdup(&tmp
.kernel
, p
);
110 else if (streq(field
, "efi"))
111 r
= free_and_strdup(&tmp
.efi
, p
);
112 else if (streq(field
, "initrd"))
113 r
= strv_extend(&tmp
.initrd
, p
);
114 else if (streq(field
, "devicetree"))
115 r
= free_and_strdup(&tmp
.device_tree
, p
);
117 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
121 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
125 tmp
= (BootEntry
) {};
129 void boot_config_free(BootConfig
*config
) {
134 free(config
->default_pattern
);
135 free(config
->timeout
);
136 free(config
->editor
);
137 free(config
->auto_entries
);
138 free(config
->auto_firmware
);
140 free(config
->entry_oneshot
);
141 free(config
->entry_default
);
143 for (i
= 0; i
< config
->n_entries
; i
++)
144 boot_entry_free(config
->entries
+ i
);
145 free(config
->entries
);
148 static int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
149 _cleanup_fclose_
FILE *f
= NULL
;
156 f
= fopen(path
, "re");
161 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
165 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
168 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
172 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
174 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
178 if (IN_SET(*strstrip(buf
), '#', '\0'))
182 r
= extract_first_word(&p
, &field
, " \t", 0);
184 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
188 log_warning("%s:%u: Bad syntax", path
, line
);
192 if (streq(field
, "default"))
193 r
= free_and_strdup(&config
->default_pattern
, p
);
194 else if (streq(field
, "timeout"))
195 r
= free_and_strdup(&config
->timeout
, p
);
196 else if (streq(field
, "editor"))
197 r
= free_and_strdup(&config
->editor
, p
);
198 else if (streq(field
, "auto-entries"))
199 r
= free_and_strdup(&config
->auto_entries
, p
);
200 else if (streq(field
, "auto-firmware"))
201 r
= free_and_strdup(&config
->auto_firmware
, p
);
202 else if (streq(field
, "console-mode"))
203 r
= free_and_strdup(&config
->console_mode
, p
);
205 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
209 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
215 static int boot_entry_compare(const BootEntry
*a
, const BootEntry
*b
) {
216 return str_verscmp(a
->id
, b
->id
);
219 static int boot_entries_find(const char *dir
, BootEntry
**ret_entries
, size_t *ret_n_entries
) {
220 _cleanup_strv_free_
char **files
= NULL
;
223 BootEntry
*array
= NULL
;
224 size_t n_allocated
= 0, n
= 0;
228 assert(ret_n_entries
);
230 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
232 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
234 STRV_FOREACH(f
, files
) {
235 if (!GREEDY_REALLOC0(array
, n_allocated
, n
+ 1))
238 r
= boot_entry_load(*f
, array
+ n
);
245 typesafe_qsort(array
, n
, boot_entry_compare
);
247 *ret_entries
= array
;
253 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
255 bool non_unique
= false;
257 assert(entries
|| n_entries
== 0);
258 assert(arr
|| n_entries
== 0);
260 for (i
= 0; i
< n_entries
; i
++)
263 for (i
= 0; i
< n_entries
; i
++)
264 for (j
= 0; j
< n_entries
; j
++)
265 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
266 boot_entry_title(entries
+ j
)))
267 non_unique
= arr
[i
] = arr
[j
] = true;
272 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
278 assert(entries
|| n_entries
== 0);
280 /* Find _all_ non-unique titles */
281 if (!find_nonunique(entries
, n_entries
, arr
))
284 /* Add version to non-unique titles */
285 for (i
= 0; i
< n_entries
; i
++)
286 if (arr
[i
] && entries
[i
].version
) {
287 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
291 free_and_replace(entries
[i
].show_title
, s
);
294 if (!find_nonunique(entries
, n_entries
, arr
))
297 /* Add machine-id to non-unique titles */
298 for (i
= 0; i
< n_entries
; i
++)
299 if (arr
[i
] && entries
[i
].machine_id
) {
300 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
304 free_and_replace(entries
[i
].show_title
, s
);
307 if (!find_nonunique(entries
, n_entries
, arr
))
310 /* Add file name to non-unique titles */
311 for (i
= 0; i
< n_entries
; i
++)
313 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].id
);
317 free_and_replace(entries
[i
].show_title
, s
);
323 static int boot_entries_select_default(const BootConfig
*config
) {
328 if (config
->entry_oneshot
)
329 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
330 if (streq(config
->entry_oneshot
, config
->entries
[i
].id
)) {
331 log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
332 config
->entries
[i
].id
);
336 if (config
->entry_default
)
337 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
338 if (streq(config
->entry_default
, config
->entries
[i
].id
)) {
339 log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
340 config
->entries
[i
].id
);
344 if (config
->default_pattern
)
345 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
346 if (fnmatch(config
->default_pattern
, config
->entries
[i
].id
, FNM_CASEFOLD
) == 0) {
347 log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
348 config
->entries
[i
].id
, config
->default_pattern
);
352 if (config
->n_entries
> 0)
353 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].id
);
355 log_debug("Found no default boot entry :(");
357 return config
->n_entries
- 1; /* -1 means "no default" */
360 int boot_entries_load_config(const char *esp_path
, BootConfig
*config
) {
367 p
= strjoina(esp_path
, "/loader/loader.conf");
368 r
= boot_loader_read_conf(p
, config
);
372 p
= strjoina(esp_path
, "/loader/entries");
373 r
= boot_entries_find(p
, &config
->entries
, &config
->n_entries
);
377 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
379 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
382 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
383 if (r
< 0 && r
!= -ENOENT
)
384 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
386 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
387 if (r
< 0 && r
!= -ENOENT
)
388 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
391 config
->default_entry
= boot_entries_select_default(config
);
395 /********************************************************************************/
397 static int verify_esp(
400 bool unprivileged_mode
,
402 uint64_t *ret_pstart
,
404 sd_id128_t
*ret_uuid
) {
406 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
407 char t
[DEV_NUM_PATH_MAX
];
410 uint64_t pstart
= 0, psize
= 0;
414 sd_id128_t uuid
= SD_ID128_NULL
;
421 relax_checks
= getenv_bool("SYSTEMD_RELAX_ESP_CHECKS") > 0;
423 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
424 * issues. Let's also, silence the error messages. */
427 if (statfs(p
, &sfs
) < 0) {
428 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
429 if (errno
== ENOENT
&& searching
)
432 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
433 "Failed to check file system type of \"%s\": %m", p
);
436 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
)) {
438 return -EADDRNOTAVAIL
;
440 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
445 if (stat(p
, &st
) < 0)
446 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
447 "Failed to determine block device node of \"%s\": %m", p
);
449 if (major(st
.st_dev
) == 0) {
450 log_error("Block device node of %p is invalid.", p
);
454 t2
= strjoina(p
, "/..");
457 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
458 "Failed to determine block device node of parent of \"%s\": %m", p
);
460 if (st
.st_dev
== st2
.st_dev
) {
461 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p
);
465 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
466 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
467 if (detect_container() > 0 || unprivileged_mode
|| relax_checks
)
471 xsprintf_dev_num_path(t
, "block", st
.st_dev
);
473 b
= blkid_new_probe_from_filename(t
);
475 return log_error_errno(errno
?: ENOMEM
, "Failed to open file system \"%s\": %m", p
);
477 blkid_probe_enable_superblocks(b
, 1);
478 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
479 blkid_probe_enable_partitions(b
, 1);
480 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
483 r
= blkid_do_safeprobe(b
);
485 log_error("File system \"%s\" is ambiguous.", p
);
488 log_error("File system \"%s\" does not contain a label.", p
);
491 return log_error_errno(errno
?: EIO
, "Failed to probe file system \"%s\": %m", p
);
494 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
496 return log_error_errno(errno
?: EIO
, "Failed to probe file system type \"%s\": %m", p
);
497 if (!streq(v
, "vfat")) {
498 log_error("File system \"%s\" is not FAT.", p
);
503 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
505 return log_error_errno(errno
?: EIO
, "Failed to probe partition scheme \"%s\": %m", p
);
506 if (!streq(v
, "gpt")) {
507 log_error("File system \"%s\" is not on a GPT partition table.", p
);
512 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
514 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID \"%s\": %m", p
);
515 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
516 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p
);
521 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
523 return log_error_errno(errno
?: EIO
, "Failed to probe partition entry UUID \"%s\": %m", p
);
524 r
= sd_id128_from_string(v
, &uuid
);
526 log_error("Partition \"%s\" has invalid UUID \"%s\".", p
, v
);
531 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
533 return log_error_errno(errno
?: EIO
, "Failed to probe partition number \"%s\": m", p
);
534 r
= safe_atou32(v
, &part
);
536 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
539 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
541 return log_error_errno(errno
?: EIO
, "Failed to probe partition offset \"%s\": %m", p
);
542 r
= safe_atou64(v
, &pstart
);
544 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
547 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
549 return log_error_errno(errno
?: EIO
, "Failed to probe partition size \"%s\": %m", p
);
550 r
= safe_atou64(v
, &psize
);
552 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
559 *ret_pstart
= pstart
;
568 int find_esp_and_warn(
570 bool unprivileged_mode
,
573 uint64_t *ret_pstart
,
575 sd_id128_t
*ret_uuid
) {
579 /* This logs about all errors except:
581 * -ENOKEY → when we can't find the partition
582 * -EACCESS → when unprivileged_mode is true, and we can't access something
586 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
593 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
595 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
598 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
602 /* No logging here */
619 int find_default_boot_entry(
620 const char *esp_path
,
623 const BootEntry
**e
) {
625 _cleanup_free_
char *where
= NULL
;
631 r
= find_esp_and_warn(esp_path
, false, &where
, NULL
, NULL
, NULL
, NULL
);
635 r
= boot_entries_load_config(where
, config
);
637 return log_error_errno(r
, "Failed to load bootspec config from \"%s/loader\": %m", where
);
639 if (config
->default_entry
< 0) {
640 log_error("No entry suitable as default, refusing to guess.");
644 *e
= &config
->entries
[config
->default_entry
];
645 log_debug("Found default boot entry in file \"%s\"", (*e
)->path
);
648 *esp_where
= TAKE_PTR(where
);