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
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 } };
56 MonotonicCounterDriverGetNextMonotonicCount (
72 // Can not be called after ExitBootServices()
74 if (EfiAtRuntime ()) {
75 return EFI_UNSUPPORTED
;
78 // Check input parameters
81 return EFI_INVALID_PARAMETER
;
84 // Update the monotonic counter with a lock
86 OldTpl
= gBS
->RaiseTPL (EFI_TPL_HIGH_LEVEL
);
89 gBS
->RestoreTPL (OldTpl
);
92 // If the MSB bit of the low part toggled, then signal that the high
93 // part needs updated now
95 if ((((UINT32
) mEfiMtc
) ^ ((UINT32
) *Count
)) & 0x80000000) {
96 gBS
->SignalEvent (mEfiMtcEvent
);
104 Returns the next high 32 bits of the platform's monotonic counter.
106 The GetNextHighMonotonicCount() function returns the next high 32 bits
107 of the platform's monotonic counter. The platform's monotonic counter is
108 comprised of two 32 bit quantities: the high 32 bits and the low 32 bits.
109 During boot service time the low 32 bit value is volatile: it is reset to
110 zero on every system reset and is increased by 1 on every call to GetNextMonotonicCount().
111 The high 32 bit value is non-volatile and is increased by 1 whenever the system resets
112 or whenever the low 32 bit count [returned by GetNextMonoticCount()] overflows.
113 The GetNextMonotonicCount() function is only available at boot services time.
114 If the operating system wishes to extend the platform monotonic counter to runtime,
115 it may do so by utilizing GetNextHighMonotonicCount(). To do this, before calling
116 ExitBootServices() the operating system would call GetNextMonotonicCount() to obtain
117 the current platform monotonic count. The operating system would then provide an
118 interface that returns the next count by:
119 Adding 1 to the last count.
120 Before the lower 32 bits of the count overflows, call GetNextHighMonotonicCount().
121 This will increase the high 32 bits of the platform's non-volatile portion of the monotonic
124 This function may only be called at Runtime.
126 @param[out] HighCount Pointer to returned value.
128 @retval EFI_INVALID_PARAMETER If HighCount is NULL.
129 @retval EFI_SUCCESS Operation is successful.
130 @retval EFI_OUT_OF_RESOURCES If variable service reports that not enough storage
131 is available to hold the variable and its data.
132 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.
137 MonotonicCounterDriverGetNextHighMonotonicCount (
138 OUT UINT32
*HighCount
153 // Check input parameters
155 if (HighCount
== NULL
) {
156 return EFI_INVALID_PARAMETER
;
159 if (!EfiAtRuntime ()) {
161 // Use a lock if called before ExitBootServices()
163 OldTpl
= gBS
->RaiseTPL (EFI_TPL_HIGH_LEVEL
);
164 *HighCount
= (UINT32
) RShiftU64 (mEfiMtc
, 32) + 1;
165 mEfiMtc
= LShiftU64 (*HighCount
, 32);
166 gBS
->RestoreTPL (OldTpl
);
168 *HighCount
= (UINT32
) RShiftU64 (mEfiMtc
, 32) + 1;
169 mEfiMtc
= LShiftU64 (*HighCount
, 32);
172 // Update the NvRam store to match the new high part
174 return EfiSetVariable (
177 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
194 Monotonic count event handler. This handler updates the high monotonic count.
198 Event The event to handle
199 Context The event context
203 EFI_SUCCESS The event has been handled properly
204 EFI_NOT_FOUND An error occurred updating the variable.
210 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount
);
215 MonotonicCounterDriverInitialize (
216 IN EFI_HANDLE ImageHandle
,
217 IN EFI_SYSTEM_TABLE
*SystemTable
224 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
235 // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system
237 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiMonotonicCounterArchProtocolGuid
);
240 // Initialize event to handle overflows
242 Status
= gBS
->CreateEvent (
243 EFI_EVENT_NOTIFY_SIGNAL
,
250 ASSERT_EFI_ERROR (Status
);
253 // Read the last high part
255 BufferSize
= sizeof (UINT32
);
256 Status
= gRT
->GetVariable (
263 if (EFI_ERROR (Status
)) {
267 // Set the current value
269 mEfiMtc
= LShiftU64 (HighCount
, 32);
272 // Increment the upper 32 bits for this boot
273 // Continue even if it fails. It will only fail if the variable services are
276 Status
= MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount
);
279 // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields
281 gBS
->GetNextMonotonicCount
= MonotonicCounterDriverGetNextMonotonicCount
;
282 gRT
->GetNextHighMonotonicCount
= MonotonicCounterDriverGetNextHighMonotonicCount
;
285 // Install the Monotonic Counter Architctural Protocol onto a new handle
287 Status
= gBS
->InstallMultipleProtocolInterfaces (
288 &mMonotonicCounterHandle
,
289 &gEfiMonotonicCounterArchProtocolGuid
,
293 ASSERT_EFI_ERROR (Status
);