]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/bootctl-status.c
Merge pull request #30513 from rpigott/resolved-ede
[thirdparty/systemd.git] / src / boot / bootctl-status.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/mman.h>
4 #include <unistd.h>
5
6 #include "bootctl.h"
7 #include "bootctl-status.h"
8 #include "bootctl-util.h"
9 #include "bootspec.h"
10 #include "chase.h"
11 #include "devnum-util.h"
12 #include "dirent-util.h"
13 #include "efi-api.h"
14 #include "efi-loader.h"
15 #include "errno-util.h"
16 #include "fd-util.h"
17 #include "fileio.h"
18 #include "find-esp.h"
19 #include "path-util.h"
20 #include "pretty-print.h"
21 #include "recurse-dir.h"
22 #include "terminal-util.h"
23 #include "tpm2-util.h"
24
25 static int boot_config_load_and_select(
26 BootConfig *config,
27 const char *esp_path,
28 dev_t esp_devid,
29 const char *xbootldr_path,
30 dev_t xbootldr_devid) {
31
32 int r;
33
34 /* If XBOOTLDR and ESP actually refer to the same block device, suppress XBOOTLDR, since it would
35 * find the same entries twice. */
36 bool same = esp_path && xbootldr_path && devnum_set_and_equal(esp_devid, xbootldr_devid);
37
38 r = boot_config_load(config, esp_path, same ? NULL : xbootldr_path);
39 if (r < 0)
40 return r;
41
42 if (!arg_root) {
43 _cleanup_strv_free_ char **efi_entries = NULL;
44
45 r = efi_loader_get_entries(&efi_entries);
46 if (r == -ENOENT || ERRNO_IS_NEG_NOT_SUPPORTED(r))
47 log_debug_errno(r, "Boot loader reported no entries.");
48 else if (r < 0)
49 log_warning_errno(r, "Failed to determine entries reported by boot loader, ignoring: %m");
50 else
51 (void) boot_config_augment_from_loader(config, efi_entries, /* only_auto= */ false);
52 }
53
54 return boot_config_select_special_entries(config, /* skip_efivars= */ !!arg_root);
55 }
56
57 static int status_entries(
58 const BootConfig *config,
59 const char *esp_path,
60 sd_id128_t esp_partition_uuid,
61 const char *xbootldr_path,
62 sd_id128_t xbootldr_partition_uuid) {
63
64 sd_id128_t dollar_boot_partition_uuid;
65 const char *dollar_boot_path;
66 int r;
67
68 assert(config);
69 assert(esp_path || xbootldr_path);
70
71 if (xbootldr_path) {
72 dollar_boot_path = xbootldr_path;
73 dollar_boot_partition_uuid = xbootldr_partition_uuid;
74 } else {
75 dollar_boot_path = esp_path;
76 dollar_boot_partition_uuid = esp_partition_uuid;
77 }
78
79 printf("%sBoot Loader Entries:%s\n"
80 " $BOOT: %s", ansi_underline(), ansi_normal(), dollar_boot_path);
81 if (!sd_id128_is_null(dollar_boot_partition_uuid))
82 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")",
83 SD_ID128_FORMAT_VAL(dollar_boot_partition_uuid));
84 if (settle_entry_token() >= 0)
85 printf("\n token: %s", arg_entry_token);
86 printf("\n\n");
87
88 if (config->default_entry < 0)
89 printf("%zu entries, no entry could be determined as default.\n", config->n_entries);
90 else {
91 printf("%sDefault Boot Loader Entry:%s\n", ansi_underline(), ansi_normal());
92
93 r = show_boot_entry(
94 boot_config_default_entry(config),
95 /* show_as_default= */ false,
96 /* show_as_selected= */ false,
97 /* show_discovered= */ false);
98 if (r > 0)
99 /* < 0 is already logged by the function itself, let's just emit an extra warning if
100 the default entry is broken */
101 printf("\nWARNING: default boot entry is broken\n");
102 }
103
104 return 0;
105 }
106
107 static int print_efi_option(uint16_t id, int *n_printed, bool in_order) {
108 _cleanup_free_ char *title = NULL;
109 _cleanup_free_ char *path = NULL;
110 sd_id128_t partition;
111 bool active;
112 int r;
113
114 assert(n_printed);
115
116 r = efi_get_boot_option(id, &title, &partition, &path, &active);
117 if (r == -ENOENT) {
118 log_debug_errno(r, "Boot option 0x%04X referenced but missing, ignoring: %m", id);
119 return 0;
120 }
121 if (r < 0)
122 return log_error_errno(r, "Failed to read boot option 0x%04X: %m", id);
123
124 /* print only configured entries with partition information */
125 if (!path || sd_id128_is_null(partition)) {
126 log_debug("Ignoring boot entry 0x%04X without partition information.", id);
127 return 0;
128 }
129
130 efi_tilt_backslashes(path);
131
132 if (*n_printed == 0) /* Print section title before first entry */
133 printf("%sBoot Loaders Listed in EFI Variables:%s\n", ansi_underline(), ansi_normal());
134
135 printf(" Title: %s%s%s\n", ansi_highlight(), strna(title), ansi_normal());
136 printf(" ID: 0x%04X\n", id);
137 printf(" Status: %sactive%s\n", active ? "" : "in", in_order ? ", boot-order" : "");
138 printf(" Partition: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
139 SD_ID128_FORMAT_VAL(partition));
140 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path);
141 printf("\n");
142
143 (*n_printed)++;
144 return 1;
145 }
146
147 static int status_variables(void) {
148 _cleanup_free_ uint16_t *options = NULL, *order = NULL;
149 int n_options, n_order, n_printed = 0;
150
151 n_options = efi_get_boot_options(&options);
152 if (n_options == -ENOENT)
153 return log_error_errno(n_options,
154 "Failed to access EFI variables, efivarfs"
155 " needs to be available at /sys/firmware/efi/efivars/.");
156 if (n_options < 0)
157 return log_error_errno(n_options, "Failed to read EFI boot entries: %m");
158
159 n_order = efi_get_boot_order(&order);
160 if (n_order == -ENOENT)
161 n_order = 0;
162 else if (n_order < 0)
163 return log_error_errno(n_order, "Failed to read EFI boot order: %m");
164
165 /* print entries in BootOrder first */
166 for (int i = 0; i < n_order; i++)
167 (void) print_efi_option(order[i], &n_printed, /* in_order= */ true);
168
169 /* print remaining entries */
170 for (int i = 0; i < n_options; i++) {
171 for (int j = 0; j < n_order; j++)
172 if (options[i] == order[j])
173 goto next_option;
174
175 (void) print_efi_option(options[i], &n_printed, /* in_order= */ false);
176
177 next_option:
178 continue;
179 }
180
181 if (n_printed == 0)
182 printf("No boot loaders listed in EFI Variables.\n\n");
183
184 return 0;
185 }
186
187 static int enumerate_binaries(
188 const char *esp_path,
189 const char *path,
190 char **previous,
191 bool *is_first) {
192
193 _cleanup_closedir_ DIR *d = NULL;
194 _cleanup_free_ char *p = NULL;
195 int c = 0, r;
196
197 assert(esp_path);
198 assert(path);
199 assert(previous);
200 assert(is_first);
201
202 r = chase_and_opendir(path, esp_path, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, &p, &d);
203 if (r == -ENOENT)
204 return 0;
205 if (r < 0)
206 return log_error_errno(r, "Failed to read \"%s/%s\": %m", esp_path, path);
207
208 FOREACH_DIRENT(de, d, break) {
209 _cleanup_free_ char *v = NULL, *filename = NULL;
210 _cleanup_close_ int fd = -EBADF;
211
212 if (!endswith_no_case(de->d_name, ".efi"))
213 continue;
214
215 filename = path_join(p, de->d_name);
216 if (!filename)
217 return log_oom();
218 LOG_SET_PREFIX(filename);
219
220 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC);
221 if (fd < 0)
222 return log_error_errno(errno, "Failed to open file for reading: %m");
223
224 r = get_file_version(fd, &v);
225
226 if (r < 0 && r != -ESRCH)
227 return r;
228
229 if (*previous) { /* Let's output the previous entry now, since now we know that there will be
230 * one more, and can draw the tree glyph properly. */
231 printf(" %s %s%s\n",
232 *is_first ? "File:" : " ",
233 special_glyph(SPECIAL_GLYPH_TREE_BRANCH), *previous);
234 *is_first = false;
235 *previous = mfree(*previous);
236 }
237
238 /* Do not output this entry immediately, but store what should be printed in a state
239 * variable, because we only will know the tree glyph to print (branch or final edge) once we
240 * read one more entry */
241 if (r == -ESRCH) /* No systemd-owned file but still interesting to print */
242 r = asprintf(previous, "/%s/%s", path, de->d_name);
243 else /* if (r >= 0) */
244 r = asprintf(previous, "/%s/%s (%s%s%s)", path, de->d_name, ansi_highlight(), v, ansi_normal());
245 if (r < 0)
246 return log_oom();
247
248 c++;
249 }
250
251 return c;
252 }
253
254 static int status_binaries(const char *esp_path, sd_id128_t partition) {
255 _cleanup_free_ char *last = NULL;
256 bool is_first = true;
257 int r, k;
258
259 printf("%sAvailable Boot Loaders on ESP:%s\n", ansi_underline(), ansi_normal());
260
261 if (!esp_path) {
262 printf(" ESP: Cannot find or access mount point of ESP.\n\n");
263 return -ENOENT;
264 }
265
266 printf(" ESP: %s", esp_path);
267 if (!sd_id128_is_null(partition))
268 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")", SD_ID128_FORMAT_VAL(partition));
269 printf("\n");
270
271 r = enumerate_binaries(esp_path, "EFI/systemd", &last, &is_first);
272 if (r < 0)
273 goto fail;
274
275 k = enumerate_binaries(esp_path, "EFI/BOOT", &last, &is_first);
276 if (k < 0) {
277 r = k;
278 goto fail;
279 }
280
281 if (last) /* let's output the last entry now, since now we know that there will be no more, and can draw the tree glyph properly */
282 printf(" %s %s%s\n",
283 is_first ? "File:" : " ",
284 special_glyph(SPECIAL_GLYPH_TREE_RIGHT), last);
285
286 if (r == 0 && !arg_quiet)
287 log_info("systemd-boot not installed in ESP.");
288 if (k == 0 && !arg_quiet)
289 log_info("No default/fallback boot loader installed in ESP.");
290
291 printf("\n");
292 return 0;
293
294 fail:
295 errno = -r;
296 printf(" File: (can't access %s: %m)\n\n", esp_path);
297 return r;
298 }
299
300 static void read_efi_var(const char *variable, char **ret) {
301 int r;
302
303 r = efi_get_variable_string(variable, ret);
304 if (r < 0 && r != -ENOENT)
305 log_warning_errno(r, "Failed to read EFI variable %s: %m", variable);
306 }
307
308 static void print_yes_no_line(bool first, bool good, const char *name) {
309 printf("%s%s %s\n",
310 first ? " Features: " : " ",
311 COLOR_MARK_BOOL(good),
312 name);
313 }
314
315 int verb_status(int argc, char *argv[], void *userdata) {
316 sd_id128_t esp_uuid = SD_ID128_NULL, xbootldr_uuid = SD_ID128_NULL;
317 dev_t esp_devid = 0, xbootldr_devid = 0;
318 int r, k;
319
320 r = acquire_esp(/* unprivileged_mode= */ -1, /* graceful= */ false, NULL, NULL, NULL, &esp_uuid, &esp_devid);
321 if (arg_print_esp_path) {
322 if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only
323 * error the find_esp_and_warn() won't log on its own) */
324 return log_error_errno(r, "Failed to determine ESP location: %m");
325 if (r < 0)
326 return r;
327
328 puts(arg_esp_path);
329 }
330
331 r = acquire_xbootldr(/* unprivileged_mode= */ -1, &xbootldr_uuid, &xbootldr_devid);
332 if (arg_print_dollar_boot_path) {
333 if (r == -EACCES)
334 return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
335 if (r < 0)
336 return r;
337
338 const char *path = arg_dollar_boot_path();
339 if (!path)
340 return log_error_errno(SYNTHETIC_ERRNO(EACCES), "Failed to determine XBOOTLDR location: %m");
341
342 puts(path);
343 }
344
345 if (arg_print_esp_path || arg_print_dollar_boot_path)
346 return 0;
347
348 r = 0; /* If we couldn't determine the path, then don't consider that a problem from here on, just
349 * show what we can show */
350
351 pager_open(arg_pager_flags);
352
353 if (!arg_root && is_efi_boot()) {
354 static const struct {
355 uint64_t flag;
356 const char *name;
357 } loader_flags[] = {
358 { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" },
359 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" },
360 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" },
361 { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" },
362 { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" },
363 { EFI_LOADER_FEATURE_XBOOTLDR, "Support for XBOOTLDR partition" },
364 { EFI_LOADER_FEATURE_RANDOM_SEED, "Support for passing random seed to OS" },
365 { EFI_LOADER_FEATURE_LOAD_DRIVER, "Load drop-in drivers" },
366 { EFI_LOADER_FEATURE_SORT_KEY, "Support Type #1 sort-key field" },
367 { EFI_LOADER_FEATURE_SAVED_ENTRY, "Support @saved pseudo-entry" },
368 { EFI_LOADER_FEATURE_DEVICETREE, "Support Type #1 devicetree field" },
369 { EFI_LOADER_FEATURE_SECUREBOOT_ENROLL, "Enroll SecureBoot keys" },
370 { EFI_LOADER_FEATURE_RETAIN_SHIM, "Retain SHIM protocols" },
371 { EFI_LOADER_FEATURE_MENU_DISABLE, "Menu can be disabled" },
372 };
373 static const struct {
374 uint64_t flag;
375 const char *name;
376 } stub_flags[] = {
377 { EFI_STUB_FEATURE_REPORT_BOOT_PARTITION, "Stub sets ESP information" },
378 { EFI_STUB_FEATURE_PICK_UP_CREDENTIALS, "Picks up credentials from boot partition" },
379 { EFI_STUB_FEATURE_PICK_UP_SYSEXTS, "Picks up system extension images from boot partition" },
380 { EFI_STUB_FEATURE_PICK_UP_CONFEXTS, "Picks up configuration extension images from boot partition" },
381 { EFI_STUB_FEATURE_THREE_PCRS, "Measures kernel+command line+sysexts" },
382 { EFI_STUB_FEATURE_RANDOM_SEED, "Support for passing random seed to OS" },
383 { EFI_STUB_FEATURE_CMDLINE_ADDONS, "Pick up .cmdline from addons" },
384 { EFI_STUB_FEATURE_CMDLINE_SMBIOS, "Pick up .cmdline from SMBIOS Type 11" },
385 { EFI_STUB_FEATURE_DEVICETREE_ADDONS, "Pick up .dtb from addons" },
386 };
387 _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL;
388 sd_id128_t loader_part_uuid = SD_ID128_NULL;
389 uint64_t loader_features = 0, stub_features = 0;
390 Tpm2Support s;
391 int have;
392
393 read_efi_var(EFI_LOADER_VARIABLE(LoaderFirmwareType), &fw_type);
394 read_efi_var(EFI_LOADER_VARIABLE(LoaderFirmwareInfo), &fw_info);
395 read_efi_var(EFI_LOADER_VARIABLE(LoaderInfo), &loader);
396 read_efi_var(EFI_LOADER_VARIABLE(StubInfo), &stub);
397 read_efi_var(EFI_LOADER_VARIABLE(LoaderImageIdentifier), &loader_path);
398 (void) efi_loader_get_features(&loader_features);
399 (void) efi_stub_get_features(&stub_features);
400
401 if (loader_path)
402 efi_tilt_backslashes(loader_path);
403
404 k = efi_loader_get_device_part_uuid(&loader_part_uuid);
405 if (k < 0 && k != -ENOENT)
406 r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m");
407
408 SecureBootMode secure = efi_get_secure_boot_mode();
409 printf("%sSystem:%s\n", ansi_underline(), ansi_normal());
410 printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal());
411 printf(" Firmware Arch: %s\n", get_efi_arch());
412 printf(" Secure Boot: %s%s%s",
413 IN_SET(secure, SECURE_BOOT_USER, SECURE_BOOT_DEPLOYED) ? ansi_highlight_green() : ansi_normal(),
414 enabled_disabled(IN_SET(secure, SECURE_BOOT_USER, SECURE_BOOT_DEPLOYED)),
415 ansi_normal());
416
417 if (secure != SECURE_BOOT_DISABLED)
418 printf(" (%s)\n", secure_boot_mode_to_string(secure));
419 else
420 printf("\n");
421
422 s = tpm2_support();
423 printf(" TPM2 Support: %s%s%s\n",
424 FLAGS_SET(s, TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER) ? ansi_highlight_green() :
425 (s & (TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER)) != 0 ? ansi_highlight_red() : ansi_highlight_yellow(),
426 FLAGS_SET(s, TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER) ? "yes" :
427 (s & TPM2_SUPPORT_FIRMWARE) ? "firmware only, driver unavailable" :
428 (s & TPM2_SUPPORT_DRIVER) ? "driver only, firmware unavailable" : "no",
429 ansi_normal());
430
431 k = efi_measured_uki(LOG_DEBUG);
432 if (k > 0)
433 printf(" Measured UKI: %syes%s\n", ansi_highlight_green(), ansi_normal());
434 else if (k == 0)
435 printf(" Measured UKI: no\n");
436 else {
437 errno = -k;
438 printf(" Measured UKI: %sfailed%s (%m)\n", ansi_highlight_red(), ansi_normal());
439 }
440
441 k = efi_get_reboot_to_firmware();
442 if (k > 0)
443 printf(" Boot into FW: %sactive%s\n", ansi_highlight_yellow(), ansi_normal());
444 else if (k == 0)
445 printf(" Boot into FW: supported\n");
446 else if (k == -EOPNOTSUPP)
447 printf(" Boot into FW: not supported\n");
448 else {
449 errno = -k;
450 printf(" Boot into FW: %sfailed%s (%m)\n", ansi_highlight_red(), ansi_normal());
451 }
452 printf("\n");
453
454 printf("%sCurrent Boot Loader:%s\n", ansi_underline(), ansi_normal());
455 printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal());
456
457 for (size_t i = 0; i < ELEMENTSOF(loader_flags); i++)
458 print_yes_no_line(i == 0, FLAGS_SET(loader_features, loader_flags[i].flag), loader_flags[i].name);
459
460 sd_id128_t bootloader_esp_uuid;
461 bool have_bootloader_esp_uuid = efi_loader_get_device_part_uuid(&bootloader_esp_uuid) >= 0;
462
463 print_yes_no_line(false, have_bootloader_esp_uuid, "Boot loader sets ESP information");
464 if (have_bootloader_esp_uuid && !sd_id128_is_null(esp_uuid) &&
465 !sd_id128_equal(esp_uuid, bootloader_esp_uuid))
466 printf("WARNING: The boot loader reports a different ESP UUID than detected ("SD_ID128_UUID_FORMAT_STR" vs. "SD_ID128_UUID_FORMAT_STR")!\n",
467 SD_ID128_FORMAT_VAL(bootloader_esp_uuid),
468 SD_ID128_FORMAT_VAL(esp_uuid));
469
470 if (stub) {
471 printf(" Stub: %s\n", stub);
472 for (size_t i = 0; i < ELEMENTSOF(stub_flags); i++)
473 print_yes_no_line(i == 0, FLAGS_SET(stub_features, stub_flags[i].flag), stub_flags[i].name);
474 }
475 if (!sd_id128_is_null(loader_part_uuid))
476 printf(" ESP: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
477 SD_ID128_FORMAT_VAL(loader_part_uuid));
478 else
479 printf(" ESP: n/a\n");
480 printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), strna(loader_path));
481 printf("\n");
482
483 printf("%sRandom Seed:%s\n", ansi_underline(), ansi_normal());
484 have = access(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderSystemToken)), F_OK) >= 0;
485 printf(" System Token: %s\n", have ? "set" : "not set");
486
487 if (arg_esp_path) {
488 _cleanup_free_ char *p = NULL;
489
490 p = path_join(arg_esp_path, "/loader/random-seed");
491 if (!p)
492 return log_oom();
493
494 have = access(p, F_OK) >= 0;
495 printf(" Exists: %s\n", yes_no(have));
496 }
497
498 printf("\n");
499 } else
500 printf("%sSystem:%s\n"
501 "Not booted with EFI\n\n",
502 ansi_underline(), ansi_normal());
503
504 if (arg_esp_path)
505 RET_GATHER(r, status_binaries(arg_esp_path, esp_uuid));
506
507 if (!arg_root && is_efi_boot())
508 RET_GATHER(r, status_variables());
509
510 if (arg_esp_path || arg_xbootldr_path) {
511 _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
512
513 k = boot_config_load_and_select(&config,
514 arg_esp_path, esp_devid,
515 arg_xbootldr_path, xbootldr_devid);
516 RET_GATHER(r, k);
517
518 if (k >= 0)
519 RET_GATHER(r,
520 status_entries(&config,
521 arg_esp_path, esp_uuid,
522 arg_xbootldr_path, xbootldr_uuid));
523 }
524
525 return r;
526 }
527
528 static int ref_file(Hashmap *known_files, const char *fn, int increment) {
529 char *k = NULL;
530 int n, r;
531
532 assert(known_files);
533
534 /* just gracefully ignore this. This way the caller doesn't
535 have to verify whether the bootloader entry is relevant */
536 if (!fn)
537 return 0;
538
539 n = PTR_TO_INT(hashmap_get2(known_files, fn, (void**)&k));
540 n += increment;
541
542 assert(n >= 0);
543
544 if (n == 0) {
545 (void) hashmap_remove(known_files, fn);
546 free(k);
547 } else if (!k) {
548 _cleanup_free_ char *t = NULL;
549
550 t = strdup(fn);
551 if (!t)
552 return -ENOMEM;
553 r = hashmap_put(known_files, t, INT_TO_PTR(n));
554 if (r < 0)
555 return r;
556 TAKE_PTR(t);
557 } else {
558 r = hashmap_update(known_files, fn, INT_TO_PTR(n));
559 if (r < 0)
560 return r;
561 }
562
563 return n;
564 }
565
566 static void deref_unlink_file(Hashmap *known_files, const char *fn, const char *root) {
567 _cleanup_free_ char *path = NULL;
568 int r;
569
570 assert(known_files);
571
572 /* just gracefully ignore this. This way the caller doesn't
573 have to verify whether the bootloader entry is relevant */
574 if (!fn || !root)
575 return;
576
577 r = ref_file(known_files, fn, -1);
578 if (r < 0)
579 return (void) log_warning_errno(r, "Failed to deref \"%s\", ignoring: %m", fn);
580 if (r > 0)
581 return;
582
583 if (arg_dry_run) {
584 r = chase_and_access(fn, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, F_OK, &path);
585 if (r < 0)
586 log_info_errno(r, "Unable to determine whether \"%s\" exists, ignoring: %m", fn);
587 else
588 log_info("Would remove \"%s\"", path);
589 return;
590 }
591
592 r = chase_and_unlink(fn, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, 0, &path);
593 if (r >= 0)
594 log_info("Removed \"%s\"", path);
595 else if (r != -ENOENT)
596 return (void) log_warning_errno(r, "Failed to remove \"%s\", ignoring: %m", fn);
597
598 _cleanup_free_ char *d = NULL;
599 if (path_extract_directory(fn, &d) >= 0 && !path_equal(d, "/")) {
600 r = chase_and_unlink(d, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, AT_REMOVEDIR, NULL);
601 if (r < 0 && !IN_SET(r, -ENOTEMPTY, -ENOENT))
602 log_warning_errno(r, "Failed to remove directory \"%s\", ignoring: %m", d);
603 }
604 }
605
606 static int count_known_files(const BootConfig *config, const char* root, Hashmap **ret_known_files) {
607 _cleanup_(hashmap_free_free_keyp) Hashmap *known_files = NULL;
608 int r;
609
610 assert(config);
611 assert(ret_known_files);
612
613 known_files = hashmap_new(&path_hash_ops);
614 if (!known_files)
615 return -ENOMEM;
616
617 for (size_t i = 0; i < config->n_entries; i++) {
618 const BootEntry *e = config->entries + i;
619
620 if (!path_equal(e->root, root))
621 continue;
622
623 r = ref_file(known_files, e->kernel, +1);
624 if (r < 0)
625 return r;
626 r = ref_file(known_files, e->efi, +1);
627 if (r < 0)
628 return r;
629 STRV_FOREACH(s, e->initrd) {
630 r = ref_file(known_files, *s, +1);
631 if (r < 0)
632 return r;
633 }
634 r = ref_file(known_files, e->device_tree, +1);
635 if (r < 0)
636 return r;
637 STRV_FOREACH(s, e->device_tree_overlay) {
638 r = ref_file(known_files, *s, +1);
639 if (r < 0)
640 return r;
641 }
642 }
643
644 *ret_known_files = TAKE_PTR(known_files);
645
646 return 0;
647 }
648
649 static int boot_config_find_in(const BootConfig *config, const char *root, const char *id) {
650 assert(config);
651
652 if (!root || !id)
653 return -ENOENT;
654
655 for (size_t i = 0; i < config->n_entries; i++)
656 if (path_equal(config->entries[i].root, root) &&
657 fnmatch(id, config->entries[i].id, FNM_CASEFOLD) == 0)
658 return i;
659
660 return -ENOENT;
661 }
662
663 static int unlink_entry(const BootConfig *config, const char *root, const char *id) {
664 _cleanup_(hashmap_free_free_keyp) Hashmap *known_files = NULL;
665 const BootEntry *e = NULL;
666 int r;
667
668 assert(config);
669
670 r = count_known_files(config, root, &known_files);
671 if (r < 0)
672 return log_error_errno(r, "Failed to count files in %s: %m", root);
673
674 r = boot_config_find_in(config, root, id);
675 if (r < 0)
676 return r;
677
678 if (r == config->default_entry)
679 log_warning("%s is the default boot entry", id);
680 if (r == config->selected_entry)
681 log_warning("%s is the selected boot entry", id);
682
683 e = &config->entries[r];
684
685 deref_unlink_file(known_files, e->kernel, e->root);
686 deref_unlink_file(known_files, e->efi, e->root);
687 STRV_FOREACH(s, e->initrd)
688 deref_unlink_file(known_files, *s, e->root);
689 deref_unlink_file(known_files, e->device_tree, e->root);
690 STRV_FOREACH(s, e->device_tree_overlay)
691 deref_unlink_file(known_files, *s, e->root);
692
693 if (arg_dry_run)
694 log_info("Would remove \"%s\"", e->path);
695 else {
696 r = chase_and_unlink(e->path, root, CHASE_PROHIBIT_SYMLINKS, 0, NULL);
697 if (r < 0)
698 return log_error_errno(r, "Failed to remove \"%s\": %m", e->path);
699
700 log_info("Removed %s", e->path);
701 }
702
703 return 0;
704 }
705
706 static int list_remove_orphaned_file(
707 RecurseDirEvent event,
708 const char *path,
709 int dir_fd,
710 int inode_fd,
711 const struct dirent *de,
712 const struct statx *sx,
713 void *userdata) {
714
715 Hashmap *known_files = userdata;
716
717 assert(path);
718 assert(known_files);
719
720 if (event != RECURSE_DIR_ENTRY)
721 return RECURSE_DIR_CONTINUE;
722
723 if (hashmap_get(known_files, path))
724 return RECURSE_DIR_CONTINUE; /* keep! */
725
726 if (arg_dry_run)
727 log_info("Would remove %s", path);
728 else if (unlinkat(dir_fd, de->d_name, 0) < 0)
729 log_warning_errno(errno, "Failed to remove \"%s\", ignoring: %m", path);
730 else
731 log_info("Removed %s", path);
732
733 return RECURSE_DIR_CONTINUE;
734 }
735
736 static int cleanup_orphaned_files(
737 const BootConfig *config,
738 const char *root) {
739
740 _cleanup_(hashmap_free_free_keyp) Hashmap *known_files = NULL;
741 _cleanup_free_ char *full = NULL, *p = NULL;
742 _cleanup_close_ int dir_fd = -EBADF;
743 int r;
744
745 assert(config);
746 assert(root);
747
748 log_info("Cleaning %s", root);
749
750 r = settle_entry_token();
751 if (r < 0)
752 return r;
753
754 r = count_known_files(config, root, &known_files);
755 if (r < 0)
756 return log_error_errno(r, "Failed to count files in %s: %m", root);
757
758 dir_fd = chase_and_open(arg_entry_token, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS,
759 O_DIRECTORY|O_CLOEXEC, &full);
760 if (dir_fd == -ENOENT)
761 return 0;
762 if (dir_fd < 0)
763 return log_error_errno(dir_fd, "Failed to open '%s/%s': %m", root, arg_entry_token);
764
765 p = path_join("/", arg_entry_token);
766 if (!p)
767 return log_oom();
768
769 r = recurse_dir(dir_fd, p, 0, UINT_MAX, RECURSE_DIR_SORT, list_remove_orphaned_file, known_files);
770 if (r < 0)
771 return log_error_errno(r, "Failed to cleanup %s: %m", full);
772
773 return r;
774 }
775
776 int verb_list(int argc, char *argv[], void *userdata) {
777 _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
778 dev_t esp_devid = 0, xbootldr_devid = 0;
779 int r;
780
781 /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two
782 * things: turn off logging about access errors and turn off potentially privileged device probing.
783 * Here we're interested in the latter but not the former, hence request the mode, and log about
784 * EACCES. */
785
786 r = acquire_esp(/* unprivileged_mode= */ -1, /* graceful= */ false, NULL, NULL, NULL, NULL, &esp_devid);
787 if (r == -EACCES) /* We really need the ESP path for this call, hence also log about access errors */
788 return log_error_errno(r, "Failed to determine ESP location: %m");
789 if (r < 0)
790 return r;
791
792 r = acquire_xbootldr(/* unprivileged_mode= */ -1, NULL, &xbootldr_devid);
793 if (r == -EACCES)
794 return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
795 if (r < 0)
796 return r;
797
798 r = boot_config_load_and_select(&config, arg_esp_path, esp_devid, arg_xbootldr_path, xbootldr_devid);
799 if (r < 0)
800 return r;
801
802 if (config.n_entries == 0 && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
803 log_info("No boot loader entries found.");
804 return 0;
805 }
806
807 if (streq(argv[0], "list")) {
808 pager_open(arg_pager_flags);
809 return show_boot_entries(&config, arg_json_format_flags);
810 } else if (streq(argv[0], "cleanup")) {
811 if (arg_xbootldr_path && xbootldr_devid != esp_devid)
812 cleanup_orphaned_files(&config, arg_xbootldr_path);
813 return cleanup_orphaned_files(&config, arg_esp_path);
814 } else {
815 assert(streq(argv[0], "unlink"));
816 if (arg_xbootldr_path && xbootldr_devid != esp_devid) {
817 r = unlink_entry(&config, arg_xbootldr_path, argv[1]);
818 if (r == 0 || r != -ENOENT)
819 return r;
820 }
821 return unlink_entry(&config, arg_esp_path, argv[1]);
822 }
823 }
824
825 int verb_unlink(int argc, char *argv[], void *userdata) {
826 return verb_list(argc, argv, userdata);
827 }