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