Skip to contentSkip to conversation

efi_loader: Disable devices before handing over control

PATCH New Ilias Apalodimas 3 participants 8 messages Oct 21, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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. We also never free

So let's fix this by mimicking what bootm does and disconnect devices when efi_exit_boot_services() is called. Note that for the OP-TEE case and in particular any subsequent bootloader that wants to use a device (e.g GRUB) will need to call exit_boot_services() in order to close the session.

---
 lib/efi_loader/efi_boottime.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/efi_loader/efi_boottime.c +5 -0
index bf78176217c6..25e6cf0fe719 100644
@@ -18,6 +18,8 @@
 #include <pe.h>
 #include <u-boot/crc.h>
 #include <watchdog.h>
+#include <dm/device.h>
+#include <dm/root.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1994,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();
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/1 efi_loader: Disable devices before handing over control

Quoted 13 lines · Ilias Apalodimas

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. We also never free

Quoted 5 lines

So let's fix this by mimicking what bootm does and disconnect devices when efi_exit_boot_services() is called. Note that for the OP-TEE case and in particular any subsequent bootloader that wants to use a device (e.g GRUB) will need to call exit_boot_services() in order to close the session.

Hello Ilias,

thanks for the patch. Adding the function calls looks correct to me, but sandbox_spl_defconfig gives me a compile time error with this patch, cf.
https://gitlab.denx.de/u-boot/custodians/u-boot-efi/-/jobs/166842

You might want to use Travis CI before resubmitting.

Best regards

Heinrich

Quoted 31 lines
---
 lib/efi_loader/efi_boottime.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/efi_loader/efi_boottime.c +5 -0
index bf78176217c6..25e6cf0fe719 100644
@@ -18,6 +18,8 @@
 #include <pe.h>
 #include <u-boot/crc.h>
 #include <watchdog.h>
+#include <dm/device.h>
+#include <dm/root.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1994,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();
#2 lore raw

PATCH 1/1 efi_loader: Disable devices before handing over control

Hi Heinrich,

Quoted 20 lines · Heinrich Schuchardt
Quoted 13 lines · Ilias Apalodimas

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. We also never free

Quoted 5 lines

So let's fix this by mimicking what bootm does and disconnect devices when efi_exit_boot_services() is called. Note that for the OP-TEE case and in particular any subsequent bootloader that wants to use a device (e.g GRUB) will need to call exit_boot_services() in order to close the session.

Quoted 3 lines

Hello Ilias,

thanks for the patch. Adding the function calls looks correct to me,

Well the only doubt I have is what if GRUB has to extend some PCRs before calling Linux? Any idea if it's currently calling ExitBootSevices?
I was considering if it would be a better idea to call the device unbinding during some kind of "exit" from U-boot's EFI code. (i.e before StartImage)

Quoted 5 lines

but
sandbox_spl_defconfig gives me a compile time error with this patch, cf.
https://gitlab.denx.de/u-boot/custodians/u-boot-efi/-/jobs/166842

You might want to use Travis CI before resubmitting.

Ok will do.

Thanks
/Ilias

Quoted 38 lines

Best regards

Heinrich

Quoted 31 lines
---
 lib/efi_loader/efi_boottime.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/efi_loader/efi_boottime.c +5 -0
index bf78176217c6..25e6cf0fe719 100644
@@ -18,6 +18,8 @@
 #include <pe.h>
 #include <u-boot/crc.h>
 #include <watchdog.h>
+#include <dm/device.h>
+#include <dm/root.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1994,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();
#3 lore raw

PATCH 1/1 efi_loader: Disable devices before handing over control

Quoted 23 lines · Ilias Apalodimas

Hi Heinrich,

Quoted 20 lines · Heinrich Schuchardt
Quoted 18 lines · Ilias Apalodimas

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. We also never free

So let's fix this by mimicking what bootm does and disconnect devices when efi_exit_boot_services() is called. Note that for the OP-TEE case and in particular any subsequent bootloader that wants to use a device (e.g GRUB) will need to call exit_boot_services() in order to close the session.

Quoted 8 lines
Quoted 3 lines

Hello Ilias,

thanks for the patch. Adding the function calls looks correct to me,

Well the only doubt I have is what if GRUB has to extend some PCRs before calling Linux? Any idea if it's currently calling ExitBootSevices?
I was considering if it would be a better idea to call the device unbinding during some kind of "exit" from U-boot's EFI code. (i.e before StartImage)

ExitBootServices() is called by the Linux EFI stub in function allocate_new_fdt_and_exit_boot().

If GRUB would call ExitBootServices(), it would not be able to launch the EFI stub via StartImage().

Best regards

Heinrich

Quoted 50 lines
Quoted 6 lines

