]>
Commit | Line | Data |
---|---|---|
70a51d71 SV |
1 | /** @file\r |
2 | HOB Library implementation for Standalone MM Core.\r | |
3 | \r | |
4 | Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r | |
5 | Copyright (c) 2017 - 2018, ARM Limited. All rights reserved.<BR>\r | |
6 | \r | |
86094561 | 7 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
70a51d71 SV |
8 | \r |
9 | **/\r | |
10 | \r | |
11 | #include <PiMm.h>\r | |
12 | \r | |
13 | #include <Library/HobLib.h>\r | |
14 | #include <Library/DebugLib.h>\r | |
15 | #include <Library/BaseMemoryLib.h>\r | |
16 | \r | |
17 | #include <Guid/MemoryAllocationHob.h>\r | |
18 | \r | |
19 | //\r | |
20 | // Cache copy of HobList pointer.\r | |
21 | //\r | |
91415a36 | 22 | VOID *gHobList = NULL;\r |
70a51d71 | 23 | \r |
70a51d71 SV |
24 | VOID *\r |
25 | CreateHob (\r | |
91415a36 MK |
26 | IN UINT16 HobType,\r |
27 | IN UINT16 HobLength\r | |
70a51d71 SV |
28 | )\r |
29 | {\r | |
30 | EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;\r | |
31 | EFI_HOB_GENERIC_HEADER *HobEnd;\r | |
32 | EFI_PHYSICAL_ADDRESS FreeMemory;\r | |
33 | VOID *Hob;\r | |
34 | \r | |
35 | HandOffHob = GetHobList ();\r | |
36 | \r | |
37 | HobLength = (UINT16)((HobLength + 0x7) & (~0x7));\r | |
38 | \r | |
39 | FreeMemory = HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryBottom;\r | |
40 | \r | |
41 | if (FreeMemory < HobLength) {\r | |
42 | return NULL;\r | |
43 | }\r | |
44 | \r | |
91415a36 MK |
45 | Hob = (VOID *)(UINTN)HandOffHob->EfiEndOfHobList;\r |
46 | ((EFI_HOB_GENERIC_HEADER *)Hob)->HobType = HobType;\r | |
47 | ((EFI_HOB_GENERIC_HEADER *)Hob)->HobLength = HobLength;\r | |
48 | ((EFI_HOB_GENERIC_HEADER *)Hob)->Reserved = 0;\r | |
70a51d71 | 49 | \r |
91415a36 MK |
50 | HobEnd = (EFI_HOB_GENERIC_HEADER *)((UINTN)Hob + HobLength);\r |
51 | HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;\r | |
70a51d71 SV |
52 | \r |
53 | HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;\r | |
54 | HobEnd->HobLength = sizeof (EFI_HOB_GENERIC_HEADER);\r | |
55 | HobEnd->Reserved = 0;\r | |
56 | HobEnd++;\r | |
91415a36 | 57 | HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;\r |
70a51d71 SV |
58 | \r |
59 | return Hob;\r | |
60 | }\r | |
61 | \r | |
62 | /**\r | |
63 | Builds a HOB for a loaded PE32 module.\r | |
64 | \r | |
65 | This function builds a HOB for a loaded PE32 module.\r | |
66 | If ModuleName is NULL, then ASSERT().\r | |
67 | If there is no additional space for HOB creation, then ASSERT().\r | |
68 | \r | |
69 | @param ModuleName The GUID File Name of the module.\r | |
70 | @param MemoryAllocationModule The 64 bit physical address of the module.\r | |
71 | @param ModuleLength The length of the module in bytes.\r | |
72 | @param EntryPoint The 64 bit physical address of the module entry point.\r | |
73 | \r | |
74 | **/\r | |
75 | VOID\r | |
76 | EFIAPI\r | |
77 | BuildModuleHob (\r | |
91415a36 MK |
78 | IN CONST EFI_GUID *ModuleName,\r |
79 | IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,\r | |
80 | IN UINT64 ModuleLength,\r | |
81 | IN EFI_PHYSICAL_ADDRESS EntryPoint\r | |
70a51d71 SV |
82 | )\r |
83 | {\r | |
84 | EFI_HOB_MEMORY_ALLOCATION_MODULE *Hob;\r | |
85 | \r | |
91415a36 MK |
86 | ASSERT (\r |
87 | ((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&\r | |
88 | ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0)\r | |
89 | );\r | |
70a51d71 SV |
90 | \r |
91 | Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));\r | |
92 | \r | |
93 | CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);\r | |
94 | Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;\r | |
95 | Hob->MemoryAllocationHeader.MemoryLength = ModuleLength;\r | |
96 | Hob->MemoryAllocationHeader.MemoryType = EfiBootServicesCode;\r | |
97 | \r | |
98 | //\r | |
99 | // Zero the reserved space to match HOB spec\r | |
100 | //\r | |
101 | ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved));\r | |
102 | \r | |
103 | CopyGuid (&Hob->ModuleName, ModuleName);\r | |
104 | Hob->EntryPoint = EntryPoint;\r | |
105 | }\r | |
106 | \r | |
107 | /**\r | |
108 | Builds a HOB that describes a chunk of system memory.\r | |
109 | \r | |
110 | This function builds a HOB that describes a chunk of system memory.\r | |
111 | If there is no additional space for HOB creation, then ASSERT().\r | |
112 | \r | |
113 | @param ResourceType The type of resource described by this HOB.\r | |
114 | @param ResourceAttribute The resource attributes of the memory described by this HOB.\r | |
115 | @param PhysicalStart The 64 bit physical address of memory described by this HOB.\r | |
116 | @param NumberOfBytes The length of the memory described by this HOB in bytes.\r | |
117 | \r | |
118 | **/\r | |
119 | VOID\r | |
120 | EFIAPI\r | |
121 | BuildResourceDescriptorHob (\r | |
122 | IN EFI_RESOURCE_TYPE ResourceType,\r | |
123 | IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,\r | |
124 | IN EFI_PHYSICAL_ADDRESS PhysicalStart,\r | |
125 | IN UINT64 NumberOfBytes\r | |
126 | )\r | |
127 | {\r | |
128 | EFI_HOB_RESOURCE_DESCRIPTOR *Hob;\r | |
129 | \r | |
130 | Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));\r | |
131 | ASSERT (Hob != NULL);\r | |
132 | \r | |
133 | Hob->ResourceType = ResourceType;\r | |
134 | Hob->ResourceAttribute = ResourceAttribute;\r | |
135 | Hob->PhysicalStart = PhysicalStart;\r | |
136 | Hob->ResourceLength = NumberOfBytes;\r | |
137 | }\r | |
138 | \r | |
139 | /**\r | |
140 | Builds a GUID HOB with a certain data length.\r | |
141 | \r | |
142 | This function builds a customized HOB tagged with a GUID for identification\r | |
143 | and returns the start address of GUID HOB data so that caller can fill the customized data.\r | |
144 | The HOB Header and Name field is already stripped.\r | |
145 | If Guid is NULL, then ASSERT().\r | |
146 | If there is no additional space for HOB creation, then ASSERT().\r | |
147 | If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().\r | |
148 | \r | |
149 | @param Guid The GUID to tag the customized HOB.\r | |
150 | @param DataLength The size of the data payload for the GUID HOB.\r | |
151 | \r | |
152 | @return The start address of GUID HOB data.\r | |
153 | \r | |
154 | **/\r | |
155 | VOID *\r | |
156 | EFIAPI\r | |
157 | BuildGuidHob (\r | |
91415a36 MK |
158 | IN CONST EFI_GUID *Guid,\r |
159 | IN UINTN DataLength\r | |
70a51d71 SV |
160 | )\r |
161 | {\r | |
91415a36 | 162 | EFI_HOB_GUID_TYPE *Hob;\r |
70a51d71 SV |
163 | \r |
164 | //\r | |
165 | // Make sure that data length is not too long.\r | |
166 | //\r | |
167 | ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));\r | |
168 | \r | |
91415a36 | 169 | Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16)(sizeof (EFI_HOB_GUID_TYPE) + DataLength));\r |
70a51d71 SV |
170 | CopyGuid (&Hob->Name, Guid);\r |
171 | return Hob + 1;\r | |
172 | }\r | |
173 | \r | |
70a51d71 SV |
174 | /**\r |
175 | Copies a data buffer to a newly-built HOB.\r | |
176 | \r | |
177 | This function builds a customized HOB tagged with a GUID for identification,\r | |
178 | copies the input data to the HOB data field and returns the start address of the GUID HOB data.\r | |
179 | The HOB Header and Name field is already stripped.\r | |
180 | If Guid is NULL, then ASSERT().\r | |
181 | If Data is NULL and DataLength > 0, then ASSERT().\r | |
182 | If there is no additional space for HOB creation, then ASSERT().\r | |
183 | If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().\r | |
184 | \r | |
185 | @param Guid The GUID to tag the customized HOB.\r | |
186 | @param Data The data to be copied into the data field of the GUID HOB.\r | |
187 | @param DataLength The size of the data payload for the GUID HOB.\r | |
188 | \r | |
189 | @return The start address of GUID HOB data.\r | |
190 | \r | |
191 | **/\r | |
192 | VOID *\r | |
193 | EFIAPI\r | |
194 | BuildGuidDataHob (\r | |
91415a36 MK |
195 | IN CONST EFI_GUID *Guid,\r |
196 | IN VOID *Data,\r | |
197 | IN UINTN DataLength\r | |
70a51d71 SV |
198 | )\r |
199 | {\r | |
200 | VOID *HobData;\r | |
201 | \r | |
202 | ASSERT (Data != NULL || DataLength == 0);\r | |
203 | \r | |
204 | HobData = BuildGuidHob (Guid, DataLength);\r | |
205 | \r | |
206 | return CopyMem (HobData, Data, DataLength);\r | |
207 | }\r | |
208 | \r | |
209 | /**\r | |
210 | Builds a Firmware Volume HOB.\r | |
211 | \r | |
212 | This function builds a Firmware Volume HOB.\r | |
213 | If there is no additional space for HOB creation, then ASSERT().\r | |
214 | \r | |
215 | @param BaseAddress The base address of the Firmware Volume.\r | |
216 | @param Length The size of the Firmware Volume in bytes.\r | |
217 | \r | |
218 | **/\r | |
219 | VOID\r | |
220 | EFIAPI\r | |
221 | BuildFvHob (\r | |
91415a36 MK |
222 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r |
223 | IN UINT64 Length\r | |
70a51d71 SV |
224 | )\r |
225 | {\r | |
226 | EFI_HOB_FIRMWARE_VOLUME *Hob;\r | |
227 | \r | |
228 | Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));\r | |
229 | \r | |
230 | Hob->BaseAddress = BaseAddress;\r | |
231 | Hob->Length = Length;\r | |
232 | }\r | |
233 | \r | |
70a51d71 SV |
234 | /**\r |
235 | Builds a EFI_HOB_TYPE_FV2 HOB.\r | |
236 | \r | |
237 | This function builds a EFI_HOB_TYPE_FV2 HOB.\r | |
238 | If there is no additional space for HOB creation, then ASSERT().\r | |
239 | \r | |
240 | @param BaseAddress The base address of the Firmware Volume.\r | |
241 | @param Length The size of the Firmware Volume in bytes.\r | |
242 | @param FvName The name of the Firmware Volume.\r | |
243 | @param FileName The name of the file.\r | |
244 | \r | |
245 | **/\r | |
246 | VOID\r | |
247 | EFIAPI\r | |
248 | BuildFv2Hob (\r | |
91415a36 MK |
249 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r |
250 | IN UINT64 Length,\r | |
251 | IN CONST EFI_GUID *FvName,\r | |
252 | IN CONST EFI_GUID *FileName\r | |
70a51d71 SV |
253 | )\r |
254 | {\r | |
255 | EFI_HOB_FIRMWARE_VOLUME2 *Hob;\r | |
256 | \r | |
257 | Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof (EFI_HOB_FIRMWARE_VOLUME2));\r | |
258 | \r | |
259 | Hob->BaseAddress = BaseAddress;\r | |
260 | Hob->Length = Length;\r | |
261 | CopyGuid (&Hob->FvName, FvName);\r | |
262 | CopyGuid (&Hob->FileName, FileName);\r | |
263 | }\r | |
264 | \r | |
70a51d71 SV |
265 | /**\r |
266 | Builds a HOB for the CPU.\r | |
267 | \r | |
268 | This function builds a HOB for the CPU.\r | |
269 | If there is no additional space for HOB creation, then ASSERT().\r | |
270 | \r | |
271 | @param SizeOfMemorySpace The maximum physical memory addressability of the processor.\r | |
272 | @param SizeOfIoSpace The maximum physical I/O addressability of the processor.\r | |
273 | \r | |
274 | **/\r | |
275 | VOID\r | |
276 | EFIAPI\r | |
277 | BuildCpuHob (\r | |
91415a36 MK |
278 | IN UINT8 SizeOfMemorySpace,\r |
279 | IN UINT8 SizeOfIoSpace\r | |
70a51d71 SV |
280 | )\r |
281 | {\r | |
282 | EFI_HOB_CPU *Hob;\r | |
283 | \r | |
284 | Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));\r | |
285 | \r | |
286 | Hob->SizeOfMemorySpace = SizeOfMemorySpace;\r | |
287 | Hob->SizeOfIoSpace = SizeOfIoSpace;\r | |
288 | \r | |
289 | //\r | |
290 | // Zero the reserved space to match HOB spec\r | |
291 | //\r | |
292 | ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));\r | |
293 | }\r | |
294 | \r | |
295 | /**\r | |
296 | Builds a HOB for the memory allocation.\r | |
297 | \r | |
298 | This function builds a HOB for the memory allocation.\r | |
299 | If there is no additional space for HOB creation, then ASSERT().\r | |
300 | \r | |
301 | @param BaseAddress The 64 bit physical address of the memory.\r | |
302 | @param Length The length of the memory allocation in bytes.\r | |
303 | @param MemoryType Type of memory allocated by this HOB.\r | |
304 | \r | |
305 | **/\r | |
306 | VOID\r | |
307 | EFIAPI\r | |
308 | BuildMemoryAllocationHob (\r | |
91415a36 MK |
309 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r |
310 | IN UINT64 Length,\r | |
311 | IN EFI_MEMORY_TYPE MemoryType\r | |
70a51d71 SV |
312 | )\r |
313 | {\r | |
314 | EFI_HOB_MEMORY_ALLOCATION *Hob;\r | |
315 | \r | |
91415a36 MK |
316 | ASSERT (\r |
317 | ((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&\r | |
318 | ((Length & (EFI_PAGE_SIZE - 1)) == 0)\r | |
319 | );\r | |
70a51d71 SV |
320 | \r |
321 | Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));\r | |
322 | \r | |
323 | ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));\r | |
324 | Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r | |
325 | Hob->AllocDescriptor.MemoryLength = Length;\r | |
326 | Hob->AllocDescriptor.MemoryType = MemoryType;\r | |
327 | //\r | |
328 | // Zero the reserved space to match HOB spec\r | |
329 | //\r | |
330 | ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r | |
331 | }\r |