summaryrefslogtreecommitdiff
path: root/EdkModulePkg/Core/Dxe/DxeMain/DxeMain.c
blob: db25fcfa8de34af0e48b9f21d448ed9505974937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
/*++

Copyright (c) 2006, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:

  DxeMain.c

Abstract:

  DXE Core Main Entry Point

--*/

#include <DxeMain.h>

VOID
EFIAPI
DxeMain (
  IN  VOID *HobStart
  );

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg0 (
  VOID
  );

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg1 (
  UINTN Arg1
  );

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg2 (
  UINTN Arg1,
  UINTN Arg2
  );

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg3 (
  UINTN Arg1,
  UINTN Arg2,
  UINTN Arg3
  );

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg4 (
  UINTN Arg1,
  UINTN Arg2,
  UINTN Arg3,
  UINTN Arg4
  );

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg5 (
  UINTN Arg1,
  UINTN Arg2,
  UINTN Arg3,
  UINTN Arg4,
  UINTN Arg5
  );

EFI_STATUS
CoreGetPeiProtocol (
  IN EFI_GUID  *ProtocolGuid,
  IN VOID      **Interface
  );
  
EFI_STATUS
DxeMainUefiDecompressGetInfo (
  IN EFI_DECOMPRESS_PROTOCOL            *This,
  IN   VOID                             *Source,
  IN   UINT32                           SourceSize,
  OUT  UINT32                           *DestinationSize,
  OUT  UINT32                           *ScratchSize
  );

EFI_STATUS
EFIAPI
DxeMainUefiDecompress (
  IN EFI_DECOMPRESS_PROTOCOL              *This,
  IN     VOID                             *Source,
  IN     UINT32                           SourceSize,
  IN OUT VOID                             *Destination,
  IN     UINT32                           DestinationSize,
  IN OUT VOID                             *Scratch,
  IN     UINT32                           ScratchSize
  );

EFI_STATUS
DxeMainTianoDecompressGetInfo (
  IN EFI_TIANO_DECOMPRESS_PROTOCOL      *This,
  IN   VOID                             *Source,
  IN   UINT32                           SourceSize,
  OUT  UINT32                           *DestinationSize,
  OUT  UINT32                           *ScratchSize
  );

EFI_STATUS
EFIAPI
DxeMainTianoDecompress (
  IN EFI_TIANO_DECOMPRESS_PROTOCOL        *This,
  IN     VOID                             *Source,
  IN     UINT32                           SourceSize,
  IN OUT VOID                             *Destination,
  IN     UINT32                           DestinationSize,
  IN OUT VOID                             *Scratch,
  IN     UINT32                           ScratchSize
  );

EFI_STATUS
DxeMainCustomDecompressGetInfo (
  IN EFI_CUSTOMIZED_DECOMPRESS_PROTOCOL  *This,
  IN   VOID                              *Source,
  IN   UINT32                            SourceSize,
  OUT  UINT32                            *DestinationSize,
  OUT  UINT32                            *ScratchSize
  );

EFI_STATUS
EFIAPI
DxeMainCustomDecompress (
  IN EFI_CUSTOMIZED_DECOMPRESS_PROTOCOL  *This,
  IN     VOID                            *Source,
  IN     UINT32                          SourceSize,
  IN OUT VOID                            *Destination,
  IN     UINT32                          DestinationSize,
  IN OUT VOID                            *Scratch,
  IN     UINT32                          ScratchSize
  );

//
// DXE Core Global Variables for Protocols from PEI
//
EFI_HANDLE                                mDecompressHandle = NULL;
EFI_PEI_PE_COFF_LOADER_PROTOCOL           *gEfiPeiPeCoffLoader          = NULL;

//
// DXE Core globals for Architecture Protocols
//
EFI_SECURITY_ARCH_PROTOCOL        *gSecurity      = NULL;
EFI_CPU_ARCH_PROTOCOL             *gCpu           = NULL;
EFI_METRONOME_ARCH_PROTOCOL       *gMetronome     = NULL;
EFI_TIMER_ARCH_PROTOCOL           *gTimer         = NULL;
EFI_BDS_ARCH_PROTOCOL             *gBds           = NULL;
EFI_WATCHDOG_TIMER_ARCH_PROTOCOL  *gWatchdogTimer = NULL;
EFI_RUNTIME_ARCH_PROTOCOL         *gRuntime       = NULL;

//
// BugBug: I'n not runtime, but is the PPI?
//
EFI_STATUS_CODE_PROTOCOL     gStatusCodeInstance = {
  NULL
};

EFI_STATUS_CODE_PROTOCOL     *gStatusCode    = &gStatusCodeInstance;


