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
;
72 // Check input parameters
75 return EFI_INVALID_PARAMETER
;
79 // Update the monotonic counter with a lock
81 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
84 gBS
->RestoreTPL (OldTpl
);
87 // If the low 32-bit counter overflows (MSB bit toggled),
88 // then signal that the high part needs update now.
90 if ((((UINT32
)mEfiMtc
) ^ ((UINT32
)*Count
)) & BIT31
) {
91 gBS
->SignalEvent (mEfiMtcEvent
);
98 Returns the next high 32 bits of the platform's monotonic counter.
100 The GetNextHighMonotonicCount() function returns the next high 32 bits
101 of the platform's monotonic counter. The platform's monotonic counter is
102 comprised of two 32 bit quantities: the high 32 bits and the low 32 bits.
103 During boot service time the low 32 bit value is volatile: it is reset to
104 zero on every system reset and is increased by 1 on every call to GetNextMonotonicCount().
105 The high 32 bit value is non-volatile and is increased by 1 whenever the system resets,
106 whenever GetNextHighMonotonicCount() is called, or whenever the low 32 bit count
107 (returned by GetNextMonoticCount()) overflows.
108 The GetNextMonotonicCount() function is only available at boot services time.
109 If the operating system wishes to extend the platform monotonic counter to runtime,
110 it may do so by utilizing GetNextHighMonotonicCount(). To do this, before calling
111 ExitBootServices() the operating system would call GetNextMonotonicCount() to obtain
112 the current platform monotonic count. The operating system would then provide an
113 interface that returns the next count by:
114 Adding 1 to the last count.
115 Before the lower 32 bits of the count overflows, call GetNextHighMonotonicCount().
116 This will increase the high 32 bits of the platform's non-volatile portion of the monotonic
119 This function may only be called at Runtime.
121 @param HighCount Pointer to returned value.
123 @retval EFI_SUCCESS The next high monotonic count was returned.
124 @retval EFI_INVALID_PARAMETER HighCount is NULL.
125 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.
126 @retval EFI_OUT_OF_RESOURCES If variable service reports that not enough storage
127 is available to hold the variable and its data.
132 MonotonicCounterDriverGetNextHighMonotonicCount (
133 OUT UINT32
*HighCount
139 // Check input parameters
141 if (HighCount
== NULL
) {
142 return EFI_INVALID_PARAMETER
;
145 if (!EfiAtRuntime ()) {
147 // Use a lock if called before ExitBootServices()
149 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
150 *HighCount
= (UINT32
)RShiftU64 (mEfiMtc
, 32) + 1;
151 mEfiMtc
= LShiftU64 (*HighCount
, 32);
152 gBS
->RestoreTPL (OldTpl
);
154 *HighCount
= (UINT32
)RShiftU64 (mEfiMtc
, 32) + 1;
155 mEfiMtc
= LShiftU64 (*HighCount
, 32);
159 // Update the NV variable to match the new high part
161 return EfiSetVariable (
164 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
171 Monotonic counter event handler. This handler updates the high part of monotonic counter.
173 @param Event The event to handle.
174 @param Context The event context.
186 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount
);
190 Entry point of monotonic counter driver.
192 @param ImageHandle The image handle of this driver.
193 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
195 @retval EFI_SUCCESS The initialization is successful.
200 MonotonicCounterDriverInitialize (
201 IN EFI_HANDLE ImageHandle
,
202 IN EFI_SYSTEM_TABLE
*SystemTable
210 // Make sure the Monotonic Counter Architectural Protocol has not been installed in the system yet.
212 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiMonotonicCounterArchProtocolGuid
);
215 // Initialize event to handle low-part overflow
217 Status
= gBS
->CreateEvent (
224 ASSERT_EFI_ERROR (Status
);
227 // Read the last high part
229 BufferSize
= sizeof (UINT32
);
230 Status
= EfiGetVariable (
237 if (EFI_ERROR (Status
)) {
242 // Set the current value
244 mEfiMtc
= LShiftU64 (HighCount
, 32);
247 // Increment the upper 32 bits for this boot
248 // Continue even if it fails. It will only fail if the variable services are
251 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount
);
254 // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields
256 gBS
->GetNextMonotonicCount
= MonotonicCounterDriverGetNextMonotonicCount
;
257 gRT
->GetNextHighMonotonicCount
= MonotonicCounterDriverGetNextHighMonotonicCount
;
260 // Install the Monotonic Counter Architctural Protocol onto a new handle
262 Status
= gBS
->InstallMultipleProtocolInterfaces (
263 &mMonotonicCounterHandle
,
264 &gEfiMonotonicCounterArchProtocolGuid
,
268 ASSERT_EFI_ERROR (Status
);