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>
25 #include "alloc-util.h"
26 #include "blkid-util.h"
28 #include "conf-files.h"
30 #include "device-nodes.h"
34 #include "parse-util.h"
35 #include "stat-util.h"
36 #include "string-util.h"
40 void boot_entry_free(BootEntry
*entry
) {
43 free(entry
->filename
);
45 free(entry
->show_title
);
47 free(entry
->machine_id
);
48 free(entry
->architecture
);
49 strv_free(entry
->options
);
52 strv_free(entry
->initrd
);
53 free(entry
->device_tree
);
56 int boot_entry_load(const char *path
, BootEntry
*entry
) {
57 _cleanup_(boot_entry_free
) BootEntry tmp
= {};
58 _cleanup_fclose_
FILE *f
= NULL
;
66 c
= endswith_no_case(path
, ".conf");
68 log_error("Invalid loader entry filename: %s", path
);
73 tmp
.filename
= strndup(b
, c
- b
);
77 f
= fopen(path
, "re");
79 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
82 _cleanup_free_
char *buf
= NULL
;
85 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
89 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
91 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
95 if (IN_SET(*strstrip(buf
), '#', '\0'))
100 log_warning("%s:%u: Bad syntax", path
, line
);
106 if (streq(buf
, "title"))
107 r
= free_and_strdup(&tmp
.title
, p
);
108 else if (streq(buf
, "version"))
109 r
= free_and_strdup(&tmp
.version
, p
);
110 else if (streq(buf
, "machine-id"))
111 r
= free_and_strdup(&tmp
.machine_id
, p
);
112 else if (streq(buf
, "architecture"))
113 r
= free_and_strdup(&tmp
.architecture
, p
);
114 else if (streq(buf
, "options"))
115 r
= strv_extend(&tmp
.options
, p
);
116 else if (streq(buf
, "linux"))
117 r
= free_and_strdup(&tmp
.kernel
, p
);
118 else if (streq(buf
, "efi"))
119 r
= free_and_strdup(&tmp
.efi
, p
);
120 else if (streq(buf
, "initrd"))
121 r
= strv_extend(&tmp
.initrd
, p
);
122 else if (streq(buf
, "devicetree"))
123 r
= free_and_strdup(&tmp
.device_tree
, p
);
125 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, buf
);
129 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
133 tmp
= (BootEntry
) {};
137 void boot_config_free(BootConfig
*config
) {
142 free(config
->default_pattern
);
143 free(config
->timeout
);
144 free(config
->editor
);
145 free(config
->auto_entries
);
146 free(config
->auto_firmware
);
148 free(config
->entry_oneshot
);
149 free(config
->entry_default
);
151 for (i
= 0; i
< config
->n_entries
; i
++)
152 boot_entry_free(config
->entries
+ i
);
153 free(config
->entries
);
156 int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
157 _cleanup_fclose_
FILE *f
= NULL
;
164 f
= fopen(path
, "re");
166 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
169 _cleanup_free_
char *buf
= NULL
;
172 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
176 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
178 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
182 if (IN_SET(*strstrip(buf
), '#', '\0'))
185 p
= strchr(buf
, ' ');
187 log_warning("%s:%u: Bad syntax", path
, line
);
193 if (streq(buf
, "default"))
194 r
= free_and_strdup(&config
->default_pattern
, p
);
195 else if (streq(buf
, "timeout"))
196 r
= free_and_strdup(&config
->timeout
, p
);
197 else if (streq(buf
, "editor"))
198 r
= free_and_strdup(&config
->editor
, p
);
199 else if (streq(buf
, "auto-entries"))
200 r
= free_and_strdup(&config
->auto_entries
, p
);
201 else if (streq(buf
, "auto-firmware"))
202 r
= free_and_strdup(&config
->auto_firmware
, p
);
204 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, buf
);
208 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
214 static int boot_entry_compare(const void *a
, const void *b
) {
215 const BootEntry
*aa
= a
, *bb
= b
;
217 return str_verscmp(aa
->filename
, bb
->filename
);
220 int boot_entries_find(const char *dir
, BootEntry
**ret_entries
, size_t *ret_n_entries
) {
221 _cleanup_strv_free_
char **files
= NULL
;
224 BootEntry
*array
= NULL
;
225 size_t n_allocated
= 0, n
= 0;
229 assert(ret_n_entries
);
231 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
233 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
235 STRV_FOREACH(f
, files
) {
236 if (!GREEDY_REALLOC0(array
, n_allocated
, n
+ 1))
239 r
= boot_entry_load(*f
, array
+ n
);
246 qsort_safe(array
, n
, sizeof(BootEntry
), boot_entry_compare
);
248 *ret_entries
= array
;
254 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
256 bool non_unique
= false;
258 assert(entries
|| n_entries
== 0);
259 assert(arr
|| n_entries
== 0);
261 for (i
= 0; i
< n_entries
; i
++)
264 for (i
= 0; i
< n_entries
; i
++)
265 for (j
= 0; j
< n_entries
; j
++)
266 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
267 boot_entry_title(entries
+ j
)))
268 non_unique
= arr
[i
] = arr
[j
] = true;
273 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
279 assert(entries
|| n_entries
== 0);
281 /* Find _all_ non-unique titles */
282 if (!find_nonunique(entries
, n_entries
, arr
))
285 /* Add version to non-unique titles */
286 for (i
= 0; i
< n_entries
; i
++)
287 if (arr
[i
] && entries
[i
].version
) {
288 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
292 free_and_replace(entries
[i
].show_title
, s
);
295 if (!find_nonunique(entries
, n_entries
, arr
))
298 /* Add machine-id to non-unique titles */
299 for (i
= 0; i
< n_entries
; i
++)
300 if (arr
[i
] && entries
[i
].machine_id
) {
301 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
305 free_and_replace(entries
[i
].show_title
, s
);
308 if (!find_nonunique(entries
, n_entries
, arr
))
311 /* Add file name to non-unique titles */
312 for (i
= 0; i
< n_entries
; i
++)
314 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].filename
);
318 free_and_replace(entries
[i
].show_title
, s
);
324 static int boot_entries_select_default(const BootConfig
*config
) {
329 if (config
->entry_oneshot
)
330 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
331 if (streq(config
->entry_oneshot
, config
->entries
[i
].filename
)) {
332 log_debug("Found default: filename \"%s\" is matched by LoaderEntryOneShot",
333 config
->entries
[i
].filename
);
337 if (config
->entry_default
)
338 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
339 if (streq(config
->entry_default
, config
->entries
[i
].filename
)) {
340 log_debug("Found default: filename \"%s\" is matched by LoaderEntryDefault",
341 config
->entries
[i
].filename
);
345 if (config
->default_pattern
)
346 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
347 if (fnmatch(config
->default_pattern
, config
->entries
[i
].filename
, FNM_CASEFOLD
) == 0) {
348 log_debug("Found default: filename \"%s\" is matched by pattern \"%s\"",
349 config
->entries
[i
].filename
, config
->default_pattern
);
353 if (config
->n_entries
> 0)
354 log_debug("Found default: last entry \"%s\"", config
->entries
[config
->n_entries
- 1].filename
);
356 log_debug("Found no default boot entry :(");
358 return config
->n_entries
- 1; /* -1 means "no default" */
361 int boot_entries_load_config(const char *esp_path
, BootConfig
*config
) {
368 p
= strjoina(esp_path
, "/loader/loader.conf");
369 r
= boot_loader_read_conf(p
, config
);
371 return log_error_errno(r
, "Failed to read boot config from \"%s\": %m", p
);
373 p
= strjoina(esp_path
, "/loader/entries");
374 r
= boot_entries_find(p
, &config
->entries
, &config
->n_entries
);
376 return log_error_errno(r
, "Failed to read boot entries from \"%s\": %m", p
);
378 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
380 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
382 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
383 if (r
< 0 && r
!= -ENOENT
)
384 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
386 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
387 if (r
< 0 && r
!= -ENOENT
)
388 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
390 config
->default_entry
= boot_entries_select_default(config
);
394 /********************************************************************************/
396 static int verify_esp(
399 bool unprivileged_mode
,
401 uint64_t *ret_pstart
,
403 sd_id128_t
*ret_uuid
) {
405 _cleanup_blkid_free_probe_ blkid_probe b
= NULL
;
406 char t
[DEV_NUM_PATH_MAX
];
409 uint64_t pstart
= 0, psize
= 0;
413 sd_id128_t uuid
= SD_ID128_NULL
;
419 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
420 * issues. Let's also, silence the error messages. */
422 if (statfs(p
, &sfs
) < 0) {
423 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
424 if (errno
== ENOENT
&& searching
)
427 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
428 "Failed to check file system type of \"%s\": %m", p
);
431 if (!F_TYPE_EQUAL(sfs
.f_type
, MSDOS_SUPER_MAGIC
)) {
433 return -EADDRNOTAVAIL
;
435 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p
);
439 if (stat(p
, &st
) < 0)
440 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
441 "Failed to determine block device node of \"%s\": %m", p
);
443 if (major(st
.st_dev
) == 0) {
444 log_error("Block device node of %p is invalid.", p
);
448 t2
= strjoina(p
, "/..");
451 return log_full_errno(unprivileged_mode
&& errno
== EACCES
? LOG_DEBUG
: LOG_ERR
, errno
,
452 "Failed to determine block device node of parent of \"%s\": %m", p
);
454 if (st
.st_dev
== st2
.st_dev
) {
455 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p
);
459 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
460 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
461 if (detect_container() > 0 || unprivileged_mode
)
465 xsprintf_dev_num_path(t
, "block", st
.st_dev
);
467 b
= blkid_new_probe_from_filename(t
);
469 return log_error_errno(errno
?: ENOMEM
, "Failed to open file system \"%s\": %m", p
);
471 blkid_probe_enable_superblocks(b
, 1);
472 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
473 blkid_probe_enable_partitions(b
, 1);
474 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
477 r
= blkid_do_safeprobe(b
);
479 log_error("File system \"%s\" is ambiguous.", p
);
482 log_error("File system \"%s\" does not contain a label.", p
);
485 return log_error_errno(errno
?: EIO
, "Failed to probe file system \"%s\": %m", p
);
488 r
= blkid_probe_lookup_value(b
, "TYPE", &v
, NULL
);
490 return log_error_errno(errno
?: EIO
, "Failed to probe file system type \"%s\": %m", p
);
491 if (!streq(v
, "vfat")) {
492 log_error("File system \"%s\" is not FAT.", p
);
497 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SCHEME", &v
, NULL
);
499 return log_error_errno(errno
?: EIO
, "Failed to probe partition scheme \"%s\": %m", p
);
500 if (!streq(v
, "gpt")) {
501 log_error("File system \"%s\" is not on a GPT partition table.", p
);
506 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_TYPE", &v
, NULL
);
508 return log_error_errno(errno
?: EIO
, "Failed to probe partition type UUID \"%s\": %m", p
);
509 if (!streq(v
, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
510 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p
);
515 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_UUID", &v
, NULL
);
517 return log_error_errno(errno
?: EIO
, "Failed to probe partition entry UUID \"%s\": %m", p
);
518 r
= sd_id128_from_string(v
, &uuid
);
520 log_error("Partition \"%s\" has invalid UUID \"%s\".", p
, v
);
525 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_NUMBER", &v
, NULL
);
527 return log_error_errno(errno
?: EIO
, "Failed to probe partition number \"%s\": m", p
);
528 r
= safe_atou32(v
, &part
);
530 return log_error_errno(r
, "Failed to parse PART_ENTRY_NUMBER field.");
533 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_OFFSET", &v
, NULL
);
535 return log_error_errno(errno
?: EIO
, "Failed to probe partition offset \"%s\": %m", p
);
536 r
= safe_atou64(v
, &pstart
);
538 return log_error_errno(r
, "Failed to parse PART_ENTRY_OFFSET field.");
541 r
= blkid_probe_lookup_value(b
, "PART_ENTRY_SIZE", &v
, NULL
);
543 return log_error_errno(errno
?: EIO
, "Failed to probe partition size \"%s\": %m", p
);
544 r
= safe_atou64(v
, &psize
);
546 return log_error_errno(r
, "Failed to parse PART_ENTRY_SIZE field.");
553 *ret_pstart
= pstart
;
562 int find_esp_and_warn(
564 bool unprivileged_mode
,
567 uint64_t *ret_pstart
,
569 sd_id128_t
*ret_uuid
) {
573 /* This logs about all errors except:
575 * -ENOKEY → when we can't find the partition
576 * -EACCESS → when unprivileged_mode is true, and we can't access something
580 r
= verify_esp(path
, false, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
587 FOREACH_STRING(path
, "/efi", "/boot", "/boot/efi") {
589 r
= verify_esp(path
, true, unprivileged_mode
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
592 if (!IN_SET(r
, -ENOENT
, -EADDRNOTAVAIL
)) /* This one is not it */
596 /* No logging here */