//
// DXE Core Global used to update core loaded image protocol handle
//
EFI_GUID                           *gDxeCoreFileName;
EFI_LOADED_IMAGE_PROTOCOL          *gDxeCoreLoadedImage;



//
// DXE Core Module Variables
//

EFI_BOOT_SERVICES mBootServices = {
  {
    EFI_BOOT_SERVICES_SIGNATURE,                                                          // Signature
    EFI_BOOT_SERVICES_REVISION,                                                           // Revision
    sizeof (EFI_BOOT_SERVICES),                                                           // HeaderSize
    0,                                                                                    // CRC32
    0                                                                                     // Reserved
  },                                                                                      
  (EFI_RAISE_TPL)                               CoreRaiseTpl,                             // RaiseTPL
  (EFI_RESTORE_TPL)                             CoreRestoreTpl,                           // RestoreTPL
  (EFI_ALLOCATE_PAGES)                          CoreAllocatePages,                        // AllocatePages
  (EFI_FREE_PAGES)                              CoreFreePages,                            // FreePages
  (EFI_GET_MEMORY_MAP)                          CoreGetMemoryMap,                         // GetMemoryMap
  (EFI_ALLOCATE_POOL)                           CoreAllocatePool,                         // AllocatePool
  (EFI_FREE_POOL)                               CoreFreePool,                             // FreePool
  (EFI_CREATE_EVENT)                            CoreCreateEvent,                          // CreateEvent
  (EFI_SET_TIMER)                               CoreSetTimer,                             // SetTimer
  (EFI_WAIT_FOR_EVENT)                          CoreWaitForEvent,                         // WaitForEvent
  (EFI_SIGNAL_EVENT)                            CoreSignalEvent,                          // SignalEvent
  (EFI_CLOSE_EVENT)                             CoreCloseEvent,                           // CloseEvent
  (EFI_CHECK_EVENT)                             CoreCheckEvent,                           // CheckEvent
  (EFI_INSTALL_PROTOCOL_INTERFACE)              CoreInstallProtocolInterface,             // InstallProtocolInterface
  (EFI_REINSTALL_PROTOCOL_INTERFACE)            CoreReinstallProtocolInterface,           // ReinstallProtocolInterface
  (EFI_UNINSTALL_PROTOCOL_INTERFACE)            CoreUninstallProtocolInterface,           // UninstallProtocolInterface
  (EFI_HANDLE_PROTOCOL)                         CoreHandleProtocol,                       // HandleProtocol
  (VOID *)                                      NULL,                                     // Reserved
  (EFI_REGISTER_PROTOCOL_NOTIFY)                CoreRegisterProtocolNotify,               // RegisterProtocolNotify
  (EFI_LOCATE_HANDLE)                           CoreLocateHandle,                         // LocateHandle
  (EFI_LOCATE_DEVICE_PATH)                      CoreLocateDevicePath,                     // LocateDevicePath
  (EFI_INSTALL_CONFIGURATION_TABLE)             CoreInstallConfigurationTable,            // InstallConfigurationTable
  (EFI_IMAGE_LOAD)                              CoreLoadImage,                            // LoadImage
  (EFI_IMAGE_START)                             CoreStartImage,                           // StartImage
  (EFI_EXIT)                                    CoreExit,                                 // Exit
  (EFI_IMAGE_UNLOAD)                            CoreUnloadImage,                          // UnloadImage
  (EFI_EXIT_BOOT_SERVICES)                      CoreExitBootServices,                     // ExitBootServices
  (EFI_GET_NEXT_MONOTONIC_COUNT)                CoreEfiNotAvailableYetArg1,               // GetNextMonotonicCount
  (EFI_STALL)                                   CoreStall,                                // Stall
  (EFI_SET_WATCHDOG_TIMER)                      CoreSetWatchdogTimer,                     // SetWatchdogTimer
  (EFI_CONNECT_CONTROLLER)                      CoreConnectController,                    // ConnectController
  (EFI_DISCONNECT_CONTROLLER)                   CoreDisconnectController,                 // DisconnectController
  (EFI_OPEN_PROTOCOL)                           CoreOpenProtocol,                         // OpenProtocol
  (EFI_CLOSE_PROTOCOL)                          CoreCloseProtocol,                        // CloseProtocol
  (EFI_OPEN_PROTOCOL_INFORMATION)               CoreOpenProtocolInformation,              // OpenProtocolInformation
  (EFI_PROTOCOLS_PER_HANDLE)                    CoreProtocolsPerHandle,                   // ProtocolsPerHandle
  (EFI_LOCATE_HANDLE_BUFFER)                    CoreLocateHandleBuffer,                   // LocateHandleBuffer
  (EFI_LOCATE_PROTOCOL)                         CoreLocateProtocol,                       // LocateProtocol
  (EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES)    CoreInstallMultipleProtocolInterfaces,    // InstallMultipleProtocolInterfaces
  (EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES)  CoreUninstallMultipleProtocolInterfaces,  // UninstallMultipleProtocolInterfaces
  (EFI_CALCULATE_CRC32)                         CoreEfiNotAvailableYetArg3,               // CalculateCrc32
  (EFI_COPY_MEM)                                CopyMem,                                  // CopyMem
  (EFI_SET_MEM)                                 SetMem                                    // SetMem
#if (EFI_SPECIFICATION_VERSION >= 0x00020000)
  ,                                   
  (EFI_CREATE_EVENT_EX)                          CoreCreateEventEx                         // CreateEventEx
#endif
};

