]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/RuntimeDxe/Runtime.c
Convert the capsule image address installed in Configuration table to virtual address.
[mirror_edk2.git] / MdeModulePkg / Core / RuntimeDxe / Runtime.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 Runtime.c
15
16 Abstract:
17
18 Runtime Architectural Protocol as defined in the DXE CIS
19
20 This code is used to produce the EFI runtime virtual switch over
21
22 THIS IS VERY DANGEROUS CODE BE VERY CAREFUL IF YOU CHANGE IT
23
24 The transition for calling EFI Runtime functions in physical mode to calling
25 them in virtual mode is very very complex. Every pointer in needs to be
26 converted from physical mode to virtual mode. Be very careful walking linked
27 lists! Then to make it really hard the code it's self needs be relocated into
28 the new virtual address space.
29
30 So here is the concept. The code in this module will never ever be called in
31 virtual mode. This is the code that collects the information needed to convert
32 to virtual mode (DXE core registers runtime stuff with this code). Since this
33 code is used to fixup all runtime images, it CAN NOT fix it's self up. So some
34 code has to stay behind and that is us.
35
36 Also you need to be careful about when you allocate memory, as once we are in
37 runtime (including our EVT_SIGNAL_EXIT_BOOT_SERVICES event) you can no longer
38 allocate memory.
39
40 Any runtime driver that gets loaded before us will not be callable in virtual
41 mode. This is due to the fact that the DXE core can not register the info
42 needed with us. This is good, since it keeps the code in this file from
43 getting registered.
44
45
46 Revision History:
47
48 - Move the CalculateCrc32 function from Runtime Arch Protocol to Boot Service.
49 Runtime Arch Protocol definition no longer contains CalculateCrc32. Boot Service
50 Table now contains an item named CalculateCrc32.
51
52 --*/
53
54 #include "Runtime.h"
55
56 //
57 // Global Variables
58 //
59 EFI_MEMORY_DESCRIPTOR *mVirtualMap = NULL;
60 UINTN mVirtualMapDescriptorSize;
61 UINTN mVirtualMapMaxIndex;
62 VOID *mMyImageBase;
63
64 //
65 // The handle onto which the Runtime Architectural Protocol instance is installed
66 //
67 EFI_HANDLE mRuntimeHandle = NULL;
68
69 //
70 // The Runtime Architectural Protocol instance produced by this driver
71 //
72 EFI_RUNTIME_ARCH_PROTOCOL mRuntime = {
73 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.ImageHead),
74 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.EventHead),
75
76 //
77 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will
78 // prevent people from having pointer math bugs in their code.
79 // now you have to use *DescriptorSize to make things work.
80 //
81 sizeof (EFI_MEMORY_DESCRIPTOR) + sizeof (UINT64) - (sizeof (EFI_MEMORY_DESCRIPTOR) % sizeof (UINT64)),
82 EFI_MEMORY_DESCRIPTOR_VERSION,
83 0,
84 NULL,
85 NULL,
86 FALSE,
87 FALSE
88 };
89
90 //
91 // Worker Functions
92 //
93 STATIC
94 VOID
95 RuntimeDriverCalculateEfiHdrCrc (
96 IN OUT EFI_TABLE_HEADER *Hdr
97 )
98 /*++
99
100 Routine Description:
101
102 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers
103 internal function. The EFI Boot Services Table can not be used because
104 the EFI Boot Services Table was destroyed at ExitBootServices()
105
106 Arguments:
107
108 Hdr - Pointer to an EFI standard header
109
110 Returns:
111
112 None
113
114 --*/
115 {
116 UINT32 Crc;
117
118 Hdr->CRC32 = 0;
119
120 Crc = 0;
121 RuntimeDriverCalculateCrc32 ((UINT8 *) Hdr, Hdr->HeaderSize, &Crc);
122 Hdr->CRC32 = Crc;
123 }
124
125 EFI_STATUS
126 EFIAPI
127 RuntimeDriverConvertPointer (
128 IN UINTN DebugDisposition,
129 IN OUT VOID **ConvertAddress
130 )
131 /*++
132
133 Routine Description:
134
135 Determines the new virtual address that is to be used on subsequent memory accesses.
136
137 Arguments:
138
139 DebugDisposition - Supplies type information for the pointer being converted.
140 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed
141 for the new virtual address mappings being applied.
142
143 Returns:
144
145 EFI_SUCCESS - The pointer pointed to by Address was modified.
146 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part
147 of the current memory map. This is normally fatal.
148 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
149
150 --*/
151 {
152 UINTN Address;
153 UINT64 VirtEndOfRange;
154 EFI_MEMORY_DESCRIPTOR *VirtEntry;
155 UINTN Index;
156
157 //
158 // Make sure ConvertAddress is a valid pointer
159 //
160 if (ConvertAddress == NULL) {
161 return EFI_INVALID_PARAMETER;
162 }
163 //
164 // Get the address to convert
165 //
166 Address = (UINTN) *ConvertAddress;
167
168 //
169 // If this is a null pointer, return if it's allowed
170 //
171 if (Address == 0) {
172 if (DebugDisposition & EFI_OPTIONAL_POINTER) {
173 return EFI_SUCCESS;
174 }
175
176 return EFI_INVALID_PARAMETER;
177 }
178
179 VirtEntry = mVirtualMap;
180 for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {
181 //
182 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
183 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
184 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
185 // multiply.
186 //
187 ASSERT (((UINTN) VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));
188
189 if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
190 if (Address >= VirtEntry->PhysicalStart) {
191 VirtEndOfRange = VirtEntry->PhysicalStart + (((UINTN) VirtEntry->NumberOfPages) * EFI_PAGE_SIZE);
192 if (Address < VirtEndOfRange) {
193 //
194 // Compute new address
195 //
196 *ConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);
197 return EFI_SUCCESS;
198 }
199 }
200 }
201
202 VirtEntry = NextMemoryDescriptor (VirtEntry, mVirtualMapDescriptorSize);
203 }
204
205 return EFI_NOT_FOUND;
206 }
207
208 STATIC
209 EFI_STATUS
210 RuntimeDriverConvertInternalPointer (
211 IN OUT VOID **ConvertAddress
212 )
213 /*++
214
215 Routine Description:
216
217 Determines the new virtual address that is to be used on subsequent memory accesses
218 for internal pointers.
219
220 Arguments:
221
222 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed
223 for the new virtual address mappings being applied.
224
225 Returns:
226
227 EFI_SUCCESS - The pointer pointed to by Address was modified.
228 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part
229 of the current memory map. This is normally fatal.
230 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
231
232 --*/
233 {
234 return RuntimeDriverConvertPointer (0x0, ConvertAddress);
235 }
236
237 EFI_STATUS
238 EFIAPI
239 RuntimeDriverSetVirtualAddressMap (
240 IN UINTN MemoryMapSize,
241 IN UINTN DescriptorSize,
242 IN UINT32 DescriptorVersion,
243 IN EFI_MEMORY_DESCRIPTOR *VirtualMap
244 )
245 /*++
246
247 Routine Description:
248
249 Changes the runtime addressing mode of EFI firmware from physical to virtual.
250
251 Arguments:
252
253 MemoryMapSize - The size in bytes of VirtualMap.
254 DescriptorSize - The size in bytes of an entry in the VirtualMap.
255 DescriptorVersion - The version of the structure entries in VirtualMap.
256 VirtualMap - An array of memory descriptors which contain new virtual
257 address mapping information for all runtime ranges.
258
259 Returns:
260
261 EFI_SUCCESS - The virtual address map has been applied.
262 EFI_UNSUPPORTED - EFI firmware is not at runtime, or the EFI firmware is already in
263 virtual address mapped mode.
264 EFI_INVALID_PARAMETER - DescriptorSize or DescriptorVersion is invalid.
265 EFI_NO_MAPPING - A virtual address was not supplied for a range in the memory
266 map that requires a mapping.
267 EFI_NOT_FOUND - A virtual address was supplied for an address that is not found
268 in the memory map.
269
270 --*/
271 {
272 EFI_STATUS Status;
273 EFI_RUNTIME_EVENT_ENTRY *RuntimeEvent;
274 EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage;
275 LIST_ENTRY *Link;
276 EFI_PHYSICAL_ADDRESS VirtImageBase;
277 UINTN Index;
278 UINTN Index1;
279 UINTN Index2;
280 UINTN Index3;
281 EFI_CAPSULE_TABLE *CapsuleTable;
282 EFI_CAPSULE_INFO_TABLE *CapsuleInfoTable;
283
284 //
285 // Can only switch to virtual addresses once the memory map is locked down,
286 // and can only set it once
287 //
288 if (!mRuntime.AtRuntime || mRuntime.VirtualMode) {
289 return EFI_UNSUPPORTED;
290 }
291 //
292 // Only understand the original descriptor format
293 //
294 if (DescriptorVersion != EFI_MEMORY_DESCRIPTOR_VERSION || DescriptorSize < sizeof (EFI_MEMORY_DESCRIPTOR)) {
295 return EFI_INVALID_PARAMETER;
296 }
297 //
298 // We are now committed to go to virtual mode, so lets get to it!
299 //
300 mRuntime.VirtualMode = TRUE;
301
302 //
303 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
304 // globals we need to parse the virtual address map.
305 //
306 mVirtualMapDescriptorSize = DescriptorSize;
307 mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;
308 mVirtualMap = VirtualMap;
309
310 //
311 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will
312 // check whether in Runtime or not (this is judged by looking at
313 // mEfiAtRuntime global So this ReportStatusCode will work
314 //
315 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdStatusCodeValueSetVirtualAddressMap));
316
317 //
318 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
319 // All runtime events are stored in a list in Runtime AP.
320 //
321 for (Link = mRuntime.EventHead.ForwardLink; Link != &mRuntime.EventHead; Link = Link->ForwardLink) {
322 RuntimeEvent = _CR (Link, EFI_RUNTIME_EVENT_ENTRY, Link);
323 if ((RuntimeEvent->Type & EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {
324 RuntimeEvent->NotifyFunction (
325 RuntimeEvent->Event,
326 RuntimeEvent->NotifyContext
327 );
328 }
329 }
330
331 //
332 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.
333 //
334 for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {
335 RuntimeImage = _CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);
336 //
337 // We don't want to relocate our selves, as we only run in physical mode.
338 //
339 if (mMyImageBase != RuntimeImage->ImageBase) {
340
341 VirtImageBase = (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase;
342 Status = RuntimeDriverConvertPointer (0, (VOID **) &VirtImageBase);
343 ASSERT_EFI_ERROR (Status);
344
345 PeCoffLoaderRelocateImageForRuntime (
346 (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase,
347 VirtImageBase,
348 (UINTN) RuntimeImage->ImageSize,
349 RuntimeImage->RelocationData
350 );
351
352 InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN)RuntimeImage->ImageSize);
353 }
354 }
355
356 //
357 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
358 // and recompute the CRC-32
359 //
360 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetTime);
361 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetTime);
362 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetWakeupTime);
363 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetWakeupTime);
364 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ResetSystem);
365 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextHighMonotonicCount);
366 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetVariable);
367 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetVariable);
368 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextVariableName);
369 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryVariableInfo);
370 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->UpdateCapsule);
371 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryCapsuleCapabilities);
372 RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);
373
374 //
375 // BugBug: PI requires System Configuration Tables Conversion.
376 // Currently, we do not implement it.
377 //
378 for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {
379 //
380 // CapsuleInfoGuid in ConfigTable refers to an array of CapsuleGuid, it is information
381 // from which you can tell which vendorGuids in ConfigTable are related to CapsuleTable.
382 // Each CapsuleTable points to a array of capsules across a system reset. Then convert
383 // the array contents to make these capsules visiable in Runtime.
384 //
385
386 //
387 // Firstly, Get CapsulInfoGuid in ConfigTable, it points to CapsuleInfoTable, which
388 // gather all the installed capsules' guids.
389 //
390 if (CompareGuid (&gEfiCapsuleInfoGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {
391 CapsuleInfoTable = gST->ConfigurationTable[Index].VendorTable;
392 //
393 // For each known CapsuleGuid in CapsuleInfoTable, loop the whole ConfigTable to
394 // find out this guid related to CapsuleTable.
395 //
396 for (Index1 = 0; Index1 < CapsuleInfoTable->CapsuleGuidNumber; Index1++) {
397 for (Index2 = 0; Index2 < gST->NumberOfTableEntries; Index2++) {
398 //
399 // Find out certain CapsuleTable, go through its contents array, and convert them.
400 //
401 if (CompareGuid (&CapsuleInfoTable->CapsuleGuidPtr[Index1], &(gST->ConfigurationTable[Index2].VendorGuid))) {
402 CapsuleTable = gST->ConfigurationTable[Index2].VendorTable;
403 for (Index3 = 0; Index3 < CapsuleTable->CapsuleArrayNumber; Index3++) {
404 RuntimeDriverConvertInternalPointer ((VOID **) &CapsuleTable->CapsulePtr[Index3]);
405 }
406 RuntimeDriverConvertInternalPointer ((VOID **) &(gST->ConfigurationTable[Index2].VendorTable));
407 }
408 }
409 }
410 break;
411 }
412 }
413
414 //
415 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
416 //
417 RuntimeDriverConvertInternalPointer ((VOID **) &gST->FirmwareVendor);
418 RuntimeDriverConvertInternalPointer ((VOID **) &gST->ConfigurationTable);
419 RuntimeDriverConvertInternalPointer ((VOID **) &gST->RuntimeServices);
420 RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);
421
422 //
423 // At this point, gRT and gST are physical pointers, but the contents of these tables
424 // have been converted to runtime.
425 //
426 //
427 // mVirtualMap is only valid during SetVirtualAddressMap() call
428 //
429 mVirtualMap = NULL;
430
431 return EFI_SUCCESS;
432 }
433
434 EFI_STATUS
435 EFIAPI
436 RuntimeDriverInitialize (
437 IN EFI_HANDLE ImageHandle,
438 IN EFI_SYSTEM_TABLE *SystemTable
439 )
440 /*++
441
442 Routine Description:
443 Install Runtime AP. This code includes the EfiDriverLib, but it functions at
444 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and
445 ASSERT macros (they do ReportStatusCode).
446
447 Arguments:
448 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
449
450 Returns:
451
452 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed
453
454 Other - Return value from gBS->InstallMultipleProtocolInterfaces
455
456 --*/
457 {
458 EFI_STATUS Status;
459 EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;
460
461 //
462 // This image needs to be exclued from relocation for virtual mode, so cache
463 // a copy of the Loaded Image protocol to test later.
464 //
465 Status = gBS->HandleProtocol (
466 ImageHandle,
467 &gEfiLoadedImageProtocolGuid,
468 (VOID**)&MyLoadedImage
469 );
470 ASSERT_EFI_ERROR (Status);
471 mMyImageBase = MyLoadedImage->ImageBase;
472
473 //
474 // Initialize the table used to compute 32-bit CRCs
475 //
476 RuntimeDriverInitializeCrc32Table ();
477
478 //
479 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
480 //
481 gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;
482 gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;
483 gRT->ConvertPointer = RuntimeDriverConvertPointer;
484
485 //
486 // Install the Runtime Architectural Protocol onto a new handle
487 //
488 Status = gBS->InstallMultipleProtocolInterfaces (
489 &mRuntimeHandle,
490 &gEfiRuntimeArchProtocolGuid,
491 &mRuntime,
492 NULL
493 );
494 ASSERT_EFI_ERROR (Status);
495
496 return EFI_SUCCESS;
497 }