2 Data structures for FAT recovery PEIM
4 Copyright (c) 2006 - 2019, 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.
21 #include <Guid/RecoveryDevice.h>
22 #include <Ppi/BlockIo.h>
23 #include <Ppi/BlockIo2.h>
24 #include <Ppi/DeviceRecoveryModule.h>
26 #include <Library/DebugLib.h>
27 #include <Library/BaseLib.h>
28 #include <Library/PeimEntryPoint.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/MemoryAllocationLib.h>
31 #include <Library/PcdLib.h>
32 #include <Library/PeiServicesTablePointerLib.h>
33 #include <Library/PeiServicesLib.h>
35 #include "FatLiteApi.h"
36 #include "FatLiteFmt.h"
42 #define PEI_FAT_CACHE_SIZE 4
43 #define PEI_FAT_MAX_BLOCK_SIZE 8192
44 #define FAT_MAX_FILE_NAME_LENGTH 128
45 #define PEI_FAT_MAX_BLOCK_DEVICE 64
46 #define PEI_FAT_MAX_BLOCK_IO_PPI 32
47 #define PEI_FAT_MAX_VOLUME 64
49 #define PEI_FAT_MEMMORY_PAGE_SIZE 0x1000
63 BOOLEAN PartitionChecked
;
66 // Following fields only valid for logical device
68 CHAR8 PartitionFlag
[8];
73 // Following fields only valid for physical device
75 EFI_PEI_BLOCK_DEVICE_TYPE DevType
;
78 // EFI_PEI_READ_BLOCKS ReadFunc;
80 EFI_PEI_RECOVERY_BLOCK_IO_PPI
*BlockIo
;
81 EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*BlockIo2
;
83 } PEI_FAT_BLOCK_DEVICE
;
86 // the Volume structure
94 CHAR16 VolumeLabel
[FAT_MAX_FILE_NAME_LENGTH
];
99 UINT64 FirstClusterPos
;
102 UINT32 RootDirCluster
;
111 PEI_FAT_VOLUME
*Volume
;
112 CHAR16 FileName
[FAT_MAX_FILE_NAME_LENGTH
];
114 BOOLEAN IsFixedRootDir
;
116 UINT32 StartingCluster
;
118 UINT32 StraightReadAmount
;
119 UINT32 CurrentCluster
;
135 UINT64 Buffer
[PEI_FAT_MAX_BLOCK_SIZE
/ 8];
138 } PEI_FAT_CACHE_BUFFER
;
142 // This structure abstracts the whole memory usage in FAT PEIM.
143 // The entry point routine will get a chunk of memory (by whatever
144 // means) whose size is sizeof(PEI_FAT_PRIVATE_DATA), which is clean
145 // in both 32 and 64 bit environment. The boundary of the memory chunk
146 // should be 64bit aligned.
148 #define PEI_FAT_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('p', 'f', 'a', 't')
153 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI DeviceRecoveryPpi
;
154 EFI_PEI_PPI_DESCRIPTOR PpiDescriptor
;
155 EFI_PEI_NOTIFY_DESCRIPTOR NotifyDescriptor
[2];
157 UINT8 UnicodeCaseMap
[0x300];
162 UINT64 BlockData
[PEI_FAT_MAX_BLOCK_SIZE
/ 8];
163 UINTN BlockDeviceCount
;
164 PEI_FAT_BLOCK_DEVICE BlockDevice
[PEI_FAT_MAX_BLOCK_DEVICE
];
166 PEI_FAT_VOLUME Volume
[PEI_FAT_MAX_VOLUME
];
168 PEI_FAT_CACHE_BUFFER CacheBuffer
[PEI_FAT_CACHE_SIZE
];
170 } PEI_FAT_PRIVATE_DATA
;
172 #define PEI_FAT_PRIVATE_DATA_FROM_THIS(a) \
173 CR (a, PEI_FAT_PRIVATE_DATA, DeviceRecoveryPpi, PEI_FAT_PRIVATE_DATA_SIGNATURE)
176 // Extract INT32 from char array
178 #define UNPACK_INT32(a) \
179 (INT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
182 // Extract UINT32 from char array
184 #define UNPACK_UINT32(a) \
185 (UINT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
193 Finds the recovery file on a FAT volume.
194 This function finds the the recovery file named FileName on a specified FAT volume and returns
195 its FileHandle pointer.
197 @param PrivateData Global memory map for accessing global
199 @param VolumeIndex The index of the volume.
200 @param FileName The recovery file name to find.
201 @param Handle The output file handle.
203 @retval EFI_DEVICE_ERROR Some error occured when operating the FAT
205 @retval EFI_NOT_FOUND The recovery file was not found.
206 @retval EFI_SUCCESS The recovery file was successfully found on the
212 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
213 IN UINTN VolumeIndex
,
215 OUT PEI_FILE_HANDLE
*Handle
220 Returns the number of DXE capsules residing on the device.
221 This function, by whatever mechanism, searches for DXE capsules from the associated device and
222 returns the number and maximum size in bytes of the capsules discovered.Entry 1 is assumed to be
223 the highest load priority and entry N is assumed to be the lowest priority.
225 @param PeiServices General-purpose services that are available to
227 @param This Indicates the
228 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
229 @param NumberRecoveryCapsules Pointer to a caller-allocated UINTN.On output,
230 *NumberRecoveryCapsules contains the number of
231 recovery capsule images available for retrieval
232 from this PEIM instance.
234 @retval EFI_SUCCESS The function completed successfully.
239 GetNumberRecoveryCapsules (
240 IN EFI_PEI_SERVICES
**PeiServices
,
241 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
242 OUT UINTN
*NumberRecoveryCapsules
247 Returns the size and type of the requested recovery capsule.
248 This function returns the size and type of the capsule specified by CapsuleInstance.
250 @param PeiServices General-purpose services that are available to
252 @param This Indicates the
253 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
254 @param CapsuleInstance Specifies for which capsule instance to
255 retrieve the information.T his parameter must
256 be between one and the value returned by
257 GetNumberRecoveryCapsules() in
258 NumberRecoveryCapsules.
259 @param Size A pointer to a caller-allocated UINTN in which
260 the size of the requested recovery module is
262 @param CapsuleType A pointer to a caller-allocated EFI_GUID in
263 which the type of the requested recovery
264 capsule is returned.T he semantic meaning of
265 the value returned is defined by the
268 @retval EFI_SUCCESS The capsule type and size were retrieved.
269 @retval EFI_INVALID_PARAMETER The input CapsuleInstance does not match any
270 discovered recovery capsule.
275 GetRecoveryCapsuleInfo (
276 IN EFI_PEI_SERVICES
**PeiServices
,
277 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
278 IN UINTN CapsuleInstance
,
280 OUT EFI_GUID
*CapsuleType
285 Loads a DXE capsule from some media into memory.
287 This function, by whatever mechanism, retrieves a DXE capsule from some device
288 and loads it into memory. Note that the published interface is device neutral.
290 @param[in] PeiServices General-purpose services that are available
292 @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
294 @param[in] CapsuleInstance Specifies which capsule instance to retrieve.
295 @param[out] Buffer Specifies a caller-allocated buffer in which
296 the requested recovery capsule will be returned.
298 @retval EFI_SUCCESS The capsule was loaded correctly.
299 @retval EFI_DEVICE_ERROR A device error occurred.
300 @retval EFI_NOT_FOUND A requested recovery DXE capsule cannot be found.
305 LoadRecoveryCapsule (
306 IN EFI_PEI_SERVICES
**PeiServices
,
307 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
308 IN UINTN CapsuleInstance
,
314 This version is different from the version in Unicode collation
315 protocol in that this version strips off trailing blanks.
316 Converts an 8.3 FAT file name using an OEM character set
317 to a Null-terminated Unicode string.
318 Here does not expand DBCS FAT chars.
320 @param FatSize The size of the string Fat in bytes.
321 @param Fat A pointer to a Null-terminated string that contains
322 an 8.3 file name using an OEM character set.
323 @param Str A pointer to a Null-terminated Unicode string. The
324 string must be allocated in advance to hold FatSize
337 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
339 @param PrivateData Global memory map for accessing global variables
340 @param Str1 First string to perform case insensitive comparison.
341 @param Str2 Second string to perform case insensitive comparison.
346 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
353 Reads a block of data from the block device by calling
354 underlying Block I/O service.
356 @param PrivateData Global memory map for accessing global variables
357 @param BlockDeviceNo The index for the block device number.
358 @param Lba The logic block address to read data from.
359 @param BufferSize The size of data in byte to read.
360 @param Buffer The buffer of the
362 @retval EFI_DEVICE_ERROR The specified block device number exceeds the maximum
364 @retval EFI_DEVICE_ERROR The maximum address has exceeded the maximum address
370 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
371 IN UINTN BlockDeviceNo
,
379 Check if there is a valid FAT in the corresponding Block device
380 of the volume and if yes, fill in the relevant fields for the
381 volume structure. Note there should be a valid Block device number
384 @param PrivateData Global memory map for accessing global
386 @param Volume On input, the BlockDeviceNumber field of the
387 Volume should be a valid value. On successful
388 output, all fields except the VolumeNumber
389 field is initialized.
391 @retval EFI_SUCCESS A FAT is found and the volume structure is
393 @retval EFI_NOT_FOUND There is no FAT on the corresponding device.
394 @retval EFI_DEVICE_ERROR There is something error while accessing device.
399 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
400 IN OUT PEI_FAT_VOLUME
*Volume
405 Gets the next cluster in the cluster chain.
407 @param PrivateData Global memory map for accessing global variables
408 @param Volume The volume
409 @param Cluster The cluster
410 @param NextCluster The cluster number of the next cluster
412 @retval EFI_SUCCESS The address is got
413 @retval EFI_INVALID_PARAMETER ClusterNo exceeds the MaxCluster of the volume.
414 @retval EFI_DEVICE_ERROR Read disk error
419 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
420 IN PEI_FAT_VOLUME
*Volume
,
422 OUT UINT32
*NextCluster
429 @param PrivateData the global memory map;
430 @param BlockDeviceNo the block device to read;
431 @param StartingAddress the starting address.
432 @param Size the amount of data to read.
433 @param Buffer the buffer holding the data
435 @retval EFI_SUCCESS The function completed successfully.
436 @retval EFI_DEVICE_ERROR Something error.
441 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
442 IN UINTN BlockDeviceNo
,
443 IN UINT64 StartingAddress
,
450 Set a file's CurrentPos and CurrentCluster, then compute StraightReadAmount.
452 @param PrivateData the global memory map
454 @param Pos the Position which is offset from the file's
457 @retval EFI_SUCCESS Success.
458 @retval EFI_INVALID_PARAMETER Pos is beyond file's size.
459 @retval EFI_DEVICE_ERROR Something error while accessing media.
464 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
465 IN PEI_FAT_FILE
*File
,
471 Reads file data. Updates the file's CurrentPos.
473 @param PrivateData Global memory map for accessing global variables
474 @param File The file.
475 @param Size The amount of data to read.
476 @param Buffer The buffer storing the data.
478 @retval EFI_SUCCESS The data is read.
479 @retval EFI_INVALID_PARAMETER File is invalid.
480 @retval EFI_DEVICE_ERROR Something error while accessing media.
485 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
486 IN PEI_FAT_FILE
*File
,
493 This function reads the next item in the parent directory and
494 initializes the output parameter SubFile (CurrentPos is initialized to 0).
495 The function updates the CurrentPos of the parent dir to after the item read.
496 If no more items were found, the function returns EFI_NOT_FOUND.
498 @param PrivateData Global memory map for accessing global variables
499 @param ParentDir The parent directory.
500 @param SubFile The File structure containing the sub file that
503 @retval EFI_SUCCESS The next sub file is obtained.
504 @retval EFI_INVALID_PARAMETER The ParentDir is not a directory.
505 @retval EFI_NOT_FOUND No more sub file exists.
506 @retval EFI_DEVICE_ERROR Something error while accessing media.
510 FatReadNextDirectoryEntry (
511 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
512 IN PEI_FAT_FILE
*ParentDir
,
513 OUT PEI_FAT_FILE
*SubFile
518 This function finds partitions (logical devices) in physical block devices.
520 @param PrivateData Global memory map for accessing global variables.
525 IN PEI_FAT_PRIVATE_DATA
*PrivateData
528 #endif // _FAT_PEIM_H_