[SDK] One step further towards ReactOS source code tree restructure: the sdk folder...
[reactos.git] / reactos / boot / environ / lib / io / fat.c
1 /*
2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/io/fat.c
5 * PURPOSE: Boot Library FAT File System Management Routines
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include <bl.h>
12 #include <fs_rec/fs_rec.h>
13
14 /* DATA VARIABLES ************************************************************/
15
16 PVOID* FatDeviceTable;
17 ULONG FatDeviceTableEntries;
18 PWCHAR FatpLongFileName;
19
20 /* FUNCTIONS *****************************************************************/
21
22 NTSTATUS
23 FatMount (
24 _In_ ULONG DeviceId,
25 _In_ ULONG Unknown,
26 _Out_ PBL_FILE_ENTRY* FileEntry
27 )
28 {
29 BL_DEVICE_INFORMATION DeviceInformation;
30 ULONG UnknownFlag;
31 NTSTATUS Status;
32 PACKED_BOOT_SECTOR FatBootSector;
33 BIOS_PARAMETER_BLOCK BiosBlock;
34
35 /* Capture thing */
36 BlDeviceGetInformation(DeviceId, &DeviceInformation);
37 UnknownFlag = DeviceInformation.BlockDeviceInfo.Unknown;
38
39 /* Set thing to 1 */
40 DeviceInformation.BlockDeviceInfo.Unknown |= 1;
41 BlDeviceSetInformation(DeviceId, &DeviceInformation);
42
43 /* Read the boot sector */
44 Status = BlDeviceReadAtOffset(DeviceId,
45 sizeof(FatBootSector),
46 0,
47 &FatBootSector,
48 NULL);
49
50 /* Restore thing back */
51 DeviceInformation.BlockDeviceInfo.Unknown = UnknownFlag;
52 BlDeviceSetInformation(DeviceId, &DeviceInformation);
53 if (!NT_SUCCESS(Status))
54 {
55 EfiPrintf(L"Failed reading drive: %lx\r\n", Status);
56 return Status;
57 }
58
59 FatUnpackBios(&BiosBlock, &FatBootSector.PackedBpb);
60
61 /* For now, quickly fail if this isn't FAT */
62 if (FatBootSector.Jump[0] != 0xE9)
63 {
64 return STATUS_UNSUCCESSFUL;
65 }
66
67 EfiPrintf(L"Jump: %lx Bytes Per Sector: %d Sectors Per Cluster: %d Reserved: %d Fats: %d Sectors: %d Large Sectors: %d Media: %lx RootEntries: %d\r\n",
68 FatBootSector.Jump[0],
69 BiosBlock.BytesPerSector,
70 BiosBlock.SectorsPerCluster,
71 BiosBlock.ReservedSectors,
72 BiosBlock.Fats,
73 BiosBlock.Sectors,
74 BiosBlock.LargeSectors,
75 BiosBlock.Media,
76 BiosBlock.RootEntries);
77 return STATUS_NOT_IMPLEMENTED;
78 }
79
80 NTSTATUS
81 FatInitialize (
82 VOID
83 )
84 {
85 NTSTATUS Status;
86
87 /* Allocate the device table with 2 entries*/
88 FatDeviceTableEntries = 2;
89 FatDeviceTable = BlMmAllocateHeap(sizeof(PBL_FILE_ENTRY) *
90 FatDeviceTableEntries);
91 if (FatDeviceTable)
92 {
93 /* Zero it out */
94 RtlZeroMemory(FatDeviceTable,
95 sizeof(PBL_FILE_ENTRY) * FatDeviceTableEntries);
96
97 /* Allocate a 512 byte buffer for long file name conversion */
98 FatpLongFileName = BlMmAllocateHeap(512);
99 Status = FatpLongFileName != NULL ? STATUS_SUCCESS : STATUS_NO_MEMORY;
100 }
101 else
102 {
103 /* No memory, fail */
104 Status = STATUS_NO_MEMORY;
105 }
106
107 /* Return back to caller */
108 return Status;
109 }
110