2 Data structures for FAT recovery PEIM
4 Copyright (c) 2006 - 2016, 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/PcdLib.h>
31 #include <Library/PeiServicesTablePointerLib.h>
32 #include <Library/PeiServicesLib.h>
34 #include "FatLiteApi.h"
35 #include "FatLiteFmt.h"
41 #define PEI_FAT_CACHE_SIZE 4
42 #define PEI_FAT_MAX_BLOCK_SIZE 8192
43 #define FAT_MAX_FILE_NAME_LENGTH 128
44 #define PEI_FAT_MAX_BLOCK_DEVICE 64
45 #define PEI_FAT_MAX_BLOCK_IO_PPI 32
46 #define PEI_FAT_MAX_VOLUME 64
48 #define PEI_FAT_MEMMORY_PAGE_SIZE 0x1000
62 BOOLEAN PartitionChecked
;
65 // Following fields only valid for logical device
67 CHAR8 PartitionFlag
[8];
72 // Following fields only valid for physical device
74 EFI_PEI_BLOCK_DEVICE_TYPE DevType
;
77 // EFI_PEI_READ_BLOCKS ReadFunc;
79 EFI_PEI_RECOVERY_BLOCK_IO_PPI
*BlockIo
;
80 EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*BlockIo2
;
82 } PEI_FAT_BLOCK_DEVICE
;
85 // the Volume structure
93 CHAR16 VolumeLabel
[FAT_MAX_FILE_NAME_LENGTH
];
98 UINT64 FirstClusterPos
;
101 UINT32 RootDirCluster
;
110 PEI_FAT_VOLUME
*Volume
;
111 CHAR16 FileName
[FAT_MAX_FILE_NAME_LENGTH
];
113 BOOLEAN IsFixedRootDir
;
115 UINT32 StartingCluster
;
117 UINT32 StraightReadAmount
;
118 UINT32 CurrentCluster
;
134 UINT64 Buffer
[PEI_FAT_MAX_BLOCK_SIZE
/ 8];
137 } PEI_FAT_CACHE_BUFFER
;
141 // This structure abstracts the whole memory usage in FAT PEIM.
142 // The entry point routine will get a chunk of memory (by whatever
143 // means) whose size is sizeof(PEI_FAT_PRIVATE_DATA), which is clean
144 // in both 32 and 64 bit environment. The boundary of the memory chunk
145 // should be 64bit aligned.
147 #define PEI_FAT_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('p', 'f', 'a', 't')
152 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI DeviceRecoveryPpi
;
153 EFI_PEI_PPI_DESCRIPTOR PpiDescriptor
;
154 EFI_PEI_NOTIFY_DESCRIPTOR NotifyDescriptor
[2];
156 UINT8 UnicodeCaseMap
[0x300];
161 UINT64 BlockData
[PEI_FAT_MAX_BLOCK_SIZE
/ 8];
162 UINTN BlockDeviceCount
;
163 PEI_FAT_BLOCK_DEVICE BlockDevice
[PEI_FAT_MAX_BLOCK_DEVICE
];
165 PEI_FAT_VOLUME Volume
[PEI_FAT_MAX_VOLUME
];
167 PEI_FAT_CACHE_BUFFER CacheBuffer
[PEI_FAT_CACHE_SIZE
];
169 } PEI_FAT_PRIVATE_DATA
;
171 #define PEI_FAT_PRIVATE_DATA_FROM_THIS(a) \
172 CR (a, PEI_FAT_PRIVATE_DATA, DeviceRecoveryPpi, PEI_FAT_PRIVATE_DATA_SIGNATURE)
175 // Extract INT32 from char array
177 #define UNPACK_INT32(a) \
178 (INT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
181 // Extract UINT32 from char array
183 #define UNPACK_UINT32(a) \
184 (UINT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
192 Finds the recovery file on a FAT volume.
193 This function finds the the recovery file named FileName on a specified FAT volume and returns
194 its FileHandle pointer.
196 @param PrivateData Global memory map for accessing global
198 @param VolumeIndex The index of the volume.
199 @param FileName The recovery file name to find.
200 @param Handle The output file handle.
202 @retval EFI_DEVICE_ERROR Some error occured when operating the FAT
204 @retval EFI_NOT_FOUND The recovery file was not found.
205 @retval EFI_SUCCESS The recovery file was successfully found on the
211 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
212 IN UINTN VolumeIndex
,
214 OUT PEI_FILE_HANDLE
*Handle
219 Returns the number of DXE capsules residing on the device.
220 This function, by whatever mechanism, searches for DXE capsules from the associated device and
221 returns the number and maximum size in bytes of the capsules discovered.Entry 1 is assumed to be
222 the highest load priority and entry N is assumed to be the lowest priority.
224 @param PeiServices General-purpose services that are available to
226 @param This Indicates the
227 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
228 @param NumberRecoveryCapsules Pointer to a caller-allocated UINTN.On output,
229 *NumberRecoveryCapsules contains the number of
230 recovery capsule images available for retrieval
231 from this PEIM instance.
233 @retval EFI_SUCCESS The function completed successfully.
238 GetNumberRecoveryCapsules (
239 IN EFI_PEI_SERVICES
**PeiServices
,
240 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
241 OUT UINTN
*NumberRecoveryCapsules
246 Returns the size and type of the requested recovery capsule.
247 This function returns the size and type of the capsule specified by CapsuleInstance.
249 @param PeiServices General-purpose services that are available to
251 @param This Indicates the
252 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
253 @param CapsuleInstance Specifies for which capsule instance to
254 retrieve the information.T his parameter must
255 be between one and the value returned by
256 GetNumberRecoveryCapsules() in
257 NumberRecoveryCapsules.
258 @param Size A pointer to a caller-allocated UINTN in which
259 the size of the requested recovery module is
261 @param CapsuleType A pointer to a caller-allocated EFI_GUID in
262 which the type of the requested recovery
263 capsule is returned.T he semantic meaning of
264 the value returned is defined by the
267 @retval EFI_SUCCESS The capsule type and size were retrieved.
268 @retval EFI_INVALID_PARAMETER The input CapsuleInstance does not match any
269 discovered recovery capsule.
274 GetRecoveryCapsuleInfo (
275 IN EFI_PEI_SERVICES
**PeiServices
,
276 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
277 IN UINTN CapsuleInstance
,
279 OUT EFI_GUID
*CapsuleType
284 Loads a DXE capsule from some media into memory.
286 This function, by whatever mechanism, retrieves a DXE capsule from some device
287 and loads it into memory. Note that the published interface is device neutral.
289 @param[in] PeiServices General-purpose services that are available
291 @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
293 @param[in] CapsuleInstance Specifies which capsule instance to retrieve.
294 @param[out] Buffer Specifies a caller-allocated buffer in which
295 the requested recovery capsule will be returned.
297 @retval EFI_SUCCESS The capsule was loaded correctly.
298 @retval EFI_DEVICE_ERROR A device error occurred.
299 @retval EFI_NOT_FOUND A requested recovery DXE capsule cannot be found.
304 LoadRecoveryCapsule (
305 IN EFI_PEI_SERVICES
**PeiServices
,
306 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
307 IN UINTN CapsuleInstance
,
313 This version is different from the version in Unicode collation
314 protocol in that this version strips off trailing blanks.
315 Converts an 8.3 FAT file name using an OEM character set
316 to a Null-terminated Unicode string.
317 Here does not expand DBCS FAT chars.
319 @param FatSize The size of the string Fat in bytes.
320 @param Fat A pointer to a Null-terminated string that contains
321 an 8.3 file name using an OEM character set.
322 @param Str A pointer to a Null-terminated Unicode string. The
323 string must be allocated in advance to hold FatSize
336 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
338 @param PrivateData Global memory map for accessing global variables
339 @param Str1 First string to perform case insensitive comparison.
340 @param Str2 Second string to perform case insensitive comparison.
345 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
352 Reads a block of data from the block device by calling
353 underlying Block I/O service.
355 @param PrivateData Global memory map for accessing global variables
356 @param BlockDeviceNo The index for the block device number.
357 @param Lba The logic block address to read data from.
358 @param BufferSize The size of data in byte to read.
359 @param Buffer The buffer of the
361 @retval EFI_DEVICE_ERROR The specified block device number exceeds the maximum
363 @retval EFI_DEVICE_ERROR The maximum address has exceeded the maximum address
369 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
370 IN UINTN BlockDeviceNo
,
378 Check if there is a valid FAT in the corresponding Block device
379 of the volume and if yes, fill in the relevant fields for the
380 volume structure. Note there should be a valid Block device number
383 @param PrivateData Global memory map for accessing global
385 @param Volume On input, the BlockDeviceNumber field of the
386 Volume should be a valid value. On successful
387 output, all fields except the VolumeNumber
388 field is initialized.
390 @retval EFI_SUCCESS A FAT is found and the volume structure is
392 @retval EFI_NOT_FOUND There is no FAT on the corresponding device.
393 @retval EFI_DEVICE_ERROR There is something error while accessing device.
398 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
399 IN OUT PEI_FAT_VOLUME
*Volume
404 Gets the next cluster in the cluster chain.
406 @param PrivateData Global memory map for accessing global variables
407 @param Volume The volume
408 @param Cluster The cluster
409 @param NextCluster The cluster number of the next cluster
411 @retval EFI_SUCCESS The address is got
412 @retval EFI_INVALID_PARAMETER ClusterNo exceeds the MaxCluster of the volume.
413 @retval EFI_DEVICE_ERROR Read disk error
418 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
419 IN PEI_FAT_VOLUME
*Volume
,
421 OUT UINT32
*NextCluster
428 @param PrivateData the global memory map;
429 @param BlockDeviceNo the block device to read;
430 @param StartingAddress the starting address.
431 @param Size the amount of data to read.
432 @param Buffer the buffer holding the data
434 @retval EFI_SUCCESS The function completed successfully.
435 @retval EFI_DEVICE_ERROR Something error.
440 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
441 IN UINTN BlockDeviceNo
,
442 IN UINT64 StartingAddress
,
449 Set a file's CurrentPos and CurrentCluster, then compute StraightReadAmount.
451 @param PrivateData the global memory map
453 @param Pos the Position which is offset from the file's
456 @retval EFI_SUCCESS Success.
457 @retval EFI_INVALID_PARAMETER Pos is beyond file's size.
458 @retval EFI_DEVICE_ERROR Something error while accessing media.
463 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
464 IN PEI_FAT_FILE
*File
,
470 Reads file data. Updates the file's CurrentPos.
472 @param PrivateData Global memory map for accessing global variables
473 @param File The file.
474 @param Size The amount of data to read.
475 @param Buffer The buffer storing the data.
477 @retval EFI_SUCCESS The data is read.
478 @retval EFI_INVALID_PARAMETER File is invalid.
479 @retval EFI_DEVICE_ERROR Something error while accessing media.
484 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
485 IN PEI_FAT_FILE
*File
,
492 This function reads the next item in the parent directory and
493 initializes the output parameter SubFile (CurrentPos is initialized to 0).
494 The function updates the CurrentPos of the parent dir to after the item read.
495 If no more items were found, the function returns EFI_NOT_FOUND.
497 @param PrivateData Global memory map for accessing global variables
498 @param ParentDir The parent directory.
499 @param SubFile The File structure containing the sub file that
502 @retval EFI_SUCCESS The next sub file is obtained.
503 @retval EFI_INVALID_PARAMETER The ParentDir is not a directory.
504 @retval EFI_NOT_FOUND No more sub file exists.
505 @retval EFI_DEVICE_ERROR Something error while accessing media.
509 FatReadNextDirectoryEntry (
510 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
511 IN PEI_FAT_FILE
*ParentDir
,
512 OUT PEI_FAT_FILE
*SubFile
517 This function finds partitions (logical devices) in physical block devices.
519 @param PrivateData Global memory map for accessing global variables.
524 IN PEI_FAT_PRIVATE_DATA
*PrivateData
527 #endif // _FAT_PEIM_H_