Minor changes for making apps/baresh compile (tough it doesn't work yet).
[reactos.git] / freeldr / freeldr / options.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 #include "freeldr.h"
21 #include "rtl.h"
22 #include "ui.h"
23 #include "options.h"
24 #include "miscboot.h"
25
26 #if 0
27 void DoOptionsMenu(void)
28 {
29 int OptionsMenuItemCount = 1; // Count is 1 because we don't show the "Set ReactOS Boot Flags" menu item yet
30 char OptionsMenuItems[2][80] = { "Boot Wizard", "Set ReactOS Boot Flags" /* i.e. Safe Mode, Last Known Good Configuration */ };
31 int OptionsMenuItemSelected = 0;
32
33 while (OptionsMenuItemSelected != -1)
34 {
35 OptionsMenuItemSelected = RunOptionsMenu(OptionsMenuItems, OptionsMenuItemCount, OptionsMenuItemSelected, "[Advanced Options]");
36
37 switch (OptionsMenuItemSelected)
38 {
39 case 0:
40 DoDiskOptionsMenu();
41 break;
42 }
43 }
44 }
45
46 void DoDiskOptionsMenu(void)
47 {
48 char DiskMenuItems[25][80];
49 int DiskMenuItemCount = 0;
50 int FloppyDiskMenuItemCount = 0;
51 int HardDiskMenuItemCount = 0;
52 int DiskMenuItemSelected = 0;
53
54 char temp[255];
55 int i;
56
57 FloppyDiskMenuItemCount = (int)*((char *)((0x40 * 16) + 0x10)); // Get number of floppy disks from bios data area 40:10
58 if (FloppyDiskMenuItemCount & 1)
59 FloppyDiskMenuItemCount = (FloppyDiskMenuItemCount >> 6) + 1;
60 else
61 FloppyDiskMenuItemCount = 0;
62 HardDiskMenuItemCount = (int)*((char *)((0x40 * 16) + 0x75)); // Get number of hard disks from bios data area 40:75
63 DiskMenuItemCount = FloppyDiskMenuItemCount + HardDiskMenuItemCount;
64
65 for (i=0; i<FloppyDiskMenuItemCount; i++)
66 {
67 strcpy(DiskMenuItems[i], "Floppy Disk ");
68 itoa(i + 1, temp, 10);
69 strcat(DiskMenuItems[i], temp);
70 }
71
72 for (i=0; i<HardDiskMenuItemCount; i++)
73 {
74 strcpy(DiskMenuItems[i + FloppyDiskMenuItemCount], "Hard Disk ");
75 itoa(i + 1, temp, 10);
76 strcat(DiskMenuItems[i + FloppyDiskMenuItemCount], temp);
77 strcat(DiskMenuItems[i + FloppyDiskMenuItemCount], " (");
78 itoa((get_heads(i+0x80) * get_cylinders(i+0x80) * get_sectors(i+0x80)) / 2048, temp, 10);
79 strcat(DiskMenuItems[i + FloppyDiskMenuItemCount], temp);
80 strcat(DiskMenuItems[i + FloppyDiskMenuItemCount], " MB)");
81 }
82
83 DiskMenuItemSelected = 0;
84 while (DiskMenuItemSelected != -1)
85 {
86 DiskMenuItemSelected = RunOptionsMenu(DiskMenuItems, DiskMenuItemCount, DiskMenuItemSelected, "[Boot Wizard]");
87
88 if (DiskMenuItemSelected != -1)
89 {
90 if (DiskMenuItemSelected < FloppyDiskMenuItemCount)
91 DoBootOptionsMenu(DiskMenuItemSelected, DiskMenuItems[DiskMenuItemSelected]);
92 else
93 DoBootOptionsMenu((DiskMenuItemSelected - FloppyDiskMenuItemCount) + 0x80, DiskMenuItems[DiskMenuItemSelected]);
94 }
95 }
96 }
97
98 void DoBootOptionsMenu(int BootDriveNum, char *BootDriveText)
99 {
100 int BootOptionsMenuItemCount = 2;
101 char BootOptionsMenuItems[2][80] = { "Boot To ", "Pick A Boot Partition" };
102 int BootOptionsMenuItemSelected = 0;
103
104 /*strcat(BootOptionsMenuItems[0], BootDriveText);
105
106 while (BootOptionsMenuItemSelected != -1)
107 {
108 BootOptionsMenuItemSelected = RunOptionsMenu(BootOptionsMenuItems, BootOptionsMenuItemCount, BootOptionsMenuItemSelected, "[Boot Options]");
109
110 switch (BootOptionsMenuItemSelected)
111 {
112 case 0:
113 BootDrive = BootDriveNum;
114
115 if (!biosdisk(_DISK_READ, BootDrive, 0, 0, 1, 1, (void*)0x7c00))
116 {
117 MessageBox("Disk Read Error");
118 return;
119 }
120
121 // Check for validity
122 if (*((WORD*)(0x7c00 + 0x1fe)) != 0xaa55)
123 {
124 MessageBox("Invalid boot sector magic (0xaa55)");
125 return;
126 }
127
128 RestoreScreen(ScreenBuffer);
129 showcursor();
130 gotoxy(CursorXPos, CursorYPos);
131
132 stop_floppy();
133 JumpToBootCode();
134
135 break;
136 case 1:
137 if (BootDriveNum < 0x80)
138 {
139 MessageBox("This option is not available for a floppy disk.");
140 continue;
141 }
142 else
143 DoBootPartitionOptionsMenu(BootDriveNum);
144
145 break;
146 }
147 }*/
148 }
149
150 void DoBootPartitionOptionsMenu(int BootDriveNum)
151 {
152 struct
153 {
154 int partition_num;
155 int partition_type;
156 int head, sector, cylinder;
157 } BootPartitions[8];
158 int BootOptionsMenuItemCount = 0;
159 char BootOptionsMenuItems[8][80];
160 int BootOptionsMenuItemSelected = 0;
161 int head, sector, cylinder;
162 int offset;
163 int i;
164 char temp[25];
165
166
167 /*BootDrive = BootDriveNum;
168
169 if (!biosdisk(_DISK_READ, BootDrive, 0, 0, 1, 1, SectorBuffer))
170 {
171 MessageBox("Disk Read Error");
172 return;
173 }
174
175 // Check for validity
176 if (*((WORD*)(SectorBuffer + 0x1fe)) != 0xaa55)
177 {
178 MessageBox("Invalid partition table magic (0xaa55)");
179 return;
180 }
181
182 offset = 0x1BE;
183
184 for (i=0; i<4; i++)
185 {
186 // Check for valid partition
187 if (SectorBuffer[offset + 4] != 0)
188 {
189 BootPartitions[BootOptionsMenuItemCount].partition_num = i;
190 BootPartitions[BootOptionsMenuItemCount].partition_type = SectorBuffer[offset + 4];
191
192 BootPartitions[BootOptionsMenuItemCount].head = SectorBuffer[offset + 1];
193 BootPartitions[BootOptionsMenuItemCount].sector = (SectorBuffer[offset + 2] & 0x3F);
194 BootPartitions[BootOptionsMenuItemCount].cylinder = SectorBuffer[offset + 3];
195 if (SectorBuffer[offset + 2] & 0x80)
196 BootPartitions[BootOptionsMenuItemCount].cylinder += 0x200;
197 if (SectorBuffer[offset + 2] & 0x40)
198 BootPartitions[BootOptionsMenuItemCount].cylinder += 0x100;
199
200 strcpy(BootOptionsMenuItems[BootOptionsMenuItemCount], "Boot To Partition ");
201 itoa(i+1, temp, 10);
202 strcat(BootOptionsMenuItems[BootOptionsMenuItemCount], temp);
203 strcat(BootOptionsMenuItems[BootOptionsMenuItemCount], " (Type: 0x");
204 itoa(BootPartitions[BootOptionsMenuItemCount].partition_type, temp, 16);
205 if (strlen(temp) < 2)
206 strcat(BootOptionsMenuItems[BootOptionsMenuItemCount], "0");
207 strcat(BootOptionsMenuItems[BootOptionsMenuItemCount], temp);
208 strcat(BootOptionsMenuItems[BootOptionsMenuItemCount], ")");
209
210 BootOptionsMenuItemCount++;
211 }
212
213 offset += 0x10;
214 }
215
216 while (BootOptionsMenuItemSelected != -1)
217 {
218 BootOptionsMenuItemSelected = RunOptionsMenu(BootOptionsMenuItems, BootOptionsMenuItemCount, BootOptionsMenuItemSelected, "[Boot Partition Options]");
219
220 if (BootOptionsMenuItemSelected != -1)
221 {
222 head = BootPartitions[BootOptionsMenuItemCount].head;
223 sector = BootPartitions[BootOptionsMenuItemCount].sector;
224 cylinder = BootPartitions[BootOptionsMenuItemCount].cylinder;
225
226 // Read partition boot sector
227 if (!biosdisk(_DISK_READ, BootDrive, head, cylinder, sector, 1, (void*)0x7c00))
228 {
229 MessageBox("Disk Read Error");
230 return;
231 }
232
233 // Check for validity
234 if (*((WORD*)(0x7c00 + 0x1fe)) != 0xaa55)
235 {
236 MessageBox("Invalid boot sector magic (0xaa55)");
237 return;
238 }
239
240 RestoreScreen(ScreenBuffer);
241 showcursor();
242 gotoxy(CursorXPos, CursorYPos);
243
244 stop_floppy();
245 JumpToBootCode();
246 }
247 }*/
248 }
249
250 int RunOptionsMenu(char OptionsMenuItems[][80], int OptionsMenuItemCount, int nOptionSelected, char *OptionsMenuTitle)
251 {
252 int key;
253 int second;
254 BOOL bDone = FALSE;
255 int nOptionsMenuBoxLeft;
256 int nOptionsMenuBoxRight;
257 int nOptionsMenuBoxTop;
258 int nOptionsMenuBoxBottom;
259
260
261 // Initialise the menu
262 InitOptionsMenu(&nOptionsMenuBoxLeft, &nOptionsMenuBoxTop, &nOptionsMenuBoxRight, &nOptionsMenuBoxBottom, OptionsMenuItemCount);
263
264 DrawBackdrop();
265
266 // Update the menu
267 DrawOptionsMenu(OptionsMenuItems, OptionsMenuItemCount, nOptionSelected, OptionsMenuTitle, nOptionsMenuBoxLeft, nOptionsMenuBoxTop, nOptionsMenuBoxRight, nOptionsMenuBoxBottom);
268
269 second = getsecond();
270
271 // Loop
272 do
273 {
274 // Check for a keypress
275 if (kbhit())
276 {
277 // Cancel the timeout
278 if (nTimeOut != -1)
279 {
280 nTimeOut = -1;
281 DrawOptionsMenu(OptionsMenuItems, OptionsMenuItemCount, nOptionSelected, OptionsMenuTitle, nOptionsMenuBoxLeft, nOptionsMenuBoxTop, nOptionsMenuBoxRight, nOptionsMenuBoxBottom);
282 }
283
284 // Get the key
285 key = getch();
286
287 // Is it extended?
288 if (key == 0)
289 key = getch(); // Yes - so get the extended key
290
291 // Process the key
292 switch (key)
293 {
294 case KEY_UP:
295 if (nOptionSelected)
296 {
297 nOptionSelected--;
298
299 // Update the menu
300 DrawOptionsMenu(OptionsMenuItems, OptionsMenuItemCount, nOptionSelected, OptionsMenuTitle, nOptionsMenuBoxLeft, nOptionsMenuBoxTop, nOptionsMenuBoxRight, nOptionsMenuBoxBottom);
301 }
302 break;
303 case KEY_DOWN:
304 if (nOptionSelected < (OptionsMenuItemCount - 1))
305 {
306 nOptionSelected++;
307
308 // Update the menu
309 DrawOptionsMenu(OptionsMenuItems, OptionsMenuItemCount, nOptionSelected, OptionsMenuTitle, nOptionsMenuBoxLeft, nOptionsMenuBoxTop, nOptionsMenuBoxRight, nOptionsMenuBoxBottom);
310 }
311 break;
312 case KEY_ENTER:
313 //MessageBox("The Advanced Options are still being implemented.");
314 bDone = TRUE;
315 break;
316 case KEY_ESC:
317 nOptionSelected = -1;
318 bDone = TRUE;
319 break;
320 }
321 }
322
323 // Update the date & time
324 UpdateDateTime();
325
326 if (nTimeOut > 0)
327 {
328 if (getsecond() != second)
329 {
330 second = getsecond();
331 nTimeOut--;
332
333 // Update the menu
334 DrawOptionsMenu(OptionsMenuItems, OptionsMenuItemCount, nOptionSelected, OptionsMenuTitle, nOptionsMenuBoxLeft, nOptionsMenuBoxTop, nOptionsMenuBoxRight, nOptionsMenuBoxBottom);
335 }
336 }
337
338 if (nTimeOut == 0)
339 bDone = TRUE;
340 }
341 while (!bDone);
342
343 return nOptionSelected;
344 }
345
346 void InitOptionsMenu(int *nOptionsMenuBoxLeft, int *nOptionsMenuBoxTop, int *nOptionsMenuBoxRight, int *nOptionsMenuBoxBottom, int OptionsMenuItemCount)
347 {
348 /*int height = OptionsMenuItemCount;
349 int width = 20;
350
351 height += 1; // Allow room for top & bottom borders
352 width += 18; // Allow room for left & right borders, plus 8 spaces on each side
353
354 // Calculate the OS list box area
355 *nOptionsMenuBoxLeft = (nScreenWidth - width) / 2;
356 *nOptionsMenuBoxRight = *nOptionsMenuBoxLeft + width;
357 *nOptionsMenuBoxTop = (nScreenHeight - height) / 2 + 1;
358 *nOptionsMenuBoxBottom = *nOptionsMenuBoxTop + height;*/
359 }
360
361 void DrawOptionsMenu(char OptionsMenuItems[][80], int OptionsMenuItemCount, int nOptionSelected, char *OptionsMenuTitle, int nOptionsMenuBoxLeft, int nOptionsMenuBoxTop, int nOptionsMenuBoxRight, int nOptionsMenuBoxBottom)
362 {
363 int i, j;
364 char text[260];
365 int space, space_left, space_right;
366
367 // Update the status bar
368 /*DrawStatusText(" Use \x18\x19 to select, then press ENTER. Press ESC to go back.");
369
370 DrawBox(nOptionsMenuBoxLeft, nOptionsMenuBoxTop, nOptionsMenuBoxRight, nOptionsMenuBoxBottom, D_VERT, D_HORZ, TRUE, TRUE, ATTR(cMenuFgColor, cMenuBgColor));
371 DrawText(nOptionsMenuBoxLeft + (((nOptionsMenuBoxRight - nOptionsMenuBoxLeft) - strlen(OptionsMenuTitle)) / 2) + 1, nOptionsMenuBoxTop, OptionsMenuTitle, ATTR(cMenuFgColor, cMenuBgColor));
372
373 for(i=0; i<OptionsMenuItemCount; i++)
374 {
375 space = (nOptionsMenuBoxRight - nOptionsMenuBoxLeft - 2) - strlen(OptionsMenuItems[i]);
376 space_left = (space / 2) + 1;
377 space_right = (space - space_left) + 1;
378
379 text[0] = '\0';
380 for(j=0; j<space_left; j++)
381 strcat(text, " ");
382 strcat(text, OptionsMenuItems[i]);
383 for(j=0; j<space_right; j++)
384 strcat(text, " ");
385
386 if(i == nOptionSelected)
387 {
388 DrawText(nOptionsMenuBoxLeft+1, nOptionsMenuBoxTop+1+i, text, ATTR(cSelectedTextColor, cSelectedTextBgColor));
389 }
390 else
391 {
392 DrawText(nOptionsMenuBoxLeft+1, nOptionsMenuBoxTop+1+i, text, ATTR(cTextColor, cMenuBgColor));
393 }
394 }*/
395 }
396 #endif