diff --git a/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.c b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.c index 81c44eece9..e24cc834c2 100644 --- a/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.c +++ b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.c @@ -16,14 +16,26 @@ #include #include "DebugLibDetect.h" +// +// Set to TRUE if the debug I/O port has been checked +// +STATIC BOOLEAN mDebugIoPortChecked = FALSE; + // // Set to TRUE if the debug I/O port is enabled // STATIC BOOLEAN mDebugIoPortFound = FALSE; /** - This constructor function checks if the debug I/O port device is present, - caching the result for later use. + This constructor function must not do anything. + + Some modules consuming this library instance, such as the DXE Core, invoke + the DEBUG() macro before they explicitly call + ProcessLibraryConstructorList(). Therefore the auto-generated call from + ProcessLibraryConstructorList() to this constructor function may be preceded + by some calls to PlatformDebugLibIoPortFound() below. Hence + PlatformDebugLibIoPortFound() must not rely on anything this constructor + could set up. @retval RETURN_SUCCESS The constructor always returns RETURN_SUCCESS. @@ -34,12 +46,12 @@ PlatformDebugLibIoPortConstructor ( VOID ) { - mDebugIoPortFound = PlatformDebugLibIoPortDetect(); return RETURN_SUCCESS; } /** - Return the cached result of detecting the debug I/O port device. + At the first call, check if the debug I/O port device is present, and cache + the result for later use. At subsequent calls, return the cached result. @retval TRUE if the debug I/O port device was detected. @retval FALSE otherwise @@ -51,5 +63,9 @@ PlatformDebugLibIoPortFound ( VOID ) { + if (!mDebugIoPortChecked) { + mDebugIoPortFound = PlatformDebugLibIoPortDetect (); + mDebugIoPortChecked = TRUE; + } return mDebugIoPortFound; }