]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
MdeModulePkg/Core: Decorate phase-transition functions with NORETURN.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / DxeMain / DxeMain.c
1 /** @file
2 DXE Core Main Entry Point
3
4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "DxeMain.h"
16
17 //
18 // DXE Core Global Variables for Protocols from PEI
19 //
20 EFI_HANDLE mDecompressHandle = NULL;
21
22 //
23 // DXE Core globals for Architecture Protocols
24 //
25 EFI_SECURITY_ARCH_PROTOCOL *gSecurity = NULL;
26 EFI_SECURITY2_ARCH_PROTOCOL *gSecurity2 = NULL;
27 EFI_CPU_ARCH_PROTOCOL *gCpu = NULL;
28 EFI_METRONOME_ARCH_PROTOCOL *gMetronome = NULL;
29 EFI_TIMER_ARCH_PROTOCOL *gTimer = NULL;
30 EFI_BDS_ARCH_PROTOCOL *gBds = NULL;
31 EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *gWatchdogTimer = NULL;
32
33 //
34 // DXE Core globals for optional protocol dependencies
35 //
36 EFI_SMM_BASE2_PROTOCOL *gSmmBase2 = NULL;
37
38 //
39 // DXE Core Global used to update core loaded image protocol handle
40 //
41 EFI_GUID *gDxeCoreFileName;
42 EFI_LOADED_IMAGE_PROTOCOL *gDxeCoreLoadedImage;
43
44 //
45 // DXE Core Module Variables
46 //
47 EFI_BOOT_SERVICES mBootServices = {
48 {
49 EFI_BOOT_SERVICES_SIGNATURE, // Signature
50 EFI_BOOT_SERVICES_REVISION, // Revision
51 sizeof (EFI_BOOT_SERVICES), // HeaderSize
52 0, // CRC32
53 0 // Reserved
54 },
55 (EFI_RAISE_TPL) CoreRaiseTpl, // RaiseTPL
56 (EFI_RESTORE_TPL) CoreRestoreTpl, // RestoreTPL
57 (EFI_ALLOCATE_PAGES) CoreAllocatePages, // AllocatePages
58 (EFI_FREE_PAGES) CoreFreePages, // FreePages
59 (EFI_GET_MEMORY_MAP) CoreGetMemoryMap, // GetMemoryMap
60 (EFI_ALLOCATE_POOL) CoreAllocatePool, // AllocatePool
61 (EFI_FREE_POOL) CoreFreePool, // FreePool
62 (EFI_CREATE_EVENT) CoreCreateEvent, // CreateEvent
63 (EFI_SET_TIMER) CoreSetTimer, // SetTimer
64 (EFI_WAIT_FOR_EVENT) CoreWaitForEvent, // WaitForEvent
65 (EFI_SIGNAL_EVENT) CoreSignalEvent, // SignalEvent
66 (EFI_CLOSE_EVENT) CoreCloseEvent, // CloseEvent
67 (EFI_CHECK_EVENT) CoreCheckEvent, // CheckEvent
68 (EFI_INSTALL_PROTOCOL_INTERFACE) CoreInstallProtocolInterface, // InstallProtocolInterface
69 (EFI_REINSTALL_PROTOCOL_INTERFACE) CoreReinstallProtocolInterface, // ReinstallProtocolInterface
70 (EFI_UNINSTALL_PROTOCOL_INTERFACE) CoreUninstallProtocolInterface, // UninstallProtocolInterface
71 (EFI_HANDLE_PROTOCOL) CoreHandleProtocol, // HandleProtocol
72 (VOID *) NULL, // Reserved
73 (EFI_REGISTER_PROTOCOL_NOTIFY) CoreRegisterProtocolNotify, // RegisterProtocolNotify
74 (EFI_LOCATE_HANDLE) CoreLocateHandle, // LocateHandle
75 (EFI_LOCATE_DEVICE_PATH) CoreLocateDevicePath, // LocateDevicePath
76 (EFI_INSTALL_CONFIGURATION_TABLE) CoreInstallConfigurationTable, // InstallConfigurationTable
77 (EFI_IMAGE_LOAD) CoreLoadImage, // LoadImage
78 (EFI_IMAGE_START) CoreStartImage, // StartImage
79 (EFI_EXIT) CoreExit, // Exit
80 (EFI_IMAGE_UNLOAD) CoreUnloadImage, // UnloadImage
81 (EFI_EXIT_BOOT_SERVICES) CoreExitBootServices, // ExitBootServices
82 (EFI_GET_NEXT_MONOTONIC_COUNT) CoreEfiNotAvailableYetArg1, // GetNextMonotonicCount
83 (EFI_STALL) CoreStall, // Stall
84 (EFI_SET_WATCHDOG_TIMER) CoreSetWatchdogTimer, // SetWatchdogTimer
85 (EFI_CONNECT_CONTROLLER) CoreConnectController, // ConnectController
86 (EFI_DISCONNECT_CONTROLLER) CoreDisconnectController, // DisconnectController
87 (EFI_OPEN_PROTOCOL) CoreOpenProtocol, // OpenProtocol
88 (EFI_CLOSE_PROTOCOL) CoreCloseProtocol, // CloseProtocol
89 (EFI_OPEN_PROTOCOL_INFORMATION) CoreOpenProtocolInformation, // OpenProtocolInformation
90 (EFI_PROTOCOLS_PER_HANDLE) CoreProtocolsPerHandle, // ProtocolsPerHandle
91 (EFI_LOCATE_HANDLE_BUFFER) CoreLocateHandleBuffer, // LocateHandleBuffer
92 (EFI_LOCATE_PROTOCOL) CoreLocateProtocol, // LocateProtocol
93 (EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES) CoreInstallMultipleProtocolInterfaces, // InstallMultipleProtocolInterfaces
94 (EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES) CoreUninstallMultipleProtocolInterfaces, // UninstallMultipleProtocolInterfaces
95 (EFI_CALCULATE_CRC32) CoreEfiNotAvailableYetArg3, // CalculateCrc32
96 (EFI_COPY_MEM) CopyMem, // CopyMem
97 (EFI_SET_MEM) SetMem, // SetMem
98 (EFI_CREATE_EVENT_EX) CoreCreateEventEx // CreateEventEx
99 };
100
101 EFI_DXE_SERVICES mDxeServices = {
102 {
103 DXE_SERVICES_SIGNATURE, // Signature
104 DXE_SERVICES_REVISION, // Revision
105 sizeof (DXE_SERVICES), // HeaderSize
106 0, // CRC32
107 0 // Reserved
108 },
109 (EFI_ADD_MEMORY_SPACE) CoreAddMemorySpace, // AddMemorySpace
110 (EFI_ALLOCATE_MEMORY_SPACE) CoreAllocateMemorySpace, // AllocateMemorySpace
111 (EFI_FREE_MEMORY_SPACE) CoreFreeMemorySpace, // FreeMemorySpace
112 (EFI_REMOVE_MEMORY_SPACE) CoreRemoveMemorySpace, // RemoveMemorySpace
113 (EFI_GET_MEMORY_SPACE_DESCRIPTOR) CoreGetMemorySpaceDescriptor, // GetMemorySpaceDescriptor
114 (EFI_SET_MEMORY_SPACE_ATTRIBUTES) CoreSetMemorySpaceAttributes, // SetMemorySpaceAttributes
115 (EFI_GET_MEMORY_SPACE_MAP) CoreGetMemorySpaceMap, // GetMemorySpaceMap
116 (EFI_ADD_IO_SPACE) CoreAddIoSpace, // AddIoSpace
117 (EFI_ALLOCATE_IO_SPACE) CoreAllocateIoSpace, // AllocateIoSpace
118 (EFI_FREE_IO_SPACE) CoreFreeIoSpace, // FreeIoSpace
119 (EFI_REMOVE_IO_SPACE) CoreRemoveIoSpace, // RemoveIoSpace
120 (EFI_GET_IO_SPACE_DESCRIPTOR) CoreGetIoSpaceDescriptor, // GetIoSpaceDescriptor
121 (EFI_GET_IO_SPACE_MAP) CoreGetIoSpaceMap, // GetIoSpaceMap
122 (EFI_DISPATCH) CoreDispatcher, // Dispatch
123 (EFI_SCHEDULE) CoreSchedule, // Schedule
124 (EFI_TRUST) CoreTrust, // Trust
125 (EFI_PROCESS_FIRMWARE_VOLUME) CoreProcessFirmwareVolume, // ProcessFirmwareVolume
126 (EFI_SET_MEMORY_SPACE_CAPABILITIES)CoreSetMemorySpaceCapabilities, // SetMemorySpaceCapabilities
127 };
128
129 EFI_SYSTEM_TABLE mEfiSystemTableTemplate = {
130 {
131 EFI_SYSTEM_TABLE_SIGNATURE, // Signature
132 EFI_SYSTEM_TABLE_REVISION, // Revision
133 sizeof (EFI_SYSTEM_TABLE), // HeaderSize
134 0, // CRC32
135 0 // Reserved
136 },
137 NULL, // FirmwareVendor
138 0, // FirmwareRevision
139 NULL, // ConsoleInHandle
140 NULL, // ConIn
141 NULL, // ConsoleOutHandle
142 NULL, // ConOut
143 NULL, // StandardErrorHandle
144 NULL, // StdErr
145 NULL, // RuntimeServices
146 &mBootServices, // BootServices
147 0, // NumberOfConfigurationTableEntries
148 NULL // ConfigurationTable
149 };
150
151 EFI_RUNTIME_SERVICES mEfiRuntimeServicesTableTemplate = {
152 {
153 EFI_RUNTIME_SERVICES_SIGNATURE, // Signature
154 EFI_RUNTIME_SERVICES_REVISION, // Revision
155 sizeof (EFI_RUNTIME_SERVICES), // HeaderSize
156 0, // CRC32
157 0 // Reserved
158 },
159 (EFI_GET_TIME) CoreEfiNotAvailableYetArg2, // GetTime
160 (EFI_SET_TIME) CoreEfiNotAvailableYetArg1, // SetTime
161 (EFI_GET_WAKEUP_TIME) CoreEfiNotAvailableYetArg3, // GetWakeupTime
162 (EFI_SET_WAKEUP_TIME) CoreEfiNotAvailableYetArg2, // SetWakeupTime
163 (EFI_SET_VIRTUAL_ADDRESS_MAP) CoreEfiNotAvailableYetArg4, // SetVirtualAddressMap
164 (EFI_CONVERT_POINTER) CoreEfiNotAvailableYetArg2, // ConvertPointer
165 (EFI_GET_VARIABLE) CoreEfiNotAvailableYetArg5, // GetVariable
166 (EFI_GET_NEXT_VARIABLE_NAME) CoreEfiNotAvailableYetArg3, // GetNextVariableName
167 (EFI_SET_VARIABLE) CoreEfiNotAvailableYetArg5, // SetVariable
168 (EFI_GET_NEXT_HIGH_MONO_COUNT) CoreEfiNotAvailableYetArg1, // GetNextHighMonotonicCount
169 (EFI_RESET_SYSTEM) CoreEfiNotAvailableYetArg4, // ResetSystem
170 (EFI_UPDATE_CAPSULE) CoreEfiNotAvailableYetArg3, // UpdateCapsule
171 (EFI_QUERY_CAPSULE_CAPABILITIES) CoreEfiNotAvailableYetArg4, // QueryCapsuleCapabilities
172 (EFI_QUERY_VARIABLE_INFO) CoreEfiNotAvailableYetArg4 // QueryVariableInfo
173 };
174
175 EFI_RUNTIME_ARCH_PROTOCOL gRuntimeTemplate = {
176 INITIALIZE_LIST_HEAD_VARIABLE (gRuntimeTemplate.ImageHead),
177 INITIALIZE_LIST_HEAD_VARIABLE (gRuntimeTemplate.EventHead),
178
179 //
180 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will
181 // prevent people from having pointer math bugs in their code.
182 // now you have to use *DescriptorSize to make things work.
183 //
184 sizeof (EFI_MEMORY_DESCRIPTOR) + sizeof (UINT64) - (sizeof (EFI_MEMORY_DESCRIPTOR) % sizeof (UINT64)),
185 EFI_MEMORY_DESCRIPTOR_VERSION,
186 0,
187 NULL,
188 NULL,
189 FALSE,
190 FALSE
191 };
192
193 EFI_RUNTIME_ARCH_PROTOCOL *gRuntime = &gRuntimeTemplate;
194
195 //
196 // DXE Core Global Variables for the EFI System Table, Boot Services Table,
197 // DXE Services Table, and Runtime Services Table
198 //
199 EFI_DXE_SERVICES *gDxeCoreDS = &mDxeServices;
200 EFI_SYSTEM_TABLE *gDxeCoreST = NULL;
201
202 //
203 // For debug initialize gDxeCoreRT to template. gDxeCoreRT must be allocated from RT memory
204 // but gDxeCoreRT is used for ASSERT () and DEBUG () type macros so lets give it
205 // a value that will not cause debug infrastructure to crash early on.
206 //
207 EFI_RUNTIME_SERVICES *gDxeCoreRT = &mEfiRuntimeServicesTableTemplate;
208 EFI_HANDLE gDxeCoreImageHandle = NULL;
209
210 BOOLEAN gMemoryMapTerminated = FALSE;
211
212 //
213 // EFI Decompress Protocol
214 //
215 EFI_DECOMPRESS_PROTOCOL gEfiDecompress = {
216 DxeMainUefiDecompressGetInfo,
217 DxeMainUefiDecompress
218 };
219
220 //
221 // For Loading modules at fixed address feature, the configuration table is to cache the top address below which to load
222 // Runtime code&boot time code
223 //
224 GLOBAL_REMOVE_IF_UNREFERENCED EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE gLoadModuleAtFixAddressConfigurationTable = {0, 0};
225
226 // Main entry point to the DXE Core
227 //
228
229 /**
230 Main entry point to DXE Core.
231
232 @param HobStart Pointer to the beginning of the HOB List from PEI.
233
234 @return This function should never return.
235
236 **/
237 VOID
238 NORETURN
239 EFIAPI
240 DxeMain (
241 IN VOID *HobStart
242 )
243 {
244 EFI_STATUS Status;
245 EFI_PHYSICAL_ADDRESS MemoryBaseAddress;
246 UINT64 MemoryLength;
247 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
248 UINTN Index;
249 EFI_HOB_GUID_TYPE *GuidHob;
250 EFI_VECTOR_HANDOFF_INFO *VectorInfoList;
251 EFI_VECTOR_HANDOFF_INFO *VectorInfo;
252 VOID *EntryPoint;
253
254 //
255 // Setup the default exception handlers
256 //
257 VectorInfoList = NULL;
258 GuidHob = GetNextGuidHob (&gEfiVectorHandoffInfoPpiGuid, HobStart);
259 if (GuidHob != NULL) {
260 VectorInfoList = (EFI_VECTOR_HANDOFF_INFO *) (GET_GUID_HOB_DATA(GuidHob));
261 }
262 Status = InitializeCpuExceptionHandlers (VectorInfoList);
263 ASSERT_EFI_ERROR (Status);
264
265 //
266 // Initialize Debug Agent to support source level debug in DXE phase
267 //
268 InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_CORE, HobStart, NULL);
269
270 //
271 // Initialize Memory Services
272 //
273 CoreInitializeMemoryServices (&HobStart, &MemoryBaseAddress, &MemoryLength);
274
275 MemoryProfileInit (HobStart);
276
277 //
278 // Allocate the EFI System Table and EFI Runtime Service Table from EfiRuntimeServicesData
279 // Use the templates to initialize the contents of the EFI System Table and EFI Runtime Services Table
280 //
281 gDxeCoreST = AllocateRuntimeCopyPool (sizeof (EFI_SYSTEM_TABLE), &mEfiSystemTableTemplate);
282 ASSERT (gDxeCoreST != NULL);
283
284 gDxeCoreRT = AllocateRuntimeCopyPool (sizeof (EFI_RUNTIME_SERVICES), &mEfiRuntimeServicesTableTemplate);
285 ASSERT (gDxeCoreRT != NULL);
286
287 gDxeCoreST->RuntimeServices = gDxeCoreRT;
288
289 //
290 // Start the Image Services.
291 //
292 Status = CoreInitializeImageServices (HobStart);
293 ASSERT_EFI_ERROR (Status);
294
295 //
296 // Initialize the Global Coherency Domain Services
297 //
298 Status = CoreInitializeGcdServices (&HobStart, MemoryBaseAddress, MemoryLength);
299 ASSERT_EFI_ERROR (Status);
300
301 //
302 // Call constructor for all libraries
303 //
304 ProcessLibraryConstructorList (gDxeCoreImageHandle, gDxeCoreST);
305 PERF_END (NULL,"PEI", NULL, 0) ;
306 PERF_START (NULL,"DXE", NULL, 0) ;
307
308 //
309 // Report DXE Core image information to the PE/COFF Extra Action Library
310 //
311 ZeroMem (&ImageContext, sizeof (ImageContext));
312 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)gDxeCoreLoadedImage->ImageBase;
313 ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*)(UINTN)ImageContext.ImageAddress);
314 ImageContext.SizeOfHeaders = PeCoffGetSizeOfHeaders ((VOID*)(UINTN)ImageContext.ImageAddress);
315 Status = PeCoffLoaderGetEntryPoint ((VOID*)(UINTN)ImageContext.ImageAddress, &EntryPoint);
316 if (Status == EFI_SUCCESS) {
317 ImageContext.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;
318 }
319 ImageContext.Handle = (VOID *)(UINTN)gDxeCoreLoadedImage->ImageBase;
320 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
321 PeCoffLoaderRelocateImageExtraAction (&ImageContext);
322
323 //
324 // Install the DXE Services Table into the EFI System Tables's Configuration Table
325 //
326 Status = CoreInstallConfigurationTable (&gEfiDxeServicesTableGuid, gDxeCoreDS);
327 ASSERT_EFI_ERROR (Status);
328
329 //
330 // Install the HOB List into the EFI System Tables's Configuration Table
331 //
332 Status = CoreInstallConfigurationTable (&gEfiHobListGuid, HobStart);
333 ASSERT_EFI_ERROR (Status);
334
335 //
336 // Install Memory Type Information Table into the EFI System Tables's Configuration Table
337 //
338 Status = CoreInstallConfigurationTable (&gEfiMemoryTypeInformationGuid, &gMemoryTypeInformation);
339 ASSERT_EFI_ERROR (Status);
340
341 //
342 // If Loading modules At fixed address feature is enabled, install Load moduels at fixed address
343 // Configuration Table so that user could easily to retrieve the top address to load Dxe and PEI
344 // Code and Tseg base to load SMM driver.
345 //
346 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
347 Status = CoreInstallConfigurationTable (&gLoadFixedAddressConfigurationTableGuid, &gLoadModuleAtFixAddressConfigurationTable);
348 ASSERT_EFI_ERROR (Status);
349 }
350 //
351 // Report Status Code here for DXE_ENTRY_POINT once it is available
352 //
353 REPORT_STATUS_CODE (
354 EFI_PROGRESS_CODE,
355 (EFI_SOFTWARE_DXE_CORE | EFI_SW_DXE_CORE_PC_ENTRY_POINT)
356 );
357
358 //
359 // Create the aligned system table pointer structure that is used by external
360 // debuggers to locate the system table... Also, install debug image info
361 // configuration table.
362 //
363 CoreInitializeDebugImageInfoTable ();
364 CoreNewDebugImageInfoEntry (
365 EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL,
366 gDxeCoreLoadedImage,
367 gDxeCoreImageHandle
368 );
369
370 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "HOBLIST address in DXE = 0x%p\n", HobStart));
371
372 DEBUG_CODE_BEGIN ();
373 EFI_PEI_HOB_POINTERS Hob;
374
375 for (Hob.Raw = HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
376 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {
377 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Memory Allocation 0x%08x 0x%0lx - 0x%0lx\n", \
378 Hob.MemoryAllocation->AllocDescriptor.MemoryType, \
379 Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress, \
380 Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress + Hob.MemoryAllocation->AllocDescriptor.MemoryLength - 1));
381 }
382 }
383 for (Hob.Raw = HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
384 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV2) {
385 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "FV2 Hob 0x%0lx - 0x%0lx\n", Hob.FirmwareVolume2->BaseAddress, Hob.FirmwareVolume2->BaseAddress + Hob.FirmwareVolume2->Length - 1));
386 } else if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV) {
387 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "FV Hob 0x%0lx - 0x%0lx\n", Hob.FirmwareVolume->BaseAddress, Hob.FirmwareVolume->BaseAddress + Hob.FirmwareVolume->Length - 1));
388 }
389 }
390 DEBUG_CODE_END ();
391
392 //
393 // Initialize the Event Services
394 //
395 Status = CoreInitializeEventServices ();
396 ASSERT_EFI_ERROR (Status);
397
398 MemoryProfileInstallProtocol ();
399
400 CoreInitializePropertiesTable ();
401 CoreInitializeMemoryAttributesTable ();
402
403 //
404 // Get persisted vector hand-off info from GUIDeed HOB again due to HobStart may be updated,
405 // and install configuration table
406 //
407 GuidHob = GetNextGuidHob (&gEfiVectorHandoffInfoPpiGuid, HobStart);
408 if (GuidHob != NULL) {
409 VectorInfoList = (EFI_VECTOR_HANDOFF_INFO *) (GET_GUID_HOB_DATA(GuidHob));
410 VectorInfo = VectorInfoList;
411 Index = 1;
412 while (VectorInfo->Attribute != EFI_VECTOR_HANDOFF_LAST_ENTRY) {
413 VectorInfo ++;
414 Index ++;
415 }
416 VectorInfo = AllocateCopyPool (sizeof (EFI_VECTOR_HANDOFF_INFO) * Index, (VOID *) VectorInfoList);
417 ASSERT (VectorInfo != NULL);
418 Status = CoreInstallConfigurationTable (&gEfiVectorHandoffTableGuid, (VOID *) VectorInfo);
419 ASSERT_EFI_ERROR (Status);
420 }
421
422 //
423 // Get the Protocols that were passed in from PEI to DXE through GUIDed HOBs
424 //
425 // These Protocols are not architectural. This implementation is sharing code between
426 // PEI and DXE in order to save FLASH space. These Protocols could also be implemented
427 // as part of the DXE Core. However, that would also require the DXE Core to be ported
428 // each time a different CPU is used, a different Decompression algorithm is used, or a
429 // different Image type is used. By placing these Protocols in PEI, the DXE Core remains
430 // generic, and only PEI and the Arch Protocols need to be ported from Platform to Platform,
431 // and from CPU to CPU.
432 //
433
434 //
435 // Publish the EFI, Tiano, and Custom Decompress protocols for use by other DXE components
436 //
437 Status = CoreInstallMultipleProtocolInterfaces (
438 &mDecompressHandle,
439 &gEfiDecompressProtocolGuid, &gEfiDecompress,
440 NULL
441 );
442 ASSERT_EFI_ERROR (Status);
443
444 //
445 // Register for the GUIDs of the Architectural Protocols, so the rest of the
446 // EFI Boot Services and EFI Runtime Services tables can be filled in.
447 // Also register for the GUIDs of optional protocols.
448 //
449 CoreNotifyOnProtocolInstallation ();
450
451 //
452 // Produce Firmware Volume Protocols, one for each FV in the HOB list.
453 //
454 Status = FwVolBlockDriverInit (gDxeCoreImageHandle, gDxeCoreST);
455 ASSERT_EFI_ERROR (Status);
456
457 Status = FwVolDriverInit (gDxeCoreImageHandle, gDxeCoreST);
458 ASSERT_EFI_ERROR (Status);
459
460 //
461 // Produce the Section Extraction Protocol
462 //
463 Status = InitializeSectionExtraction (gDxeCoreImageHandle, gDxeCoreST);
464 ASSERT_EFI_ERROR (Status);
465
466 //
467 // Initialize the DXE Dispatcher
468 //
469 PERF_START (NULL,"CoreInitializeDispatcher", "DxeMain", 0) ;
470 CoreInitializeDispatcher ();
471 PERF_END (NULL,"CoreInitializeDispatcher", "DxeMain", 0) ;
472
473 //
474 // Invoke the DXE Dispatcher
475 //
476 PERF_START (NULL, "CoreDispatcher", "DxeMain", 0);
477 CoreDispatcher ();
478 PERF_END (NULL, "CoreDispatcher", "DxeMain", 0);
479
480 //
481 // Display Architectural protocols that were not loaded if this is DEBUG build
482 //
483 DEBUG_CODE_BEGIN ();
484 CoreDisplayMissingArchProtocols ();
485 DEBUG_CODE_END ();
486
487 //
488 // Display any drivers that were not dispatched because dependency expression
489 // evaluated to false if this is a debug build
490 //
491 DEBUG_CODE_BEGIN ();
492 CoreDisplayDiscoveredNotDispatched ();
493 DEBUG_CODE_END ();
494
495 //
496 // Assert if the Architectural Protocols are not present.
497 //
498 Status = CoreAllEfiServicesAvailable ();
499 if (EFI_ERROR(Status)) {
500 //
501 // Report Status code that some Architectural Protocols are not present.
502 //
503 REPORT_STATUS_CODE (
504 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
505 (EFI_SOFTWARE_DXE_CORE | EFI_SW_DXE_CORE_EC_NO_ARCH)
506 );
507 }
508 ASSERT_EFI_ERROR (Status);
509
510 //
511 // Report Status code before transfer control to BDS
512 //
513 REPORT_STATUS_CODE (
514 EFI_PROGRESS_CODE,
515 (EFI_SOFTWARE_DXE_CORE | EFI_SW_DXE_CORE_PC_HANDOFF_TO_NEXT)
516 );
517
518 //
519 // Transfer control to the BDS Architectural Protocol
520 //
521 gBds->Entry (gBds);
522
523 //
524 // BDS should never return
525 //
526 ASSERT (FALSE);
527 CpuDeadLoop ();
528
529 UNREACHABLE ();
530 }
531
532
533
534 /**
535 Place holder function until all the Boot Services and Runtime Services are
536 available.
537
538 @return EFI_NOT_AVAILABLE_YET
539
540 **/
541 EFI_STATUS
542 EFIAPI
543 CoreEfiNotAvailableYetArg0 (
544 VOID
545 )
546 {
547 //
548 // This function should never be executed. If it does, then the architectural protocols
549 // have not been designed correctly. The CpuBreakpoint () is commented out for now until the
550 // DXE Core and all the Architectural Protocols are complete.
551 //
552
553 return EFI_NOT_AVAILABLE_YET;
554 }
555
556
557 /**
558 Place holder function until all the Boot Services and Runtime Services are
559 available.
560
561 @param Arg1 Undefined
562
563 @return EFI_NOT_AVAILABLE_YET
564
565 **/
566 EFI_STATUS
567 EFIAPI
568 CoreEfiNotAvailableYetArg1 (
569 UINTN Arg1
570 )
571 {
572 //
573 // This function should never be executed. If it does, then the architectural protocols
574 // have not been designed correctly. The CpuBreakpoint () is commented out for now until the
575 // DXE Core and all the Architectural Protocols are complete.
576 //
577
578 return EFI_NOT_AVAILABLE_YET;
579 }
580
581
582 /**
583 Place holder function until all the Boot Services and Runtime Services are available.
584
585 @param Arg1 Undefined
586 @param Arg2 Undefined
587
588 @return EFI_NOT_AVAILABLE_YET
589
590 **/
591 EFI_STATUS
592 EFIAPI
593 CoreEfiNotAvailableYetArg2 (
594 UINTN Arg1,
595 UINTN Arg2
596 )
597 {
598 //
599 // This function should never be executed. If it does, then the architectural protocols
600 // have not been designed correctly. The CpuBreakpoint () is commented out for now until the
601 // DXE Core and all the Architectural Protocols are complete.
602 //
603
604 return EFI_NOT_AVAILABLE_YET;
605 }
606
607
608 /**
609 Place holder function until all the Boot Services and Runtime Services are available.
610
611 @param Arg1 Undefined
612 @param Arg2 Undefined
613 @param Arg3 Undefined
614
615 @return EFI_NOT_AVAILABLE_YET
616
617 **/
618 EFI_STATUS
619 EFIAPI
620 CoreEfiNotAvailableYetArg3 (
621 UINTN Arg1,
622 UINTN Arg2,
623 UINTN Arg3
624 )
625 {
626 //
627 // This function should never be executed. If it does, then the architectural protocols
628 // have not been designed correctly. The CpuBreakpoint () is commented out for now until the
629 // DXE Core and all the Architectural Protocols are complete.
630 //
631
632 return EFI_NOT_AVAILABLE_YET;
633 }
634
635
636 /**
637 Place holder function until all the Boot Services and Runtime Services are available.
638
639 @param Arg1 Undefined
640 @param Arg2 Undefined
641 @param Arg3 Undefined
642 @param Arg4 Undefined
643
644 @return EFI_NOT_AVAILABLE_YET
645
646 **/
647 EFI_STATUS
648 EFIAPI
649 CoreEfiNotAvailableYetArg4 (
650 UINTN Arg1,
651 UINTN Arg2,
652 UINTN Arg3,
653 UINTN Arg4
654 )
655 {
656 //
657 // This function should never be executed. If it does, then the architectural protocols
658 // have not been designed correctly. The CpuBreakpoint () is commented out for now until the
659 // DXE Core and all the Architectural Protocols are complete.
660 //
661
662 return EFI_NOT_AVAILABLE_YET;
663 }
664
665
666 /**
667 Place holder function until all the Boot Services and Runtime Services are available.
668
669 @param Arg1 Undefined
670 @param Arg2 Undefined
671 @param Arg3 Undefined
672 @param Arg4 Undefined
673 @param Arg5 Undefined
674
675 @return EFI_NOT_AVAILABLE_YET
676
677 **/
678 EFI_STATUS
679 EFIAPI
680 CoreEfiNotAvailableYetArg5 (
681 UINTN Arg1,
682 UINTN Arg2,
683 UINTN Arg3,
684 UINTN Arg4,
685 UINTN Arg5
686 )
687 {
688 //
689 // This function should never be executed. If it does, then the architectural protocols
690 // have not been designed correctly. The CpuBreakpoint () is commented out for now until the
691 // DXE Core and all the Architectural Protocols are complete.
692 //
693
694 return EFI_NOT_AVAILABLE_YET;
695 }
696
697
698 /**
699 Calcualte the 32-bit CRC in a EFI table using the service provided by the
700 gRuntime service.
701
702 @param Hdr Pointer to an EFI standard header
703
704 **/
705 VOID
706 CalculateEfiHdrCrc (
707 IN OUT EFI_TABLE_HEADER *Hdr
708 )
709 {
710 UINT32 Crc;
711
712 Hdr->CRC32 = 0;
713
714 //
715 // If gBS->CalculateCrce32 () == CoreEfiNotAvailableYet () then
716 // Crc will come back as zero if we set it to zero here
717 //
718 Crc = 0;
719 gBS->CalculateCrc32 ((UINT8 *)Hdr, Hdr->HeaderSize, &Crc);
720 Hdr->CRC32 = Crc;
721 }
722
723
724 /**
725 Terminates all boot services.
726
727 @param ImageHandle Handle that identifies the exiting image.
728 @param MapKey Key to the latest memory map.
729
730 @retval EFI_SUCCESS Boot Services terminated
731 @retval EFI_INVALID_PARAMETER MapKey is incorrect.
732
733 **/
734 EFI_STATUS
735 EFIAPI
736 CoreExitBootServices (
737 IN EFI_HANDLE ImageHandle,
738 IN UINTN MapKey
739 )
740 {
741 EFI_STATUS Status;
742
743 //
744 // Disable Timer
745 //
746 gTimer->SetTimerPeriod (gTimer, 0);
747
748 //
749 // Terminate memory services if the MapKey matches
750 //
751 Status = CoreTerminateMemoryMap (MapKey);
752 if (EFI_ERROR (Status)) {
753 //
754 // Notify other drivers that ExitBootServices fail
755 //
756 CoreNotifySignalList (&gEventExitBootServicesFailedGuid);
757 return Status;
758 }
759
760 gMemoryMapTerminated = TRUE;
761
762 //
763 // Notify other drivers that we are exiting boot services.
764 //
765 CoreNotifySignalList (&gEfiEventExitBootServicesGuid);
766
767 //
768 // Report that ExitBootServices() has been called
769 //
770 REPORT_STATUS_CODE (
771 EFI_PROGRESS_CODE,
772 (EFI_SOFTWARE_EFI_BOOT_SERVICE | EFI_SW_BS_PC_EXIT_BOOT_SERVICES)
773 );
774
775 //
776 // Disable interrupt of Debug timer.
777 //
778 SaveAndSetDebugTimerInterrupt (FALSE);
779
780 //
781 // Disable CPU Interrupts
782 //
783 gCpu->DisableInterrupt (gCpu);
784
785 //
786 // Clear the non-runtime values of the EFI System Table
787 //
788 gDxeCoreST->BootServices = NULL;
789 gDxeCoreST->ConIn = NULL;
790 gDxeCoreST->ConsoleInHandle = NULL;
791 gDxeCoreST->ConOut = NULL;
792 gDxeCoreST->ConsoleOutHandle = NULL;
793 gDxeCoreST->StdErr = NULL;
794 gDxeCoreST->StandardErrorHandle = NULL;
795
796 //
797 // Recompute the 32-bit CRC of the EFI System Table
798 //
799 CalculateEfiHdrCrc (&gDxeCoreST->Hdr);
800
801 //
802 // Zero out the Boot Service Table
803 //
804 ZeroMem (gBS, sizeof (EFI_BOOT_SERVICES));
805 gBS = NULL;
806
807 //
808 // Update the AtRuntime field in Runtiem AP.
809 //
810 gRuntime->AtRuntime = TRUE;
811
812 return Status;
813 }
814
815
816 /**
817 Given a compressed source buffer, this function retrieves the size of the
818 uncompressed buffer and the size of the scratch buffer required to decompress
819 the compressed source buffer.
820
821 The GetInfo() function retrieves the size of the uncompressed buffer and the
822 temporary scratch buffer required to decompress the buffer specified by Source
823 and SourceSize. If the size of the uncompressed buffer or the size of the
824 scratch buffer cannot be determined from the compressed data specified by
825 Source and SourceData, then EFI_INVALID_PARAMETER is returned. Otherwise, the
826 size of the uncompressed buffer is returned in DestinationSize, the size of
827 the scratch buffer is returned in ScratchSize, and EFI_SUCCESS is returned.
828 The GetInfo() function does not have scratch buffer available to perform a
829 thorough checking of the validity of the source data. It just retrieves the
830 "Original Size" field from the beginning bytes of the source data and output
831 it as DestinationSize. And ScratchSize is specific to the decompression
832 implementation.
833
834 @param This A pointer to the EFI_DECOMPRESS_PROTOCOL instance.
835 @param Source The source buffer containing the compressed data.
836 @param SourceSize The size, in bytes, of the source buffer.
837 @param DestinationSize A pointer to the size, in bytes, of the
838 uncompressed buffer that will be generated when the
839 compressed buffer specified by Source and
840 SourceSize is decompressed.
841 @param ScratchSize A pointer to the size, in bytes, of the scratch
842 buffer that is required to decompress the
843 compressed buffer specified by Source and
844 SourceSize.
845
846 @retval EFI_SUCCESS The size of the uncompressed data was returned in
847 DestinationSize and the size of the scratch buffer
848 was returned in ScratchSize.
849 @retval EFI_INVALID_PARAMETER The size of the uncompressed data or the size of
850 the scratch buffer cannot be determined from the
851 compressed data specified by Source and
852 SourceSize.
853
854 **/
855 EFI_STATUS
856 EFIAPI
857 DxeMainUefiDecompressGetInfo (
858 IN EFI_DECOMPRESS_PROTOCOL *This,
859 IN VOID *Source,
860 IN UINT32 SourceSize,
861 OUT UINT32 *DestinationSize,
862 OUT UINT32 *ScratchSize
863 )
864 {
865 if (Source == NULL || DestinationSize == NULL || ScratchSize == NULL) {
866 return EFI_INVALID_PARAMETER;
867 }
868 return UefiDecompressGetInfo (Source, SourceSize, DestinationSize, ScratchSize);
869 }
870
871
872 /**
873 Decompresses a compressed source buffer.
874
875 The Decompress() function extracts decompressed data to its original form.
876 This protocol is designed so that the decompression algorithm can be
877 implemented without using any memory services. As a result, the Decompress()
878 Function is not allowed to call AllocatePool() or AllocatePages() in its
879 implementation. It is the caller's responsibility to allocate and free the
880 Destination and Scratch buffers.
881 If the compressed source data specified by Source and SourceSize is
882 successfully decompressed into Destination, then EFI_SUCCESS is returned. If
883 the compressed source data specified by Source and SourceSize is not in a
884 valid compressed data format, then EFI_INVALID_PARAMETER is returned.
885
886 @param This A pointer to the EFI_DECOMPRESS_PROTOCOL instance.
887 @param Source The source buffer containing the compressed data.
888 @param SourceSize SourceSizeThe size of source data.
889 @param Destination On output, the destination buffer that contains
890 the uncompressed data.
891 @param DestinationSize The size of the destination buffer. The size of
892 the destination buffer needed is obtained from
893 EFI_DECOMPRESS_PROTOCOL.GetInfo().
894 @param Scratch A temporary scratch buffer that is used to perform
895 the decompression.
896 @param ScratchSize The size of scratch buffer. The size of the
897 scratch buffer needed is obtained from GetInfo().
898
899 @retval EFI_SUCCESS Decompression completed successfully, and the
900 uncompressed buffer is returned in Destination.
901 @retval EFI_INVALID_PARAMETER The source buffer specified by Source and
902 SourceSize is corrupted (not in a valid
903 compressed format).
904
905 **/
906 EFI_STATUS
907 EFIAPI
908 DxeMainUefiDecompress (
909 IN EFI_DECOMPRESS_PROTOCOL *This,
910 IN VOID *Source,
911 IN UINT32 SourceSize,
912 IN OUT VOID *Destination,
913 IN UINT32 DestinationSize,
914 IN OUT VOID *Scratch,
915 IN UINT32 ScratchSize
916 )
917 {
918 EFI_STATUS Status;
919 UINT32 TestDestinationSize;
920 UINT32 TestScratchSize;
921
922 if (Source == NULL || Destination== NULL || Scratch == NULL) {
923 return EFI_INVALID_PARAMETER;
924 }
925
926 Status = UefiDecompressGetInfo (Source, SourceSize, &TestDestinationSize, &TestScratchSize);
927 if (EFI_ERROR (Status)) {
928 return Status;
929 }
930
931 if (ScratchSize < TestScratchSize || DestinationSize < TestDestinationSize) {
932 return RETURN_INVALID_PARAMETER;
933 }
934
935 return UefiDecompress (Source, Destination, Scratch);
936 }