1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <linux/magic.h>
17 #include <sys/statfs.h>
22 #include "alloc-util.h"
23 #include "blkid-util.h"
26 #include "dirent-util.h"
31 #include "locale-util.h"
32 #include "main-func.h"
34 #include "parse-util.h"
35 #include "pretty-print.h"
37 #include "stat-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_path
= NULL
;
49 static bool arg_print_path
= false;
50 static bool arg_touch_variables
= true;
51 static PagerFlags arg_pager_flags
= 0;
53 STATIC_DESTRUCTOR_REGISTER(arg_path
, freep
);
55 static int acquire_esp(
56 bool unprivileged_mode
,
60 sd_id128_t
*ret_uuid
) {
65 /* Find the ESP, and log about errors. Note that find_esp_and_warn() will log in all error cases on its own,
66 * except for ENOKEY (which is good, we want to show our own message in that case, suggesting use of --path=)
67 * and EACCESS (only when we request unprivileged mode; in this case we simply eat up the error here, so that
68 * --list and --status work too, without noise about this). */
70 r
= find_esp_and_warn(arg_path
, unprivileged_mode
, &np
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
72 return log_error_errno(r
,
73 "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n"
74 "Alternatively, use --path= to specify path to mount point.");
78 free_and_replace(arg_path
, np
);
80 log_debug("Using EFI System Partition at %s.", arg_path
);
85 /* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */
86 static int get_file_version(int fd
, char **v
) {
96 if (fstat(fd
, &st
) < 0)
97 return log_error_errno(errno
, "Failed to stat EFI binary: %m");
99 r
= stat_verify_regular(&st
);
101 return log_error_errno(errno
, "EFI binary is not a regular file: %m");
103 if (st
.st_size
< 27) {
108 buf
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
109 if (buf
== MAP_FAILED
)
110 return log_error_errno(errno
, "Failed to memory map EFI binary: %m");
112 s
= memmem(buf
, st
.st_size
- 8, "#### LoaderInfo: ", 17);
117 e
= memmem(s
, st
.st_size
- (s
- buf
), " ####", 5);
118 if (!e
|| e
- s
< 3) {
119 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Malformed version string.");
123 x
= strndup(s
, e
- s
);
131 (void) munmap(buf
, st
.st_size
);
136 static int enumerate_binaries(const char *esp_path
, const char *path
, const char *prefix
) {
138 _cleanup_closedir_
DIR *d
= NULL
;
142 p
= strjoina(esp_path
, "/", path
);
148 return log_error_errno(errno
, "Failed to read \"%s\": %m", p
);
151 FOREACH_DIRENT(de
, d
, break) {
152 _cleanup_close_
int fd
= -1;
153 _cleanup_free_
char *v
= NULL
;
155 if (!endswith_no_case(de
->d_name
, ".efi"))
158 if (prefix
&& !startswith_no_case(de
->d_name
, prefix
))
161 fd
= openat(dirfd(d
), de
->d_name
, O_RDONLY
|O_CLOEXEC
);
163 return log_error_errno(errno
, "Failed to open \"%s/%s\" for reading: %m", p
, de
->d_name
);
165 r
= get_file_version(fd
, &v
);
169 printf(" File: %s/%s/%s (%s%s%s)\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), path
, de
->d_name
, ansi_highlight(), v
, ansi_normal());
171 printf(" File: %s/%s/%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), path
, de
->d_name
);
178 static int status_binaries(const char *esp_path
, sd_id128_t partition
) {
181 printf("Available Boot Loaders on ESP:\n");
184 printf(" ESP: Cannot find or access mount point of ESP.\n\n");
188 printf(" ESP: %s", esp_path
);
189 if (!sd_id128_is_null(partition
))
190 printf(" (/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x)", SD_ID128_FORMAT_VAL(partition
));
193 r
= enumerate_binaries(esp_path
, "EFI/systemd", NULL
);
195 log_info("systemd-boot not installed in ESP.");
199 r
= enumerate_binaries(esp_path
, "EFI/BOOT", "boot");
201 log_info("No default/fallback boot loader installed in ESP.");
210 static int print_efi_option(uint16_t id
, bool in_order
) {
211 _cleanup_free_
char *title
= NULL
;
212 _cleanup_free_
char *path
= NULL
;
213 sd_id128_t partition
;
217 r
= efi_get_boot_option(id
, &title
, &partition
, &path
, &active
);
221 /* print only configured entries with partition information */
222 if (!path
|| sd_id128_is_null(partition
))
225 efi_tilt_backslashes(path
);
227 printf(" Title: %s%s%s\n", ansi_highlight(), strna(title
), ansi_normal());
228 printf(" ID: 0x%04X\n", id
);
229 printf(" Status: %sactive%s\n", active
? "" : "in", in_order
? ", boot-order" : "");
230 printf(" Partition: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n", SD_ID128_FORMAT_VAL(partition
));
231 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), path
);
237 static int status_variables(void) {
238 _cleanup_free_
uint16_t *options
= NULL
, *order
= NULL
;
239 int n_options
, n_order
, i
;
241 n_options
= efi_get_boot_options(&options
);
242 if (n_options
== -ENOENT
)
243 return log_error_errno(n_options
,
244 "Failed to access EFI variables, efivarfs"
245 " needs to be available at /sys/firmware/efi/efivars/.");
247 return log_error_errno(n_options
, "Failed to read EFI boot entries: %m");
249 n_order
= efi_get_boot_order(&order
);
250 if (n_order
== -ENOENT
)
252 else if (n_order
< 0)
253 return log_error_errno(n_order
, "Failed to read EFI boot order: %m");
255 /* print entries in BootOrder first */
256 printf("Boot Loaders Listed in EFI Variables:\n");
257 for (i
= 0; i
< n_order
; i
++)
258 print_efi_option(order
[i
], true);
260 /* print remaining entries */
261 for (i
= 0; i
< n_options
; i
++) {
264 for (j
= 0; j
< n_order
; j
++)
265 if (options
[i
] == order
[j
])
268 print_efi_option(options
[i
], false);
277 static int boot_entry_show(const BootEntry
*e
, bool show_as_default
) {
280 printf(" title: %s%s%s%s%s%s\n",
284 ansi_highlight_green(),
285 show_as_default
? " (default)" : "",
289 printf(" id: %s\n", e
->id
);
291 printf(" version: %s\n", e
->version
);
293 printf(" machine-id: %s\n", e
->machine_id
);
295 printf(" architecture: %s\n", e
->architecture
);
297 printf(" linux: %s\n", e
->kernel
);
298 if (!strv_isempty(e
->initrd
)) {
299 _cleanup_free_
char *t
;
301 t
= strv_join(e
->initrd
, " ");
305 printf(" initrd: %s\n", t
);
307 if (!strv_isempty(e
->options
)) {
308 _cleanup_free_
char *t
;
310 t
= strv_join(e
->options
, " ");
314 printf(" options: %s\n", t
);
317 printf(" devicetree: %s\n", e
->device_tree
);
322 static int status_entries(const char *esp_path
, sd_id128_t partition
) {
323 _cleanup_(boot_config_free
) BootConfig config
= {};
326 r
= boot_entries_load_config(esp_path
, NULL
, &config
);
330 if (config
.default_entry
< 0)
331 printf("%zu entries, no entry could be determined as default.\n", config
.n_entries
);
333 printf("Default Boot Loader Entry:\n");
335 boot_entry_show(config
.entries
+ config
.default_entry
, false);
341 static int compare_product(const char *a
, const char *b
) {
350 return x
< y
? -1 : x
> y
? 1 : 0;
352 return strncmp(a
, b
, x
);
355 static int compare_version(const char *a
, const char *b
) {
359 a
+= strcspn(a
, " ");
361 b
+= strcspn(b
, " ");
364 return strverscmp(a
, b
);
367 static int version_check(int fd_from
, const char *from
, int fd_to
, const char *to
) {
368 _cleanup_free_
char *a
= NULL
, *b
= NULL
;
371 assert(fd_from
>= 0);
376 r
= get_file_version(fd_from
, &a
);
380 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
381 "Source file \"%s\" does not carry version information!",
384 r
= get_file_version(fd_to
, &b
);
387 if (r
== 0 || compare_product(a
, b
) != 0)
388 return log_notice_errno(SYNTHETIC_ERRNO(EEXIST
),
389 "Skipping \"%s\", since it's owned by another boot loader.",
392 if (compare_version(a
, b
) < 0)
393 return log_warning_errno(SYNTHETIC_ERRNO(ESTALE
), "Skipping \"%s\", since a newer boot loader version exists already.", to
);
398 static int copy_file_with_version_check(const char *from
, const char *to
, bool force
) {
399 _cleanup_close_
int fd_from
= -1, fd_to
= -1;
400 _cleanup_free_
char *t
= NULL
;
403 fd_from
= open(from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
405 return log_error_errno(errno
, "Failed to open \"%s\" for reading: %m", from
);
408 fd_to
= open(to
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
410 if (errno
!= -ENOENT
)
411 return log_error_errno(errno
, "Failed to open \"%s\" for reading: %m", to
);
413 r
= version_check(fd_from
, from
, fd_to
, to
);
417 if (lseek(fd_from
, 0, SEEK_SET
) == (off_t
) -1)
418 return log_error_errno(errno
, "Failed to seek in \"%s\": %m", from
);
420 fd_to
= safe_close(fd_to
);
424 r
= tempfn_random(to
, NULL
, &t
);
428 RUN_WITH_UMASK(0000) {
429 fd_to
= open(t
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_EXCL
|O_NOFOLLOW
, 0644);
431 return log_error_errno(errno
, "Failed to open \"%s\" for writing: %m", t
);
434 r
= copy_bytes(fd_from
, fd_to
, (uint64_t) -1, COPY_REFLINK
);
437 return log_error_errno(r
, "Failed to copy data from \"%s\" to \"%s\": %m", from
, t
);
440 (void) copy_times(fd_from
, fd_to
);
442 if (fsync(fd_to
) < 0) {
443 (void) unlink_noerrno(t
);
444 return log_error_errno(errno
, "Failed to copy data from \"%s\" to \"%s\": %m", from
, t
);
447 (void) fsync_directory_of_file(fd_to
);
449 if (renameat(AT_FDCWD
, t
, AT_FDCWD
, to
) < 0) {
450 (void) unlink_noerrno(t
);
451 return log_error_errno(errno
, "Failed to rename \"%s\" to \"%s\": %m", t
, to
);
454 log_info("Copied \"%s\" to \"%s\".", from
, to
);
459 static int mkdir_one(const char *prefix
, const char *suffix
) {
462 p
= strjoina(prefix
, "/", suffix
);
463 if (mkdir(p
, 0700) < 0) {
465 return log_error_errno(errno
, "Failed to create \"%s\": %m", p
);
467 log_info("Created \"%s\".", p
);
472 static const char *efi_subdirs
[] = {
481 static int create_dirs(const char *esp_path
) {
485 STRV_FOREACH(i
, efi_subdirs
) {
486 r
= mkdir_one(esp_path
, *i
);
494 static int copy_one_file(const char *esp_path
, const char *name
, bool force
) {
498 p
= strjoina(BOOTLIBDIR
"/", name
);
499 q
= strjoina(esp_path
, "/EFI/systemd/", name
);
500 r
= copy_file_with_version_check(p
, q
, force
);
502 if (startswith(name
, "systemd-boot")) {
506 /* Create the EFI default boot loader name (specified for removable devices) */
507 v
= strjoina(esp_path
, "/EFI/BOOT/BOOT",
508 name
+ STRLEN("systemd-boot"));
509 ascii_strupper(strrchr(v
, '/') + 1);
511 k
= copy_file_with_version_check(p
, v
, force
);
519 static int install_binaries(const char *esp_path
, bool force
) {
521 _cleanup_closedir_
DIR *d
= NULL
;
525 /* Don't create any of these directories when we are
526 * just updating. When we update we'll drop-in our
527 * files (unless there are newer ones already), but we
528 * won't create the directories for them in the first
530 r
= create_dirs(esp_path
);
535 d
= opendir(BOOTLIBDIR
);
537 return log_error_errno(errno
, "Failed to open \""BOOTLIBDIR
"\": %m
");
539 FOREACH_DIRENT(de, d, break) {
542 if (!endswith_no_case(de->d_name, ".efi
"))
545 k = copy_one_file(esp_path, de->d_name, force);
553 static bool same_entry(uint16_t id, const sd_id128_t uuid, const char *path) {
554 _cleanup_free_ char *opath = NULL;
558 r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
561 if (!sd_id128_equal(uuid, ouuid))
563 if (!streq_ptr(path, opath))
569 static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
570 _cleanup_free_ uint16_t *options = NULL;
573 n = efi_get_boot_options(&options);
577 /* find already existing systemd-boot entry */
578 for (i = 0; i < n; i++)
579 if (same_entry(options[i], uuid, path)) {
584 /* find free slot in the sorted BootXXXX variable list */
585 for (i = 0; i < n; i++)
586 if (i != options[i]) {
591 /* use the next one */
598 static int insert_into_order(uint16_t slot, bool first) {
599 _cleanup_free_ uint16_t *order = NULL;
603 n = efi_get_boot_order(&order);
605 /* no entry, add us */
606 return efi_set_boot_order(&slot, 1);
608 /* are we the first and only one? */
609 if (n == 1 && order[0] == slot)
612 /* are we already in the boot order? */
613 for (i = 0; i < n; i++) {
614 if (order[i] != slot)
617 /* we do not require to be the first one, all is fine */
621 /* move us to the first slot */
622 memmove(order + 1, order, i * sizeof(uint16_t));
624 return efi_set_boot_order(order, n);
628 t = realloc(order, (n + 1) * sizeof(uint16_t));
633 /* add us to the top or end of the list */
635 memmove(order + 1, order, n * sizeof(uint16_t));
640 return efi_set_boot_order(order, n + 1);
643 static int remove_from_order(uint16_t slot) {
644 _cleanup_free_ uint16_t *order = NULL;
647 n = efi_get_boot_order(&order);
651 for (i = 0; i < n; i++) {
652 if (order[i] != slot)
656 memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
657 return efi_set_boot_order(order, n - 1);
663 static int install_variables(const char *esp_path,
664 uint32_t part, uint64_t pstart, uint64_t psize,
665 sd_id128_t uuid, const char *path,
671 if (!is_efi_boot()) {
672 log_warning("Not booted with EFI
, skipping EFI variable setup
.");
676 p = strjoina(esp_path, path);
677 if (access(p, F_OK) < 0) {
681 return log_error_errno(errno, "Cannot access
\"%s
\": %m
", p);
684 r = find_slot(uuid, path, &slot);
686 return log_error_errno(r,
688 "Failed to access EFI variables
. Is the
\"efivarfs
\" filesystem mounted
?" :
689 "Failed to determine current boot order
: %m
");
691 if (first || r == 0) {
692 r = efi_add_boot_option(slot, "Linux Boot Manager
",
696 return log_error_errno(r, "Failed to create EFI Boot variable entry
: %m
");
698 log_info("Created EFI boot entry
\"Linux Boot Manager
\".");
701 return insert_into_order(slot, first);
704 static int remove_boot_efi(const char *esp_path) {
706 _cleanup_closedir_ DIR *d = NULL;
710 p = strjoina(esp_path, "/EFI
/BOOT
");
716 return log_error_errno(errno, "Failed to open directory
\"%s
\": %m
", p);
719 FOREACH_DIRENT(de, d, break) {
720 _cleanup_close_ int fd = -1;
721 _cleanup_free_ char *v = NULL;
723 if (!endswith_no_case(de->d_name, ".efi
"))
726 if (!startswith_no_case(de->d_name, "boot
"))
729 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
731 return log_error_errno(errno, "Failed to open
\"%s
/%s
\" for reading
: %m
", p, de->d_name);
733 r = get_file_version(fd, &v);
736 if (r > 0 && startswith(v, "systemd
-boot
")) {
737 r = unlinkat(dirfd(d), de->d_name, 0);
739 return log_error_errno(errno, "Failed to remove
\"%s
/%s
\": %m
", p, de->d_name);
741 log_info("Removed
\"%s
/%s
\".", p, de->d_name);
750 static int rmdir_one(const char *prefix, const char *suffix) {
753 p = strjoina(prefix, "/", suffix);
755 if (!IN_SET(errno, ENOENT, ENOTEMPTY))
756 return log_error_errno(errno, "Failed to remove
\"%s
\": %m
", p);
758 log_info("Removed
\"%s
\".", p);
763 static int remove_binaries(const char *esp_path) {
768 p = strjoina(esp_path, "/EFI
/systemd
");
769 r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
771 q = remove_boot_efi(esp_path);
775 for (i = ELEMENTSOF(efi_subdirs)-1; i > 0; i--) {
776 q = rmdir_one(esp_path, efi_subdirs[i-1]);
784 static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
791 r = find_slot(uuid, path, &slot);
795 r = efi_remove_boot_option(slot);
800 return remove_from_order(slot);
805 static int install_loader_config(const char *esp_path) {
807 char machine_string[SD_ID128_STRING_MAX];
808 _cleanup_(unlink_and_freep) char *t = NULL;
809 _cleanup_fclose_ FILE *f = NULL;
810 sd_id128_t machine_id;
814 r = sd_id128_get_machine(&machine_id);
816 return log_error_errno(r, "Failed to get machine id
: %m
");
818 p = strjoina(esp_path, "/loader
/loader
.conf
");
820 if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
823 fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
825 return log_error_errno(fd, "Failed to open
\"%s
\" for writing
: %m
", p);
833 fprintf(f, "#timeout 3\n"
834 "#console-mode keep\n"
835 "default %s-*\n", sd_id128_to_string(machine_id, machine_string));
837 r = fflush_sync_and_check(f);
839 return log_error_errno(r, "Failed to write \"%s\": %m", p);
841 r = link_tmpfile(fd, t, p);
843 return 0; /* Silently skip creation if the file exists now (recheck) */
845 return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
852 static int help(int argc, char *argv[], void *userdata) {
853 _cleanup_free_ char *link = NULL;
856 r = terminal_urlify_man("bootctl", "1", &link);
860 printf("%s [COMMAND] [OPTIONS...]\n\n"
861 "Install, update or remove the systemd-boot EFI boot manager.\n\n"
862 " -h --help Show this help\n"
863 " --version Print version\n"
864 " --path=PATH Path to the EFI System Partition (ESP)\n"
865 " -p --print-path Print path to the EFI partition\n"
866 " --no-variables Don't touch EFI variables\n"
867 " --no-pager Do not pipe output into a pager\n"
868 "\nBoot Loader Commands:\n"
869 " status Show status of installed systemd-boot and EFI variables\n"
870 " install Install systemd-boot to the ESP and EFI variables\n"
871 " update Update systemd-boot in the ESP and EFI variables\n"
872 " remove Remove systemd-boot from the ESP and EFI variables\n"
873 "\nBoot Loader Entries Commands:\n"
874 " list List boot loader entries\n"
875 " set-default ID Set default boot loader entry\n"
876 " set-oneshot ID Set default boot loader entry, for next boot only\n"
877 "\nSee the %s for details.\n"
878 , program_invocation_short_name
884 static int parse_argv(int argc, char *argv[]) {
892 static const struct option options[] = {
893 { "help", no_argument, NULL, 'h' },
894 { "version", no_argument, NULL, ARG_VERSION },
895 { "path", required_argument, NULL, ARG_PATH },
896 { "print-path", no_argument, NULL, 'p' },
897 { "no-variables", no_argument, NULL, ARG_NO_VARIABLES },
898 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
907 while ((c = getopt_long(argc, argv, "hp", options, NULL)) >= 0)
918 r = free_and_strdup(&arg_path, optarg);
924 arg_print_path = true;
927 case ARG_NO_VARIABLES:
928 arg_touch_variables = false;
932 arg_pager_flags |= PAGER_DISABLE;
939 assert_not_reached("Unknown option");
945 static void read_loader_efi_var(const char *name, char **var) {
948 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var);
949 if (r < 0 && r != -ENOENT)
950 log_warning_errno(r, "Failed to read EFI variable %s: %m", name);
953 static int verb_status(int argc, char *argv[], void *userdata) {
955 sd_id128_t uuid = SD_ID128_NULL;
958 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &uuid);
960 if (arg_print_path) {
961 if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only
962 * error the
find_esp_and_warn() won
't log on its own) */
963 return log_error_errno(r, "Failed to determine ESP: %m");
971 r = 0; /* If we couldn't determine the path
, then don
't consider that a problem from here on, just show what we
974 (void) pager_open(arg_pager_flags);
977 static const struct {
981 { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" },
982 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" },
983 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" },
984 { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" },
985 { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" },
988 _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL;
989 sd_id128_t loader_part_uuid = SD_ID128_NULL;
990 uint64_t loader_features = 0;
993 read_loader_efi_var("LoaderFirmwareType", &fw_type);
994 read_loader_efi_var("LoaderFirmwareInfo", &fw_info);
995 read_loader_efi_var("LoaderInfo", &loader);
996 read_loader_efi_var("StubInfo", &stub);
997 read_loader_efi_var("LoaderImageIdentifier", &loader_path);
998 (void) efi_loader_get_features(&loader_features);
1001 efi_tilt_backslashes(loader_path);
1003 k = efi_loader_get_device_part_uuid(&loader_part_uuid);
1004 if (k < 0 && k != -ENOENT)
1005 r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m");
1007 printf("System:\n");
1008 printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal());
1009 printf(" Secure Boot: %sd\n", enable_disable(is_efi_secure_boot()));
1010 printf(" Setup Mode: %s\n", is_efi_secure_boot_setup_mode() ? "setup" : "user");
1013 printf("Current Boot Loader:\n");
1014 printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal());
1016 for (i = 0; i < ELEMENTSOF(flags); i++) {
1019 printf(" Features: ");
1023 if (FLAGS_SET(loader_features, flags[i].flag))
1024 printf("%s%s%s %s\n", ansi_highlight_green(), special_glyph(SPECIAL_GLYPH_CHECK_MARK), ansi_normal(), flags[i].name);
1026 printf("%s%s%s %s\n", ansi_highlight_red(), special_glyph(SPECIAL_GLYPH_CROSS_MARK), ansi_normal(), flags[i].name);
1030 printf(" Stub: %s\n", stub);
1031 if (!sd_id128_is_null(loader_part_uuid))
1032 printf(" ESP: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
1033 SD_ID128_FORMAT_VAL(loader_part_uuid));
1035 printf(" ESP: n/a\n");
1036 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), strna(loader_path));
1039 printf("System:\n Not booted with EFI\n\n");
1042 k = status_binaries(arg_path, uuid);
1047 if (is_efi_boot()) {
1048 k = status_variables();
1054 k = status_entries(arg_path, uuid);
1062 static int verb_list(int argc, char *argv[], void *userdata) {
1063 _cleanup_(boot_config_free) BootConfig config = {};
1064 _cleanup_free_ char **found_by_loader = NULL;
1065 sd_id128_t uuid = SD_ID128_NULL;
1068 /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two things: turn
1069 * off logging about access errors and turn off potentially privileged device probing. Here we're interested in
1070 * the latter but
not the former
, hence request the mode
, and log about EACCES
. */
1072 r
= acquire_esp(geteuid() != 0, NULL
, NULL
, NULL
, &uuid
);
1073 if (r
== -EACCES
) /* We really need the ESP path for this call, hence also log about access errors */
1074 return log_error_errno(r
, "Failed to determine ESP: %m");
1078 r
= boot_entries_load_config(arg_path
, NULL
, &config
);
1082 r
= efi_loader_get_entries(&found_by_loader
);
1083 if (r
< 0 && !IN_SET(r
, -ENOENT
, -EOPNOTSUPP
))
1084 log_debug_errno(r
, "Failed to acquire boot loader discovered entries: %m");
1086 if (config
.n_entries
== 0)
1087 log_info("No boot loader entries found.");
1091 (void) pager_open(arg_pager_flags
);
1093 printf("Boot Loader Entries:\n");
1095 for (n
= 0; n
< config
.n_entries
; n
++) {
1096 r
= boot_entry_show(config
.entries
+ n
, n
== (size_t) config
.default_entry
);
1102 strv_remove(found_by_loader
, config
.entries
[n
].id
);
1106 if (!strv_isempty(found_by_loader
)) {
1109 printf("Automatic/Other Entries Found by Boot Loader:\n\n");
1111 STRV_FOREACH(i
, found_by_loader
)
1118 static int sync_esp(void) {
1119 _cleanup_close_
int fd
= -1;
1124 fd
= open(arg_path
, O_CLOEXEC
|O_DIRECTORY
|O_RDONLY
);
1126 return log_error_errno(errno
, "Couldn't open ESP '%s' for synchronization: %m", arg_path
);
1129 return log_error_errno(errno
, "Failed to synchronize the ESP '%s': %m", arg_path
);
1134 static int verb_install(int argc
, char *argv
[], void *userdata
) {
1136 sd_id128_t uuid
= SD_ID128_NULL
;
1137 uint64_t pstart
= 0, psize
= 0;
1142 r
= acquire_esp(false, &part
, &pstart
, &psize
, &uuid
);
1146 install
= streq(argv
[0], "install");
1148 RUN_WITH_UMASK(0002) {
1149 r
= install_binaries(arg_path
, install
);
1154 r
= install_loader_config(arg_path
);
1162 if (arg_touch_variables
)
1163 r
= install_variables(arg_path
,
1164 part
, pstart
, psize
, uuid
,
1165 "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME
".efi",
1171 static int verb_remove(int argc
, char *argv
[], void *userdata
) {
1172 sd_id128_t uuid
= SD_ID128_NULL
;
1175 r
= acquire_esp(false, NULL
, NULL
, NULL
, &uuid
);
1179 r
= remove_binaries(arg_path
);
1183 if (arg_touch_variables
) {
1186 q
= remove_variables(uuid
, "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME
".efi", true);
1187 if (q
< 0 && r
== 0)
1194 static int verb_set_default(int argc
, char *argv
[], void *userdata
) {
1199 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1200 "Not booted with UEFI.");
1202 if (access("/sys/firmware/efi/efivars/LoaderInfo-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK
) < 0) {
1203 if (errno
== ENOENT
) {
1204 log_error_errno(errno
, "Not booted with a supported boot loader.");
1208 return log_error_errno(errno
, "Failed to detect whether boot loader supports '%s' operation: %m", argv
[0]);
1211 if (detect_container() > 0)
1212 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1213 "'%s' operation not supported in a container.",
1216 if (!arg_touch_variables
)
1217 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1218 "'%s' operation cannot be combined with --touch-variables=no.",
1221 name
= streq(argv
[0], "set-default") ? "LoaderEntryDefault" : "LoaderEntryOneShot";
1223 if (isempty(argv
[1])) {
1224 r
= efi_set_variable(EFI_VENDOR_LOADER
, name
, NULL
, 0);
1225 if (r
< 0 && r
!= -ENOENT
)
1226 return log_error_errno(r
, "Failed to remove EFI variale: %m");
1228 _cleanup_free_ char16_t
*encoded
= NULL
;
1230 encoded
= utf8_to_utf16(argv
[1], strlen(argv
[1]));
1234 r
= efi_set_variable(EFI_VENDOR_LOADER
, name
, encoded
, char16_strlen(encoded
) * 2 + 2);
1236 return log_error_errno(r
, "Failed to update EFI variable: %m");
1242 static int bootctl_main(int argc
, char *argv
[]) {
1244 static const Verb verbs
[] = {
1245 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1246 { "status", VERB_ANY
, 1, VERB_DEFAULT
, verb_status
},
1247 { "install", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_install
},
1248 { "update", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_install
},
1249 { "remove", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_remove
},
1250 { "list", VERB_ANY
, 1, 0, verb_list
},
1251 { "set-default", 2, 2, VERB_MUST_BE_ROOT
, verb_set_default
},
1252 { "set-oneshot", 2, 2, VERB_MUST_BE_ROOT
, verb_set_default
},
1256 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1259 static int run(int argc
, char *argv
[]) {
1262 log_parse_environment();
1265 /* If we run in a container, automatically turn of EFI file system access */
1266 if (detect_container() > 0)
1267 arg_touch_variables
= false;
1269 r
= parse_argv(argc
, argv
);
1273 return bootctl_main(argc
, argv
);
1276 DEFINE_MAIN_FUNCTION(run
);