EFI_DXE_SERVICES mDxeServices = {
  {
    EFI_DXE_SERVICES_SIGNATURE,                                           // Signature
    EFI_DXE_SERVICES_REVISION,                                            // Revision
    sizeof (EFI_DXE_SERVICES),                                            // HeaderSize
    0,                                                                    // CRC32
    0                                                                     // Reserved
  },
  (EFI_ADD_MEMORY_SPACE)             CoreAddMemorySpace,                  // AddMemorySpace
  (EFI_ALLOCATE_MEMORY_SPACE)        CoreAllocateMemorySpace,             // AllocateMemorySpace
  (EFI_FREE_MEMORY_SPACE)            CoreFreeMemorySpace,                 // FreeMemorySpace
  (EFI_REMOVE_MEMORY_SPACE)          CoreRemoveMemorySpace,               // RemoveMemorySpace
  (EFI_GET_MEMORY_SPACE_DESCRIPTOR)  CoreGetMemorySpaceDescriptor,        // GetMemorySpaceDescriptor
  (EFI_SET_MEMORY_SPACE_ATTRIBUTES)  CoreSetMemorySpaceAttributes,        // SetMemorySpaceAttributes
  (EFI_GET_MEMORY_SPACE_MAP)         CoreGetMemorySpaceMap,               // GetMemorySpaceMap
  (EFI_ADD_IO_SPACE)                 CoreAddIoSpace,                      // AddIoSpace
  (EFI_ALLOCATE_IO_SPACE)            CoreAllocateIoSpace,                 // AllocateIoSpace
  (EFI_FREE_IO_SPACE)                CoreFreeIoSpace,                     // FreeIoSpace
  (EFI_REMOVE_IO_SPACE)              CoreRemoveIoSpace,                   // RemoveIoSpace
  (EFI_GET_IO_SPACE_DESCRIPTOR)      CoreGetIoSpaceDescriptor,            // GetIoSpaceDescriptor
  (EFI_GET_IO_SPACE_MAP)             CoreGetIoSpaceMap,                   // GetIoSpaceMap
  (EFI_DISPATCH)                     CoreDispatcher,                      // Dispatch
  (EFI_SCHEDULE)                     CoreSchedule,                        // Schedule
  (EFI_TRUST)                        CoreTrust,                           // Trust
  (EFI_PROCESS_FIRMWARE_VOLUME)      CoreProcessFirmwareVolume,           // ProcessFirmwareVolume
};

EFI_SYSTEM_TABLE mEfiSystemTableTemplate = {
  {
    EFI_SYSTEM_TABLE_SIGNATURE,                                           // Signature
    EFI_SYSTEM_TABLE_REVISION,                                            // Revision
    sizeof (EFI_SYSTEM_TABLE),                                            // HeaderSize
    0,                                                                    // CRC32
    0                                                                     // Reserved
  },
  NULL,                                                                   // FirmwareVendor
  0,                                                                      // FirmwareRevision
  NULL,                                                                   // ConsoleInHandle
  NULL,                                                                   // ConIn
  NULL,                                                                   // ConsoleOutHandle
  NULL,                                                                   // ConOut
  NULL,                                                                   // StandardErrorHandle
  NULL,                                                                   // StdErr
  NULL,                                                                   // RuntimeServices
  &mBootServices,                                                         // BootServices
  0,                                                                      // NumberOfConfigurationTableEntries
  NULL                                                                    // ConfigurationTable
};

