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