merge ROS Shell without integrated explorer part into trunk
[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 #include <fs.h>
22 #include "fsrec.h"
23 #include "fat.h"
24 #include "iso.h"
25 #include "ext2.h"
26 #include "ntfs.h"
27 #include <disk.h>
28 #include <rtl.h>
29 #include <arch.h>
30 #include <debug.h>
31 #include <machine.h>
32
33
34
35
36 /////////////////////////////////////////////////////////////////////////////////////////////
37 // FUNCTIONS
38 /////////////////////////////////////////////////////////////////////////////////////////////
39
40 /*
41 *
42 * BOOL FsRecognizeVolume(ULONG DriveNumber, ULONG VolumeStartSector, UCHAR* VolumeType);
43 *
44 */
45 BOOL FsRecognizeVolume(ULONG DriveNumber, ULONG VolumeStartSector, UCHAR* VolumeType)
46 {
47
48 DbgPrint((DPRINT_FILESYSTEM, "FsRecognizeVolume() DriveNumber: 0x%x VolumeStartSector: %d\n", DriveNumber, VolumeStartSector));
49
50 if (FsRecIsExt2(DriveNumber, VolumeStartSector))
51 {
52 *VolumeType = PARTITION_EXT2;
53 return TRUE;
54 }
55 else if (FsRecIsFat(DriveNumber, VolumeStartSector))
56 {
57 *VolumeType = PARTITION_FAT32;
58 return TRUE;
59 }
60 else if (FsRecIsNtfs(DriveNumber, VolumeStartSector))
61 {
62 *VolumeType = PARTITION_NTFS;
63 return TRUE;
64 }
65
66 return FALSE;
67 }
68
69 BOOL FsRecIsIso9660(ULONG DriveNumber)
70 {
71 PUCHAR Sector = (PUCHAR)DISKREADBUFFER;
72
73 if (!MachDiskReadLogicalSectors(DriveNumber, 16, 1, Sector))
74 {
75 FileSystemError("Failed to read the PVD.");
76 return FALSE;
77 }
78
79 return (Sector[0] == 1 &&
80 Sector[1] == 'C' &&
81 Sector[2] == 'D' &&
82 Sector[3] == '0' &&
83 Sector[4] == '0' &&
84 Sector[5] == '1');
85 }
86
87 BOOL FsRecIsExt2(ULONG DriveNumber, ULONG VolumeStartSector)
88 {
89 PEXT2_SUPER_BLOCK SuperBlock = (PEXT2_SUPER_BLOCK)DISKREADBUFFER;
90
91 if (!MachDiskReadLogicalSectors(DriveNumber, VolumeStartSector + 2, 2, SuperBlock))
92 {
93 FileSystemError("Failed to read the super block.");
94 return FALSE;
95 }
96
97 if (SuperBlock->s_magic == EXT3_SUPER_MAGIC)
98 {
99 return TRUE;
100 }
101
102 return FALSE;
103 }
104
105 BOOL FsRecIsFat(ULONG DriveNumber, ULONG VolumeStartSector)
106 {
107 PFAT_BOOTSECTOR BootSector = (PFAT_BOOTSECTOR)DISKREADBUFFER;
108 PFAT32_BOOTSECTOR BootSector32 = (PFAT32_BOOTSECTOR)DISKREADBUFFER;
109 PFATX_BOOTSECTOR BootSectorX = (PFATX_BOOTSECTOR)DISKREADBUFFER;
110 if (!MachDiskReadLogicalSectors(DriveNumber, VolumeStartSector, 1, BootSector))
111 {
112 FileSystemError("Failed to read the boot sector.");
113 return FALSE;
114 }
115
116 if (strncmp(BootSector->FileSystemType, "FAT12 ", 8) == 0 ||
117 strncmp(BootSector->FileSystemType, "FAT16 ", 8) == 0 ||
118 strncmp(BootSector32->FileSystemType, "FAT32 ", 8) == 0 ||
119 strncmp(BootSectorX->FileSystemType, "FATX", 4) == 0)
120 {
121 return TRUE;
122 }
123
124 return FALSE;
125 }
126
127 BOOL FsRecIsNtfs(ULONG DriveNumber, ULONG VolumeStartSector)
128 {
129 PNTFS_BOOTSECTOR BootSector = (PNTFS_BOOTSECTOR)DISKREADBUFFER;
130 if (!MachDiskReadLogicalSectors(DriveNumber, VolumeStartSector, 1, BootSector))
131 {
132 FileSystemError("Failed to read the boot sector.");
133 return FALSE;
134 }
135
136 if (!RtlCompareMemory(BootSector->SystemId, "NTFS", 4))
137 {
138 return TRUE;
139 }
140
141 return FALSE;
142 }