]>
Commit | Line | Data |
---|---|---|
fb0b259e | 1 | /** @file\r |
636578d3 | 2 | Produce the UEFI boot service GetNextMonotonicCount() and runtime service\r |
3 | GetNextHighMonotonicCount().\r | |
e70d34ca | 4 | \r |
e5eed7d3 HT |
5 | Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>\r |
6 | This program and the accompanying materials\r | |
e70d34ca | 7 | are licensed and made available under the terms and conditions of the BSD License\r |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT 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 |
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 | |
e70d34ca | 26 | \r |
27 | //\r | |
636578d3 | 28 | // The handle to install Monotonic Counter Architctural Protocol\r |
e70d34ca | 29 | //\r |
30 | EFI_HANDLE mMonotonicCounterHandle = NULL;\r | |
31 | \r | |
32 | //\r | |
636578d3 | 33 | // The current monotonic counter value\r |
e70d34ca | 34 | //\r |
35 | UINT64 mEfiMtc;\r | |
36 | \r | |
37 | //\r | |
636578d3 | 38 | // Event to update the monotonic Counter's high part when low part overflows.\r |
e70d34ca | 39 | //\r |
40 | EFI_EVENT mEfiMtcEvent;\r | |
41 | \r | |
42 | //\r | |
636578d3 | 43 | // Name of the variable for the high part of monotonic counter\r |
e70d34ca | 44 | //\r |
45 | CHAR16 *mEfiMtcName = (CHAR16 *) L"MTC";\r | |
46 | \r | |
47 | //\r | |
636578d3 | 48 | // Vendor GUID of the variable for the high part of monotonic counter\r |
e70d34ca | 49 | //\r |
50 | EFI_GUID mEfiMtcGuid = { 0xeb704011, 0x1402, 0x11d3, { 0x8e, 0x77, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } };\r | |
51 | \r | |
52c7a544 | 52 | /**\r |
636578d3 | 53 | Returns a monotonically increasing count for the platform.\r |
52c7a544 | 54 | \r |
636578d3 | 55 | This function returns a 64-bit value that is numerically larger then the last\r |
56 | time the function was called.\r | |
d089bd5f | 57 | The platform monotonic counter is comprised of two parts: the high 32 bits\r |
636578d3 | 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 | |
52c7a544 | 62 | \r |
63 | @param Count Pointer to returned value.\r | |
64 | \r | |
636578d3 | 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 | |
52c7a544 | 69 | \r |
70 | **/\r | |
e70d34ca | 71 | EFI_STATUS\r |
72 | EFIAPI\r | |
73 | MonotonicCounterDriverGetNextMonotonicCount (\r | |
74 | OUT UINT64 *Count\r | |
75 | )\r | |
e70d34ca | 76 | {\r |
77 | EFI_TPL OldTpl;\r | |
78 | \r | |
79 | //\r | |
636578d3 | 80 | // Cannot be called after ExitBootServices()\r |
e70d34ca | 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 | |
636578d3 | 100 | // If the low 32-bit counter overflows (MSB bit toggled),\r |
101 | // then signal that the high part needs update now.\r | |
e70d34ca | 102 | //\r |
636578d3 | 103 | if ((((UINT32) mEfiMtc) ^ ((UINT32) *Count)) & BIT31) {\r |
e70d34ca | 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 | |
52c7a544 | 134 | @param HighCount Pointer to returned value.\r |
e70d34ca | 135 | \r |
636578d3 | 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 | |
e70d34ca | 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 | |
e70d34ca | 141 | \r |
142 | **/\r | |
e70d34ca | 143 | EFI_STATUS\r |
144 | EFIAPI\r | |
145 | MonotonicCounterDriverGetNextHighMonotonicCount (\r | |
146 | OUT UINT32 *HighCount\r | |
147 | )\r | |
e70d34ca | 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 | |
636578d3 | 171 | // Update the NV variable to match the new high part\r |
e70d34ca | 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 | |
52c7a544 | 183 | /**\r |
636578d3 | 184 | Monotonic counter event handler. This handler updates the high part of monotonic counter.\r |
52c7a544 | 185 | \r |
186 | @param Event The event to handle.\r | |
187 | @param Context The event context.\r | |
188 | \r | |
52c7a544 | 189 | **/\r |
e70d34ca | 190 | VOID\r |
191 | EFIAPI\r | |
192 | EfiMtcEventHandler (\r | |
193 | IN EFI_EVENT Event,\r | |
194 | IN VOID *Context\r | |
195 | )\r | |
e70d34ca | 196 | {\r |
197 | UINT32 HighCount;\r | |
198 | \r | |
199 | MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r | |
200 | }\r | |
201 | \r | |
52c7a544 | 202 | /**\r |
636578d3 | 203 | Entry point of monotonic counter driver.\r |
52c7a544 | 204 | \r |
636578d3 | 205 | @param ImageHandle The image handle of this driver.\r |
206 | @param SystemTable The pointer of EFI_SYSTEM_TABLE.\r | |
52c7a544 | 207 | \r |
d41be01a | 208 | @retval EFI_SUCCESS The initialization is successful.\r |
52c7a544 | 209 | \r |
210 | **/\r | |
e70d34ca | 211 | EFI_STATUS\r |
212 | EFIAPI\r | |
213 | MonotonicCounterDriverInitialize (\r | |
214 | IN EFI_HANDLE ImageHandle,\r | |
215 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
216 | )\r | |
e70d34ca | 217 | {\r |
218 | EFI_STATUS Status;\r | |
219 | UINT32 HighCount;\r | |
220 | UINTN BufferSize;\r | |
221 | \r | |
222 | //\r | |
636578d3 | 223 | // Make sure the Monotonic Counter Architectural Protocol has not been installed in the system yet.\r |
e70d34ca | 224 | //\r |
225 | ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);\r | |
226 | \r | |
227 | //\r | |
636578d3 | 228 | // Initialize event to handle low-part overflow\r |
e70d34ca | 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 | |
e70d34ca | 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 | |
636578d3 | 263 | MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r |
e70d34ca | 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 |