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 if (st
.st_size
< 27) {
104 buf
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
105 if (buf
== MAP_FAILED
)
106 return log_error_errno(errno
, "Failed to memory map EFI binary: %m");
108 s
= memmem(buf
, st
.st_size
- 8, "#### LoaderInfo: ", 17);
113 e
= memmem(s
, st
.st_size
- (s
- buf
), " ####", 5);
114 if (!e
|| e
- s
< 3) {
115 log_error("Malformed version string.");
120 x
= strndup(s
, e
- s
);
128 (void) munmap(buf
, st
.st_size
);
133 static int enumerate_binaries(const char *esp_path
, const char *path
, const char *prefix
) {
135 _cleanup_closedir_
DIR *d
= NULL
;
139 p
= strjoina(esp_path
, "/", path
);
145 return log_error_errno(errno
, "Failed to read \"%s\": %m", p
);
148 FOREACH_DIRENT(de
, d
, break) {
149 _cleanup_close_
int fd
= -1;
150 _cleanup_free_
char *v
= NULL
;
152 if (!endswith_no_case(de
->d_name
, ".efi"))
155 if (prefix
&& !startswith_no_case(de
->d_name
, prefix
))
158 fd
= openat(dirfd(d
), de
->d_name
, O_RDONLY
|O_CLOEXEC
);
160 return log_error_errno(errno
, "Failed to open \"%s/%s\" for reading: %m", p
, de
->d_name
);
162 r
= get_file_version(fd
, &v
);
166 printf(" File: %s/%s/%s (%s%s%s)\n", special_glyph(TREE_RIGHT
), path
, de
->d_name
, ansi_highlight(), v
, ansi_normal());
168 printf(" File: %s/%s/%s\n", special_glyph(TREE_RIGHT
), path
, de
->d_name
);
175 static int status_binaries(const char *esp_path
, sd_id128_t partition
) {
178 printf("Available Boot Loaders on ESP:\n");
181 printf(" ESP: Cannot find or access mount point of ESP.\n\n");
185 printf(" ESP: %s", esp_path
);
186 if (!sd_id128_is_null(partition
))
187 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
));
190 r
= enumerate_binaries(esp_path
, "EFI/systemd", NULL
);
192 log_info("systemd-boot not installed in ESP.");
196 r
= enumerate_binaries(esp_path
, "EFI/BOOT", "boot");
198 log_info("No default/fallback boot loader installed in ESP.");
207 static int print_efi_option(uint16_t id
, bool in_order
) {
208 _cleanup_free_
char *title
= NULL
;
209 _cleanup_free_
char *path
= NULL
;
210 sd_id128_t partition
;
214 r
= efi_get_boot_option(id
, &title
, &partition
, &path
, &active
);
218 /* print only configured entries with partition information */
219 if (!path
|| sd_id128_is_null(partition
))
222 efi_tilt_backslashes(path
);
224 printf(" Title: %s%s%s\n", ansi_highlight(), strna(title
), ansi_normal());
225 printf(" ID: 0x%04X\n", id
);
226 printf(" Status: %sactive%s\n", active
? "" : "in", in_order
? ", boot-order" : "");
227 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
));
228 printf(" File: %s%s\n", special_glyph(TREE_RIGHT
), path
);
234 static int status_variables(void) {
235 _cleanup_free_
uint16_t *options
= NULL
, *order
= NULL
;
236 int n_options
, n_order
, i
;
238 n_options
= efi_get_boot_options(&options
);
239 if (n_options
== -ENOENT
)
240 return log_error_errno(n_options
,
241 "Failed to access EFI variables, efivarfs"
242 " needs to be available at /sys/firmware/efi/efivars/.");
244 return log_error_errno(n_options
, "Failed to read EFI boot entries: %m");
246 n_order
= efi_get_boot_order(&order
);
247 if (n_order
== -ENOENT
)
249 else if (n_order
< 0)
250 return log_error_errno(n_order
, "Failed to read EFI boot order: %m");
252 /* print entries in BootOrder first */
253 printf("Boot Loaders Listed in EFI Variables:\n");
254 for (i
= 0; i
< n_order
; i
++)
255 print_efi_option(order
[i
], true);
257 /* print remaining entries */
258 for (i
= 0; i
< n_options
; i
++) {
261 for (j
= 0; j
< n_order
; j
++)
262 if (options
[i
] == order
[j
])
265 print_efi_option(options
[i
], false);
274 static int boot_entry_show(const BootEntry
*e
, bool show_as_default
) {
277 printf(" title: %s%s%s%s%s%s\n",
281 ansi_highlight_green(),
282 show_as_default
? " (default)" : "",
286 printf(" id: %s\n", e
->id
);
288 printf(" version: %s\n", e
->version
);
290 printf(" machine-id: %s\n", e
->machine_id
);
292 printf(" architecture: %s\n", e
->architecture
);
294 printf(" linux: %s\n", e
->kernel
);
295 if (!strv_isempty(e
->initrd
)) {
296 _cleanup_free_
char *t
;
298 t
= strv_join(e
->initrd
, " ");
302 printf(" initrd: %s\n", t
);
304 if (!strv_isempty(e
->options
)) {
305 _cleanup_free_
char *t
;
307 t
= strv_join(e
->options
, " ");
311 printf(" options: %s\n", t
);
314 printf(" devicetree: %s\n", e
->device_tree
);
319 static int status_entries(const char *esp_path
, sd_id128_t partition
) {
320 _cleanup_(boot_config_free
) BootConfig config
= {};
323 r
= boot_entries_load_config(esp_path
, &config
);
327 if (config
.default_entry
< 0)
328 printf("%zu entries, no entry could be determined as default.\n", config
.n_entries
);
330 printf("Default Boot Loader Entry:\n");
332 boot_entry_show(config
.entries
+ config
.default_entry
, false);
338 static int compare_product(const char *a
, const char *b
) {
347 return x
< y
? -1 : x
> y
? 1 : 0;
349 return strncmp(a
, b
, x
);
352 static int compare_version(const char *a
, const char *b
) {
356 a
+= strcspn(a
, " ");
358 b
+= strcspn(b
, " ");
361 return strverscmp(a
, b
);
364 static int version_check(int fd_from
, const char *from
, int fd_to
, const char *to
) {
365 _cleanup_free_
char *a
= NULL
, *b
= NULL
;
368 assert(fd_from
>= 0);
373 r
= get_file_version(fd_from
, &a
);
377 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
378 "Source file \"%s\" does not carry version information!",
381 r
= get_file_version(fd_to
, &b
);
384 if (r
== 0 || compare_product(a
, b
) != 0)
385 return log_notice_errno(SYNTHETIC_ERRNO(EEXIST
),
386 "Skipping \"%s\", since it's owned by another boot loader.",
389 if (compare_version(a
, b
) < 0) {
390 log_warning("Skipping \"%s\", since a newer boot loader version exists already.", to
);
397 static int copy_file_with_version_check(const char *from
, const char *to
, bool force
) {
398 _cleanup_close_
int fd_from
= -1, fd_to
= -1;
399 _cleanup_free_
char *t
= NULL
;
402 fd_from
= open(from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
404 return log_error_errno(errno
, "Failed to open \"%s\" for reading: %m", from
);
407 fd_to
= open(to
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
409 if (errno
!= -ENOENT
)
410 return log_error_errno(errno
, "Failed to open \"%s\" for reading: %m", to
);
412 r
= version_check(fd_from
, from
, fd_to
, to
);
416 if (lseek(fd_from
, 0, SEEK_SET
) == (off_t
) -1)
417 return log_error_errno(errno
, "Failed to seek in \"%s\": %m", from
);
419 fd_to
= safe_close(fd_to
);
423 r
= tempfn_random(to
, NULL
, &t
);
427 RUN_WITH_UMASK(0000) {
428 fd_to
= open(t
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_EXCL
|O_NOFOLLOW
, 0644);
430 return log_error_errno(errno
, "Failed to open \"%s\" for writing: %m", t
);
433 r
= copy_bytes(fd_from
, fd_to
, (uint64_t) -1, COPY_REFLINK
);
436 return log_error_errno(r
, "Failed to copy data from \"%s\" to \"%s\": %m", from
, t
);
439 (void) copy_times(fd_from
, fd_to
);
441 if (fsync(fd_to
) < 0) {
442 (void) unlink_noerrno(t
);
443 return log_error_errno(errno
, "Failed to copy data from \"%s\" to \"%s\": %m", from
, t
);
446 (void) fsync_directory_of_file(fd_to
);
448 if (renameat(AT_FDCWD
, t
, AT_FDCWD
, to
) < 0) {
449 (void) unlink_noerrno(t
);
450 return log_error_errno(errno
, "Failed to rename \"%s\" to \"%s\": %m", t
, to
);
453 log_info("Copied \"%s\" to \"%s\".", from
, to
);
458 static int mkdir_one(const char *prefix
, const char *suffix
) {
461 p
= strjoina(prefix
, "/", suffix
);
462 if (mkdir(p
, 0700) < 0) {
464 return log_error_errno(errno
, "Failed to create \"%s\": %m", p
);
466 log_info("Created \"%s\".", p
);
471 static const char *efi_subdirs
[] = {
480 static int create_dirs(const char *esp_path
) {
484 STRV_FOREACH(i
, efi_subdirs
) {
485 r
= mkdir_one(esp_path
, *i
);
493 static int copy_one_file(const char *esp_path
, const char *name
, bool force
) {
497 p
= strjoina(BOOTLIBDIR
"/", name
);
498 q
= strjoina(esp_path
, "/EFI/systemd/", name
);
499 r
= copy_file_with_version_check(p
, q
, force
);
501 if (startswith(name
, "systemd-boot")) {
505 /* Create the EFI default boot loader name (specified for removable devices) */
506 v
= strjoina(esp_path
, "/EFI/BOOT/BOOT",
507 name
+ STRLEN("systemd-boot"));
508 ascii_strupper(strrchr(v
, '/') + 1);
510 k
= copy_file_with_version_check(p
, v
, force
);
518 static int install_binaries(const char *esp_path
, bool force
) {
520 _cleanup_closedir_
DIR *d
= NULL
;
524 /* Don't create any of these directories when we are
525 * just updating. When we update we'll drop-in our
526 * files (unless there are newer ones already), but we
527 * won't create the directories for them in the first
529 r
= create_dirs(esp_path
);
534 d
= opendir(BOOTLIBDIR
);
536 return log_error_errno(errno
, "Failed to open \""BOOTLIBDIR
"\": %m
");
538 FOREACH_DIRENT(de, d, break) {
541 if (!endswith_no_case(de->d_name, ".efi
"))
544 k = copy_one_file(esp_path, de->d_name, force);
552 static bool same_entry(uint16_t id, const sd_id128_t uuid, const char *path) {
553 _cleanup_free_ char *opath = NULL;
557 r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
560 if (!sd_id128_equal(uuid, ouuid))
562 if (!streq_ptr(path, opath))
568 static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
569 _cleanup_free_ uint16_t *options = NULL;
572 n = efi_get_boot_options(&options);
576 /* find already existing systemd-boot entry */
577 for (i = 0; i < n; i++)
578 if (same_entry(options[i], uuid, path)) {
583 /* find free slot in the sorted BootXXXX variable list */
584 for (i = 0; i < n; i++)
585 if (i != options[i]) {
590 /* use the next one */
597 static int insert_into_order(uint16_t slot, bool first) {
598 _cleanup_free_ uint16_t *order = NULL;
602 n = efi_get_boot_order(&order);
604 /* no entry, add us */
605 return efi_set_boot_order(&slot, 1);
607 /* are we the first and only one? */
608 if (n == 1 && order[0] == slot)
611 /* are we already in the boot order? */
612 for (i = 0; i < n; i++) {
613 if (order[i] != slot)
616 /* we do not require to be the first one, all is fine */
620 /* move us to the first slot */
621 memmove(order + 1, order, i * sizeof(uint16_t));
623 return efi_set_boot_order(order, n);
627 t = realloc(order, (n + 1) * sizeof(uint16_t));
632 /* add us to the top or end of the list */
634 memmove(order + 1, order, n * sizeof(uint16_t));
639 return efi_set_boot_order(order, n + 1);
642 static int remove_from_order(uint16_t slot) {
643 _cleanup_free_ uint16_t *order = NULL;
646 n = efi_get_boot_order(&order);
650 for (i = 0; i < n; i++) {
651 if (order[i] != slot)
655 memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
656 return efi_set_boot_order(order, n - 1);
662 static int install_variables(const char *esp_path,
663 uint32_t part, uint64_t pstart, uint64_t psize,
664 sd_id128_t uuid, const char *path,
670 if (!is_efi_boot()) {
671 log_warning("Not booted with EFI
, skipping EFI variable setup
.");
675 p = strjoina(esp_path, path);
676 if (access(p, F_OK) < 0) {
680 return log_error_errno(errno, "Cannot access
\"%s
\": %m
", p);
683 r = find_slot(uuid, path, &slot);
685 return log_error_errno(r,
687 "Failed to access EFI variables
. Is the
\"efivarfs
\" filesystem mounted
?" :
688 "Failed to determine current boot order
: %m
");
690 if (first || r == 0) {
691 r = efi_add_boot_option(slot, "Linux Boot Manager
",
695 return log_error_errno(r, "Failed to create EFI Boot variable entry
: %m
");
697 log_info("Created EFI boot entry
\"Linux Boot Manager
\".");
700 return insert_into_order(slot, first);
703 static int remove_boot_efi(const char *esp_path) {
705 _cleanup_closedir_ DIR *d = NULL;
709 p = strjoina(esp_path, "/EFI
/BOOT
");
715 return log_error_errno(errno, "Failed to open directory
\"%s
\": %m
", p);
718 FOREACH_DIRENT(de, d, break) {
719 _cleanup_close_ int fd = -1;
720 _cleanup_free_ char *v = NULL;
722 if (!endswith_no_case(de->d_name, ".efi
"))
725 if (!startswith_no_case(de->d_name, "boot
"))
728 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
730 return log_error_errno(errno, "Failed to open
\"%s
/%s
\" for reading
: %m
", p, de->d_name);
732 r = get_file_version(fd, &v);
735 if (r > 0 && startswith(v, "systemd
-boot
")) {
736 r = unlinkat(dirfd(d), de->d_name, 0);
738 return log_error_errno(errno, "Failed to remove
\"%s
/%s
\": %m
", p, de->d_name);
740 log_info("Removed
\"%s
/%s
\".", p, de->d_name);
749 static int rmdir_one(const char *prefix, const char *suffix) {
752 p = strjoina(prefix, "/", suffix);
754 if (!IN_SET(errno, ENOENT, ENOTEMPTY))
755 return log_error_errno(errno, "Failed to remove
\"%s
\": %m
", p);
757 log_info("Removed
\"%s
\".", p);
762 static int remove_binaries(const char *esp_path) {
767 p = strjoina(esp_path, "/EFI
/systemd
");
768 r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
770 q = remove_boot_efi(esp_path);
774 for (i = ELEMENTSOF(efi_subdirs)-1; i > 0; i--) {
775 q = rmdir_one(esp_path, efi_subdirs[i-1]);
783 static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
790 r = find_slot(uuid, path, &slot);
794 r = efi_remove_boot_option(slot);
799 return remove_from_order(slot);
804 static int install_loader_config(const char *esp_path) {
806 char machine_string[SD_ID128_STRING_MAX];
807 _cleanup_(unlink_and_freep) char *t = NULL;
808 _cleanup_fclose_ FILE *f = NULL;
809 sd_id128_t machine_id;
813 r = sd_id128_get_machine(&machine_id);
815 return log_error_errno(r, "Failed to get machine id
: %m
");
817 p = strjoina(esp_path, "/loader
/loader
.conf
");
819 if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
822 fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
824 return log_error_errno(fd, "Failed to open
\"%s
\" for writing
: %m
", p);
832 fprintf(f, "#timeout 3\n"
833 "#console-mode keep\n"
834 "default %s-*\n", sd_id128_to_string(machine_id, machine_string));
836 r = fflush_sync_and_check(f);
838 return log_error_errno(r, "Failed to write \"%s\": %m", p);
840 r = link_tmpfile(fd, t, p);
842 return 0; /* Silently skip creation if the file exists now (recheck) */
844 return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
851 static int help(int argc, char *argv[], void *userdata) {
852 _cleanup_free_ char *link = NULL;
855 r = terminal_urlify_man("bootctl", "1", &link);
859 printf("%s [COMMAND] [OPTIONS...]\n\n"
860 "Install, update or remove the systemd-boot EFI boot manager.\n\n"
861 " -h --help Show this help\n"
862 " --version Print version\n"
863 " --path=PATH Path to the EFI System Partition (ESP)\n"
864 " -p --print-path Print path to the EFI partition\n"
865 " --no-variables Don't touch EFI variables\n"
866 " --no-pager Do not pipe output into a pager\n"
867 "\nBoot Loader Commands:\n"
868 " status Show status of installed systemd-boot and EFI variables\n"
869 " install Install systemd-boot to the ESP and EFI variables\n"
870 " update Update systemd-boot in the ESP and EFI variables\n"
871 " remove Remove systemd-boot from the ESP and EFI variables\n"
872 "\nBoot Loader Entries Commands:\n"
873 " list List boot loader entries\n"
874 " set-default ID Set default boot loader entry\n"
875 " set-oneshot ID Set default boot loader entry, for next boot only\n"
876 "\nSee the %s for details.\n"
877 , program_invocation_short_name
883 static int parse_argv(int argc, char *argv[]) {
891 static const struct option options[] = {
892 { "help", no_argument, NULL, 'h' },
893 { "version", no_argument, NULL, ARG_VERSION },
894 { "path", required_argument, NULL, ARG_PATH },
895 { "print-path", no_argument, NULL, 'p' },
896 { "no-variables", no_argument, NULL, ARG_NO_VARIABLES },
897 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
906 while ((c = getopt_long(argc, argv, "hp", options, NULL)) >= 0)
917 r = free_and_strdup(&arg_path, optarg);
923 arg_print_path = true;
926 case ARG_NO_VARIABLES:
927 arg_touch_variables = false;
931 arg_pager_flags |= PAGER_DISABLE;
938 assert_not_reached("Unknown option");
944 static void read_loader_efi_var(const char *name, char **var) {
947 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var);
948 if (r < 0 && r != -ENOENT)
949 log_warning_errno(r, "Failed to read EFI variable %s: %m", name);
952 static int verb_status(int argc, char *argv[], void *userdata) {
954 sd_id128_t uuid = SD_ID128_NULL;
957 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &uuid);
959 if (arg_print_path) {
960 if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only
961 * error the
find_esp_and_warn() won
't log on its own) */
962 return log_error_errno(r, "Failed to determine ESP: %m");
970 r = 0; /* If we couldn't determine the path
, then don
't consider that a problem from here on, just show what we
973 (void) pager_open(arg_pager_flags);
976 static const struct {
980 { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" },
981 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" },
982 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" },
983 { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" },
984 { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" },
987 _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL;
988 sd_id128_t loader_part_uuid = SD_ID128_NULL;
989 uint64_t loader_features = 0;
992 read_loader_efi_var("LoaderFirmwareType", &fw_type);
993 read_loader_efi_var("LoaderFirmwareInfo", &fw_info);
994 read_loader_efi_var("LoaderInfo", &loader);
995 read_loader_efi_var("StubInfo", &stub);
996 read_loader_efi_var("LoaderImageIdentifier", &loader_path);
997 (void) efi_loader_get_features(&loader_features);
1000 efi_tilt_backslashes(loader_path);
1002 k = efi_loader_get_device_part_uuid(&loader_part_uuid);
1003 if (k < 0 && k != -ENOENT)
1004 r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m");
1006 printf("System:\n");
1007 printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal());
1008 printf(" Secure Boot: %sd\n", enable_disable(is_efi_secure_boot()));
1009 printf(" Setup Mode: %s\n", is_efi_secure_boot_setup_mode() ? "setup" : "user");
1012 printf("Current Boot Loader:\n");
1013 printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal());
1015 for (i = 0; i < ELEMENTSOF(flags); i++) {
1018 printf(" Features: ");
1022 if (FLAGS_SET(loader_features, flags[i].flag))
1023 printf("%s%s%s %s\n", ansi_highlight_green(), special_glyph(CHECK_MARK), ansi_normal(), flags[i].name);
1025 printf("%s%s%s %s\n", ansi_highlight_red(), special_glyph(CROSS_MARK), ansi_normal(), flags[i].name);
1029 printf(" Stub: %s\n", stub);
1030 if (!sd_id128_is_null(loader_part_uuid))
1031 printf(" ESP: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
1032 SD_ID128_FORMAT_VAL(loader_part_uuid));
1034 printf(" ESP: n/a\n");
1035 printf(" File: %s%s\n", special_glyph(TREE_RIGHT), strna(loader_path));
1038 printf("System:\n Not booted with EFI\n\n");
1041 k = status_binaries(arg_path, uuid);
1046 if (is_efi_boot()) {
1047 k = status_variables();
1053 k = status_entries(arg_path, uuid);
1061 static int verb_list(int argc, char *argv[], void *userdata) {
1062 _cleanup_(boot_config_free) BootConfig config = {};
1063 _cleanup_free_ char **found_by_loader = NULL;
1064 sd_id128_t uuid = SD_ID128_NULL;
1067 /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two things: turn
1068 * off logging about access errors and turn off potentially privileged device probing. Here we're interested in
1069 * the latter but
not the former
, hence request the mode
, and log about EACCES
. */
1071 r
= acquire_esp(geteuid() != 0, NULL
, NULL
, NULL
, &uuid
);
1072 if (r
== -EACCES
) /* We really need the ESP path for this call, hence also log about access errors */
1073 return log_error_errno(r
, "Failed to determine ESP: %m");
1077 r
= boot_entries_load_config(arg_path
, &config
);
1081 r
= efi_loader_get_entries(&found_by_loader
);
1082 if (r
< 0 && !IN_SET(r
, -ENOENT
, -EOPNOTSUPP
))
1083 log_debug_errno(r
, "Failed to acquire boot loader discovered entries: %m");
1085 if (config
.n_entries
== 0)
1086 log_info("No boot loader entries found.");
1090 (void) pager_open(arg_pager_flags
);
1092 printf("Boot Loader Entries:\n");
1094 for (n
= 0; n
< config
.n_entries
; n
++) {
1095 r
= boot_entry_show(config
.entries
+ n
, n
== (size_t) config
.default_entry
);
1101 strv_remove(found_by_loader
, config
.entries
[n
].id
);
1105 if (!strv_isempty(found_by_loader
)) {
1108 printf("Automatic/Other Entries Found by Boot Loader:\n\n");
1110 STRV_FOREACH(i
, found_by_loader
)
1117 static int sync_esp(void) {
1118 _cleanup_close_
int fd
= -1;
1123 fd
= open(arg_path
, O_CLOEXEC
|O_DIRECTORY
|O_RDONLY
);
1125 return log_error_errno(errno
, "Couldn't open ESP '%s' for synchronization: %m", arg_path
);
1128 return log_error_errno(errno
, "Failed to synchronize the ESP '%s': %m", arg_path
);
1133 static int verb_install(int argc
, char *argv
[], void *userdata
) {
1135 sd_id128_t uuid
= SD_ID128_NULL
;
1136 uint64_t pstart
= 0, psize
= 0;
1141 r
= acquire_esp(false, &part
, &pstart
, &psize
, &uuid
);
1145 install
= streq(argv
[0], "install");
1147 RUN_WITH_UMASK(0002) {
1148 r
= install_binaries(arg_path
, install
);
1153 r
= install_loader_config(arg_path
);
1161 if (arg_touch_variables
)
1162 r
= install_variables(arg_path
,
1163 part
, pstart
, psize
, uuid
,
1164 "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME
".efi",
1170 static int verb_remove(int argc
, char *argv
[], void *userdata
) {
1171 sd_id128_t uuid
= SD_ID128_NULL
;
1174 r
= acquire_esp(false, NULL
, NULL
, NULL
, &uuid
);
1178 r
= remove_binaries(arg_path
);
1182 if (arg_touch_variables
) {
1185 q
= remove_variables(uuid
, "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME
".efi", true);
1186 if (q
< 0 && r
== 0)
1193 static int verb_set_default(int argc
, char *argv
[], void *userdata
) {
1198 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1199 "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 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1212 "'%s' operation not supported in a container.",
1215 if (!arg_touch_variables
)
1216 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1217 "'%s' operation cannot be combined with --touch-variables=no.",
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
);