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