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