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 "stdio-util.h"
39 #include "string-util.h"
41 #include "terminal-util.h"
42 #include "tmpfile-util.h"
43 #include "umask-util.h"
49 static char *arg_esp_path
= NULL
;
50 static char *arg_xbootldr_path
= NULL
;
51 static bool arg_print_esp_path
= false;
52 static bool arg_print_dollar_boot_path
= false;
53 static bool arg_touch_variables
= true;
54 static PagerFlags arg_pager_flags
= 0;
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
;
108 free_and_replace(arg_xbootldr_path
, np
);
109 log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path
);
114 /* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */
115 static int get_file_version(int fd
, char **v
) {
125 if (fstat(fd
, &st
) < 0)
126 return log_error_errno(errno
, "Failed to stat EFI binary: %m");
128 r
= stat_verify_regular(&st
);
130 return log_error_errno(errno
, "EFI binary is not a regular file: %m");
132 if (st
.st_size
< 27) {
137 buf
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
138 if (buf
== MAP_FAILED
)
139 return log_error_errno(errno
, "Failed to memory map EFI binary: %m");
141 s
= memmem(buf
, st
.st_size
- 8, "#### LoaderInfo: ", 17);
146 e
= memmem(s
, st
.st_size
- (s
- buf
), " ####", 5);
147 if (!e
|| e
- s
< 3) {
148 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Malformed version string.");
152 x
= strndup(s
, e
- s
);
160 (void) munmap(buf
, st
.st_size
);
165 static int enumerate_binaries(const char *esp_path
, const char *path
, const char *prefix
) {
166 _cleanup_closedir_
DIR *d
= NULL
;
174 p
= strjoina(esp_path
, "/", path
);
180 return log_error_errno(errno
, "Failed to read \"%s\": %m", p
);
183 FOREACH_DIRENT(de
, d
, break) {
184 _cleanup_free_
char *v
= NULL
;
185 _cleanup_close_
int fd
= -1;
187 if (!endswith_no_case(de
->d_name
, ".efi"))
190 if (prefix
&& !startswith_no_case(de
->d_name
, prefix
))
193 fd
= openat(dirfd(d
), de
->d_name
, O_RDONLY
|O_CLOEXEC
);
195 return log_error_errno(errno
, "Failed to open \"%s/%s\" for reading: %m", p
, de
->d_name
);
197 r
= get_file_version(fd
, &v
);
201 printf(" File: %s/%s/%s (%s%s%s)\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), path
, de
->d_name
, ansi_highlight(), v
, ansi_normal());
203 printf(" File: %s/%s/%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), path
, de
->d_name
);
211 static int status_binaries(const char *esp_path
, sd_id128_t partition
) {
214 printf("Available Boot Loaders on ESP:\n");
217 printf(" ESP: Cannot find or access mount point of ESP.\n\n");
221 printf(" ESP: %s", esp_path
);
222 if (!sd_id128_is_null(partition
))
223 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR
")", SD_ID128_FORMAT_VAL(partition
));
226 r
= enumerate_binaries(esp_path
, "EFI/systemd", NULL
);
230 log_info("systemd-boot not installed in ESP.");
232 r
= enumerate_binaries(esp_path
, "EFI/BOOT", "boot");
236 log_info("No default/fallback boot loader installed in ESP.");
245 static int print_efi_option(uint16_t id
, bool in_order
) {
246 _cleanup_free_
char *title
= NULL
;
247 _cleanup_free_
char *path
= NULL
;
248 sd_id128_t partition
;
252 r
= efi_get_boot_option(id
, &title
, &partition
, &path
, &active
);
256 /* print only configured entries with partition information */
257 if (!path
|| sd_id128_is_null(partition
))
260 efi_tilt_backslashes(path
);
262 printf(" Title: %s%s%s\n", ansi_highlight(), strna(title
), ansi_normal());
263 printf(" ID: 0x%04X\n", id
);
264 printf(" Status: %sactive%s\n", active
? "" : "in", in_order
? ", boot-order" : "");
265 printf(" Partition: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR
"\n",
266 SD_ID128_FORMAT_VAL(partition
));
267 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), path
);
273 static int status_variables(void) {
274 _cleanup_free_
uint16_t *options
= NULL
, *order
= NULL
;
275 int n_options
, n_order
, i
;
277 n_options
= efi_get_boot_options(&options
);
278 if (n_options
== -ENOENT
)
279 return log_error_errno(n_options
,
280 "Failed to access EFI variables, efivarfs"
281 " needs to be available at /sys/firmware/efi/efivars/.");
283 return log_error_errno(n_options
, "Failed to read EFI boot entries: %m");
285 n_order
= efi_get_boot_order(&order
);
286 if (n_order
== -ENOENT
)
288 else if (n_order
< 0)
289 return log_error_errno(n_order
, "Failed to read EFI boot order: %m");
291 /* print entries in BootOrder first */
292 printf("Boot Loaders Listed in EFI Variables:\n");
293 for (i
= 0; i
< n_order
; i
++)
294 print_efi_option(order
[i
], true);
296 /* print remaining entries */
297 for (i
= 0; i
< n_options
; i
++) {
300 for (j
= 0; j
< n_order
; j
++)
301 if (options
[i
] == order
[j
])
304 print_efi_option(options
[i
], false);
313 static int boot_entry_file_check(const char *root
, const char *p
) {
314 _cleanup_free_
char *path
;
316 path
= path_join(root
, p
);
320 if (access(path
, F_OK
) < 0)
326 static void boot_entry_file_list(const char *field
, const char *root
, const char *p
, int *ret_status
) {
327 int status
= boot_entry_file_check(root
, p
);
329 printf("%13s%s ", strempty(field
), field
? ":" : " ");
332 printf("%s%s%s (%m)\n", ansi_highlight_red(), p
, ansi_normal());
336 if (*ret_status
== 0 && status
< 0)
337 *ret_status
= status
;
340 static int boot_entry_show(const BootEntry
*e
, bool show_as_default
) {
343 /* Returns 0 on success, negative on processing error, and positive if something is wrong with the
344 boot entry itself. */
348 printf(" title: %s%s%s" "%s%s%s\n",
349 ansi_highlight(), boot_entry_title(e
), ansi_normal(),
350 ansi_highlight_green(), show_as_default
? " (default)" : "", ansi_normal());
353 printf(" id: %s\n", e
->id
);
355 _cleanup_free_
char *link
= NULL
;
357 /* Let's urlify the link to make it easy to view in an editor, but only if it is a text
358 * file. Unified images are binary ELFs, and EFI variables are not pure text either. */
359 if (e
->type
== BOOT_ENTRY_CONF
)
360 (void) terminal_urlify_path(e
->path
, NULL
, &link
);
362 printf(" source: %s\n", link
?: e
->path
);
365 printf(" version: %s\n", e
->version
);
367 printf(" machine-id: %s\n", e
->machine_id
);
369 printf(" architecture: %s\n", e
->architecture
);
371 boot_entry_file_list("linux", e
->root
, e
->kernel
, &status
);
374 STRV_FOREACH(s
, e
->initrd
)
375 boot_entry_file_list(s
== e
->initrd
? "initrd" : NULL
,
379 if (!strv_isempty(e
->options
)) {
380 _cleanup_free_
char *t
;
382 t
= strv_join(e
->options
, " ");
386 printf(" options: %s\n", t
);
389 boot_entry_file_list("devicetree", e
->root
, e
->device_tree
, &status
);
394 static int status_entries(
395 const char *esp_path
,
396 sd_id128_t esp_partition_uuid
,
397 const char *xbootldr_path
,
398 sd_id128_t xbootldr_partition_uuid
) {
400 _cleanup_(boot_config_free
) BootConfig config
= {};
401 sd_id128_t dollar_boot_partition_uuid
;
402 const char *dollar_boot_path
;
405 assert(esp_path
|| xbootldr_path
);
408 dollar_boot_path
= xbootldr_path
;
409 dollar_boot_partition_uuid
= xbootldr_partition_uuid
;
411 dollar_boot_path
= esp_path
;
412 dollar_boot_partition_uuid
= esp_partition_uuid
;
415 printf("Boot Loader Entries:\n"
416 " $BOOT: %s", dollar_boot_path
);
417 if (!sd_id128_is_null(dollar_boot_partition_uuid
))
418 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR
")",
419 SD_ID128_FORMAT_VAL(dollar_boot_partition_uuid
));
422 r
= boot_entries_load_config(esp_path
, xbootldr_path
, &config
);
426 if (config
.default_entry
< 0)
427 printf("%zu entries, no entry could be determined as default.\n", config
.n_entries
);
429 printf("Default Boot Loader Entry:\n");
431 r
= boot_entry_show(config
.entries
+ config
.default_entry
, false);
433 /* < 0 is already logged by the function itself, let's just emit an extra warning if
434 the default entry is broken */
435 printf("\nWARNING: default boot entry is broken\n");
441 static int compare_product(const char *a
, const char *b
) {
450 return x
< y
? -1 : x
> y
? 1 : 0;
452 return strncmp(a
, b
, x
);
455 static int compare_version(const char *a
, const char *b
) {
459 a
+= strcspn(a
, " ");
461 b
+= strcspn(b
, " ");
464 return strverscmp(a
, b
);
467 static int version_check(int fd_from
, const char *from
, int fd_to
, const char *to
) {
468 _cleanup_free_
char *a
= NULL
, *b
= NULL
;
471 assert(fd_from
>= 0);
476 r
= get_file_version(fd_from
, &a
);
480 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
481 "Source file \"%s\" does not carry version information!",
484 r
= get_file_version(fd_to
, &b
);
487 if (r
== 0 || compare_product(a
, b
) != 0)
488 return log_notice_errno(SYNTHETIC_ERRNO(EEXIST
),
489 "Skipping \"%s\", since it's owned by another boot loader.",
492 if (compare_version(a
, b
) < 0)
493 return log_warning_errno(SYNTHETIC_ERRNO(ESTALE
), "Skipping \"%s\", since a newer boot loader version exists already.", to
);
498 static int copy_file_with_version_check(const char *from
, const char *to
, bool force
) {
499 _cleanup_close_
int fd_from
= -1, fd_to
= -1;
500 _cleanup_free_
char *t
= NULL
;
503 fd_from
= open(from
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
505 return log_error_errno(errno
, "Failed to open \"%s\" for reading: %m", from
);
508 fd_to
= open(to
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
510 if (errno
!= -ENOENT
)
511 return log_error_errno(errno
, "Failed to open \"%s\" for reading: %m", to
);
513 r
= version_check(fd_from
, from
, fd_to
, to
);
517 if (lseek(fd_from
, 0, SEEK_SET
) == (off_t
) -1)
518 return log_error_errno(errno
, "Failed to seek in \"%s\": %m", from
);
520 fd_to
= safe_close(fd_to
);
524 r
= tempfn_random(to
, NULL
, &t
);
528 RUN_WITH_UMASK(0000) {
529 fd_to
= open(t
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_EXCL
|O_NOFOLLOW
, 0644);
531 return log_error_errno(errno
, "Failed to open \"%s\" for writing: %m", t
);
534 r
= copy_bytes(fd_from
, fd_to
, (uint64_t) -1, COPY_REFLINK
);
537 return log_error_errno(r
, "Failed to copy data from \"%s\" to \"%s\": %m", from
, t
);
540 (void) copy_times(fd_from
, fd_to
, 0);
542 if (fsync(fd_to
) < 0) {
543 (void) unlink_noerrno(t
);
544 return log_error_errno(errno
, "Failed to copy data from \"%s\" to \"%s\": %m", from
, t
);
547 (void) fsync_directory_of_file(fd_to
);
549 if (renameat(AT_FDCWD
, t
, AT_FDCWD
, to
) < 0) {
550 (void) unlink_noerrno(t
);
551 return log_error_errno(errno
, "Failed to rename \"%s\" to \"%s\": %m", t
, to
);
554 log_info("Copied \"%s\" to \"%s\".", from
, to
);
559 static int mkdir_one(const char *prefix
, const char *suffix
) {
560 _cleanup_free_
char *p
= NULL
;
562 p
= path_join(prefix
, suffix
);
563 if (mkdir(p
, 0700) < 0) {
565 return log_error_errno(errno
, "Failed to create \"%s\": %m", p
);
567 log_info("Created \"%s\".", p
);
572 static const char *const esp_subdirs
[] = {
577 /* Note that "/loader/entries" is not listed here, since it should be placed in $BOOT, which might
578 * not necessarily be the ESP */
582 static int create_esp_subdirs(const char *esp_path
) {
583 const char *const *i
;
586 STRV_FOREACH(i
, esp_subdirs
) {
587 r
= mkdir_one(esp_path
, *i
);
595 static int copy_one_file(const char *esp_path
, const char *name
, bool force
) {
599 p
= strjoina(BOOTLIBDIR
"/", name
);
600 q
= strjoina(esp_path
, "/EFI/systemd/", name
);
601 r
= copy_file_with_version_check(p
, q
, force
);
603 if (startswith(name
, "systemd-boot")) {
607 /* Create the EFI default boot loader name (specified for removable devices) */
608 v
= strjoina(esp_path
, "/EFI/BOOT/BOOT",
609 name
+ STRLEN("systemd-boot"));
610 ascii_strupper(strrchr(v
, '/') + 1);
612 k
= copy_file_with_version_check(p
, v
, force
);
620 static int install_binaries(const char *esp_path
, bool force
) {
622 _cleanup_closedir_
DIR *d
= NULL
;
625 d
= opendir(BOOTLIBDIR
);
627 return log_error_errno(errno
, "Failed to open \""BOOTLIBDIR
"\": %m
");
629 FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read
\""BOOTLIBDIR"\": %m
")) {
632 if (!endswith_no_case(de->d_name, ".efi
"))
635 k = copy_one_file(esp_path, de->d_name, force);
643 static bool same_entry(uint16_t id, const sd_id128_t uuid, const char *path) {
644 _cleanup_free_ char *opath = NULL;
648 r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
651 if (!sd_id128_equal(uuid, ouuid))
653 if (!streq_ptr(path, opath))
659 static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
660 _cleanup_free_ uint16_t *options = NULL;
663 n = efi_get_boot_options(&options);
667 /* find already existing systemd-boot entry */
668 for (i = 0; i < n; i++)
669 if (same_entry(options[i], uuid, path)) {
674 /* find free slot in the sorted BootXXXX variable list */
675 for (i = 0; i < n; i++)
676 if (i != options[i]) {
681 /* use the next one */
688 static int insert_into_order(uint16_t slot, bool first) {
689 _cleanup_free_ uint16_t *order = NULL;
693 n = efi_get_boot_order(&order);
695 /* no entry, add us */
696 return efi_set_boot_order(&slot, 1);
698 /* are we the first and only one? */
699 if (n == 1 && order[0] == slot)
702 /* are we already in the boot order? */
703 for (i = 0; i < n; i++) {
704 if (order[i] != slot)
707 /* we do not require to be the first one, all is fine */
711 /* move us to the first slot */
712 memmove(order + 1, order, i * sizeof(uint16_t));
714 return efi_set_boot_order(order, n);
718 t = reallocarray(order, n + 1, sizeof(uint16_t));
723 /* add us to the top or end of the list */
725 memmove(order + 1, order, n * sizeof(uint16_t));
730 return efi_set_boot_order(order, n + 1);
733 static int remove_from_order(uint16_t slot) {
734 _cleanup_free_ uint16_t *order = NULL;
737 n = efi_get_boot_order(&order);
741 for (i = 0; i < n; i++) {
742 if (order[i] != slot)
746 memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
747 return efi_set_boot_order(order, n - 1);
753 static int install_variables(const char *esp_path,
754 uint32_t part, uint64_t pstart, uint64_t psize,
755 sd_id128_t uuid, const char *path,
761 if (!is_efi_boot()) {
762 log_warning("Not booted with EFI
, skipping EFI variable setup
.");
766 p = strjoina(esp_path, path);
767 if (access(p, F_OK) < 0) {
771 return log_error_errno(errno, "Cannot access
\"%s
\": %m
", p);
774 r = find_slot(uuid, path, &slot);
776 return log_error_errno(r,
778 "Failed to access EFI variables
. Is the
\"efivarfs
\" filesystem mounted
?" :
779 "Failed to determine current boot order
: %m
");
781 if (first || r == 0) {
782 r = efi_add_boot_option(slot, "Linux Boot Manager
",
786 return log_error_errno(r, "Failed to create EFI Boot variable entry
: %m
");
788 log_info("Created EFI boot entry
\"Linux Boot Manager
\".");
791 return insert_into_order(slot, first);
794 static int remove_boot_efi(const char *esp_path) {
796 _cleanup_closedir_ DIR *d = NULL;
800 p = strjoina(esp_path, "/EFI
/BOOT
");
806 return log_error_errno(errno, "Failed to open directory
\"%s
\": %m
", p);
809 FOREACH_DIRENT(de, d, break) {
810 _cleanup_close_ int fd = -1;
811 _cleanup_free_ char *v = NULL;
813 if (!endswith_no_case(de->d_name, ".efi
"))
816 if (!startswith_no_case(de->d_name, "boot
"))
819 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
821 return log_error_errno(errno, "Failed to open
\"%s
/%s
\" for reading
: %m
", p, de->d_name);
823 r = get_file_version(fd, &v);
826 if (r > 0 && startswith(v, "systemd
-boot
")) {
827 r = unlinkat(dirfd(d), de->d_name, 0);
829 return log_error_errno(errno, "Failed to remove
\"%s
/%s
\": %m
", p, de->d_name);
831 log_info("Removed
\"%s
/%s
\".", p, de->d_name);
840 static int rmdir_one(const char *prefix, const char *suffix) {
843 p = strjoina(prefix, "/", suffix);
845 bool ignore = IN_SET(errno, ENOENT, ENOTEMPTY);
847 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, errno,
848 "Failed to remove directory
\"%s
\": %m
", p);
852 log_info("Removed
\"%s
\".", p);
857 static int remove_esp_subdirs(const char *esp_path) {
861 for (i = ELEMENTSOF(esp_subdirs)-1; i > 0; i--) {
864 q = rmdir_one(esp_path, esp_subdirs[i-1]);
872 static int remove_binaries(const char *esp_path) {
876 p = strjoina(esp_path, "/EFI
/systemd
");
877 r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
879 q = remove_boot_efi(esp_path);
886 static int remove_loader_config(const char *esp_path) {
891 p = strjoina(esp_path, "/loader
/loader
.conf
");
893 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, "Failed to unlink file
\"%s
\": %m
", p);
897 log_info("Removed
\"%s
\".", p);
902 static int remove_entries_directory(const char *dollar_boot_path) {
903 assert(dollar_boot_path);
905 return rmdir_one(dollar_boot_path, "/loader
/entries
");
908 static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
915 r = find_slot(uuid, path, &slot);
919 r = efi_remove_boot_option(slot);
924 return remove_from_order(slot);
929 static int install_loader_config(const char *esp_path, sd_id128_t machine_id) {
930 char machine_string[SD_ID128_STRING_MAX];
931 _cleanup_(unlink_and_freep) char *t = NULL;
932 _cleanup_fclose_ FILE *f = NULL;
936 p = strjoina(esp_path, "/loader
/loader
.conf
");
937 if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
940 fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
942 return log_error_errno(fd, "Failed to open
\"%s
\" for writing
: %m
", p);
950 fprintf(f, "#timeout 3\n"
951 "#console-mode keep\n"
952 "default %s-*\n", sd_id128_to_string(machine_id, machine_string));
954 r = fflush_sync_and_check(f);
956 return log_error_errno(r, "Failed to write \"%s\": %m", p);
958 r = link_tmpfile(fd, t, p);
960 return 0; /* Silently skip creation if the file exists now (recheck) */
962 return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
968 static int install_entries_directories(const char *dollar_boot_path, sd_id128_t machine_id) {
970 char buf[SD_ID128_STRING_MAX];
972 assert(dollar_boot_path);
974 /* Both /loader/entries and the entry directories themselves should be located on the same
975 * partition. Also create the parent directory for entry directories, so that kernel-install
976 * knows where to put them. */
978 r = mkdir_one(dollar_boot_path, "loader/entries");
982 return mkdir_one(dollar_boot_path, sd_id128_to_string(machine_id, buf));
985 static int help(int argc, char *argv[], void *userdata) {
986 _cleanup_free_ char *link = NULL;
989 r = terminal_urlify_man("bootctl", "1", &link);
993 printf("%s [COMMAND] [OPTIONS...]\n\n"
994 "Install, update or remove the systemd-boot EFI boot manager.\n\n"
995 " -h --help Show this help\n"
996 " --version Print version\n"
997 " --esp-path=PATH Path to the EFI System Partition (ESP)\n"
998 " --boot-path=PATH Path to the $BOOT partition\n"
999 " -p --print-esp-path Print path to the EFI System Partition\n"
1000 " -x --print-boot-path Print path to the $BOOT partition\n"
1001 " --no-variables Don't touch EFI variables\n"
1002 " --no-pager Do not pipe output into a pager\n"
1003 "\nBoot Loader Commands:\n"
1004 " status Show status of installed systemd-boot and EFI variables\n"
1005 " install Install systemd-boot to the ESP and EFI variables\n"
1006 " update Update systemd-boot in the ESP and EFI variables\n"
1007 " remove Remove systemd-boot from the ESP and EFI variables\n"
1008 "\nBoot Loader Entries Commands:\n"
1009 " list List boot loader entries\n"
1010 " set-default ID Set default boot loader entry\n"
1011 " set-oneshot ID Set default boot loader entry, for next boot only\n"
1012 "\nSee the %s for details.\n"
1013 , program_invocation_short_name
1019 static int parse_argv(int argc, char *argv[]) {
1021 ARG_ESP_PATH = 0x100,
1028 static const struct option options[] = {
1029 { "help", no_argument, NULL, 'h' },
1030 { "version", no_argument, NULL, ARG_VERSION },
1031 { "esp-path", required_argument, NULL, ARG_ESP_PATH },
1032 { "path", required_argument, NULL, ARG_ESP_PATH }, /* Compatibility alias */
1033 { "boot-path", required_argument, NULL, ARG_BOOT_PATH },
1034 { "print-esp-path", no_argument, NULL, 'p' },
1035 { "print-path", no_argument, NULL, 'p' }, /* Compatibility alias */
1036 { "print-boot-path", no_argument, NULL, 'x' },
1037 { "no-variables", no_argument, NULL, ARG_NO_VARIABLES },
1038 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1047 while ((c = getopt_long(argc, argv, "hpx", options, NULL)) >= 0)
1051 help(0, NULL, NULL);
1058 r = free_and_strdup(&arg_esp_path, optarg);
1064 r = free_and_strdup(&arg_xbootldr_path, optarg);
1070 if (arg_print_dollar_boot_path)
1071 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1072 "--print-boot-path/-x cannot be combined with --print-esp-path/-p");
1073 arg_print_esp_path = true;
1077 if (arg_print_esp_path)
1078 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1079 "--print-boot-path/-x cannot be combined with --print-esp-path/-p");
1080 arg_print_dollar_boot_path = true;
1083 case ARG_NO_VARIABLES:
1084 arg_touch_variables = false;
1088 arg_pager_flags |= PAGER_DISABLE;
1095 assert_not_reached("Unknown option");
1101 static void read_loader_efi_var(const char *name, char **var) {
1104 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var);
1105 if (r < 0 && r != -ENOENT)
1106 log_warning_errno(r, "Failed to read EFI variable %s: %m", name);
1109 static int verb_status(int argc, char *argv[], void *userdata) {
1110 sd_id128_t esp_uuid = SD_ID128_NULL, xbootldr_uuid = SD_ID128_NULL;
1113 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &esp_uuid);
1114 if (arg_print_esp_path) {
1115 if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only
1116 * error the
find_esp_and_warn() won
't log on its own) */
1117 return log_error_errno(r, "Failed to determine ESP location: %m");
1124 r = acquire_xbootldr(geteuid() != 0, &xbootldr_uuid);
1125 if (arg_print_dollar_boot_path) {
1127 return log_error_errno(r, "Failed to determine XBOOTLDR location: %m");
1131 puts(arg_dollar_boot_path());
1134 if (arg_print_esp_path || arg_print_dollar_boot_path)
1137 r = 0; /* If we couldn't determine the path
, then don
't consider that a problem from here on, just show what we
1140 (void) pager_open(arg_pager_flags);
1142 if (is_efi_boot()) {
1143 static const struct {
1147 { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" },
1148 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" },
1149 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" },
1150 { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" },
1151 { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" },
1154 _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL;
1155 sd_id128_t loader_part_uuid = SD_ID128_NULL;
1156 uint64_t loader_features = 0;
1159 read_loader_efi_var("LoaderFirmwareType", &fw_type);
1160 read_loader_efi_var("LoaderFirmwareInfo", &fw_info);
1161 read_loader_efi_var("LoaderInfo", &loader);
1162 read_loader_efi_var("StubInfo", &stub);
1163 read_loader_efi_var("LoaderImageIdentifier", &loader_path);
1164 (void) efi_loader_get_features(&loader_features);
1167 efi_tilt_backslashes(loader_path);
1169 k = efi_loader_get_device_part_uuid(&loader_part_uuid);
1170 if (k < 0 && k != -ENOENT)
1171 r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m");
1173 printf("System:\n");
1174 printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal());
1175 printf(" Secure Boot: %sd\n", enable_disable(is_efi_secure_boot()));
1176 printf(" Setup Mode: %s\n", is_efi_secure_boot_setup_mode() ? "setup" : "user");
1179 printf("Current Boot Loader:\n");
1180 printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal());
1182 for (i = 0; i < ELEMENTSOF(flags); i++) {
1185 printf(" Features: ");
1189 if (FLAGS_SET(loader_features, flags[i].flag))
1190 printf("%s%s%s %s\n", ansi_highlight_green(), special_glyph(SPECIAL_GLYPH_CHECK_MARK), ansi_normal(), flags[i].name);
1192 printf("%s%s%s %s\n", ansi_highlight_red(), special_glyph(SPECIAL_GLYPH_CROSS_MARK), ansi_normal(), flags[i].name);
1196 printf(" Stub: %s\n", stub);
1197 if (!sd_id128_is_null(loader_part_uuid))
1198 printf(" ESP: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
1199 SD_ID128_FORMAT_VAL(loader_part_uuid));
1201 printf(" ESP: n/a\n");
1202 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), strna(loader_path));
1205 printf("System:\n Not booted with EFI\n\n");
1208 k = status_binaries(arg_esp_path, esp_uuid);
1213 if (is_efi_boot()) {
1214 k = status_variables();
1219 if (arg_esp_path || arg_xbootldr_path) {
1220 k = status_entries(arg_esp_path, esp_uuid, arg_xbootldr_path, xbootldr_uuid);
1228 static int verb_list(int argc, char *argv[], void *userdata) {
1229 _cleanup_(boot_config_free) BootConfig config = {};
1232 /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two things: turn
1233 * off logging about access errors and turn off potentially privileged device probing. Here we're interested in
1234 * the latter but
not the former
, hence request the mode
, and log about EACCES
. */
1236 r
= acquire_esp(geteuid() != 0, NULL
, NULL
, NULL
, NULL
);
1237 if (r
== -EACCES
) /* We really need the ESP path for this call, hence also log about access errors */
1238 return log_error_errno(r
, "Failed to determine ESP: %m");
1242 r
= acquire_xbootldr(geteuid() != 0, NULL
);
1244 return log_error_errno(r
, "Failed to determine XBOOTLDR partition: %m");
1248 r
= boot_entries_load_config(arg_esp_path
, arg_xbootldr_path
, &config
);
1252 (void) boot_entries_augment_from_loader(&config
, false);
1254 if (config
.n_entries
== 0)
1255 log_info("No boot loader entries found.");
1259 (void) pager_open(arg_pager_flags
);
1261 printf("Boot Loader Entries:\n");
1263 for (n
= 0; n
< config
.n_entries
; n
++) {
1264 r
= boot_entry_show(config
.entries
+ n
, n
== (size_t) config
.default_entry
);
1268 if (n
+1 < config
.n_entries
)
1276 static int sync_everything(void) {
1280 k
= syncfs_path(AT_FDCWD
, arg_esp_path
);
1282 ret
= log_error_errno(k
, "Failed to synchronize the ESP '%s': %m", arg_esp_path
);
1285 if (arg_xbootldr_path
) {
1286 k
= syncfs_path(AT_FDCWD
, arg_xbootldr_path
);
1288 ret
= log_error_errno(k
, "Failed to synchronize $BOOT '%s': %m", arg_xbootldr_path
);
1294 static int verb_install(int argc
, char *argv
[], void *userdata
) {
1295 sd_id128_t uuid
= SD_ID128_NULL
;
1296 uint64_t pstart
= 0, psize
= 0;
1298 sd_id128_t machine_id
;
1302 r
= acquire_esp(false, &part
, &pstart
, &psize
, &uuid
);
1306 r
= acquire_xbootldr(false, NULL
);
1310 r
= sd_id128_get_machine(&machine_id
);
1312 return log_error_errno(r
, "Failed to get machine id: %m");
1314 install
= streq(argv
[0], "install");
1316 RUN_WITH_UMASK(0002) {
1318 /* Don't create any of these directories when we are just updating. When we update
1319 * we'll drop-in our files (unless there are newer ones already), but we won't create
1320 * the directories for them in the first place. */
1321 r
= create_esp_subdirs(arg_esp_path
);
1326 r
= install_binaries(arg_esp_path
, install
);
1331 r
= install_loader_config(arg_esp_path
, machine_id
);
1335 r
= install_entries_directories(arg_dollar_boot_path(), machine_id
);
1341 (void) sync_everything();
1343 if (arg_touch_variables
)
1344 r
= install_variables(arg_esp_path
,
1345 part
, pstart
, psize
, uuid
,
1346 "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME
".efi",
1352 static int verb_remove(int argc
, char *argv
[], void *userdata
) {
1353 sd_id128_t uuid
= SD_ID128_NULL
;
1356 r
= acquire_esp(false, NULL
, NULL
, NULL
, &uuid
);
1360 r
= acquire_xbootldr(false, NULL
);
1364 r
= remove_binaries(arg_esp_path
);
1366 q
= remove_loader_config(arg_esp_path
);
1367 if (q
< 0 && r
>= 0)
1370 q
= remove_entries_directory(arg_dollar_boot_path());
1371 if (q
< 0 && r
>= 0)
1374 q
= remove_esp_subdirs(arg_esp_path
);
1375 if (q
< 0 && r
>= 0)
1378 (void) sync_everything();
1380 if (arg_touch_variables
) {
1381 q
= remove_variables(uuid
, "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME
".efi", true);
1382 if (q
< 0 && r
>= 0)
1389 static int verb_set_default(int argc
, char *argv
[], void *userdata
) {
1394 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1395 "Not booted with UEFI.");
1397 if (access("/sys/firmware/efi/efivars/LoaderInfo-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK
) < 0) {
1398 if (errno
== ENOENT
) {
1399 log_error_errno(errno
, "Not booted with a supported boot loader.");
1403 return log_error_errno(errno
, "Failed to detect whether boot loader supports '%s' operation: %m", argv
[0]);
1406 if (detect_container() > 0)
1407 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1408 "'%s' operation not supported in a container.",
1411 if (!arg_touch_variables
)
1412 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1413 "'%s' operation cannot be combined with --touch-variables=no.",
1416 name
= streq(argv
[0], "set-default") ? "LoaderEntryDefault" : "LoaderEntryOneShot";
1418 if (isempty(argv
[1])) {
1419 r
= efi_set_variable(EFI_VENDOR_LOADER
, name
, NULL
, 0);
1420 if (r
< 0 && r
!= -ENOENT
)
1421 return log_error_errno(r
, "Failed to remove EFI variale: %m");
1423 _cleanup_free_ char16_t
*encoded
= NULL
;
1425 encoded
= utf8_to_utf16(argv
[1], strlen(argv
[1]));
1429 r
= efi_set_variable(EFI_VENDOR_LOADER
, name
, encoded
, char16_strlen(encoded
) * 2 + 2);
1431 return log_error_errno(r
, "Failed to update EFI variable: %m");
1437 static int bootctl_main(int argc
, char *argv
[]) {
1438 static const Verb verbs
[] = {
1439 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1440 { "status", VERB_ANY
, 1, VERB_DEFAULT
, verb_status
},
1441 { "install", VERB_ANY
, 1, 0, verb_install
},
1442 { "update", VERB_ANY
, 1, 0, verb_install
},
1443 { "remove", VERB_ANY
, 1, 0, verb_remove
},
1444 { "list", VERB_ANY
, 1, 0, verb_list
},
1445 { "set-default", 2, 2, 0, verb_set_default
},
1446 { "set-oneshot", 2, 2, 0, verb_set_default
},
1450 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1453 static int run(int argc
, char *argv
[]) {
1456 log_parse_environment();
1459 /* If we run in a container, automatically turn off EFI file system access */
1460 if (detect_container() > 0)
1461 arg_touch_variables
= false;
1463 r
= parse_argv(argc
, argv
);
1467 return bootctl_main(argc
, argv
);
1470 DEFINE_MAIN_FUNCTION(run
);