]> git.proxmox.com Git - mirror_edk2.git/blob - QuarkPlatformPkg/Platform/Pei/PlatformInit/Generic/Recovery.c
QuarkPlatformPkg: Remove PcdFrameworkCompatibilitySupport usage
[mirror_edk2.git] / QuarkPlatformPkg / Platform / Pei / PlatformInit / Generic / Recovery.c
1 /** @file
2 Install Platform EFI_PEI_RECOVERY_MODULE_PPI and Implementation of
3 EFI_PEI_LOAD_RECOVERY_CAPSULE service.
4
5 Copyright (c) 2013-2019 Intel Corporation.
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "CommonHeader.h"
12 #include "PlatformEarlyInit.h"
13
14 #include <Ppi/BlockIo.h>
15
16 //
17 // Capsule Types supported in this platform module
18 //
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>
24
25 #include <Ppi/RecoveryModule.h>
26 #include <Ppi/DeviceRecoveryModule.h>
27
28 #include <Library/PeiServicesLib.h>
29
30 //
31 // Required Service
32 //
33 EFI_STATUS
34 EFIAPI
35 PlatformRecoveryModule (
36 IN EFI_PEI_SERVICES **PeiServices,
37 IN EFI_PEI_RECOVERY_MODULE_PPI *This
38 );
39
40 VOID
41 AssertNoCapsulesError (
42 IN EFI_PEI_SERVICES **PeiServices
43 );
44
45 VOID
46 AssertMediaDeviceError (
47 IN EFI_PEI_SERVICES **PeiServices
48 );
49
50 VOID
51 ReportLoadCapsuleSuccess (
52 IN EFI_PEI_SERVICES **PeiServices
53 );
54
55 VOID
56 CheckIfMediaPresentOnBlockIoDevice (
57 IN EFI_PEI_SERVICES **PeiServices,
58 IN OUT BOOLEAN *MediaDeviceError,
59 IN OUT BOOLEAN *MediaPresent
60 );
61
62 //
63 // Module globals
64 //
65 EFI_PEI_RECOVERY_MODULE_PPI mRecoveryPpi = { PlatformRecoveryModule };
66
67 EFI_PEI_PPI_DESCRIPTOR mRecoveryPpiList = {
68 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
69 &gEfiPeiRecoveryModulePpiGuid,
70 &mRecoveryPpi
71 };
72
73 EFI_STATUS
74 EFIAPI
75 PeimInitializeRecovery (
76 IN EFI_PEI_SERVICES **PeiServices
77 )
78 /*++
79
80 Routine Description:
81
82 Provide the functionality of the Recovery Module.
83
84 Arguments:
85
86 PeiServices - General purpose services available to every PEIM.
87
88 Returns:
89
90 EFI_SUCCESS - If the interface could be successfully
91 installed.
92
93 --*/
94 {
95 EFI_STATUS Status;
96
97 Status = PeiServicesInstallPpi (&mRecoveryPpiList);
98
99 return Status;
100 }
101
102 EFI_STATUS
103 EFIAPI
104 PlatformRecoveryModule (
105 IN EFI_PEI_SERVICES **PeiServices,
106 IN EFI_PEI_RECOVERY_MODULE_PPI *This
107 )
108 /*++
109
110 Routine Description:
111
112 Provide the functionality of the Platform Recovery Module.
113
114 Arguments:
115
116 PeiServices - General purpose services available to every PEIM.
117 This - Pointer to EFI_PEI_RECOVERY_MODULE_PPI.
118
119 Returns:
120
121 EFI_SUCCESS - If the interface could be successfully
122 installed.
123 EFI_UNSUPPORTED - Not supported.
124
125 --*/
126 {
127 EFI_STATUS Status;
128 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *DeviceRecoveryModule;
129 UINTN NumberOfImageProviders;
130 BOOLEAN ProviderAvailable;
131 UINTN NumberRecoveryCapsules;
132 UINTN RecoveryCapsuleSize;
133 EFI_GUID DeviceId;
134 EFI_PHYSICAL_ADDRESS Address;
135 VOID *Buffer;
136 EFI_CAPSULE_HEADER *CapsuleHeader;
137 EFI_PEI_HOB_POINTERS Hob;
138 BOOLEAN HobUpdate;
139 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
140 UINTN Index;
141 EFI_GUID mEfiCapsuleHeaderGuid = QUARK_CAPSULE_GUID;
142
143 Index = 0;
144
145 Status = EFI_SUCCESS;
146 HobUpdate = FALSE;
147
148 ProviderAvailable = TRUE;
149 NumberOfImageProviders = 0;
150
151 DeviceRecoveryModule = NULL;
152
153 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Recovery Entry\n"));
154
155 //
156 // Search the platform for some recovery capsule if the DXE IPL
157 // discovered a recovery condition and has requested a load.
158 //
159 while (ProviderAvailable) {
160
161 Status = PeiServicesLocatePpi (
162 &gEfiPeiDeviceRecoveryModulePpiGuid,
163 Index,
164 NULL,
165 (VOID **)&DeviceRecoveryModule
166 );
167
168 if (!EFI_ERROR (Status)) {
169 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Device Recovery PPI located\n"));
170 NumberOfImageProviders++;
171
172 Status = DeviceRecoveryModule->GetNumberRecoveryCapsules (
173 PeiServices,
174 DeviceRecoveryModule,
175 &NumberRecoveryCapsules
176 );
177
178 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Number Of Recovery Capsules: %d\n", NumberRecoveryCapsules));
179
180 if (NumberRecoveryCapsules == 0) {
181 Index++;
182 } else {
183 break;
184 }
185 } else {
186 ProviderAvailable = FALSE;
187 }
188 }
189 //
190 // The number of recovery capsules is 0.
191 //
192 if (!ProviderAvailable) {
193 AssertNoCapsulesError (PeiServices);
194 }
195 //
196 // If there is an image provider, get the capsule ID
197 //
198 if (ProviderAvailable) {
199 RecoveryCapsuleSize = 0;
200 Status = DeviceRecoveryModule->GetRecoveryCapsuleInfo (
201 PeiServices,
202 DeviceRecoveryModule,
203 1,
204 &RecoveryCapsuleSize,
205 &DeviceId
206 );
207
208 if (EFI_ERROR (Status)) {
209 return Status;
210 }
211
212 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Recovery Capsule Size: %d\n", RecoveryCapsuleSize));
213
214 //
215 // Only support the 2 capsule types known
216 // Future enhancement is to rank-order the selection
217 //
218 if ((!CompareGuid (&DeviceId, &gPeiCapsuleOnFatIdeDiskGuid)) &&
219 (!CompareGuid (&DeviceId, &gPeiCapsuleOnDataCDGuid)) &&
220 (!CompareGuid (&DeviceId, &gPeiCapsuleOnFatUsbDiskGuid))
221 ) {
222 return EFI_UNSUPPORTED;
223 }
224
225 Buffer = NULL;
226 Address = (UINTN) AllocatePages ((RecoveryCapsuleSize - 1) / 0x1000 + 1);
227 ASSERT (Address);
228
229 Buffer = (UINT8 *) (UINTN) Address;
230 Status = DeviceRecoveryModule->LoadRecoveryCapsule (
231 PeiServices,
232 DeviceRecoveryModule,
233 1,
234 Buffer
235 );
236
237 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "LoadRecoveryCapsule Returns: %r\n", Status));
238
239 if (Status == EFI_DEVICE_ERROR) {
240 AssertMediaDeviceError (PeiServices);
241 }
242
243 if (EFI_ERROR (Status)) {
244 return Status;
245 } else {
246 ReportLoadCapsuleSuccess (PeiServices);
247 }
248
249 //
250 // Update FV Hob if found
251 //
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));
257
258 if (Hob.FirmwareVolume->BaseAddress == (UINTN) PcdGet32 (PcdFlashFvMainBase)) {
259 HobUpdate = TRUE;
260 //
261 // This looks like the Hob we are interested in
262 //
263 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Hob Updated\n"));
264 Hob.FirmwareVolume->BaseAddress = (UINTN) Buffer;
265 Hob.FirmwareVolume->Length = RecoveryCapsuleSize;
266 }
267 }
268
269 Hob.Raw = GET_NEXT_HOB (Hob);
270 }
271 //
272 // Check if the top of the file is a firmware volume header
273 //
274 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) Buffer;
275 CapsuleHeader = (EFI_CAPSULE_HEADER *) Buffer;
276 if (FvHeader->Signature == EFI_FVH_SIGNATURE) {
277 //
278 // build FV Hob if it is not built before
279 //
280 if (!HobUpdate) {
281 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "FV Hob is not found, Build FV Hob then..\n"));
282 BuildFvHob (
283 (UINTN) Buffer,
284 FvHeader->FvLength
285 );
286
287 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Install FV Info PPI..\n"));
288
289 PeiServicesInstallFvInfoPpi (
290 NULL,
291 Buffer,
292 (UINT32) FvHeader->FvLength,
293 NULL,
294 NULL
295 );
296 }
297 //
298 // Point to the location immediately after the FV.
299 //
300 CapsuleHeader = (EFI_CAPSULE_HEADER *) ((UINT8 *) Buffer + FvHeader->FvLength);
301 }
302
303 //
304 // Check if pointer is still within the buffer
305 //
306 if ((UINTN) CapsuleHeader < (UINTN) ((UINT8 *) Buffer + RecoveryCapsuleSize)) {
307
308 //
309 // Check if it is a capsule
310 //
311 if (CompareGuid ((EFI_GUID *) CapsuleHeader, &mEfiCapsuleHeaderGuid)) {
312
313 //
314 // Set bootmode to capsule update so the capsule hob gets the right bootmode in the hob header.
315 //
316 Status = PeiServicesSetBootMode (BOOT_ON_FLASH_UPDATE);
317 if (EFI_ERROR (Status)) {
318 return Status;
319 }
320
321 //
322 // Build capsule hob
323 //
324 BuildCvHob ((EFI_PHYSICAL_ADDRESS)(UINTN)CapsuleHeader, (UINT64)CapsuleHeader->CapsuleImageSize);
325 }
326 }
327 }
328
329 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Recovery Module Returning: %r\n", Status));
330 return Status;
331 }
332
333 /*
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.
340 */
341 VOID
342 AssertNoCapsulesError (
343 IN EFI_PEI_SERVICES **PeiServices
344 )
345 {
346 BOOLEAN MediaDeviceError;
347 BOOLEAN MediaPresent;
348
349 MediaDeviceError = TRUE;
350 MediaPresent = FALSE;
351
352 CheckIfMediaPresentOnBlockIoDevice (PeiServices, &MediaDeviceError, &MediaPresent);
353 /* if (MediaDeviceError) {
354 ReportStatusCode (
355 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
356 (EFI_PERIPHERAL_RECOVERY_MEDIA | EFI_P_EC_MEDIA_DEVICE_ERROR)
357 );
358
359 } else if (!MediaPresent) {
360 ReportStatusCode (
361 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
362 (EFI_PERIPHERAL_RECOVERY_MEDIA | EFI_P_EC_MEDIA_NOT_PRESENT)
363 );
364
365 } else {
366 ReportStatusCode (
367 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
368 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEIM_EC_NO_RECOVERY_CAPSULE)
369 );
370 }*/
371 //
372 // Hang.
373 //
374 CpuDeadLoop();
375 }
376
377 #define MAX_BLOCK_IO_PPI 32
378
379 /*
380 CheckIfMediaPresentOnBlockIoDevice:
381 Checks to see whether there was a media device error or to see if there is media present.
382 */
383 VOID
384 CheckIfMediaPresentOnBlockIoDevice (
385 IN EFI_PEI_SERVICES **PeiServices,
386 IN OUT BOOLEAN *MediaDeviceError,
387 IN OUT BOOLEAN *MediaPresent
388 )
389 {
390 EFI_STATUS Status;
391 UINTN BlockIoPpiInstance;
392 EFI_PEI_RECOVERY_BLOCK_IO_PPI *BlockIoPpi;
393 UINTN NumberBlockDevices;
394 EFI_PEI_BLOCK_IO_MEDIA Media;
395
396 *MediaDeviceError = TRUE;
397 *MediaPresent = FALSE;
398
399 for (BlockIoPpiInstance = 0; BlockIoPpiInstance < MAX_BLOCK_IO_PPI; BlockIoPpiInstance++) {
400 Status = PeiServicesLocatePpi (
401 &gEfiPeiVirtualBlockIoPpiGuid,
402 BlockIoPpiInstance,
403 NULL,
404 (VOID **)&BlockIoPpi
405 );
406 if (EFI_ERROR (Status)) {
407 //
408 // Done with all Block Io Ppis
409 //
410 break;
411 }
412
413 Status = BlockIoPpi->GetNumberOfBlockDevices (
414 PeiServices,
415 BlockIoPpi,
416 &NumberBlockDevices
417 );
418 if (EFI_ERROR (Status) || (NumberBlockDevices == 0)) {
419 continue;
420 }
421 //
422 // Just retrieve the first block
423 //
424 Status = BlockIoPpi->GetBlockDeviceMediaInfo (
425 PeiServices,
426 BlockIoPpi,
427 0,
428 &Media
429 );
430 if (!EFI_ERROR (Status)) {
431 *MediaDeviceError = FALSE;
432 if (Media.MediaPresent) {
433 *MediaPresent = TRUE;
434 break;
435 }
436 }
437 }
438 }
439
440 VOID
441 AssertMediaDeviceError (
442 IN EFI_PEI_SERVICES **PeiServices
443 )
444 {
445 /* ReportStatusCode (
446 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
447 (EFI_PERIPHERAL_RECOVERY_MEDIA | EFI_P_EC_MEDIA_DEVICE_ERROR)
448 );
449 */
450 CpuDeadLoop ();
451 }
452
453 VOID
454 ReportLoadCapsuleSuccess (
455 IN EFI_PEI_SERVICES **PeiServices
456 )
457 {
458 //
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.
461 //
462 /* ReportStatusCode (
463 EFI_PROGRESS_CODE,
464 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEIM_PC_CAPSULE_LOAD_SUCCESS)
465 );*/
466 }
467