2 Data structures for FAT recovery PEIM
4 Copyright (c) 2006 - 2015, 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"
40 #define PEI_FAT_RECOVERY_CAPSULE_WITH_NT_EMULATOR L"fv0001.fv"
41 #define PEI_FAT_RECOVERY_CAPSULE_WITHOUT_NT_EMULATOR L"fvmain.fv"
43 #define PEI_FAT_CACHE_SIZE 4
44 #define PEI_FAT_MAX_BLOCK_SIZE 8192
45 #define FAT_MAX_FILE_NAME_LENGTH 128
46 #define PEI_FAT_MAX_BLOCK_DEVICE 64
47 #define PEI_FAT_MAX_BLOCK_IO_PPI 32
48 #define PEI_FAT_MAX_VOLUME 64
50 #define PEI_FAT_MEMMORY_PAGE_SIZE 0x1000
64 BOOLEAN PartitionChecked
;
67 // Following fields only valid for logical device
69 CHAR8 PartitionFlag
[8];
74 // Following fields only valid for physical device
76 EFI_PEI_BLOCK_DEVICE_TYPE DevType
;
79 // EFI_PEI_READ_BLOCKS ReadFunc;
81 EFI_PEI_RECOVERY_BLOCK_IO_PPI
*BlockIo
;
82 EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*BlockIo2
;
84 } PEI_FAT_BLOCK_DEVICE
;
87 // the Volume structure
95 CHAR16 VolumeLabel
[FAT_MAX_FILE_NAME_LENGTH
];
100 UINT64 FirstClusterPos
;
103 UINT32 RootDirCluster
;
112 PEI_FAT_VOLUME
*Volume
;
113 CHAR16 FileName
[FAT_MAX_FILE_NAME_LENGTH
];
115 BOOLEAN IsFixedRootDir
;
117 UINT32 StartingCluster
;
119 UINT32 StraightReadAmount
;
120 UINT32 CurrentCluster
;
136 UINT64 Buffer
[PEI_FAT_MAX_BLOCK_SIZE
/ 8];
139 } PEI_FAT_CACHE_BUFFER
;
143 // This structure abstracts the whole memory usage in FAT PEIM.
144 // The entry point routine will get a chunk of memory (by whatever
145 // means) whose size is sizeof(PEI_FAT_PRIVATE_DATA), which is clean
146 // in both 32 and 64 bit environment. The boundary of the memory chunk
147 // should be 64bit aligned.
149 #define PEI_FAT_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('p', 'f', 'a', 't')
154 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI DeviceRecoveryPpi
;
155 EFI_PEI_PPI_DESCRIPTOR PpiDescriptor
;
156 EFI_PEI_NOTIFY_DESCRIPTOR NotifyDescriptor
[2];
158 UINT8 UnicodeCaseMap
[0x300];
163 UINT64 BlockData
[PEI_FAT_MAX_BLOCK_SIZE
/ 8];
164 UINTN BlockDeviceCount
;
165 PEI_FAT_BLOCK_DEVICE BlockDevice
[PEI_FAT_MAX_BLOCK_DEVICE
];
167 PEI_FAT_VOLUME Volume
[PEI_FAT_MAX_VOLUME
];
169 PEI_FAT_CACHE_BUFFER CacheBuffer
[PEI_FAT_CACHE_SIZE
];
171 } PEI_FAT_PRIVATE_DATA
;
173 #define PEI_FAT_PRIVATE_DATA_FROM_THIS(a) \
174 CR (a, PEI_FAT_PRIVATE_DATA, DeviceRecoveryPpi, PEI_FAT_PRIVATE_DATA_SIGNATURE)
177 // Extract INT32 from char array
179 #define UNPACK_INT32(a) \
180 (INT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
183 // Extract UINT32 from char array
185 #define UNPACK_UINT32(a) \
186 (UINT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
194 Finds the recovery file on a FAT volume.
195 This function finds the the recovery file named FileName on a specified FAT volume and returns
196 its FileHandle pointer.
198 @param PrivateData Global memory map for accessing global
200 @param VolumeIndex The index of the volume.
201 @param FileName The recovery file name to find.
202 @param Handle The output file handle.
204 @retval EFI_DEVICE_ERROR Some error occured when operating the FAT
206 @retval EFI_NOT_FOUND The recovery file was not found.
207 @retval EFI_SUCCESS The recovery file was successfully found on the
213 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
214 IN UINTN VolumeIndex
,
216 OUT PEI_FILE_HANDLE
*Handle
221 Returns the number of DXE capsules residing on the device.
222 This function, by whatever mechanism, searches for DXE capsules from the associated device and
223 returns the number and maximum size in bytes of the capsules discovered.Entry 1 is assumed to be
224 the highest load priority and entry N is assumed to be the lowest priority.
226 @param PeiServices General-purpose services that are available to
228 @param This Indicates the
229 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
230 @param NumberRecoveryCapsules Pointer to a caller-allocated UINTN.On output,
231 *NumberRecoveryCapsules contains the number of
232 recovery capsule images available for retrieval
233 from this PEIM instance.
235 @retval EFI_SUCCESS The function completed successfully.
240 GetNumberRecoveryCapsules (
241 IN EFI_PEI_SERVICES
**PeiServices
,
242 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
243 OUT UINTN
*NumberRecoveryCapsules
248 Returns the size and type of the requested recovery capsule.
249 This function returns the size and type of the capsule specified by CapsuleInstance.
251 @param PeiServices General-purpose services that are available to
253 @param This Indicates the
254 EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
255 @param CapsuleInstance Specifies for which capsule instance to
256 retrieve the information.T his parameter must
257 be between one and the value returned by
258 GetNumberRecoveryCapsules() in
259 NumberRecoveryCapsules.
260 @param Size A pointer to a caller-allocated UINTN in which
261 the size of the requested recovery module is
263 @param CapsuleType A pointer to a caller-allocated EFI_GUID in
264 which the type of the requested recovery
265 capsule is returned.T he semantic meaning of
266 the value returned is defined by the
269 @retval EFI_SUCCESS The capsule type and size were retrieved.
270 @retval EFI_INVALID_PARAMETER The input CapsuleInstance does not match any
271 discovered recovery capsule.
276 GetRecoveryCapsuleInfo (
277 IN EFI_PEI_SERVICES
**PeiServices
,
278 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
279 IN UINTN CapsuleInstance
,
281 OUT EFI_GUID
*CapsuleType
286 Loads a DXE capsule from some media into memory.
288 This function, by whatever mechanism, retrieves a DXE capsule from some device
289 and loads it into memory. Note that the published interface is device neutral.
291 @param[in] PeiServices General-purpose services that are available
293 @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
295 @param[in] CapsuleInstance Specifies which capsule instance to retrieve.
296 @param[out] Buffer Specifies a caller-allocated buffer in which
297 the requested recovery capsule will be returned.
299 @retval EFI_SUCCESS The capsule was loaded correctly.
300 @retval EFI_DEVICE_ERROR A device error occurred.
301 @retval EFI_NOT_FOUND A requested recovery DXE capsule cannot be found.
306 LoadRecoveryCapsule (
307 IN EFI_PEI_SERVICES
**PeiServices
,
308 IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
*This
,
309 IN UINTN CapsuleInstance
,
315 This version is different from the version in Unicode collation
316 protocol in that this version strips off trailing blanks.
317 Converts an 8.3 FAT file name using an OEM character set
318 to a Null-terminated Unicode string.
319 Here does not expand DBCS FAT chars.
321 @param FatSize The size of the string Fat in bytes.
322 @param Fat A pointer to a Null-terminated string that contains
323 an 8.3 file name using an OEM character set.
324 @param Str A pointer to a Null-terminated Unicode string. The
325 string must be allocated in advance to hold FatSize
338 Performs a case-insensitive comparison of two Null-terminated Unicode strings.
340 @param PrivateData Global memory map for accessing global variables
341 @param Str1 First string to perform case insensitive comparison.
342 @param Str2 Second string to perform case insensitive comparison.
347 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
354 Reads a block of data from the block device by calling
355 underlying Block I/O service.
357 @param PrivateData Global memory map for accessing global variables
358 @param BlockDeviceNo The index for the block device number.
359 @param Lba The logic block address to read data from.
360 @param BufferSize The size of data in byte to read.
361 @param Buffer The buffer of the
363 @retval EFI_DEVICE_ERROR The specified block device number exceeds the maximum
365 @retval EFI_DEVICE_ERROR The maximum address has exceeded the maximum address
371 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
372 IN UINTN BlockDeviceNo
,
380 Check if there is a valid FAT in the corresponding Block device
381 of the volume and if yes, fill in the relevant fields for the
382 volume structure. Note there should be a valid Block device number
385 @param PrivateData Global memory map for accessing global
387 @param Volume On input, the BlockDeviceNumber field of the
388 Volume should be a valid value. On successful
389 output, all fields except the VolumeNumber
390 field is initialized.
392 @retval EFI_SUCCESS A FAT is found and the volume structure is
394 @retval EFI_NOT_FOUND There is no FAT on the corresponding device.
395 @retval EFI_DEVICE_ERROR There is something error while accessing device.
400 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
401 IN OUT PEI_FAT_VOLUME
*Volume
406 Gets the next cluster in the cluster chain.
408 @param PrivateData Global memory map for accessing global variables
409 @param Volume The volume
410 @param Cluster The cluster
411 @param NextCluster The cluster number of the next cluster
413 @retval EFI_SUCCESS The address is got
414 @retval EFI_INVALID_PARAMETER ClusterNo exceeds the MaxCluster of the volume.
415 @retval EFI_DEVICE_ERROR Read disk error
420 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
421 IN PEI_FAT_VOLUME
*Volume
,
423 OUT UINT32
*NextCluster
430 @param PrivateData the global memory map;
431 @param BlockDeviceNo the block device to read;
432 @param StartingAddress the starting address.
433 @param Size the amount of data to read.
434 @param Buffer the buffer holding the data
436 @retval EFI_SUCCESS The function completed successfully.
437 @retval EFI_DEVICE_ERROR Something error.
442 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
443 IN UINTN BlockDeviceNo
,
444 IN UINT64 StartingAddress
,
451 Set a file's CurrentPos and CurrentCluster, then compute StraightReadAmount.
453 @param PrivateData the global memory map
455 @param Pos the Position which is offset from the file's
458 @retval EFI_SUCCESS Success.
459 @retval EFI_INVALID_PARAMETER Pos is beyond file's size.
460 @retval EFI_DEVICE_ERROR Something error while accessing media.
465 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
466 IN PEI_FAT_FILE
*File
,
472 Reads file data. Updates the file's CurrentPos.
474 @param PrivateData Global memory map for accessing global variables
475 @param File The file.
476 @param Size The amount of data to read.
477 @param Buffer The buffer storing the data.
479 @retval EFI_SUCCESS The data is read.
480 @retval EFI_INVALID_PARAMETER File is invalid.
481 @retval EFI_DEVICE_ERROR Something error while accessing media.
486 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
487 IN PEI_FAT_FILE
*File
,
494 This function reads the next item in the parent directory and
495 initializes the output parameter SubFile (CurrentPos is initialized to 0).
496 The function updates the CurrentPos of the parent dir to after the item read.
497 If no more items were found, the function returns EFI_NOT_FOUND.
499 @param PrivateData Global memory map for accessing global variables
500 @param ParentDir The parent directory.
501 @param SubFile The File structure containing the sub file that
504 @retval EFI_SUCCESS The next sub file is obtained.
505 @retval EFI_INVALID_PARAMETER The ParentDir is not a directory.
506 @retval EFI_NOT_FOUND No more sub file exists.
507 @retval EFI_DEVICE_ERROR Something error while accessing media.
511 FatReadNextDirectoryEntry (
512 IN PEI_FAT_PRIVATE_DATA
*PrivateData
,
513 IN PEI_FAT_FILE
*ParentDir
,
514 OUT PEI_FAT_FILE
*SubFile
519 This function finds partitions (logical devices) in physical block devices.
521 @param PrivateData Global memory map for accessing global variables.
526 IN PEI_FAT_PRIVATE_DATA
*PrivateData
529 #endif // _FAT_PEIM_H_