]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounter.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / MonotonicCounterRuntimeDxe / MonotonicCounter.c
CommitLineData
fb0b259e 1/** @file\r
636578d3 2 Produce the UEFI boot service GetNextMonotonicCount() and runtime service\r
3 GetNextHighMonotonicCount().\r
e70d34ca 4\r
d1102dba 5Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
e70d34ca 7\r
fb0b259e 8**/\r
e70d34ca 9\r
636578d3 10#include <Uefi.h>\r
e70d34ca 11\r
636578d3 12#include <Protocol/MonotonicCounter.h>\r
c8ad2d7a 13#include <Guid/MtcVendor.h>\r
636578d3 14\r
15#include <Library/BaseLib.h>\r
16#include <Library/UefiDriverEntryPoint.h>\r
17#include <Library/UefiRuntimeLib.h>\r
18#include <Library/DebugLib.h>\r
19#include <Library/UefiBootServicesTableLib.h>\r
20#include <Library/UefiRuntimeServicesTableLib.h>\r
e70d34ca 21\r
22//\r
636578d3 23// The handle to install Monotonic Counter Architctural Protocol\r
e70d34ca 24//\r
25EFI_HANDLE mMonotonicCounterHandle = NULL;\r
26\r
27//\r
636578d3 28// The current monotonic counter value\r
e70d34ca 29//\r
30UINT64 mEfiMtc;\r
31\r
32//\r
636578d3 33// Event to update the monotonic Counter's high part when low part overflows.\r
e70d34ca 34//\r
35EFI_EVENT mEfiMtcEvent;\r
36\r
52c7a544 37/**\r
636578d3 38 Returns a monotonically increasing count for the platform.\r
52c7a544 39\r
636578d3 40 This function returns a 64-bit value that is numerically larger then the last\r
41 time the function was called.\r
d089bd5f 42 The platform monotonic counter is comprised of two parts: the high 32 bits\r
636578d3 43 and the low 32 bits. The low 32-bit value is volatile and is reset to zero on\r
44 every system reset. It is increased by 1 on every call to GetNextMonotonicCount().\r
45 The high 32-bit value is nonvolatile and is increased by one on whenever the\r
46 system resets or the low 32-bit counter overflows.\r
52c7a544 47\r
d1102dba 48 @param Count Pointer to returned value.\r
52c7a544 49\r
636578d3 50 @retval EFI_SUCCESS The next monotonic count was returned.\r
51 @retval EFI_DEVICE_ERROR The device is not functioning properly.\r
52 @retval EFI_INVALID_PARAMETER Count is NULL.\r
53 @retval EFI_UNSUPPORTED This function is called at runtime.\r
52c7a544 54\r
55**/\r
e70d34ca 56EFI_STATUS\r
57EFIAPI\r
58MonotonicCounterDriverGetNextMonotonicCount (\r
59 OUT UINT64 *Count\r
60 )\r
e70d34ca 61{\r
62 EFI_TPL OldTpl;\r
63\r
64 //\r
636578d3 65 // Cannot be called after ExitBootServices()\r
e70d34ca 66 //\r
67 if (EfiAtRuntime ()) {\r
68 return EFI_UNSUPPORTED;\r
69 }\r
70 //\r
71 // Check input parameters\r
72 //\r
73 if (Count == NULL) {\r
74 return EFI_INVALID_PARAMETER;\r
75 }\r
76 //\r
77 // Update the monotonic counter with a lock\r
78 //\r
79 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
80 *Count = mEfiMtc;\r
81 mEfiMtc++;\r
82 gBS->RestoreTPL (OldTpl);\r
83\r
84 //\r
636578d3 85 // If the low 32-bit counter overflows (MSB bit toggled),\r
86 // then signal that the high part needs update now.\r
e70d34ca 87 //\r
636578d3 88 if ((((UINT32) mEfiMtc) ^ ((UINT32) *Count)) & BIT31) {\r
e70d34ca 89 gBS->SignalEvent (mEfiMtcEvent);\r
90 }\r
91\r
92 return EFI_SUCCESS;\r
93}\r
94\r
95\r
96/**\r
97 Returns the next high 32 bits of the platform's monotonic counter.\r
98\r
99 The GetNextHighMonotonicCount() function returns the next high 32 bits\r
100 of the platform's monotonic counter. The platform's monotonic counter is\r
101 comprised of two 32 bit quantities: the high 32 bits and the low 32 bits.\r
102 During boot service time the low 32 bit value is volatile: it is reset to\r
103 zero on every system reset and is increased by 1 on every call to GetNextMonotonicCount().\r
898552fd
SZ
104 The high 32 bit value is non-volatile and is increased by 1 whenever the system resets,\r
105 whenever GetNextHighMonotonicCount() is called, or whenever the low 32 bit count\r
106 (returned by GetNextMonoticCount()) overflows.\r
e70d34ca 107 The GetNextMonotonicCount() function is only available at boot services time.\r
108 If the operating system wishes to extend the platform monotonic counter to runtime,\r
109 it may do so by utilizing GetNextHighMonotonicCount(). To do this, before calling\r
110 ExitBootServices() the operating system would call GetNextMonotonicCount() to obtain\r
111 the current platform monotonic count. The operating system would then provide an\r
112 interface that returns the next count by:\r
113 Adding 1 to the last count.\r
114 Before the lower 32 bits of the count overflows, call GetNextHighMonotonicCount().\r
115 This will increase the high 32 bits of the platform's non-volatile portion of the monotonic\r
116 count by 1.\r
117\r
118 This function may only be called at Runtime.\r
119\r
d1102dba 120 @param HighCount Pointer to returned value.\r
e70d34ca 121\r
636578d3 122 @retval EFI_SUCCESS The next high monotonic count was returned.\r
123 @retval EFI_INVALID_PARAMETER HighCount is NULL.\r
124 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r
e70d34ca 125 @retval EFI_OUT_OF_RESOURCES If variable service reports that not enough storage\r
126 is available to hold the variable and its data.\r
e70d34ca 127\r
128**/\r
e70d34ca 129EFI_STATUS\r
130EFIAPI\r
131MonotonicCounterDriverGetNextHighMonotonicCount (\r
132 OUT UINT32 *HighCount\r
133 )\r
e70d34ca 134{\r
135 EFI_TPL OldTpl;\r
136\r
137 //\r
138 // Check input parameters\r
139 //\r
140 if (HighCount == NULL) {\r
141 return EFI_INVALID_PARAMETER;\r
142 }\r
143\r
144 if (!EfiAtRuntime ()) {\r
145 //\r
146 // Use a lock if called before ExitBootServices()\r
147 //\r
148 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
149 *HighCount = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r
150 mEfiMtc = LShiftU64 (*HighCount, 32);\r
151 gBS->RestoreTPL (OldTpl);\r
152 } else {\r
153 *HighCount = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r
154 mEfiMtc = LShiftU64 (*HighCount, 32);\r
155 }\r
156 //\r
636578d3 157 // Update the NV variable to match the new high part\r
e70d34ca 158 //\r
159 return EfiSetVariable (\r
c8ad2d7a
LG
160 MTC_VARIABLE_NAME,\r
161 &gMtcVendorGuid,\r
e70d34ca 162 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
163 sizeof (UINT32),\r
164 HighCount\r
165 );\r
166\r
167}\r
168\r
52c7a544 169/**\r
636578d3 170 Monotonic counter event handler. This handler updates the high part of monotonic counter.\r
52c7a544 171\r
172 @param Event The event to handle.\r
173 @param Context The event context.\r
174\r
52c7a544 175**/\r
e70d34ca 176VOID\r
177EFIAPI\r
178EfiMtcEventHandler (\r
179 IN EFI_EVENT Event,\r
180 IN VOID *Context\r
181 )\r
e70d34ca 182{\r
183 UINT32 HighCount;\r
184\r
185 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
186}\r
187\r
52c7a544 188/**\r
636578d3 189 Entry point of monotonic counter driver.\r
52c7a544 190\r
636578d3 191 @param ImageHandle The image handle of this driver.\r
192 @param SystemTable The pointer of EFI_SYSTEM_TABLE.\r
52c7a544 193\r
d41be01a 194 @retval EFI_SUCCESS The initialization is successful.\r
52c7a544 195\r
196**/\r
e70d34ca 197EFI_STATUS\r
198EFIAPI\r
199MonotonicCounterDriverInitialize (\r
200 IN EFI_HANDLE ImageHandle,\r
201 IN EFI_SYSTEM_TABLE *SystemTable\r
202 )\r
e70d34ca 203{\r
204 EFI_STATUS Status;\r
205 UINT32 HighCount;\r
206 UINTN BufferSize;\r
207\r
208 //\r
636578d3 209 // Make sure the Monotonic Counter Architectural Protocol has not been installed in the system yet.\r
e70d34ca 210 //\r
211 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);\r
212\r
213 //\r
636578d3 214 // Initialize event to handle low-part overflow\r
e70d34ca 215 //\r
216 Status = gBS->CreateEvent (\r
217 EVT_NOTIFY_SIGNAL,\r
218 TPL_CALLBACK,\r
219 EfiMtcEventHandler,\r
220 NULL,\r
221 &mEfiMtcEvent\r
222 );\r
e70d34ca 223 ASSERT_EFI_ERROR (Status);\r
224\r
225 //\r
226 // Read the last high part\r
227 //\r
228 BufferSize = sizeof (UINT32);\r
229 Status = EfiGetVariable (\r
c8ad2d7a
LG
230 MTC_VARIABLE_NAME,\r
231 &gMtcVendorGuid,\r
e70d34ca 232 NULL,\r
233 &BufferSize,\r
234 &HighCount\r
235 );\r
236 if (EFI_ERROR (Status)) {\r
237 HighCount = 0;\r
238 }\r
239 //\r
240 // Set the current value\r
241 //\r
242 mEfiMtc = LShiftU64 (HighCount, 32);\r
243\r
244 //\r
245 // Increment the upper 32 bits for this boot\r
246 // Continue even if it fails. It will only fail if the variable services are\r
247 // not functional.\r
248 //\r
636578d3 249 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
e70d34ca 250\r
251 //\r
252 // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields\r
253 //\r
254 gBS->GetNextMonotonicCount = MonotonicCounterDriverGetNextMonotonicCount;\r
255 gRT->GetNextHighMonotonicCount = MonotonicCounterDriverGetNextHighMonotonicCount;\r
256\r
257 //\r
258 // Install the Monotonic Counter Architctural Protocol onto a new handle\r
259 //\r
260 Status = gBS->InstallMultipleProtocolInterfaces (\r
261 &mMonotonicCounterHandle,\r
262 &gEfiMonotonicCounterArchProtocolGuid,\r
263 NULL,\r
264 NULL\r
265 );\r
266 ASSERT_EFI_ERROR (Status);\r
267\r
268 return EFI_SUCCESS;\r
269}\r