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"
17 #include "parse-util.h"
18 #include "stat-util.h"
19 #include "string-util.h"
23 static void boot_entry_free(BootEntry
*entry
) {
29 free(entry
->show_title
);
31 free(entry
->machine_id
);
32 free(entry
->architecture
);
33 strv_free(entry
->options
);
36 strv_free(entry
->initrd
);
37 free(entry
->device_tree
);
40 static int boot_entry_load(const char *path
, BootEntry
*entry
) {
41 _cleanup_(boot_entry_free
) BootEntry tmp
= {};
42 _cleanup_fclose_
FILE *f
= NULL
;
50 c
= endswith_no_case(path
, ".conf");
52 log_error("Invalid loader entry filename: %s", path
);
57 tmp
.id
= strndup(b
, c
- b
);
61 tmp
.path
= strdup(path
);
65 f
= fopen(path
, "re");
67 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
70 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
73 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
77 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
79 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
83 if (IN_SET(*strstrip(buf
), '#', '\0'))
87 r
= extract_first_word(&p
, &field
, " \t", 0);
89 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
93 log_warning("%s:%u: Bad syntax", path
, line
);
97 if (streq(field
, "title"))
98 r
= free_and_strdup(&tmp
.title
, p
);
99 else if (streq(field
, "version"))
100 r
= free_and_strdup(&tmp
.version
, p
);
101 else if (streq(field
, "machine-id"))
102 r
= free_and_strdup(&tmp
.machine_id
, p
);
103 else if (streq(field
, "architecture"))
104 r
= free_and_strdup(&tmp
.architecture
, p
);
105 else if (streq(field
, "options"))
106 r
= strv_extend(&tmp
.options
, p
);
107 else if (streq(field
, "linux"))
108 r
= free_and_strdup(&tmp
.kernel
, p
);
109 else if (streq(field
, "efi"))
110 r
= free_and_strdup(&tmp
.efi
, p
);
111 else if (streq(field
, "initrd"))
112 r
= strv_extend(&tmp
.initrd
, p
);
113 else if (streq(field
, "devicetree"))
114 r
= free_and_strdup(&tmp
.device_tree
, p
);
116 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
120 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
124 tmp
= (BootEntry
) {};
128 void boot_config_free(BootConfig
*config
) {
133 free(config
->default_pattern
);
134 free(config
->timeout
);
135 free(config
->editor
);
136 free(config
->auto_entries
);
137 free(config
->auto_firmware
);
139 free(config
->entry_oneshot
);
140 free(config
->entry_default
);
142 for (i
= 0; i
< config
->n_entries
; i
++)
143 boot_entry_free(config
->entries
+ i
);
144 free(config
->entries
);
147 static int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
148 _cleanup_fclose_
FILE *f
= NULL
;
155 f
= fopen(path
, "re");
160 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
164 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
167 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
171 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
173 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
177 if (IN_SET(*strstrip(buf
), '#', '\0'))
181 r
= extract_first_word(&p
, &field
, " \t", 0);
183 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
187 log_warning("%s:%u: Bad syntax", path
, line
);
191 if (streq(field
, "default"))
192 r
= free_and_strdup(&config
->default_pattern
, p
);
193 else if (streq(field
, "timeout"))
194 r
= free_and_strdup(&config
->timeout
, p
);
195 else if (streq(field
, "editor"))
196 r
= free_and_strdup(&config
->editor
, p
);
197 else if (streq(field
, "auto-entries"))
198 r
= free_and_strdup(&config
->auto_entries
, p
);
199 else if (streq(field
, "auto-firmware"))
200 r
= free_and_strdup(&config
->auto_firmware
, p
);
201 else if (streq(field
, "console-mode"))
202 r
= free_and_strdup(&config
->console_mode
, p
);
204 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
208 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
214 static int boot_entry_compare(const BootEntry
*a
, const BootEntry
*b
) {
215 return str_verscmp(a
->id
, b
->id
);
218 static int boot_entries_find(const char *dir
, BootEntry
**ret_entries
, size_t *ret_n_entries
) {
219 _cleanup_strv_free_
char **files
= NULL
;
222 BootEntry
*array
= NULL
;
223 size_t n_allocated
= 0, n
= 0;
227 assert(ret_n_entries
);
229 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
231 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
233 STRV_FOREACH(f
, files
) {
234 if (!GREEDY_REALLOC0(array
, n_allocated
, n
+ 1))
237 r
= boot_entry_load(*f
, array
+ n
);
244 typesafe_qsort(array
, n
, boot_entry_compare
);
246 *ret_entries
= array
;
252 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
254 bool non_unique
= false;
256 assert(entries
|| n_entries
== 0);
257 assert(arr
|| n_entries
== 0);
259 for (i
= 0; i
< n_entries
; i
++)
262 for (i
= 0; i
< n_entries
; i
++)
263 for (j
= 0; j
< n_entries
; j
++)
264 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
265 boot_entry_title(entries
+ j
)))
266 non_unique
= arr
[i
] = arr
[j
] = true;
271 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
277 assert(entries
|| n_entries
== 0);
279 /* Find _all_ non-unique titles */
280 if (!find_nonunique(entries
, n_entries
, arr
))
283 /* Add version to non-unique titles */
284 for (i
= 0; i
< n_entries
; i
++)
285 if (arr
[i
] && entries
[i
].version
) {
286 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
290 free_and_replace(entries
[i
].show_title
, s
);
293 if (!find_nonunique(entries
, n_entries
, arr
))
296 /* Add machine-id to non-unique titles */
297 for (i
= 0; i
< n_entries
; i
++)
298 if (arr
[i
] && entries
[i
].machine_id
) {
299 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
303 free_and_replace(entries
[i
].show_title
, s
);
306 if (!find_nonunique(entries
, n_entries
, arr
))
309 /* Add file name to non-unique titles */
310 for (i
= 0; i
< n_entries
; i
++)
312 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].id
);
316 free_and_replace(entries
[i
].show_title
, s
);
322 static int boot_entries_select_default(const BootConfig
*config
) {
327 if (config
->entry_oneshot
)
328 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
329 if (streq(config
->entry_oneshot
, config
->entries
[i
].id
)) {
330 log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
331 config
->entries
[i
].id
);
335 if (config
->entry_default
)
336 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
337 if (streq(config
->entry_default
, config
->entries
[i
].id
)) {
338 log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
339 config
->entries
[i
].id
);
343 if (config
->default_pattern
)
344 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
345 if (fnmatch(config
->default_pattern
, config
->entries
[i
].id
, FNM_CASEFOLD
) == 0) {
346 log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
347 config
->entries
[i
].id
, config
->default_pattern
);
351 if (config
->n_entries
> 0)
352 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].id
);
354 log_debug("Found no default boot entry :(");
356 return config
->n_entries
- 1; /* -1 means "no default" */
359 int boot_entries_load_config(const char *esp_path
, BootConfig
*config
) {
366 p
= strjoina(esp_path
, "/loader/loader.conf");
367 r
= boot_loader_read_conf(p
, config
);
371 p
= strjoina(esp_path
, "/loader/entries");
372 r
= boot_entries_find(p
, &config
->entries
, &config
->n_entries
);
376 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
378 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
381 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
382 if (r
< 0 && r
!= -ENOENT
)
383 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
385 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
386 if (r
< 0 && r
!= -ENOENT
)
387 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
390 config
->default_entry
= boot_entries_select_default(config
);
394 /********************************************************************************/
396 static int verify_esp(
399 bool unprivileged_mode
,
401 uint64_t *ret_pstart
,
403 sd_id128_t
*ret_uuid
) {
405 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
406 char t
[DEV_NUM_PATH_MAX
];
409 uint64_t pstart
= 0, psize
= 0;
413 sd_id128_t uuid
= SD_ID128_NULL
;
419 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
420 * issues. Let's also, silence the error messages. */
422 if (statfs(p
, &sfs
) < 0) {
423 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
424 if (errno
== ENOENT
&& searching
)
427 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
428 "Failed to check file system type of \"%s\": %m", p
);
431 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
)) {
433 return -EADDRNOTAVAIL
;
435 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
439 if (stat(p
, &st
) < 0)
440 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
441 "Failed to determine block device node of \"%s\": %m", p
);
443 if (major(st
.st_dev
) == 0) {
444 log_error("Block device node of %p is invalid.", p
);
448 t2
= strjoina(p
, "/..");
451 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
452 "Failed to determine block device node of parent of \"%s\": %m", p
);
454 if (st
.st_dev
== st2
.st_dev
) {
455 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p
);
459 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
460 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
461 if (detect_container() > 0 || unprivileged_mode
)
465 xsprintf_dev_num_path(t
, "block", st
.st_dev
);
467 b
= blkid_new_probe_from_filename(t
);
469 return log_error_errno(errno
?: ENOMEM
, "Failed to open file system \"%s\": %m", p
);
471 blkid_probe_enable_superblocks(b
, 1);
472 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
473 blkid_probe_enable_partitions(b
, 1);
474 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
477 r
= blkid_do_safeprobe(b
);
479 log_error("File system \"%s\" is ambiguous.", p
);
482 log_error("File system \"%s\" does not contain a label.", p
);
485 return log_error_errno(errno
?: EIO
, "Failed to probe file system \"%s\": %m", p
);
488 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
490 return log_error_errno(errno
?: EIO
, "Failed to probe file system type \"%s\": %m", p
);
491 if (!streq(v
, "vfat")) {
492 log_error("File system \"%s\" is not FAT.", p
);
497 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
499 return log_error_errno(errno
?: EIO
, "Failed to probe partition scheme \"%s\": %m", p
);
500 if (!streq(v
, "gpt")) {
501 log_error("File system \"%s\" is not on a GPT partition table.", p
);
506 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
508 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID \"%s\": %m", p
);
509 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
510 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p
);
515 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
517 return log_error_errno(errno
?: EIO
, "Failed to probe partition entry UUID \"%s\": %m", p
);
518 r
= sd_id128_from_string(v
, &uuid
);
520 log_error("Partition \"%s\" has invalid UUID \"%s\".", p
, v
);
525 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
527 return log_error_errno(errno
?: EIO
, "Failed to probe partition number \"%s\": m", p
);
528 r
= safe_atou32(v
, &part
);
530 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
533 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
535 return log_error_errno(errno
?: EIO
, "Failed to probe partition offset \"%s\": %m", p
);
536 r
= safe_atou64(v
, &pstart
);
538 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
541 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
543 return log_error_errno(errno
?: EIO
, "Failed to probe partition size \"%s\": %m", p
);
544 r
= safe_atou64(v
, &psize
);
546 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
553 *ret_pstart
= pstart
;
562 int find_esp_and_warn(
564 bool unprivileged_mode
,
567 uint64_t *ret_pstart
,
569 sd_id128_t
*ret_uuid
) {
573 /* This logs about all errors except:
575 * -ENOKEY → when we can't find the partition
576 * -EACCESS → when unprivileged_mode is true, and we can't access something
580 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
587 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
589 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
592 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
596 /* No logging here */
613 int find_default_boot_entry(
614 const char *esp_path
,
617 const BootEntry
**e
) {
619 _cleanup_free_
char *where
= NULL
;
625 r
= find_esp_and_warn(esp_path
, false, &where
, NULL
, NULL
, NULL
, NULL
);
629 r
= boot_entries_load_config(where
, config
);
631 return log_error_errno(r
, "Failed to load bootspec config from \"%s/loader\": %m", where
);
633 if (config
->default_entry
< 0) {
634 log_error("No entry suitable as default, refusing to guess.");
638 *e
= &config
->entries
[config
->default_entry
];
639 log_debug("Found default boot entry in file \"%s\"", (*e
)->path
);
642 *esp_where
= TAKE_PTR(where
);