but
sandbox_spl_defconfig gives me a compile time error with this patch, cf.
https://gitlab.denx.de/u-boot/custodians/u-boot-efi/-/jobs/166842

You might want to use Travis CI before resubmitting.

Ok will do.

Thanks
/Ilias

Quoted 38 lines

Best regards

Heinrich

Quoted 31 lines
---
 lib/efi_loader/efi_boottime.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/efi_loader/efi_boottime.c +5 -0
index bf78176217c6..25e6cf0fe719 100644
@@ -18,6 +18,8 @@
 #include <pe.h>
 #include <u-boot/crc.h>
 #include <watchdog.h>
+#include <dm/device.h>
+#include <dm/root.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1994,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();
#4 lore raw

PATCH 1/1 efi_loader: Disable devices before handing over control

Hi Heinrich,

Quoted 34 lines · Heinrich Schuchardt
Quoted 23 lines · Ilias Apalodimas

Hi Heinrich,

Quoted 20 lines · Heinrich Schuchardt
Quoted 18 lines · Ilias Apalodimas

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. We also never free

So let's fix this by mimicking what bootm does and disconnect devices when efi_exit_boot_services() is called. Note that for the OP-TEE case and in particular any subsequent bootloader that wants to use a device (e.g GRUB) will need to call exit_boot_services() in order to close the session.

Quoted 8 lines
Quoted 3 lines

Hello Ilias,

thanks for the patch. Adding the function calls looks correct to me,

Well the only doubt I have is what if GRUB has to extend some PCRs before calling Linux? Any idea if it's currently calling ExitBootSevices?
I was considering if it would be a better idea to call the device unbinding during some kind of "exit" from U-boot's EFI code. (i.e before StartImage)

Quoted 5 lines

ExitBootServices() is called by the Linux EFI stub in function allocate_new_fdt_and_exit_boot().

If GRUB would call ExitBootServices(), it would not be able to launch the EFI stub via StartImage().

Yea that's my point. So with the current patch, you won't be able to access the fTPM driver from GRUB
(or any other EFI application) until the Linux EFI stub calls exit boot services. Maybe calling those 2 functions in StartImage is a better idea?

/Ilias

Quoted 56 lines

Best regards

Heinrich

Quoted 50 lines
Quoted 6 lines

but
sandbox_spl_defconfig gives me a compile time error with this patch, cf.
https://gitlab.denx.de/u-boot/custodians/u-boot-efi/-/jobs/166842

You might want to use Travis CI before resubmitting.

Ok will do.

Thanks
/Ilias

Quoted 38 lines

Best regards

Heinrich

Quoted 31 lines
---
 lib/efi_loader/efi_boottime.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/efi_loader/efi_boottime.c +5 -0
index bf78176217c6..25e6cf0fe719 100644
@@ -18,6 +18,8 @@
 #include <pe.h>
 #include <u-boot/crc.h>
 #include <watchdog.h>
+#include <dm/device.h>
+#include <dm/root.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1994,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();
#5 lore raw

PATCH 1/1 efi_loader: Disable devices before handing over control

Hi Heinrich

Quoted 42 lines · Ilias Apalodimas

Hi Heinrich,

Quoted 37 lines · Heinrich Schuchardt
Quoted 23 lines · Ilias Apalodimas

Hi Heinrich,

Quoted 20 lines · Heinrich Schuchardt
Quoted 18 lines · Ilias Apalodimas

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. We also never free

So let's fix this by mimicking what bootm does and disconnect devices when efi_exit_boot_services() is called. Note that for the OP-TEE case and in particular any subsequent bootloader that wants to use a device (e.g GRUB) will need to call exit_boot_services() in order to close the session.

Quoted 8 lines
Quoted 3 lines

Hello Ilias,

thanks for the patch. Adding the function calls looks correct to me,

Well the only doubt I have is what if GRUB has to extend some PCRs before calling Linux? Any idea if it's currently calling ExitBootSevices?
I was considering if it would be a better idea to call the device unbinding during some kind of "exit" from U-boot's EFI code. (i.e before StartImage)

ExitBootServices() is called by the Linux EFI stub in function allocate_new_fdt_and_exit_boot().

Quoted 8 lines
Quoted 2 lines

If GRUB would call ExitBootServices(), it would not be able to launch the EFI stub via StartImage().

Yea that's my point. So with the current patch, you won't be able to access the fTPM driver from GRUB
(or any other EFI application) until the Linux EFI stub calls exit boot services. Maybe calling those 2 functions in StartImage is a better idea?

So as it seems (over irc discussion), GRUB expects an EFI protocol to use the fTPM device.
So removing the device on ExitBootServices is fine, since GRUB never access the TPM directly.
U-Boot still doesn't have support for EFI_TCG2_PROTOCOL , but if we add that GRUB will be able
to access the device via the EFI protocol.

