Debian Codegraph @

src/bootctl/bootctl-status.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-varlink.h"
6
7 #include "alloc-util.h"
8 #include "bootctl.h"
9 #include "bootctl-status.h"
10 #include "bootctl-util.h"
11 #include "bootspec.h"
12 #include "bootspec-util.h"
13 #include "chase.h"
14 #include "dirent-util.h"
15 #include "efi-api.h"
16 #include "efi-loader.h"
17 #include "efivars.h"
18 #include "errno-util.h"
19 #include "fd-util.h"
20 #include "locale-setup.h"
21 #include "log.h"
22 #include "pager.h"
23 #include "pretty-print.h"
24 #include "string-util.h"
25 #include "tpm2-util.h"
26
27 static int status_entries(
28 const BootConfig *config,
29 const char *esp_path,
30 sd_id128_t esp_partition_uuid,
31 const char *xbootldr_path,
32 sd_id128_t xbootldr_partition_uuid) {
33
34 int r;
35
36 assert(config);
37 assert(esp_path || xbootldr_path);
38
39 printf("%sBoot Loader Entry Locations:%s\n", ansi_underline(), ansi_normal());
40
41 bool need_paren = false;
42 printf(" ESP: %s", esp_path);
43 if (!sd_id128_is_null(esp_partition_uuid)) {
44 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "",
45 SD_ID128_FORMAT_VAL(esp_partition_uuid));
46 need_paren = true;
47 }
48 if (!xbootldr_path) {
49 if (!need_paren) {
50 fputs(" (", stdout);
51 need_paren = true;
52 } else
53 fputs(", ", stdout);
54
55 /* ESP is $BOOT if XBOOTLDR not present. */
56 printf("%s$BOOT%s", ansi_green(), ansi_normal());
57 }
58 if (need_paren)
59 putchar(')');
60 putchar('\n');
61
62 if (config->loader_conf_status != 0) {
63 assert(esp_path);
64 printf(" config: %s%s/%s%s",
65 ansi_grey(), esp_path, ansi_normal(), "/loader/loader.conf");
66 if (config->loader_conf_status < 0)
67 printf(": %s%s%s",
68 config->loader_conf_status == -ENOENT ? ansi_grey() : ansi_highlight_yellow(),
69 STRERROR(config->loader_conf_status),
70 ansi_normal());
71 putchar('\n');
72 }
73
74 if (xbootldr_path) {
75 printf(" XBOOTLDR: %s (", xbootldr_path);
76 if (!sd_id128_is_null(xbootldr_partition_uuid))
77 printf("/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ", ",
78 SD_ID128_FORMAT_VAL(xbootldr_partition_uuid));
79 /* XBOOTLDR is always $BOOT if present. */
80 printf("%s$BOOT%s)\n", ansi_green(), ansi_normal());
81 }
82
83 if (settle_entry_token() >= 0)
84 printf(" token: %s\n", arg_entry_token);
85 putchar('\n');
86
87 if (config->default_entry < 0)
88 printf("%zu entries, no entry could be determined as default.\n", config->n_entries);
89 else {
90 printf("%sDefault Boot Loader Entry:%s\n", ansi_underline(), ansi_normal());
91
92 r = show_boot_entry(
93 boot_config_default_entry(config),
94 /* show_as_default= */ false,
95 /* show_as_selected= */ false,
96 /* show_reported= */ false);
97 if (r > 0)
98 /* < 0 is already logged by the function itself, let's just emit an extra warning if
99 the default entry is broken */
100 printf("\nWARNING: default boot entry is broken\n");
101 }
102
103 return 0;
104 }
105
106 static int print_efi_option(uint16_t id, int *n_printed, bool in_order) {
107 _cleanup_free_ char *title = NULL;
108 _cleanup_free_ char *path = NULL;
109 sd_id128_t partition;
110 bool active;
111 int r;
112
113 assert(n_printed);
114
115 r = efi_get_boot_option(id, &title, &partition, &path, &active);
116 if (r == -ENOENT) {
117 log_debug_errno(r, "Boot option 0x%04X referenced but missing, ignoring: %m", id);
118 return 0;
119 }
120 if (r < 0)
121 return log_error_errno(r, "Failed to read boot option 0x%04X: %m", id);
122
123 /* print only configured entries with partition information */
124 if (!path || sd_id128_is_null(partition)) {
125 log_debug("Ignoring boot entry 0x%04X without partition information.", id);
126 return 0;
127 }
128
129 efi_tilt_backslashes(path);
130
131 if (*n_printed == 0) /* Print section title before first entry */
132 printf("%sBoot Loaders Listed in EFI Variables:%s\n", ansi_underline(), ansi_normal());
133
134 printf(" Title: %s%s%s\n", ansi_highlight(), strna(title), ansi_normal());
135 printf(" ID: 0x%04X\n", id);
136 printf(" Status: %sactive%s\n", active ? "" : "in", in_order ? ", boot-order" : "");
137 printf(" Partition: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
138 SD_ID128_FORMAT_VAL(partition));
139 printf(" File: %s%s%s/%s%s\n",
140 glyph(GLYPH_TREE_RIGHT), ansi_grey(), arg_esp_path, ansi_normal(), 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|CHASE_TRIGGER_AUTOFS, &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, skip_leading_slash(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 = RET_NERRNO(openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC));
221 if (fd == -ENOENT)
222 continue;
223 if (fd < 0)
224 return log_error_errno(fd, "Failed to open file '%s' for reading: %m", de->d_name);
225
226 r = get_file_version(fd, &v);
227 if (r < 0 && r != -ESRCH)
228 return log_error_errno(r, "Failed to get file version of '%s': %m", de->d_name);
229
230 if (*previous) { /* Let's output the previous entry now, since now we know that there will be
231 * one more, and can draw the tree glyph properly. */
232 printf(" %s %s%s\n",
233 *is_first ? "File:" : " ",
234 glyph(GLYPH_TREE_BRANCH), *previous);
235 *is_first = false;
236 *previous = mfree(*previous);
237 }
238
239 /* Do not output this entry immediately, but store what should be printed in a state
240 * variable, because we only will know the tree glyph to print (branch or final edge) once we
241 * read one more entry */
242 if (r == -ESRCH) /* Not systemd-owned file but still interesting to print */
243 r = asprintf(previous, "%s%s/%s/%s/%s",
244 ansi_grey(), esp_path, ansi_normal(), path, de->d_name);
245 else /* if (r >= 0) */
246 r = asprintf(previous, "%s%s/%s/%s/%s (%s%s%s)",
247 ansi_grey(), esp_path, ansi_normal(), path, de->d_name,
248 ansi_highlight(), v, ansi_normal());
249 if (r < 0)
250 return log_oom();
251
252 c++;
253 }
254
255 return c;
256 }
257
258 static int status_binaries(const char *esp_path, sd_id128_t partition) {
259 _cleanup_free_ char *last = NULL;
260 bool is_first = true;
261 int r, k;
262
263 printf("%sAvailable Boot Loaders on ESP:%s\n", ansi_underline(), ansi_normal());
264
265 if (!esp_path) {
266 printf(" ESP: Cannot find or access mount point of ESP.\n\n");
267 return -ENOENT;
268 }
269
270 printf(" ESP: %s", esp_path);
271 if (!sd_id128_is_null(partition))
272 printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")", SD_ID128_FORMAT_VAL(partition));
273 printf("\n");
274
275 r = enumerate_binaries(esp_path, "EFI/systemd", &last, &is_first);
276 if (r < 0)
277 goto fail;
278
279 k = enumerate_binaries(esp_path, "EFI/BOOT", &last, &is_first);
280 if (k < 0) {
281 r = k;
282 goto fail;
283 }
284
285 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 */
286 printf(" %s %s%s\n",
287 is_first ? "File:" : " ",
288 glyph(GLYPH_TREE_RIGHT), last);
289
290 if (r == 0 && !arg_quiet)
291 log_info("systemd-boot not installed in ESP.");
292 if (k == 0 && !arg_quiet)
293 log_info("No default/fallback boot loader installed in ESP.");
294
295 printf("\n");
296 return 0;
297
298 fail:
299 errno = -r;
300 printf(" File: (can't access %s: %m)\n\n", esp_path);
301 return r;
302 }
303
304 static int efi_get_variable_string_and_warn(const char *variable, char **ret) {
305 int r;
306
307 r = efi_get_variable_string(variable, ret);
308 if (r < 0 && r != -ENOENT)
309 return log_warning_errno(r, "Failed to read EFI variable '%s', ignoring: %m", variable);
310
311 return r;
312 }
313
314 static int efi_get_variable_path_and_warn(const char *variable, char **ret) {
315 int r;
316
317 r = efi_get_variable_path(variable, ret);
318 if (r < 0 && r != -ENOENT)
319 return log_warning_errno(r, "Failed to read EFI variable '%s', ignoring: %m", variable);
320
321 return r;
322 }
323
324 static void print_yes_no_line(bool first, bool good, const char *name) {
325 printf("%s%s %s\n",
326 first ? " Features: " : " ",
327 COLOR_MARK_BOOL(good),
328 name);
329 }
330
331 int verb_status(int argc, char *argv[], uintptr_t _data, void *userdata) {
332 sd_id128_t esp_uuid = SD_ID128_NULL, xbootldr_uuid = SD_ID128_NULL;
333 dev_t esp_devid = 0, xbootldr_devid = 0;
334 int r, k;
335
336 bool has_efi = touch_variables();
337
338 r = acquire_esp(/* unprivileged_mode= */ -1,
339 /* graceful= */ false,
340 /* ret_fd= */ NULL,
341 /* ret_part= */ NULL,
342 /* ret_pstart= */ NULL,
343 /* ret_psize= */ NULL,
344 &esp_uuid,
345 &esp_devid);
346 if (arg_print_esp_path) {
347 if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only
348 * error the find_esp_and_warn() won't log on its own) */
349 return log_error_errno(r, "Failed to determine ESP location: %m");
350 if (r < 0)
351 return r;
352
353 puts(arg_esp_path);
354 return 0;
355 }
356
357 r = acquire_xbootldr(
358 /* unprivileged_mode= */ -1,
359 /* ret_fd= */ NULL,
360 &xbootldr_uuid,
361 &xbootldr_devid);
362 if (arg_print_dollar_boot_path) {
363 if (r == -EACCES)
364 return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
365 if (r < 0)
366 return r;
367
368 const char *path = arg_dollar_boot_path();
369 if (!path)
370 return log_error_errno(SYNTHETIC_ERRNO(EACCES), "Failed to determine XBOOTLDR location.");
371
372 puts(path);
373 return 0;
374 }
375
376 r = 0; /* If we couldn't determine the path, then don't consider that a problem from here on, just
377 * show what we can show */
378
379 pager_open(arg_pager_flags);
380
381 if (!has_efi) {
382 if (arg_root)
383 log_debug("Skipping 'System' section, operating offline.");
384 else
385 printf("%sSystem:%s\n"
386 "Not booted with EFI\n\n",
387 ansi_underline(), ansi_normal());
388 } else {
389 static const struct {
390 uint64_t flag;
391 const char *name;
392 } loader_flags[] = {
393 { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" },
394 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" },
395 { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" },
396 { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" },
397 { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" },
398 { EFI_LOADER_FEATURE_XBOOTLDR, "Support for XBOOTLDR partition" },
399 { EFI_LOADER_FEATURE_RANDOM_SEED, "Support for passing random seed to OS" },
400 { EFI_LOADER_FEATURE_LOAD_DRIVER, "Load drop-in drivers" },
401 { EFI_LOADER_FEATURE_SORT_KEY, "Support Type #1 sort-key field" },
402 { EFI_LOADER_FEATURE_SAVED_ENTRY, "Support @saved pseudo-entry" },
403 { EFI_LOADER_FEATURE_DEVICETREE, "Support Type #1 devicetree field" },
404 { EFI_LOADER_FEATURE_SECUREBOOT_ENROLL, "Enroll SecureBoot keys" },
405 { EFI_LOADER_FEATURE_RETAIN_SHIM, "Retain SHIM protocols" },
406 { EFI_LOADER_FEATURE_MENU_DISABLE, "Menu can be disabled" },
407 { EFI_LOADER_FEATURE_MULTI_PROFILE_UKI, "Multi-Profile UKIs are supported" },
408 { EFI_LOADER_FEATURE_REPORT_URL, "Loader reports network boot URL" },
409 { EFI_LOADER_FEATURE_TYPE1_UKI, "Support Type #1 uki field" },
410 { EFI_LOADER_FEATURE_TYPE1_UKI_URL, "Support Type #1 uki-url field" },
411 { EFI_LOADER_FEATURE_TPM2_ACTIVE_PCR_BANKS, "Loader reports active TPM2 PCR banks" },
412 { EFI_LOADER_FEATURE_KEYBOARD_LAYOUT, "Loader reports firmware keyboard layout" },
413 { EFI_LOADER_FEATURE_SMBIOS_MEASURED, "Loader measures SMBIOS information" },
414 };
415 static const struct {
416 uint64_t flag;
417 const char *name;
418 } stub_flags[] = {
419 { EFI_STUB_FEATURE_REPORT_BOOT_PARTITION, "Stub reports loader partition information" },
420 { EFI_STUB_FEATURE_REPORT_STUB_PARTITION, "Stub reports stub partition information" },
421 { EFI_STUB_FEATURE_REPORT_URL, "Stub reports network boot URL" },
422 { EFI_STUB_FEATURE_PICK_UP_CREDENTIALS, "Picks up credentials from boot partition" },
423 { EFI_STUB_FEATURE_PICK_UP_SYSEXTS, "Picks up system extension images from boot partition" },
424 { EFI_STUB_FEATURE_PICK_UP_CONFEXTS, "Picks up configuration extension images from boot partition" },
425 { EFI_STUB_FEATURE_THREE_PCRS, "Measures kernel+command line+sysexts" },
426 { EFI_STUB_FEATURE_RANDOM_SEED, "Support for passing random seed to OS" },
427 { EFI_STUB_FEATURE_CMDLINE_ADDONS, "Pick up .cmdline from addons" },
428 { EFI_STUB_FEATURE_CMDLINE_SMBIOS, "Pick up .cmdline from SMBIOS Type 11" },
429 { EFI_STUB_FEATURE_DEVICETREE_ADDONS, "Pick up .dtb from addons" },
430 { EFI_STUB_FEATURE_MULTI_PROFILE_UKI, "Stub understands profile selector" },
431 { EFI_STUB_FEATURE_SMBIOS_MEASURED, "Stub measures SMBIOS information" },
432 };
433 _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL, *stub_path = NULL,
434 *current_entry = NULL, *oneshot_entry = NULL, *preferred_entry = NULL, *default_entry = NULL, *sysfail_entry = NULL,
435 *sysfail_reason = NULL, *keyboard_layout = NULL;
436 uint64_t loader_features = 0, stub_features = 0;
437 int have;
438
439 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderFirmwareType"), &fw_type);
440 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderFirmwareInfo"), &fw_info);
441 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderInfo"), &loader);
442 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("StubInfo"), &stub);
443 (void) efi_get_variable_path_and_warn(EFI_LOADER_VARIABLE_STR("LoaderImageIdentifier"), &loader_path);
444 (void) efi_get_variable_path_and_warn(EFI_LOADER_VARIABLE_STR("StubImageIdentifier"), &stub_path);
445 (void) efi_loader_get_features(&loader_features);
446 (void) efi_stub_get_features(&stub_features);
447 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderEntrySelected"), &current_entry);
448 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), &oneshot_entry);
449 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderEntryPreferred"), &preferred_entry);
450 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"), &default_entry);
451 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderEntrySysFail"), &sysfail_entry);
452 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderSysFailReason"), &sysfail_reason);
453 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderKeyboardLayout"), &keyboard_layout);
454
455 SecureBootMode secure = efi_get_secure_boot_mode();
456 printf("%sSystem:%s\n", ansi_underline(), ansi_normal());
457 printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal());
458 printf(" Firmware Arch: %s\n", get_efi_arch());
459 printf(" Secure Boot: %s%s%s",
460 IN_SET(secure, SECURE_BOOT_USER, SECURE_BOOT_DEPLOYED) ? ansi_highlight_green() : ansi_normal(),
461 enabled_disabled(IN_SET(secure, SECURE_BOOT_USER, SECURE_BOOT_DEPLOYED)),
462 ansi_normal());
463
464 if (secure != SECURE_BOOT_DISABLED)
465 printf(" (%s)\n", secure_boot_mode_to_string(secure));
466 else
467 printf("\n");
468
469 Tpm2Support s = tpm2_support_full(TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER);
470 printf(" TPM2 Support: %s%s%s\n",
471 FLAGS_SET(s, TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER) ? ansi_highlight_green() :
472 (s & (TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER)) != 0 ? ansi_highlight_red() : ansi_highlight_yellow(),
473 FLAGS_SET(s, TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER) ? "yes" :
474 (s & TPM2_SUPPORT_FIRMWARE) ? "firmware only, driver unavailable" :
475 (s & TPM2_SUPPORT_DRIVER) ? "driver only, firmware unavailable" : "no",
476 ansi_normal());
477
478 k = efi_measured_uki(LOG_DEBUG);
479 if (k > 0)
480 printf(" Measured UKI: %syes%s\n", ansi_highlight_green(), ansi_normal());
481 else if (k == 0)
482 printf(" Measured UKI: no\n");
483 else {
484 errno = -k;
485 printf(" Measured UKI: %sfailed%s (%m)\n", ansi_highlight_red(), ansi_normal());
486 }
487
488 k = efi_measured_os(LOG_DEBUG);
489 if (k > 0)
490 printf(" Measured OS: %syes%s\n", ansi_highlight_green(), ansi_normal());
491 else if (k == 0)
492 printf(" Measured OS: no\n");
493 else {
494 errno = -k;
495 printf(" Measured OS: %sfailed%s (%m)\n", ansi_highlight_red(), ansi_normal());
496 }
497
498 k = efi_get_reboot_to_firmware();
499 if (k > 0)
500 printf(" Boot into FW: %sactive%s\n", ansi_highlight_yellow(), ansi_normal());
501 else if (k == 0)
502 printf(" Boot into FW: supported\n");
503 else if (k == -EOPNOTSUPP)
504 printf(" Boot into FW: not supported\n");
505 else {
506 errno = -k;
507 printf(" Boot into FW: %sfailed%s (%m)\n", ansi_highlight_red(), ansi_normal());
508 }
509
510 _cleanup_free_ char *lang = NULL;
511 k = locale_lang_from_efi(&lang, /* flags= */ 0);
512 if (k > 0)
513 printf(" Platform Lang: %s\n", lang);
514 else if (k == 0)
515 printf(" Platform Lang: n/a\n");
516 else {
517 errno = -k;
518 printf(" Platform Lang: %sfailed%s (%m)\n", ansi_highlight_red(), ansi_normal());
519 }
520
521 printf("\n");
522
523 if (loader) {
524 printf("%sCurrent Boot Loader:%s\n", ansi_underline(), ansi_normal());
525 printf(" Product: %s%s%s\n", ansi_highlight(), loader, ansi_normal());
526 for (size_t i = 0; i < ELEMENTSOF(loader_flags); i++)
527 print_yes_no_line(i == 0, FLAGS_SET(loader_features, loader_flags[i].flag), loader_flags[i].name);
528
529 sd_id128_t loader_partition_uuid = SD_ID128_NULL;
530 (void) efi_loader_get_device_part_uuid(&loader_partition_uuid);
531
532 _cleanup_free_ char *loader_url = NULL;
533 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("LoaderDeviceURL"), &loader_url);
534
535 if (!sd_id128_is_null(loader_partition_uuid)) {
536 /* If we know esp_uuid and loader_partition_uuid is not equal to it, print a warning. */
537 if (!sd_id128_is_null(esp_uuid) && !sd_id128_equal(loader_partition_uuid, esp_uuid))
538 printf("WARNING: The boot loader reports a different partition UUID than the detected ESP "
539 "("SD_ID128_UUID_FORMAT_STR" vs. "SD_ID128_UUID_FORMAT_STR")!\n",
540 SD_ID128_FORMAT_VAL(loader_partition_uuid),
541 SD_ID128_FORMAT_VAL(esp_uuid));
542
543 printf(" Partition: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
544 SD_ID128_FORMAT_VAL(loader_partition_uuid));
545 } else if (loader_path)
546 printf(" Partition: n/a\n");
547
548 if (loader_path)
549 printf(" Loader: %s%s%s/%s%s\n",
550 glyph(GLYPH_TREE_RIGHT), ansi_grey(), arg_esp_path, ansi_normal(), loader_path);
551
552 if (loader_url)
553 printf(" Net Boot URL: %s\n", loader_url);
554
555 if (FLAGS_SET(loader_features, EFI_LOADER_FEATURE_KEYBOARD_LAYOUT))
556 printf("Keyboard Layout: %s\n",
557 keyboard_layout ?: "n/a (not reported by firmware)");
558
559 if (sysfail_entry)
560 printf(" SysFail Reason: %s\n", sysfail_reason);
561
562 if (current_entry)
563 printf(" Current Entry: %s\n", current_entry);
564 if (preferred_entry)
565 printf("Preferred Entry: %s\n", preferred_entry);
566 if (default_entry)
567 printf(" Default Entry: %s\n", default_entry);
568 if (oneshot_entry && !streq_ptr(oneshot_entry, default_entry))
569 printf(" OneShot Entry: %s\n", oneshot_entry);
570 if (sysfail_entry)
571 printf(" SysFail Entry: %s\n", sysfail_entry);
572
573 printf("\n");
574 }
575
576 if (stub) {
577 printf("%sCurrent Stub:%s\n", ansi_underline(), ansi_normal());
578 printf(" Product: %s%s%s\n", ansi_highlight(), stub, ansi_normal());
579 for (size_t i = 0; i < ELEMENTSOF(stub_flags); i++)
580 print_yes_no_line(i == 0, FLAGS_SET(stub_features, stub_flags[i].flag), stub_flags[i].name);
581
582 sd_id128_t stub_partition_uuid = SD_ID128_NULL;
583 (void) efi_stub_get_device_part_uuid(&stub_partition_uuid);
584
585 _cleanup_free_ char *stub_url = NULL;
586 (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE_STR("StubDeviceURL"), &stub_url);
587
588 if (!sd_id128_is_null(stub_partition_uuid)) {
589 /* _If_ we know both esp_uuid and xbootldr_uuid and stub_partition_uuid is not equal
590 * to _either_ of them, print a warning. */
591 if (!sd_id128_is_null(esp_uuid) && !sd_id128_equal(stub_partition_uuid, esp_uuid) &&
592 !sd_id128_is_null(xbootldr_uuid) && !sd_id128_equal(stub_partition_uuid, xbootldr_uuid))
593 printf("WARNING: The stub loader reports a different UUID than the detected ESP and XBOOTLDR partitions "
594 "("SD_ID128_UUID_FORMAT_STR" vs. "SD_ID128_UUID_FORMAT_STR"/"SD_ID128_UUID_FORMAT_STR")!\n",
595 SD_ID128_FORMAT_VAL(stub_partition_uuid),
596 SD_ID128_FORMAT_VAL(esp_uuid),
597 SD_ID128_FORMAT_VAL(xbootldr_uuid));
598
599 printf(" Partition: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n",
600 SD_ID128_FORMAT_VAL(stub_partition_uuid));
601 } else if (stub_path)
602 printf(" Partition: n/a\n");
603
604 if (stub_path)
605 printf(" Stub: %s%s\n", glyph(GLYPH_TREE_RIGHT), strna(stub_path));
606
607 if (stub_url)
608 printf(" Net Boot URL: %s\n", stub_url);
609
610 printf("\n");
611 }
612
613 printf("%sRandom Seed:%s\n", ansi_underline(), ansi_normal());
614 have = access(EFIVAR_PATH(EFI_LOADER_VARIABLE_STR("LoaderSystemToken")), F_OK) >= 0;
615 printf(" System Token: %s\n", have ? "set" : "not set");
616
617 if (arg_esp_path) {
618 _cleanup_free_ char *p = NULL;
619
620 p = path_join(arg_esp_path, "/loader/random-seed");
621 if (!p)
622 return log_oom();
623
624 r = access(p, F_OK);
625 if (r < 0 && errno != ENOENT)
626 printf(" Exists: Can't access %s (%m)\n", p);
627 else
628 printf(" Exists: %s\n", yes_no(r >= 0));
629 }
630
631 printf("\n");
632 }
633
634 if (arg_esp_path)
635 RET_GATHER(r, status_binaries(arg_esp_path, esp_uuid));
636
637 if (has_efi)
638 RET_GATHER(r, status_variables());
639
640 if (arg_esp_path || arg_xbootldr_path) {
641 _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
642
643 k = boot_config_load_and_select(
644 &config,
645 arg_root,
646 arg_esp_path, esp_devid,
647 arg_xbootldr_path, xbootldr_devid);
648 RET_GATHER(r, k);
649
650 if (k >= 0)
651 RET_GATHER(r,
652 status_entries(&config,
653 arg_esp_path, esp_uuid,
654 arg_xbootldr_path, xbootldr_uuid));
655 }
656
657 return r;
658 }
659
660 int verb_list(int argc, char *argv[], uintptr_t _data, void *userdata) {
661 _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
662 dev_t esp_devid = 0, xbootldr_devid = 0;
663 int r;
664
665 /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two
666 * things: turn off logging about access errors and turn off potentially privileged device probing.
667 * Here we're interested in the latter but not the former, hence request the mode, and log about
668 * EACCES. */
669
670 (void) touch_variables();
671
672 r = acquire_esp(/* unprivileged_mode= */ -1,
673 /* graceful= */ false,
674 /* ret_fd= */ NULL,
675 /* ret_part= */ NULL,
676 /* ret_pstart= */ NULL,
677 /* ret_psize= */ NULL,
678 /* ret_uuid= */ NULL,
679 &esp_devid);
680 if (r == -EACCES) /* We really need the ESP path for this call, hence also log about access errors */
681 return log_error_errno(r, "Failed to determine ESP location: %m");
682 if (r < 0)
683 return r;
684
685 r = acquire_xbootldr(
686 /* unprivileged_mode= */ -1,
687 /* ret_fd= */ NULL,
688 /* ret_uuid= */ NULL,
689 &xbootldr_devid);
690 if (r == -EACCES)
691 return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
692 if (r < 0)
693 return r;
694
695 r = boot_config_load_and_select(&config, arg_root, arg_esp_path, esp_devid, arg_xbootldr_path, xbootldr_devid);
696 if (r < 0)
697 return r;
698
699 if (config.n_entries == 0 && !sd_json_format_enabled(arg_json_format_flags)) {
700 log_info("No boot loader entries found.");
701 return 0;
702 }
703
704 pager_open(arg_pager_flags);
705 return show_boot_entries(&config, arg_json_format_flags);
706 }
707
708 int vl_method_list_boot_entries(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
709 _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
710 dev_t esp_devid = 0, xbootldr_devid = 0;
711 int r;
712
713 assert(link);
714 assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE));
715
716 r = sd_varlink_dispatch(link, parameters, /* dispatch_table= */ NULL, /* userdata= */ NULL);
717 if (r != 0)
718 return r;
719
720 r = acquire_esp(/* unprivileged_mode= */ false,
721 /* graceful= */ false,
722 /* ret_fd= */ NULL,
723 /* ret_part= */ NULL,
724 /* ret_pstart= */ NULL,
725 /* ret_psize= */ NULL,
726 /* ret_uuid= */ NULL,
727 &esp_devid);
728 if (r == -EACCES) /* We really need the ESP path for this call, hence also log about access errors */
729 return log_error_errno(r, "Failed to determine ESP location: %m");
730 if (r < 0)
731 return r;
732
733 r = acquire_xbootldr(
734 /* unprivileged_mode= */ false,
735 /* ret_fd= */ NULL,
736 /* ret_uuid= */ NULL,
737 &xbootldr_devid);
738 if (r == -EACCES)
739 return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
740 if (r < 0)
741 return r;
742
743 r = boot_config_load_and_select(&config, arg_root, arg_esp_path, esp_devid, arg_xbootldr_path, xbootldr_devid);
744 if (r < 0)
745 return r;
746
747 r = sd_varlink_set_sentinel(link, "io.systemd.BootControl.NoSuchBootEntry");
748 if (r < 0)
749 return r;
750
751 for (size_t i = 0; i < config.n_entries; i++) {
752 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
753
754 r = boot_entry_to_json(&config, i, &v);
755 if (r < 0)
756 return r;
757
758 r = sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_VARIANT("entry", v));
759 if (r < 0)
760 return r;
761 }
762
763 return 0;
764 }