FatLiteAccess.c 15 KB

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