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 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 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 int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
148 _cleanup_fclose_
FILE *f
= NULL
;
155 f
= fopen(path
, "re");
157 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
160 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
163 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
167 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
169 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
173 if (IN_SET(*strstrip(buf
), '#', '\0'))
177 r
= extract_first_word(&p
, &field
, " \t", 0);
179 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
183 log_warning("%s:%u: Bad syntax", path
, line
);
187 if (streq(field
, "default"))
188 r
= free_and_strdup(&config
->default_pattern
, p
);
189 else if (streq(field
, "timeout"))
190 r
= free_and_strdup(&config
->timeout
, p
);
191 else if (streq(field
, "editor"))
192 r
= free_and_strdup(&config
->editor
, p
);
193 else if (streq(field
, "auto-entries"))
194 r
= free_and_strdup(&config
->auto_entries
, p
);
195 else if (streq(field
, "auto-firmware"))
196 r
= free_and_strdup(&config
->auto_firmware
, p
);
197 else if (streq(field
, "console-mode"))
198 r
= free_and_strdup(&config
->console_mode
, p
);
200 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
204 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
210 static int boot_entry_compare(const BootEntry
*a
, const BootEntry
*b
) {
211 return str_verscmp(a
->id
, b
->id
);
214 int boot_entries_find(const char *dir
, BootEntry
**ret_entries
, size_t *ret_n_entries
) {
215 _cleanup_strv_free_
char **files
= NULL
;
218 BootEntry
*array
= NULL
;
219 size_t n_allocated
= 0, n
= 0;
223 assert(ret_n_entries
);
225 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
227 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
229 STRV_FOREACH(f
, files
) {
230 if (!GREEDY_REALLOC0(array
, n_allocated
, n
+ 1))
233 r
= boot_entry_load(*f
, array
+ n
);
240 typesafe_qsort(array
, n
, boot_entry_compare
);
242 *ret_entries
= array
;
248 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
250 bool non_unique
= false;
252 assert(entries
|| n_entries
== 0);
253 assert(arr
|| n_entries
== 0);
255 for (i
= 0; i
< n_entries
; i
++)
258 for (i
= 0; i
< n_entries
; i
++)
259 for (j
= 0; j
< n_entries
; j
++)
260 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
261 boot_entry_title(entries
+ j
)))
262 non_unique
= arr
[i
] = arr
[j
] = true;
267 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
273 assert(entries
|| n_entries
== 0);
275 /* Find _all_ non-unique titles */
276 if (!find_nonunique(entries
, n_entries
, arr
))
279 /* Add version to non-unique titles */
280 for (i
= 0; i
< n_entries
; i
++)
281 if (arr
[i
] && entries
[i
].version
) {
282 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
286 free_and_replace(entries
[i
].show_title
, s
);
289 if (!find_nonunique(entries
, n_entries
, arr
))
292 /* Add machine-id to non-unique titles */
293 for (i
= 0; i
< n_entries
; i
++)
294 if (arr
[i
] && entries
[i
].machine_id
) {
295 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
299 free_and_replace(entries
[i
].show_title
, s
);
302 if (!find_nonunique(entries
, n_entries
, arr
))
305 /* Add file name to non-unique titles */
306 for (i
= 0; i
< n_entries
; i
++)
308 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].id
);
312 free_and_replace(entries
[i
].show_title
, s
);
318 static int boot_entries_select_default(const BootConfig
*config
) {
323 if (config
->entry_oneshot
)
324 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
325 if (streq(config
->entry_oneshot
, config
->entries
[i
].id
)) {
326 log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
327 config
->entries
[i
].id
);
331 if (config
->entry_default
)
332 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
333 if (streq(config
->entry_default
, config
->entries
[i
].id
)) {
334 log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
335 config
->entries
[i
].id
);
339 if (config
->default_pattern
)
340 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
341 if (fnmatch(config
->default_pattern
, config
->entries
[i
].id
, FNM_CASEFOLD
) == 0) {
342 log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
343 config
->entries
[i
].id
, config
->default_pattern
);
347 if (config
->n_entries
> 0)
348 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].id
);
350 log_debug("Found no default boot entry :(");
352 return config
->n_entries
- 1; /* -1 means "no default" */
355 int boot_entries_load_config(const char *esp_path
, BootConfig
*config
) {
362 p
= strjoina(esp_path
, "/loader/loader.conf");
363 r
= boot_loader_read_conf(p
, config
);
365 return log_error_errno(r
, "Failed to read boot config from \"%s\": %m", p
);
367 p
= strjoina(esp_path
, "/loader/entries");
368 r
= boot_entries_find(p
, &config
->entries
, &config
->n_entries
);
370 return log_error_errno(r
, "Failed to read boot entries from \"%s\": %m", p
);
372 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
374 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
376 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
377 if (r
< 0 && r
!= -ENOENT
)
378 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
380 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
381 if (r
< 0 && r
!= -ENOENT
)
382 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
384 config
->default_entry
= boot_entries_select_default(config
);
388 /********************************************************************************/
390 static int verify_esp(
393 bool unprivileged_mode
,
395 uint64_t *ret_pstart
,
397 sd_id128_t
*ret_uuid
) {
399 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
400 char t
[DEV_NUM_PATH_MAX
];
403 uint64_t pstart
= 0, psize
= 0;
407 sd_id128_t uuid
= SD_ID128_NULL
;
413 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
414 * issues. Let's also, silence the error messages. */
416 if (statfs(p
, &sfs
) < 0) {
417 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
418 if (errno
== ENOENT
&& searching
)
421 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
422 "Failed to check file system type of \"%s\": %m", p
);
425 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
)) {
427 return -EADDRNOTAVAIL
;
429 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
433 if (stat(p
, &st
) < 0)
434 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
435 "Failed to determine block device node of \"%s\": %m", p
);
437 if (major(st
.st_dev
) == 0) {
438 log_error("Block device node of %p is invalid.", p
);
442 t2
= strjoina(p
, "/..");
445 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
446 "Failed to determine block device node of parent of \"%s\": %m", p
);
448 if (st
.st_dev
== st2
.st_dev
) {
449 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p
);
453 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
454 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
455 if (detect_container() > 0 || unprivileged_mode
)
459 xsprintf_dev_num_path(t
, "block", st
.st_dev
);
461 b
= blkid_new_probe_from_filename(t
);
463 return log_error_errno(errno
?: ENOMEM
, "Failed to open file system \"%s\": %m", p
);
465 blkid_probe_enable_superblocks(b
, 1);
466 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
467 blkid_probe_enable_partitions(b
, 1);
468 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
471 r
= blkid_do_safeprobe(b
);
473 log_error("File system \"%s\" is ambiguous.", p
);
476 log_error("File system \"%s\" does not contain a label.", p
);
479 return log_error_errno(errno
?: EIO
, "Failed to probe file system \"%s\": %m", p
);
482 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
484 return log_error_errno(errno
?: EIO
, "Failed to probe file system type \"%s\": %m", p
);
485 if (!streq(v
, "vfat")) {
486 log_error("File system \"%s\" is not FAT.", p
);
491 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
493 return log_error_errno(errno
?: EIO
, "Failed to probe partition scheme \"%s\": %m", p
);
494 if (!streq(v
, "gpt")) {
495 log_error("File system \"%s\" is not on a GPT partition table.", p
);
500 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
502 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID \"%s\": %m", p
);
503 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
504 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p
);
509 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
511 return log_error_errno(errno
?: EIO
, "Failed to probe partition entry UUID \"%s\": %m", p
);
512 r
= sd_id128_from_string(v
, &uuid
);
514 log_error("Partition \"%s\" has invalid UUID \"%s\".", p
, v
);
519 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
521 return log_error_errno(errno
?: EIO
, "Failed to probe partition number \"%s\": m", p
);
522 r
= safe_atou32(v
, &part
);
524 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
527 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
529 return log_error_errno(errno
?: EIO
, "Failed to probe partition offset \"%s\": %m", p
);
530 r
= safe_atou64(v
, &pstart
);
532 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
535 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
537 return log_error_errno(errno
?: EIO
, "Failed to probe partition size \"%s\": %m", p
);
538 r
= safe_atou64(v
, &psize
);
540 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
547 *ret_pstart
= pstart
;
556 int find_esp_and_warn(
558 bool unprivileged_mode
,
561 uint64_t *ret_pstart
,
563 sd_id128_t
*ret_uuid
) {
567 /* This logs about all errors except:
569 * -ENOKEY → when we can't find the partition
570 * -EACCESS → when unprivileged_mode is true, and we can't access something
574 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
581 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
583 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
586 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
590 /* No logging here */
607 int find_default_boot_entry(
608 const char *esp_path
,
611 const BootEntry
**e
) {
613 _cleanup_free_
char *where
= NULL
;
619 r
= find_esp_and_warn(esp_path
, false, &where
, NULL
, NULL
, NULL
, NULL
);
623 r
= boot_entries_load_config(where
, config
);
625 return log_error_errno(r
, "Failed to load bootspec config from \"%s/loader\": %m", where
);
627 if (config
->default_entry
< 0) {
628 log_error("No entry suitable as default, refusing to guess.");
632 *e
= &config
->entries
[config
->default_entry
];
633 log_debug("Found default boot entry in file \"%s\"", (*e
)->path
);
636 *esp_where
= TAKE_PTR(where
);