]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Hob/Hob.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Hob / Hob.c
CommitLineData
615c6dd0 1/** @file\r
93b8ed68 2 This module provide Hand-Off Block manipulation.\r
d1102dba 3\r
d39d1260 4Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
9d510e61 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
192f6d4c 6\r
b1f6a7c6 7**/\r
192f6d4c 8\r
0d516397 9#include "PeiMain.h"\r
192f6d4c 10\r
b1f6a7c6 11/**\r
192f6d4c 12\r
b1f6a7c6 13 Gets the pointer to the HOB List.\r
192f6d4c 14\r
d73d93c3 15 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
b1f6a7c6 16 @param HobList Pointer to the HOB List.\r
17\r
18 @retval EFI_SUCCESS Get the pointer of HOB List\r
19 @retval EFI_NOT_AVAILABLE_YET the HOB List is not yet published\r
20 @retval EFI_INVALID_PARAMETER HobList is NULL (in debug mode)\r
192f6d4c 21\r
b1f6a7c6 22**/\r
192f6d4c 23EFI_STATUS\r
24EFIAPI\r
25PeiGetHobList (\r
0c2b5da8 26 IN CONST EFI_PEI_SERVICES **PeiServices,\r
1436aea4 27 IN OUT VOID **HobList\r
192f6d4c 28 )\r
192f6d4c 29{\r
1436aea4 30 PEI_CORE_INSTANCE *PrivateData;\r
d1102dba 31\r
192f6d4c 32 //\r
33 // Only check this parameter in debug mode\r
34 //\r
d1102dba
LG
35\r
36 DEBUG_CODE_BEGIN ();\r
1436aea4
MK
37 if (HobList == NULL) {\r
38 return EFI_INVALID_PARAMETER;\r
39 }\r
40\r
192f6d4c 41 DEBUG_CODE_END ();\r
d1102dba 42\r
1436aea4 43 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
192f6d4c 44\r
1436aea4 45 *HobList = PrivateData->HobList.Raw;\r
192f6d4c 46\r
d1102dba 47 return EFI_SUCCESS;\r
192f6d4c 48}\r
49\r
b1f6a7c6 50/**\r
51 Add a new HOB to the HOB List.\r
52\r
d73d93c3 53 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
40f26b8f 54 @param Type Type of the new HOB.\r
55 @param Length Length of the new HOB to allocate.\r
56 @param Hob Pointer to the new HOB.\r
b1f6a7c6 57\r
d39d1260 58 @return EFI_SUCCESS Success to create HOB.\r
b1f6a7c6 59 @retval EFI_INVALID_PARAMETER if Hob is NULL\r
60 @retval EFI_NOT_AVAILABLE_YET if HobList is still not available.\r
61 @retval EFI_OUT_OF_RESOURCES if there is no more memory to grow the Hoblist.\r
62\r
63**/\r
192f6d4c 64EFI_STATUS\r
65EFIAPI\r
66PeiCreateHob (\r
0c2b5da8 67 IN CONST EFI_PEI_SERVICES **PeiServices,\r
1436aea4
MK
68 IN UINT16 Type,\r
69 IN UINT16 Length,\r
70 IN OUT VOID **Hob\r
192f6d4c 71 )\r
192f6d4c 72{\r
1436aea4
MK
73 EFI_STATUS Status;\r
74 EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;\r
75 EFI_HOB_GENERIC_HEADER *HobEnd;\r
76 EFI_PHYSICAL_ADDRESS FreeMemory;\r
192f6d4c 77\r
78 Status = PeiGetHobList (PeiServices, Hob);\r
1436aea4 79 if (EFI_ERROR (Status)) {\r
192f6d4c 80 return Status;\r
81 }\r
82\r
83 HandOffHob = *Hob;\r
84\r
e94728b3 85 //\r
d1102dba 86 // Check Length to avoid data overflow.\r
e94728b3
LG
87 //\r
88 if (0x10000 - Length <= 0x7) {\r
89 return EFI_INVALID_PARAMETER;\r
90 }\r
1436aea4
MK
91\r
92 Length = (UINT16)((Length + 0x7) & (~0x7));\r
192f6d4c 93\r
94 FreeMemory = HandOffHob->EfiFreeMemoryTop -\r
95 HandOffHob->EfiFreeMemoryBottom;\r
96\r
97 if (FreeMemory < Length) {\r
87000d77
MK
98 DEBUG ((DEBUG_ERROR, "PeiCreateHob fail: Length - 0x%08x\n", (UINTN)Length));\r
99 DEBUG ((DEBUG_ERROR, " FreeMemoryTop - 0x%08x\n", (UINTN)HandOffHob->EfiFreeMemoryTop));\r
100 DEBUG ((DEBUG_ERROR, " FreeMemoryBottom - 0x%08x\n", (UINTN)HandOffHob->EfiFreeMemoryBottom));\r
192f6d4c 101 return EFI_OUT_OF_RESOURCES;\r
102 }\r
d1102dba 103\r
1436aea4
MK
104 *Hob = (VOID *)(UINTN)HandOffHob->EfiEndOfHobList;\r
105 ((EFI_HOB_GENERIC_HEADER *)*Hob)->HobType = Type;\r
106 ((EFI_HOB_GENERIC_HEADER *)*Hob)->HobLength = Length;\r
107 ((EFI_HOB_GENERIC_HEADER *)*Hob)->Reserved = 0;\r
192f6d4c 108\r
1436aea4
MK
109 HobEnd = (EFI_HOB_GENERIC_HEADER *)((UINTN)*Hob + Length);\r
110 HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;\r
d1102dba 111\r
192f6d4c 112 HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;\r
1436aea4 113 HobEnd->HobLength = (UINT16)sizeof (EFI_HOB_GENERIC_HEADER);\r
192f6d4c 114 HobEnd->Reserved = 0;\r
115 HobEnd++;\r
1436aea4 116 HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;\r
192f6d4c 117\r
d1102dba 118 return EFI_SUCCESS;\r
192f6d4c 119}\r
120\r
483e2cdd
SZ
121/**\r
122 Install SEC HOB data to the HOB List.\r
123\r
124 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
125 @param SecHobList Pointer to SEC HOB List.\r
126\r
127 @return EFI_SUCCESS Success to install SEC HOB data.\r
128 @retval EFI_OUT_OF_RESOURCES If there is no more memory to grow the Hoblist.\r
129\r
130**/\r
131EFI_STATUS\r
132PeiInstallSecHobData (\r
1436aea4
MK
133 IN CONST EFI_PEI_SERVICES **PeiServices,\r
134 IN EFI_HOB_GENERIC_HEADER *SecHobList\r
483e2cdd
SZ
135 )\r
136{\r
1436aea4
MK
137 EFI_STATUS Status;\r
138 EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;\r
139 EFI_PEI_HOB_POINTERS HobStart;\r
140 EFI_PEI_HOB_POINTERS Hob;\r
141 UINTN SecHobListLength;\r
142 EFI_PHYSICAL_ADDRESS FreeMemory;\r
143 EFI_HOB_GENERIC_HEADER *HobEnd;\r
483e2cdd
SZ
144\r
145 HandOffHob = NULL;\r
1436aea4
MK
146 Status = PeiGetHobList (PeiServices, (VOID **)&HandOffHob);\r
147 if (EFI_ERROR (Status)) {\r
483e2cdd
SZ
148 return Status;\r
149 }\r
1436aea4 150\r
483e2cdd
SZ
151 ASSERT (HandOffHob != NULL);\r
152\r
1436aea4 153 HobStart.Raw = (UINT8 *)SecHobList;\r
483e2cdd
SZ
154 //\r
155 // The HobList must not contain a EFI_HOB_HANDOFF_INFO_TABLE HOB (PHIT) HOB.\r
156 //\r
157 ASSERT (HobStart.Header->HobType != EFI_HOB_TYPE_HANDOFF);\r
158 //\r
159 // Calculate the SEC HOB List length,\r
160 // not including the terminated HOB(EFI_HOB_TYPE_END_OF_HOB_LIST).\r
161 //\r
1436aea4
MK
162 for (Hob.Raw = HobStart.Raw; !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {\r
163 }\r
164\r
165 SecHobListLength = (UINTN)Hob.Raw - (UINTN)HobStart.Raw;\r
483e2cdd
SZ
166 //\r
167 // The length must be 8-bytes aligned.\r
168 //\r
169 ASSERT ((SecHobListLength & 0x7) == 0);\r
170\r
171 FreeMemory = HandOffHob->EfiFreeMemoryTop -\r
172 HandOffHob->EfiFreeMemoryBottom;\r
173\r
174 if (FreeMemory < SecHobListLength) {\r
175 DEBUG ((DEBUG_ERROR, "PeiInstallSecHobData fail: SecHobListLength - 0x%08x\n", SecHobListLength));\r
176 DEBUG ((DEBUG_ERROR, " FreeMemoryTop - 0x%08x\n", (UINTN)HandOffHob->EfiFreeMemoryTop));\r
177 DEBUG ((DEBUG_ERROR, " FreeMemoryBottom - 0x%08x\n", (UINTN)HandOffHob->EfiFreeMemoryBottom));\r
178 return EFI_OUT_OF_RESOURCES;\r
179 }\r
180\r
1436aea4 181 Hob.Raw = (UINT8 *)(UINTN)HandOffHob->EfiEndOfHobList;\r
483e2cdd
SZ
182 CopyMem (Hob.Raw, HobStart.Raw, SecHobListLength);\r
183\r
1436aea4
MK
184 HobEnd = (EFI_HOB_GENERIC_HEADER *)((UINTN)Hob.Raw + SecHobListLength);\r
185 HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;\r
483e2cdd
SZ
186\r
187 HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;\r
1436aea4 188 HobEnd->HobLength = (UINT16)sizeof (EFI_HOB_GENERIC_HEADER);\r
483e2cdd
SZ
189 HobEnd->Reserved = 0;\r
190 HobEnd++;\r
1436aea4 191 HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;\r
483e2cdd
SZ
192\r
193 return EFI_SUCCESS;\r
194}\r
195\r
b1f6a7c6 196/**\r
197\r
198 Builds a Handoff Information Table HOB\r
199\r
200 @param BootMode - Current Bootmode\r
201 @param MemoryBegin - Start Memory Address.\r
202 @param MemoryLength - Length of Memory.\r
203\r
204 @return EFI_SUCCESS Always success to initialize HOB.\r
205\r
206**/\r
192f6d4c 207EFI_STATUS\r
208PeiCoreBuildHobHandoffInfoTable (\r
209 IN EFI_BOOT_MODE BootMode,\r
210 IN EFI_PHYSICAL_ADDRESS MemoryBegin,\r
211 IN UINT64 MemoryLength\r
212 )\r
192f6d4c 213{\r
1436aea4
MK
214 EFI_HOB_HANDOFF_INFO_TABLE *Hob;\r
215 EFI_HOB_GENERIC_HEADER *HobEnd;\r
192f6d4c 216\r
1436aea4
MK
217 Hob = (VOID *)(UINTN)MemoryBegin;\r
218 HobEnd = (EFI_HOB_GENERIC_HEADER *)(Hob+1);\r
219 Hob->Header.HobType = EFI_HOB_TYPE_HANDOFF;\r
220 Hob->Header.HobLength = (UINT16)sizeof (EFI_HOB_HANDOFF_INFO_TABLE);\r
221 Hob->Header.Reserved = 0;\r
d1102dba 222\r
1436aea4
MK
223 HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;\r
224 HobEnd->HobLength = (UINT16)sizeof (EFI_HOB_GENERIC_HEADER);\r
225 HobEnd->Reserved = 0;\r
192f6d4c 226\r
1436aea4
MK
227 Hob->Version = EFI_HOB_HANDOFF_TABLE_VERSION;\r
228 Hob->BootMode = BootMode;\r
d1102dba 229\r
192f6d4c 230 Hob->EfiMemoryTop = MemoryBegin + MemoryLength;\r
231 Hob->EfiMemoryBottom = MemoryBegin;\r
232 Hob->EfiFreeMemoryTop = MemoryBegin + MemoryLength;\r
1436aea4
MK
233 Hob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)(HobEnd + 1);\r
234 Hob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;\r
192f6d4c 235\r
236 return EFI_SUCCESS;\r
237}\r