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