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