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