]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/PeiHobLib/HobLib.c
Add in the 1st version of ECP.
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / EdkIIGlueLib / Library / PeiHobLib / HobLib.c
1 /*++
2
3 Copyright (c) 2004 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 Module Name:
14
15 HobLib.c
16
17 Abstract:
18
19 HOB Library.
20
21 --*/
22
23 #include "EdkIIGluePeim.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 GlueGetNextGuidHob (
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 GlueGetFirstGuidHob (
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 GlueBuildModuleHob (
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 //
402 // Check FV Signature
403 //
404 ASSERT (((EFI_FIRMWARE_VOLUME_HEADER*)((UINTN)BaseAddress))->Signature == EFI_FVH_SIGNATURE);
405 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));
406
407 Hob->BaseAddress = BaseAddress;
408 Hob->Length = Length;
409 }
410
411 /**
412 Builds a Capsule Volume HOB.
413
414 This function builds a Capsule Volume HOB.
415 It can only be invoked during PEI phase;
416 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
417 If there is no additional space for HOB creation, then ASSERT().
418
419 @param BaseAddress The base address of the Capsule Volume.
420 @param Length The size of the Capsule Volume in bytes.
421
422 **/
423 VOID
424 EFIAPI
425 BuildCvHob (
426 IN EFI_PHYSICAL_ADDRESS BaseAddress,
427 IN UINT64 Length
428 )
429 {
430 EFI_HOB_CAPSULE_VOLUME *Hob;
431
432 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_CV, sizeof (EFI_HOB_CAPSULE_VOLUME));
433
434 Hob->BaseAddress = BaseAddress;
435 Hob->Length = Length;
436 }
437
438 /**
439 Builds a HOB for the CPU.
440
441 This function builds a HOB for the CPU.
442 It can only be invoked during PEI phase;
443 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
444 If there is no additional space for HOB creation, then ASSERT().
445
446 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
447 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
448
449 **/
450 VOID
451 EFIAPI
452 BuildCpuHob (
453 IN UINT8 SizeOfMemorySpace,
454 IN UINT8 SizeOfIoSpace
455 )
456 {
457 EFI_HOB_CPU *Hob;
458
459 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
460
461 Hob->SizeOfMemorySpace = SizeOfMemorySpace;
462 Hob->SizeOfIoSpace = SizeOfIoSpace;
463
464 //
465 // Zero the reserved space to match HOB spec
466 //
467 ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));
468 }
469
470 /**
471 Builds a HOB for the Stack.
472
473 This function builds a HOB for the stack.
474 It can only be invoked during PEI phase;
475 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
476 If there is no additional space for HOB creation, then ASSERT().
477
478 @param BaseAddress The 64 bit physical address of the Stack.
479 @param Length The length of the stack in bytes.
480
481 **/
482 VOID
483 EFIAPI
484 BuildStackHob (
485 IN EFI_PHYSICAL_ADDRESS BaseAddress,
486 IN UINT64 Length
487 )
488 {
489 EFI_HOB_MEMORY_ALLOCATION_STACK *Hob;
490
491 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK));
492
493 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocStackGuid);
494 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
495 Hob->AllocDescriptor.MemoryLength = Length;
496 Hob->AllocDescriptor.MemoryType = EfiConventionalMemory;
497
498 //
499 // Zero the reserved space to match HOB spec
500 //
501 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
502 }
503
504 /**
505 Builds a HOB for the BSP store.
506
507 This function builds a HOB for BSP store.
508 It can only be invoked during PEI phase;
509 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
510 If there is no additional space for HOB creation, then ASSERT().
511
512 @param BaseAddress The 64 bit physical address of the BSP.
513 @param Length The length of the BSP store in bytes.
514 @param MemoryType Type of memory allocated by this HOB.
515
516 **/
517 VOID
518 EFIAPI
519 BuildBspStoreHob (
520 IN EFI_PHYSICAL_ADDRESS BaseAddress,
521 IN UINT64 Length,
522 IN EFI_MEMORY_TYPE MemoryType
523 )
524 {
525 EFI_HOB_MEMORY_ALLOCATION_BSP_STORE *Hob;
526
527 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_BSP_STORE));
528
529 CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocBspStoreGuid);
530 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
531 Hob->AllocDescriptor.MemoryLength = Length;
532 Hob->AllocDescriptor.MemoryType = MemoryType;
533
534 //
535 // Zero the reserved space to match HOB spec
536 //
537 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
538 }
539
540 /**
541 Builds a HOB for the memory allocation.
542
543 This function builds a HOB for the memory allocation.
544 It can only be invoked during PEI phase;
545 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
546 If there is no additional space for HOB creation, then ASSERT().
547
548 @param BaseAddress The 64 bit physical address of the memory.
549 @param Length The length of the memory allocation in bytes.
550 @param MemoryType Type of memory allocated by this HOB.
551
552 **/
553 VOID
554 EFIAPI
555 GlueBuildMemoryAllocationHob (
556 IN EFI_PHYSICAL_ADDRESS BaseAddress,
557 IN UINT64 Length,
558 IN EFI_MEMORY_TYPE MemoryType
559 )
560 {
561 EFI_HOB_MEMORY_ALLOCATION *Hob;
562
563 Hob = InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));
564
565 ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
566 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
567 Hob->AllocDescriptor.MemoryLength = Length;
568 Hob->AllocDescriptor.MemoryType = MemoryType;
569 //
570 // Zero the reserved space to match HOB spec
571 //
572 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
573 }