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 "umask-util.h"
47 static char *arg_path
= NULL
;
48 static bool arg_print_path
= false;
49 static bool arg_touch_variables
= true;
50 static PagerFlags arg_pager_flags
= 0;
52 STATIC_DESTRUCTOR_REGISTER(arg_path
, freep
);
54 static int acquire_esp(
55 bool unprivileged_mode
,
59 sd_id128_t
*ret_uuid
) {
64 /* Find the ESP, and log about errors. Note that find_esp_and_warn() will log in all error cases on its own,
65 * except for ENOKEY (which is good, we want to show our own message in that case, suggesting use of --path=)
66 * and EACCESS (only when we request unprivileged mode; in this case we simply eat up the error here, so that
67 * --list and --status work too, without noise about this). */
69 r
= find_esp_and_warn(arg_path
, unprivileged_mode
, &np
, ret_part
, ret_pstart
, ret_psize
, ret_uuid
);
71 return log_error_errno(r
,
72 "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n"
73 "Alternatively, use --path= to specify path to mount point.");
77 free_and_replace(arg_path
, np
);
79 log_debug("Using EFI System Partition at %s.", arg_path
);
84 /* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */
85 static int get_file_version(int fd
, char **v
) {
95 if (fstat(fd
, &st
) < 0)
96 return log_error_errno(errno
, "Failed to stat EFI binary: %m");
98 if (st
.st_size
< 27) {
103 buf
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
104 if (buf
== MAP_FAILED
)
105 return log_error_errno(errno
, "Failed to memory map EFI binary: %m");
107 s
= memmem(buf
, st
.st_size
- 8, "#### LoaderInfo: ", 17);
112 e
= memmem(s
, st
.st_size
- (s
- buf
), " ####", 5);
113 if (!e
|| e
- s
< 3) {
114 log_error("Malformed version string.");
119 x
= strndup(s
, e
- s
);
127 (void) munmap(buf
, st
.st_size
);
132 static int enumerate_binaries(const char *esp_path
, const char *path
, const char *prefix
) {
134 _cleanup_closedir_
DIR *d
= NULL
;
138 p
= strjoina(esp_path
, "/", path
);
144 return log_error_errno(errno
, "Failed to read \"%s\": %m", p
);
147 FOREACH_DIRENT(de
, d
, break) {
148 _cleanup_close_
int fd
= -1;
149 _cleanup_free_
char *v
= NULL
;
151 if (!endswith_no_case(de
->d_name
, ".efi"))
154 if (prefix
&& !startswith_no_case(de
->d_name
, prefix
))
157 fd
= openat(dirfd(d
), de
->d_name
, O_RDONLY
|O_CLOEXEC
);
159 return log_error_errno(errno
, "Failed to open \"%s/%s\" for reading: %m", p
, de
->d_name
);
161 r
= get_file_version(fd
, &v
);
165 printf(" File: %s/%s/%s (%s%s%s)\n", special_glyph(TREE_RIGHT
), path
, de
->d_name
, ansi_highlight(), v
, ansi_normal());
167 printf(" File: %s/%s/%s\n", special_glyph(TREE_RIGHT
), path
, de
->d_name
);
174 static int status_binaries(const char *esp_path
, sd_id128_t partition
) {
177 printf("Available Boot Loaders on ESP:\n");
180 printf(" ESP: Cannot find or access mount point of ESP.\n\n");
184 printf(" ESP: %s", esp_path
);
185 if (!sd_id128_is_null(partition
))
186 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
));
189 r
= enumerate_binaries(esp_path
, "EFI/systemd", NULL
);
191 log_info("systemd-boot not installed in ESP.");
195 r
= enumerate_binaries(esp_path
, "EFI/BOOT", "boot");
197 log_info("No default/fallback boot loader installed in ESP.");
206 static int print_efi_option(uint16_t id
, bool in_order
) {
207 _cleanup_free_
char *title
= NULL
;
208 _cleanup_free_
char *path
= NULL
;
209 sd_id128_t partition
;
213 r
= efi_get_boot_option(id
, &title
, &partition
, &path
, &active
);
217 /* print only configured entries with partition information */
218 if (!path
|| sd_id128_is_null(partition
))
221 efi_tilt_backslashes(path
);
223 printf(" Title: %s%s%s\n", ansi_highlight(), strna(title
), ansi_normal());
224 printf(" ID: 0x%04X\n", id
);
225 printf(" Status: %sactive%s\n", active
? "" : "in", in_order
? ", boot-order" : "");
226 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
));
227 printf(" File: %s%s\n", special_glyph(TREE_RIGHT
), path
);
233 static int status_variables(void) {
234 _cleanup_free_
uint16_t *options
= NULL
, *order
= NULL
;
235 int n_options
, n_order
, i
;
237 n_options
= efi_get_boot_options(&options
);
238 if (n_options
== -ENOENT
)
239 return log_error_errno(n_options
,
240 "Failed to access EFI variables, efivarfs"
241 " needs to be available at /sys/firmware/efi/efivars/.");
243 return log_error_errno(n_options
, "Failed to read EFI boot entries: %m");
245 n_order
= efi_get_boot_order(&order
);
246 if (n_order
== -ENOENT
)
248 else if (n_order
< 0)
249 return log_error_errno(n_order
, "Failed to read EFI boot order: %m");
251 /* print entries in BootOrder first */
252 printf("Boot Loaders Listed in EFI Variables:\n");
253 for (i
= 0; i
< n_order
; i
++)
254 print_efi_option(order
[i
], true);
256 /* print remaining entries */
257 for (i
= 0; i
< n_options
; i
++) {
260 for (j
= 0; j
< n_order
; j
++)
261 if (options
[i
] == order
[j
])
264 print_efi_option(options
[i
], false);
273 static int boot_entry_show(const BootEntry
*e
, bool show_as_default
) {
276 printf(" title: %s%s%s%s%s%s\n",
280 ansi_highlight_green(),
281 show_as_default
? " (default)" : "",
285 printf(" id: %s\n", e
->id
);
287 printf(" version: %s\n", e
->version
);
289 printf(" machine-id: %s\n", e
->machine_id
);
291 printf(" architecture: %s\n", e
->architecture
);
293 printf(" linux: %s\n", e
->kernel
);
294 if (!strv_isempty(e
->initrd
)) {
295 _cleanup_free_
char *t
;
297 t
= strv_join(e
->initrd
, " ");
301 printf(" initrd: %s\n", t
);
303 if (!strv_isempty(e
->options
)) {
304 _cleanup_free_
char *t
;
306 t
= strv_join(e
->options
, " ");
310 printf(" options: %s\n", t
);
313 printf(" devicetree: %s\n", e
->device_tree
);
318 static int status_entries(const char *esp_path
, sd_id128_t partition
) {
319 _cleanup_(boot_config_free
) BootConfig config
= {};
322 r
= boot_entries_load_config(esp_path
, &config
);
326 if (config
.default_entry
< 0)
327 printf("%zu entries, no entry could be determined as default.\n", config
.n_entries
);
329 printf("Default Boot Loader Entry:\n");
331 boot_entry_show(config
.entries
+ config
.default_entry
, false);
337 static int compare_product(const char *a
, const char *b
) {
346 return x
< y
? -1 : x
> y
? 1 : 0;
348 return strncmp(a
, b
, x
);
351 static int compare_version(const char *a
, const char *b
) {
355 a
+= strcspn(a
, " ");
357 b
+= strcspn(b
, " ");
360 return strverscmp(a
, b
);
363 static int version_check(int fd_from
, const char *from
, int fd_to
, const char *to
) {
364 _cleanup_free_
char *a
= NULL
, *b
= NULL
;
367 assert(fd_from
>= 0);
372 r
= get_file_version(fd_from
, &a
);
376 log_error("Source file \"%s\" does not carry version information!", from
);
380 r
= get_file_version(fd_to
, &b
);
383 if (r
== 0 || compare_product(a
, b
) != 0) {
384 log_notice("Skipping \"%s\", since it's owned by another boot loader.", to
);
388 if (compare_version(a
, b
) < 0) {
389 log_warning("Skipping \"%s\", since a newer boot loader version exists already.", to
);
396 static int copy_file_with_version_check(const char *from
, const char *to
, bool force
) {
397 _cleanup_close_
int fd_from
= -1, fd_to
= -1;
398 _cleanup_free_
char *t
= NULL
;
401 fd_from
= open(from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
403 return log_error_errno(errno
, "Failed to open \"%s\" for reading: %m", from
);
406 fd_to
= open(to
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
408 if (errno
!= -ENOENT
)
409 return log_error_errno(errno
, "Failed to open \"%s\" for reading: %m", to
);
411 r
= version_check(fd_from
, from
, fd_to
, to
);
415 if (lseek(fd_from
, 0, SEEK_SET
) == (off_t
) -1)
416 return log_error_errno(errno
, "Failed to seek in \"%s\": %m", from
);
418 fd_to
= safe_close(fd_to
);
422 r
= tempfn_random(to
, NULL
, &t
);
426 RUN_WITH_UMASK(0000) {
427 fd_to
= open(t
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_EXCL
|O_NOFOLLOW
, 0644);
429 return log_error_errno(errno
, "Failed to open \"%s\" for writing: %m", t
);
432 r
= copy_bytes(fd_from
, fd_to
, (uint64_t) -1, COPY_REFLINK
);
435 return log_error_errno(r
, "Failed to copy data from \"%s\" to \"%s\": %m", from
, t
);
438 (void) copy_times(fd_from
, fd_to
);
440 if (fsync(fd_to
) < 0) {
441 (void) unlink_noerrno(t
);
442 return log_error_errno(errno
, "Failed to copy data from \"%s\" to \"%s\": %m", from
, t
);
445 (void) fsync_directory_of_file(fd_to
);
447 if (renameat(AT_FDCWD
, t
, AT_FDCWD
, to
) < 0) {
448 (void) unlink_noerrno(t
);
449 return log_error_errno(errno
, "Failed to rename \"%s\" to \"%s\": %m", t
, to
);
452 log_info("Copied \"%s\" to \"%s\".", from
, to
);
457 static int mkdir_one(const char *prefix
, const char *suffix
) {
460 p
= strjoina(prefix
, "/", suffix
);
461 if (mkdir(p
, 0700) < 0) {
463 return log_error_errno(errno
, "Failed to create \"%s\": %m", p
);
465 log_info("Created \"%s\".", p
);
470 static const char *efi_subdirs
[] = {
479 static int create_dirs(const char *esp_path
) {
483 STRV_FOREACH(i
, efi_subdirs
) {
484 r
= mkdir_one(esp_path
, *i
);
492 static int copy_one_file(const char *esp_path
, const char *name
, bool force
) {
496 p
= strjoina(BOOTLIBDIR
"/", name
);
497 q
= strjoina(esp_path
, "/EFI/systemd/", name
);
498 r
= copy_file_with_version_check(p
, q
, force
);
500 if (startswith(name
, "systemd-boot")) {
504 /* Create the EFI default boot loader name (specified for removable devices) */
505 v
= strjoina(esp_path
, "/EFI/BOOT/BOOT",
506 name
+ STRLEN("systemd-boot"));
507 ascii_strupper(strrchr(v
, '/') + 1);
509 k
= copy_file_with_version_check(p
, v
, force
);
517 static int install_binaries(const char *esp_path
, bool force
) {
519 _cleanup_closedir_
DIR *d
= NULL
;
523 /* Don't create any of these directories when we are
524 * just updating. When we update we'll drop-in our
525 * files (unless there are newer ones already), but we
526 * won't create the directories for them in the first
528 r
= create_dirs(esp_path
);
533 d
= opendir(BOOTLIBDIR
);
535 return log_error_errno(errno
, "Failed to open \""BOOTLIBDIR
"\": %m
");
537 FOREACH_DIRENT(de, d, break) {
540 if (!endswith_no_case(de->d_name, ".efi
"))
543 k = copy_one_file(esp_path, de->d_name, force);
551 static bool same_entry(uint16_t id, const sd_id128_t uuid, const char *path) {
552 _cleanup_free_ char *opath = NULL;
556 r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
559 if (!sd_id128_equal(uuid, ouuid))
561 if (!streq_ptr(path, opath))
567 static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
568 _cleanup_free_ uint16_t *options = NULL;
571 n = efi_get_boot_options(&options);
575 /* find already existing systemd-boot entry */
576 for (i = 0; i < n; i++)
577 if (same_entry(options[i], uuid, path)) {
582 /* find free slot in the sorted BootXXXX variable list */
583 for (i = 0; i < n; i++)
584 if (i != options[i]) {
589 /* use the next one */
596 static int insert_into_order(uint16_t slot, bool first) {
597 _cleanup_free_ uint16_t *order = NULL;
601 n = efi_get_boot_order(&order);
603 /* no entry, add us */
604 return efi_set_boot_order(&slot, 1);
606 /* are we the first and only one? */
607 if (n == 1 && order[0] == slot)
610 /* are we already in the boot order? */
611 for (i = 0; i < n; i++) {
612 if (order[i] != slot)
615 /* we do not require to be the first one, all is fine */
619 /* move us to the first slot */
620 memmove(order + 1, order, i * sizeof(uint16_t));
622 return efi_set_boot_order(order, n);
626 t = realloc(order, (n + 1) * sizeof(uint16_t));
631 /* add us to the top or end of the list */
633 memmove(order + 1, order, n * sizeof(uint16_t));
638 return efi_set_boot_order(order, n + 1);
641 static int remove_from_order(uint16_t slot) {
642 _cleanup_free_ uint16_t *order = NULL;
645 n = efi_get_boot_order(&order);
649 for (i = 0; i < n; i++) {
650 if (order[i] != slot)
654 memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
655 return efi_set_boot_order(order, n - 1);
661 static int install_variables(const char *esp_path,
662 uint32_t part, uint64_t pstart, uint64_t psize,
663 sd_id128_t uuid, const char *path,
669 if (!is_efi_boot()) {
670 log_warning("Not booted with EFI
, skipping EFI variable setup
.");
674 p = strjoina(esp_path, path);
675 if (access(p, F_OK) < 0) {
679 return log_error_errno(errno, "Cannot access
\"%s
\": %m
", p);
682 r = find_slot(uuid, path, &slot);
684 return log_error_errno(r,
686 "Failed to access EFI variables
. Is the
\"efivarfs
\" filesystem mounted
?" :
687 "Failed to determine current boot order
: %m
");
689 if (first || r == 0) {
690 r = efi_add_boot_option(slot, "Linux Boot Manager
",
694 return log_error_errno(r, "Failed to create EFI Boot variable entry
: %m
");
696 log_info("Created EFI boot entry
\"Linux Boot Manager
\".");
699 return insert_into_order(slot, first);
702 static int remove_boot_efi(const char *esp_path) {
704 _cleanup_closedir_ DIR *d = NULL;
708 p = strjoina(esp_path, "/EFI
/BOOT
");
714 return log_error_errno(errno, "Failed to open directory
\"%s
\": %m
", p);
717 FOREACH_DIRENT(de, d, break) {
718 _cleanup_close_ int fd = -1;
719 _cleanup_free_ char *v = NULL;
721 if (!endswith_no_case(de->d_name, ".efi
"))
724 if (!startswith_no_case(de->d_name, "boot
"))
727 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
729 return log_error_errno(errno, "Failed to open
\"%s
/%s
\" for reading
: %m
", p, de->d_name);
731 r = get_file_version(fd, &v);
734 if (r > 0 && startswith(v, "systemd
-boot
")) {
735 r = unlinkat(dirfd(d), de->d_name, 0);
737 return log_error_errno(errno, "Failed to remove
\"%s
/%s
\": %m
", p, de->d_name);
739 log_info("Removed
\"%s
/%s
\".", p, de->d_name);
748 static int rmdir_one(const char *prefix, const char *suffix) {
751 p = strjoina(prefix, "/", suffix);
753 if (!IN_SET(errno, ENOENT, ENOTEMPTY))
754 return log_error_errno(errno, "Failed to remove
\"%s
\": %m
", p);
756 log_info("Removed
\"%s
\".", p);
761 static int remove_binaries(const char *esp_path) {
766 p = strjoina(esp_path, "/EFI
/systemd
");
767 r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
769 q = remove_boot_efi(esp_path);
773 for (i = ELEMENTSOF(efi_subdirs)-1; i > 0; i--) {
774 q = rmdir_one(esp_path, efi_subdirs[i-1]);
782 static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
789 r = find_slot(uuid, path, &slot);
793 r = efi_remove_boot_option(slot);
798 return remove_from_order(slot);
803 static int install_loader_config(const char *esp_path) {
805 char machine_string[SD_ID128_STRING_MAX];
806 _cleanup_(unlink_and_freep) char *t = NULL;
807 _cleanup_fclose_ FILE *f = NULL;
808 sd_id128_t machine_id;
812 r = sd_id128_get_machine(&machine_id);
814 return log_error_errno(r, "Failed to get machine id
: %m
");
816 p = strjoina(esp_path, "/loader
/loader
.conf
");
818 if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
821 fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
823 return log_error_errno(fd, "Failed to open
\"%s
\" for writing
: %m
", p);
825 f = fdopen(fd, "we
");
831 fprintf(f, "#timeout 3\n"
832 "#console-mode keep\n"
833 "default %s-*\n", sd_id128_to_string(machine_id, machine_string));
835 r = fflush_sync_and_check(f);
837 return log_error_errno(r, "Failed to write \"%s\": %m", p);
839 r = link_tmpfile(fd, t, p);
841 return 0; /* Silently skip creation if the file exists now (recheck) */
843 return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
850 static int help(int argc, char *argv[], void *userdata) {
851 _cleanup_free_ char *link = NULL;
854 r = terminal_urlify_man("bootctl", "1", &link);
858 printf("%s [COMMAND] [OPTIONS...]\n\n"
859 "Install, update or remove the systemd-boot EFI boot manager.\n\n"
860 " -h --help Show this help\n"
861 " --version Print version\n"
862 " --path=PATH Path to the EFI System Partition (ESP)\n"
863 " -p --print-path Print path to the EFI partition\n"
864 " --no-variables Don't touch EFI variables\n"
865 " --no-pager Do not pipe output into a pager\n"
866 "\nBoot Loader Commands:\n"
867 " status Show status of installed systemd-boot and EFI variables\n"
868 " install Install systemd-boot to the ESP and EFI variables\n"
869 " update Update systemd-boot in the ESP and EFI variables\n"
870 " remove Remove systemd-boot from the ESP and EFI variables\n"
871 "\nBoot Loader Entries Commands:\n"
872 " list List boot loader entries\n"
873 " set-default ID Set default boot loader entry\n"
874 " set-oneshot ID Set default boot loader entry, for next boot only\n"
875 "\nSee the %s for details.\n"
876 , program_invocation_short_name
882 static int parse_argv(int argc, char *argv[]) {
890 static const struct option options[] = {
891 { "help", no_argument, NULL, 'h' },
892 { "version", no_argument, NULL, ARG_VERSION },
893 { "path", required_argument, NULL, ARG_PATH },
894 { "print-path", no_argument, NULL, 'p' },
895 { "no-variables", no_argument, NULL, ARG_NO_VARIABLES },
896 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
905 while ((c = getopt_long(argc, argv, "hp", options, NULL)) >= 0)
916 r = free_and_strdup(&arg_path, optarg);
922 arg_print_path = true;
925 case ARG_NO_VARIABLES:
926 arg_touch_variables = false;
930 arg_pager_flags |= PAGER_DISABLE;
937 assert_not_reached("Unknown option");
943 static void read_loader_efi_var(const char *name, char **var) {
946 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var);
947 if (r < 0 && r != -ENOENT)
948 log_warning_errno(r, "Failed to read EFI variable %s: %m", name);
951 static int verb_status(int argc, char *argv[], void *userdata) {
953 sd_id128_t uuid = SD_ID128_NULL;
956 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &uuid);
958 if (arg_print_path) {
959 if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only
960 * error the
find_esp_and_warn() won
't log on its own) */
961 return log_error_errno(r, "Failed to determine ESP: %m");
969 r = 0; /* If we couldn't determine the path
, then don
't consider that a problem from here on, just show what we
972 (void) pager_open(arg_pager_flags);
975 static const struct {
979 { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" },
980 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" },
981 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" },
982 { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" },
983 { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" },
986 _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL;
987 sd_id128_t loader_part_uuid = SD_ID128_NULL;
988 uint64_t loader_features = 0;
991 read_loader_efi_var("LoaderFirmwareType", &fw_type);
992 read_loader_efi_var("LoaderFirmwareInfo", &fw_info);
993 read_loader_efi_var("LoaderInfo", &loader);
994 read_loader_efi_var("StubInfo", &stub);
995 read_loader_efi_var("LoaderImageIdentifier", &loader_path);
996 (void) efi_loader_get_features(&loader_features);
999 efi_tilt_backslashes(loader_path);
1001 k = efi_loader_get_device_part_uuid(&loader_part_uuid);
1002 if (k < 0 && k != -ENOENT)
1003 r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m");
1005 printf("System:\n");
1006 printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal());
1007 printf(" Secure Boot: %sd\n", enable_disable(is_efi_secure_boot()));
1008 printf(" Setup Mode: %s\n", is_efi_secure_boot_setup_mode() ? "setup" : "user");
1011 printf("Current Boot Loader:\n");
1012 printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal());
1014 for (i = 0; i < ELEMENTSOF(flags); i++) {
1017 printf(" Features: ");
1021 if (FLAGS_SET(loader_features, flags[i].flag))
1022 printf("%s%s%s %s\n", ansi_highlight_green(), special_glyph(CHECK_MARK), ansi_normal(), flags[i].name);
1024 printf("%s%s%s %s\n", ansi_highlight_red(), special_glyph(CROSS_MARK), ansi_normal(), flags[i].name);
1028 printf(" Stub: %s\n", stub);
1029 if (!sd_id128_is_null(loader_part_uuid))
1030 printf(" ESP: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
1031 SD_ID128_FORMAT_VAL(loader_part_uuid));
1033 printf(" ESP: n/a\n");
1034 printf(" File: %s%s\n", special_glyph(TREE_RIGHT), strna(loader_path));
1037 printf("System:\n Not booted with EFI\n\n");
1040 k = status_binaries(arg_path, uuid);
1045 if (is_efi_boot()) {
1046 k = status_variables();
1052 k = status_entries(arg_path, uuid);
1060 static int verb_list(int argc, char *argv[], void *userdata) {
1061 _cleanup_(boot_config_free) BootConfig config = {};
1062 _cleanup_free_ char **found_by_loader = NULL;
1063 sd_id128_t uuid = SD_ID128_NULL;
1066 /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two things: turn
1067 * off logging about access errors and turn off potentially privileged device probing. Here we're interested in
1068 * the latter but
not the former
, hence request the mode
, and log about EACCES
. */
1070 r
= acquire_esp(geteuid() != 0, NULL
, NULL
, NULL
, &uuid
);
1071 if (r
== -EACCES
) /* We really need the ESP path for this call, hence also log about access errors */
1072 return log_error_errno(r
, "Failed to determine ESP: %m");
1076 r
= boot_entries_load_config(arg_path
, &config
);
1080 r
= efi_loader_get_entries(&found_by_loader
);
1081 if (r
< 0 && !IN_SET(r
, -ENOENT
, -EOPNOTSUPP
))
1082 log_debug_errno(r
, "Failed to acquire boot loader discovered entries: %m");
1084 if (config
.n_entries
== 0)
1085 log_info("No boot loader entries found.");
1089 (void) pager_open(arg_pager_flags
);
1091 printf("Boot Loader Entries:\n");
1093 for (n
= 0; n
< config
.n_entries
; n
++) {
1094 r
= boot_entry_show(config
.entries
+ n
, n
== (size_t) config
.default_entry
);
1100 strv_remove(found_by_loader
, config
.entries
[n
].id
);
1104 if (!strv_isempty(found_by_loader
)) {
1107 printf("Automatic/Other Entries Found by Boot Loader:\n\n");
1109 STRV_FOREACH(i
, found_by_loader
)
1116 static int sync_esp(void) {
1117 _cleanup_close_
int fd
= -1;
1122 fd
= open(arg_path
, O_CLOEXEC
|O_DIRECTORY
|O_RDONLY
);
1124 return log_error_errno(errno
, "Couldn't open ESP '%s' for synchronization: %m", arg_path
);
1127 return log_error_errno(errno
, "Failed to synchronize the ESP '%s': %m", arg_path
);
1132 static int verb_install(int argc
, char *argv
[], void *userdata
) {
1134 sd_id128_t uuid
= SD_ID128_NULL
;
1135 uint64_t pstart
= 0, psize
= 0;
1140 r
= acquire_esp(false, &part
, &pstart
, &psize
, &uuid
);
1144 install
= streq(argv
[0], "install");
1146 RUN_WITH_UMASK(0002) {
1147 r
= install_binaries(arg_path
, install
);
1152 r
= install_loader_config(arg_path
);
1160 if (arg_touch_variables
)
1161 r
= install_variables(arg_path
,
1162 part
, pstart
, psize
, uuid
,
1163 "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME
".efi",
1169 static int verb_remove(int argc
, char *argv
[], void *userdata
) {
1170 sd_id128_t uuid
= SD_ID128_NULL
;
1173 r
= acquire_esp(false, NULL
, NULL
, NULL
, &uuid
);
1177 r
= remove_binaries(arg_path
);
1181 if (arg_touch_variables
) {
1184 q
= remove_variables(uuid
, "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME
".efi", true);
1185 if (q
< 0 && r
== 0)
1192 static int verb_set_default(int argc
, char *argv
[], void *userdata
) {
1196 if (!is_efi_boot()) {
1197 log_error("Not booted with UEFI.");
1201 if (access("/sys/firmware/efi/efivars/LoaderInfo-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK
) < 0) {
1202 if (errno
== ENOENT
) {
1203 log_error_errno(errno
, "Not booted with a supported boot loader.");
1207 return log_error_errno(errno
, "Failed to detect whether boot loader supports '%s' operation: %m", argv
[0]);
1210 if (detect_container() > 0) {
1211 log_error("'%s' operation not supported in a container.", argv
[0]);
1215 if (!arg_touch_variables
) {
1216 log_error("'%s' operation cannot be combined with --touch-variables=no.", argv
[0]);
1220 name
= streq(argv
[0], "set-default") ? "LoaderEntryDefault" : "LoaderEntryOneShot";
1222 if (isempty(argv
[1])) {
1223 r
= efi_set_variable(EFI_VENDOR_LOADER
, name
, NULL
, 0);
1224 if (r
< 0 && r
!= -ENOENT
)
1225 return log_error_errno(r
, "Failed to remove EFI variale: %m");
1227 _cleanup_free_ char16_t
*encoded
= NULL
;
1229 encoded
= utf8_to_utf16(argv
[1], strlen(argv
[1]));
1233 r
= efi_set_variable(EFI_VENDOR_LOADER
, name
, encoded
, char16_strlen(encoded
) * 2 + 2);
1235 return log_error_errno(r
, "Failed to update EFI variable: %m");
1241 static int bootctl_main(int argc
, char *argv
[]) {
1243 static const Verb verbs
[] = {
1244 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1245 { "status", VERB_ANY
, 1, VERB_DEFAULT
, verb_status
},
1246 { "install", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_install
},
1247 { "update", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_install
},
1248 { "remove", VERB_ANY
, 1, VERB_MUST_BE_ROOT
, verb_remove
},
1249 { "list", VERB_ANY
, 1, 0, verb_list
},
1250 { "set-default", 2, 2, VERB_MUST_BE_ROOT
, verb_set_default
},
1251 { "set-oneshot", 2, 2, VERB_MUST_BE_ROOT
, verb_set_default
},
1255 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1258 static int run(int argc
, char *argv
[]) {
1261 log_parse_environment();
1264 /* If we run in a container, automatically turn of EFI file system access */
1265 if (detect_container() > 0)
1266 arg_touch_variables
= false;
1268 r
= parse_argv(argc
, argv
);
1272 return bootctl_main(argc
, argv
);
1275 DEFINE_MAIN_FUNCTION(run
);