Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nJbKS-0003Kn-BH for mharc-grub-devel@gnu.org; Mon, 14 Feb 2022 08:23:44 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60470) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nJbKP-0003CH-90 for grub-devel@gnu.org; Mon, 14 Feb 2022 08:23:41 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:22688) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nJbKM-0003x2-SF for grub-devel@gnu.org; Mon, 14 Feb 2022 08:23:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644845017; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Vlu02WZ1mfkDLRFNMhheitFUvi8tkb+O2kUfm95J+Wg=; b=Fb8x9z5TdMCtj9T+Gaz0hFvfrUgrtfDlqXL7YN7EWilw5ceDPK5TGfwF3vxfrgpylh7EQT DfFO+edTb2hv1kcDvdgMROAdkt89Kni3+TUeAeZEO05MQyRaTusPGbArRjikPJGFZ228mA GU0t9RNaC4Mh/V9U105JvAHFv588mHo= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-108-5GD_qvKxPfCypUTuBKP7cA-1; Mon, 14 Feb 2022 08:23:36 -0500 X-MC-Unique: 5GD_qvKxPfCypUTuBKP7cA-1 Received: by mail-wm1-f70.google.com with SMTP id a8-20020a7bc1c8000000b0037bc4c62e97so5024621wmj.0 for ; Mon, 14 Feb 2022 05:23:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Vlu02WZ1mfkDLRFNMhheitFUvi8tkb+O2kUfm95J+Wg=; b=h/GTzjARapr+11TK8fbstWRcBdrolXpHxOQPUgQDe9uAi4fe2Zh5/lzKsJGaEO5x+A tfKoF7THO/y6X8TjQGLo/jA71ZbeqyLVtpxwRNcpwbmdw7j31IPMc1KwXchkDiI4TbXw LnLr6vCGBWN94zU92539ogWz2z3pG6U3pvOok+qGk0KguTJw+goeQd7ycgueOsLU57PK xwXS1ibxb6jLExdx39aRyRbn3OAzS2z/8F5oD/zddd9ucEtOYq64TF55ItZUjC3tVDpX 7DDmfHch9LvyAHawxniccnAZhhC/FQBWgxK3E6TuY7w8aQlyZl2eZj8sObDIfoCA6/xR fHIw== X-Gm-Message-State: AOAM531T+P+zAB2R4yhiDApRvVkYt23H1d1YG5gL0TDN9A83pGjoAkNq yQ4w3yLKxZVhV4Zkr+LMt9JsTMCg6JB0UMkdvQcX+pSAu9jiKPeFZCtDxIC66tU8u0xBADmW2SP hN+8Lt199A7rT5Hu6yTazhA6kNWmLvTjJbI6JryWzWGtt2aA74GinS68/iaJ9SdI= X-Received: by 2002:a5d:53c9:: with SMTP id a9mr11021787wrw.672.1644845011699; Mon, 14 Feb 2022 05:23:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJwZRAy23bnUKpukncQ3PQGbQSvqhFsyLhz412uDeBgFFKqsItHl1EynZrFMIe67qsBTlsD7vQ== X-Received: by 2002:a5d:53c9:: with SMTP id a9mr11021769wrw.672.1644845011445; Mon, 14 Feb 2022 05:23:31 -0800 (PST) Received: from t590.redhat.com (lfbn-gre-1-180-51.w90-112.abo.wanadoo.fr. [90.112.144.51]) by smtp.gmail.com with ESMTPSA id i3sm30669556wrq.72.2022.02.14.05.23.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 05:23:30 -0800 (PST) From: =?UTF-8?q?Renaud=20M=C3=A9trich?= To: grub-devel@gnu.org Cc: =?UTF-8?q?Renaud=20M=C3=A9trich?= Subject: [PATCH v2 1/2] efi: new 'connectefi' command Date: Mon, 14 Feb 2022 14:23:22 +0100 Message-Id: <20220214132323.37302-1-rmetrich@redhat.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=rmetrich@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=170.10.133.124; envelope-from=rmetrich@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.083, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Feb 2022 13:23:41 -0000 When efi.quickboot is enabled on VMWare (which is the default for hardware release 16 and later), it may happen that not all EFI devices are connected. Due to this, browsing the devices in make_devices() just fails to find devices, in particular disks or partitions for a given disk. This typically happens when network booting, then trying to chainload to local disk (this is used in deployment tools such as Red Hat Satellite), which is done through using the following grub.cfg snippet: -------- 8< ---------------- 8< ---------------- 8< -------- unset prefix search --file --set=prefix /EFI/redhat/grubx64.efi if [ -n "$prefix" ]; then chainloader ($prefix)/EFI/redhat/grubx64/efi ... -------- 8< ---------------- 8< ---------------- 8< -------- With efi.quickboot, none of the devices are connected, causing "search" to fail. Sometimes devices are connected but not the partition of the disk matching $prefix, causing partition to not be found by "chainloader". This patch introduces a new "connectefi" command which recursively connects all EFI devices starting from PCI root devices. Typical grub.cfg snippet would then be: -------- 8< ---------------- 8< ---------------- 8< -------- unset prefix connectefi search --file --set=prefix /EFI/redhat/grubx64.efi if [ -n "$prefix" ]; then chainloader ($prefix)/EFI/redhat/grubx64/efi ... -------- 8< ---------------- 8< ---------------- 8< -------- In the future, if really needed, the code may be enhanced to avoid the recursion and only connect PCI devices block devices. Signed-off-by: Renaud Métrich --- grub-core/commands/efi/lsefi.c | 52 ++++++++++++++++++++++++++++++++++ grub-core/disk/efi/efidisk.c | 13 +++++++++ grub-core/kern/efi/efi.c | 13 +++++++++ include/grub/efi/disk.h | 2 ++ include/grub/efi/efi.h | 5 ++++ 5 files changed, 85 insertions(+) diff --git a/grub-core/commands/efi/lsefi.c b/grub-core/commands/efi/lsefi.c index d1ce99af4..7acba3b39 100644 --- a/grub-core/commands/efi/lsefi.c +++ b/grub-core/commands/efi/lsefi.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,7 @@ #include #include #include +#include GRUB_MOD_LICENSE ("GPLv3+"); @@ -78,6 +80,54 @@ struct known_protocol { GRUB_EFI_USB_IO_PROTOCOL_GUID, "USB I/O" }, }; +static grub_err_t +grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) +{ + grub_efi_handle_t *handles; + grub_efi_uintn_t num_handles; + unsigned i, j; + grub_efi_guid_t pci_root_guid = GRUB_EFI_PCI_ROOT_IO_GUID; + + handles = grub_efi_locate_handle (GRUB_EFI_ALL_HANDLES, + NULL, NULL, &num_handles); + + for (i = 0; i < num_handles; i++) + { + grub_efi_handle_t handle = handles[i]; + grub_efi_status_t status; + grub_efi_uintn_t num_protocols; + grub_efi_packed_guid_t **protocols; + + status = efi_call_3 (grub_efi_system_table->boot_services->protocols_per_handle, + handle, &protocols, &num_protocols); + if (status != GRUB_EFI_SUCCESS) + { + grub_dprintf ("efi", "handle %p: unable to retrieve protocols\n", handle); + continue; + } + for (j = 0; j < num_protocols; j++) + if (! grub_memcmp (protocols[j], &pci_root_guid, sizeof (pci_root_guid))) + { + status = grub_efi_connect_controller(handle, NULL, NULL, 1); + grub_dprintf ("efi", "connected handle %p (PCI root) " + "recursively: result=%" PRIxGRUB_EFI_STATUS "\n", + handle, status); + break; + } + + if (protocols) + grub_efi_free_pool (protocols); + } + + grub_free (handles); + + grub_efidisk_reenumerate_disks (); + + return GRUB_ERR_NONE; +} + static grub_err_t grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)), int argc __attribute__ ((unused)), @@ -147,6 +197,8 @@ GRUB_MOD_INIT(lsefi) { cmd = grub_register_command ("lsefi", grub_cmd_lsefi, NULL, "Display EFI handles."); + cmd = grub_register_command ("connectefi", grub_cmd_connectefi, + NULL, "Connect EFI handles."); } GRUB_MOD_FINI(lsefi) diff --git a/grub-core/disk/efi/efidisk.c b/grub-core/disk/efi/efidisk.c index f077b5f55..eb0de5625 100644 --- a/grub-core/disk/efi/efidisk.c +++ b/grub-core/disk/efi/efidisk.c @@ -396,6 +396,19 @@ enumerate_disks (void) free_devices (devices); } +void +grub_efidisk_reenumerate_disks (void) +{ + free_devices (fd_devices); + free_devices (hd_devices); + free_devices (cd_devices); + fd_devices = 0; + hd_devices = 0; + cd_devices = 0; + + enumerate_disks (); +} + static int grub_efidisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data, grub_disk_pull_t pull) diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c index 18858c327..bdfff3dd0 100644 --- a/grub-core/kern/efi/efi.c +++ b/grub-core/kern/efi/efi.c @@ -95,6 +95,19 @@ grub_efi_locate_handle (grub_efi_locate_search_type_t search_type, return buffer; } +grub_efi_status_t +grub_efi_connect_controller (grub_efi_handle_t controller_handle, + grub_efi_handle_t *driver_image_handle, + grub_efi_device_path_protocol_t *remaining_device_path, + grub_efi_boolean_t recursive) +{ + grub_efi_boot_services_t *b; + + b = grub_efi_system_table->boot_services; + return efi_call_4 (b->connect_controller, controller_handle, + driver_image_handle, remaining_device_path, recursive); +} + void * grub_efi_open_protocol (grub_efi_handle_t handle, grub_efi_guid_t *protocol, diff --git a/include/grub/efi/disk.h b/include/grub/efi/disk.h index 254475c84..6845c2f1f 100644 --- a/include/grub/efi/disk.h +++ b/include/grub/efi/disk.h @@ -27,6 +27,8 @@ grub_efi_handle_t EXPORT_FUNC(grub_efidisk_get_device_handle) (grub_disk_t disk); char *EXPORT_FUNC(grub_efidisk_get_device_name) (grub_efi_handle_t *handle); +void EXPORT_FUNC(grub_efidisk_reenumerate_disks) (void); + void grub_efidisk_init (void); void grub_efidisk_fini (void); diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index fc723962d..62dbd9788 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -32,6 +32,11 @@ EXPORT_FUNC(grub_efi_locate_handle) (grub_efi_locate_search_type_t search_type, grub_efi_guid_t *protocol, void *search_key, grub_efi_uintn_t *num_handles); +grub_efi_status_t +EXPORT_FUNC(grub_efi_connect_controller) (grub_efi_handle_t controller_handle, + grub_efi_handle_t *driver_image_handle, + grub_efi_device_path_protocol_t *remaining_device_path, + grub_efi_boolean_t recursive); void *EXPORT_FUNC(grub_efi_open_protocol) (grub_efi_handle_t handle, grub_efi_guid_t *protocol, grub_efi_uint32_t attributes); -- 2.34.1