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
) {
26 free(entry
->filename
);
28 free(entry
->show_title
);
30 free(entry
->machine_id
);
31 free(entry
->architecture
);
32 strv_free(entry
->options
);
35 strv_free(entry
->initrd
);
36 free(entry
->device_tree
);
39 int boot_entry_load(const char *path
, BootEntry
*entry
) {
40 _cleanup_(boot_entry_free
) BootEntry tmp
= {};
41 _cleanup_fclose_
FILE *f
= NULL
;
49 c
= endswith_no_case(path
, ".conf");
51 log_error("Invalid loader entry filename: %s", path
);
56 tmp
.filename
= strndup(b
, c
- b
);
60 f
= fopen(path
, "re");
62 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
65 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
68 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
72 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
74 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
78 if (IN_SET(*strstrip(buf
), '#', '\0'))
82 r
= extract_first_word(&p
, &field
, " \t", 0);
84 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
88 log_warning("%s:%u: Bad syntax", path
, line
);
92 if (streq(field
, "title"))
93 r
= free_and_strdup(&tmp
.title
, p
);
94 else if (streq(field
, "version"))
95 r
= free_and_strdup(&tmp
.version
, p
);
96 else if (streq(field
, "machine-id"))
97 r
= free_and_strdup(&tmp
.machine_id
, p
);
98 else if (streq(field
, "architecture"))
99 r
= free_and_strdup(&tmp
.architecture
, p
);
100 else if (streq(field
, "options"))
101 r
= strv_extend(&tmp
.options
, p
);
102 else if (streq(field
, "linux"))
103 r
= free_and_strdup(&tmp
.kernel
, p
);
104 else if (streq(field
, "efi"))
105 r
= free_and_strdup(&tmp
.efi
, p
);
106 else if (streq(field
, "initrd"))
107 r
= strv_extend(&tmp
.initrd
, p
);
108 else if (streq(field
, "devicetree"))
109 r
= free_and_strdup(&tmp
.device_tree
, p
);
111 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
115 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
119 tmp
= (BootEntry
) {};
123 void boot_config_free(BootConfig
*config
) {
128 free(config
->default_pattern
);
129 free(config
->timeout
);
130 free(config
->editor
);
131 free(config
->auto_entries
);
132 free(config
->auto_firmware
);
134 free(config
->entry_oneshot
);
135 free(config
->entry_default
);
137 for (i
= 0; i
< config
->n_entries
; i
++)
138 boot_entry_free(config
->entries
+ i
);
139 free(config
->entries
);
142 int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
143 _cleanup_fclose_
FILE *f
= NULL
;
150 f
= fopen(path
, "re");
152 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
155 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
158 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
162 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
164 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
168 if (IN_SET(*strstrip(buf
), '#', '\0'))
172 r
= extract_first_word(&p
, &field
, " \t", 0);
174 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
178 log_warning("%s:%u: Bad syntax", path
, line
);
182 if (streq(field
, "default"))
183 r
= free_and_strdup(&config
->default_pattern
, p
);
184 else if (streq(field
, "timeout"))
185 r
= free_and_strdup(&config
->timeout
, p
);
186 else if (streq(field
, "editor"))
187 r
= free_and_strdup(&config
->editor
, p
);
188 else if (streq(field
, "auto-entries"))
189 r
= free_and_strdup(&config
->auto_entries
, p
);
190 else if (streq(field
, "auto-firmware"))
191 r
= free_and_strdup(&config
->auto_firmware
, p
);
192 else if (streq(field
, "console-mode"))
193 r
= free_and_strdup(&config
->console_mode
, p
);
195 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
199 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
205 static int boot_entry_compare(const BootEntry
*a
, const BootEntry
*b
) {
206 return str_verscmp(a
->filename
, b
->filename
);
209 int boot_entries_find(const char *dir
, BootEntry
**ret_entries
, size_t *ret_n_entries
) {
210 _cleanup_strv_free_
char **files
= NULL
;
213 BootEntry
*array
= NULL
;
214 size_t n_allocated
= 0, n
= 0;
218 assert(ret_n_entries
);
220 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
222 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
224 STRV_FOREACH(f
, files
) {
225 if (!GREEDY_REALLOC0(array
, n_allocated
, n
+ 1))
228 r
= boot_entry_load(*f
, array
+ n
);
235 typesafe_qsort(array
, n
, boot_entry_compare
);
237 *ret_entries
= array
;
243 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
245 bool non_unique
= false;
247 assert(entries
|| n_entries
== 0);
248 assert(arr
|| n_entries
== 0);
250 for (i
= 0; i
< n_entries
; i
++)
253 for (i
= 0; i
< n_entries
; i
++)
254 for (j
= 0; j
< n_entries
; j
++)
255 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
256 boot_entry_title(entries
+ j
)))
257 non_unique
= arr
[i
] = arr
[j
] = true;
262 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
268 assert(entries
|| n_entries
== 0);
270 /* Find _all_ non-unique titles */
271 if (!find_nonunique(entries
, n_entries
, arr
))
274 /* Add version to non-unique titles */
275 for (i
= 0; i
< n_entries
; i
++)
276 if (arr
[i
] && entries
[i
].version
) {
277 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
281 free_and_replace(entries
[i
].show_title
, s
);
284 if (!find_nonunique(entries
, n_entries
, arr
))
287 /* Add machine-id to non-unique titles */
288 for (i
= 0; i
< n_entries
; i
++)
289 if (arr
[i
] && entries
[i
].machine_id
) {
290 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
294 free_and_replace(entries
[i
].show_title
, s
);
297 if (!find_nonunique(entries
, n_entries
, arr
))
300 /* Add file name to non-unique titles */
301 for (i
= 0; i
< n_entries
; i
++)
303 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].filename
);
307 free_and_replace(entries
[i
].show_title
, s
);
313 static int boot_entries_select_default(const BootConfig
*config
) {
318 if (config
->entry_oneshot
)
319 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
320 if (streq(config
->entry_oneshot
, config
->entries
[i
].filename
)) {
321 log_debug("Found default: filename \"%s\" is matched by LoaderEntryOneShot",
322 config
->entries
[i
].filename
);
326 if (config
->entry_default
)
327 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
328 if (streq(config
->entry_default
, config
->entries
[i
].filename
)) {
329 log_debug("Found default: filename \"%s\" is matched by LoaderEntryDefault",
330 config
->entries
[i
].filename
);
334 if (config
->default_pattern
)
335 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
336 if (fnmatch(config
->default_pattern
, config
->entries
[i
].filename
, FNM_CASEFOLD
) == 0) {
337 log_debug("Found default: filename \"%s\" is matched by pattern \"%s\"",
338 config
->entries
[i
].filename
, config
->default_pattern
);
342 if (config
->n_entries
> 0)
343 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].filename
);
345 log_debug("Found no default boot entry :(");
347 return config
->n_entries
- 1; /* -1 means "no default" */
350 int boot_entries_load_config(const char *esp_path
, BootConfig
*config
) {
357 p
= strjoina(esp_path
, "/loader/loader.conf");
358 r
= boot_loader_read_conf(p
, config
);
360 return log_error_errno(r
, "Failed to read boot config from \"%s\": %m", p
);
362 p
= strjoina(esp_path
, "/loader/entries");
363 r
= boot_entries_find(p
, &config
->entries
, &config
->n_entries
);
365 return log_error_errno(r
, "Failed to read boot entries from \"%s\": %m", p
);
367 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
369 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
371 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
372 if (r
< 0 && r
!= -ENOENT
)
373 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
375 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
376 if (r
< 0 && r
!= -ENOENT
)
377 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
379 config
->default_entry
= boot_entries_select_default(config
);
383 /********************************************************************************/
385 static int verify_esp(
388 bool unprivileged_mode
,
390 uint64_t *ret_pstart
,
392 sd_id128_t
*ret_uuid
) {
394 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
395 char t
[DEV_NUM_PATH_MAX
];
398 uint64_t pstart
= 0, psize
= 0;
402 sd_id128_t uuid
= SD_ID128_NULL
;
408 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
409 * issues. Let's also, silence the error messages. */
411 if (statfs(p
, &sfs
) < 0) {
412 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
413 if (errno
== ENOENT
&& searching
)
416 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
417 "Failed to check file system type of \"%s\": %m", p
);
420 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
)) {
422 return -EADDRNOTAVAIL
;
424 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
428 if (stat(p
, &st
) < 0)
429 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
430 "Failed to determine block device node of \"%s\": %m", p
);
432 if (major(st
.st_dev
) == 0) {
433 log_error("Block device node of %p is invalid.", p
);
437 t2
= strjoina(p
, "/..");
440 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
441 "Failed to determine block device node of parent of \"%s\": %m", p
);
443 if (st
.st_dev
== st2
.st_dev
) {
444 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p
);
448 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
449 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
450 if (detect_container() > 0 || unprivileged_mode
)
454 xsprintf_dev_num_path(t
, "block", st
.st_dev
);
456 b
= blkid_new_probe_from_filename(t
);
458 return log_error_errno(errno
?: ENOMEM
, "Failed to open file system \"%s\": %m", p
);
460 blkid_probe_enable_superblocks(b
, 1);
461 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
462 blkid_probe_enable_partitions(b
, 1);
463 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
466 r
= blkid_do_safeprobe(b
);
468 log_error("File system \"%s\" is ambiguous.", p
);
471 log_error("File system \"%s\" does not contain a label.", p
);
474 return log_error_errno(errno
?: EIO
, "Failed to probe file system \"%s\": %m", p
);
477 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
479 return log_error_errno(errno
?: EIO
, "Failed to probe file system type \"%s\": %m", p
);
480 if (!streq(v
, "vfat")) {
481 log_error("File system \"%s\" is not FAT.", p
);
486 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
488 return log_error_errno(errno
?: EIO
, "Failed to probe partition scheme \"%s\": %m", p
);
489 if (!streq(v
, "gpt")) {
490 log_error("File system \"%s\" is not on a GPT partition table.", p
);
495 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
497 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID \"%s\": %m", p
);
498 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
499 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p
);
504 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
506 return log_error_errno(errno
?: EIO
, "Failed to probe partition entry UUID \"%s\": %m", p
);
507 r
= sd_id128_from_string(v
, &uuid
);
509 log_error("Partition \"%s\" has invalid UUID \"%s\".", p
, v
);
514 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
516 return log_error_errno(errno
?: EIO
, "Failed to probe partition number \"%s\": m", p
);
517 r
= safe_atou32(v
, &part
);
519 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
522 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
524 return log_error_errno(errno
?: EIO
, "Failed to probe partition offset \"%s\": %m", p
);
525 r
= safe_atou64(v
, &pstart
);
527 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
530 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
532 return log_error_errno(errno
?: EIO
, "Failed to probe partition size \"%s\": %m", p
);
533 r
= safe_atou64(v
, &psize
);
535 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
542 *ret_pstart
= pstart
;
551 int find_esp_and_warn(
553 bool unprivileged_mode
,
556 uint64_t *ret_pstart
,
558 sd_id128_t
*ret_uuid
) {
562 /* This logs about all errors except:
564 * -ENOKEY → when we can't find the partition
565 * -EACCESS → when unprivileged_mode is true, and we can't access something
569 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
576 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
578 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
581 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
585 /* No logging here */