- Check Input Parameter (fixes a skype crash)
[reactos.git] / reactos / boot / freeldr / freeldr / miscboot.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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <freeldr.h>
21
22 #ifdef __i386__
23 VOID LoadAndBootBootSector(PCSTR OperatingSystemName)
24 {
25 PFILE FilePointer;
26 CHAR SettingName[80];
27 ULONG SectionId;
28 CHAR FileName[260];
29 ULONG BytesRead;
30
31 // Find all the message box settings and run them
32 UiShowMessageBoxesInSection(OperatingSystemName);
33
34 // Try to open the operating system section in the .ini file
35 if (!IniOpenSection(OperatingSystemName, &SectionId))
36 {
37 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
38 UiMessageBox(SettingName);
39 return;
40 }
41
42 if (!IniReadSettingByName(SectionId, "BootSectorFile", FileName, sizeof(FileName)))
43 {
44 UiMessageBox("Boot sector file not specified for selected OS!");
45 return;
46 }
47
48 if (!MachDiskNormalizeSystemPath(FileName, sizeof(FileName)))
49 {
50 UiMessageBox("Invalid path to boot sector file");
51 return;
52 }
53
54 FilePointer = FsOpenFile(FileName);
55 if (!FilePointer)
56 {
57 strcat(FileName, " not found.");
58 UiMessageBox(FileName);
59 return;
60 }
61
62 // Read boot sector
63 if (!FsReadFile(FilePointer, 512, &BytesRead, (void*)0x7c00) || (BytesRead != 512))
64 {
65 UiMessageBox("Unable to read boot sector.");
66 return;
67 }
68
69 // Check for validity
70 if (*((USHORT*)(0x7c00 + 0x1fe)) != 0xaa55)
71 {
72 UiMessageBox("Invalid boot sector magic (0xaa55)");
73 return;
74 }
75
76 UiUnInitialize("Booting...");
77 // Don't stop the floppy drive motor when we
78 // are just booting a bootsector, or drive, or partition.
79 // If we were to stop the floppy motor then
80 // the BIOS wouldn't be informed and if the
81 // next read is to a floppy then the BIOS will
82 // still think the motor is on and this will
83 // result in a read error.
84 //DiskStopFloppyMotor();
85 //DisableA20();
86 ChainLoadBiosBootSectorCode();
87 }
88
89 VOID LoadAndBootPartition(PCSTR OperatingSystemName)
90 {
91 CHAR SettingName[80];
92 CHAR SettingValue[80];
93 ULONG SectionId;
94 PARTITION_TABLE_ENTRY PartitionTableEntry;
95 ULONG DriveNumber;
96 ULONG PartitionNumber;
97
98 // Find all the message box settings and run them
99 UiShowMessageBoxesInSection(OperatingSystemName);
100
101 // Try to open the operating system section in the .ini file
102 if (!IniOpenSection(OperatingSystemName, &SectionId))
103 {
104 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
105 UiMessageBox(SettingName);
106 return;
107 }
108
109 // Read the boot drive
110 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, sizeof(SettingValue)))
111 {
112 UiMessageBox("Boot drive not specified for selected OS!");
113 return;
114 }
115
116 DriveNumber = DriveMapGetBiosDriveNumber(SettingValue);
117
118 // Read the boot partition
119 if (!IniReadSettingByName(SectionId, "BootPartition", SettingValue, sizeof(SettingValue)))
120 {
121 UiMessageBox("Boot partition not specified for selected OS!");
122 return;
123 }
124
125 PartitionNumber = atoi(SettingValue);
126
127 // Get the partition table entry
128 if (!DiskGetPartitionEntry(DriveNumber, PartitionNumber, &PartitionTableEntry))
129 {
130 return;
131 }
132
133 // Now try to read the partition boot sector
134 // If this fails then abort
135 if (!MachDiskReadLogicalSectors(DriveNumber, PartitionTableEntry.SectorCountBeforePartition, 1, (PVOID)0x7C00))
136 {
137 UiMessageBox("Unable to read partition's boot sector.");
138 return;
139 }
140
141 // Check for validity
142 if (*((USHORT*)(0x7c00 + 0x1fe)) != 0xaa55)
143 {
144 UiMessageBox("Invalid boot sector magic (0xaa55)");
145 return;
146 }
147
148 UiUnInitialize("Booting...");
149 // Don't stop the floppy drive motor when we
150 // are just booting a bootsector, or drive, or partition.
151 // If we were to stop the floppy motor then
152 // the BIOS wouldn't be informed and if the
153 // next read is to a floppy then the BIOS will
154 // still think the motor is on and this will
155 // result in a read error.
156 //DiskStopFloppyMotor();
157 //DisableA20();
158 ChainLoadBiosBootSectorCode();
159 }
160
161 VOID LoadAndBootDrive(PCSTR OperatingSystemName)
162 {
163 CHAR SettingName[80];
164 CHAR SettingValue[80];
165 ULONG SectionId;
166 ULONG DriveNumber;
167
168 // Find all the message box settings and run them
169 UiShowMessageBoxesInSection(OperatingSystemName);
170
171 // Try to open the operating system section in the .ini file
172 if (!IniOpenSection(OperatingSystemName, &SectionId))
173 {
174 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
175 UiMessageBox(SettingName);
176 return;
177 }
178
179 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, sizeof(SettingValue)))
180 {
181 UiMessageBox("Boot drive not specified for selected OS!");
182 return;
183 }
184
185 DriveNumber = DriveMapGetBiosDriveNumber(SettingValue);
186
187 // Now try to read the boot sector (or mbr)
188 // If this fails then abort
189 if (!MachDiskReadLogicalSectors(DriveNumber, 0, 1, (PVOID)0x7C00))
190 {
191 UiMessageBox("Unable to read boot sector");
192 return;
193 }
194
195 // Check for validity
196 if (*((USHORT*)(0x7c00 + 0x1fe)) != 0xaa55)
197 {
198 UiMessageBox("Invalid boot sector magic (0xaa55)");
199 return;
200 }
201
202 UiUnInitialize("Booting...");
203 // Don't stop the floppy drive motor when we
204 // are just booting a bootsector, or drive, or partition.
205 // If we were to stop the floppy motor then
206 // the BIOS wouldn't be informed and if the
207 // next read is to a floppy then the BIOS will
208 // still think the motor is on and this will
209 // result in a read error.
210 //DiskStopFloppyMotor();
211 //DisableA20();
212 ChainLoadBiosBootSectorCode();
213 }
214 #endif /* __i386__ */