]>
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 |
c8ad2d7a | 5 | Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r |
e5eed7d3 | 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 |
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 |
31 | EFI_HANDLE mMonotonicCounterHandle = NULL;\r | |
32 | \r | |
33 | //\r | |
636578d3 | 34 | // The current monotonic counter value\r |
e70d34ca | 35 | //\r |
36 | UINT64 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 |
41 | EFI_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 | 62 | EFI_STATUS\r |
63 | EFIAPI\r | |
64 | MonotonicCounterDriverGetNextMonotonicCount (\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 | |
110 | The high 32 bit value is non-volatile and is increased by 1 whenever the system resets\r | |
111 | or whenever the low 32 bit count [returned by GetNextMonoticCount()] overflows.\r | |
112 | The GetNextMonotonicCount() function is only available at boot services time.\r | |
113 | If the operating system wishes to extend the platform monotonic counter to runtime,\r | |
114 | it may do so by utilizing GetNextHighMonotonicCount(). To do this, before calling\r | |
115 | ExitBootServices() the operating system would call GetNextMonotonicCount() to obtain\r | |
116 | the current platform monotonic count. The operating system would then provide an\r | |
117 | interface that returns the next count by:\r | |
118 | Adding 1 to the last count.\r | |
119 | Before the lower 32 bits of the count overflows, call GetNextHighMonotonicCount().\r | |
120 | This will increase the high 32 bits of the platform's non-volatile portion of the monotonic\r | |
121 | count by 1.\r | |
122 | \r | |
123 | This function may only be called at Runtime.\r | |
124 | \r | |
52c7a544 | 125 | @param HighCount Pointer to returned value.\r |
e70d34ca | 126 | \r |
636578d3 | 127 | @retval EFI_SUCCESS The next high monotonic count was returned.\r |
128 | @retval EFI_INVALID_PARAMETER HighCount is NULL.\r | |
129 | @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r | |
e70d34ca | 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 | |
e70d34ca | 132 | \r |
133 | **/\r | |
e70d34ca | 134 | EFI_STATUS\r |
135 | EFIAPI\r | |
136 | MonotonicCounterDriverGetNextHighMonotonicCount (\r | |
137 | OUT UINT32 *HighCount\r | |
138 | )\r | |
e70d34ca | 139 | {\r |
140 | EFI_TPL OldTpl;\r | |
141 | \r | |
142 | //\r | |
143 | // Check input parameters\r | |
144 | //\r | |
145 | if (HighCount == NULL) {\r | |
146 | return EFI_INVALID_PARAMETER;\r | |
147 | }\r | |
148 | \r | |
149 | if (!EfiAtRuntime ()) {\r | |
150 | //\r | |
151 | // Use a lock if called before ExitBootServices()\r | |
152 | //\r | |
153 | OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
154 | *HighCount = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r | |
155 | mEfiMtc = LShiftU64 (*HighCount, 32);\r | |
156 | gBS->RestoreTPL (OldTpl);\r | |
157 | } else {\r | |
158 | *HighCount = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r | |
159 | mEfiMtc = LShiftU64 (*HighCount, 32);\r | |
160 | }\r | |
161 | //\r | |
636578d3 | 162 | // Update the NV variable to match the new high part\r |
e70d34ca | 163 | //\r |
164 | return EfiSetVariable (\r | |
c8ad2d7a LG |
165 | MTC_VARIABLE_NAME,\r |
166 | &gMtcVendorGuid,\r | |
e70d34ca | 167 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r |
168 | sizeof (UINT32),\r | |
169 | HighCount\r | |
170 | );\r | |
171 | \r | |
172 | }\r | |
173 | \r | |
52c7a544 | 174 | /**\r |
636578d3 | 175 | Monotonic counter event handler. This handler updates the high part of monotonic counter.\r |
52c7a544 | 176 | \r |
177 | @param Event The event to handle.\r | |
178 | @param Context The event context.\r | |
179 | \r | |
52c7a544 | 180 | **/\r |
e70d34ca | 181 | VOID\r |
182 | EFIAPI\r | |
183 | EfiMtcEventHandler (\r | |
184 | IN EFI_EVENT Event,\r | |
185 | IN VOID *Context\r | |
186 | )\r | |
e70d34ca | 187 | {\r |
188 | UINT32 HighCount;\r | |
189 | \r | |
190 | MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r | |
191 | }\r | |
192 | \r | |
52c7a544 | 193 | /**\r |
636578d3 | 194 | Entry point of monotonic counter driver.\r |
52c7a544 | 195 | \r |
636578d3 | 196 | @param ImageHandle The image handle of this driver.\r |
197 | @param SystemTable The pointer of EFI_SYSTEM_TABLE.\r | |
52c7a544 | 198 | \r |
d41be01a | 199 | @retval EFI_SUCCESS The initialization is successful.\r |
52c7a544 | 200 | \r |
201 | **/\r | |
e70d34ca | 202 | EFI_STATUS\r |
203 | EFIAPI\r | |
204 | MonotonicCounterDriverInitialize (\r | |
205 | IN EFI_HANDLE ImageHandle,\r | |
206 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
207 | )\r | |
e70d34ca | 208 | {\r |
209 | EFI_STATUS Status;\r | |
210 | UINT32 HighCount;\r | |
211 | UINTN BufferSize;\r | |
212 | \r | |
213 | //\r | |
636578d3 | 214 | // Make sure the Monotonic Counter Architectural Protocol has not been installed in the system yet.\r |
e70d34ca | 215 | //\r |
216 | ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);\r | |
217 | \r | |
218 | //\r | |
636578d3 | 219 | // Initialize event to handle low-part overflow\r |
e70d34ca | 220 | //\r |
221 | Status = gBS->CreateEvent (\r | |
222 | EVT_NOTIFY_SIGNAL,\r | |
223 | TPL_CALLBACK,\r | |
224 | EfiMtcEventHandler,\r | |
225 | NULL,\r | |
226 | &mEfiMtcEvent\r | |
227 | );\r | |
e70d34ca | 228 | ASSERT_EFI_ERROR (Status);\r |
229 | \r | |
230 | //\r | |
231 | // Read the last high part\r | |
232 | //\r | |
233 | BufferSize = sizeof (UINT32);\r | |
234 | Status = EfiGetVariable (\r | |
c8ad2d7a LG |
235 | MTC_VARIABLE_NAME,\r |
236 | &gMtcVendorGuid,\r | |
e70d34ca | 237 | NULL,\r |
238 | &BufferSize,\r | |
239 | &HighCount\r | |
240 | );\r | |
241 | if (EFI_ERROR (Status)) {\r | |
242 | HighCount = 0;\r | |
243 | }\r | |
244 | //\r | |
245 | // Set the current value\r | |
246 | //\r | |
247 | mEfiMtc = LShiftU64 (HighCount, 32);\r | |
248 | \r | |
249 | //\r | |
250 | // Increment the upper 32 bits for this boot\r | |
251 | // Continue even if it fails. It will only fail if the variable services are\r | |
252 | // not functional.\r | |
253 | //\r | |
636578d3 | 254 | MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r |
e70d34ca | 255 | \r |
256 | //\r | |
257 | // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields\r | |
258 | //\r | |
259 | gBS->GetNextMonotonicCount = MonotonicCounterDriverGetNextMonotonicCount;\r | |
260 | gRT->GetNextHighMonotonicCount = MonotonicCounterDriverGetNextHighMonotonicCount;\r | |
261 | \r | |
262 | //\r | |
263 | // Install the Monotonic Counter Architctural Protocol onto a new handle\r | |
264 | //\r | |
265 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
266 | &mMonotonicCounterHandle,\r | |
267 | &gEfiMonotonicCounterArchProtocolGuid,\r | |
268 | NULL,\r | |
269 | NULL\r | |
270 | );\r | |
271 | ASSERT_EFI_ERROR (Status);\r | |
272 | \r | |
273 | return EFI_SUCCESS;\r | |
274 | }\r |