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