FatLitePeim.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /** @file
  2. Data structures for FAT recovery PEIM
  3. Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef _FAT_PEIM_H_
  7. #define _FAT_PEIM_H_
  8. #include <PiPei.h>
  9. #include <Guid/RecoveryDevice.h>
  10. #include <Ppi/BlockIo.h>
  11. #include <Ppi/BlockIo2.h>
  12. #include <Ppi/DeviceRecoveryModule.h>
  13. #include <Library/DebugLib.h>
  14. #include <Library/BaseLib.h>
  15. #include <Library/PeimEntryPoint.h>
  16. #include <Library/BaseMemoryLib.h>
  17. #include <Library/MemoryAllocationLib.h>
  18. #include <Library/PcdLib.h>
  19. #include <Library/PeiServicesTablePointerLib.h>
  20. #include <Library/PeiServicesLib.h>
  21. #include "FatLiteApi.h"
  22. #include "FatLiteFmt.h"
  23. //
  24. // Definitions
  25. //
  26. #define PEI_FAT_CACHE_SIZE 4
  27. #define PEI_FAT_MAX_BLOCK_SIZE 8192
  28. #define FAT_MAX_FILE_NAME_LENGTH 128
  29. #define PEI_FAT_MAX_BLOCK_DEVICE 64
  30. #define PEI_FAT_MAX_BLOCK_IO_PPI 32
  31. #define PEI_FAT_MAX_VOLUME 64
  32. #define PEI_FAT_MEMORY_PAGE_SIZE 0x1000
  33. //
  34. // Data Structures
  35. //
  36. //
  37. // The block device
  38. //
  39. typedef struct {
  40. UINT32 BlockSize;
  41. UINT64 LastBlock;
  42. UINT32 IoAlign;
  43. BOOLEAN Logical;
  44. BOOLEAN PartitionChecked;
  45. //
  46. // Following fields only valid for logical device
  47. //
  48. CHAR8 PartitionFlag[8];
  49. UINT64 StartingPos;
  50. UINTN ParentDevNo;
  51. //
  52. // Following fields only valid for physical device
  53. //
  54. EFI_PEI_BLOCK_DEVICE_TYPE DevType;
  55. UINT8 InterfaceType;
  56. //
  57. // EFI_PEI_READ_BLOCKS ReadFunc;
  58. //
  59. EFI_PEI_RECOVERY_BLOCK_IO_PPI *BlockIo;
  60. EFI_PEI_RECOVERY_BLOCK_IO2_PPI *BlockIo2;
  61. UINT8 PhysicalDevNo;
  62. } PEI_FAT_BLOCK_DEVICE;
  63. //
  64. // the Volume structure
  65. //
  66. typedef struct {
  67. UINTN BlockDeviceNo;
  68. UINTN VolumeNo;
  69. UINT64 VolumeSize;
  70. UINTN MaxCluster;
  71. CHAR16 VolumeLabel[FAT_MAX_FILE_NAME_LENGTH];
  72. PEI_FAT_TYPE FatType;
  73. UINT64 FatPos;
  74. UINT32 SectorSize;
  75. UINT32 ClusterSize;
  76. UINT64 FirstClusterPos;
  77. UINT64 RootDirPos;
  78. UINT32 RootEntries;
  79. UINT32 RootDirCluster;
  80. } PEI_FAT_VOLUME;
  81. //
  82. // File instance
  83. //
  84. typedef struct {
  85. PEI_FAT_VOLUME *Volume;
  86. CHAR16 FileName[FAT_MAX_FILE_NAME_LENGTH];
  87. BOOLEAN IsFixedRootDir;
  88. UINT32 StartingCluster;
  89. UINT32 CurrentPos;
  90. UINT32 StraightReadAmount;
  91. UINT32 CurrentCluster;
  92. UINT8 Attributes;
  93. UINT32 FileSize;
  94. } PEI_FAT_FILE;
  95. //
  96. // Cache Buffer
  97. //
  98. typedef struct {
  99. BOOLEAN Valid;
  100. UINTN BlockDeviceNo;
  101. UINT64 Lba;
  102. UINT32 Lru;
  103. UINT64 Buffer[PEI_FAT_MAX_BLOCK_SIZE / 8];
  104. UINTN Size;
  105. } PEI_FAT_CACHE_BUFFER;
  106. //
  107. // Private Data.
  108. // This structure abstracts the whole memory usage in FAT PEIM.
  109. // The entry point routine will get a chunk of memory (by whatever
  110. // means) whose size is sizeof(PEI_FAT_PRIVATE_DATA), which is clean
  111. // in both 32 and 64 bit environment. The boundary of the memory chunk
  112. // should be 64bit aligned.
  113. //
  114. #define PEI_FAT_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('p', 'f', 'a', 't')
  115. typedef struct {
  116. UINTN Signature;
  117. EFI_PEI_DEVICE_RECOVERY_MODULE_PPI DeviceRecoveryPpi;
  118. EFI_PEI_PPI_DESCRIPTOR PpiDescriptor;
  119. EFI_PEI_NOTIFY_DESCRIPTOR NotifyDescriptor[2];
  120. UINT8 UnicodeCaseMap[0x300];
  121. CHAR8 *EngUpperMap;
  122. CHAR8 *EngLowerMap;
  123. CHAR8 *EngInfoMap;
  124. UINT64 BlockData[PEI_FAT_MAX_BLOCK_SIZE / 8];
  125. UINTN BlockDeviceCount;
  126. PEI_FAT_BLOCK_DEVICE BlockDevice[PEI_FAT_MAX_BLOCK_DEVICE];
  127. UINTN VolumeCount;
  128. PEI_FAT_VOLUME Volume[PEI_FAT_MAX_VOLUME];
  129. PEI_FAT_FILE File;
  130. PEI_FAT_CACHE_BUFFER CacheBuffer[PEI_FAT_CACHE_SIZE];
  131. } PEI_FAT_PRIVATE_DATA;
  132. #define PEI_FAT_PRIVATE_DATA_FROM_THIS(a) \
  133. CR (a, PEI_FAT_PRIVATE_DATA, DeviceRecoveryPpi, PEI_FAT_PRIVATE_DATA_SIGNATURE)
  134. //
  135. // Extract INT32 from char array
  136. //
  137. #define UNPACK_INT32(a) \
  138. (INT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
  139. //
  140. // Extract UINT32 from char array
  141. //
  142. #define UNPACK_UINT32(a) \
  143. (UINT32) ((((UINT8 *) a)[0] << 0) | (((UINT8 *) a)[1] << 8) | (((UINT8 *) a)[2] << 16) | (((UINT8 *) a)[3] << 24))
  144. //
  145. // API functions
  146. //
  147. /**
  148. Finds the recovery file on a FAT volume.
  149. This function finds the recovery file named FileName on a specified FAT volume and returns
  150. its FileHandle pointer.
  151. @param PrivateData Global memory map for accessing global
  152. variables.
  153. @param VolumeIndex The index of the volume.
  154. @param FileName The recovery file name to find.
  155. @param Handle The output file handle.
  156. @retval EFI_DEVICE_ERROR Some error occurred when operating the FAT
  157. volume.
  158. @retval EFI_NOT_FOUND The recovery file was not found.
  159. @retval EFI_SUCCESS The recovery file was successfully found on the
  160. FAT volume.
  161. **/
  162. EFI_STATUS
  163. FindRecoveryFile (
  164. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  165. IN UINTN VolumeIndex,
  166. IN CHAR16 *FileName,
  167. OUT PEI_FILE_HANDLE *Handle
  168. );
  169. /**
  170. Returns the number of DXE capsules residing on the device.
  171. This function, by whatever mechanism, searches for DXE capsules from the associated device and
  172. returns the number and maximum size in bytes of the capsules discovered.Entry 1 is assumed to be
  173. the highest load priority and entry N is assumed to be the lowest priority.
  174. @param PeiServices General-purpose services that are available to
  175. every PEIM.
  176. @param This Indicates the
  177. EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
  178. @param NumberRecoveryCapsules Pointer to a caller-allocated UINTN.On output,
  179. *NumberRecoveryCapsules contains the number of
  180. recovery capsule images available for retrieval
  181. from this PEIM instance.
  182. @retval EFI_SUCCESS The function completed successfully.
  183. **/
  184. EFI_STATUS
  185. EFIAPI
  186. GetNumberRecoveryCapsules (
  187. IN EFI_PEI_SERVICES **PeiServices,
  188. IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
  189. OUT UINTN *NumberRecoveryCapsules
  190. );
  191. /**
  192. Returns the size and type of the requested recovery capsule.
  193. This function returns the size and type of the capsule specified by CapsuleInstance.
  194. @param PeiServices General-purpose services that are available to
  195. every PEIM.
  196. @param This Indicates the
  197. EFI_PEI_DEVICE_RECOVERY_MODULE_PPI instance.
  198. @param CapsuleInstance Specifies for which capsule instance to
  199. retrieve the information.T his parameter must
  200. be between one and the value returned by
  201. GetNumberRecoveryCapsules() in
  202. NumberRecoveryCapsules.
  203. @param Size A pointer to a caller-allocated UINTN in which
  204. the size of the requested recovery module is
  205. returned.
  206. @param CapsuleType A pointer to a caller-allocated EFI_GUID in
  207. which the type of the requested recovery
  208. capsule is returned.T he semantic meaning of
  209. the value returned is defined by the
  210. implementation.
  211. @retval EFI_SUCCESS The capsule type and size were retrieved.
  212. @retval EFI_INVALID_PARAMETER The input CapsuleInstance does not match any
  213. discovered recovery capsule.
  214. **/
  215. EFI_STATUS
  216. EFIAPI
  217. GetRecoveryCapsuleInfo (
  218. IN EFI_PEI_SERVICES **PeiServices,
  219. IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
  220. IN UINTN CapsuleInstance,
  221. OUT UINTN *Size,
  222. OUT EFI_GUID *CapsuleType
  223. );
  224. /**
  225. Loads a DXE capsule from some media into memory.
  226. This function, by whatever mechanism, retrieves a DXE capsule from some device
  227. and loads it into memory. Note that the published interface is device neutral.
  228. @param[in] PeiServices General-purpose services that are available
  229. to every PEIM
  230. @param[in] This Indicates the EFI_PEI_DEVICE_RECOVERY_MODULE_PPI
  231. instance.
  232. @param[in] CapsuleInstance Specifies which capsule instance to retrieve.
  233. @param[out] Buffer Specifies a caller-allocated buffer in which
  234. the requested recovery capsule will be returned.
  235. @retval EFI_SUCCESS The capsule was loaded correctly.
  236. @retval EFI_DEVICE_ERROR A device error occurred.
  237. @retval EFI_NOT_FOUND A requested recovery DXE capsule cannot be found.
  238. **/
  239. EFI_STATUS
  240. EFIAPI
  241. LoadRecoveryCapsule (
  242. IN EFI_PEI_SERVICES **PeiServices,
  243. IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
  244. IN UINTN CapsuleInstance,
  245. OUT VOID *Buffer
  246. );
  247. /**
  248. This version is different from the version in Unicode collation
  249. protocol in that this version strips off trailing blanks.
  250. Converts an 8.3 FAT file name using an OEM character set
  251. to a Null-terminated Unicode string.
  252. Here does not expand DBCS FAT chars.
  253. @param FatSize The size of the string Fat in bytes.
  254. @param Fat A pointer to a Null-terminated string that contains
  255. an 8.3 file name using an OEM character set.
  256. @param Str A pointer to a Null-terminated Unicode string. The
  257. string must be allocated in advance to hold FatSize
  258. Unicode characters
  259. **/
  260. VOID
  261. EngFatToStr (
  262. IN UINTN FatSize,
  263. IN CHAR8 *Fat,
  264. OUT CHAR16 *Str
  265. );
  266. /**
  267. Performs a case-insensitive comparison of two Null-terminated Unicode strings.
  268. @param PrivateData Global memory map for accessing global variables
  269. @param Str1 First string to perform case insensitive comparison.
  270. @param Str2 Second string to perform case insensitive comparison.
  271. **/
  272. BOOLEAN
  273. EngStriColl (
  274. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  275. IN CHAR16 *Str1,
  276. IN CHAR16 *Str2
  277. );
  278. /**
  279. Reads a block of data from the block device by calling
  280. underlying Block I/O service.
  281. @param PrivateData Global memory map for accessing global variables
  282. @param BlockDeviceNo The index for the block device number.
  283. @param Lba The logic block address to read data from.
  284. @param BufferSize The size of data in byte to read.
  285. @param Buffer The buffer of the
  286. @retval EFI_DEVICE_ERROR The specified block device number exceeds the maximum
  287. device number.
  288. @retval EFI_DEVICE_ERROR The maximum address has exceeded the maximum address
  289. of the block device.
  290. **/
  291. EFI_STATUS
  292. FatReadBlock (
  293. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  294. IN UINTN BlockDeviceNo,
  295. IN EFI_PEI_LBA Lba,
  296. IN UINTN BufferSize,
  297. OUT VOID *Buffer
  298. );
  299. /**
  300. Check if there is a valid FAT in the corresponding Block device
  301. of the volume and if yes, fill in the relevant fields for the
  302. volume structure. Note there should be a valid Block device number
  303. already set.
  304. @param PrivateData Global memory map for accessing global
  305. variables.
  306. @param Volume On input, the BlockDeviceNumber field of the
  307. Volume should be a valid value. On successful
  308. output, all fields except the VolumeNumber
  309. field is initialized.
  310. @retval EFI_SUCCESS A FAT is found and the volume structure is
  311. initialized.
  312. @retval EFI_NOT_FOUND There is no FAT on the corresponding device.
  313. @retval EFI_DEVICE_ERROR There is something error while accessing device.
  314. **/
  315. EFI_STATUS
  316. FatGetBpbInfo (
  317. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  318. IN OUT PEI_FAT_VOLUME *Volume
  319. );
  320. /**
  321. Gets the next cluster in the cluster chain.
  322. @param PrivateData Global memory map for accessing global variables
  323. @param Volume The volume
  324. @param Cluster The cluster
  325. @param NextCluster The cluster number of the next cluster
  326. @retval EFI_SUCCESS The address is got
  327. @retval EFI_INVALID_PARAMETER ClusterNo exceeds the MaxCluster of the volume.
  328. @retval EFI_DEVICE_ERROR Read disk error
  329. **/
  330. EFI_STATUS
  331. FatGetNextCluster (
  332. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  333. IN PEI_FAT_VOLUME *Volume,
  334. IN UINT32 Cluster,
  335. OUT UINT32 *NextCluster
  336. );
  337. /**
  338. Disk reading.
  339. @param PrivateData the global memory map;
  340. @param BlockDeviceNo the block device to read;
  341. @param StartingAddress the starting address.
  342. @param Size the amount of data to read.
  343. @param Buffer the buffer holding the data
  344. @retval EFI_SUCCESS The function completed successfully.
  345. @retval EFI_DEVICE_ERROR Something error.
  346. **/
  347. EFI_STATUS
  348. FatReadDisk (
  349. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  350. IN UINTN BlockDeviceNo,
  351. IN UINT64 StartingAddress,
  352. IN UINTN Size,
  353. OUT VOID *Buffer
  354. );
  355. /**
  356. Set a file's CurrentPos and CurrentCluster, then compute StraightReadAmount.
  357. @param PrivateData the global memory map
  358. @param File the file
  359. @param Pos the Position which is offset from the file's
  360. CurrentPos
  361. @retval EFI_SUCCESS Success.
  362. @retval EFI_INVALID_PARAMETER Pos is beyond file's size.
  363. @retval EFI_DEVICE_ERROR Something error while accessing media.
  364. **/
  365. EFI_STATUS
  366. FatSetFilePos (
  367. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  368. IN PEI_FAT_FILE *File,
  369. IN UINT32 Pos
  370. );
  371. /**
  372. Reads file data. Updates the file's CurrentPos.
  373. @param PrivateData Global memory map for accessing global variables
  374. @param File The file.
  375. @param Size The amount of data to read.
  376. @param Buffer The buffer storing the data.
  377. @retval EFI_SUCCESS The data is read.
  378. @retval EFI_INVALID_PARAMETER File is invalid.
  379. @retval EFI_DEVICE_ERROR Something error while accessing media.
  380. **/
  381. EFI_STATUS
  382. FatReadFile (
  383. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  384. IN PEI_FAT_FILE *File,
  385. IN UINTN Size,
  386. OUT VOID *Buffer
  387. );
  388. /**
  389. This function reads the next item in the parent directory and
  390. initializes the output parameter SubFile (CurrentPos is initialized to 0).
  391. The function updates the CurrentPos of the parent dir to after the item read.
  392. If no more items were found, the function returns EFI_NOT_FOUND.
  393. @param PrivateData Global memory map for accessing global variables
  394. @param ParentDir The parent directory.
  395. @param SubFile The File structure containing the sub file that
  396. is caught.
  397. @retval EFI_SUCCESS The next sub file is obtained.
  398. @retval EFI_INVALID_PARAMETER The ParentDir is not a directory.
  399. @retval EFI_NOT_FOUND No more sub file exists.
  400. @retval EFI_DEVICE_ERROR Something error while accessing media.
  401. **/
  402. EFI_STATUS
  403. FatReadNextDirectoryEntry (
  404. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  405. IN PEI_FAT_FILE *ParentDir,
  406. OUT PEI_FAT_FILE *SubFile
  407. );
  408. /**
  409. This function finds partitions (logical devices) in physical block devices.
  410. @param PrivateData Global memory map for accessing global variables.
  411. **/
  412. VOID
  413. FatFindPartitions (
  414. IN PEI_FAT_PRIVATE_DATA *PrivateData
  415. );
  416. #endif // _FAT_PEIM_H_