FileSpace.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*++
  2. Copyright (c) 2005, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials are licensed and made available
  4. under the terms and conditions of the BSD License which accompanies this
  5. distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. FileSpace.c
  11. Abstract:
  12. Routines dealing with disk spaces and FAT table entries
  13. Revision History
  14. --*/
  15. #include "Fat.h"
  16. STATIC
  17. VOID *
  18. FatLoadFatEntry (
  19. IN FAT_VOLUME *Volume,
  20. IN UINTN Index
  21. )
  22. /*++
  23. Routine Description:
  24. Get the FAT entry of the volume, which is identified with the Index.
  25. Arguments:
  26. Volume - FAT file system volume.
  27. Index - The index of the FAT entry of the volume.
  28. Returns:
  29. The buffer of the FAT entry
  30. --*/
  31. {
  32. UINTN Pos;
  33. EFI_STATUS Status;
  34. if (Index > (Volume->MaxCluster + 1)) {
  35. Volume->FatEntryBuffer = (UINT32) -1;
  36. return &Volume->FatEntryBuffer;
  37. }
  38. //
  39. // Compute buffer position needed
  40. //
  41. switch (Volume->FatType) {
  42. case FAT12:
  43. Pos = FAT_POS_FAT12 (Index);
  44. break;
  45. case FAT16:
  46. Pos = FAT_POS_FAT16 (Index);
  47. break;
  48. default:
  49. Pos = FAT_POS_FAT32 (Index);
  50. }
  51. //
  52. // Set the position and read the buffer
  53. //
  54. Volume->FatEntryPos = Volume->FatPos + Pos;
  55. Status = FatDiskIo (
  56. Volume,
  57. READ_FAT,
  58. Volume->FatEntryPos,
  59. Volume->FatEntrySize,
  60. &Volume->FatEntryBuffer
  61. );
  62. if (EFI_ERROR (Status)) {
  63. Volume->FatEntryBuffer = (UINT32) -1;
  64. }
  65. return &Volume->FatEntryBuffer;
  66. }
  67. STATIC
  68. UINTN
  69. FatGetFatEntry (
  70. IN FAT_VOLUME *Volume,
  71. IN UINTN Index
  72. )
  73. /*++
  74. Routine Description:
  75. Get the FAT entry value of the volume, which is identified with the Index.
  76. Arguments:
  77. Volume - FAT file system volume.
  78. Index - The index of the FAT entry of the volume.
  79. Returns:
  80. The value of the FAT entry.
  81. --*/
  82. {
  83. VOID *Pos;
  84. UINT8 *E12;
  85. UINT16 *E16;
  86. UINT32 *E32;
  87. UINTN Accum;
  88. Pos = FatLoadFatEntry (Volume, Index);
  89. if (Index > (Volume->MaxCluster + 1)) {
  90. return (UINTN) -1;
  91. }
  92. switch (Volume->FatType) {
  93. case FAT12:
  94. E12 = Pos;
  95. Accum = E12[0] | (E12[1] << 8);
  96. Accum = FAT_ODD_CLUSTER_FAT12 (Index) ? (Accum >> 4) : (Accum & FAT_CLUSTER_MASK_FAT12);
  97. Accum = Accum | ((Accum >= FAT_CLUSTER_SPECIAL_FAT12) ? FAT_CLUSTER_SPECIAL_EXT : 0);
  98. break;
  99. case FAT16:
  100. E16 = Pos;
  101. Accum = *E16;
  102. Accum = Accum | ((Accum >= FAT_CLUSTER_SPECIAL_FAT16) ? FAT_CLUSTER_SPECIAL_EXT : 0);
  103. break;
  104. default:
  105. E32 = Pos;
  106. Accum = *E32 & FAT_CLUSTER_MASK_FAT32;
  107. Accum = Accum | ((Accum >= FAT_CLUSTER_SPECIAL_FAT32) ? FAT_CLUSTER_SPECIAL_EXT : 0);
  108. }
  109. return Accum;
  110. }
  111. STATIC
  112. EFI_STATUS
  113. FatSetFatEntry (
  114. IN FAT_VOLUME *Volume,
  115. IN UINTN Index,
  116. IN UINTN Value
  117. )
  118. /*++
  119. Routine Description:
  120. Set the FAT entry value of the volume, which is identified with the Index.
  121. Arguments:
  122. Volume - FAT file system volume.
  123. Index - The index of the FAT entry of the volume.
  124. Value - The new value of the FAT entry.
  125. Returns:
  126. EFI_SUCCESS - Set the new FAT entry value sucessfully.
  127. EFI_VOLUME_CORRUPTED - The FAT type of the volume is error.
  128. other - An error occurred when operation the FAT entries.
  129. --*/
  130. {
  131. VOID *Pos;
  132. UINT8 *E12;
  133. UINT16 *E16;
  134. UINT32 *E32;
  135. UINTN Accum;
  136. EFI_STATUS Status;
  137. UINTN OriginalVal;
  138. if (Index < FAT_MIN_CLUSTER) {
  139. return EFI_VOLUME_CORRUPTED;
  140. }
  141. OriginalVal = FatGetFatEntry (Volume, Index);
  142. if (Value == FAT_CLUSTER_FREE && OriginalVal != FAT_CLUSTER_FREE) {
  143. Volume->FatInfoSector.FreeInfo.ClusterCount += 1;
  144. if (Index < Volume->FatInfoSector.FreeInfo.NextCluster) {
  145. Volume->FatInfoSector.FreeInfo.NextCluster = (UINT32) Index;
  146. }
  147. } else if (Value != FAT_CLUSTER_FREE && OriginalVal == FAT_CLUSTER_FREE) {
  148. if (Volume->FatInfoSector.FreeInfo.ClusterCount != 0) {
  149. Volume->FatInfoSector.FreeInfo.ClusterCount -= 1;
  150. }
  151. }
  152. //
  153. // Make sure the entry is in memory
  154. //
  155. Pos = FatLoadFatEntry (Volume, Index);
  156. //
  157. // Update the value
  158. //
  159. switch (Volume->FatType) {
  160. case FAT12:
  161. E12 = Pos;
  162. Accum = E12[0] | (E12[1] << 8);
  163. Value = Value & FAT_CLUSTER_MASK_FAT12;
  164. if (FAT_ODD_CLUSTER_FAT12 (Index)) {
  165. Accum = (Value << 4) | (Accum & 0xF);
  166. } else {
  167. Accum = Value | (Accum & FAT_CLUSTER_UNMASK_FAT12);
  168. }
  169. E12[0] = (UINT8) (Accum & 0xFF);
  170. E12[1] = (UINT8) (Accum >> 8);
  171. break;
  172. case FAT16:
  173. E16 = Pos;
  174. *E16 = (UINT16) Value;
  175. break;
  176. default:
  177. E32 = Pos;
  178. *E32 = (*E32 & FAT_CLUSTER_UNMASK_FAT32) | (UINT32) (Value & FAT_CLUSTER_MASK_FAT32);
  179. }
  180. //
  181. // If the volume's dirty bit is not set, set it now
  182. //
  183. if (!Volume->FatDirty && Volume->FatType != FAT12) {
  184. Volume->FatDirty = TRUE;
  185. FatAccessVolumeDirty (Volume, WRITE_FAT, &Volume->DirtyValue);
  186. }
  187. //
  188. // Write the updated fat entry value to the volume
  189. // The fat is the first fat, and other fat will be in sync
  190. // when the FAT cache flush back.
  191. //
  192. Status = FatDiskIo (
  193. Volume,
  194. WRITE_FAT,
  195. Volume->FatEntryPos,
  196. Volume->FatEntrySize,
  197. &Volume->FatEntryBuffer
  198. );
  199. return Status;
  200. }
  201. STATIC
  202. EFI_STATUS
  203. FatFreeClusters (
  204. IN FAT_VOLUME *Volume,
  205. IN UINTN Cluster
  206. )
  207. /*++
  208. Routine Description:
  209. Free the cluster clain.
  210. Arguments:
  211. Volume - FAT file system volume.
  212. Cluster - The first cluster of cluster chain.
  213. Returns:
  214. EFI_SUCCESS - The cluster chain is freed successfully.
  215. EFI_VOLUME_CORRUPTED - There are errors in the file's clusters.
  216. --*/
  217. {
  218. UINTN LastCluster;
  219. while (!FAT_END_OF_FAT_CHAIN (Cluster)) {
  220. if (Cluster == FAT_CLUSTER_FREE || Cluster >= FAT_CLUSTER_SPECIAL) {
  221. DEBUG ((EFI_D_INIT | EFI_D_ERROR, "FatShrinkEof: cluster chain corrupt\n"));
  222. return EFI_VOLUME_CORRUPTED;
  223. }
  224. LastCluster = Cluster;
  225. Cluster = FatGetFatEntry (Volume, Cluster);
  226. FatSetFatEntry (Volume, LastCluster, FAT_CLUSTER_FREE);
  227. }
  228. return EFI_SUCCESS;
  229. }
  230. STATIC
  231. UINTN
  232. FatAllocateCluster (
  233. IN FAT_VOLUME *Volume
  234. )
  235. /*++
  236. Routine Description:
  237. Allocate a free cluster and return the cluster index.
  238. Arguments:
  239. Volume - FAT file system volume.
  240. Returns:
  241. The index of the free cluster
  242. --*/
  243. {
  244. UINTN Cluster;
  245. //
  246. // Start looking at FatFreePos for the next unallocated cluster
  247. //
  248. if (Volume->DiskError) {
  249. return (UINTN) FAT_CLUSTER_LAST;
  250. }
  251. for (;;) {
  252. //
  253. // If the end of the list, return no available cluster
  254. //
  255. if (Volume->FatInfoSector.FreeInfo.NextCluster > (Volume->MaxCluster + 1)) {
  256. if (Volume->FreeInfoValid && 0 < (INT32) (Volume->FatInfoSector.FreeInfo.ClusterCount)) {
  257. Volume->FreeInfoValid = FALSE;
  258. }
  259. FatComputeFreeInfo (Volume);
  260. if (Volume->FatInfoSector.FreeInfo.NextCluster > (Volume->MaxCluster + 1)) {
  261. return (UINTN) FAT_CLUSTER_LAST;
  262. }
  263. }
  264. Cluster = FatGetFatEntry (Volume, Volume->FatInfoSector.FreeInfo.NextCluster);
  265. if (Cluster == FAT_CLUSTER_FREE) {
  266. break;
  267. }
  268. //
  269. // Try the next cluster
  270. //
  271. Volume->FatInfoSector.FreeInfo.NextCluster += 1;
  272. }
  273. Cluster = Volume->FatInfoSector.FreeInfo.NextCluster;
  274. Volume->FatInfoSector.FreeInfo.NextCluster += 1;
  275. return Cluster;
  276. }
  277. STATIC
  278. UINTN
  279. FatSizeToClusters (
  280. IN FAT_VOLUME *Volume,
  281. IN UINTN Size
  282. )
  283. /*++
  284. Routine Description:
  285. Count the number of clusters given a size
  286. Arguments:
  287. Volume - The file system volume.
  288. Size - The size in bytes.
  289. Returns:
  290. The number of the clusters.
  291. --*/
  292. {
  293. UINTN Clusters;
  294. Clusters = Size >> Volume->ClusterAlignment;
  295. if ((Size & (Volume->ClusterSize - 1)) > 0) {
  296. Clusters += 1;
  297. }
  298. return Clusters;
  299. }
  300. EFI_STATUS
  301. FatShrinkEof (
  302. IN FAT_OFILE *OFile
  303. )
  304. /*++
  305. Routine Description:
  306. Shrink the end of the open file base on the file size.
  307. Arguments:
  308. OFile - The open file.
  309. Returns:
  310. EFI_SUCCESS - Shrinked sucessfully.
  311. EFI_VOLUME_CORRUPTED - There are errors in the file's clusters.
  312. --*/
  313. {
  314. FAT_VOLUME *Volume;
  315. UINTN NewSize;
  316. UINTN CurSize;
  317. UINTN Cluster;
  318. UINTN LastCluster;
  319. Volume = OFile->Volume;
  320. ASSERT_VOLUME_LOCKED (Volume);
  321. NewSize = FatSizeToClusters (Volume, OFile->FileSize);
  322. //
  323. // Find the address of the last cluster
  324. //
  325. Cluster = OFile->FileCluster;
  326. LastCluster = FAT_CLUSTER_FREE;
  327. if (NewSize != 0) {
  328. for (CurSize = 0; CurSize < NewSize; CurSize++) {
  329. if (Cluster == FAT_CLUSTER_FREE || Cluster >= FAT_CLUSTER_SPECIAL) {
  330. DEBUG ((EFI_D_INIT | EFI_D_ERROR, "FatShrinkEof: cluster chain corrupt\n"));
  331. return EFI_VOLUME_CORRUPTED;
  332. }
  333. LastCluster = Cluster;
  334. Cluster = FatGetFatEntry (Volume, Cluster);
  335. }
  336. FatSetFatEntry (Volume, LastCluster, (UINTN) FAT_CLUSTER_LAST);
  337. } else {
  338. //
  339. // Check to see if the file is already completely truncated
  340. //
  341. if (Cluster == FAT_CLUSTER_FREE) {
  342. return EFI_SUCCESS;
  343. }
  344. //
  345. // The file is being completely truncated.
  346. //
  347. OFile->FileCluster = FAT_CLUSTER_FREE;
  348. }
  349. //
  350. // Set CurrentCluster == FileCluster
  351. // to force a recalculation of Position related stuffs
  352. //
  353. OFile->FileCurrentCluster = OFile->FileCluster;
  354. OFile->FileLastCluster = LastCluster;
  355. OFile->Dirty = TRUE;
  356. //
  357. // Free the remaining cluster chain
  358. //
  359. return FatFreeClusters (Volume, Cluster);
  360. }
  361. EFI_STATUS
  362. FatGrowEof (
  363. IN FAT_OFILE *OFile,
  364. IN UINT64 NewSizeInBytes
  365. )
  366. /*++
  367. Routine Description:
  368. Grow the end of the open file base on the NewSizeInBytes.
  369. Arguments:
  370. OFile - The open file.
  371. NewSizeInBytes - The new size in bytes of the open file.
  372. Returns:
  373. EFI_SUCCESS - The file is grown sucessfully.
  374. EFI_UNSUPPORTED - The file size is larger than 4GB.
  375. EFI_VOLUME_CORRUPTED - There are errors in the files' clusters.
  376. EFI_VOLUME_FULL - The volume is full and can not grow the file.
  377. --*/
  378. {
  379. FAT_VOLUME *Volume;
  380. EFI_STATUS Status;
  381. UINTN Cluster;
  382. UINTN CurSize;
  383. UINTN NewSize;
  384. UINTN LastCluster;
  385. UINTN NewCluster;
  386. UINTN ClusterCount;
  387. //
  388. // For FAT file system, the max file is 4GB.
  389. //
  390. if (NewSizeInBytes > 0x0FFFFFFFFL) {
  391. return EFI_UNSUPPORTED;
  392. }
  393. Volume = OFile->Volume;
  394. ASSERT_VOLUME_LOCKED (Volume);
  395. //
  396. // If the file is already large enough, do nothing
  397. //
  398. CurSize = FatSizeToClusters (Volume, OFile->FileSize);
  399. NewSize = FatSizeToClusters (Volume, (UINTN) NewSizeInBytes);
  400. if (CurSize < NewSize) {
  401. //
  402. // If we haven't found the files last cluster do it now
  403. //
  404. if ((OFile->FileCluster != 0) && (OFile->FileLastCluster == 0)) {
  405. Cluster = OFile->FileCluster;
  406. ClusterCount = 0;
  407. while (!FAT_END_OF_FAT_CHAIN (Cluster)) {
  408. if (Cluster == FAT_CLUSTER_FREE || Cluster >= FAT_CLUSTER_SPECIAL) {
  409. DEBUG (
  410. (EFI_D_INIT | EFI_D_ERROR,
  411. "FatGrowEof: cluster chain corrupt\n")
  412. );
  413. Status = EFI_VOLUME_CORRUPTED;
  414. goto Done;
  415. }
  416. ClusterCount++;
  417. OFile->FileLastCluster = Cluster;
  418. Cluster = FatGetFatEntry (Volume, Cluster);
  419. }
  420. if (ClusterCount != CurSize) {
  421. DEBUG (
  422. (EFI_D_INIT | EFI_D_ERROR,
  423. "FatGrowEof: cluster chain size does not match file size\n")
  424. );
  425. Status = EFI_VOLUME_CORRUPTED;
  426. goto Done;
  427. }
  428. }
  429. //
  430. // Loop until we've allocated enough space
  431. //
  432. LastCluster = OFile->FileLastCluster;
  433. while (CurSize < NewSize) {
  434. NewCluster = FatAllocateCluster (Volume);
  435. if (FAT_END_OF_FAT_CHAIN (NewCluster)) {
  436. if (LastCluster != FAT_CLUSTER_FREE) {
  437. FatSetFatEntry (Volume, LastCluster, (UINTN) FAT_CLUSTER_LAST);
  438. OFile->FileLastCluster = LastCluster;
  439. }
  440. Status = EFI_VOLUME_FULL;
  441. goto Done;
  442. }
  443. if (LastCluster != 0) {
  444. FatSetFatEntry (Volume, LastCluster, NewCluster);
  445. } else {
  446. OFile->FileCluster = NewCluster;
  447. OFile->FileCurrentCluster = NewCluster;
  448. }
  449. LastCluster = NewCluster;
  450. CurSize += 1;
  451. }
  452. //
  453. // Terminate the cluster list
  454. //
  455. FatSetFatEntry (Volume, LastCluster, (UINTN) FAT_CLUSTER_LAST);
  456. OFile->FileLastCluster = LastCluster;
  457. }
  458. OFile->FileSize = (UINTN) NewSizeInBytes;
  459. OFile->Dirty = TRUE;
  460. return EFI_SUCCESS;
  461. Done:
  462. FatShrinkEof (OFile);
  463. return Status;
  464. }
  465. EFI_STATUS
  466. FatOFilePosition (
  467. IN FAT_OFILE *OFile,
  468. IN UINTN Position,
  469. IN UINTN PosLimit
  470. )
  471. /*++
  472. Routine Description:
  473. Seek OFile to requested position, and calculate the number of
  474. consecutive clusters from the position in the file
  475. Arguments:
  476. OFile - The open file.
  477. Position - The file's position which will be accessed.
  478. PosLimit - The maximum length current reading/writing may access
  479. Returns:
  480. EFI_SUCCESS - Set the info successfully.
  481. EFI_VOLUME_CORRUPTED - Cluster chain corrupt.
  482. --*/
  483. {
  484. FAT_VOLUME *Volume;
  485. UINTN ClusterSize;
  486. UINTN Cluster;
  487. UINTN StartPos;
  488. UINTN Run;
  489. Volume = OFile->Volume;
  490. ClusterSize = Volume->ClusterSize;
  491. ASSERT_VOLUME_LOCKED (Volume);
  492. //
  493. // If this is the fixed root dir, then compute it's position
  494. // from it's fixed info in the fat bpb
  495. //
  496. if (OFile->IsFixedRootDir) {
  497. OFile->PosDisk = Volume->RootPos + Position;
  498. Run = OFile->FileSize - Position;
  499. } else {
  500. //
  501. // Run the file's cluster chain to find the current position
  502. // If possible, run from the current cluster rather than
  503. // start from beginning
  504. // Assumption: OFile->Position is always consistent with
  505. // OFile->FileCurrentCluster.
  506. // OFile->Position is not modified outside this function;
  507. // OFile->FileCurrentCluster is modified outside this function
  508. // to be the same as OFile->FileCluster
  509. // when OFile->FileCluster is updated, so make a check of this
  510. // and invalidate the original OFile->Position in this case
  511. //
  512. Cluster = OFile->FileCurrentCluster;
  513. StartPos = OFile->Position;
  514. if (Position < StartPos || OFile->FileCluster == Cluster) {
  515. StartPos = 0;
  516. Cluster = OFile->FileCluster;
  517. }
  518. while (StartPos + ClusterSize <= Position) {
  519. StartPos += ClusterSize;
  520. if (Cluster == FAT_CLUSTER_FREE || (Cluster >= FAT_CLUSTER_SPECIAL)) {
  521. DEBUG ((EFI_D_INIT | EFI_D_ERROR, "FatOFilePosition:"" cluster chain corrupt\n"));
  522. return EFI_VOLUME_CORRUPTED;
  523. }
  524. Cluster = FatGetFatEntry (Volume, Cluster);
  525. }
  526. if (Cluster < FAT_MIN_CLUSTER) {
  527. return EFI_VOLUME_CORRUPTED;
  528. }
  529. OFile->PosDisk = Volume->FirstClusterPos +
  530. LShiftU64 (Cluster - FAT_MIN_CLUSTER, Volume->ClusterAlignment) +
  531. Position - StartPos;
  532. OFile->FileCurrentCluster = Cluster;
  533. OFile->Position = StartPos;
  534. //
  535. // Compute the number of consecutive clusters in the file
  536. //
  537. Run = StartPos + ClusterSize - Position;
  538. if (!FAT_END_OF_FAT_CHAIN (Cluster)) {
  539. while ((FatGetFatEntry (Volume, Cluster) == Cluster + 1) && Run < PosLimit) {
  540. Run += ClusterSize;
  541. Cluster += 1;
  542. }
  543. }
  544. }
  545. OFile->PosRem = Run;
  546. return EFI_SUCCESS;
  547. }
  548. UINTN
  549. FatPhysicalDirSize (
  550. IN FAT_VOLUME *Volume,
  551. IN UINTN Cluster
  552. )
  553. /*++
  554. Routine Description:
  555. Get the size of directory of the open file
  556. Arguments:
  557. Volume - The File System Volume.
  558. Cluster - The Starting cluster.
  559. Returns:
  560. The physical size of the file starting at the input cluster, if there is error in the
  561. cluster chain, the return value is 0.
  562. --*/
  563. {
  564. UINTN Size;
  565. ASSERT_VOLUME_LOCKED (Volume);
  566. //
  567. // Run the cluster chain for the OFile
  568. //
  569. Size = 0;
  570. //
  571. // N.B. ".." directories on some media do not contain a starting
  572. // cluster. In the case of "." or ".." we don't need the size anyway.
  573. //
  574. if (Cluster != 0) {
  575. while (!FAT_END_OF_FAT_CHAIN (Cluster)) {
  576. if (Cluster == FAT_CLUSTER_FREE || Cluster >= FAT_CLUSTER_SPECIAL) {
  577. DEBUG (
  578. (EFI_D_INIT | EFI_D_ERROR,
  579. "FATDirSize: cluster chain corrupt\n")
  580. );
  581. return 0;
  582. }
  583. Size += Volume->ClusterSize;
  584. Cluster = FatGetFatEntry (Volume, Cluster);
  585. }
  586. }
  587. return Size;
  588. }
  589. UINT64
  590. FatPhysicalFileSize (
  591. IN FAT_VOLUME *Volume,
  592. IN UINTN RealSize
  593. )
  594. /*++
  595. Routine Description:
  596. Get the physical size of a file on the disk.
  597. Arguments:
  598. Volume - The file system volume.
  599. RealSize - The real size of a file.
  600. Returns:
  601. The physical size of a file on the disk.
  602. --*/
  603. {
  604. UINTN ClusterSizeMask;
  605. UINT64 PhysicalSize;
  606. ClusterSizeMask = Volume->ClusterSize - 1;
  607. PhysicalSize = (RealSize + ClusterSizeMask) & (~((UINT64) ClusterSizeMask));
  608. return PhysicalSize;
  609. }
  610. VOID
  611. FatComputeFreeInfo (
  612. IN FAT_VOLUME *Volume
  613. )
  614. /*++
  615. Routine Description:
  616. Update the free cluster info of FatInfoSector of the volume.
  617. Arguments:
  618. Volume - FAT file system volume.
  619. Returns:
  620. None.
  621. --*/
  622. {
  623. UINTN Index;
  624. //
  625. // If we don't have valid info, compute it now
  626. //
  627. if (!Volume->FreeInfoValid) {
  628. Volume->FreeInfoValid = TRUE;
  629. Volume->FatInfoSector.FreeInfo.ClusterCount = 0;
  630. for (Index = Volume->MaxCluster + 1; Index >= FAT_MIN_CLUSTER; Index--) {
  631. if (Volume->DiskError) {
  632. break;
  633. }
  634. if (FatGetFatEntry (Volume, Index) == FAT_CLUSTER_FREE) {
  635. Volume->FatInfoSector.FreeInfo.ClusterCount += 1;
  636. Volume->FatInfoSector.FreeInfo.NextCluster = (UINT32) Index;
  637. }
  638. }
  639. Volume->FatInfoSector.Signature = FAT_INFO_SIGNATURE;
  640. Volume->FatInfoSector.InfoBeginSignature = FAT_INFO_BEGIN_SIGNATURE;
  641. Volume->FatInfoSector.InfoEndSignature = FAT_INFO_END_SIGNATURE;
  642. }
  643. }