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