1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include <linux/magic.h>
16 #include "alloc-util.h"
17 #include "blkid-util.h"
20 #include "dirent-util.h"
21 #include "efi-loader.h"
28 #include "locale-util.h"
29 #include "main-func.h"
32 #include "parse-util.h"
33 #include "pretty-print.h"
34 #include "random-util.h"
36 #include "stat-util.h"
37 #include "stdio-util.h"
38 #include "string-util.h"
40 #include "terminal-util.h"
41 #include "tmpfile-util.h"
42 #include "umask-util.h"
48 static char *arg_esp_path
= NULL
;
49 static char *arg_xbootldr_path
= NULL
;
50 static bool arg_print_esp_path
= false;
51 static bool arg_print_dollar_boot_path
= false;
52 static bool arg_touch_variables
= true;
53 static PagerFlags arg_pager_flags
= 0;
54 static bool arg_graceful
= false;
56 STATIC_DESTRUCTOR_REGISTER(arg_esp_path
, freep
);
57 STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path
, freep
);
59 static const char *arg_dollar_boot_path(void) {
60 /* $BOOT shall be the XBOOTLDR partition if it exists, and otherwise the ESP */
61 return arg_xbootldr_path
?: arg_esp_path
;
64 static int acquire_esp(
65 bool unprivileged_mode
,
69 sd_id128_t
*ret_uuid
) {
74 /* Find the ESP, and log about errors. Note that find_esp_and_warn() will log in all error cases on
75 * its own, except for ENOKEY (which is good, we want to show our own message in that case,
76 * suggesting use of --esp-path=) and EACCESS (only when we request unprivileged mode; in this case
77 * we simply eat up the error here, so that --list and --status work too, without noise about
80 r
= find_esp_and_warn(arg_esp_path
, unprivileged_mode
, &np
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
82 return log_error_errno(r
,
83 "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n"
84 "Alternatively, use --esp-path= to specify path to mount point.");
88 free_and_replace(arg_esp_path
, np
);
89 log_debug("Using EFI System Partition at %s.", arg_esp_path
);
94 static int acquire_xbootldr(bool unprivileged_mode
, sd_id128_t
*ret_uuid
) {
98 r
= find_xbootldr_and_warn(arg_xbootldr_path
, unprivileged_mode
, &np
, ret_uuid
);
100 log_debug_errno(r
, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
102 *ret_uuid
= SD_ID128_NULL
;
103 arg_xbootldr_path
= mfree(arg_xbootldr_path
);
109 free_and_replace(arg_xbootldr_path
, np
);
110 log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path
);
115 /* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */
116 static int get_file_version(int fd
, char **v
) {
126 if (fstat(fd
, &st
) < 0)
127 return log_error_errno(errno
, "Failed to stat EFI binary: %m");
129 r
= stat_verify_regular(&st
);
131 return log_error_errno(r
, "EFI binary is not a regular file: %m");
133 if (st
.st_size
< 27) {
138 buf
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
139 if (buf
== MAP_FAILED
)
140 return log_error_errno(errno
, "Failed to memory map EFI binary: %m");
142 s
= memmem(buf
, st
.st_size
- 8, "#### LoaderInfo: ", 17);
147 e
= memmem(s
, st
.st_size
- (s
- buf
), " ####", 5);
148 if (!e
|| e
- s
< 3) {
149 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Malformed version string.");
153 x
= strndup(s
, e
- s
);
161 (void) munmap(buf
, st
.st_size
);
166 static int enumerate_binaries(const char *esp_path
, const char *path
, const char *prefix
) {
167 _cleanup_closedir_
DIR *d
= NULL
;
175 p
= prefix_roota(esp_path
, path
);
181 return log_error_errno(errno
, "Failed to read \"%s\": %m", p
);
184 FOREACH_DIRENT(de
, d
, break) {
185 _cleanup_free_
char *v
= NULL
;
186 _cleanup_close_
int fd
= -1;
188 if (!endswith_no_case(de
->d_name
, ".efi"))
191 if (prefix
&& !startswith_no_case(de
->d_name
, prefix
))
194 fd
= openat(dirfd(d
), de
->d_name
, O_RDONLY
|O_CLOEXEC
);
196 return log_error_errno(errno
, "Failed to open \"%s/%s\" for reading: %m", p
, de
->d_name
);
198 r
= get_file_version(fd
, &v
);
202 printf(" File: %s/%s/%s (%s%s%s)\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), path
, de
->d_name
, ansi_highlight(), v
, ansi_normal());
204 printf(" File: %s/%s/%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), path
, de
->d_name
);
212 static int status_binaries(const char *esp_path
, sd_id128_t partition
) {
215 printf("Available Boot Loaders on ESP:\n");
218 printf(" ESP: Cannot find or access mount point of ESP.\n\n");
222 printf(" ESP: %s", esp_path
);
223 if (!sd_id128_is_null(partition
))
224 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR
")", SD_ID128_FORMAT_VAL(partition
));
227 r
= enumerate_binaries(esp_path
, "EFI/systemd", NULL
);
231 log_info("systemd-boot not installed in ESP.");
233 r
= enumerate_binaries(esp_path
, "EFI/BOOT", "boot");
237 log_info("No default/fallback boot loader installed in ESP.");
246 static int print_efi_option(uint16_t id
, bool in_order
) {
247 _cleanup_free_
char *title
= NULL
;
248 _cleanup_free_
char *path
= NULL
;
249 sd_id128_t partition
;
253 r
= efi_get_boot_option(id
, &title
, &partition
, &path
, &active
);
257 /* print only configured entries with partition information */
258 if (!path
|| sd_id128_is_null(partition
))
261 efi_tilt_backslashes(path
);
263 printf(" Title: %s%s%s\n", ansi_highlight(), strna(title
), ansi_normal());
264 printf(" ID: 0x%04X\n", id
);
265 printf(" Status: %sactive%s\n", active
? "" : "in", in_order
? ", boot-order" : "");
266 printf(" Partition: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR
"\n",
267 SD_ID128_FORMAT_VAL(partition
));
268 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), path
);
274 static int status_variables(void) {
275 _cleanup_free_
uint16_t *options
= NULL
, *order
= NULL
;
276 int n_options
, n_order
, i
;
278 n_options
= efi_get_boot_options(&options
);
279 if (n_options
== -ENOENT
)
280 return log_error_errno(n_options
,
281 "Failed to access EFI variables, efivarfs"
282 " needs to be available at /sys/firmware/efi/efivars/.");
284 return log_error_errno(n_options
, "Failed to read EFI boot entries: %m");
286 n_order
= efi_get_boot_order(&order
);
287 if (n_order
== -ENOENT
)
289 else if (n_order
< 0)
290 return log_error_errno(n_order
, "Failed to read EFI boot order: %m");
292 /* print entries in BootOrder first */
293 printf("Boot Loaders Listed in EFI Variables:\n");
294 for (i
= 0; i
< n_order
; i
++)
295 print_efi_option(order
[i
], true);
297 /* print remaining entries */
298 for (i
= 0; i
< n_options
; i
++) {
301 for (j
= 0; j
< n_order
; j
++)
302 if (options
[i
] == order
[j
])
305 print_efi_option(options
[i
], false);
314 static int boot_entry_file_check(const char *root
, const char *p
) {
315 _cleanup_free_
char *path
;
317 path
= path_join(root
, p
);
321 if (access(path
, F_OK
) < 0)
327 static void boot_entry_file_list(const char *field
, const char *root
, const char *p
, int *ret_status
) {
328 int status
= boot_entry_file_check(root
, p
);
330 printf("%13s%s ", strempty(field
), field
? ":" : " ");
333 printf("%s%s%s (%m)\n", ansi_highlight_red(), p
, ansi_normal());
337 if (*ret_status
== 0 && status
< 0)
338 *ret_status
= status
;
341 static int boot_entry_show(const BootEntry
*e
, bool show_as_default
) {
344 /* Returns 0 on success, negative on processing error, and positive if something is wrong with the
345 boot entry itself. */
349 printf(" title: %s%s%s" "%s%s%s\n",
350 ansi_highlight(), boot_entry_title(e
), ansi_normal(),
351 ansi_highlight_green(), show_as_default
? " (default)" : "", ansi_normal());
354 printf(" id: %s\n", e
->id
);
356 _cleanup_free_
char *link
= NULL
;
358 /* Let's urlify the link to make it easy to view in an editor, but only if it is a text
359 * file. Unified images are binary ELFs, and EFI variables are not pure text either. */
360 if (e
->type
== BOOT_ENTRY_CONF
)
361 (void) terminal_urlify_path(e
->path
, NULL
, &link
);
363 printf(" source: %s\n", link
?: e
->path
);
366 printf(" version: %s\n", e
->version
);
368 printf(" machine-id: %s\n", e
->machine_id
);
370 printf(" architecture: %s\n", e
->architecture
);
372 boot_entry_file_list("linux", e
->root
, e
->kernel
, &status
);
375 STRV_FOREACH(s
, e
->initrd
)
376 boot_entry_file_list(s
== e
->initrd
? "initrd" : NULL
,
380 if (!strv_isempty(e
->options
)) {
381 _cleanup_free_
char *t
= NULL
, *t2
= NULL
;
382 _cleanup_strv_free_
char **ts
= NULL
;
384 t
= strv_join(e
->options
, " ");
388 ts
= strv_split_newlines(t
);
392 t2
= strv_join(ts
, "\n ");
396 printf(" options: %s\n", t2
);
399 boot_entry_file_list("devicetree", e
->root
, e
->device_tree
, &status
);
404 static int status_entries(
405 const char *esp_path
,
406 sd_id128_t esp_partition_uuid
,
407 const char *xbootldr_path
,
408 sd_id128_t xbootldr_partition_uuid
) {
410 _cleanup_(boot_config_free
) BootConfig config
= {};
411 sd_id128_t dollar_boot_partition_uuid
;
412 const char *dollar_boot_path
;
415 assert(esp_path
|| xbootldr_path
);
418 dollar_boot_path
= xbootldr_path
;
419 dollar_boot_partition_uuid
= xbootldr_partition_uuid
;
421 dollar_boot_path
= esp_path
;
422 dollar_boot_partition_uuid
= esp_partition_uuid
;
425 printf("Boot Loader Entries:\n"
426 " $BOOT: %s", dollar_boot_path
);
427 if (!sd_id128_is_null(dollar_boot_partition_uuid
))
428 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR
")",
429 SD_ID128_FORMAT_VAL(dollar_boot_partition_uuid
));
432 r
= boot_entries_load_config(esp_path
, xbootldr_path
, &config
);
436 if (config
.default_entry
< 0)
437 printf("%zu entries, no entry could be determined as default.\n", config
.n_entries
);
439 printf("Default Boot Loader Entry:\n");
441 r
= boot_entry_show(config
.entries
+ config
.default_entry
, false);
443 /* < 0 is already logged by the function itself, let's just emit an extra warning if
444 the default entry is broken */
445 printf("\nWARNING: default boot entry is broken\n");
451 static int compare_product(const char *a
, const char *b
) {
460 return x
< y
? -1 : x
> y
? 1 : 0;
462 return strncmp(a
, b
, x
);
465 static int compare_version(const char *a
, const char *b
) {
469 a
+= strcspn(a
, " ");
471 b
+= strcspn(b
, " ");
474 return strverscmp_improved(a
, b
);
477 static int version_check(int fd_from
, const char *from
, int fd_to
, const char *to
) {
478 _cleanup_free_
char *a
= NULL
, *b
= NULL
;
481 assert(fd_from
>= 0);
486 r
= get_file_version(fd_from
, &a
);
490 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
491 "Source file \"%s\" does not carry version information!",
494 r
= get_file_version(fd_to
, &b
);
497 if (r
== 0 || compare_product(a
, b
) != 0)
498 return log_notice_errno(SYNTHETIC_ERRNO(EEXIST
),
499 "Skipping \"%s\", since it's owned by another boot loader.",
502 if (compare_version(a
, b
) < 0)
503 return log_warning_errno(SYNTHETIC_ERRNO(ESTALE
), "Skipping \"%s\", since a newer boot loader version exists already.", to
);
508 static int copy_file_with_version_check(const char *from
, const char *to
, bool force
) {
509 _cleanup_close_
int fd_from
= -1, fd_to
= -1;
510 _cleanup_free_
char *t
= NULL
;
513 fd_from
= open(from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
515 return log_error_errno(errno
, "Failed to open \"%s\" for reading: %m", from
);
518 fd_to
= open(to
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
521 return log_error_errno(errno
, "Failed to open \"%s\" for reading: %m", to
);
523 r
= version_check(fd_from
, from
, fd_to
, to
);
527 if (lseek(fd_from
, 0, SEEK_SET
) == (off_t
) -1)
528 return log_error_errno(errno
, "Failed to seek in \"%s\": %m", from
);
530 fd_to
= safe_close(fd_to
);
534 r
= tempfn_random(to
, NULL
, &t
);
538 RUN_WITH_UMASK(0000) {
539 fd_to
= open(t
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_EXCL
|O_NOFOLLOW
, 0644);
541 return log_error_errno(errno
, "Failed to open \"%s\" for writing: %m", t
);
544 r
= copy_bytes(fd_from
, fd_to
, (uint64_t) -1, COPY_REFLINK
);
547 return log_error_errno(r
, "Failed to copy data from \"%s\" to \"%s\": %m", from
, t
);
550 (void) copy_times(fd_from
, fd_to
, 0);
552 if (fsync(fd_to
) < 0) {
553 (void) unlink_noerrno(t
);
554 return log_error_errno(errno
, "Failed to copy data from \"%s\" to \"%s\": %m", from
, t
);
557 (void) fsync_directory_of_file(fd_to
);
559 if (renameat(AT_FDCWD
, t
, AT_FDCWD
, to
) < 0) {
560 (void) unlink_noerrno(t
);
561 return log_error_errno(errno
, "Failed to rename \"%s\" to \"%s\": %m", t
, to
);
564 log_info("Copied \"%s\" to \"%s\".", from
, to
);
569 static int mkdir_one(const char *prefix
, const char *suffix
) {
570 _cleanup_free_
char *p
= NULL
;
572 p
= path_join(prefix
, suffix
);
573 if (mkdir(p
, 0700) < 0) {
575 return log_error_errno(errno
, "Failed to create \"%s\": %m", p
);
577 log_info("Created \"%s\".", p
);
582 static const char *const esp_subdirs
[] = {
583 /* The directories to place in the ESP */
591 static const char *const dollar_boot_subdirs
[] = {
592 /* The directories to place in the XBOOTLDR partition or the ESP, depending what exists */
594 "loader/entries", /* Type #1 entries */
596 "EFI/Linux", /* Type #2 entries */
600 static int create_subdirs(const char *root
, const char * const *subdirs
) {
601 const char *const *i
;
604 STRV_FOREACH(i
, subdirs
) {
605 r
= mkdir_one(root
, *i
);
613 static int copy_one_file(const char *esp_path
, const char *name
, bool force
) {
618 p
= strjoina(BOOTLIBDIR
"/", name
);
619 q
= strjoina(esp_path
, "/EFI/systemd/", name
);
620 r
= copy_file_with_version_check(p
, q
, force
);
622 e
= startswith(name
, "systemd-boot");
627 /* Create the EFI default boot loader name (specified for removable devices) */
628 v
= strjoina(esp_path
, "/EFI/BOOT/BOOT", e
);
629 ascii_strupper(strrchr(v
, '/') + 1);
631 k
= copy_file_with_version_check(p
, v
, force
);
639 static int install_binaries(const char *esp_path
, bool force
) {
641 _cleanup_closedir_
DIR *d
= NULL
;
644 d
= opendir(BOOTLIBDIR
);
646 return log_error_errno(errno
, "Failed to open \""BOOTLIBDIR
"\": %m
");
648 FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read
\""BOOTLIBDIR"\": %m
")) {
651 if (!endswith_no_case(de->d_name, ".efi
"))
654 k = copy_one_file(esp_path, de->d_name, force);
662 static bool same_entry(uint16_t id, sd_id128_t uuid, const char *path) {
663 _cleanup_free_ char *opath = NULL;
667 r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
670 if (!sd_id128_equal(uuid, ouuid))
672 if (!streq_ptr(path, opath))
678 static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
679 _cleanup_free_ uint16_t *options = NULL;
682 n = efi_get_boot_options(&options);
686 /* find already existing systemd-boot entry */
687 for (i = 0; i < n; i++)
688 if (same_entry(options[i], uuid, path)) {
693 /* find free slot in the sorted BootXXXX variable list */
694 for (i = 0; i < n; i++)
695 if (i != options[i]) {
700 /* use the next one */
707 static int insert_into_order(uint16_t slot, bool first) {
708 _cleanup_free_ uint16_t *order = NULL;
712 n = efi_get_boot_order(&order);
714 /* no entry, add us */
715 return efi_set_boot_order(&slot, 1);
717 /* are we the first and only one? */
718 if (n == 1 && order[0] == slot)
721 /* are we already in the boot order? */
722 for (i = 0; i < n; i++) {
723 if (order[i] != slot)
726 /* we do not require to be the first one, all is fine */
730 /* move us to the first slot */
731 memmove(order + 1, order, i * sizeof(uint16_t));
733 return efi_set_boot_order(order, n);
737 t = reallocarray(order, n + 1, sizeof(uint16_t));
742 /* add us to the top or end of the list */
744 memmove(order + 1, order, n * sizeof(uint16_t));
749 return efi_set_boot_order(order, n + 1);
752 static int remove_from_order(uint16_t slot) {
753 _cleanup_free_ uint16_t *order = NULL;
756 n = efi_get_boot_order(&order);
760 for (i = 0; i < n; i++) {
761 if (order[i] != slot)
765 memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
766 return efi_set_boot_order(order, n - 1);
772 static int install_variables(const char *esp_path,
773 uint32_t part, uint64_t pstart, uint64_t psize,
774 sd_id128_t uuid, const char *path,
780 if (!is_efi_boot()) {
781 log_warning("Not booted with EFI
, skipping EFI variable setup
.");
785 p = prefix_roota(esp_path, path);
786 if (access(p, F_OK) < 0) {
790 return log_error_errno(errno, "Cannot access
\"%s
\": %m
", p);
793 r = find_slot(uuid, path, &slot);
795 return log_error_errno(r,
797 "Failed to access EFI variables
. Is the
\"efivarfs
\" filesystem mounted
?" :
798 "Failed to determine current boot order
: %m
");
800 if (first || r == 0) {
801 r = efi_add_boot_option(slot, "Linux Boot Manager
",
805 return log_error_errno(r, "Failed to create EFI Boot variable entry
: %m
");
807 log_info("Created EFI boot entry
\"Linux Boot Manager
\".");
810 return insert_into_order(slot, first);
813 static int remove_boot_efi(const char *esp_path) {
814 _cleanup_closedir_ DIR *d = NULL;
819 p = prefix_roota(esp_path, "/EFI
/BOOT
");
825 return log_error_errno(errno, "Failed to open directory
\"%s
\": %m
", p);
828 FOREACH_DIRENT(de, d, break) {
829 _cleanup_close_ int fd = -1;
830 _cleanup_free_ char *v = NULL;
832 if (!endswith_no_case(de->d_name, ".efi
"))
835 if (!startswith_no_case(de->d_name, "boot
"))
838 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
840 return log_error_errno(errno, "Failed to open
\"%s
/%s
\" for reading
: %m
", p, de->d_name);
842 r = get_file_version(fd, &v);
845 if (r > 0 && startswith(v, "systemd
-boot
")) {
846 r = unlinkat(dirfd(d), de->d_name, 0);
848 return log_error_errno(errno, "Failed to remove
\"%s
/%s
\": %m
", p, de->d_name);
850 log_info("Removed
\"%s
/%s
\".", p, de->d_name);
859 static int rmdir_one(const char *prefix, const char *suffix) {
862 p = prefix_roota(prefix, suffix);
864 bool ignore = IN_SET(errno, ENOENT, ENOTEMPTY);
866 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, errno,
867 "Failed to remove directory
\"%s
\": %m
", p);
871 log_info("Removed
\"%s
\".", p);
876 static int remove_subdirs(const char *root, const char *const *subdirs) {
879 /* We use recursion here to destroy the directories in reverse order. Which should be safe given how
880 * short the array is. */
882 if (!subdirs[0]) /* A the end of the list */
885 r = remove_subdirs(root, subdirs + 1);
886 q = rmdir_one(root, subdirs[0]);
888 return r < 0 ? r : q;
891 static int remove_binaries(const char *esp_path) {
895 p = prefix_roota(esp_path, "/EFI
/systemd
");
896 r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
898 q = remove_boot_efi(esp_path);
905 static int remove_file(const char *root, const char *file) {
911 p = prefix_roota(root, file);
913 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
914 "Failed to unlink file
\"%s
\": %m
", p);
916 return errno == ENOENT ? 0 : -errno;
919 log_info("Removed
\"%s
\".", p);
923 static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
930 r = find_slot(uuid, path, &slot);
934 r = efi_remove_boot_option(slot);
939 return remove_from_order(slot);
944 static int remove_loader_variables(void) {
948 /* Remove all persistent loader variables we define */
951 "LoaderConfigTimeout
",
952 "LoaderConfigTimeoutOneShot
",
953 "LoaderEntryDefault
",
954 "LoaderEntryOneShot
",
955 "LoaderSystemToken
") {
959 q = efi_set_variable(EFI_VENDOR_LOADER, p, NULL, 0);
963 log_warning_errno(q, "Failed to remove
%s variable
: %m
", p);
967 log_info("Removed EFI variable
%s
.", p);
973 static int install_loader_config(const char *esp_path) {
974 _cleanup_(unlink_and_freep) char *t = NULL;
975 _cleanup_fclose_ FILE *f = NULL;
976 _cleanup_close_ int fd = -1;
980 p = prefix_roota(esp_path, "/loader
/loader
.conf
");
981 if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
984 fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
986 return log_error_errno(fd, "Failed to open
\"%s
\" for writing
: %m
", p);
988 f = take_fdopen(&fd, "w
");
992 fprintf(f, "#timeout 3\n"
993 "#console-mode keep\n");
995 r = fflush_sync_and_check(f);
997 return log_error_errno(r, "Failed to write \"%s\": %m", p);
999 r = link_tmpfile(fileno(f), t, p);
1001 return 0; /* Silently skip creation if the file exists now (recheck) */
1003 return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
1009 static int help(int argc, char *argv[], void *userdata) {
1010 _cleanup_free_ char *link = NULL;
1013 r = terminal_urlify_man("bootctl", "1", &link);
1017 printf("%1$s [OPTIONS...] COMMAND ...\n"
1018 "\n%5$sControl EFI firmware boot settings and manage boot loader.%6$s\n"
1019 "\n%3$sGeneric EFI Firmware/Boot Loader Commands:%4$s\n"
1020 " status Show status of installed boot loader and EFI variables\n"
1021 " reboot-to-firmware [BOOL]\n"
1022 " Query or set reboot-to-firmware EFI flag\n"
1023 " systemd-efi-options [STRING]\n"
1024 " Query or set system options string in EFI variable\n"
1025 "\n%3$sBoot Loader Specification Commands:%4$s\n"
1026 " list List boot loader entries\n"
1027 " set-default ID Set default boot loader entry\n"
1028 " set-oneshot ID Set default boot loader entry, for next boot only\n"
1029 "\n%3$ssystemd-boot Commands:%4$s\n"
1030 " install Install systemd-boot to the ESP and EFI variables\n"
1031 " update Update systemd-boot in the ESP and EFI variables\n"
1032 " remove Remove systemd-boot from the ESP and EFI variables\n"
1033 " is-installed Test whether systemd-boot is installed in the ESP\n"
1034 " random-seed Initialize random seed in ESP and EFI variables\n"
1035 "\n%3$sOptions:%4$s\n"
1036 " -h --help Show this help\n"
1037 " --version Print version\n"
1038 " --esp-path=PATH Path to the EFI System Partition (ESP)\n"
1039 " --boot-path=PATH Path to the $BOOT partition\n"
1040 " -p --print-esp-path Print path to the EFI System Partition\n"
1041 " -x --print-boot-path Print path to the $BOOT partition\n"
1042 " --no-variables Don't touch EFI variables\n"
1043 " --no-pager Do not pipe output into a pager\n"
1044 " --graceful Don't fail when the ESP cannot be found or EFI\n"
1045 " variables cannot be written\n"
1046 "\nSee the %2$s for details.\n",
1047 program_invocation_short_name,
1057 static int parse_argv(int argc, char *argv[]) {
1059 ARG_ESP_PATH = 0x100,
1067 static const struct option options[] = {
1068 { "help", no_argument, NULL, 'h' },
1069 { "version", no_argument, NULL, ARG_VERSION },
1070 { "esp-path", required_argument, NULL, ARG_ESP_PATH },
1071 { "path", required_argument, NULL, ARG_ESP_PATH }, /* Compatibility alias */
1072 { "boot-path", required_argument, NULL, ARG_BOOT_PATH },
1073 { "print-esp-path", no_argument, NULL, 'p' },
1074 { "print-path", no_argument, NULL, 'p' }, /* Compatibility alias */
1075 { "print-boot-path", no_argument, NULL, 'x' },
1076 { "no-variables", no_argument, NULL, ARG_NO_VARIABLES },
1077 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1078 { "graceful", no_argument, NULL, ARG_GRACEFUL },
1087 while ((c = getopt_long(argc, argv, "hpx", options, NULL)) >= 0)
1091 help(0, NULL, NULL);
1098 r = free_and_strdup(&arg_esp_path, optarg);
1104 r = free_and_strdup(&arg_xbootldr_path, optarg);
1110 if (arg_print_dollar_boot_path)
1111 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1112 "--print-boot-path/-x cannot be combined with --print-esp-path/-p");
1113 arg_print_esp_path = true;
1117 if (arg_print_esp_path)
1118 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1119 "--print-boot-path/-x cannot be combined with --print-esp-path/-p");
1120 arg_print_dollar_boot_path = true;
1123 case ARG_NO_VARIABLES:
1124 arg_touch_variables = false;
1128 arg_pager_flags |= PAGER_DISABLE;
1132 arg_graceful = true;
1139 assert_not_reached("Unknown option");
1145 static void read_loader_efi_var(const char *name, char **var) {
1148 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var);
1149 if (r < 0 && r != -ENOENT)
1150 log_warning_errno(r, "Failed to read EFI variable %s: %m", name);
1153 static void print_yes_no_line(bool first, bool good, const char *name) {
1154 printf("%s%s%s%s %s\n",
1155 first ? " Features: " : " ",
1156 good ? ansi_highlight_green() : ansi_highlight_red(),
1157 good ? special_glyph(SPECIAL_GLYPH_CHECK_MARK) : special_glyph(SPECIAL_GLYPH_CROSS_MARK),
1162 static int verb_status(int argc, char *argv[], void *userdata) {
1163 sd_id128_t esp_uuid = SD_ID128_NULL, xbootldr_uuid = SD_ID128_NULL;
1166 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &esp_uuid);
1167 if (arg_print_esp_path) {
1168 if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only
1169 * error the find_esp_and_warn() won't log on its own) */
1170 return log_error_errno(r, "Failed to determine ESP location: %m");
1177 r = acquire_xbootldr(geteuid() != 0, &xbootldr_uuid);
1178 if (arg_print_dollar_boot_path) {
1180 return log_error_errno(r, "Failed to determine XBOOTLDR location: %m");
1184 const char *path = arg_dollar_boot_path();
1186 return log_error_errno(SYNTHETIC_ERRNO(EACCES), "Failed to determine XBOOTLDR location: %m");
1191 if (arg_print_esp_path || arg_print_dollar_boot_path)
1194 r = 0; /* If we couldn't determine the path, then don't consider that a problem from here on, just show what we
1197 (void) pager_open(arg_pager_flags);
1199 if (is_efi_boot()) {
1200 static const struct {
1204 { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" },
1205 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" },
1206 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" },
1207 { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" },
1208 { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" },
1209 { EFI_LOADER_FEATURE_XBOOTLDR, "Support for XBOOTLDR partition" },
1210 { EFI_LOADER_FEATURE_RANDOM_SEED, "Support for passing random seed to OS" },
1213 _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL;
1214 sd_id128_t loader_part_uuid = SD_ID128_NULL;
1215 uint64_t loader_features = 0;
1218 read_loader_efi_var("LoaderFirmwareType", &fw_type);
1219 read_loader_efi_var("LoaderFirmwareInfo", &fw_info);
1220 read_loader_efi_var("LoaderInfo", &loader);
1221 read_loader_efi_var("StubInfo", &stub);
1222 read_loader_efi_var("LoaderImageIdentifier", &loader_path);
1223 (void) efi_loader_get_features(&loader_features);
1226 efi_tilt_backslashes(loader_path);
1228 k = efi_loader_get_device_part_uuid(&loader_part_uuid);
1229 if (k < 0 && k != -ENOENT)
1230 r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m");
1232 printf("System:\n");
1233 printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal());
1234 printf(" Secure Boot: %sd\n", enable_disable(is_efi_secure_boot()));
1235 printf(" Setup Mode: %s\n", is_efi_secure_boot_setup_mode() ? "setup" : "user");
1236 printf(" TPM2 Support: %s\n", yes_no(efi_has_tpm2()));
1238 k = efi_get_reboot_to_firmware();
1240 printf(" Boot into FW: %sactive%s\n", ansi_highlight_yellow(), ansi_normal());
1242 printf(" Boot into FW: supported\n");
1243 else if (k == -EOPNOTSUPP)
1244 printf(" Boot into FW: not supported\n");
1247 printf(" Boot into FW: %sfailed%s (%m)\n", ansi_highlight_red(), ansi_normal());
1251 printf("Current Boot Loader:\n");
1252 printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal());
1254 for (i = 0; i < ELEMENTSOF(flags); i++)
1255 print_yes_no_line(i == 0, FLAGS_SET(loader_features, flags[i].flag), flags[i].name);
1257 sd_id128_t bootloader_esp_uuid;
1258 bool have_bootloader_esp_uuid = efi_loader_get_device_part_uuid(&bootloader_esp_uuid) >= 0;
1260 print_yes_no_line(false, have_bootloader_esp_uuid, "Boot loader sets ESP partition information");
1261 if (have_bootloader_esp_uuid && !sd_id128_equal(esp_uuid, bootloader_esp_uuid))
1262 printf("WARNING: The boot loader reports a different ESP UUID than detected!\n");
1265 printf(" Stub: %s\n", stub);
1266 if (!sd_id128_is_null(loader_part_uuid))
1267 printf(" ESP: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
1268 SD_ID128_FORMAT_VAL(loader_part_uuid));
1270 printf(" ESP: n/a\n");
1271 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), strna(loader_path));
1274 printf("Random Seed:\n");
1275 printf(" Passed to OS: %s\n", yes_no(access("/sys/firmware/efi/efivars/LoaderRandomSeed-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) >= 0));
1276 printf(" System Token: %s\n", access("/sys/firmware/efi/efivars/LoaderSystemToken-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) >= 0 ? "set" : "not set");
1279 _cleanup_free_ char *p = NULL;
1281 p = path_join(arg_esp_path, "/loader/random-seed");
1285 printf(" Exists: %s\n", yes_no(access(p, F_OK) >= 0));
1290 printf("System:\n Not booted with EFI\n\n");
1293 k = status_binaries(arg_esp_path, esp_uuid);
1298 if (is_efi_boot()) {
1299 k = status_variables();
1304 if (arg_esp_path || arg_xbootldr_path) {
1305 k = status_entries(arg_esp_path, esp_uuid, arg_xbootldr_path, xbootldr_uuid);
1313 static int verb_list(int argc, char *argv[], void *userdata) {
1314 _cleanup_(boot_config_free) BootConfig config = {};
1315 _cleanup_strv_free_ char **efi_entries = NULL;
1318 /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two things: turn
1319 * off logging about access errors and turn off potentially privileged device probing. Here we're interested in
1320 * the latter but not the former, hence request the mode, and log about EACCES. */
1322 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, NULL);
1323 if (r == -EACCES) /* We really need the ESP path for this call, hence also log about access errors */
1324 return log_error_errno(r, "Failed to determine ESP: %m");
1328 r = acquire_xbootldr(geteuid() != 0, NULL);
1330 return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
1334 r = boot_entries_load_config(arg_esp_path, arg_xbootldr_path, &config);
1338 r = efi_loader_get_entries(&efi_entries);
1339 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
1340 log_debug_errno(r, "Boot loader reported no entries.");
1342 log_warning_errno(r, "Failed to determine entries reported by boot loader, ignoring: %m");
1344 (void) boot_entries_augment_from_loader(&config, efi_entries, false);
1346 if (config.n_entries == 0)
1347 log_info("No boot loader entries found.");
1351 (void) pager_open(arg_pager_flags);
1353 printf("Boot Loader Entries:\n");
1355 for (n = 0; n < config.n_entries; n++) {
1356 r = boot_entry_show(config.entries + n, n == (size_t) config.default_entry);
1360 if (n+1 < config.n_entries)
1368 static int install_random_seed(const char *esp) {
1369 _cleanup_(unlink_and_freep) char *tmp = NULL;
1370 _cleanup_free_ void *buffer = NULL;
1371 _cleanup_free_ char *path = NULL;
1372 _cleanup_close_ int fd = -1;
1373 size_t sz, token_size;
1379 path = path_join(esp, "/loader/random-seed");
1383 sz = random_pool_size();
1385 buffer = malloc(sz);
1389 r = genuine_random_bytes(buffer, sz, RANDOM_BLOCK);
1391 return log_error_errno(r, "Failed to acquire random seed: %m");
1393 /* Normally create_subdirs() should already have created everything we need, but in case "bootctl
1394 * random
-seed
" is called we want to just create the minimum we need for it, and not the full
1396 r = mkdir_parents(path, 0755);
1398 return log_error_errno(r, "Failed to create parent directory
for %s
: %m
", path);
1400 r = tempfn_random(path, "bootctl
", &tmp);
1404 fd = open(tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|O_WRONLY|O_CLOEXEC, 0600);
1407 return log_error_errno(fd, "Failed to open random seed file
for writing
: %m
");
1410 n = write(fd, buffer, sz);
1412 return log_error_errno(errno, "Failed to write random seed file
: %m
");
1413 if ((size_t) n != sz)
1414 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write
while writing random seed file
.");
1416 if (rename(tmp, path) < 0)
1417 return log_error_errno(r, "Failed to move random seed file into place
: %m
");
1421 log_info("Random seed file
%s successfully
written (%zu bytes
).", path, sz);
1423 if (!arg_touch_variables)
1426 if (!is_efi_boot()) {
1427 log_notice("Not booted with EFI
, skipping EFI variable setup
.");
1431 r = getenv_bool("SYSTEMD_WRITE_SYSTEM_TOKEN
");
1434 log_warning_errno(r, "Failed to parse $SYSTEMD_WRITE_SYSTEM_TOKEN
, ignoring
.");
1436 if (detect_vm() > 0) {
1437 /* Let's not write a system token if we detect we are running in a VM
1438 * environment. Why? Our default security model for the random seed uses the system
1439 * token as a mechanism to ensure we are not vulnerable to golden master sloppiness
1440 * issues, i.e. that people initialize the random seed file, then copy the image to
1441 * many systems and end up with the same random seed in each that is assumed to be
1442 * valid but in reality is the same for all machines. By storing a system token in
1443 * the EFI variable space we can make sure that even though the random seeds on disk
1444 * are all the same they will be different on each system under the assumption that
1445 * the EFI variable space is maintained separate from the random seed storage. That
1446 * is generally the case on physical systems, as the ESP is stored on persistent
1447 * storage, and the EFI variables in NVRAM. However in virtualized environments this
1448 * is generally not true: the EFI variable set is typically stored along with the
1449 * disk image itself. For example, using the OVMF EFI firmware the EFI variables are
1450 * stored in a file in the ESP itself. */
1452 log_notice("Not installing system token
, since we are running in a virtualized environment
.");
1455 } else if (r == 0) {
1456 log_notice("Not writing system token
, because $SYSTEMD_WRITE_SYSTEM_TOKEN is set to
false.");
1460 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderSystemToken
", NULL, NULL, &token_size);
1462 log_debug_errno(r, "LoaderSystemToken EFI variable is
invalid (too
short?), replacing
.");
1465 return log_error_errno(r, "Failed to test system token validity
: %m
");
1467 if (token_size >= sz) {
1468 /* Let's avoid writes if we can, and initialize this only once. */
1469 log_debug("System token already written
, not updating
.");
1473 log_debug("Existing system token
size (%zu
) does
not match our
expectations (%zu
), replacing
.", token_size, sz);
1476 r = genuine_random_bytes(buffer, sz, RANDOM_BLOCK);
1478 return log_error_errno(r, "Failed to acquire random seed
: %m
");
1480 /* Let's write this variable with an umask in effect, so that unprivileged users can't see the token
1481 * and possibly get identification information or too much insight into the kernel's entropy pool
1483 RUN_WITH_UMASK(0077) {
1484 r = efi_set_variable(EFI_VENDOR_LOADER, "LoaderSystemToken
", buffer, sz);
1487 return log_error_errno(r, "Failed to write
'LoaderSystemToken' EFI variable
: %m
");
1490 log_warning_errno(r, "Unable to write
'LoaderSystemToken' EFI
variable (firmware problem
?), ignoring
: %m
");
1492 log_warning_errno(r, "Unable to write
'LoaderSystemToken' EFI variable
, ignoring
: %m
");
1494 log_info("Successfully initialized system token in EFI variable with
%zu bytes
.", sz);
1500 static int sync_everything(void) {
1504 k = syncfs_path(AT_FDCWD, arg_esp_path);
1506 ret = log_error_errno(k, "Failed to synchronize the ESP
'%s': %m
", arg_esp_path);
1509 if (arg_xbootldr_path) {
1510 k = syncfs_path(AT_FDCWD, arg_xbootldr_path);
1512 ret = log_error_errno(k, "Failed to synchronize $BOOT
'%s': %m
", arg_xbootldr_path);
1518 static int verb_install(int argc, char *argv[], void *userdata) {
1519 sd_id128_t uuid = SD_ID128_NULL;
1520 uint64_t pstart = 0, psize = 0;
1525 r = acquire_esp(false, &part, &pstart, &psize, &uuid);
1529 r = acquire_xbootldr(false, NULL);
1533 install = streq(argv[0], "install
");
1535 RUN_WITH_UMASK(0002) {
1537 /* Don't create any of these directories when we are just updating. When we update
1538 * we'll drop-in our files (unless there are newer ones already), but we won't create
1539 * the directories for them in the first place. */
1540 r = create_subdirs(arg_esp_path, esp_subdirs);
1544 r = create_subdirs(arg_dollar_boot_path(), dollar_boot_subdirs);
1549 r = install_binaries(arg_esp_path, install);
1554 r = install_loader_config(arg_esp_path);
1558 r = install_random_seed(arg_esp_path);
1564 (void) sync_everything();
1566 if (arg_touch_variables)
1567 r = install_variables(arg_esp_path,
1568 part, pstart, psize, uuid,
1569 "/EFI
/systemd
/systemd
-boot
" EFI_MACHINE_TYPE_NAME ".efi
",
1575 static int verb_remove(int argc, char *argv[], void *userdata) {
1576 sd_id128_t uuid = SD_ID128_NULL;
1579 r = acquire_esp(false, NULL, NULL, NULL, &uuid);
1583 r = acquire_xbootldr(false, NULL);
1587 r = remove_binaries(arg_esp_path);
1589 q = remove_file(arg_esp_path, "/loader
/loader
.conf
");
1590 if (q < 0 && r >= 0)
1593 q = remove_file(arg_esp_path, "/loader
/random
-seed
");
1594 if (q < 0 && r >= 0)
1597 q = remove_subdirs(arg_esp_path, esp_subdirs);
1598 if (q < 0 && r >= 0)
1601 q = remove_subdirs(arg_esp_path, dollar_boot_subdirs);
1602 if (q < 0 && r >= 0)
1605 if (arg_xbootldr_path) {
1606 /* Remove the latter two also in the XBOOTLDR partition if it exists */
1607 q = remove_subdirs(arg_xbootldr_path, dollar_boot_subdirs);
1608 if (q < 0 && r >= 0)
1612 (void) sync_everything();
1614 if (!arg_touch_variables)
1617 q = remove_variables(uuid, "/EFI
/systemd
/systemd
-boot
" EFI_MACHINE_TYPE_NAME ".efi
", true);
1618 if (q < 0 && r >= 0)
1621 q = remove_loader_variables();
1622 if (q < 0 && r >= 0)
1628 static int verb_is_installed(int argc, char *argv[], void *userdata) {
1629 _cleanup_free_ char *p = NULL;
1632 r = acquire_esp(false, NULL, NULL, NULL, NULL);
1636 /* Tests whether systemd-boot is installed. It's not obvious what to use as check here: we could
1637 * check EFI variables, we could check what binary /EFI/BOOT/BOOT*.EFI points to, or whether the
1638 * loader entries directory exists. Here we opted to check whether /EFI/systemd/ is non-empty, which
1639 * should be a suitable and very minimal check for a number of reasons:
1641 * → The check is architecture independent (i.e. we check if any systemd-boot loader is installed, not a
1644 * → It doesn't assume we are the only boot loader (i.e doesn't check if we own the main
1645 * /EFI/BOOT/BOOT*.EFI fallback binary.
1647 * → It specifically checks for systemd-boot, not for other boot loaders (which a check for
1648 * /boot/loader/entries would do). */
1650 p = path_join(arg_esp_path, "/EFI
/systemd
/");
1654 r = dir_is_empty(p);
1655 if (r > 0 || r == -ENOENT) {
1657 return EXIT_FAILURE;
1660 return log_error_errno(r, "Failed to detect whether systemd
-boot is installed
: %m
");
1663 return EXIT_SUCCESS;
1666 static int parse_loader_entry_target_arg(const char *arg1, char16_t **ret_target, size_t *ret_target_size) {
1668 if (streq(arg1, "@current
")) {
1669 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntrySelected
", NULL, (void *) ret_target, ret_target_size);
1671 return log_error_errno(r, "Failed to get EFI variable
'LoaderEntrySelected': %m
");
1672 } else if (streq(arg1, "@oneshot
")) {
1673 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntryOneShot
", NULL, (void *) ret_target, ret_target_size);
1675 return log_error_errno(r, "Failed to get EFI variable
'LoaderEntryOneShot': %m
");
1676 } else if (streq(arg1, "@
default")) {
1677 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntryDefault
", NULL, (void *) ret_target, ret_target_size);
1679 return log_error_errno(r, "Failed to get EFI variable
'LoaderEntryDefault': %m
");
1681 char16_t *encoded = NULL;
1682 encoded = utf8_to_utf16(arg1, strlen(arg1));
1685 *ret_target = encoded;
1686 *ret_target_size = char16_strlen(encoded) * 2 + 2;
1691 static int verb_set_default(int argc, char *argv[], void *userdata) {
1696 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1697 "Not booted with UEFI
.");
1699 if (access("/sys
/firmware
/efi
/efivars
/LoaderInfo
-4a67b082
-0a4c
-41cf
-b6c7
-440b29bb8c4f
", F_OK) < 0) {
1700 if (errno == ENOENT) {
1701 log_error_errno(errno, "Not booted with a supported boot loader
.");
1705 return log_error_errno(errno, "Failed to detect whether boot loader supports
'%s' operation
: %m
", argv[0]);
1708 if (detect_container() > 0)
1709 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1710 "'%s' operation
not supported in a container
.",
1713 if (!arg_touch_variables)
1714 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1715 "'%s' operation cannot be combined with
--touch
-variables
=no
.",
1718 name = streq(argv[0], "set
-default") ? "LoaderEntryDefault
" : "LoaderEntryOneShot
";
1720 if (isempty(argv[1])) {
1721 r = efi_set_variable(EFI_VENDOR_LOADER, name, NULL, 0);
1722 if (r < 0 && r != -ENOENT)
1723 return log_error_errno(r, "Failed to remove EFI variable
'%s': %m
", name);
1725 _cleanup_free_ char16_t *target = NULL;
1726 size_t target_size = 0;
1728 r = parse_loader_entry_target_arg(argv[1], &target, &target_size);
1731 r = efi_set_variable(EFI_VENDOR_LOADER, name, target, target_size);
1733 return log_error_errno(r, "Failed to update EFI variable
'%s': %m
", name);
1739 static int verb_random_seed(int argc, char *argv[], void *userdata) {
1742 r = find_esp_and_warn(arg_esp_path, false, &arg_esp_path, NULL, NULL, NULL, NULL);
1744 /* find_esp_and_warn() doesn't warn about ENOKEY, so let's do that on our own */
1746 return log_error_errno(r, "Unable to find ESP
.");
1748 log_notice("No ESP found
, not initializing random seed
.");
1754 r = install_random_seed(arg_esp_path);
1758 (void) sync_everything();
1762 static int verb_systemd_efi_options(int argc, char *argv[], void *userdata) {
1766 _cleanup_free_ char *line = NULL;
1768 r = systemd_efi_options_variable(&line);
1770 return log_error_errno(r, "Failed to query SystemdOptions EFI variable
: %m
");
1775 r = efi_set_variable_string(EFI_VENDOR_SYSTEMD, "SystemdOptions
", argv[1]);
1777 return log_error_errno(r, "Failed to set SystemdOptions EFI variable
: %m
");
1783 static int verb_reboot_to_firmware(int argc, char *argv[], void *userdata) {
1787 r = efi_get_reboot_to_firmware();
1790 return EXIT_SUCCESS; /* success */
1794 return 1; /* recognizable error #1 */
1796 if (r == -EOPNOTSUPP) {
1797 puts("not supported
");
1798 return 2; /* recognizable error #2 */
1801 log_error_errno(r, "Failed to query reboot
-to
-firmware state
: %m
");
1802 return 3; /* other kind of error */
1804 r = parse_boolean(argv[1]);
1806 return log_error_errno(r, "Failed to parse argument
: %s
", argv[1]);
1808 r = efi_set_reboot_to_firmware(r);
1810 return log_error_errno(r, "Failed to set reboot
-to
-firmware option
: %m
");
1816 static int bootctl_main(int argc, char *argv[]) {
1817 static const Verb verbs[] = {
1818 { "help
", VERB_ANY, VERB_ANY, 0, help },
1819 { "status
", VERB_ANY, 1, VERB_DEFAULT, verb_status },
1820 { "install
", VERB_ANY, 1, 0, verb_install },
1821 { "update
", VERB_ANY, 1, 0, verb_install },
1822 { "remove
", VERB_ANY, 1, 0, verb_remove },
1823 { "is
-installed
", VERB_ANY, 1, 0, verb_is_installed },
1824 { "list
", VERB_ANY, 1, 0, verb_list },
1825 { "set
-default", 2, 2, 0, verb_set_default },
1826 { "set
-oneshot
", 2, 2, 0, verb_set_default },
1827 { "random
-seed
", VERB_ANY, 1, 0, verb_random_seed },
1828 { "systemd
-efi
-options
", VERB_ANY, 2, 0, verb_systemd_efi_options },
1829 { "reboot
-to
-firmware
", VERB_ANY, 2, 0, verb_reboot_to_firmware },
1833 return dispatch_verb(argc, argv, verbs, NULL);
1836 static int run(int argc, char *argv[]) {
1839 log_parse_environment();
1842 /* If we run in a container, automatically turn off EFI file system access */
1843 if (detect_container() > 0)
1844 arg_touch_variables = false;
1846 r = parse_argv(argc, argv);
1850 return bootctl_main(argc, argv);
1853 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);