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