MdeModulePkg EsrtFmpDxe: Use EfiBootServicesData for ESRT table
[mirror_edk2.git] / MdeModulePkg / Universal / EsrtFmpDxe / EsrtFmp.c
CommitLineData
4184aabd
MK
1/** @file\r
2 Publishes ESRT table from Firmware Management Protocol instances\r
3\r
4 Copyright (c) 2016, Microsoft Corporation\r
5 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>\r
6\r
7 All rights reserved.\r
8 Redistribution and use in source and binary forms, with or without\r
9 modification, are permitted provided that the following conditions are met:\r
10 1. Redistributions of source code must retain the above copyright notice,\r
11 this list of conditions and the following disclaimer.\r
12 2. Redistributions in binary form must reproduce the above copyright notice,\r
13 this list of conditions and the following disclaimer in the documentation\r
14 and/or other materials provided with the distribution.\r
15\r
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
19 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\r
20 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
21 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\r
24 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
26\r
27**/\r
28\r
29#include <Uefi.h>\r
30#include <Library/BaseLib.h>\r
31#include <Library/BaseMemoryLib.h>\r
32#include <Library/MemoryAllocationLib.h>\r
33#include <Library/UefiBootServicesTableLib.h>\r
34#include <Library/DebugLib.h>\r
35#include <Library/PcdLib.h>\r
36#include <Library/UefiLib.h>\r
37#include <Protocol/FirmwareManagement.h>\r
38#include <Guid/EventGroup.h>\r
39#include <Guid/SystemResourceTable.h>\r
40\r
ae38c976
DB
41/**\r
42 Print ESRT to debug console.\r
43\r
44 @param[in] Table Pointer to the ESRT table.\r
45\r
46**/\r
4184aabd
MK
47VOID\r
48EFIAPI\r
49PrintTable (\r
50 IN EFI_SYSTEM_RESOURCE_TABLE *Table\r
51 );\r
52\r
53//\r
54// Number of ESRT entries to grow by each time we run out of room\r
55//\r
56#define GROWTH_STEP 10\r
57\r
58//\r
59// Module globals.\r
60//\r
61EFI_EVENT mEsrtReadyToBootEvent;\r
62EFI_SYSTEM_RESOURCE_TABLE *mTable = NULL;\r
63BOOLEAN mEsrtInstalled = FALSE;\r
64EFI_EVENT mFmpInstallEvent;\r
65VOID *mFmpInstallEventRegistration = NULL;\r
66\r
67/**\r
68 Install EFI System Resource Table into the UEFI Configuration Table\r
69\r
70 @return Status code.\r
71\r
72**/\r
73EFI_STATUS\r
74InstallEfiSystemResourceTableInUefiConfigurationTable (\r
75 VOID\r
76 )\r
77{\r
78 EFI_STATUS Status;\r
79\r
80 Status = EFI_SUCCESS;\r
81 if (!mEsrtInstalled) {\r
82 if (mTable == NULL) {\r
83 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Can't install ESRT table because it is NULL. \n"));\r
84 Status = EFI_OUT_OF_RESOURCES;\r
85 } else if (mTable->FwResourceCount == 0) {\r
86 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Can't install ESRT table because it has zero Entries. \n"));\r
87 Status = EFI_UNSUPPORTED;\r
88 } else {\r
89 //\r
90 // Install the pointer into config table\r
91 //\r
92 Status = gBS->InstallConfigurationTable (&gEfiSystemResourceTableGuid, mTable);\r
93 if (EFI_ERROR (Status)) {\r
94 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Can't install ESRT table. Status: %r. \n", Status));\r
95 } else {\r
96 DEBUG ((DEBUG_INFO, "EsrtFmpDxe: Installed ESRT table. \n"));\r
97 mEsrtInstalled = TRUE;\r
98 }\r
99 }\r
100 }\r
101 return Status;\r
102}\r
103\r
104/**\r
105 Return if this FMP is a system FMP or a device FMP, based upon FmpImageInfo.\r
106\r
107 @param[in] FmpImageInfo A pointer to EFI_FIRMWARE_IMAGE_DESCRIPTOR\r
108\r
109 @return TRUE It is a system FMP.\r
110 @return FALSE It is a device FMP.\r
111**/\r
112BOOLEAN\r
113IsSystemFmp (\r
114 IN EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfo\r
115 )\r
116{\r
117 GUID *Guid;\r
118 UINTN Count;\r
119 UINTN Index;\r
120\r
121 Guid = PcdGetPtr (PcdSystemFmpCapsuleImageTypeIdGuid);\r
122 Count = PcdGetSize (PcdSystemFmpCapsuleImageTypeIdGuid) / sizeof(GUID);\r
123\r
124 for (Index = 0; Index < Count; Index++, Guid++) {\r
125 if (CompareGuid (&FmpImageInfo->ImageTypeId, Guid)) {\r
126 return TRUE;\r
127 }\r
128 }\r
129\r
130 return FALSE;\r
131}\r
132\r
133/**\r
134 Function to create a single ESRT Entry and add it to the ESRT\r
135 given a FMP descriptor. If the guid is already in the ESRT it\r
136 will be ignored. The ESRT will grow if it does not have enough room.\r
137\r
ae38c976
DB
138 @param[in] FmpImageInfoBuf Pointer to the EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
139 @param[in] FmpVersion FMP Version.\r
140\r
4184aabd
MK
141 @return Status code.\r
142\r
143**/\r
144EFI_STATUS\r
145EFIAPI\r
146CreateEsrtEntry (\r
147 IN EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf,\r
148 IN UINT32 FmpVersion\r
149 )\r
150{\r
151 UINTN Index;\r
152 EFI_SYSTEM_RESOURCE_ENTRY *Entry;\r
153 UINTN NewSize;\r
154 EFI_SYSTEM_RESOURCE_TABLE *NewTable;\r
155\r
156 Index = 0;\r
157 Entry = NULL;\r
158\r
159 //\r
160 // Get our ESRT table. This should never be null at this point\r
161 //\r
162 if (mTable == NULL) {\r
163 return EFI_DEVICE_ERROR;\r
164 }\r
165\r
166 Entry = (EFI_SYSTEM_RESOURCE_ENTRY *)(mTable + 1);\r
167 //\r
168 // Make sure Guid isn't already in the list\r
169 //\r
170 for (Index = 0; Index < mTable->FwResourceCount; Index++) {\r
171 if (CompareGuid (&Entry->FwClass, &FmpImageInfoBuf->ImageTypeId)) {\r
172 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: ESRT Entry already exists for FMP Instance with GUID %g\n", &Entry->FwClass));\r
173 return EFI_INVALID_PARAMETER;\r
174 }\r
175 Entry++;\r
176 }\r
177\r
178 //\r
179 // Grow table if needed\r
180 //\r
181 if (mTable->FwResourceCount >= mTable->FwResourceCountMax) {\r
182 //\r
183 // Can't grow table after installed.\r
184 // Only because didn't add support for this.\r
185 // Would need to re-install ESRT in system table if wanted to support\r
186 //\r
187 if (mEsrtInstalled) {\r
188 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to install entry because ESRT table needed to grow after table already installed. \n"));\r
189 return EFI_OUT_OF_RESOURCES;\r
190 }\r
191\r
192 NewSize = ((mTable->FwResourceCountMax + GROWTH_STEP) * sizeof (EFI_SYSTEM_RESOURCE_ENTRY)) + sizeof (EFI_SYSTEM_RESOURCE_TABLE);\r
3545488d 193 NewTable = AllocateZeroPool (NewSize);\r
4184aabd
MK
194 if (NewTable == NULL) {\r
195 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to allocate memory larger table for ESRT. \n"));\r
196 return EFI_OUT_OF_RESOURCES;\r
197 }\r
198 //\r
199 // Copy the whole old table into new table buffer\r
200 //\r
201 CopyMem (\r
202 NewTable,\r
203 mTable,\r
204 ((mTable->FwResourceCountMax) * sizeof (EFI_SYSTEM_RESOURCE_ENTRY)) + sizeof (EFI_SYSTEM_RESOURCE_TABLE)\r
205 );\r
206 //\r
207 // Update max\r
208 //\r
209 NewTable->FwResourceCountMax = NewTable->FwResourceCountMax + GROWTH_STEP;\r
210 //\r
211 // Free old table\r
212 //\r
213 FreePool (mTable);\r
214 //\r
215 // Reassign pointer to new table.\r
216 //\r
217 mTable = NewTable;\r
218 }\r
219\r
220 //\r
221 // ESRT table has enough room for the new entry so add new entry\r
222 //\r
223 Entry = (EFI_SYSTEM_RESOURCE_ENTRY *)(((UINT8 *)mTable) + sizeof (EFI_SYSTEM_RESOURCE_TABLE));\r
224 //\r
225 // Move to the location of new entry\r
226 //\r
227 Entry = Entry + mTable->FwResourceCount;\r
228 //\r
229 // Increment resource count\r
230 //\r
231 mTable->FwResourceCount++;\r
232\r
233 CopyGuid (&Entry->FwClass, &FmpImageInfoBuf->ImageTypeId);\r
234\r
235 if (IsSystemFmp (FmpImageInfoBuf)) {\r
236 DEBUG ((DEBUG_INFO, "EsrtFmpDxe: Found an ESRT entry for a System Device.\n"));\r
237 Entry->FwType = (UINT32)(ESRT_FW_TYPE_SYSTEMFIRMWARE);\r
238 } else {\r
239 Entry->FwType = (UINT32)(ESRT_FW_TYPE_DEVICEFIRMWARE);\r
240 }\r
241\r
242 Entry->FwVersion = FmpImageInfoBuf->Version;\r
243 Entry->LowestSupportedFwVersion = 0;\r
244 Entry->CapsuleFlags = 0;\r
245 Entry->LastAttemptVersion = 0;\r
246 Entry->LastAttemptStatus = 0;\r
247\r
248 //\r
249 // VERSION 2 has Lowest Supported\r
250 //\r
251 if (FmpVersion >= 2) {\r
252 Entry->LowestSupportedFwVersion = FmpImageInfoBuf->LowestSupportedImageVersion;\r
253 }\r
254\r
255 //\r
256 // VERSION 3 supports last attempt values\r
257 //\r
258 if (FmpVersion >= 3) {\r
259 Entry->LastAttemptVersion = FmpImageInfoBuf->LastAttemptVersion;\r
260 Entry->LastAttemptStatus = FmpImageInfoBuf->LastAttemptStatus;\r
261 }\r
262\r
263 return EFI_SUCCESS;\r
264}\r
265\r
266/**\r
267 Notify function for every Firmware Management Protocol being installed.\r
268 Get the descriptors from FMP Instance and create ESRT entries (ESRE)\r
269\r
270 @param[in] Event The Event that is being processed.\r
271 @param[in] Context The Event Context.\r
272\r
273**/\r
274VOID\r
275EFIAPI\r
276FmpInstallProtocolNotify (\r
277 IN EFI_EVENT Event,\r
278 IN VOID *Context\r
279 )\r
280{\r
281 EFI_STATUS Status;\r
282 EFI_HANDLE Handle;\r
283 UINTN BufferSize;\r
284 EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;\r
285 UINTN DescriptorSize;\r
286 EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf;\r
287 EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBufOrg;\r
288 UINT8 FmpImageInfoCount;\r
289 UINT32 FmpImageInfoDescriptorVer;\r
290 UINTN ImageInfoSize;\r
291 UINT32 PackageVersion;\r
292 CHAR16 *PackageVersionName;\r
293\r
294 Status = EFI_SUCCESS;\r
295 Handle = 0;\r
296 BufferSize = 0;\r
297 PackageVersionName = NULL;\r
298 FmpImageInfoBuf = NULL;\r
299 FmpImageInfoBufOrg = NULL;\r
300 Fmp = NULL;\r
301\r
302 DEBUG ((DEBUG_INFO, "FMP Installed Notify\n"));\r
303 while (TRUE) {\r
304 BufferSize = sizeof (EFI_HANDLE);\r
305 Status = gBS->LocateHandle (ByRegisterNotify, NULL, mFmpInstallEventRegistration, &BufferSize, &Handle);\r
306 if (EFI_ERROR (Status)) {\r
307 DEBUG ((DEBUG_WARN, "EsrtFmpDxe: Failed to Locate handle from notify value. Status: %r\n", Status));\r
308 return;\r
309 }\r
310\r
311 Status = gBS->HandleProtocol (Handle, &gEfiFirmwareManagementProtocolGuid, (VOID **)&Fmp);\r
312 if (EFI_ERROR (Status)) {\r
313 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to get FMP for a handle 0x%x\n", Handle));\r
314 continue;\r
315 }\r
316 ImageInfoSize = 0;\r
317\r
318 Status = Fmp->GetImageInfo (\r
319 Fmp, // FMP Pointer\r
320 &ImageInfoSize, // Buffer Size (in this case 0)\r
321 NULL, // NULL so we can get size\r
322 &FmpImageInfoDescriptorVer, // DescriptorVersion\r
323 &FmpImageInfoCount, // DescriptorCount\r
324 &DescriptorSize, // DescriptorSize\r
325 &PackageVersion, // PackageVersion\r
326 &PackageVersionName // PackageVersionName\r
327 );\r
328\r
329 if (Status != EFI_BUFFER_TOO_SMALL) {\r
330 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Unexpected Failure in GetImageInfo. Status = %r\n", Status));\r
331 continue;\r
332 }\r
333\r
334 FmpImageInfoBuf = NULL;\r
335 FmpImageInfoBuf = AllocateZeroPool (ImageInfoSize);\r
336 if (FmpImageInfoBuf == NULL) {\r
337 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to get memory for descriptors.\n"));\r
338 continue;\r
339 }\r
340\r
341 FmpImageInfoBufOrg = FmpImageInfoBuf;\r
342 PackageVersionName = NULL;\r
343 Status = Fmp->GetImageInfo (\r
344 Fmp,\r
345 &ImageInfoSize, // ImageInfoSize\r
346 FmpImageInfoBuf, // ImageInfo\r
347 &FmpImageInfoDescriptorVer, // DescriptorVersion\r
348 &FmpImageInfoCount, // DescriptorCount\r
349 &DescriptorSize, // DescriptorSize\r
350 &PackageVersion, // PackageVersion\r
351 &PackageVersionName // PackageVersionName\r
352 );\r
353 if (EFI_ERROR (Status)) {\r
354 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failure in GetImageInfo. Status = %r\n", Status));\r
355 goto CleanUp;\r
356 }\r
357\r
358 //\r
359 // Check each descriptor and read from the one specified\r
360 //\r
361 while (FmpImageInfoCount > 0) {\r
362 //\r
363 // If the descriptor has the IN USE bit set, create ESRT entry otherwise ignore.\r
364 //\r
365 if ((FmpImageInfoBuf->AttributesSetting & FmpImageInfoBuf->AttributesSupported & IMAGE_ATTRIBUTE_IN_USE) == IMAGE_ATTRIBUTE_IN_USE) {\r
366 //\r
367 // Create ESRT entry\r
368 //\r
369 CreateEsrtEntry (FmpImageInfoBuf, FmpImageInfoDescriptorVer);\r
370 }\r
371 FmpImageInfoCount--;\r
372 //\r
373 // Increment the buffer pointer ahead by the size of the descriptor\r
374 //\r
375 FmpImageInfoBuf = (EFI_FIRMWARE_IMAGE_DESCRIPTOR *)(((UINT8 *)FmpImageInfoBuf) + DescriptorSize);\r
376 }\r
377\r
378 if (PackageVersionName != NULL) {\r
379 FreePool (PackageVersionName);\r
380 PackageVersionName = NULL;\r
381 }\r
382 if (FmpImageInfoBufOrg != NULL) {\r
383 FreePool (FmpImageInfoBufOrg);\r
384 FmpImageInfoBufOrg = NULL;\r
385 }\r
386 }\r
387\r
388CleanUp:\r
389 if (FmpImageInfoBufOrg != NULL) {\r
390 FreePool (FmpImageInfoBufOrg);\r
391 }\r
392 return;\r
393}\r
394\r
395/**\r
396 Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to\r
397 install the Efi System Resource Table.\r
398\r
399 @param[in] Event The Event that is being processed.\r
400 @param[in] Context The Event Context.\r
401\r
402**/\r
403VOID\r
404EFIAPI\r
405EsrtReadyToBootEventNotify (\r
406 IN EFI_EVENT Event,\r
407 IN VOID *Context\r
408 )\r
409{\r
410 InstallEfiSystemResourceTableInUefiConfigurationTable ();\r
411\r
412 //\r
413 // Print table on debug builds\r
414 //\r
415 DEBUG_CODE_BEGIN ();\r
416 PrintTable (mTable);\r
417 DEBUG_CODE_END ();\r
418}\r
419\r
420/**\r
421 The module Entry Point of the Efi System Resource Table DXE driver.\r
422\r
423 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
424 @param[in] SystemTable A pointer to the EFI System Table.\r
425\r
426 @retval EFI_SUCCESS The entry point is executed successfully.\r
427 @retval Other Some error occurs when executing this entry point.\r
428\r
429**/\r
430EFI_STATUS\r
431EFIAPI\r
432EsrtFmpEntryPoint (\r
433 IN EFI_HANDLE ImageHandle,\r
434 IN EFI_SYSTEM_TABLE *SystemTable\r
435 )\r
436{\r
437 EFI_STATUS Status;\r
438\r
439 //\r
440 // Allocate Memory for table\r
441 //\r
3545488d 442 mTable = AllocateZeroPool (\r
4184aabd
MK
443 (GROWTH_STEP * sizeof (EFI_SYSTEM_RESOURCE_ENTRY)) + sizeof (EFI_SYSTEM_RESOURCE_TABLE)\r
444 );\r
445 ASSERT (mTable != NULL);\r
446 if (mTable == NULL) {\r
447 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to allocate memory for ESRT.\n"));\r
448 return EFI_OUT_OF_RESOURCES;\r
449 }\r
450\r
451 mTable->FwResourceCount = 0;\r
452 mTable->FwResourceCountMax = GROWTH_STEP;\r
453 mTable->FwResourceVersion = EFI_SYSTEM_RESOURCE_TABLE_FIRMWARE_RESOURCE_VERSION;\r
454\r
455 //\r
456 // Register notify function for all FMP installed\r
457 //\r
458 mFmpInstallEvent = EfiCreateProtocolNotifyEvent (\r
459 &gEfiFirmwareManagementProtocolGuid,\r
460 TPL_CALLBACK,\r
461 FmpInstallProtocolNotify,\r
462 NULL,\r
463 &mFmpInstallEventRegistration\r
464 );\r
465\r
466 ASSERT (mFmpInstallEvent != NULL);\r
467\r
468 if (mFmpInstallEvent == NULL) {\r
469 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to Create Protocol Notify Event for FMP.\n"));\r
470 }\r
471\r
472 //\r
473 // Register notify function to install ESRT on ReadyToBoot Event.\r
474 //\r
475 Status = EfiCreateEventReadyToBootEx (\r
476 TPL_CALLBACK,\r
477 EsrtReadyToBootEventNotify,\r
478 NULL,\r
479 &mEsrtReadyToBootEvent\r
480 );\r
481\r
482 ASSERT_EFI_ERROR (Status);\r
483 if (EFI_ERROR (Status)) {\r
484 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to register for ready to boot\n"));\r
485 }\r
486\r
487 return Status;\r
488}\r