From 9a7915299484a767fbffc7234b8dc45c29954cca Mon Sep 17 00:00:00 2001 From: Warner Losh Date: Thu, 16 Feb 2023 08:53:45 -0700 Subject: [PATCH] efibootmgr: Add --efidev (-u) to discover UEFI's device path to a dev or file "efibootmgr --efidev unix-path" will return the UEFI device-path to the file or device specified by unix-path. It's useful for debugging, but may also be useful for scripting. Sponsored by: Netflix Reviewed by: corvink, manu Differential Revision: https://reviews.freebsd.org/D38617 --- usr.sbin/efibootmgr/efibootmgr.8 | 6 +++++- usr.sbin/efibootmgr/efibootmgr.c | 36 +++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/usr.sbin/efibootmgr/efibootmgr.8 b/usr.sbin/efibootmgr/efibootmgr.8 index 6b53de33db56..0aa70fe63da7 100644 --- a/usr.sbin/efibootmgr/efibootmgr.8 +++ b/usr.sbin/efibootmgr/efibootmgr.8 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd April 3, 2022 +.Dd February 15, 2023 .Dt EFIBOOTMGR 8 .Os .Sh NAME @@ -69,6 +69,7 @@ .Fl t Ar timeout .Nm .Fl T +.Nm Fl u Ar unix-path .Sh "DESCRIPTION" The .Nm @@ -174,6 +175,9 @@ Set the bootmenu timeout value. Delete the .Cm BootTimeout variable. +.It Fl u -efidev Ar unix-path +Displays the UEFI device path of +.Ar unix-path . .It Fl v -verbose Display the device path of boot entries in the output. .El diff --git a/usr.sbin/efibootmgr/efibootmgr.c b/usr.sbin/efibootmgr/efibootmgr.c index 53bc417c4e07..58d83d28d6e5 100644 --- a/usr.sbin/efibootmgr/efibootmgr.c +++ b/usr.sbin/efibootmgr/efibootmgr.c @@ -70,6 +70,7 @@ __FBSDID("$FreeBSD$"); #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001 typedef struct _bmgr_opts { + char *dev; char *env; char *loader; char *label; @@ -85,6 +86,7 @@ typedef struct _bmgr_opts { bool dry_run; bool device_path; bool esp_device; + bool find_dev; bool fw_ui; bool no_fw_ui; bool has_bootnum; @@ -114,6 +116,7 @@ static struct option lopts[] = { {"dry-run", no_argument, NULL, 'D'}, {"env", required_argument, NULL, 'e'}, {"esp", no_argument, NULL, 'E'}, + {"efidev", required_argument, NULL, 'u'}, {"fw-ui", no_argument, NULL, 'f'}, {"no-fw-ui", no_argument, NULL, 'F'}, {"help", no_argument, NULL, 'h'}, @@ -284,6 +287,9 @@ parse_args(int argc, char *argv[]) opts.set_timeout = true; opts.timeout = strtoul(optarg, NULL, 10); break; + case 'u': + opts.find_dev = true; + opts.dev = strdup(optarg); case 'v': opts.verbose = true; break; @@ -996,7 +1002,7 @@ report_esp_device(bool do_dp, bool do_unix) char *name, *dev, *relpath, *abspath; uint8_t *walker, *ep; efi_char *descr; - efidp dp, edp; + efidp dp; char buf[PATH_MAX]; if (do_dp && do_unix) @@ -1026,7 +1032,6 @@ report_esp_device(bool do_dp, bool do_unix) // Now we have fplen bytes worth of file path stuff dp = (efidp)walker; walker += fplen; - edp = (efidp)walker; if (walker > ep) errx(1, "malformed boot variable %s", name); if (do_dp) { @@ -1068,6 +1073,26 @@ set_boot_to_fw_ui(bool to_fw) errx(1, "failed to set boot to fw ui"); } +static void +find_efi_device(const char *path) +{ + efidp dp = NULL; + size_t len; + int ret; + char buf[1024]; + + ret = efivar_unix_path_to_device_path(path, &dp); + if (ret != 0) + errc(1, ret, + "Cannot translate path '%s' to UEFI", path); + len = efidp_size(dp); + if (len > MAX_DP_LEN) + errx(1, "Resulting device path too long."); + efidp_format_device_path(buf, sizeof(buf), dp, len); + printf("%s -> %s\n", path, buf); + exit (0); +} + int main(int argc, char *argv[]) { @@ -1075,7 +1100,10 @@ main(int argc, char *argv[]) memset(&opts, 0, sizeof (bmgr_opts_t)); parse_args(argc, argv); - if (!efi_variables_supported()) + /* + * find_dev can operate without any efi variables + */ + if (!efi_variables_supported() && !opts.find_dev) errx(1, "efi variables not supported on this system. root? kldload efirt?"); read_vars(); @@ -1107,6 +1135,8 @@ main(int argc, char *argv[]) set_boot_to_fw_ui(true); else if (opts.no_fw_ui) set_boot_to_fw_ui(false); + else if (opts.find_dev) + find_efi_device(opts.dev); print_boot_vars(opts.verbose); }