]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/PeiHobLib/HobLib.c
710e112deebd7fcfc505133311a25438f190e305
[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 Capsule Volume HOB.
409
410 This function builds a Capsule Volume 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 Capsule Volume.
416 @param Length The size of the Capsule Volume in bytes.
417
418 **/
419 VOID
420 EFIAPI
421 BuildCvHob (
422 IN EFI_PHYSICAL_ADDRESS BaseAddress,
423 IN UINT64 Length
424 )
425 {
426 ASSERT (FALSE);
427 }
428
429 /**
430 Builds a HOB for the CPU.
431
432 This function builds a HOB for the CPU.
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 there is no additional space for HOB creation, then ASSERT().
436
437 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
438 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
439
440 **/
441 VOID
442 EFIAPI
443 BuildCpuHob (
444 IN UINT8 SizeOfMemorySpace,
445 IN UINT8 SizeOfIoSpace
446 )
447 {
448 EFI_HOB_CPU *Hob;
449
450 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
451
452 Hob->SizeOfMemorySpace = SizeOfMemorySpace;
453 Hob->SizeOfIoSpace = SizeOfIoSpace;
454
455 //
456 // Zero the reserved space to match HOB spec
457 //
458 ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));
459 }
460
461 /**
462 Builds a HOB for the Stack.
463
464 This function builds a HOB for the stack.
465 It can only be invoked during PEI phase;
466 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
467 If there is no additional space for HOB creation, then ASSERT().
468
469 @param BaseAddress The 64 bit physical address of the Stack.
470 @param Length The length of the stack in bytes.
471
472 **/
473 VOID
474 EFIAPI
475 BuildStackHob (
476 IN EFI_PHYSICAL_ADDRESS BaseAddress,
477 IN UINT64 Length
478 )
479 {
480 EFI_HOB_MEMORY_ALLOCATION_STACK *Hob;
481
482 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK));
483
484 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocStackGuid);
485 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
486 Hob->AllocDescriptor.MemoryLength = Length;
487 Hob->AllocDescriptor.MemoryType = EfiConventionalMemory;
488
489 //
490 // Zero the reserved space to match HOB spec
491 //
492 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
493 }
494
495 /**
496 Builds a HOB for the BSP store.
497
498 This function builds a HOB for BSP store.
499 It can only be invoked during PEI phase;
500 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
501 If there is no additional space for HOB creation, then ASSERT().
502
503 @param BaseAddress The 64 bit physical address of the BSP.
504 @param Length The length of the BSP store in bytes.
505 @param MemoryType Type of memory allocated by this HOB.
506
507 **/
508 VOID
509 EFIAPI
510 BuildBspStoreHob (
511 IN EFI_PHYSICAL_ADDRESS BaseAddress,
512 IN UINT64 Length,
513 IN EFI_MEMORY_TYPE MemoryType
514 )
515 {
516 EFI_HOB_MEMORY_ALLOCATION_BSP_STORE *Hob;
517
518 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_BSP_STORE));
519
520 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocBspStoreGuid);
521 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
522 Hob->AllocDescriptor.MemoryLength = Length;
523 Hob->AllocDescriptor.MemoryType = MemoryType;
524
525 //
526 // Zero the reserved space to match HOB spec
527 //
528 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
529 }
530
531 /**
532 Builds a HOB for the memory allocation.
533
534 This function builds a HOB for the memory allocation.
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 64 bit physical address of the memory.
540 @param Length The length of the memory allocation in bytes.
541 @param MemoryType Type of memory allocated by this HOB.
542
543 **/
544 VOID
545 EFIAPI
546 BuildMemoryAllocationHob (
547 IN EFI_PHYSICAL_ADDRESS BaseAddress,
548 IN UINT64 Length,
549 IN EFI_MEMORY_TYPE MemoryType
550 )
551 {
552 EFI_HOB_MEMORY_ALLOCATION *Hob;
553
554 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));
555
556 ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
557 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
558 Hob->AllocDescriptor.MemoryLength = Length;
559 Hob->AllocDescriptor.MemoryType = MemoryType;
560 //
561 // Zero the reserved space to match HOB spec
562 //
563 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
564 }