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