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