EFI_RUNTIME_SERVICES mEfiRuntimeServicesTableTemplate = {
  {
    EFI_RUNTIME_SERVICES_SIGNATURE,                               // Signature
    EFI_RUNTIME_SERVICES_REVISION,                                // Revision
    sizeof (EFI_RUNTIME_SERVICES),                                // HeaderSize
    0,                                                            // CRC32
    0                                                             // Reserved
  },
  (EFI_GET_TIME)                 CoreEfiNotAvailableYetArg2,      // GetTime
  (EFI_SET_TIME)                 CoreEfiNotAvailableYetArg1,      // SetTime
  (EFI_GET_WAKEUP_TIME)          CoreEfiNotAvailableYetArg3,      // GetWakeupTime
  (EFI_SET_WAKEUP_TIME)          CoreEfiNotAvailableYetArg2,      // SetWakeupTime
  (EFI_SET_VIRTUAL_ADDRESS_MAP)  CoreEfiNotAvailableYetArg4,      // SetVirtualAddressMap
  (EFI_CONVERT_POINTER)          CoreEfiNotAvailableYetArg2,      // ConvertPointer
  (EFI_GET_VARIABLE)             CoreEfiNotAvailableYetArg5,      // GetVariable
  (EFI_GET_NEXT_VARIABLE_NAME)   CoreEfiNotAvailableYetArg3,      // GetNextVariableName
  (EFI_SET_VARIABLE)             CoreEfiNotAvailableYetArg5,      // SetVariable
  (EFI_GET_NEXT_HIGH_MONO_COUNT) CoreEfiNotAvailableYetArg1,      // GetNextHighMonotonicCount
  (EFI_RESET_SYSTEM)             CoreEfiNotAvailableYetArg4       // ResetSystem 
#if ((EDK_RELEASE_VERSION != 0) && (EFI_SPECIFICATION_VERSION <  0x00020000))
  //
  // This Tiano extension was removed when UEFI 2.0 support was added. 
  // It's now just a protocol.
  //
  ,
  (EFI_REPORT_STATUS_CODE)       CoreEfiNotAvailableYetArg5       // ReportStatusCode
#elif (EFI_SPECIFICATION_VERSION >= 0x00020000)
  //
  // New runtime services added by UEFI 2.0
  //
  ,
  (EFI_UPDATE_CAPSULE)              CoreEfiNotAvailableYetArg3,   // UpdateCapsule
  (EFI_QUERY_CAPSULE_CAPABILITIES)  CoreEfiNotAvailableYetArg4,   // QueryCapsuleCapabilities
  (EFI_QUERY_VARIABLE_INFO)         CoreEfiNotAvailableYetArg4    // QueryVariableInfo
#endif
};

//
// DXE Core Global Variables for the EFI System Table, Boot Services Table, 
// DXE Services Table, and Runtime Services Table
//
EFI_BOOT_SERVICES     *gBS = &mBootServices;
EFI_DXE_SERVICES      *gDS = &mDxeServices;
EFI_SYSTEM_TABLE      *gST = NULL;

//
// For debug initialize gRT to template. gRT must be allocated from RT memory
//  but gRT is used for ASSERT () and DEBUG () type macros so lets give it
//  a value that will not cause debug infrastructure to crash early on.
//
EFI_RUNTIME_SERVICES  *gRT = &mEfiRuntimeServicesTableTemplate;
EFI_HANDLE            gDxeCoreImageHandle = NULL;

VOID  *mHobStart;

//
// EFI Decompress Protocol
//
EFI_DECOMPRESS_PROTOCOL  gEfiDecompress = {
  DxeMainUefiDecompressGetInfo,
  DxeMainUefiDecompress
};

//
// Tiano Decompress Protocol
//
EFI_TIANO_DECOMPRESS_PROTOCOL  gEfiTianoDecompress = {
  DxeMainTianoDecompressGetInfo,
  DxeMainTianoDecompress
};

//
// Customized Decompress Protocol
//
EFI_CUSTOMIZED_DECOMPRESS_PROTOCOL  gEfiCustomizedDecompress = {
  DxeMainCustomDecompressGetInfo,
  DxeMainCustomDecompress
};

//
// Main entry point to the DXE Core
//
VOID
EFIAPI
DxeMain (
  IN  VOID *HobStart
  )
