1 /** @file
2 
3   Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.<BR>
4   SPDX-License-Identifier: BSD-2-Clause-Patent
5 
6 **/
7 
8 #include "SecFsp.h"
9 
10 /**
11 
12   Calculate the FSP IDT gate descriptor.
13 
14   @param[in] IdtEntryTemplate     IDT gate descriptor template.
15 
16   @return                     FSP specific IDT gate descriptor.
17 
18 **/
19 UINT64
FspGetExceptionHandler(IN UINT64 IdtEntryTemplate)20 FspGetExceptionHandler(
21   IN  UINT64  IdtEntryTemplate
22   )
23 {
24   UINT32                    Entry;
25   UINT64                    ExceptionHandler;
26   IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor;
27   FSP_INFO_HEADER          *FspInfoHeader;
28 
29   FspInfoHeader     = (FSP_INFO_HEADER *)AsmGetFspInfoHeader();
30   ExceptionHandler  = IdtEntryTemplate;
31   IdtGateDescriptor = (IA32_IDT_GATE_DESCRIPTOR *)&ExceptionHandler;
32   Entry = (IdtGateDescriptor->Bits.OffsetHigh << 16) | IdtGateDescriptor->Bits.OffsetLow;
33   Entry = FspInfoHeader->ImageBase + FspInfoHeader->ImageSize - (~Entry + 1);
34   IdtGateDescriptor->Bits.OffsetHigh = (UINT16)(Entry >> 16);
35   IdtGateDescriptor->Bits.OffsetLow  = (UINT16)Entry;
36 
37   return ExceptionHandler;
38 }
39 
40 /**
41   This interface fills platform specific data.
42 
43   @param[in,out]  FspData           Pointer to the FSP global data.
44 
45 **/
46 VOID
47 EFIAPI
SecGetPlatformData(IN OUT FSP_GLOBAL_DATA * FspData)48 SecGetPlatformData (
49   IN OUT  FSP_GLOBAL_DATA    *FspData
50   )
51 {
52   FSP_PLAT_DATA    *FspPlatformData;
53   UINT32            TopOfCar;
54   UINT32           *StackPtr;
55   UINT32            DwordSize;
56 
57   FspPlatformData = &FspData->PlatformData;
58 
59   //
60   // The entries of platform information, together with the number of them,
61   // reside in the bottom of stack, left untouched by normal stack operation.
62   //
63 
64   FspPlatformData->DataPtr   = NULL;
65   FspPlatformData->MicrocodeRegionBase = 0;
66   FspPlatformData->MicrocodeRegionSize = 0;
67   FspPlatformData->CodeRegionBase      = 0;
68   FspPlatformData->CodeRegionSize      = 0;
69 
70   //
71   // Pointer to the size field
72   //
73   TopOfCar = PcdGet32(PcdTemporaryRamBase) + PcdGet32(PcdTemporaryRamSize);
74   StackPtr = (UINT32 *)(TopOfCar - sizeof (UINT32));
75 
76   if (*(StackPtr - 1) == FSP_MCUD_SIGNATURE) {
77     while (*StackPtr != 0) {
78       if (*(StackPtr - 1) == FSP_MCUD_SIGNATURE) {
79         //
80         // This following data was pushed onto stack after TempRamInit API
81         //
82         DwordSize = 4;
83         StackPtr  = StackPtr - 1 - DwordSize;
84         CopyMem (&(FspPlatformData->MicrocodeRegionBase), StackPtr, (DwordSize << 2));
85         StackPtr--;
86       } else if (*(StackPtr - 1) == FSP_PER0_SIGNATURE) {
87         //
88         // This is the performance data for InitTempMemory API entry/exit
89         //
90         DwordSize = 4;
91         StackPtr  = StackPtr - 1 - DwordSize;
92         CopyMem (FspData->PerfData, StackPtr, (DwordSize << 2));
93 
94         ((UINT8 *)(&FspData->PerfData[0]))[7] = FSP_PERF_ID_API_TEMP_RAM_INIT_ENTRY;
95         ((UINT8 *)(&FspData->PerfData[1]))[7] = FSP_PERF_ID_API_TEMP_RAM_INIT_EXIT;
96 
97         StackPtr--;
98       } else {
99         StackPtr -= (*StackPtr);
100       }
101     }
102   }
103 }
104 
105 /**
106 
107   Initialize the FSP global data region.
108   It needs to be done as soon as possible after the stack is setup.
109 
110   @param[in,out] PeiFspData             Pointer of the FSP global data.
111   @param[in]     BootLoaderStack        BootLoader stack.
112   @param[in]     ApiIdx                 The index of the FSP API.
113 
114 **/
115 VOID
FspGlobalDataInit(IN OUT FSP_GLOBAL_DATA * PeiFspData,IN UINT32 BootLoaderStack,IN UINT8 ApiIdx)116 FspGlobalDataInit (
117   IN OUT  FSP_GLOBAL_DATA    *PeiFspData,
118   IN UINT32                   BootLoaderStack,
119   IN UINT8                    ApiIdx
120   )
121 {
122   VOID              *FspmUpdDataPtr;
123   CHAR8              ImageId[9];
124   UINTN              Idx;
125 
126   //
127   // Set FSP Global Data pointer
128   //
129   SetFspGlobalDataPointer    (PeiFspData);
130   ZeroMem  ((VOID *)PeiFspData, sizeof(FSP_GLOBAL_DATA));
131 
132   PeiFspData->Signature            = FSP_GLOBAL_DATA_SIGNATURE;
133   PeiFspData->Version              = 0;
134   PeiFspData->CoreStack            = BootLoaderStack;
135   PeiFspData->PerfIdx              = 2;
136   PeiFspData->PerfSig              = FSP_PERFORMANCE_DATA_SIGNATURE;
137 
138   SetFspMeasurePoint (FSP_PERF_ID_API_FSP_MEMORY_INIT_ENTRY);
139 
140   //
141   // Get FSP Header offset
142   // It may have multiple FVs, so look into the last one for FSP header
143   //
144   PeiFspData->FspInfoHeader      = (FSP_INFO_HEADER *)AsmGetFspInfoHeader();
145   SecGetPlatformData (PeiFspData);
146 
147   //
148   // Set API calling mode
149   //
150   SetFspApiCallingIndex (ApiIdx);
151 
152   //
153   // Set UPD pointer
154   //
155   FspmUpdDataPtr = (VOID *) GetFspApiParameter ();
156   if (FspmUpdDataPtr == NULL) {
157     FspmUpdDataPtr = (VOID *)(PeiFspData->FspInfoHeader->ImageBase + PeiFspData->FspInfoHeader->CfgRegionOffset);
158   }
159   SetFspUpdDataPointer (FspmUpdDataPtr);
160   SetFspMemoryInitUpdDataPointer (FspmUpdDataPtr);
161   SetFspSiliconInitUpdDataPointer (NULL);
162 
163   //
164   // Initialize serial port
165   // It might have been done in ProcessLibraryConstructorList(), however,
166   // the FSP global data is not initialized at that time. So do it again
167   // for safe.
168   //
169   SerialPortInitialize ();
170 
171   //
172   // Ensure the global data pointer is valid
173   //
174   ASSERT (GetFspGlobalDataPointer () == PeiFspData);
175 
176   for (Idx = 0; Idx < 8; Idx++) {
177     ImageId[Idx] = PeiFspData->FspInfoHeader->ImageId[Idx];
178   }
179   ImageId[Idx] = 0;
180 
181   DEBUG ((DEBUG_INFO | DEBUG_INIT, "\n============= FSP Spec v%d.%d Header Revision v%x (%a v%x.%x.%x.%x) =============\n", \
182          (PeiFspData->FspInfoHeader->SpecVersion >> 4) & 0xF, \
183          PeiFspData->FspInfoHeader->SpecVersion & 0xF, \
184          PeiFspData->FspInfoHeader->HeaderRevision, \
185          ImageId, \
186          (PeiFspData->FspInfoHeader->ImageRevision >> 24) & 0xFF, \
187          (PeiFspData->FspInfoHeader->ImageRevision >> 16) & 0xFF, \
188          (PeiFspData->FspInfoHeader->ImageRevision >> 8) & 0xFF, \
189          PeiFspData->FspInfoHeader->ImageRevision & 0xFF));
190 }
191 
192 /**
193 
194   Adjust the FSP data pointers after the stack is migrated to memory.
195 
196   @param[in] OffsetGap             The offset gap between the old stack and the new stack.
197 
198 **/
199 VOID
FspDataPointerFixUp(IN UINT32 OffsetGap)200 FspDataPointerFixUp (
201   IN UINT32   OffsetGap
202   )
203 {
204   FSP_GLOBAL_DATA  *NewFspData;
205 
206   NewFspData = (FSP_GLOBAL_DATA *)((UINTN)GetFspGlobalDataPointer() + (UINTN)OffsetGap);
207   SetFspGlobalDataPointer (NewFspData);
208 }
209