Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/Chazi/i386/boot2/options.c

1/*
2 * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2004 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25#include "boot.h"
26#include "fdisk.h"
27#include "ramdisk.h"
28#include "gui.h"
29#include "embedded.h"
30#include "pci.h"
31#include "options.h"
32
33bool showBootBanner = true; //Azi:debuginfo
34static bool shouldboot = false;
35
36extern int multiboot_timeout;
37extern int multiboot_timeout_set;
38
39extern BVRef bvChain;
40//extern intmenucount;
41
42//extern intgDeviceCount; //Azi: header
43chargMacOSVersion[8]; //Azi: moved from boot.c
44static bool getOSVersion(char *str); //||
45
46intselectIndex = 0;
47MenuItem * menuItems = NULL;
48
49//==========================================================================
50
51//static
52void changeCursor( int col, int row, int type, CursorState * cs )
53{
54 if (cs) getCursorPositionAndType( &cs->x, &cs->y, &cs->type );
55 setCursorType( type );
56 setCursorPosition( col, row, 0 );
57}
58
59static void moveCursor( int col, int row )
60{
61 setCursorPosition( col, row, 0 );
62}
63
64static void restoreCursor( const CursorState * cs )
65{
66 setCursorPosition( cs->x, cs->y, 0 );
67 setCursorType( cs->type );
68}
69
70//==========================================================================
71
72/* Flush keyboard buffer; returns TRUE if any of the flushed
73 * characters was F8.
74 */
75
76static bool flushKeyboardBuffer(void)
77{
78 bool status = false;
79
80 while ( readKeyboardStatus() ) {
81 if (bgetc() == 0x4200) status = true;
82 }
83 return status;
84}
85
86//==========================================================================
87
88static int countdown( const char * msg, int row, int timeout )
89{
90 unsigned long time;
91 int ch = 0;
92 int col = strlen(msg) + 1;
93
94 flushKeyboardBuffer();
95
96if( bootArgs->Video.v_display == VGA_TEXT_MODE )
97{
98moveCursor( 0, row );
99printf(msg);
100
101} else {
102
103position_t p = pos( gui.screen.width / 2 + 1 , ( gui.devicelist.pos.y + 3 ) + ( ( gui.devicelist.height - gui.devicelist.iconspacing ) / 2 ) );
104
105char dummy[80];
106getBootVolumeDescription( gBootVolume, dummy, sizeof(dummy) - 1, true );
107drawDeviceIcon( gBootVolume, gui.screen.pixmap, p, true );
108drawStrCenteredAt( (char *) msg, &font_small, gui.screen.pixmap, gui.countdown.pos );
109
110// make this screen the new background
111memcpy( gui.backbuffer->pixels, gui.screen.pixmap->pixels, gui.backbuffer->width * gui.backbuffer->height * 4 );
112
113}
114
115int multi_buff = 18 * (timeout);
116 int multi = ++multi_buff;
117
118 int lasttime=0;
119
120 for ( time = time18(), timeout++; timeout > 0; )
121 {
122if( time18() > lasttime)
123{
124multi--;
125lasttime=time18();
126}
127
128 if (ch = readKeyboardStatus())
129 break;
130
131 // Count can be interrupted by holding down shift,
132 // control or alt key
133 if ( ( readKeyboardShiftFlags() & 0x0F ) != 0 )
134{
135 ch = 1;
136 break;
137 }
138
139 if ( time18() >= time )
140 {
141 time += 18;
142 timeout--;
143
144if( bootArgs->Video.v_display == VGA_TEXT_MODE )
145{
146moveCursor( col, row );
147printf("(%d) ", timeout);
148}
149 }
150
151if( bootArgs->Video.v_display == GRAPHICS_MODE )
152{
153drawProgressBar( gui.screen.pixmap, 100, gui.progressbar.pos , ( multi * 100 / multi_buff ) );
154gui.redraw = true;
155updateVRAM();
156}
157
158 }
159
160 flushKeyboardBuffer();
161
162 return ch;
163}
164
165//==========================================================================
166
167static char gBootArgs[BOOT_STRING_LEN];
168static char * gBootArgsPtr = gBootArgs;
169static char * gBootArgsEnd = gBootArgs + BOOT_STRING_LEN - 1;
170static char booterCommand[BOOT_STRING_LEN];
171static char booterParam[BOOT_STRING_LEN];
172
173//static
174void clearBootArgs(void)
175{
176gBootArgsPtr = gBootArgs;
177memset(gBootArgs, '\0', BOOT_STRING_LEN);
178
179if (bootArgs->Video.v_display == GRAPHICS_MODE) {
180clearGraphicBootPrompt();
181}
182}
183
184static void addBootArg(const char * argStr)
185{
186if ( (gBootArgsPtr + strlen(argStr) + 1) < gBootArgsEnd)
187{
188*gBootArgsPtr++ = ' ';
189strcat(gBootArgs, argStr);
190gBootArgsPtr += strlen(argStr);
191}
192}
193
194//==========================================================================
195
196//static
197void showBootPrompt(int row, bool visible)
198{
199extern char bootPrompt[];
200extern char bootRescanPrompt[];
201
202if( bootArgs->Video.v_display == VGA_TEXT_MODE ) {
203changeCursor( 0, row, kCursorTypeUnderline, 0 );
204clearScreenRows( row, kScreenLastRow );
205}
206
207clearBootArgs();
208
209if (visible) {
210if (bootArgs->Video.v_display == VGA_TEXT_MODE) {
211if (gEnableCDROMRescan) {
212printf( bootRescanPrompt );
213} else {
214printf( bootPrompt );
215}
216}
217} else {
218if (bootArgs->Video.v_display == GRAPHICS_MODE) {
219clearGraphicBootPrompt();
220} else {
221printf("Press Enter to start up the foreign OS. ");
222}
223}
224}
225
226//==========================================================================
227
228static void updateBootArgs( int key )
229{
230 key &= kASCIIKeyMask;
231
232 switch ( key )
233 {
234 case kBackspaceKey:
235 if ( gBootArgsPtr > gBootArgs )
236 {
237 int x, y, t;
238 getCursorPositionAndType( &x, &y, &t );
239 if ( x == 0 && y )
240 {
241 x = 80; y--;
242 }
243 if (x)
244x--;
245if( bootArgs->Video.v_display == VGA_TEXT_MODE )
246{
247setCursorPosition( x, y, 0 );
248putca(' ', 0x07, 1);
249} else
250updateGraphicBootPrompt(kBackspaceKey);
251
252*gBootArgsPtr-- = '\0';
253}
254
255break;
256
257 default:
258 if ( key >= ' ' && gBootArgsPtr < gBootArgsEnd)
259 {
260if( bootArgs->Video.v_display == VGA_TEXT_MODE )
261putchar(key); // echo to screen
262else
263updateGraphicBootPrompt(key);
264
265*gBootArgsPtr++ = key;
266}
267
268break;
269 }
270}
271
272//==========================================================================
273
274static const MenuItem * gMenuItems = NULL;
275
276static int gMenuItemCount;
277static int gMenuRow;
278static int gMenuHeight;
279static int gMenuTop;
280static int gMenuBottom;
281static int gMenuSelection;
282
283static int gMenuStart;
284static int gMenuEnd;
285
286static void printMenuItem( const MenuItem * item, int highlight )
287{
288 printf(" ");
289
290 if ( highlight )
291 putca(' ', 0x70, strlen(item->name) + 4);
292 else
293 putca(' ', 0x07, 40);
294
295 printf(" %40s\n", item->name);
296}
297
298//==========================================================================
299
300//static
301void showMenu( const MenuItem * items, int count,
302 int selection, int row, int height )
303{
304 int i;
305 CursorState cursorState;
306
307 if ( items == NULL || count == 0 )
308return;
309
310 // head and tail points to the start and the end of the list.
311 // top and bottom points to the first and last visible items
312 // in the menu window.
313
314 gMenuItems= items;
315 gMenuRow= row;
316 gMenuHeight= height;
317 gMenuItemCount= count;
318 gMenuTop= 0;
319 gMenuBottom= MIN( count, height ) - 1;
320 gMenuSelection= selection;
321
322 gMenuStart= 0;
323 gMenuEnd = MIN( count, gui.maxdevices ) - 1;
324
325// If the selected item is not visible, shift the list down.
326
327 if ( gMenuSelection > gMenuBottom )
328 {
329 gMenuTop += ( gMenuSelection - gMenuBottom );
330 gMenuBottom = gMenuSelection;
331 }
332
333if ( gMenuSelection > gMenuEnd )
334 {
335gMenuStart += ( gMenuSelection - gMenuEnd );
336 gMenuEnd = gMenuSelection;
337 }
338
339// Draw the visible items.
340
341if( bootArgs->Video.v_display == GRAPHICS_MODE )
342
343drawDeviceList(gMenuStart, gMenuEnd, gMenuSelection);
344
345else {
346
347changeCursor( 0, row, kCursorTypeHidden, &cursorState );
348
349for ( i = gMenuTop; i <= gMenuBottom; i++ )
350{
351printMenuItem( &items[i], (i == gMenuSelection) );
352}
353
354restoreCursor( &cursorState );
355 }
356}
357
358//==========================================================================
359
360static int updateMenu( int key, void ** paramPtr )
361{
362 int moved = 0;
363
364 union {
365 struct {
366 unsigned int
367 selectionUp : 1,
368 selectionDown : 1,
369 scrollUp : 1,
370 scrollDown : 1;
371 } f;
372 unsigned int w;
373 } draw = {{0}};
374
375 if ( gMenuItems == NULL )
376return 0;
377
378if( bootArgs->Video.v_display == GRAPHICS_MODE )
379{
380int res;
381
382// set navigation keys for horizontal layout as defaults
383int previous= 0x4B00;// left arrow
384int subsequent= 0x4D00;// right arrow
385int menu= 0x5000;// down arrow
386
387if ( gui.layout == VerticalLayout )
388{
389// set navigation keys for vertical layout
390previous= 0x4800;// up arrow
391subsequent= 0x5000;// down arrow
392menu= 0x4B00;// right arrow
393}
394
395if ( key == previous )
396{
397if ( gMenuSelection > gMenuTop )
398draw.f.selectionUp = 1;
399else if ( gMenuTop > 0 )
400draw.f.scrollDown = 1;
401
402}
403
404else if ( key == subsequent )
405{
406if ( gMenuSelection != gMenuBottom)
407draw.f.selectionDown = 1;
408else if ( gMenuBottom < ( gMenuItemCount - 1 ) )
409draw.f.scrollUp = 1;
410}
411
412else if ( key == menu )
413{
414if ( gui.menu.draw )
415updateInfoMenu(key);
416else
417drawInfoMenu();
418}
419
420else if ( gui.menu.draw )
421{
422res = updateInfoMenu(key);
423
424if ( res == CLOSE_INFO_MENU )
425gui.menu.draw = false;
426else
427{
428shouldboot = ( res != DO_NOT_BOOT );
429
430if ( shouldboot )
431gui.menu.draw = false;
432
433switch (res)
434{
435case BOOT_NORMAL:
436gVerboseMode = false;
437gBootMode = kBootModeNormal;
438break;
439
440case BOOT_VERBOSE:
441gVerboseMode = true;
442gBootMode = kBootModeNormal;
443addBootArg(kVerboseModeFlag);
444break;
445
446case BOOT_IGNORECACHE:
447gVerboseMode = false;
448gBootMode = kBootModeNormal;
449addBootArg(kIgnoreCachesFlag);
450break;
451
452case BOOT_SINGLEUSER:
453gVerboseMode = true;
454gBootMode = kBootModeNormal;
455addBootArg(kSingleUserModeFlag);
456break;
457}
458
459}
460
461}
462
463} else {
464switch ( key )
465{
466 case 0x4800: // Up Arrow
467if ( gMenuSelection != gMenuTop )
468draw.f.selectionUp = 1;
469else if ( gMenuTop > 0 )
470draw.f.scrollDown = 1;
471break;
472
473case 0x5000: // Down Arrow
474if ( gMenuSelection != gMenuBottom )
475draw.f.selectionDown = 1;
476else if ( gMenuBottom < (gMenuItemCount - 1) )
477draw.f.scrollUp = 1;
478break;
479}
480}
481
482 if ( draw.w )
483 {
484 if ( draw.f.scrollUp )
485 {
486 scollPage(0, gMenuRow, 40, gMenuRow + gMenuHeight - 1, 0x07, 1, 1);
487 gMenuTop++; gMenuBottom++;
488gMenuStart++; gMenuEnd++;
489 draw.f.selectionDown = 1;
490 }
491
492 if ( draw.f.scrollDown )
493 {
494 scollPage(0, gMenuRow, 40, gMenuRow + gMenuHeight - 1, 0x07, 1, -1);
495 gMenuTop--; gMenuBottom--;
496 gMenuStart--; gMenuEnd--;
497 draw.f.selectionUp = 1;
498 }
499
500 if ( draw.f.selectionUp || draw.f.selectionDown )
501 {
502
503CursorState cursorState;
504
505// Set cursor at current position, and clear inverse video.
506
507if( bootArgs->Video.v_display == VGA_TEXT_MODE )
508{
509changeCursor( 0, gMenuRow + gMenuSelection - gMenuTop, kCursorTypeHidden, &cursorState );
510printMenuItem( &gMenuItems[gMenuSelection], 0 );
511}
512
513if ( draw.f.selectionUp )
514{
515gMenuSelection--;
516if(( gMenuSelection - gMenuStart) == -1 )
517{
518gMenuStart--;
519gMenuEnd--;
520}
521
522} else {
523gMenuSelection++;
524if(( gMenuSelection - ( gui.maxdevices - 1) - gMenuStart) > 0 )
525{
526gMenuStart++;
527gMenuEnd++;
528}
529 }
530
531if( bootArgs->Video.v_display == VGA_TEXT_MODE )
532 {
533moveCursor( 0, gMenuRow + gMenuSelection - gMenuTop );
534printMenuItem( &gMenuItems[gMenuSelection], 1 );
535restoreCursor( &cursorState );
536
537 } else
538
539drawDeviceList (gMenuStart, gMenuEnd, gMenuSelection);
540
541}
542
543 *paramPtr = gMenuItems[gMenuSelection].param;
544 moved = 1;
545 }
546
547return moved;
548}
549
550//==========================================================================
551
552static void skipblanks( const char ** cpp )
553{
554 while ( **(cpp) == ' ' || **(cpp) == '\t' ) ++(*cpp);
555}
556
557//==========================================================================
558
559static const char * extractKernelName( char ** cpp )
560{
561 char * kn = *cpp;
562 char * cp = *cpp;
563 char c;
564
565 // Convert char to lower case.
566
567 c = *cp | 0x20;
568
569 // Must start with a letter or a '/'.
570
571 if ( (c < 'a' || c > 'z') && ( c != '/' ) )
572 return 0;
573
574 // Keep consuming characters until we hit a separator.
575
576 while ( *cp && (*cp != '=') && (*cp != ' ') && (*cp != '\t') )
577 cp++;
578
579 // Only SPACE or TAB separator is accepted.
580 // Reject everything else.
581
582 if (*cp == '=')
583 return 0;
584
585 // Overwrite the separator, and move the pointer past
586 // the kernel name.
587
588 if (*cp != '\0') *cp++ = '\0';
589 *cpp = cp;
590
591 return kn;
592}
593
594//==========================================================================
595
596static void
597printMemoryInfo(void)
598{
599 int line;
600 int i;
601 MemoryRange *mp = bootInfo->memoryMap;
602
603 // Activate and clear page 1
604 setActiveDisplayPage(1);
605 clearScreenRows(0, 24);
606 setCursorPosition( 0, 0, 1 );
607
608 printf("BIOS reported memory ranges:\n");
609 line = 1;
610 for (i=0; i<bootInfo->memoryMapCount; i++) {
611 printf("Base 0x%08x%08x, ",
612 (unsigned long)(mp->base >> 32),
613 (unsigned long)(mp->base));
614 printf("length 0x%08x%08x, type %d\n",
615 (unsigned long)(mp->length >> 32),
616 (unsigned long)(mp->length),
617 mp->type);
618 if (line++ > 20) {
619 pause();
620 line = 0;
621 }
622 mp++;
623 }
624 if (line > 0) {
625 pause();
626 }
627
628 setActiveDisplayPage(0);
629}
630
631char *getMemoryInfoString()
632{
633 int i;
634 MemoryRange *mp = bootInfo->memoryMap;
635char *buff = malloc(sizeof(char)*1024);
636if(!buff) return 0;
637
638char info[] = "BIOS reported memory ranges:\n";
639sprintf(buff, "%s", info);
640 for (i=0; i<bootInfo->memoryMapCount; i++) {
641 sprintf( buff+strlen(buff), "Base 0x%08x%08x, ",
642 (unsigned long)(mp->base >> 32),
643 (unsigned long)(mp->base));
644 sprintf( buff+strlen(buff), "length 0x%08x%08x, type %d\n",
645 (unsigned long)(mp->length >> 32),
646 (unsigned long)(mp->length),
647 mp->type);
648 mp++;
649 }
650return buff;
651}
652
653//==========================================================================
654
655void lspci(void)
656{
657if (bootArgs->Video.v_display == VGA_TEXT_MODE) {
658setActiveDisplayPage(1);
659clearScreenRows(0, 24);
660setCursorPosition(0, 0, 1);
661}
662
663dump_pci_dt(root_pci_dev->children);
664
665pause();
666
667if (bootArgs->Video.v_display == VGA_TEXT_MODE) {
668setActiveDisplayPage(0);
669}
670}
671
672//==========================================================================
673//Azi: autoresolution
674int key;
675int nextRow;
676BVRef menuBVR;
677bool showPrompt;
678extern void reloadAutoRes();
679
680int getBootOptions(bool firstRun)
681{
682int i;
683//int key;
684//int nextRow;
685int timeout;
686int bvCount;
687BVRef bvr;
688//BVRef menuBVR;
689//bool showPrompt;
690bool newShowPrompt, isCDROM;
691
692// Initialize default menu selection entry.
693gBootVolume = menuBVR = selectBootVolume(bvChain);
694
695if (biosDevIsCDROM(gBIOSDev)) {
696isCDROM = true;
697} else {
698isCDROM = false;
699}
700
701// ensure we're in graphics mode if gui is setup
702if (gui.initialised && bootArgs->Video.v_display == VGA_TEXT_MODE)
703{
704setVideoMode(GRAPHICS_MODE, 0);
705}
706
707// Clear command line boot arguments
708clearBootArgs();
709
710// Allow user to override default timeout.
711if (multiboot_timeout_set) {
712timeout = multiboot_timeout;
713} else if (!getIntForKey(kTimeoutKey, &timeout, &bootInfo->bootConfig)) {
714/* If there is no timeout key in the file use the default timeout
715 which is different for CDs vs. hard disks. However, if not booting
716 a CD and no config file could be loaded set the timeout
717 to zero which causes the menu to display immediately.
718 This way, if no partitions can be found, that is the disk is unpartitioned
719 or simply cannot be read) then an empty menu is displayed.
720 If some partitions are found, for example a Windows partition, then
721 these will be displayed in the menu as foreign partitions.
722 */
723if (isCDROM) {
724timeout = kCDBootTimeout;
725} else {
726timeout = sysConfigValid ? kBootTimeout : 0;
727}
728}
729
730if (timeout < 0) {
731gBootMode |= kBootModeQuiet;
732}
733
734// If the user is holding down a modifier key, enter safe mode.
735if ((readKeyboardShiftFlags() & 0x0F) != 0) {
736gBootMode |= kBootModeSafe;
737}
738
739// Checking user pressed keys
740bool f8press = false, spress = false, vpress = false;
741while (readKeyboardStatus()) {
742key = bgetc ();
743if (key == 0x4200) f8press = true;
744if ((key & 0xff) == 's' || (key & 0xff) == 'S') spress = true;
745if ((key & 0xff) == 'v' || (key & 0xff) == 'V') vpress = true;
746}
747// If user typed F8, abort quiet mode, and display the menu.
748if (f8press) {
749gBootMode &= ~kBootModeQuiet;
750timeout = 0;
751}
752// If user typed 'v' or 'V', boot in verbose mode.
753if ((gBootMode & kBootModeQuiet) && firstRun && vpress) {
754addBootArg(kVerboseModeFlag);
755}
756// If user typed 's' or 'S', boot in single user mode.
757if ((gBootMode & kBootModeQuiet) && firstRun && spress) {
758addBootArg(kSingleUserModeFlag);
759}
760
761if (bootArgs->Video.v_display == VGA_TEXT_MODE) {
762setCursorPosition(0, 0, 0);
763clearScreenRows(0, kScreenLastRow);
764if (!(gBootMode & kBootModeQuiet)) {
765// Display banner and show hardware info.
766printf(bootBanner, (bootInfo->convmem + bootInfo->extmem) / 1024);
767printf(getVBEInfoString());
768}
769changeCursor(0, kMenuTopRow, kCursorTypeUnderline, 0);
770verbose("Scanning device %x...", gBIOSDev);
771}
772
773// When booting from CD, default to hard drive boot when possible.
774if (isCDROM && firstRun) {
775const char *val;
776char *prompt = NULL;
777char *name = NULL;
778int cnt;
779int optionKey;
780
781//Azi:---
782if (getValueForKey(kCDROMPromptKey, &val, &cnt, &bootInfo->bootConfig))
783{
784prompt = malloc(cnt + 1);
785strncat(prompt, val, cnt);
786}
787else
788{
789name = malloc(80);
790getBootVolumeDescription(gBootVolume, name, 79, false);
791prompt = malloc(256);
792//Azi: too long for 80X25, GUI=n, vmware, depending on the device name (more than 5 char).
793sprintf(prompt, "Press any key to start up from %s, or press F8 to enter startup options.", name);
794free(name);
795}
796
797if (getIntForKey( kCDROMOptionKey, &optionKey, &bootInfo->bootConfig ))
798{
799// The key specified is a special key.
800}
801else
802{
803// Default to F8.
804optionKey = 0x4200;
805}
806
807// If the timeout is zero then it must have been set above due to the
808// early catch of F8 which means the user wants to set boot options
809// which we ought to interpret as meaning he wants to boot the CD.
810if (timeout != 0) {
811key = countdown(prompt, kMenuTopRow, timeout);
812} else {
813key = optionKey;
814}
815
816if (prompt != NULL) {
817free(prompt);
818}
819
820clearScreenRows( kMenuTopRow, kMenuTopRow + 2 );
821
822// Hit the option key ?
823if (key == optionKey) {
824gBootMode &= ~kBootModeQuiet;
825timeout = 0;
826} else {
827key = key & 0xFF;
828
829// Try booting hard disk if user pressed 'h'
830if (biosDevIsCDROM(gBIOSDev) && key == 'h') {
831BVRef bvr;
832
833// Look at partitions hosting OS X other than the CD-ROM
834for (bvr = bvChain; bvr; bvr=bvr->next) {
835if ((bvr->flags & kBVFlagSystemVolume) && bvr->biosdev != gBIOSDev) {
836gBootVolume = bvr;
837}
838}
839}
840goto done;
841}
842}
843
844if (gBootMode & kBootModeQuiet) {
845// No input allowed from user.
846goto done;
847}
848
849if (firstRun && timeout > 0 && countdown("Press any key to enter startup options.", kMenuTopRow, timeout) == 0) {
850// If the user is holding down a modifier key,
851// enter safe mode.
852if ((readKeyboardShiftFlags() & 0x0F) != 0) {
853gBootMode |= kBootModeSafe;
854}
855goto done;
856}
857
858if (gDeviceCount) {
859// Allocate memory for an array of menu items.
860menuItems = malloc(sizeof(MenuItem) * gDeviceCount);
861if (menuItems == NULL) {
862goto done;
863}
864
865// Associate a menu item for each BVRef.
866for (bvr=bvChain, i=gDeviceCount-1, selectIndex=0; bvr; bvr=bvr->next) {
867if (bvr->visible) {
868getBootVolumeDescription(bvr, menuItems[i].name, sizeof(menuItems[i].name) - 1, true);
869menuItems[i].param = (void *) bvr;
870if (bvr == menuBVR) {
871selectIndex = i;
872}
873i--;
874}
875}
876}
877
878if (bootArgs->Video.v_display == GRAPHICS_MODE) {
879// redraw the background buffer
880gui.logo.draw = true;
881drawBackground();
882gui.devicelist.draw = true;
883gui.redraw = true;
884if (!(gBootMode & kBootModeQuiet)) {
885
886// Check if "Boot Banner"=N switch is present in config file.
887getBoolForKey(kBootBannerKey, &showBootBanner, &bootInfo->bootConfig);
888if (showBootBanner) {
889// Display banner and show hardware info.
890gprintf(&gui.screen, bootBanner + 1, (bootInfo->convmem + bootInfo->extmem) / 1024);
891}
892
893// redraw background
894memcpy(gui.backbuffer->pixels, gui.screen.pixmap->pixels, gui.backbuffer->width * gui.backbuffer->height * 4);
895}
896} else {
897// Clear screen and hide the blinking cursor.
898clearScreenRows(kMenuTopRow, kMenuTopRow + 2);
899changeCursor(0, kMenuTopRow, kCursorTypeHidden, 0);
900}
901
902nextRow = kMenuTopRow;
903showPrompt = true;
904
905if (gDeviceCount) {
906if( bootArgs->Video.v_display == VGA_TEXT_MODE ) {
907printf("Use \30\31 keys to select the startup volume.");
908}
909showMenu( menuItems, gDeviceCount, selectIndex, kMenuTopRow + 2, kMenuMaxItems );
910nextRow += MIN( gDeviceCount, kMenuMaxItems ) + 3;
911}
912
913// Show the boot prompt.
914showPrompt = (gDeviceCount == 0) || (menuBVR->flags & kBVFlagNativeBoot);
915showBootPrompt( nextRow, showPrompt );
916
917do {
918if (bootArgs->Video.v_display == GRAPHICS_MODE) {
919// redraw background
920memcpy( gui.backbuffer->pixels, gui.screen.pixmap->pixels, gui.backbuffer->width * gui.backbuffer->height * 4 );
921// reset cursor co-ords
922gui.debug.cursor = pos( gui.screen.width - 160 , 10 );
923}
924key = getc();
925updateMenu( key, (void **) &menuBVR );
926newShowPrompt = (gDeviceCount == 0) || (menuBVR->flags & kBVFlagNativeBoot);
927
928if (newShowPrompt != showPrompt) {
929showPrompt = newShowPrompt;
930showBootPrompt( nextRow, showPrompt );
931}
932
933if (showPrompt) {
934updateBootArgs(key);
935}
936
937switch (key) {
938case kReturnKey:
939if (gui.menu.draw) {
940key=0;
941break;
942}
943if (*gBootArgs == '?') {
944char * argPtr = gBootArgs;
945
946// Skip the leading "?" character.
947argPtr++;
948getNextArg(&argPtr, booterCommand);
949getNextArg(&argPtr, booterParam);
950
951/*
952* TODO: this needs to be refactored.
953*/
954if (strcmp( booterCommand, "video" ) == 0)
955{
956if (bootArgs->Video.v_display == GRAPHICS_MODE)
957{
958showInfoBox(getVBEInfoString(), getVBEModeInfoString());
959}
960else
961{
962printVBEModeInfo();
963}
964}
965else if ( strcmp( booterCommand, "memory" ) == 0)
966{
967if (bootArgs->Video.v_display == GRAPHICS_MODE )
968{
969showInfoBox("Memory Map", getMemoryInfoString());
970}
971else
972{
973printMemoryInfo();
974}
975}
976else if (strcmp(booterCommand, "lspci") == 0)
977{
978lspci();
979}
980else if (strcmp(booterCommand, "more") == 0)
981{
982showTextFile(booterParam);
983}
984else if (strcmp(booterCommand, "rd") == 0)
985{
986processRAMDiskCommand(&argPtr, booterParam);
987}
988else if (strcmp(booterCommand, "norescan") == 0)
989{
990if (gEnableCDROMRescan)
991{
992gEnableCDROMRescan = false;
993break;
994}
995}
996else
997{
998showHelp();
999}
1000key = 0;
1001showBootPrompt(nextRow, showPrompt);
1002break;
1003}
1004gBootVolume = menuBVR;
1005setRootVolume(menuBVR);
1006gBIOSDev = menuBVR->biosdev;
1007break;
1008
1009case kEscapeKey:
1010clearBootArgs();
1011break;
1012
1013// AutoResolution - Reapply the patch if Graphics Mode was incorrect
1014// or EDID Info was insane.
1015case kF2Key:
1016reloadAutoRes();
1017break;
1018
1019case kF5Key:
1020// New behavior:
1021// Clear gBootVolume to restart the loop
1022// if the user enabled rescanning the optical drive.
1023// Otherwise boot the default boot volume.
1024if (gEnableCDROMRescan) {
1025gBootVolume = NULL;
1026clearBootArgs();
1027}
1028break;
1029
1030case kF10Key: //Azi: disable "Scan Single Drive"=y (if set) and rescan disks.
1031gScanSingleDrive = false;
1032scanDisks(gBIOSDev, &bvCount);
1033gBootVolume = NULL;
1034clearBootArgs();
1035break;
1036
1037case kTabKey:
1038// New behavior:
1039// Switch between text & graphic interfaces
1040// Only Permitted if started in graphics interface
1041if (useGUI) {
1042if (bootArgs->Video.v_display == GRAPHICS_MODE) {
1043setVideoMode(VGA_TEXT_MODE, 0);
1044
1045setCursorPosition(0, 0, 0);
1046clearScreenRows(0, kScreenLastRow);
1047
1048// Display banner and show hardware info.
1049printf(bootBanner, (bootInfo->convmem + bootInfo->extmem) / 1024);
1050printf(getVBEInfoString());
1051
1052clearScreenRows(kMenuTopRow, kMenuTopRow + 2);
1053changeCursor(0, kMenuTopRow, kCursorTypeHidden, 0);
1054
1055nextRow = kMenuTopRow;
1056showPrompt = true;
1057
1058if (gDeviceCount) {
1059printf("Use \30\31 keys to select the startup volume.");
1060showMenu(menuItems, gDeviceCount, selectIndex, kMenuTopRow + 2, kMenuMaxItems);
1061nextRow += MIN(gDeviceCount, kMenuMaxItems) + 3;
1062}
1063
1064showPrompt = (gDeviceCount == 0) || (menuBVR->flags & kBVFlagNativeBoot);
1065showBootPrompt(nextRow, showPrompt);
1066//changeCursor( 0, kMenuTopRow, kCursorTypeUnderline, 0 );
1067} else {
1068gui.redraw = true;
1069setVideoMode(GRAPHICS_MODE, 0);
1070updateVRAM();
1071}
1072}
1073key = 0;
1074break;
1075
1076default:
1077key = 0;
1078break;
1079}
1080} while (0 == key);
1081
1082done:
1083if (bootArgs->Video.v_display == VGA_TEXT_MODE) {
1084clearScreenRows(kMenuTopRow, kScreenLastRow);
1085changeCursor(0, kMenuTopRow, kCursorTypeUnderline, 0);
1086}
1087shouldboot = false;
1088gui.menu.draw = false;
1089if (menuItems) {
1090free(menuItems);
1091menuItems = NULL;
1092}
1093return 0;
1094}
1095
1096//==========================================================================
1097
1098extern unsigned char chainbootdev;
1099extern unsigned char chainbootflag;
1100
1101bool copyArgument(const char *argName, const char *val, int cnt, char **argP, int *cntRemainingP)
1102{
1103 int argLen = argName ? strlen(argName) : 0;
1104 int len = argLen + cnt + 1; // +1 to account for space
1105
1106 if (len > *cntRemainingP) {
1107 error("Warning: boot arguments too long, truncating\n");
1108 return false;
1109 }
1110
1111 if (argName) {
1112 strncpy( *argP, argName, argLen );
1113 *argP += argLen;
1114 *argP[0] = '=';
1115 (*argP)++;
1116 len++; // +1 to account for '='
1117 }
1118 strncpy( *argP, val, cnt );
1119 *argP += cnt;
1120 *argP[0] = ' ';
1121 (*argP)++;
1122
1123 *cntRemainingP -= len;
1124 return true;
1125}
1126
1127//
1128// Returns TRUE if an argument was copied, FALSE otherwise
1129bool
1130processBootArgument(
1131 const char *argName, // The argument to search for
1132 const char *userString, // Typed-in boot arguments
1133 const char *kernelFlags, // Kernel flags from config table
1134 const char *configTable,
1135 char **argP, // Output value
1136 int *cntRemainingP, // Output count
1137 char *foundVal // found value
1138 )
1139{
1140 const char *val;
1141 int cnt;
1142 bool found = false;
1143
1144 if (getValueForBootKey(userString, argName, &val, &cnt)) {
1145 // Don't copy; these values will be copied at the end of argument processing.
1146 found = true;
1147 } else if (getValueForBootKey(kernelFlags, argName, &val, &cnt)) {
1148 // Don't copy; these values will be copied at the end of argument processing.
1149 found = true;
1150 } else if (getValueForKey(argName, &val, &cnt, &bootInfo->bootConfig)) {
1151 copyArgument(argName, val, cnt, argP, cntRemainingP);
1152 found = true;
1153 }
1154 if (found && foundVal) {
1155 strlcpy(foundVal, val, cnt+1);
1156 }
1157 return found;
1158}
1159
1160// Maximum config table value size
1161#define VALUE_SIZE 2048
1162
1163int
1164processBootOptions()
1165{
1166 const char * cp = gBootArgs;
1167 const char * val = 0;
1168 const char * kernel;
1169 int cnt;
1170 int userCnt;
1171 int cntRemaining;
1172 char * argP;
1173 char uuidStr[64];
1174 bool uuidSet = false;
1175 char * configKernelFlags;
1176 char * valueBuffer;
1177
1178 valueBuffer = malloc(VALUE_SIZE);
1179
1180 skipblanks( &cp );
1181
1182 // Update the unit and partition number.
1183
1184 if ( gBootVolume )
1185 {
1186 if (!( gBootVolume->flags & kBVFlagNativeBoot ))
1187 {
1188 readBootSector( gBootVolume->biosdev, gBootVolume->part_boff,
1189 (void *) 0x7c00 );
1190
1191 //
1192 // Setup edx, and signal intention to chain load the
1193 // foreign booter.
1194 //
1195
1196 chainbootdev = gBootVolume->biosdev;
1197 chainbootflag = 1;
1198
1199 return 1;
1200 }
1201
1202 setRootVolume(gBootVolume);
1203
1204 }
1205 // If no boot volume fail immediately because we're just going to fail
1206 // trying to load the config file anyway.
1207 else
1208 return -1;
1209
1210// Moved here to enable search for override Boot.plist on OS specific folders.
1211// Find out which Mac OS version we're booting.
1212getOSVersion(gMacOSVersion);
1213
1214//Azi: implemented at loadOverrideConfig.
1215 // Load config table specified by the user, or use the default.
1216 //if (!getValueForBootKey(cp, "config", &val, &cnt)) {
1217 // val = 0;
1218 // cnt = 0;
1219 //}
1220
1221 // Load com.apple.Boot.plist from the selected volume
1222 // and use its contents to override default bootConfig.
1223 // This is not a mandatory operation anymore.
1224
1225 loadOverrideConfig(&bootInfo->overrideConfig);
1226
1227 // Use the kernel name specified by the user, or fetch the name
1228 // in the config table, or use the default if not specified.
1229 // Specifying a kernel name on the command line, or specifying
1230 // a non-default kernel name in the config file counts as
1231 // overriding the kernel, which causes the kernelcache not
1232 // to be used.
1233
1234 gOverrideKernel = false;
1235 if (( kernel = extractKernelName((char **)&cp) )) {
1236 strcpy( bootInfo->bootFile, kernel );
1237 gOverrideKernel = true;
1238 } else {
1239 if ( getValueForKey( kKernelNameKey, &val, &cnt, &bootInfo->bootConfig ) ) {
1240 strlcpy( bootInfo->bootFile, val, cnt+1 );
1241 if (strcmp( bootInfo->bootFile, kDefaultKernelName ) != 0) {
1242 gOverrideKernel = true;
1243 }
1244 } else {
1245 strcpy( bootInfo->bootFile, kDefaultKernelName );
1246 }
1247 }
1248
1249 cntRemaining = BOOT_STRING_LEN - 2; // save 1 for NULL, 1 for space
1250 argP = bootArgs->CommandLine;
1251
1252 // Get config table kernel flags, if not ignored.
1253 if (getValueForBootKey(cp, kIgnoreBootFileFlag, &val, &cnt) ||
1254 !getValueForKey( kKernelFlagsKey, &val, &cnt, &bootInfo->bootConfig )) {
1255 val = "";
1256 cnt = 0;
1257 }
1258 configKernelFlags = malloc(cnt + 1);
1259 strlcpy(configKernelFlags, val, cnt + 1);
1260
1261 if (processBootArgument(kBootUUIDKey, cp, configKernelFlags, bootInfo->config, &argP, &cntRemaining, 0))
1262{
1263 // boot-uuid was set either on the command-line or in the config file.
1264 uuidSet = true;
1265 }
1266else
1267{
1268 //
1269 // Try an alternate method for getting the root UUID on boot helper partitions.
1270 //
1271 if (gBootVolume->flags & kBVFlagBooter)
1272 {
1273 if ((loadHelperConfig(&bootInfo->helperConfig) == 0) &&
1274getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig) )
1275 {
1276 getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig);
1277 copyArgument(kBootUUIDKey, val, cnt, &argP, &cntRemaining);
1278 uuidSet = true;
1279 }
1280 }
1281
1282 if (!uuidSet && gBootVolume->fs_getuuid && gBootVolume->fs_getuuid (gBootVolume, uuidStr) == 0)
1283{
1284 verbose("Setting boot-uuid to: %s\n", uuidStr);
1285 copyArgument(kBootUUIDKey, uuidStr, strlen(uuidStr), &argP, &cntRemaining);
1286 uuidSet = true;
1287 }
1288 }
1289
1290 if (!processBootArgument(kRootDeviceKey, cp, configKernelFlags, bootInfo->config, &argP, &cntRemaining, gRootDevice))
1291{
1292 cnt = 0;
1293 if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig))
1294{
1295 valueBuffer[0] = '*';
1296 cnt++;
1297 strlcpy(valueBuffer + 1, val, cnt);
1298 val = valueBuffer;
1299 }
1300else
1301{
1302 if (uuidSet)
1303{
1304 val = "*uuid";
1305 cnt = 5;
1306 }
1307else
1308{
1309 // Don't set "rd=.." if there is no boot device key
1310 // and no UUID.
1311 val = "";
1312 cnt = 0;
1313 }
1314 }
1315
1316 if (cnt > 0)
1317{
1318 copyArgument( kRootDeviceKey, val, cnt, &argP, &cntRemaining);
1319 }
1320 strlcpy( gRootDevice, val, (cnt + 1));
1321 }
1322
1323 if (!getValueForBootKey(cp, kSafeModeFlag, &val, &cnt) &&
1324 !getValueForBootKey(configKernelFlags, kSafeModeFlag, &val, &cnt))
1325{
1326 if (gBootMode & kBootModeSafe)
1327{
1328 copyArgument(0, kSafeModeFlag, strlen(kSafeModeFlag), &argP, &cntRemaining);
1329 }
1330 }
1331
1332 // Store the merged kernel flags and boot args.
1333
1334 cnt = strlen(configKernelFlags);
1335 if (cnt) {
1336 if (cnt > cntRemaining) {
1337 error("Warning: boot arguments too long, truncating\n");
1338 cnt = cntRemaining;
1339 }
1340 strncpy(argP, configKernelFlags, cnt);
1341 argP[cnt++] = ' ';
1342 cntRemaining -= cnt;
1343 }
1344 userCnt = strlen(cp);
1345 if (userCnt > cntRemaining) {
1346 error("Warning: boot arguments too long, truncating\n");
1347 userCnt = cntRemaining;
1348 }
1349 strncpy(&argP[cnt], cp, userCnt);
1350 argP[cnt+userCnt] = '\0';
1351
1352if(!shouldboot)
1353{
1354gVerboseMode = getValueForKey( kVerboseModeFlag, &val, &cnt, &bootInfo->bootConfig ) ||
1355getValueForKey( kSingleUserModeFlag, &val, &cnt, &bootInfo->bootConfig );
1356
1357gBootMode = ( getValueForKey( kSafeModeFlag, &val, &cnt, &bootInfo->bootConfig ) ) ?
1358kBootModeSafe : kBootModeNormal;
1359
1360 if ( getValueForKey( kIgnoreCachesFlag, &val, &cnt, &bootInfo->bootConfig ) ) {
1361 gBootMode = kBootModeSafe;
1362 }
1363}
1364
1365if ( getValueForKey( kMKextCacheKey, &val, &cnt, &bootInfo->bootConfig ) )
1366{
1367strlcpy(gMKextName, val, cnt + 1);
1368}
1369
1370 free(configKernelFlags);
1371 free(valueBuffer);
1372
1373 return 0;
1374}
1375
1376static bool getOSVersion(char *str) //Azi: moved from boot.c
1377{
1378bool valid = false;
1379config_file_t systemVersion;
1380const char *val;
1381int len;
1382
1383if (!loadConfigFile("/System/Library/CoreServices/SystemVersion.plist", &systemVersion))
1384{ //Azi: so, is this path on selected or current volume??
1385valid = true;
1386}
1387else if (!loadConfigFile("/System/Library/CoreServices/ServerVersion.plist", &systemVersion))
1388{
1389valid = true;
1390}
1391
1392if (valid)
1393{
1394if (getValueForKey("ProductVersion", &val, &len, &systemVersion))
1395{
1396// getValueForKey uses const char for val
1397// so copy it and trim
1398*str = '\0';
1399strncat(str, val, MIN(len, 4));
1400}
1401else
1402valid = false;
1403}
1404
1405return valid;
1406}
1407
1408//==========================================================================
1409// Load the help file and display the file contents on the screen.
1410
1411static void showTextBuffer(char *buf, int size)
1412{
1413char*bp;
1414intline;
1415intline_offset;
1416intc;
1417
1418if (bootArgs->Video.v_display == GRAPHICS_MODE) {
1419showInfoBox( "Press q to quit\n",buf );
1420return;
1421}
1422
1423 bp = buf;
1424 while (size-- > 0) {
1425if (*bp == '\n') {
1426*bp = '\0';
1427}
1428bp++;
1429 }
1430 *bp = '\1';
1431 line_offset = 0;
1432
1433 setActiveDisplayPage(1);
1434
1435 while (1) {
1436clearScreenRows(0, 24);
1437setCursorPosition(0, 0, 1);
1438bp = buf;
1439for (line = 0; *bp != '\1' && line < line_offset; line++) {
1440while (*bp != '\0') {
1441bp++;
1442}
1443bp++;
1444}
1445for (line = 0; *bp != '\1' && line < 23; line++) {
1446setCursorPosition(0, line, 1);
1447printf("%s\n", bp);
1448while (*bp != '\0') {
1449bp++;
1450}
1451bp++;
1452}
1453
1454setCursorPosition(0, 23, 1);
1455if (*bp == '\1') {
1456printf("[Type %sq or space to quit viewer]", (line_offset > 0) ? "p for previous page, " : "");
1457} else {
1458printf("[Type %s%sq to quit viewer]", (line_offset > 0) ? "p for previous page, " : "", (*bp != '\1') ? "space for next page, " : "");
1459}
1460
1461c = getc();
1462if (c == 'q' || c == 'Q') {
1463break;
1464}
1465if ((c == 'p' || c == 'P') && line_offset > 0) {
1466line_offset -= 23;
1467}
1468if (c == ' ') {
1469if (*bp == '\1') {
1470break;
1471} else {
1472line_offset += 23;
1473}
1474}
1475 }
1476 setActiveDisplayPage(0);
1477}
1478
1479void showHelp(void)
1480{
1481if (bootArgs->Video.v_display == GRAPHICS_MODE) {
1482showInfoBox("Help. Press q to quit.\n", (char *)BootHelp_txt);
1483} else {
1484showTextBuffer((char *)BootHelp_txt, BootHelp_txt_len);
1485}
1486}
1487
1488void showTextFile(const char * filename) //Azi:?more
1489{
1490#define MAX_TEXT_FILE_SIZE 65536
1491char *buf;
1492int fd;
1493int size;
1494
1495if ((fd = open_bvdev("bt(0,0)", filename, 0)) < 0)
1496{
1497printf("\nFile not found: %s\n", filename);
1498sleep(2);
1499return;
1500}
1501
1502 size = file_size(fd);
1503
1504 if (size > MAX_TEXT_FILE_SIZE)
1505 {
1506size = MAX_TEXT_FILE_SIZE;
1507}
1508
1509 buf = malloc(size);
1510 read(fd, buf, size);
1511 close(fd);
1512
1513showTextBuffer(buf, size);
1514free(buf);
1515}
1516
1517// This is a very simplistic prompting scheme that just grabs two hex characters
1518// Eventually we need to do something more user-friendly like display a menu
1519// based off of the Multiboot device list
1520
1521//Azi: is this stuff still used for anything?? check multiboot()...
1522int selectAlternateBootDevice(int bootdevice)
1523{
1524int key;
1525int newbootdevice;
1526int digitsI = 0;
1527char *end;
1528char digits[3] = {0,0,0};
1529
1530// We've already printed the current boot device so user knows what it is
1531printf("Typical boot devices are 80 (First HD), 81 (Second HD)\n");
1532printf("Enter two-digit hexadecimal boot device [%02x]: ", bootdevice);
1533do {
1534key = getc();
1535switch (key & kASCIIKeyMask) {
1536case kBackspaceKey:
1537if (digitsI > 0) {
1538int x, y, t;
1539getCursorPositionAndType(&x, &y, &t);
1540// Assume x is not 0;
1541x--;
1542setCursorPosition(x,y,0); // back up one char
1543// Overwrite with space without moving cursor position
1544putca(' ', 0x07, 1);
1545digitsI--;
1546} else {
1547// TODO: Beep or something
1548}
1549break;
1550
1551case kReturnKey:
1552digits[digitsI] = '\0';
1553newbootdevice = strtol(digits, &end, 16);
1554if (end == digits && *end == '\0') {
1555// User entered empty string
1556printf("\nUsing default boot device %x\n", bootdevice);
1557key = 0;
1558} else if(end != digits && *end == '\0') {
1559bootdevice = newbootdevice;
1560printf("\n");
1561key = 0; // We gots da boot device
1562} else {
1563printf("\nCouldn't parse. try again: ");
1564digitsI = 0;
1565}
1566break;
1567
1568default:
1569if (isxdigit(key & kASCIIKeyMask) && digitsI < 2) {
1570putc(key & kASCIIKeyMask);
1571digits[digitsI++] = key & kASCIIKeyMask;
1572} else {
1573// TODO: Beep or something
1574}
1575break;
1576};
1577} while (key != 0);
1578
1579return bootdevice;
1580}
1581
1582bool promptForRescanOption(void)
1583{
1584printf("\nWould you like to enable media rescan option?\nPress ENTER to enable or any key to skip.\n");
1585if (getc() == kReturnKey) {
1586return true;
1587} else {
1588return false;
1589}
1590}
1591

Archive Download this file

Revision: 621