TcgMorLock.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /** @file
  2. TCG MOR (Memory Overwrite Request) Lock Control Driver.
  3. This driver initializes MemoryOverwriteRequestControlLock variable.
  4. This module will add Variable Hook and allow MemoryOverwriteRequestControlLock variable set only once.
  5. Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include <PiDxe.h>
  9. #include <Guid/MemoryOverwriteControl.h>
  10. #include <IndustryStandard/MemoryOverwriteRequestControlLock.h>
  11. #include <Library/DebugLib.h>
  12. #include <Library/BaseLib.h>
  13. #include <Library/BaseMemoryLib.h>
  14. #include "TcgMorLock.h"
  15. typedef struct {
  16. CHAR16 *VariableName;
  17. EFI_GUID *VendorGuid;
  18. } VARIABLE_TYPE;
  19. VARIABLE_TYPE mMorVariableType[] = {
  20. {MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME, &gEfiMemoryOverwriteControlDataGuid},
  21. {MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME, &gEfiMemoryOverwriteRequestControlLockGuid},
  22. };
  23. /**
  24. Returns if this is MOR related variable.
  25. @param VariableName the name of the vendor's variable, it's a Null-Terminated Unicode String
  26. @param VendorGuid Unify identifier for vendor.
  27. @retval TRUE The variable is MOR related.
  28. @retval FALSE The variable is NOT MOR related.
  29. **/
  30. BOOLEAN
  31. IsAnyMorVariable (
  32. IN CHAR16 *VariableName,
  33. IN EFI_GUID *VendorGuid
  34. )
  35. {
  36. UINTN Index;
  37. for (Index = 0; Index < sizeof(mMorVariableType)/sizeof(mMorVariableType[0]); Index++) {
  38. if ((StrCmp (VariableName, mMorVariableType[Index].VariableName) == 0) &&
  39. (CompareGuid (VendorGuid, mMorVariableType[Index].VendorGuid))) {
  40. return TRUE;
  41. }
  42. }
  43. return FALSE;
  44. }
  45. /**
  46. Returns if this is MOR lock variable.
  47. @param VariableName the name of the vendor's variable, it's a Null-Terminated Unicode String
  48. @param VendorGuid Unify identifier for vendor.
  49. @retval TRUE The variable is MOR lock variable.
  50. @retval FALSE The variable is NOT MOR lock variable.
  51. **/
  52. BOOLEAN
  53. IsMorLockVariable (
  54. IN CHAR16 *VariableName,
  55. IN EFI_GUID *VendorGuid
  56. )
  57. {
  58. if ((StrCmp (VariableName, MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME) == 0) &&
  59. (CompareGuid (VendorGuid, &gEfiMemoryOverwriteRequestControlLockGuid))) {
  60. return TRUE;
  61. }
  62. return FALSE;
  63. }
  64. /**
  65. This service is a checker handler for the UEFI Runtime Service SetVariable()
  66. @param VariableName the name of the vendor's variable, as a
  67. Null-Terminated Unicode String
  68. @param VendorGuid Unify identifier for vendor.
  69. @param Attributes Point to memory location to return the attributes of variable. If the point
  70. is NULL, the parameter would be ignored.
  71. @param DataSize The size in bytes of Data-Buffer.
  72. @param Data Point to the content of the variable.
  73. @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
  74. defined by the Attributes.
  75. @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the
  76. DataSize exceeds the maximum allowed.
  77. @retval EFI_INVALID_PARAMETER VariableName is an empty Unicode string.
  78. @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
  79. @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.
  80. @retval EFI_WRITE_PROTECTED The variable in question is read-only.
  81. @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
  82. @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
  83. set but the AuthInfo does NOT pass the validation check carried
  84. out by the firmware.
  85. @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
  86. **/
  87. EFI_STATUS
  88. EFIAPI
  89. SetVariableCheckHandlerMor (
  90. IN CHAR16 *VariableName,
  91. IN EFI_GUID *VendorGuid,
  92. IN UINT32 Attributes,
  93. IN UINTN DataSize,
  94. IN VOID *Data
  95. )
  96. {
  97. UINTN MorLockDataSize;
  98. BOOLEAN MorLock;
  99. EFI_STATUS Status;
  100. //
  101. // do not handle non-MOR variable
  102. //
  103. if (!IsAnyMorVariable (VariableName, VendorGuid)) {
  104. return EFI_SUCCESS;
  105. }
  106. MorLockDataSize = sizeof(MorLock);
  107. Status = InternalGetVariable (
  108. MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME,
  109. &gEfiMemoryOverwriteRequestControlLockGuid,
  110. NULL,
  111. &MorLockDataSize,
  112. &MorLock
  113. );
  114. if (!EFI_ERROR (Status) && MorLock) {
  115. //
  116. // If lock, deny access
  117. //
  118. return EFI_INVALID_PARAMETER;
  119. }
  120. //
  121. // Delete not OK
  122. //
  123. if ((DataSize != sizeof(UINT8)) || (Data == NULL) || (Attributes == 0)) {
  124. return EFI_INVALID_PARAMETER;
  125. }
  126. //
  127. // check format
  128. //
  129. if (IsMorLockVariable(VariableName, VendorGuid)) {
  130. //
  131. // set to any other value not OK
  132. //
  133. if ((*(UINT8 *)Data != 1) && (*(UINT8 *)Data != 0)) {
  134. return EFI_INVALID_PARAMETER;
  135. }
  136. }
  137. //
  138. // Or grant access
  139. //
  140. return EFI_SUCCESS;
  141. }
  142. /**
  143. Entry Point for MOR Lock Control driver.
  144. @param[in] ImageHandle Image handle of this driver.
  145. @param[in] SystemTable A Pointer to the EFI System Table.
  146. @retval EFI_SUCCESS
  147. @return Others Some error occurs.
  148. **/
  149. EFI_STATUS
  150. EFIAPI
  151. MorLockDriverInit (
  152. VOID
  153. )
  154. {
  155. EFI_STATUS Status;
  156. UINT8 Data;
  157. Data = 0;
  158. Status = InternalSetVariable (
  159. MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME,
  160. &gEfiMemoryOverwriteRequestControlLockGuid,
  161. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  162. 1,
  163. &Data
  164. );
  165. return Status;
  166. }