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