Add IntelFspWrapper to support boot EDKII on FSP bin.
[mirror_edk2.git] / IntelFspWrapperPkg / FspInitPei / FspInitPei.c
1 /** @file
2 This PEIM will be invoked twice by pei core. In 1st entry, it will call FspInit API.
3 In 2nd entry, it will parse the hoblist from fsp and report them into pei core.
4 This file contains the main entrypoint of the PEIM.
5
6 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17
18 #include "FspInitPei.h"
19
20 /**
21 FSP Init continuation function.
22 Control will be returned to this callback function after FspInit API call.
23
24 @param[in] Status Status of the FSP INIT API
25 @param[in] HobListPtr Pointer to the HOB data structure defined in the PI specification.
26
27 **/
28 VOID
29 ContinuationFunc (
30 IN FSP_STATUS Status,
31 IN VOID *HobListPtr
32 )
33 {
34 EFI_BOOT_MODE BootMode;
35 UINT64 StackSize;
36 EFI_PHYSICAL_ADDRESS StackBase;
37
38 DEBUG ((DEBUG_INFO, "ContinuationFunc - %r\n", Status));
39 DEBUG ((DEBUG_INFO, "HobListPtr - 0x%x\n", HobListPtr));
40
41 if (Status != FSP_SUCCESS) {
42 CpuDeadLoop ();
43 }
44
45 //
46 // Can not call any PeiServices
47 //
48 BootMode = GetBootMode ();
49
50 GetStackInfo (BootMode, TRUE, &StackBase, &StackSize);
51 DEBUG ((DEBUG_INFO, "StackBase - 0x%x\n", StackBase));
52 DEBUG ((DEBUG_INFO, "StackSize - 0x%x\n", StackSize));
53 CallPeiCoreEntryPoint (
54 HobListPtr,
55 (VOID *)(UINTN)StackBase,
56 (VOID *)(UINTN)(StackBase + StackSize)
57 );
58 }
59
60 /**
61 Call FspInit API.
62
63 @param[in] FspHeader FSP header pointer.
64 **/
65 VOID
66 SecFspInit (
67 IN FSP_INFO_HEADER *FspHeader
68 )
69 {
70 FSP_INIT_PARAMS FspInitParams;
71 FSP_INIT_RT_COMMON_BUFFER FspRtBuffer;
72 UINT8 FspUpdRgn[FixedPcdGet32 (PcdMaxUpdRegionSize)];
73 UINT32 UpdRegionSize;
74 EFI_BOOT_MODE BootMode;
75 UINT64 StackSize;
76 EFI_PHYSICAL_ADDRESS StackBase;
77 FSP_STATUS FspStatus;
78
79 DEBUG ((DEBUG_INFO, "SecFspInit enter\n"));
80
81 PeiServicesGetBootMode (&BootMode);
82 DEBUG ((DEBUG_INFO, "BootMode - 0x%x\n", BootMode));
83
84 GetStackInfo (BootMode, FALSE, &StackBase, &StackSize);
85 DEBUG ((DEBUG_INFO, "StackBase - 0x%x\n", StackBase));
86 DEBUG ((DEBUG_INFO, "StackSize - 0x%x\n", StackSize));
87
88 ZeroMem (&FspRtBuffer, sizeof(FspRtBuffer));
89 FspRtBuffer.StackTop = (UINT32 *)(UINTN)(StackBase + StackSize);
90
91 FspRtBuffer.BootMode = BootMode;
92
93 /* Platform override any UPD configs */
94 UpdRegionSize = GetUpdRegionSize();
95 DEBUG ((DEBUG_INFO, "UpdRegionSize - 0x%x\n", UpdRegionSize));
96 DEBUG ((DEBUG_INFO, "sizeof(FspUpdRgn) - 0x%x\n", sizeof(FspUpdRgn)));
97 ASSERT(sizeof(FspUpdRgn) >= UpdRegionSize);
98 ZeroMem (FspUpdRgn, UpdRegionSize);
99 FspRtBuffer.UpdDataRgnPtr = UpdateFspUpdConfigs (FspUpdRgn);
100
101 ZeroMem (&FspInitParams, sizeof(FspInitParams));
102 FspInitParams.NvsBufferPtr = GetNvsBuffer ();
103 DEBUG ((DEBUG_INFO, "NvsBufferPtr - 0x%x\n", FspInitParams.NvsBufferPtr));
104 FspInitParams.RtBufferPtr = (VOID *)&FspRtBuffer;
105 FspInitParams.ContinuationFunc = (CONTINUATION_PROC)ContinuationFunc;
106
107 SaveSecContext (GetPeiServicesTablePointer ());
108
109 DEBUG ((DEBUG_INFO, "FspInitParams - 0x%x\n", &FspInitParams));
110 DEBUG ((DEBUG_INFO, " NvsBufferPtr - 0x%x\n", FspInitParams.NvsBufferPtr));
111 DEBUG ((DEBUG_INFO, " RtBufferPtr - 0x%x\n", FspInitParams.RtBufferPtr));
112 DEBUG ((DEBUG_INFO, " StackTop - 0x%x\n", FspRtBuffer.StackTop));
113 DEBUG ((DEBUG_INFO, " BootMode - 0x%x\n", FspRtBuffer.BootMode));
114 DEBUG ((DEBUG_INFO, " UpdDataRgnPtr - 0x%x\n", FspRtBuffer.UpdDataRgnPtr));
115 DEBUG ((DEBUG_INFO, " ContinuationFunc - 0x%x\n", FspInitParams.ContinuationFunc));
116
117 FspStatus = CallFspInit (FspHeader, &FspInitParams);
118 //
119 // Should never return
120 //
121 DEBUG((DEBUG_ERROR, "FSP Init failed, status: 0x%x\n", FspStatus));
122 CpuDeadLoop ();
123 }
124
125 /**
126 This is the entrypoint of PEIM
127
128 @param[in] FileHandle Handle of the file being invoked.
129 @param[in] PeiServices Describes the list of possible PEI Services.
130
131 @retval EFI_SUCCESS if it completed successfully.
132 **/
133 EFI_STATUS
134 EFIAPI
135 FspPeiEntryPoint (
136 IN EFI_PEI_FILE_HANDLE FileHandle,
137 IN CONST EFI_PEI_SERVICES **PeiServices
138 )
139 {
140 FSP_INFO_HEADER *FspHeader;
141 EFI_STATUS Status;
142 FSP_INIT_DONE_PPI *FspInitDone;
143 VOID *FspHobList;
144 EFI_BOOT_MODE BootMode;
145
146 DEBUG ((DEBUG_INFO, "FspPeiEntryPoint\n"));
147
148 Status = PeiServicesLocatePpi (
149 &gFspInitDonePpiGuid,
150 0,
151 NULL,
152 (VOID **) &FspInitDone
153 );
154 if (EFI_ERROR (Status)) {
155 //
156 // 1st entry
157 //
158 DEBUG ((DEBUG_INFO, "1st entry\n"));
159 FspHeader = FspFindFspHeader (PcdGet32 (PcdFlashFvFspBase));
160 DEBUG ((DEBUG_INFO, "FspHeader - 0x%x\n", FspHeader));
161 if (FspHeader == NULL) {
162 return EFI_DEVICE_ERROR;
163 }
164
165 SecFspInit (FspHeader);
166
167 //
168 // Never return here
169 //
170 CpuDeadLoop ();
171 } else {
172 //
173 // 2nd entry
174 //
175 DEBUG ((DEBUG_INFO, "2nd entry\n"));
176 Status = FspInitDone->GetFspHobList (PeiServices, FspInitDone, &FspHobList);
177 DEBUG ((DEBUG_INFO, "FspHobList - 0x%x\n", FspHobList));
178 FspHobProcess (FspHobList);
179
180 PeiServicesGetBootMode (&BootMode);
181 if (BootMode == BOOT_ON_S3_RESUME) {
182 Status = PeiServicesNotifyPpi (&mS3EndOfPeiNotifyDesc);
183 ASSERT_EFI_ERROR (Status);
184 }
185 }
186
187 return EFI_SUCCESS;
188 }