Add IntelFspWrapper to support boot EDKII on FSP bin.
[mirror_edk2.git] / IntelFspWrapperPkg / FspNotifyDxe / FspNotifyDxe.c
1 /** @file
2 This driver will register two callbacks to call fsp's notifies.
3
4 Copyright (c) 2014, 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 <PiDxe.h>
16
17 #include <Protocol/PciEnumerationComplete.h>
18
19 #include <Library/UefiDriverEntryPoint.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/UefiLib.h>
24 #include <Library/FspApiLib.h>
25
26 FSP_INFO_HEADER *mFspHeader = NULL;
27
28 /**
29 PciEnumerationComplete Protocol notification event handler.
30
31 @param[in] Event Event whose notification function is being invoked.
32 @param[in] Context Pointer to the notification function's context.
33 **/
34 VOID
35 EFIAPI
36 OnPciEnumerationComplete (
37 IN EFI_EVENT Event,
38 IN VOID *Context
39 )
40 {
41 NOTIFY_PHASE_PARAMS NotifyPhaseParams;
42 EFI_STATUS Status;
43 FSP_STATUS FspStatus;
44 VOID *Interface;
45
46 //
47 // Try to locate it because gEfiPciEnumerationCompleteProtocolGuid will trigger it once when registration.
48 // Just return if it is not found.
49 //
50 Status = gBS->LocateProtocol (
51 &gEfiPciEnumerationCompleteProtocolGuid,
52 NULL,
53 &Interface
54 );
55 if (EFI_ERROR (Status)) {
56 return ;
57 }
58
59 NotifyPhaseParams.Phase = EnumInitPhaseAfterPciEnumeration;
60 FspStatus = CallFspNotifyPhase (mFspHeader, &NotifyPhaseParams);
61 if (FspStatus != FSP_SUCCESS) {
62 DEBUG((DEBUG_ERROR, "FSP NotifyPhase AfterPciEnumeration failed, status: 0x%x\n", FspStatus));
63 } else {
64 DEBUG((DEBUG_INFO, "FSP NotifyPhase AfterPciEnumeration Success.\n"));
65 }
66 }
67
68 /**
69 Notification function of EVT_GROUP_READY_TO_BOOT event group.
70
71 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
72 When the Boot Manager is about to load and execute a boot option, it reclaims variable
73 storage if free size is below the threshold.
74
75 @param[in] Event Event whose notification function is being invoked.
76 @param[in] Context Pointer to the notification function's context.
77
78 **/
79 VOID
80 EFIAPI
81 OnReadyToBoot (
82 IN EFI_EVENT Event,
83 IN VOID *Context
84 )
85 {
86 NOTIFY_PHASE_PARAMS NotifyPhaseParams;
87 FSP_STATUS FspStatus;
88
89 gBS->CloseEvent (Event);
90
91 NotifyPhaseParams.Phase = EnumInitPhaseReadyToBoot;
92 FspStatus = CallFspNotifyPhase (mFspHeader, &NotifyPhaseParams);
93 if (FspStatus != FSP_SUCCESS) {
94 DEBUG((DEBUG_ERROR, "FSP NotifyPhase ReadyToBoot failed, status: 0x%x\n", FspStatus));
95 } else {
96 DEBUG((DEBUG_INFO, "FSP NotifyPhase ReadyToBoot Success.\n"));
97 }
98 }
99
100 /**
101 Main entry for the FSP DXE module.
102
103 This routine registers two callbacks to call fsp's notifies.
104
105 @param[in] ImageHandle The firmware allocated handle for the EFI image.
106 @param[in] SystemTable A pointer to the EFI System Table.
107
108 @retval EFI_SUCCESS The entry point is executed successfully.
109 @retval other Some error occurs when executing this entry point.
110
111 **/
112 EFI_STATUS
113 EFIAPI
114 FspDxeEntryPoint (
115 IN EFI_HANDLE ImageHandle,
116 IN EFI_SYSTEM_TABLE *SystemTable
117 )
118 {
119 EFI_STATUS Status;
120 EFI_EVENT ReadyToBootEvent;
121 VOID *Registration;
122 EFI_EVENT ProtocolNotifyEvent;
123
124 mFspHeader = FspFindFspHeader (PcdGet32 (PcdFlashFvFspBase));
125 DEBUG ((DEBUG_INFO, "FspHeader - 0x%x\n", mFspHeader));
126 if (mFspHeader == NULL) {
127 return EFI_DEVICE_ERROR;
128 }
129
130 ProtocolNotifyEvent = EfiCreateProtocolNotifyEvent (
131 &gEfiPciEnumerationCompleteProtocolGuid,
132 TPL_CALLBACK,
133 OnPciEnumerationComplete,
134 NULL,
135 &Registration
136 );
137 ASSERT (ProtocolNotifyEvent != NULL);
138
139 Status = EfiCreateEventReadyToBootEx (
140 TPL_CALLBACK,
141 OnReadyToBoot,
142 NULL,
143 &ReadyToBootEvent
144 );
145 ASSERT_EFI_ERROR (Status);
146
147 return EFI_SUCCESS;
148 }
149