]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/MonotonicCounter/RuntimeDxe/MonotonicCounter.c
All runtime driver should use functions provided by UefiRuntimeLib library class...
[mirror_edk2.git] / EdkModulePkg / Universal / MonotonicCounter / RuntimeDxe / MonotonicCounter.c
1 /*++
2
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
8
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.
11
12 Module Name:
13
14 MonotonicCounter.c
15
16 Abstract:
17
18 Produced the Monotonic Counter Services as defined in the DXE CIS
19
20 Revision History:
21
22 --*/
23
24 #include "MonotonicCounter.h"
25
26 //
27 // The Monotonic Counter Handle
28 //
29 EFI_HANDLE mMonotonicCounterHandle = NULL;
30
31 //
32 // The current Monotonic count value
33 //
34 UINT64 mEfiMtc;
35
36 //
37 // Event to use to update the Mtc's high part when wrapping
38 //
39 EFI_EVENT mEfiMtcEvent;
40
41 //
42 // EfiMtcName - Variable name of the MTC value
43 //
44 CHAR16 *mEfiMtcName = (CHAR16 *) L"MTC";
45
46 //
47 // EfiMtcGuid - Guid of the MTC value
48 //
49 EFI_GUID mEfiMtcGuid = { 0xeb704011, 0x1402, 0x11d3, { 0x8e, 0x77, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } };
50
51 //
52 // Worker functions
53 //
54 EFI_STATUS
55 EFIAPI
56 MonotonicCounterDriverGetNextMonotonicCount (
57 OUT UINT64 *Count
58 )
59 /*++
60
61 Routine Description:
62
63 Arguments:
64
65 Returns:
66
67 --*/
68 {
69 EFI_TPL OldTpl;
70
71 //
72 // Can not be called after ExitBootServices()
73 //
74 if (EfiAtRuntime ()) {
75 return EFI_UNSUPPORTED;
76 }
77 //
78 // Check input parameters
79 //
80 if (Count == NULL) {
81 return EFI_INVALID_PARAMETER;
82 }
83 //
84 // Update the monotonic counter with a lock
85 //
86 OldTpl = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);
87 *Count = mEfiMtc;
88 mEfiMtc++;
89 gBS->RestoreTPL (OldTpl);
90
91 //
92 // If the MSB bit of the low part toggled, then signal that the high
93 // part needs updated now
94 //
95 if ((((UINT32) mEfiMtc) ^ ((UINT32) *Count)) & 0x80000000) {
96 gBS->SignalEvent (mEfiMtcEvent);
97 }
98
99 return EFI_SUCCESS;
100 }
101
102
103 /**
104 Returns the next high 32 bits of the platform's monotonic counter.
105
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
122 count by 1.
123
124 This function may only be called at Runtime.
125
126 @param[out] HighCount Pointer to returned value.
127
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.
133
134 **/
135 EFI_STATUS
136 EFIAPI
137 MonotonicCounterDriverGetNextHighMonotonicCount (
138 OUT UINT32 *HighCount
139 )
140 /*++
141
142 Routine Description:
143
144 Arguments:
145
146 Returns:
147
148 --*/
149 {
150 EFI_TPL OldTpl;
151
152 //
153 // Check input parameters
154 //
155 if (HighCount == NULL) {
156 return EFI_INVALID_PARAMETER;
157 }
158
159 if (!EfiAtRuntime ()) {
160 //
161 // Use a lock if called before ExitBootServices()
162 //
163 OldTpl = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);
164 *HighCount = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;
165 mEfiMtc = LShiftU64 (*HighCount, 32);
166 gBS->RestoreTPL (OldTpl);
167 } else {
168 *HighCount = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;
169 mEfiMtc = LShiftU64 (*HighCount, 32);
170 }
171 //
172 // Update the NvRam store to match the new high part
173 //
174 return EfiSetVariable (
175 mEfiMtcName,
176 &mEfiMtcGuid,
177 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
178 sizeof (UINT32),
179 HighCount
180 );
181
182 }
183
184 VOID
185 EFIAPI
186 EfiMtcEventHandler (
187 IN EFI_EVENT Event,
188 IN VOID *Context
189 )
190 /*++
191
192 Routine Description:
193
194 Monotonic count event handler. This handler updates the high monotonic count.
195
196 Arguments:
197
198 Event The event to handle
199 Context The event context
200
201 Returns:
202
203 EFI_SUCCESS The event has been handled properly
204 EFI_NOT_FOUND An error occurred updating the variable.
205
206 --*/
207 {
208 UINT32 HighCount;
209
210 MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);
211 }
212
213 EFI_STATUS
214 EFIAPI
215 MonotonicCounterDriverInitialize (
216 IN EFI_HANDLE ImageHandle,
217 IN EFI_SYSTEM_TABLE *SystemTable
218 )
219 /*++
220
221 Routine Description:
222
223 Arguments:
224 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
225
226 Returns:
227
228 --*/
229 {
230 EFI_STATUS Status;
231 UINT32 HighCount;
232 UINTN BufferSize;
233
234 //
235 // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system
236 //
237 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);
238
239 //
240 // Initialize event to handle overflows
241 //
242 Status = gBS->CreateEvent (
243 EFI_EVENT_NOTIFY_SIGNAL,
244 EFI_TPL_CALLBACK,
245 EfiMtcEventHandler,
246 NULL,
247 &mEfiMtcEvent
248 );
249
250 ASSERT_EFI_ERROR (Status);
251
252 //
253 // Read the last high part
254 //
255 BufferSize = sizeof (UINT32);
256 Status = gRT->GetVariable (
257 mEfiMtcName,
258 &mEfiMtcGuid,
259 NULL,
260 &BufferSize,
261 &HighCount
262 );
263 if (EFI_ERROR (Status)) {
264 HighCount = 0;
265 }
266 //
267 // Set the current value
268 //
269 mEfiMtc = LShiftU64 (HighCount, 32);
270
271 //
272 // Increment the upper 32 bits for this boot
273 // Continue even if it fails. It will only fail if the variable services are
274 // not functional.
275 //
276 Status = MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);
277
278 //
279 // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields
280 //
281 gBS->GetNextMonotonicCount = MonotonicCounterDriverGetNextMonotonicCount;
282 gRT->GetNextHighMonotonicCount = MonotonicCounterDriverGetNextHighMonotonicCount;
283
284 //
285 // Install the Monotonic Counter Architctural Protocol onto a new handle
286 //
287 Status = gBS->InstallMultipleProtocolInterfaces (
288 &mMonotonicCounterHandle,
289 &gEfiMonotonicCounterArchProtocolGuid,
290 NULL,
291 NULL
292 );
293 ASSERT_EFI_ERROR (Status);
294
295 return EFI_SUCCESS;
296 }