Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/boot2/boot.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 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 * Mach Operating System
26 * Copyright (c) 1990 Carnegie-Mellon University
27 * Copyright (c) 1989 Carnegie-Mellon University
28 * All rights reserved. The CMU software License Agreement specifies
29 * the terms and conditions for use and redistribution.
30 */
31
32/*
33 * INTEL CORPORATION PROPRIETARY INFORMATION
34 *
35 * This software is supplied under the terms of a license agreement or
36 * nondisclosure agreement with Intel Corporation and may not be copied
37 * nor disclosed except in accordance with the terms of that agreement.
38 *
39 * Copyright 1988, 1989 by Intel Corporation
40 */
41
42/*
43 * Copyright 1993 NeXT Computer, Inc.
44 * All rights reserved.
45 */
46
47/*
48 * Completely reworked by Sam Streeper (sam_s@NeXT.com)
49 * Reworked again by Curtis Galloway (galloway@NeXT.com)
50 */
51
52
53#include "boot.h"
54#include "bootstruct.h"
55#include "fake_efi.h"
56#include "sl.h"
57#include "libsa.h"
58#include "platform.h"
59#include "graphics.h"
60#ifndef OPTION_ROM
61#include "appleboot.h"
62#endif
63#include "modules.h"
64#include "xml.h"
65
66#ifndef DEBUG_BOOT
67#define DEBUG_BOOT 0
68#endif
69
70#if DEBUG_BOOT
71#define DBG(x...)printf(x)
72#else
73#define DBG(x...)
74#endif
75
76
77typedef struct platform_info {
78char platformName[PLATFORM_NAME_LEN];
79char rootPath[ROOT_PATH_LEN];
80} PlatformInfo;
81
82
83char *gboardproduct = NULL;
84char *gPlatformName = NULL;
85//char gRootPath[256];
86long gBootMode; /* defaults to 0 == kBootModeNormal */
87bool gOverrideKernel;
88char gBootKernelCacheFile[512];
89char gMKextName[512];
90char gMacOSVersion[8];
91char *gRootDevice = NULL;
92
93#ifndef OPTION_ROM
94bool gEnableCDROMRescan;
95bool gScanSingleDrive;
96#endif
97
98int bvCount = 0;
99//intmenucount = 0;
100int gDeviceCount = 0;
101
102BVRef bvr;
103BVRef menuBVR;
104BVRef bvChain;
105static bool forcecache = false;
106
107static void zeroBSS(void);
108#ifdef SAFE_MALLOC
109static inline void malloc_error(char *addr, size_t size, const char *file, int line);
110#else
111static inline void malloc_error(char *addr, size_t size);
112#endif
113static int ExecKernel(void *binary);
114static bool getOSVersion(char *str);
115static void getRootDevice();
116#ifdef NBP_SUPPORT
117static bool gUnloadPXEOnExit = false;
118#endif
119static bool find_file_with_ext(const char* dir, const char *ext, const char * name_compare, size_t ext_size);
120static bool found_extra_kext(void);
121static void determineCpuArch(void);
122
123void getKernelCachePath();
124
125
126/*
127 * How long to wait (in seconds) to load the
128 * kernel after displaying the "boot:" prompt.
129 */
130#define kBootErrorTimeout 5
131
132/*
133 * Default path to kernel cache file
134 */
135#define kDefaultCachePath "/System/Library/Caches/com.apple.kext.caches/Startup/kernelcache"
136
137//==========================================================================
138// Zero the BSS.
139
140static void zeroBSS(void)
141{
142extern char _DATA__bss__begin, _DATA__bss__end;
143extern char _DATA__common__begin, _DATA__common__end;
144
145bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));
146bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));
147}
148
149//==========================================================================
150// Malloc error function
151
152#ifdef SAFE_MALLOC
153static inline void malloc_error(char *addr, size_t size, const char *file, int line)
154{
155 stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);
156}
157#else
158static inline void malloc_error(char *addr, size_t size)
159{
160 printf("\nMemory allocation error (0x%x, 0x%x)\n", (unsigned)addr, (unsigned)size);
161 asm volatile ("hlt");
162}
163#endif
164
165#if 0
166static inline void exception_error(char *msg, int nb)
167{
168 printf("\r\nException number = %d\r\nmessage = %s\r\n\r\n", nb, msg);
169 asm volatile ("hlt");
170}
171#endif
172
173//==========================================================================
174//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
175//
176void initialize_runtime(void)
177{
178zeroBSS();
179malloc_init(0, 0, 0, malloc_error);
180#if 0
181exception_init(exception_error);
182#endif
183}
184
185//==========================================================================
186// execKernel - Load the kernel image (mach-o) and jump to its entry point.
187
188static int ExecKernel(void *binary)
189{
190 entry_t kernelEntry;
191 int ret;
192
193 bootArgs->kaddr = bootArgs->ksize = 0;
194
195if(gMacOSVersion[3] <= '6')
196{
197bootArgs->Version = kBootArgsVersion1;
198bootArgs->Revision = gMacOSVersion[3];
199}
200else
201{
202#if kBootArgsVersion > 1
203
204bootArgs->Version = kBootArgsVersion;
205bootArgs->Revision = kBootArgsRevision;
206#else
207bootArgs->Version = 2;
208 bootArgs->Revision = 0;
209#endif
210}
211
212execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL, NULL, NULL);
213
214 ret = DecodeKernel(binary,
215 &kernelEntry,
216 (char **) &bootArgs->kaddr,
217 (int *)&bootArgs->ksize );
218
219 if ( ret != 0 )
220 return ret;
221
222 // Reserve space for boot args for 10.7 only (for 10.6 and earlier, we will convert (to legacy) the structure and reserve kernel memory for it later.)
223if(gMacOSVersion[3] == '7')
224 reserveKernBootStruct();
225
226 // Load boot drivers from the specifed root path.
227
228 if (!gHaveKernelCache)
229{
230LoadDrivers("/");
231 }
232
233#if TEXT_SPINNER
234clearActivityIndicator();
235#endif
236
237 if (gErrors)
238{
239 printf("Errors encountered while starting up the computer.\n");
240#if DEBUG_BOOT
241 printf("Pausing %d seconds...\n", kBootErrorTimeout);
242 sleep(kBootErrorTimeout);
243#endif
244 }
245
246execute_hook("md0Ramdisk", NULL, NULL, NULL, NULL, NULL, NULL);
247
248 setupFakeEfi();
249
250 verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");
251#ifdef NBP_SUPPORT
252 // Cleanup the PXE base code.
253
254 if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit )
255{
256if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
257 {
258 printf("nbpUnloadBaseCode error %d\n", (int) ret);
259 sleep(2);
260 }
261 }
262#endif
263{
264bool wait = false;
265const char *strval = 0;
266int dummysize /*= 0*/;
267
268getBoolForKey(kWaitForKeypressKey, &wait, &bootInfo->bootConfig);
269
270if (getValueForBootKey(bootArgs->CommandLine, "-wait", &strval, &dummysize))
271{
272wait = true;
273
274if (strval && ((strcmp(strval, "no") == 0) || (strcmp(strval, "No") == 0)))
275{
276wait = false;
277}
278}
279
280if (wait == true)
281{
282pause();
283}
284}
285
286if ((execute_hook("GUI_ExecKernel", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)) // (bootArgs->Video.v_display == VGA_TEXT_MODE)
287{
288#if UNUSED
289setVideoMode( GRAPHICS_MODE, 0 );
290#else
291setVideoMode( GRAPHICS_MODE );
292#endif
293
294#ifndef OPTION_ROM
295
296if(!gVerboseMode)
297{
298drawColorRectangle(0, 0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 0x01);
299
300uint8_t *appleBootPict;
301uint16_t bootImageWidth = kAppleBootWidth;
302uint16_t bootImageHeight = kAppleBootHeight;
303uint8_t *bootImageData = NULL;
304uint16_t x, y;
305
306unsigned long screen_params[4] = {DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 32, 0};// here we store the used screen resolution
307// Prepare the data for the default Apple boot image.
308appleBootPict = (uint8_t *) decodeRLE(gAppleBootPictRLE, kAppleBootRLEBlocks, bootImageWidth * bootImageHeight);
309if (appleBootPict)
310{
311convertImage(bootImageWidth, bootImageHeight, appleBootPict, &bootImageData);
312if (bootImageData)
313{
314x = (screen_params[0] - MIN(kAppleBootWidth, screen_params[0])) / 2;
315y = (screen_params[1] - MIN(kAppleBootHeight, screen_params[1])) / 2;
316drawDataRectangle(x, y, kAppleBootWidth, kAppleBootHeight, bootImageData);
317free(bootImageData);
318}
319free(appleBootPict);
320}
321
322}
323#endif
324}
325
326 finalizeEFIConfigTable();
327
328setupBooterLog();
329
330 finalizeBootStruct();
331
332
333if (gMacOSVersion[3] <= '6')
334reserveKernLegacyBootStruct();
335
336execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL, NULL, NULL);// Notify modules that the kernel is about to be started
337#if UNUSED
338turnOffFloppy();
339#endif
340#if BETA
341#include "smp-imps.h"
342#include "apic.h"
343IMPS_LAPIC_WRITE(LAPIC_LVT1, LAPIC_ICR_DM_NMI);
344#endif
345
346if (gMacOSVersion[3] <= '6')
347{
348// Jump to kernel's entry point. There's no going back now. XXX LEGACY OS XXX
349startprog( kernelEntry, bootArgsLegacy );
350}
351
352outb(0x21, 0xff); /* Maskout all interrupts Pic1 */
353outb(0xa1, 0xff); /* Maskout all interrupts Pic2 */
354
355// Jump to kernel's entry point. There's no going back now. XXX LION XXX
356 startprog( kernelEntry, bootArgs );
357
358 // Not reached
359
360 return 0;
361}
362
363//==========================================================================
364// This is the entrypoint from real-mode which functions exactly as it did
365// before. Multiboot does its own runtime initialization, does some of its
366// own things, and then calls common_boot.
367void boot(int biosdev)
368{
369initialize_runtime();
370// Enable A20 gate before accessing memory above 1Mb.
371enableA20();
372common_boot(biosdev);
373}
374
375//==========================================================================
376// The 'main' function for the booter. Called by boot0 when booting
377// from a block device, or by the network booter.
378//
379// arguments:
380// biosdev - Value passed from boot1/NBP to specify the device
381// that the booter was loaded from.
382//
383// If biosdev is kBIOSDevNetwork, then this function will return if
384// booting was unsuccessful. This allows the PXE firmware to try the
385// next boot device on its list.
386void common_boot(int biosdev)
387{
388 int status;
389 bool firstRun = true;
390
391 unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;
392 unsigned int denyBVFlags = kBVFlagEFISystem;
393
394#ifdef NBP_SUPPORT
395 // Set reminder to unload the PXE base code. Neglect to unload
396 // the base code will result in a hang or kernel panic.
397 gUnloadPXEOnExit = true;
398#endif
399 // Record the device that the booter was loaded from.
400 gBIOSDev = biosdev & kBIOSDevMask;
401
402
403
404 // Setup VGA text mode.
405 // Not sure if it is safe to call setVideoMode() before the
406 // config table has been loaded. Call video_mode() instead.
407#if DEBUG
408 printf("before video_mode\n");
409#endif
410 video_mode( 2 ); // 80x25 mono text mode.
411#if DEBUG
412 printf("after video_mode\n");
413#endif
414#if !TEXT_SPINNER
415printf("Starting Chameleon ...\n");
416#endif
417
418initBooterLog();
419
420// Initialize boot info structure.
421 initKernBootStruct();
422
423
424 // Scan and record the system's hardware information.
425 scan_platform();
426
427 // First get info for boot volume.
428 scanBootVolumes(gBIOSDev, 0);
429 bvChain = getBVChainForBIOSDev(gBIOSDev);
430 setBootGlobals(bvChain);
431
432 // Load Booter boot.plist config file
433 status = loadBooterConfig(&bootInfo->bootConfig);
434
435Platform->CPU.isServer = false;
436 getBoolForKey(kIsServer, &Platform->CPU.isServer, &bootInfo->bootConfig); // set this as soon as possible
437
438{
439bool quiet = false;
440if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet)
441{
442gBootMode |= kBootModeQuiet;
443}
444}
445
446{
447bool instantMenu = false;
448// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
449if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu)
450{
451firstRun = false;
452}
453#if virtualM
454 firstRun = false;
455#endif
456}
457
458#ifndef OPTION_ROM
459// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
460 if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive)
461{
462 gScanSingleDrive = true;
463 }
464
465// Create a list of partitions on device(s).
466 if (gScanSingleDrive)
467{
468scanBootVolumes(gBIOSDev, &bvCount);
469 }
470else
471#endif
472{
473#if UNUSED
474 scanDisks(gBIOSDev, &bvCount);
475#else
476 scanDisks();
477#endif
478 }
479
480// Create a separated bvr chain using the specified filters.
481 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
482gBootVolume = selectBootVolume(bvChain);
483
484{
485// Intialize module system
486EFI_STATUS sysinit = init_module_system();
487if((sysinit == EFI_SUCCESS) || (sysinit == EFI_ALREADY_STARTED) /*should never happen*/ )
488{
489load_all_modules();
490}
491}
492
493 load_all_internal_modules();
494
495 // Loading preboot ramdisk if exists.
496execute_hook("loadPrebootRAMDisk", NULL, NULL, NULL, NULL, NULL, NULL);
497
498#ifndef OPTION_ROM
499
500 // Disable rescan option by default
501 gEnableCDROMRescan = false;
502
503 // Enable it with Rescan=y in system config
504 if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan)
505{
506 gEnableCDROMRescan = true;
507 }
508
509{
510bool rescanPrompt = false;
511// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
512if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))
513{
514gEnableCDROMRescan = promptForRescanOption();
515}
516}
517
518#endif
519
520#if DEBUG
521 printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
522 printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
523 getc();
524#endif
525
526 setBootGlobals(bvChain);
527
528// Display the GUI
529execute_hook("GUI_Display", NULL, NULL, NULL, NULL, NULL, NULL);
530
531 // Parse args, load and start kernel.
532 while (1) {
533 const char *val;
534 int len;
535char *bootFile;
536bool trycache = true; // Always try to catch the kernelcache first
537
538 long flags;
539#ifdef BOOT_HELPER_SUPPORT
540long time;
541#endif
542 int ret = -1;
543 void *binary = (void *)kLoadAddr;
544
545 // additional variable for testing alternate kernel image locations on boot helper partitions.
546 char bootFileSpec[512];
547
548 // Initialize globals.
549
550 sysConfigValid = false;
551 gErrors = false;
552
553 status = getBootOptions(firstRun);
554 firstRun = false;
555 if (status == -1) continue;
556
557 status = processBootOptions();
558 // Status==1 means to chainboot
559 if ( status == 1 ) break;
560 // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL
561 if ( status == -1 )
562 {
563// gBootVolume == NULL usually means the user hit escape.
564if(gBootVolume == NULL)
565{
566freeFilteredBVChain(bvChain);
567#ifndef OPTION_ROM
568if (gEnableCDROMRescan)
569rescanBIOSDevice(gBIOSDev);
570#endif
571
572bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
573setBootGlobals(bvChain);
574}
575continue;
576 }
577
578 // Other status (e.g. 0) means that we should proceed with boot.
579execute_hook("GUI_PreBoot", NULL, NULL, NULL, NULL, NULL, NULL);
580
581// Find out which version mac os we're booting.
582getOSVersion(gMacOSVersion);
583
584if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig) && val)
585{
586if (strncmp(val, "x86_64", 4) == 0)
587{
588archCpuType = CPU_TYPE_X86_64;
589}
590else if (strncmp(val, "i386", 4) == 0)
591{
592archCpuType = CPU_TYPE_I386;
593}
594else
595{
596DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");
597determineCpuArch();
598}
599
600}
601else determineCpuArch();
602
603
604getRootDevice();
605
606// Notify to all modules that we are attempting to boot
607execute_hook("PreBoot", NULL, NULL, NULL, NULL, NULL, NULL);
608
609if (execute_hook("getProductNamePatched", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)
610readSMBIOS(thePlatformName); // read smbios Platform Name
611
612
613if (((gBootMode & kBootModeSafe) == 0) &&
614!gOverrideKernel &&
615(gBootFileType == kBlockDeviceType) &&
616(gMKextName[0] == '\0') &&
617!getValueForBootKey(bootArgs->CommandLine, kIgnorePrelinkKern, &val, &len))
618{
619getBoolForKey(kUseKernelCache, &trycache, &bootInfo->bootConfig);
620if (trycache == true)
621{
622// try to find the cache and fill the gBootKernelCacheFile string
623getKernelCachePath();
624
625// Check for cache file
626trycache = (gBootKernelCacheFile[0] != '\0') ? true : false; // if gBootKernelCacheFile is filled et bla bla bla.... :-)
627}
628
629}
630else
631{
632trycache = false;
633}
634
635verbose("Loading Darwin %s\n", gMacOSVersion);
636{
637long cachetime, kerneltime, exttime;
638if (trycache && !forcecache) do {
639
640// if we haven't found the kernel yet, don't use the cache
641ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
642if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
643{
644trycache = 0;
645bootInfo->adler32 = 0;
646DBG("No kernel found, kernelcache disabled !!!\n");
647break;
648}
649ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
650if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
651|| (cachetime < kerneltime))
652{
653trycache = 0;
654bootInfo->adler32 = 0;
655DBG("Warning: No kernelcache found or kernelcache too old (timestamp of the kernel > timestamp of the cache), kernelcache disabled !!!\n");
656
657break;
658}
659ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
660if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
661&& (cachetime < exttime))
662{
663trycache = 0;
664bootInfo->adler32 = 0;
665DBG("Warning: kernelcache too old, timestamp of S/L/E > timestamp of the cache, kernelcache disabled !!! \n");
666
667break;
668}
669if (kerneltime > exttime)
670{
671exttime = kerneltime;
672}
673if (cachetime != (exttime + 1))
674{
675trycache = 0;
676bootInfo->adler32 = 0;
677DBG("Warning: invalid timestamp, kernelcache disabled !!!\n");
678
679break;
680}
681} while (0);
682}
683
684 do {
685 if (trycache)
686{
687 bootFile = gBootKernelCacheFile;
688 verbose("Loading kernel cache %s\n", bootFile);
689if (gMacOSVersion[3] == '7')
690{
691ret = LoadThinFatFile(bootFile, &binary);
692if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
693{
694archCpuType = CPU_TYPE_I386;
695ret = LoadThinFatFile(bootFile, &binary);
696}
697}
698else
699{
700ret = LoadFile(bootFile);
701binary = (void *)kLoadAddr;
702}
703
704 if (ret >= 0)
705{
706 break;
707 }
708
709 }
710bootInfo->adler32 = 0;
711 bootFile = bootInfo->bootFile;
712#ifdef BOOT_HELPER_SUPPORT
713
714 // Try to load kernel image from alternate locations on boot helper partitions.
715 sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);
716 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
717 if (ret == -1)
718 {
719sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);
720ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
721if (ret == -1)
722{
723sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);
724ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
725if (ret == -1)
726{
727// Not found any alternate locations, using the original kernel image path.
728strlcpy(bootFileSpec, bootFile,sizeof(bootFileSpec)+1);
729}
730}
731 }
732#else
733strlcpy(bootFileSpec, bootFile,sizeof(bootFileSpec)+1);
734#endif
735
736 verbose("Loading kernel %s\n", bootFileSpec);
737 ret = LoadThinFatFile(bootFileSpec, &binary);
738 if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
739 {
740archCpuType = CPU_TYPE_I386;
741ret = LoadThinFatFile(bootFileSpec, &binary);
742 }
743
744 } while (0);
745#if TEXT_SPINNER
746 clearActivityIndicator();
747#endif
748
749#if DEBUG
750 printf("Pausing...");
751 sleep(8);
752#endif
753
754 if (ret <= 0)
755{
756printf("Can't find %s\n", bootFile);
757
758sleep(1);
759#ifdef NBP_SUPPORT
760 if (gBootFileType == kNetworkDeviceType)
761{
762 // Return control back to PXE. Don't unload PXE base code.
763 gUnloadPXEOnExit = false;
764 break;
765 }
766#endif
767 }
768else
769{
770 /* Won't return if successful. */
771 ret = ExecKernel(binary);
772 }
773 }
774
775 // chainboot
776 if (status==1)
777{
778if (getVideoMode() == GRAPHICS_MODE)
779{// if we are already in graphics-mode,
780#if UNUSED
781setVideoMode(VGA_TEXT_MODE, 0);// switch back to text mode
782#else
783setVideoMode(VGA_TEXT_MODE);// switch back to text mode
784#endif
785}
786 }
787#ifdef NBP_SUPPORT
788 if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
789{
790nbpUnloadBaseCode();
791 }
792#endif
793}
794
795static void determineCpuArch(void)
796{
797if (cpu_mode_is64bit())
798{
799archCpuType = CPU_TYPE_X86_64;
800}
801else
802{
803archCpuType = CPU_TYPE_I386;
804}
805}
806
807void getKernelCachePath()
808{
809{
810// If there is an extra kext/mkext, we return immediatly and we skip the kernelCache
811// since kexts/mkexts are not loaded properly when the kernelCache is used.
812// Another method would be to re-build the kernelCache one the fly
813if (found_extra_kext() == true) return;
814}
815
816{
817const char *val;
818int len;
819
820if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))
821{
822 char * buffer = newString(val);
823
824if (val[0] == '\\')
825{
826// Flip the back slash's to slash's .
827 len = 0;
828 while (buffer[len] != '\0') {
829 if (buffer[len] == '\\')
830 {
831 buffer[len] = '/';
832 }
833 len++;
834 }
835}
836strlcpy(gBootKernelCacheFile, buffer, sizeof(gBootKernelCacheFile)+1);
837 forcecache = true;
838}
839else
840{
841if(gMacOSVersion[3] == '7')
842{
843sprintf(gBootKernelCacheFile, "%s", kDefaultCachePath);
844}
845else if(gMacOSVersion[3] <= '6')
846{
847
848PlatformInfo *platformInfo = malloc(sizeof(PlatformInfo));
849if (platformInfo)
850{
851
852bzero(platformInfo, sizeof(PlatformInfo));
853
854if (gPlatformName)
855strlcpy(platformInfo->platformName,gPlatformName, sizeof(platformInfo->platformName)+1);
856
857if (gRootDevice)
858{
859char *rootPath_p = platformInfo->rootPath;
860len = strlen(gRootDevice) + 1;
861if ((unsigned)len > sizeof(platformInfo->rootPath))
862{
863len = sizeof(platformInfo->rootPath);
864}
865memcpy(rootPath_p, gRootDevice,len);
866
867rootPath_p += len;
868
869len = strlen(bootInfo->bootFile);
870
871if ((unsigned)(rootPath_p - platformInfo->rootPath + len) >=
872sizeof(platformInfo->rootPath))
873{
874
875len = sizeof(platformInfo->rootPath) -
876(rootPath_p - platformInfo->rootPath);
877}
878memcpy(rootPath_p, bootInfo->bootFile, len);
879
880}
881
882if (!platformInfo->platformName[0] || !platformInfo->rootPath[0])
883{
884platformInfo->platformName[0] = platformInfo->rootPath[0] = 0;
885}
886//memcpy(gRootPath,platformInfo->rootPath, sizeof(gRootPath));
887
888
889bootInfo->adler32 = OSSwapHostToBigInt32(adler32((unsigned char *)platformInfo, sizeof(*platformInfo)));
890
891free(platformInfo);
892}
893
894DBG("Adler32: %08lX\n",bootInfo->adler32);
895
896if (gMacOSVersion[3] < '6')
897{
898long flags, cachetime;
899int ret = -1;
900sprintf(gBootKernelCacheFile, "%s.%08lX", "/System/Library/Caches/com.apple.kernelcaches/kernelcache",bootInfo->adler32);
901ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
902if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
903{
904bootInfo->adler32 = 0;
905sprintf(gBootKernelCacheFile, "%s", "/System/Library/Caches/com.apple.kernelcaches/kernelcache");
906}
907
908} else
909sprintf(gBootKernelCacheFile, "%s_%s.%08lX", kDefaultCachePath, (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64", bootInfo->adler32); //Snow Leopard
910
911}
912}
913}
914}
915
916
917static void getRootDevice()
918{
919// Maximum config table value size
920#define VALUE_SIZE 2048
921bool uuidSet = false;
922const char *val = 0;
923 int cnt = 0;
924
925if (getValueForKey(kBootUUIDKey, &val, &cnt, &bootInfo->bootConfig))
926{
927uuidSet = true;
928}
929else
930{
931if (getValueForBootKey(bootArgs->CommandLine, kRootDeviceKey, &val, &cnt))
932{
933if (*val == '*' && *(val + 1) != '/' && *(val + 1) != 'u')
934{
935val += 1; //skip the *
936uuidSet = true;
937
938}
939else if (*val == '*' && *(val + 1) == 'u')
940{
941
942if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig))
943uuidSet = true;
944
945}
946}
947else
948{
949#ifdef BOOT_HELPER_SUPPORT
950//
951// Try an alternate method for getting the root UUID on boot helper partitions.
952//
953if (gBootVolume->flags & kBVFlagBooter)
954{
955if((loadHelperConfig(&bootInfo->helperConfig) == 0)
956 && getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig) )
957{
958getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig);
959uuidSet = true;
960goto out;
961}
962}
963#endif
964if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig))
965{
966extern int ArgCntRemaining;
967uuidSet = false;
968char * valueBuffer;
969valueBuffer = malloc(VALUE_SIZE);
970char * argP = bootArgs->CommandLine;
971valueBuffer[0] = '*';
972if (cnt > VALUE_SIZE)
973{
974cnt = VALUE_SIZE;
975}
976//cnt++;
977strlcpy(valueBuffer + 1, val, cnt+1);
978if (!copyArgument( kRootDeviceKey, valueBuffer, cnt, &argP, &ArgCntRemaining))
979{
980free(valueBuffer);
981printf("Error: boot arguments too long, unable to set root device !!");
982getc();
983return;
984}
985free(valueBuffer);
986goto out;
987}
988
989if (gBootVolume->fs_getuuid && gBootVolume->fs_getuuid (gBootVolume, bootInfo->uuidStr) == 0)
990{
991verbose("Setting boot-uuid to: %s\n", bootInfo->uuidStr);
992uuidSet = true;
993gRootDevice = bootInfo->uuidStr;
994return;
995}
996
997}
998}
999
1000out:
1001verbose("Setting %s to: %s\n", uuidSet ? kBootUUIDKey : "root device", (char* )val);
1002gRootDevice = (char* )val;
1003}
1004
1005static bool getOSVersion(char *str)
1006{
1007bool valid = false;
1008config_file_t systemVersion;
1009
1010if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))
1011{
1012valid = true;
1013}
1014else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))
1015{
1016valid = true;
1017}
1018
1019if (valid)
1020{
1021const char *val;
1022int len;
1023
1024if (getValueForKey(kProductVersion, &val, &len, &systemVersion))
1025{
1026// getValueForKey uses const char for val
1027// so copy it and trim
1028*str = '\0';
1029strncat(str, val, MIN(len, 4));
1030}
1031else
1032valid = false;
1033}
1034
1035return valid;
1036}
1037
1038static bool find_file_with_ext(const char* dir, const char *ext, const char * name_compare, size_t ext_size)
1039{
1040char* name;
1041long flags;
1042long time;
1043struct dirstuff* moduleDir = opendir(dir);
1044while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)
1045{
1046int len = strlen(name);
1047
1048if (len >= ext_size)
1049{
1050if(strcmp(&name[len - ext_size], ext) == 0)
1051{
1052if (name_compare)
1053{
1054if (strcmp(name, name_compare) == 0)
1055{
1056DBG("found : %s\n", name);
1057return true;
1058}
1059}
1060else
1061{
1062DBG("found : %s\n", name);
1063return true;
1064}
1065}
1066#if DEBUG_BOOT
1067else
1068{
1069DBG("Ignoring %s\n", name);
1070}
1071#endif
1072}
1073#if DEBUG_BOOT
1074else
1075{
1076DBG("Ignoring %s\n", name);
1077}
1078#endif
1079}
1080
1081return false;
1082}
1083
1084// If a kext is found in /Extra/Extentions return true
1085// If a mkext is found in /Extra return true
1086// Otherwise return false
1087// Tips (if you still use extra kext(s)/mkext(s) ): With Lion it's recommended to create a system mkext (see the kextcache commandline) to decrease boot time,
1088// otherwise it will act like a -f each time a extra kext/kext is detected
1089static bool found_extra_kext(void)
1090{
1091#define EXTENSIONS "Extensions"
1092#define MKEXT_EXT ".mkext"
1093#define MKEXT_EXT_SIZE sizeof("mkext")
1094#define KEXT_EXT ".kext"
1095#define KEXT_EXT_SIZE sizeof("kext")
1096
1097long flags;
1098long exttime;
1099int ret = -1;
1100
1101ret = GetFileInfo("rd(0,0)/Extra/", EXTENSIONS, &flags, &exttime);
1102if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))
1103{
1104if (((flags & kFileTypeMask) == kFileTypeFlat))
1105{
1106if (find_file_with_ext("rd(0,0)/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))
1107{
1108return true;
1109}
1110}
1111else if (((flags & kFileTypeMask) == kFileTypeDirectory))
1112{
1113if (find_file_with_ext("rd(0,0)/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))
1114{
1115return true;
1116}
1117}
1118}
1119ret = GetFileInfo("/Extra/", EXTENSIONS, &flags, &exttime);
1120if (ret == 0)
1121{
1122if (((flags & kFileTypeMask) == kFileTypeFlat))
1123{
1124if (find_file_with_ext("/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))
1125{
1126return true;
1127}
1128}
1129else if (((flags & kFileTypeMask) == kFileTypeDirectory))
1130{
1131if (find_file_with_ext("/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))
1132{
1133return true;
1134}
1135}
1136}
1137ret = GetFileInfo("bt(0,0)/Extra/", EXTENSIONS, &flags, &exttime);
1138if (ret == 0)
1139{
1140if (((flags & kFileTypeMask) == kFileTypeFlat))
1141{
1142if (find_file_with_ext("bt(0,0)/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))
1143{
1144return true;
1145}
1146}
1147else if (((flags & kFileTypeMask) == kFileTypeDirectory))
1148{
1149if (find_file_with_ext("bt(0,0)/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))
1150{
1151return true;
1152}
1153}
1154}
1155DBG("NO Extra Mkext/Kext found\n");
1156
1157// nothing found
1158return false;
1159}
1160
1161#if 0
1162static char *FIXED_BOOTFILE_PATH(char * str)
1163{
1164char bootfile[128];
1165
1166bool bootFileWithDevice = false;
1167// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
1168if (strncmp(str,"bt(",3) == 0 ||
1169strncmp(str,"hd(",3) == 0 ||
1170strncmp(str,"rd(",3) == 0)
1171{
1172bootFileWithDevice = true;
1173}
1174
1175// bootFile must start with a / if it not start with a device name
1176if (!bootFileWithDevice && (str)[0] != '/')
1177sprintf(bootFile, "/%s", str); // append a leading /
1178else
1179strlcpy(bootFile, bootInfo->bootFile, sizeof(bootInfo->bootFile)+1);
1180
1181return bootfile;
1182}
1183#endif
1184

Archive Download this file

Revision: 1704