3 Copyright (c) 2006 - 2007, 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
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.
18 Produced the Monotonic Counter Services as defined in the DXE CIS
24 #include "MonotonicCounter.h"
27 // The Monotonic Counter Handle
29 EFI_HANDLE mMonotonicCounterHandle
= NULL
;
32 // The current Monotonic count value
37 // Event to use to update the Mtc's high part when wrapping
39 EFI_EVENT mEfiMtcEvent
;
42 // EfiMtcName - Variable name of the MTC value
44 CHAR16
*mEfiMtcName
= (CHAR16
*) L
"MTC";
47 // EfiMtcGuid - Guid of the MTC value
49 EFI_GUID mEfiMtcGuid
= { 0xeb704011, 0x1402, 0x11d3, { 0x8e, 0x77, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } };
57 MonotonicCounterDriverGetNextMonotonicCount (
73 // Can not be called after ExitBootServices()
75 if (EfiAtRuntime ()) {
76 return EFI_UNSUPPORTED
;
79 // Check input parameters
82 return EFI_INVALID_PARAMETER
;
85 // Update the monotonic counter with a lock
87 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
90 gBS
->RestoreTPL (OldTpl
);
93 // If the MSB bit of the low part toggled, then signal that the high
94 // part needs updated now
96 if ((((UINT32
) mEfiMtc
) ^ ((UINT32
) *Count
)) & 0x80000000) {
97 gBS
->SignalEvent (mEfiMtcEvent
);
105 Returns the next high 32 bits of the platform's monotonic counter.
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
125 This function may only be called at Runtime.
127 @param[out] HighCount Pointer to returned value.
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.
139 MonotonicCounterDriverGetNextHighMonotonicCount (
140 OUT UINT32
*HighCount
155 // Check input parameters
157 if (HighCount
== NULL
) {
158 return EFI_INVALID_PARAMETER
;
161 if (!EfiAtRuntime ()) {
163 // Use a lock if called before ExitBootServices()
165 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
166 *HighCount
= (UINT32
) RShiftU64 (mEfiMtc
, 32) + 1;
167 mEfiMtc
= LShiftU64 (*HighCount
, 32);
168 gBS
->RestoreTPL (OldTpl
);
170 *HighCount
= (UINT32
) RShiftU64 (mEfiMtc
, 32) + 1;
171 mEfiMtc
= LShiftU64 (*HighCount
, 32);
174 // Update the NvRam store to match the new high part
176 return EfiSetVariable (
179 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
197 Monotonic count event handler. This handler updates the high monotonic count.
201 Event The event to handle
202 Context The event context
206 EFI_SUCCESS The event has been handled properly
207 EFI_NOT_FOUND An error occurred updating the variable.
213 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount
);
218 MonotonicCounterDriverInitialize (
219 IN EFI_HANDLE ImageHandle
,
220 IN EFI_SYSTEM_TABLE
*SystemTable
227 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
238 // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system
240 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiMonotonicCounterArchProtocolGuid
);
243 // Initialize event to handle overflows
245 Status
= gBS
->CreateEvent (
253 ASSERT_EFI_ERROR (Status
);
256 // Read the last high part
258 BufferSize
= sizeof (UINT32
);
259 Status
= EfiGetVariable (
266 if (EFI_ERROR (Status
)) {
270 // Set the current value
272 mEfiMtc
= LShiftU64 (HighCount
, 32);
275 // Increment the upper 32 bits for this boot
276 // Continue even if it fails. It will only fail if the variable services are
279 Status
= MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount
);
282 // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields
284 gBS
->GetNextMonotonicCount
= MonotonicCounterDriverGetNextMonotonicCount
;
285 gRT
->GetNextHighMonotonicCount
= MonotonicCounterDriverGetNextHighMonotonicCount
;
288 // Install the Monotonic Counter Architctural Protocol onto a new handle
290 Status
= gBS
->InstallMultipleProtocolInterfaces (
291 &mMonotonicCounterHandle
,
292 &gEfiMonotonicCounterArchProtocolGuid
,
296 ASSERT_EFI_ERROR (Status
);