Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3969 lines
103 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. fatboot.c
  5. Abstract:
  6. This module implements the FAT boot file system used by the operating
  7. system loader.
  8. Author:
  9. Gary Kimura (garyki) 29-Aug-1989
  10. Revision History:
  11. --*/
  12. #include "bootlib.h"
  13. #include "stdio.h"
  14. #include "blcache.h"
  15. BOOTFS_INFO FatBootFsInfo={L"fastfat"};
  16. //
  17. // Conditional debug print routine
  18. //
  19. #ifdef FATBOOTDBG
  20. #define FatDebugOutput(X,Y,Z) { \
  21. if (BlConsoleOutDeviceId) { \
  22. CHAR _b[128]; \
  23. ULONG _c; \
  24. sprintf(&_b[0], X, Y, Z); \
  25. ArcWrite(BlConsoleOutDeviceId, &_b[0], strlen(&_b[0]), &_c); \
  26. } \
  27. }
  28. #define CharOrSpace(C) ((C) < 0x20 ? 0x20: (C))
  29. #define FatDebugOutput83(X,N,Y,Z) { \
  30. if (BlConsoleOutDeviceId) { \
  31. CHAR _b[128]; \
  32. CHAR _n[13]; \
  33. ULONG _c; \
  34. sprintf(&_n[0], "> %c%c%c%c%c%c%c%c.%c%c%c <", \
  35. CharOrSpace(*((PCHAR)N +0)), \
  36. CharOrSpace(*((PCHAR)N +1)), \
  37. CharOrSpace(*((PCHAR)N +2)), \
  38. CharOrSpace(*((PCHAR)N +3)), \
  39. CharOrSpace(*((PCHAR)N +4)), \
  40. CharOrSpace(*((PCHAR)N +5)), \
  41. CharOrSpace(*((PCHAR)N +6)), \
  42. CharOrSpace(*((PCHAR)N +7)), \
  43. CharOrSpace(*((PCHAR)N +8)), \
  44. CharOrSpace(*((PCHAR)N +9)), \
  45. CharOrSpace(*((PCHAR)N +10))); \
  46. sprintf(&_b[0], X, _n, Y, Z); \
  47. ArcWrite(BlConsoleOutDeviceId, &_b[0], strlen(&_b[0]), &_c); \
  48. } \
  49. }
  50. #else
  51. #define FatDebugOutput(X,Y,Z) {NOTHING;}
  52. #define FatDebugOutput83(X,N,Y,Z) {NOTHING;}
  53. #endif // FATBOOTDBG
  54. //
  55. // Low level disk I/O procedure prototypes
  56. //
  57. ARC_STATUS
  58. FatDiskRead (
  59. IN ULONG DeviceId,
  60. IN LBO Lbo,
  61. IN ULONG ByteCount,
  62. IN PVOID Buffer,
  63. IN BOOLEAN CacheNewData
  64. );
  65. ARC_STATUS
  66. FatDiskWrite (
  67. IN ULONG DeviceId,
  68. IN LBO Lbo,
  69. IN ULONG ByteCount,
  70. IN PVOID Buffer
  71. );
  72. //
  73. // VOID
  74. // DiskRead (
  75. // IN ULONG DeviceId,
  76. // IN LBO Lbo,
  77. // IN ULONG ByteCount,
  78. // IN PVOID Buffer,
  79. // IN BOOLEAN CacheNewData,
  80. // IN BOOLEAN IsDoubleSpace
  81. // );
  82. //
  83. #define DiskRead(A,B,C,D,E,ignored) { ARC_STATUS _s; \
  84. if ((_s = FatDiskRead(A,B,C,D,E)) != ESUCCESS) { return _s; } \
  85. }
  86. #define DiskWrite(A,B,C,D) { ARC_STATUS _s; \
  87. if ((_s = FatDiskWrite(A,B,C,D)) != ESUCCESS) { return _s; } \
  88. }
  89. //
  90. // Cluster/Index routines
  91. //
  92. typedef enum _CLUSTER_TYPE {
  93. FatClusterAvailable,
  94. FatClusterReserved,
  95. FatClusterBad,
  96. FatClusterLast,
  97. FatClusterNext
  98. } CLUSTER_TYPE;
  99. CLUSTER_TYPE
  100. FatInterpretClusterType (
  101. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  102. IN FAT_ENTRY Entry
  103. );
  104. ARC_STATUS
  105. FatLookupFatEntry (
  106. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  107. IN ULONG DeviceId,
  108. IN ULONG FatIndex,
  109. OUT PULONG FatEntry,
  110. IN BOOLEAN IsDoubleSpace
  111. );
  112. ARC_STATUS
  113. FatSetFatEntry (
  114. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  115. IN ULONG DeviceId,
  116. IN FAT_ENTRY FatIndex,
  117. IN FAT_ENTRY FatEntry
  118. );
  119. ARC_STATUS
  120. FatFlushFatEntries (
  121. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  122. IN ULONG DeviceId
  123. );
  124. LBO
  125. FatIndexToLbo (
  126. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  127. IN FAT_ENTRY FatIndex
  128. );
  129. #define LookupFatEntry(A,B,C,D,E) { ARC_STATUS _s; \
  130. if ((_s = FatLookupFatEntry(A,B,C,D,E)) != ESUCCESS) { return _s; } \
  131. }
  132. #define SetFatEntry(A,B,C,D) { ARC_STATUS _s; \
  133. if ((_s = FatSetFatEntry(A,B,C,D)) != ESUCCESS) { return _s; } \
  134. }
  135. #define FlushFatEntries(A,B) { ARC_STATUS _s; \
  136. if ((_s = FatFlushFatEntries(A,B)) != ESUCCESS) { return _s; } \
  137. }
  138. //
  139. // Directory routines
  140. //
  141. ARC_STATUS
  142. FatSearchForDirent (
  143. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  144. IN ULONG DeviceId,
  145. IN FAT_ENTRY DirectoriesStartingIndex,
  146. IN PFAT8DOT3 FileName,
  147. OUT PDIRENT Dirent,
  148. OUT PLBO Lbo,
  149. IN BOOLEAN IsDoubleSpace
  150. );
  151. ARC_STATUS
  152. FatCreateDirent (
  153. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  154. IN ULONG DeviceId,
  155. IN FAT_ENTRY DirectoriesStartingIndex,
  156. IN PDIRENT Dirent,
  157. OUT PLBO Lbo
  158. );
  159. VOID
  160. FatSetDirent (
  161. IN PFAT8DOT3 FileName,
  162. IN OUT PDIRENT Dirent,
  163. IN UCHAR Attributes
  164. );
  165. #define SearchForDirent(A,B,C,D,E,F,G) { ARC_STATUS _s; \
  166. if ((_s = FatSearchForDirent(A,B,C,D,E,F,G)) != ESUCCESS) { return _s; } \
  167. }
  168. #define CreateDirent(A,B,C,D,E) { ARC_STATUS _s; \
  169. if ((_s = FatCreateDirent(A,B,C,D,E)) != ESUCCESS) { return _s; } \
  170. }
  171. //
  172. // Allocation and mcb routines
  173. //
  174. ARC_STATUS
  175. FatLoadMcb (
  176. IN ULONG FileId,
  177. IN VBO StartingVbo,
  178. IN BOOLEAN IsDoubleSpace
  179. );
  180. ARC_STATUS
  181. FatVboToLbo (
  182. IN ULONG FileId,
  183. IN VBO Vbo,
  184. OUT PLBO Lbo,
  185. OUT PULONG ByteCount,
  186. IN BOOLEAN IsDoubleSpace
  187. );
  188. ARC_STATUS
  189. FatIncreaseFileAllocation (
  190. IN ULONG FileId,
  191. IN ULONG ByteSize
  192. );
  193. ARC_STATUS
  194. FatTruncateFileAllocation (
  195. IN ULONG FileId,
  196. IN ULONG ByteSize
  197. );
  198. ARC_STATUS
  199. FatAllocateClusters (
  200. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  201. IN ULONG DeviceId,
  202. IN ULONG ClusterCount,
  203. IN ULONG Hint,
  204. OUT PULONG AllocatedEntry
  205. );
  206. #define LoadMcb(A,B,C) { ARC_STATUS _s; \
  207. if ((_s = FatLoadMcb(A,B,C)) != ESUCCESS) { return _s; } \
  208. }
  209. #define VboToLbo(A,B,C,D) { ARC_STATUS _s; \
  210. if ((_s = FatVboToLbo(A,B,C,D,FALSE)) != ESUCCESS) { return _s; } \
  211. }
  212. #define IncreaseFileAllocation(A,B) { ARC_STATUS _s; \
  213. if ((_s = FatIncreaseFileAllocation(A,B)) != ESUCCESS) { return _s; } \
  214. }
  215. #define TruncateFileAllocation(A,B) { ARC_STATUS _s; \
  216. if ((_s = FatTruncateFileAllocation(A,B)) != ESUCCESS) { return _s; } \
  217. }
  218. #define AllocateClusters(A,B,C,D,E) { ARC_STATUS _s; \
  219. if ((_s = FatAllocateClusters(A,B,C,D,E)) != ESUCCESS) { return _s; } \
  220. }
  221. //
  222. // Miscellaneous routines
  223. //
  224. VOID
  225. FatFirstComponent (
  226. IN OUT PSTRING String,
  227. OUT PFAT8DOT3 FirstComponent
  228. );
  229. #define AreNamesEqual(X,Y) ( \
  230. ((*(X))[0]==(*(Y))[0]) && ((*(X))[1]==(*(Y))[1]) && ((*(X))[2]==(*(Y))[2]) && \
  231. ((*(X))[3]==(*(Y))[3]) && ((*(X))[4]==(*(Y))[4]) && ((*(X))[5]==(*(Y))[5]) && \
  232. ((*(X))[6]==(*(Y))[6]) && ((*(X))[7]==(*(Y))[7]) && ((*(X))[8]==(*(Y))[8]) && \
  233. ((*(X))[9]==(*(Y))[9]) && ((*(X))[10]==(*(Y))[10]) \
  234. )
  235. #define ToUpper(C) ((((C) >= 'a') && ((C) <= 'z')) ? (C) - 'a' + 'A' : (C))
  236. #define FlagOn(Flags,SingleFlag) ((Flags) & (SingleFlag))
  237. #define BooleanFlagOn(Flags,SingleFlag) ((BOOLEAN)(((Flags) & (SingleFlag)) != 0))
  238. #define SetFlag(Flags,SingleFlag) { (Flags) |= (SingleFlag); }
  239. #define ClearFlag(Flags,SingleFlag) { (Flags) &= ~(SingleFlag); }
  240. #define FatFirstFatAreaLbo(B) ( (B)->ReservedSectors * (B)->BytesPerSector )
  241. #define Minimum(X,Y) ((X) < (Y) ? (X) : (Y))
  242. #define Maximum(X,Y) ((X) < (Y) ? (Y) : (X))
  243. //
  244. // The following types and macros are used to help unpack the packed and
  245. // misaligned fields found in the Bios parameter block
  246. //
  247. typedef union _UCHAR1 { UCHAR Uchar[1]; UCHAR ForceAlignment; } UCHAR1, *PUCHAR1;
  248. typedef union _UCHAR2 { UCHAR Uchar[2]; USHORT ForceAlignment; } UCHAR2, *PUCHAR2;
  249. typedef union _UCHAR4 { UCHAR Uchar[4]; ULONG ForceAlignment; } UCHAR4, *PUCHAR4;
  250. //
  251. // This macro copies an unaligned src byte to an aligned dst byte
  252. //
  253. #define CopyUchar1(Dst,Src) { \
  254. *((UCHAR1 *)(Dst)) = *((UNALIGNED UCHAR1 *)(Src)); \
  255. }
  256. //
  257. // This macro copies an unaligned src word to an aligned dst word
  258. //
  259. #define CopyUchar2(Dst,Src) { \
  260. *((UCHAR2 *)(Dst)) = *((UNALIGNED UCHAR2 *)(Src)); \
  261. }
  262. //
  263. // This macro copies an unaligned src longword to an aligned dsr longword
  264. //
  265. #define CopyUchar4(Dst,Src) { \
  266. *((UCHAR4 *)(Dst)) = *((UNALIGNED UCHAR4 *)(Src)); \
  267. }
  268. //
  269. // DirectoryEntry routines
  270. //
  271. VOID
  272. FatDirToArcDir (
  273. IN PDIRENT FatDirent,
  274. OUT PDIRECTORY_ENTRY ArcDirent
  275. );
  276. //
  277. // Define global data.
  278. //
  279. //
  280. // File entry table - This is a structure that provides entry to the FAT
  281. // file system procedures. It is exported when a FAT file structure
  282. // is recognized.
  283. //
  284. BL_DEVICE_ENTRY_TABLE FatDeviceEntryTable;
  285. PBL_DEVICE_ENTRY_TABLE
  286. IsFatFileStructure (
  287. IN ULONG DeviceId,
  288. IN PVOID StructureContext
  289. )
  290. /*++
  291. Routine Description:
  292. This routine determines if the partition on the specified channel
  293. contains a FAT file system volume.
  294. Arguments:
  295. DeviceId - Supplies the file table index for the device on which
  296. read operations are to be performed.
  297. StructureContext - Supplies a pointer to a FAT file structure context.
  298. Return Value:
  299. A pointer to the FAT entry table is returned if the partition is
  300. recognized as containing a FAT volume. Otherwise, NULL is returned.
  301. --*/
  302. {
  303. PPACKED_BOOT_SECTOR BootSector;
  304. UCHAR Buffer[sizeof(PACKED_BOOT_SECTOR)+256];
  305. PFAT_STRUCTURE_CONTEXT FatStructureContext;
  306. FatDebugOutput("IsFatFileStructure\r\n", 0, 0);
  307. //
  308. // Clear the file system context block for the specified channel and
  309. // establish a pointer to the context structure that can be used by other
  310. // routines
  311. //
  312. FatStructureContext = (PFAT_STRUCTURE_CONTEXT)StructureContext;
  313. RtlZeroMemory(FatStructureContext, sizeof(FAT_STRUCTURE_CONTEXT));
  314. //
  315. // Setup and read in the boot sector for the potential fat partition
  316. //
  317. BootSector = (PPACKED_BOOT_SECTOR)ALIGN_BUFFER( &Buffer[0] );
  318. if (FatDiskRead(DeviceId, 0, sizeof(PACKED_BOOT_SECTOR), BootSector, CACHE_NEW_DATA) != ESUCCESS) {
  319. return NULL;
  320. }
  321. //
  322. // Unpack the Bios parameter block
  323. //
  324. FatUnpackBios(&FatStructureContext->Bpb, &BootSector->PackedBpb);
  325. //
  326. // Check if it is fat
  327. //
  328. if ((BootSector->Jump[0] != 0xeb) &&
  329. (BootSector->Jump[0] != 0xe9)) {
  330. return NULL;
  331. } else if ((FatStructureContext->Bpb.BytesPerSector != 128) &&
  332. (FatStructureContext->Bpb.BytesPerSector != 256) &&
  333. (FatStructureContext->Bpb.BytesPerSector != 512) &&
  334. (FatStructureContext->Bpb.BytesPerSector != 1024)) {
  335. return NULL;
  336. } else if ((FatStructureContext->Bpb.SectorsPerCluster != 1) &&
  337. (FatStructureContext->Bpb.SectorsPerCluster != 2) &&
  338. (FatStructureContext->Bpb.SectorsPerCluster != 4) &&
  339. (FatStructureContext->Bpb.SectorsPerCluster != 8) &&
  340. (FatStructureContext->Bpb.SectorsPerCluster != 16) &&
  341. (FatStructureContext->Bpb.SectorsPerCluster != 32) &&
  342. (FatStructureContext->Bpb.SectorsPerCluster != 64) &&
  343. (FatStructureContext->Bpb.SectorsPerCluster != 128)) {
  344. return NULL;
  345. } else if (FatStructureContext->Bpb.ReservedSectors == 0) {
  346. return NULL;
  347. } else if (((FatStructureContext->Bpb.Sectors == 0) && (FatStructureContext->Bpb.LargeSectors == 0)) ||
  348. ((FatStructureContext->Bpb.Sectors != 0) && (FatStructureContext->Bpb.LargeSectors != 0))) {
  349. return NULL;
  350. } else if (FatStructureContext->Bpb.Fats == 0) {
  351. return NULL;
  352. } else if ((FatStructureContext->Bpb.Media != 0xf0) &&
  353. (FatStructureContext->Bpb.Media != 0xf8) &&
  354. (FatStructureContext->Bpb.Media != 0xf9) &&
  355. (FatStructureContext->Bpb.Media != 0xfc) &&
  356. (FatStructureContext->Bpb.Media != 0xfd) &&
  357. (FatStructureContext->Bpb.Media != 0xfe) &&
  358. (FatStructureContext->Bpb.Media != 0xff)) {
  359. return NULL;
  360. } else if (FatStructureContext->Bpb.SectorsPerFat == 0) {
  361. if (!IsBpbFat32(&BootSector->PackedBpb)) {
  362. return NULL;
  363. }
  364. } else if (FatStructureContext->Bpb.RootEntries == 0) {
  365. return NULL;
  366. }
  367. //
  368. // Initialize the file entry table and return the address of the table.
  369. //
  370. FatDeviceEntryTable.Open = FatOpen;
  371. FatDeviceEntryTable.Close = FatClose;
  372. FatDeviceEntryTable.Read = FatRead;
  373. FatDeviceEntryTable.Seek = FatSeek;
  374. FatDeviceEntryTable.Write = FatWrite;
  375. FatDeviceEntryTable.GetFileInformation = FatGetFileInformation;
  376. FatDeviceEntryTable.SetFileInformation = FatSetFileInformation;
  377. FatDeviceEntryTable.Rename = FatRename;
  378. FatDeviceEntryTable.GetDirectoryEntry = FatGetDirectoryEntry;
  379. FatDeviceEntryTable.BootFsInfo = &FatBootFsInfo;
  380. return &FatDeviceEntryTable;
  381. }
  382. ARC_STATUS
  383. FatClose (
  384. IN ULONG FileId
  385. )
  386. /*++
  387. Routine Description:
  388. This routine closes the file specified by the file id.
  389. Arguments:
  390. FileId - Supplies the file table index.
  391. Return Value:
  392. ESUCCESS if returned as the function value.
  393. --*/
  394. {
  395. PBL_FILE_TABLE FileTableEntry;
  396. PFAT_STRUCTURE_CONTEXT FatStructureContext;
  397. ULONG DeviceId;
  398. FatDebugOutput("FatClose\r\n", 0, 0);
  399. //
  400. // Load our local variables
  401. //
  402. FileTableEntry = &BlFileTable[FileId];
  403. FatStructureContext = (PFAT_STRUCTURE_CONTEXT)FileTableEntry->StructureContext;
  404. DeviceId = FileTableEntry->DeviceId;
  405. //
  406. // Mark the file closed
  407. //
  408. BlFileTable[FileId].Flags.Open = 0;
  409. //
  410. // Check if the fat is dirty and flush it out if it is.
  411. //
  412. if (FatStructureContext->CachedFatDirty) {
  413. FlushFatEntries( FatStructureContext, DeviceId );
  414. }
  415. //
  416. // Check if the current mcb is for this file and if it is then zero it out.
  417. // By setting the file id for the mcb to be the table size we guarantee that
  418. // we've just set it to an invalid file id.
  419. //
  420. if (FatStructureContext->FileId == FileId) {
  421. FatStructureContext->FileId = BL_FILE_TABLE_SIZE;
  422. FatStructureContext->Mcb.InUse = 0;
  423. }
  424. return ESUCCESS;
  425. }
  426. ARC_STATUS
  427. FatGetDirectoryEntry (
  428. IN ULONG FileId,
  429. IN DIRECTORY_ENTRY * FIRMWARE_PTR DirEntry,
  430. IN ULONG NumberDir,
  431. OUT ULONG * FIRMWARE_PTR CountDir
  432. )
  433. /*++
  434. Routine Description:
  435. This routine implements the GetDirectoryEntry operation for the
  436. FAT file system.
  437. Arguments:
  438. FileId - Supplies the file table index.
  439. DirEntry - Supplies a pointer to a directory entry structure.
  440. NumberDir - Supplies the number of directory entries to read.
  441. Count - Supplies a pointer to a variable to receive the number
  442. of entries read.
  443. Return Value:
  444. ESUCCESS is returned if the read was successful, otherwise
  445. an error code is returned.
  446. --*/
  447. {
  448. //
  449. // define local variables
  450. //
  451. ARC_STATUS Status; // ARC status
  452. ULONG Position; // file position
  453. PFAT_FILE_CONTEXT pContext; // FAT file context
  454. ULONG RunByteCount = 0; // max sequential bytes
  455. ULONG RunDirCount; // max dir entries to read per time
  456. ULONG i; // general index
  457. PDIRENT FatDirEnt; // directory entry pointer
  458. UCHAR Buffer[ 16 * sizeof(DIRENT) + 32 ];
  459. LBO Lbo = 0;
  460. BOOLEAN EofDir = FALSE; // not end of file
  461. //
  462. // initialize local variables
  463. //
  464. pContext = &BlFileTable[ FileId ].u.FatFileContext;
  465. FatDirEnt = (PDIRENT)ALIGN_BUFFER( &Buffer[0] );
  466. //
  467. // if not directory entry, exit with error
  468. //
  469. if ( !FlagOn(pContext->Dirent.Attributes, FAT_DIRENT_ATTR_DIRECTORY) ) {
  470. return EBADF;
  471. }
  472. //
  473. // Initialize the output count to zero
  474. //
  475. *CountDir = 0;
  476. //
  477. // if NumberDir is zero, return ESUCCESS.
  478. //
  479. if ( !NumberDir ) {
  480. return ESUCCESS;
  481. }
  482. //
  483. // read one directory at a time.
  484. //
  485. do {
  486. //
  487. // save position
  488. //
  489. Position = BlFileTable[ FileId ].Position.LowPart;
  490. //
  491. // Lookup the corresponding Lbo and run length for the current position
  492. //
  493. if ( !RunByteCount ) {
  494. if ((Status = FatVboToLbo( FileId, Position, &Lbo, &RunByteCount, FALSE )) != 0) {
  495. if ( Status == EINVAL ) {
  496. break; // eof has been reached
  497. } else {
  498. return Status; // I/O error
  499. }
  500. }
  501. }
  502. //
  503. // validate the # of bytes readable in sequance (exit loop if eof)
  504. // the block is always multiple of a directory entry size.
  505. //
  506. if ( (RunDirCount = Minimum( RunByteCount/sizeof(DIRENT), 16)) == 0 ) {
  507. break;
  508. }
  509. //
  510. // issue the read
  511. //
  512. if ( (Status = FatDiskRead( BlFileTable[ FileId ].DeviceId,
  513. Lbo,
  514. RunDirCount * sizeof(DIRENT),
  515. (PVOID)FatDirEnt,
  516. CACHE_NEW_DATA)) != 0 ) {
  517. BlFileTable[ FileId ].Position.LowPart = Position;
  518. return Status;
  519. }
  520. for ( i=0; i<RunDirCount; i++ ) {
  521. //
  522. // exit from loop if logical end of directory
  523. //
  524. if ( FatDirEnt[i].FileName[0] == FAT_DIRENT_NEVER_USED ) {
  525. EofDir = TRUE;
  526. break;
  527. }
  528. //
  529. // update the current position and the number of bytes transfered
  530. //
  531. BlFileTable[ FileId ].Position.LowPart += sizeof(DIRENT);
  532. Lbo += sizeof(DIRENT);
  533. RunByteCount -= sizeof(DIRENT);
  534. //
  535. // skip this entry if the file or directory has been erased
  536. //
  537. if ( FatDirEnt[i].FileName[0] == FAT_DIRENT_DELETED ) {
  538. continue;
  539. }
  540. //
  541. // skip this entry if this is a valume label
  542. //
  543. if (FlagOn( FatDirEnt[i].Attributes, FAT_DIRENT_ATTR_VOLUME_ID )) {
  544. continue;
  545. }
  546. //
  547. // convert FAT directory entry in ARC directory entry
  548. //
  549. FatDirToArcDir( &FatDirEnt[i], DirEntry++ );
  550. //
  551. // update pointers
  552. //
  553. if ( ++*CountDir >= NumberDir ) {
  554. break;
  555. }
  556. }
  557. } while ( !EofDir && *CountDir < NumberDir );
  558. //
  559. // all done
  560. //
  561. return *CountDir ? ESUCCESS : ENOTDIR;
  562. }
  563. ARC_STATUS
  564. FatGetFileInformation (
  565. IN ULONG FileId,
  566. OUT PFILE_INFORMATION Buffer
  567. )
  568. /*++
  569. Routine Description:
  570. This procedure returns to the user a buffer filled with file information
  571. Arguments:
  572. FileId - Supplies the File id for the operation
  573. Buffer - Supplies the buffer to receive the file information. Note that
  574. it must be large enough to hold the full file name
  575. Return Value:
  576. ESUCCESS is returned if the open operation is successful. Otherwise,
  577. an unsuccessful status is returned that describes the reason for failure.
  578. --*/
  579. {
  580. PBL_FILE_TABLE FileTableEntry;
  581. UCHAR Attributes;
  582. ULONG i;
  583. FatDebugOutput("FatGetFileInformation\r\n", 0, 0);
  584. //
  585. // Load our local variables
  586. //
  587. FileTableEntry = &BlFileTable[FileId];
  588. Attributes = FileTableEntry->u.FatFileContext.Dirent.Attributes;
  589. //
  590. // Zero out the buffer, and fill in its non-zero values.
  591. //
  592. RtlZeroMemory(Buffer, sizeof(FILE_INFORMATION));
  593. Buffer->EndingAddress.LowPart = FileTableEntry->u.FatFileContext.Dirent.FileSize;
  594. Buffer->CurrentPosition.LowPart = FileTableEntry->Position.LowPart;
  595. Buffer->CurrentPosition.HighPart = 0;
  596. if (FlagOn(Attributes, FAT_DIRENT_ATTR_READ_ONLY)) { SetFlag(Buffer->Attributes, ArcReadOnlyFile) };
  597. if (FlagOn(Attributes, FAT_DIRENT_ATTR_HIDDEN)) { SetFlag(Buffer->Attributes, ArcHiddenFile) };
  598. if (FlagOn(Attributes, FAT_DIRENT_ATTR_SYSTEM)) { SetFlag(Buffer->Attributes, ArcSystemFile) };
  599. if (FlagOn(Attributes, FAT_DIRENT_ATTR_ARCHIVE)) { SetFlag(Buffer->Attributes, ArcArchiveFile) };
  600. if (FlagOn(Attributes, FAT_DIRENT_ATTR_DIRECTORY)) { SetFlag(Buffer->Attributes, ArcDirectoryFile) };
  601. Buffer->FileNameLength = FileTableEntry->FileNameLength;
  602. for (i = 0; i < FileTableEntry->FileNameLength; i += 1) {
  603. Buffer->FileName[i] = FileTableEntry->FileName[i];
  604. }
  605. return ESUCCESS;
  606. }
  607. ARC_STATUS
  608. FatOpen (
  609. IN CHAR * FIRMWARE_PTR FileName,
  610. IN OPEN_MODE OpenMode,
  611. IN ULONG * FIRMWARE_PTR FileId
  612. )
  613. /*++
  614. Routine Description:
  615. This routine searches the device for a file matching FileName.
  616. If a match is found the dirent for the file is saved and the file is
  617. opened.
  618. Arguments:
  619. FileName - Supplies a pointer to a zero terminated file name.
  620. OpenMode - Supplies the mode of the open.
  621. FileId - Supplies a pointer to a variable that specifies the file
  622. table entry that is to be filled in if the open is successful.
  623. Return Value:
  624. ESUCCESS is returned if the open operation is successful. Otherwise,
  625. an unsuccessful status is returned that describes the reason for failure.
  626. --*/
  627. {
  628. PBL_FILE_TABLE FileTableEntry;
  629. PFAT_STRUCTURE_CONTEXT FatStructureContext;
  630. ULONG DeviceId;
  631. FAT_ENTRY CurrentDirectoryIndex;
  632. BOOLEAN SearchSucceeded;
  633. BOOLEAN IsDirectory;
  634. BOOLEAN IsReadOnly;
  635. STRING PathName;
  636. FAT8DOT3 Name;
  637. FatDebugOutput("FatOpen: %s\r\n", FileName, 0);
  638. //
  639. // Load our local variables
  640. //
  641. FileTableEntry = &BlFileTable[*FileId];
  642. FatStructureContext = (PFAT_STRUCTURE_CONTEXT)FileTableEntry->StructureContext;
  643. DeviceId = FileTableEntry->DeviceId;
  644. //
  645. // Construct a file name descriptor from the input file name
  646. //
  647. RtlInitString( &PathName, FileName );
  648. //
  649. // While the path name has some characters in it we'll go through our loop
  650. // which extracts the first part of the path name and searches the current
  651. // directory for an entry. If what we find is a directory then we have to
  652. // continue looping until we're done with the path name.
  653. //
  654. FileTableEntry->u.FatFileContext.DirentLbo = 0;
  655. FileTableEntry->Position.LowPart = 0;
  656. FileTableEntry->Position.HighPart = 0;
  657. CurrentDirectoryIndex = 0;
  658. SearchSucceeded = TRUE;
  659. IsDirectory = TRUE;
  660. IsReadOnly = TRUE;
  661. if ((PathName.Buffer[0] == '\\') && (PathName.Length == 1)) {
  662. //
  663. // We are opening the root directory.
  664. //
  665. // N.B.: IsDirectory and SearchSucceeded are already TRUE.
  666. //
  667. PathName.Length = 0;
  668. FileTableEntry->FileNameLength = 1;
  669. FileTableEntry->FileName[0] = PathName.Buffer[0];
  670. //
  671. // Root dirent is all zeroes with a directory attribute.
  672. //
  673. RtlZeroMemory(&FileTableEntry->u.FatFileContext.Dirent, sizeof(DIRENT));
  674. FileTableEntry->u.FatFileContext.Dirent.Attributes = FAT_DIRENT_ATTR_DIRECTORY;
  675. FileTableEntry->u.FatFileContext.DirentLbo = 0;
  676. IsReadOnly = FALSE;
  677. CurrentDirectoryIndex = FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile;
  678. } else {
  679. //
  680. // We are not opening the root directory.
  681. //
  682. //
  683. // If the search begins in a FAT32 root, set up the starting point
  684. // for the search.
  685. //
  686. if (IsBpbFat32(&FatStructureContext->Bpb)) {
  687. CurrentDirectoryIndex = FatStructureContext->Bpb.RootDirFirstCluster;
  688. }
  689. while ((PathName.Length > 0) && IsDirectory) {
  690. ARC_STATUS Status;
  691. //
  692. // Extract the first component and search the directory for a match, but
  693. // first copy the first part to the file name buffer in the file table entry
  694. //
  695. if (PathName.Buffer[0] == '\\') {
  696. PathName.Buffer +=1;
  697. PathName.Length -=1;
  698. }
  699. for (FileTableEntry->FileNameLength = 0;
  700. (((USHORT)FileTableEntry->FileNameLength < PathName.Length) &&
  701. (PathName.Buffer[FileTableEntry->FileNameLength] != '\\'));
  702. FileTableEntry->FileNameLength += 1) {
  703. FileTableEntry->FileName[FileTableEntry->FileNameLength] =
  704. PathName.Buffer[FileTableEntry->FileNameLength];
  705. }
  706. FatFirstComponent( &PathName, (PFAT8DOT3) Name );
  707. Status = FatSearchForDirent( FatStructureContext,
  708. DeviceId,
  709. CurrentDirectoryIndex,
  710. (PFAT8DOT3) Name,
  711. &FileTableEntry->u.FatFileContext.Dirent,
  712. &FileTableEntry->u.FatFileContext.DirentLbo,
  713. FALSE );
  714. if (Status == ENOENT) {
  715. SearchSucceeded = FALSE;
  716. break;
  717. }
  718. if (Status != ESUCCESS) {
  719. return Status;
  720. }
  721. //
  722. // We have a match now check to see if it is a directory, and also
  723. // if it is readonly
  724. //
  725. IsDirectory = BooleanFlagOn( FileTableEntry->u.FatFileContext.Dirent.Attributes,
  726. FAT_DIRENT_ATTR_DIRECTORY );
  727. IsReadOnly = BooleanFlagOn( FileTableEntry->u.FatFileContext.Dirent.Attributes,
  728. FAT_DIRENT_ATTR_READ_ONLY );
  729. if (IsDirectory) {
  730. CurrentDirectoryIndex = FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile;
  731. if (IsBpbFat32(&FatStructureContext->Bpb)) {
  732. CurrentDirectoryIndex += 0x10000 *
  733. FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFileHi;
  734. }
  735. }
  736. }
  737. }
  738. //
  739. // If the path name length is not zero then we were trying to crack a path
  740. // with an nonexistent (or non directory) name in it. For example, we tried
  741. // to crack a\b\c\d and b is not a directory or does not exist (then the path
  742. // name will still contain c\d).
  743. //
  744. if (PathName.Length != 0) {
  745. return ENOTDIR;
  746. }
  747. //
  748. // At this point we've cracked the name up to (an maybe including the last
  749. // component). We located the last component if the SearchSucceeded flag is
  750. // true, otherwise the last component does not exist. If we located the last
  751. // component then this is like an open or a supersede, but not a create.
  752. //
  753. if (SearchSucceeded) {
  754. //
  755. // Check if the last component is a directory
  756. //
  757. if (IsDirectory) {
  758. //
  759. // For an existing directory the only valid open mode is OpenDirectory
  760. // all other modes return an error
  761. //
  762. switch (OpenMode) {
  763. case ArcOpenReadOnly:
  764. case ArcOpenWriteOnly:
  765. case ArcOpenReadWrite:
  766. case ArcCreateWriteOnly:
  767. case ArcCreateReadWrite:
  768. case ArcSupersedeWriteOnly:
  769. case ArcSupersedeReadWrite:
  770. //
  771. // If we reach here then the caller got a directory but didn't
  772. // want to open a directory
  773. //
  774. return EISDIR;
  775. case ArcOpenDirectory:
  776. //
  777. // If we reach here then the caller got a directory and wanted
  778. // to open a directory.
  779. //
  780. FileTableEntry->Flags.Open = 1;
  781. FileTableEntry->Flags.Read = 1;
  782. return ESUCCESS;
  783. case ArcCreateDirectory:
  784. //
  785. // If we reach here then the caller got a directory and wanted
  786. // to create a new directory
  787. //
  788. return EACCES;
  789. }
  790. }
  791. //
  792. // If we get there then we have an existing file that is being opened.
  793. // We can open existing files through a lot of different open modes in
  794. // some cases we need to check the read only part of file and/or truncate
  795. // the file.
  796. //
  797. switch (OpenMode) {
  798. case ArcOpenReadOnly:
  799. //
  800. // If we reach here then the user got a file and wanted to open the
  801. // file read only
  802. //
  803. FileTableEntry->Flags.Open = 1;
  804. FileTableEntry->Flags.Read = 1;
  805. return ESUCCESS;
  806. case ArcOpenWriteOnly:
  807. //
  808. // If we reach here then the user got a file and wanted to open the
  809. // file write only
  810. //
  811. if (IsReadOnly) { return EROFS; }
  812. FileTableEntry->Flags.Open = 1;
  813. FileTableEntry->Flags.Write = 1;
  814. return ESUCCESS;
  815. case ArcOpenReadWrite:
  816. //
  817. // If we reach here then the user got a file and wanted to open the
  818. // file read/write
  819. //
  820. if (IsReadOnly) { return EROFS; }
  821. FileTableEntry->Flags.Open = 1;
  822. FileTableEntry->Flags.Read = 1;
  823. FileTableEntry->Flags.Write = 1;
  824. return ESUCCESS;
  825. case ArcCreateWriteOnly:
  826. case ArcCreateReadWrite:
  827. //
  828. // If we reach here then the user got a file and wanted to create a new
  829. // file
  830. //
  831. return EACCES;
  832. case ArcSupersedeWriteOnly:
  833. //
  834. // If we reach here then the user got a file and wanted to supersede a
  835. // file
  836. //
  837. if (IsReadOnly) { return EROFS; }
  838. TruncateFileAllocation( *FileId, 0 );
  839. FileTableEntry->Flags.Open = 1;
  840. FileTableEntry->Flags.Read = 1;
  841. FileTableEntry->Flags.Write = 1;
  842. return ESUCCESS;
  843. case ArcSupersedeReadWrite:
  844. //
  845. // If we reach here then the user got a file and wanted to supersede a
  846. // file
  847. //
  848. if (IsReadOnly) { return EROFS; }
  849. TruncateFileAllocation( *FileId, 0 );
  850. FileTableEntry->Flags.Open = 1;
  851. FileTableEntry->Flags.Read = 1;
  852. FileTableEntry->Flags.Write = 1;
  853. return ESUCCESS;
  854. case ArcOpenDirectory:
  855. case ArcCreateDirectory:
  856. //
  857. // If we reach here then the user got a file and wanted a directory
  858. //
  859. return ENOTDIR;
  860. }
  861. }
  862. //
  863. // If we get here the last component does not exist so we are trying to create
  864. // either a new file or a directory.
  865. //
  866. switch (OpenMode) {
  867. case ArcOpenReadOnly:
  868. case ArcOpenWriteOnly:
  869. case ArcOpenReadWrite:
  870. //
  871. // If we reach here then the user did not get a file but wanted a file
  872. //
  873. return ENOENT;
  874. case ArcCreateWriteOnly:
  875. case ArcSupersedeWriteOnly:
  876. //
  877. // If we reach here then the user did not get a file and wanted to create
  878. // or supersede a file write only
  879. //
  880. RtlZeroMemory( &FileTableEntry->u.FatFileContext.Dirent, sizeof(DIRENT));
  881. FatSetDirent( (PFAT8DOT3) Name, &FileTableEntry->u.FatFileContext.Dirent, 0 );
  882. CreateDirent( FatStructureContext,
  883. DeviceId,
  884. CurrentDirectoryIndex,
  885. &FileTableEntry->u.FatFileContext.Dirent,
  886. &FileTableEntry->u.FatFileContext.DirentLbo );
  887. FileTableEntry->Flags.Open = 1;
  888. FileTableEntry->Flags.Write = 1;
  889. return ESUCCESS;
  890. case ArcCreateReadWrite:
  891. case ArcSupersedeReadWrite:
  892. //
  893. // If we reach here then the user did not get a file and wanted to create
  894. // or supersede a file read/write
  895. //
  896. RtlZeroMemory( &FileTableEntry->u.FatFileContext.Dirent, sizeof(DIRENT));
  897. FatSetDirent( (PFAT8DOT3) Name, &FileTableEntry->u.FatFileContext.Dirent, 0 );
  898. CreateDirent( FatStructureContext,
  899. DeviceId,
  900. CurrentDirectoryIndex,
  901. &FileTableEntry->u.FatFileContext.Dirent,
  902. &FileTableEntry->u.FatFileContext.DirentLbo );
  903. FileTableEntry->Flags.Open = 1;
  904. FileTableEntry->Flags.Read = 1;
  905. FileTableEntry->Flags.Write = 1;
  906. return ESUCCESS;
  907. case ArcOpenDirectory:
  908. //
  909. // If we reach here then the user did not get a file and wanted to open
  910. // an existing directory
  911. //
  912. return ENOENT;
  913. case ArcCreateDirectory:
  914. //
  915. // If we reach here then the user did not get a file and wanted to create
  916. // a new directory.
  917. //
  918. RtlZeroMemory( &FileTableEntry->u.FatFileContext.Dirent, sizeof(DIRENT));
  919. FatSetDirent( (PFAT8DOT3) Name,
  920. &FileTableEntry->u.FatFileContext.Dirent,
  921. FAT_DIRENT_ATTR_DIRECTORY );
  922. CreateDirent( FatStructureContext,
  923. DeviceId,
  924. CurrentDirectoryIndex,
  925. &FileTableEntry->u.FatFileContext.Dirent,
  926. &FileTableEntry->u.FatFileContext.DirentLbo );
  927. IncreaseFileAllocation( *FileId, sizeof(DIRENT) * 2 );
  928. {
  929. DIRENT Buffer;
  930. LBO Lbo;
  931. ULONG Count;
  932. ULONG i;
  933. RtlZeroMemory((PVOID)&Buffer.FileName[0], sizeof(DIRENT) );
  934. for (i = 0; i < 11; i += 1) {
  935. Buffer.FileName[i] = ' ';
  936. }
  937. Buffer.Attributes = FAT_DIRENT_ATTR_DIRECTORY;
  938. VboToLbo( *FileId, 0, &Lbo, &Count );
  939. Buffer.FileName[0] = FAT_DIRENT_DIRECTORY_ALIAS;
  940. Buffer.FirstClusterOfFile =
  941. FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile;
  942. Buffer.FirstClusterOfFileHi =
  943. FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFileHi;
  944. DiskWrite( DeviceId, Lbo, sizeof(DIRENT), (PVOID)&Buffer.FileName[0] );
  945. VboToLbo( *FileId, sizeof(DIRENT), &Lbo, &Count );
  946. Buffer.FileName[1] = FAT_DIRENT_DIRECTORY_ALIAS;
  947. Buffer.FirstClusterOfFile = (USHORT)CurrentDirectoryIndex;
  948. Buffer.FirstClusterOfFileHi = (USHORT)(CurrentDirectoryIndex >> 16);
  949. DiskWrite( DeviceId, Lbo, sizeof(DIRENT), (PVOID)&Buffer.FileName[0] );
  950. }
  951. FileTableEntry->Flags.Open = 1;
  952. FileTableEntry->Flags.Read = 1;
  953. return ESUCCESS;
  954. }
  955. return( EINVAL );
  956. }
  957. ARC_STATUS
  958. FatRead (
  959. IN ULONG FileId,
  960. OUT VOID * FIRMWARE_PTR Buffer,
  961. IN ULONG Length,
  962. OUT ULONG * FIRMWARE_PTR Transfer
  963. )
  964. /*++
  965. Routine Description:
  966. This routine reads data from the specified file.
  967. Arguments:
  968. FileId - Supplies the file table index.
  969. Buffer - Supplies a pointer to the buffer that receives the data
  970. read.
  971. Length - Supplies the number of bytes that are to be read.
  972. Transfer - Supplies a pointer to a variable that receives the number
  973. of bytes actually transfered.
  974. Return Value:
  975. ESUCCESS is returned if the read operation is successful. Otherwise,
  976. an unsuccessful status is returned that describes the reason for failure.
  977. --*/
  978. {
  979. PBL_FILE_TABLE FileTableEntry;
  980. PFAT_STRUCTURE_CONTEXT FatStructureContext;
  981. ULONG DeviceId;
  982. FatDebugOutput("FatRead\r\n", 0, 0);
  983. //
  984. // Load out local variables
  985. //
  986. FileTableEntry = &BlFileTable[FileId];
  987. FatStructureContext = (PFAT_STRUCTURE_CONTEXT)FileTableEntry->StructureContext;
  988. DeviceId = FileTableEntry->DeviceId;
  989. //
  990. // Clear the transfer count
  991. //
  992. *Transfer = 0;
  993. //
  994. // Read in runs (i.e., bytes) until the byte count goes to zero
  995. //
  996. while (Length > 0) {
  997. LBO Lbo;
  998. ULONG CurrentRunByteCount;
  999. //
  1000. // Lookup the corresponding Lbo and run length for the current position
  1001. // (i.e., Vbo).
  1002. //
  1003. if (FatVboToLbo( FileId, FileTableEntry->Position.LowPart, &Lbo, &CurrentRunByteCount, FALSE ) != ESUCCESS) {
  1004. return ESUCCESS;
  1005. }
  1006. //
  1007. // while there are bytes to be read in from the current run
  1008. // length and we haven't exhausted the request we loop reading
  1009. // in bytes. The biggest request we'll handle is only 32KB
  1010. // contiguous bytes per physical read. So we might need to loop
  1011. // through the run.
  1012. //
  1013. while ((Length > 0) && (CurrentRunByteCount > 0)) {
  1014. LONG SingleReadSize;
  1015. //
  1016. // Compute the size of the next physical read
  1017. //
  1018. SingleReadSize = Minimum(Length, 32 * 1024);
  1019. SingleReadSize = Minimum((ULONG)SingleReadSize, CurrentRunByteCount);
  1020. //
  1021. // Don't read beyond the eof
  1022. //
  1023. if (((ULONG)SingleReadSize + FileTableEntry->Position.LowPart) >
  1024. FileTableEntry->u.FatFileContext.Dirent.FileSize) {
  1025. SingleReadSize = FileTableEntry->u.FatFileContext.Dirent.FileSize -
  1026. FileTableEntry->Position.LowPart;
  1027. //
  1028. // If the readjusted read length is now zero then we're done.
  1029. //
  1030. if (SingleReadSize <= 0) {
  1031. return ESUCCESS;
  1032. }
  1033. //
  1034. // By also setting length here we'll make sure that this is our last
  1035. // read
  1036. //
  1037. Length = SingleReadSize;
  1038. }
  1039. //
  1040. // Issue the read
  1041. //
  1042. DiskRead( DeviceId, Lbo, SingleReadSize, Buffer, DONT_CACHE_NEW_DATA, FALSE );
  1043. //
  1044. // Update the remaining length, Current run byte count
  1045. // and new Lbo offset
  1046. //
  1047. Length -= SingleReadSize;
  1048. CurrentRunByteCount -= SingleReadSize;
  1049. Lbo += SingleReadSize;
  1050. //
  1051. // Update the current position and the number of bytes transfered
  1052. //
  1053. FileTableEntry->Position.LowPart += SingleReadSize;
  1054. *Transfer += SingleReadSize;
  1055. //
  1056. // Update buffer to point to the next byte location to fill in
  1057. //
  1058. Buffer = (PCHAR)Buffer + SingleReadSize;
  1059. }
  1060. }
  1061. //
  1062. // If we get here then remaining sector count is zero so we can
  1063. // return success to our caller
  1064. //
  1065. return ESUCCESS;
  1066. }
  1067. ARC_STATUS
  1068. FatRename(
  1069. IN ULONG FileId,
  1070. IN CHAR * FIRMWARE_PTR NewFileName
  1071. )
  1072. /*++
  1073. Routine Description:
  1074. This routine renames an open file. It does no checking to
  1075. see if the target filename already exists. It is intended for use
  1076. only when dual-booting DOS on x86 machines, where it is used to
  1077. replace the NT MVDM CONFIG.SYS and AUTOEXEC.BAT with the native DOS
  1078. CONFIG.SYS and AUTOEXEC.BAT files.
  1079. Arguments:
  1080. FileId - Supplies the file id of the file to be renamed
  1081. NewFileName - Supplies the new name for the file.
  1082. Return Value:
  1083. ARC_STATUS
  1084. --*/
  1085. {
  1086. PBL_FILE_TABLE FileTableEntry;
  1087. PFAT_STRUCTURE_CONTEXT FatStructureContext;
  1088. ULONG DeviceId;
  1089. FAT8DOT3 FatName;
  1090. STRING String;
  1091. //
  1092. // Initialize our local variables
  1093. //
  1094. RtlInitString( &String, NewFileName );
  1095. FileTableEntry = &BlFileTable[FileId];
  1096. FatStructureContext = (PFAT_STRUCTURE_CONTEXT)FileTableEntry->StructureContext;
  1097. DeviceId = FileTableEntry->DeviceId;
  1098. //
  1099. // Modify a in-memory copy of the dirent with the new name
  1100. //
  1101. FatFirstComponent( &String, (PFAT8DOT3) FatName );
  1102. FatSetDirent( (PFAT8DOT3) FatName,
  1103. &FileTableEntry->u.FatFileContext.Dirent,
  1104. FileTableEntry->u.FatFileContext.Dirent.Attributes );
  1105. //
  1106. // Write the modified dirent to disk
  1107. //
  1108. DiskWrite( DeviceId,
  1109. FileTableEntry->u.FatFileContext.DirentLbo,
  1110. sizeof(DIRENT),
  1111. &FileTableEntry->u.FatFileContext.Dirent );
  1112. //
  1113. // And return to our caller
  1114. //
  1115. return ESUCCESS;
  1116. }
  1117. ARC_STATUS
  1118. FatSeek (
  1119. IN ULONG FileId,
  1120. IN LARGE_INTEGER * FIRMWARE_PTR Offset,
  1121. IN SEEK_MODE SeekMode
  1122. )
  1123. /*++
  1124. Routine Description:
  1125. This routine seeks to the specified position for the file specified
  1126. by the file id.
  1127. Arguments:
  1128. FileId - Supplies the file table index.
  1129. Offset - Supplies the offset in the file to position to.
  1130. SeekMode - Supplies the mode of the seek operation.
  1131. Return Value:
  1132. ESUCCESS is returned if the seek operation is successful. Otherwise,
  1133. EINVAL is returned.
  1134. --*/
  1135. {
  1136. PBL_FILE_TABLE FileTableEntry;
  1137. ULONG NewPosition;
  1138. FatDebugOutput("FatSeek\r\n", 0, 0);
  1139. //
  1140. // Load our local variables
  1141. //
  1142. FileTableEntry = &BlFileTable[FileId];
  1143. //
  1144. // Compute the new position
  1145. //
  1146. if (SeekMode == SeekAbsolute) {
  1147. NewPosition = Offset->LowPart;
  1148. } else {
  1149. NewPosition = FileTableEntry->Position.LowPart + Offset->LowPart;
  1150. }
  1151. //
  1152. // If the new position is greater than the file size then return
  1153. // an error
  1154. //
  1155. if (NewPosition > FileTableEntry->u.FatFileContext.Dirent.FileSize) {
  1156. return EINVAL;
  1157. }
  1158. //
  1159. // Otherwise set the new position and return to our caller
  1160. //
  1161. FileTableEntry->Position.LowPart = NewPosition;
  1162. return ESUCCESS;
  1163. }
  1164. ARC_STATUS
  1165. FatSetFileInformation (
  1166. IN ULONG FileId,
  1167. IN ULONG AttributeFlags,
  1168. IN ULONG AttributeMask
  1169. )
  1170. /*++
  1171. Routine Description:
  1172. This routine sets the file attributes of the indicated file
  1173. Arguments:
  1174. FileId - Supplies the File Id for the operation
  1175. AttributeFlags - Supplies the value (on or off) for each attribute being modified
  1176. AttributeMask - Supplies a mask of the attributes being altered. All other
  1177. file attributes are left alone.
  1178. Return Value:
  1179. EROFS is always returned
  1180. --*/
  1181. {
  1182. UNREFERENCED_PARAMETER( FileId );
  1183. UNREFERENCED_PARAMETER( AttributeFlags );
  1184. UNREFERENCED_PARAMETER( AttributeMask );
  1185. FatDebugOutput("FatSetFileInformation\r\n", 0, 0);
  1186. return EROFS;
  1187. }
  1188. ARC_STATUS
  1189. FatWrite (
  1190. IN ULONG FileId,
  1191. IN VOID * FIRMWARE_PTR Buffer,
  1192. IN ULONG Length,
  1193. OUT ULONG * FIRMWARE_PTR Transfer
  1194. )
  1195. /*++
  1196. Routine Description:
  1197. This routine writes data to the specified file.
  1198. Arguments:
  1199. FileId - Supplies the file table index.
  1200. Buffer - Supplies a pointer to the buffer that contains the data
  1201. written.
  1202. Length - Supplies the number of bytes that are to be written.
  1203. Transfer - Supplies a pointer to a variable that receives the number
  1204. of bytes actually transfered.
  1205. Return Value:
  1206. ESUCCESS is returned if the write operation is successful. Otherwise,
  1207. an unsuccessful status is returned that describes the reason for failure.
  1208. --*/
  1209. {
  1210. PBL_FILE_TABLE FileTableEntry;
  1211. PFAT_STRUCTURE_CONTEXT FatStructureContext;
  1212. ULONG DeviceId;
  1213. ULONG OffsetBeyondWrite;
  1214. FatDebugOutput("FatWrite\r\n", 0, 0);
  1215. //
  1216. // Load our local variables
  1217. //
  1218. FileTableEntry = &BlFileTable[FileId];
  1219. FatStructureContext = (PFAT_STRUCTURE_CONTEXT)FileTableEntry->StructureContext;
  1220. DeviceId = FileTableEntry->DeviceId;
  1221. //
  1222. // Reset the file size to be the maximum of what is it now and the end of
  1223. // our write. We will assume that there is always enough allocation to support
  1224. // the file size, so we only need to increase allocation if we are increasing
  1225. // the file size.
  1226. //
  1227. OffsetBeyondWrite = FileTableEntry->Position.LowPart + Length;
  1228. if (OffsetBeyondWrite > FileTableEntry->u.FatFileContext.Dirent.FileSize) {
  1229. IncreaseFileAllocation( FileId, OffsetBeyondWrite );
  1230. FileTableEntry->u.FatFileContext.Dirent.FileSize = OffsetBeyondWrite;
  1231. DiskWrite( DeviceId,
  1232. FileTableEntry->u.FatFileContext.DirentLbo,
  1233. sizeof(DIRENT),
  1234. &FileTableEntry->u.FatFileContext.Dirent );
  1235. }
  1236. //
  1237. // Clear the transfer count
  1238. //
  1239. *Transfer = 0;
  1240. //
  1241. // Write out runs (i.e., bytes) until the byte count goes to zero
  1242. //
  1243. while (Length > 0) {
  1244. LBO Lbo;
  1245. ULONG CurrentRunByteCount;
  1246. //
  1247. // Lookup the corresponding Lbo and run length for the current position
  1248. // (i.e., Vbo).
  1249. //
  1250. VboToLbo( FileId, FileTableEntry->Position.LowPart, &Lbo, &CurrentRunByteCount );
  1251. //
  1252. // While there are bytes to be written out to the current run
  1253. // length and we haven't exhausted the request we loop reading
  1254. // in bytes. The biggest request we'll handle is only 32KB
  1255. // contiguous bytes per physical read. So we might need to loop
  1256. // through the run.
  1257. //
  1258. while ((Length > 0) && (CurrentRunByteCount > 0)) {
  1259. LONG SingleWriteSize;
  1260. //
  1261. // Compute the size of the next physical read
  1262. //
  1263. SingleWriteSize = Minimum(Length, 32 * 1024);
  1264. SingleWriteSize = Minimum((ULONG)SingleWriteSize, CurrentRunByteCount);
  1265. //
  1266. // Issue the Write
  1267. //
  1268. DiskWrite( DeviceId, Lbo, SingleWriteSize, Buffer);
  1269. //
  1270. // Update the remaining length, Current run byte count
  1271. // and new Lbo offset
  1272. //
  1273. Length -= SingleWriteSize;
  1274. CurrentRunByteCount -= SingleWriteSize;
  1275. Lbo += SingleWriteSize;
  1276. //
  1277. // Update the current position and the number of bytes transfered
  1278. //
  1279. FileTableEntry->Position.LowPart += SingleWriteSize;
  1280. *Transfer += SingleWriteSize;
  1281. //
  1282. // Update buffer to point to the next byte location to fill in
  1283. //
  1284. Buffer = (PCHAR)Buffer + SingleWriteSize;
  1285. }
  1286. }
  1287. //
  1288. // Check if the fat is dirty and flush it out if it is.
  1289. //
  1290. if (FatStructureContext->CachedFatDirty) {
  1291. FlushFatEntries( FatStructureContext, DeviceId );
  1292. }
  1293. //
  1294. // If we get here then remaining sector count is zero so we can
  1295. // return success to our caller
  1296. //
  1297. return ESUCCESS;
  1298. }
  1299. ARC_STATUS
  1300. FatInitialize (
  1301. VOID
  1302. )
  1303. /*++
  1304. Routine Description:
  1305. This routine initializes the fat boot filesystem.
  1306. Currently this is a no-op.
  1307. Arguments:
  1308. None.
  1309. Return Value:
  1310. ESUCCESS.
  1311. --*/
  1312. {
  1313. return ESUCCESS;
  1314. }
  1315. //
  1316. // Internal support routine
  1317. //
  1318. ARC_STATUS
  1319. FatDiskRead (
  1320. IN ULONG DeviceId,
  1321. IN LBO Lbo,
  1322. IN ULONG ByteCount,
  1323. IN PVOID Buffer,
  1324. IN BOOLEAN CacheNewData
  1325. )
  1326. /*++
  1327. Routine Description:
  1328. This routine reads in zero or more bytes from the specified device.
  1329. Arguments:
  1330. DeviceId - Supplies the device id to use in the arc calls.
  1331. Lbo - Supplies the LBO to start reading from.
  1332. ByteCount - Supplies the number of bytes to read.
  1333. Buffer - Supplies a pointer to the buffer to read the bytes into.
  1334. Return Value:
  1335. ESUCCESS is returned if the read operation is successful. Otherwise,
  1336. an unsuccessful status is returned that describes the reason for failure.
  1337. --*/
  1338. {
  1339. LARGE_INTEGER LargeLbo;
  1340. ARC_STATUS Status;
  1341. ULONG i;
  1342. //
  1343. // Special case the zero byte read request
  1344. //
  1345. if (ByteCount == 0) {
  1346. return ESUCCESS;
  1347. }
  1348. //
  1349. // Issue the read through the cache.
  1350. //
  1351. LargeLbo.QuadPart = Lbo;
  1352. Status = BlDiskCacheRead(DeviceId,
  1353. &LargeLbo,
  1354. Buffer,
  1355. ByteCount,
  1356. &i,
  1357. CacheNewData);
  1358. if (Status != ESUCCESS) {
  1359. return Status;
  1360. }
  1361. //
  1362. // Make sure we got back the amount requested
  1363. //
  1364. if (ByteCount != i) {
  1365. return EIO;
  1366. }
  1367. //
  1368. // Everything is fine so return success to our caller
  1369. //
  1370. return ESUCCESS;
  1371. }
  1372. //
  1373. // Internal support routine
  1374. //
  1375. ARC_STATUS
  1376. FatDiskWrite (
  1377. IN ULONG DeviceId,
  1378. IN LBO Lbo,
  1379. IN ULONG ByteCount,
  1380. IN PVOID Buffer
  1381. )
  1382. /*++
  1383. Routine Description:
  1384. This routine writes in zero or more bytes to the specified device.
  1385. Arguments:
  1386. DeviceId - Supplies the device id to use in the arc calls.
  1387. Lbo - Supplies the LBO to start writing from.
  1388. ByteCount - Supplies the number of bytes to write.
  1389. Buffer - Supplies a pointer to the buffer of bytes to write out.
  1390. Return Value:
  1391. ESUCCESS is returned if the write operation is successful. Otherwise,
  1392. an unsuccessful status is returned that describes the reason for failure.
  1393. --*/
  1394. {
  1395. LARGE_INTEGER LargeLbo;
  1396. ARC_STATUS Status;
  1397. ULONG i;
  1398. //
  1399. // Special case the zero byte write request
  1400. //
  1401. if (ByteCount == 0) {
  1402. return ESUCCESS;
  1403. }
  1404. //
  1405. // Issue the write through the cache.
  1406. //
  1407. LargeLbo.QuadPart = Lbo;
  1408. Status = BlDiskCacheWrite (DeviceId,
  1409. &LargeLbo,
  1410. Buffer,
  1411. ByteCount,
  1412. &i);
  1413. if (Status != ESUCCESS) {
  1414. return Status;
  1415. }
  1416. //
  1417. // Make sure we wrote out the amount requested
  1418. //
  1419. if (ByteCount != i) {
  1420. return EIO;
  1421. }
  1422. //
  1423. // Everything is fine so return success to our caller
  1424. //
  1425. return ESUCCESS;
  1426. }
  1427. //
  1428. // Internal support routine
  1429. //
  1430. CLUSTER_TYPE
  1431. FatInterpretClusterType (
  1432. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  1433. IN FAT_ENTRY Entry
  1434. )
  1435. /*++
  1436. Routine Description:
  1437. This procedure tells the caller how to interpret a fat table entry. It will
  1438. indicate if the fat cluster is available, reserved, bad, the last one, or another
  1439. fat index.
  1440. Arguments:
  1441. FatStructureContext - Supplies the volume structure for the operation
  1442. DeviceId - Supplies the DeviceId for the volume being used.
  1443. Entry - Supplies the fat entry to examine.
  1444. Return Value:
  1445. The type of the input fat entry is returned
  1446. --*/
  1447. {
  1448. //
  1449. // Check for 12 or 16 bit fat.
  1450. //
  1451. if (FatIndexBitSize(&FatStructureContext->Bpb) == 12) {
  1452. //
  1453. // For 12 bit fat check for one of the cluster types, but first
  1454. // make sure we only looking at 12 bits of the entry
  1455. //
  1456. Entry &= 0x00000fff;
  1457. if (Entry == 0x000) { return FatClusterAvailable; }
  1458. else if ((Entry >= 0xff0) && (Entry <= 0xff6)) { return FatClusterReserved; }
  1459. else if (Entry == 0xff7) { return FatClusterBad; }
  1460. else if ((Entry >= 0xff8) && (Entry <= 0xfff)) { return FatClusterLast; }
  1461. else { return FatClusterNext; }
  1462. } else if (FatIndexBitSize(&FatStructureContext->Bpb) == 32) {
  1463. Entry &= 0x0fffffff;
  1464. if (Entry == 0x0000) { return FatClusterAvailable; }
  1465. else if (Entry == 0x0ffffff7) { return FatClusterBad; }
  1466. else if ((Entry >= 0x0ffffff8)) { return FatClusterLast; }
  1467. else { return FatClusterNext; }
  1468. } else {
  1469. //
  1470. // For 16 bit fat check for one of the cluster types, but first
  1471. // make sure we are only looking at 16 bits of the entry
  1472. //
  1473. Entry &= 0x0000ffff;
  1474. if (Entry == 0x0000) { return FatClusterAvailable; }
  1475. else if ((Entry >= 0xfff0) && (Entry <= 0xfff6)) { return FatClusterReserved; }
  1476. else if (Entry == 0xfff7) { return FatClusterBad; }
  1477. else if ((Entry >= 0xfff8) && (Entry <= 0xffff)) { return FatClusterLast; }
  1478. else { return FatClusterNext; }
  1479. }
  1480. }
  1481. //
  1482. // Internal support routine
  1483. //
  1484. ARC_STATUS
  1485. FatLookupFatEntry (
  1486. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  1487. IN ULONG DeviceId,
  1488. IN ULONG FatIndex,
  1489. OUT PULONG FatEntry,
  1490. IN BOOLEAN IsDoubleSpace
  1491. )
  1492. /*++
  1493. Routine Description:
  1494. This routine returns the value stored within the fat table and the specified
  1495. fat index. It is semantically equivalent to doing
  1496. x = Fat[FatIndex]
  1497. Arguments:
  1498. FatStrutureContext - Supplies the volume struture being used
  1499. DeviceId - Supplies the device being used
  1500. FatIndex - Supplies the index being looked up.
  1501. FatEntry - Receives the value stored at the specified fat index
  1502. IsDoubleSpace - Indicates if the search is being done on a double space volume
  1503. Return Value:
  1504. ESUCCESS is returned if the operation is successful. Otherwise,
  1505. an unsuccessful status is returned that describes the reason for failure.
  1506. --*/
  1507. {
  1508. BOOLEAN TwelveBitFat;
  1509. VBO Vbo;
  1510. //****if (IsDoubleSpace) { DbgPrint("FatLookupFatEntry(%0x,%0x,%0x,%0x,%0x)\n",FatStructureContext, DeviceId, FatIndex, FatEntry, IsDoubleSpace); }
  1511. //
  1512. // Calculate the Vbo of the word in the fat we need and
  1513. // also figure out if this is a 12 or 16 bit fat
  1514. //
  1515. if (FatIndexBitSize( &FatStructureContext->Bpb ) == 12) {
  1516. TwelveBitFat = TRUE;
  1517. Vbo = (FatIndex * 3) / 2;
  1518. } else if (FatIndexBitSize( &FatStructureContext->Bpb ) == 32) {
  1519. TwelveBitFat = FALSE;
  1520. Vbo = FatIndex * 4;
  1521. } else {
  1522. TwelveBitFat = FALSE;
  1523. Vbo = FatIndex * 2;
  1524. }
  1525. //
  1526. // Check if the Vbo we need is already in the cached fat
  1527. //
  1528. if ((FatStructureContext->CachedFat == NULL) ||
  1529. (Vbo < FatStructureContext->CachedFatVbo) ||
  1530. ((Vbo+1) > (FatStructureContext->CachedFatVbo + FAT_CACHE_SIZE))) {
  1531. //
  1532. // Set the aligned cached fat buffer in the structure context
  1533. //
  1534. FatStructureContext->CachedFat = ALIGN_BUFFER( &FatStructureContext->CachedFatBuffer[0] );
  1535. //
  1536. // As a safety net we'll flush any dirty fats that we might have cached before
  1537. // we turn the window
  1538. //
  1539. if (!IsDoubleSpace && FatStructureContext->CachedFatDirty) {
  1540. FlushFatEntries( FatStructureContext, DeviceId );
  1541. }
  1542. //
  1543. // Now set the new cached Vbo to be the Vbo of the cache sized section that
  1544. // we're trying to map. Each time we read in the cache we only read in
  1545. // cache sized and cached aligned pieces of the fat. So first compute an
  1546. // aligned cached fat vbo and then do the read.
  1547. //
  1548. FatStructureContext->CachedFatVbo = (Vbo / FAT_CACHE_SIZE) * FAT_CACHE_SIZE;
  1549. DiskRead( DeviceId,
  1550. FatStructureContext->CachedFatVbo + FatFirstFatAreaLbo(&FatStructureContext->Bpb),
  1551. FAT_CACHE_SIZE,
  1552. FatStructureContext->CachedFat,
  1553. CACHE_NEW_DATA,
  1554. IsDoubleSpace );
  1555. }
  1556. //
  1557. // At this point the cached fat contains the vbo we're after so simply
  1558. // extract the word
  1559. //
  1560. if (IsBpbFat32(&FatStructureContext->Bpb)) {
  1561. CopyUchar4( FatEntry,
  1562. &FatStructureContext->CachedFat[Vbo - FatStructureContext->CachedFatVbo] );
  1563. } else {
  1564. CopyUchar2( FatEntry,
  1565. &FatStructureContext->CachedFat[Vbo - FatStructureContext->CachedFatVbo] );
  1566. }
  1567. //
  1568. // Now if this is a 12 bit fat then check if the index is odd or even
  1569. // If it is odd then we need to shift it over 4 bits, and in all
  1570. // cases we need to mask out the high 4 bits.
  1571. //
  1572. if (TwelveBitFat) {
  1573. if ((FatIndex % 2) == 1) { *FatEntry >>= 4; }
  1574. *FatEntry &= 0x0fff;
  1575. }
  1576. return ESUCCESS;
  1577. }
  1578. //
  1579. // Internal support routine
  1580. //
  1581. ARC_STATUS
  1582. FatSetFatEntry(
  1583. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  1584. IN ULONG DeviceId,
  1585. IN FAT_ENTRY FatIndex,
  1586. IN FAT_ENTRY FatEntry
  1587. )
  1588. /*++
  1589. Routine Description:
  1590. This procedure sets the data within the fat table at the specified index to
  1591. to the specified value. It is semantically equivalent to doing
  1592. Fat[FatIndex] = FatEntry;
  1593. Arguments:
  1594. FatStructureContext - Supplies the structure context for the operation
  1595. DeviceId - Supplies the device for the operation
  1596. FatIndex - Supplies the index within the fat table to set
  1597. FatEntry - Supplies the value to store within the fat table
  1598. Return Value:
  1599. ESUCCESS is returned if the operation is successful. Otherwise,
  1600. an unsuccessful status is returned that describes the reason for failure.
  1601. --*/
  1602. {
  1603. BOOLEAN TwelveBitFat;
  1604. VBO Vbo;
  1605. //
  1606. // Calculate the Vbo of the word in the fat we are modifying and
  1607. // also figure out if this is a 12 or 16 bit fat
  1608. //
  1609. if (FatIndexBitSize( &FatStructureContext->Bpb ) == 12) {
  1610. TwelveBitFat = TRUE;
  1611. Vbo = (FatIndex * 3) / 2;
  1612. } else if (FatIndexBitSize( &FatStructureContext->Bpb ) == 32) {
  1613. TwelveBitFat = FALSE;
  1614. Vbo = FatIndex * 4;
  1615. } else {
  1616. TwelveBitFat = FALSE;
  1617. Vbo = FatIndex * 2;
  1618. }
  1619. //
  1620. // Check if the Vbo we need is already in the cached fat
  1621. //
  1622. if ((FatStructureContext->CachedFat == NULL) ||
  1623. (Vbo < FatStructureContext->CachedFatVbo) ||
  1624. ((Vbo+1) > (FatStructureContext->CachedFatVbo + FAT_CACHE_SIZE))) {
  1625. //
  1626. // Set the aligned cached fat buffer in the structure context
  1627. //
  1628. FatStructureContext->CachedFat = ALIGN_BUFFER( &FatStructureContext->CachedFatBuffer[0] );
  1629. //
  1630. // As a safety net we'll flush any dirty fats that we might have cached before
  1631. // we turn the window
  1632. //
  1633. if (FatStructureContext->CachedFatDirty) {
  1634. FlushFatEntries( FatStructureContext, DeviceId );
  1635. }
  1636. //
  1637. // Now set the new cached Vbo to be the Vbo of the cache sized section that
  1638. // we're trying to map. Each time we read in the cache we only read in
  1639. // cache sized and cached aligned pieces of the fat. So first compute an
  1640. // aligned cached fat vbo and then do the read.
  1641. //
  1642. FatStructureContext->CachedFatVbo = (Vbo / FAT_CACHE_SIZE) * FAT_CACHE_SIZE;
  1643. DiskRead( DeviceId,
  1644. FatStructureContext->CachedFatVbo + FatFirstFatAreaLbo(&FatStructureContext->Bpb),
  1645. FAT_CACHE_SIZE,
  1646. FatStructureContext->CachedFat,
  1647. CACHE_NEW_DATA,
  1648. FALSE );
  1649. }
  1650. //
  1651. // At this point the cached fat contains the vbo we're after. For a 16 bit
  1652. // fat we simply put in the fat entry. For the 12 bit fat we first need to extract
  1653. // the word containing the entry, modify the word, and then put it back.
  1654. //
  1655. if (TwelveBitFat) {
  1656. FAT_ENTRY Temp;
  1657. CopyUchar2( &Temp,
  1658. &FatStructureContext->CachedFat[Vbo - FatStructureContext->CachedFatVbo] );
  1659. if ((FatIndex % 2) == 0) {
  1660. FatEntry = (FAT_ENTRY)((Temp & 0xf000) | (FatEntry & 0x0fff));
  1661. } else {
  1662. FatEntry = (FAT_ENTRY)((Temp & 0x000f) | ((FatEntry << 4) & 0xfff0));
  1663. }
  1664. }
  1665. if (IsBpbFat32(&FatStructureContext->Bpb)) {
  1666. CopyUchar4( &FatStructureContext->CachedFat[Vbo - FatStructureContext->CachedFatVbo],
  1667. &FatEntry );
  1668. } else {
  1669. CopyUchar2( &FatStructureContext->CachedFat[Vbo - FatStructureContext->CachedFatVbo],
  1670. &FatEntry );
  1671. }
  1672. //
  1673. // Now that we're done we can set the fat dirty
  1674. //
  1675. FatStructureContext->CachedFatDirty = TRUE;
  1676. return ESUCCESS;
  1677. }
  1678. //
  1679. // Internal support routine
  1680. //
  1681. ARC_STATUS
  1682. FatFlushFatEntries (
  1683. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  1684. IN ULONG DeviceId
  1685. )
  1686. /*++
  1687. Routine Description:
  1688. This routine flushes out any dirty cached fat entries to the volume.
  1689. Arguments:
  1690. FatStructureContext - Supplies the structure context for the operation
  1691. DeviceId - Supplies the Device for the operation
  1692. Return Value:
  1693. ESUCCESS is returned if the operation is successful. Otherwise,
  1694. an unsuccessful status is returned that describes the reason for failure.
  1695. --*/
  1696. {
  1697. ULONG BytesPerFat;
  1698. ULONG AmountToWrite;
  1699. ULONG i;
  1700. //
  1701. // Compute the actual number of bytes that we need to write. We do this
  1702. // because we don't want to overwrite beyond the fat.
  1703. //
  1704. BytesPerFat = FatBytesPerFat(&FatStructureContext->Bpb);
  1705. if (FatStructureContext->CachedFatVbo + FAT_CACHE_SIZE <= BytesPerFat) {
  1706. AmountToWrite = FAT_CACHE_SIZE;
  1707. } else {
  1708. AmountToWrite = BytesPerFat - FatStructureContext->CachedFatVbo;
  1709. }
  1710. //
  1711. // For each fat table on the volume we will calculate the lbo for the operation
  1712. // and then write out the cached fat
  1713. //
  1714. for (i = 0; i < FatStructureContext->Bpb.Fats; i += 1) {
  1715. LBO Lbo;
  1716. Lbo = FatStructureContext->CachedFatVbo +
  1717. FatFirstFatAreaLbo(&FatStructureContext->Bpb) +
  1718. (i * BytesPerFat);
  1719. DiskWrite( DeviceId,
  1720. Lbo,
  1721. AmountToWrite,
  1722. FatStructureContext->CachedFat );
  1723. }
  1724. //
  1725. // we are all done so now mark the fat clean
  1726. //
  1727. FatStructureContext->CachedFatDirty = FALSE;
  1728. return ESUCCESS;
  1729. }
  1730. //
  1731. // Internal support routine
  1732. //
  1733. LBO
  1734. FatIndexToLbo (
  1735. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  1736. IN FAT_ENTRY FatIndex
  1737. )
  1738. /*++
  1739. Routine Description:
  1740. This procedure translates a fat index into its corresponding lbo.
  1741. Arguments:
  1742. FatStructureContext - Supplies the volume structure for the operation
  1743. Entry - Supplies the fat entry to examine.
  1744. Return Value:
  1745. The LBO for the input fat index is returned
  1746. --*/
  1747. {
  1748. //
  1749. // The formula for translating an index into an lbo is to take the index subtract
  1750. // 2 (because index values 0 and 1 are reserved) multiply that by the bytes per
  1751. // cluster and add the results to the first file area lbo.
  1752. //
  1753. return ((FatIndex-2) * (LBO) FatBytesPerCluster(&FatStructureContext->Bpb))
  1754. + FatFileAreaLbo(&FatStructureContext->Bpb);
  1755. }
  1756. //
  1757. // Internal support routine
  1758. //
  1759. ARC_STATUS
  1760. FatSearchForDirent (
  1761. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  1762. IN ULONG DeviceId,
  1763. IN FAT_ENTRY DirectoriesStartingIndex,
  1764. IN PFAT8DOT3 FileName,
  1765. OUT PDIRENT Dirent,
  1766. OUT PLBO Lbo,
  1767. IN BOOLEAN IsDoubleSpace
  1768. )
  1769. /*++
  1770. Routine Description:
  1771. The procedure searches the indicated directory for a dirent that matches
  1772. the input file name.
  1773. Arguments:
  1774. FatStructureContext - Supplies the structure context for the operation
  1775. DeviceId - Supplies the Device id for the operation
  1776. DirectoriesStartingIndex - Supplies the fat index of the directory we are
  1777. to search. A value of zero indicates that we are searching the root directory
  1778. of a non-FAT32 volume. FAT32 volumes will have a non-zero index.
  1779. FileName - Supplies the file name to look for. The name must have already been
  1780. biased by the 0xe5 transmogrification
  1781. Dirent - The caller supplies the memory for a dirent and this procedure will
  1782. fill in the dirent if one is located
  1783. Lbo - Receives the Lbo of the dirent if one is located
  1784. IsDoubleSpace - Indicates if the search is being done on a double space volume
  1785. Return Value:
  1786. ESUCCESS is returned if the operation is successful. Otherwise,
  1787. an unsuccessful status is returned that describes the reason for failure.
  1788. --*/
  1789. {
  1790. PDIRENT DirentBuffer;
  1791. UCHAR Buffer[ 16 * sizeof(DIRENT) + 256 ];
  1792. ULONG i;
  1793. ULONG j;
  1794. ULONG BytesPerCluster;
  1795. FAT_ENTRY FatEntry;
  1796. CLUSTER_TYPE ClusterType;
  1797. DirentBuffer = (PDIRENT)ALIGN_BUFFER( &Buffer[0] );
  1798. FatDebugOutput83("FatSearchForDirent: %s\r\n", FileName, 0, 0);
  1799. //****if (IsDoubleSpace) { (*FileName)[11] = 0; DbgPrint("FatSearchForDirent(%0x,%0x,%0x,\"%11s\",%0x,%0x,%0x)\n", FatStructureContext, DeviceId, DirectoriesStartingIndex, FileName, Dirent, Lbo, IsDoubleSpace); }
  1800. //
  1801. // Check if this is the root directory that is being searched
  1802. //
  1803. if (DirectoriesStartingIndex == FAT_CLUSTER_AVAILABLE) {
  1804. VBO Vbo;
  1805. ULONG RootLbo = FatRootDirectoryLbo(&FatStructureContext->Bpb);
  1806. ULONG RootSize = FatRootDirectorySize(&FatStructureContext->Bpb);
  1807. //
  1808. // For the root directory we'll zoom down the dirents until we find
  1809. // a match, or run out of dirents or hit the never used dirent.
  1810. // The outer loop reads in 512 bytes of the directory at a time into
  1811. // dirent buffer.
  1812. //
  1813. for (Vbo = 0; Vbo < RootSize; Vbo += 16 * sizeof(DIRENT)) {
  1814. *Lbo = Vbo + RootLbo;
  1815. DiskRead( DeviceId, *Lbo, 16 * sizeof(DIRENT), DirentBuffer, CACHE_NEW_DATA, IsDoubleSpace );
  1816. //
  1817. // The inner loop cycles through the 16 dirents that we've just read in
  1818. //
  1819. for (i = 0; i < 16; i += 1) {
  1820. //
  1821. // Check if we've found a non label match for file name, and if so
  1822. // then copy the buffer into the dirent and set the real lbo
  1823. // of the dirent and return
  1824. //
  1825. if (!FlagOn(DirentBuffer[i].Attributes, FAT_DIRENT_ATTR_VOLUME_ID ) &&
  1826. AreNamesEqual(&DirentBuffer[i].FileName, FileName)) {
  1827. for (j = 0; j < sizeof(DIRENT); j += 1) {
  1828. ((PCHAR)Dirent)[j] = ((PCHAR)DirentBuffer)[(i * sizeof(DIRENT)) + j];
  1829. }
  1830. *Lbo = Vbo + RootLbo + (i * sizeof(DIRENT));
  1831. return ESUCCESS;
  1832. }
  1833. if (DirentBuffer[i].FileName[0] == FAT_DIRENT_NEVER_USED) {
  1834. return ENOENT;
  1835. }
  1836. }
  1837. }
  1838. return ENOENT;
  1839. }
  1840. //
  1841. // If we get here we need to search a non-root directory. The alrogithm
  1842. // for doing the search is that for each cluster we read in each dirent
  1843. // until we find a match, or run out of clusters, or hit the never used
  1844. // dirent. First set some local variables and then get the cluster type
  1845. // of the first cluster
  1846. //
  1847. BytesPerCluster = FatBytesPerCluster( &FatStructureContext->Bpb );
  1848. FatEntry = DirectoriesStartingIndex;
  1849. ClusterType = FatInterpretClusterType( FatStructureContext, FatEntry );
  1850. //
  1851. // Now loop through each cluster, and compute the starting Lbo for each cluster
  1852. // that we encounter
  1853. //
  1854. while (ClusterType == FatClusterNext) {
  1855. LBO ClusterLbo;
  1856. ULONG Offset;
  1857. ClusterLbo = FatIndexToLbo( FatStructureContext, FatEntry );
  1858. //
  1859. // Now for each dirent in the cluster compute the lbo, read in the dirent
  1860. // and check for a match, the outer loop reads in 512 bytes of dirents at
  1861. // a time.
  1862. //
  1863. for (Offset = 0; Offset < BytesPerCluster; Offset += 16 * sizeof(DIRENT)) {
  1864. *Lbo = Offset + ClusterLbo;
  1865. DiskRead( DeviceId, *Lbo, 16 * sizeof(DIRENT), DirentBuffer, CACHE_NEW_DATA, IsDoubleSpace );
  1866. //
  1867. // The inner loop cycles through the 16 dirents that we've just read in
  1868. //
  1869. for (i = 0; i < 16; i += 1) {
  1870. //
  1871. // Check if we've found a for file name, and if so
  1872. // then copy the buffer into the dirent and set the real lbo
  1873. // of the dirent and return
  1874. //
  1875. if (!FlagOn(DirentBuffer[i].Attributes, FAT_DIRENT_ATTR_VOLUME_ID ) &&
  1876. AreNamesEqual(&DirentBuffer[i].FileName, FileName)) {
  1877. for (j = 0; j < sizeof(DIRENT); j += 1) {
  1878. ((PCHAR)Dirent)[j] = ((PCHAR)DirentBuffer)[(i * sizeof(DIRENT)) + j];
  1879. }
  1880. *Lbo = Offset + ClusterLbo + (i * sizeof(DIRENT));
  1881. return ESUCCESS;
  1882. }
  1883. if (DirentBuffer[i].FileName[0] == FAT_DIRENT_NEVER_USED) {
  1884. return ENOENT;
  1885. }
  1886. }
  1887. }
  1888. //
  1889. // Now that we've exhausted the current cluster we need to read
  1890. // in the next cluster. So locate the next fat entry in the chain
  1891. // and go back to the top of the while loop.
  1892. //
  1893. LookupFatEntry( FatStructureContext, DeviceId, FatEntry, (PULONG) &FatEntry, IsDoubleSpace );
  1894. ClusterType = FatInterpretClusterType(FatStructureContext, FatEntry);
  1895. }
  1896. return ENOENT;
  1897. }
  1898. //
  1899. // Internal support routine
  1900. //
  1901. ARC_STATUS
  1902. FatCreateDirent (
  1903. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  1904. IN ULONG DeviceId,
  1905. IN FAT_ENTRY DirectoriesStartingIndex,
  1906. IN PDIRENT Dirent,
  1907. OUT PLBO Lbo
  1908. )
  1909. /*++
  1910. Routine Description:
  1911. This procedure allocates and write out a new dirent for a data file in the
  1912. specified directory. It assumes that the file name does not already exist.
  1913. Arguments:
  1914. FatStructureContext - Supplies the structure context for the operation
  1915. DeviceId - Supplies the device id for the operation
  1916. DirectoriesStartingIndex - Supplies the fat index of the directory we are
  1917. to use. A value of zero indicates that we are using the root directory
  1918. Dirent - Supplies a copy of the dirent to put out on the disk
  1919. Lbo - Recieves the Lbo of where the dirent is placed
  1920. Return Value:
  1921. ESUCCESS is returned if the operation is successful. Otherwise,
  1922. an unsuccessful status is returned that describes the reason for failure.
  1923. --*/
  1924. {
  1925. DIRENT TemporaryDirent;
  1926. ULONG BytesPerCluster;
  1927. FAT_ENTRY FatEntry;
  1928. FAT_ENTRY PreviousEntry;
  1929. //
  1930. // Check if this is the root directory that is being used
  1931. //
  1932. if (DirectoriesStartingIndex == FAT_CLUSTER_AVAILABLE) {
  1933. VBO Vbo;
  1934. ULONG RootLbo = FatRootDirectoryLbo(&FatStructureContext->Bpb);
  1935. ULONG RootSize = FatRootDirectorySize(&FatStructureContext->Bpb);
  1936. //
  1937. // For the root directory we'll zoom down the dirents until we find
  1938. // a the never used (or deleted) dirent, if we never find one then the
  1939. // directory is full.
  1940. //
  1941. for (Vbo = 0; Vbo < RootSize; Vbo += sizeof(DIRENT)) {
  1942. *Lbo = Vbo + RootLbo;
  1943. DiskRead( DeviceId, *Lbo, sizeof(DIRENT), &TemporaryDirent, CACHE_NEW_DATA, FALSE );
  1944. if ((TemporaryDirent.FileName[0] == FAT_DIRENT_DELETED) ||
  1945. (TemporaryDirent.FileName[0] == FAT_DIRENT_NEVER_USED)) {
  1946. //
  1947. // This dirent is free so write out the dirent, and we're done.
  1948. //
  1949. DiskWrite( DeviceId, *Lbo, sizeof(DIRENT), Dirent );
  1950. return ESUCCESS;
  1951. }
  1952. }
  1953. return ENOSPC;
  1954. }
  1955. //
  1956. // If we get here we need to use a non-root directory. The alrogithm
  1957. // for doing the work is that for each cluster we read in each dirent
  1958. // until we hit a never used dirent or run out of clusters. First set
  1959. // some local variables and then get the cluster type of the first
  1960. // cluster
  1961. //
  1962. BytesPerCluster = FatBytesPerCluster( &FatStructureContext->Bpb );
  1963. FatEntry = DirectoriesStartingIndex;
  1964. //
  1965. // Now loop through each cluster, and compute the starting Lbo for each cluster
  1966. // that we encounter
  1967. //
  1968. while (TRUE) {
  1969. LBO ClusterLbo;
  1970. ULONG Offset;
  1971. ClusterLbo = FatIndexToLbo( FatStructureContext, FatEntry );
  1972. //
  1973. // Now for each dirent in the cluster compute the lbo, read in the dirent
  1974. // and check if it is available.
  1975. //
  1976. for (Offset = 0; Offset < BytesPerCluster; Offset += sizeof(DIRENT)) {
  1977. *Lbo = Offset + ClusterLbo;
  1978. DiskRead( DeviceId, *Lbo, sizeof(DIRENT), &TemporaryDirent, CACHE_NEW_DATA, FALSE );
  1979. if ((TemporaryDirent.FileName[0] == FAT_DIRENT_DELETED) ||
  1980. (TemporaryDirent.FileName[0] == FAT_DIRENT_NEVER_USED)) {
  1981. //
  1982. // This dirent is free so write out the dirent, and we're done.
  1983. //
  1984. DiskWrite( DeviceId, *Lbo, sizeof(DIRENT), Dirent );
  1985. return ESUCCESS;
  1986. }
  1987. }
  1988. //
  1989. // Now that we've exhausted the current cluster we need to read
  1990. // in the next cluster. So locate the next fat entry in the chain.
  1991. // Set previous entry to be the saved entry just in case we run off
  1992. // the chain and need to allocate another cluster.
  1993. //
  1994. PreviousEntry = FatEntry;
  1995. LookupFatEntry( FatStructureContext, DeviceId, FatEntry, (PULONG) &FatEntry, FALSE );
  1996. //
  1997. // If there isn't another cluster in the chain then we need to allocate a
  1998. // new cluster, and set previous entry to point to it.
  1999. //
  2000. if (FatInterpretClusterType(FatStructureContext, FatEntry) != FatClusterNext) {
  2001. AllocateClusters( FatStructureContext, DeviceId, 1, PreviousEntry, (PULONG) &FatEntry );
  2002. SetFatEntry( FatStructureContext, DeviceId, PreviousEntry, FatEntry );
  2003. }
  2004. }
  2005. return ENOSPC;
  2006. }
  2007. //
  2008. // Internal support routine
  2009. //
  2010. VOID
  2011. FatSetDirent (
  2012. IN PFAT8DOT3 FileName,
  2013. IN OUT PDIRENT Dirent,
  2014. IN UCHAR Attributes
  2015. )
  2016. /*++
  2017. Routine Description:
  2018. This routine sets up the dirent
  2019. Arguments:
  2020. FileName - Supplies the name to store in the dirent
  2021. Dirent - Receives the current date and time
  2022. Attributes - Supplies the attributes to initialize the dirent with
  2023. Return Value:
  2024. None.
  2025. --*/
  2026. {
  2027. PTIME_FIELDS Time;
  2028. ULONG i;
  2029. for (i = 0; i < sizeof(FAT8DOT3); i+= 1) {
  2030. Dirent->FileName[i] = (*FileName)[i];
  2031. }
  2032. Dirent->Attributes = (UCHAR)(Attributes | FAT_DIRENT_ATTR_ARCHIVE);
  2033. Time = ArcGetTime();
  2034. Dirent->LastWriteTime.Time.DoubleSeconds = (USHORT)(Time->Second/2);
  2035. Dirent->LastWriteTime.Time.Minute = Time->Minute;
  2036. Dirent->LastWriteTime.Time.Hour = Time->Hour;
  2037. Dirent->LastWriteTime.Date.Day = Time->Day;
  2038. Dirent->LastWriteTime.Date.Month = Time->Month;
  2039. Dirent->LastWriteTime.Date.Year = (USHORT)(Time->Year - 1980);
  2040. return;
  2041. }
  2042. //
  2043. // Internal support routine
  2044. //
  2045. ARC_STATUS
  2046. FatLoadMcb (
  2047. IN ULONG FileId,
  2048. IN VBO StartingVbo,
  2049. IN BOOLEAN IsDoubleSpace
  2050. )
  2051. /*++
  2052. Routine Description:
  2053. This routine loads into the cached mcb table the the retrival information for
  2054. the starting vbo.
  2055. Arguments:
  2056. FileId - Supplies the FileId for the operation
  2057. StartingVbo - Supplies the starting vbo to use when loading the mcb
  2058. IsDoubleSpace - Indicates if the operation is being done on a double space volume
  2059. Return Value:
  2060. ESUCCESS is returned if the operation is successful. Otherwise,
  2061. an unsuccessful status is returned that describes the reason for failure.
  2062. --*/
  2063. {
  2064. PBL_FILE_TABLE FileTableEntry;
  2065. PFAT_STRUCTURE_CONTEXT FatStructureContext;
  2066. PFAT_MCB Mcb;
  2067. ULONG DeviceId;
  2068. ULONG BytesPerCluster;
  2069. FAT_ENTRY FatEntry;
  2070. CLUSTER_TYPE ClusterType;
  2071. VBO Vbo;
  2072. //****if (IsDoubleSpace) { DbgPrint("FatLoadMcb(%0x,%0x,%0x)\n", FileId, StartingVbo, IsDoubleSpace); }
  2073. //
  2074. // Preload some of the local variables
  2075. //
  2076. FileTableEntry = &BlFileTable[FileId];
  2077. FatStructureContext = (PFAT_STRUCTURE_CONTEXT)FileTableEntry->StructureContext;
  2078. Mcb = &FatStructureContext->Mcb;
  2079. DeviceId = FileTableEntry->DeviceId;
  2080. BytesPerCluster = FatBytesPerCluster(&FatStructureContext->Bpb);
  2081. if (IsDoubleSpace) { DeviceId = FileId; }
  2082. //
  2083. // Set the file id in the structure context, and also set the mcb to be initially
  2084. // empty
  2085. //
  2086. FatStructureContext->FileId = FileId;
  2087. Mcb->InUse = 0;
  2088. Mcb->Vbo[0] = 0;
  2089. if (!IsBpbFat32(&FatStructureContext->Bpb)) {
  2090. //
  2091. // Check if this is the root directory. If it is then we build the single
  2092. // run mcb entry for the root directory.
  2093. //
  2094. if (FileTableEntry->u.FatFileContext.DirentLbo == 0) {
  2095. Mcb->InUse = 1;
  2096. Mcb->Lbo[0] = FatRootDirectoryLbo(&FatStructureContext->Bpb);
  2097. Mcb->Vbo[1] = FatRootDirectorySize(&FatStructureContext->Bpb);
  2098. return ESUCCESS;
  2099. }
  2100. //
  2101. // For all other files/directories we need to do some work. First get the fat
  2102. // entry and cluster type of the fat entry stored in the dirent
  2103. //
  2104. FatEntry = FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile;
  2105. } else {
  2106. //
  2107. // Check if this is the root directory. If it is then we use
  2108. // the BPB values to start the run.
  2109. //
  2110. if (FileTableEntry->u.FatFileContext.DirentLbo == 0) {
  2111. FatEntry = FatStructureContext->Bpb.RootDirFirstCluster;
  2112. } else {
  2113. //
  2114. // For all other files/directories we use the dirent values
  2115. //
  2116. if (IsBpbFat32(&FatStructureContext->Bpb)) {
  2117. FatEntry = FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile |
  2118. (FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFileHi << 16);
  2119. } else {
  2120. FatEntry = FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile;
  2121. }
  2122. }
  2123. }
  2124. ClusterType = FatInterpretClusterType(FatStructureContext, FatEntry);
  2125. //
  2126. // Scan through the fat until we reach the vbo we're after and then build the
  2127. // mcb for the file
  2128. //
  2129. for (Vbo = BytesPerCluster; Vbo < StartingVbo; Vbo += BytesPerCluster) {
  2130. //
  2131. // Check if the file does not have any allocation beyond this point in which
  2132. // case the mcb we return is empty
  2133. //
  2134. if (ClusterType != FatClusterNext) {
  2135. return ESUCCESS;
  2136. }
  2137. LookupFatEntry( FatStructureContext, DeviceId, FatEntry, (PULONG) &FatEntry, IsDoubleSpace );
  2138. ClusterType = FatInterpretClusterType(FatStructureContext, FatEntry);
  2139. }
  2140. //
  2141. // We need to check again if the file does not have any allocation beyond this
  2142. // point in which case the mcb we return is empty
  2143. //
  2144. if (ClusterType != FatClusterNext) {
  2145. return ESUCCESS;
  2146. }
  2147. //
  2148. // At this point FatEntry denotes another cluster, and it happens to be the
  2149. // cluster we want to start loading into the mcb. So set up the first run in
  2150. // the mcb to be this cluster, with a size of a single cluster.
  2151. //
  2152. Mcb->InUse = 1;
  2153. Mcb->Vbo[0] = Vbo - BytesPerCluster;
  2154. Mcb->Lbo[0] = FatIndexToLbo( FatStructureContext, FatEntry );
  2155. Mcb->Vbo[1] = Vbo;
  2156. //
  2157. // Now we'll scan through the fat chain until we either exhaust the fat chain
  2158. // or we fill up the mcb
  2159. //
  2160. while (TRUE) {
  2161. LBO Lbo;
  2162. //
  2163. // Get the next fat entry and interpret its cluster type
  2164. //
  2165. LookupFatEntry( FatStructureContext, DeviceId, FatEntry, (PULONG) &FatEntry, IsDoubleSpace );
  2166. ClusterType = FatInterpretClusterType(FatStructureContext, FatEntry);
  2167. if (ClusterType != FatClusterNext) {
  2168. return ESUCCESS;
  2169. }
  2170. //
  2171. // Now calculate the lbo for this cluster and determine if it
  2172. // is a continuation of the previous run or a start of a new run
  2173. //
  2174. Lbo = FatIndexToLbo(FatStructureContext, FatEntry);
  2175. //
  2176. // It is a continuation if the lbo of the last run plus the current
  2177. // size of the run is equal to the lbo for the next cluster. If it
  2178. // is a contination then we only need to add a cluster amount to the
  2179. // last vbo to increase the run size. If it is a new run then
  2180. // we need to check if the run will fit, and if so then add in the
  2181. // new run.
  2182. //
  2183. if ((Mcb->Lbo[Mcb->InUse-1] + (Mcb->Vbo[Mcb->InUse] - Mcb->Vbo[Mcb->InUse-1])) == Lbo) {
  2184. Mcb->Vbo[Mcb->InUse] += BytesPerCluster;
  2185. } else {
  2186. if ((Mcb->InUse + 1) >= FAT_MAXIMUM_MCB) {
  2187. return ESUCCESS;
  2188. }
  2189. Mcb->InUse += 1;
  2190. Mcb->Lbo[Mcb->InUse-1] = Lbo;
  2191. Mcb->Vbo[Mcb->InUse] = Mcb->Vbo[Mcb->InUse-1] + BytesPerCluster;
  2192. }
  2193. }
  2194. return ESUCCESS;
  2195. }
  2196. //
  2197. // Internal support routine
  2198. //
  2199. ARC_STATUS
  2200. FatVboToLbo (
  2201. IN ULONG FileId,
  2202. IN VBO Vbo,
  2203. OUT PLBO Lbo,
  2204. OUT PULONG ByteCount,
  2205. IN BOOLEAN IsDoubleSpace
  2206. )
  2207. /*++
  2208. Routine Description:
  2209. This routine computes the run denoted by the input vbo to into its
  2210. corresponding lbo and also returns the number of bytes remaining in
  2211. the run.
  2212. Arguments:
  2213. Vbo - Supplies the Vbo to match
  2214. Lbo - Recieves the corresponding Lbo
  2215. ByteCount - Receives the number of bytes remaining in the run
  2216. IsDoubleSpace - Indicates if the operation is being done on a double space volume
  2217. Return Value:
  2218. ESUCCESS is returned if the operation is successful. Otherwise,
  2219. an unsuccessful status is returned that describes the reason for failure.
  2220. --*/
  2221. {
  2222. PFAT_STRUCTURE_CONTEXT FatStructureContext;
  2223. PFAT_MCB Mcb;
  2224. ULONG i;
  2225. //****if (IsDoubleSpace) { DbgPrint("FatVboToLbo(%0x,%0x,%0x,%0x,%0x)\n", FileId, Vbo, Lbo, ByteCount, IsDoubleSpace); }
  2226. FatStructureContext = (PFAT_STRUCTURE_CONTEXT)BlFileTable[FileId].StructureContext;
  2227. Mcb = &FatStructureContext->Mcb;
  2228. //
  2229. // Check if the mcb is for the correct file id and has the range we're asking for.
  2230. // If it doesn't then call load mcb to load in the right range.
  2231. //
  2232. if ((FileId != FatStructureContext->FileId) ||
  2233. (Vbo < Mcb->Vbo[0]) || (Vbo >= Mcb->Vbo[Mcb->InUse])) {
  2234. LoadMcb(FileId, Vbo, IsDoubleSpace);
  2235. }
  2236. //
  2237. // Now search for the slot where the Vbo fits in the mcb. Note that
  2238. // we could also do a binary search here but because the run count
  2239. // is probably small the extra overhead of a binary search doesn't
  2240. // buy us anything
  2241. //
  2242. for (i = 0; i < Mcb->InUse; i += 1) {
  2243. //
  2244. // We found our slot if the vbo we're after is less then the
  2245. // next mcb's vbo
  2246. //
  2247. if (Vbo < Mcb->Vbo[i+1]) {
  2248. //
  2249. // Compute the corresponding lbo which is the stored lbo plus
  2250. // the difference between the stored vbo and the vbo we're
  2251. // looking up. Also compute the byte count which is the
  2252. // difference between the current vbo we're looking up and
  2253. // the vbo for the next run.
  2254. //
  2255. *Lbo = Mcb->Lbo[i] + (Vbo - Mcb->Vbo[i]);
  2256. *ByteCount = Mcb->Vbo[i+1] - Vbo;
  2257. //
  2258. // and return success to our caller
  2259. //
  2260. return ESUCCESS;
  2261. }
  2262. }
  2263. //
  2264. // If we really reach here we have an error, most likely because the file is
  2265. // not large enough for the requested Vbo.
  2266. //
  2267. return EINVAL;
  2268. }
  2269. //
  2270. // Internal support routine
  2271. //
  2272. ARC_STATUS
  2273. FatIncreaseFileAllocation (
  2274. IN ULONG FileId,
  2275. IN ULONG ByteSize
  2276. )
  2277. /*++
  2278. Routine Description:
  2279. This procedure increases the file allocation to be at minimum the indicated
  2280. size.
  2281. Arguments:
  2282. FileId - Supplies the file id being processed
  2283. ByteSize - Supplies the minimum byte size for file allocation
  2284. Return Value:
  2285. ESUCCESS is returned if the operation is successful. Otherwise,
  2286. an unsuccessful status is returned that describes the reason for failure.
  2287. --*/
  2288. {
  2289. PBL_FILE_TABLE FileTableEntry;
  2290. PFAT_STRUCTURE_CONTEXT FatStructureContext;
  2291. ULONG DeviceId;
  2292. ULONG BytesPerCluster;
  2293. ULONG NumberOfClustersNeeded;
  2294. FAT_ENTRY FatEntry;
  2295. CLUSTER_TYPE ClusterType;
  2296. FAT_ENTRY PreviousEntry;
  2297. ULONG i;
  2298. //
  2299. // Preload some of the local variables
  2300. //
  2301. FileTableEntry = &BlFileTable[FileId];
  2302. FatStructureContext = (PFAT_STRUCTURE_CONTEXT)FileTableEntry->StructureContext;
  2303. DeviceId = FileTableEntry->DeviceId;
  2304. BytesPerCluster = FatBytesPerCluster(&FatStructureContext->Bpb);
  2305. //
  2306. // Check if this is the root directory. If it is then check if the allocation
  2307. // increase is already accommodated in the volume
  2308. //
  2309. if (FileTableEntry->u.FatFileContext.DirentLbo == 0) {
  2310. if (FatRootDirectorySize(&FatStructureContext->Bpb) >= ByteSize) {
  2311. return ESUCCESS;
  2312. } else {
  2313. return ENOSPC;
  2314. }
  2315. }
  2316. //
  2317. // Compute the actual number of clusters needed to satisfy the request
  2318. // Also get the first fat entry and its cluster type from the dirent.
  2319. //
  2320. NumberOfClustersNeeded = (ByteSize + BytesPerCluster - 1) / BytesPerCluster;
  2321. if (IsBpbFat32(&FatStructureContext->Bpb)) {
  2322. FatEntry = FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile |
  2323. (FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFileHi << 16);
  2324. } else {
  2325. FatEntry = FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile;
  2326. }
  2327. ClusterType = FatInterpretClusterType(FatStructureContext, FatEntry);
  2328. //
  2329. // Previous Entry is as a hint to allocate new space and to show us where
  2330. // the end of the current fat chain is located
  2331. //
  2332. PreviousEntry = 2;
  2333. //
  2334. // We loop for the number of clusters we need trying to go down the fat chain.
  2335. // When we exit i is either number of clusters in the file (if less then
  2336. // the number of clusters we need) or it is set equal to the number of clusters
  2337. // we need
  2338. //
  2339. for (i = 0; i < NumberOfClustersNeeded; i += 1) {
  2340. if (ClusterType != FatClusterNext) { break; }
  2341. PreviousEntry = FatEntry;
  2342. LookupFatEntry( FatStructureContext, DeviceId, PreviousEntry, (PULONG) &FatEntry, FALSE );
  2343. ClusterType = FatInterpretClusterType(FatStructureContext, FatEntry);
  2344. }
  2345. if (i >= NumberOfClustersNeeded) {
  2346. return ESUCCESS;
  2347. }
  2348. //
  2349. // At this point previous entry points to the last entry and i contains the
  2350. // number of clusters in the file. We now need to build up the allocation
  2351. //
  2352. AllocateClusters( FatStructureContext,
  2353. DeviceId,
  2354. NumberOfClustersNeeded - i,
  2355. PreviousEntry,
  2356. (PULONG) &FatEntry );
  2357. //
  2358. // We have our additional allocation, so now figure out if we need to chain off of
  2359. // the dirent or it we already have a few clusters in the chain and we
  2360. // need to munge the fat.
  2361. //
  2362. if (FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile == FAT_CLUSTER_AVAILABLE) {
  2363. FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile = (USHORT)FatEntry;
  2364. FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFileHi =
  2365. (USHORT)(FatEntry >> 16);
  2366. DiskWrite( DeviceId,
  2367. FileTableEntry->u.FatFileContext.DirentLbo,
  2368. sizeof(DIRENT),
  2369. &FileTableEntry->u.FatFileContext.Dirent );
  2370. } else {
  2371. SetFatEntry( FatStructureContext, DeviceId, PreviousEntry, FatEntry );
  2372. }
  2373. return ESUCCESS;
  2374. }
  2375. //
  2376. // Internal support routine
  2377. //
  2378. ARC_STATUS
  2379. FatTruncateFileAllocation (
  2380. IN ULONG FileId,
  2381. IN ULONG ByteSize
  2382. )
  2383. /*++
  2384. Routine Description:
  2385. This procedure decreases the file allocation to be at maximum the indicated
  2386. size.
  2387. Arguments:
  2388. FileId - Supplies the file id being processed
  2389. ByteSize - Supplies the maximum byte size for file allocation
  2390. Return Value:
  2391. ESUCCESS is returned if the operation is successful. Otherwise,
  2392. an unsuccessful status is returned that describes the reason for failure.
  2393. --*/
  2394. {
  2395. PBL_FILE_TABLE FileTableEntry;
  2396. PFAT_STRUCTURE_CONTEXT FatStructureContext;
  2397. ULONG DeviceId;
  2398. ULONG BytesPerCluster;
  2399. ULONG NumberOfClustersNeeded;
  2400. FAT_ENTRY FatEntry;
  2401. CLUSTER_TYPE ClusterType;
  2402. FAT_ENTRY CurrentIndex;
  2403. ULONG i;
  2404. //
  2405. // Preload some of the local variables
  2406. //
  2407. FileTableEntry = &BlFileTable[FileId];
  2408. FatStructureContext = (PFAT_STRUCTURE_CONTEXT)FileTableEntry->StructureContext;
  2409. DeviceId = FileTableEntry->DeviceId;
  2410. BytesPerCluster = FatBytesPerCluster(&FatStructureContext->Bpb);
  2411. //
  2412. // Check if this is the root directory. If it is then noop this request
  2413. //
  2414. if (FileTableEntry->u.FatFileContext.DirentLbo == 0) {
  2415. return ESUCCESS;
  2416. }
  2417. //
  2418. // Compute the actual number of clusters needed to satisfy the request
  2419. // Also get the first fat entry and its cluster type from the dirent
  2420. //
  2421. NumberOfClustersNeeded = (ByteSize + BytesPerCluster - 1) / BytesPerCluster;
  2422. if (IsBpbFat32(&FatStructureContext->Bpb)) {
  2423. FatEntry = FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile |
  2424. (FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFileHi << 16);
  2425. } else {
  2426. FatEntry = FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile;
  2427. }
  2428. ClusterType = FatInterpretClusterType(FatStructureContext, FatEntry);
  2429. //
  2430. // The current index variable is used to indicate where we extracted the current
  2431. // fat entry value from. It has a value of 0 we got the fat entry from the
  2432. // dirent.
  2433. //
  2434. CurrentIndex = FAT_CLUSTER_AVAILABLE;
  2435. //
  2436. // Now loop through the fat chain for the number of clusters needed.
  2437. // If we run out of the chain before we run out of clusters needed then the
  2438. // current allocation is already smaller than necessary.
  2439. //
  2440. for (i = 0; i < NumberOfClustersNeeded; i += 1) {
  2441. //
  2442. // If we run out of the chain before we run out of clusters needed then the
  2443. // current allocation is already smaller than necessary.
  2444. //
  2445. if (ClusterType != FatClusterNext) { return ESUCCESS; }
  2446. //
  2447. // Update the current index, and read in a new fat entry and interpret its
  2448. // type
  2449. //
  2450. CurrentIndex = FatEntry;
  2451. LookupFatEntry( FatStructureContext, DeviceId, CurrentIndex, (PULONG) &FatEntry, FALSE );
  2452. ClusterType = FatInterpretClusterType(FatStructureContext, FatEntry);
  2453. }
  2454. //
  2455. // If we get here then we've found that the current allocation is equal to or
  2456. // larger than what we want. It is equal if the current cluster type does not
  2457. // point to another cluster. The first thing we have to do is terminate the
  2458. // fat chain correctly. If the current index is zero then we zero out the
  2459. // dirent, otherwise we need to set the value to be last cluster.
  2460. //
  2461. if (CurrentIndex == FAT_CLUSTER_AVAILABLE) {
  2462. FatEntry = FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile;
  2463. if (IsBpbFat32(&FatStructureContext->Bpb)) {
  2464. FatEntry |= FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFileHi << 16;
  2465. }
  2466. if (FatEntry != FAT_CLUSTER_AVAILABLE) {
  2467. //
  2468. // By setting the dirent we set in a new date.
  2469. //
  2470. FatSetDirent( &FileTableEntry->u.FatFileContext.Dirent.FileName,
  2471. &FileTableEntry->u.FatFileContext.Dirent,
  2472. 0 );
  2473. FatEntry = FAT_CLUSTER_AVAILABLE;
  2474. FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFile = (USHORT)FatEntry;
  2475. if (IsBpbFat32(&FatStructureContext->Bpb)) {
  2476. FileTableEntry->u.FatFileContext.Dirent.FirstClusterOfFileHi =
  2477. (USHORT)(FatEntry >> 16);
  2478. }
  2479. FileTableEntry->u.FatFileContext.Dirent.FileSize = 0;
  2480. DiskWrite( DeviceId,
  2481. FileTableEntry->u.FatFileContext.DirentLbo,
  2482. sizeof(DIRENT),
  2483. &FileTableEntry->u.FatFileContext.Dirent );
  2484. }
  2485. } else {
  2486. if (ClusterType != FatClusterLast) {
  2487. SetFatEntry( FatStructureContext, DeviceId, CurrentIndex, FAT_CLUSTER_LAST );
  2488. }
  2489. }
  2490. //
  2491. // Now while there are clusters left to deallocate then we need to go down the
  2492. // chain freeing up the clusters
  2493. //
  2494. while (ClusterType == FatClusterNext) {
  2495. //
  2496. // Read in the value at the next fat entry and interpret its cluster type
  2497. //
  2498. CurrentIndex = FatEntry;
  2499. LookupFatEntry( FatStructureContext, DeviceId, CurrentIndex, (PULONG) &FatEntry, FALSE );
  2500. ClusterType = FatInterpretClusterType(FatStructureContext, FatEntry);
  2501. //
  2502. // Now deallocate the cluster at the current index
  2503. //
  2504. SetFatEntry( FatStructureContext, DeviceId, CurrentIndex, FAT_CLUSTER_AVAILABLE );
  2505. }
  2506. return ESUCCESS;
  2507. }
  2508. //
  2509. // Internal support routine
  2510. //
  2511. ARC_STATUS
  2512. FatAllocateClusters (
  2513. IN PFAT_STRUCTURE_CONTEXT FatStructureContext,
  2514. IN ULONG DeviceId,
  2515. IN ULONG ClusterCount,
  2516. IN ULONG Hint,
  2517. OUT PULONG AllocatedEntry
  2518. )
  2519. /*++
  2520. Routine Description:
  2521. This procedure allocates a new cluster, set its entry to be the last one,
  2522. and zeros out the cluster.
  2523. Arguments:
  2524. FatStructureContext - Supplies the structure context for the operation
  2525. DeviceId - Supplies the device id for the operation
  2526. ClusterCount - Supplies the number of clusters we need to allocate
  2527. Hint - Supplies a hint to start from when looking for a free cluster
  2528. AllocatedEntry - Receives the first fat index for the new allocated cluster chain
  2529. Return Value:
  2530. ESUCCESS is returned if the operation is successful. Otherwise,
  2531. an unsuccessful status is returned that describes the reason for failure.
  2532. --*/
  2533. {
  2534. ULONG TotalClustersInVolume;
  2535. ULONG BytesPerCluster;
  2536. UCHAR BlankBuffer[512];
  2537. FAT_ENTRY PreviousEntry;
  2538. ULONG CurrentClusterCount;
  2539. ULONG j;
  2540. LBO ClusterLbo;
  2541. ULONG i;
  2542. //
  2543. // Load some local variables
  2544. //
  2545. TotalClustersInVolume = FatNumberOfClusters(&FatStructureContext->Bpb);
  2546. BytesPerCluster = FatBytesPerCluster(&FatStructureContext->Bpb);
  2547. RtlZeroMemory((PVOID)&BlankBuffer[0], 512);
  2548. PreviousEntry = 0;
  2549. CurrentClusterCount = 0;
  2550. //
  2551. // For each cluster on the disk we'll do the following loop
  2552. //
  2553. for (j = 0; j < TotalClustersInVolume; j += 1) {
  2554. FAT_ENTRY EntryToExamine;
  2555. FAT_ENTRY FatEntry;
  2556. //
  2557. // Check if the current allocation is enough.
  2558. //
  2559. if (CurrentClusterCount >= ClusterCount) {
  2560. return ESUCCESS;
  2561. }
  2562. //
  2563. // Compute an entry to examine based on the loop iteration and our hint
  2564. //
  2565. EntryToExamine = (FAT_ENTRY)(((j + Hint - 2) % TotalClustersInVolume) + 2);
  2566. //
  2567. // Read in the prospective fat entry and check if it is available. If it
  2568. // is not available then continue looping.
  2569. //
  2570. LookupFatEntry( FatStructureContext, DeviceId, EntryToExamine, (PULONG) &FatEntry, FALSE );
  2571. if (FatInterpretClusterType(FatStructureContext, FatEntry) != FatClusterAvailable) {
  2572. continue;
  2573. }
  2574. //
  2575. // We have a free cluster, so put it at the end of the chain.
  2576. //
  2577. if (PreviousEntry == 0) {
  2578. *AllocatedEntry = EntryToExamine;
  2579. } else {
  2580. SetFatEntry( FatStructureContext, DeviceId, PreviousEntry, EntryToExamine );
  2581. }
  2582. SetFatEntry( FatStructureContext, DeviceId, EntryToExamine, FAT_CLUSTER_LAST );
  2583. //
  2584. // Now we need to go through and zero out the data in the cluster that we've
  2585. // just allocated. Because all clusters must be a multiple of dirents we'll
  2586. // do it a dirent at a time.
  2587. //
  2588. ClusterLbo = FatIndexToLbo( FatStructureContext, EntryToExamine );
  2589. for (i = 0; i < BytesPerCluster; i += 512) {
  2590. DiskWrite( DeviceId, ClusterLbo + i, 512, BlankBuffer );
  2591. }
  2592. //
  2593. // Before we go back to the top of the loop we need to update the
  2594. // previous entry so that it points to the end of the current chain and
  2595. // also i because we've just added another cluster.
  2596. //
  2597. PreviousEntry = EntryToExamine;
  2598. CurrentClusterCount += 1;
  2599. }
  2600. return ENOSPC;
  2601. }
  2602. //
  2603. // Internal support routine
  2604. //
  2605. VOID
  2606. FatFirstComponent (
  2607. IN OUT PSTRING String,
  2608. OUT PFAT8DOT3 FirstComponent
  2609. )
  2610. /*++
  2611. Routine Description:
  2612. Convert a string into fat 8.3 format and advance the input string
  2613. descriptor to point to the next file name component.
  2614. Arguments:
  2615. InputString - Supplies a pointer to the input string descriptor.
  2616. Output8dot3 - Supplies a pointer to the converted string.
  2617. Return Value:
  2618. None.
  2619. --*/
  2620. {
  2621. ULONG Extension;
  2622. ULONG Index;
  2623. //
  2624. // Fill the output name with blanks.
  2625. //
  2626. for (Index = 0; Index < 11; Index += 1) { (*FirstComponent)[Index] = ' '; }
  2627. //
  2628. // Copy the first part of the file name up to eight characters and
  2629. // skip to the end of the name or the input string as appropriate.
  2630. //
  2631. for (Index = 0; Index < String->Length; Index += 1) {
  2632. if ((String->Buffer[Index] == '\\') || (String->Buffer[Index] == '.')) {
  2633. break;
  2634. }
  2635. if (Index < 8) {
  2636. (*FirstComponent)[Index] = (CHAR)ToUpper(String->Buffer[Index]);
  2637. }
  2638. }
  2639. //
  2640. // Check if the end of the string was reached, an extension was specified,
  2641. // or a subdirectory was specified..
  2642. //
  2643. if (Index < String->Length) {
  2644. if (String->Buffer[Index] == '.') {
  2645. //
  2646. // Skip over the extension separator and add the extension to
  2647. // the file name.
  2648. //
  2649. Index += 1;
  2650. Extension = 8;
  2651. while (Index < String->Length) {
  2652. if (String->Buffer[Index] == '\\') {
  2653. break;
  2654. }
  2655. if (Extension < 11) {
  2656. (*FirstComponent)[Extension] = (CHAR)ToUpper(String->Buffer[Index]);
  2657. Extension += 1;
  2658. }
  2659. Index += 1;
  2660. }
  2661. }
  2662. }
  2663. //
  2664. // Now we'll bias the first component by the 0xe5 factor so that all our tests
  2665. // to names on the disk will be ready for a straight 11 byte comparison
  2666. //
  2667. if ((*FirstComponent)[0] == 0xe5) {
  2668. (*FirstComponent)[0] = FAT_DIRENT_REALLY_0E5;
  2669. }
  2670. //
  2671. // Update string descriptor.
  2672. //
  2673. String->Buffer += Index;
  2674. String->Length = String->Length - (USHORT)Index;
  2675. return;
  2676. }
  2677. //
  2678. // Internal support routine
  2679. //
  2680. VOID
  2681. FatDirToArcDir (
  2682. IN PDIRENT FatDirent,
  2683. OUT PDIRECTORY_ENTRY ArcDirent
  2684. )
  2685. /*++
  2686. Routine Description:
  2687. This routine converts a FAT directory entry into an ARC
  2688. directory entry.
  2689. Arguments:
  2690. FatDirent - supplies a pointer to a FAT directory entry.
  2691. ArcDirent - supplies a pointer to an ARC directory entry.
  2692. Return Value:
  2693. None.
  2694. --*/
  2695. {
  2696. ULONG i, e;
  2697. //
  2698. // clear info area
  2699. //
  2700. RtlZeroMemory( ArcDirent, sizeof(DIRECTORY_ENTRY) );
  2701. //
  2702. // check the directory flag
  2703. //
  2704. if (FlagOn( FatDirent->Attributes, FAT_DIRENT_ATTR_DIRECTORY )) {
  2705. SetFlag( ArcDirent->FileAttribute, ArcDirectoryFile );
  2706. }
  2707. //
  2708. // check the read-only flag
  2709. //
  2710. if (FlagOn( FatDirent->Attributes, FAT_DIRENT_ATTR_READ_ONLY )) {
  2711. SetFlag( ArcDirent->FileAttribute, ArcReadOnlyFile );
  2712. }
  2713. //
  2714. // clear name string
  2715. //
  2716. RtlZeroMemory( ArcDirent->FileName, 32 );
  2717. //
  2718. // copy first portion of file name
  2719. //
  2720. for (i = 0; (i < 8) && (FatDirent->FileName[i] != ' '); i += 1) {
  2721. ArcDirent->FileName[i] = FatDirent->FileName[i];
  2722. }
  2723. //
  2724. // check for an extension
  2725. //
  2726. if ( FatDirent->FileName[8] != ' ' ) {
  2727. //
  2728. // store the dot char
  2729. //
  2730. ArcDirent->FileName[i++] = '.';
  2731. //
  2732. // add the extension
  2733. //
  2734. for (e = 8; (e < 11) && (FatDirent->FileName[e] != ' '); e += 1) {
  2735. ArcDirent->FileName[i++] = FatDirent->FileName[e];
  2736. }
  2737. }
  2738. //
  2739. // set file name length before returning
  2740. //
  2741. ArcDirent->FileNameLength = i;
  2742. return;
  2743. }