即使在安装它的驱动程序被卸载后,协议是否继续存在于内存中并可供其他驱动程序使用?

Does a protocol continue to be in memory and usable by other drivers even after the driver that installed it is unloaded?

如果我们有一个驱动程序在 EDK2/UEFI 中安装了一个协议,然后卸载了该驱动程序,该协议是否继续存在并可供其他驱动程序和服务使用? UEFI 应用程序的相同问题。 我主要想知道 UEFI 框架是否以某种方式将协议与安装它们的驱动程序联系起来,如果驱动程序被卸载,协议是否在图像被卸载后仍可由其他驱动程序使用,或者协议是否在驱动程序被卸载时退出内存。

我认为当然可以编写代码来实现这种情况,即当驱动程序退出时它会卸载它创建的协议,但是默认情况下这会在 UEFI/EDK2 内发生吗?或者仅当驱动程序在卸载协议时明确声明要卸载协议时才会发生这种情况?

此外,这里的行业最佳实践是什么?协议通常应该继续存在于安装它们的驱动程序之后,还是应该将两者链接起来?

抱歉,如果这个问题是高级别的。我正在尝试获得有关此主题的一些高级直觉。

驱动程序或应用程序需要在退出前卸载其协议并释放相关内存。它不是由 UEFI 核心自动完成的。

由于协议通常包含指向驱动程序中函数的指针,因此在协议仍然存在的情况下不可能卸载驱动程序。 (但是,可以孤立一个不包含任何函数指针的协议。)

(引用来自 UEFI 规范版本 2.8。)

7.4,EFI_IMAGE_ENTRY_POINT:

If the image supports dynamic unloading, it must supply an unload function in the EFI_LOADED_IMAGE_PROTOCOL structure before returning control from its entry point.

7.4 退出:

When an EFI boot service driver or runtime service driver exits, firmware frees the image only if the ExitStatus is an error code; otherwise the image stays resident in memory. The driver must not return an error code if it has installed any protocol handlers or other active callbacks into the system that have not (or cannot) be cleaned up. If the driver exits with an error code, it is responsible for freeing all resources before exiting.

7.4,卸载图像:

If the image has been started and has an Unload() entry point, control is passed to that entry point. If the image’s unload function returns EFI_SUCCESS, the image is unloaded; otherwise, the error returned by the image’s unload function is returned to the caller. The image unload function is responsible for freeing all allocated memory and ensuring that there are no references to any freed memory, or to the image itself, before returning EFI_SUCCESS.
If the image has been started and does not have an Unload() entry point, the function returns EFI_UNSUPPORTED.

4.7.3:

Any protocols installed or memory allocated ... must be uninstalled or freed [in the driver's Unload function].