From: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Date: Wed, 21 Oct 2020 22:04:21 +0000 (+0300)
Subject: efi_loader: Disable devices before handing over control
X-Git-Tag: v2025.01-rc5-pxa1908~2156^2~1
X-Git-Url: http://git.dujemihanovic.xyz/%22http:/kyber.dk/phpMyBuilder/static/%7B%7B%20.Permalink%20%7D%7D?a=commitdiff_plain;h=529441ca89b155abcad2acea62b6f35c19b1c0ac;p=u-boot.git

efi_loader: Disable devices before handing over control

U-Boot Driver Model is supposed to remove devices with either
DM_REMOVE_ACTIVE_DMA or DM_REMOVE_OS_PREPARE flags set, before exiting.
Our bootm command does that by explicitly calling calling
"dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);" and we also disable any
USB devices.

The EFI equivalent is doing none of those at the moment. As a result
probing an fTPM driver now renders it unusable in Linux. During our
(*probe) callback we open a session with OP-TEE, which is supposed to
close with our (*remove) callback. Since the (*remove) is never called,
once we boot into Linux and try to probe the device again we are getting
a busy error response. Moreover all uclass (*preremove) functions won't
run.

So let's fix this by mimicking what bootm does and disconnect devices
when efi_exit_boot_services() is called.

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---

diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
index 2896c3ea5a..b26ac9fbfc 100644
--- a/lib/efi_loader/efi_boottime.c
+++ b/lib/efi_loader/efi_boottime.c
@@ -8,6 +8,8 @@
 #include <common.h>
 #include <bootm.h>
 #include <div64.h>
+#include <dm/device.h>
+#include <dm/root.h>
 #include <efi_loader.h>
 #include <irq_func.h>
 #include <log.h>
@@ -15,6 +17,7 @@
 #include <pe.h>
 #include <time.h>
 #include <u-boot/crc.h>
+#include <usb.h>
 #include <watchdog.h>
 #include <linux/libfdt_env.h>
 
@@ -1993,7 +1996,10 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
 			list_del(&evt->link);
 	}
 
+	if IS_ENABLED(CONFIG_USB_DEVICE)
+		udc_disconnect();
 	board_quiesce_devices();
+	dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
 
 	/* Patch out unsupported runtime function */
 	efi_runtime_detach();