Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 11