2 Publishes ESRT table from Firmware Management Protocol instances
4 Copyright (c) 2016, Microsoft Corporation
5 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <Library/BaseLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/UefiBootServicesTableLib.h>
34 #include <Library/DebugLib.h>
35 #include <Library/PcdLib.h>
36 #include <Library/UefiLib.h>
37 #include <Protocol/FirmwareManagement.h>
38 #include <Guid/EventGroup.h>
39 #include <Guid/SystemResourceTable.h>
42 Print ESRT to debug console.
44 @param[in] Table Pointer to the ESRT table.
50 IN EFI_SYSTEM_RESOURCE_TABLE
*Table
54 // Number of ESRT entries to grow by each time we run out of room
56 #define GROWTH_STEP 10
61 EFI_EVENT mEsrtReadyToBootEvent
;
62 EFI_SYSTEM_RESOURCE_TABLE
*mTable
= NULL
;
63 BOOLEAN mEsrtInstalled
= FALSE
;
64 EFI_EVENT mFmpInstallEvent
;
65 VOID
*mFmpInstallEventRegistration
= NULL
;
68 Install EFI System Resource Table into the UEFI Configuration Table
74 InstallEfiSystemResourceTableInUefiConfigurationTable (
81 if (!mEsrtInstalled
) {
83 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Can't install ESRT table because it is NULL. \n"));
84 Status
= EFI_OUT_OF_RESOURCES
;
85 } else if (mTable
->FwResourceCount
== 0) {
86 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Can't install ESRT table because it has zero Entries. \n"));
87 Status
= EFI_UNSUPPORTED
;
90 // Install the pointer into config table
92 Status
= gBS
->InstallConfigurationTable (&gEfiSystemResourceTableGuid
, mTable
);
93 if (EFI_ERROR (Status
)) {
94 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Can't install ESRT table. Status: %r. \n", Status
));
96 DEBUG ((DEBUG_INFO
, "EsrtFmpDxe: Installed ESRT table. \n"));
97 mEsrtInstalled
= TRUE
;
105 Return if this FMP is a system FMP or a device FMP, based upon FmpImageInfo.
107 @param[in] FmpImageInfo A pointer to EFI_FIRMWARE_IMAGE_DESCRIPTOR
109 @return TRUE It is a system FMP.
110 @return FALSE It is a device FMP.
114 IN EFI_FIRMWARE_IMAGE_DESCRIPTOR
*FmpImageInfo
121 Guid
= PcdGetPtr (PcdSystemFmpCapsuleImageTypeIdGuid
);
122 Count
= PcdGetSize (PcdSystemFmpCapsuleImageTypeIdGuid
) / sizeof(GUID
);
124 for (Index
= 0; Index
< Count
; Index
++, Guid
++) {
125 if (CompareGuid (&FmpImageInfo
->ImageTypeId
, Guid
)) {
134 Function to create a single ESRT Entry and add it to the ESRT
135 given a FMP descriptor. If the guid is already in the ESRT it
136 will be ignored. The ESRT will grow if it does not have enough room.
138 @param[in] FmpImageInfoBuf Pointer to the EFI_FIRMWARE_IMAGE_DESCRIPTOR.
139 @param[in] FmpVersion FMP Version.
147 IN EFI_FIRMWARE_IMAGE_DESCRIPTOR
*FmpImageInfoBuf
,
152 EFI_SYSTEM_RESOURCE_ENTRY
*Entry
;
154 EFI_SYSTEM_RESOURCE_TABLE
*NewTable
;
160 // Get our ESRT table. This should never be null at this point
162 if (mTable
== NULL
) {
163 return EFI_DEVICE_ERROR
;
166 Entry
= (EFI_SYSTEM_RESOURCE_ENTRY
*)(mTable
+ 1);
168 // Make sure Guid isn't already in the list
170 for (Index
= 0; Index
< mTable
->FwResourceCount
; Index
++) {
171 if (CompareGuid (&Entry
->FwClass
, &FmpImageInfoBuf
->ImageTypeId
)) {
172 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: ESRT Entry already exists for FMP Instance with GUID %g\n", &Entry
->FwClass
));
173 return EFI_INVALID_PARAMETER
;
179 // Grow table if needed
181 if (mTable
->FwResourceCount
>= mTable
->FwResourceCountMax
) {
183 // Can't grow table after installed.
184 // Only because didn't add support for this.
185 // Would need to re-install ESRT in system table if wanted to support
187 if (mEsrtInstalled
) {
188 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Failed to install entry because ESRT table needed to grow after table already installed. \n"));
189 return EFI_OUT_OF_RESOURCES
;
192 NewSize
= ((mTable
->FwResourceCountMax
+ GROWTH_STEP
) * sizeof (EFI_SYSTEM_RESOURCE_ENTRY
)) + sizeof (EFI_SYSTEM_RESOURCE_TABLE
);
193 NewTable
= AllocateZeroPool (NewSize
);
194 if (NewTable
== NULL
) {
195 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Failed to allocate memory larger table for ESRT. \n"));
196 return EFI_OUT_OF_RESOURCES
;
199 // Copy the whole old table into new table buffer
204 ((mTable
->FwResourceCountMax
) * sizeof (EFI_SYSTEM_RESOURCE_ENTRY
)) + sizeof (EFI_SYSTEM_RESOURCE_TABLE
)
209 NewTable
->FwResourceCountMax
= NewTable
->FwResourceCountMax
+ GROWTH_STEP
;
215 // Reassign pointer to new table.
221 // ESRT table has enough room for the new entry so add new entry
223 Entry
= (EFI_SYSTEM_RESOURCE_ENTRY
*)(((UINT8
*)mTable
) + sizeof (EFI_SYSTEM_RESOURCE_TABLE
));
225 // Move to the location of new entry
227 Entry
= Entry
+ mTable
->FwResourceCount
;
229 // Increment resource count
231 mTable
->FwResourceCount
++;
233 CopyGuid (&Entry
->FwClass
, &FmpImageInfoBuf
->ImageTypeId
);
235 if (IsSystemFmp (FmpImageInfoBuf
)) {
236 DEBUG ((DEBUG_INFO
, "EsrtFmpDxe: Found an ESRT entry for a System Device.\n"));
237 Entry
->FwType
= (UINT32
)(ESRT_FW_TYPE_SYSTEMFIRMWARE
);
239 Entry
->FwType
= (UINT32
)(ESRT_FW_TYPE_DEVICEFIRMWARE
);
242 Entry
->FwVersion
= FmpImageInfoBuf
->Version
;
243 Entry
->LowestSupportedFwVersion
= 0;
244 Entry
->CapsuleFlags
= 0;
245 Entry
->LastAttemptVersion
= 0;
246 Entry
->LastAttemptStatus
= 0;
249 // VERSION 2 has Lowest Supported
251 if (FmpVersion
>= 2) {
252 Entry
->LowestSupportedFwVersion
= FmpImageInfoBuf
->LowestSupportedImageVersion
;
256 // VERSION 3 supports last attempt values
258 if (FmpVersion
>= 3) {
259 Entry
->LastAttemptVersion
= FmpImageInfoBuf
->LastAttemptVersion
;
260 Entry
->LastAttemptStatus
= FmpImageInfoBuf
->LastAttemptStatus
;
267 Notify function for every Firmware Management Protocol being installed.
268 Get the descriptors from FMP Instance and create ESRT entries (ESRE)
270 @param[in] Event The Event that is being processed.
271 @param[in] Context The Event Context.
276 FmpInstallProtocolNotify (
284 EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*Fmp
;
285 UINTN DescriptorSize
;
286 EFI_FIRMWARE_IMAGE_DESCRIPTOR
*FmpImageInfoBuf
;
287 EFI_FIRMWARE_IMAGE_DESCRIPTOR
*FmpImageInfoBufOrg
;
288 UINT8 FmpImageInfoCount
;
289 UINT32 FmpImageInfoDescriptorVer
;
291 UINT32 PackageVersion
;
292 CHAR16
*PackageVersionName
;
294 Status
= EFI_SUCCESS
;
297 PackageVersionName
= NULL
;
298 FmpImageInfoBuf
= NULL
;
299 FmpImageInfoBufOrg
= NULL
;
302 DEBUG ((DEBUG_INFO
, "FMP Installed Notify\n"));
304 BufferSize
= sizeof (EFI_HANDLE
);
305 Status
= gBS
->LocateHandle (ByRegisterNotify
, NULL
, mFmpInstallEventRegistration
, &BufferSize
, &Handle
);
306 if (EFI_ERROR (Status
)) {
307 DEBUG ((DEBUG_WARN
, "EsrtFmpDxe: Failed to Locate handle from notify value. Status: %r\n", Status
));
311 Status
= gBS
->HandleProtocol (Handle
, &gEfiFirmwareManagementProtocolGuid
, (VOID
**)&Fmp
);
312 if (EFI_ERROR (Status
)) {
313 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Failed to get FMP for a handle 0x%x\n", Handle
));
318 Status
= Fmp
->GetImageInfo (
320 &ImageInfoSize
, // Buffer Size (in this case 0)
321 NULL
, // NULL so we can get size
322 &FmpImageInfoDescriptorVer
, // DescriptorVersion
323 &FmpImageInfoCount
, // DescriptorCount
324 &DescriptorSize
, // DescriptorSize
325 &PackageVersion
, // PackageVersion
326 &PackageVersionName
// PackageVersionName
329 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
330 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Unexpected Failure in GetImageInfo. Status = %r\n", Status
));
334 FmpImageInfoBuf
= NULL
;
335 FmpImageInfoBuf
= AllocateZeroPool (ImageInfoSize
);
336 if (FmpImageInfoBuf
== NULL
) {
337 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Failed to get memory for descriptors.\n"));
341 FmpImageInfoBufOrg
= FmpImageInfoBuf
;
342 PackageVersionName
= NULL
;
343 Status
= Fmp
->GetImageInfo (
345 &ImageInfoSize
, // ImageInfoSize
346 FmpImageInfoBuf
, // ImageInfo
347 &FmpImageInfoDescriptorVer
, // DescriptorVersion
348 &FmpImageInfoCount
, // DescriptorCount
349 &DescriptorSize
, // DescriptorSize
350 &PackageVersion
, // PackageVersion
351 &PackageVersionName
// PackageVersionName
353 if (EFI_ERROR (Status
)) {
354 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Failure in GetImageInfo. Status = %r\n", Status
));
359 // Check each descriptor and read from the one specified
361 while (FmpImageInfoCount
> 0) {
363 // If the descriptor has the IN USE bit set, create ESRT entry otherwise ignore.
365 if ((FmpImageInfoBuf
->AttributesSetting
& FmpImageInfoBuf
->AttributesSupported
& IMAGE_ATTRIBUTE_IN_USE
) == IMAGE_ATTRIBUTE_IN_USE
) {
369 CreateEsrtEntry (FmpImageInfoBuf
, FmpImageInfoDescriptorVer
);
373 // Increment the buffer pointer ahead by the size of the descriptor
375 FmpImageInfoBuf
= (EFI_FIRMWARE_IMAGE_DESCRIPTOR
*)(((UINT8
*)FmpImageInfoBuf
) + DescriptorSize
);
378 if (PackageVersionName
!= NULL
) {
379 FreePool (PackageVersionName
);
380 PackageVersionName
= NULL
;
382 if (FmpImageInfoBufOrg
!= NULL
) {
383 FreePool (FmpImageInfoBufOrg
);
384 FmpImageInfoBufOrg
= NULL
;
389 if (FmpImageInfoBufOrg
!= NULL
) {
390 FreePool (FmpImageInfoBufOrg
);
396 Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to
397 install the Efi System Resource Table.
399 @param[in] Event The Event that is being processed.
400 @param[in] Context The Event Context.
405 EsrtReadyToBootEventNotify (
410 InstallEfiSystemResourceTableInUefiConfigurationTable ();
413 // Print table on debug builds
421 The module Entry Point of the Efi System Resource Table DXE driver.
423 @param[in] ImageHandle The firmware allocated handle for the EFI image.
424 @param[in] SystemTable A pointer to the EFI System Table.
426 @retval EFI_SUCCESS The entry point is executed successfully.
427 @retval Other Some error occurs when executing this entry point.
433 IN EFI_HANDLE ImageHandle
,
434 IN EFI_SYSTEM_TABLE
*SystemTable
440 // Allocate Memory for table
442 mTable
= AllocateZeroPool (
443 (GROWTH_STEP
* sizeof (EFI_SYSTEM_RESOURCE_ENTRY
)) + sizeof (EFI_SYSTEM_RESOURCE_TABLE
)
445 ASSERT (mTable
!= NULL
);
446 if (mTable
== NULL
) {
447 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Failed to allocate memory for ESRT.\n"));
448 return EFI_OUT_OF_RESOURCES
;
451 mTable
->FwResourceCount
= 0;
452 mTable
->FwResourceCountMax
= GROWTH_STEP
;
453 mTable
->FwResourceVersion
= EFI_SYSTEM_RESOURCE_TABLE_FIRMWARE_RESOURCE_VERSION
;
456 // Register notify function for all FMP installed
458 mFmpInstallEvent
= EfiCreateProtocolNotifyEvent (
459 &gEfiFirmwareManagementProtocolGuid
,
461 FmpInstallProtocolNotify
,
463 &mFmpInstallEventRegistration
466 ASSERT (mFmpInstallEvent
!= NULL
);
468 if (mFmpInstallEvent
== NULL
) {
469 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Failed to Create Protocol Notify Event for FMP.\n"));
473 // Register notify function to install ESRT on ReadyToBoot Event.
475 Status
= EfiCreateEventReadyToBootEx (
477 EsrtReadyToBootEventNotify
,
479 &mEsrtReadyToBootEvent
482 ASSERT_EFI_ERROR (Status
);
483 if (EFI_ERROR (Status
)) {
484 DEBUG ((DEBUG_ERROR
, "EsrtFmpDxe: Failed to register for ready to boot\n"));