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