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