]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/boot/bootctl.c
Merge pull request #12753 from jrouleau/fix/hibernate-resume-timeout
[thirdparty/systemd.git] / src / boot / bootctl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
7b4d7cc0 2
6b5cf3ea 3#include <blkid.h>
0974a682 4#include <ctype.h>
3f6fd1ba
LP
5#include <dirent.h>
6#include <errno.h>
0974a682 7#include <ftw.h>
3f6fd1ba
LP
8#include <getopt.h>
9#include <limits.h>
5fa6c13c 10#include <linux/magic.h>
0974a682 11#include <stdbool.h>
3f6fd1ba
LP
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/mman.h>
16#include <sys/stat.h>
17#include <sys/statfs.h>
18#include <unistd.h>
7b4d7cc0 19
dccca82b
LP
20#include "sd-id128.h"
21
b5efdb8a 22#include "alloc-util.h"
3f6fd1ba 23#include "blkid-util.h"
7e87c7d9 24#include "bootspec.h"
175d308c 25#include "copy.h"
e41256dc 26#include "dirent-util.h"
0974a682 27#include "efivars.h"
3ffd4af2 28#include "fd-util.h"
0d39fa9c 29#include "fileio.h"
175d308c 30#include "fs-util.h"
8752c575 31#include "locale-util.h"
608f8ec9 32#include "main-func.h"
57db6f18 33#include "pager.h"
2f2c539c 34#include "parse-util.h"
294bf0c3 35#include "pretty-print.h"
c6878637 36#include "rm-rf.h"
175d308c 37#include "stat-util.h"
341890de 38#include "stdio-util.h"
07630cea 39#include "string-util.h"
2f2c539c 40#include "strv.h"
7e87c7d9 41#include "terminal-util.h"
e4de7287 42#include "tmpfile-util.h"
2f2c539c 43#include "umask-util.h"
d88c96ff 44#include "utf8.h"
3f6fd1ba 45#include "util.h"
2f2c539c
LP
46#include "verbs.h"
47#include "virt.h"
7b4d7cc0 48
fbf45d22
LP
49static char *arg_esp_path = NULL;
50static char *arg_xbootldr_path = NULL;
51static bool arg_print_esp_path = false;
52static bool arg_print_dollar_boot_path = false;
25579a43 53static bool arg_touch_variables = true;
0221d68a 54static PagerFlags arg_pager_flags = 0;
25579a43 55
fbf45d22
LP
56STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
57STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
58
59static 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;
62}
608f8ec9 63
5caa3167
LP
64static int acquire_esp(
65 bool unprivileged_mode,
66 uint32_t *ret_part,
67 uint64_t *ret_pstart,
68 uint64_t *ret_psize,
69 sd_id128_t *ret_uuid) {
70
71 char *np;
2f2c539c
LP
72 int r;
73
fbf45d22
LP
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
78 * this). */
5caa3167 79
fbf45d22 80 r = find_esp_and_warn(arg_esp_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid);
5caa3167 81 if (r == -ENOKEY)
af918182 82 return log_error_errno(r,
5caa3167 83 "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n"
fbf45d22 84 "Alternatively, use --esp-path= to specify path to mount point.");
5caa3167
LP
85 if (r < 0)
86 return r;
87
fbf45d22
LP
88 free_and_replace(arg_esp_path, np);
89 log_debug("Using EFI System Partition at %s.", arg_esp_path);
90
91 return 1;
92}
0974a682 93
fbf45d22
LP
94static int acquire_xbootldr(bool unprivileged_mode, sd_id128_t *ret_uuid) {
95 char *np;
96 int r;
5caa3167 97
fbf45d22
LP
98 r = find_xbootldr_and_warn(arg_xbootldr_path, unprivileged_mode, &np, ret_uuid);
99 if (r == -ENOKEY) {
100 log_debug_errno(r, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
101 if (ret_uuid)
102 *ret_uuid = SD_ID128_NULL;
103 return 0;
104 }
105 if (r < 0)
106 return r;
107
108 free_and_replace(arg_xbootldr_path, np);
109 log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path);
110
111 return 1;
7b4d7cc0
KS
112}
113
e7dd673d 114/* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */
d3226d77 115static int get_file_version(int fd, char **v) {
0974a682
KS
116 struct stat st;
117 char *buf;
118 const char *s, *e;
119 char *x = NULL;
120 int r = 0;
7b4d7cc0 121
d3226d77 122 assert(fd >= 0);
0974a682 123 assert(v);
7b4d7cc0 124
d3226d77 125 if (fstat(fd, &st) < 0)
db6d9fae 126 return log_error_errno(errno, "Failed to stat EFI binary: %m");
7b4d7cc0 127
c4ba5b51
LP
128 r = stat_verify_regular(&st);
129 if (r < 0)
130 return log_error_errno(errno, "EFI binary is not a regular file: %m");
131
db6d9fae
LP
132 if (st.st_size < 27) {
133 *v = NULL;
0974a682 134 return 0;
db6d9fae 135 }
eb9da376 136
d3226d77 137 buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
0974a682 138 if (buf == MAP_FAILED)
db6d9fae 139 return log_error_errno(errno, "Failed to memory map EFI binary: %m");
7b4d7cc0 140
0974a682
KS
141 s = memmem(buf, st.st_size - 8, "#### LoaderInfo: ", 17);
142 if (!s)
143 goto finish;
144 s += 17;
7b4d7cc0 145
0974a682
KS
146 e = memmem(s, st.st_size - (s - buf), " ####", 5);
147 if (!e || e - s < 3) {
78d5d4ed 148 r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Malformed version string.");
0974a682
KS
149 goto finish;
150 }
7b4d7cc0 151
0974a682
KS
152 x = strndup(s, e - s);
153 if (!x) {
d3226d77 154 r = log_oom();
0974a682
KS
155 goto finish;
156 }
157 r = 1;
7b4d7cc0 158
0974a682 159finish:
db6d9fae 160 (void) munmap(buf, st.st_size);
0974a682
KS
161 *v = x;
162 return r;
163}
7b4d7cc0 164
0974a682 165static int enumerate_binaries(const char *esp_path, const char *path, const char *prefix) {
d3226d77 166 _cleanup_closedir_ DIR *d = NULL;
0974a682 167 struct dirent *de;
fbf45d22
LP
168 int c = 0, r;
169 char *p;
170
171 assert(esp_path);
172 assert(path);
0974a682 173
d3226d77 174 p = strjoina(esp_path, "/", path);
0974a682
KS
175 d = opendir(p);
176 if (!d) {
d3226d77
ZJS
177 if (errno == ENOENT)
178 return 0;
7b4d7cc0 179
d3226d77 180 return log_error_errno(errno, "Failed to read \"%s\": %m", p);
0974a682
KS
181 }
182
e41256dc 183 FOREACH_DIRENT(de, d, break) {
d3226d77 184 _cleanup_free_ char *v = NULL;
fbf45d22 185 _cleanup_close_ int fd = -1;
0974a682 186
d3226d77 187 if (!endswith_no_case(de->d_name, ".efi"))
0974a682
KS
188 continue;
189
d3226d77 190 if (prefix && !startswith_no_case(de->d_name, prefix))
0974a682
KS
191 continue;
192
d3226d77
ZJS
193 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
194 if (fd < 0)
195 return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name);
0974a682 196
d3226d77 197 r = get_file_version(fd, &v);
0974a682 198 if (r < 0)
d3226d77 199 return r;
0974a682 200 if (r > 0)
9a6f746f 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());
0974a682 202 else
9a6f746f 203 printf(" File: %s/%s/%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path, de->d_name);
fbf45d22 204
0974a682 205 c++;
0974a682
KS
206 }
207
d3226d77 208 return c;
7b4d7cc0
KS
209}
210
0974a682
KS
211static int status_binaries(const char *esp_path, sd_id128_t partition) {
212 int r;
213
7fd66f3c 214 printf("Available Boot Loaders on ESP:\n");
0974a682 215
cd2d4c7f
YW
216 if (!esp_path) {
217 printf(" ESP: Cannot find or access mount point of ESP.\n\n");
218 return -ENOENT;
219 }
220
221 printf(" ESP: %s", esp_path);
222 if (!sd_id128_is_null(partition))
bd44566c 223 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")", SD_ID128_FORMAT_VAL(partition));
cd2d4c7f 224 printf("\n");
0974a682
KS
225
226 r = enumerate_binaries(esp_path, "EFI/systemd", NULL);
fbf45d22 227 if (r < 0)
882b3bd6 228 goto finish;
0974a682 229 if (r == 0)
48184e43 230 log_info("systemd-boot not installed in ESP.");
0974a682 231
00f69504 232 r = enumerate_binaries(esp_path, "EFI/BOOT", "boot");
fbf45d22 233 if (r < 0)
882b3bd6 234 goto finish;
0974a682 235 if (r == 0)
48184e43 236 log_info("No default/fallback boot loader installed in ESP.");
0974a682 237
882b3bd6 238 r = 0;
c11ae0ba 239
882b3bd6
LP
240finish:
241 printf("\n");
242 return r;
0974a682
KS
243}
244
245static int print_efi_option(uint16_t id, bool in_order) {
7cb0f263
TA
246 _cleanup_free_ char *title = NULL;
247 _cleanup_free_ char *path = NULL;
0974a682
KS
248 sd_id128_t partition;
249 bool active;
250 int r = 0;
251
252 r = efi_get_boot_option(id, &title, &partition, &path, &active);
253 if (r < 0)
7cb0f263 254 return r;
7b4d7cc0 255
0974a682 256 /* print only configured entries with partition information */
3bbaff3e 257 if (!path || sd_id128_is_null(partition))
0974a682
KS
258 return 0;
259
260 efi_tilt_backslashes(path);
261
ba857253 262 printf(" Title: %s%s%s\n", ansi_highlight(), strna(title), ansi_normal());
0974a682
KS
263 printf(" ID: 0x%04X\n", id);
264 printf(" Status: %sactive%s\n", active ? "" : "in", in_order ? ", boot-order" : "");
bd44566c
ZJS
265 printf(" Partition: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
266 SD_ID128_FORMAT_VAL(partition));
9a6f746f 267 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path);
0974a682
KS
268 printf("\n");
269
7cb0f263 270 return 0;
0974a682
KS
271}
272
273static int status_variables(void) {
d3226d77 274 _cleanup_free_ uint16_t *options = NULL, *order = NULL;
a36b411e 275 int n_options, n_order, i;
0974a682 276
0974a682 277 n_options = efi_get_boot_options(&options);
d3226d77 278 if (n_options == -ENOENT)
f939cff7
LP
279 return log_error_errno(n_options,
280 "Failed to access EFI variables, efivarfs"
d3226d77 281 " needs to be available at /sys/firmware/efi/efivars/.");
f939cff7 282 if (n_options < 0)
d3226d77 283 return log_error_errno(n_options, "Failed to read EFI boot entries: %m");
0974a682 284
0974a682 285 n_order = efi_get_boot_order(&order);
d3226d77 286 if (n_order == -ENOENT)
0974a682 287 n_order = 0;
d3226d77 288 else if (n_order < 0)
7709ef3a 289 return log_error_errno(n_order, "Failed to read EFI boot order: %m");
0974a682
KS
290
291 /* print entries in BootOrder first */
7fd66f3c 292 printf("Boot Loaders Listed in EFI Variables:\n");
0974a682
KS
293 for (i = 0; i < n_order; i++)
294 print_efi_option(order[i], true);
295
296 /* print remaining entries */
297 for (i = 0; i < n_options; i++) {
298 int j;
0974a682
KS
299
300 for (j = 0; j < n_order; j++)
d3226d77 301 if (options[i] == order[j])
a908cf0a 302 goto next_option;
0974a682
KS
303
304 print_efi_option(options[i], false);
a908cf0a
MM
305
306 next_option:
307 continue;
0974a682
KS
308 }
309
d3226d77 310 return 0;
0974a682
KS
311}
312
44e6a5ef
ZJS
313static int boot_entry_file_check(const char *root, const char *p) {
314 _cleanup_free_ char *path;
315
316 path = path_join(root, p);
317 if (!path)
318 return log_oom();
319
320 if (access(path, F_OK) < 0)
321 return -errno;
322
323 return 0;
324}
325
d3eb6072 326static void boot_entry_file_list(const char *field, const char *root, const char *p, int *ret_status) {
44e6a5ef
ZJS
327 int status = boot_entry_file_check(root, p);
328
405b104d 329 printf("%13s%s ", strempty(field), field ? ":" : " ");
44e6a5ef
ZJS
330 if (status < 0) {
331 errno = -status;
332 printf("%s%s%s (%m)\n", ansi_highlight_red(), p, ansi_normal());
333 } else
334 printf("%s\n", p);
d3eb6072
ZJS
335
336 if (*ret_status == 0 && status < 0)
337 *ret_status = status;
44e6a5ef
ZJS
338}
339
20a28174 340static int boot_entry_show(const BootEntry *e, bool show_as_default) {
d3eb6072
ZJS
341 int status = 0;
342
343 /* Returns 0 on success, negative on processing error, and positive if something is wrong with the
344 boot entry itself. */
345
20a28174
LP
346 assert(e);
347
ce4c4f81
ZJS
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());
20a28174
LP
351
352 if (e->id)
353 printf(" id: %s\n", e->id);
cce9457c
ZJS
354 if (e->path) {
355 _cleanup_free_ char *link = NULL;
356
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);
361
362 printf(" source: %s\n", link ?: e->path);
363 }
20a28174
LP
364 if (e->version)
365 printf(" version: %s\n", e->version);
366 if (e->machine_id)
367 printf(" machine-id: %s\n", e->machine_id);
368 if (e->architecture)
369 printf(" architecture: %s\n", e->architecture);
370 if (e->kernel)
d3eb6072 371 boot_entry_file_list("linux", e->root, e->kernel, &status);
20a28174 372
44e6a5ef
ZJS
373 char **s;
374 STRV_FOREACH(s, e->initrd)
375 boot_entry_file_list(s == e->initrd ? "initrd" : NULL,
376 e->root,
d3eb6072
ZJS
377 *s,
378 &status);
20a28174
LP
379 if (!strv_isempty(e->options)) {
380 _cleanup_free_ char *t;
381
382 t = strv_join(e->options, " ");
383 if (!t)
384 return log_oom();
385
386 printf(" options: %s\n", t);
387 }
388 if (e->device_tree)
d3eb6072 389 boot_entry_file_list("devicetree", e->root, e->device_tree, &status);
20a28174 390
d3eb6072 391 return -status;
20a28174
LP
392}
393
81fed855
LP
394static 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) {
399
7e87c7d9 400 _cleanup_(boot_config_free) BootConfig config = {};
81fed855
LP
401 sd_id128_t dollar_boot_partition_uuid;
402 const char *dollar_boot_path;
a099e035 403 int r;
7e87c7d9 404
81fed855
LP
405 assert(esp_path || xbootldr_path);
406
407 if (xbootldr_path) {
408 dollar_boot_path = xbootldr_path;
409 dollar_boot_partition_uuid = xbootldr_partition_uuid;
410 } else {
411 dollar_boot_path = esp_path;
412 dollar_boot_partition_uuid = esp_partition_uuid;
413 }
414
415 printf("Boot Loader Entries:\n"
416 " $BOOT: %s", dollar_boot_path);
417 if (!sd_id128_is_null(dollar_boot_partition_uuid))
bd44566c
ZJS
418 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")",
419 SD_ID128_FORMAT_VAL(dollar_boot_partition_uuid));
81fed855
LP
420 printf("\n\n");
421
fbf45d22 422 r = boot_entries_load_config(esp_path, xbootldr_path, &config);
7e87c7d9 423 if (r < 0)
21f7a622 424 return r;
7e87c7d9
ZJS
425
426 if (config.default_entry < 0)
a099e035 427 printf("%zu entries, no entry could be determined as default.\n", config.n_entries);
7e87c7d9 428 else {
7fd66f3c 429 printf("Default Boot Loader Entry:\n");
a099e035 430
d3eb6072
ZJS
431 r = boot_entry_show(config.entries + config.default_entry, false);
432 if (r > 0)
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");
7e87c7d9
ZJS
436 }
437
438 return 0;
439}
440
0974a682
KS
441static int compare_product(const char *a, const char *b) {
442 size_t x, y;
443
444 assert(a);
445 assert(b);
446
447 x = strcspn(a, " ");
448 y = strcspn(b, " ");
449 if (x != y)
450 return x < y ? -1 : x > y ? 1 : 0;
451
452 return strncmp(a, b, x);
453}
454
455static int compare_version(const char *a, const char *b) {
456 assert(a);
457 assert(b);
458
459 a += strcspn(a, " ");
460 a += strspn(a, " ");
461 b += strcspn(b, " ");
462 b += strspn(b, " ");
463
464 return strverscmp(a, b);
465}
466
175d308c 467static int version_check(int fd_from, const char *from, int fd_to, const char *to) {
d3226d77 468 _cleanup_free_ char *a = NULL, *b = NULL;
0974a682
KS
469 int r;
470
175d308c 471 assert(fd_from >= 0);
0974a682 472 assert(from);
175d308c 473 assert(fd_to >= 0);
0974a682
KS
474 assert(to);
475
175d308c 476 r = get_file_version(fd_from, &a);
0974a682 477 if (r < 0)
d3226d77 478 return r;
baaa35ad
ZJS
479 if (r == 0)
480 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
481 "Source file \"%s\" does not carry version information!",
482 from);
0974a682 483
175d308c 484 r = get_file_version(fd_to, &b);
0974a682 485 if (r < 0)
d3226d77 486 return r;
baaa35ad
ZJS
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.",
490 to);
0974a682 491
78d5d4ed
LP
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);
0974a682 494
d3226d77 495 return 0;
0974a682
KS
496}
497
175d308c
LP
498static 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;
0974a682 501 int r;
0974a682 502
175d308c
LP
503 fd_from = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
504 if (fd_from < 0)
d3226d77 505 return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", from);
0974a682
KS
506
507 if (!force) {
175d308c
LP
508 fd_to = open(to, O_RDONLY|O_CLOEXEC|O_NOCTTY);
509 if (fd_to < 0) {
510 if (errno != -ENOENT)
511 return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", to);
512 } else {
513 r = version_check(fd_from, from, fd_to, to);
514 if (r < 0)
515 return r;
516
517 if (lseek(fd_from, 0, SEEK_SET) == (off_t) -1)
6719ca72 518 return log_error_errno(errno, "Failed to seek in \"%s\": %m", from);
175d308c
LP
519
520 fd_to = safe_close(fd_to);
0974a682 521 }
175d308c 522 }
d3226d77 523
175d308c
LP
524 r = tempfn_random(to, NULL, &t);
525 if (r < 0)
526 return log_oom();
0974a682 527
175d308c
LP
528 RUN_WITH_UMASK(0000) {
529 fd_to = open(t, O_WRONLY|O_CREAT|O_CLOEXEC|O_EXCL|O_NOFOLLOW, 0644);
530 if (fd_to < 0)
531 return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", t);
0974a682
KS
532 }
533
175d308c 534 r = copy_bytes(fd_from, fd_to, (uint64_t) -1, COPY_REFLINK);
0974a682 535 if (r < 0) {
0675e94a
AJ
536 (void) unlink(t);
537 return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
0974a682
KS
538 }
539
adc6f43b 540 (void) copy_times(fd_from, fd_to, 0);
0974a682 541
8ac2f74f 542 if (fsync(fd_to) < 0) {
0675e94a
AJ
543 (void) unlink_noerrno(t);
544 return log_error_errno(errno, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
545 }
546
8ac2f74f
LP
547 (void) fsync_directory_of_file(fd_to);
548
b1c05b98 549 if (renameat(AT_FDCWD, t, AT_FDCWD, to) < 0) {
175d308c
LP
550 (void) unlink_noerrno(t);
551 return log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", t, to);
0974a682
KS
552 }
553
d3226d77 554 log_info("Copied \"%s\" to \"%s\".", from, to);
0974a682 555
175d308c 556 return 0;
0974a682
KS
557}
558
0974a682 559static int mkdir_one(const char *prefix, const char *suffix) {
e2600fd5 560 _cleanup_free_ char *p = NULL;
0974a682 561
e2600fd5 562 p = path_join(prefix, suffix);
0974a682 563 if (mkdir(p, 0700) < 0) {
d3226d77
ZJS
564 if (errno != EEXIST)
565 return log_error_errno(errno, "Failed to create \"%s\": %m", p);
0974a682 566 } else
d3226d77 567 log_info("Created \"%s\".", p);
7b4d7cc0 568
0974a682
KS
569 return 0;
570}
571
fbf45d22 572static const char *const esp_subdirs[] = {
d3226d77
ZJS
573 "EFI",
574 "EFI/systemd",
00f69504 575 "EFI/BOOT",
d3226d77 576 "loader",
fbf45d22
LP
577 /* Note that "/loader/entries" is not listed here, since it should be placed in $BOOT, which might
578 * not necessarily be the ESP */
9ee051b9 579 NULL
d3226d77
ZJS
580};
581
fbf45d22
LP
582static int create_esp_subdirs(const char *esp_path) {
583 const char *const *i;
0974a682
KS
584 int r;
585
fbf45d22 586 STRV_FOREACH(i, esp_subdirs) {
181ccb43 587 r = mkdir_one(esp_path, *i);
d3226d77
ZJS
588 if (r < 0)
589 return r;
590 }
7b4d7cc0 591
7b4d7cc0 592 return 0;
7b4d7cc0
KS
593}
594
0974a682 595static int copy_one_file(const char *esp_path, const char *name, bool force) {
d3226d77 596 char *p, *q;
0974a682
KS
597 int r;
598
d3226d77
ZJS
599 p = strjoina(BOOTLIBDIR "/", name);
600 q = strjoina(esp_path, "/EFI/systemd/", name);
175d308c 601 r = copy_file_with_version_check(p, q, force);
0974a682 602
e7dd673d 603 if (startswith(name, "systemd-boot")) {
0974a682 604 int k;
d3226d77 605 char *v;
0974a682
KS
606
607 /* Create the EFI default boot loader name (specified for removable devices) */
fbd0b64f
LP
608 v = strjoina(esp_path, "/EFI/BOOT/BOOT",
609 name + STRLEN("systemd-boot"));
846b8fc3 610 ascii_strupper(strrchr(v, '/') + 1);
0974a682 611
175d308c 612 k = copy_file_with_version_check(p, v, force);
0974a682 613 if (k < 0 && r == 0)
d3226d77 614 r = k;
0974a682
KS
615 }
616
617 return r;
7b4d7cc0
KS
618}
619
0974a682
KS
620static int install_binaries(const char *esp_path, bool force) {
621 struct dirent *de;
d3226d77 622 _cleanup_closedir_ DIR *d = NULL;
0974a682
KS
623 int r = 0;
624
e7dd673d 625 d = opendir(BOOTLIBDIR);
d3226d77
ZJS
626 if (!d)
627 return log_error_errno(errno, "Failed to open \""BOOTLIBDIR"\": %m");
0974a682 628
0d73a816 629 FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \""BOOTLIBDIR"\": %m")) {
0974a682
KS
630 int k;
631
d3226d77 632 if (!endswith_no_case(de->d_name, ".efi"))
0974a682
KS
633 continue;
634
635 k = copy_one_file(esp_path, de->d_name, force);
636 if (k < 0 && r == 0)
637 r = k;
638 }
639
0974a682 640 return r;
7b4d7cc0
KS
641}
642
0974a682 643static bool same_entry(uint16_t id, const sd_id128_t uuid, const char *path) {
d3226d77 644 _cleanup_free_ char *opath = NULL;
0974a682 645 sd_id128_t ouuid;
d3226d77 646 int r;
7b4d7cc0 647
d3226d77
ZJS
648 r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
649 if (r < 0)
0974a682
KS
650 return false;
651 if (!sd_id128_equal(uuid, ouuid))
d3226d77 652 return false;
0974a682 653 if (!streq_ptr(path, opath))
d3226d77 654 return false;
0974a682 655
d3226d77 656 return true;
0974a682
KS
657}
658
659static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
d3226d77
ZJS
660 _cleanup_free_ uint16_t *options = NULL;
661 int n, i;
0974a682 662
d3226d77
ZJS
663 n = efi_get_boot_options(&options);
664 if (n < 0)
665 return n;
0974a682 666
e7dd673d 667 /* find already existing systemd-boot entry */
d3226d77 668 for (i = 0; i < n; i++)
0974a682 669 if (same_entry(options[i], uuid, path)) {
d3226d77
ZJS
670 *id = options[i];
671 return 1;
0974a682
KS
672 }
673
674 /* find free slot in the sorted BootXXXX variable list */
d3226d77 675 for (i = 0; i < n; i++)
0974a682 676 if (i != options[i]) {
d3226d77
ZJS
677 *id = i;
678 return 1;
0974a682
KS
679 }
680
681 /* use the next one */
682 if (i == 0xffff)
683 return -ENOSPC;
d3226d77
ZJS
684 *id = i;
685 return 0;
0974a682
KS
686}
687
688static int insert_into_order(uint16_t slot, bool first) {
d3226d77
ZJS
689 _cleanup_free_ uint16_t *order = NULL;
690 uint16_t *t;
691 int n, i;
0974a682 692
d3226d77
ZJS
693 n = efi_get_boot_order(&order);
694 if (n <= 0)
0974a682 695 /* no entry, add us */
d3226d77 696 return efi_set_boot_order(&slot, 1);
0974a682
KS
697
698 /* are we the first and only one? */
d3226d77
ZJS
699 if (n == 1 && order[0] == slot)
700 return 0;
0974a682
KS
701
702 /* are we already in the boot order? */
d3226d77 703 for (i = 0; i < n; i++) {
0974a682
KS
704 if (order[i] != slot)
705 continue;
706
707 /* we do not require to be the first one, all is fine */
708 if (!first)
d3226d77 709 return 0;
0974a682
KS
710
711 /* move us to the first slot */
d3226d77 712 memmove(order + 1, order, i * sizeof(uint16_t));
0974a682 713 order[0] = slot;
d3226d77 714 return efi_set_boot_order(order, n);
0974a682
KS
715 }
716
717 /* extend array */
a7798cd8 718 t = reallocarray(order, n + 1, sizeof(uint16_t));
d3226d77
ZJS
719 if (!t)
720 return -ENOMEM;
721 order = t;
0974a682
KS
722
723 /* add us to the top or end of the list */
724 if (first) {
d3226d77 725 memmove(order + 1, order, n * sizeof(uint16_t));
0974a682
KS
726 order[0] = slot;
727 } else
d3226d77 728 order[n] = slot;
0974a682 729
d3226d77 730 return efi_set_boot_order(order, n + 1);
0974a682
KS
731}
732
733static int remove_from_order(uint16_t slot) {
7cb0f263 734 _cleanup_free_ uint16_t *order = NULL;
d3226d77 735 int n, i;
0974a682 736
d3226d77
ZJS
737 n = efi_get_boot_order(&order);
738 if (n <= 0)
739 return n;
0974a682 740
d3226d77 741 for (i = 0; i < n; i++) {
0974a682
KS
742 if (order[i] != slot)
743 continue;
744
d3226d77
ZJS
745 if (i + 1 < n)
746 memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
747 return efi_set_boot_order(order, n - 1);
0974a682
KS
748 }
749
d3226d77 750 return 0;
0974a682
KS
751}
752
753static 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,
756 bool first) {
d3226d77 757 char *p;
0974a682
KS
758 uint16_t slot;
759 int r;
760
761 if (!is_efi_boot()) {
d3226d77 762 log_warning("Not booted with EFI, skipping EFI variable setup.");
0974a682
KS
763 return 0;
764 }
765
d3226d77 766 p = strjoina(esp_path, path);
0974a682
KS
767 if (access(p, F_OK) < 0) {
768 if (errno == ENOENT)
d3226d77 769 return 0;
f939cff7
LP
770
771 return log_error_errno(errno, "Cannot access \"%s\": %m", p);
0974a682 772 }
7b4d7cc0 773
0974a682 774 r = find_slot(uuid, path, &slot);
d3226d77
ZJS
775 if (r < 0)
776 return log_error_errno(r,
777 r == -ENOENT ?
778 "Failed to access EFI variables. Is the \"efivarfs\" filesystem mounted?" :
779 "Failed to determine current boot order: %m");
7b4d7cc0 780
181ccb43 781 if (first || r == 0) {
0974a682
KS
782 r = efi_add_boot_option(slot, "Linux Boot Manager",
783 part, pstart, psize,
784 uuid, path);
d3226d77
ZJS
785 if (r < 0)
786 return log_error_errno(r, "Failed to create EFI Boot variable entry: %m");
0974a682 787
d3226d77
ZJS
788 log_info("Created EFI boot entry \"Linux Boot Manager\".");
789 }
0974a682 790
d3226d77 791 return insert_into_order(slot, first);
0974a682
KS
792}
793
794static int remove_boot_efi(const char *esp_path) {
d3226d77
ZJS
795 char *p;
796 _cleanup_closedir_ DIR *d = NULL;
0974a682 797 struct dirent *de;
d3226d77 798 int r, c = 0;
0974a682 799
00f69504 800 p = strjoina(esp_path, "/EFI/BOOT");
0974a682
KS
801 d = opendir(p);
802 if (!d) {
d3226d77
ZJS
803 if (errno == ENOENT)
804 return 0;
0974a682 805
d3226d77 806 return log_error_errno(errno, "Failed to open directory \"%s\": %m", p);
0974a682
KS
807 }
808
e41256dc 809 FOREACH_DIRENT(de, d, break) {
d3226d77
ZJS
810 _cleanup_close_ int fd = -1;
811 _cleanup_free_ char *v = NULL;
0974a682 812
d3226d77 813 if (!endswith_no_case(de->d_name, ".efi"))
0974a682
KS
814 continue;
815
b7536c45 816 if (!startswith_no_case(de->d_name, "boot"))
0974a682
KS
817 continue;
818
d3226d77 819 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
dd114e11 820 if (fd < 0)
d3226d77 821 return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name);
0974a682 822
d3226d77 823 r = get_file_version(fd, &v);
0974a682 824 if (r < 0)
d3226d77
ZJS
825 return r;
826 if (r > 0 && startswith(v, "systemd-boot ")) {
827 r = unlinkat(dirfd(d), de->d_name, 0);
828 if (r < 0)
829 return log_error_errno(errno, "Failed to remove \"%s/%s\": %m", p, de->d_name);
830
a592ab6a 831 log_info("Removed \"%s/%s\".", p, de->d_name);
0974a682
KS
832 }
833
834 c++;
0974a682
KS
835 }
836
d3226d77 837 return c;
0974a682
KS
838}
839
840static int rmdir_one(const char *prefix, const char *suffix) {
841 char *p;
7b4d7cc0 842
d3226d77 843 p = strjoina(prefix, "/", suffix);
0974a682 844 if (rmdir(p) < 0) {
fbf45d22
LP
845 bool ignore = IN_SET(errno, ENOENT, ENOTEMPTY);
846
847 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, errno,
848 "Failed to remove directory \"%s\": %m", p);
849 if (!ignore)
850 return -errno;
7b4d7cc0 851 } else
d3226d77 852 log_info("Removed \"%s\".", p);
7b4d7cc0 853
0974a682 854 return 0;
7b4d7cc0
KS
855}
856
fbf45d22
LP
857static int remove_esp_subdirs(const char *esp_path) {
858 size_t i;
859 int r = 0;
860
861 for (i = ELEMENTSOF(esp_subdirs)-1; i > 0; i--) {
862 int q;
863
864 q = rmdir_one(esp_path, esp_subdirs[i-1]);
865 if (q < 0 && r >= 0)
866 r = q;
867 }
868
869 return r;
870}
871
0974a682
KS
872static int remove_binaries(const char *esp_path) {
873 char *p;
874 int r, q;
875
d3226d77 876 p = strjoina(esp_path, "/EFI/systemd");
c6878637 877 r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
0974a682
KS
878
879 q = remove_boot_efi(esp_path);
880 if (q < 0 && r == 0)
881 r = q;
882
0974a682
KS
883 return r;
884}
885
fbf45d22
LP
886static int remove_loader_config(const char *esp_path) {
887 const char *p;
888
889 assert(esp_path);
890
891 p = strjoina(esp_path, "/loader/loader.conf");
892 if (unlink(p) < 0) {
893 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, "Failed to unlink file \"%s\": %m", p);
894 if (errno != ENOENT)
895 return -errno;
896 } else
897 log_info("Removed \"%s\".", p);
898
899 return 0;
900}
901
902static int remove_entries_directory(const char *dollar_boot_path) {
903 assert(dollar_boot_path);
904
905 return rmdir_one(dollar_boot_path, "/loader/entries");
906}
907
0974a682
KS
908static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
909 uint16_t slot;
910 int r;
911
912 if (!is_efi_boot())
913 return 0;
914
915 r = find_slot(uuid, path, &slot);
916 if (r != 1)
917 return 0;
918
919 r = efi_remove_boot_option(slot);
920 if (r < 0)
921 return r;
922
923 if (in_order)
d3226d77 924 return remove_from_order(slot);
f939cff7
LP
925
926 return 0;
0974a682
KS
927}
928
341890de 929static int install_loader_config(const char *esp_path, sd_id128_t machine_id) {
d5ff6d6d 930 char machine_string[SD_ID128_STRING_MAX];
f5b84de2
LP
931 _cleanup_(unlink_and_freep) char *t = NULL;
932 _cleanup_fclose_ FILE *f = NULL;
d5ff6d6d 933 const char *p;
f5b84de2 934 int r, fd;
0974a682 935
d5ff6d6d 936 p = strjoina(esp_path, "/loader/loader.conf");
f5b84de2
LP
937 if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
938 return 0;
939
940 fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
941 if (fd < 0)
942 return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p);
943
e92aaed3 944 f = fdopen(fd, "w");
f5b84de2
LP
945 if (!f) {
946 safe_close(fd);
947 return log_oom();
948 }
0974a682 949
a36b411e
LP
950 fprintf(f, "#timeout 3\n"
951 "#console-mode keep\n"
952 "default %s-*\n", sd_id128_to_string(machine_id, machine_string));
0974a682 953
0675e94a 954 r = fflush_sync_and_check(f);
d5ff6d6d
LP
955 if (r < 0)
956 return log_error_errno(r, "Failed to write \"%s\": %m", p);
0974a682 957
f5b84de2
LP
958 r = link_tmpfile(fd, t, p);
959 if (r == -EEXIST)
960 return 0; /* Silently skip creation if the file exists now (recheck) */
961 if (r < 0)
962 return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
963
964 t = mfree(t);
f5b84de2 965 return 1;
0974a682
KS
966}
967
341890de
ZJS
968static int install_entries_directories(const char *dollar_boot_path, sd_id128_t machine_id) {
969 int r;
970 char buf[SD_ID128_STRING_MAX];
971
fbf45d22
LP
972 assert(dollar_boot_path);
973
341890de
ZJS
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. */
977
e2600fd5 978 r = mkdir_one(dollar_boot_path, "loader/entries");
341890de
ZJS
979 if (r < 0)
980 return r;
981
982 return mkdir_one(dollar_boot_path, sd_id128_to_string(machine_id, buf));
fbf45d22
LP
983}
984
2f2c539c 985static int help(int argc, char *argv[], void *userdata) {
37ec0fdd
LP
986 _cleanup_free_ char *link = NULL;
987 int r;
988
989 r = terminal_urlify_man("bootctl", "1", &link);
990 if (r < 0)
991 return log_oom();
2f2c539c 992
37ec0fdd 993 printf("%s [COMMAND] [OPTIONS...]\n\n"
68cc17f1 994 "Install, update or remove the systemd-boot EFI boot manager.\n\n"
fbf45d22
LP
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"
fba4e945 1000 " -x --print-boot-path Print path to the $BOOT partition\n"
fbf45d22
LP
1001 " --no-variables Don't touch EFI variables\n"
1002 " --no-pager Do not pipe output into a pager\n"
d88c96ff 1003 "\nBoot Loader Commands:\n"
fbf45d22
LP
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"
d88c96ff 1008 "\nBoot Loader Entries Commands:\n"
fbf45d22
LP
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"
37ec0fdd
LP
1012 "\nSee the %s for details.\n"
1013 , program_invocation_short_name
d88c96ff 1014 , link);
0974a682
KS
1015
1016 return 0;
1017}
1018
0974a682
KS
1019static int parse_argv(int argc, char *argv[]) {
1020 enum {
fbf45d22
LP
1021 ARG_ESP_PATH = 0x100,
1022 ARG_BOOT_PATH,
0974a682
KS
1023 ARG_VERSION,
1024 ARG_NO_VARIABLES,
57db6f18 1025 ARG_NO_PAGER,
7b4d7cc0
KS
1026 };
1027
0974a682 1028 static const struct option options[] = {
fbf45d22
LP
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 */
fba4e945 1036 { "print-boot-path", no_argument, NULL, 'x' },
fbf45d22
LP
1037 { "no-variables", no_argument, NULL, ARG_NO_VARIABLES },
1038 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
a36b411e 1039 {}
0974a682
KS
1040 };
1041
2f2c539c 1042 int c, r;
7b4d7cc0
KS
1043
1044 assert(argc >= 0);
1045 assert(argv);
1046
fba4e945 1047 while ((c = getopt_long(argc, argv, "hpx", options, NULL)) >= 0)
0974a682 1048 switch (c) {
7b4d7cc0 1049
0974a682 1050 case 'h':
2f2c539c 1051 help(0, NULL, NULL);
7b4d7cc0 1052 return 0;
7b4d7cc0 1053
0974a682 1054 case ARG_VERSION:
3f6fd1ba 1055 return version();
7b4d7cc0 1056
fbf45d22
LP
1057 case ARG_ESP_PATH:
1058 r = free_and_strdup(&arg_esp_path, optarg);
1059 if (r < 0)
1060 return log_oom();
1061 break;
1062
1063 case ARG_BOOT_PATH:
1064 r = free_and_strdup(&arg_xbootldr_path, optarg);
2f2c539c
LP
1065 if (r < 0)
1066 return log_oom();
0974a682
KS
1067 break;
1068
30b50477 1069 case 'p':
aa467bca
ZJS
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");
fbf45d22
LP
1073 arg_print_esp_path = true;
1074 break;
1075
fba4e945 1076 case 'x':
aa467bca
ZJS
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");
fbf45d22 1080 arg_print_dollar_boot_path = true;
30b50477
ZJS
1081 break;
1082
0974a682
KS
1083 case ARG_NO_VARIABLES:
1084 arg_touch_variables = false;
1085 break;
1086
57db6f18 1087 case ARG_NO_PAGER:
0221d68a 1088 arg_pager_flags |= PAGER_DISABLE;
57db6f18
LP
1089 break;
1090
0974a682
KS
1091 case '?':
1092 return -EINVAL;
1093
1094 default:
d3226d77 1095 assert_not_reached("Unknown option");
7b4d7cc0 1096 }
7b4d7cc0 1097
0974a682
KS
1098 return 1;
1099}
7b4d7cc0 1100
551710cf
ZJS
1101static void read_loader_efi_var(const char *name, char **var) {
1102 int r;
1103
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);
1107}
1108
2f2c539c 1109static int verb_status(int argc, char *argv[], void *userdata) {
fbf45d22 1110 sd_id128_t esp_uuid = SD_ID128_NULL, xbootldr_uuid = SD_ID128_NULL;
46fb255b 1111 int r, k;
0974a682 1112
fbf45d22
LP
1113 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &esp_uuid);
1114 if (arg_print_esp_path) {
5caa3167
LP
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) */
fbf45d22 1117 return log_error_errno(r, "Failed to determine ESP location: %m");
30b50477
ZJS
1118 if (r < 0)
1119 return r;
1120
fbf45d22
LP
1121 puts(arg_esp_path);
1122 }
1123
1124 r = acquire_xbootldr(geteuid() != 0, &xbootldr_uuid);
1125 if (arg_print_dollar_boot_path) {
1126 if (r == -EACCES)
1127 return log_error_errno(r, "Failed to determine XBOOTLDR location: %m");
1128 if (r < 0)
1129 return r;
1130
1131 puts(arg_dollar_boot_path());
30b50477 1132 }
551710cf 1133
fbf45d22
LP
1134 if (arg_print_esp_path || arg_print_dollar_boot_path)
1135 return 0;
1136
5caa3167
LP
1137 r = 0; /* If we couldn't determine the path, then don't consider that a problem from here on, just show what we
1138 * can show */
1139
0221d68a 1140 (void) pager_open(arg_pager_flags);
57db6f18 1141
2f2c539c 1142 if (is_efi_boot()) {
80641a81
LP
1143 static const struct {
1144 uint64_t flag;
1145 const char *name;
1146 } flags[] = {
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" },
1152 };
1153
81375b9b 1154 _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL;
25579a43 1155 sd_id128_t loader_part_uuid = SD_ID128_NULL;
80641a81
LP
1156 uint64_t loader_features = 0;
1157 size_t i;
0974a682 1158
2f2c539c
LP
1159 read_loader_efi_var("LoaderFirmwareType", &fw_type);
1160 read_loader_efi_var("LoaderFirmwareInfo", &fw_info);
1161 read_loader_efi_var("LoaderInfo", &loader);
81375b9b 1162 read_loader_efi_var("StubInfo", &stub);
2f2c539c 1163 read_loader_efi_var("LoaderImageIdentifier", &loader_path);
80641a81 1164 (void) efi_loader_get_features(&loader_features);
551710cf 1165
2f2c539c
LP
1166 if (loader_path)
1167 efi_tilt_backslashes(loader_path);
1168
46fb255b
ZJS
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");
2f2c539c
LP
1172
1173 printf("System:\n");
ba857253 1174 printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal());
33987ba0
YW
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");
2f2c539c
LP
1177 printf("\n");
1178
7fd66f3c 1179 printf("Current Boot Loader:\n");
ba857253 1180 printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal());
80641a81
LP
1181
1182 for (i = 0; i < ELEMENTSOF(flags); i++) {
1183
1184 if (i == 0)
1185 printf(" Features: ");
1186 else
1187 printf(" ");
1188
1189 if (FLAGS_SET(loader_features, flags[i].flag))
9a6f746f 1190 printf("%s%s%s %s\n", ansi_highlight_green(), special_glyph(SPECIAL_GLYPH_CHECK_MARK), ansi_normal(), flags[i].name);
80641a81 1191 else
9a6f746f 1192 printf("%s%s%s %s\n", ansi_highlight_red(), special_glyph(SPECIAL_GLYPH_CROSS_MARK), ansi_normal(), flags[i].name);
80641a81
LP
1193 }
1194
81375b9b
ДГ
1195 if (stub)
1196 printf(" Stub: %s\n", stub);
e28973ee 1197 if (!sd_id128_is_null(loader_part_uuid))
bd44566c 1198 printf(" ESP: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
2f2c539c
LP
1199 SD_ID128_FORMAT_VAL(loader_part_uuid));
1200 else
cd2d4c7f 1201 printf(" ESP: n/a\n");
9a6f746f 1202 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), strna(loader_path));
2f2c539c
LP
1203 printf("\n");
1204 } else
cd2d4c7f 1205 printf("System:\n Not booted with EFI\n\n");
7b4d7cc0 1206
fbf45d22
LP
1207 if (arg_esp_path) {
1208 k = status_binaries(arg_esp_path, esp_uuid);
ecec2a5d
LP
1209 if (k < 0)
1210 r = k;
1211 }
2f2c539c 1212
cd2d4c7f 1213 if (is_efi_boot()) {
46fb255b
ZJS
1214 k = status_variables();
1215 if (k < 0)
1216 r = k;
cd2d4c7f 1217 }
0974a682 1218
fbf45d22 1219 if (arg_esp_path || arg_xbootldr_path) {
81fed855 1220 k = status_entries(arg_esp_path, esp_uuid, arg_xbootldr_path, xbootldr_uuid);
ecec2a5d
LP
1221 if (k < 0)
1222 r = k;
1223 }
7e87c7d9 1224
46fb255b 1225 return r;
2f2c539c 1226}
7b4d7cc0 1227
7e87c7d9 1228static int verb_list(int argc, char *argv[], void *userdata) {
5caa3167 1229 _cleanup_(boot_config_free) BootConfig config = {};
5caa3167 1230 int r;
7e87c7d9 1231
5caa3167
LP
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. */
7e87c7d9 1235
fbf45d22 1236 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, NULL);
5caa3167
LP
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");
7e87c7d9
ZJS
1239 if (r < 0)
1240 return r;
1241
fbf45d22
LP
1242 r = acquire_xbootldr(geteuid() != 0, NULL);
1243 if (r == -EACCES)
1244 return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
1245 if (r < 0)
1246 return r;
1247
1248 r = boot_entries_load_config(arg_esp_path, arg_xbootldr_path, &config);
7e87c7d9 1249 if (r < 0)
21f7a622 1250 return r;
7e87c7d9 1251
93f14ce2 1252 (void) boot_entries_augment_from_loader(&config, false);
bd2865ca 1253
20a28174
LP
1254 if (config.n_entries == 0)
1255 log_info("No boot loader entries found.");
1256 else {
1257 size_t n;
7e87c7d9 1258
0221d68a 1259 (void) pager_open(arg_pager_flags);
57db6f18 1260
20a28174 1261 printf("Boot Loader Entries:\n");
7e87c7d9 1262
20a28174
LP
1263 for (n = 0; n < config.n_entries; n++) {
1264 r = boot_entry_show(config.entries + n, n == (size_t) config.default_entry);
1265 if (r < 0)
1266 return r;
7e87c7d9 1267
4629499e
LP
1268 if (n+1 < config.n_entries)
1269 putchar('\n');
7e87c7d9 1270 }
cd2d4c7f 1271 }
0974a682 1272
7e87c7d9 1273 return 0;
2f2c539c 1274}
7b4d7cc0 1275
fbf45d22
LP
1276static int sync_everything(void) {
1277 int ret = 0, k;
e0e8d177 1278
fbf45d22
LP
1279 if (arg_esp_path) {
1280 k = syncfs_path(AT_FDCWD, arg_esp_path);
1281 if (k < 0)
1282 ret = log_error_errno(k, "Failed to synchronize the ESP '%s': %m", arg_esp_path);
1283 }
e0e8d177 1284
fbf45d22
LP
1285 if (arg_xbootldr_path) {
1286 k = syncfs_path(AT_FDCWD, arg_xbootldr_path);
1287 if (k < 0)
1288 ret = log_error_errno(k, "Failed to synchronize $BOOT '%s': %m", arg_xbootldr_path);
1289 }
e0e8d177 1290
fbf45d22 1291 return ret;
e0e8d177
LP
1292}
1293
2f2c539c 1294static int verb_install(int argc, char *argv[], void *userdata) {
2f2c539c
LP
1295 sd_id128_t uuid = SD_ID128_NULL;
1296 uint64_t pstart = 0, psize = 0;
1297 uint32_t part = 0;
341890de 1298 sd_id128_t machine_id;
2f2c539c
LP
1299 bool install;
1300 int r;
1301
5caa3167 1302 r = acquire_esp(false, &part, &pstart, &psize, &uuid);
2f2c539c
LP
1303 if (r < 0)
1304 return r;
1305
fbf45d22
LP
1306 r = acquire_xbootldr(false, NULL);
1307 if (r < 0)
1308 return r;
1309
341890de
ZJS
1310 r = sd_id128_get_machine(&machine_id);
1311 if (r < 0)
1312 return log_error_errno(r, "Failed to get machine id: %m");
1313
2f2c539c
LP
1314 install = streq(argv[0], "install");
1315
1316 RUN_WITH_UMASK(0002) {
fbf45d22
LP
1317 if (install) {
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);
1322 if (r < 0)
1323 return r;
1324 }
1325
1326 r = install_binaries(arg_esp_path, install);
0974a682 1327 if (r < 0)
d3226d77 1328 return r;
0974a682 1329
2f2c539c 1330 if (install) {
341890de 1331 r = install_loader_config(arg_esp_path, machine_id);
fbf45d22
LP
1332 if (r < 0)
1333 return r;
1334
341890de 1335 r = install_entries_directories(arg_dollar_boot_path(), machine_id);
d3226d77
ZJS
1336 if (r < 0)
1337 return r;
1338 }
2f2c539c 1339 }
0974a682 1340
fbf45d22 1341 (void) sync_everything();
e0e8d177 1342
2f2c539c 1343 if (arg_touch_variables)
fbf45d22 1344 r = install_variables(arg_esp_path,
2f2c539c
LP
1345 part, pstart, psize, uuid,
1346 "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi",
1347 install);
7b4d7cc0 1348
2f2c539c
LP
1349 return r;
1350}
0974a682 1351
2f2c539c
LP
1352static int verb_remove(int argc, char *argv[], void *userdata) {
1353 sd_id128_t uuid = SD_ID128_NULL;
fbf45d22 1354 int r, q;
0974a682 1355
5caa3167 1356 r = acquire_esp(false, NULL, NULL, NULL, &uuid);
2f2c539c
LP
1357 if (r < 0)
1358 return r;
1359
fbf45d22
LP
1360 r = acquire_xbootldr(false, NULL);
1361 if (r < 0)
1362 return r;
2f2c539c 1363
fbf45d22 1364 r = remove_binaries(arg_esp_path);
e0e8d177 1365
fbf45d22
LP
1366 q = remove_loader_config(arg_esp_path);
1367 if (q < 0 && r >= 0)
1368 r = q;
1369
1370 q = remove_entries_directory(arg_dollar_boot_path());
1371 if (q < 0 && r >= 0)
1372 r = q;
2f2c539c 1373
fbf45d22
LP
1374 q = remove_esp_subdirs(arg_esp_path);
1375 if (q < 0 && r >= 0)
1376 r = q;
1377
1378 (void) sync_everything();
1379
1380 if (arg_touch_variables) {
2f2c539c 1381 q = remove_variables(uuid, "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi", true);
fbf45d22 1382 if (q < 0 && r >= 0)
2f2c539c 1383 r = q;
7b4d7cc0
KS
1384 }
1385
d3226d77 1386 return r;
7b4d7cc0
KS
1387}
1388
d88c96ff
LP
1389static int verb_set_default(int argc, char *argv[], void *userdata) {
1390 const char *name;
1391 int r;
1392
baaa35ad
ZJS
1393 if (!is_efi_boot())
1394 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1395 "Not booted with UEFI.");
d88c96ff
LP
1396
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.");
1400 return -EOPNOTSUPP;
1401 }
1402
1403 return log_error_errno(errno, "Failed to detect whether boot loader supports '%s' operation: %m", argv[0]);
1404 }
1405
baaa35ad
ZJS
1406 if (detect_container() > 0)
1407 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1408 "'%s' operation not supported in a container.",
1409 argv[0]);
d88c96ff 1410
baaa35ad
ZJS
1411 if (!arg_touch_variables)
1412 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1413 "'%s' operation cannot be combined with --touch-variables=no.",
1414 argv[0]);
d88c96ff
LP
1415
1416 name = streq(argv[0], "set-default") ? "LoaderEntryDefault" : "LoaderEntryOneShot";
1417
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");
1422 } else {
1423 _cleanup_free_ char16_t *encoded = NULL;
1424
1425 encoded = utf8_to_utf16(argv[1], strlen(argv[1]));
1426 if (!encoded)
1427 return log_oom();
1428
1429 r = efi_set_variable(EFI_VENDOR_LOADER, name, encoded, char16_strlen(encoded) * 2 + 2);
1430 if (r < 0)
1431 return log_error_errno(r, "Failed to update EFI variable: %m");
1432 }
1433
1434 return 0;
1435}
1436
2f2c539c 1437static int bootctl_main(int argc, char *argv[]) {
2f2c539c 1438 static const Verb verbs[] = {
7c3ce8b5
ZJS
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 },
2f2c539c
LP
1447 {}
1448 };
1449
1450 return dispatch_verb(argc, argv, verbs, NULL);
1451}
1452
608f8ec9 1453static int run(int argc, char *argv[]) {
601185b4 1454 int r;
7b4d7cc0
KS
1455
1456 log_parse_environment();
1457 log_open();
1458
341890de 1459 /* If we run in a container, automatically turn off EFI file system access */
2f2c539c
LP
1460 if (detect_container() > 0)
1461 arg_touch_variables = false;
1462
7b4d7cc0 1463 r = parse_argv(argc, argv);
601185b4 1464 if (r <= 0)
608f8ec9 1465 return r;
57db6f18 1466
608f8ec9 1467 return bootctl_main(argc, argv);
7b4d7cc0 1468}
608f8ec9
YW
1469
1470DEFINE_MAIN_FUNCTION(run);