]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Library/PrePiHobLib/Hob.c
EmbeddedPkg/PrePiHobLib: drop CreateHobList() from library
[mirror_edk2.git] / EmbeddedPkg / Library / PrePiHobLib / Hob.c
1 /** @file
2
3 Copyright (c) 2010, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <PiPei.h>
17
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/PeCoffLib.h>
22 #include <Library/HobLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/PrePiHobListPointerLib.h>
25
26 #include <Protocol/PeCoffLoader.h>
27 #include <Guid/ExtractSection.h>
28 #include <Guid/MemoryTypeInformation.h>
29 #include <Guid/MemoryAllocationHob.h>
30
31 VOID
32 BuildMemoryTypeInformationHob (
33 VOID
34 );
35
36 /**
37 Returns the pointer to the HOB list.
38
39 This function returns the pointer to first HOB in the list.
40
41 @return The pointer to the HOB list.
42
43 **/
44 VOID *
45 EFIAPI
46 GetHobList (
47 VOID
48 )
49 {
50 return PrePeiGetHobList ();
51 }
52
53
54
55 /**
56 Updates the pointer to the HOB list.
57
58 @param HobList Hob list pointer to store
59
60 **/
61 EFI_STATUS
62 EFIAPI
63 SetHobList (
64 IN VOID *HobList
65 )
66 {
67 return PrePeiSetHobList (HobList);
68 }
69
70 /**
71
72
73 **/
74 EFI_HOB_HANDOFF_INFO_TABLE*
75 HobConstructor (
76 IN VOID *EfiMemoryBegin,
77 IN UINTN EfiMemoryLength,
78 IN VOID *EfiFreeMemoryBottom,
79 IN VOID *EfiFreeMemoryTop
80 )
81 {
82 EFI_HOB_HANDOFF_INFO_TABLE *Hob;
83 EFI_HOB_GENERIC_HEADER *HobEnd;
84
85 Hob = EfiFreeMemoryBottom;
86 HobEnd = (EFI_HOB_GENERIC_HEADER *)(Hob+1);
87
88 Hob->Header.HobType = EFI_HOB_TYPE_HANDOFF;
89 Hob->Header.HobLength = sizeof(EFI_HOB_HANDOFF_INFO_TABLE);
90 Hob->Header.Reserved = 0;
91
92 HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;
93 HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER);
94 HobEnd->Reserved = 0;
95
96 Hob->Version = EFI_HOB_HANDOFF_TABLE_VERSION;
97 Hob->BootMode = BOOT_WITH_FULL_CONFIGURATION;
98
99 Hob->EfiMemoryTop = (UINTN)EfiMemoryBegin + EfiMemoryLength;
100 Hob->EfiMemoryBottom = (UINTN)EfiMemoryBegin;
101 Hob->EfiFreeMemoryTop = (UINTN)EfiFreeMemoryTop;
102 Hob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)(HobEnd+1);
103 Hob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;
104
105 return Hob;
106 }
107
108 VOID *
109 CreateHob (
110 IN UINT16 HobType,
111 IN UINT16 HobLength
112 )
113 {
114 EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;
115 EFI_HOB_GENERIC_HEADER *HobEnd;
116 EFI_PHYSICAL_ADDRESS FreeMemory;
117 VOID *Hob;
118
119 HandOffHob = GetHobList ();
120
121 HobLength = (UINT16)((HobLength + 0x7) & (~0x7));
122
123 FreeMemory = HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryBottom;
124
125 if (FreeMemory < HobLength) {
126 return NULL;
127 }
128
129 Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;
130 ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType;
131 ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength;
132 ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0;
133
134 HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength);
135 HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
136
137 HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;
138 HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER);
139 HobEnd->Reserved = 0;
140 HobEnd++;
141 HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
142
143 return Hob;
144 }
145
146 /**
147 Builds a HOB that describes a chunk of system memory.
148
149 This function builds a HOB that describes a chunk of system memory.
150 If there is no additional space for HOB creation, then ASSERT().
151
152 @param ResourceType The type of resource described by this HOB.
153 @param ResourceAttribute The resource attributes of the memory described by this HOB.
154 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
155 @param NumberOfBytes The length of the memory described by this HOB in bytes.
156
157 **/
158 VOID
159 EFIAPI
160 BuildResourceDescriptorHob (
161 IN EFI_RESOURCE_TYPE ResourceType,
162 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,
163 IN EFI_PHYSICAL_ADDRESS PhysicalStart,
164 IN UINT64 NumberOfBytes
165 )
166 {
167 EFI_HOB_RESOURCE_DESCRIPTOR *Hob;
168
169 Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));
170 ASSERT(Hob != NULL);
171
172 Hob->ResourceType = ResourceType;
173 Hob->ResourceAttribute = ResourceAttribute;
174 Hob->PhysicalStart = PhysicalStart;
175 Hob->ResourceLength = NumberOfBytes;
176 }
177
178 VOID
179 EFIAPI
180 BuildFvHobs (
181 IN EFI_PHYSICAL_ADDRESS PhysicalStart,
182 IN UINT64 NumberOfBytes,
183 IN EFI_RESOURCE_ATTRIBUTE_TYPE *ResourceAttribute
184 )
185 {
186
187 EFI_RESOURCE_ATTRIBUTE_TYPE Resource;
188
189 BuildFvHob (PhysicalStart, NumberOfBytes);
190
191 if (ResourceAttribute == NULL) {
192 Resource = (EFI_RESOURCE_ATTRIBUTE_PRESENT |
193 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
194 EFI_RESOURCE_ATTRIBUTE_TESTED |
195 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE);
196 } else {
197 Resource = *ResourceAttribute;
198 }
199
200 BuildResourceDescriptorHob (EFI_RESOURCE_FIRMWARE_DEVICE, Resource, PhysicalStart, NumberOfBytes);
201 }
202
203 /**
204 Returns the next instance of a HOB type from the starting HOB.
205
206 This function searches the first instance of a HOB type from the starting HOB pointer.
207 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
208 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
209 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
210 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
211 If HobStart is NULL, then ASSERT().
212
213 @param Type The HOB type to return.
214 @param HobStart The starting HOB pointer to search from.
215
216 @return The next instance of a HOB type from the starting HOB.
217
218 **/
219 VOID *
220 EFIAPI
221 GetNextHob (
222 IN UINT16 Type,
223 IN CONST VOID *HobStart
224 )
225 {
226 EFI_PEI_HOB_POINTERS Hob;
227
228 ASSERT (HobStart != NULL);
229
230 Hob.Raw = (UINT8 *) HobStart;
231 //
232 // Parse the HOB list until end of list or matching type is found.
233 //
234 while (!END_OF_HOB_LIST (Hob)) {
235 if (Hob.Header->HobType == Type) {
236 return Hob.Raw;
237 }
238 Hob.Raw = GET_NEXT_HOB (Hob);
239 }
240 return NULL;
241 }
242
243
244
245 /**
246 Returns the first instance of a HOB type among the whole HOB list.
247
248 This function searches the first instance of a HOB type among the whole HOB list.
249 If there does not exist such HOB type in the HOB list, it will return NULL.
250
251 @param Type The HOB type to return.
252
253 @return The next instance of a HOB type from the starting HOB.
254
255 **/
256 VOID *
257 EFIAPI
258 GetFirstHob (
259 IN UINT16 Type
260 )
261 {
262 VOID *HobList;
263
264 HobList = GetHobList ();
265 return GetNextHob (Type, HobList);
266 }
267
268
269 /**
270 This function searches the first instance of a HOB from the starting HOB pointer.
271 Such HOB should satisfy two conditions:
272 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
273 If there does not exist such HOB from the starting HOB pointer, it will return NULL.
274 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
275 to extract the data section and its size info respectively.
276 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
277 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
278 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
279 If Guid is NULL, then ASSERT().
280 If HobStart is NULL, then ASSERT().
281
282 @param Guid The GUID to match with in the HOB list.
283 @param HobStart A pointer to a Guid.
284
285 @return The next instance of the matched GUID HOB from the starting HOB.
286
287 **/
288 VOID *
289 EFIAPI
290 GetNextGuidHob (
291 IN CONST EFI_GUID *Guid,
292 IN CONST VOID *HobStart
293 ){
294 EFI_PEI_HOB_POINTERS GuidHob;
295
296 GuidHob.Raw = (UINT8 *) HobStart;
297 while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {
298 if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
299 break;
300 }
301 GuidHob.Raw = GET_NEXT_HOB (GuidHob);
302 }
303 return GuidHob.Raw;
304 }
305
306
307 /**
308 This function searches the first instance of a HOB among the whole HOB list.
309 Such HOB should satisfy two conditions:
310 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
311 If there does not exist such HOB from the starting HOB pointer, it will return NULL.
312 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
313 to extract the data section and its size info respectively.
314 If Guid is NULL, then ASSERT().
315
316 @param Guid The GUID to match with in the HOB list.
317
318 @return The first instance of the matched GUID HOB among the whole HOB list.
319
320 **/
321 VOID *
322 EFIAPI
323 GetFirstGuidHob (
324 IN CONST EFI_GUID *Guid
325 )
326 {
327 VOID *HobList;
328
329 HobList = GetHobList ();
330 return GetNextGuidHob (Guid, HobList);
331 }
332
333
334 /**
335 Get the Boot Mode from the HOB list.
336
337 This function returns the system boot mode information from the
338 PHIT HOB in HOB list.
339
340 @param VOID
341
342 @return The Boot Mode.
343
344 **/
345 EFI_BOOT_MODE
346 EFIAPI
347 GetBootMode (
348 VOID
349 )
350 {
351 EFI_PEI_HOB_POINTERS Hob;
352
353 Hob.Raw = GetHobList ();
354 return Hob.HandoffInformationTable->BootMode;
355 }
356
357
358 /**
359 Get the Boot Mode from the HOB list.
360
361 This function returns the system boot mode information from the
362 PHIT HOB in HOB list.
363
364 @param VOID
365
366 @return The Boot Mode.
367
368 **/
369 EFI_STATUS
370 EFIAPI
371 SetBootMode (
372 IN EFI_BOOT_MODE BootMode
373 )
374 {
375 EFI_PEI_HOB_POINTERS Hob;
376
377 Hob.Raw = GetHobList ();
378 Hob.HandoffInformationTable->BootMode = BootMode;
379 return BootMode;
380 }
381
382 /**
383 Builds a HOB for a loaded PE32 module.
384
385 This function builds a HOB for a loaded PE32 module.
386 It can only be invoked during PEI phase;
387 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
388 If ModuleName is NULL, then ASSERT().
389 If there is no additional space for HOB creation, then ASSERT().
390
391 @param ModuleName The GUID File Name of the module.
392 @param MemoryAllocationModule The 64 bit physical address of the module.
393 @param ModuleLength The length of the module in bytes.
394 @param EntryPoint The 64 bit physical address of the module entry point.
395
396 **/
397 VOID
398 EFIAPI
399 BuildModuleHob (
400 IN CONST EFI_GUID *ModuleName,
401 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,
402 IN UINT64 ModuleLength,
403 IN EFI_PHYSICAL_ADDRESS EntryPoint
404 )
405 {
406 EFI_HOB_MEMORY_ALLOCATION_MODULE *Hob;
407
408 ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&
409 ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0));
410
411 Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));
412
413 CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);
414 Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;
415 Hob->MemoryAllocationHeader.MemoryLength = ModuleLength;
416 Hob->MemoryAllocationHeader.MemoryType = EfiBootServicesCode;
417
418 //
419 // Zero the reserved space to match HOB spec
420 //
421 ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved));
422
423 CopyGuid (&Hob->ModuleName, ModuleName);
424 Hob->EntryPoint = EntryPoint;
425 }
426
427 /**
428 Builds a GUID HOB with a certain data length.
429
430 This function builds a customized HOB tagged with a GUID for identification
431 and returns the start address of GUID HOB data so that caller can fill the customized data.
432 The HOB Header and Name field is already stripped.
433 It can only be invoked during PEI phase;
434 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
435 If Guid is NULL, then ASSERT().
436 If there is no additional space for HOB creation, then ASSERT().
437 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
438
439 @param Guid The GUID to tag the customized HOB.
440 @param DataLength The size of the data payload for the GUID HOB.
441
442 @return The start address of GUID HOB data.
443
444 **/
445 VOID *
446 EFIAPI
447 BuildGuidHob (
448 IN CONST EFI_GUID *Guid,
449 IN UINTN DataLength
450 )
451 {
452 EFI_HOB_GUID_TYPE *Hob;
453
454 //
455 // Make sure that data length is not too long.
456 //
457 ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));
458
459 Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength));
460 CopyGuid (&Hob->Name, Guid);
461 return Hob + 1;
462 }
463
464
465 /**
466 Copies a data buffer to a newly-built HOB.
467
468 This function builds a customized HOB tagged with a GUID for identification,
469 copies the input data to the HOB data field and returns the start address of the GUID HOB data.
470 The HOB Header and Name field is already stripped.
471 It can only be invoked during PEI phase;
472 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
473 If Guid is NULL, then ASSERT().
474 If Data is NULL and DataLength > 0, then ASSERT().
475 If there is no additional space for HOB creation, then ASSERT().
476 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
477
478 @param Guid The GUID to tag the customized HOB.
479 @param Data The data to be copied into the data field of the GUID HOB.
480 @param DataLength The size of the data payload for the GUID HOB.
481
482 @return The start address of GUID HOB data.
483
484 **/
485 VOID *
486 EFIAPI
487 BuildGuidDataHob (
488 IN CONST EFI_GUID *Guid,
489 IN VOID *Data,
490 IN UINTN DataLength
491 )
492 {
493 VOID *HobData;
494
495 ASSERT (Data != NULL || DataLength == 0);
496
497 HobData = BuildGuidHob (Guid, DataLength);
498
499 return CopyMem (HobData, Data, DataLength);
500 }
501
502
503 /**
504 Builds a Firmware Volume HOB.
505
506 This function builds a Firmware Volume HOB.
507 It can only be invoked during PEI phase;
508 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
509 If there is no additional space for HOB creation, then ASSERT().
510
511 @param BaseAddress The base address of the Firmware Volume.
512 @param Length The size of the Firmware Volume in bytes.
513
514 **/
515 VOID
516 EFIAPI
517 BuildFvHob (
518 IN EFI_PHYSICAL_ADDRESS BaseAddress,
519 IN UINT64 Length
520 )
521 {
522 EFI_HOB_FIRMWARE_VOLUME *Hob;
523
524 Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));
525
526 Hob->BaseAddress = BaseAddress;
527 Hob->Length = Length;
528 }
529
530
531 /**
532 Builds a EFI_HOB_TYPE_FV2 HOB.
533
534 This function builds a EFI_HOB_TYPE_FV2 HOB.
535 It can only be invoked during PEI phase;
536 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
537 If there is no additional space for HOB creation, then ASSERT().
538
539 @param BaseAddress The base address of the Firmware Volume.
540 @param Length The size of the Firmware Volume in bytes.
541 @param FvName The name of the Firmware Volume.
542 @param FileName The name of the file.
543
544 **/
545 VOID
546 EFIAPI
547 BuildFv2Hob (
548 IN EFI_PHYSICAL_ADDRESS BaseAddress,
549 IN UINT64 Length,
550 IN CONST EFI_GUID *FvName,
551 IN CONST EFI_GUID *FileName
552 )
553 {
554 EFI_HOB_FIRMWARE_VOLUME2 *Hob;
555
556 Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof (EFI_HOB_FIRMWARE_VOLUME2));
557
558 Hob->BaseAddress = BaseAddress;
559 Hob->Length = Length;
560 CopyGuid (&Hob->FvName, FvName);
561 CopyGuid (&Hob->FileName, FileName);
562 }
563
564 /**
565 Builds a EFI_HOB_TYPE_FV3 HOB.
566
567 This function builds a EFI_HOB_TYPE_FV3 HOB.
568 It can only be invoked during PEI phase;
569 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
570
571 If there is no additional space for HOB creation, then ASSERT().
572
573 @param BaseAddress The base address of the Firmware Volume.
574 @param Length The size of the Firmware Volume in bytes.
575 @param AuthenticationStatus The authentication status.
576 @param ExtractedFv TRUE if the FV was extracted as a file within
577 another firmware volume. FALSE otherwise.
578 @param FvName The name of the Firmware Volume.
579 Valid only if IsExtractedFv is TRUE.
580 @param FileName The name of the file.
581 Valid only if IsExtractedFv is TRUE.
582
583 **/
584 VOID
585 EFIAPI
586 BuildFv3Hob (
587 IN EFI_PHYSICAL_ADDRESS BaseAddress,
588 IN UINT64 Length,
589 IN UINT32 AuthenticationStatus,
590 IN BOOLEAN ExtractedFv,
591 IN CONST EFI_GUID *FvName, OPTIONAL
592 IN CONST EFI_GUID *FileName OPTIONAL
593 )
594 {
595 EFI_HOB_FIRMWARE_VOLUME3 *Hob;
596
597 Hob = CreateHob (EFI_HOB_TYPE_FV3, sizeof (EFI_HOB_FIRMWARE_VOLUME3));
598
599 Hob->BaseAddress = BaseAddress;
600 Hob->Length = Length;
601 Hob->AuthenticationStatus = AuthenticationStatus;
602 Hob->ExtractedFv = ExtractedFv;
603 if (ExtractedFv) {
604 CopyGuid (&Hob->FvName, FvName);
605 CopyGuid (&Hob->FileName, FileName);
606 }
607 }
608
609 /**
610 Builds a Capsule Volume HOB.
611
612 This function builds a Capsule Volume HOB.
613 It can only be invoked during PEI phase;
614 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
615 If there is no additional space for HOB creation, then ASSERT().
616
617 @param BaseAddress The base address of the Capsule Volume.
618 @param Length The size of the Capsule Volume in bytes.
619
620 **/
621 VOID
622 EFIAPI
623 BuildCvHob (
624 IN EFI_PHYSICAL_ADDRESS BaseAddress,
625 IN UINT64 Length
626 )
627 {
628 ASSERT (FALSE);
629 }
630
631
632 /**
633 Builds a HOB for the CPU.
634
635 This function builds a HOB for the CPU.
636 It can only be invoked during PEI phase;
637 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
638 If there is no additional space for HOB creation, then ASSERT().
639
640 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
641 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
642
643 **/
644 VOID
645 EFIAPI
646 BuildCpuHob (
647 IN UINT8 SizeOfMemorySpace,
648 IN UINT8 SizeOfIoSpace
649 )
650 {
651 EFI_HOB_CPU *Hob;
652
653 Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
654
655 Hob->SizeOfMemorySpace = SizeOfMemorySpace;
656 Hob->SizeOfIoSpace = SizeOfIoSpace;
657
658 //
659 // Zero the reserved space to match HOB spec
660 //
661 ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));
662 }
663
664
665 /**
666 Builds a HOB for the Stack.
667
668 This function builds a HOB for the stack.
669 It can only be invoked during PEI phase;
670 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
671 If there is no additional space for HOB creation, then ASSERT().
672
673 @param BaseAddress The 64 bit physical address of the Stack.
674 @param Length The length of the stack in bytes.
675
676 **/
677 VOID
678 EFIAPI
679 BuildStackHob (
680 IN EFI_PHYSICAL_ADDRESS BaseAddress,
681 IN UINT64 Length
682 )
683 {
684 EFI_HOB_MEMORY_ALLOCATION_STACK *Hob;
685
686 ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
687 ((Length & (EFI_PAGE_SIZE - 1)) == 0));
688
689 Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK));
690
691 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocStackGuid);
692 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
693 Hob->AllocDescriptor.MemoryLength = Length;
694 Hob->AllocDescriptor.MemoryType = EfiBootServicesData;
695
696 //
697 // Zero the reserved space to match HOB spec
698 //
699 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
700 }
701
702
703 /**
704 Update the Stack Hob if the stack has been moved
705
706 @param BaseAddress The 64 bit physical address of the Stack.
707 @param Length The length of the stack in bytes.
708
709 **/
710 VOID
711 UpdateStackHob (
712 IN EFI_PHYSICAL_ADDRESS BaseAddress,
713 IN UINT64 Length
714 )
715 {
716 EFI_PEI_HOB_POINTERS Hob;
717
718 Hob.Raw = GetHobList ();
719 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) {
720 if (CompareGuid (&gEfiHobMemoryAllocStackGuid, &(Hob.MemoryAllocationStack->AllocDescriptor.Name))) {
721 //
722 // Build a new memory allocation HOB with old stack info with EfiConventionalMemory type
723 // to be reclaimed by DXE core.
724 //
725 BuildMemoryAllocationHob (
726 Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress,
727 Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength,
728 EfiConventionalMemory
729 );
730 //
731 // Update the BSP Stack Hob to reflect the new stack info.
732 //
733 Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress = BaseAddress;
734 Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength = Length;
735 break;
736 }
737 Hob.Raw = GET_NEXT_HOB (Hob);
738 }
739 }
740
741
742
743 /**
744 Builds a HOB for the memory allocation.
745
746 This function builds a HOB for the memory allocation.
747 It can only be invoked during PEI phase;
748 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
749 If there is no additional space for HOB creation, then ASSERT().
750
751 @param BaseAddress The 64 bit physical address of the memory.
752 @param Length The length of the memory allocation in bytes.
753 @param MemoryType Type of memory allocated by this HOB.
754
755 **/
756 VOID
757 EFIAPI
758 BuildMemoryAllocationHob (
759 IN EFI_PHYSICAL_ADDRESS BaseAddress,
760 IN UINT64 Length,
761 IN EFI_MEMORY_TYPE MemoryType
762 )
763 {
764 EFI_HOB_MEMORY_ALLOCATION *Hob;
765
766 ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
767 ((Length & (EFI_PAGE_SIZE - 1)) == 0));
768
769 Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));
770
771 ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
772 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
773 Hob->AllocDescriptor.MemoryLength = Length;
774 Hob->AllocDescriptor.MemoryType = MemoryType;
775 //
776 // Zero the reserved space to match HOB spec
777 //
778 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
779 }
780
781
782
783 VOID
784 EFIAPI
785 BuildExtractSectionHob (
786 IN EFI_GUID *Guid,
787 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER SectionGetInfo,
788 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER SectionExtraction
789 )
790 {
791 EXTRACT_SECTION_DATA Data;
792
793 Data.SectionGetInfo = SectionGetInfo;
794 Data.SectionExtraction = SectionExtraction;
795 BuildGuidDataHob (Guid, &Data, sizeof (Data));
796 }
797
798 PE_COFF_LOADER_PROTOCOL gPeCoffProtocol = {
799 PeCoffLoaderGetImageInfo,
800 PeCoffLoaderLoadImage,
801 PeCoffLoaderRelocateImage,
802 PeCoffLoaderImageReadFromMemory,
803 PeCoffLoaderRelocateImageForRuntime,
804 PeCoffLoaderUnloadImage
805 };
806
807
808
809 VOID
810 EFIAPI
811 BuildPeCoffLoaderHob (
812 VOID
813 )
814 {
815 VOID *Ptr;
816
817 Ptr = &gPeCoffProtocol;
818 BuildGuidDataHob (&gPeCoffLoaderProtocolGuid, &Ptr, sizeof (VOID *));
819 }
820
821 // May want to put this into a library so you only need the PCD setings if you are using the feature?
822 VOID
823 BuildMemoryTypeInformationHob (
824 VOID
825 )
826 {
827 EFI_MEMORY_TYPE_INFORMATION Info[10];
828
829 Info[0].Type = EfiACPIReclaimMemory;
830 Info[0].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIReclaimMemory);
831 Info[1].Type = EfiACPIMemoryNVS;
832 Info[1].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIMemoryNVS);
833 Info[2].Type = EfiReservedMemoryType;
834 Info[2].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiReservedMemoryType);
835 Info[3].Type = EfiRuntimeServicesData;
836 Info[3].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesData);
837 Info[4].Type = EfiRuntimeServicesCode;
838 Info[4].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesCode);
839 Info[5].Type = EfiBootServicesCode;
840 Info[5].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesCode);
841 Info[6].Type = EfiBootServicesData;
842 Info[6].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesData);
843 Info[7].Type = EfiLoaderCode;
844 Info[7].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderCode);
845 Info[8].Type = EfiLoaderData;
846 Info[8].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderData);
847
848 // Terminator for the list
849 Info[9].Type = EfiMaxMemoryType;
850 Info[9].NumberOfPages = 0;
851
852
853 BuildGuidDataHob (&gEfiMemoryTypeInformationGuid, &Info, sizeof (Info));
854 }
855