Debian Codegraph @

src/bootctl/bootctl-install.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "sd-device.h"
7 #include "sd-varlink.h"
8
9 #include "alloc-util.h"
10 #include "ask-password-api.h"
11 #include "blockdev-util.h"
12 #include "boot-entry.h"
13 #include "bootctl.h"
14 #include "bootctl-install.h"
15 #include "bootctl-random-seed.h"
16 #include "bootctl-util.h"
17 #include "chase.h"
18 #include "copy.h"
19 #include "crypto-util.h"
20 #include "dirent-util.h"
21 #include "efi-api.h"
22 #include "efi.h"
23 #include "efivars.h"
24 #include "env-file.h"
25 #include "fd-util.h"
26 #include "fileio.h"
27 #include "find-esp.h"
28 #include "fs-util.h"
29 #include "glyph-util.h"
30 #include "id128-util.h"
31 #include "install-file.h"
32 #include "io-util.h"
33 #include "json-util.h"
34 #include "kernel-config.h"
35 #include "log.h"
36 #include "parse-argument.h"
37 #include "path-util.h"
38 #include "pe-binary.h"
39 #include "rm-rf.h"
40 #include "stat-util.h"
41 #include "string-table.h"
42 #include "string-util.h"
43 #include "strv.h"
44 #include "time-util.h"
45 #include "tmpfile-util.h"
46 #include "umask-util.h"
47 #include "utf8.h"
48
49 typedef enum InstallOperation {
50 INSTALL_NEW,
51 INSTALL_UPDATE,
52 INSTALL_REMOVE,
53 INSTALL_TEST,
54 _INSTALL_OPERATION_MAX,
55 _INSTALL_OPERATION_INVALID = -1,
56 } InstallOperation;
57
58 typedef struct InstallContext {
59 InstallOperation operation;
60 bool graceful;
61 char *root;
62 int root_fd;
63 sd_id128_t machine_id;
64 char *install_layout;
65 BootEntryTokenType entry_token_type;
66 char *entry_token;
67 int make_entry_directory; /* tri-state */
68 InstallSource install_source;
69 char *esp_path;
70 int esp_fd;
71 uint32_t esp_part;
72 uint64_t esp_pstart;
73 uint64_t esp_psize;
74 sd_id128_t esp_uuid;
75 char *xbootldr_path;
76 int xbootldr_fd;
77 #if HAVE_OPENSSL
78 X509 *secure_boot_certificate;
79 EVP_PKEY *secure_boot_private_key;
80 #endif
81 int touch_variables; /* tri-state */
82 } InstallContext;
83
84 #define INSTALL_CONTEXT_NULL \
85 (InstallContext) { \
86 .operation = _INSTALL_OPERATION_INVALID, \
87 .root_fd = -EBADF, \
88 .entry_token_type = _BOOT_ENTRY_TOKEN_TYPE_INVALID, \
89 .make_entry_directory = -1, \
90 .install_source = _INSTALL_SOURCE_INVALID, \
91 .esp_part = UINT32_MAX, \
92 .esp_pstart = UINT64_MAX, \
93 .esp_psize = UINT64_MAX, \
94 .esp_fd = -EBADF, \
95 .xbootldr_fd = -EBADF, \
96 .touch_variables = -1, \
97 }
98
99 static const char* install_operation_table[_INSTALL_OPERATION_MAX] = {
100 [INSTALL_NEW] = "new",
101 [INSTALL_UPDATE] = "update",
102 [INSTALL_REMOVE] = "remove",
103 [INSTALL_TEST] = "test",
104 };
105
106 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(install_operation, InstallOperation);
107
108 static void install_context_done(InstallContext *c) {
109 assert(c);
110
111 c->root = mfree(c->root);
112 c->root_fd = safe_close(c->root_fd);
113 c->install_layout = mfree(c->install_layout);
114 c->entry_token = mfree(c->entry_token);
115 c->esp_path = mfree(c->esp_path);
116 c->esp_fd = safe_close(c->esp_fd);
117 c->xbootldr_path = mfree(c->xbootldr_path);
118 c->xbootldr_fd = safe_close(c->xbootldr_fd);
119 #if HAVE_OPENSSL
120 if (c->secure_boot_private_key) {
121 sym_EVP_PKEY_free(c->secure_boot_private_key);
122 c->secure_boot_private_key = NULL;
123 }
124 if (c->secure_boot_certificate) {
125 sym_X509_free(c->secure_boot_certificate);
126 c->secure_boot_certificate = NULL;
127 }
128 #endif
129 }
130
131 static int install_context_from_cmdline(
132 InstallContext *ret,
133 InstallOperation operation) {
134
135 int r;
136
137 assert(ret);
138 assert(operation >= 0);
139 assert(operation < _INSTALL_OPERATION_MAX);
140
141 _cleanup_(install_context_done) InstallContext b = INSTALL_CONTEXT_NULL;
142 b.operation = operation;
143 b.graceful = arg_graceful() == ARG_GRACEFUL_FORCE ||
144 (operation == INSTALL_UPDATE && arg_graceful() != ARG_GRACEFUL_NO);
145 b.machine_id = arg_machine_id;
146 b.entry_token_type = arg_entry_token_type;
147 b.make_entry_directory = arg_make_entry_directory;
148 b.install_source = arg_install_source;
149
150 if (strdup_to(&b.entry_token, arg_entry_token) < 0 ||
151 strdup_to(&b.install_layout, arg_install_layout) < 0)
152 return log_oom();
153
154 if (arg_root) {
155 b.root_fd = open(arg_root, O_CLOEXEC|O_DIRECTORY|O_PATH);
156 if (b.root_fd < 0)
157 return log_error_errno(errno, "Failed to open root directory '%s': %m", arg_root);
158
159 r = strdup_to(&b.root, arg_root);
160 if (r < 0)
161 return log_oom();
162 } else
163 b.root_fd = XAT_FDROOT;
164
165 r = acquire_esp(/* unprivileged_mode= */ false,
166 b.graceful,
167 &b.esp_fd,
168 &b.esp_part,
169 &b.esp_pstart,
170 &b.esp_psize,
171 &b.esp_uuid,
172 /* ret_devid= */ NULL);
173 /* If --graceful is specified and we can't find an ESP, handle this cleanly */
174 if (r < 0 && (!b.graceful || r != -ENOKEY))
175 return r;
176
177 if (r >= 0) { /* An ESP has been found */
178 assert(arg_esp_path);
179
180 if (arg_root) {
181 const char *e = path_startswith(arg_esp_path, arg_root);
182 if (!e)
183 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "ESP path '%s' not below specified root '%s', refusing.", arg_esp_path, arg_root);
184
185 r = strdup_to(&b.esp_path, e);
186 } else
187 r = strdup_to(&b.esp_path, arg_esp_path);
188 if (r < 0)
189 return log_oom();
190 }
191
192 r = acquire_xbootldr(
193 /* unprivileged_mode= */ false,
194 &b.xbootldr_fd,
195 /* ret_uuid= */ NULL,
196 /* ret_devid= */ NULL);
197 if (r < 0)
198 return r;
199 if (r > 0) { /* XBOOTLDR has been found */
200 assert(arg_xbootldr_path);
201
202 if (arg_root) {
203 const char *e = path_startswith(arg_xbootldr_path, arg_root);
204 if (!e)
205 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "XBOOTLDR path '%s' not below specified root '%s', refusing.", arg_xbootldr_path, arg_root);
206
207 r = strdup_to(&b.xbootldr_path, e);
208 } else
209 r = strdup_to(&b.xbootldr_path, arg_xbootldr_path);
210 if (r < 0)
211 return log_oom();
212 }
213
214 b.touch_variables = arg_touch_variables;
215
216 *ret = TAKE_GENERIC(b, InstallContext, INSTALL_CONTEXT_NULL);
217
218 return !!ret->esp_path; /* return positive if we found an ESP */
219 }
220
221 static int acquire_dollar_boot_fd(InstallContext *c) {
222 assert(c);
223
224 if (c->xbootldr_fd >= 0)
225 return c->xbootldr_fd;
226
227 if (c->esp_fd >= 0)
228 return c->esp_fd;
229
230 return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Cannot access $BOOT, as neither ESP nor XBOOTLDR have been found.");
231 }
232
233 static const char* dollar_boot_path(InstallContext *c) {
234 assert(c);
235
236 return c->xbootldr_path ?: c->esp_path;
237 }
238
239 static bool should_touch_install_variables(InstallContext *c) {
240 assert(c);
241
242 if (c->touch_variables >= 0)
243 return c->touch_variables;
244
245 if (!is_efi_boot()) /* NB: this internally checks if we run in a container */
246 return false;
247
248 return empty_or_root(c->root);
249 }
250
251 static int load_etc_machine_id(InstallContext *c) {
252 int r;
253
254 assert(c);
255
256 r = id128_get_machine_at(c->root_fd, &c->machine_id);
257 if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r)) /* Not set or empty */
258 return 0;
259 if (r < 0)
260 return log_error_errno(r, "Failed to get machine-id: %m");
261
262 log_debug("Loaded machine ID %s from '%s/etc/machine-id'.", strempty(c->root), SD_ID128_TO_STRING(c->machine_id));
263 return 0;
264 }
265
266 static int load_etc_machine_info(InstallContext *c) {
267 /* systemd v250 added support to store the kernel-install layout setting and the machine ID to use
268 * for setting up the ESP in /etc/machine-info. The newer /etc/kernel/entry-token file, as well as
269 * the $layout field in /etc/kernel/install.conf are better replacements for this though, hence this
270 * has been deprecated and is only returned for compatibility. */
271 _cleanup_free_ char *s = NULL, *layout = NULL;
272 int r;
273
274 assert(c);
275
276 _cleanup_free_ char *j = path_join(c->root, "/etc/machine-info");
277 if (!j)
278 return log_oom();
279
280 _cleanup_close_ int fd =
281 chase_and_openat(
282 c->root_fd,
283 c->root_fd,
284 "/etc/machine-info",
285 CHASE_MUST_BE_REGULAR,
286 O_RDONLY|O_CLOEXEC,
287 /* ret_path= */ NULL);
288 if (fd == -ENOENT)
289 return 0;
290 if (fd < 0)
291 return log_error_errno(fd, "Failed to open '%s': %m", j);
292
293 r = parse_env_file_fd(
294 fd, "/etc/machine-info",
295 "KERNEL_INSTALL_LAYOUT", &layout,
296 "KERNEL_INSTALL_MACHINE_ID", &s);
297 if (r < 0)
298 return log_error_errno(r, "Failed to parse '%s': %m", j);
299
300 if (!isempty(s)) {
301 if (!arg_quiet)
302 log_notice("Read $KERNEL_INSTALL_MACHINE_ID from '%s'. "
303 "Please move it to '%s/etc/kernel/entry-token'.", j, strempty(c->root));
304
305 r = sd_id128_from_string(s, &c->machine_id);
306 if (r < 0)
307 return log_error_errno(r, "Failed to parse KERNEL_INSTALL_MACHINE_ID=\"%s\" in '%s': %m", s, j);
308
309 log_debug("Loaded KERNEL_INSTALL_MACHINE_ID=\"%s\" from '%s'.",
310 SD_ID128_TO_STRING(c->machine_id), j);
311 }
312
313 if (!isempty(layout)) {
314 if (!arg_quiet)
315 log_notice("Read $KERNEL_INSTALL_LAYOUT from '%s'. "
316 "Please move it to the layout= setting of '%s/etc/kernel/install.conf'.", j, strempty(c->root));
317
318 log_debug("KERNEL_INSTALL_LAYOUT=\"%s\" is specified in '%s'.", layout, j);
319 free_and_replace(c->install_layout, layout);
320 }
321
322 return 0;
323 }
324
325 static int load_kernel_install_layout(InstallContext *c) {
326 _cleanup_free_ char *layout = NULL;
327 int r;
328
329 assert(c);
330
331 const char *e = secure_getenv("KERNEL_INSTALL_CONF_ROOT");
332 r = load_kernel_install_conf_at(
333 e ? NULL : c->root,
334 e ? XAT_FDROOT : c->root_fd,
335 e,
336 /* ret_machine_id= */ NULL,
337 /* ret_boot_root= */ NULL,
338 &layout,
339 /* ret_initrd_generator= */ NULL,
340 /* ret_uki_generator= */ NULL);
341 if (r <= 0)
342 return r;
343
344 if (!isempty(layout)) {
345 log_debug("layout=\"%s\" is specified in config.", layout);
346 free_and_replace(c->install_layout, layout);
347 }
348
349 return 0;
350 }
351
352 static bool use_boot_loader_spec_type1(InstallContext *c) {
353 assert(c);
354 /* If the layout is not specified, or if it is set explicitly to "bls" we assume Boot Loader
355 * Specification Type #1 is the chosen format for our boot loader entries */
356 return !c->install_layout || streq(c->install_layout, "bls");
357 }
358
359 static int settle_make_entry_directory(InstallContext *c) {
360 int r;
361
362 assert(c);
363
364 r = load_etc_machine_id(c);
365 if (r < 0)
366 return r;
367
368 r = load_etc_machine_info(c);
369 if (r < 0)
370 return r;
371
372 r = load_kernel_install_layout(c);
373 if (r < 0)
374 return r;
375
376 const char *e = secure_getenv("KERNEL_INSTALL_CONF_ROOT");
377 r = boot_entry_token_ensure_at(
378 e ? XAT_FDROOT : c->root_fd,
379 e,
380 c->machine_id,
381 /* machine_id_is_random= */ false,
382 &c->entry_token_type,
383 &c->entry_token);
384 if (r < 0)
385 return r;
386
387 log_debug("Using entry token: %s", c->entry_token);
388
389 bool layout_type1 = use_boot_loader_spec_type1(c);
390 if (c->make_entry_directory < 0) { /* Automatic mode */
391 if (layout_type1) {
392 if (c->entry_token_type == BOOT_ENTRY_TOKEN_MACHINE_ID) {
393 _cleanup_free_ char *j = path_join(c->root, "/etc/machine-id");
394 if (!j)
395 return log_oom();
396
397 _cleanup_close_ int fd = -EBADF;
398 r = chaseat(c->root_fd,
399 c->root_fd,
400 "/etc/machine-id",
401 CHASE_MUST_BE_REGULAR,
402 /* ret_path= */ NULL,
403 &fd);
404 if (r < 0)
405 return log_debug_errno(r, "Unable to open '%s': %m", j);
406
407 r = fd_is_temporary_fs(fd);
408 if (r < 0)
409 return log_debug_errno(r, "Couldn't determine whether '%s' is on a temporary file system: %m", j);
410
411 c->make_entry_directory = r == 0;
412 } else
413 c->make_entry_directory = true;
414 } else
415 c->make_entry_directory = false;
416 }
417
418 if (c->make_entry_directory > 0 && !layout_type1)
419 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
420 "KERNEL_INSTALL_LAYOUT=\"%s\" is configured, but Boot Loader Specification Type #1 entry directory creation was requested.",
421 c->install_layout);
422
423 return 0;
424 }
425
426 static int compare_product(const char *a, const char *b) {
427 size_t x, y;
428
429 assert(a);
430 assert(b);
431
432 x = strcspn(a, " ");
433 y = strcspn(b, " ");
434 if (x != y)
435 return x < y ? -1 : x > y ? 1 : 0;
436
437 return strncmp(a, b, x);
438 }
439
440 static int compare_version(const char *a, const char *b) {
441 assert(a);
442 assert(b);
443
444 a += strcspn(a, " ");
445 a += strspn(a, " ");
446 b += strcspn(b, " ");
447 b += strspn(b, " ");
448
449 return strverscmp_improved(a, b);
450 }
451
452 static int version_check(int fd_from, const char *from, int fd_to, const char *to) {
453 _cleanup_free_ char *a = NULL, *b = NULL;
454 int r;
455
456 assert(fd_from >= 0);
457 assert(from);
458 assert(fd_to >= 0);
459 assert(to);
460
461 /* Does not reposition file offset */
462
463 r = get_file_version(fd_from, &a);
464 if (r == -ESRCH)
465 return log_notice_errno(r, "Source file \"%s\" does not carry version information!", from);
466 if (r < 0)
467 return log_error_errno(r, "Failed to get file version of '%s': %m", from);
468
469 r = get_file_version(fd_to, &b);
470 if (r == -ESRCH)
471 return log_info_errno(r, "Skipping \"%s\", it's owned by another boot loader (no version info found).", to);
472 if (r < 0)
473 return log_error_errno(r, "Failed to get file version of '%s': %m", to);
474 if (compare_product(a, b) != 0)
475 return log_info_errno(SYNTHETIC_ERRNO(ESRCH),
476 "Skipping \"%s\", it's owned by another boot loader.", to);
477
478 r = compare_version(a, b);
479 log_debug("Comparing versions: \"%s\" %s \"%s\"", a, comparison_operator(r), b);
480 if (r < 0)
481 return log_warning_errno(SYNTHETIC_ERRNO(ESTALE),
482 "Skipping \"%s\", newer boot loader version in place already.", to);
483 if (r == 0)
484 return log_info_errno(SYNTHETIC_ERRNO(ESTALE),
485 "Skipping \"%s\", same boot loader version in place already.", to);
486
487 return 0;
488 }
489
490 static int copy_file_with_version_check(
491 const char *source_path,
492 int source_fd,
493 const char *dest_path,
494 int dest_parent_fd,
495 const char *dest_filename,
496 int dest_fd,
497 bool force) {
498
499 int r;
500
501 assert(source_path);
502 assert(source_fd >= 0);
503 assert(dest_path);
504 assert(dest_parent_fd >= 0);
505 assert(dest_filename);
506
507 if (!force && dest_fd >= 0) {
508 r = version_check(source_fd, source_path, dest_fd, dest_path);
509 if (r < 0)
510 return r;
511 }
512
513 _cleanup_free_ char *t = NULL;
514 _cleanup_close_ int write_fd = -EBADF;
515 write_fd = open_tmpfile_linkable_at(dest_parent_fd, dest_filename, O_WRONLY|O_CLOEXEC, &t);
516 if (write_fd < 0)
517 return log_error_errno(write_fd, "Failed to open \"%s\" for writing: %m", dest_path);
518
519 CLEANUP_TMPFILE_AT(dest_parent_fd, t);
520
521 /* Reset file offset before we start copying, since we copy this file multiple times, and the offset
522 * might be left at the end of the file. (Resetting before rather than after a copy attempt is safer
523 * because a previous attempt might have failed half-way, leaving the file offset at some undefined
524 * place.) */
525 r = copy_bytes(source_fd, write_fd, UINT64_MAX, COPY_REFLINK|COPY_SEEK0_SOURCE);
526 if (r < 0)
527 return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", source_path, dest_path);
528
529 (void) copy_times(source_fd, write_fd, /* flags= */ 0);
530 (void) fchmod(write_fd, 0644);
531
532 r = link_tmpfile_at(write_fd, dest_parent_fd, t, dest_filename, LINK_TMPFILE_REPLACE|LINK_TMPFILE_SYNC);
533 if (r < 0)
534 return log_error_errno(r, "Failed to move data from \"%s\" to \"%s\": %m", source_path, dest_path);
535
536 t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
537
538 log_info("Copied \"%s\" to \"%s\".", source_path, dest_path);
539 return 0;
540 }
541
542 static int mkdir_one(const char *root, int root_fd, const char *path) {
543 int r;
544
545 assert(root);
546 assert(root_fd >= 0);
547 assert(path);
548
549 _cleanup_free_ char *p = path_join(empty_to_root(root), path);
550 if (!p)
551 return log_oom();
552
553 r = chaseat(root_fd,
554 root_fd,
555 path,
556 CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
557 /* ret_path= */ NULL,
558 /* ret_fd= */ NULL);
559 if (r < 0)
560 return log_error_errno(r, "Failed to create \"%s\": %m", p);
561
562 log_info("Created directory \"%s\".", p);
563 return 0;
564 }
565
566 static const char *const esp_subdirs[] = {
567 /* The directories to place in the ESP */
568 "EFI",
569 "EFI/systemd",
570 "EFI/BOOT",
571 "loader",
572 "loader/keys",
573 NULL
574 };
575
576 static const char *const dollar_boot_subdirs[] = {
577 /* The directories to place in the XBOOTLDR partition or the ESP, depending what exists */
578 "loader",
579 "loader/entries", /* Type #1 entries */
580 "EFI",
581 "EFI/Linux", /* Type #2 entries */
582 NULL
583 };
584
585 static int create_subdirs(const char *root, int root_fd, const char * const *subdirs) {
586 int r;
587
588 assert(root);
589 assert(root_fd >= 0);
590
591 STRV_FOREACH(i, subdirs) {
592 r = mkdir_one(root, root_fd, *i);
593 if (r < 0)
594 return r;
595 }
596
597 return 0;
598 }
599
600 static int update_efi_boot_binaries(
601 InstallContext *c,
602 const char *source_path,
603 int source_fd,
604 const char *ignore_filename) {
605
606 int r, ret = 0;
607
608 assert(c);
609 assert(source_path);
610
611 if (c->esp_fd < 0)
612 return c->esp_fd;
613
614 _cleanup_free_ char *j = path_join(c->root, c->esp_path);
615 if (!j)
616 return log_oom();
617
618 _cleanup_closedir_ DIR *d = NULL;
619 r = chase_and_opendirat(
620 c->esp_fd,
621 c->esp_fd,
622 "/EFI/BOOT",
623 CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
624 /* ret_path= */ NULL,
625 &d);
626 if (r == -ENOENT)
627 return 0;
628 if (r < 0)
629 return log_error_errno(r, "Failed to open directory \"%s/EFI/BOOT\": %m", j);
630
631 FOREACH_DIRENT(de, d, break) {
632 _cleanup_close_ int fd = -EBADF;
633
634 if (!endswith_no_case(de->d_name, ".efi"))
635 continue;
636
637 if (strcaseeq_ptr(ignore_filename, de->d_name))
638 continue;
639
640 fd = xopenat_full(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_NOFOLLOW, XO_REGULAR, /* mode= */ MODE_INVALID);
641 if (fd < 0)
642 return log_error_errno(fd, "Failed to open \"%s/%s\" for reading: %m", j, de->d_name);
643
644 r = pe_is_native_fd(fd);
645 if (r < 0) {
646 log_warning_errno(r, "Failed to detect if \"%s/%s\" is for native architecture, ignoring: %m", j, de->d_name);
647 continue;
648 }
649 if (r == 0)
650 continue;
651
652 _cleanup_free_ char *dest_path = path_join(j, "/EFI/BOOT", de->d_name);
653 if (!dest_path)
654 return log_oom();
655
656 r = copy_file_with_version_check(source_path, source_fd, dest_path, dirfd(d), de->d_name, fd, /* force= */ false);
657 if (IN_SET(r, -ESTALE, -ESRCH))
658 continue;
659 RET_GATHER(ret, r);
660 }
661
662 return ret;
663 }
664
665 static int copy_one_file(
666 InstallContext *c,
667 const char *name,
668 bool force) {
669
670 int r, ret = 0;
671
672 assert(c);
673
674 _cleanup_free_ char *dest_name = strdup(name);
675 if (!dest_name)
676 return log_oom();
677 char *s = endswith_no_case(dest_name, ".signed");
678 if (s)
679 *s = 0;
680
681 _cleanup_free_ char *sp = path_join(BOOTLIBDIR, name);
682 if (!sp)
683 return log_oom();
684
685 _cleanup_free_ char *source_path = NULL;
686 _cleanup_close_ int source_fd = -EBADF;
687 if (IN_SET(c->install_source, INSTALL_SOURCE_AUTO, INSTALL_SOURCE_IMAGE)) {
688 source_fd = chase_and_openat(
689 c->root_fd,
690 c->root_fd,
691 sp,
692 CHASE_MUST_BE_REGULAR,
693 O_RDONLY|O_CLOEXEC,
694 &source_path);
695 if (source_fd < 0 && (source_fd != -ENOENT || c->install_source != INSTALL_SOURCE_AUTO))
696 return log_error_errno(source_fd, "Failed to resolve path '%s' under directory '%s': %m", sp, c->root);
697
698 /* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
699 }
700 if (source_fd < 0) {
701 source_fd = chase_and_open(
702 sp,
703 /* root= */ NULL,
704 CHASE_MUST_BE_REGULAR,
705 O_RDONLY|O_CLOEXEC,
706 &source_path);
707 if (source_fd < 0)
708 return log_error_errno(source_fd, "Failed to resolve path '%s': %m", sp);
709 }
710
711 if (c->esp_fd < 0)
712 return c->esp_fd;
713
714 _cleanup_free_ char *j = path_join(c->root, c->esp_path);
715 if (!j)
716 return log_oom();
717
718 _cleanup_close_ int dest_parent_fd = -EBADF;
719 r = chaseat(c->esp_fd,
720 c->esp_fd,
721 "/EFI/systemd",
722 CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
723 /* ret_path= */ NULL,
724 &dest_parent_fd);
725 if (r < 0)
726 return log_error_errno(r, "Failed to resolve path '/EFI/systemd' under directory '%s': %m", j);
727
728 _cleanup_free_ char *dest_path = path_join(j, "/EFI/systemd", dest_name);
729 if (!dest_path)
730 return log_oom();
731
732 _cleanup_close_ int dest_fd = xopenat_full(dest_parent_fd, dest_name, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
733 if (dest_fd < 0 && dest_fd != -ENOENT)
734 return log_error_errno(dest_fd, "Failed to open '%s' under '%s/EFI/systemd' directory: %m", dest_name, j);
735
736 const char *e = startswith(dest_name, "systemd-boot");
737
738 /* If a primary sd-boot binary already exists and the source is a newer version, copy
739 * the existing primary to systemd-boot-fallback{arch}.efi before installing the new
740 * one, so firmware has a fallback to the previous binary. The fallback is left alone
741 * when its product and version match the currently booted bootloader (from LoaderInfo),
742 * so a known good binary stays as the fallback. In all other cases, like no fallback yet,
743 * LoaderInfo is unavailable, or product/version differs from what booted, it is
744 * overwritten with the current primary. */
745 if (e && dest_fd >= 0 && !force) {
746 r = version_check(source_fd, source_path, dest_fd, dest_path);
747 if (r < 0)
748 /* Stash the error and fall through; the BOOT{arch}.EFI updates below still run. */
749 ret = r;
750 else {
751 _cleanup_free_ char *fallback_name = strjoin("systemd-boot-fallback", e);
752 if (!fallback_name)
753 return log_oom();
754
755 _cleanup_free_ char *fallback_path = path_join(j, "/EFI/systemd", fallback_name);
756 if (!fallback_path)
757 return log_oom();
758
759 /* Leave the fallback alone if it already holds the currently booted product
760 * and version, so a known good binary stays as the fallback. If there is no
761 * fallback yet, LoaderInfo is unavailable, or there is a mismatch, then
762 * overwrite it with the current primary. */
763 bool should_rotate = true;
764 _cleanup_close_ int fallback_fd = xopenat_full(dest_parent_fd, fallback_name, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
765 if (fallback_fd >= 0) {
766 _cleanup_free_ char *loader_info = NULL, *fallback_version = NULL;
767
768 if (efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderInfo"), &loader_info) >= 0 &&
769 get_file_version(fallback_fd, &fallback_version) >= 0)
770 should_rotate = compare_product(loader_info, fallback_version) != 0 ||
771 compare_version(loader_info, fallback_version) != 0;
772 }
773
774 if (should_rotate) {
775 r = copy_file_with_version_check(dest_path, dest_fd, fallback_path, dest_parent_fd, fallback_name, /* dest_fd= */ -EBADF, /* force= */ true);
776 if (r < 0)
777 log_warning_errno(r, "Failed to back up sd-boot binary to fallback path, continuing: %m");
778 }
779
780 ret = copy_file_with_version_check(source_path, source_fd, dest_path, dest_parent_fd, dest_name, dest_fd, /* force= */ true);
781 }
782 } else
783 /* Note that if this fails we do the second copy anyway, but return this error code,
784 * so we stash it away in a separate variable. */
785 ret = copy_file_with_version_check(source_path, source_fd, dest_path, dest_parent_fd, dest_name, dest_fd, force);
786
787 if (e) {
788
789 /* Create the EFI default boot loader name (specified for removable devices) */
790 _cleanup_free_ char *boot_dot_efi = strjoin("BOOT", e);
791 if (!boot_dot_efi)
792 return log_oom();
793
794 ascii_strupper(boot_dot_efi);
795
796 _cleanup_close_ int default_dest_parent_fd = -EBADF;
797 r = chaseat(c->esp_fd,
798 c->esp_fd,
799 "/EFI/BOOT",
800 CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
801 /* ret_path= */ NULL,
802 &default_dest_parent_fd);
803 if (r < 0)
804 return log_error_errno(r, "Failed to resolve path '/EFI/BOOT/' under directory '%s': %m", j);
805
806 _cleanup_free_ char *default_dest_path = path_join(j, "/EFI/BOOT", boot_dot_efi);
807 if (!default_dest_path)
808 return log_oom();
809
810 _cleanup_close_ int default_dest_fd = xopenat_full(default_dest_parent_fd, boot_dot_efi, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
811 if (default_dest_fd < 0 && default_dest_fd != -ENOENT)
812 return log_error_errno(default_dest_fd, "Failed to open '%s' under '%s/EFI/BOOT' directory: %m", boot_dot_efi, j);
813
814 RET_GATHER(ret, copy_file_with_version_check(source_path, source_fd, default_dest_path, default_dest_parent_fd, boot_dot_efi, default_dest_fd, force));
815
816 /* If we were installed under any other name in /EFI/BOOT/, make sure we update those
817 * binaries as well. */
818 if (!force)
819 RET_GATHER(ret, update_efi_boot_binaries(c, source_path, source_fd, boot_dot_efi));
820 }
821
822 return ret;
823 }
824
825 static int install_binaries(
826 InstallContext *c,
827 const char *arch) {
828
829 int r;
830
831 assert(c);
832
833 _cleanup_free_ char *source_path = NULL;
834 _cleanup_closedir_ DIR *d = NULL;
835 if (IN_SET(c->install_source, INSTALL_SOURCE_AUTO, INSTALL_SOURCE_IMAGE)) {
836 r = chase_and_opendirat(
837 c->root_fd,
838 c->root_fd,
839 BOOTLIBDIR,
840 CHASE_MUST_BE_DIRECTORY,
841 &source_path,
842 &d);
843 if (r < 0 && (r != -ENOENT || c->install_source != INSTALL_SOURCE_AUTO))
844 return log_error_errno(r, "Failed to resolve path '%s' under directory '%s': %m", BOOTLIBDIR, c->root);
845
846 /* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
847 }
848 if (!d) {
849 r = chase_and_opendir(
850 BOOTLIBDIR,
851 /* root= */ NULL,
852 CHASE_MUST_BE_DIRECTORY,
853 &source_path,
854 &d);
855 if (r == -ENOENT && c->graceful) {
856 log_debug("Source directory '%s' does not exist, ignoring.", BOOTLIBDIR);
857 return 0;
858 }
859 if (r < 0)
860 return log_error_errno(r, "Failed to resolve path '%s': %m", BOOTLIBDIR);
861 }
862
863 const char *suffix = strjoina(arch, ".efi");
864 const char *suffix_signed = strjoina(arch, ".efi.signed");
865
866 FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \"%s\": %m", source_path)) {
867 int k;
868
869 if (endswith_no_case(de->d_name, suffix)) {
870 /* skip the .efi file, if there's a .signed version of it */
871 _cleanup_free_ const char *s = strjoin(de->d_name, ".signed");
872 if (!s)
873 return log_oom();
874 if (faccessat(dirfd(d), s, F_OK, 0) >= 0)
875 continue;
876 } else if (!endswith_no_case(de->d_name, suffix_signed))
877 continue;
878
879 k = copy_one_file(c, de->d_name, c->operation == INSTALL_NEW);
880 /* Don't propagate an error code if no update necessary, installed version already equal or
881 * newer version, or other boot loader in place. */
882 if (c->graceful && IN_SET(k, -ESTALE, -ESRCH))
883 continue;
884 RET_GATHER(r, k);
885 }
886
887 return r;
888 }
889
890 static int install_loader_config(InstallContext *c) {
891 int r;
892
893 assert(c);
894 assert(c->make_entry_directory >= 0);
895
896 if (c->esp_fd < 0)
897 return c->esp_fd;
898
899 _cleanup_free_ char *j = path_join(c->root, c->esp_path);
900 if (!j)
901 return log_oom();
902
903 _cleanup_close_ int loader_dir_fd = -EBADF;
904 r = chaseat(c->esp_fd,
905 c->esp_fd,
906 "loader",
907 CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
908 /* ret_path= */ NULL,
909 &loader_dir_fd);
910 if (r < 0)
911 return log_error_errno(r, "Failed to open '/loader/' directory below '%s': %m", j);
912
913 if (faccessat(loader_dir_fd, "loader.conf", F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
914 if (errno != ENOENT)
915 return log_error_errno(errno, "Failed to check if '/loader/loader.conf' exists below '%s': %m", j);
916 } else /* Silently skip creation if the file already exists (early check) */
917 return 0;
918
919 _cleanup_free_ char *t = NULL;
920 _cleanup_fclose_ FILE *f = NULL;
921 r = fopen_tmpfile_linkable_at(loader_dir_fd, "loader.conf", O_WRONLY|O_CLOEXEC, &t, &f);
922 if (r < 0)
923 return log_error_errno(r, "Failed to open '%s/loader/loader.conf' for writing: %m", j);
924
925 CLEANUP_TMPFILE_AT(loader_dir_fd, t);
926
927 fprintf(f, "#timeout 3\n"
928 "#console-mode keep\n");
929
930 if (c->make_entry_directory) {
931 assert(c->entry_token);
932 fprintf(f, "default %s-*\n", c->entry_token);
933 }
934
935 r = flink_tmpfile_at(f, loader_dir_fd, t, "loader.conf", LINK_TMPFILE_SYNC);
936 if (r == -EEXIST)
937 return 0; /* Silently skip creation if the file exists now (recheck) */
938 if (r < 0)
939 return log_error_errno(r, "Failed to move '%s/loader/loader.conf' into place: %m", j);
940
941 t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
942 return 1;
943 }
944
945 static int install_loader_specification(InstallContext *c) {
946 int r;
947
948 assert(c);
949
950 int dollar_boot_fd = acquire_dollar_boot_fd(c);
951 if (dollar_boot_fd < 0)
952 return dollar_boot_fd;
953
954 _cleanup_free_ char *j = path_join(c->root, dollar_boot_path(c));
955 if (!j)
956 return log_oom();
957
958 _cleanup_close_ int loader_dir_fd = -EBADF;
959 r = chaseat(dollar_boot_fd,
960 dollar_boot_fd,
961 "loader",
962 CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
963 /* ret_path= */ NULL,
964 &loader_dir_fd);
965 if (r < 0)
966 return log_error_errno(r, "Failed to pin '/loader' directory below '%s': %m", j);
967
968 if (faccessat(loader_dir_fd, "entries.srel", F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
969 if (errno != ENOENT)
970 return log_error_errno(errno, "Failed to check if '/loader/entries.srel' exists below '%s': %m", j);
971 } else /* Silently skip creation if the file already exists (early check) */
972 return 0;
973
974 _cleanup_free_ char *t = NULL;
975 _cleanup_fclose_ FILE *f = NULL;
976 r = fopen_tmpfile_linkable_at(loader_dir_fd, "entries.srel", O_WRONLY|O_CLOEXEC, &t, &f);
977 if (r < 0)
978 return log_error_errno(r, "Failed to open '%s/loader/entries.srel' for writing: %m", j);
979
980 CLEANUP_TMPFILE_AT(loader_dir_fd, t);
981
982 fprintf(f, "type1\n");
983
984 r = flink_tmpfile_at(f, loader_dir_fd, t, "entries.srel", LINK_TMPFILE_SYNC);
985 if (r == -EEXIST)
986 return 0; /* Silently skip creation if the file exists now (recheck) */
987 if (r < 0)
988 return log_error_errno(r, "Failed to move '%s/loader/entries.srel' into place: %m", j);
989
990 t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
991 return 1;
992 }
993
994 static int install_entry_directory(InstallContext *c) {
995 assert(c);
996 assert(c->make_entry_directory >= 0);
997
998 if (!c->make_entry_directory)
999 return 0;
1000
1001 assert(c->entry_token);
1002
1003 int dollar_boot_fd = acquire_dollar_boot_fd(c);
1004 if (dollar_boot_fd < 0)
1005 return dollar_boot_fd;
1006
1007 _cleanup_free_ char *j = path_join(c->root, dollar_boot_path(c));
1008 if (!j)
1009 return log_oom();
1010
1011 return mkdir_one(j, dollar_boot_fd, c->entry_token);
1012 }
1013
1014 static int install_entry_token(InstallContext *c) {
1015 int r;
1016
1017 assert(c);
1018 assert(c->make_entry_directory >= 0);
1019 assert(c->entry_token);
1020
1021 /* Let's save the used entry token in /etc/kernel/entry-token if we used it to create the entry
1022 * directory, or if anything else but the machine ID */
1023
1024 if (!c->make_entry_directory && c->entry_token_type == BOOT_ENTRY_TOKEN_MACHINE_ID)
1025 return 0;
1026
1027 const char *confdir = secure_getenv("KERNEL_INSTALL_CONF_ROOT") ?: "/etc/kernel/";
1028
1029 _cleanup_free_ char *j = path_join(c->root, confdir);
1030 if (!j)
1031 return log_oom();
1032
1033 _cleanup_close_ int dfd = -EBADF;
1034 r = chaseat(c->root_fd,
1035 c->root_fd,
1036 confdir,
1037 CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
1038 /* ret_path= */ NULL,
1039 &dfd);
1040 if (r < 0)
1041 return log_error_errno(r, "Failed to open '%s': %m", j);
1042
1043 r = write_string_file_at(dfd, "entry-token", c->entry_token, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755);
1044 if (r < 0)
1045 return log_error_errno(r, "Failed to write entry token '%s' to '%s/entry-token': %m", c->entry_token, j);
1046
1047 return 0;
1048 }
1049
1050 #if HAVE_OPENSSL
1051 static int efi_timestamp(EFI_TIME *ret) {
1052 struct tm tm = {};
1053 int r;
1054
1055 assert(ret);
1056
1057 r = localtime_or_gmtime_usec(source_date_epoch_or_now(), /* utc= */ true, &tm);
1058 if (r < 0)
1059 return log_error_errno(r, "Failed to convert timestamp to calendar time: %m");
1060
1061 *ret = (EFI_TIME) {
1062 .Year = 1900 + tm.tm_year,
1063 /* tm_mon starts at 0, EFI_TIME months start at 1. */
1064 .Month = tm.tm_mon + 1,
1065 .Day = tm.tm_mday,
1066 .Hour = tm.tm_hour,
1067 .Minute = tm.tm_min,
1068 .Second = tm.tm_sec,
1069 };
1070
1071 return 0;
1072 }
1073 #endif
1074
1075 static int install_secure_boot_auto_enroll(InstallContext *c) {
1076 #if HAVE_OPENSSL
1077 int r;
1078 #endif
1079
1080 if (!arg_secure_boot_auto_enroll)
1081 return 0;
1082
1083 #if HAVE_OPENSSL
1084 if (!c->secure_boot_certificate || !c->secure_boot_private_key)
1085 return 0;
1086
1087 r = DLOPEN_LIBCRYPTO(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
1088 if (r < 0)
1089 return r;
1090
1091 _cleanup_free_ uint8_t *dercert = NULL;
1092 int dercertsz;
1093 dercertsz = sym_i2d_X509(c->secure_boot_certificate, &dercert);
1094 if (dercertsz < 0)
1095 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert X.509 certificate to DER: %s",
1096 sym_ERR_error_string(sym_ERR_get_error(), NULL));
1097
1098 if (c->esp_fd < 0)
1099 return c->esp_fd;
1100
1101 _cleanup_free_ char *j = path_join(c->root, c->esp_path);
1102 if (!j)
1103 return log_oom();
1104
1105 _cleanup_close_ int keys_fd = -EBADF;
1106 r = chaseat(c->esp_fd,
1107 c->esp_fd,
1108 "loader/keys/auto",
1109 CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
1110 /* ret_path= */ NULL,
1111 &keys_fd);
1112 if (r < 0)
1113 return log_error_errno(r, "Failed to chase /loader/keys/auto/ below '%s': %m", j);
1114
1115 uint32_t siglistsz = offsetof(EFI_SIGNATURE_LIST, Signatures) + offsetof(EFI_SIGNATURE_DATA, SignatureData) + dercertsz;
1116 /* We use malloc0() to zero-initialize the SignatureOwner field of Signatures[0]. */
1117 _cleanup_free_ EFI_SIGNATURE_LIST *siglist = malloc0(siglistsz);
1118 if (!siglist)
1119 return log_oom();
1120
1121 *siglist = (EFI_SIGNATURE_LIST) {
1122 .SignatureType = EFI_CERT_X509_GUID,
1123 .SignatureListSize = siglistsz,
1124 .SignatureSize = offsetof(EFI_SIGNATURE_DATA, SignatureData) + dercertsz,
1125 };
1126
1127 memcpy(siglist->Signatures[0].SignatureData, dercert, dercertsz);
1128
1129 EFI_TIME timestamp;
1130 r = efi_timestamp(&timestamp);
1131 if (r < 0)
1132 return r;
1133
1134 uint32_t attrs =
1135 EFI_VARIABLE_NON_VOLATILE|
1136 EFI_VARIABLE_BOOTSERVICE_ACCESS|
1137 EFI_VARIABLE_RUNTIME_ACCESS|
1138 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1139
1140 FOREACH_STRING(db, "PK", "KEK", "db") {
1141 _cleanup_(BIO_freep) BIO *bio = NULL;
1142
1143 bio = sym_BIO_new(sym_BIO_s_mem());
1144 if (!bio)
1145 return log_oom();
1146
1147 _cleanup_free_ char16_t *db16 = utf8_to_utf16(db, SIZE_MAX);
1148 if (!db16)
1149 return log_oom();
1150
1151 /* Don't count the trailing NUL terminator. */
1152 if (sym_BIO_write(bio, db16, char16_strsize(db16) - sizeof(char16_t)) < 0)
1153 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable name to bio");
1154
1155 EFI_GUID *guid = STR_IN_SET(db, "PK", "KEK") ? &(EFI_GUID) EFI_GLOBAL_VARIABLE : &(EFI_GUID) EFI_IMAGE_SECURITY_DATABASE_GUID;
1156
1157 if (sym_BIO_write(bio, guid, sizeof(*guid)) < 0)
1158 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable GUID to bio");
1159
1160 if (sym_BIO_write(bio, &attrs, sizeof(attrs)) < 0)
1161 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable attributes to bio");
1162
1163 if (sym_BIO_write(bio, &timestamp, sizeof(timestamp)) < 0)
1164 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write timestamp to bio");
1165
1166 if (sym_BIO_write(bio, siglist, siglistsz) < 0)
1167 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write signature list to bio");
1168
1169 _cleanup_(PKCS7_freep) PKCS7 *p7 = NULL;
1170 p7 = sym_PKCS7_sign(c->secure_boot_certificate, c->secure_boot_private_key, /* certs= */ NULL, bio, PKCS7_DETACHED|PKCS7_NOATTR|PKCS7_BINARY|PKCS7_NOSMIMECAP);
1171 if (!p7)
1172 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to calculate PKCS7 signature: %s",
1173 sym_ERR_error_string(sym_ERR_get_error(), NULL));
1174
1175 _cleanup_free_ uint8_t *sig = NULL;
1176 int sigsz = sym_i2d_PKCS7(p7, &sig);
1177 if (sigsz < 0)
1178 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert PKCS7 signature to DER: %s",
1179 sym_ERR_error_string(sym_ERR_get_error(), NULL));
1180
1181 size_t authsz = offsetof(EFI_VARIABLE_AUTHENTICATION_2, AuthInfo.CertData) + sigsz;
1182 _cleanup_free_ EFI_VARIABLE_AUTHENTICATION_2 *auth = malloc(authsz);
1183 if (!auth)
1184 return log_oom();
1185
1186 *auth = (EFI_VARIABLE_AUTHENTICATION_2) {
1187 .TimeStamp = timestamp,
1188 .AuthInfo = {
1189 .Hdr = {
1190 .dwLength = offsetof(WIN_CERTIFICATE_UEFI_GUID, CertData) + sigsz,
1191 .wRevision = 0x0200,
1192 .wCertificateType = 0x0EF1, /* WIN_CERT_TYPE_EFI_GUID */
1193 },
1194 .CertType = EFI_CERT_TYPE_PKCS7_GUID,
1195 }
1196 };
1197
1198 memcpy(auth->AuthInfo.CertData, sig, sigsz);
1199
1200 _cleanup_free_ char *filename = strjoin(db, ".auth");
1201 if (!filename)
1202 return log_oom();
1203
1204 _cleanup_free_ char *t = NULL;
1205 _cleanup_close_ int fd = open_tmpfile_linkable_at(keys_fd, filename, O_WRONLY|O_CLOEXEC, &t);
1206 if (fd < 0)
1207 return log_error_errno(fd, "Failed to open secure boot auto-enrollment file for writing: %m");
1208
1209 CLEANUP_TMPFILE_AT(keys_fd, t);
1210
1211 r = loop_write(fd, auth, authsz);
1212 if (r < 0)
1213 return log_error_errno(r, "Failed to write authentication descriptor to secure boot auto-enrollment file: %m");
1214
1215 r = loop_write(fd, siglist, siglistsz);
1216 if (r < 0)
1217 return log_error_errno(r, "Failed to write signature list to secure boot auto-enrollment file: %m");
1218
1219 r = link_tmpfile_at(fd, keys_fd, t, filename, LINK_TMPFILE_SYNC);
1220 if (r < 0)
1221 return log_error_errno(errno, "Failed to link secure boot auto-enrollment file: %m");
1222
1223 t = mfree(t); /* Disarm CLEANUP_TMPFILE_AT() */
1224
1225 log_info("Secure boot auto-enrollment file '%s/loader/keys/auto/%s' successfully written.", j, filename);
1226 }
1227
1228 return 0;
1229 #else
1230 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Built without OpenSSL support, cannot set up auto-enrollment.");
1231 #endif
1232 }
1233
1234 static bool same_entry(uint16_t id, sd_id128_t uuid, const char *path) {
1235 _cleanup_free_ char *opath = NULL;
1236 sd_id128_t ouuid;
1237 int r;
1238
1239 r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
1240 if (r < 0)
1241 return false;
1242 if (!sd_id128_equal(uuid, ouuid))
1243 return false;
1244
1245 /* Some motherboards convert the path to uppercase under certain circumstances
1246 * (e.g. after booting into the Boot Menu in the ASUS ROG STRIX B350-F GAMING),
1247 * so use case-insensitive checking */
1248 if (!strcaseeq_ptr(path, opath))
1249 return false;
1250
1251 return true;
1252 }
1253
1254 static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
1255 _cleanup_free_ uint16_t *options = NULL;
1256
1257 assert(id);
1258
1259 int n = efi_get_boot_options(&options);
1260 if (n < 0)
1261 return n;
1262
1263 /* find already existing systemd-boot entry */
1264 for (int i = 0; i < n; i++)
1265 if (same_entry(options[i], uuid, path)) {
1266 *id = options[i];
1267 return 1;
1268 }
1269
1270 /* find free slot in the sorted BootXXXX variable list */
1271 for (int i = 0; i < n; i++)
1272 if (i != options[i]) {
1273 *id = i;
1274 return 0;
1275 }
1276
1277 /* use the next one */
1278 if (n == 0xffff)
1279 return -ENOSPC;
1280 *id = n;
1281 return 0;
1282 }
1283
1284 static int insert_into_order(InstallContext *c, uint16_t slot, uint16_t after_slot) {
1285 _cleanup_free_ uint16_t *order = NULL;
1286 uint16_t *t;
1287 int n;
1288
1289 assert(c);
1290
1291 n = efi_get_boot_order(&order);
1292 if (n <= 0)
1293 /* no entry, add us */
1294 return efi_set_boot_order(&slot, 1);
1295
1296 /* are we the first and only one? */
1297 if (n == 1 && order[0] == slot)
1298 return 0;
1299
1300 /* are we already in the boot order? */
1301 for (int i = 0; i < n; i++) {
1302 if (order[i] != slot)
1303 continue;
1304
1305 /* we do not require to be the first one, all is fine */
1306 /* if after_slot is set, leave existing position alone to preserve user reordering. */
1307 if (i == 0 || c->operation != INSTALL_NEW || after_slot != UINT16_MAX)
1308 return 0;
1309
1310 /* move us to the first slot */
1311 memmove(order + 1, order, i * sizeof(uint16_t));
1312 order[0] = slot;
1313 return efi_set_boot_order(order, n);
1314 }
1315
1316 /* slot is not yet in the order, so insert after a specific slot if requested */
1317 if (after_slot != UINT16_MAX) {
1318 t = reallocarray(order, n + 1, sizeof(uint16_t));
1319 if (!t)
1320 return -ENOMEM;
1321 order = t;
1322
1323 for (int i = 0; i < n; i++) {
1324 if (order[i] != after_slot)
1325 continue;
1326
1327 memmove(order + i + 2, order + i + 1, (n - i - 1) * sizeof(uint16_t));
1328 order[i + 1] = slot;
1329 return efi_set_boot_order(order, n + 1);
1330 }
1331
1332 log_warning("Boot entry %04" PRIx16 " not found in BootOrder, appending new entry at the end.", after_slot);
1333 order[n] = slot;
1334 return efi_set_boot_order(order, n + 1);
1335 }
1336
1337 /* extend array */
1338 t = reallocarray(order, n + 1, sizeof(uint16_t));
1339 if (!t)
1340 return -ENOMEM;
1341 order = t;
1342
1343 /* add us to the top or end of the list */
1344 if (c->operation != INSTALL_NEW) {
1345 memmove(order + 1, order, n * sizeof(uint16_t));
1346 order[0] = slot;
1347 } else
1348 order[n] = slot;
1349
1350 return efi_set_boot_order(order, n + 1);
1351 }
1352
1353 static int remove_from_order(uint16_t slot) {
1354 _cleanup_free_ uint16_t *order = NULL;
1355 int n;
1356
1357 n = efi_get_boot_order(&order);
1358 if (n <= 0)
1359 return n;
1360
1361 for (int i = 0; i < n; i++) {
1362 if (order[i] != slot)
1363 continue;
1364
1365 if (i + 1 < n)
1366 memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
1367 return efi_set_boot_order(order, n - 1);
1368 }
1369
1370 return 0;
1371 }
1372
1373 static int pick_efi_boot_option_description(int esp_fd, char **ret) {
1374 int r;
1375
1376 assert(esp_fd >= 0);
1377 assert(ret);
1378
1379 /* early declarations, so that they are definitely initialized even if we follow any of the gotos */
1380 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
1381 _cleanup_free_ char *j = NULL;
1382
1383 const char *b = arg_efi_boot_option_description ?: "Linux Boot Manager";
1384 if (!arg_efi_boot_option_description_with_device)
1385 goto fallback;
1386
1387 r = block_device_new_from_fd(
1388 esp_fd,
1389 BLOCK_DEVICE_LOOKUP_WHOLE_DISK|BLOCK_DEVICE_LOOKUP_BACKING,
1390 &d);
1391 if (r < 0) {
1392 log_debug_errno(r, "Failed to find backing device of ESP: %m");
1393 goto fallback;
1394 }
1395
1396 const char *serial;
1397 r = sd_device_get_property_value(d, "ID_SERIAL", &serial);
1398 if (r < 0) {
1399 log_debug_errno(r, "Unable to read ID_SERIAL field of backing device of ESP: %m");
1400 goto fallback;
1401 }
1402
1403 j = strjoin(b, " (", serial, ")");
1404 if (!j)
1405 return log_oom();
1406
1407 if (strlen(j) > EFI_BOOT_OPTION_DESCRIPTION_MAX) {
1408 log_debug("Boot option string suffixed with device serial would be too long, skipping: %s", j);
1409 j = mfree(j);
1410 goto fallback;
1411 }
1412
1413 *ret = TAKE_PTR(j);
1414 return 0;
1415
1416 fallback:
1417 j = strdup(b);
1418 if (!j)
1419 return log_oom();
1420
1421 *ret = TAKE_PTR(j);
1422 return 0;
1423 }
1424
1425 static int install_boot_option(
1426 InstallContext *c,
1427 const char *path,
1428 const char *description,
1429 bool require_existing,
1430 uint16_t after_slot,
1431 uint16_t *ret_slot) {
1432
1433 uint16_t slot;
1434 int r;
1435
1436 assert(c);
1437 assert(path);
1438 assert(description);
1439
1440 if (c->esp_fd < 0)
1441 return c->esp_fd;
1442
1443 _cleanup_free_ char *j = path_join(c->root, c->esp_path);
1444 if (!j)
1445 return log_oom();
1446
1447 r = chase_and_accessat(
1448 c->esp_fd,
1449 c->esp_fd,
1450 path,
1451 CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_REGULAR,
1452 F_OK,
1453 /* ret_path= */ NULL);
1454 if (r == -ENOENT && require_existing)
1455 return 0;
1456 if (r < 0 && r != -ENOENT)
1457 return log_error_errno(r, "Cannot access \"%s/%s\": %m", j, skip_leading_slash(path));
1458
1459 r = find_slot(c->esp_uuid, path, &slot);
1460 if (r < 0) {
1461 int level = c->graceful ? arg_quiet ? LOG_DEBUG : LOG_INFO : LOG_ERR;
1462 const char *skip = c->graceful ? ", skipping" : "";
1463
1464 log_full_errno(level, r,
1465 r == -ENOENT ?
1466 "Failed to access EFI variables%s. Is the \"efivarfs\" filesystem mounted?" :
1467 "Failed to determine current boot order%s: %m", skip);
1468
1469 return c->graceful ? 0 : r;
1470 }
1471
1472 bool existing = r > 0;
1473
1474 if (c->operation == INSTALL_NEW || !existing) {
1475 r = efi_add_boot_option(
1476 slot,
1477 description,
1478 c->esp_part,
1479 c->esp_pstart,
1480 c->esp_psize,
1481 c->esp_uuid,
1482 path);
1483 if (r < 0) {
1484 int level = c->graceful ? arg_quiet ? LOG_DEBUG : LOG_INFO : LOG_ERR;
1485 const char *skip = c->graceful ? ", skipping" : "";
1486
1487 log_full_errno(level, r, "Failed to create EFI Boot variable entry%s: %m", skip);
1488
1489 return c->graceful ? 0 : r;
1490 }
1491
1492 log_info("%s EFI boot entry \"%s\".",
1493 existing ? "Updated" : "Created",
1494 description);
1495 }
1496
1497 r = insert_into_order(c, slot, after_slot);
1498 if (r < 0)
1499 return r;
1500
1501 if (ret_slot)
1502 *ret_slot = slot;
1503
1504 return 0;
1505 }
1506
1507 static int are_we_installed(InstallContext *c) {
1508 int r;
1509
1510 assert(c);
1511
1512 /* Tests whether systemd-boot is installed. It's not obvious what to use as check here: we could
1513 * check EFI variables, we could check what binary /EFI/BOOT/BOOT*.EFI points to, or whether the
1514 * loader entries directory exists. Here we opted to check whether /EFI/systemd/ is non-empty, which
1515 * should be a suitable and very minimal check for a number of reasons:
1516 *
1517 * → The check is architecture independent (i.e. we check if any systemd-boot loader is installed,
1518 * not a specific one.)
1519 *
1520 * → It doesn't assume we are the only boot loader (i.e doesn't check if we own the main
1521 * /EFI/BOOT/BOOT*.EFI fallback binary.
1522 *
1523 * → It specifically checks for systemd-boot, not for other boot loaders (which a check for
1524 * /boot/loader/entries would do). */
1525
1526 _cleanup_free_ char *p = path_join(c->esp_path, "/EFI/systemd");
1527 if (!p)
1528 return log_oom();
1529
1530 if (c->esp_fd < 0)
1531 return c->esp_fd;
1532
1533 _cleanup_close_ int fd = chase_and_openat(
1534 c->esp_fd,
1535 c->esp_fd,
1536 "/EFI/systemd",
1537 CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
1538 O_RDONLY|O_CLOEXEC|O_DIRECTORY,
1539 /* ret_path= */ NULL);
1540 if (fd == -ENOENT)
1541 return 0;
1542 if (fd < 0)
1543 return log_error_errno(fd, "Failed to open '%s': %m", p);
1544
1545 log_debug("Checking whether '%s' contains any files%s", p, glyph(GLYPH_ELLIPSIS));
1546 r = dir_is_empty_at(fd, /* path= */ NULL, /* ignore_hidden_or_backup= */ false);
1547 if (r < 0 && r != -ENOENT)
1548 return log_error_errno(r, "Failed to check whether '%s' contains any files: %m", p);
1549
1550 return r == 0;
1551 }
1552
1553 #if HAVE_OPENSSL
1554 static int load_secure_boot_auto_enroll(
1555 X509 **ret_certificate,
1556 EVP_PKEY **ret_private_key,
1557 OpenSSLAskPasswordUI **ret_ui) {
1558
1559 int r;
1560
1561 assert(ret_certificate);
1562 assert(ret_private_key);
1563 assert(ret_ui);
1564
1565 if (!arg_secure_boot_auto_enroll) {
1566 *ret_certificate = NULL;
1567 *ret_private_key = NULL;
1568 return 0;
1569 }
1570
1571 if (arg_certificate_source_type == OPENSSL_CERTIFICATE_SOURCE_FILE) {
1572 r = parse_path_argument(arg_certificate, /* suppress_root= */ false, &arg_certificate);
1573 if (r < 0)
1574 return r;
1575 }
1576
1577 _cleanup_(X509_freep) X509 *certificate = NULL;
1578 r = openssl_load_x509_certificate(
1579 arg_certificate_source_type,
1580 arg_certificate_source,
1581 arg_certificate,
1582 &certificate);
1583 if (r < 0)
1584 return log_error_errno(r, "Failed to load X.509 certificate from %s: %m", arg_certificate);
1585
1586 if (arg_private_key_source_type == OPENSSL_KEY_SOURCE_FILE) {
1587 r = parse_path_argument(arg_private_key, /* suppress_root= */ false, &arg_private_key);
1588 if (r < 0)
1589 return r;
1590 }
1591
1592 r = openssl_load_private_key(
1593 arg_private_key_source_type,
1594 arg_private_key_source,
1595 arg_private_key,
1596 &(AskPasswordRequest) {
1597 .tty_fd = -EBADF,
1598 .id = "bootctl-private-key-pin",
1599 .keyring = arg_private_key,
1600 .credential = "bootctl.private-key-pin",
1601 .until = USEC_INFINITY,
1602 .hup_fd = -EBADF,
1603 },
1604 ret_private_key,
1605 ret_ui);
1606 if (r < 0)
1607 return log_error_errno(r, "Failed to load private key from %s: %m", arg_private_key);
1608
1609 *ret_certificate = TAKE_PTR(certificate);
1610
1611 return 0;
1612 }
1613 #endif
1614
1615 static int install_variables(InstallContext *c, const char *arch) {
1616 int r;
1617
1618 assert(c);
1619
1620 const char *path = strjoina("/EFI/systemd/systemd-boot", arch, ".efi");
1621
1622 _cleanup_free_ char *description = NULL;
1623 r = pick_efi_boot_option_description(c->esp_fd, &description);
1624 if (r < 0)
1625 return r;
1626
1627 uint16_t primary_slot = UINT16_MAX;
1628 r = install_boot_option(c, path, description, /* require_existing= */ true, /* after_slot= */ UINT16_MAX, &primary_slot);
1629 if (r < 0)
1630 return r;
1631 /* If primary registration was skipped (e.g. binary not on ESP), skip the fallback too
1632 * or else it would land at position 0 in BootOrder with no primary ahead of it. */
1633 if (primary_slot == UINT16_MAX)
1634 return 0;
1635
1636 const char *fallback_path = strjoina("/EFI/systemd/systemd-boot-fallback", arch, ".efi");
1637
1638 _cleanup_free_ char *fallback_description = strjoin("Fallback ", description);
1639 if (!fallback_description)
1640 return log_oom();
1641
1642 strshorten(fallback_description, EFI_BOOT_OPTION_DESCRIPTION_MAX);
1643
1644 return install_boot_option(c, fallback_path, fallback_description, /* require_existing= */ false, /* after_slot= */ primary_slot, /* ret_slot= */ NULL);
1645 }
1646
1647 static int run_install(InstallContext *c) {
1648 int r;
1649
1650 assert(c);
1651 assert(c->operation >= 0);
1652
1653 if (c->operation == INSTALL_UPDATE) {
1654 /* If we are updating, don't do anything if sd-boot wasn't actually installed. */
1655 r = are_we_installed(c);
1656 if (r < 0)
1657 return r;
1658 if (r == 0) {
1659 log_debug("Skipping update because sd-boot is not installed in the ESP.");
1660 return 0;
1661 }
1662 }
1663
1664 r = settle_make_entry_directory(c);
1665 if (r < 0)
1666 return r;
1667
1668 const char *arch = arg_arch_all ? "" : get_efi_arch();
1669
1670 if (c->esp_fd < 0)
1671 return c->esp_fd;
1672
1673 _cleanup_free_ char *j = path_join(c->root, c->esp_path);
1674 if (!j)
1675 return log_oom();
1676
1677 int dollar_boot_fd = acquire_dollar_boot_fd(c);
1678 if (dollar_boot_fd < 0)
1679 return dollar_boot_fd;
1680
1681 _cleanup_free_ char *w = path_join(c->root, dollar_boot_path(c));
1682 if (!w)
1683 return log_oom();
1684
1685 WITH_UMASK(0002) {
1686 if (c->operation == INSTALL_NEW) {
1687 /* Don't create any of these directories when we are just updating. When we update
1688 * we'll drop-in our files (unless there are newer ones already), but we won't create
1689 * the directories for them in the first place. */
1690
1691 r = create_subdirs(j, c->esp_fd, esp_subdirs);
1692 if (r < 0)
1693 return r;
1694
1695 r = create_subdirs(w, dollar_boot_fd, dollar_boot_subdirs);
1696 if (r < 0)
1697 return r;
1698 }
1699
1700 r = install_binaries(c, arch);
1701 if (r < 0)
1702 return r;
1703
1704 if (c->operation == INSTALL_NEW) {
1705 r = install_loader_config(c);
1706 if (r < 0)
1707 return r;
1708
1709 r = install_entry_directory(c);
1710 if (r < 0)
1711 return r;
1712
1713 r = install_entry_token(c);
1714 if (r < 0)
1715 return r;
1716
1717 if (arg_install_random_seed && !c->root) {
1718 r = install_random_seed(c->esp_path, c->esp_fd);
1719 if (r < 0)
1720 return r;
1721 }
1722
1723 r = install_secure_boot_auto_enroll(c);
1724 if (r < 0)
1725 return r;
1726 }
1727
1728 r = install_loader_specification(c);
1729 if (r < 0)
1730 return r;
1731 }
1732
1733 (void) sync_everything();
1734
1735 if (!should_touch_install_variables(c))
1736 return 0;
1737
1738 if (arg_arch_all) {
1739 log_info("Not changing EFI variables with --all-architectures.");
1740 return 0;
1741 }
1742
1743 return install_variables(c, arch);
1744 }
1745
1746 int verb_install(int argc, char *argv[], uintptr_t _data, void *userdata) {
1747 int r;
1748
1749 /* Invoked for both "update" and "install" */
1750
1751 _cleanup_(install_context_done) InstallContext c = INSTALL_CONTEXT_NULL;
1752 r = install_context_from_cmdline(&c, streq(argv[0], "install") ? INSTALL_NEW : INSTALL_UPDATE);
1753 if (r < 0)
1754 return r;
1755 if (r == 0) {
1756 log_debug("No ESP found and operating in graceful mode, skipping.");
1757 return 0;
1758 }
1759
1760 #if HAVE_OPENSSL
1761 _cleanup_(openssl_ask_password_ui_freep) OpenSSLAskPasswordUI *ui = NULL;
1762 r = load_secure_boot_auto_enroll(&c.secure_boot_certificate, &c.secure_boot_private_key, &ui);
1763 if (r < 0)
1764 return r;
1765 #endif
1766
1767 return run_install(&c);
1768 }
1769
1770 static int remove_boot_efi(InstallContext *c) {
1771 int r, n = 0;
1772
1773 assert(c);
1774
1775 if (c->esp_fd < 0)
1776 return c->esp_fd;
1777
1778 _cleanup_free_ char *w = path_join(c->root, c->esp_path);
1779 if (!w)
1780 return log_oom();
1781
1782 _cleanup_closedir_ DIR *d = NULL;
1783 _cleanup_free_ char *p = NULL;
1784 r = chase_and_opendirat(
1785 c->esp_fd,
1786 c->esp_fd,
1787 "/EFI/BOOT",
1788 CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
1789 &p,
1790 &d);
1791 if (r == -ENOENT)
1792 return 0;
1793 if (r < 0)
1794 return log_error_errno(r, "Failed to open directory \"%s/EFI/BOOT\": %m", w);
1795
1796 _cleanup_free_ char *j = path_join(w, p);
1797 if (!j)
1798 return log_oom();
1799
1800 FOREACH_DIRENT(de, d, break) {
1801 _cleanup_close_ int fd = -EBADF;
1802
1803 if (!endswith_no_case(de->d_name, ".efi"))
1804 continue;
1805
1806 _cleanup_free_ char *z = path_join(j, de->d_name);
1807 if (!z)
1808 return log_oom();
1809
1810 fd = xopenat_full(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_NOFOLLOW, XO_REGULAR, /* mode= */ MODE_INVALID);
1811 if (fd == -ENOENT)
1812 continue;
1813 if (fd < 0)
1814 return log_error_errno(fd, "Failed to open '%s' for reading: %m", z);
1815
1816 r = pe_is_native_fd(fd);
1817 if (r < 0) {
1818 log_warning_errno(r, "Failed to detect if '%s' is native architecture, ignoring: %m", z);
1819 continue;
1820 }
1821 if (r == 0)
1822 continue;
1823
1824 _cleanup_free_ char *v = NULL;
1825 r = get_file_version(fd, &v);
1826 if (r == -ESRCH)
1827 continue; /* No version information */
1828 if (r < 0) {
1829 log_warning_errno(r, "Failed to get file version of '%s', skipping: %m", de->d_name);
1830 continue;
1831 }
1832 if (!startswith(v, "systemd-boot "))
1833 continue;
1834
1835 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1836 return log_error_errno(errno, "Failed to remove '%s': %m", z);
1837
1838 log_info("Removed '%s'.", z);
1839
1840 n++;
1841 }
1842
1843 log_debug("Removed %i EFI binaries from '%s'.", n, j);
1844 return n;
1845 }
1846
1847 static int unlink_inode(const char *root, int root_fd, const char *path, mode_t type) {
1848 int r;
1849
1850 assert(root);
1851 assert(root_fd >= 0);
1852 assert(path);
1853 assert(IN_SET(type, S_IFREG, S_IFDIR));
1854
1855 _cleanup_free_ char *p = path_join(empty_to_root(root), path);
1856 if (!p)
1857 return log_oom();
1858
1859 r = chase_and_unlinkat(
1860 root_fd,
1861 root_fd,
1862 path,
1863 CHASE_PROHIBIT_SYMLINKS,
1864 S_ISDIR(type) ? AT_REMOVEDIR : 0,
1865 /* ret_path= */ NULL);
1866 if (r < 0) {
1867 bool ignore = IN_SET(r, -ENOENT, -ENOTEMPTY);
1868 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r, "Failed to remove '%s': %m", p);
1869 return ignore ? 0 : r;
1870 }
1871
1872 log_info("Removed %s\"%s\".", S_ISDIR(type) ? "directory " : "", p);
1873 return 0;
1874 }
1875
1876 static int remove_subdirs(const char *root, int root_fd, const char *const *subdirs) {
1877 int r = 0;
1878
1879 assert(root);
1880 assert(root_fd);
1881
1882 STRV_FOREACH_BACKWARDS(i, (char**) subdirs)
1883 RET_GATHER(r, unlink_inode(root, root_fd, *i, S_IFDIR));
1884
1885 return r;
1886 }
1887
1888 static int remove_entry_directory(InstallContext *c, const char *path, int fd) {
1889 assert(c);
1890 assert(c->make_entry_directory >= 0);
1891 assert(path);
1892 assert(fd >= 0);
1893
1894 if (!c->make_entry_directory || !c->entry_token)
1895 return 0;
1896
1897 return unlink_inode(path, fd, c->entry_token, S_IFDIR);
1898 }
1899
1900 static int remove_binaries(InstallContext *c) {
1901 int r;
1902
1903 _cleanup_free_ char *p = path_join(c->root, "/EFI/systemd");
1904 if (!p)
1905 return log_oom();
1906
1907 _cleanup_close_ int efi_fd = -EBADF;
1908 r = chaseat(c->esp_fd,
1909 c->esp_fd,
1910 "EFI",
1911 CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
1912 /* ret_path= */ NULL,
1913 &efi_fd);
1914 if (r < 0) {
1915 if (r != -ENOENT)
1916 return log_error_errno(r, "Failed to remove '%s': %m", p);
1917
1918 r = 0;
1919 } else
1920 r = rm_rf_at(efi_fd, "systemd", REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_MISSING_OK);
1921
1922 return RET_GATHER(r, remove_boot_efi(c));
1923 }
1924
1925 static int remove_boot_option(sd_id128_t uuid, const char *path, bool in_order) {
1926 uint16_t slot;
1927 int r;
1928
1929 r = find_slot(uuid, path, &slot);
1930 if (r != 1)
1931 return 0;
1932
1933 r = efi_remove_boot_option(slot);
1934 if (r < 0)
1935 return r;
1936
1937 if (in_order)
1938 return remove_from_order(slot);
1939
1940 return 0;
1941 }
1942
1943 static int remove_loader_variables(void) {
1944 int r = 0;
1945
1946 /* Remove all persistent loader variables we define */
1947
1948 FOREACH_STRING(var,
1949 EFI_LOADER_VARIABLE_STR("LoaderConfigConsoleMode"),
1950 EFI_LOADER_VARIABLE_STR("LoaderConfigTimeout"),
1951 EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"),
1952 EFI_LOADER_VARIABLE_STR("LoaderEntryPreferred"),
1953 EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"),
1954 EFI_LOADER_VARIABLE_STR("LoaderEntrySysFail"),
1955 EFI_LOADER_VARIABLE_STR("LoaderEntryLastBooted"),
1956 EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"),
1957 EFI_LOADER_VARIABLE_STR("LoaderSystemToken")) {
1958
1959 int q;
1960
1961 q = efi_set_variable(var, NULL, 0);
1962 if (q == -ENOENT)
1963 continue;
1964 if (q < 0)
1965 RET_GATHER(r, log_warning_errno(q, "Failed to remove EFI variable %s: %m", var));
1966 else
1967 log_info("Removed EFI variable %s.", var);
1968 }
1969
1970 return r;
1971 }
1972
1973 static int remove_variables(sd_id128_t uuid) {
1974 int r = 0;
1975
1976 const char *path = strjoina("/EFI/systemd/systemd-boot", get_efi_arch(), ".efi");
1977 RET_GATHER(r, remove_boot_option(uuid, path, /* in_order= */ true));
1978
1979 const char *fallback_path = strjoina("/EFI/systemd/systemd-boot-fallback", get_efi_arch(), ".efi");
1980 RET_GATHER(r, remove_boot_option(uuid, fallback_path, /* in_order= */ true));
1981
1982 return RET_GATHER(r, remove_loader_variables());
1983 }
1984
1985 int verb_remove(int argc, char *argv[], uintptr_t _data, void *userdata) {
1986 int r;
1987
1988 _cleanup_(install_context_done) InstallContext c = INSTALL_CONTEXT_NULL;
1989 r = install_context_from_cmdline(&c, INSTALL_REMOVE);
1990 if (r < 0)
1991 return r;
1992 if (r == 0) {
1993 log_debug("No ESP found and operating in graceful mode, skipping.");
1994 return 0;
1995 }
1996
1997 r = settle_make_entry_directory(&c);
1998 if (r < 0)
1999 return r;
2000
2001 if (c.esp_fd < 0)
2002 return c.esp_fd;
2003
2004 _cleanup_free_ char *j = path_join(c.root, c.esp_path);
2005 if (!j)
2006 return log_oom();
2007
2008 int dollar_boot_fd = acquire_dollar_boot_fd(&c);
2009 if (dollar_boot_fd < 0)
2010 return dollar_boot_fd;
2011
2012 _cleanup_free_ char *w = path_join(c.root, dollar_boot_path(&c));
2013 if (!w)
2014 return log_oom();
2015
2016 r = remove_binaries(&c);
2017 RET_GATHER(r, unlink_inode(j, c.esp_fd, "/loader/loader.conf", S_IFREG));
2018 RET_GATHER(r, unlink_inode(j, c.esp_fd, "/loader/random-seed", S_IFREG));
2019 RET_GATHER(r, unlink_inode(j, c.esp_fd, "/loader/entries.srel", S_IFREG));
2020
2021 FOREACH_STRING(db, "PK.auth", "KEK.auth", "db.auth") {
2022 _cleanup_free_ char *p = path_join("/loader/keys/auto", db);
2023 if (!p)
2024 return log_oom();
2025
2026 RET_GATHER(r, unlink_inode(j, c.esp_fd, p, S_IFREG));
2027 }
2028 RET_GATHER(r, unlink_inode(j, c.esp_fd, "/loader/keys/auto", S_IFDIR));
2029 RET_GATHER(r, unlink_inode(j, c.esp_fd, "/loader/entries.srel", S_IFREG));
2030
2031 RET_GATHER(r, remove_subdirs(j, c.esp_fd, esp_subdirs));
2032 RET_GATHER(r, remove_subdirs(j, c.esp_fd, dollar_boot_subdirs));
2033 RET_GATHER(r, remove_entry_directory(&c, j, c.esp_fd));
2034
2035 if (c.xbootldr_fd >= 0) {
2036 /* Remove a subset of these also from the XBOOTLDR partition if it exists */
2037 RET_GATHER(r, unlink_inode(w, c.xbootldr_fd, "/loader/entries.srel", S_IFREG));
2038 RET_GATHER(r, remove_subdirs(w, c.xbootldr_fd, dollar_boot_subdirs));
2039 RET_GATHER(r, remove_entry_directory(&c, w, c.xbootldr_fd));
2040 }
2041
2042 (void) sync_everything();
2043
2044 if (!should_touch_install_variables(&c))
2045 return r;
2046
2047 if (arg_arch_all) {
2048 log_info("Not changing EFI variables with --all-architectures.");
2049 return r;
2050 }
2051
2052 return remove_variables(c.esp_uuid);
2053 }
2054
2055 int verb_is_installed(int argc, char *argv[], uintptr_t _data, void *userdata) {
2056 int r;
2057
2058 _cleanup_(install_context_done) InstallContext c = INSTALL_CONTEXT_NULL;
2059 r = install_context_from_cmdline(&c, INSTALL_TEST);
2060 if (r < 0)
2061 return r;
2062 if (r == 0) {
2063 log_debug("No ESP found and operating in graceful mode, claiming not installed.");
2064 if (!arg_quiet)
2065 puts("no");
2066 return EXIT_FAILURE;
2067 }
2068
2069 r = are_we_installed(&c);
2070 if (r < 0)
2071 return r;
2072
2073 if (r > 0) {
2074 if (!arg_quiet)
2075 puts("yes");
2076 return EXIT_SUCCESS;
2077 } else {
2078 if (!arg_quiet)
2079 puts("no");
2080 return EXIT_FAILURE;
2081 }
2082 }
2083
2084 static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_install_operation, InstallOperation, install_operation_from_string);
2085 static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_boot_entry_token_type, BootEntryTokenType, boot_entry_token_type_from_string);
2086
2087 typedef struct InstallParameters {
2088 InstallContext context;
2089 unsigned root_fd_index;
2090 } InstallParameters;
2091
2092 static void install_parameters_done(InstallParameters *p) {
2093 assert(p);
2094
2095 install_context_done(&p->context);
2096 }
2097
2098 int vl_method_install(
2099 sd_varlink *link,
2100 sd_json_variant *parameters,
2101 sd_varlink_method_flags_t flags,
2102 void *userdata) {
2103
2104 int r;
2105
2106 assert(link);
2107
2108 _cleanup_(install_parameters_done) InstallParameters p = {
2109 .context = INSTALL_CONTEXT_NULL,
2110 .root_fd_index = UINT_MAX,
2111 };
2112
2113 static const sd_json_dispatch_field dispatch_table[] = {
2114 { "operation", SD_JSON_VARIANT_STRING, json_dispatch_install_operation, voffsetof(p, context.operation), SD_JSON_MANDATORY },
2115 { "graceful", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, voffsetof(p, context.graceful), 0 },
2116 { "rootFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, voffsetof(p, root_fd_index), 0 },
2117 { "rootDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, voffsetof(p, context.root), 0 },
2118 { "bootEntryTokenType", SD_JSON_VARIANT_STRING, json_dispatch_boot_entry_token_type, voffsetof(p, context.entry_token_type), 0 },
2119 { "touchVariables", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, voffsetof(p, context.touch_variables), 0 },
2120 {},
2121 };
2122
2123 r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
2124 if (r != 0)
2125 return r;
2126
2127 if (!IN_SET(p.context.operation, INSTALL_NEW, INSTALL_UPDATE))
2128 return sd_varlink_error_invalid_parameter_name(link, "operation");
2129
2130 if (p.root_fd_index != UINT_MAX) {
2131 p.context.root_fd = sd_varlink_peek_dup_fd(link, p.root_fd_index);
2132 if (p.context.root_fd < 0)
2133 return log_debug_errno(p.context.root_fd, "Failed to acquire root fd from Varlink: %m");
2134
2135 r = fd_verify_safe_flags_full(p.context.root_fd, O_DIRECTORY);
2136 if (r < 0)
2137 return sd_varlink_error_invalid_parameter_name(link, "rootFileDescriptor");
2138
2139 r = fd_verify_directory(p.context.root_fd);
2140 if (r < 0)
2141 return log_debug_errno(r, "Specified file descriptor does not refer to a directory: %m");
2142
2143 if (!p.context.root) {
2144 r = fd_get_path(p.context.root_fd, &p.context.root);
2145 if (r < 0)
2146 return log_debug_errno(r, "Failed to get path of file descriptor: %m");
2147
2148 if (empty_or_root(p.context.root))
2149 p.context.root = mfree(p.context.root);
2150 }
2151 } else if (p.context.root) {
2152 p.context.root_fd = open(p.context.root, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
2153 if (p.context.root_fd < 0)
2154 return log_debug_errno(errno, "Failed to open '%s': %m", p.context.root);
2155 } else
2156 p.context.root_fd = XAT_FDROOT;
2157
2158 if (p.context.entry_token_type < 0)
2159 p.context.entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
2160
2161 r = find_esp_and_warn_at_full(
2162 p.context.root_fd,
2163 /* path= */ NULL,
2164 /* unprivileged_mode= */ false,
2165 &p.context.esp_path,
2166 &p.context.esp_fd,
2167 &p.context.esp_part,
2168 &p.context.esp_pstart,
2169 &p.context.esp_psize,
2170 &p.context.esp_uuid,
2171 /* ret_devid= */ NULL);
2172 if (r == -ENOKEY)
2173 return sd_varlink_error(link, "io.systemd.BootControl.NoESPFound", NULL);
2174 if (r < 0)
2175 return r;
2176
2177 r = find_xbootldr_and_warn_at(
2178 p.context.root_fd,
2179 /* path= */ NULL,
2180 /* unprivileged_mode= */ false,
2181 &p.context.xbootldr_path,
2182 &p.context.xbootldr_fd);
2183 if (r == -ENOKEY)
2184 log_debug_errno(r, "Didn't find an XBOOTLDR partition, using ESP as $BOOT.");
2185 else if (r < 0)
2186 return r;
2187
2188 r = run_install(&p.context);
2189 if (r == -EUNATCH) /* no boot entry token is set */
2190 return sd_varlink_error(link, "io.systemd.BootControl.BootEntryTokenUnavailable", NULL);
2191 if (r < 0)
2192 return r;
2193
2194 return sd_varlink_reply(link, NULL);
2195 }