]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/EsrtFmpDxe/EsrtFmp.c
3da4a62fb1c404e07a1da7c5fb9e61a9f8c2763e
[mirror_edk2.git] / MdeModulePkg / Universal / EsrtFmpDxe / EsrtFmp.c
1 /** @file
2 Publishes ESRT table from Firmware Management Protocol instances
3
4 Copyright (c) 2016, Microsoft Corporation
5 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
6
7 All rights reserved.
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.
15
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.
26
27 **/
28
29 #include <Uefi.h>
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>
40
41 /**
42 Print ESRT to debug console.
43
44 @param[in] Table Pointer to the ESRT table.
45
46 **/
47 VOID
48 EFIAPI
49 PrintTable (
50 IN EFI_SYSTEM_RESOURCE_TABLE *Table
51 );
52
53 //
54 // Number of ESRT entries to grow by each time we run out of room
55 //
56 #define GROWTH_STEP 10
57
58 //
59 // Module globals.
60 //
61 EFI_EVENT mEsrtReadyToBootEvent;
62 EFI_SYSTEM_RESOURCE_TABLE *mTable = NULL;
63 BOOLEAN mEsrtInstalled = FALSE;
64 EFI_EVENT mFmpInstallEvent;
65 VOID *mFmpInstallEventRegistration = NULL;
66
67 /**
68 Install EFI System Resource Table into the UEFI Configuration Table
69
70 @return Status code.
71
72 **/
73 EFI_STATUS
74 InstallEfiSystemResourceTableInUefiConfigurationTable (
75 VOID
76 )
77 {
78 EFI_STATUS Status;
79
80 Status = EFI_SUCCESS;
81 if (!mEsrtInstalled) {
82 if (mTable == NULL) {
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;
88 } else {
89 //
90 // Install the pointer into config table
91 //
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));
95 } else {
96 DEBUG ((DEBUG_INFO, "EsrtFmpDxe: Installed ESRT table. \n"));
97 mEsrtInstalled = TRUE;
98 }
99 }
100 }
101 return Status;
102 }
103
104 /**
105 Return if this FMP is a system FMP or a device FMP, based upon FmpImageInfo.
106
107 @param[in] FmpImageInfo A pointer to EFI_FIRMWARE_IMAGE_DESCRIPTOR
108
109 @return TRUE It is a system FMP.
110 @return FALSE It is a device FMP.
111 **/
112 BOOLEAN
113 IsSystemFmp (
114 IN EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfo
115 )
116 {
117 GUID *Guid;
118 UINTN Count;
119 UINTN Index;
120
121 Guid = PcdGetPtr (PcdSystemFmpCapsuleImageTypeIdGuid);
122 Count = PcdGetSize (PcdSystemFmpCapsuleImageTypeIdGuid) / sizeof(GUID);
123
124 for (Index = 0; Index < Count; Index++, Guid++) {
125 if (CompareGuid (&FmpImageInfo->ImageTypeId, Guid)) {
126 return TRUE;
127 }
128 }
129
130 return FALSE;
131 }
132
133 /**
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.
137
138 @param[in] FmpImageInfoBuf Pointer to the EFI_FIRMWARE_IMAGE_DESCRIPTOR.
139 @param[in] FmpVersion FMP Version.
140
141 @return Status code.
142
143 **/
144 EFI_STATUS
145 EFIAPI
146 CreateEsrtEntry (
147 IN EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf,
148 IN UINT32 FmpVersion
149 )
150 {
151 UINTN Index;
152 EFI_SYSTEM_RESOURCE_ENTRY *Entry;
153 UINTN NewSize;
154 EFI_SYSTEM_RESOURCE_TABLE *NewTable;
155
156 Index = 0;
157 Entry = NULL;
158
159 //
160 // Get our ESRT table. This should never be null at this point
161 //
162 if (mTable == NULL) {
163 return EFI_DEVICE_ERROR;
164 }
165
166 Entry = (EFI_SYSTEM_RESOURCE_ENTRY *)(mTable + 1);
167 //
168 // Make sure Guid isn't already in the list
169 //
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;
174 }
175 Entry++;
176 }
177
178 //
179 // Grow table if needed
180 //
181 if (mTable->FwResourceCount >= mTable->FwResourceCountMax) {
182 //
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
186 //
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;
190 }
191
192 NewSize = ((mTable->FwResourceCountMax + GROWTH_STEP) * sizeof (EFI_SYSTEM_RESOURCE_ENTRY)) + sizeof (EFI_SYSTEM_RESOURCE_TABLE);
193 NewTable = AllocateRuntimeZeroPool (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;
197 }
198 //
199 // Copy the whole old table into new table buffer
200 //
201 CopyMem (
202 NewTable,
203 mTable,
204 ((mTable->FwResourceCountMax) * sizeof (EFI_SYSTEM_RESOURCE_ENTRY)) + sizeof (EFI_SYSTEM_RESOURCE_TABLE)
205 );
206 //
207 // Update max
208 //
209 NewTable->FwResourceCountMax = NewTable->FwResourceCountMax + GROWTH_STEP;
210 //
211 // Free old table
212 //
213 FreePool (mTable);
214 //
215 // Reassign pointer to new table.
216 //
217 mTable = NewTable;
218 }
219
220 //
221 // ESRT table has enough room for the new entry so add new entry
222 //
223 Entry = (EFI_SYSTEM_RESOURCE_ENTRY *)(((UINT8 *)mTable) + sizeof (EFI_SYSTEM_RESOURCE_TABLE));
224 //
225 // Move to the location of new entry
226 //
227 Entry = Entry + mTable->FwResourceCount;
228 //
229 // Increment resource count
230 //
231 mTable->FwResourceCount++;
232
233 CopyGuid (&Entry->FwClass, &FmpImageInfoBuf->ImageTypeId);
234
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);
238 } else {
239 Entry->FwType = (UINT32)(ESRT_FW_TYPE_DEVICEFIRMWARE);
240 }
241
242 Entry->FwVersion = FmpImageInfoBuf->Version;
243 Entry->LowestSupportedFwVersion = 0;
244 Entry->CapsuleFlags = 0;
245 Entry->LastAttemptVersion = 0;
246 Entry->LastAttemptStatus = 0;
247
248 //
249 // VERSION 2 has Lowest Supported
250 //
251 if (FmpVersion >= 2) {
252 Entry->LowestSupportedFwVersion = FmpImageInfoBuf->LowestSupportedImageVersion;
253 }
254
255 //
256 // VERSION 3 supports last attempt values
257 //
258 if (FmpVersion >= 3) {
259 Entry->LastAttemptVersion = FmpImageInfoBuf->LastAttemptVersion;
260 Entry->LastAttemptStatus = FmpImageInfoBuf->LastAttemptStatus;
261 }
262
263 return EFI_SUCCESS;
264 }
265
266 /**
267 Notify function for every Firmware Management Protocol being installed.
268 Get the descriptors from FMP Instance and create ESRT entries (ESRE)
269
270 @param[in] Event The Event that is being processed.
271 @param[in] Context The Event Context.
272
273 **/
274 VOID
275 EFIAPI
276 FmpInstallProtocolNotify (
277 IN EFI_EVENT Event,
278 IN VOID *Context
279 )
280 {
281 EFI_STATUS Status;
282 EFI_HANDLE Handle;
283 UINTN BufferSize;
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;
290 UINTN ImageInfoSize;
291 UINT32 PackageVersion;
292 CHAR16 *PackageVersionName;
293
294 Status = EFI_SUCCESS;
295 Handle = 0;
296 BufferSize = 0;
297 PackageVersionName = NULL;
298 FmpImageInfoBuf = NULL;
299 FmpImageInfoBufOrg = NULL;
300 Fmp = NULL;
301
302 DEBUG ((DEBUG_INFO, "FMP Installed Notify\n"));
303 while (TRUE) {
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));
308 return;
309 }
310
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));
314 continue;
315 }
316 ImageInfoSize = 0;
317
318 Status = Fmp->GetImageInfo (
319 Fmp, // FMP Pointer
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
327 );
328
329 if (Status != EFI_BUFFER_TOO_SMALL) {
330 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Unexpected Failure in GetImageInfo. Status = %r\n", Status));
331 continue;
332 }
333
334 FmpImageInfoBuf = NULL;
335 FmpImageInfoBuf = AllocateZeroPool (ImageInfoSize);
336 if (FmpImageInfoBuf == NULL) {
337 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to get memory for descriptors.\n"));
338 continue;
339 }
340
341 FmpImageInfoBufOrg = FmpImageInfoBuf;
342 PackageVersionName = NULL;
343 Status = Fmp->GetImageInfo (
344 Fmp,
345 &ImageInfoSize, // ImageInfoSize
346 FmpImageInfoBuf, // ImageInfo
347 &FmpImageInfoDescriptorVer, // DescriptorVersion
348 &FmpImageInfoCount, // DescriptorCount
349 &DescriptorSize, // DescriptorSize
350 &PackageVersion, // PackageVersion
351 &PackageVersionName // PackageVersionName
352 );
353 if (EFI_ERROR (Status)) {
354 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failure in GetImageInfo. Status = %r\n", Status));
355 goto CleanUp;
356 }
357
358 //
359 // Check each descriptor and read from the one specified
360 //
361 while (FmpImageInfoCount > 0) {
362 //
363 // If the descriptor has the IN USE bit set, create ESRT entry otherwise ignore.
364 //
365 if ((FmpImageInfoBuf->AttributesSetting & FmpImageInfoBuf->AttributesSupported & IMAGE_ATTRIBUTE_IN_USE) == IMAGE_ATTRIBUTE_IN_USE) {
366 //
367 // Create ESRT entry
368 //
369 CreateEsrtEntry (FmpImageInfoBuf, FmpImageInfoDescriptorVer);
370 }
371 FmpImageInfoCount--;
372 //
373 // Increment the buffer pointer ahead by the size of the descriptor
374 //
375 FmpImageInfoBuf = (EFI_FIRMWARE_IMAGE_DESCRIPTOR *)(((UINT8 *)FmpImageInfoBuf) + DescriptorSize);
376 }
377
378 if (PackageVersionName != NULL) {
379 FreePool (PackageVersionName);
380 PackageVersionName = NULL;
381 }
382 if (FmpImageInfoBufOrg != NULL) {
383 FreePool (FmpImageInfoBufOrg);
384 FmpImageInfoBufOrg = NULL;
385 }
386 }
387
388 CleanUp:
389 if (FmpImageInfoBufOrg != NULL) {
390 FreePool (FmpImageInfoBufOrg);
391 }
392 return;
393 }
394
395 /**
396 Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to
397 install the Efi System Resource Table.
398
399 @param[in] Event The Event that is being processed.
400 @param[in] Context The Event Context.
401
402 **/
403 VOID
404 EFIAPI
405 EsrtReadyToBootEventNotify (
406 IN EFI_EVENT Event,
407 IN VOID *Context
408 )
409 {
410 InstallEfiSystemResourceTableInUefiConfigurationTable ();
411
412 //
413 // Print table on debug builds
414 //
415 DEBUG_CODE_BEGIN ();
416 PrintTable (mTable);
417 DEBUG_CODE_END ();
418 }
419
420 /**
421 The module Entry Point of the Efi System Resource Table DXE driver.
422
423 @param[in] ImageHandle The firmware allocated handle for the EFI image.
424 @param[in] SystemTable A pointer to the EFI System Table.
425
426 @retval EFI_SUCCESS The entry point is executed successfully.
427 @retval Other Some error occurs when executing this entry point.
428
429 **/
430 EFI_STATUS
431 EFIAPI
432 EsrtFmpEntryPoint (
433 IN EFI_HANDLE ImageHandle,
434 IN EFI_SYSTEM_TABLE *SystemTable
435 )
436 {
437 EFI_STATUS Status;
438
439 //
440 // Allocate Memory for table
441 //
442 mTable = AllocateRuntimeZeroPool (
443 (GROWTH_STEP * sizeof (EFI_SYSTEM_RESOURCE_ENTRY)) + sizeof (EFI_SYSTEM_RESOURCE_TABLE)
444 );
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;
449 }
450
451 mTable->FwResourceCount = 0;
452 mTable->FwResourceCountMax = GROWTH_STEP;
453 mTable->FwResourceVersion = EFI_SYSTEM_RESOURCE_TABLE_FIRMWARE_RESOURCE_VERSION;
454
455 //
456 // Register notify function for all FMP installed
457 //
458 mFmpInstallEvent = EfiCreateProtocolNotifyEvent (
459 &gEfiFirmwareManagementProtocolGuid,
460 TPL_CALLBACK,
461 FmpInstallProtocolNotify,
462 NULL,
463 &mFmpInstallEventRegistration
464 );
465
466 ASSERT (mFmpInstallEvent != NULL);
467
468 if (mFmpInstallEvent == NULL) {
469 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to Create Protocol Notify Event for FMP.\n"));
470 }
471
472 //
473 // Register notify function to install ESRT on ReadyToBoot Event.
474 //
475 Status = EfiCreateEventReadyToBootEx (
476 TPL_CALLBACK,
477 EsrtReadyToBootEventNotify,
478 NULL,
479 &mEsrtReadyToBootEvent
480 );
481
482 ASSERT_EFI_ERROR (Status);
483 if (EFI_ERROR (Status)) {
484 DEBUG ((DEBUG_ERROR, "EsrtFmpDxe: Failed to register for ready to boot\n"));
485 }
486
487 return Status;
488 }