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