From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Mon, 04 Mar 2024 20:10:50 +0100 Received: from metis.whiteo.stw.pengutronix.de ([2a0a:edc0:2:b01:1d::104]) by lore.white.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1rhDi6-008dmu-30 for lore@lore.pengutronix.de; Mon, 04 Mar 2024 20:10:50 +0100 Received: from bombadil.infradead.org ([2607:7c80:54:3::133]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rhDi3-0005i0-Tg for lore@pengutronix.de; Mon, 04 Mar 2024 20:10:50 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=1HbbjGdNYtuNxwnhA8eID1h6WVSvKbeEdEs9qtZk95o=; b=bTfpKTGZ3cr6Af9xlIwc817GA2 iah/JVJxF/BbaKRadOaOP93ObPbHAEeV7fcdJj9agybnL0Kjy2HoopO/WarrdAti71zA0/7aHZrj/ VKHepFGZL1aXVONIfduTQiMRXBwnC5vxHHnrKIolXPlht+U6rHaiQpoEbLqM/kPxvc8CMVEnZC8R+ 2r1y71MfemK5hYTH/tKVuby3ma7tFSzL1k6Y6X8SFT1S+np5wdBFGIglah3c1M+ZZ5Ru6okb1Pokb d5yJAArX/JRoFVRB48pHdtjKqxL+9OpowuHkcQlBtulEAy1JXgWftcIR/c0mPTiDo+PoJNczN/jh/ qyZ9ESIA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhDhN-0000000ANIZ-1fJL; Mon, 04 Mar 2024 19:10:05 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhDad-0000000AIdR-1h5T for barebox@bombadil.infradead.org; Mon, 04 Mar 2024 19:03:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=1HbbjGdNYtuNxwnhA8eID1h6WVSvKbeEdEs9qtZk95o=; b=mofVHXBIKFrrHBpMfn6TSU1b+n Fp25rGS0RE6IoUwbakVoBIIzSybEkzapfgT3E+vZpqhT+/slrdoWDZAtd0Q9arfAM7dl5ueIYKvwm HAQQ7jCc6M5W4MU14VQbKNiSCMxRRoadXH6DTKfNkU8f7hD7UXqq3vljDVS4WRVMoP7N4tz8i6hOt 6WwAP2kLoJTSxocRovZvejXeFSKiN+XtMJ8HGA05qLfegq6pKSKQG7ogOYtwr7QrbLmmqMXFljr1o FqStQtoaWzgkk8N79Idlcs1v6HDNiybpdpavUx1/GJg7PQ4aaALXOhVSCpQMKil5yx7hEycYeMGyn GWgpttZw==; Received: from metis.whiteo.stw.pengutronix.de ([2a0a:edc0:2:b01:1d::104]) by casper.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhDaX-00000002BSu-0TLE for barebox@lists.infradead.org; Mon, 04 Mar 2024 19:03:06 +0000 Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rhDaG-00068A-Am; Mon, 04 Mar 2024 20:02:44 +0100 Received: from [2a0a:edc0:0:1101:1d::54] (helo=dude05.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rhDaF-004PJd-TT; Mon, 04 Mar 2024 20:02:43 +0100 Received: from localhost ([::1] helo=dude05.red.stw.pengutronix.de) by dude05.red.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1rhDYN-00Ed9V-2u; Mon, 04 Mar 2024 20:00:48 +0100 From: Ahmad Fatoum To: barebox@lists.infradead.org Cc: Ahmad Fatoum Date: Mon, 4 Mar 2024 20:00:33 +0100 Message-Id: <20240304190038.3486881-109-a.fatoum@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240304190038.3486881-1-a.fatoum@pengutronix.de> References: <20240304190038.3486881-1-a.fatoum@pengutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240304_190301_372058_8D60CB8D X-CRM114-Status: GOOD ( 25.93 ) X-BeenThere: barebox@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "barebox" X-SA-Exim-Connect-IP: 2607:7c80:54:3::133 X-SA-Exim-Mail-From: barebox-bounces+lore=pengutronix.de@lists.infradead.org X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on metis.whiteo.stw.pengutronix.de X-Spam-Level: X-Spam-Status: No, score=-5.4 required=4.0 tests=AWL,BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Subject: [PATCH v2 108/113] commands: provide efi_handle_dump in both payload and loader X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on metis.whiteo.stw.pengutronix.de) The efi_handle_dump command is currently used when barebox is built as payload, but it's also useful when running as a loader to list handles and their protocols. Move the code into a common area to make it accessible in both configurations. Signed-off-by: Ahmad Fatoum --- commands/Kconfig | 8 ++ commands/Makefile | 2 + commands/efi_handle_dump.c | 181 ++++++++++++++++++++++++++++++++++ drivers/efi/Makefile | 3 +- drivers/efi/efi-device.c | 196 +------------------------------------ drivers/efi/efi-handle.c | 43 ++++++++ include/efi/efi-device.h | 9 ++ 7 files changed, 247 insertions(+), 195 deletions(-) create mode 100644 commands/efi_handle_dump.c create mode 100644 drivers/efi/efi-handle.c diff --git a/commands/Kconfig b/commands/Kconfig index 269d2432b45e..8d0816c4d0f0 100644 --- a/commands/Kconfig +++ b/commands/Kconfig @@ -123,6 +123,14 @@ config CMD_DRVINFO help List compiled-in device drivers and the devices they support. +config CMD_EFI_HANDLE_DUMP + tristate + default y + depends on EFI + prompt "efi_handle_dump" + help + Dump information on EFI handles + config CMD_HELP tristate default y diff --git a/commands/Makefile b/commands/Makefile index 19c006101dc6..30e1f8403eef 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -149,4 +149,6 @@ obj-$(CONFIG_CMD_SELFTEST) += selftest.o obj-$(CONFIG_CMD_TUTORIAL) += tutorial.o obj-$(CONFIG_CMD_STACKSMASH) += stacksmash.o obj-$(CONFIG_CMD_PARTED) += parted.o +obj-$(CONFIG_CMD_EFI_HANDLE_DUMP) += efi_handle_dump.o + UBSAN_SANITIZE_ubsan.o := y diff --git a/commands/efi_handle_dump.c b/commands/efi_handle_dump.c new file mode 100644 index 000000000000..43cdb8ac503e --- /dev/null +++ b/commands/efi_handle_dump.c @@ -0,0 +1,181 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * efi-device.c - barebox EFI payload support + * + * Copyright (c) 2014 Sascha Hauer , Pengutronix + */ + +#include +#include +#include +#include +#include + +static void efi_devpath(efi_handle_t handle) +{ + efi_status_t efiret; + void *devpath; + char *dev_path_str; + + efiret = BS->open_protocol(handle, &efi_device_path_protocol_guid, + &devpath, NULL, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (EFI_ERROR(efiret)) + return; + + dev_path_str = device_path_to_str(devpath); + if (dev_path_str) { + printf(" Devpath: \n %s\n", dev_path_str); + free(dev_path_str); + } +} + +static void efi_dump(efi_handle_t *handles, unsigned long handle_count) +{ + int i, j; + unsigned long num_guids; + efi_guid_t **guids; + + if (!handles || !handle_count) + return; + + for (i = 0; i < handle_count; i++) { + printf("handle-%p\n", handles[i]); + + BS->protocols_per_handle(handles[i], &guids, &num_guids); + printf(" Protocols:\n"); + for (j = 0; j < num_guids; j++) + printf(" %d: %pUl: %s\n", j, guids[j], + efi_guid_string(guids[j])); + efi_devpath(handles[i]); + } + printf("\n"); +} + +static unsigned char to_digit(unsigned char c) +{ + if (c >= '0' && c <= '9') + c -= '0'; + else if (c >= 'A' && c <= 'F') + c -= 'A' - 10; + else + c -= 'a' - 10; + + return c; +} + +#define read_xbit(src, dest, bit) \ + do { \ + int __i; \ + for (__i = (bit - 4); __i >= 0; __i -= 4, src++) \ + dest |= to_digit(*src) << __i; \ + } while (0) + +static int do_efi_protocol_dump(int argc, char **argv) +{ + unsigned long handle_count = 0; + efi_handle_t *handles = NULL; + int ret; + efi_guid_t guid; + u32 a = 0; + u16 b = 0; + u16 c = 0; + u8 d0 = 0; + u8 d1 = 0; + u8 d2 = 0; + u8 d3 = 0; + u8 d4 = 0; + u8 d5 = 0; + u8 d6 = 0; + u8 d7 = 0; + + /* Format 220e73b6-6bdb-4413-8405-b974b108619a */ + if (argc == 1) { + char *s = argv[0]; + int len = strlen(s); + + if (len != 36) + return -EINVAL; + + read_xbit(s, a, 32); + if (*s != '-') + return -EINVAL; + s++; + read_xbit(s, b, 16); + if (*s != '-') + return -EINVAL; + s++; + read_xbit(s, c, 16); + if (*s != '-') + return -EINVAL; + s++; + read_xbit(s, d0, 8); + read_xbit(s, d1, 8); + if (*s != '-') + return -EINVAL; + s++; + read_xbit(s, d2, 8); + read_xbit(s, d3, 8); + read_xbit(s, d4, 8); + read_xbit(s, d5, 8); + read_xbit(s, d6, 8); + read_xbit(s, d7, 8); + } else if (argc == 11) { + /* Format : + * 220e73b6 6bdb 4413 84 05 b9 74 b1 08 61 9a + * or + * 0x220e73b6 0x6bdb 0x14413 0x84 0x05 0xb9 0x74 0xb1 0x08 0x61 0x9a + */ + a = simple_strtoul(argv[0], NULL, 16); + b = simple_strtoul(argv[1], NULL, 16); + c = simple_strtoul(argv[2], NULL, 16); + d0 = simple_strtoul(argv[3], NULL, 16); + d1 = simple_strtoul(argv[4], NULL, 16); + d2 = simple_strtoul(argv[5], NULL, 16); + d3 = simple_strtoul(argv[6], NULL, 16); + d4 = simple_strtoul(argv[7], NULL, 16); + d5 = simple_strtoul(argv[8], NULL, 16); + d6 = simple_strtoul(argv[9], NULL, 16); + d7 = simple_strtoul(argv[10], NULL, 16); + } else { + return -EINVAL; + } + + guid = EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7); + + printf("Searching for:\n"); + printf(" %pUl: %s\n", &guid, efi_guid_string(&guid)); + + ret = __efi_locate_handle(BS, BY_PROTOCOL, &guid, NULL, &handle_count, &handles); + if (!ret) + efi_dump(handles, handle_count); + + return 0; +} + +static int do_efi_handle_dump(int argc, char *argv[]) +{ + unsigned long handle_count = 0; + efi_handle_t *handles = NULL; + int ret; + + if (argc > 1) + return do_efi_protocol_dump(--argc, ++argv); + + ret = __efi_locate_handle(BS, ALL_HANDLES, NULL, NULL, &handle_count, &handles); + if (!ret) + efi_dump(handles, handle_count); + + return 0; +} + +BAREBOX_CMD_HELP_START(efi_handle_dump) +BAREBOX_CMD_HELP_TEXT("Dump all the efi handle with protocol and devpath\n") +BAREBOX_CMD_HELP_END + +BAREBOX_CMD_START(efi_handle_dump) + .cmd = do_efi_handle_dump, + BAREBOX_CMD_DESC("Usage: efi_handle_dump") + BAREBOX_CMD_OPTS("[a-b-c-d0d1-d3d4d5d6d7] or [a b c d0 d1 d2 d3 d4 d5 d6 d7]") + BAREBOX_CMD_GROUP(CMD_GRP_MISC) + BAREBOX_CMD_HELP(cmd_efi_handle_dump_help) +BAREBOX_CMD_END diff --git a/drivers/efi/Makefile b/drivers/efi/Makefile index bd8fadac4222..4e3c39e14486 100644 --- a/drivers/efi/Makefile +++ b/drivers/efi/Makefile @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y += efi-device.o +obj-y += efi-handle.o +obj-$(CONFIG_EFI_PAYLOAD) += efi-device.o diff --git a/drivers/efi/efi-device.c b/drivers/efi/efi-device.c index bbc76e9ef769..33c82c81dd1d 100644 --- a/drivers/efi/efi-device.c +++ b/drivers/efi/efi-device.c @@ -6,7 +6,6 @@ */ #include -#include #include #include #include @@ -27,30 +26,8 @@ static int efi_locate_handle(enum efi_locate_search_type search_type, unsigned long *no_handles, efi_handle_t **buffer) { - efi_status_t efiret; - unsigned long buffer_size = 0; - efi_handle_t *buf; - - efiret = BS->locate_handle(search_type, protocol, search_key, &buffer_size, - NULL); - if (EFI_ERROR(efiret) && efiret != EFI_BUFFER_TOO_SMALL) - return -efi_errno(efiret); - - buf = malloc(buffer_size); - if (!buf) - return -ENOMEM; - - efiret = BS->locate_handle(search_type, protocol, search_key, &buffer_size, - buf); - if (EFI_ERROR(efiret)) { - free(buf); - return -efi_errno(efiret); - } - - *no_handles = buffer_size / sizeof(efi_handle_t); - *buffer = buf; - - return 0; + return __efi_locate_handle(BS, search_type, protocol, search_key, no_handles, + buffer); } static struct efi_device *efi_find_device(efi_handle_t handle) @@ -499,172 +476,3 @@ void efi_continue_devices(void) efidrv->dev_continue(efidev); } } - -static void efi_devpath(efi_handle_t handle) -{ - efi_status_t efiret; - void *devpath; - char *dev_path_str; - - efiret = BS->open_protocol(handle, &efi_device_path_protocol_guid, - &devpath, NULL, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (EFI_ERROR(efiret)) - return; - - dev_path_str = device_path_to_str(devpath); - if (dev_path_str) { - printf(" Devpath: \n %s\n", dev_path_str); - free(dev_path_str); - } -} - -static void efi_dump(efi_handle_t *handles, unsigned long handle_count) -{ - int i, j; - unsigned long num_guids; - efi_guid_t **guids; - - if (!handles || !handle_count) - return; - - for (i = 0; i < handle_count; i++) { - printf("handle-%p\n", handles[i]); - - BS->protocols_per_handle(handles[i], &guids, &num_guids); - printf(" Protocols:\n"); - for (j = 0; j < num_guids; j++) - printf(" %d: %pUl: %s\n", j, guids[j], - efi_guid_string(guids[j])); - efi_devpath(handles[i]); - } - printf("\n"); -} - -static unsigned char to_digit(unsigned char c) -{ - if (c >= '0' && c <= '9') - c -= '0'; - else if (c >= 'A' && c <= 'F') - c -= 'A' - 10; - else - c -= 'a' - 10; - - return c; -} - -#define read_xbit(src, dest, bit) \ - do { \ - int __i; \ - for (__i = (bit - 4); __i >= 0; __i -= 4, src++) \ - dest |= to_digit(*src) << __i; \ - } while (0) - -static int do_efi_protocol_dump(int argc, char **argv) -{ - unsigned long handle_count = 0; - efi_handle_t *handles = NULL; - int ret; - efi_guid_t guid; - u32 a = 0; - u16 b = 0; - u16 c = 0; - u8 d0 = 0; - u8 d1 = 0; - u8 d2 = 0; - u8 d3 = 0; - u8 d4 = 0; - u8 d5 = 0; - u8 d6 = 0; - u8 d7 = 0; - - /* Format 220e73b6-6bdb-4413-8405-b974b108619a */ - if (argc == 1) { - char *s = argv[0]; - int len = strlen(s); - - if (len != 36) - return -EINVAL; - - read_xbit(s, a, 32); - if (*s != '-') - return -EINVAL; - s++; - read_xbit(s, b, 16); - if (*s != '-') - return -EINVAL; - s++; - read_xbit(s, c, 16); - if (*s != '-') - return -EINVAL; - s++; - read_xbit(s, d0, 8); - read_xbit(s, d1, 8); - if (*s != '-') - return -EINVAL; - s++; - read_xbit(s, d2, 8); - read_xbit(s, d3, 8); - read_xbit(s, d4, 8); - read_xbit(s, d5, 8); - read_xbit(s, d6, 8); - read_xbit(s, d7, 8); - } else if (argc == 11) { - /* Format : - * 220e73b6 6bdb 4413 84 05 b9 74 b1 08 61 9a - * or - * 0x220e73b6 0x6bdb 0x14413 0x84 0x05 0xb9 0x74 0xb1 0x08 0x61 0x9a - */ - a = simple_strtoul(argv[0], NULL, 16); - b = simple_strtoul(argv[1], NULL, 16); - c = simple_strtoul(argv[2], NULL, 16); - d0 = simple_strtoul(argv[3], NULL, 16); - d1 = simple_strtoul(argv[4], NULL, 16); - d2 = simple_strtoul(argv[5], NULL, 16); - d3 = simple_strtoul(argv[6], NULL, 16); - d4 = simple_strtoul(argv[7], NULL, 16); - d5 = simple_strtoul(argv[8], NULL, 16); - d6 = simple_strtoul(argv[9], NULL, 16); - d7 = simple_strtoul(argv[10], NULL, 16); - } else { - return -EINVAL; - } - - guid = EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7); - - printf("Searching for:\n"); - printf(" %pUl: %s\n", &guid, efi_guid_string(&guid)); - - ret = efi_locate_handle(BY_PROTOCOL, &guid, NULL, &handle_count, &handles); - if (!ret) - efi_dump(handles, handle_count); - - return 0; -} - -static int do_efi_handle_dump(int argc, char *argv[]) -{ - unsigned long handle_count = 0; - efi_handle_t *handles = NULL; - int ret; - - if (argc > 1) - return do_efi_protocol_dump(--argc, ++argv); - - ret = efi_locate_handle(ALL_HANDLES, NULL, NULL, &handle_count, &handles); - if (!ret) - efi_dump(handles, handle_count); - - return 0; -} - -BAREBOX_CMD_HELP_START(efi_handle_dump) -BAREBOX_CMD_HELP_TEXT("Dump all the efi handle with protocol and devpath\n") -BAREBOX_CMD_HELP_END - -BAREBOX_CMD_START(efi_handle_dump) - .cmd = do_efi_handle_dump, - BAREBOX_CMD_DESC("Usage: efi_handle_dump") - BAREBOX_CMD_OPTS("[a-b-c-d0d1-d3d4d5d6d7] or [a b c d0 d1 d2 d3 d4 d5 d6 d7]") - BAREBOX_CMD_GROUP(CMD_GRP_MISC) - BAREBOX_CMD_HELP(cmd_efi_handle_dump_help) -BAREBOX_CMD_END diff --git a/drivers/efi/efi-handle.c b/drivers/efi/efi-handle.c new file mode 100644 index 000000000000..be9013cb648f --- /dev/null +++ b/drivers/efi/efi-handle.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2014 Sascha Hauer , Pengutronix + */ + +#include +#include +#include +#include +#include + +int __efi_locate_handle(struct efi_boot_services *bs, + enum efi_locate_search_type search_type, + efi_guid_t *protocol, + void *search_key, + unsigned long *no_handles, + efi_handle_t **buffer) +{ + efi_status_t efiret; + unsigned long buffer_size = 0; + efi_handle_t *buf; + + efiret = bs->locate_handle(search_type, protocol, search_key, &buffer_size, + NULL); + if (EFI_ERROR(efiret) && efiret != EFI_BUFFER_TOO_SMALL) + return -efi_errno(efiret); + + buf = malloc(buffer_size); + if (!buf) + return -ENOMEM; + + efiret = bs->locate_handle(search_type, protocol, search_key, &buffer_size, + buf); + if (EFI_ERROR(efiret)) { + free(buf); + return -efi_errno(efiret); + } + + *no_handles = buffer_size / sizeof(efi_handle_t); + *buffer = buf; + + return 0; +} diff --git a/include/efi/efi-device.h b/include/efi/efi-device.h index 5f7490028b9e..5d2110356fd4 100644 --- a/include/efi/efi-device.h +++ b/include/efi/efi-device.h @@ -63,4 +63,13 @@ static inline bool efi_device_has_guid(struct efi_device *efidev, efi_guid_t gui return false; } +enum efi_locate_search_type; + +int __efi_locate_handle(struct efi_boot_services *bs, + enum efi_locate_search_type search_type, + efi_guid_t *protocol, + void *search_key, + unsigned long *no_handles, + efi_handle_t **buffer); + #endif /* __EFI_EFI_DEVICE_H */ -- 2.39.2