I'll send a v2 fixing the compilation error shortly

Cheers
/Ilias

Quoted 59 lines

/Ilias

Quoted 56 lines

Best regards

Heinrich

Quoted 50 lines
Quoted 6 lines

but
sandbox_spl_defconfig gives me a compile time error with this patch, cf.
https://gitlab.denx.de/u-boot/custodians/u-boot-efi/-/jobs/166842

You might want to use Travis CI before resubmitting.

Ok will do.

Thanks
/Ilias

Quoted 38 lines

Best regards

Heinrich

Quoted 31 lines
---
 lib/efi_loader/efi_boottime.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/efi_loader/efi_boottime.c +5 -0
index bf78176217c6..25e6cf0fe719 100644
@@ -18,6 +18,8 @@
 #include <pe.h>
 #include <u-boot/crc.h>
 #include <watchdog.h>
+#include <dm/device.h>
+#include <dm/root.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1994,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();
#6 lore raw

PATCH 1/1 efi_loader: Disable devices before handing over control

In reply to Ilias Apalodimas
Quoted 44 lines

From: Ilias Apalodimas <[email protected]>
Date: Wed, 21 Oct 2020 15:42:02 +0300

Hi Heinrich,

Quoted 37 lines · Heinrich Schuchardt
Quoted 23 lines · Ilias Apalodimas

Hi Heinrich,

Quoted 20 lines · Heinrich Schuchardt
Quoted 18 lines · Ilias Apalodimas

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. We also never free

So let's fix this by mimicking what bootm does and disconnect devices when efi_exit_boot_services() is called. Note that for the OP-TEE case and in particular any subsequent bootloader that wants to use a device (e.g GRUB) will need to call exit_boot_services() in order to close the session.

Quoted 8 lines
Quoted 3 lines

Hello Ilias,

thanks for the patch. Adding the function calls looks correct to me,

Well the only doubt I have is what if GRUB has to extend some PCRs before calling Linux? Any idea if it's currently calling ExitBootSevices?
I was considering if it would be a better idea to call the device unbinding during some kind of "exit" from U-boot's EFI code. (i.e before StartImage)

ExitBootServices() is called by the Linux EFI stub in function allocate_new_fdt_and_exit_boot().

Quoted 8 lines
Quoted 2 lines

If GRUB would call ExitBootServices(), it would not be able to launch the EFI stub via StartImage().

Yea that's my point. So with the current patch, you won't be able to access the fTPM driver from GRUB
(or any other EFI application) until the Linux EFI stub calls exit boot services. Maybe calling those 2 functions in StartImage is a better idea?

Shouldn't an EFI application (such as GRUB) be using EFI protocols to access the TPM?

#7 lore raw

PATCH 1/1 efi_loader: Disable devices before handing over control

Hi Mark,

Quoted 47 lines · Mark Kettenis
Quoted 47 lines

From: Ilias Apalodimas <[email protected]>
Date: Wed, 21 Oct 2020 15:42:02 +0300

Hi Heinrich,

Quoted 39 lines · Heinrich Schuchardt
Quoted 23 lines · Ilias Apalodimas

Hi Heinrich,

Quoted 20 lines · Heinrich Schuchardt
Quoted 18 lines · Ilias Apalodimas

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. We also never free

So let's fix this by mimicking what bootm does and disconnect devices when efi_exit_boot_services() is called. Note that for the OP-TEE case and in particular any subsequent bootloader that wants to use a device (e.g GRUB) will need to call exit_boot_services() in order to close the session.

Quoted 8 lines
Quoted 3 lines

Hello Ilias,

thanks for the patch. Adding the function calls looks correct to me,

Well the only doubt I have is what if GRUB has to extend some PCRs before calling Linux? Any idea if it's currently calling ExitBootSevices?
I was considering if it would be a better idea to call the device unbinding during some kind of "exit" from U-boot's EFI code. (i.e before StartImage)

ExitBootServices() is called by the Linux EFI stub in function allocate_new_fdt_and_exit_boot().

If GRUB would call ExitBootServices(), it would not be able to launch the EFI stub via StartImage().

Quoted 8 lines
Quoted 5 lines

Yea that's my point. So with the current patch, you won't be able to access the fTPM driver from GRUB
(or any other EFI application) until the Linux EFI stub calls exit boot services. Maybe calling those 2 functions in StartImage is a better idea?

Shouldn't an EFI application (such as GRUB) be using EFI protocols to access the TPM?

Yea I already responded to myself on the previous mail. We don't have support for the EFI protocol in U-Boot (yet), but cleaning up in exit boot services makes more sense. So I'll fix the compilation error and send a V2 with the DM removal as-is. GRUB will be able to access the TPM once we add the EFI protocols in U-boot.

Cheers
/Ilias