2 Data structures for FAT recovery PEIM
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include <Guid/RecoveryDevice.h>
16 #include <Ppi/BlockIo.h>
17 #include <Ppi/BlockIo2.h>
18 #include <Ppi/DeviceRecoveryModule.h>
20 #include <Library/DebugLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/PeimEntryPoint.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/PeiServicesTablePointerLib.h>
27 #include <Library/PeiServicesLib.h>
29 #include "FatLiteApi.h"
30 #include "FatLiteFmt.h"
36 #define PEI_FAT_CACHE_SIZE 4
37 #define PEI_FAT_MAX_BLOCK_SIZE 8192
38 #define FAT_MAX_FILE_NAME_LENGTH 128
39 #define PEI_FAT_MAX_BLOCK_DEVICE 64
40 #define PEI_FAT_MAX_BLOCK_IO_PPI 32
41 #define PEI_FAT_MAX_VOLUME 64
43 #define PEI_FAT_MEMORY_PAGE_SIZE 0x1000
57 BOOLEAN PartitionChecked
;
60 // Following fields only valid for logical device
62 CHAR8 PartitionFlag
[8];
67 // Following fields only valid for physical device
69 EFI_PEI_BLOCK_DEVICE_TYPE DevType
;
72 // EFI_PEI_READ_BLOCKS ReadFunc;
74 EFI_PEI_RECOVERY_BLOCK_IO_PPI
*BlockIo
;
75 EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*BlockIo2
;
77 } PEI_FAT_BLOCK_DEVICE
;
80 // the Volume structure
88 CHAR16 VolumeLabel
[FAT_MAX_FILE_NAME_LENGTH
];
93 UINT64 FirstClusterPos
;
96 UINT32 RootDirCluster
;
105 PEI_FAT_VOLUME
*Volume
;
106 CHAR16 FileName
[FAT_MAX_FILE_NAME_LENGTH
];
108 BOOLEAN IsFixedRootDir
;
110 UINT32 StartingCluster
;
112 UINT32 StraightReadAmount
;
113 UINT32 CurrentCluster
;
129 UINT64 Buffer
[PEI_FAT_MAX_BLOCK_SIZE
/ 8];
132 } PEI_FAT_CACHE_BUFFER
;
136 // This structure abstracts the whole memory usage in FAT PEIM.
137 // The entry point routine will get a chunk of memory (by whatever
138 // means) whose size is sizeof(PEI_FAT_PRIVATE_DATA), which is clean
139 // in both 32 and 64 bit environment. The boundary of the memory chunk
140 // should be 64bit aligned.
142 #define PEI_FAT_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('p', 'f', 'a', 't')
147 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI DeviceRecoveryPpi
;
148 EFI_PEI_PPI_DESCRIPTOR PpiDescriptor
;
149 EFI_PEI_NOTIFY_DESCRIPTOR NotifyDescriptor
[2];
151 UINT8 UnicodeCaseMap
[0x300];
156 UINT64 BlockData
[PEI_FAT_MAX_BLOCK_SIZE
/ 8];
157 UINTN BlockDeviceCount
;
158 PEI_FAT_BLOCK_DEVICE BlockDevice
[PEI_FAT_MAX_BLOCK_DEVICE
];
160 PEI_FAT_VOLUME Volume
[PEI_FAT_MAX_VOLUME
];
162 PEI_FAT_CACHE_BUFFER CacheBuffer
[PEI_FAT_CACHE_SIZE
];
164 } PEI_FAT_PRIVATE_DATA
;
166 #define PEI_FAT_PRIVATE_DATA_FROM_THIS(a) \
167 CR (a, PEI_FAT_PRIVATE_DATA, DeviceRecoveryPpi, PEI_FAT_PRIVATE_DATA_SIGNATURE)
170 // Extract INT32 from char array
172 #define UNPACK_INT32(a) \
173 (INT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
176 // Extract UINT32 from char array
178 #define UNPACK_UINT32(a) \
179 (UINT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
187 Finds the recovery file on a FAT volume.
188 This function finds the recovery file named FileName on a specified FAT volume and returns
189 its FileHandle pointer.
191 @param PrivateData Global memory map for accessing global
193 @param VolumeIndex The index of the volume.
194 @param FileName The recovery file name to find.
195 @param Handle The output file handle.
197 @retval EFI_DEVICE_ERROR Some error occurred when operating the FAT
199 @retval EFI_NOT_FOUND The recovery file was not found.
200 @retval EFI_SUCCESS The recovery file was successfully found on the
206 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
207 IN UINTN VolumeIndex
,
209 OUT PEI_FILE_HANDLE
*Handle
214 Returns the number of DXE capsules residing on the device.
215 This function, by whatever mechanism, searches for DXE capsules from the associated device and
216 returns the number and maximum size in bytes of the capsules discovered.Entry 1 is assumed to be
217 the highest load priority and entry N is assumed to be the lowest priority.
219 @param PeiServices General-purpose services that are available to
221 @param This Indicates the
222 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
223 @param NumberRecoveryCapsules Pointer to a caller-allocated UINTN.On output,
224 *NumberRecoveryCapsules contains the number of
225 recovery capsule images available for retrieval
226 from this PEIM instance.
228 @retval EFI_SUCCESS The function completed successfully.
233 GetNumberRecoveryCapsules (
234 IN EFI_PEI_SERVICES
**PeiServices
,
235 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
236 OUT UINTN
*NumberRecoveryCapsules
241 Returns the size and type of the requested recovery capsule.
242 This function returns the size and type of the capsule specified by CapsuleInstance.
244 @param PeiServices General-purpose services that are available to
246 @param This Indicates the
247 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
248 @param CapsuleInstance Specifies for which capsule instance to
249 retrieve the information.T his parameter must
250 be between one and the value returned by
251 GetNumberRecoveryCapsules() in
252 NumberRecoveryCapsules.
253 @param Size A pointer to a caller-allocated UINTN in which
254 the size of the requested recovery module is
256 @param CapsuleType A pointer to a caller-allocated EFI_GUID in
257 which the type of the requested recovery
258 capsule is returned.T he semantic meaning of
259 the value returned is defined by the
262 @retval EFI_SUCCESS The capsule type and size were retrieved.
263 @retval EFI_INVALID_PARAMETER The input CapsuleInstance does not match any
264 discovered recovery capsule.
269 GetRecoveryCapsuleInfo (
270 IN EFI_PEI_SERVICES
**PeiServices
,
271 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
272 IN UINTN CapsuleInstance
,
274 OUT EFI_GUID
*CapsuleType
279 Loads a DXE capsule from some media into memory.
281 This function, by whatever mechanism, retrieves a DXE capsule from some device
282 and loads it into memory. Note that the published interface is device neutral.
284 @param[in] PeiServices General-purpose services that are available
286 @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
288 @param[in] CapsuleInstance Specifies which capsule instance to retrieve.
289 @param[out] Buffer Specifies a caller-allocated buffer in which
290 the requested recovery capsule will be returned.
292 @retval EFI_SUCCESS The capsule was loaded correctly.
293 @retval EFI_DEVICE_ERROR A device error occurred.
294 @retval EFI_NOT_FOUND A requested recovery DXE capsule cannot be found.
299 LoadRecoveryCapsule (
300 IN EFI_PEI_SERVICES
**PeiServices
,
301 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
302 IN UINTN CapsuleInstance
,
308 This version is different from the version in Unicode collation
309 protocol in that this version strips off trailing blanks.
310 Converts an 8.3 FAT file name using an OEM character set
311 to a Null-terminated Unicode string.
312 Here does not expand DBCS FAT chars.
314 @param FatSize The size of the string Fat in bytes.
315 @param Fat A pointer to a Null-terminated string that contains
316 an 8.3 file name using an OEM character set.
317 @param Str A pointer to a Null-terminated Unicode string. The
318 string must be allocated in advance to hold FatSize
331 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
333 @param PrivateData Global memory map for accessing global variables
334 @param Str1 First string to perform case insensitive comparison.
335 @param Str2 Second string to perform case insensitive comparison.
340 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
347 Reads a block of data from the block device by calling
348 underlying Block I/O service.
350 @param PrivateData Global memory map for accessing global variables
351 @param BlockDeviceNo The index for the block device number.
352 @param Lba The logic block address to read data from.
353 @param BufferSize The size of data in byte to read.
354 @param Buffer The buffer of the
356 @retval EFI_DEVICE_ERROR The specified block device number exceeds the maximum
358 @retval EFI_DEVICE_ERROR The maximum address has exceeded the maximum address
364 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
365 IN UINTN BlockDeviceNo
,
373 Check if there is a valid FAT in the corresponding Block device
374 of the volume and if yes, fill in the relevant fields for the
375 volume structure. Note there should be a valid Block device number
378 @param PrivateData Global memory map for accessing global
380 @param Volume On input, the BlockDeviceNumber field of the
381 Volume should be a valid value. On successful
382 output, all fields except the VolumeNumber
383 field is initialized.
385 @retval EFI_SUCCESS A FAT is found and the volume structure is
387 @retval EFI_NOT_FOUND There is no FAT on the corresponding device.
388 @retval EFI_DEVICE_ERROR There is something error while accessing device.
393 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
394 IN OUT PEI_FAT_VOLUME
*Volume
399 Gets the next cluster in the cluster chain.
401 @param PrivateData Global memory map for accessing global variables
402 @param Volume The volume
403 @param Cluster The cluster
404 @param NextCluster The cluster number of the next cluster
406 @retval EFI_SUCCESS The address is got
407 @retval EFI_INVALID_PARAMETER ClusterNo exceeds the MaxCluster of the volume.
408 @retval EFI_DEVICE_ERROR Read disk error
413 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
414 IN PEI_FAT_VOLUME
*Volume
,
416 OUT UINT32
*NextCluster
423 @param PrivateData the global memory map;
424 @param BlockDeviceNo the block device to read;
425 @param StartingAddress the starting address.
426 @param Size the amount of data to read.
427 @param Buffer the buffer holding the data
429 @retval EFI_SUCCESS The function completed successfully.
430 @retval EFI_DEVICE_ERROR Something error.
435 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
436 IN UINTN BlockDeviceNo
,
437 IN UINT64 StartingAddress
,
444 Set a file's CurrentPos and CurrentCluster, then compute StraightReadAmount.
446 @param PrivateData the global memory map
448 @param Pos the Position which is offset from the file's
451 @retval EFI_SUCCESS Success.
452 @retval EFI_INVALID_PARAMETER Pos is beyond file's size.
453 @retval EFI_DEVICE_ERROR Something error while accessing media.
458 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
459 IN PEI_FAT_FILE
*File
,
465 Reads file data. Updates the file's CurrentPos.
467 @param PrivateData Global memory map for accessing global variables
468 @param File The file.
469 @param Size The amount of data to read.
470 @param Buffer The buffer storing the data.
472 @retval EFI_SUCCESS The data is read.
473 @retval EFI_INVALID_PARAMETER File is invalid.
474 @retval EFI_DEVICE_ERROR Something error while accessing media.
479 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
480 IN PEI_FAT_FILE
*File
,
487 This function reads the next item in the parent directory and
488 initializes the output parameter SubFile (CurrentPos is initialized to 0).
489 The function updates the CurrentPos of the parent dir to after the item read.
490 If no more items were found, the function returns EFI_NOT_FOUND.
492 @param PrivateData Global memory map for accessing global variables
493 @param ParentDir The parent directory.
494 @param SubFile The File structure containing the sub file that
497 @retval EFI_SUCCESS The next sub file is obtained.
498 @retval EFI_INVALID_PARAMETER The ParentDir is not a directory.
499 @retval EFI_NOT_FOUND No more sub file exists.
500 @retval EFI_DEVICE_ERROR Something error while accessing media.
504 FatReadNextDirectoryEntry (
505 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
506 IN PEI_FAT_FILE
*ParentDir
,
507 OUT PEI_FAT_FILE
*SubFile
512 This function finds partitions (logical devices) in physical block devices.
514 @param PrivateData Global memory map for accessing global variables.
519 IN PEI_FAT_PRIVATE_DATA
*PrivateData
522 #endif // _FAT_PEIM_H_