]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/boot/bootctl.c
tree-wide: drop dirent.h when dirent-util.h is included
[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 5#include <errno.h>
0974a682 6#include <ftw.h>
3f6fd1ba
LP
7#include <getopt.h>
8#include <limits.h>
5fa6c13c 9#include <linux/magic.h>
0974a682 10#include <stdbool.h>
3f6fd1ba
LP
11#include <stdio.h>
12#include <stdlib.h>
3f6fd1ba
LP
13#include <sys/mman.h>
14#include <sys/stat.h>
15#include <sys/statfs.h>
16#include <unistd.h>
7b4d7cc0 17
dccca82b
LP
18#include "sd-id128.h"
19
b5efdb8a 20#include "alloc-util.h"
3f6fd1ba 21#include "blkid-util.h"
7e87c7d9 22#include "bootspec.h"
175d308c 23#include "copy.h"
e41256dc 24#include "dirent-util.h"
0bb2f0f1 25#include "efi-loader.h"
0974a682 26#include "efivars.h"
e44c3229 27#include "env-util.h"
9dae4c8a 28#include "escape.h"
3ffd4af2 29#include "fd-util.h"
0d39fa9c 30#include "fileio.h"
175d308c 31#include "fs-util.h"
8752c575 32#include "locale-util.h"
608f8ec9 33#include "main-func.h"
57db6f18 34#include "pager.h"
2f2c539c 35#include "parse-util.h"
294bf0c3 36#include "pretty-print.h"
e44c3229 37#include "random-util.h"
c6878637 38#include "rm-rf.h"
175d308c 39#include "stat-util.h"
341890de 40#include "stdio-util.h"
07630cea 41#include "string-util.h"
2f2c539c 42#include "strv.h"
7e87c7d9 43#include "terminal-util.h"
e4de7287 44#include "tmpfile-util.h"
2f2c539c 45#include "umask-util.h"
d88c96ff 46#include "utf8.h"
3f6fd1ba 47#include "util.h"
2f2c539c
LP
48#include "verbs.h"
49#include "virt.h"
7b4d7cc0 50
fbf45d22
LP
51static char *arg_esp_path = NULL;
52static char *arg_xbootldr_path = NULL;
53static bool arg_print_esp_path = false;
54static bool arg_print_dollar_boot_path = false;
25579a43 55static bool arg_touch_variables = true;
0221d68a 56static PagerFlags arg_pager_flags = 0;
25579a43 57
fbf45d22
LP
58STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
59STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
60
61static const char *arg_dollar_boot_path(void) {
62 /* $BOOT shall be the XBOOTLDR partition if it exists, and otherwise the ESP */
63 return arg_xbootldr_path ?: arg_esp_path;
64}
608f8ec9 65
5caa3167
LP
66static int acquire_esp(
67 bool unprivileged_mode,
68 uint32_t *ret_part,
69 uint64_t *ret_pstart,
70 uint64_t *ret_psize,
71 sd_id128_t *ret_uuid) {
72
73 char *np;
2f2c539c
LP
74 int r;
75
fbf45d22
LP
76 /* Find the ESP, and log about errors. Note that find_esp_and_warn() will log in all error cases on
77 * its own, except for ENOKEY (which is good, we want to show our own message in that case,
78 * suggesting use of --esp-path=) and EACCESS (only when we request unprivileged mode; in this case
79 * we simply eat up the error here, so that --list and --status work too, without noise about
80 * this). */
5caa3167 81
fbf45d22 82 r = find_esp_and_warn(arg_esp_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid);
5caa3167 83 if (r == -ENOKEY)
af918182 84 return log_error_errno(r,
5caa3167 85 "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n"
fbf45d22 86 "Alternatively, use --esp-path= to specify path to mount point.");
5caa3167
LP
87 if (r < 0)
88 return r;
89
fbf45d22
LP
90 free_and_replace(arg_esp_path, np);
91 log_debug("Using EFI System Partition at %s.", arg_esp_path);
92
93 return 1;
94}
0974a682 95
fbf45d22
LP
96static int acquire_xbootldr(bool unprivileged_mode, sd_id128_t *ret_uuid) {
97 char *np;
98 int r;
5caa3167 99
fbf45d22
LP
100 r = find_xbootldr_and_warn(arg_xbootldr_path, unprivileged_mode, &np, ret_uuid);
101 if (r == -ENOKEY) {
102 log_debug_errno(r, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
103 if (ret_uuid)
104 *ret_uuid = SD_ID128_NULL;
a2ae0d49 105 arg_xbootldr_path = mfree(arg_xbootldr_path);
fbf45d22
LP
106 return 0;
107 }
108 if (r < 0)
109 return r;
110
111 free_and_replace(arg_xbootldr_path, np);
112 log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path);
113
114 return 1;
7b4d7cc0
KS
115}
116
e7dd673d 117/* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */
d3226d77 118static int get_file_version(int fd, char **v) {
0974a682
KS
119 struct stat st;
120 char *buf;
121 const char *s, *e;
122 char *x = NULL;
123 int r = 0;
7b4d7cc0 124
d3226d77 125 assert(fd >= 0);
0974a682 126 assert(v);
7b4d7cc0 127
d3226d77 128 if (fstat(fd, &st) < 0)
db6d9fae 129 return log_error_errno(errno, "Failed to stat EFI binary: %m");
7b4d7cc0 130
c4ba5b51
LP
131 r = stat_verify_regular(&st);
132 if (r < 0)
d90f2add 133 return log_error_errno(r, "EFI binary is not a regular file: %m");
c4ba5b51 134
db6d9fae
LP
135 if (st.st_size < 27) {
136 *v = NULL;
0974a682 137 return 0;
db6d9fae 138 }
eb9da376 139
d3226d77 140 buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
0974a682 141 if (buf == MAP_FAILED)
db6d9fae 142 return log_error_errno(errno, "Failed to memory map EFI binary: %m");
7b4d7cc0 143
0974a682
KS
144 s = memmem(buf, st.st_size - 8, "#### LoaderInfo: ", 17);
145 if (!s)
146 goto finish;
147 s += 17;
7b4d7cc0 148
0974a682
KS
149 e = memmem(s, st.st_size - (s - buf), " ####", 5);
150 if (!e || e - s < 3) {
78d5d4ed 151 r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Malformed version string.");
0974a682
KS
152 goto finish;
153 }
7b4d7cc0 154
0974a682
KS
155 x = strndup(s, e - s);
156 if (!x) {
d3226d77 157 r = log_oom();
0974a682
KS
158 goto finish;
159 }
160 r = 1;
7b4d7cc0 161
0974a682 162finish:
db6d9fae 163 (void) munmap(buf, st.st_size);
0974a682
KS
164 *v = x;
165 return r;
166}
7b4d7cc0 167
0974a682 168static int enumerate_binaries(const char *esp_path, const char *path, const char *prefix) {
d3226d77 169 _cleanup_closedir_ DIR *d = NULL;
0974a682 170 struct dirent *de;
270384b2 171 const char *p;
fbf45d22 172 int c = 0, r;
fbf45d22
LP
173
174 assert(esp_path);
175 assert(path);
0974a682 176
270384b2 177 p = prefix_roota(esp_path, path);
0974a682
KS
178 d = opendir(p);
179 if (!d) {
d3226d77
ZJS
180 if (errno == ENOENT)
181 return 0;
7b4d7cc0 182
d3226d77 183 return log_error_errno(errno, "Failed to read \"%s\": %m", p);
0974a682
KS
184 }
185
e41256dc 186 FOREACH_DIRENT(de, d, break) {
d3226d77 187 _cleanup_free_ char *v = NULL;
fbf45d22 188 _cleanup_close_ int fd = -1;
0974a682 189
d3226d77 190 if (!endswith_no_case(de->d_name, ".efi"))
0974a682
KS
191 continue;
192
d3226d77 193 if (prefix && !startswith_no_case(de->d_name, prefix))
0974a682
KS
194 continue;
195
d3226d77
ZJS
196 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
197 if (fd < 0)
198 return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name);
0974a682 199
d3226d77 200 r = get_file_version(fd, &v);
0974a682 201 if (r < 0)
d3226d77 202 return r;
0974a682 203 if (r > 0)
9a6f746f 204 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 205 else
9a6f746f 206 printf(" File: %s/%s/%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path, de->d_name);
fbf45d22 207
0974a682 208 c++;
0974a682
KS
209 }
210
d3226d77 211 return c;
7b4d7cc0
KS
212}
213
0974a682
KS
214static int status_binaries(const char *esp_path, sd_id128_t partition) {
215 int r;
216
7fd66f3c 217 printf("Available Boot Loaders on ESP:\n");
0974a682 218
cd2d4c7f
YW
219 if (!esp_path) {
220 printf(" ESP: Cannot find or access mount point of ESP.\n\n");
221 return -ENOENT;
222 }
223
224 printf(" ESP: %s", esp_path);
225 if (!sd_id128_is_null(partition))
bd44566c 226 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")", SD_ID128_FORMAT_VAL(partition));
cd2d4c7f 227 printf("\n");
0974a682
KS
228
229 r = enumerate_binaries(esp_path, "EFI/systemd", NULL);
fbf45d22 230 if (r < 0)
882b3bd6 231 goto finish;
0974a682 232 if (r == 0)
48184e43 233 log_info("systemd-boot not installed in ESP.");
0974a682 234
00f69504 235 r = enumerate_binaries(esp_path, "EFI/BOOT", "boot");
fbf45d22 236 if (r < 0)
882b3bd6 237 goto finish;
0974a682 238 if (r == 0)
48184e43 239 log_info("No default/fallback boot loader installed in ESP.");
0974a682 240
882b3bd6 241 r = 0;
c11ae0ba 242
882b3bd6
LP
243finish:
244 printf("\n");
245 return r;
0974a682
KS
246}
247
248static int print_efi_option(uint16_t id, bool in_order) {
7cb0f263
TA
249 _cleanup_free_ char *title = NULL;
250 _cleanup_free_ char *path = NULL;
0974a682
KS
251 sd_id128_t partition;
252 bool active;
253 int r = 0;
254
255 r = efi_get_boot_option(id, &title, &partition, &path, &active);
256 if (r < 0)
7cb0f263 257 return r;
7b4d7cc0 258
0974a682 259 /* print only configured entries with partition information */
3bbaff3e 260 if (!path || sd_id128_is_null(partition))
0974a682
KS
261 return 0;
262
263 efi_tilt_backslashes(path);
264
ba857253 265 printf(" Title: %s%s%s\n", ansi_highlight(), strna(title), ansi_normal());
0974a682
KS
266 printf(" ID: 0x%04X\n", id);
267 printf(" Status: %sactive%s\n", active ? "" : "in", in_order ? ", boot-order" : "");
bd44566c
ZJS
268 printf(" Partition: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
269 SD_ID128_FORMAT_VAL(partition));
9a6f746f 270 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path);
0974a682
KS
271 printf("\n");
272
7cb0f263 273 return 0;
0974a682
KS
274}
275
276static int status_variables(void) {
d3226d77 277 _cleanup_free_ uint16_t *options = NULL, *order = NULL;
a36b411e 278 int n_options, n_order, i;
0974a682 279
0974a682 280 n_options = efi_get_boot_options(&options);
d3226d77 281 if (n_options == -ENOENT)
f939cff7
LP
282 return log_error_errno(n_options,
283 "Failed to access EFI variables, efivarfs"
d3226d77 284 " needs to be available at /sys/firmware/efi/efivars/.");
f939cff7 285 if (n_options < 0)
d3226d77 286 return log_error_errno(n_options, "Failed to read EFI boot entries: %m");
0974a682 287
0974a682 288 n_order = efi_get_boot_order(&order);
d3226d77 289 if (n_order == -ENOENT)
0974a682 290 n_order = 0;
d3226d77 291 else if (n_order < 0)
7709ef3a 292 return log_error_errno(n_order, "Failed to read EFI boot order: %m");
0974a682
KS
293
294 /* print entries in BootOrder first */
7fd66f3c 295 printf("Boot Loaders Listed in EFI Variables:\n");
0974a682
KS
296 for (i = 0; i < n_order; i++)
297 print_efi_option(order[i], true);
298
299 /* print remaining entries */
300 for (i = 0; i < n_options; i++) {
301 int j;
0974a682
KS
302
303 for (j = 0; j < n_order; j++)
d3226d77 304 if (options[i] == order[j])
a908cf0a 305 goto next_option;
0974a682
KS
306
307 print_efi_option(options[i], false);
a908cf0a
MM
308
309 next_option:
310 continue;
0974a682
KS
311 }
312
d3226d77 313 return 0;
0974a682
KS
314}
315
44e6a5ef
ZJS
316static int boot_entry_file_check(const char *root, const char *p) {
317 _cleanup_free_ char *path;
318
319 path = path_join(root, p);
320 if (!path)
321 return log_oom();
322
323 if (access(path, F_OK) < 0)
324 return -errno;
325
326 return 0;
327}
328
d3eb6072 329static void boot_entry_file_list(const char *field, const char *root, const char *p, int *ret_status) {
44e6a5ef
ZJS
330 int status = boot_entry_file_check(root, p);
331
405b104d 332 printf("%13s%s ", strempty(field), field ? ":" : " ");
44e6a5ef
ZJS
333 if (status < 0) {
334 errno = -status;
335 printf("%s%s%s (%m)\n", ansi_highlight_red(), p, ansi_normal());
336 } else
337 printf("%s\n", p);
d3eb6072
ZJS
338
339 if (*ret_status == 0 && status < 0)
340 *ret_status = status;
44e6a5ef
ZJS
341}
342
20a28174 343static int boot_entry_show(const BootEntry *e, bool show_as_default) {
d3eb6072
ZJS
344 int status = 0;
345
346 /* Returns 0 on success, negative on processing error, and positive if something is wrong with the
347 boot entry itself. */
348
20a28174
LP
349 assert(e);
350
ce4c4f81
ZJS
351 printf(" title: %s%s%s" "%s%s%s\n",
352 ansi_highlight(), boot_entry_title(e), ansi_normal(),
353 ansi_highlight_green(), show_as_default ? " (default)" : "", ansi_normal());
20a28174
LP
354
355 if (e->id)
356 printf(" id: %s\n", e->id);
cce9457c
ZJS
357 if (e->path) {
358 _cleanup_free_ char *link = NULL;
359
360 /* Let's urlify the link to make it easy to view in an editor, but only if it is a text
361 * file. Unified images are binary ELFs, and EFI variables are not pure text either. */
362 if (e->type == BOOT_ENTRY_CONF)
363 (void) terminal_urlify_path(e->path, NULL, &link);
364
365 printf(" source: %s\n", link ?: e->path);
366 }
20a28174
LP
367 if (e->version)
368 printf(" version: %s\n", e->version);
369 if (e->machine_id)
370 printf(" machine-id: %s\n", e->machine_id);
371 if (e->architecture)
372 printf(" architecture: %s\n", e->architecture);
373 if (e->kernel)
d3eb6072 374 boot_entry_file_list("linux", e->root, e->kernel, &status);
20a28174 375
44e6a5ef
ZJS
376 char **s;
377 STRV_FOREACH(s, e->initrd)
378 boot_entry_file_list(s == e->initrd ? "initrd" : NULL,
379 e->root,
d3eb6072
ZJS
380 *s,
381 &status);
20a28174 382 if (!strv_isempty(e->options)) {
9dae4c8a
ZJS
383 _cleanup_free_ char *t = NULL, *t2 = NULL;
384 _cleanup_strv_free_ char **ts = NULL;
20a28174
LP
385
386 t = strv_join(e->options, " ");
387 if (!t)
388 return log_oom();
389
9dae4c8a
ZJS
390 ts = strv_split_newlines(t);
391 if (!ts)
392 return log_oom();
393
394 t2 = strv_join(ts, "\n ");
395 if (!t2)
396 return log_oom();
397
9dae4c8a 398 printf(" options: %s\n", t2);
20a28174
LP
399 }
400 if (e->device_tree)
d3eb6072 401 boot_entry_file_list("devicetree", e->root, e->device_tree, &status);
20a28174 402
d3eb6072 403 return -status;
20a28174
LP
404}
405
81fed855
LP
406static int status_entries(
407 const char *esp_path,
408 sd_id128_t esp_partition_uuid,
409 const char *xbootldr_path,
410 sd_id128_t xbootldr_partition_uuid) {
411
7e87c7d9 412 _cleanup_(boot_config_free) BootConfig config = {};
81fed855
LP
413 sd_id128_t dollar_boot_partition_uuid;
414 const char *dollar_boot_path;
a099e035 415 int r;
7e87c7d9 416
81fed855
LP
417 assert(esp_path || xbootldr_path);
418
419 if (xbootldr_path) {
420 dollar_boot_path = xbootldr_path;
421 dollar_boot_partition_uuid = xbootldr_partition_uuid;
422 } else {
423 dollar_boot_path = esp_path;
424 dollar_boot_partition_uuid = esp_partition_uuid;
425 }
426
427 printf("Boot Loader Entries:\n"
428 " $BOOT: %s", dollar_boot_path);
429 if (!sd_id128_is_null(dollar_boot_partition_uuid))
bd44566c
ZJS
430 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")",
431 SD_ID128_FORMAT_VAL(dollar_boot_partition_uuid));
81fed855
LP
432 printf("\n\n");
433
fbf45d22 434 r = boot_entries_load_config(esp_path, xbootldr_path, &config);
7e87c7d9 435 if (r < 0)
21f7a622 436 return r;
7e87c7d9
ZJS
437
438 if (config.default_entry < 0)
a099e035 439 printf("%zu entries, no entry could be determined as default.\n", config.n_entries);
7e87c7d9 440 else {
7fd66f3c 441 printf("Default Boot Loader Entry:\n");
a099e035 442
d3eb6072
ZJS
443 r = boot_entry_show(config.entries + config.default_entry, false);
444 if (r > 0)
445 /* < 0 is already logged by the function itself, let's just emit an extra warning if
446 the default entry is broken */
447 printf("\nWARNING: default boot entry is broken\n");
7e87c7d9
ZJS
448 }
449
450 return 0;
451}
452
0974a682
KS
453static int compare_product(const char *a, const char *b) {
454 size_t x, y;
455
456 assert(a);
457 assert(b);
458
459 x = strcspn(a, " ");
460 y = strcspn(b, " ");
461 if (x != y)
462 return x < y ? -1 : x > y ? 1 : 0;
463
464 return strncmp(a, b, x);
465}
466
467static int compare_version(const char *a, const char *b) {
468 assert(a);
469 assert(b);
470
471 a += strcspn(a, " ");
472 a += strspn(a, " ");
473 b += strcspn(b, " ");
474 b += strspn(b, " ");
475
476 return strverscmp(a, b);
477}
478
175d308c 479static int version_check(int fd_from, const char *from, int fd_to, const char *to) {
d3226d77 480 _cleanup_free_ char *a = NULL, *b = NULL;
0974a682
KS
481 int r;
482
175d308c 483 assert(fd_from >= 0);
0974a682 484 assert(from);
175d308c 485 assert(fd_to >= 0);
0974a682
KS
486 assert(to);
487
175d308c 488 r = get_file_version(fd_from, &a);
0974a682 489 if (r < 0)
d3226d77 490 return r;
baaa35ad
ZJS
491 if (r == 0)
492 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
493 "Source file \"%s\" does not carry version information!",
494 from);
0974a682 495
175d308c 496 r = get_file_version(fd_to, &b);
0974a682 497 if (r < 0)
d3226d77 498 return r;
baaa35ad
ZJS
499 if (r == 0 || compare_product(a, b) != 0)
500 return log_notice_errno(SYNTHETIC_ERRNO(EEXIST),
501 "Skipping \"%s\", since it's owned by another boot loader.",
502 to);
0974a682 503
78d5d4ed
LP
504 if (compare_version(a, b) < 0)
505 return log_warning_errno(SYNTHETIC_ERRNO(ESTALE), "Skipping \"%s\", since a newer boot loader version exists already.", to);
0974a682 506
d3226d77 507 return 0;
0974a682
KS
508}
509
175d308c
LP
510static int copy_file_with_version_check(const char *from, const char *to, bool force) {
511 _cleanup_close_ int fd_from = -1, fd_to = -1;
512 _cleanup_free_ char *t = NULL;
0974a682 513 int r;
0974a682 514
175d308c
LP
515 fd_from = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
516 if (fd_from < 0)
d3226d77 517 return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", from);
0974a682
KS
518
519 if (!force) {
175d308c
LP
520 fd_to = open(to, O_RDONLY|O_CLOEXEC|O_NOCTTY);
521 if (fd_to < 0) {
522 if (errno != -ENOENT)
523 return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", to);
524 } else {
525 r = version_check(fd_from, from, fd_to, to);
526 if (r < 0)
527 return r;
528
529 if (lseek(fd_from, 0, SEEK_SET) == (off_t) -1)
6719ca72 530 return log_error_errno(errno, "Failed to seek in \"%s\": %m", from);
175d308c
LP
531
532 fd_to = safe_close(fd_to);
0974a682 533 }
175d308c 534 }
d3226d77 535
175d308c
LP
536 r = tempfn_random(to, NULL, &t);
537 if (r < 0)
538 return log_oom();
0974a682 539
175d308c
LP
540 RUN_WITH_UMASK(0000) {
541 fd_to = open(t, O_WRONLY|O_CREAT|O_CLOEXEC|O_EXCL|O_NOFOLLOW, 0644);
542 if (fd_to < 0)
543 return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", t);
0974a682
KS
544 }
545
175d308c 546 r = copy_bytes(fd_from, fd_to, (uint64_t) -1, COPY_REFLINK);
0974a682 547 if (r < 0) {
0675e94a
AJ
548 (void) unlink(t);
549 return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
0974a682
KS
550 }
551
adc6f43b 552 (void) copy_times(fd_from, fd_to, 0);
0974a682 553
8ac2f74f 554 if (fsync(fd_to) < 0) {
0675e94a
AJ
555 (void) unlink_noerrno(t);
556 return log_error_errno(errno, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
557 }
558
8ac2f74f
LP
559 (void) fsync_directory_of_file(fd_to);
560
b1c05b98 561 if (renameat(AT_FDCWD, t, AT_FDCWD, to) < 0) {
175d308c
LP
562 (void) unlink_noerrno(t);
563 return log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", t, to);
0974a682
KS
564 }
565
d3226d77 566 log_info("Copied \"%s\" to \"%s\".", from, to);
0974a682 567
175d308c 568 return 0;
0974a682
KS
569}
570
0974a682 571static int mkdir_one(const char *prefix, const char *suffix) {
e2600fd5 572 _cleanup_free_ char *p = NULL;
0974a682 573
e2600fd5 574 p = path_join(prefix, suffix);
0974a682 575 if (mkdir(p, 0700) < 0) {
d3226d77
ZJS
576 if (errno != EEXIST)
577 return log_error_errno(errno, "Failed to create \"%s\": %m", p);
0974a682 578 } else
d3226d77 579 log_info("Created \"%s\".", p);
7b4d7cc0 580
0974a682
KS
581 return 0;
582}
583
fbf45d22 584static const char *const esp_subdirs[] = {
e44c3229 585 /* The directories to place in the ESP */
d3226d77
ZJS
586 "EFI",
587 "EFI/systemd",
00f69504 588 "EFI/BOOT",
d3226d77 589 "loader",
9ee051b9 590 NULL
d3226d77
ZJS
591};
592
e44c3229
LP
593static const char *const dollar_boot_subdirs[] = {
594 /* The directories to place in the XBOOTLDR partition or the ESP, depending what exists */
595 "loader",
596 "loader/entries", /* Type #1 entries */
597 "EFI",
598 "EFI/Linux", /* Type #2 entries */
599 NULL
600};
601
602static int create_subdirs(const char *root, const char * const *subdirs) {
fbf45d22 603 const char *const *i;
0974a682
KS
604 int r;
605
e44c3229
LP
606 STRV_FOREACH(i, subdirs) {
607 r = mkdir_one(root, *i);
d3226d77
ZJS
608 if (r < 0)
609 return r;
610 }
7b4d7cc0 611
7b4d7cc0 612 return 0;
7b4d7cc0
KS
613}
614
0974a682 615static int copy_one_file(const char *esp_path, const char *name, bool force) {
5509f912 616 const char *e;
d3226d77 617 char *p, *q;
0974a682
KS
618 int r;
619
d3226d77
ZJS
620 p = strjoina(BOOTLIBDIR "/", name);
621 q = strjoina(esp_path, "/EFI/systemd/", name);
175d308c 622 r = copy_file_with_version_check(p, q, force);
0974a682 623
5509f912
LP
624 e = startswith(name, "systemd-boot");
625 if (e) {
0974a682 626 int k;
d3226d77 627 char *v;
0974a682
KS
628
629 /* Create the EFI default boot loader name (specified for removable devices) */
5509f912 630 v = strjoina(esp_path, "/EFI/BOOT/BOOT", e);
846b8fc3 631 ascii_strupper(strrchr(v, '/') + 1);
0974a682 632
175d308c 633 k = copy_file_with_version_check(p, v, force);
0974a682 634 if (k < 0 && r == 0)
d3226d77 635 r = k;
0974a682
KS
636 }
637
638 return r;
7b4d7cc0
KS
639}
640
0974a682
KS
641static int install_binaries(const char *esp_path, bool force) {
642 struct dirent *de;
d3226d77 643 _cleanup_closedir_ DIR *d = NULL;
0974a682
KS
644 int r = 0;
645
e7dd673d 646 d = opendir(BOOTLIBDIR);
d3226d77
ZJS
647 if (!d)
648 return log_error_errno(errno, "Failed to open \""BOOTLIBDIR"\": %m");
0974a682 649
0d73a816 650 FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \""BOOTLIBDIR"\": %m")) {
0974a682
KS
651 int k;
652
d3226d77 653 if (!endswith_no_case(de->d_name, ".efi"))
0974a682
KS
654 continue;
655
656 k = copy_one_file(esp_path, de->d_name, force);
657 if (k < 0 && r == 0)
658 r = k;
659 }
660
0974a682 661 return r;
7b4d7cc0
KS
662}
663
b461576d 664static bool same_entry(uint16_t id, sd_id128_t uuid, const char *path) {
d3226d77 665 _cleanup_free_ char *opath = NULL;
0974a682 666 sd_id128_t ouuid;
d3226d77 667 int r;
7b4d7cc0 668
d3226d77
ZJS
669 r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
670 if (r < 0)
0974a682
KS
671 return false;
672 if (!sd_id128_equal(uuid, ouuid))
d3226d77 673 return false;
0974a682 674 if (!streq_ptr(path, opath))
d3226d77 675 return false;
0974a682 676
d3226d77 677 return true;
0974a682
KS
678}
679
680static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
d3226d77
ZJS
681 _cleanup_free_ uint16_t *options = NULL;
682 int n, i;
0974a682 683
d3226d77
ZJS
684 n = efi_get_boot_options(&options);
685 if (n < 0)
686 return n;
0974a682 687
e7dd673d 688 /* find already existing systemd-boot entry */
d3226d77 689 for (i = 0; i < n; i++)
0974a682 690 if (same_entry(options[i], uuid, path)) {
d3226d77
ZJS
691 *id = options[i];
692 return 1;
0974a682
KS
693 }
694
695 /* find free slot in the sorted BootXXXX variable list */
d3226d77 696 for (i = 0; i < n; i++)
0974a682 697 if (i != options[i]) {
d3226d77
ZJS
698 *id = i;
699 return 1;
0974a682
KS
700 }
701
702 /* use the next one */
703 if (i == 0xffff)
704 return -ENOSPC;
d3226d77
ZJS
705 *id = i;
706 return 0;
0974a682
KS
707}
708
709static int insert_into_order(uint16_t slot, bool first) {
d3226d77
ZJS
710 _cleanup_free_ uint16_t *order = NULL;
711 uint16_t *t;
712 int n, i;
0974a682 713
d3226d77
ZJS
714 n = efi_get_boot_order(&order);
715 if (n <= 0)
0974a682 716 /* no entry, add us */
d3226d77 717 return efi_set_boot_order(&slot, 1);
0974a682
KS
718
719 /* are we the first and only one? */
d3226d77
ZJS
720 if (n == 1 && order[0] == slot)
721 return 0;
0974a682
KS
722
723 /* are we already in the boot order? */
d3226d77 724 for (i = 0; i < n; i++) {
0974a682
KS
725 if (order[i] != slot)
726 continue;
727
728 /* we do not require to be the first one, all is fine */
729 if (!first)
d3226d77 730 return 0;
0974a682
KS
731
732 /* move us to the first slot */
d3226d77 733 memmove(order + 1, order, i * sizeof(uint16_t));
0974a682 734 order[0] = slot;
d3226d77 735 return efi_set_boot_order(order, n);
0974a682
KS
736 }
737
738 /* extend array */
a7798cd8 739 t = reallocarray(order, n + 1, sizeof(uint16_t));
d3226d77
ZJS
740 if (!t)
741 return -ENOMEM;
742 order = t;
0974a682
KS
743
744 /* add us to the top or end of the list */
745 if (first) {
d3226d77 746 memmove(order + 1, order, n * sizeof(uint16_t));
0974a682
KS
747 order[0] = slot;
748 } else
d3226d77 749 order[n] = slot;
0974a682 750
d3226d77 751 return efi_set_boot_order(order, n + 1);
0974a682
KS
752}
753
754static int remove_from_order(uint16_t slot) {
7cb0f263 755 _cleanup_free_ uint16_t *order = NULL;
d3226d77 756 int n, i;
0974a682 757
d3226d77
ZJS
758 n = efi_get_boot_order(&order);
759 if (n <= 0)
760 return n;
0974a682 761
d3226d77 762 for (i = 0; i < n; i++) {
0974a682
KS
763 if (order[i] != slot)
764 continue;
765
d3226d77
ZJS
766 if (i + 1 < n)
767 memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
768 return efi_set_boot_order(order, n - 1);
0974a682
KS
769 }
770
d3226d77 771 return 0;
0974a682
KS
772}
773
774static int install_variables(const char *esp_path,
775 uint32_t part, uint64_t pstart, uint64_t psize,
776 sd_id128_t uuid, const char *path,
777 bool first) {
270384b2 778 const char *p;
0974a682
KS
779 uint16_t slot;
780 int r;
781
782 if (!is_efi_boot()) {
d3226d77 783 log_warning("Not booted with EFI, skipping EFI variable setup.");
0974a682
KS
784 return 0;
785 }
786
270384b2 787 p = prefix_roota(esp_path, path);
0974a682
KS
788 if (access(p, F_OK) < 0) {
789 if (errno == ENOENT)
d3226d77 790 return 0;
f939cff7
LP
791
792 return log_error_errno(errno, "Cannot access \"%s\": %m", p);
0974a682 793 }
7b4d7cc0 794
0974a682 795 r = find_slot(uuid, path, &slot);
d3226d77
ZJS
796 if (r < 0)
797 return log_error_errno(r,
798 r == -ENOENT ?
799 "Failed to access EFI variables. Is the \"efivarfs\" filesystem mounted?" :
800 "Failed to determine current boot order: %m");
7b4d7cc0 801
181ccb43 802 if (first || r == 0) {
0974a682
KS
803 r = efi_add_boot_option(slot, "Linux Boot Manager",
804 part, pstart, psize,
805 uuid, path);
d3226d77
ZJS
806 if (r < 0)
807 return log_error_errno(r, "Failed to create EFI Boot variable entry: %m");
0974a682 808
d3226d77
ZJS
809 log_info("Created EFI boot entry \"Linux Boot Manager\".");
810 }
0974a682 811
d3226d77 812 return insert_into_order(slot, first);
0974a682
KS
813}
814
815static int remove_boot_efi(const char *esp_path) {
d3226d77 816 _cleanup_closedir_ DIR *d = NULL;
0974a682 817 struct dirent *de;
270384b2 818 const char *p;
d3226d77 819 int r, c = 0;
0974a682 820
270384b2 821 p = prefix_roota(esp_path, "/EFI/BOOT");
0974a682
KS
822 d = opendir(p);
823 if (!d) {
d3226d77
ZJS
824 if (errno == ENOENT)
825 return 0;
0974a682 826
d3226d77 827 return log_error_errno(errno, "Failed to open directory \"%s\": %m", p);
0974a682
KS
828 }
829
e41256dc 830 FOREACH_DIRENT(de, d, break) {
d3226d77
ZJS
831 _cleanup_close_ int fd = -1;
832 _cleanup_free_ char *v = NULL;
0974a682 833
d3226d77 834 if (!endswith_no_case(de->d_name, ".efi"))
0974a682
KS
835 continue;
836
b7536c45 837 if (!startswith_no_case(de->d_name, "boot"))
0974a682
KS
838 continue;
839
d3226d77 840 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
dd114e11 841 if (fd < 0)
d3226d77 842 return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name);
0974a682 843
d3226d77 844 r = get_file_version(fd, &v);
0974a682 845 if (r < 0)
d3226d77
ZJS
846 return r;
847 if (r > 0 && startswith(v, "systemd-boot ")) {
848 r = unlinkat(dirfd(d), de->d_name, 0);
849 if (r < 0)
850 return log_error_errno(errno, "Failed to remove \"%s/%s\": %m", p, de->d_name);
851
a592ab6a 852 log_info("Removed \"%s/%s\".", p, de->d_name);
0974a682
KS
853 }
854
855 c++;
0974a682
KS
856 }
857
d3226d77 858 return c;
0974a682
KS
859}
860
861static int rmdir_one(const char *prefix, const char *suffix) {
270384b2 862 const char *p;
7b4d7cc0 863
270384b2 864 p = prefix_roota(prefix, suffix);
0974a682 865 if (rmdir(p) < 0) {
fbf45d22
LP
866 bool ignore = IN_SET(errno, ENOENT, ENOTEMPTY);
867
868 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, errno,
869 "Failed to remove directory \"%s\": %m", p);
870 if (!ignore)
871 return -errno;
7b4d7cc0 872 } else
d3226d77 873 log_info("Removed \"%s\".", p);
7b4d7cc0 874
0974a682 875 return 0;
7b4d7cc0
KS
876}
877
e44c3229
LP
878static int remove_subdirs(const char *root, const char *const *subdirs) {
879 int r, q;
fbf45d22 880
e44c3229
LP
881 /* We use recursion here to destroy the directories in reverse order. Which should be safe given how
882 * short the array is. */
fbf45d22 883
e44c3229
LP
884 if (!subdirs[0]) /* A the end of the list */
885 return 0;
fbf45d22 886
e44c3229
LP
887 r = remove_subdirs(root, subdirs + 1);
888 q = rmdir_one(root, subdirs[0]);
889
890 return r < 0 ? r : q;
891}
892
893static int remove_machine_id_directory(const char *root, sd_id128_t machine_id) {
894 char buf[SD_ID128_STRING_MAX];
895
896 assert(root);
897
898 return rmdir_one(root, sd_id128_to_string(machine_id, buf));
fbf45d22
LP
899}
900
0974a682 901static int remove_binaries(const char *esp_path) {
270384b2 902 const char *p;
0974a682
KS
903 int r, q;
904
270384b2 905 p = prefix_roota(esp_path, "/EFI/systemd");
c6878637 906 r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
0974a682
KS
907
908 q = remove_boot_efi(esp_path);
909 if (q < 0 && r == 0)
910 r = q;
911
0974a682
KS
912 return r;
913}
914
e44c3229 915static int remove_file(const char *root, const char *file) {
fbf45d22
LP
916 const char *p;
917
e44c3229
LP
918 assert(root);
919 assert(file);
fbf45d22 920
e44c3229 921 p = prefix_roota(root, file);
fbf45d22 922 if (unlink(p) < 0) {
e44c3229
LP
923 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
924 "Failed to unlink file \"%s\": %m", p);
fbf45d22 925
e44c3229
LP
926 return errno == ENOENT ? 0 : -errno;
927 }
fbf45d22 928
e44c3229
LP
929 log_info("Removed \"%s\".", p);
930 return 1;
fbf45d22
LP
931}
932
0974a682
KS
933static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
934 uint16_t slot;
935 int r;
936
937 if (!is_efi_boot())
938 return 0;
939
940 r = find_slot(uuid, path, &slot);
941 if (r != 1)
942 return 0;
943
944 r = efi_remove_boot_option(slot);
945 if (r < 0)
946 return r;
947
948 if (in_order)
d3226d77 949 return remove_from_order(slot);
f939cff7
LP
950
951 return 0;
0974a682
KS
952}
953
e44c3229
LP
954static int remove_loader_variables(void) {
955 const char *p;
956 int r = 0;
957
958 /* Remove all persistent loader variables we define */
959
960 FOREACH_STRING(p,
961 "LoaderConfigTimeout",
962 "LoaderConfigTimeoutOneShot",
963 "LoaderEntryDefault",
964 "LoaderEntryOneShot",
965 "LoaderSystemToken") {
966
967 int q;
968
969 q = efi_set_variable(EFI_VENDOR_LOADER, p, NULL, 0);
970 if (q == -ENOENT)
971 continue;
972 if (q < 0) {
973 log_warning_errno(q, "Failed to remove %s variable: %m", p);
974 if (r >= 0)
975 r = q;
976 } else
977 log_info("Removed EFI variable %s.", p);
978 }
979
980 return r;
981}
982
341890de 983static int install_loader_config(const char *esp_path, sd_id128_t machine_id) {
d5ff6d6d 984 char machine_string[SD_ID128_STRING_MAX];
f5b84de2
LP
985 _cleanup_(unlink_and_freep) char *t = NULL;
986 _cleanup_fclose_ FILE *f = NULL;
d5ff6d6d 987 const char *p;
f5b84de2 988 int r, fd;
0974a682 989
270384b2 990 p = prefix_roota(esp_path, "/loader/loader.conf");
f5b84de2
LP
991 if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
992 return 0;
993
994 fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
995 if (fd < 0)
996 return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p);
997
e92aaed3 998 f = fdopen(fd, "w");
f5b84de2
LP
999 if (!f) {
1000 safe_close(fd);
1001 return log_oom();
1002 }
0974a682 1003
a36b411e
LP
1004 fprintf(f, "#timeout 3\n"
1005 "#console-mode keep\n"
1006 "default %s-*\n", sd_id128_to_string(machine_id, machine_string));
0974a682 1007
0675e94a 1008 r = fflush_sync_and_check(f);
d5ff6d6d
LP
1009 if (r < 0)
1010 return log_error_errno(r, "Failed to write \"%s\": %m", p);
0974a682 1011
f5b84de2
LP
1012 r = link_tmpfile(fd, t, p);
1013 if (r == -EEXIST)
1014 return 0; /* Silently skip creation if the file exists now (recheck) */
1015 if (r < 0)
1016 return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
1017
1018 t = mfree(t);
f5b84de2 1019 return 1;
0974a682
KS
1020}
1021
e44c3229 1022static int install_machine_id_directory(const char *root, sd_id128_t machine_id) {
341890de
ZJS
1023 char buf[SD_ID128_STRING_MAX];
1024
e44c3229 1025 assert(root);
341890de 1026
e44c3229 1027 return mkdir_one(root, sd_id128_to_string(machine_id, buf));
fbf45d22
LP
1028}
1029
2f2c539c 1030static int help(int argc, char *argv[], void *userdata) {
37ec0fdd
LP
1031 _cleanup_free_ char *link = NULL;
1032 int r;
1033
1034 r = terminal_urlify_man("bootctl", "1", &link);
1035 if (r < 0)
1036 return log_oom();
2f2c539c 1037
37ec0fdd 1038 printf("%s [COMMAND] [OPTIONS...]\n\n"
e1fac8a6 1039 "Install, update or remove the systemd-boot EFI boot manager.\n"
d88c96ff 1040 "\nBoot Loader Commands:\n"
fbf45d22
LP
1041 " status Show status of installed systemd-boot and EFI variables\n"
1042 " install Install systemd-boot to the ESP and EFI variables\n"
1043 " update Update systemd-boot in the ESP and EFI variables\n"
1044 " remove Remove systemd-boot from the ESP and EFI variables\n"
a2aa605d 1045 " is-installed Test whether systemd-boot is installed in the ESP\n"
4e5aa791
ZJS
1046 " random-seed Initialize random seed in ESP and EFI variables\n"
1047 " system-options Query or set system options string in EFI variable\n"
d88c96ff 1048 "\nBoot Loader Entries Commands:\n"
fbf45d22
LP
1049 " list List boot loader entries\n"
1050 " set-default ID Set default boot loader entry\n"
1051 " set-oneshot ID Set default boot loader entry, for next boot only\n"
e1fac8a6
ZJS
1052 " -h --help Show this help\n"
1053 " --version Print version\n"
1054 " --esp-path=PATH Path to the EFI System Partition (ESP)\n"
1055 " --boot-path=PATH Path to the $BOOT partition\n"
1056 " -p --print-esp-path Print path to the EFI System Partition\n"
1057 " -x --print-boot-path Print path to the $BOOT partition\n"
1058 " --no-variables Don't touch EFI variables\n"
1059 " --no-pager Do not pipe output into a pager\n"
37ec0fdd
LP
1060 "\nSee the %s for details.\n"
1061 , program_invocation_short_name
d88c96ff 1062 , link);
0974a682
KS
1063
1064 return 0;
1065}
1066
0974a682
KS
1067static int parse_argv(int argc, char *argv[]) {
1068 enum {
fbf45d22
LP
1069 ARG_ESP_PATH = 0x100,
1070 ARG_BOOT_PATH,
0974a682
KS
1071 ARG_VERSION,
1072 ARG_NO_VARIABLES,
57db6f18 1073 ARG_NO_PAGER,
7b4d7cc0
KS
1074 };
1075
0974a682 1076 static const struct option options[] = {
fbf45d22
LP
1077 { "help", no_argument, NULL, 'h' },
1078 { "version", no_argument, NULL, ARG_VERSION },
1079 { "esp-path", required_argument, NULL, ARG_ESP_PATH },
1080 { "path", required_argument, NULL, ARG_ESP_PATH }, /* Compatibility alias */
1081 { "boot-path", required_argument, NULL, ARG_BOOT_PATH },
1082 { "print-esp-path", no_argument, NULL, 'p' },
1083 { "print-path", no_argument, NULL, 'p' }, /* Compatibility alias */
fba4e945 1084 { "print-boot-path", no_argument, NULL, 'x' },
fbf45d22
LP
1085 { "no-variables", no_argument, NULL, ARG_NO_VARIABLES },
1086 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
a36b411e 1087 {}
0974a682
KS
1088 };
1089
2f2c539c 1090 int c, r;
7b4d7cc0
KS
1091
1092 assert(argc >= 0);
1093 assert(argv);
1094
fba4e945 1095 while ((c = getopt_long(argc, argv, "hpx", options, NULL)) >= 0)
0974a682 1096 switch (c) {
7b4d7cc0 1097
0974a682 1098 case 'h':
2f2c539c 1099 help(0, NULL, NULL);
7b4d7cc0 1100 return 0;
7b4d7cc0 1101
0974a682 1102 case ARG_VERSION:
3f6fd1ba 1103 return version();
7b4d7cc0 1104
fbf45d22
LP
1105 case ARG_ESP_PATH:
1106 r = free_and_strdup(&arg_esp_path, optarg);
1107 if (r < 0)
1108 return log_oom();
1109 break;
1110
1111 case ARG_BOOT_PATH:
1112 r = free_and_strdup(&arg_xbootldr_path, optarg);
2f2c539c
LP
1113 if (r < 0)
1114 return log_oom();
0974a682
KS
1115 break;
1116
30b50477 1117 case 'p':
aa467bca
ZJS
1118 if (arg_print_dollar_boot_path)
1119 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1120 "--print-boot-path/-x cannot be combined with --print-esp-path/-p");
fbf45d22
LP
1121 arg_print_esp_path = true;
1122 break;
1123
fba4e945 1124 case 'x':
aa467bca
ZJS
1125 if (arg_print_esp_path)
1126 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1127 "--print-boot-path/-x cannot be combined with --print-esp-path/-p");
fbf45d22 1128 arg_print_dollar_boot_path = true;
30b50477
ZJS
1129 break;
1130
0974a682
KS
1131 case ARG_NO_VARIABLES:
1132 arg_touch_variables = false;
1133 break;
1134
57db6f18 1135 case ARG_NO_PAGER:
0221d68a 1136 arg_pager_flags |= PAGER_DISABLE;
57db6f18
LP
1137 break;
1138
0974a682
KS
1139 case '?':
1140 return -EINVAL;
1141
1142 default:
d3226d77 1143 assert_not_reached("Unknown option");
7b4d7cc0 1144 }
7b4d7cc0 1145
0974a682
KS
1146 return 1;
1147}
7b4d7cc0 1148
551710cf
ZJS
1149static void read_loader_efi_var(const char *name, char **var) {
1150 int r;
1151
1152 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var);
1153 if (r < 0 && r != -ENOENT)
1154 log_warning_errno(r, "Failed to read EFI variable %s: %m", name);
1155}
1156
2f2c539c 1157static int verb_status(int argc, char *argv[], void *userdata) {
fbf45d22 1158 sd_id128_t esp_uuid = SD_ID128_NULL, xbootldr_uuid = SD_ID128_NULL;
46fb255b 1159 int r, k;
0974a682 1160
fbf45d22
LP
1161 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &esp_uuid);
1162 if (arg_print_esp_path) {
5caa3167
LP
1163 if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only
1164 * error the find_esp_and_warn() won't log on its own) */
fbf45d22 1165 return log_error_errno(r, "Failed to determine ESP location: %m");
30b50477
ZJS
1166 if (r < 0)
1167 return r;
1168
fbf45d22
LP
1169 puts(arg_esp_path);
1170 }
1171
1172 r = acquire_xbootldr(geteuid() != 0, &xbootldr_uuid);
1173 if (arg_print_dollar_boot_path) {
1174 if (r == -EACCES)
1175 return log_error_errno(r, "Failed to determine XBOOTLDR location: %m");
1176 if (r < 0)
1177 return r;
1178
f8a2b09a
YW
1179 const char *path = arg_dollar_boot_path();
1180 if (!path)
1181 return log_error_errno(SYNTHETIC_ERRNO(EACCES), "Failed to determine XBOOTLDR location: %m");
1182
1183 puts(path);
30b50477 1184 }
551710cf 1185
fbf45d22
LP
1186 if (arg_print_esp_path || arg_print_dollar_boot_path)
1187 return 0;
1188
5caa3167
LP
1189 r = 0; /* If we couldn't determine the path, then don't consider that a problem from here on, just show what we
1190 * can show */
1191
0221d68a 1192 (void) pager_open(arg_pager_flags);
57db6f18 1193
2f2c539c 1194 if (is_efi_boot()) {
80641a81
LP
1195 static const struct {
1196 uint64_t flag;
1197 const char *name;
1198 } flags[] = {
22c5ff51
LP
1199 { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" },
1200 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" },
1201 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" },
1202 { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" },
1203 { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" },
1204 { EFI_LOADER_FEATURE_XBOOTLDR, "Support for XBOOTLDR partition" },
1205 { EFI_LOADER_FEATURE_RANDOM_SEED, "Support for passing random seed to OS" },
80641a81
LP
1206 };
1207
81375b9b 1208 _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL;
25579a43 1209 sd_id128_t loader_part_uuid = SD_ID128_NULL;
80641a81
LP
1210 uint64_t loader_features = 0;
1211 size_t i;
0974a682 1212
2f2c539c
LP
1213 read_loader_efi_var("LoaderFirmwareType", &fw_type);
1214 read_loader_efi_var("LoaderFirmwareInfo", &fw_info);
1215 read_loader_efi_var("LoaderInfo", &loader);
81375b9b 1216 read_loader_efi_var("StubInfo", &stub);
2f2c539c 1217 read_loader_efi_var("LoaderImageIdentifier", &loader_path);
80641a81 1218 (void) efi_loader_get_features(&loader_features);
551710cf 1219
2f2c539c
LP
1220 if (loader_path)
1221 efi_tilt_backslashes(loader_path);
1222
46fb255b
ZJS
1223 k = efi_loader_get_device_part_uuid(&loader_part_uuid);
1224 if (k < 0 && k != -ENOENT)
1225 r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m");
2f2c539c
LP
1226
1227 printf("System:\n");
ba857253 1228 printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal());
33987ba0
YW
1229 printf(" Secure Boot: %sd\n", enable_disable(is_efi_secure_boot()));
1230 printf(" Setup Mode: %s\n", is_efi_secure_boot_setup_mode() ? "setup" : "user");
2f2c539c
LP
1231 printf("\n");
1232
7fd66f3c 1233 printf("Current Boot Loader:\n");
ba857253 1234 printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal());
80641a81
LP
1235
1236 for (i = 0; i < ELEMENTSOF(flags); i++) {
1237
1238 if (i == 0)
1239 printf(" Features: ");
1240 else
1241 printf(" ");
1242
1243 if (FLAGS_SET(loader_features, flags[i].flag))
9a6f746f 1244 printf("%s%s%s %s\n", ansi_highlight_green(), special_glyph(SPECIAL_GLYPH_CHECK_MARK), ansi_normal(), flags[i].name);
80641a81 1245 else
9a6f746f 1246 printf("%s%s%s %s\n", ansi_highlight_red(), special_glyph(SPECIAL_GLYPH_CROSS_MARK), ansi_normal(), flags[i].name);
80641a81
LP
1247 }
1248
81375b9b
ДГ
1249 if (stub)
1250 printf(" Stub: %s\n", stub);
e28973ee 1251 if (!sd_id128_is_null(loader_part_uuid))
bd44566c 1252 printf(" ESP: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
2f2c539c
LP
1253 SD_ID128_FORMAT_VAL(loader_part_uuid));
1254 else
cd2d4c7f 1255 printf(" ESP: n/a\n");
9a6f746f 1256 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), strna(loader_path));
2f2c539c 1257 printf("\n");
d6e9a347
LP
1258
1259 printf("Random Seed:\n");
1260 printf(" Passed to OS: %s\n", yes_no(access("/sys/firmware/efi/efivars/LoaderRandomSeed-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) >= 0));
1261 printf(" System Token: %s\n", access("/sys/firmware/efi/efivars/LoaderSystemToken-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) >= 0 ? "set" : "not set");
1262
1263 if (arg_esp_path) {
1264 _cleanup_free_ char *p = NULL;
1265
1266 p = path_join(arg_esp_path, "/loader/random-seed");
1267 if (!p)
1268 return log_oom();
1269
1270 printf(" Exists: %s\n", yes_no(access(p, F_OK) >= 0));
1271 }
1272
1273 printf("\n");
2f2c539c 1274 } else
cd2d4c7f 1275 printf("System:\n Not booted with EFI\n\n");
7b4d7cc0 1276
fbf45d22
LP
1277 if (arg_esp_path) {
1278 k = status_binaries(arg_esp_path, esp_uuid);
ecec2a5d
LP
1279 if (k < 0)
1280 r = k;
1281 }
2f2c539c 1282
cd2d4c7f 1283 if (is_efi_boot()) {
46fb255b
ZJS
1284 k = status_variables();
1285 if (k < 0)
1286 r = k;
cd2d4c7f 1287 }
0974a682 1288
fbf45d22 1289 if (arg_esp_path || arg_xbootldr_path) {
81fed855 1290 k = status_entries(arg_esp_path, esp_uuid, arg_xbootldr_path, xbootldr_uuid);
ecec2a5d
LP
1291 if (k < 0)
1292 r = k;
1293 }
7e87c7d9 1294
46fb255b 1295 return r;
2f2c539c 1296}
7b4d7cc0 1297
7e87c7d9 1298static int verb_list(int argc, char *argv[], void *userdata) {
5caa3167 1299 _cleanup_(boot_config_free) BootConfig config = {};
5caa3167 1300 int r;
7e87c7d9 1301
5caa3167
LP
1302 /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two things: turn
1303 * off logging about access errors and turn off potentially privileged device probing. Here we're interested in
1304 * the latter but not the former, hence request the mode, and log about EACCES. */
7e87c7d9 1305
fbf45d22 1306 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, NULL);
5caa3167
LP
1307 if (r == -EACCES) /* We really need the ESP path for this call, hence also log about access errors */
1308 return log_error_errno(r, "Failed to determine ESP: %m");
7e87c7d9
ZJS
1309 if (r < 0)
1310 return r;
1311
fbf45d22
LP
1312 r = acquire_xbootldr(geteuid() != 0, NULL);
1313 if (r == -EACCES)
1314 return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
1315 if (r < 0)
1316 return r;
1317
1318 r = boot_entries_load_config(arg_esp_path, arg_xbootldr_path, &config);
7e87c7d9 1319 if (r < 0)
21f7a622 1320 return r;
7e87c7d9 1321
93f14ce2 1322 (void) boot_entries_augment_from_loader(&config, false);
bd2865ca 1323
20a28174
LP
1324 if (config.n_entries == 0)
1325 log_info("No boot loader entries found.");
1326 else {
1327 size_t n;
7e87c7d9 1328
0221d68a 1329 (void) pager_open(arg_pager_flags);
57db6f18 1330
20a28174 1331 printf("Boot Loader Entries:\n");
7e87c7d9 1332
20a28174
LP
1333 for (n = 0; n < config.n_entries; n++) {
1334 r = boot_entry_show(config.entries + n, n == (size_t) config.default_entry);
1335 if (r < 0)
1336 return r;
7e87c7d9 1337
4629499e
LP
1338 if (n+1 < config.n_entries)
1339 putchar('\n');
7e87c7d9 1340 }
cd2d4c7f 1341 }
0974a682 1342
7e87c7d9 1343 return 0;
2f2c539c 1344}
7b4d7cc0 1345
e44c3229
LP
1346static int install_random_seed(const char *esp) {
1347 _cleanup_(unlink_and_freep) char *tmp = NULL;
1348 _cleanup_free_ void *buffer = NULL;
1349 _cleanup_free_ char *path = NULL;
1350 _cleanup_close_ int fd = -1;
1351 size_t sz, token_size;
1352 ssize_t n;
1353 int r;
1354
1355 assert(esp);
1356
1357 path = path_join(esp, "/loader/random-seed");
1358 if (!path)
1359 return log_oom();
1360
1361 sz = random_pool_size();
1362
1363 buffer = malloc(sz);
1364 if (!buffer)
1365 return log_oom();
1366
1367 r = genuine_random_bytes(buffer, sz, RANDOM_BLOCK);
1368 if (r < 0)
a4d20801 1369 return log_error_errno(r, "Failed to acquire random seed: %m");
e44c3229
LP
1370
1371 r = tempfn_random(path, "bootctl", &tmp);
1372 if (r < 0)
1373 return log_oom();
1374
1375 fd = open(tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|O_WRONLY|O_CLOEXEC, 0600);
1376 if (fd < 0) {
1377 tmp = mfree(tmp);
1378 return log_error_errno(fd, "Failed to open random seed file for writing: %m");
1379 }
1380
1381 n = write(fd, buffer, sz);
1382 if (n < 0)
1383 return log_error_errno(errno, "Failed to write random seed file: %m");
1384 if ((size_t) n != sz)
1385 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write while writing random seed file.");
1386
1387 if (rename(tmp, path) < 0)
1388 return log_error_errno(r, "Failed to move random seed file into place: %m");
1389
1390 tmp = mfree(tmp);
1391
a4d20801 1392 log_info("Random seed file %s successfully written (%zu bytes).", path, sz);
e44c3229
LP
1393
1394 if (!arg_touch_variables)
1395 return 0;
1396
1397 if (!is_efi_boot()) {
1398 log_notice("Not booted with EFI, skipping EFI variable setup.");
1399 return 0;
1400 }
1401
1402 r = getenv_bool("SYSTEMD_WRITE_SYSTEM_TOKEN");
1403 if (r < 0) {
1404 if (r != -ENXIO)
1405 log_warning_errno(r, "Failed to parse $SYSTEMD_WRITE_SYSTEM_TOKEN, ignoring.");
1406
1407 if (detect_vm() > 0) {
1408 /* Let's not write a system token if we detect we are running in a VM
1409 * environment. Why? Our default security model for the random seed uses the system
1410 * token as a mechanism to ensure we are not vulnerable to golden master sloppiness
1411 * issues, i.e. that people initialize the random seed file, then copy the image to
1412 * many systems and end up with the same random seed in each that is assumed to be
1413 * valid but in reality is the same for all machines. By storing a system token in
1414 * the EFI variable space we can make sure that even though the random seeds on disk
1415 * are all the same they will be different on each system under the assumption that
1416 * the EFI variable space is maintained separate from the random seed storage. That
1417 * is generally the case on physical systems, as the ESP is stored on persistant
1418 * storage, and the EFI variables in NVRAM. However in virtualized environments this
1419 * is generally not true: the EFI variable set is typically stored along with the
1420 * disk image itself. For example, using the OVMF EFI firmware the EFI variables are
1421 * stored in a file in the ESP itself. */
1422
1423 log_notice("Not installing system token, since we are running in a virtualized environment.");
1424 return 0;
1425 }
1426 } else if (r == 0) {
1427 log_notice("Not writing system token, because $SYSTEMD_WRITE_SYSTEM_TOKEN is set to false.");
1428 return 0;
1429 }
1430
1431 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderSystemToken", NULL, NULL, &token_size);
1432 if (r < 0) {
1433 if (r != -ENOENT)
1434 return log_error_errno(r, "Failed to test system token validity: %m");
1435 } else {
1436 if (token_size >= sz) {
1437 /* Let's avoid writes if we can, and initialize this only once. */
1438 log_debug("System token already written, not updating.");
1439 return 0;
1440 }
1441
1442 log_debug("Existing system token size (%zu) does not match our expectations (%zu), replacing.", token_size, sz);
1443 }
1444
1445 r = genuine_random_bytes(buffer, sz, RANDOM_BLOCK);
1446 if (r < 0)
1447 return log_error_errno(r, "Failed to acquire random seed: %m");
1448
1449 /* Let's write this variable with an umask in effect, so that unprivileged users can't see the token
1450 * and possibly get identification information or too much insight into the kernel's entropy pool
1451 * state. */
1452 RUN_WITH_UMASK(0077) {
1453 r = efi_set_variable(EFI_VENDOR_LOADER, "LoaderSystemToken", buffer, sz);
1454 if (r < 0)
1455 return log_error_errno(r, "Failed to set LoaderSystemToken EFI variable: %m");
1456 }
1457
1458 log_info("Successfully initialized system token in EFI variable with %zu bytes.", sz);
1459 return 0;
1460}
1461
fbf45d22
LP
1462static int sync_everything(void) {
1463 int ret = 0, k;
e0e8d177 1464
fbf45d22
LP
1465 if (arg_esp_path) {
1466 k = syncfs_path(AT_FDCWD, arg_esp_path);
1467 if (k < 0)
1468 ret = log_error_errno(k, "Failed to synchronize the ESP '%s': %m", arg_esp_path);
1469 }
e0e8d177 1470
fbf45d22
LP
1471 if (arg_xbootldr_path) {
1472 k = syncfs_path(AT_FDCWD, arg_xbootldr_path);
1473 if (k < 0)
1474 ret = log_error_errno(k, "Failed to synchronize $BOOT '%s': %m", arg_xbootldr_path);
1475 }
e0e8d177 1476
fbf45d22 1477 return ret;
e0e8d177
LP
1478}
1479
2f2c539c 1480static int verb_install(int argc, char *argv[], void *userdata) {
2f2c539c
LP
1481 sd_id128_t uuid = SD_ID128_NULL;
1482 uint64_t pstart = 0, psize = 0;
1483 uint32_t part = 0;
341890de 1484 sd_id128_t machine_id;
2f2c539c
LP
1485 bool install;
1486 int r;
1487
5caa3167 1488 r = acquire_esp(false, &part, &pstart, &psize, &uuid);
2f2c539c
LP
1489 if (r < 0)
1490 return r;
1491
fbf45d22
LP
1492 r = acquire_xbootldr(false, NULL);
1493 if (r < 0)
1494 return r;
1495
341890de
ZJS
1496 r = sd_id128_get_machine(&machine_id);
1497 if (r < 0)
1498 return log_error_errno(r, "Failed to get machine id: %m");
1499
2f2c539c
LP
1500 install = streq(argv[0], "install");
1501
1502 RUN_WITH_UMASK(0002) {
fbf45d22
LP
1503 if (install) {
1504 /* Don't create any of these directories when we are just updating. When we update
1505 * we'll drop-in our files (unless there are newer ones already), but we won't create
1506 * the directories for them in the first place. */
e44c3229
LP
1507 r = create_subdirs(arg_esp_path, esp_subdirs);
1508 if (r < 0)
1509 return r;
1510
1511 r = create_subdirs(arg_dollar_boot_path(), dollar_boot_subdirs);
fbf45d22
LP
1512 if (r < 0)
1513 return r;
1514 }
1515
1516 r = install_binaries(arg_esp_path, install);
0974a682 1517 if (r < 0)
d3226d77 1518 return r;
0974a682 1519
2f2c539c 1520 if (install) {
341890de 1521 r = install_loader_config(arg_esp_path, machine_id);
fbf45d22
LP
1522 if (r < 0)
1523 return r;
1524
e44c3229
LP
1525 r = install_machine_id_directory(arg_dollar_boot_path(), machine_id);
1526 if (r < 0)
1527 return r;
1528
1529 r = install_random_seed(arg_esp_path);
d3226d77
ZJS
1530 if (r < 0)
1531 return r;
1532 }
2f2c539c 1533 }
0974a682 1534
fbf45d22 1535 (void) sync_everything();
e0e8d177 1536
2f2c539c 1537 if (arg_touch_variables)
fbf45d22 1538 r = install_variables(arg_esp_path,
2f2c539c
LP
1539 part, pstart, psize, uuid,
1540 "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi",
1541 install);
7b4d7cc0 1542
2f2c539c
LP
1543 return r;
1544}
0974a682 1545
2f2c539c 1546static int verb_remove(int argc, char *argv[], void *userdata) {
e44c3229 1547 sd_id128_t uuid = SD_ID128_NULL, machine_id;
fbf45d22 1548 int r, q;
0974a682 1549
5caa3167 1550 r = acquire_esp(false, NULL, NULL, NULL, &uuid);
2f2c539c
LP
1551 if (r < 0)
1552 return r;
1553
fbf45d22
LP
1554 r = acquire_xbootldr(false, NULL);
1555 if (r < 0)
1556 return r;
2f2c539c 1557
e44c3229
LP
1558 r = sd_id128_get_machine(&machine_id);
1559 if (r < 0)
1560 return log_error_errno(r, "Failed to get machine id: %m");
1561
fbf45d22 1562 r = remove_binaries(arg_esp_path);
e0e8d177 1563
e44c3229 1564 q = remove_file(arg_esp_path, "/loader/loader.conf");
fbf45d22
LP
1565 if (q < 0 && r >= 0)
1566 r = q;
1567
e44c3229 1568 q = remove_file(arg_esp_path, "/loader/random-seed");
fbf45d22
LP
1569 if (q < 0 && r >= 0)
1570 r = q;
2f2c539c 1571
e44c3229 1572 q = remove_subdirs(arg_esp_path, esp_subdirs);
fbf45d22
LP
1573 if (q < 0 && r >= 0)
1574 r = q;
1575
e44c3229
LP
1576 q = remove_subdirs(arg_esp_path, dollar_boot_subdirs);
1577 if (q < 0 && r >= 0)
1578 r = q;
fbf45d22 1579
e44c3229
LP
1580 q = remove_machine_id_directory(arg_esp_path, machine_id);
1581 if (q < 0 && r >= 0)
1582 r = 1;
1583
1584 if (arg_xbootldr_path) {
1585 /* Remove the latter two also in the XBOOTLDR partition if it exists */
1586 q = remove_subdirs(arg_xbootldr_path, dollar_boot_subdirs);
1587 if (q < 0 && r >= 0)
1588 r = q;
1589
1590 q = remove_machine_id_directory(arg_xbootldr_path, machine_id);
fbf45d22 1591 if (q < 0 && r >= 0)
2f2c539c 1592 r = q;
7b4d7cc0
KS
1593 }
1594
e44c3229
LP
1595 (void) sync_everything();
1596
1597 if (!arg_touch_variables)
1598 return r;
1599
1600 q = remove_variables(uuid, "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi", true);
1601 if (q < 0 && r >= 0)
1602 r = q;
1603
1604 q = remove_loader_variables();
1605 if (q < 0 && r >= 0)
1606 r = q;
1607
d3226d77 1608 return r;
7b4d7cc0
KS
1609}
1610
a2aa605d
LP
1611static int verb_is_installed(int argc, char *argv[], void *userdata) {
1612 _cleanup_free_ char *p = NULL;
1613 int r;
1614
1615 r = acquire_esp(false, NULL, NULL, NULL, NULL);
1616 if (r < 0)
1617 return r;
1618
1619 /* Tests whether systemd-boot is installed. It's not obvious what to use as check here: we could
1620 * check EFI variables, we could check what binary /EFI/BOOT/BOOT*.EFI points to, or whether the
1621 * loader entries directory exists. Here we opted to check whether /EFI/systemd/ is non-empty, which
1622 * should be a suitable and very minimal check for a number of reasons:
1623 *
1624 * → The check is architecture independent (i.e. we check if any systemd-boot loader is installed, not a
1625 * specific one.)
1626 *
1627 * → It doesn't assume we are the only boot loader (i.e doesn't check if we own the main
1628 * /EFI/BOOT/BOOT*.EFI fallback binary.
1629 *
1630 * → It specifically checks for systemd-boot, not for other boot loaders (which a check for
1631 * /boot/loader/entries would do). */
1632
1633 p = path_join(arg_esp_path, "/EFI/systemd/");
1634 if (!p)
1635 return log_oom();
1636
1637 r = dir_is_empty(p);
1638 if (r > 0 || r == -ENOENT) {
1639 puts("no");
1640 return EXIT_FAILURE;
1641 }
1642 if (r < 0)
1643 return log_error_errno(r, "Failed to detect whether systemd-boot is installed: %m");
1644
1645 puts("yes");
1646 return EXIT_SUCCESS;
1647}
1648
d88c96ff
LP
1649static int verb_set_default(int argc, char *argv[], void *userdata) {
1650 const char *name;
1651 int r;
1652
baaa35ad
ZJS
1653 if (!is_efi_boot())
1654 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1655 "Not booted with UEFI.");
d88c96ff
LP
1656
1657 if (access("/sys/firmware/efi/efivars/LoaderInfo-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) < 0) {
1658 if (errno == ENOENT) {
1659 log_error_errno(errno, "Not booted with a supported boot loader.");
1660 return -EOPNOTSUPP;
1661 }
1662
1663 return log_error_errno(errno, "Failed to detect whether boot loader supports '%s' operation: %m", argv[0]);
1664 }
1665
baaa35ad
ZJS
1666 if (detect_container() > 0)
1667 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1668 "'%s' operation not supported in a container.",
1669 argv[0]);
d88c96ff 1670
baaa35ad
ZJS
1671 if (!arg_touch_variables)
1672 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1673 "'%s' operation cannot be combined with --touch-variables=no.",
1674 argv[0]);
d88c96ff
LP
1675
1676 name = streq(argv[0], "set-default") ? "LoaderEntryDefault" : "LoaderEntryOneShot";
1677
1678 if (isempty(argv[1])) {
1679 r = efi_set_variable(EFI_VENDOR_LOADER, name, NULL, 0);
1680 if (r < 0 && r != -ENOENT)
1681 return log_error_errno(r, "Failed to remove EFI variale: %m");
1682 } else {
1683 _cleanup_free_ char16_t *encoded = NULL;
1684
1685 encoded = utf8_to_utf16(argv[1], strlen(argv[1]));
1686 if (!encoded)
1687 return log_oom();
1688
1689 r = efi_set_variable(EFI_VENDOR_LOADER, name, encoded, char16_strlen(encoded) * 2 + 2);
1690 if (r < 0)
1691 return log_error_errno(r, "Failed to update EFI variable: %m");
1692 }
1693
1694 return 0;
1695}
1696
e44c3229
LP
1697static int verb_random_seed(int argc, char *argv[], void *userdata) {
1698 int r;
1699
1700 r = acquire_esp(false, NULL, NULL, NULL, NULL);
1701 if (r < 0)
1702 return r;
1703
1704 r = install_random_seed(arg_esp_path);
1705 if (r < 0)
1706 return r;
1707
1708 (void) sync_everything();
1709 return 0;
1710}
1711
4e5aa791
ZJS
1712static int verb_system_options(int argc, char *argv[], void *userdata) {
1713 int r;
1714
1715 if (argc == 1) {
1716 _cleanup_free_ char *line = NULL;
1717
1718 r = efi_systemd_options_variable(&line);
1719 if (r < 0)
1720 return log_error_errno(r, "Failed to query SystemdOptions EFI variable: %m");
1721
1722 printf("SystemdOptions: %s\n", line);
1723
1724 } else {
1725 r = efi_set_variable_string(EFI_VENDOR_SYSTEMD, "SystemdOptions", argv[1]);
1726 if (r < 0)
1727 return log_error_errno(r, "Failed to set SystemdOptions EFI variable: %m");
1728 }
1729
1730 return 0;
1731}
1732
2f2c539c 1733static int bootctl_main(int argc, char *argv[]) {
2f2c539c 1734 static const Verb verbs[] = {
4e5aa791
ZJS
1735 { "help", VERB_ANY, VERB_ANY, 0, help },
1736 { "status", VERB_ANY, 1, VERB_DEFAULT, verb_status },
1737 { "install", VERB_ANY, 1, 0, verb_install },
1738 { "update", VERB_ANY, 1, 0, verb_install },
1739 { "remove", VERB_ANY, 1, 0, verb_remove },
1740 { "is-installed", VERB_ANY, 1, 0, verb_is_installed },
1741 { "list", VERB_ANY, 1, 0, verb_list },
1742 { "set-default", 2, 2, 0, verb_set_default },
1743 { "set-oneshot", 2, 2, 0, verb_set_default },
1744 { "random-seed", VERB_ANY, 1, 0, verb_random_seed },
1745 { "system-options", VERB_ANY, 2, 0, verb_system_options },
2f2c539c
LP
1746 {}
1747 };
1748
1749 return dispatch_verb(argc, argv, verbs, NULL);
1750}
1751
608f8ec9 1752static int run(int argc, char *argv[]) {
601185b4 1753 int r;
7b4d7cc0
KS
1754
1755 log_parse_environment();
1756 log_open();
1757
341890de 1758 /* If we run in a container, automatically turn off EFI file system access */
2f2c539c
LP
1759 if (detect_container() > 0)
1760 arg_touch_variables = false;
1761
7b4d7cc0 1762 r = parse_argv(argc, argv);
601185b4 1763 if (r <= 0)
608f8ec9 1764 return r;
57db6f18 1765
608f8ec9 1766 return bootctl_main(argc, argv);
7b4d7cc0 1767}
608f8ec9
YW
1768
1769DEFINE_MAIN_FUNCTION(run);