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