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