+ mMorLockInitializationRequired = TRUE;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Delayed initialization for MOR Control Lock at EndOfDxe.\r
+\r
+ This function performs any operations queued by MorLockInit().\r
+**/\r
+VOID\r
+MorLockInitAtEndOfDxe (\r
+ VOID\r
+ )\r
+{\r
+ UINTN MorSize;\r
+ EFI_STATUS MorStatus;\r
+ EFI_STATUS TcgStatus;\r
+ VOID *TcgInterface;\r
+\r
+ if (!mMorLockInitializationRequired) {\r
+ //\r
+ // The EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL has never been installed, thus\r
+ // the variable write service is unavailable. This should never happen.\r
+ //\r
+ ASSERT (FALSE);\r
+ return;\r
+ }\r
+\r
+ //\r
+ // Check if the MOR variable exists.\r
+ //\r
+ MorSize = 0;\r
+ MorStatus = VariableServiceGetVariable (\r
+ MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
+ &gEfiMemoryOverwriteControlDataGuid,\r
+ NULL, // Attributes\r
+ &MorSize,\r
+ NULL // Data\r
+ );\r
+ //\r
+ // We provided a zero-sized buffer, so the above call can never succeed.\r
+ //\r
+ ASSERT (EFI_ERROR (MorStatus));\r
+\r
+ if (MorStatus == EFI_BUFFER_TOO_SMALL) {\r
+ //\r
+ // The MOR variable exists.\r
+ //\r
+ // Some OSes don't follow the TCG's Platform Reset Attack Mitigation spec\r
+ // in that the OS should never create the MOR variable, only read and write\r
+ // it -- these OSes (unintentionally) create MOR if the platform firmware\r
+ // does not produce it. Whether this is the case (from the last OS boot)\r
+ // can be deduced from the absence of the TCG / TCG2 protocols, as edk2's\r
+ // MOR implementation depends on (one of) those protocols.\r
+ //\r
+ TcgStatus = gBS->LocateProtocol (\r
+ &gEfiTcgProtocolGuid,\r
+ NULL, // Registration\r
+ &TcgInterface\r
+ );\r
+ if (EFI_ERROR (TcgStatus)) {\r
+ TcgStatus = gBS->LocateProtocol (\r
+ &gEfiTcg2ProtocolGuid,\r
+ NULL, // Registration\r
+ &TcgInterface\r
+ );\r
+ }\r
+\r
+ if (!EFI_ERROR (TcgStatus)) {\r
+ //\r
+ // The MOR variable originates from the platform firmware; set the MOR\r
+ // Control Lock variable to report the locking capability to the OS.\r
+ //\r
+ SetMorLockVariable (0);\r
+ return;\r
+ }\r
+\r
+ //\r
+ // The MOR variable's origin is inexplicable; delete it.\r
+ //\r
+ DEBUG ((\r
+ DEBUG_WARN,\r
+ "%a: deleting unexpected / unsupported variable %g:%s\n",\r
+ __FUNCTION__,\r
+ &gEfiMemoryOverwriteControlDataGuid,\r
+ MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME\r
+ ));\r
+\r
+ mMorPassThru = TRUE;\r
+ VariableServiceSetVariable (\r
+ MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
+ &gEfiMemoryOverwriteControlDataGuid,\r
+ 0, // Attributes\r
+ 0, // DataSize\r
+ NULL // Data\r
+ );\r
+ mMorPassThru = FALSE;\r
+ }\r
+\r
+ //\r
+ // The MOR variable is absent; the platform firmware does not support it.\r
+ // Lock the variable so that no other module may create it.\r
+ //\r
+ VariableLockRequestToLock (\r
+ NULL, // This\r
+ MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
+ &gEfiMemoryOverwriteControlDataGuid\r
+ );\r
+\r