/*++

Routine Description:

  Main entry point to DXE Core.

Arguments:

  HobStart - Pointer to the beginning of the HOB List from PEI

Returns:

  This function should never return

--*/
{
  EFI_STATUS                         Status;
  EFI_PHYSICAL_ADDRESS               MemoryBaseAddress;
  UINT64                             MemoryLength;

  mHobStart = HobStart;

  //
  // Initialize Memory Services
  //
  CoreInitializeMemoryServices (&HobStart, &MemoryBaseAddress, &MemoryLength);

  //
  // Allocate the EFI System Table and EFI Runtime Service Table from EfiRuntimeServicesData
  // Use the templates to initialize the contents of the EFI System Table and EFI Runtime Services Table
  //
  gST = CoreAllocateRuntimeCopyPool (sizeof (EFI_SYSTEM_TABLE), &mEfiSystemTableTemplate);
  ASSERT (gST != NULL);

  gRT = CoreAllocateRuntimeCopyPool (sizeof (EFI_RUNTIME_SERVICES), &mEfiRuntimeServicesTableTemplate);
  ASSERT (gRT != NULL);

  gST->RuntimeServices = gRT;
  
  //
  // Start the Image Services.
  //
  Status = CoreInitializeImageServices (HobStart);
  ASSERT_EFI_ERROR (Status);

  //
  // Call constructor for all libraries
  //
  ProcessLibraryConstructorList (gDxeCoreImageHandle, gST);

  PERF_START (0,DXE_TOK, NULL, 0) ;

  //
  // Initialize the Global Coherency Domain Services
  //
  Status = CoreInitializeGcdServices (&HobStart, MemoryBaseAddress, MemoryLength);
  ASSERT_EFI_ERROR (Status);

  //
  // Install the DXE Services Table into the EFI System Tables's Configuration Table
  //
  Status = CoreInstallConfigurationTable (&gEfiDxeServicesTableGuid, gDS);
  ASSERT_EFI_ERROR (Status);

  //
  // Install the HOB List into the EFI System Tables's Configuration Table
  //
  Status = CoreInstallConfigurationTable (&gEfiHobListGuid, HobStart);
  ASSERT_EFI_ERROR (Status);

  //
  // Install Memory Type Information Table into the EFI System Tables's Configuration Table
  //
  Status = CoreInstallConfigurationTable (&gEfiMemoryTypeInformationGuid, &gMemoryTypeInformation);
  ASSERT_EFI_ERROR (Status);

  //
  // Initialize the ReportStatusCode with PEI version, if availible
  //
  CoreGetPeiProtocol (&gEfiStatusCodeRuntimeProtocolGuid, (VOID **)&gStatusCode->ReportStatusCode);
#if ((EDK_RELEASE_VERSION != 0) && (EFI_SPECIFICATION_VERSION < 0x00020000))
  //
  // This Tiano extension was removed when UEFI 2.0 support was added. 
  // It's now just a protocol.
  //
  gRT->ReportStatusCode = gStatusCode->ReportStatusCode;
#endif

  //
  // Report Status Code here for DXE_ENTRY_POINT once it is available
  //
  CoreReportProgressCode ((EFI_SOFTWARE_DXE_CORE | EFI_SW_DXE_CORE_PC_ENTRY_POINT));

  //
  // Create the aligned system table pointer structure that is used by external
  // debuggers to locate the system table...  Also, install debug image info
  // configuration table.
  //
  CoreInitializeDebugImageInfoTable ();
  CoreNewDebugImageInfoEntry (
    EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL,
    gDxeCoreLoadedImage,
    gDxeCoreImageHandle
    );

  DEBUG ((EFI_D_INFO | EFI_D_LOAD, "HOBLIST address in DXE = 0x%08x\n", HobStart));

  //
  // Initialize the Event Services
  //
  Status = CoreInitializeEventServices ();
  ASSERT_EFI_ERROR (Status);

   
  //
  // Get the Protocols that were passed in from PEI to DXE through GUIDed HOBs
  //
  // These Protocols are not architectural. This implementation is sharing code between 
  // PEI and DXE in order to save FLASH space. These Protocols could also be implemented 
  // as part of the DXE Core. However, that would also require the DXE Core to be ported 
  // each time a different CPU is used, a different Decompression algorithm is used, or a 
  // different Image type is used. By placing these Protocols in PEI, the DXE Core remains 
  // generic, and only PEI and the Arch Protocols need to be ported from Platform to Platform, 
  // and from CPU to CPU.
  //

  //
  // Publish the EFI, Tiano, and Custom Decompress protocols for use by other DXE components
  //   
  Status = CoreInstallMultipleProtocolInterfaces (
              &mDecompressHandle,
              &gEfiDecompressProtocolGuid,           &gEfiDecompress,
              &gEfiTianoDecompressProtocolGuid,      &gEfiTianoDecompress,
              &gEfiCustomizedDecompressProtocolGuid, &gEfiCustomizedDecompress,
              NULL
              );
  ASSERT_EFI_ERROR (Status);

  gEfiPeiPeCoffLoader = GetPeCoffLoaderProtocol ();
  ASSERT (gEfiPeiPeCoffLoader != NULL);

  //
  // Register for the GUIDs of the Architectural Protocols, so the rest of the
  // EFI Boot Services and EFI Runtime Services tables can be filled in.
  //
  CoreNotifyOnArchProtocolInstallation ();

  //
  // Produce Firmware Volume Protocols, one for each FV in the HOB list.
  //
  Status = FwVolBlockDriverInit (gDxeCoreImageHandle, gST);
  ASSERT_EFI_ERROR (Status);

  Status = FwVolDriverInit (gDxeCoreImageHandle, gST);
  ASSERT_EFI_ERROR (Status);

  //
  // Produce the Section Extraction Protocol 
  //
  Status = InitializeSectionExtraction (gDxeCoreImageHandle, gST);
  ASSERT_EFI_ERROR (Status);

  //
  // Initialize the DXE Dispatcher
  //
  PERF_START (0,"CoreInitializeDispatcher", "DxeMain", 0) ;
  CoreInitializeDispatcher ();
  PERF_END (0,"CoreInitializeDispatcher", "DxeMain", 0) ;

  //
  // Invoke the DXE Dispatcher
  //
  PERF_START (0, "CoreDispatcher", "DxeMain", 0);
  CoreDispatcher ();
  PERF_END (0, "CoreDispatcher", "DxeMain", 0);

  //
  // Display Architectural protocols that were not loaded if this is DEBUG build
  //
  DEBUG_CODE_BEGIN ();
    CoreDisplayMissingArchProtocols ();
  DEBUG_CODE_END ();
  
  //
  // Assert if the Architectural Protocols are not present.
  //
  ASSERT_EFI_ERROR (CoreAllEfiServicesAvailable ());

  //
  // Report Status code before transfer control to BDS
  //
  CoreReportProgressCode ((EFI_SOFTWARE_DXE_CORE | EFI_SW_DXE_CORE_PC_HANDOFF_TO_NEXT));
  //
  // Display any drivers that were not dispatched because dependency expression
  // evaluated to false if this is a debug build
  //
  DEBUG_CODE_BEGIN ();
    CoreDisplayDiscoveredNotDispatched ();
  DEBUG_CODE_END ();

  //
  // Transfer control to the BDS Architectural Protocol
  //
  gBds->Entry (gBds);
  
  //
  // BDS should never return
  //
  ASSERT (FALSE);
  CpuDeadLoop ();
}


EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg0 (
  VOID
  )
/*++

Routine Description:

  Place holder function until all the Boot Services and Runtime Services are available

Arguments:

  None

Returns:

  EFI_NOT_AVAILABLE_YET

--*/
{
  //
  // This function should never be executed.  If it does, then the architectural protocols
  // have not been designed correctly.  The CpuBreakpoint () is commented out for now until the
  // DXE Core and all the Architectural Protocols are complete.
  //

  return EFI_NOT_AVAILABLE_YET;
}

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg1 (
  UINTN Arg1
  )
/*++

Routine Description:

  Place holder function until all the Boot Services and Runtime Services are available

Arguments:

  Arg1        - Undefined

Returns:

  EFI_NOT_AVAILABLE_YET

--*/
{
  //
  // This function should never be executed.  If it does, then the architectural protocols
  // have not been designed correctly.  The CpuBreakpoint () is commented out for now until the
  // DXE Core and all the Architectural Protocols are complete.
  //

  return EFI_NOT_AVAILABLE_YET;
}

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg2 (
  UINTN Arg1,
  UINTN Arg2
  )
/*++

Routine Description:

  Place holder function until all the Boot Services and Runtime Services are available

Arguments:

  Arg1        - Undefined
  
  Arg2        - Undefined

Returns:

  EFI_NOT_AVAILABLE_YET

--*/
{
  //
  // This function should never be executed.  If it does, then the architectural protocols
  // have not been designed correctly.  The CpuBreakpoint () is commented out for now until the
  // DXE Core and all the Architectural Protocols are complete.
  //

  return EFI_NOT_AVAILABLE_YET;
}

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg3 (
  UINTN Arg1,
  UINTN Arg2,
  UINTN Arg3
  )
/*++

Routine Description:

  Place holder function until all the Boot Services and Runtime Services are available

Arguments:

  Arg1        - Undefined
  
  Arg2        - Undefined
  
  Arg3        - Undefined

Returns:

  EFI_NOT_AVAILABLE_YET

--*/
{
  //
  // This function should never be executed.  If it does, then the architectural protocols
  // have not been designed correctly.  The CpuBreakpoint () is commented out for now until the
  // DXE Core and all the Architectural Protocols are complete.
  //

  return EFI_NOT_AVAILABLE_YET;
}

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg4 (
  UINTN Arg1,
  UINTN Arg2,
  UINTN Arg3,
  UINTN Arg4
  )
