FatLiteAccess.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /** @file
  2. FAT file system access routines for FAT recovery PEIM
  3. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "FatLitePeim.h"
  7. /**
  8. Check if there is a valid FAT in the corresponding Block device
  9. of the volume and if yes, fill in the relevant fields for the
  10. volume structure. Note there should be a valid Block device number
  11. already set.
  12. @param PrivateData Global memory map for accessing global
  13. variables.
  14. @param Volume On input, the BlockDeviceNumber field of the
  15. Volume should be a valid value. On successful
  16. output, all fields except the VolumeNumber
  17. field is initialized.
  18. @retval EFI_SUCCESS A FAT is found and the volume structure is
  19. initialized.
  20. @retval EFI_NOT_FOUND There is no FAT on the corresponding device.
  21. @retval EFI_DEVICE_ERROR There is something error while accessing device.
  22. **/
  23. EFI_STATUS
  24. FatGetBpbInfo (
  25. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  26. IN OUT PEI_FAT_VOLUME *Volume
  27. )
  28. {
  29. EFI_STATUS Status;
  30. PEI_FAT_BOOT_SECTOR Bpb;
  31. PEI_FAT_BOOT_SECTOR_EX BpbEx;
  32. UINT32 Sectors;
  33. UINT32 SectorsPerFat;
  34. UINT32 RootDirSectors;
  35. UINT64 FatLba;
  36. UINT64 RootLba;
  37. UINT64 FirstClusterLba;
  38. //
  39. // Read in the BPB
  40. //
  41. Status = FatReadDisk (
  42. PrivateData,
  43. Volume->BlockDeviceNo,
  44. 0,
  45. sizeof (PEI_FAT_BOOT_SECTOR_EX),
  46. &BpbEx
  47. );
  48. if (EFI_ERROR (Status)) {
  49. return Status;
  50. }
  51. CopyMem (
  52. (UINT8 *)(&Bpb),
  53. (UINT8 *)(&BpbEx),
  54. sizeof (PEI_FAT_BOOT_SECTOR)
  55. );
  56. Volume->FatType = FatUnknown;
  57. Sectors = Bpb.Sectors;
  58. if (Sectors == 0) {
  59. Sectors = Bpb.LargeSectors;
  60. }
  61. SectorsPerFat = Bpb.SectorsPerFat;
  62. if (SectorsPerFat == 0) {
  63. SectorsPerFat = BpbEx.LargeSectorsPerFat;
  64. Volume->FatType = Fat32;
  65. }
  66. //
  67. // Filter out those not a FAT
  68. //
  69. if ((Bpb.Ia32Jump[0] != 0xe9) && (Bpb.Ia32Jump[0] != 0xeb) && (Bpb.Ia32Jump[0] != 0x49)) {
  70. return EFI_NOT_FOUND;
  71. }
  72. if ((Bpb.ReservedSectors == 0) || (Bpb.NoFats == 0) || (Sectors == 0)) {
  73. return EFI_NOT_FOUND;
  74. }
  75. if ((Bpb.SectorsPerCluster != 1) &&
  76. (Bpb.SectorsPerCluster != 2) &&
  77. (Bpb.SectorsPerCluster != 4) &&
  78. (Bpb.SectorsPerCluster != 8) &&
  79. (Bpb.SectorsPerCluster != 16) &&
  80. (Bpb.SectorsPerCluster != 32) &&
  81. (Bpb.SectorsPerCluster != 64) &&
  82. (Bpb.SectorsPerCluster != 128)
  83. )
  84. {
  85. return EFI_NOT_FOUND;
  86. }
  87. if ((Volume->FatType == Fat32) && ((SectorsPerFat == 0) || (BpbEx.FsVersion != 0))) {
  88. return EFI_NOT_FOUND;
  89. }
  90. if ((Bpb.Media != 0xf0) &&
  91. (Bpb.Media != 0xf8) &&
  92. (Bpb.Media != 0xf9) &&
  93. (Bpb.Media != 0xfb) &&
  94. (Bpb.Media != 0xfc) &&
  95. (Bpb.Media != 0xfd) &&
  96. (Bpb.Media != 0xfe) &&
  97. (Bpb.Media != 0xff) &&
  98. //
  99. // FujitsuFMR
  100. //
  101. (Bpb.Media != 0x00) &&
  102. (Bpb.Media != 0x01) &&
  103. (Bpb.Media != 0xfa)
  104. )
  105. {
  106. return EFI_NOT_FOUND;
  107. }
  108. if ((Volume->FatType != Fat32) && (Bpb.RootEntries == 0)) {
  109. return EFI_NOT_FOUND;
  110. }
  111. //
  112. // If this is fat32, refuse to mount mirror-disabled volumes
  113. //
  114. if ((Volume->FatType == Fat32) && ((BpbEx.ExtendedFlags & 0x80) != 0)) {
  115. return EFI_NOT_FOUND;
  116. }
  117. //
  118. // Fill in the volume structure fields
  119. // (Sectors & SectorsPerFat is computed earlier already)
  120. //
  121. Volume->ClusterSize = Bpb.SectorSize * Bpb.SectorsPerCluster;
  122. Volume->RootEntries = Bpb.RootEntries;
  123. Volume->SectorSize = Bpb.SectorSize;
  124. RootDirSectors = ((Volume->RootEntries * sizeof (FAT_DIRECTORY_ENTRY)) + (Volume->SectorSize - 1)) / Volume->SectorSize;
  125. FatLba = Bpb.ReservedSectors;
  126. RootLba = Bpb.NoFats * SectorsPerFat + FatLba;
  127. FirstClusterLba = RootLba + RootDirSectors;
  128. Volume->VolumeSize = MultU64x32 (Sectors, Volume->SectorSize);
  129. Volume->FatPos = MultU64x32 (FatLba, Volume->SectorSize);
  130. Volume->RootDirPos = MultU64x32 (RootLba, Volume->SectorSize);
  131. Volume->FirstClusterPos = MultU64x32 (FirstClusterLba, Volume->SectorSize);
  132. Volume->MaxCluster = (UINT32)(Sectors - FirstClusterLba) / Bpb.SectorsPerCluster;
  133. Volume->RootDirCluster = BpbEx.RootDirFirstCluster;
  134. //
  135. // If this is not a fat32, determine if it's a fat16 or fat12
  136. //
  137. if (Volume->FatType != Fat32) {
  138. if (Volume->MaxCluster >= 65525) {
  139. return EFI_NOT_FOUND;
  140. }
  141. Volume->FatType = Volume->MaxCluster < 4085 ? Fat12 : Fat16;
  142. }
  143. return EFI_SUCCESS;
  144. }
  145. /**
  146. Gets the next cluster in the cluster chain
  147. @param PrivateData Global memory map for accessing global variables
  148. @param Volume The volume
  149. @param Cluster The cluster
  150. @param NextCluster The cluster number of the next cluster
  151. @retval EFI_SUCCESS The address is got
  152. @retval EFI_INVALID_PARAMETER ClusterNo exceeds the MaxCluster of the volume.
  153. @retval EFI_DEVICE_ERROR Read disk error
  154. **/
  155. EFI_STATUS
  156. FatGetNextCluster (
  157. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  158. IN PEI_FAT_VOLUME *Volume,
  159. IN UINT32 Cluster,
  160. OUT UINT32 *NextCluster
  161. )
  162. {
  163. EFI_STATUS Status;
  164. UINT64 FatEntryPos;
  165. UINT32 Dummy;
  166. *NextCluster = 0;
  167. if (Volume->FatType == Fat32) {
  168. FatEntryPos = Volume->FatPos + MultU64x32 (4, Cluster);
  169. Status = FatReadDisk (PrivateData, Volume->BlockDeviceNo, FatEntryPos, 4, NextCluster);
  170. *NextCluster &= 0x0fffffff;
  171. //
  172. // Pad high bits for our FAT_CLUSTER_... macro definitions to work
  173. //
  174. if ((*NextCluster) >= 0x0ffffff7) {
  175. *NextCluster |= (-1 &~0xf);
  176. }
  177. } else if (Volume->FatType == Fat16) {
  178. FatEntryPos = Volume->FatPos + MultU64x32 (2, Cluster);
  179. Status = FatReadDisk (PrivateData, Volume->BlockDeviceNo, FatEntryPos, 2, NextCluster);
  180. //
  181. // Pad high bits for our FAT_CLUSTER_... macro definitions to work
  182. //
  183. if ((*NextCluster) >= 0xfff7) {
  184. *NextCluster |= (-1 &~0xf);
  185. }
  186. } else {
  187. FatEntryPos = Volume->FatPos + DivU64x32Remainder (MultU64x32 (3, Cluster), 2, &Dummy);
  188. Status = FatReadDisk (PrivateData, Volume->BlockDeviceNo, FatEntryPos, 2, NextCluster);
  189. if ((Cluster & 0x01) != 0) {
  190. *NextCluster = (*NextCluster) >> 4;
  191. } else {
  192. *NextCluster = (*NextCluster) & 0x0fff;
  193. }
  194. //
  195. // Pad high bits for our FAT_CLUSTER_... macro definitions to work
  196. //
  197. if ((*NextCluster) >= 0x0ff7) {
  198. *NextCluster |= (-1 &~0xf);
  199. }
  200. }
  201. if (EFI_ERROR (Status)) {
  202. return EFI_DEVICE_ERROR;
  203. }
  204. return EFI_SUCCESS;
  205. }
  206. /**
  207. Set a file's CurrentPos and CurrentCluster, then compute StraightReadAmount.
  208. @param PrivateData the global memory map
  209. @param File the file
  210. @param Pos the Position which is offset from the file's
  211. CurrentPos
  212. @retval EFI_SUCCESS Success.
  213. @retval EFI_INVALID_PARAMETER Pos is beyond file's size.
  214. @retval EFI_DEVICE_ERROR Something error while accessing media.
  215. **/
  216. EFI_STATUS
  217. FatSetFilePos (
  218. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  219. IN PEI_FAT_FILE *File,
  220. IN UINT32 Pos
  221. )
  222. {
  223. EFI_STATUS Status;
  224. UINT32 AlignedPos;
  225. UINT32 Offset;
  226. UINT32 Cluster;
  227. UINT32 PrevCluster;
  228. if (File->IsFixedRootDir) {
  229. if (Pos >= MultU64x32 (File->Volume->RootEntries, 32) - File->CurrentPos) {
  230. return EFI_INVALID_PARAMETER;
  231. }
  232. File->CurrentPos += Pos;
  233. File->StraightReadAmount = (UINT32)(MultU64x32 (File->Volume->RootEntries, 32) - File->CurrentPos);
  234. } else {
  235. DivU64x32Remainder (File->CurrentPos, File->Volume->ClusterSize, &Offset);
  236. AlignedPos = (UINT32)File->CurrentPos - (UINT32)Offset;
  237. while
  238. (
  239. !FAT_CLUSTER_FUNCTIONAL (File->CurrentCluster) &&
  240. AlignedPos + File->Volume->ClusterSize <= File->CurrentPos + Pos
  241. )
  242. {
  243. AlignedPos += File->Volume->ClusterSize;
  244. Status = FatGetNextCluster (
  245. PrivateData,
  246. File->Volume,
  247. File->CurrentCluster,
  248. &File->CurrentCluster
  249. );
  250. if (EFI_ERROR (Status)) {
  251. return EFI_DEVICE_ERROR;
  252. }
  253. }
  254. if (FAT_CLUSTER_FUNCTIONAL (File->CurrentCluster)) {
  255. return EFI_INVALID_PARAMETER;
  256. }
  257. File->CurrentPos += Pos;
  258. //
  259. // Calculate the amount of consecutive cluster occupied by the file.
  260. // FatReadFile() will use it to read these blocks once.
  261. //
  262. File->StraightReadAmount = 0;
  263. Cluster = File->CurrentCluster;
  264. while (!FAT_CLUSTER_FUNCTIONAL (Cluster)) {
  265. File->StraightReadAmount += File->Volume->ClusterSize;
  266. PrevCluster = Cluster;
  267. Status = FatGetNextCluster (PrivateData, File->Volume, Cluster, &Cluster);
  268. if (EFI_ERROR (Status)) {
  269. return EFI_DEVICE_ERROR;
  270. }
  271. if (Cluster != PrevCluster + 1) {
  272. break;
  273. }
  274. }
  275. DivU64x32Remainder (File->CurrentPos, File->Volume->ClusterSize, &Offset);
  276. File->StraightReadAmount -= (UINT32)Offset;
  277. }
  278. return EFI_SUCCESS;
  279. }
  280. /**
  281. Reads file data. Updates the file's CurrentPos.
  282. @param PrivateData Global memory map for accessing global variables
  283. @param File The file.
  284. @param Size The amount of data to read.
  285. @param Buffer The buffer storing the data.
  286. @retval EFI_SUCCESS The data is read.
  287. @retval EFI_INVALID_PARAMETER File is invalid.
  288. @retval EFI_DEVICE_ERROR Something error while accessing media.
  289. **/
  290. EFI_STATUS
  291. FatReadFile (
  292. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  293. IN PEI_FAT_FILE *File,
  294. IN UINTN Size,
  295. OUT VOID *Buffer
  296. )
  297. {
  298. EFI_STATUS Status;
  299. CHAR8 *BufferPtr;
  300. UINT32 Offset;
  301. UINT64 PhysicalAddr;
  302. UINTN Amount;
  303. BufferPtr = Buffer;
  304. if (File->IsFixedRootDir) {
  305. //
  306. // This is the fixed root dir in FAT12 and FAT16
  307. //
  308. if (File->CurrentPos + Size > File->Volume->RootEntries * sizeof (FAT_DIRECTORY_ENTRY)) {
  309. return EFI_INVALID_PARAMETER;
  310. }
  311. Status = FatReadDisk (
  312. PrivateData,
  313. File->Volume->BlockDeviceNo,
  314. File->Volume->RootDirPos + File->CurrentPos,
  315. Size,
  316. Buffer
  317. );
  318. File->CurrentPos += (UINT32)Size;
  319. return Status;
  320. } else {
  321. if ((File->Attributes & FAT_ATTR_DIRECTORY) == 0) {
  322. Size = Size < (File->FileSize - File->CurrentPos) ? Size : (File->FileSize - File->CurrentPos);
  323. }
  324. //
  325. // This is a normal cluster based file
  326. //
  327. while (Size != 0) {
  328. DivU64x32Remainder (File->CurrentPos, File->Volume->ClusterSize, &Offset);
  329. PhysicalAddr = File->Volume->FirstClusterPos + MultU64x32 (File->Volume->ClusterSize, File->CurrentCluster - 2);
  330. Amount = File->StraightReadAmount;
  331. Amount = Size > Amount ? Amount : Size;
  332. Status = FatReadDisk (
  333. PrivateData,
  334. File->Volume->BlockDeviceNo,
  335. PhysicalAddr + Offset,
  336. Amount,
  337. BufferPtr
  338. );
  339. if (EFI_ERROR (Status)) {
  340. return EFI_DEVICE_ERROR;
  341. }
  342. //
  343. // Advance the file's current pos and current cluster
  344. //
  345. FatSetFilePos (PrivateData, File, (UINT32)Amount);
  346. BufferPtr += Amount;
  347. Size -= Amount;
  348. }
  349. return EFI_SUCCESS;
  350. }
  351. }
  352. /**
  353. This function reads the next item in the parent directory and
  354. initializes the output parameter SubFile (CurrentPos is initialized to 0).
  355. The function updates the CurrentPos of the parent dir to after the item read.
  356. If no more items were found, the function returns EFI_NOT_FOUND.
  357. @param PrivateData Global memory map for accessing global variables
  358. @param ParentDir The parent directory.
  359. @param SubFile The File structure containing the sub file that
  360. is caught.
  361. @retval EFI_SUCCESS The next sub file is obtained.
  362. @retval EFI_INVALID_PARAMETER The ParentDir is not a directory.
  363. @retval EFI_NOT_FOUND No more sub file exists.
  364. @retval EFI_DEVICE_ERROR Something error while accessing media.
  365. **/
  366. EFI_STATUS
  367. FatReadNextDirectoryEntry (
  368. IN PEI_FAT_PRIVATE_DATA *PrivateData,
  369. IN PEI_FAT_FILE *ParentDir,
  370. OUT PEI_FAT_FILE *SubFile
  371. )
  372. {
  373. EFI_STATUS Status;
  374. FAT_DIRECTORY_ENTRY DirEntry;
  375. CHAR16 *Pos;
  376. CHAR16 BaseName[9];
  377. CHAR16 Ext[4];
  378. ZeroMem ((UINT8 *)SubFile, sizeof (PEI_FAT_FILE));
  379. //
  380. // Pick a valid directory entry
  381. //
  382. while (1) {
  383. //
  384. // Read one entry
  385. //
  386. Status = FatReadFile (PrivateData, ParentDir, 32, &DirEntry);
  387. if (EFI_ERROR (Status)) {
  388. return EFI_DEVICE_ERROR;
  389. }
  390. //
  391. // We only search for *FILE* in root directory
  392. // Long file name entry is *NOT* supported
  393. //
  394. if (((DirEntry.Attributes & FAT_ATTR_DIRECTORY) == FAT_ATTR_DIRECTORY) || (DirEntry.Attributes == FAT_ATTR_LFN)) {
  395. continue;
  396. }
  397. //
  398. // if this is a terminator dir entry, just return EFI_NOT_FOUND
  399. //
  400. if (DirEntry.FileName[0] == EMPTY_ENTRY_MARK) {
  401. return EFI_NOT_FOUND;
  402. }
  403. //
  404. // If this not an invalid entry neither an empty entry, this is what we want.
  405. // otherwise we will start a new loop to continue to find something meaningful
  406. //
  407. if ((UINT8)DirEntry.FileName[0] != DELETE_ENTRY_MARK) {
  408. break;
  409. }
  410. }
  411. //
  412. // fill in the output parameter
  413. //
  414. EngFatToStr (8, DirEntry.FileName, BaseName);
  415. EngFatToStr (3, DirEntry.FileName + 8, Ext);
  416. Pos = (UINT16 *)SubFile->FileName;
  417. SetMem ((UINT8 *)Pos, FAT_MAX_FILE_NAME_LENGTH, 0);
  418. CopyMem ((UINT8 *)Pos, (UINT8 *)BaseName, 2 * (StrLen (BaseName) + 1));
  419. if (Ext[0] != 0) {
  420. Pos += StrLen (BaseName);
  421. *Pos = '.';
  422. Pos++;
  423. CopyMem ((UINT8 *)Pos, (UINT8 *)Ext, 2 * (StrLen (Ext) + 1));
  424. }
  425. SubFile->Attributes = DirEntry.Attributes;
  426. SubFile->CurrentCluster = DirEntry.FileCluster;
  427. if (ParentDir->Volume->FatType == Fat32) {
  428. SubFile->CurrentCluster |= DirEntry.FileClusterHigh << 16;
  429. }
  430. SubFile->CurrentPos = 0;
  431. SubFile->FileSize = DirEntry.FileSize;
  432. SubFile->StartingCluster = SubFile->CurrentCluster;
  433. SubFile->Volume = ParentDir->Volume;
  434. //
  435. // in Pei phase, time parameters do not need to be filled for minimum use.
  436. //
  437. return Status;
  438. }