2 This file is part of systemd.
4 Copyright 2017 Zbigniew Jędrzejewski-Szmek
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/magic.h>
23 #include "alloc-util.h"
24 #include "blkid-util.h"
26 #include "conf-files.h"
31 #include "parse-util.h"
32 #include "stat-util.h"
33 #include "string-util.h"
37 void boot_entry_free(BootEntry
*entry
) {
38 free(entry
->filename
);
41 free(entry
->show_title
);
43 free(entry
->machine_id
);
44 free(entry
->architecture
);
45 strv_free(entry
->options
);
48 strv_free(entry
->initrd
);
49 free(entry
->device_tree
);
52 int boot_entry_load(const char *path
, BootEntry
*entry
) {
53 _cleanup_fclose_
FILE *f
= NULL
;
55 _cleanup_(boot_entry_free
) BootEntry tmp
= {};
58 f
= fopen(path
, "re");
60 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
62 tmp
.filename
= strdup(basename(path
));
67 _cleanup_free_
char *buf
= 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'))
85 log_warning("%s:%u: Bad syntax", path
, line
);
91 if (streq(buf
, "title"))
92 r
= free_and_strdup(&tmp
.title
, p
);
93 else if (streq(buf
, "version"))
94 r
= free_and_strdup(&tmp
.version
, p
);
95 else if (streq(buf
, "machine-id"))
96 r
= free_and_strdup(&tmp
.machine_id
, p
);
97 else if (streq(buf
, "architecture"))
98 r
= free_and_strdup(&tmp
.architecture
, p
);
99 else if (streq(buf
, "options"))
100 r
= strv_extend(&tmp
.options
, p
);
101 else if (streq(buf
, "linux"))
102 r
= free_and_strdup(&tmp
.kernel
, p
);
103 else if (streq(buf
, "efi"))
104 r
= free_and_strdup(&tmp
.efi
, p
);
105 else if (streq(buf
, "initrd"))
106 r
= strv_extend(&tmp
.initrd
, p
);
107 else if (streq(buf
, "devicetree"))
108 r
= free_and_strdup(&tmp
.device_tree
, p
);
110 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, buf
);
114 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
118 tmp
= (BootEntry
) {};
122 void boot_config_free(BootConfig
*config
) {
125 free(config
->default_pattern
);
126 free(config
->timeout
);
127 free(config
->editor
);
129 free(config
->entry_oneshot
);
130 free(config
->entry_default
);
132 for (i
= 0; i
< config
->n_entries
; i
++)
133 boot_entry_free(config
->entries
+ i
);
134 free(config
->entries
);
137 int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
138 _cleanup_fclose_
FILE *f
= NULL
;
142 f
= fopen(path
, "re");
144 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
147 _cleanup_free_
char *buf
= NULL
;
150 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
154 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
156 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
160 if (IN_SET(*strstrip(buf
), '#', '\0'))
163 p
= strchr(buf
, ' ');
165 log_warning("%s:%u: Bad syntax", path
, line
);
171 if (streq(buf
, "default"))
172 r
= free_and_strdup(&config
->default_pattern
, p
);
173 else if (streq(buf
, "timeout"))
174 r
= free_and_strdup(&config
->timeout
, p
);
175 else if (streq(buf
, "editor"))
176 r
= free_and_strdup(&config
->editor
, p
);
178 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, buf
);
182 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
188 /* This is a direct translation of str_verscmp from boot.c */
189 static bool is_digit(int c
) {
190 return c
>= '0' && c
<= '9';
193 static int c_order(int c
) {
198 else if ((c
>= 'a') && (c
<= 'z'))
204 static int str_verscmp(const char *s1
, const char *s2
) {
205 const char *os1
= s1
;
206 const char *os2
= s2
;
211 while ((*s1
&& !is_digit(*s1
)) || (*s2
&& !is_digit(*s2
))) {
214 order
= c_order(*s1
) - c_order(*s2
);
227 while (is_digit(*s1
) && is_digit(*s2
)) {
243 return strcmp(os1
, os2
);
246 static int boot_entry_compare(const void *a
, const void *b
) {
247 const BootEntry
*aa
= a
;
248 const BootEntry
*bb
= b
;
250 return str_verscmp(aa
->filename
, bb
->filename
);
253 int boot_entries_find(const char *dir
, BootEntry
**entries
, size_t *n_entries
) {
254 _cleanup_strv_free_
char **files
= NULL
;
258 BootEntry
*array
= NULL
;
259 size_t n_allocated
= 0, n
= 0;
261 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
263 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
265 STRV_FOREACH(f
, files
) {
266 if (!GREEDY_REALLOC0(array
, n_allocated
, n
+ 1))
269 r
= boot_entry_load(*f
, array
+ n
);
276 qsort_safe(array
, n
, sizeof(BootEntry
), boot_entry_compare
);
283 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
285 bool non_unique
= false;
287 for (i
= 0; i
< n_entries
; i
++)
290 for (i
= 0; i
< n_entries
; i
++)
291 for (j
= 0; j
< n_entries
; j
++)
292 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
293 boot_entry_title(entries
+ j
)))
294 non_unique
= arr
[i
] = arr
[j
] = true;
299 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
305 /* Find _all_ non-unique titles */
306 if (!find_nonunique(entries
, n_entries
, arr
))
309 /* Add version to non-unique titles */
310 for (i
= 0; i
< n_entries
; i
++)
311 if (arr
[i
] && entries
[i
].version
) {
312 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
316 free_and_replace(entries
[i
].show_title
, s
);
319 if (!find_nonunique(entries
, n_entries
, arr
))
322 /* Add machine-id to non-unique titles */
323 for (i
= 0; i
< n_entries
; i
++)
324 if (arr
[i
] && entries
[i
].machine_id
) {
325 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
329 free_and_replace(entries
[i
].show_title
, s
);
332 if (!find_nonunique(entries
, n_entries
, arr
))
335 /* Add file name to non-unique titles */
336 for (i
= 0; i
< n_entries
; i
++)
338 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].filename
);
342 free_and_replace(entries
[i
].show_title
, s
);
348 int boot_entries_select_default(const BootConfig
*config
) {
351 if (config
->entry_oneshot
)
352 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
353 if (streq(config
->entry_oneshot
, config
->entries
[i
].filename
)) {
354 log_debug("Found default: filename \"%s\" is matched by LoaderEntryOneShot",
355 config
->entries
[i
].filename
);
359 if (config
->entry_default
)
360 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
361 if (streq(config
->entry_default
, config
->entries
[i
].filename
)) {
362 log_debug("Found default: filename \"%s\" is matched by LoaderEntryDefault",
363 config
->entries
[i
].filename
);
367 if (config
->default_pattern
)
368 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
369 if (fnmatch(config
->default_pattern
, config
->entries
[i
].filename
, FNM_CASEFOLD
) == 0) {
370 log_debug("Found default: filename \"%s\" is matched by pattern \"%s\"",
371 config
->entries
[i
].filename
, config
->default_pattern
);
375 if (config
->n_entries
> 0)
376 log_debug("Found default: last entry \"%s\"", config
->entries
[i
].filename
);
378 log_debug("Found no default boot entry :(");
379 return config
->n_entries
- 1; /* -1 means "no default" */
382 int boot_entries_load_config(const char *esp_path
, BootConfig
*config
) {
386 p
= strjoina(esp_path
, "/loader/loader.conf");
387 r
= boot_loader_read_conf(p
, config
);
389 return log_error_errno(r
, "Failed to read boot config from \"%s\": %m", p
);
391 p
= strjoina(esp_path
, "/loader/entries");
392 r
= boot_entries_find(p
, &config
->entries
, &config
->n_entries
);
394 return log_error_errno(r
, "Failed to read boot entries from \"%s\": %m", p
);
396 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
398 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
400 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
401 if (r
< 0 && r
!= -ENOENT
)
402 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
404 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
405 if (r
< 0 && r
!= -ENOENT
)
406 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
408 config
->default_entry
= boot_entries_select_default(config
);
412 /********************************************************************************/
414 static int verify_esp(
418 uint64_t *ret_pstart
,
420 sd_id128_t
*ret_uuid
) {
422 _cleanup_blkid_free_probe_ blkid_probe b
= NULL
;
423 _cleanup_free_
char *t
= NULL
;
424 uint64_t pstart
= 0, psize
= 0;
428 sd_id128_t uuid
= SD_ID128_NULL
;
435 /* Non-root user can only check the status, so if an error occured in the following,
436 * it does not cause any issues. Let's silence the error messages. */
437 quiet
= geteuid() != 0;
439 if (statfs(p
, &sfs
) < 0) {
440 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
441 if (errno
== ENOENT
&& searching
)
444 return log_full_errno(quiet
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
445 "Failed to check file system type of \"%s\": %m", p
);
448 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
)) {
450 return -EADDRNOTAVAIL
;
452 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
456 if (stat(p
, &st
) < 0)
457 return log_full_errno(quiet
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
458 "Failed to determine block device node of \"%s\": %m", p
);
460 if (major(st
.st_dev
) == 0) {
461 log_error("Block device node of %p is invalid.", p
);
465 t2
= strjoina(p
, "/..");
468 return log_full_errno(quiet
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
469 "Failed to determine block device node of parent of \"%s\": %m", p
);
471 if (st
.st_dev
== st2
.st_dev
) {
472 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p
);
476 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
477 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
478 if (detect_container() > 0 || geteuid() != 0)
481 r
= asprintf(&t
, "/dev/block/%u:%u", major(st
.st_dev
), minor(st
.st_dev
));
486 b
= blkid_new_probe_from_filename(t
);
488 return log_error_errno(errno
?: ENOMEM
, "Failed to open file system \"%s\": %m", p
);
490 blkid_probe_enable_superblocks(b
, 1);
491 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
492 blkid_probe_enable_partitions(b
, 1);
493 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
496 r
= blkid_do_safeprobe(b
);
498 log_error("File system \"%s\" is ambiguous.", p
);
501 log_error("File system \"%s\" does not contain a label.", p
);
504 return log_error_errno(errno
?: EIO
, "Failed to probe file system \"%s\": %m", p
);
507 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
509 return log_error_errno(errno
?: EIO
, "Failed to probe file system type \"%s\": %m", p
);
510 if (!streq(v
, "vfat")) {
511 log_error("File system \"%s\" is not FAT.", p
);
516 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
518 return log_error_errno(errno
?: EIO
, "Failed to probe partition scheme \"%s\": %m", p
);
519 if (!streq(v
, "gpt")) {
520 log_error("File system \"%s\" is not on a GPT partition table.", p
);
525 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
527 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID \"%s\": %m", p
);
528 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
529 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p
);
534 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
536 return log_error_errno(errno
?: EIO
, "Failed to probe partition entry UUID \"%s\": %m", p
);
537 r
= sd_id128_from_string(v
, &uuid
);
539 log_error("Partition \"%s\" has invalid UUID \"%s\".", p
, v
);
544 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
546 return log_error_errno(errno
?: EIO
, "Failed to probe partition number \"%s\": m", p
);
547 r
= safe_atou32(v
, &part
);
549 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
552 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
554 return log_error_errno(errno
?: EIO
, "Failed to probe partition offset \"%s\": %m", p
);
555 r
= safe_atou64(v
, &pstart
);
557 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
560 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
562 return log_error_errno(errno
?: EIO
, "Failed to probe partition size \"%s\": %m", p
);
563 r
= safe_atou64(v
, &psize
);
565 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
571 *ret_pstart
= pstart
;
580 int find_esp(char **path
,
581 uint32_t *part
, uint64_t *pstart
, uint64_t *psize
, sd_id128_t
*uuid
) {
587 return verify_esp(false, *path
, part
, pstart
, psize
, uuid
);
589 FOREACH_STRING(p
, "/efi", "/boot", "/boot/efi") {
591 r
= verify_esp(true, p
, part
, pstart
, psize
, uuid
);
592 if (IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */