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

Archive Download this file

Revision: 1724