2 Install Platform EFI_PEI_RECOVERY_MODULE_PPI and Implementation of
3 EFI_PEI_LOAD_RECOVERY_CAPSULE service.
5 Copyright (c) 2013-2019 Intel Corporation.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "CommonHeader.h"
12 #include "PlatformEarlyInit.h"
14 #include <Ppi/BlockIo.h>
17 // Capsule Types supported in this platform module
19 #include <Guid/CapsuleOnFatFloppyDisk.h>
20 #include <Guid/CapsuleOnFatIdeDisk.h>
21 #include <Guid/CapsuleOnFatUsbDisk.h>
22 #include <Guid/CapsuleOnDataCD.h>
23 #include <Guid/QuarkCapsuleGuid.h>
25 #include <Ppi/RecoveryModule.h>
26 #include <Ppi/DeviceRecoveryModule.h>
28 #include <Library/PeiServicesLib.h>
35 PlatformRecoveryModule (
36 IN EFI_PEI_SERVICES
**PeiServices
,
37 IN EFI_PEI_RECOVERY_MODULE_PPI
*This
41 AssertNoCapsulesError (
42 IN EFI_PEI_SERVICES
**PeiServices
46 AssertMediaDeviceError (
47 IN EFI_PEI_SERVICES
**PeiServices
51 ReportLoadCapsuleSuccess (
52 IN EFI_PEI_SERVICES
**PeiServices
56 CheckIfMediaPresentOnBlockIoDevice (
57 IN EFI_PEI_SERVICES
**PeiServices
,
58 IN OUT BOOLEAN
*MediaDeviceError
,
59 IN OUT BOOLEAN
*MediaPresent
65 EFI_PEI_RECOVERY_MODULE_PPI mRecoveryPpi
= { PlatformRecoveryModule
};
67 EFI_PEI_PPI_DESCRIPTOR mRecoveryPpiList
= {
68 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
69 &gEfiPeiRecoveryModulePpiGuid
,
75 PeimInitializeRecovery (
76 IN EFI_PEI_SERVICES
**PeiServices
82 Provide the functionality of the Recovery Module.
86 PeiServices - General purpose services available to every PEIM.
90 EFI_SUCCESS - If the interface could be successfully
97 Status
= PeiServicesInstallPpi (&mRecoveryPpiList
);
104 PlatformRecoveryModule (
105 IN EFI_PEI_SERVICES
**PeiServices
,
106 IN EFI_PEI_RECOVERY_MODULE_PPI
*This
112 Provide the functionality of the Platform Recovery Module.
116 PeiServices - General purpose services available to every PEIM.
117 This - Pointer to EFI_PEI_RECOVERY_MODULE_PPI.
121 EFI_SUCCESS - If the interface could be successfully
123 EFI_UNSUPPORTED - Not supported.
128 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*DeviceRecoveryModule
;
129 UINTN NumberOfImageProviders
;
130 BOOLEAN ProviderAvailable
;
131 UINTN NumberRecoveryCapsules
;
132 UINTN RecoveryCapsuleSize
;
134 EFI_PHYSICAL_ADDRESS Address
;
136 EFI_CAPSULE_HEADER
*CapsuleHeader
;
137 EFI_PEI_HOB_POINTERS Hob
;
139 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
141 EFI_GUID mEfiCapsuleHeaderGuid
= QUARK_CAPSULE_GUID
;
145 Status
= EFI_SUCCESS
;
148 ProviderAvailable
= TRUE
;
149 NumberOfImageProviders
= 0;
151 DeviceRecoveryModule
= NULL
;
153 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Recovery Entry\n"));
156 // Search the platform for some recovery capsule if the DXE IPL
157 // discovered a recovery condition and has requested a load.
159 while (ProviderAvailable
) {
161 Status
= PeiServicesLocatePpi (
162 &gEfiPeiDeviceRecoveryModulePpiGuid
,
165 (VOID
**)&DeviceRecoveryModule
168 if (!EFI_ERROR (Status
)) {
169 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Device Recovery PPI located\n"));
170 NumberOfImageProviders
++;
172 Status
= DeviceRecoveryModule
->GetNumberRecoveryCapsules (
174 DeviceRecoveryModule
,
175 &NumberRecoveryCapsules
178 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Number Of Recovery Capsules: %d\n", NumberRecoveryCapsules
));
180 if (NumberRecoveryCapsules
== 0) {
186 ProviderAvailable
= FALSE
;
190 // The number of recovery capsules is 0.
192 if (!ProviderAvailable
) {
193 AssertNoCapsulesError (PeiServices
);
196 // If there is an image provider, get the capsule ID
198 if (ProviderAvailable
) {
199 RecoveryCapsuleSize
= 0;
200 Status
= DeviceRecoveryModule
->GetRecoveryCapsuleInfo (
202 DeviceRecoveryModule
,
204 &RecoveryCapsuleSize
,
208 if (EFI_ERROR (Status
)) {
212 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Recovery Capsule Size: %d\n", RecoveryCapsuleSize
));
215 // Only support the 2 capsule types known
216 // Future enhancement is to rank-order the selection
218 if ((!CompareGuid (&DeviceId
, &gPeiCapsuleOnFatIdeDiskGuid
)) &&
219 (!CompareGuid (&DeviceId
, &gPeiCapsuleOnDataCDGuid
)) &&
220 (!CompareGuid (&DeviceId
, &gPeiCapsuleOnFatUsbDiskGuid
))
222 return EFI_UNSUPPORTED
;
226 Address
= (UINTN
) AllocatePages ((RecoveryCapsuleSize
- 1) / 0x1000 + 1);
229 Buffer
= (UINT8
*) (UINTN
) Address
;
230 Status
= DeviceRecoveryModule
->LoadRecoveryCapsule (
232 DeviceRecoveryModule
,
237 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "LoadRecoveryCapsule Returns: %r\n", Status
));
239 if (Status
== EFI_DEVICE_ERROR
) {
240 AssertMediaDeviceError (PeiServices
);
243 if (EFI_ERROR (Status
)) {
246 ReportLoadCapsuleSuccess (PeiServices
);
250 // Update FV Hob if found
252 Buffer
= (VOID
*)((UINT8
*) Buffer
);
253 Status
= PeiServicesGetHobList ((VOID
**)&Hob
.Raw
);
254 while (!END_OF_HOB_LIST (Hob
)) {
255 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_FV
) {
256 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Hob FV Length: %x\n", Hob
.FirmwareVolume
->Length
));
258 if (Hob
.FirmwareVolume
->BaseAddress
== (UINTN
) PcdGet32 (PcdFlashFvMainBase
)) {
261 // This looks like the Hob we are interested in
263 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Hob Updated\n"));
264 Hob
.FirmwareVolume
->BaseAddress
= (UINTN
) Buffer
;
265 Hob
.FirmwareVolume
->Length
= RecoveryCapsuleSize
;
269 Hob
.Raw
= GET_NEXT_HOB (Hob
);
272 // Check if the top of the file is a firmware volume header
274 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) Buffer
;
275 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) Buffer
;
276 if (FvHeader
->Signature
== EFI_FVH_SIGNATURE
) {
278 // build FV Hob if it is not built before
281 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "FV Hob is not found, Build FV Hob then..\n"));
287 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Install FV Info PPI..\n"));
289 PeiServicesInstallFvInfoPpi (
292 (UINT32
) FvHeader
->FvLength
,
298 // Point to the location immediately after the FV.
300 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) ((UINT8
*) Buffer
+ FvHeader
->FvLength
);
304 // Check if pointer is still within the buffer
306 if ((UINTN
) CapsuleHeader
< (UINTN
) ((UINT8
*) Buffer
+ RecoveryCapsuleSize
)) {
309 // Check if it is a capsule
311 if (CompareGuid ((EFI_GUID
*) CapsuleHeader
, &mEfiCapsuleHeaderGuid
)) {
314 // Set bootmode to capsule update so the capsule hob gets the right bootmode in the hob header.
316 Status
= PeiServicesSetBootMode (BOOT_ON_FLASH_UPDATE
);
317 if (EFI_ERROR (Status
)) {
324 BuildCvHob ((EFI_PHYSICAL_ADDRESS
)(UINTN
)CapsuleHeader
, (UINT64
)CapsuleHeader
->CapsuleImageSize
);
329 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Recovery Module Returning: %r\n", Status
));
334 AssertNoCapsulesError:
335 There were no recovery capsules found.
336 Case 1: Report the error that no recovery block io device/media is readable and assert.
337 Case 2: Report the error that there is no media present on any recovery block io device and assert.
338 Case 3: There is media present on some recovery block io device,
339 but there is no recovery capsule on it. Report the error and assert.
342 AssertNoCapsulesError (
343 IN EFI_PEI_SERVICES
**PeiServices
346 BOOLEAN MediaDeviceError
;
347 BOOLEAN MediaPresent
;
349 MediaDeviceError
= TRUE
;
350 MediaPresent
= FALSE
;
352 CheckIfMediaPresentOnBlockIoDevice (PeiServices
, &MediaDeviceError
, &MediaPresent
);
353 /* if (MediaDeviceError) {
355 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
356 (EFI_PERIPHERAL_RECOVERY_MEDIA | EFI_P_EC_MEDIA_DEVICE_ERROR)
359 } else if (!MediaPresent) {
361 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
362 (EFI_PERIPHERAL_RECOVERY_MEDIA | EFI_P_EC_MEDIA_NOT_PRESENT)
367 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
368 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEIM_EC_NO_RECOVERY_CAPSULE)
377 #define MAX_BLOCK_IO_PPI 32
380 CheckIfMediaPresentOnBlockIoDevice:
381 Checks to see whether there was a media device error or to see if there is media present.
384 CheckIfMediaPresentOnBlockIoDevice (
385 IN EFI_PEI_SERVICES
**PeiServices
,
386 IN OUT BOOLEAN
*MediaDeviceError
,
387 IN OUT BOOLEAN
*MediaPresent
391 UINTN BlockIoPpiInstance
;
392 EFI_PEI_RECOVERY_BLOCK_IO_PPI
*BlockIoPpi
;
393 UINTN NumberBlockDevices
;
394 EFI_PEI_BLOCK_IO_MEDIA Media
;
396 *MediaDeviceError
= TRUE
;
397 *MediaPresent
= FALSE
;
399 for (BlockIoPpiInstance
= 0; BlockIoPpiInstance
< MAX_BLOCK_IO_PPI
; BlockIoPpiInstance
++) {
400 Status
= PeiServicesLocatePpi (
401 &gEfiPeiVirtualBlockIoPpiGuid
,
406 if (EFI_ERROR (Status
)) {
408 // Done with all Block Io Ppis
413 Status
= BlockIoPpi
->GetNumberOfBlockDevices (
418 if (EFI_ERROR (Status
) || (NumberBlockDevices
== 0)) {
422 // Just retrieve the first block
424 Status
= BlockIoPpi
->GetBlockDeviceMediaInfo (
430 if (!EFI_ERROR (Status
)) {
431 *MediaDeviceError
= FALSE
;
432 if (Media
.MediaPresent
) {
433 *MediaPresent
= TRUE
;
441 AssertMediaDeviceError (
442 IN EFI_PEI_SERVICES
**PeiServices
445 /* ReportStatusCode (
446 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
447 (EFI_PERIPHERAL_RECOVERY_MEDIA | EFI_P_EC_MEDIA_DEVICE_ERROR)
454 ReportLoadCapsuleSuccess (
455 IN EFI_PEI_SERVICES
**PeiServices
459 // EFI_SW_PEI_PC_CAPSULE_START: (from the status code spec):
460 // Loaded the recovery capsule. About to hand off control to the capsule.
462 /* ReportStatusCode (
464 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEIM_PC_CAPSULE_LOAD_SUCCESS)