/*++

Routine Description:

  Place holder function until all the Boot Services and Runtime Services are available

Arguments:

  Arg1        - Undefined
  
  Arg2        - Undefined
  
  Arg3        - Undefined
  
  Arg4        - Undefined

Returns:

  EFI_NOT_AVAILABLE_YET

--*/
{
  //
  // This function should never be executed.  If it does, then the architectural protocols
  // have not been designed correctly.  The CpuBreakpoint () is commented out for now until the
  // DXE Core and all the Architectural Protocols are complete.
  //

  return EFI_NOT_AVAILABLE_YET;
}

EFI_STATUS
EFIAPI
CoreEfiNotAvailableYetArg5 (
  UINTN Arg1,
  UINTN Arg2,
  UINTN Arg3,
  UINTN Arg4,
  UINTN Arg5
  )
/*++

Routine Description:

  Place holder function until all the Boot Services and Runtime Services are available

Arguments:

  Arg1        - Undefined
  
  Arg2        - Undefined
  
  Arg3        - Undefined
  
  Arg4        - Undefined
  
  Arg5        - Undefined

Returns:

  EFI_NOT_AVAILABLE_YET

--*/
{
  //
  // This function should never be executed.  If it does, then the architectural protocols
  // have not been designed correctly.  The CpuBreakpoint () is commented out for now until the
  // DXE Core and all the Architectural Protocols are complete.
  //

  return EFI_NOT_AVAILABLE_YET;
}


EFI_STATUS
CoreGetPeiProtocol (
  IN EFI_GUID  *ProtocolGuid,
  IN VOID      **Interface
  )
/*++

Routine Description:

  Searches for a Protocol Interface passed from PEI through a HOB

Arguments:

  ProtocolGuid - The Protocol GUID to search for in the HOB List

  Interface    - A pointer to the interface for the Protocol GUID

Returns:

  EFI_SUCCESS   - The Protocol GUID was found and its interface is returned in Interface

  EFI_NOT_FOUND - The Protocol GUID was not found in the HOB List

--*/
{
  EFI_HOB_GUID_TYPE   *GuidHob;
  VOID                *Buffer;

  GuidHob = GetNextGuidHob (ProtocolGuid, mHobStart);
  if (GuidHob == NULL) {
    return EFI_NOT_FOUND;
  }
  
  Buffer = GET_GUID_HOB_DATA (GuidHob);
  ASSERT (Buffer != NULL);

  *Interface = (VOID *)(*(UINTN *)(Buffer));

  return EFI_SUCCESS;
}


VOID
CalculateEfiHdrCrc (
  IN  OUT EFI_TABLE_HEADER    *Hdr
  )
/*++

Routine Description:

  Calcualte the 32-bit CRC in a EFI table using the service provided by the
  gRuntime service.

Arguments:

  Hdr  - Pointer to an EFI standard header

Returns:

  None

--*/
{
  UINT32 Crc;

  Hdr->CRC32 = 0;
  
  //
  // If gBS->CalculateCrce32 () == CoreEfiNotAvailableYet () then
  //  Crc will come back as zero if we set it to zero here
  //
  Crc = 0;
  gBS->CalculateCrc32 ((UINT8 *)Hdr, Hdr->HeaderSize, &Crc);
  Hdr->CRC32 = Crc; 
}



EFI_STATUS
EFIAPI
CoreExitBootServices (
  IN EFI_HANDLE   ImageHandle,
  IN UINTN        MapKey
  )
/*++

Routine Description:

  EFI 1.0 API to terminate Boot Services

Arguments:

  ImageHandle  - Handle that represents the identity of the calling image

  MapKey -Key to the latest memory map.

Returns:

  EFI_SUCCESS - Boot Services terminated
  EFI_INVALID_PARAMETER - MapKey is incorrect.

--*/
{
  EFI_STATUS    Status;

  //
  // Terminate memory services if the MapKey matches
  //
  Status = CoreTerminateMemoryMap (MapKey);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // Notify other drivers that we are exiting boot services.
  //
  CoreNotifySignalList (&gEfiEventExitBootServicesGuid);

  //
  // Disable Timer
  //
  gTimer->SetTimerPeriod (gTimer, 0);

  //
  // Disable CPU Interrupts
  //
  gCpu->DisableInterrupt (gCpu);

  //
  // Register Runtime events with the Runtime Architectural Protocol
  //
  CoreShutdownEventServices ();

  //
  // Register Runtime images with the Runtime Architectural Protocol
  //
  CoreShutdownImageServices ();

  //
  // Report that ExitBootServices() has been called
  //
  // We are using gEfiDxeServicesTableGuid as the caller ID for Dxe Core
  //
  CoreReportProgressCode ((EFI_SOFTWARE_EFI_BOOT_SERVICE | EFI_SW_BS_PC_EXIT_BOOT_SERVICES));

  //
  // Clear the non-runtime values of the EFI System Table
  //
  gST->BootServices        = NULL;
  gST->ConIn               = NULL;
  gST->ConsoleInHandle     = NULL;
  gST->ConOut              = NULL;
  gST->ConsoleOutHandle    = NULL;
  gST->StdErr              = NULL;
  gST->StandardErrorHandle = NULL;

  //
  // Recompute the 32-bit CRC of the EFI System Table
  //
  CalculateEfiHdrCrc (&gST->Hdr);

  //
  // Zero out the Boot Service Table
  //
  SetMem (gBS, sizeof (EFI_BOOT_SERVICES), 0);
  gBS = NULL;

  return Status;
}

