]>
Commit | Line | Data |
---|---|---|
fb0b259e | 1 | /** @file\r |
52c7a544 | 2 | \r |
fb0b259e | 3 | Produced the Monotonic Counter Services as defined in the DXE CIS.\r |
e70d34ca | 4 | \r |
52c7a544 | 5 | Copyright (c) 2006 - 2008, Intel Corporation\r |
e70d34ca | 6 | All rights reserved. This program and the accompanying materials\r |
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 |
e70d34ca | 16 | \r |
17 | #include "MonotonicCounter.h"\r | |
18 | \r | |
19 | //\r | |
20 | // The Monotonic Counter Handle\r | |
21 | //\r | |
22 | EFI_HANDLE mMonotonicCounterHandle = NULL;\r | |
23 | \r | |
24 | //\r | |
25 | // The current Monotonic count value\r | |
26 | //\r | |
27 | UINT64 mEfiMtc;\r | |
28 | \r | |
29 | //\r | |
30 | // Event to use to update the Mtc's high part when wrapping\r | |
31 | //\r | |
32 | EFI_EVENT mEfiMtcEvent;\r | |
33 | \r | |
34 | //\r | |
35 | // EfiMtcName - Variable name of the MTC value\r | |
36 | //\r | |
37 | CHAR16 *mEfiMtcName = (CHAR16 *) L"MTC";\r | |
38 | \r | |
39 | //\r | |
40 | // EfiMtcGuid - Guid of the MTC value\r | |
41 | //\r | |
42 | EFI_GUID mEfiMtcGuid = { 0xeb704011, 0x1402, 0x11d3, { 0x8e, 0x77, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } };\r | |
43 | \r | |
52c7a544 | 44 | /**\r |
45 | Returns the low 32 bits of the platform's monotonic counter.\r | |
46 | \r | |
47 | The platform's monotonic counter is comprised of two 32 bit quantities: \r | |
48 | the high 32 bits and the low 32 bits.\r | |
49 | During boot service time the low 32 bit value is volatile: it is reset to\r | |
50 | zero on every system reset and is increased by 1 on every call to this function.\r | |
51 | This function is only available at boot services time.\r | |
52 | Before calling ExitBootServices() the operating system would call this function\r | |
53 | to obtain the current platform monotonic count. \r | |
54 | \r | |
55 | @param Count Pointer to returned value.\r | |
56 | \r | |
57 | @retval EFI_INVALID_PARAMETER If Count is NULL.\r | |
58 | @retval EFI_SUCCESS Operation is successful.\r | |
59 | @retval EFI_UNSUPPORTED If this function is called at Runtime.\r | |
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 | |
71 | // Can not be called after ExitBootServices()\r | |
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 | |
91 | // If the MSB bit of the low part toggled, then signal that the high\r | |
92 | // part needs updated now\r | |
93 | //\r | |
94 | if ((((UINT32) mEfiMtc) ^ ((UINT32) *Count)) & 0x80000000) {\r | |
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 |
127 | @retval EFI_INVALID_PARAMETER If HighCount is NULL.\r | |
128 | @retval EFI_SUCCESS Operation is successful.\r | |
129 | @retval EFI_OUT_OF_RESOURCES If variable service reports that not enough storage\r | |
130 | is available to hold the variable and its data.\r | |
131 | @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r | |
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 | |
162 | // Update the NvRam store to match the new high part\r | |
163 | //\r | |
164 | return EfiSetVariable (\r | |
165 | mEfiMtcName,\r | |
166 | &mEfiMtcGuid,\r | |
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 |
175 | Monotonic count event handler. This handler updates the high monotonic count.\r | |
176 | \r | |
177 | @param Event The event to handle.\r | |
178 | @param Context The event context.\r | |
179 | \r | |
180 | @return None.\r | |
181 | \r | |
182 | **/\r | |
e70d34ca | 183 | VOID\r |
184 | EFIAPI\r | |
185 | EfiMtcEventHandler (\r | |
186 | IN EFI_EVENT Event,\r | |
187 | IN VOID *Context\r | |
188 | )\r | |
e70d34ca | 189 | {\r |
190 | UINT32 HighCount;\r | |
191 | \r | |
192 | MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r | |
193 | }\r | |
194 | \r | |
52c7a544 | 195 | /**\r |
196 | The initial function of monotonic counter driver.\r | |
197 | \r | |
198 | @param ImageHandle The handle of image.\r | |
199 | @param SystemTable The pointer to system table.\r | |
200 | \r | |
201 | @return EFI_SUCCESS The initialize action is successful.\r | |
202 | \r | |
203 | **/\r | |
e70d34ca | 204 | EFI_STATUS\r |
205 | EFIAPI\r | |
206 | MonotonicCounterDriverInitialize (\r | |
207 | IN EFI_HANDLE ImageHandle,\r | |
208 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
209 | )\r | |
e70d34ca | 210 | {\r |
211 | EFI_STATUS Status;\r | |
212 | UINT32 HighCount;\r | |
213 | UINTN BufferSize;\r | |
214 | \r | |
215 | //\r | |
216 | // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system\r | |
217 | //\r | |
218 | ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);\r | |
219 | \r | |
220 | //\r | |
221 | // Initialize event to handle overflows\r | |
222 | //\r | |
223 | Status = gBS->CreateEvent (\r | |
224 | EVT_NOTIFY_SIGNAL,\r | |
225 | TPL_CALLBACK,\r | |
226 | EfiMtcEventHandler,\r | |
227 | NULL,\r | |
228 | &mEfiMtcEvent\r | |
229 | );\r | |
230 | \r | |
231 | ASSERT_EFI_ERROR (Status);\r | |
232 | \r | |
233 | //\r | |
234 | // Read the last high part\r | |
235 | //\r | |
236 | BufferSize = sizeof (UINT32);\r | |
237 | Status = EfiGetVariable (\r | |
238 | mEfiMtcName,\r | |
239 | &mEfiMtcGuid,\r | |
240 | NULL,\r | |
241 | &BufferSize,\r | |
242 | &HighCount\r | |
243 | );\r | |
244 | if (EFI_ERROR (Status)) {\r | |
245 | HighCount = 0;\r | |
246 | }\r | |
247 | //\r | |
248 | // Set the current value\r | |
249 | //\r | |
250 | mEfiMtc = LShiftU64 (HighCount, 32);\r | |
251 | \r | |
252 | //\r | |
253 | // Increment the upper 32 bits for this boot\r | |
254 | // Continue even if it fails. It will only fail if the variable services are\r | |
255 | // not functional.\r | |
256 | //\r | |
257 | Status = MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r | |
258 | \r | |
259 | //\r | |
260 | // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields\r | |
261 | //\r | |
262 | gBS->GetNextMonotonicCount = MonotonicCounterDriverGetNextMonotonicCount;\r | |
263 | gRT->GetNextHighMonotonicCount = MonotonicCounterDriverGetNextHighMonotonicCount;\r | |
264 | \r | |
265 | //\r | |
266 | // Install the Monotonic Counter Architctural Protocol onto a new handle\r | |
267 | //\r | |
268 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
269 | &mMonotonicCounterHandle,\r | |
270 | &gEfiMonotonicCounterArchProtocolGuid,\r | |
271 | NULL,\r | |
272 | NULL\r | |
273 | );\r | |
274 | ASSERT_EFI_ERROR (Status);\r | |
275 | \r | |
276 | return EFI_SUCCESS;\r | |
277 | }\r |