2 Copyright © 2017 Zbigniew Jędrzejewski-Szmek
6 #include <linux/magic.h>
10 #include "alloc-util.h"
11 #include "blkid-util.h"
13 #include "conf-files.h"
15 #include "device-nodes.h"
19 #include "parse-util.h"
20 #include "stat-util.h"
21 #include "string-util.h"
25 void boot_entry_free(BootEntry
*entry
) {
28 free(entry
->filename
);
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 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
.filename
= strndup(b
, c
- b
);
62 f
= fopen(path
, "re");
64 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
67 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
70 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
74 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
76 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
80 if (IN_SET(*strstrip(buf
), '#', '\0'))
84 r
= extract_first_word(&p
, &field
, " \t", 0);
86 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
90 log_warning("%s:%u: Bad syntax", path
, line
);
94 if (streq(field
, "title"))
95 r
= free_and_strdup(&tmp
.title
, p
);
96 else if (streq(field
, "version"))
97 r
= free_and_strdup(&tmp
.version
, p
);
98 else if (streq(field
, "machine-id"))
99 r
= free_and_strdup(&tmp
.machine_id
, p
);
100 else if (streq(field
, "architecture"))
101 r
= free_and_strdup(&tmp
.architecture
, p
);
102 else if (streq(field
, "options"))
103 r
= strv_extend(&tmp
.options
, p
);
104 else if (streq(field
, "linux"))
105 r
= free_and_strdup(&tmp
.kernel
, p
);
106 else if (streq(field
, "efi"))
107 r
= free_and_strdup(&tmp
.efi
, p
);
108 else if (streq(field
, "initrd"))
109 r
= strv_extend(&tmp
.initrd
, p
);
110 else if (streq(field
, "devicetree"))
111 r
= free_and_strdup(&tmp
.device_tree
, p
);
113 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
117 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
121 tmp
= (BootEntry
) {};
125 void boot_config_free(BootConfig
*config
) {
130 free(config
->default_pattern
);
131 free(config
->timeout
);
132 free(config
->editor
);
133 free(config
->auto_entries
);
134 free(config
->auto_firmware
);
136 free(config
->entry_oneshot
);
137 free(config
->entry_default
);
139 for (i
= 0; i
< config
->n_entries
; i
++)
140 boot_entry_free(config
->entries
+ i
);
141 free(config
->entries
);
144 int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
145 _cleanup_fclose_
FILE *f
= NULL
;
152 f
= fopen(path
, "re");
154 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
157 _cleanup_free_
char *buf
= NULL
, *field
= NULL
;
160 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
164 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
166 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
170 if (IN_SET(*strstrip(buf
), '#', '\0'))
174 r
= extract_first_word(&p
, &field
, " \t", 0);
176 log_error_errno(r
, "Failed to parse config file %s line %u: %m", path
, line
);
180 log_warning("%s:%u: Bad syntax", path
, line
);
184 if (streq(field
, "default"))
185 r
= free_and_strdup(&config
->default_pattern
, p
);
186 else if (streq(field
, "timeout"))
187 r
= free_and_strdup(&config
->timeout
, p
);
188 else if (streq(field
, "editor"))
189 r
= free_and_strdup(&config
->editor
, p
);
190 else if (streq(field
, "auto-entries"))
191 r
= free_and_strdup(&config
->auto_entries
, p
);
192 else if (streq(field
, "auto-firmware"))
193 r
= free_and_strdup(&config
->auto_firmware
, p
);
194 else if (streq(field
, "console-mode"))
195 r
= free_and_strdup(&config
->console_mode
, p
);
197 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, field
);
201 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
207 static int boot_entry_compare(const void *a
, const void *b
) {
208 const BootEntry
*aa
= a
, *bb
= b
;
210 return str_verscmp(aa
->filename
, bb
->filename
);
213 int boot_entries_find(const char *dir
, BootEntry
**ret_entries
, size_t *ret_n_entries
) {
214 _cleanup_strv_free_
char **files
= NULL
;
217 BootEntry
*array
= NULL
;
218 size_t n_allocated
= 0, n
= 0;
222 assert(ret_n_entries
);
224 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
226 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
228 STRV_FOREACH(f
, files
) {
229 if (!GREEDY_REALLOC0(array
, n_allocated
, n
+ 1))
232 r
= boot_entry_load(*f
, array
+ n
);
239 qsort_safe(array
, n
, sizeof(BootEntry
), boot_entry_compare
);
241 *ret_entries
= array
;
247 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
249 bool non_unique
= false;
251 assert(entries
|| n_entries
== 0);
252 assert(arr
|| n_entries
== 0);
254 for (i
= 0; i
< n_entries
; i
++)
257 for (i
= 0; i
< n_entries
; i
++)
258 for (j
= 0; j
< n_entries
; j
++)
259 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
260 boot_entry_title(entries
+ j
)))
261 non_unique
= arr
[i
] = arr
[j
] = true;
266 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
272 assert(entries
|| n_entries
== 0);
274 /* Find _all_ non-unique titles */
275 if (!find_nonunique(entries
, n_entries
, arr
))
278 /* Add version to non-unique titles */
279 for (i
= 0; i
< n_entries
; i
++)
280 if (arr
[i
] && entries
[i
].version
) {
281 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
285 free_and_replace(entries
[i
].show_title
, s
);
288 if (!find_nonunique(entries
, n_entries
, arr
))
291 /* Add machine-id to non-unique titles */
292 for (i
= 0; i
< n_entries
; i
++)
293 if (arr
[i
] && entries
[i
].machine_id
) {
294 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
298 free_and_replace(entries
[i
].show_title
, s
);
301 if (!find_nonunique(entries
, n_entries
, arr
))
304 /* Add file name to non-unique titles */
305 for (i
= 0; i
< n_entries
; i
++)
307 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].filename
);
311 free_and_replace(entries
[i
].show_title
, s
);
317 static int boot_entries_select_default(const BootConfig
*config
) {
322 if (config
->entry_oneshot
)
323 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
324 if (streq(config
->entry_oneshot
, config
->entries
[i
].filename
)) {
325 log_debug("Found default: filename \"%s\" is matched by LoaderEntryOneShot",
326 config
->entries
[i
].filename
);
330 if (config
->entry_default
)
331 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
332 if (streq(config
->entry_default
, config
->entries
[i
].filename
)) {
333 log_debug("Found default: filename \"%s\" is matched by LoaderEntryDefault",
334 config
->entries
[i
].filename
);
338 if (config
->default_pattern
)
339 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
340 if (fnmatch(config
->default_pattern
, config
->entries
[i
].filename
, FNM_CASEFOLD
) == 0) {
341 log_debug("Found default: filename \"%s\" is matched by pattern \"%s\"",
342 config
->entries
[i
].filename
, config
->default_pattern
);
346 if (config
->n_entries
> 0)
347 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].filename
);
349 log_debug("Found no default boot entry :(");
351 return config
->n_entries
- 1; /* -1 means "no default" */
354 int boot_entries_load_config(const char *esp_path
, BootConfig
*config
) {
361 p
= strjoina(esp_path
, "/loader/loader.conf");
362 r
= boot_loader_read_conf(p
, config
);
364 return log_error_errno(r
, "Failed to read boot config from \"%s\": %m", p
);
366 p
= strjoina(esp_path
, "/loader/entries");
367 r
= boot_entries_find(p
, &config
->entries
, &config
->n_entries
);
369 return log_error_errno(r
, "Failed to read boot entries from \"%s\": %m", p
);
371 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
373 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
375 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
376 if (r
< 0 && r
!= -ENOENT
)
377 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
379 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
380 if (r
< 0 && r
!= -ENOENT
)
381 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
383 config
->default_entry
= boot_entries_select_default(config
);
387 /********************************************************************************/
389 static int verify_esp(
392 bool unprivileged_mode
,
394 uint64_t *ret_pstart
,
396 sd_id128_t
*ret_uuid
) {
398 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
399 char t
[DEV_NUM_PATH_MAX
];
402 uint64_t pstart
= 0, psize
= 0;
406 sd_id128_t uuid
= SD_ID128_NULL
;
412 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
413 * issues. Let's also, silence the error messages. */
415 if (statfs(p
, &sfs
) < 0) {
416 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
417 if (errno
== ENOENT
&& searching
)
420 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
421 "Failed to check file system type of \"%s\": %m", p
);
424 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
)) {
426 return -EADDRNOTAVAIL
;
428 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
432 if (stat(p
, &st
) < 0)
433 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
434 "Failed to determine block device node of \"%s\": %m", p
);
436 if (major(st
.st_dev
) == 0) {
437 log_error("Block device node of %p is invalid.", p
);
441 t2
= strjoina(p
, "/..");
444 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
445 "Failed to determine block device node of parent of \"%s\": %m", p
);
447 if (st
.st_dev
== st2
.st_dev
) {
448 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p
);
452 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
453 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
454 if (detect_container() > 0 || unprivileged_mode
)
458 xsprintf_dev_num_path(t
, "block", st
.st_dev
);
460 b
= blkid_new_probe_from_filename(t
);
462 return log_error_errno(errno
?: ENOMEM
, "Failed to open file system \"%s\": %m", p
);
464 blkid_probe_enable_superblocks(b
, 1);
465 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
466 blkid_probe_enable_partitions(b
, 1);
467 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
470 r
= blkid_do_safeprobe(b
);
472 log_error("File system \"%s\" is ambiguous.", p
);
475 log_error("File system \"%s\" does not contain a label.", p
);
478 return log_error_errno(errno
?: EIO
, "Failed to probe file system \"%s\": %m", p
);
481 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
483 return log_error_errno(errno
?: EIO
, "Failed to probe file system type \"%s\": %m", p
);
484 if (!streq(v
, "vfat")) {
485 log_error("File system \"%s\" is not FAT.", p
);
490 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
492 return log_error_errno(errno
?: EIO
, "Failed to probe partition scheme \"%s\": %m", p
);
493 if (!streq(v
, "gpt")) {
494 log_error("File system \"%s\" is not on a GPT partition table.", p
);
499 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
501 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID \"%s\": %m", p
);
502 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
503 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p
);
508 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
510 return log_error_errno(errno
?: EIO
, "Failed to probe partition entry UUID \"%s\": %m", p
);
511 r
= sd_id128_from_string(v
, &uuid
);
513 log_error("Partition \"%s\" has invalid UUID \"%s\".", p
, v
);
518 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
520 return log_error_errno(errno
?: EIO
, "Failed to probe partition number \"%s\": m", p
);
521 r
= safe_atou32(v
, &part
);
523 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
526 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
528 return log_error_errno(errno
?: EIO
, "Failed to probe partition offset \"%s\": %m", p
);
529 r
= safe_atou64(v
, &pstart
);
531 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
534 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
536 return log_error_errno(errno
?: EIO
, "Failed to probe partition size \"%s\": %m", p
);
537 r
= safe_atou64(v
, &psize
);
539 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
546 *ret_pstart
= pstart
;
555 int find_esp_and_warn(
557 bool unprivileged_mode
,
560 uint64_t *ret_pstart
,
562 sd_id128_t
*ret_uuid
) {
566 /* This logs about all errors except:
568 * -ENOKEY → when we can't find the partition
569 * -EACCESS → when unprivileged_mode is true, and we can't access something
573 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
580 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
582 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
585 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
589 /* No logging here */