2 Produce the UEFI boot service GetNextMonotonicCount() and runtime service
3 GetNextHighMonotonicCount().
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Protocol/MonotonicCounter.h>
13 #include <Guid/MtcVendor.h>
15 #include <Library/BaseLib.h>
16 #include <Library/UefiDriverEntryPoint.h>
17 #include <Library/UefiRuntimeLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/UefiRuntimeServicesTableLib.h>
23 // The handle to install Monotonic Counter Architctural Protocol
25 EFI_HANDLE mMonotonicCounterHandle
= NULL
;
28 // The current monotonic counter value
33 // Event to update the monotonic Counter's high part when low part overflows.
35 EFI_EVENT mEfiMtcEvent
;
38 Returns a monotonically increasing count for the platform.
40 This function returns a 64-bit value that is numerically larger then the last
41 time the function was called.
42 The platform monotonic counter is comprised of two parts: the high 32 bits
43 and the low 32 bits. The low 32-bit value is volatile and is reset to zero on
44 every system reset. It is increased by 1 on every call to GetNextMonotonicCount().
45 The high 32-bit value is nonvolatile and is increased by one on whenever the
46 system resets or the low 32-bit counter overflows.
48 @param Count Pointer to returned value.
50 @retval EFI_SUCCESS The next monotonic count was returned.
51 @retval EFI_DEVICE_ERROR The device is not functioning properly.
52 @retval EFI_INVALID_PARAMETER Count is NULL.
53 @retval EFI_UNSUPPORTED This function is called at runtime.
58 MonotonicCounterDriverGetNextMonotonicCount (
65 // Cannot be called after ExitBootServices()
67 if (EfiAtRuntime ()) {
68 return EFI_UNSUPPORTED
;
71 // Check input parameters
74 return EFI_INVALID_PARAMETER
;
77 // Update the monotonic counter with a lock
79 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
82 gBS
->RestoreTPL (OldTpl
);
85 // If the low 32-bit counter overflows (MSB bit toggled),
86 // then signal that the high part needs update now.
88 if ((((UINT32
) mEfiMtc
) ^ ((UINT32
) *Count
)) & BIT31
) {
89 gBS
->SignalEvent (mEfiMtcEvent
);
97 Returns the next high 32 bits of the platform's monotonic counter.
99 The GetNextHighMonotonicCount() function returns the next high 32 bits
100 of the platform's monotonic counter. The platform's monotonic counter is
101 comprised of two 32 bit quantities: the high 32 bits and the low 32 bits.
102 During boot service time the low 32 bit value is volatile: it is reset to
103 zero on every system reset and is increased by 1 on every call to GetNextMonotonicCount().
104 The high 32 bit value is non-volatile and is increased by 1 whenever the system resets,
105 whenever GetNextHighMonotonicCount() is called, or whenever the low 32 bit count
106 (returned by GetNextMonoticCount()) overflows.
107 The GetNextMonotonicCount() function is only available at boot services time.
108 If the operating system wishes to extend the platform monotonic counter to runtime,
109 it may do so by utilizing GetNextHighMonotonicCount(). To do this, before calling
110 ExitBootServices() the operating system would call GetNextMonotonicCount() to obtain
111 the current platform monotonic count. The operating system would then provide an
112 interface that returns the next count by:
113 Adding 1 to the last count.
114 Before the lower 32 bits of the count overflows, call GetNextHighMonotonicCount().
115 This will increase the high 32 bits of the platform's non-volatile portion of the monotonic
118 This function may only be called at Runtime.
120 @param HighCount Pointer to returned value.
122 @retval EFI_SUCCESS The next high monotonic count was returned.
123 @retval EFI_INVALID_PARAMETER HighCount is NULL.
124 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.
125 @retval EFI_OUT_OF_RESOURCES If variable service reports that not enough storage
126 is available to hold the variable and its data.
131 MonotonicCounterDriverGetNextHighMonotonicCount (
132 OUT UINT32
*HighCount
138 // Check input parameters
140 if (HighCount
== NULL
) {
141 return EFI_INVALID_PARAMETER
;
144 if (!EfiAtRuntime ()) {
146 // Use a lock if called before ExitBootServices()
148 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
149 *HighCount
= (UINT32
) RShiftU64 (mEfiMtc
, 32) + 1;
150 mEfiMtc
= LShiftU64 (*HighCount
, 32);
151 gBS
->RestoreTPL (OldTpl
);
153 *HighCount
= (UINT32
) RShiftU64 (mEfiMtc
, 32) + 1;
154 mEfiMtc
= LShiftU64 (*HighCount
, 32);
157 // Update the NV variable to match the new high part
159 return EfiSetVariable (
162 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
170 Monotonic counter event handler. This handler updates the high part of monotonic counter.
172 @param Event The event to handle.
173 @param Context The event context.
185 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount
);
189 Entry point of monotonic counter driver.
191 @param ImageHandle The image handle of this driver.
192 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
194 @retval EFI_SUCCESS The initialization is successful.
199 MonotonicCounterDriverInitialize (
200 IN EFI_HANDLE ImageHandle
,
201 IN EFI_SYSTEM_TABLE
*SystemTable
209 // Make sure the Monotonic Counter Architectural Protocol has not been installed in the system yet.
211 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiMonotonicCounterArchProtocolGuid
);
214 // Initialize event to handle low-part overflow
216 Status
= gBS
->CreateEvent (
223 ASSERT_EFI_ERROR (Status
);
226 // Read the last high part
228 BufferSize
= sizeof (UINT32
);
229 Status
= EfiGetVariable (
236 if (EFI_ERROR (Status
)) {
240 // Set the current value
242 mEfiMtc
= LShiftU64 (HighCount
, 32);
245 // Increment the upper 32 bits for this boot
246 // Continue even if it fails. It will only fail if the variable services are
249 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount
);
252 // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields
254 gBS
->GetNextMonotonicCount
= MonotonicCounterDriverGetNextMonotonicCount
;
255 gRT
->GetNextHighMonotonicCount
= MonotonicCounterDriverGetNextHighMonotonicCount
;
258 // Install the Monotonic Counter Architctural Protocol onto a new handle
260 Status
= gBS
->InstallMultipleProtocolInterfaces (
261 &mMonotonicCounterHandle
,
262 &gEfiMonotonicCounterArchProtocolGuid
,
266 ASSERT_EFI_ERROR (Status
);