Fixed the length of the command line.
[reactos.git] / freeldr / freeldr / miscboot.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 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
30 VOID LoadAndBootBootSector(PUCHAR OperatingSystemName)
31 {
32 PFILE FilePointer;
33 UCHAR SettingName[80];
34 UCHAR SettingValue[80];
35 ULONG SectionId;
36 UCHAR FileName[260];
37 ULONG BytesRead;
38
39 // Find all the message box settings and run them
40 ShowMessageBoxesInSection(OperatingSystemName);
41
42 // Try to open the operating system section in the .ini file
43 if (!IniOpenSection(OperatingSystemName, &SectionId))
44 {
45 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
46 MessageBox(SettingName);
47 return;
48 }
49
50 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, 80))
51 {
52 MessageBox("Boot drive not specified for selected OS!");
53 return;
54 }
55
56 BootDrive = atoi(SettingValue);
57
58 BootPartition = 0;
59 if (IniReadSettingByName(SectionId, "BootPartition", SettingValue, 80))
60 {
61 BootPartition = atoi(SettingValue);
62 }
63
64 if (!IniReadSettingByName(SectionId, "BootSectorFile", FileName, 260))
65 {
66 MessageBox("Boot sector file not specified for selected OS!");
67 return;
68 }
69
70 if (!OpenDiskDrive(BootDrive, BootPartition))
71 {
72 MessageBox("Failed to open boot drive.");
73 return;
74 }
75
76 FilePointer = OpenFile(FileName);
77 if (FilePointer == NULL)
78 {
79 strcat(FileName, " not found.");
80 MessageBox(FileName);
81 return;
82 }
83
84 // Read boot sector
85 if (!ReadFile(FilePointer, 512, &BytesRead, (void*)0x7c00) || (BytesRead != 512))
86 {
87 DiskError("Disk read error.");
88 return;
89 }
90
91 // Check for validity
92 if (*((WORD*)(0x7c00 + 0x1fe)) != 0xaa55)
93 {
94 MessageBox("Invalid boot sector magic (0xaa55)");
95 return;
96 }
97
98 clrscr();
99 showcursor();
100 stop_floppy();
101 JumpToBootCode();
102 }
103
104 VOID LoadAndBootPartition(PUCHAR OperatingSystemName)
105 {
106 UCHAR SettingName[80];
107 UCHAR SettingValue[80];
108 ULONG SectionId;
109 PARTITION_TABLE_ENTRY PartitionTableEntry;
110
111 // Find all the message box settings and run them
112 ShowMessageBoxesInSection(OperatingSystemName);
113
114 // Try to open the operating system section in the .ini file
115 if (!IniOpenSection(OperatingSystemName, &SectionId))
116 {
117 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
118 MessageBox(SettingName);
119 return;
120 }
121
122 // Read the boot drive
123 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, 80))
124 {
125 MessageBox("Boot drive not specified for selected OS!");
126 return;
127 }
128
129 BootDrive = atoi(SettingValue);
130
131 // Read the boot partition
132 if (!IniReadSettingByName(SectionId, "BootPartition", SettingValue, 80))
133 {
134 MessageBox("Boot partition not specified for selected OS!");
135 return;
136 }
137
138 BootPartition = atoi(SettingValue);
139
140 // Get the partition table entry
141 if (!DiskGetPartitionEntry(BootDrive, BootPartition, &PartitionTableEntry))
142 {
143 return;
144 }
145
146 // Now try to read the partition boot sector
147 // If this fails then abort
148 if (!DiskReadLogicalSectors(BootDrive, PartitionTableEntry.SectorCountBeforePartition, 1, (PVOID)0x7C00))
149 {
150 return;
151 }
152
153 // Check for validity
154 if (*((WORD*)(0x7c00 + 0x1fe)) != 0xaa55)
155 {
156 MessageBox("Invalid boot sector magic (0xaa55)");
157 return;
158 }
159
160 clrscr();
161 showcursor();
162 stop_floppy();
163 JumpToBootCode();
164 }
165
166 VOID LoadAndBootDrive(PUCHAR OperatingSystemName)
167 {
168 UCHAR SettingName[80];
169 UCHAR SettingValue[80];
170 ULONG SectionId;
171
172 // Find all the message box settings and run them
173 ShowMessageBoxesInSection(OperatingSystemName);
174
175 // Try to open the operating system section in the .ini file
176 if (!IniOpenSection(OperatingSystemName, &SectionId))
177 {
178 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
179 MessageBox(SettingName);
180 return;
181 }
182
183 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, 80))
184 {
185 MessageBox("Boot drive not specified for selected OS!");
186 return;
187 }
188
189 BootDrive = atoi(SettingValue);
190
191 // Now try to read the boot sector (or mbr)
192 // If this fails then abort
193 if (!DiskReadLogicalSectors(BootDrive, 0, 1, (PVOID)0x7C00))
194 {
195 return;
196 }
197
198 // Check for validity
199 if (*((WORD*)(0x7c00 + 0x1fe)) != 0xaa55)
200 {
201 MessageBox("Invalid boot sector magic (0xaa55)");
202 return;
203 }
204
205 clrscr();
206 showcursor();
207 stop_floppy();
208 JumpToBootCode();
209 }