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