]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Universal/MonotonicCounter/RuntimeDxe/MonotonicCounter.c
All runtime driver should use functions provided by UefiRuntimeLib library class...
[mirror_edk2.git] / EdkModulePkg / Universal / MonotonicCounter / RuntimeDxe / MonotonicCounter.c
CommitLineData
878ddf1f 1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 MonotonicCounter.c\r
15\r
16Abstract:\r
17\r
18 Produced the Monotonic Counter Services as defined in the DXE CIS\r
19\r
20Revision History:\r
21\r
22--*/\r
23\r
24#include "MonotonicCounter.h"\r
25\r
26//\r
27// The Monotonic Counter Handle\r
28//\r
29EFI_HANDLE mMonotonicCounterHandle = NULL;\r
30\r
31//\r
32// The current Monotonic count value\r
33//\r
34UINT64 mEfiMtc;\r
35\r
878ddf1f 36//\r
37// Event to use to update the Mtc's high part when wrapping\r
38//\r
39EFI_EVENT mEfiMtcEvent;\r
40\r
41//\r
42// EfiMtcName - Variable name of the MTC value\r
43//\r
44CHAR16 *mEfiMtcName = (CHAR16 *) L"MTC";\r
45\r
46//\r
47// EfiMtcGuid - Guid of the MTC value\r
48//\r
49EFI_GUID mEfiMtcGuid = { 0xeb704011, 0x1402, 0x11d3, { 0x8e, 0x77, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } };\r
50\r
51//\r
52// Worker functions\r
53//\r
54EFI_STATUS\r
55EFIAPI\r
56MonotonicCounterDriverGetNextMonotonicCount (\r
57 OUT UINT64 *Count\r
58 )\r
59/*++\r
60\r
61Routine Description:\r
62\r
63Arguments:\r
64\r
65Returns:\r
66\r
67--*/\r
68{\r
69 EFI_TPL OldTpl;\r
70\r
71 //\r
72 // Can not be called after ExitBootServices()\r
73 //\r
74 if (EfiAtRuntime ()) {\r
75 return EFI_UNSUPPORTED;\r
76 }\r
77 //\r
78 // Check input parameters\r
79 //\r
80 if (Count == NULL) {\r
81 return EFI_INVALID_PARAMETER;\r
82 }\r
83 //\r
84 // Update the monotonic counter with a lock\r
85 //\r
86 OldTpl = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);\r
87 *Count = mEfiMtc;\r
88 mEfiMtc++;\r
89 gBS->RestoreTPL (OldTpl);\r
90\r
91 //\r
92 // If the MSB bit of the low part toggled, then signal that the high\r
93 // part needs updated now\r
94 //\r
95 if ((((UINT32) mEfiMtc) ^ ((UINT32) *Count)) & 0x80000000) {\r
96 gBS->SignalEvent (mEfiMtcEvent);\r
97 }\r
98\r
99 return EFI_SUCCESS;\r
100}\r
101\r
2f23473f 102\r
2f23473f 103/**\r
88c8537c 104 Returns the next high 32 bits of the platform's monotonic counter.\r
105\r
106 The GetNextHighMonotonicCount() function returns the next high 32 bits \r
107 of the platform's monotonic counter. The platform's monotonic counter is \r
108 comprised of two 32 bit quantities: the high 32 bits and the low 32 bits. \r
109 During boot service time the low 32 bit value is volatile: it is reset to \r
110 zero on every system reset and is increased by 1 on every call to GetNextMonotonicCount().\r
111 The high 32 bit value is non-volatile and is increased by 1 whenever the system resets \r
112 or whenever the low 32 bit count [returned by GetNextMonoticCount()] overflows.\r
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
126 @param[out] HighCount Pointer to returned value.\r
127\r
128 @retval EFI_INVALID_PARAMETER If HighCount is NULL.\r
129 @retval EFI_SUCCESS Operation is successful.\r
130 @retval EFI_OUT_OF_RESOURCES If variable service reports that not enough storage \r
131 is available to hold the variable and its data.\r
132 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r
2f23473f 133\r
134**/\r
878ddf1f 135EFI_STATUS\r
136EFIAPI\r
137MonotonicCounterDriverGetNextHighMonotonicCount (\r
138 OUT UINT32 *HighCount\r
139 )\r
140/*++\r
141\r
142Routine Description:\r
143\r
144Arguments:\r
145\r
146Returns:\r
147\r
148--*/\r
149{\r
878ddf1f 150 EFI_TPL OldTpl;\r
151\r
152 //\r
153 // Check input parameters\r
154 //\r
155 if (HighCount == NULL) {\r
156 return EFI_INVALID_PARAMETER;\r
157 }\r
158\r
159 if (!EfiAtRuntime ()) {\r
160 //\r
161 // Use a lock if called before ExitBootServices()\r
162 //\r
163 OldTpl = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);\r
164 *HighCount = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r
165 mEfiMtc = LShiftU64 (*HighCount, 32);\r
166 gBS->RestoreTPL (OldTpl);\r
167 } else {\r
168 *HighCount = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r
169 mEfiMtc = LShiftU64 (*HighCount, 32);\r
170 }\r
171 //\r
172 // Update the NvRam store to match the new high part\r
173 //\r
88c8537c 174 return EfiSetVariable (\r
175 mEfiMtcName,\r
176 &mEfiMtcGuid,\r
177 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
178 sizeof (UINT32),\r
179 HighCount\r
180 );\r
181\r
878ddf1f 182}\r
183\r
184VOID\r
185EFIAPI\r
186EfiMtcEventHandler (\r
187 IN EFI_EVENT Event,\r
188 IN VOID *Context\r
189 )\r
190/*++\r
191\r
192Routine Description:\r
193\r
194 Monotonic count event handler. This handler updates the high monotonic count.\r
195\r
196Arguments:\r
197\r
198 Event The event to handle\r
199 Context The event context\r
200\r
201Returns:\r
202\r
203 EFI_SUCCESS The event has been handled properly \r
204 EFI_NOT_FOUND An error occurred updating the variable.\r
205\r
206--*/\r
207{\r
208 UINT32 HighCount;\r
209\r
210 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
211}\r
212\r
213EFI_STATUS\r
214EFIAPI\r
215MonotonicCounterDriverInitialize (\r
216 IN EFI_HANDLE ImageHandle,\r
217 IN EFI_SYSTEM_TABLE *SystemTable\r
218 )\r
219/*++\r
220\r
221Routine Description:\r
222\r
223Arguments:\r
224 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)\r
225\r
226Returns:\r
227\r
228--*/\r
229{\r
230 EFI_STATUS Status;\r
231 UINT32 HighCount;\r
232 UINTN BufferSize;\r
233\r
234 //\r
235 // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system\r
236 //\r
237 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);\r
238\r
239 //\r
240 // Initialize event to handle overflows\r
241 //\r
242 Status = gBS->CreateEvent (\r
243 EFI_EVENT_NOTIFY_SIGNAL,\r
244 EFI_TPL_CALLBACK,\r
245 EfiMtcEventHandler,\r
246 NULL,\r
247 &mEfiMtcEvent\r
248 );\r
249\r
250 ASSERT_EFI_ERROR (Status);\r
251\r
252 //\r
253 // Read the last high part\r
254 //\r
255 BufferSize = sizeof (UINT32);\r
256 Status = gRT->GetVariable (\r
257 mEfiMtcName,\r
258 &mEfiMtcGuid,\r
259 NULL,\r
260 &BufferSize,\r
261 &HighCount\r
262 );\r
263 if (EFI_ERROR (Status)) {\r
264 HighCount = 0;\r
265 }\r
266 //\r
267 // Set the current value\r
268 //\r
269 mEfiMtc = LShiftU64 (HighCount, 32);\r
270\r
271 //\r
272 // Increment the upper 32 bits for this boot\r
273 // Continue even if it fails. It will only fail if the variable services are\r
274 // not functional.\r
275 //\r
276 Status = MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
277\r
278 //\r
279 // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields\r
280 //\r
281 gBS->GetNextMonotonicCount = MonotonicCounterDriverGetNextMonotonicCount;\r
282 gRT->GetNextHighMonotonicCount = MonotonicCounterDriverGetNextHighMonotonicCount;\r
283\r
284 //\r
285 // Install the Monotonic Counter Architctural Protocol onto a new handle\r
286 //\r
287 Status = gBS->InstallMultipleProtocolInterfaces (\r
288 &mMonotonicCounterHandle,\r
289 &gEfiMonotonicCounterArchProtocolGuid,\r
290 NULL,\r
291 NULL\r
292 );\r
293 ASSERT_EFI_ERROR (Status);\r
294\r
295 return EFI_SUCCESS;\r
296}\r