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