]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Universal/MonotonicCounter/RuntimeDxe/MonotonicCounter.c
Initial import.
[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
36\r
37//\r
38// Event to use to update the Mtc's high part when wrapping\r
39//\r
40EFI_EVENT mEfiMtcEvent;\r
41\r
42//\r
43// EfiMtcName - Variable name of the MTC value\r
44//\r
45CHAR16 *mEfiMtcName = (CHAR16 *) L"MTC";\r
46\r
47//\r
48// EfiMtcGuid - Guid of the MTC value\r
49//\r
50EFI_GUID mEfiMtcGuid = { 0xeb704011, 0x1402, 0x11d3, { 0x8e, 0x77, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } };\r
51\r
52//\r
53// Worker functions\r
54//\r
55EFI_STATUS\r
56EFIAPI\r
57MonotonicCounterDriverGetNextMonotonicCount (\r
58 OUT UINT64 *Count\r
59 )\r
60/*++\r
61\r
62Routine Description:\r
63\r
64Arguments:\r
65\r
66Returns:\r
67\r
68--*/\r
69{\r
70 EFI_TPL OldTpl;\r
71\r
72 //\r
73 // Can not be called after ExitBootServices()\r
74 //\r
75 if (EfiAtRuntime ()) {\r
76 return EFI_UNSUPPORTED;\r
77 }\r
78 //\r
79 // Check input parameters\r
80 //\r
81 if (Count == NULL) {\r
82 return EFI_INVALID_PARAMETER;\r
83 }\r
84 //\r
85 // Update the monotonic counter with a lock\r
86 //\r
87 OldTpl = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);\r
88 *Count = mEfiMtc;\r
89 mEfiMtc++;\r
90 gBS->RestoreTPL (OldTpl);\r
91\r
92 //\r
93 // If the MSB bit of the low part toggled, then signal that the high\r
94 // part needs updated now\r
95 //\r
96 if ((((UINT32) mEfiMtc) ^ ((UINT32) *Count)) & 0x80000000) {\r
97 gBS->SignalEvent (mEfiMtcEvent);\r
98 }\r
99\r
100 return EFI_SUCCESS;\r
101}\r
102\r
103EFI_STATUS\r
104EFIAPI\r
105MonotonicCounterDriverGetNextHighMonotonicCount (\r
106 OUT UINT32 *HighCount\r
107 )\r
108/*++\r
109\r
110Routine Description:\r
111\r
112Arguments:\r
113\r
114Returns:\r
115\r
116--*/\r
117{\r
118 EFI_STATUS Status;\r
119 EFI_TPL OldTpl;\r
120\r
121 //\r
122 // Check input parameters\r
123 //\r
124 if (HighCount == NULL) {\r
125 return EFI_INVALID_PARAMETER;\r
126 }\r
127\r
128 if (!EfiAtRuntime ()) {\r
129 //\r
130 // Use a lock if called before ExitBootServices()\r
131 //\r
132 OldTpl = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);\r
133 *HighCount = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r
134 mEfiMtc = LShiftU64 (*HighCount, 32);\r
135 gBS->RestoreTPL (OldTpl);\r
136 } else {\r
137 *HighCount = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r
138 mEfiMtc = LShiftU64 (*HighCount, 32);\r
139 }\r
140 //\r
141 // Update the NvRam store to match the new high part\r
142 //\r
143 Status = gRT->SetVariable (\r
144 mEfiMtcName,\r
145 &mEfiMtcGuid,\r
146 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
147 sizeof (UINT32),\r
148 HighCount\r
149 );\r
150\r
151 return Status;\r
152}\r
153\r
154VOID\r
155EFIAPI\r
156EfiMtcEventHandler (\r
157 IN EFI_EVENT Event,\r
158 IN VOID *Context\r
159 )\r
160/*++\r
161\r
162Routine Description:\r
163\r
164 Monotonic count event handler. This handler updates the high monotonic count.\r
165\r
166Arguments:\r
167\r
168 Event The event to handle\r
169 Context The event context\r
170\r
171Returns:\r
172\r
173 EFI_SUCCESS The event has been handled properly \r
174 EFI_NOT_FOUND An error occurred updating the variable.\r
175\r
176--*/\r
177{\r
178 UINT32 HighCount;\r
179\r
180 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
181}\r
182\r
183EFI_STATUS\r
184EFIAPI\r
185MonotonicCounterDriverInitialize (\r
186 IN EFI_HANDLE ImageHandle,\r
187 IN EFI_SYSTEM_TABLE *SystemTable\r
188 )\r
189/*++\r
190\r
191Routine Description:\r
192\r
193Arguments:\r
194 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)\r
195\r
196Returns:\r
197\r
198--*/\r
199{\r
200 EFI_STATUS Status;\r
201 UINT32 HighCount;\r
202 UINTN BufferSize;\r
203\r
204 //\r
205 // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system\r
206 //\r
207 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);\r
208\r
209 //\r
210 // Initialize event to handle overflows\r
211 //\r
212 Status = gBS->CreateEvent (\r
213 EFI_EVENT_NOTIFY_SIGNAL,\r
214 EFI_TPL_CALLBACK,\r
215 EfiMtcEventHandler,\r
216 NULL,\r
217 &mEfiMtcEvent\r
218 );\r
219\r
220 ASSERT_EFI_ERROR (Status);\r
221\r
222 //\r
223 // Read the last high part\r
224 //\r
225 BufferSize = sizeof (UINT32);\r
226 Status = gRT->GetVariable (\r
227 mEfiMtcName,\r
228 &mEfiMtcGuid,\r
229 NULL,\r
230 &BufferSize,\r
231 &HighCount\r
232 );\r
233 if (EFI_ERROR (Status)) {\r
234 HighCount = 0;\r
235 }\r
236 //\r
237 // Set the current value\r
238 //\r
239 mEfiMtc = LShiftU64 (HighCount, 32);\r
240\r
241 //\r
242 // Increment the upper 32 bits for this boot\r
243 // Continue even if it fails. It will only fail if the variable services are\r
244 // not functional.\r
245 //\r
246 Status = MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
247\r
248 //\r
249 // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields\r
250 //\r
251 gBS->GetNextMonotonicCount = MonotonicCounterDriverGetNextMonotonicCount;\r
252 gRT->GetNextHighMonotonicCount = MonotonicCounterDriverGetNextHighMonotonicCount;\r
253\r
254 //\r
255 // Install the Monotonic Counter Architctural Protocol onto a new handle\r
256 //\r
257 Status = gBS->InstallMultipleProtocolInterfaces (\r
258 &mMonotonicCounterHandle,\r
259 &gEfiMonotonicCounterArchProtocolGuid,\r
260 NULL,\r
261 NULL\r
262 );\r
263 ASSERT_EFI_ERROR (Status);\r
264\r
265 return EFI_SUCCESS;\r
266}\r