2 This file is part of systemd.
4 Copyright 2017 Zbigniew Jędrzejewski-Szmek
8 #include <linux/magic.h>
12 #include "alloc-util.h"
13 #include "blkid-util.h"
15 #include "conf-files.h"
17 #include "device-nodes.h"
21 #include "parse-util.h"
22 #include "stat-util.h"
23 #include "string-util.h"
27 void boot_entry_free(BootEntry
*entry
) {
30 free(entry
->filename
);
32 free(entry
->show_title
);
34 free(entry
->machine_id
);
35 free(entry
->architecture
);
36 strv_free(entry
->options
);
39 strv_free(entry
->initrd
);
40 free(entry
->device_tree
);
43 int boot_entry_load(const char *path
, BootEntry
*entry
) {
44 _cleanup_(boot_entry_free
) BootEntry tmp
= {};
45 _cleanup_fclose_
FILE *f
= NULL
;
53 c
= endswith_no_case(path
, ".conf");
55 log_error("Invalid loader entry filename: %s", path
);
60 tmp
.filename
= strndup(b
, c
- b
);
64 f
= fopen(path
, "re");
66 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
69 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
72 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
76 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
78 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
82 if (IN_SET(*strstrip(buf
), '#', '\0'))
86 r
= extract_first_word(&p
, &field
, " \t", 0);
88 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
92 log_warning("%s:%u: Bad syntax", path
, line
);
96 if (streq(field
, "title"))
97 r
= free_and_strdup(&tmp
.title
, p
);
98 else if (streq(field
, "version"))
99 r
= free_and_strdup(&tmp
.version
, p
);
100 else if (streq(field
, "machine-id"))
101 r
= free_and_strdup(&tmp
.machine_id
, p
);
102 else if (streq(field
, "architecture"))
103 r
= free_and_strdup(&tmp
.architecture
, p
);
104 else if (streq(field
, "options"))
105 r
= strv_extend(&tmp
.options
, p
);
106 else if (streq(field
, "linux"))
107 r
= free_and_strdup(&tmp
.kernel
, p
);
108 else if (streq(field
, "efi"))
109 r
= free_and_strdup(&tmp
.efi
, p
);
110 else if (streq(field
, "initrd"))
111 r
= strv_extend(&tmp
.initrd
, p
);
112 else if (streq(field
, "devicetree"))
113 r
= free_and_strdup(&tmp
.device_tree
, p
);
115 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
119 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
123 tmp
= (BootEntry
) {};
127 void boot_config_free(BootConfig
*config
) {
132 free(config
->default_pattern
);
133 free(config
->timeout
);
134 free(config
->editor
);
135 free(config
->auto_entries
);
136 free(config
->auto_firmware
);
138 free(config
->entry_oneshot
);
139 free(config
->entry_default
);
141 for (i
= 0; i
< config
->n_entries
; i
++)
142 boot_entry_free(config
->entries
+ i
);
143 free(config
->entries
);
146 int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
147 _cleanup_fclose_
FILE *f
= NULL
;
154 f
= fopen(path
, "re");
156 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
159 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
162 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
166 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
168 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
172 if (IN_SET(*strstrip(buf
), '#', '\0'))
176 r
= extract_first_word(&p
, &field
, " \t", 0);
178 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
182 log_warning("%s:%u: Bad syntax", path
, line
);
186 if (streq(field
, "default"))
187 r
= free_and_strdup(&config
->default_pattern
, p
);
188 else if (streq(field
, "timeout"))
189 r
= free_and_strdup(&config
->timeout
, p
);
190 else if (streq(field
, "editor"))
191 r
= free_and_strdup(&config
->editor
, p
);
192 else if (streq(field
, "auto-entries"))
193 r
= free_and_strdup(&config
->auto_entries
, p
);
194 else if (streq(field
, "auto-firmware"))
195 r
= free_and_strdup(&config
->auto_firmware
, p
);
196 else if (streq(field
, "console-mode"))
197 r
= free_and_strdup(&config
->console_mode
, p
);
199 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
203 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
209 static int boot_entry_compare(const void *a
, const void *b
) {
210 const BootEntry
*aa
= a
, *bb
= b
;
212 return str_verscmp(aa
->filename
, bb
->filename
);
215 int boot_entries_find(const char *dir
, BootEntry
**ret_entries
, size_t *ret_n_entries
) {
216 _cleanup_strv_free_
char **files
= NULL
;
219 BootEntry
*array
= NULL
;
220 size_t n_allocated
= 0, n
= 0;
224 assert(ret_n_entries
);
226 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
228 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
230 STRV_FOREACH(f
, files
) {
231 if (!GREEDY_REALLOC0(array
, n_allocated
, n
+ 1))
234 r
= boot_entry_load(*f
, array
+ n
);
241 qsort_safe(array
, n
, sizeof(BootEntry
), boot_entry_compare
);
243 *ret_entries
= array
;
249 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
251 bool non_unique
= false;
253 assert(entries
|| n_entries
== 0);
254 assert(arr
|| n_entries
== 0);
256 for (i
= 0; i
< n_entries
; i
++)
259 for (i
= 0; i
< n_entries
; i
++)
260 for (j
= 0; j
< n_entries
; j
++)
261 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
262 boot_entry_title(entries
+ j
)))
263 non_unique
= arr
[i
] = arr
[j
] = true;
268 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
274 assert(entries
|| n_entries
== 0);
276 /* Find _all_ non-unique titles */
277 if (!find_nonunique(entries
, n_entries
, arr
))
280 /* Add version to non-unique titles */
281 for (i
= 0; i
< n_entries
; i
++)
282 if (arr
[i
] && entries
[i
].version
) {
283 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
287 free_and_replace(entries
[i
].show_title
, s
);
290 if (!find_nonunique(entries
, n_entries
, arr
))
293 /* Add machine-id to non-unique titles */
294 for (i
= 0; i
< n_entries
; i
++)
295 if (arr
[i
] && entries
[i
].machine_id
) {
296 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
300 free_and_replace(entries
[i
].show_title
, s
);
303 if (!find_nonunique(entries
, n_entries
, arr
))
306 /* Add file name to non-unique titles */
307 for (i
= 0; i
< n_entries
; i
++)
309 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].filename
);
313 free_and_replace(entries
[i
].show_title
, s
);
319 static int boot_entries_select_default(const BootConfig
*config
) {
324 if (config
->entry_oneshot
)
325 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
326 if (streq(config
->entry_oneshot
, config
->entries
[i
].filename
)) {
327 log_debug("Found default: filename \"%s\" is matched by LoaderEntryOneShot",
328 config
->entries
[i
].filename
);
332 if (config
->entry_default
)
333 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
334 if (streq(config
->entry_default
, config
->entries
[i
].filename
)) {
335 log_debug("Found default: filename \"%s\" is matched by LoaderEntryDefault",
336 config
->entries
[i
].filename
);
340 if (config
->default_pattern
)
341 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
342 if (fnmatch(config
->default_pattern
, config
->entries
[i
].filename
, FNM_CASEFOLD
) == 0) {
343 log_debug("Found default: filename \"%s\" is matched by pattern \"%s\"",
344 config
->entries
[i
].filename
, config
->default_pattern
);
348 if (config
->n_entries
> 0)
349 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].filename
);
351 log_debug("Found no default boot entry :(");
353 return config
->n_entries
- 1; /* -1 means "no default" */
356 int boot_entries_load_config(const char *esp_path
, BootConfig
*config
) {
363 p
= strjoina(esp_path
, "/loader/loader.conf");
364 r
= boot_loader_read_conf(p
, config
);
366 return log_error_errno(r
, "Failed to read boot config from \"%s\": %m", p
);
368 p
= strjoina(esp_path
, "/loader/entries");
369 r
= boot_entries_find(p
, &config
->entries
, &config
->n_entries
);
371 return log_error_errno(r
, "Failed to read boot entries from \"%s\": %m", p
);
373 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
375 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
377 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
378 if (r
< 0 && r
!= -ENOENT
)
379 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
381 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
382 if (r
< 0 && r
!= -ENOENT
)
383 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
385 config
->default_entry
= boot_entries_select_default(config
);
389 /********************************************************************************/
391 static int verify_esp(
394 bool unprivileged_mode
,
396 uint64_t *ret_pstart
,
398 sd_id128_t
*ret_uuid
) {
400 _cleanup_blkid_free_probe_ blkid_probe b
= NULL
;
401 char t
[DEV_NUM_PATH_MAX
];
404 uint64_t pstart
= 0, psize
= 0;
408 sd_id128_t uuid
= SD_ID128_NULL
;
414 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
415 * issues. Let's also, silence the error messages. */
417 if (statfs(p
, &sfs
) < 0) {
418 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
419 if (errno
== ENOENT
&& searching
)
422 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
423 "Failed to check file system type of \"%s\": %m", p
);
426 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
)) {
428 return -EADDRNOTAVAIL
;
430 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
434 if (stat(p
, &st
) < 0)
435 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
436 "Failed to determine block device node of \"%s\": %m", p
);
438 if (major(st
.st_dev
) == 0) {
439 log_error("Block device node of %p is invalid.", p
);
443 t2
= strjoina(p
, "/..");
446 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
447 "Failed to determine block device node of parent of \"%s\": %m", p
);
449 if (st
.st_dev
== st2
.st_dev
) {
450 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p
);
454 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
455 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
456 if (detect_container() > 0 || unprivileged_mode
)
460 xsprintf_dev_num_path(t
, "block", st
.st_dev
);
462 b
= blkid_new_probe_from_filename(t
);
464 return log_error_errno(errno
?: ENOMEM
, "Failed to open file system \"%s\": %m", p
);
466 blkid_probe_enable_superblocks(b
, 1);
467 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
468 blkid_probe_enable_partitions(b
, 1);
469 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
472 r
= blkid_do_safeprobe(b
);
474 log_error("File system \"%s\" is ambiguous.", p
);
477 log_error("File system \"%s\" does not contain a label.", p
);
480 return log_error_errno(errno
?: EIO
, "Failed to probe file system \"%s\": %m", p
);
483 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
485 return log_error_errno(errno
?: EIO
, "Failed to probe file system type \"%s\": %m", p
);
486 if (!streq(v
, "vfat")) {
487 log_error("File system \"%s\" is not FAT.", p
);
492 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
494 return log_error_errno(errno
?: EIO
, "Failed to probe partition scheme \"%s\": %m", p
);
495 if (!streq(v
, "gpt")) {
496 log_error("File system \"%s\" is not on a GPT partition table.", p
);
501 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
503 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID \"%s\": %m", p
);
504 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
505 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p
);
510 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
512 return log_error_errno(errno
?: EIO
, "Failed to probe partition entry UUID \"%s\": %m", p
);
513 r
= sd_id128_from_string(v
, &uuid
);
515 log_error("Partition \"%s\" has invalid UUID \"%s\".", p
, v
);
520 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
522 return log_error_errno(errno
?: EIO
, "Failed to probe partition number \"%s\": m", p
);
523 r
= safe_atou32(v
, &part
);
525 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
528 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
530 return log_error_errno(errno
?: EIO
, "Failed to probe partition offset \"%s\": %m", p
);
531 r
= safe_atou64(v
, &pstart
);
533 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
536 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
538 return log_error_errno(errno
?: EIO
, "Failed to probe partition size \"%s\": %m", p
);
539 r
= safe_atou64(v
, &psize
);
541 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
548 *ret_pstart
= pstart
;
557 int find_esp_and_warn(
559 bool unprivileged_mode
,
562 uint64_t *ret_pstart
,
564 sd_id128_t
*ret_uuid
) {
568 /* This logs about all errors except:
570 * -ENOKEY → when we can't find the partition
571 * -EACCESS → when unprivileged_mode is true, and we can't access something
575 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
582 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
584 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
587 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
591 /* No logging here */