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