EFI_STATUS
DxeMainUefiDecompressGetInfo (
  IN EFI_DECOMPRESS_PROTOCOL            *This,
  IN   VOID                             *Source,
  IN   UINT32                           SourceSize,
  OUT  UINT32                           *DestinationSize,
  OUT  UINT32                           *ScratchSize
  )
{
  return UefiDecompressGetInfo (Source, SourceSize, DestinationSize, ScratchSize);
}

EFI_STATUS
EFIAPI
DxeMainUefiDecompress (
  IN EFI_DECOMPRESS_PROTOCOL              *This,
  IN     VOID                             *Source,
  IN     UINT32                           SourceSize,
  IN OUT VOID                             *Destination,
  IN     UINT32                           DestinationSize,
  IN OUT VOID                             *Scratch,
  IN     UINT32                           ScratchSize
  )
{
  EFI_STATUS  Status;
  UINT32      TestDestinationSize;
  UINT32      TestScratchSize;

  Status = UefiDecompressGetInfo (Source, SourceSize, &TestDestinationSize, &TestScratchSize);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  if (ScratchSize < TestScratchSize || DestinationSize < TestDestinationSize) {
    return RETURN_INVALID_PARAMETER;
  }

  return UefiDecompress (Source, Destination, Scratch);
}

EFI_STATUS
DxeMainTianoDecompressGetInfo (
  IN EFI_TIANO_DECOMPRESS_PROTOCOL      *This,
  IN   VOID                             *Source,
  IN   UINT32                           SourceSize,
  OUT  UINT32                           *DestinationSize,
  OUT  UINT32                           *ScratchSize
  )
{
  return TianoDecompressGetInfo (Source, SourceSize, DestinationSize, ScratchSize);
}

EFI_STATUS
EFIAPI
DxeMainTianoDecompress (
  IN EFI_TIANO_DECOMPRESS_PROTOCOL        *This,
  IN     VOID                             *Source,
  IN     UINT32                           SourceSize,
  IN OUT VOID                             *Destination,
  IN     UINT32                           DestinationSize,
  IN OUT VOID                             *Scratch,
  IN     UINT32                           ScratchSize
  )
{
  EFI_STATUS  Status;
  UINT32      TestDestinationSize;
  UINT32      TestScratchSize;

  Status = TianoDecompressGetInfo (Source, SourceSize, &TestDestinationSize, &TestScratchSize);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  if (ScratchSize < TestScratchSize || DestinationSize < TestDestinationSize) {
    return RETURN_INVALID_PARAMETER;
  }

  return TianoDecompress (Source, Destination, Scratch);
}

EFI_STATUS
DxeMainCustomDecompressGetInfo (
  IN EFI_CUSTOMIZED_DECOMPRESS_PROTOCOL  *This,
  IN   VOID                              *Source,
  IN   UINT32                            SourceSize,
  OUT  UINT32                            *DestinationSize,
  OUT  UINT32                            *ScratchSize
  )
{
  return CustomDecompressGetInfo (Source, SourceSize, DestinationSize, ScratchSize);
}

EFI_STATUS
EFIAPI
DxeMainCustomDecompress (
  IN EFI_CUSTOMIZED_DECOMPRESS_PROTOCOL  *This,
  IN     VOID                            *Source,
  IN     UINT32                          SourceSize,
  IN OUT VOID                            *Destination,
  IN     UINT32                          DestinationSize,
  IN OUT VOID                            *Scratch,
  IN     UINT32                          ScratchSize
  )
{
  EFI_STATUS  Status;
  UINT32      TestDestinationSize;
  UINT32      TestScratchSize;

  Status = CustomDecompressGetInfo (Source, SourceSize, &TestDestinationSize, &TestScratchSize);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  if (ScratchSize < TestScratchSize || DestinationSize < TestDestinationSize) {
    return RETURN_INVALID_PARAMETER;
  }

  return CustomDecompress (Source, Destination, Scratch);
}