]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Library/StandaloneMmCoreHobLib/StandaloneMmCoreHobLib.c
StandaloneMmPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmCoreHobLib / StandaloneMmCoreHobLib.c
CommitLineData
70a51d71
SV
1/** @file\r
2 HOB Library implementation for Standalone MM Core.\r
3\r
4Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
5Copyright (c) 2017 - 2018, ARM Limited. All rights reserved.<BR>\r
6\r
86094561 7SPDX-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
22VOID *gHobList = NULL;\r
23\r
24/**\r
25 Returns the pointer to the HOB list.\r
26\r
27 This function returns the pointer to first HOB in the list.\r
28 If the pointer to the HOB list is NULL, then ASSERT().\r
29\r
30 @return The pointer to the HOB list.\r
31\r
32**/\r
33VOID *\r
34EFIAPI\r
35GetHobList (\r
36 VOID\r
37 )\r
38{\r
39 ASSERT (gHobList != NULL);\r
40 return gHobList;\r
41}\r
42\r
43/**\r
44 Returns the next instance of a HOB type from the starting HOB.\r
45\r
46 This function searches the first instance of a HOB type from the starting HOB pointer.\r
47 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.\r
48 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer\r
49 unconditionally: it returns HobStart back if HobStart itself meets the requirement;\r
50 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.\r
51\r
52 If HobStart is NULL, then ASSERT().\r
53\r
54 @param Type The HOB type to return.\r
55 @param HobStart The starting HOB pointer to search from.\r
56\r
57 @return The next instance of a HOB type from the starting HOB.\r
58\r
59**/\r
60VOID *\r
61EFIAPI\r
62GetNextHob (\r
63 IN UINT16 Type,\r
64 IN CONST VOID *HobStart\r
65 )\r
66{\r
67 EFI_PEI_HOB_POINTERS Hob;\r
68\r
69 ASSERT (HobStart != NULL);\r
70\r
71 Hob.Raw = (UINT8 *) HobStart;\r
72 //\r
73 // Parse the HOB list until end of list or matching type is found.\r
74 //\r
75 while (!END_OF_HOB_LIST (Hob)) {\r
76 if (Hob.Header->HobType == Type) {\r
77 return Hob.Raw;\r
78 }\r
79 Hob.Raw = GET_NEXT_HOB (Hob);\r
80 }\r
81 return NULL;\r
82}\r
83\r
84/**\r
85 Returns the first instance of a HOB type among the whole HOB list.\r
86\r
87 This function searches the first instance of a HOB type among the whole HOB list.\r
88 If there does not exist such HOB type in the HOB list, it will return NULL.\r
89\r
90 If the pointer to the HOB list is NULL, then ASSERT().\r
91\r
92 @param Type The HOB type to return.\r
93\r
94 @return The next instance of a HOB type from the starting HOB.\r
95\r
96**/\r
97VOID *\r
98EFIAPI\r
99GetFirstHob (\r
100 IN UINT16 Type\r
101 )\r
102{\r
103 VOID *HobList;\r
104\r
105 HobList = GetHobList ();\r
106 return GetNextHob (Type, HobList);\r
107}\r
108\r
109/**\r
110 Returns the next instance of the matched GUID HOB from the starting HOB.\r
111\r
112 This function searches the first instance of a HOB from the starting HOB pointer.\r
113 Such HOB should satisfy two conditions:\r
114 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name equals to the input Guid.\r
115 If such a HOB from the starting HOB pointer does not exist, it will return NULL.\r
116 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()\r
117 to extract the data section and its size information, respectively.\r
118 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer\r
119 unconditionally: it returns HobStart back if HobStart itself meets the requirement;\r
120 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.\r
121\r
122 If Guid is NULL, then ASSERT().\r
123 If HobStart is NULL, then ASSERT().\r
124\r
125 @param Guid The GUID to match with in the HOB list.\r
126 @param HobStart A pointer to a Guid.\r
127\r
128 @return The next instance of the matched GUID HOB from the starting HOB.\r
129\r
130**/\r
131VOID *\r
132EFIAPI\r
133GetNextGuidHob (\r
134 IN CONST EFI_GUID *Guid,\r
135 IN CONST VOID *HobStart\r
136 )\r
137{\r
138 EFI_PEI_HOB_POINTERS GuidHob;\r
139\r
140 GuidHob.Raw = (UINT8 *) HobStart;\r
141 while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {\r
142 if (CompareGuid (Guid, &GuidHob.Guid->Name)) {\r
143 break;\r
144 }\r
145 GuidHob.Raw = GET_NEXT_HOB (GuidHob);\r
146 }\r
147 return GuidHob.Raw;\r
148}\r
149\r
150/**\r
151 Returns the first instance of the matched GUID HOB among the whole HOB list.\r
152\r
153 This function searches the first instance of a HOB among the whole HOB list.\r
154 Such HOB should satisfy two conditions:\r
155 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.\r
156 If such a HOB from the starting HOB pointer does not exist, it will return NULL.\r
157 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()\r
158 to extract the data section and its size information, respectively.\r
159\r
160 If the pointer to the HOB list is NULL, then ASSERT().\r
161 If Guid is NULL, then ASSERT().\r
162\r
163 @param Guid The GUID to match with in the HOB list.\r
164\r
165 @return The first instance of the matched GUID HOB among the whole HOB list.\r
166\r
167**/\r
168VOID *\r
169EFIAPI\r
170GetFirstGuidHob (\r
171 IN CONST EFI_GUID *Guid\r
172 )\r
173{\r
174 VOID *HobList;\r
175\r
176 HobList = GetHobList ();\r
177 return GetNextGuidHob (Guid, HobList);\r
178}\r
179\r
180/**\r
181 Get the system boot mode from the HOB list.\r
182\r
183 This function returns the system boot mode information from the\r
184 PHIT HOB in HOB list.\r
185\r
186 If the pointer to the HOB list is NULL, then ASSERT().\r
187\r
188 @param VOID\r
189\r
190 @return The Boot Mode.\r
191\r
192**/\r
193EFI_BOOT_MODE\r
194EFIAPI\r
195GetBootModeHob (\r
196 VOID\r
197 )\r
198{\r
199 EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;\r
200\r
201 HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *) GetHobList ();\r
202\r
203 return HandOffHob->BootMode;\r
204}\r
205\r
206VOID *\r
207CreateHob (\r
208 IN UINT16 HobType,\r
209 IN UINT16 HobLength\r
210 )\r
211{\r
212 EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;\r
213 EFI_HOB_GENERIC_HEADER *HobEnd;\r
214 EFI_PHYSICAL_ADDRESS FreeMemory;\r
215 VOID *Hob;\r
216\r
217 HandOffHob = GetHobList ();\r
218\r
219 HobLength = (UINT16)((HobLength + 0x7) & (~0x7));\r
220\r
221 FreeMemory = HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryBottom;\r
222\r
223 if (FreeMemory < HobLength) {\r
224 return NULL;\r
225 }\r
226\r
227 Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;\r
228 ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType;\r
229 ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength;\r
230 ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0;\r
231\r
232 HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength);\r
233 HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;\r
234\r
235 HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;\r
236 HobEnd->HobLength = sizeof (EFI_HOB_GENERIC_HEADER);\r
237 HobEnd->Reserved = 0;\r
238 HobEnd++;\r
239 HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;\r
240\r
241 return Hob;\r
242}\r
243\r
244/**\r
245 Builds a HOB for a loaded PE32 module.\r
246\r
247 This function builds a HOB for a loaded PE32 module.\r
248 If ModuleName is NULL, then ASSERT().\r
249 If there is no additional space for HOB creation, then ASSERT().\r
250\r
251 @param ModuleName The GUID File Name of the module.\r
252 @param MemoryAllocationModule The 64 bit physical address of the module.\r
253 @param ModuleLength The length of the module in bytes.\r
254 @param EntryPoint The 64 bit physical address of the module entry point.\r
255\r
256**/\r
257VOID\r
258EFIAPI\r
259BuildModuleHob (\r
260 IN CONST EFI_GUID *ModuleName,\r
261 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,\r
262 IN UINT64 ModuleLength,\r
263 IN EFI_PHYSICAL_ADDRESS EntryPoint\r
264 )\r
265{\r
266 EFI_HOB_MEMORY_ALLOCATION_MODULE *Hob;\r
267\r
268 ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&\r
269 ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0));\r
270\r
271 Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));\r
272\r
273 CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);\r
274 Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;\r
275 Hob->MemoryAllocationHeader.MemoryLength = ModuleLength;\r
276 Hob->MemoryAllocationHeader.MemoryType = EfiBootServicesCode;\r
277\r
278 //\r
279 // Zero the reserved space to match HOB spec\r
280 //\r
281 ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved));\r
282\r
283 CopyGuid (&Hob->ModuleName, ModuleName);\r
284 Hob->EntryPoint = EntryPoint;\r
285}\r
286\r
287/**\r
288 Builds a HOB that describes a chunk of system memory.\r
289\r
290 This function builds a HOB that describes a chunk of system memory.\r
291 If there is no additional space for HOB creation, then ASSERT().\r
292\r
293 @param ResourceType The type of resource described by this HOB.\r
294 @param ResourceAttribute The resource attributes of the memory described by this HOB.\r
295 @param PhysicalStart The 64 bit physical address of memory described by this HOB.\r
296 @param NumberOfBytes The length of the memory described by this HOB in bytes.\r
297\r
298**/\r
299VOID\r
300EFIAPI\r
301BuildResourceDescriptorHob (\r
302 IN EFI_RESOURCE_TYPE ResourceType,\r
303 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,\r
304 IN EFI_PHYSICAL_ADDRESS PhysicalStart,\r
305 IN UINT64 NumberOfBytes\r
306 )\r
307{\r
308 EFI_HOB_RESOURCE_DESCRIPTOR *Hob;\r
309\r
310 Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));\r
311 ASSERT (Hob != NULL);\r
312\r
313 Hob->ResourceType = ResourceType;\r
314 Hob->ResourceAttribute = ResourceAttribute;\r
315 Hob->PhysicalStart = PhysicalStart;\r
316 Hob->ResourceLength = NumberOfBytes;\r
317}\r
318\r
319/**\r
320 Builds a GUID HOB with a certain data length.\r
321\r
322 This function builds a customized HOB tagged with a GUID for identification\r
323 and returns the start address of GUID HOB data so that caller can fill the customized data.\r
324 The HOB Header and Name field is already stripped.\r
325 If Guid is NULL, then ASSERT().\r
326 If there is no additional space for HOB creation, then ASSERT().\r
327 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().\r
328\r
329 @param Guid The GUID to tag the customized HOB.\r
330 @param DataLength The size of the data payload for the GUID HOB.\r
331\r
332 @return The start address of GUID HOB data.\r
333\r
334**/\r
335VOID *\r
336EFIAPI\r
337BuildGuidHob (\r
338 IN CONST EFI_GUID *Guid,\r
339 IN UINTN DataLength\r
340 )\r
341{\r
342 EFI_HOB_GUID_TYPE *Hob;\r
343\r
344 //\r
345 // Make sure that data length is not too long.\r
346 //\r
347 ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));\r
348\r
349 Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength));\r
350 CopyGuid (&Hob->Name, Guid);\r
351 return Hob + 1;\r
352}\r
353\r
354\r
355/**\r
356 Copies a data buffer to a newly-built HOB.\r
357\r
358 This function builds a customized HOB tagged with a GUID for identification,\r
359 copies the input data to the HOB data field and returns the start address of the GUID HOB data.\r
360 The HOB Header and Name field is already stripped.\r
361 If Guid is NULL, then ASSERT().\r
362 If Data is NULL and DataLength > 0, then ASSERT().\r
363 If there is no additional space for HOB creation, then ASSERT().\r
364 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().\r
365\r
366 @param Guid The GUID to tag the customized HOB.\r
367 @param Data The data to be copied into the data field of the GUID HOB.\r
368 @param DataLength The size of the data payload for the GUID HOB.\r
369\r
370 @return The start address of GUID HOB data.\r
371\r
372**/\r
373VOID *\r
374EFIAPI\r
375BuildGuidDataHob (\r
376 IN CONST EFI_GUID *Guid,\r
377 IN VOID *Data,\r
378 IN UINTN DataLength\r
379 )\r
380{\r
381 VOID *HobData;\r
382\r
383 ASSERT (Data != NULL || DataLength == 0);\r
384\r
385 HobData = BuildGuidHob (Guid, DataLength);\r
386\r
387 return CopyMem (HobData, Data, DataLength);\r
388}\r
389\r
390/**\r
391 Builds a Firmware Volume HOB.\r
392\r
393 This function builds a Firmware Volume HOB.\r
394 If there is no additional space for HOB creation, then ASSERT().\r
395\r
396 @param BaseAddress The base address of the Firmware Volume.\r
397 @param Length The size of the Firmware Volume in bytes.\r
398\r
399**/\r
400VOID\r
401EFIAPI\r
402BuildFvHob (\r
403 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
404 IN UINT64 Length\r
405 )\r
406{\r
407 EFI_HOB_FIRMWARE_VOLUME *Hob;\r
408\r
409 Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));\r
410\r
411 Hob->BaseAddress = BaseAddress;\r
412 Hob->Length = Length;\r
413}\r
414\r
415\r
416/**\r
417 Builds a EFI_HOB_TYPE_FV2 HOB.\r
418\r
419 This function builds a EFI_HOB_TYPE_FV2 HOB.\r
420 If there is no additional space for HOB creation, then ASSERT().\r
421\r
422 @param BaseAddress The base address of the Firmware Volume.\r
423 @param Length The size of the Firmware Volume in bytes.\r
424 @param FvName The name of the Firmware Volume.\r
425 @param FileName The name of the file.\r
426\r
427**/\r
428VOID\r
429EFIAPI\r
430BuildFv2Hob (\r
431 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
432 IN UINT64 Length,\r
433 IN CONST EFI_GUID *FvName,\r
434 IN CONST EFI_GUID *FileName\r
435 )\r
436{\r
437 EFI_HOB_FIRMWARE_VOLUME2 *Hob;\r
438\r
439 Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof (EFI_HOB_FIRMWARE_VOLUME2));\r
440\r
441 Hob->BaseAddress = BaseAddress;\r
442 Hob->Length = Length;\r
443 CopyGuid (&Hob->FvName, FvName);\r
444 CopyGuid (&Hob->FileName, FileName);\r
445}\r
446\r
447\r
448/**\r
449 Builds a HOB for the CPU.\r
450\r
451 This function builds a HOB for the CPU.\r
452 If there is no additional space for HOB creation, then ASSERT().\r
453\r
454 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.\r
455 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.\r
456\r
457**/\r
458VOID\r
459EFIAPI\r
460BuildCpuHob (\r
461 IN UINT8 SizeOfMemorySpace,\r
462 IN UINT8 SizeOfIoSpace\r
463 )\r
464{\r
465 EFI_HOB_CPU *Hob;\r
466\r
467 Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));\r
468\r
469 Hob->SizeOfMemorySpace = SizeOfMemorySpace;\r
470 Hob->SizeOfIoSpace = SizeOfIoSpace;\r
471\r
472 //\r
473 // Zero the reserved space to match HOB spec\r
474 //\r
475 ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));\r
476}\r
477\r
478/**\r
479 Builds a HOB for the memory allocation.\r
480\r
481 This function builds a HOB for the memory allocation.\r
482 If there is no additional space for HOB creation, then ASSERT().\r
483\r
484 @param BaseAddress The 64 bit physical address of the memory.\r
485 @param Length The length of the memory allocation in bytes.\r
486 @param MemoryType Type of memory allocated by this HOB.\r
487\r
488**/\r
489VOID\r
490EFIAPI\r
491BuildMemoryAllocationHob (\r
492 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
493 IN UINT64 Length,\r
494 IN EFI_MEMORY_TYPE MemoryType\r
495 )\r
496{\r
497 EFI_HOB_MEMORY_ALLOCATION *Hob;\r
498\r
499 ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&\r
500 ((Length & (EFI_PAGE_SIZE - 1)) == 0));\r
501\r
502 Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));\r
503\r
504 ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));\r
505 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
506 Hob->AllocDescriptor.MemoryLength = Length;\r
507 Hob->AllocDescriptor.MemoryType = MemoryType;\r
508 //\r
509 // Zero the reserved space to match HOB spec\r
510 //\r
511 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));\r
512}\r
513\r
514/**\r
515 Builds a HOB that describes a chunk of system memory with Owner GUID.\r
516\r
517 This function builds a HOB that describes a chunk of system memory.\r
518 If there is no additional space for HOB creation, then ASSERT().\r
519\r
520 @param ResourceType The type of resource described by this HOB.\r
521 @param ResourceAttribute The resource attributes of the memory described by this HOB.\r
522 @param PhysicalStart The 64 bit physical address of memory described by this HOB.\r
523 @param NumberOfBytes The length of the memory described by this HOB in bytes.\r
524 @param OwnerGUID GUID for the owner of this resource.\r
525\r
526**/\r
527VOID\r
528EFIAPI\r
529BuildResourceDescriptorWithOwnerHob (\r
530 IN EFI_RESOURCE_TYPE ResourceType,\r
531 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,\r
532 IN EFI_PHYSICAL_ADDRESS PhysicalStart,\r
533 IN UINT64 NumberOfBytes,\r
534 IN EFI_GUID *OwnerGUID\r
535 )\r
536{\r
537 ASSERT (FALSE);\r
538}\r
539\r
540/**\r
541 Builds a Capsule Volume HOB.\r
542\r
543 This function builds a Capsule Volume HOB.\r
544 If the platform does not support Capsule Volume HOBs, then ASSERT().\r
545 If there is no additional space for HOB creation, then ASSERT().\r
546\r
547 @param BaseAddress The base address of the Capsule Volume.\r
548 @param Length The size of the Capsule Volume in bytes.\r
549\r
550**/\r
551VOID\r
552EFIAPI\r
553BuildCvHob (\r
554 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
555 IN UINT64 Length\r
556 )\r
557{\r
558 ASSERT (FALSE);\r
559}\r
560\r
561\r
562/**\r
563 Builds a HOB for the BSP store.\r
564\r
565 This function builds a HOB for BSP store.\r
566 If there is no additional space for HOB creation, then ASSERT().\r
567\r
568 @param BaseAddress The 64 bit physical address of the BSP.\r
569 @param Length The length of the BSP store in bytes.\r
570 @param MemoryType Type of memory allocated by this HOB.\r
571\r
572**/\r
573VOID\r
574EFIAPI\r
575BuildBspStoreHob (\r
576 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
577 IN UINT64 Length,\r
578 IN EFI_MEMORY_TYPE MemoryType\r
579 )\r
580{\r
581 ASSERT (FALSE);\r
582}\r
583\r
584/**\r
585 Builds a HOB for the Stack.\r
586\r
587 This function builds a HOB for the stack.\r
588 If there is no additional space for HOB creation, then ASSERT().\r
589\r
590 @param BaseAddress The 64 bit physical address of the Stack.\r
591 @param Length The length of the stack in bytes.\r
592\r
593**/\r
594VOID\r
595EFIAPI\r
596BuildStackHob (\r
597 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
598 IN UINT64 Length\r
599 )\r
600{\r
601 ASSERT (FALSE);\r
602}\r