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