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

Archive Download this file

Revision: 1599