]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/bootctl-install.c
c14bdb69f8387af1cce92a06a36fa2a402980e73
[thirdparty/systemd.git] / src / boot / bootctl-install.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "bootctl.h"
4 #include "bootctl-install.h"
5 #include "bootctl-random-seed.h"
6 #include "bootctl-util.h"
7 #include "chase-symlinks.h"
8 #include "copy.h"
9 #include "env-file.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "fs-util.h"
13 #include "glyph-util.h"
14 #include "os-util.h"
15 #include "path-util.h"
16 #include "stat-util.h"
17 #include "sync-util.h"
18 #include "tmpfile-util.h"
19 #include "umask-util.h"
20 #include "utf8.h"
21 #include "dirent-util.h"
22 #include "efi-api.h"
23 #include "rm-rf.h"
24
25 static int load_etc_machine_id(void) {
26 int r;
27
28 r = sd_id128_get_machine(&arg_machine_id);
29 if (IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG)) /* Not set or empty */
30 return 0;
31 if (r < 0)
32 return log_error_errno(r, "Failed to get machine-id: %m");
33
34 log_debug("Loaded machine ID %s from /etc/machine-id.", SD_ID128_TO_STRING(arg_machine_id));
35 return 0;
36 }
37
38 static int load_etc_machine_info(void) {
39 /* systemd v250 added support to store the kernel-install layout setting and the machine ID to use
40 * for setting up the ESP in /etc/machine-info. The newer /etc/kernel/entry-token file, as well as
41 * the $layout field in /etc/kernel/install.conf are better replacements for this though, hence this
42 * has been deprecated and is only returned for compatibility. */
43 _cleanup_free_ char *s = NULL, *layout = NULL;
44 int r;
45
46 r = parse_env_file(NULL, "/etc/machine-info",
47 "KERNEL_INSTALL_LAYOUT", &layout,
48 "KERNEL_INSTALL_MACHINE_ID", &s);
49 if (r == -ENOENT)
50 return 0;
51 if (r < 0)
52 return log_error_errno(r, "Failed to parse /etc/machine-info: %m");
53
54 if (!isempty(s)) {
55 if (!arg_quiet)
56 log_notice("Read $KERNEL_INSTALL_MACHINE_ID from /etc/machine-info. "
57 "Please move it to /etc/kernel/entry-token.");
58
59 r = sd_id128_from_string(s, &arg_machine_id);
60 if (r < 0)
61 return log_error_errno(r, "Failed to parse KERNEL_INSTALL_MACHINE_ID=%s in /etc/machine-info: %m", s);
62
63 log_debug("Loaded KERNEL_INSTALL_MACHINE_ID=%s from KERNEL_INSTALL_MACHINE_ID in /etc/machine-info.",
64 SD_ID128_TO_STRING(arg_machine_id));
65 }
66
67 if (!isempty(layout)) {
68 if (!arg_quiet)
69 log_notice("Read $KERNEL_INSTALL_LAYOUT from /etc/machine-info. "
70 "Please move it to the layout= setting of /etc/kernel/install.conf.");
71
72 log_debug("KERNEL_INSTALL_LAYOUT=%s is specified in /etc/machine-info.", layout);
73 free_and_replace(arg_install_layout, layout);
74 }
75
76 return 0;
77 }
78
79 static int load_etc_kernel_install_conf(void) {
80 _cleanup_free_ char *layout = NULL;
81 int r;
82
83 r = parse_env_file(NULL, "/etc/kernel/install.conf",
84 "layout", &layout);
85 if (r == -ENOENT)
86 return 0;
87 if (r < 0)
88 return log_error_errno(r, "Failed to parse /etc/kernel/install.conf: %m");
89
90 if (!isempty(layout)) {
91 log_debug("layout=%s is specified in /etc/machine-info.", layout);
92 free_and_replace(arg_install_layout, layout);
93 }
94
95 return 0;
96 }
97
98 static int settle_entry_token(void) {
99 int r;
100
101 switch (arg_entry_token_type) {
102
103 case ARG_ENTRY_TOKEN_AUTO: {
104 _cleanup_free_ char *buf = NULL;
105 r = read_one_line_file("/etc/kernel/entry-token", &buf);
106 if (r < 0 && r != -ENOENT)
107 return log_error_errno(r, "Failed to read /etc/kernel/entry-token: %m");
108
109 if (!isempty(buf)) {
110 free_and_replace(arg_entry_token, buf);
111 arg_entry_token_type = ARG_ENTRY_TOKEN_LITERAL;
112 } else if (sd_id128_is_null(arg_machine_id)) {
113 _cleanup_free_ char *id = NULL, *image_id = NULL;
114
115 r = parse_os_release(NULL,
116 "IMAGE_ID", &image_id,
117 "ID", &id);
118 if (r < 0)
119 return log_error_errno(r, "Failed to load /etc/os-release: %m");
120
121 if (!isempty(image_id)) {
122 free_and_replace(arg_entry_token, image_id);
123 arg_entry_token_type = ARG_ENTRY_TOKEN_OS_IMAGE_ID;
124 } else if (!isempty(id)) {
125 free_and_replace(arg_entry_token, id);
126 arg_entry_token_type = ARG_ENTRY_TOKEN_OS_ID;
127 } else
128 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No machine ID set, and /etc/os-release carries no ID=/IMAGE_ID= fields.");
129 } else {
130 r = free_and_strdup_warn(&arg_entry_token, SD_ID128_TO_STRING(arg_machine_id));
131 if (r < 0)
132 return r;
133
134 arg_entry_token_type = ARG_ENTRY_TOKEN_MACHINE_ID;
135 }
136
137 break;
138 }
139
140 case ARG_ENTRY_TOKEN_MACHINE_ID:
141 if (sd_id128_is_null(arg_machine_id))
142 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No machine ID set.");
143
144 r = free_and_strdup_warn(&arg_entry_token, SD_ID128_TO_STRING(arg_machine_id));
145 if (r < 0)
146 return r;
147
148 break;
149
150 case ARG_ENTRY_TOKEN_OS_IMAGE_ID: {
151 _cleanup_free_ char *buf = NULL;
152
153 r = parse_os_release(NULL, "IMAGE_ID", &buf);
154 if (r < 0)
155 return log_error_errno(r, "Failed to load /etc/os-release: %m");
156
157 if (isempty(buf))
158 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "IMAGE_ID= field not set in /etc/os-release.");
159
160 free_and_replace(arg_entry_token, buf);
161 break;
162 }
163
164 case ARG_ENTRY_TOKEN_OS_ID: {
165 _cleanup_free_ char *buf = NULL;
166
167 r = parse_os_release(NULL, "ID", &buf);
168 if (r < 0)
169 return log_error_errno(r, "Failed to load /etc/os-release: %m");
170
171 if (isempty(buf))
172 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "ID= field not set in /etc/os-release.");
173
174 free_and_replace(arg_entry_token, buf);
175 break;
176 }
177
178 case ARG_ENTRY_TOKEN_LITERAL:
179 assert(!isempty(arg_entry_token)); /* already filled in by command line parser */
180 break;
181 }
182
183 if (isempty(arg_entry_token) || !(utf8_is_valid(arg_entry_token) && string_is_safe(arg_entry_token)))
184 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected entry token not valid: %s", arg_entry_token);
185
186 log_debug("Using entry token: %s", arg_entry_token);
187 return 0;
188 }
189
190 static bool use_boot_loader_spec_type1(void) {
191 /* If the layout is not specified, or if it is set explicitly to "bls" we assume Boot Loader
192 * Specification Type #1 is the chosen format for our boot loader entries */
193 return !arg_install_layout || streq(arg_install_layout, "bls");
194 }
195
196 static int settle_make_entry_directory(void) {
197 int r;
198
199 r = load_etc_machine_id();
200 if (r < 0)
201 return r;
202
203 r = load_etc_machine_info();
204 if (r < 0)
205 return r;
206
207 r = load_etc_kernel_install_conf();
208 if (r < 0)
209 return r;
210
211 r = settle_entry_token();
212 if (r < 0)
213 return r;
214
215 bool layout_type1 = use_boot_loader_spec_type1();
216 if (arg_make_entry_directory < 0) { /* Automatic mode */
217 if (layout_type1) {
218 if (arg_entry_token == ARG_ENTRY_TOKEN_MACHINE_ID) {
219 r = path_is_temporary_fs("/etc/machine-id");
220 if (r < 0)
221 return log_debug_errno(r, "Couldn't determine whether /etc/machine-id is on a temporary file system: %m");
222
223 arg_make_entry_directory = r == 0;
224 } else
225 arg_make_entry_directory = true;
226 } else
227 arg_make_entry_directory = false;
228 }
229
230 if (arg_make_entry_directory > 0 && !layout_type1)
231 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
232 "KERNEL_INSTALL_LAYOUT=%s is configured, but Boot Loader Specification Type #1 entry directory creation was requested.",
233 arg_install_layout);
234
235 return 0;
236 }
237
238 static int compare_product(const char *a, const char *b) {
239 size_t x, y;
240
241 assert(a);
242 assert(b);
243
244 x = strcspn(a, " ");
245 y = strcspn(b, " ");
246 if (x != y)
247 return x < y ? -1 : x > y ? 1 : 0;
248
249 return strncmp(a, b, x);
250 }
251
252 static int compare_version(const char *a, const char *b) {
253 assert(a);
254 assert(b);
255
256 a += strcspn(a, " ");
257 a += strspn(a, " ");
258 b += strcspn(b, " ");
259 b += strspn(b, " ");
260
261 return strverscmp_improved(a, b);
262 }
263
264 static int version_check(int fd_from, const char *from, int fd_to, const char *to) {
265 _cleanup_free_ char *a = NULL, *b = NULL;
266 int r;
267
268 assert(fd_from >= 0);
269 assert(from);
270 assert(fd_to >= 0);
271 assert(to);
272
273 r = get_file_version(fd_from, &a);
274 if (r < 0)
275 return r;
276 if (r == 0)
277 return log_notice_errno(SYNTHETIC_ERRNO(EREMOTE),
278 "Source file \"%s\" does not carry version information!",
279 from);
280
281 r = get_file_version(fd_to, &b);
282 if (r < 0)
283 return r;
284 if (r == 0 || compare_product(a, b) != 0)
285 return log_notice_errno(SYNTHETIC_ERRNO(EREMOTE),
286 "Skipping \"%s\", since it's owned by another boot loader.",
287 to);
288
289 r = compare_version(a, b);
290 log_debug("Comparing versions: \"%s\" %s \"%s", a, comparison_operator(r), b);
291 if (r < 0)
292 return log_warning_errno(SYNTHETIC_ERRNO(ESTALE),
293 "Skipping \"%s\", since newer boot loader version in place already.", to);
294 if (r == 0)
295 return log_info_errno(SYNTHETIC_ERRNO(ESTALE),
296 "Skipping \"%s\", since same boot loader version in place already.", to);
297
298 return 0;
299 }
300
301 static int copy_file_with_version_check(const char *from, const char *to, bool force) {
302 _cleanup_close_ int fd_from = -1, fd_to = -1;
303 _cleanup_free_ char *t = NULL;
304 int r;
305
306 fd_from = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
307 if (fd_from < 0)
308 return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", from);
309
310 if (!force) {
311 fd_to = open(to, O_RDONLY|O_CLOEXEC|O_NOCTTY);
312 if (fd_to < 0) {
313 if (errno != ENOENT)
314 return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", to);
315 } else {
316 r = version_check(fd_from, from, fd_to, to);
317 if (r < 0)
318 return r;
319
320 if (lseek(fd_from, 0, SEEK_SET) == (off_t) -1)
321 return log_error_errno(errno, "Failed to seek in \"%s\": %m", from);
322
323 fd_to = safe_close(fd_to);
324 }
325 }
326
327 r = tempfn_random(to, NULL, &t);
328 if (r < 0)
329 return log_oom();
330
331 WITH_UMASK(0000) {
332 fd_to = open(t, O_WRONLY|O_CREAT|O_CLOEXEC|O_EXCL|O_NOFOLLOW, 0644);
333 if (fd_to < 0)
334 return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", t);
335 }
336
337 r = copy_bytes(fd_from, fd_to, UINT64_MAX, COPY_REFLINK);
338 if (r < 0) {
339 (void) unlink(t);
340 return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
341 }
342
343 (void) copy_times(fd_from, fd_to, 0);
344
345 r = fsync_full(fd_to);
346 if (r < 0) {
347 (void) unlink_noerrno(t);
348 return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
349 }
350
351 if (renameat(AT_FDCWD, t, AT_FDCWD, to) < 0) {
352 (void) unlink_noerrno(t);
353 return log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", t, to);
354 }
355
356 log_info("Copied \"%s\" to \"%s\".", from, to);
357
358 return 0;
359 }
360
361 static int mkdir_one(const char *prefix, const char *suffix) {
362 _cleanup_free_ char *p = NULL;
363
364 p = path_join(prefix, suffix);
365 if (mkdir(p, 0700) < 0) {
366 if (errno != EEXIST)
367 return log_error_errno(errno, "Failed to create \"%s\": %m", p);
368 } else
369 log_info("Created \"%s\".", p);
370
371 return 0;
372 }
373
374 static const char *const esp_subdirs[] = {
375 /* The directories to place in the ESP */
376 "EFI",
377 "EFI/systemd",
378 "EFI/BOOT",
379 "loader",
380 NULL
381 };
382
383 static const char *const dollar_boot_subdirs[] = {
384 /* The directories to place in the XBOOTLDR partition or the ESP, depending what exists */
385 "loader",
386 "loader/entries", /* Type #1 entries */
387 "EFI",
388 "EFI/Linux", /* Type #2 entries */
389 NULL
390 };
391
392 static int create_subdirs(const char *root, const char * const *subdirs) {
393 int r;
394
395 STRV_FOREACH(i, subdirs) {
396 r = mkdir_one(root, *i);
397 if (r < 0)
398 return r;
399 }
400
401 return 0;
402 }
403
404
405 static int copy_one_file(const char *esp_path, const char *name, bool force) {
406 char *root = IN_SET(arg_install_source, ARG_INSTALL_SOURCE_AUTO, ARG_INSTALL_SOURCE_IMAGE) ? arg_root : NULL;
407 _cleanup_free_ char *source_path = NULL, *dest_path = NULL, *p = NULL, *q = NULL;
408 const char *e;
409 char *dest_name, *s;
410 int r, ret;
411
412 dest_name = strdupa_safe(name);
413 s = endswith_no_case(dest_name, ".signed");
414 if (s)
415 *s = 0;
416
417 p = path_join(BOOTLIBDIR, name);
418 if (!p)
419 return log_oom();
420
421 r = chase_symlinks(p, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, &source_path, NULL);
422 /* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
423 if (r == -ENOENT && root && arg_install_source == ARG_INSTALL_SOURCE_AUTO)
424 r = chase_symlinks(p, NULL, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, &source_path, NULL);
425 if (r < 0)
426 return log_error_errno(r,
427 "Failed to resolve path %s%s%s: %m",
428 p,
429 root ? " under directory " : "",
430 strempty(root));
431
432 q = path_join("/EFI/systemd/", dest_name);
433 if (!q)
434 return log_oom();
435
436 r = chase_symlinks(q, esp_path, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_NONEXISTENT, &dest_path, NULL);
437 if (r < 0)
438 return log_error_errno(r, "Failed to resolve path %s under directory %s: %m", q, esp_path);
439
440 /* Note that if this fails we do the second copy anyway, but return this error code,
441 * so we stash it away in a separate variable. */
442 ret = copy_file_with_version_check(source_path, dest_path, force);
443
444 e = startswith(dest_name, "systemd-boot");
445 if (e) {
446 _cleanup_free_ char *default_dest_path = NULL;
447 char *v;
448
449 /* Create the EFI default boot loader name (specified for removable devices) */
450 v = strjoina("/EFI/BOOT/BOOT", e);
451 ascii_strupper(strrchr(v, '/') + 1);
452
453 r = chase_symlinks(v, esp_path, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_NONEXISTENT, &default_dest_path, NULL);
454 if (r < 0)
455 return log_error_errno(r, "Failed to resolve path %s under directory %s: %m", v, esp_path);
456
457 r = copy_file_with_version_check(source_path, default_dest_path, force);
458 if (r < 0 && ret == 0)
459 ret = r;
460 }
461
462 return ret;
463 }
464
465 static int install_binaries(const char *esp_path, const char *arch, bool force) {
466 char *root = IN_SET(arg_install_source, ARG_INSTALL_SOURCE_AUTO, ARG_INSTALL_SOURCE_IMAGE) ? arg_root : NULL;
467 _cleanup_closedir_ DIR *d = NULL;
468 _cleanup_free_ char *path = NULL;
469 int r;
470
471 r = chase_symlinks_and_opendir(BOOTLIBDIR, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, &path, &d);
472 /* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
473 if (r == -ENOENT && root && arg_install_source == ARG_INSTALL_SOURCE_AUTO)
474 r = chase_symlinks_and_opendir(BOOTLIBDIR, NULL, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, &path, &d);
475 if (r < 0)
476 return log_error_errno(r, "Failed to open boot loader directory %s%s: %m", strempty(root), BOOTLIBDIR);
477
478 const char *suffix = strjoina(arch, ".efi");
479 const char *suffix_signed = strjoina(arch, ".efi.signed");
480
481 FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \"%s\": %m", path)) {
482 int k;
483
484 if (!endswith_no_case(de->d_name, suffix) && !endswith_no_case(de->d_name, suffix_signed))
485 continue;
486
487 /* skip the .efi file, if there's a .signed version of it */
488 if (endswith_no_case(de->d_name, ".efi")) {
489 _cleanup_free_ const char *s = strjoin(de->d_name, ".signed");
490 if (!s)
491 return log_oom();
492 if (faccessat(dirfd(d), s, F_OK, 0) >= 0)
493 continue;
494 }
495
496 k = copy_one_file(esp_path, de->d_name, force);
497 /* Don't propagate an error code if no update necessary, installed version already equal or
498 * newer version, or other boot loader in place. */
499 if (arg_graceful && IN_SET(k, -ESTALE, -EREMOTE))
500 continue;
501 if (k < 0 && r == 0)
502 r = k;
503 }
504
505 return r;
506 }
507
508 static int install_loader_config(const char *esp_path) {
509 _cleanup_(unlink_and_freep) char *t = NULL;
510 _cleanup_fclose_ FILE *f = NULL;
511 const char *p;
512 int r;
513
514 assert(arg_make_entry_directory >= 0);
515
516 p = prefix_roota(esp_path, "/loader/loader.conf");
517 if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
518 return 0;
519
520 r = fopen_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t, &f);
521 if (r < 0)
522 return log_error_errno(r, "Failed to open \"%s\" for writing: %m", p);
523
524 fprintf(f, "#timeout 3\n"
525 "#console-mode keep\n");
526
527 if (arg_make_entry_directory) {
528 assert(arg_entry_token);
529 fprintf(f, "default %s-*\n", arg_entry_token);
530 }
531
532 r = flink_tmpfile(f, t, p);
533 if (r == -EEXIST)
534 return 0; /* Silently skip creation if the file exists now (recheck) */
535 if (r < 0)
536 return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
537
538 t = mfree(t);
539 return 1;
540 }
541
542 static int install_loader_specification(const char *root) {
543 _cleanup_(unlink_and_freep) char *t = NULL;
544 _cleanup_fclose_ FILE *f = NULL;
545 _cleanup_free_ char *p = NULL;
546 int r;
547
548 p = path_join(root, "/loader/entries.srel");
549 if (!p)
550 return log_oom();
551
552 if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
553 return 0;
554
555 r = fopen_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t, &f);
556 if (r < 0)
557 return log_error_errno(r, "Failed to open \"%s\" for writing: %m", p);
558
559 fprintf(f, "type1\n");
560
561 r = flink_tmpfile(f, t, p);
562 if (r == -EEXIST)
563 return 0; /* Silently skip creation if the file exists now (recheck) */
564 if (r < 0)
565 return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
566
567 t = mfree(t);
568 return 1;
569 }
570
571 static int install_entry_directory(const char *root) {
572 assert(root);
573 assert(arg_make_entry_directory >= 0);
574
575 if (!arg_make_entry_directory)
576 return 0;
577
578 assert(arg_entry_token);
579 return mkdir_one(root, arg_entry_token);
580 }
581
582 static int install_entry_token(void) {
583 int r;
584
585 assert(arg_make_entry_directory >= 0);
586 assert(arg_entry_token);
587
588 /* Let's save the used entry token in /etc/kernel/entry-token if we used it to create the entry
589 * directory, or if anything else but the machine ID */
590
591 if (!arg_make_entry_directory && arg_entry_token_type == ARG_ENTRY_TOKEN_MACHINE_ID)
592 return 0;
593
594 r = write_string_file("/etc/kernel/entry-token", arg_entry_token, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755);
595 if (r < 0)
596 return log_error_errno(r, "Failed to write entry token '%s' to /etc/kernel/entry-token", arg_entry_token);
597
598 return 0;
599 }
600
601 static bool same_entry(uint16_t id, sd_id128_t uuid, const char *path) {
602 _cleanup_free_ char *opath = NULL;
603 sd_id128_t ouuid;
604 int r;
605
606 r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
607 if (r < 0)
608 return false;
609 if (!sd_id128_equal(uuid, ouuid))
610 return false;
611
612 /* Some motherboards convert the path to uppercase under certain circumstances
613 * (e.g. after booting into the Boot Menu in the ASUS ROG STRIX B350-F GAMING),
614 * so use case-insensitive checking */
615 if (!strcaseeq_ptr(path, opath))
616 return false;
617
618 return true;
619 }
620
621 static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
622 _cleanup_free_ uint16_t *options = NULL;
623
624 int n = efi_get_boot_options(&options);
625 if (n < 0)
626 return n;
627
628 /* find already existing systemd-boot entry */
629 for (int i = 0; i < n; i++)
630 if (same_entry(options[i], uuid, path)) {
631 *id = options[i];
632 return 1;
633 }
634
635 /* find free slot in the sorted BootXXXX variable list */
636 for (int i = 0; i < n; i++)
637 if (i != options[i]) {
638 *id = i;
639 return 0;
640 }
641
642 /* use the next one */
643 if (n == 0xffff)
644 return -ENOSPC;
645 *id = n;
646 return 0;
647 }
648
649 static int insert_into_order(uint16_t slot, bool first) {
650 _cleanup_free_ uint16_t *order = NULL;
651 uint16_t *t;
652 int n;
653
654 n = efi_get_boot_order(&order);
655 if (n <= 0)
656 /* no entry, add us */
657 return efi_set_boot_order(&slot, 1);
658
659 /* are we the first and only one? */
660 if (n == 1 && order[0] == slot)
661 return 0;
662
663 /* are we already in the boot order? */
664 for (int i = 0; i < n; i++) {
665 if (order[i] != slot)
666 continue;
667
668 /* we do not require to be the first one, all is fine */
669 if (!first)
670 return 0;
671
672 /* move us to the first slot */
673 memmove(order + 1, order, i * sizeof(uint16_t));
674 order[0] = slot;
675 return efi_set_boot_order(order, n);
676 }
677
678 /* extend array */
679 t = reallocarray(order, n + 1, sizeof(uint16_t));
680 if (!t)
681 return -ENOMEM;
682 order = t;
683
684 /* add us to the top or end of the list */
685 if (first) {
686 memmove(order + 1, order, n * sizeof(uint16_t));
687 order[0] = slot;
688 } else
689 order[n] = slot;
690
691 return efi_set_boot_order(order, n + 1);
692 }
693
694 static int remove_from_order(uint16_t slot) {
695 _cleanup_free_ uint16_t *order = NULL;
696 int n;
697
698 n = efi_get_boot_order(&order);
699 if (n <= 0)
700 return n;
701
702 for (int i = 0; i < n; i++) {
703 if (order[i] != slot)
704 continue;
705
706 if (i + 1 < n)
707 memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
708 return efi_set_boot_order(order, n - 1);
709 }
710
711 return 0;
712 }
713
714 static const char *pick_efi_boot_option_description(void) {
715 return arg_efi_boot_option_description ?: "Linux Boot Manager";
716 }
717
718 static int install_variables(
719 const char *esp_path,
720 uint32_t part,
721 uint64_t pstart,
722 uint64_t psize,
723 sd_id128_t uuid,
724 const char *path,
725 bool first) {
726
727 uint16_t slot;
728 int r;
729
730 if (arg_root) {
731 log_info("Acting on %s, skipping EFI variable setup.",
732 arg_image ? "image" : "root directory");
733 return 0;
734 }
735
736 if (!is_efi_boot()) {
737 log_warning("Not booted with EFI, skipping EFI variable setup.");
738 return 0;
739 }
740
741 r = chase_symlinks_and_access(path, esp_path, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, F_OK, NULL, NULL);
742 if (r == -ENOENT)
743 return 0;
744 if (r < 0)
745 return log_error_errno(r, "Cannot access \"%s/%s\": %m", esp_path, path);
746
747 r = find_slot(uuid, path, &slot);
748 if (r < 0)
749 return log_error_errno(r,
750 r == -ENOENT ?
751 "Failed to access EFI variables. Is the \"efivarfs\" filesystem mounted?" :
752 "Failed to determine current boot order: %m");
753
754 if (first || r == 0) {
755 r = efi_add_boot_option(slot, pick_efi_boot_option_description(),
756 part, pstart, psize,
757 uuid, path);
758 if (r < 0)
759 return log_error_errno(r, "Failed to create EFI Boot variable entry: %m");
760
761 log_info("Created EFI boot entry \"%s\".", pick_efi_boot_option_description());
762 }
763
764 return insert_into_order(slot, first);
765 }
766
767 static int are_we_installed(const char *esp_path) {
768 int r;
769
770 /* Tests whether systemd-boot is installed. It's not obvious what to use as check here: we could
771 * check EFI variables, we could check what binary /EFI/BOOT/BOOT*.EFI points to, or whether the
772 * loader entries directory exists. Here we opted to check whether /EFI/systemd/ is non-empty, which
773 * should be a suitable and very minimal check for a number of reasons:
774 *
775 * → The check is architecture independent (i.e. we check if any systemd-boot loader is installed,
776 * not a specific one.)
777 *
778 * → It doesn't assume we are the only boot loader (i.e doesn't check if we own the main
779 * /EFI/BOOT/BOOT*.EFI fallback binary.
780 *
781 * → It specifically checks for systemd-boot, not for other boot loaders (which a check for
782 * /boot/loader/entries would do). */
783
784 _cleanup_free_ char *p = path_join(esp_path, "/EFI/systemd/");
785 if (!p)
786 return log_oom();
787
788 log_debug("Checking whether %s contains any files%s", p, special_glyph(SPECIAL_GLYPH_ELLIPSIS));
789 r = dir_is_empty(p, /* ignore_hidden_or_backup= */ false);
790 if (r < 0 && r != -ENOENT)
791 return log_error_errno(r, "Failed to check whether %s contains any files: %m", p);
792
793 return r == 0;
794 }
795
796 int verb_install(int argc, char *argv[], void *userdata) {
797 sd_id128_t uuid = SD_ID128_NULL;
798 uint64_t pstart = 0, psize = 0;
799 uint32_t part = 0;
800 bool install, graceful;
801 int r;
802
803 /* Invoked for both "update" and "install" */
804
805 install = streq(argv[0], "install");
806 graceful = !install && arg_graceful; /* support graceful mode for updates */
807
808 r = acquire_esp(/* unprivileged_mode= */ false, graceful, &part, &pstart, &psize, &uuid, NULL);
809 if (graceful && r == -ENOKEY)
810 return 0; /* If --graceful is specified and we can't find an ESP, handle this cleanly */
811 if (r < 0)
812 return r;
813
814 if (!install) {
815 /* If we are updating, don't do anything if sd-boot wasn't actually installed. */
816 r = are_we_installed(arg_esp_path);
817 if (r < 0)
818 return r;
819 if (r == 0) {
820 log_debug("Skipping update because sd-boot is not installed in the ESP.");
821 return 0;
822 }
823 }
824
825 r = acquire_xbootldr(/* unprivileged_mode= */ false, NULL, NULL);
826 if (r < 0)
827 return r;
828
829 r = settle_make_entry_directory();
830 if (r < 0)
831 return r;
832
833 const char *arch = arg_arch_all ? "" : get_efi_arch();
834
835 WITH_UMASK(0002) {
836 if (install) {
837 /* Don't create any of these directories when we are just updating. When we update
838 * we'll drop-in our files (unless there are newer ones already), but we won't create
839 * the directories for them in the first place. */
840 r = create_subdirs(arg_esp_path, esp_subdirs);
841 if (r < 0)
842 return r;
843
844 r = create_subdirs(arg_dollar_boot_path(), dollar_boot_subdirs);
845 if (r < 0)
846 return r;
847 }
848
849 r = install_binaries(arg_esp_path, arch, install);
850 if (r < 0)
851 return r;
852
853 if (install) {
854 r = install_loader_config(arg_esp_path);
855 if (r < 0)
856 return r;
857
858 r = install_entry_directory(arg_dollar_boot_path());
859 if (r < 0)
860 return r;
861
862 r = install_entry_token();
863 if (r < 0)
864 return r;
865
866 r = install_random_seed(arg_esp_path);
867 if (r < 0)
868 return r;
869 }
870
871 r = install_loader_specification(arg_dollar_boot_path());
872 if (r < 0)
873 return r;
874 }
875
876 (void) sync_everything();
877
878 if (!arg_touch_variables)
879 return 0;
880
881 if (arg_arch_all) {
882 log_info("Not changing EFI variables with --all-architectures.");
883 return 0;
884 }
885
886 char *path = strjoina("/EFI/systemd/systemd-boot", arch, ".efi");
887 return install_variables(arg_esp_path, part, pstart, psize, uuid, path, install);
888 }
889
890 static int remove_boot_efi(const char *esp_path) {
891 _cleanup_closedir_ DIR *d = NULL;
892 _cleanup_free_ char *p = NULL;
893 int r, c = 0;
894
895 r = chase_symlinks_and_opendir("/EFI/BOOT", esp_path, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, &p, &d);
896 if (r == -ENOENT)
897 return 0;
898 if (r < 0)
899 return log_error_errno(r, "Failed to open directory \"%s/EFI/BOOT\": %m", esp_path);
900
901 FOREACH_DIRENT(de, d, break) {
902 _cleanup_close_ int fd = -1;
903 _cleanup_free_ char *v = NULL;
904
905 if (!endswith_no_case(de->d_name, ".efi"))
906 continue;
907
908 if (!startswith_no_case(de->d_name, "boot"))
909 continue;
910
911 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
912 if (fd < 0)
913 return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name);
914
915 r = get_file_version(fd, &v);
916 if (r < 0)
917 return r;
918 if (r > 0 && startswith(v, "systemd-boot ")) {
919 r = unlinkat(dirfd(d), de->d_name, 0);
920 if (r < 0)
921 return log_error_errno(errno, "Failed to remove \"%s/%s\": %m", p, de->d_name);
922
923 log_info("Removed \"%s/%s\".", p, de->d_name);
924 }
925
926 c++;
927 }
928
929 return c;
930 }
931
932 static int rmdir_one(const char *prefix, const char *suffix) {
933 const char *p;
934
935 p = prefix_roota(prefix, suffix);
936 if (rmdir(p) < 0) {
937 bool ignore = IN_SET(errno, ENOENT, ENOTEMPTY);
938
939 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, errno,
940 "Failed to remove directory \"%s\": %m", p);
941 if (!ignore)
942 return -errno;
943 } else
944 log_info("Removed \"%s\".", p);
945
946 return 0;
947 }
948
949 static int remove_subdirs(const char *root, const char *const *subdirs) {
950 int r, q;
951
952 /* We use recursion here to destroy the directories in reverse order. Which should be safe given how
953 * short the array is. */
954
955 if (!subdirs[0]) /* A the end of the list */
956 return 0;
957
958 r = remove_subdirs(root, subdirs + 1);
959 q = rmdir_one(root, subdirs[0]);
960
961 return r < 0 ? r : q;
962 }
963
964 static int remove_entry_directory(const char *root) {
965 assert(root);
966 assert(arg_make_entry_directory >= 0);
967
968 if (!arg_make_entry_directory || !arg_entry_token)
969 return 0;
970
971 return rmdir_one(root, arg_entry_token);
972 }
973
974 static int remove_binaries(const char *esp_path) {
975 const char *p;
976 int r, q;
977
978 p = prefix_roota(esp_path, "/EFI/systemd");
979 r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
980
981 q = remove_boot_efi(esp_path);
982 if (q < 0 && r == 0)
983 r = q;
984
985 return r;
986 }
987
988 static int remove_file(const char *root, const char *file) {
989 const char *p;
990
991 assert(root);
992 assert(file);
993
994 p = prefix_roota(root, file);
995 if (unlink(p) < 0) {
996 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
997 "Failed to unlink file \"%s\": %m", p);
998
999 return errno == ENOENT ? 0 : -errno;
1000 }
1001
1002 log_info("Removed \"%s\".", p);
1003 return 1;
1004 }
1005
1006 static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
1007 uint16_t slot;
1008 int r;
1009
1010 if (arg_root || !is_efi_boot())
1011 return 0;
1012
1013 r = find_slot(uuid, path, &slot);
1014 if (r != 1)
1015 return 0;
1016
1017 r = efi_remove_boot_option(slot);
1018 if (r < 0)
1019 return r;
1020
1021 if (in_order)
1022 return remove_from_order(slot);
1023
1024 return 0;
1025 }
1026
1027 static int remove_loader_variables(void) {
1028 int r = 0;
1029
1030 /* Remove all persistent loader variables we define */
1031
1032 FOREACH_STRING(var,
1033 EFI_LOADER_VARIABLE(LoaderConfigTimeout),
1034 EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot),
1035 EFI_LOADER_VARIABLE(LoaderEntryDefault),
1036 EFI_LOADER_VARIABLE(LoaderEntryOneShot),
1037 EFI_LOADER_VARIABLE(LoaderSystemToken)){
1038
1039 int q;
1040
1041 q = efi_set_variable(var, NULL, 0);
1042 if (q == -ENOENT)
1043 continue;
1044 if (q < 0) {
1045 log_warning_errno(q, "Failed to remove EFI variable %s: %m", var);
1046 if (r >= 0)
1047 r = q;
1048 } else
1049 log_info("Removed EFI variable %s.", var);
1050 }
1051
1052 return r;
1053 }
1054
1055 int verb_remove(int argc, char *argv[], void *userdata) {
1056 sd_id128_t uuid = SD_ID128_NULL;
1057 int r, q;
1058
1059 r = acquire_esp(/* unprivileged_mode= */ false, /* graceful= */ false, NULL, NULL, NULL, &uuid, NULL);
1060 if (r < 0)
1061 return r;
1062
1063 r = acquire_xbootldr(/* unprivileged_mode= */ false, NULL, NULL);
1064 if (r < 0)
1065 return r;
1066
1067 r = settle_make_entry_directory();
1068 if (r < 0)
1069 return r;
1070
1071 r = remove_binaries(arg_esp_path);
1072
1073 q = remove_file(arg_esp_path, "/loader/loader.conf");
1074 if (q < 0 && r >= 0)
1075 r = q;
1076
1077 q = remove_file(arg_esp_path, "/loader/random-seed");
1078 if (q < 0 && r >= 0)
1079 r = q;
1080
1081 q = remove_file(arg_esp_path, "/loader/entries.srel");
1082 if (q < 0 && r >= 0)
1083 r = q;
1084
1085 q = remove_subdirs(arg_esp_path, esp_subdirs);
1086 if (q < 0 && r >= 0)
1087 r = q;
1088
1089 q = remove_subdirs(arg_esp_path, dollar_boot_subdirs);
1090 if (q < 0 && r >= 0)
1091 r = q;
1092
1093 q = remove_entry_directory(arg_esp_path);
1094 if (q < 0 && r >= 0)
1095 r = q;
1096
1097 if (arg_xbootldr_path) {
1098 /* Remove a subset of these also from the XBOOTLDR partition if it exists */
1099
1100 q = remove_file(arg_xbootldr_path, "/loader/entries.srel");
1101 if (q < 0 && r >= 0)
1102 r = q;
1103
1104 q = remove_subdirs(arg_xbootldr_path, dollar_boot_subdirs);
1105 if (q < 0 && r >= 0)
1106 r = q;
1107
1108 q = remove_entry_directory(arg_xbootldr_path);
1109 if (q < 0 && r >= 0)
1110 r = q;
1111 }
1112
1113 (void) sync_everything();
1114
1115 if (!arg_touch_variables)
1116 return r;
1117
1118 if (arg_arch_all) {
1119 log_info("Not changing EFI variables with --all-architectures.");
1120 return r;
1121 }
1122
1123 char *path = strjoina("/EFI/systemd/systemd-boot", get_efi_arch(), ".efi");
1124 q = remove_variables(uuid, path, true);
1125 if (q < 0 && r >= 0)
1126 r = q;
1127
1128 q = remove_loader_variables();
1129 if (q < 0 && r >= 0)
1130 r = q;
1131
1132 return r;
1133 }
1134
1135 int verb_is_installed(int argc, char *argv[], void *userdata) {
1136 int r;
1137
1138 r = acquire_esp(/* privileged_mode= */ false,
1139 /* graceful= */ arg_graceful,
1140 NULL, NULL, NULL, NULL, NULL);
1141 if (r < 0)
1142 return r;
1143
1144 r = are_we_installed(arg_esp_path);
1145 if (r < 0)
1146 return r;
1147
1148 if (r > 0) {
1149 if (!arg_quiet)
1150 puts("yes");
1151 return EXIT_SUCCESS;
1152 } else {
1153 if (!arg_quiet)
1154 puts("no");
1155 return EXIT_FAILURE;
1156 }
1157 }