2 Install Platform EFI_PEI_RECOVERY_MODULE_PPI and Implementation of
3 EFI_PEI_LOAD_RECOVERY_CAPSULE service.
5 Copyright (c) 2013-2016 Intel Corporation.
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "CommonHeader.h"
18 #include "PlatformEarlyInit.h"
20 #include <Ppi/BlockIo.h>
23 // Capsule Types supported in this platform module
25 #include <Guid/CapsuleOnFatFloppyDisk.h>
26 #include <Guid/CapsuleOnFatIdeDisk.h>
27 #include <Guid/CapsuleOnFatUsbDisk.h>
28 #include <Guid/CapsuleOnDataCD.h>
29 #include <Guid/QuarkCapsuleGuid.h>
31 #include <Ppi/RecoveryModule.h>
32 #include <Ppi/DeviceRecoveryModule.h>
34 #include <Library/PeiServicesLib.h>
41 PlatformRecoveryModule (
42 IN EFI_PEI_SERVICES
**PeiServices
,
43 IN EFI_PEI_RECOVERY_MODULE_PPI
*This
47 AssertNoCapsulesError (
48 IN EFI_PEI_SERVICES
**PeiServices
52 AssertMediaDeviceError (
53 IN EFI_PEI_SERVICES
**PeiServices
57 ReportLoadCapsuleSuccess (
58 IN EFI_PEI_SERVICES
**PeiServices
62 CheckIfMediaPresentOnBlockIoDevice (
63 IN EFI_PEI_SERVICES
**PeiServices
,
64 IN OUT BOOLEAN
*MediaDeviceError
,
65 IN OUT BOOLEAN
*MediaPresent
71 EFI_PEI_RECOVERY_MODULE_PPI mRecoveryPpi
= { PlatformRecoveryModule
};
73 EFI_PEI_PPI_DESCRIPTOR mRecoveryPpiList
= {
74 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
75 &gEfiPeiRecoveryModulePpiGuid
,
81 PeimInitializeRecovery (
82 IN EFI_PEI_SERVICES
**PeiServices
88 Provide the functionality of the Recovery Module.
92 PeiServices - General purpose services available to every PEIM.
96 EFI_SUCCESS - If the interface could be successfully
103 Status
= PeiServicesInstallPpi (&mRecoveryPpiList
);
110 PlatformRecoveryModule (
111 IN EFI_PEI_SERVICES
**PeiServices
,
112 IN EFI_PEI_RECOVERY_MODULE_PPI
*This
118 Provide the functionality of the Platform Recovery Module.
122 PeiServices - General purpose services available to every PEIM.
123 This - Pointer to EFI_PEI_RECOVERY_MODULE_PPI.
127 EFI_SUCCESS - If the interface could be successfully
129 EFI_UNSUPPORTED - Not supported.
134 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*DeviceRecoveryModule
;
135 UINTN NumberOfImageProviders
;
136 BOOLEAN ProviderAvailable
;
137 UINTN NumberRecoveryCapsules
;
138 UINTN RecoveryCapsuleSize
;
140 EFI_PHYSICAL_ADDRESS Address
;
142 EFI_CAPSULE_HEADER
*CapsuleHeader
;
143 EFI_PEI_HOB_POINTERS Hob
;
145 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
147 EFI_GUID mEfiCapsuleHeaderGuid
= QUARK_CAPSULE_GUID
;
151 Status
= EFI_SUCCESS
;
154 ProviderAvailable
= TRUE
;
155 NumberOfImageProviders
= 0;
157 DeviceRecoveryModule
= NULL
;
159 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Recovery Entry\n"));
162 // Search the platform for some recovery capsule if the DXE IPL
163 // discovered a recovery condition and has requested a load.
165 while (ProviderAvailable
) {
167 Status
= PeiServicesLocatePpi (
168 &gEfiPeiDeviceRecoveryModulePpiGuid
,
171 (VOID
**)&DeviceRecoveryModule
174 if (!EFI_ERROR (Status
)) {
175 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Device Recovery PPI located\n"));
176 NumberOfImageProviders
++;
178 Status
= DeviceRecoveryModule
->GetNumberRecoveryCapsules (
180 DeviceRecoveryModule
,
181 &NumberRecoveryCapsules
184 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Number Of Recovery Capsules: %d\n", NumberRecoveryCapsules
));
186 if (NumberRecoveryCapsules
== 0) {
192 ProviderAvailable
= FALSE
;
196 // The number of recovery capsules is 0.
198 if (!ProviderAvailable
) {
199 AssertNoCapsulesError (PeiServices
);
202 // If there is an image provider, get the capsule ID
204 if (ProviderAvailable
) {
205 RecoveryCapsuleSize
= 0;
206 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport
)) {
207 Status
= DeviceRecoveryModule
->GetRecoveryCapsuleInfo (
209 DeviceRecoveryModule
,
211 &RecoveryCapsuleSize
,
215 Status
= DeviceRecoveryModule
->GetRecoveryCapsuleInfo (
217 DeviceRecoveryModule
,
219 &RecoveryCapsuleSize
,
226 if (EFI_ERROR (Status
)) {
230 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Recovery Capsule Size: %d\n", RecoveryCapsuleSize
));
233 // Only support the 2 capsule types known
234 // Future enhancement is to rank-order the selection
236 if ((!CompareGuid (&DeviceId
, &gPeiCapsuleOnFatIdeDiskGuid
)) &&
237 (!CompareGuid (&DeviceId
, &gPeiCapsuleOnDataCDGuid
)) &&
238 (!CompareGuid (&DeviceId
, &gPeiCapsuleOnFatUsbDiskGuid
))
240 return EFI_UNSUPPORTED
;
244 Address
= (UINTN
) AllocatePages ((RecoveryCapsuleSize
- 1) / 0x1000 + 1);
247 Buffer
= (UINT8
*) (UINTN
) Address
;
248 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport
)) {
249 Status
= DeviceRecoveryModule
->LoadRecoveryCapsule (
251 DeviceRecoveryModule
,
256 Status
= DeviceRecoveryModule
->LoadRecoveryCapsule (
258 DeviceRecoveryModule
,
265 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "LoadRecoveryCapsule Returns: %r\n", Status
));
267 if (Status
== EFI_DEVICE_ERROR
) {
268 AssertMediaDeviceError (PeiServices
);
271 if (EFI_ERROR (Status
)) {
274 ReportLoadCapsuleSuccess (PeiServices
);
278 // Update FV Hob if found
280 Buffer
= (VOID
*)((UINT8
*) Buffer
);
281 Status
= PeiServicesGetHobList ((VOID
**)&Hob
.Raw
);
282 while (!END_OF_HOB_LIST (Hob
)) {
283 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_FV
) {
284 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Hob FV Length: %x\n", Hob
.FirmwareVolume
->Length
));
286 if (Hob
.FirmwareVolume
->BaseAddress
== (UINTN
) PcdGet32 (PcdFlashFvMainBase
)) {
289 // This looks like the Hob we are interested in
291 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Hob Updated\n"));
292 Hob
.FirmwareVolume
->BaseAddress
= (UINTN
) Buffer
;
293 Hob
.FirmwareVolume
->Length
= RecoveryCapsuleSize
;
297 Hob
.Raw
= GET_NEXT_HOB (Hob
);
300 // Check if the top of the file is a firmware volume header
302 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) Buffer
;
303 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) Buffer
;
304 if (FvHeader
->Signature
== EFI_FVH_SIGNATURE
) {
306 // build FV Hob if it is not built before
309 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "FV Hob is not found, Build FV Hob then..\n"));
315 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Install FV Info PPI..\n"));
317 PeiServicesInstallFvInfoPpi (
320 (UINT32
) FvHeader
->FvLength
,
326 // Point to the location immediately after the FV.
328 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) ((UINT8
*) Buffer
+ FvHeader
->FvLength
);
332 // Check if pointer is still within the buffer
334 if ((UINTN
) CapsuleHeader
< (UINTN
) ((UINT8
*) Buffer
+ RecoveryCapsuleSize
)) {
337 // Check if it is a capsule
339 if (CompareGuid ((EFI_GUID
*) CapsuleHeader
, &mEfiCapsuleHeaderGuid
)) {
342 // Set bootmode to capsule update so the capsule hob gets the right bootmode in the hob header.
344 Status
= PeiServicesSetBootMode (BOOT_ON_FLASH_UPDATE
);
345 if (EFI_ERROR (Status
)) {
352 BuildCvHob ((EFI_PHYSICAL_ADDRESS
)(UINTN
)CapsuleHeader
, (UINT64
)CapsuleHeader
->CapsuleImageSize
);
357 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Recovery Module Returning: %r\n", Status
));
362 AssertNoCapsulesError:
363 There were no recovery capsules found.
364 Case 1: Report the error that no recovery block io device/media is readable and assert.
365 Case 2: Report the error that there is no media present on any recovery block io device and assert.
366 Case 3: There is media present on some recovery block io device,
367 but there is no recovery capsule on it. Report the error and assert.
370 AssertNoCapsulesError (
371 IN EFI_PEI_SERVICES
**PeiServices
374 BOOLEAN MediaDeviceError
;
375 BOOLEAN MediaPresent
;
377 MediaDeviceError
= TRUE
;
378 MediaPresent
= FALSE
;
380 CheckIfMediaPresentOnBlockIoDevice (PeiServices
, &MediaDeviceError
, &MediaPresent
);
381 /* if (MediaDeviceError) {
383 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
384 (EFI_PERIPHERAL_RECOVERY_MEDIA | EFI_P_EC_MEDIA_DEVICE_ERROR)
387 } else if (!MediaPresent) {
389 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
390 (EFI_PERIPHERAL_RECOVERY_MEDIA | EFI_P_EC_MEDIA_NOT_PRESENT)
395 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
396 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEIM_EC_NO_RECOVERY_CAPSULE)
405 #define MAX_BLOCK_IO_PPI 32
408 CheckIfMediaPresentOnBlockIoDevice:
409 Checks to see whether there was a media device error or to see if there is media present.
412 CheckIfMediaPresentOnBlockIoDevice (
413 IN EFI_PEI_SERVICES
**PeiServices
,
414 IN OUT BOOLEAN
*MediaDeviceError
,
415 IN OUT BOOLEAN
*MediaPresent
419 UINTN BlockIoPpiInstance
;
420 EFI_PEI_RECOVERY_BLOCK_IO_PPI
*BlockIoPpi
;
421 UINTN NumberBlockDevices
;
422 EFI_PEI_BLOCK_IO_MEDIA Media
;
424 *MediaDeviceError
= TRUE
;
425 *MediaPresent
= FALSE
;
427 for (BlockIoPpiInstance
= 0; BlockIoPpiInstance
< MAX_BLOCK_IO_PPI
; BlockIoPpiInstance
++) {
428 Status
= PeiServicesLocatePpi (
429 &gEfiPeiVirtualBlockIoPpiGuid
,
434 if (EFI_ERROR (Status
)) {
436 // Done with all Block Io Ppis
441 Status
= BlockIoPpi
->GetNumberOfBlockDevices (
446 if (EFI_ERROR (Status
) || (NumberBlockDevices
== 0)) {
450 // Just retrieve the first block
452 Status
= BlockIoPpi
->GetBlockDeviceMediaInfo (
458 if (!EFI_ERROR (Status
)) {
459 *MediaDeviceError
= FALSE
;
460 if (Media
.MediaPresent
) {
461 *MediaPresent
= TRUE
;
469 AssertMediaDeviceError (
470 IN EFI_PEI_SERVICES
**PeiServices
473 /* ReportStatusCode (
474 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
475 (EFI_PERIPHERAL_RECOVERY_MEDIA | EFI_P_EC_MEDIA_DEVICE_ERROR)
482 ReportLoadCapsuleSuccess (
483 IN EFI_PEI_SERVICES
**PeiServices
487 // EFI_SW_PEI_PC_CAPSULE_START: (from the status code spec):
488 // Loaded the recovery capsule. About to hand off control to the capsule.
490 /* ReportStatusCode (
492 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEIM_PC_CAPSULE_LOAD_SUCCESS)