2 FAT recovery PEIM entry point, Ppi Functions and FAT Api functions.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "FatLitePeim.h"
18 PEI_FAT_PRIVATE_DATA
*mPrivateData
= NULL
;
21 BlockIo installation nofication function. Find out all the current BlockIO
22 PPIs in the system and add them into private data. Assume there is
24 @param PeiServices General purpose services available to every
26 @param NotifyDescriptor The typedef structure of the notification
27 descriptor. Not used in this function.
28 @param Ppi The typedef structure of the PPI descriptor.
29 Not used in this function.
31 @retval EFI_SUCCESS The function completed successfully.
37 IN EFI_PEI_SERVICES
**PeiServices
,
38 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
44 Discover all the block I/O devices to find the FAT volume.
46 @param PrivateData Global memory map for accessing global
49 @retval EFI_SUCCESS The function completed successfully.
53 UpdateBlocksAndVolumes (
54 IN OUT PEI_FAT_PRIVATE_DATA
*PrivateData
58 EFI_PEI_PPI_DESCRIPTOR
*TempPpiDescriptor
;
59 UINTN BlockIoPpiInstance
;
60 EFI_PEI_RECOVERY_BLOCK_IO_PPI
*BlockIoPpi
;
61 UINTN NumberBlockDevices
;
63 EFI_PEI_BLOCK_IO_MEDIA Media
;
64 PEI_FAT_VOLUME Volume
;
65 EFI_PEI_SERVICES
**PeiServices
;
67 PeiServices
= (EFI_PEI_SERVICES
**) GetPeiServicesTablePointer ();
72 for (Index
= 0; Index
< PEI_FAT_CACHE_SIZE
; Index
++) {
73 PrivateData
->CacheBuffer
[Index
].Valid
= FALSE
;
76 PrivateData
->BlockDeviceCount
= 0;
79 // Find out all Block Io Ppi instances within the system
80 // Assuming all device Block Io Peims are dispatched already
82 for (BlockIoPpiInstance
= 0; BlockIoPpiInstance
< PEI_FAT_MAX_BLOCK_IO_PPI
; BlockIoPpiInstance
++) {
83 Status
= PeiServicesLocatePpi (
84 &gEfiPeiVirtualBlockIoPpiGuid
,
89 if (EFI_ERROR (Status
)) {
91 // Done with all Block Io Ppis
96 Status
= BlockIoPpi
->GetNumberOfBlockDevices (
101 if (EFI_ERROR (Status
)) {
105 for (Index
= 1; Index
<= NumberBlockDevices
&& PrivateData
->BlockDeviceCount
< PEI_FAT_MAX_BLOCK_DEVICE
; Index
++) {
107 Status
= BlockIoPpi
->GetBlockDeviceMediaInfo (
113 if (EFI_ERROR (Status
) || !Media
.MediaPresent
) {
117 PrivateData
->BlockDevice
[PrivateData
->BlockDeviceCount
].BlockSize
= (UINT32
) Media
.BlockSize
;
118 PrivateData
->BlockDevice
[PrivateData
->BlockDeviceCount
].LastBlock
= Media
.LastBlock
;
119 PrivateData
->BlockDevice
[PrivateData
->BlockDeviceCount
].IoAlign
= 0;
123 PrivateData
->BlockDevice
[PrivateData
->BlockDeviceCount
].Logical
= FALSE
;
124 PrivateData
->BlockDevice
[PrivateData
->BlockDeviceCount
].PartitionChecked
= FALSE
;
126 PrivateData
->BlockDevice
[PrivateData
->BlockDeviceCount
].BlockIo
= BlockIoPpi
;
127 PrivateData
->BlockDevice
[PrivateData
->BlockDeviceCount
].PhysicalDevNo
= (UINT8
) Index
;
128 PrivateData
->BlockDevice
[PrivateData
->BlockDeviceCount
].DevType
= Media
.DeviceType
;
130 PrivateData
->BlockDeviceCount
++;
134 // Find out all logical devices
136 FatFindPartitions (PrivateData
);
139 // Build up file system volume array
141 PrivateData
->VolumeCount
= 0;
142 for (Index
= 0; Index
< PrivateData
->BlockDeviceCount
; Index
++) {
143 Volume
.BlockDeviceNo
= Index
;
144 Status
= FatGetBpbInfo (PrivateData
, &Volume
);
145 if (Status
== EFI_SUCCESS
) {
147 // Add the detected volume to the volume array
150 (UINT8
*) &(PrivateData
->Volume
[PrivateData
->VolumeCount
]),
152 sizeof (PEI_FAT_VOLUME
)
154 PrivateData
->VolumeCount
+= 1;
155 if (PrivateData
->VolumeCount
>= PEI_FAT_MAX_VOLUME
) {
166 BlockIo installation notification function. Find out all the current BlockIO
167 PPIs in the system and add them into private data. Assume there is
169 @param PeiServices General purpose services available to every
171 @param NotifyDescriptor The typedef structure of the notification
172 descriptor. Not used in this function.
173 @param Ppi The typedef structure of the PPI descriptor.
174 Not used in this function.
176 @retval EFI_SUCCESS The function completed successfully.
182 IN EFI_PEI_SERVICES
**PeiServices
,
183 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
187 UpdateBlocksAndVolumes (mPrivateData
);
194 Installs the Device Recovery Module PPI, Initialize BlockIo Ppi
195 installation notification
197 @param FileHandle Handle of the file being invoked. Type
198 EFI_PEI_FILE_HANDLE is defined in
200 @param PeiServices Describes the list of possible PEI Services.
202 @retval EFI_SUCCESS The entry point was executed successfully.
203 @retval EFI_OUT_OF_RESOURCES There is no enough memory to complete the
210 IN EFI_PEI_FILE_HANDLE FileHandle
,
211 IN CONST EFI_PEI_SERVICES
**PeiServices
215 EFI_PHYSICAL_ADDRESS Address
;
216 PEI_FAT_PRIVATE_DATA
*PrivateData
;
218 Status
= PeiServicesRegisterForShadow (FileHandle
);
219 if (!EFI_ERROR (Status
)) {
223 Status
= PeiServicesAllocatePages (
225 (sizeof (PEI_FAT_PRIVATE_DATA
) - 1) / PEI_FAT_MEMMORY_PAGE_SIZE
+ 1,
228 if (EFI_ERROR (Status
)) {
229 return EFI_OUT_OF_RESOURCES
;
232 PrivateData
= (PEI_FAT_PRIVATE_DATA
*) (UINTN
) Address
;
235 // Initialize Private Data (to zero, as is required by subsequent operations)
237 ZeroMem ((UINT8
*) PrivateData
, sizeof (PEI_FAT_PRIVATE_DATA
));
239 PrivateData
->Signature
= PEI_FAT_PRIVATE_DATA_SIGNATURE
;
244 PrivateData
->DeviceRecoveryPpi
.GetNumberRecoveryCapsules
= GetNumberRecoveryCapsules
;
245 PrivateData
->DeviceRecoveryPpi
.GetRecoveryCapsuleInfo
= GetRecoveryCapsuleInfo
;
246 PrivateData
->DeviceRecoveryPpi
.LoadRecoveryCapsule
= LoadRecoveryCapsule
;
248 PrivateData
->PpiDescriptor
.Flags
= (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
);
249 PrivateData
->PpiDescriptor
.Guid
= &gEfiPeiDeviceRecoveryModulePpiGuid
;
250 PrivateData
->PpiDescriptor
.Ppi
= &PrivateData
->DeviceRecoveryPpi
;
252 Status
= PeiServicesInstallPpi (&PrivateData
->PpiDescriptor
);
253 if (EFI_ERROR (Status
)) {
254 return EFI_OUT_OF_RESOURCES
;
257 // Other initializations
259 PrivateData
->BlockDeviceCount
= 0;
261 UpdateBlocksAndVolumes (PrivateData
);
264 // PrivateData is allocated now, set it to the module variable
266 mPrivateData
= PrivateData
;
269 // Installs Block Io Ppi notification function
271 PrivateData
->NotifyDescriptor
.Flags
=
273 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
|
274 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
276 PrivateData
->NotifyDescriptor
.Guid
= &gEfiPeiVirtualBlockIoPpiGuid
;
277 PrivateData
->NotifyDescriptor
.Notify
= BlockIoNotifyEntry
;
278 return PeiServicesNotifyPpi (&PrivateData
->NotifyDescriptor
);
283 Returns the number of DXE capsules residing on the device.
285 This function searches for DXE capsules from the associated device and returns
286 the number and maximum size in bytes of the capsules discovered. Entry 1 is
287 assumed to be the highest load priority and entry N is assumed to be the lowest
290 @param[in] PeiServices General-purpose services that are available
292 @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
294 @param[out] NumberRecoveryCapsules Pointer to a caller-allocated UINTN. On
295 output, *NumberRecoveryCapsules contains
296 the number of recovery capsule images
297 available for retrieval from this PEIM
300 @retval EFI_SUCCESS One or more capsules were discovered.
301 @retval EFI_DEVICE_ERROR A device error occurred.
302 @retval EFI_NOT_FOUND A recovery DXE capsule cannot be found.
307 GetNumberRecoveryCapsules (
308 IN EFI_PEI_SERVICES
**PeiServices
,
309 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
310 OUT UINTN
*NumberRecoveryCapsules
314 PEI_FAT_PRIVATE_DATA
*PrivateData
;
316 UINTN RecoveryCapsuleCount
;
317 PEI_FILE_HANDLE Handle
;
319 PrivateData
= PEI_FAT_PRIVATE_DATA_FROM_THIS (This
);
322 // Search each volume in the root directory for the Recovery capsule
324 RecoveryCapsuleCount
= 0;
325 for (Index
= 0; Index
< PrivateData
->VolumeCount
; Index
++) {
326 Status
= FindRecoveryFile (PrivateData
, Index
, PEI_FAT_RECOVERY_CAPSULE_WITHOUT_NT_EMULATOR
, &Handle
);
327 if (EFI_ERROR (Status
)) {
331 RecoveryCapsuleCount
++;
334 *NumberRecoveryCapsules
= RecoveryCapsuleCount
;
336 if (*NumberRecoveryCapsules
== 0) {
337 return EFI_NOT_FOUND
;
345 Returns the size and type of the requested recovery capsule.
347 This function gets the size and type of the capsule specified by CapsuleInstance.
349 @param[in] PeiServices General-purpose services that are available to every PEIM
350 @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
352 @param[in] CapsuleInstance Specifies for which capsule instance to retrieve
353 the information. This parameter must be between
354 one and the value returned by GetNumberRecoveryCapsules()
355 in NumberRecoveryCapsules.
356 @param[out] Size A pointer to a caller-allocated UINTN in which
357 the size of the requested recovery module is
359 @param[out] CapsuleType A pointer to a caller-allocated EFI_GUID in which
360 the type of the requested recovery capsule is
361 returned. The semantic meaning of the value
362 returned is defined by the implementation.
364 @retval EFI_SUCCESS One or more capsules were discovered.
365 @retval EFI_DEVICE_ERROR A device error occurred.
366 @retval EFI_NOT_FOUND A recovery DXE capsule cannot be found.
371 GetRecoveryCapsuleInfo (
372 IN EFI_PEI_SERVICES
**PeiServices
,
373 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
374 IN UINTN CapsuleInstance
,
376 OUT EFI_GUID
*CapsuleType
380 PEI_FAT_PRIVATE_DATA
*PrivateData
;
383 UINTN RecoveryCapsuleCount
;
384 PEI_FILE_HANDLE Handle
;
385 UINTN NumberRecoveryCapsules
;
387 Status
= GetNumberRecoveryCapsules (PeiServices
, This
, &NumberRecoveryCapsules
);
389 if (EFI_ERROR (Status
)) {
393 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport
)) {
394 CapsuleInstance
= CapsuleInstance
+ 1;
397 if ((CapsuleInstance
== 0) || (CapsuleInstance
> NumberRecoveryCapsules
)) {
398 return EFI_NOT_FOUND
;
401 PrivateData
= PEI_FAT_PRIVATE_DATA_FROM_THIS (This
);
404 // Search each volume in the root directory for the Recovery capsule
406 RecoveryCapsuleCount
= 0;
407 for (Index
= 0; Index
< PrivateData
->VolumeCount
; Index
++) {
408 Status
= FindRecoveryFile (PrivateData
, Index
, PEI_FAT_RECOVERY_CAPSULE_WITHOUT_NT_EMULATOR
, &Handle
);
410 if (EFI_ERROR (Status
)) {
414 if (CapsuleInstance
- 1 == RecoveryCapsuleCount
) {
418 *Size
= (UINTN
) (((PEI_FAT_FILE
*) Handle
)->FileSize
);
421 // Find corresponding physical block device
423 BlockDeviceNo
= PrivateData
->Volume
[Index
].BlockDeviceNo
;
424 while (PrivateData
->BlockDevice
[BlockDeviceNo
].Logical
&& BlockDeviceNo
< PrivateData
->BlockDeviceCount
) {
425 BlockDeviceNo
= PrivateData
->BlockDevice
[BlockDeviceNo
].ParentDevNo
;
428 // Fill in the Capsule Type GUID according to the block device type
430 if (BlockDeviceNo
< PrivateData
->BlockDeviceCount
) {
431 switch (PrivateData
->BlockDevice
[BlockDeviceNo
].DevType
) {
433 CopyGuid (CapsuleType
, &gRecoveryOnFatFloppyDiskGuid
);
438 CopyGuid (CapsuleType
, &gRecoveryOnFatIdeDiskGuid
);
442 CopyGuid (CapsuleType
, &gRecoveryOnFatUsbDiskGuid
);
453 RecoveryCapsuleCount
++;
456 return EFI_NOT_FOUND
;
461 Loads a DXE capsule from some media into memory.
463 This function, by whatever mechanism, retrieves a DXE capsule from some device
464 and loads it into memory. Note that the published interface is device neutral.
466 @param[in] PeiServices General-purpose services that are available
468 @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
470 @param[in] CapsuleInstance Specifies which capsule instance to retrieve.
471 @param[out] Buffer Specifies a caller-allocated buffer in which
472 the requested recovery capsule will be returned.
474 @retval EFI_SUCCESS The capsule was loaded correctly.
475 @retval EFI_DEVICE_ERROR A device error occurred.
476 @retval EFI_NOT_FOUND A requested recovery DXE capsule cannot be found.
481 LoadRecoveryCapsule (
482 IN EFI_PEI_SERVICES
**PeiServices
,
483 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
484 IN UINTN CapsuleInstance
,
489 PEI_FAT_PRIVATE_DATA
*PrivateData
;
491 UINTN RecoveryCapsuleCount
;
492 PEI_FILE_HANDLE Handle
;
493 UINTN NumberRecoveryCapsules
;
495 Status
= GetNumberRecoveryCapsules (PeiServices
, This
, &NumberRecoveryCapsules
);
497 if (EFI_ERROR (Status
)) {
501 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport
)) {
502 CapsuleInstance
= CapsuleInstance
+ 1;
505 if ((CapsuleInstance
== 0) || (CapsuleInstance
> NumberRecoveryCapsules
)) {
506 return EFI_NOT_FOUND
;
509 PrivateData
= PEI_FAT_PRIVATE_DATA_FROM_THIS (This
);
512 // Search each volume in the root directory for the Recovery capsule
514 RecoveryCapsuleCount
= 0;
515 for (Index
= 0; Index
< PrivateData
->VolumeCount
; Index
++) {
516 Status
= FindRecoveryFile (PrivateData
, Index
, PEI_FAT_RECOVERY_CAPSULE_WITHOUT_NT_EMULATOR
, &Handle
);
517 if (EFI_ERROR (Status
)) {
521 if (CapsuleInstance
- 1 == RecoveryCapsuleCount
) {
523 Status
= FatReadFile (
526 (UINTN
) (((PEI_FAT_FILE
*) Handle
)->FileSize
),
532 RecoveryCapsuleCount
++;
535 return EFI_NOT_FOUND
;
540 Finds the recovery file on a FAT volume.
541 This function finds the the recovery file named FileName on a specified FAT volume and returns
542 its FileHandle pointer.
544 @param PrivateData Global memory map for accessing global
546 @param VolumeIndex The index of the volume.
547 @param FileName The recovery file name to find.
548 @param Handle The output file handle.
550 @retval EFI_DEVICE_ERROR Some error occured when operating the FAT
552 @retval EFI_NOT_FOUND The recovery file was not found.
553 @retval EFI_SUCCESS The recovery file was successfully found on the
559 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
560 IN UINTN VolumeIndex
,
562 OUT PEI_FILE_HANDLE
*Handle
569 File
= &PrivateData
->File
;
572 // VolumeIndex must be less than PEI_FAT_MAX_VOLUME because PrivateData->VolumeCount
573 // cannot be larger than PEI_FAT_MAX_VOLUME when detecting recovery volume.
575 ASSERT (VolumeIndex
< PEI_FAT_MAX_VOLUME
);
578 // Construct root directory file
580 Parent
.IsFixedRootDir
= (BOOLEAN
) ((PrivateData
->Volume
[VolumeIndex
].FatType
== Fat32
) ? FALSE
: TRUE
);
581 Parent
.Attributes
= FAT_ATTR_DIRECTORY
;
582 Parent
.CurrentPos
= 0;
583 Parent
.CurrentCluster
= Parent
.IsFixedRootDir
? 0 : PrivateData
->Volume
[VolumeIndex
].RootDirCluster
;
584 Parent
.StartingCluster
= Parent
.CurrentCluster
;
585 Parent
.Volume
= &PrivateData
->Volume
[VolumeIndex
];
587 Status
= FatSetFilePos (PrivateData
, &Parent
, 0);
588 if (EFI_ERROR (Status
)) {
589 return EFI_DEVICE_ERROR
;
592 // Search for recovery capsule in root directory
594 Status
= FatReadNextDirectoryEntry (PrivateData
, &Parent
, File
);
595 while (Status
== EFI_SUCCESS
) {
596 if (EngStriColl (PrivateData
, FileName
, File
->FileName
)) {
600 Status
= FatReadNextDirectoryEntry (PrivateData
, &Parent
, File
);
603 if (EFI_ERROR (Status
)) {
604 return EFI_NOT_FOUND
;