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