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