]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/boot/bootctl.c
calendarspec: rename free_chain() to chain_free()
[thirdparty/systemd.git] / src / boot / bootctl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
7b4d7cc0 2
6b5cf3ea 3#include <blkid.h>
0974a682 4#include <ctype.h>
3f6fd1ba
LP
5#include <dirent.h>
6#include <errno.h>
0974a682 7#include <ftw.h>
3f6fd1ba
LP
8#include <getopt.h>
9#include <limits.h>
5fa6c13c 10#include <linux/magic.h>
0974a682 11#include <stdbool.h>
3f6fd1ba
LP
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/mman.h>
16#include <sys/stat.h>
17#include <sys/statfs.h>
18#include <unistd.h>
7b4d7cc0 19
dccca82b
LP
20#include "sd-id128.h"
21
b5efdb8a 22#include "alloc-util.h"
3f6fd1ba 23#include "blkid-util.h"
7e87c7d9 24#include "bootspec.h"
175d308c 25#include "copy.h"
e41256dc 26#include "dirent-util.h"
0974a682 27#include "efivars.h"
3ffd4af2 28#include "fd-util.h"
0d39fa9c 29#include "fileio.h"
175d308c 30#include "fs-util.h"
8752c575 31#include "locale-util.h"
608f8ec9 32#include "main-func.h"
57db6f18 33#include "pager.h"
2f2c539c 34#include "parse-util.h"
294bf0c3 35#include "pretty-print.h"
c6878637 36#include "rm-rf.h"
175d308c 37#include "stat-util.h"
341890de 38#include "stdio-util.h"
07630cea 39#include "string-util.h"
2f2c539c 40#include "strv.h"
7e87c7d9 41#include "terminal-util.h"
e4de7287 42#include "tmpfile-util.h"
2f2c539c 43#include "umask-util.h"
d88c96ff 44#include "utf8.h"
3f6fd1ba 45#include "util.h"
2f2c539c
LP
46#include "verbs.h"
47#include "virt.h"
7b4d7cc0 48
fbf45d22
LP
49static char *arg_esp_path = NULL;
50static char *arg_xbootldr_path = NULL;
51static bool arg_print_esp_path = false;
52static bool arg_print_dollar_boot_path = false;
25579a43 53static bool arg_touch_variables = true;
0221d68a 54static PagerFlags arg_pager_flags = 0;
25579a43 55
fbf45d22
LP
56STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
57STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
58
59static const char *arg_dollar_boot_path(void) {
60 /* $BOOT shall be the XBOOTLDR partition if it exists, and otherwise the ESP */
61 return arg_xbootldr_path ?: arg_esp_path;
62}
608f8ec9 63
5caa3167
LP
64static int acquire_esp(
65 bool unprivileged_mode,
66 uint32_t *ret_part,
67 uint64_t *ret_pstart,
68 uint64_t *ret_psize,
69 sd_id128_t *ret_uuid) {
70
71 char *np;
2f2c539c
LP
72 int r;
73
fbf45d22
LP
74 /* Find the ESP, and log about errors. Note that find_esp_and_warn() will log in all error cases on
75 * its own, except for ENOKEY (which is good, we want to show our own message in that case,
76 * suggesting use of --esp-path=) and EACCESS (only when we request unprivileged mode; in this case
77 * we simply eat up the error here, so that --list and --status work too, without noise about
78 * this). */
5caa3167 79
fbf45d22 80 r = find_esp_and_warn(arg_esp_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid);
5caa3167 81 if (r == -ENOKEY)
af918182 82 return log_error_errno(r,
5caa3167 83 "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n"
fbf45d22 84 "Alternatively, use --esp-path= to specify path to mount point.");
5caa3167
LP
85 if (r < 0)
86 return r;
87
fbf45d22
LP
88 free_and_replace(arg_esp_path, np);
89 log_debug("Using EFI System Partition at %s.", arg_esp_path);
90
91 return 1;
92}
0974a682 93
fbf45d22
LP
94static int acquire_xbootldr(bool unprivileged_mode, sd_id128_t *ret_uuid) {
95 char *np;
96 int r;
5caa3167 97
fbf45d22
LP
98 r = find_xbootldr_and_warn(arg_xbootldr_path, unprivileged_mode, &np, ret_uuid);
99 if (r == -ENOKEY) {
100 log_debug_errno(r, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
101 if (ret_uuid)
102 *ret_uuid = SD_ID128_NULL;
103 return 0;
104 }
105 if (r < 0)
106 return r;
107
108 free_and_replace(arg_xbootldr_path, np);
109 log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path);
110
111 return 1;
7b4d7cc0
KS
112}
113
e7dd673d 114/* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */
d3226d77 115static int get_file_version(int fd, char **v) {
0974a682
KS
116 struct stat st;
117 char *buf;
118 const char *s, *e;
119 char *x = NULL;
120 int r = 0;
7b4d7cc0 121
d3226d77 122 assert(fd >= 0);
0974a682 123 assert(v);
7b4d7cc0 124
d3226d77 125 if (fstat(fd, &st) < 0)
db6d9fae 126 return log_error_errno(errno, "Failed to stat EFI binary: %m");
7b4d7cc0 127
c4ba5b51
LP
128 r = stat_verify_regular(&st);
129 if (r < 0)
130 return log_error_errno(errno, "EFI binary is not a regular file: %m");
131
db6d9fae
LP
132 if (st.st_size < 27) {
133 *v = NULL;
0974a682 134 return 0;
db6d9fae 135 }
eb9da376 136
d3226d77 137 buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
0974a682 138 if (buf == MAP_FAILED)
db6d9fae 139 return log_error_errno(errno, "Failed to memory map EFI binary: %m");
7b4d7cc0 140
0974a682
KS
141 s = memmem(buf, st.st_size - 8, "#### LoaderInfo: ", 17);
142 if (!s)
143 goto finish;
144 s += 17;
7b4d7cc0 145
0974a682
KS
146 e = memmem(s, st.st_size - (s - buf), " ####", 5);
147 if (!e || e - s < 3) {
78d5d4ed 148 r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Malformed version string.");
0974a682
KS
149 goto finish;
150 }
7b4d7cc0 151
0974a682
KS
152 x = strndup(s, e - s);
153 if (!x) {
d3226d77 154 r = log_oom();
0974a682
KS
155 goto finish;
156 }
157 r = 1;
7b4d7cc0 158
0974a682 159finish:
db6d9fae 160 (void) munmap(buf, st.st_size);
0974a682
KS
161 *v = x;
162 return r;
163}
7b4d7cc0 164
0974a682 165static int enumerate_binaries(const char *esp_path, const char *path, const char *prefix) {
d3226d77 166 _cleanup_closedir_ DIR *d = NULL;
0974a682 167 struct dirent *de;
fbf45d22
LP
168 int c = 0, r;
169 char *p;
170
171 assert(esp_path);
172 assert(path);
0974a682 173
d3226d77 174 p = strjoina(esp_path, "/", path);
0974a682
KS
175 d = opendir(p);
176 if (!d) {
d3226d77
ZJS
177 if (errno == ENOENT)
178 return 0;
7b4d7cc0 179
d3226d77 180 return log_error_errno(errno, "Failed to read \"%s\": %m", p);
0974a682
KS
181 }
182
e41256dc 183 FOREACH_DIRENT(de, d, break) {
d3226d77 184 _cleanup_free_ char *v = NULL;
fbf45d22 185 _cleanup_close_ int fd = -1;
0974a682 186
d3226d77 187 if (!endswith_no_case(de->d_name, ".efi"))
0974a682
KS
188 continue;
189
d3226d77 190 if (prefix && !startswith_no_case(de->d_name, prefix))
0974a682
KS
191 continue;
192
d3226d77
ZJS
193 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
194 if (fd < 0)
195 return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name);
0974a682 196
d3226d77 197 r = get_file_version(fd, &v);
0974a682 198 if (r < 0)
d3226d77 199 return r;
0974a682 200 if (r > 0)
9a6f746f 201 printf(" File: %s/%s/%s (%s%s%s)\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path, de->d_name, ansi_highlight(), v, ansi_normal());
0974a682 202 else
9a6f746f 203 printf(" File: %s/%s/%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path, de->d_name);
fbf45d22 204
0974a682 205 c++;
0974a682
KS
206 }
207
d3226d77 208 return c;
7b4d7cc0
KS
209}
210
0974a682
KS
211static int status_binaries(const char *esp_path, sd_id128_t partition) {
212 int r;
213
7fd66f3c 214 printf("Available Boot Loaders on ESP:\n");
0974a682 215
cd2d4c7f
YW
216 if (!esp_path) {
217 printf(" ESP: Cannot find or access mount point of ESP.\n\n");
218 return -ENOENT;
219 }
220
221 printf(" ESP: %s", esp_path);
222 if (!sd_id128_is_null(partition))
223 printf(" (/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x)", SD_ID128_FORMAT_VAL(partition));
224 printf("\n");
0974a682
KS
225
226 r = enumerate_binaries(esp_path, "EFI/systemd", NULL);
fbf45d22 227 if (r < 0)
882b3bd6 228 goto finish;
0974a682 229 if (r == 0)
48184e43 230 log_info("systemd-boot not installed in ESP.");
0974a682 231
00f69504 232 r = enumerate_binaries(esp_path, "EFI/BOOT", "boot");
fbf45d22 233 if (r < 0)
882b3bd6 234 goto finish;
0974a682 235 if (r == 0)
48184e43 236 log_info("No default/fallback boot loader installed in ESP.");
0974a682 237
882b3bd6 238 r = 0;
c11ae0ba 239
882b3bd6
LP
240finish:
241 printf("\n");
242 return r;
0974a682
KS
243}
244
245static int print_efi_option(uint16_t id, bool in_order) {
7cb0f263
TA
246 _cleanup_free_ char *title = NULL;
247 _cleanup_free_ char *path = NULL;
0974a682
KS
248 sd_id128_t partition;
249 bool active;
250 int r = 0;
251
252 r = efi_get_boot_option(id, &title, &partition, &path, &active);
253 if (r < 0)
7cb0f263 254 return r;
7b4d7cc0 255
0974a682 256 /* print only configured entries with partition information */
3bbaff3e 257 if (!path || sd_id128_is_null(partition))
0974a682
KS
258 return 0;
259
260 efi_tilt_backslashes(path);
261
ba857253 262 printf(" Title: %s%s%s\n", ansi_highlight(), strna(title), ansi_normal());
0974a682
KS
263 printf(" ID: 0x%04X\n", id);
264 printf(" Status: %sactive%s\n", active ? "" : "in", in_order ? ", boot-order" : "");
265 printf(" Partition: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n", SD_ID128_FORMAT_VAL(partition));
9a6f746f 266 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path);
0974a682
KS
267 printf("\n");
268
7cb0f263 269 return 0;
0974a682
KS
270}
271
272static int status_variables(void) {
d3226d77 273 _cleanup_free_ uint16_t *options = NULL, *order = NULL;
a36b411e 274 int n_options, n_order, i;
0974a682 275
0974a682 276 n_options = efi_get_boot_options(&options);
d3226d77 277 if (n_options == -ENOENT)
f939cff7
LP
278 return log_error_errno(n_options,
279 "Failed to access EFI variables, efivarfs"
d3226d77 280 " needs to be available at /sys/firmware/efi/efivars/.");
f939cff7 281 if (n_options < 0)
d3226d77 282 return log_error_errno(n_options, "Failed to read EFI boot entries: %m");
0974a682 283
0974a682 284 n_order = efi_get_boot_order(&order);
d3226d77 285 if (n_order == -ENOENT)
0974a682 286 n_order = 0;
d3226d77 287 else if (n_order < 0)
7709ef3a 288 return log_error_errno(n_order, "Failed to read EFI boot order: %m");
0974a682
KS
289
290 /* print entries in BootOrder first */
7fd66f3c 291 printf("Boot Loaders Listed in EFI Variables:\n");
0974a682
KS
292 for (i = 0; i < n_order; i++)
293 print_efi_option(order[i], true);
294
295 /* print remaining entries */
296 for (i = 0; i < n_options; i++) {
297 int j;
0974a682
KS
298
299 for (j = 0; j < n_order; j++)
d3226d77 300 if (options[i] == order[j])
a908cf0a 301 goto next_option;
0974a682
KS
302
303 print_efi_option(options[i], false);
a908cf0a
MM
304
305 next_option:
306 continue;
0974a682
KS
307 }
308
d3226d77 309 return 0;
0974a682
KS
310}
311
20a28174
LP
312static int boot_entry_show(const BootEntry *e, bool show_as_default) {
313 assert(e);
314
93f14ce2
LP
315 printf(" title: %s%s%s%s%s%s\n"
316 " type: %s\n",
20a28174
LP
317 ansi_highlight(),
318 boot_entry_title(e),
319 ansi_normal(),
320 ansi_highlight_green(),
321 show_as_default ? " (default)" : "",
93f14ce2
LP
322 ansi_normal(),
323 boot_entry_type_to_string(e->type));
20a28174
LP
324
325 if (e->id)
326 printf(" id: %s\n", e->id);
327 if (e->version)
328 printf(" version: %s\n", e->version);
329 if (e->machine_id)
330 printf(" machine-id: %s\n", e->machine_id);
331 if (e->architecture)
332 printf(" architecture: %s\n", e->architecture);
333 if (e->kernel)
334 printf(" linux: %s\n", e->kernel);
335 if (!strv_isempty(e->initrd)) {
336 _cleanup_free_ char *t;
337
338 t = strv_join(e->initrd, " ");
339 if (!t)
340 return log_oom();
341
342 printf(" initrd: %s\n", t);
343 }
344 if (!strv_isempty(e->options)) {
345 _cleanup_free_ char *t;
346
347 t = strv_join(e->options, " ");
348 if (!t)
349 return log_oom();
350
351 printf(" options: %s\n", t);
352 }
353 if (e->device_tree)
354 printf(" devicetree: %s\n", e->device_tree);
355
356 return 0;
357}
358
81fed855
LP
359static int status_entries(
360 const char *esp_path,
361 sd_id128_t esp_partition_uuid,
362 const char *xbootldr_path,
363 sd_id128_t xbootldr_partition_uuid) {
364
7e87c7d9 365 _cleanup_(boot_config_free) BootConfig config = {};
81fed855
LP
366 sd_id128_t dollar_boot_partition_uuid;
367 const char *dollar_boot_path;
a099e035 368 int r;
7e87c7d9 369
81fed855
LP
370 assert(esp_path || xbootldr_path);
371
372 if (xbootldr_path) {
373 dollar_boot_path = xbootldr_path;
374 dollar_boot_partition_uuid = xbootldr_partition_uuid;
375 } else {
376 dollar_boot_path = esp_path;
377 dollar_boot_partition_uuid = esp_partition_uuid;
378 }
379
380 printf("Boot Loader Entries:\n"
381 " $BOOT: %s", dollar_boot_path);
382 if (!sd_id128_is_null(dollar_boot_partition_uuid))
383 printf(" (/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x)", SD_ID128_FORMAT_VAL(dollar_boot_partition_uuid));
384 printf("\n\n");
385
fbf45d22 386 r = boot_entries_load_config(esp_path, xbootldr_path, &config);
7e87c7d9 387 if (r < 0)
21f7a622 388 return r;
7e87c7d9
ZJS
389
390 if (config.default_entry < 0)
a099e035 391 printf("%zu entries, no entry could be determined as default.\n", config.n_entries);
7e87c7d9 392 else {
7fd66f3c 393 printf("Default Boot Loader Entry:\n");
a099e035 394
20a28174 395 boot_entry_show(config.entries + config.default_entry, false);
7e87c7d9
ZJS
396 }
397
398 return 0;
399}
400
0974a682
KS
401static int compare_product(const char *a, const char *b) {
402 size_t x, y;
403
404 assert(a);
405 assert(b);
406
407 x = strcspn(a, " ");
408 y = strcspn(b, " ");
409 if (x != y)
410 return x < y ? -1 : x > y ? 1 : 0;
411
412 return strncmp(a, b, x);
413}
414
415static int compare_version(const char *a, const char *b) {
416 assert(a);
417 assert(b);
418
419 a += strcspn(a, " ");
420 a += strspn(a, " ");
421 b += strcspn(b, " ");
422 b += strspn(b, " ");
423
424 return strverscmp(a, b);
425}
426
175d308c 427static int version_check(int fd_from, const char *from, int fd_to, const char *to) {
d3226d77 428 _cleanup_free_ char *a = NULL, *b = NULL;
0974a682
KS
429 int r;
430
175d308c 431 assert(fd_from >= 0);
0974a682 432 assert(from);
175d308c 433 assert(fd_to >= 0);
0974a682
KS
434 assert(to);
435
175d308c 436 r = get_file_version(fd_from, &a);
0974a682 437 if (r < 0)
d3226d77 438 return r;
baaa35ad
ZJS
439 if (r == 0)
440 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
441 "Source file \"%s\" does not carry version information!",
442 from);
0974a682 443
175d308c 444 r = get_file_version(fd_to, &b);
0974a682 445 if (r < 0)
d3226d77 446 return r;
baaa35ad
ZJS
447 if (r == 0 || compare_product(a, b) != 0)
448 return log_notice_errno(SYNTHETIC_ERRNO(EEXIST),
449 "Skipping \"%s\", since it's owned by another boot loader.",
450 to);
0974a682 451
78d5d4ed
LP
452 if (compare_version(a, b) < 0)
453 return log_warning_errno(SYNTHETIC_ERRNO(ESTALE), "Skipping \"%s\", since a newer boot loader version exists already.", to);
0974a682 454
d3226d77 455 return 0;
0974a682
KS
456}
457
175d308c
LP
458static int copy_file_with_version_check(const char *from, const char *to, bool force) {
459 _cleanup_close_ int fd_from = -1, fd_to = -1;
460 _cleanup_free_ char *t = NULL;
0974a682 461 int r;
0974a682 462
175d308c
LP
463 fd_from = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
464 if (fd_from < 0)
d3226d77 465 return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", from);
0974a682
KS
466
467 if (!force) {
175d308c
LP
468 fd_to = open(to, O_RDONLY|O_CLOEXEC|O_NOCTTY);
469 if (fd_to < 0) {
470 if (errno != -ENOENT)
471 return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", to);
472 } else {
473 r = version_check(fd_from, from, fd_to, to);
474 if (r < 0)
475 return r;
476
477 if (lseek(fd_from, 0, SEEK_SET) == (off_t) -1)
6719ca72 478 return log_error_errno(errno, "Failed to seek in \"%s\": %m", from);
175d308c
LP
479
480 fd_to = safe_close(fd_to);
0974a682 481 }
175d308c 482 }
d3226d77 483
175d308c
LP
484 r = tempfn_random(to, NULL, &t);
485 if (r < 0)
486 return log_oom();
0974a682 487
175d308c
LP
488 RUN_WITH_UMASK(0000) {
489 fd_to = open(t, O_WRONLY|O_CREAT|O_CLOEXEC|O_EXCL|O_NOFOLLOW, 0644);
490 if (fd_to < 0)
491 return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", t);
0974a682
KS
492 }
493
175d308c 494 r = copy_bytes(fd_from, fd_to, (uint64_t) -1, COPY_REFLINK);
0974a682 495 if (r < 0) {
0675e94a
AJ
496 (void) unlink(t);
497 return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
0974a682
KS
498 }
499
adc6f43b 500 (void) copy_times(fd_from, fd_to, 0);
0974a682 501
8ac2f74f 502 if (fsync(fd_to) < 0) {
0675e94a
AJ
503 (void) unlink_noerrno(t);
504 return log_error_errno(errno, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
505 }
506
8ac2f74f
LP
507 (void) fsync_directory_of_file(fd_to);
508
b1c05b98 509 if (renameat(AT_FDCWD, t, AT_FDCWD, to) < 0) {
175d308c
LP
510 (void) unlink_noerrno(t);
511 return log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", t, to);
0974a682
KS
512 }
513
d3226d77 514 log_info("Copied \"%s\" to \"%s\".", from, to);
0974a682 515
175d308c 516 return 0;
0974a682
KS
517}
518
0974a682 519static int mkdir_one(const char *prefix, const char *suffix) {
e2600fd5 520 _cleanup_free_ char *p = NULL;
0974a682 521
e2600fd5 522 p = path_join(prefix, suffix);
0974a682 523 if (mkdir(p, 0700) < 0) {
d3226d77
ZJS
524 if (errno != EEXIST)
525 return log_error_errno(errno, "Failed to create \"%s\": %m", p);
0974a682 526 } else
d3226d77 527 log_info("Created \"%s\".", p);
7b4d7cc0 528
0974a682
KS
529 return 0;
530}
531
fbf45d22 532static const char *const esp_subdirs[] = {
d3226d77
ZJS
533 "EFI",
534 "EFI/systemd",
00f69504 535 "EFI/BOOT",
d3226d77 536 "loader",
fbf45d22
LP
537 /* Note that "/loader/entries" is not listed here, since it should be placed in $BOOT, which might
538 * not necessarily be the ESP */
9ee051b9 539 NULL
d3226d77
ZJS
540};
541
fbf45d22
LP
542static int create_esp_subdirs(const char *esp_path) {
543 const char *const *i;
0974a682
KS
544 int r;
545
fbf45d22 546 STRV_FOREACH(i, esp_subdirs) {
181ccb43 547 r = mkdir_one(esp_path, *i);
d3226d77
ZJS
548 if (r < 0)
549 return r;
550 }
7b4d7cc0 551
7b4d7cc0 552 return 0;
7b4d7cc0
KS
553}
554
0974a682 555static int copy_one_file(const char *esp_path, const char *name, bool force) {
d3226d77 556 char *p, *q;
0974a682
KS
557 int r;
558
d3226d77
ZJS
559 p = strjoina(BOOTLIBDIR "/", name);
560 q = strjoina(esp_path, "/EFI/systemd/", name);
175d308c 561 r = copy_file_with_version_check(p, q, force);
0974a682 562
e7dd673d 563 if (startswith(name, "systemd-boot")) {
0974a682 564 int k;
d3226d77 565 char *v;
0974a682
KS
566
567 /* Create the EFI default boot loader name (specified for removable devices) */
fbd0b64f
LP
568 v = strjoina(esp_path, "/EFI/BOOT/BOOT",
569 name + STRLEN("systemd-boot"));
846b8fc3 570 ascii_strupper(strrchr(v, '/') + 1);
0974a682 571
175d308c 572 k = copy_file_with_version_check(p, v, force);
0974a682 573 if (k < 0 && r == 0)
d3226d77 574 r = k;
0974a682
KS
575 }
576
577 return r;
7b4d7cc0
KS
578}
579
0974a682
KS
580static int install_binaries(const char *esp_path, bool force) {
581 struct dirent *de;
d3226d77 582 _cleanup_closedir_ DIR *d = NULL;
0974a682
KS
583 int r = 0;
584
e7dd673d 585 d = opendir(BOOTLIBDIR);
d3226d77
ZJS
586 if (!d)
587 return log_error_errno(errno, "Failed to open \""BOOTLIBDIR"\": %m");
0974a682 588
0d73a816 589 FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \""BOOTLIBDIR"\": %m")) {
0974a682
KS
590 int k;
591
d3226d77 592 if (!endswith_no_case(de->d_name, ".efi"))
0974a682
KS
593 continue;
594
595 k = copy_one_file(esp_path, de->d_name, force);
596 if (k < 0 && r == 0)
597 r = k;
598 }
599
0974a682 600 return r;
7b4d7cc0
KS
601}
602
0974a682 603static bool same_entry(uint16_t id, const sd_id128_t uuid, const char *path) {
d3226d77 604 _cleanup_free_ char *opath = NULL;
0974a682 605 sd_id128_t ouuid;
d3226d77 606 int r;
7b4d7cc0 607
d3226d77
ZJS
608 r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
609 if (r < 0)
0974a682
KS
610 return false;
611 if (!sd_id128_equal(uuid, ouuid))
d3226d77 612 return false;
0974a682 613 if (!streq_ptr(path, opath))
d3226d77 614 return false;
0974a682 615
d3226d77 616 return true;
0974a682
KS
617}
618
619static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
d3226d77
ZJS
620 _cleanup_free_ uint16_t *options = NULL;
621 int n, i;
0974a682 622
d3226d77
ZJS
623 n = efi_get_boot_options(&options);
624 if (n < 0)
625 return n;
0974a682 626
e7dd673d 627 /* find already existing systemd-boot entry */
d3226d77 628 for (i = 0; i < n; i++)
0974a682 629 if (same_entry(options[i], uuid, path)) {
d3226d77
ZJS
630 *id = options[i];
631 return 1;
0974a682
KS
632 }
633
634 /* find free slot in the sorted BootXXXX variable list */
d3226d77 635 for (i = 0; i < n; i++)
0974a682 636 if (i != options[i]) {
d3226d77
ZJS
637 *id = i;
638 return 1;
0974a682
KS
639 }
640
641 /* use the next one */
642 if (i == 0xffff)
643 return -ENOSPC;
d3226d77
ZJS
644 *id = i;
645 return 0;
0974a682
KS
646}
647
648static int insert_into_order(uint16_t slot, bool first) {
d3226d77
ZJS
649 _cleanup_free_ uint16_t *order = NULL;
650 uint16_t *t;
651 int n, i;
0974a682 652
d3226d77
ZJS
653 n = efi_get_boot_order(&order);
654 if (n <= 0)
0974a682 655 /* no entry, add us */
d3226d77 656 return efi_set_boot_order(&slot, 1);
0974a682
KS
657
658 /* are we the first and only one? */
d3226d77
ZJS
659 if (n == 1 && order[0] == slot)
660 return 0;
0974a682
KS
661
662 /* are we already in the boot order? */
d3226d77 663 for (i = 0; i < n; i++) {
0974a682
KS
664 if (order[i] != slot)
665 continue;
666
667 /* we do not require to be the first one, all is fine */
668 if (!first)
d3226d77 669 return 0;
0974a682
KS
670
671 /* move us to the first slot */
d3226d77 672 memmove(order + 1, order, i * sizeof(uint16_t));
0974a682 673 order[0] = slot;
d3226d77 674 return efi_set_boot_order(order, n);
0974a682
KS
675 }
676
677 /* extend array */
a7798cd8 678 t = reallocarray(order, n + 1, sizeof(uint16_t));
d3226d77
ZJS
679 if (!t)
680 return -ENOMEM;
681 order = t;
0974a682
KS
682
683 /* add us to the top or end of the list */
684 if (first) {
d3226d77 685 memmove(order + 1, order, n * sizeof(uint16_t));
0974a682
KS
686 order[0] = slot;
687 } else
d3226d77 688 order[n] = slot;
0974a682 689
d3226d77 690 return efi_set_boot_order(order, n + 1);
0974a682
KS
691}
692
693static int remove_from_order(uint16_t slot) {
7cb0f263 694 _cleanup_free_ uint16_t *order = NULL;
d3226d77 695 int n, i;
0974a682 696
d3226d77
ZJS
697 n = efi_get_boot_order(&order);
698 if (n <= 0)
699 return n;
0974a682 700
d3226d77 701 for (i = 0; i < n; i++) {
0974a682
KS
702 if (order[i] != slot)
703 continue;
704
d3226d77
ZJS
705 if (i + 1 < n)
706 memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
707 return efi_set_boot_order(order, n - 1);
0974a682
KS
708 }
709
d3226d77 710 return 0;
0974a682
KS
711}
712
713static int install_variables(const char *esp_path,
714 uint32_t part, uint64_t pstart, uint64_t psize,
715 sd_id128_t uuid, const char *path,
716 bool first) {
d3226d77 717 char *p;
0974a682
KS
718 uint16_t slot;
719 int r;
720
721 if (!is_efi_boot()) {
d3226d77 722 log_warning("Not booted with EFI, skipping EFI variable setup.");
0974a682
KS
723 return 0;
724 }
725
d3226d77 726 p = strjoina(esp_path, path);
0974a682
KS
727 if (access(p, F_OK) < 0) {
728 if (errno == ENOENT)
d3226d77 729 return 0;
f939cff7
LP
730
731 return log_error_errno(errno, "Cannot access \"%s\": %m", p);
0974a682 732 }
7b4d7cc0 733
0974a682 734 r = find_slot(uuid, path, &slot);
d3226d77
ZJS
735 if (r < 0)
736 return log_error_errno(r,
737 r == -ENOENT ?
738 "Failed to access EFI variables. Is the \"efivarfs\" filesystem mounted?" :
739 "Failed to determine current boot order: %m");
7b4d7cc0 740
181ccb43 741 if (first || r == 0) {
0974a682
KS
742 r = efi_add_boot_option(slot, "Linux Boot Manager",
743 part, pstart, psize,
744 uuid, path);
d3226d77
ZJS
745 if (r < 0)
746 return log_error_errno(r, "Failed to create EFI Boot variable entry: %m");
0974a682 747
d3226d77
ZJS
748 log_info("Created EFI boot entry \"Linux Boot Manager\".");
749 }
0974a682 750
d3226d77 751 return insert_into_order(slot, first);
0974a682
KS
752}
753
754static int remove_boot_efi(const char *esp_path) {
d3226d77
ZJS
755 char *p;
756 _cleanup_closedir_ DIR *d = NULL;
0974a682 757 struct dirent *de;
d3226d77 758 int r, c = 0;
0974a682 759
00f69504 760 p = strjoina(esp_path, "/EFI/BOOT");
0974a682
KS
761 d = opendir(p);
762 if (!d) {
d3226d77
ZJS
763 if (errno == ENOENT)
764 return 0;
0974a682 765
d3226d77 766 return log_error_errno(errno, "Failed to open directory \"%s\": %m", p);
0974a682
KS
767 }
768
e41256dc 769 FOREACH_DIRENT(de, d, break) {
d3226d77
ZJS
770 _cleanup_close_ int fd = -1;
771 _cleanup_free_ char *v = NULL;
0974a682 772
d3226d77 773 if (!endswith_no_case(de->d_name, ".efi"))
0974a682
KS
774 continue;
775
b7536c45 776 if (!startswith_no_case(de->d_name, "boot"))
0974a682
KS
777 continue;
778
d3226d77 779 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
dd114e11 780 if (fd < 0)
d3226d77 781 return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name);
0974a682 782
d3226d77 783 r = get_file_version(fd, &v);
0974a682 784 if (r < 0)
d3226d77
ZJS
785 return r;
786 if (r > 0 && startswith(v, "systemd-boot ")) {
787 r = unlinkat(dirfd(d), de->d_name, 0);
788 if (r < 0)
789 return log_error_errno(errno, "Failed to remove \"%s/%s\": %m", p, de->d_name);
790
a592ab6a 791 log_info("Removed \"%s/%s\".", p, de->d_name);
0974a682
KS
792 }
793
794 c++;
0974a682
KS
795 }
796
d3226d77 797 return c;
0974a682
KS
798}
799
800static int rmdir_one(const char *prefix, const char *suffix) {
801 char *p;
7b4d7cc0 802
d3226d77 803 p = strjoina(prefix, "/", suffix);
0974a682 804 if (rmdir(p) < 0) {
fbf45d22
LP
805 bool ignore = IN_SET(errno, ENOENT, ENOTEMPTY);
806
807 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, errno,
808 "Failed to remove directory \"%s\": %m", p);
809 if (!ignore)
810 return -errno;
7b4d7cc0 811 } else
d3226d77 812 log_info("Removed \"%s\".", p);
7b4d7cc0 813
0974a682 814 return 0;
7b4d7cc0
KS
815}
816
fbf45d22
LP
817static int remove_esp_subdirs(const char *esp_path) {
818 size_t i;
819 int r = 0;
820
821 for (i = ELEMENTSOF(esp_subdirs)-1; i > 0; i--) {
822 int q;
823
824 q = rmdir_one(esp_path, esp_subdirs[i-1]);
825 if (q < 0 && r >= 0)
826 r = q;
827 }
828
829 return r;
830}
831
0974a682
KS
832static int remove_binaries(const char *esp_path) {
833 char *p;
834 int r, q;
835
d3226d77 836 p = strjoina(esp_path, "/EFI/systemd");
c6878637 837 r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
0974a682
KS
838
839 q = remove_boot_efi(esp_path);
840 if (q < 0 && r == 0)
841 r = q;
842
0974a682
KS
843 return r;
844}
845
fbf45d22
LP
846static int remove_loader_config(const char *esp_path) {
847 const char *p;
848
849 assert(esp_path);
850
851 p = strjoina(esp_path, "/loader/loader.conf");
852 if (unlink(p) < 0) {
853 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, "Failed to unlink file \"%s\": %m", p);
854 if (errno != ENOENT)
855 return -errno;
856 } else
857 log_info("Removed \"%s\".", p);
858
859 return 0;
860}
861
862static int remove_entries_directory(const char *dollar_boot_path) {
863 assert(dollar_boot_path);
864
865 return rmdir_one(dollar_boot_path, "/loader/entries");
866}
867
0974a682
KS
868static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
869 uint16_t slot;
870 int r;
871
872 if (!is_efi_boot())
873 return 0;
874
875 r = find_slot(uuid, path, &slot);
876 if (r != 1)
877 return 0;
878
879 r = efi_remove_boot_option(slot);
880 if (r < 0)
881 return r;
882
883 if (in_order)
d3226d77 884 return remove_from_order(slot);
f939cff7
LP
885
886 return 0;
0974a682
KS
887}
888
341890de 889static int install_loader_config(const char *esp_path, sd_id128_t machine_id) {
d5ff6d6d 890 char machine_string[SD_ID128_STRING_MAX];
f5b84de2
LP
891 _cleanup_(unlink_and_freep) char *t = NULL;
892 _cleanup_fclose_ FILE *f = NULL;
d5ff6d6d 893 const char *p;
f5b84de2 894 int r, fd;
0974a682 895
d5ff6d6d 896 p = strjoina(esp_path, "/loader/loader.conf");
f5b84de2
LP
897 if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
898 return 0;
899
900 fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
901 if (fd < 0)
902 return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p);
903
e92aaed3 904 f = fdopen(fd, "w");
f5b84de2
LP
905 if (!f) {
906 safe_close(fd);
907 return log_oom();
908 }
0974a682 909
a36b411e
LP
910 fprintf(f, "#timeout 3\n"
911 "#console-mode keep\n"
912 "default %s-*\n", sd_id128_to_string(machine_id, machine_string));
0974a682 913
0675e94a 914 r = fflush_sync_and_check(f);
d5ff6d6d
LP
915 if (r < 0)
916 return log_error_errno(r, "Failed to write \"%s\": %m", p);
0974a682 917
f5b84de2
LP
918 r = link_tmpfile(fd, t, p);
919 if (r == -EEXIST)
920 return 0; /* Silently skip creation if the file exists now (recheck) */
921 if (r < 0)
922 return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
923
924 t = mfree(t);
f5b84de2 925 return 1;
0974a682
KS
926}
927
341890de
ZJS
928static int install_entries_directories(const char *dollar_boot_path, sd_id128_t machine_id) {
929 int r;
930 char buf[SD_ID128_STRING_MAX];
931
fbf45d22
LP
932 assert(dollar_boot_path);
933
341890de
ZJS
934 /* Both /loader/entries and the entry directories themselves should be located on the same
935 * partition. Also create the parent directory for entry directories, so that kernel-install
936 * knows where to put them. */
937
e2600fd5 938 r = mkdir_one(dollar_boot_path, "loader/entries");
341890de
ZJS
939 if (r < 0)
940 return r;
941
942 return mkdir_one(dollar_boot_path, sd_id128_to_string(machine_id, buf));
fbf45d22
LP
943}
944
2f2c539c 945static int help(int argc, char *argv[], void *userdata) {
37ec0fdd
LP
946 _cleanup_free_ char *link = NULL;
947 int r;
948
949 r = terminal_urlify_man("bootctl", "1", &link);
950 if (r < 0)
951 return log_oom();
2f2c539c 952
37ec0fdd 953 printf("%s [COMMAND] [OPTIONS...]\n\n"
68cc17f1 954 "Install, update or remove the systemd-boot EFI boot manager.\n\n"
fbf45d22
LP
955 " -h --help Show this help\n"
956 " --version Print version\n"
957 " --esp-path=PATH Path to the EFI System Partition (ESP)\n"
958 " --boot-path=PATH Path to the $BOOT partition\n"
959 " -p --print-esp-path Print path to the EFI System Partition\n"
960 " --print-boot-path Print path to the $BOOT partition\n"
961 " --no-variables Don't touch EFI variables\n"
962 " --no-pager Do not pipe output into a pager\n"
d88c96ff 963 "\nBoot Loader Commands:\n"
fbf45d22
LP
964 " status Show status of installed systemd-boot and EFI variables\n"
965 " install Install systemd-boot to the ESP and EFI variables\n"
966 " update Update systemd-boot in the ESP and EFI variables\n"
967 " remove Remove systemd-boot from the ESP and EFI variables\n"
d88c96ff 968 "\nBoot Loader Entries Commands:\n"
fbf45d22
LP
969 " list List boot loader entries\n"
970 " set-default ID Set default boot loader entry\n"
971 " set-oneshot ID Set default boot loader entry, for next boot only\n"
37ec0fdd
LP
972 "\nSee the %s for details.\n"
973 , program_invocation_short_name
d88c96ff 974 , link);
0974a682
KS
975
976 return 0;
977}
978
0974a682
KS
979static int parse_argv(int argc, char *argv[]) {
980 enum {
fbf45d22
LP
981 ARG_ESP_PATH = 0x100,
982 ARG_BOOT_PATH,
983 ARG_PRINT_BOOT_PATH,
0974a682
KS
984 ARG_VERSION,
985 ARG_NO_VARIABLES,
57db6f18 986 ARG_NO_PAGER,
7b4d7cc0
KS
987 };
988
0974a682 989 static const struct option options[] = {
fbf45d22
LP
990 { "help", no_argument, NULL, 'h' },
991 { "version", no_argument, NULL, ARG_VERSION },
992 { "esp-path", required_argument, NULL, ARG_ESP_PATH },
993 { "path", required_argument, NULL, ARG_ESP_PATH }, /* Compatibility alias */
994 { "boot-path", required_argument, NULL, ARG_BOOT_PATH },
995 { "print-esp-path", no_argument, NULL, 'p' },
996 { "print-path", no_argument, NULL, 'p' }, /* Compatibility alias */
997 { "print-boot-path", no_argument, NULL, ARG_PRINT_BOOT_PATH },
998 { "no-variables", no_argument, NULL, ARG_NO_VARIABLES },
999 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
a36b411e 1000 {}
0974a682
KS
1001 };
1002
2f2c539c 1003 int c, r;
7b4d7cc0
KS
1004
1005 assert(argc >= 0);
1006 assert(argv);
1007
30b50477 1008 while ((c = getopt_long(argc, argv, "hp", options, NULL)) >= 0)
0974a682 1009 switch (c) {
7b4d7cc0 1010
0974a682 1011 case 'h':
2f2c539c 1012 help(0, NULL, NULL);
7b4d7cc0 1013 return 0;
7b4d7cc0 1014
0974a682 1015 case ARG_VERSION:
3f6fd1ba 1016 return version();
7b4d7cc0 1017
fbf45d22
LP
1018 case ARG_ESP_PATH:
1019 r = free_and_strdup(&arg_esp_path, optarg);
1020 if (r < 0)
1021 return log_oom();
1022 break;
1023
1024 case ARG_BOOT_PATH:
1025 r = free_and_strdup(&arg_xbootldr_path, optarg);
2f2c539c
LP
1026 if (r < 0)
1027 return log_oom();
0974a682
KS
1028 break;
1029
30b50477 1030 case 'p':
fbf45d22
LP
1031 arg_print_esp_path = true;
1032 break;
1033
1034 case ARG_PRINT_BOOT_PATH:
1035 arg_print_dollar_boot_path = true;
30b50477
ZJS
1036 break;
1037
0974a682
KS
1038 case ARG_NO_VARIABLES:
1039 arg_touch_variables = false;
1040 break;
1041
57db6f18 1042 case ARG_NO_PAGER:
0221d68a 1043 arg_pager_flags |= PAGER_DISABLE;
57db6f18
LP
1044 break;
1045
0974a682
KS
1046 case '?':
1047 return -EINVAL;
1048
1049 default:
d3226d77 1050 assert_not_reached("Unknown option");
7b4d7cc0 1051 }
7b4d7cc0 1052
0974a682
KS
1053 return 1;
1054}
7b4d7cc0 1055
551710cf
ZJS
1056static void read_loader_efi_var(const char *name, char **var) {
1057 int r;
1058
1059 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var);
1060 if (r < 0 && r != -ENOENT)
1061 log_warning_errno(r, "Failed to read EFI variable %s: %m", name);
1062}
1063
2f2c539c 1064static int verb_status(int argc, char *argv[], void *userdata) {
fbf45d22 1065 sd_id128_t esp_uuid = SD_ID128_NULL, xbootldr_uuid = SD_ID128_NULL;
46fb255b 1066 int r, k;
0974a682 1067
fbf45d22
LP
1068 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &esp_uuid);
1069 if (arg_print_esp_path) {
5caa3167
LP
1070 if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only
1071 * error the find_esp_and_warn() won't log on its own) */
fbf45d22 1072 return log_error_errno(r, "Failed to determine ESP location: %m");
30b50477
ZJS
1073 if (r < 0)
1074 return r;
1075
fbf45d22
LP
1076 puts(arg_esp_path);
1077 }
1078
1079 r = acquire_xbootldr(geteuid() != 0, &xbootldr_uuid);
1080 if (arg_print_dollar_boot_path) {
1081 if (r == -EACCES)
1082 return log_error_errno(r, "Failed to determine XBOOTLDR location: %m");
1083 if (r < 0)
1084 return r;
1085
1086 puts(arg_dollar_boot_path());
30b50477 1087 }
551710cf 1088
fbf45d22
LP
1089 if (arg_print_esp_path || arg_print_dollar_boot_path)
1090 return 0;
1091
5caa3167
LP
1092 r = 0; /* If we couldn't determine the path, then don't consider that a problem from here on, just show what we
1093 * can show */
1094
0221d68a 1095 (void) pager_open(arg_pager_flags);
57db6f18 1096
2f2c539c 1097 if (is_efi_boot()) {
80641a81
LP
1098 static const struct {
1099 uint64_t flag;
1100 const char *name;
1101 } flags[] = {
1102 { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" },
1103 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" },
1104 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" },
1105 { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" },
1106 { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" },
1107 };
1108
81375b9b 1109 _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL;
25579a43 1110 sd_id128_t loader_part_uuid = SD_ID128_NULL;
80641a81
LP
1111 uint64_t loader_features = 0;
1112 size_t i;
0974a682 1113
2f2c539c
LP
1114 read_loader_efi_var("LoaderFirmwareType", &fw_type);
1115 read_loader_efi_var("LoaderFirmwareInfo", &fw_info);
1116 read_loader_efi_var("LoaderInfo", &loader);
81375b9b 1117 read_loader_efi_var("StubInfo", &stub);
2f2c539c 1118 read_loader_efi_var("LoaderImageIdentifier", &loader_path);
80641a81 1119 (void) efi_loader_get_features(&loader_features);
551710cf 1120
2f2c539c
LP
1121 if (loader_path)
1122 efi_tilt_backslashes(loader_path);
1123
46fb255b
ZJS
1124 k = efi_loader_get_device_part_uuid(&loader_part_uuid);
1125 if (k < 0 && k != -ENOENT)
1126 r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m");
2f2c539c
LP
1127
1128 printf("System:\n");
ba857253 1129 printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal());
33987ba0
YW
1130 printf(" Secure Boot: %sd\n", enable_disable(is_efi_secure_boot()));
1131 printf(" Setup Mode: %s\n", is_efi_secure_boot_setup_mode() ? "setup" : "user");
2f2c539c
LP
1132 printf("\n");
1133
7fd66f3c 1134 printf("Current Boot Loader:\n");
ba857253 1135 printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal());
80641a81
LP
1136
1137 for (i = 0; i < ELEMENTSOF(flags); i++) {
1138
1139 if (i == 0)
1140 printf(" Features: ");
1141 else
1142 printf(" ");
1143
1144 if (FLAGS_SET(loader_features, flags[i].flag))
9a6f746f 1145 printf("%s%s%s %s\n", ansi_highlight_green(), special_glyph(SPECIAL_GLYPH_CHECK_MARK), ansi_normal(), flags[i].name);
80641a81 1146 else
9a6f746f 1147 printf("%s%s%s %s\n", ansi_highlight_red(), special_glyph(SPECIAL_GLYPH_CROSS_MARK), ansi_normal(), flags[i].name);
80641a81
LP
1148 }
1149
81375b9b
ДГ
1150 if (stub)
1151 printf(" Stub: %s\n", stub);
e28973ee 1152 if (!sd_id128_is_null(loader_part_uuid))
cd2d4c7f 1153 printf(" ESP: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
2f2c539c
LP
1154 SD_ID128_FORMAT_VAL(loader_part_uuid));
1155 else
cd2d4c7f 1156 printf(" ESP: n/a\n");
9a6f746f 1157 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), strna(loader_path));
2f2c539c
LP
1158 printf("\n");
1159 } else
cd2d4c7f 1160 printf("System:\n Not booted with EFI\n\n");
7b4d7cc0 1161
fbf45d22
LP
1162 if (arg_esp_path) {
1163 k = status_binaries(arg_esp_path, esp_uuid);
ecec2a5d
LP
1164 if (k < 0)
1165 r = k;
1166 }
2f2c539c 1167
cd2d4c7f 1168 if (is_efi_boot()) {
46fb255b
ZJS
1169 k = status_variables();
1170 if (k < 0)
1171 r = k;
cd2d4c7f 1172 }
0974a682 1173
fbf45d22 1174 if (arg_esp_path || arg_xbootldr_path) {
81fed855 1175 k = status_entries(arg_esp_path, esp_uuid, arg_xbootldr_path, xbootldr_uuid);
ecec2a5d
LP
1176 if (k < 0)
1177 r = k;
1178 }
7e87c7d9 1179
46fb255b 1180 return r;
2f2c539c 1181}
7b4d7cc0 1182
7e87c7d9 1183static int verb_list(int argc, char *argv[], void *userdata) {
5caa3167 1184 _cleanup_(boot_config_free) BootConfig config = {};
5caa3167 1185 int r;
7e87c7d9 1186
5caa3167
LP
1187 /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two things: turn
1188 * off logging about access errors and turn off potentially privileged device probing. Here we're interested in
1189 * the latter but not the former, hence request the mode, and log about EACCES. */
7e87c7d9 1190
fbf45d22 1191 r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, NULL);
5caa3167
LP
1192 if (r == -EACCES) /* We really need the ESP path for this call, hence also log about access errors */
1193 return log_error_errno(r, "Failed to determine ESP: %m");
7e87c7d9
ZJS
1194 if (r < 0)
1195 return r;
1196
fbf45d22
LP
1197 r = acquire_xbootldr(geteuid() != 0, NULL);
1198 if (r == -EACCES)
1199 return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
1200 if (r < 0)
1201 return r;
1202
1203 r = boot_entries_load_config(arg_esp_path, arg_xbootldr_path, &config);
7e87c7d9 1204 if (r < 0)
21f7a622 1205 return r;
7e87c7d9 1206
93f14ce2 1207 (void) boot_entries_augment_from_loader(&config, false);
bd2865ca 1208
20a28174
LP
1209 if (config.n_entries == 0)
1210 log_info("No boot loader entries found.");
1211 else {
1212 size_t n;
7e87c7d9 1213
0221d68a 1214 (void) pager_open(arg_pager_flags);
57db6f18 1215
20a28174 1216 printf("Boot Loader Entries:\n");
7e87c7d9 1217
20a28174
LP
1218 for (n = 0; n < config.n_entries; n++) {
1219 r = boot_entry_show(config.entries + n, n == (size_t) config.default_entry);
1220 if (r < 0)
1221 return r;
7e87c7d9 1222
4629499e
LP
1223 if (n+1 < config.n_entries)
1224 putchar('\n');
7e87c7d9 1225 }
cd2d4c7f 1226 }
0974a682 1227
7e87c7d9 1228 return 0;
2f2c539c 1229}
7b4d7cc0 1230
fbf45d22
LP
1231static int sync_everything(void) {
1232 int ret = 0, k;
e0e8d177 1233
fbf45d22
LP
1234 if (arg_esp_path) {
1235 k = syncfs_path(AT_FDCWD, arg_esp_path);
1236 if (k < 0)
1237 ret = log_error_errno(k, "Failed to synchronize the ESP '%s': %m", arg_esp_path);
1238 }
e0e8d177 1239
fbf45d22
LP
1240 if (arg_xbootldr_path) {
1241 k = syncfs_path(AT_FDCWD, arg_xbootldr_path);
1242 if (k < 0)
1243 ret = log_error_errno(k, "Failed to synchronize $BOOT '%s': %m", arg_xbootldr_path);
1244 }
e0e8d177 1245
fbf45d22 1246 return ret;
e0e8d177
LP
1247}
1248
2f2c539c 1249static int verb_install(int argc, char *argv[], void *userdata) {
2f2c539c
LP
1250 sd_id128_t uuid = SD_ID128_NULL;
1251 uint64_t pstart = 0, psize = 0;
1252 uint32_t part = 0;
341890de 1253 sd_id128_t machine_id;
2f2c539c
LP
1254 bool install;
1255 int r;
1256
5caa3167 1257 r = acquire_esp(false, &part, &pstart, &psize, &uuid);
2f2c539c
LP
1258 if (r < 0)
1259 return r;
1260
fbf45d22
LP
1261 r = acquire_xbootldr(false, NULL);
1262 if (r < 0)
1263 return r;
1264
341890de
ZJS
1265 r = sd_id128_get_machine(&machine_id);
1266 if (r < 0)
1267 return log_error_errno(r, "Failed to get machine id: %m");
1268
2f2c539c
LP
1269 install = streq(argv[0], "install");
1270
1271 RUN_WITH_UMASK(0002) {
fbf45d22
LP
1272 if (install) {
1273 /* Don't create any of these directories when we are just updating. When we update
1274 * we'll drop-in our files (unless there are newer ones already), but we won't create
1275 * the directories for them in the first place. */
1276 r = create_esp_subdirs(arg_esp_path);
1277 if (r < 0)
1278 return r;
1279 }
1280
1281 r = install_binaries(arg_esp_path, install);
0974a682 1282 if (r < 0)
d3226d77 1283 return r;
0974a682 1284
2f2c539c 1285 if (install) {
341890de 1286 r = install_loader_config(arg_esp_path, machine_id);
fbf45d22
LP
1287 if (r < 0)
1288 return r;
1289
341890de 1290 r = install_entries_directories(arg_dollar_boot_path(), machine_id);
d3226d77
ZJS
1291 if (r < 0)
1292 return r;
1293 }
2f2c539c 1294 }
0974a682 1295
fbf45d22 1296 (void) sync_everything();
e0e8d177 1297
2f2c539c 1298 if (arg_touch_variables)
fbf45d22 1299 r = install_variables(arg_esp_path,
2f2c539c
LP
1300 part, pstart, psize, uuid,
1301 "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi",
1302 install);
7b4d7cc0 1303
2f2c539c
LP
1304 return r;
1305}
0974a682 1306
2f2c539c
LP
1307static int verb_remove(int argc, char *argv[], void *userdata) {
1308 sd_id128_t uuid = SD_ID128_NULL;
fbf45d22 1309 int r, q;
0974a682 1310
5caa3167 1311 r = acquire_esp(false, NULL, NULL, NULL, &uuid);
2f2c539c
LP
1312 if (r < 0)
1313 return r;
1314
fbf45d22
LP
1315 r = acquire_xbootldr(false, NULL);
1316 if (r < 0)
1317 return r;
2f2c539c 1318
fbf45d22 1319 r = remove_binaries(arg_esp_path);
e0e8d177 1320
fbf45d22
LP
1321 q = remove_loader_config(arg_esp_path);
1322 if (q < 0 && r >= 0)
1323 r = q;
1324
1325 q = remove_entries_directory(arg_dollar_boot_path());
1326 if (q < 0 && r >= 0)
1327 r = q;
2f2c539c 1328
fbf45d22
LP
1329 q = remove_esp_subdirs(arg_esp_path);
1330 if (q < 0 && r >= 0)
1331 r = q;
1332
1333 (void) sync_everything();
1334
1335 if (arg_touch_variables) {
2f2c539c 1336 q = remove_variables(uuid, "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi", true);
fbf45d22 1337 if (q < 0 && r >= 0)
2f2c539c 1338 r = q;
7b4d7cc0
KS
1339 }
1340
d3226d77 1341 return r;
7b4d7cc0
KS
1342}
1343
d88c96ff
LP
1344static int verb_set_default(int argc, char *argv[], void *userdata) {
1345 const char *name;
1346 int r;
1347
baaa35ad
ZJS
1348 if (!is_efi_boot())
1349 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1350 "Not booted with UEFI.");
d88c96ff
LP
1351
1352 if (access("/sys/firmware/efi/efivars/LoaderInfo-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) < 0) {
1353 if (errno == ENOENT) {
1354 log_error_errno(errno, "Not booted with a supported boot loader.");
1355 return -EOPNOTSUPP;
1356 }
1357
1358 return log_error_errno(errno, "Failed to detect whether boot loader supports '%s' operation: %m", argv[0]);
1359 }
1360
baaa35ad
ZJS
1361 if (detect_container() > 0)
1362 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1363 "'%s' operation not supported in a container.",
1364 argv[0]);
d88c96ff 1365
baaa35ad
ZJS
1366 if (!arg_touch_variables)
1367 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1368 "'%s' operation cannot be combined with --touch-variables=no.",
1369 argv[0]);
d88c96ff
LP
1370
1371 name = streq(argv[0], "set-default") ? "LoaderEntryDefault" : "LoaderEntryOneShot";
1372
1373 if (isempty(argv[1])) {
1374 r = efi_set_variable(EFI_VENDOR_LOADER, name, NULL, 0);
1375 if (r < 0 && r != -ENOENT)
1376 return log_error_errno(r, "Failed to remove EFI variale: %m");
1377 } else {
1378 _cleanup_free_ char16_t *encoded = NULL;
1379
1380 encoded = utf8_to_utf16(argv[1], strlen(argv[1]));
1381 if (!encoded)
1382 return log_oom();
1383
1384 r = efi_set_variable(EFI_VENDOR_LOADER, name, encoded, char16_strlen(encoded) * 2 + 2);
1385 if (r < 0)
1386 return log_error_errno(r, "Failed to update EFI variable: %m");
1387 }
1388
1389 return 0;
1390}
1391
2f2c539c
LP
1392static int bootctl_main(int argc, char *argv[]) {
1393
1394 static const Verb verbs[] = {
d88c96ff
LP
1395 { "help", VERB_ANY, VERB_ANY, 0, help },
1396 { "status", VERB_ANY, 1, VERB_DEFAULT, verb_status },
1397 { "install", VERB_ANY, 1, VERB_MUST_BE_ROOT, verb_install },
1398 { "update", VERB_ANY, 1, VERB_MUST_BE_ROOT, verb_install },
1399 { "remove", VERB_ANY, 1, VERB_MUST_BE_ROOT, verb_remove },
1400 { "list", VERB_ANY, 1, 0, verb_list },
1401 { "set-default", 2, 2, VERB_MUST_BE_ROOT, verb_set_default },
1402 { "set-oneshot", 2, 2, VERB_MUST_BE_ROOT, verb_set_default },
2f2c539c
LP
1403 {}
1404 };
1405
1406 return dispatch_verb(argc, argv, verbs, NULL);
1407}
1408
608f8ec9 1409static int run(int argc, char *argv[]) {
601185b4 1410 int r;
7b4d7cc0
KS
1411
1412 log_parse_environment();
1413 log_open();
1414
341890de 1415 /* If we run in a container, automatically turn off EFI file system access */
2f2c539c
LP
1416 if (detect_container() > 0)
1417 arg_touch_variables = false;
1418
7b4d7cc0 1419 r = parse_argv(argc, argv);
601185b4 1420 if (r <= 0)
608f8ec9 1421 return r;
57db6f18 1422
608f8ec9 1423 return bootctl_main(argc, argv);
7b4d7cc0 1424}
608f8ec9
YW
1425
1426DEFINE_MAIN_FUNCTION(run);