]> git.proxmox.com Git - mirror_edk2.git/blob - StandaloneMmPkg/Library/StandaloneMmCoreHobLib/AArch64/StandaloneMmCoreHobLib.c
0ec2d4ad6f6bdcfd9e60e58efff2f63529aaa5b5
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmCoreHobLib / AArch64 / 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 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <PiMm.h>
12
13 #include <Library/HobLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/BaseMemoryLib.h>
16
17 #include <Guid/MemoryAllocationHob.h>
18
19 //
20 // Cache copy of HobList pointer.
21 //
22 VOID *gHobList = NULL;
23
24 VOID *
25 CreateHob (
26 IN UINT16 HobType,
27 IN UINT16 HobLength
28 )
29 {
30 EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;
31 EFI_HOB_GENERIC_HEADER *HobEnd;
32 EFI_PHYSICAL_ADDRESS FreeMemory;
33 VOID *Hob;
34
35 HandOffHob = GetHobList ();
36
37 HobLength = (UINT16)((HobLength + 0x7) & (~0x7));
38
39 FreeMemory = HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryBottom;
40
41 if (FreeMemory < HobLength) {
42 return NULL;
43 }
44
45 Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;
46 ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType;
47 ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength;
48 ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0;
49
50 HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength);
51 HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
52
53 HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;
54 HobEnd->HobLength = sizeof (EFI_HOB_GENERIC_HEADER);
55 HobEnd->Reserved = 0;
56 HobEnd++;
57 HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
58
59 return Hob;
60 }
61
62 /**
63 Builds a HOB for a loaded PE32 module.
64
65 This function builds a HOB for a loaded PE32 module.
66 If ModuleName is NULL, then ASSERT().
67 If there is no additional space for HOB creation, then ASSERT().
68
69 @param ModuleName The GUID File Name of the module.
70 @param MemoryAllocationModule The 64 bit physical address of the module.
71 @param ModuleLength The length of the module in bytes.
72 @param EntryPoint The 64 bit physical address of the module entry point.
73
74 **/
75 VOID
76 EFIAPI
77 BuildModuleHob (
78 IN CONST EFI_GUID *ModuleName,
79 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,
80 IN UINT64 ModuleLength,
81 IN EFI_PHYSICAL_ADDRESS EntryPoint
82 )
83 {
84 EFI_HOB_MEMORY_ALLOCATION_MODULE *Hob;
85
86 ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&
87 ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0));
88
89 Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));
90
91 CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);
92 Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;
93 Hob->MemoryAllocationHeader.MemoryLength = ModuleLength;
94 Hob->MemoryAllocationHeader.MemoryType = EfiBootServicesCode;
95
96 //
97 // Zero the reserved space to match HOB spec
98 //
99 ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved));
100
101 CopyGuid (&Hob->ModuleName, ModuleName);
102 Hob->EntryPoint = EntryPoint;
103 }
104
105 /**
106 Builds a HOB that describes a chunk of system memory.
107
108 This function builds a HOB that describes a chunk of system memory.
109 If there is no additional space for HOB creation, then ASSERT().
110
111 @param ResourceType The type of resource described by this HOB.
112 @param ResourceAttribute The resource attributes of the memory described by this HOB.
113 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
114 @param NumberOfBytes The length of the memory described by this HOB in bytes.
115
116 **/
117 VOID
118 EFIAPI
119 BuildResourceDescriptorHob (
120 IN EFI_RESOURCE_TYPE ResourceType,
121 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,
122 IN EFI_PHYSICAL_ADDRESS PhysicalStart,
123 IN UINT64 NumberOfBytes
124 )
125 {
126 EFI_HOB_RESOURCE_DESCRIPTOR *Hob;
127
128 Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));
129 ASSERT (Hob != NULL);
130
131 Hob->ResourceType = ResourceType;
132 Hob->ResourceAttribute = ResourceAttribute;
133 Hob->PhysicalStart = PhysicalStart;
134 Hob->ResourceLength = NumberOfBytes;
135 }
136
137 /**
138 Builds a GUID HOB with a certain data length.
139
140 This function builds a customized HOB tagged with a GUID for identification
141 and returns the start address of GUID HOB data so that caller can fill the customized data.
142 The HOB Header and Name field is already stripped.
143 If Guid is NULL, then ASSERT().
144 If there is no additional space for HOB creation, then ASSERT().
145 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
146
147 @param Guid The GUID to tag the customized HOB.
148 @param DataLength The size of the data payload for the GUID HOB.
149
150 @return The start address of GUID HOB data.
151
152 **/
153 VOID *
154 EFIAPI
155 BuildGuidHob (
156 IN CONST EFI_GUID *Guid,
157 IN UINTN DataLength
158 )
159 {
160 EFI_HOB_GUID_TYPE *Hob;
161
162 //
163 // Make sure that data length is not too long.
164 //
165 ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));
166
167 Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength));
168 CopyGuid (&Hob->Name, Guid);
169 return Hob + 1;
170 }
171
172
173 /**
174 Copies a data buffer to a newly-built HOB.
175
176 This function builds a customized HOB tagged with a GUID for identification,
177 copies the input data to the HOB data field and returns the start address of the GUID HOB data.
178 The HOB Header and Name field is already stripped.
179 If Guid is NULL, then ASSERT().
180 If Data is NULL and DataLength > 0, then ASSERT().
181 If there is no additional space for HOB creation, then ASSERT().
182 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
183
184 @param Guid The GUID to tag the customized HOB.
185 @param Data The data to be copied into the data field of the GUID HOB.
186 @param DataLength The size of the data payload for the GUID HOB.
187
188 @return The start address of GUID HOB data.
189
190 **/
191 VOID *
192 EFIAPI
193 BuildGuidDataHob (
194 IN CONST EFI_GUID *Guid,
195 IN VOID *Data,
196 IN UINTN DataLength
197 )
198 {
199 VOID *HobData;
200
201 ASSERT (Data != NULL || DataLength == 0);
202
203 HobData = BuildGuidHob (Guid, DataLength);
204
205 return CopyMem (HobData, Data, DataLength);
206 }
207
208 /**
209 Builds a Firmware Volume HOB.
210
211 This function builds a Firmware Volume HOB.
212 If there is no additional space for HOB creation, then ASSERT().
213
214 @param BaseAddress The base address of the Firmware Volume.
215 @param Length The size of the Firmware Volume in bytes.
216
217 **/
218 VOID
219 EFIAPI
220 BuildFvHob (
221 IN EFI_PHYSICAL_ADDRESS BaseAddress,
222 IN UINT64 Length
223 )
224 {
225 EFI_HOB_FIRMWARE_VOLUME *Hob;
226
227 Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));
228
229 Hob->BaseAddress = BaseAddress;
230 Hob->Length = Length;
231 }
232
233
234 /**
235 Builds a EFI_HOB_TYPE_FV2 HOB.
236
237 This function builds a EFI_HOB_TYPE_FV2 HOB.
238 If there is no additional space for HOB creation, then ASSERT().
239
240 @param BaseAddress The base address of the Firmware Volume.
241 @param Length The size of the Firmware Volume in bytes.
242 @param FvName The name of the Firmware Volume.
243 @param FileName The name of the file.
244
245 **/
246 VOID
247 EFIAPI
248 BuildFv2Hob (
249 IN EFI_PHYSICAL_ADDRESS BaseAddress,
250 IN UINT64 Length,
251 IN CONST EFI_GUID *FvName,
252 IN CONST EFI_GUID *FileName
253 )
254 {
255 EFI_HOB_FIRMWARE_VOLUME2 *Hob;
256
257 Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof (EFI_HOB_FIRMWARE_VOLUME2));
258
259 Hob->BaseAddress = BaseAddress;
260 Hob->Length = Length;
261 CopyGuid (&Hob->FvName, FvName);
262 CopyGuid (&Hob->FileName, FileName);
263 }
264
265
266 /**
267 Builds a HOB for the CPU.
268
269 This function builds a HOB for the CPU.
270 If there is no additional space for HOB creation, then ASSERT().
271
272 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
273 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
274
275 **/
276 VOID
277 EFIAPI
278 BuildCpuHob (
279 IN UINT8 SizeOfMemorySpace,
280 IN UINT8 SizeOfIoSpace
281 )
282 {
283 EFI_HOB_CPU *Hob;
284
285 Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
286
287 Hob->SizeOfMemorySpace = SizeOfMemorySpace;
288 Hob->SizeOfIoSpace = SizeOfIoSpace;
289
290 //
291 // Zero the reserved space to match HOB spec
292 //
293 ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));
294 }
295
296 /**
297 Builds a HOB for the memory allocation.
298
299 This function builds a HOB for the memory allocation.
300 If there is no additional space for HOB creation, then ASSERT().
301
302 @param BaseAddress The 64 bit physical address of the memory.
303 @param Length The length of the memory allocation in bytes.
304 @param MemoryType Type of memory allocated by this HOB.
305
306 **/
307 VOID
308 EFIAPI
309 BuildMemoryAllocationHob (
310 IN EFI_PHYSICAL_ADDRESS BaseAddress,
311 IN UINT64 Length,
312 IN EFI_MEMORY_TYPE MemoryType
313 )
314 {
315 EFI_HOB_MEMORY_ALLOCATION *Hob;
316
317 ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
318 ((Length & (EFI_PAGE_SIZE - 1)) == 0));
319
320 Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));
321
322 ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
323 Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
324 Hob->AllocDescriptor.MemoryLength = Length;
325 Hob->AllocDescriptor.MemoryType = MemoryType;
326 //
327 // Zero the reserved space to match HOB spec
328 //
329 ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
330 }