- Remove some header duplication.
[reactos.git] / reactos / boot / freeldr / freeldr / fs / fsrec.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21
22 #define NDEBUG
23 #include <debug.h>
24
25 /////////////////////////////////////////////////////////////////////////////////////////////
26 // FUNCTIONS
27 /////////////////////////////////////////////////////////////////////////////////////////////
28
29 /*
30 *
31 * BOOL FsRecognizeVolume(ULONG DriveNumber, ULONG VolumeStartSector, UCHAR* VolumeType);
32 *
33 */
34 BOOL FsRecognizeVolume(ULONG DriveNumber, ULONG VolumeStartSector, UCHAR* VolumeType)
35 {
36
37 DbgPrint((DPRINT_FILESYSTEM, "FsRecognizeVolume() DriveNumber: 0x%x VolumeStartSector: %d\n", DriveNumber, VolumeStartSector));
38
39 if (FsRecIsExt2(DriveNumber, VolumeStartSector))
40 {
41 *VolumeType = PARTITION_EXT2;
42 return TRUE;
43 }
44 else if (FsRecIsFat(DriveNumber, VolumeStartSector))
45 {
46 *VolumeType = PARTITION_FAT32;
47 return TRUE;
48 }
49 else if (FsRecIsNtfs(DriveNumber, VolumeStartSector))
50 {
51 *VolumeType = PARTITION_NTFS;
52 return TRUE;
53 }
54
55 return FALSE;
56 }
57
58 BOOL FsRecIsIso9660(ULONG DriveNumber)
59 {
60 PUCHAR Sector = (PUCHAR)DISKREADBUFFER;
61
62 if (!MachDiskReadLogicalSectors(DriveNumber, 16, 1, Sector))
63 {
64 FileSystemError("Failed to read the PVD.");
65 return FALSE;
66 }
67
68 return (Sector[0] == 1 &&
69 Sector[1] == 'C' &&
70 Sector[2] == 'D' &&
71 Sector[3] == '0' &&
72 Sector[4] == '0' &&
73 Sector[5] == '1');
74 }
75
76 BOOL FsRecIsExt2(ULONG DriveNumber, ULONG VolumeStartSector)
77 {
78 PEXT2_SUPER_BLOCK SuperBlock = (PEXT2_SUPER_BLOCK)DISKREADBUFFER;
79
80 if (!MachDiskReadLogicalSectors(DriveNumber, VolumeStartSector + 2, 2, SuperBlock))
81 {
82 FileSystemError("Failed to read the super block.");
83 return FALSE;
84 }
85
86 if (SuperBlock->s_magic == EXT3_SUPER_MAGIC)
87 {
88 return TRUE;
89 }
90
91 return FALSE;
92 }
93
94 BOOL FsRecIsFat(ULONG DriveNumber, ULONG VolumeStartSector)
95 {
96 PFAT_BOOTSECTOR BootSector = (PFAT_BOOTSECTOR)DISKREADBUFFER;
97 PFAT32_BOOTSECTOR BootSector32 = (PFAT32_BOOTSECTOR)DISKREADBUFFER;
98 PFATX_BOOTSECTOR BootSectorX = (PFATX_BOOTSECTOR)DISKREADBUFFER;
99 if (!MachDiskReadLogicalSectors(DriveNumber, VolumeStartSector, 1, BootSector))
100 {
101 FileSystemError("Failed to read the boot sector.");
102 return FALSE;
103 }
104
105 if (strncmp(BootSector->FileSystemType, "FAT12 ", 8) == 0 ||
106 strncmp(BootSector->FileSystemType, "FAT16 ", 8) == 0 ||
107 strncmp(BootSector32->FileSystemType, "FAT32 ", 8) == 0 ||
108 strncmp(BootSectorX->FileSystemType, "FATX", 4) == 0)
109 {
110 return TRUE;
111 }
112
113 return FALSE;
114 }
115
116 BOOL FsRecIsNtfs(ULONG DriveNumber, ULONG VolumeStartSector)
117 {
118 PNTFS_BOOTSECTOR BootSector = (PNTFS_BOOTSECTOR)DISKREADBUFFER;
119 if (!MachDiskReadLogicalSectors(DriveNumber, VolumeStartSector, 1, BootSector))
120 {
121 FileSystemError("Failed to read the boot sector.");
122 return FALSE;
123 }
124
125 if (RtlEqualMemory(BootSector->SystemId, "NTFS", 4))
126 {
127 return TRUE;
128 }
129
130 return FALSE;
131 }