Chameleon

Chameleon Svn Source Tree

Root/branches/Bungo/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/*
26 * Mach Operating System
27 * Copyright (c) 1990 Carnegie-Mellon University
28 * Copyright (c) 1989 Carnegie-Mellon University
29 * All rights reserved. The CMU software License Agreement specifies
30 * the terms and conditions for use and redistribution.
31 *
32 * INTEL CORPORATION PROPRIETARY INFORMATION
33 *
34 * This software is supplied under the terms of a license agreement or
35 * nondisclosure agreement with Intel Corporation and may not be copied
36 * nor disclosed except in accordance with the terms of that agreement.
37 *
38 *Copyright 1988, 1989 by Intel Corporation
39 *
40 *
41 * Copyright 1993 NeXT Computer, Inc.
42 * All rights reserved.
43 *
44 * Completely reworked by Sam Streeper (sam_s@NeXT.com)
45 * Reworked again by Curtis Galloway (galloway@NeXT.com)
46 */
47
48#include "boot.h"
49#include "bootstruct.h"
50#include "fake_efi.h"
51#include "sl.h"
52#include "libsa.h"
53#include "ramdisk.h"
54#include "gui.h"
55#include "platform.h"
56#include "modules.h"
57#include "device_tree.h"
58
59#ifndef DEBUG_BOOT2
60#define DEBUG_BOOT2 0
61#endif
62
63#if DEBUG_BOOT2
64#define DBG(x...)printf(x)
65#else
66#define DBG(x...)msglog(x)
67#endif
68
69/*
70 * How long to wait (in seconds) to load the
71 * kernel after displaying the "boot:" prompt.
72 */
73#define kBootErrorTimeout 5
74
75boolgOverrideKernel, gEnableCDROMRescan, gScanSingleDrive, useGUI;
76static boolgUnloadPXEOnExit = false;
77
78static chargCacheNameAdler[64 + 256];
79char*gPlatformName = gCacheNameAdler;
80
81chargRootDevice[ROOT_DEVICE_SIZE];
82chargMKextName[512];
83int bvCount = 0, gDeviceCount = 0;
84//intmenucount = 0;
85longgBootMode; /* defaults to 0 == kBootModeNormal */
86BVRefbvr, menuBVR, bvChain;
87
88static unsigned longAdler32(unsigned char *buffer, long length);
89//static voidselectBiosDevice(void);
90
91/** options.c **/
92extern char* msgbuf;
93void showTextBuffer(char *buf, int size);
94
95//==========================================================================
96// Zero the BSS.
97
98static void zeroBSS(void)
99{
100extern char bss_start __asm("section$start$__DATA$__bss");
101extern char bss_end __asm("section$end$__DATA$__bss");
102extern char common_start __asm("section$start$__DATA$__common");
103extern char common_end __asm("section$end$__DATA$__common");
104
105bzero(&bss_start, (&bss_end - &bss_start));
106bzero(&common_start, (&common_end - &common_start));
107}
108
109//==========================================================================
110// Malloc error function
111
112static void malloc_error(char *addr, size_t size, const char *file, int line)
113{
114stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",
115 (unsigned)addr, (unsigned)size, file, line);
116}
117
118//==========================================================================
119//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
120//
121void initialize_runtime(void)
122{
123zeroBSS();
124malloc_init(0, 0, 0, malloc_error);
125}
126
127//==========================================================================
128// ExecKernel - Load the kernel image (mach-o) and jump to its entry point.
129
130static int ExecKernel(void *binary)
131{
132intret;
133entry_tkernelEntry;
134
135bootArgs->kaddr = bootArgs->ksize = 0;
136execute_hook("ExecKernel", (void *)binary, NULL, NULL, NULL);
137
138ret = DecodeKernel(binary,
139 &kernelEntry,
140 (char **) &bootArgs->kaddr,
141 (int *)&bootArgs->ksize);
142
143if ( ret != 0 )
144{
145return ret;
146}
147
148// Reserve space for boot args
149reserveKernBootStruct();
150
151// Notify modules that the kernel has been decoded
152execute_hook("DecodedKernel", (void *)binary, (void *)bootArgs->kaddr, (void *)bootArgs->ksize, NULL);
153
154setupFakeEfi();
155
156// Load boot drivers from the specifed root path.
157//if (!gHaveKernelCache)
158{
159LoadDrivers("/");
160}
161
162execute_hook("DriversLoaded", (void *)binary, NULL, NULL, NULL);
163
164clearActivityIndicator();
165
166if (gErrors)
167{
168printf("Errors encountered while starting up the computer.\n");
169printf("Pausing %d seconds...\n", kBootErrorTimeout);
170sleep(kBootErrorTimeout);
171}
172
173md0Ramdisk();
174
175// Cleanup the PXE base code.
176
177if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit )
178{
179if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
180{
181printf("nbpUnloadBaseCode error %d\n", (int)ret);
182sleep(2);
183}
184}
185
186bool dummyVal;
187if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal)
188{
189showTextBuffer(msgbuf, strlen(msgbuf));
190}
191
192usb_loop();
193
194// If we were in text mode, switch to graphics mode.
195// This will draw the boot graphics unless we are in
196// verbose mode.
197if (gVerboseMode)
198{
199setVideoMode(GRAPHICS_MODE, 0);
200}
201else
202{
203drawBootGraphics();
204}
205
206 getCurrentEDID();
207
208 verbose("GUI Graphics: %dx%dx%d.\n", gui.screen.width, gui.screen.height, bootArgs->Video.v_depth);
209 verbose("Boot Graphics: %dx%dx%d, mode=0x%04X.\n", bootArgs->Video.v_width, bootArgs->Video.v_height, bootArgs->Video.v_depth, getCurrentGraphicsMode());
210
211verbose("Starting Mac OS X %s [0x%08X], %s\n", gBootVolume->OSVersion, MacOSVerCurrent, gDarwinBuildVerStr);
212verbose("Boot Args: %s\n", bootArgs->CommandLine);
213
214setupBooterLog();
215
216finalizeBootStruct();
217
218// Jump to kernel's entry point. There's no going back now.
219 if (MacOSVerCurrent >= MacOSVer2Int("10.7"))
220//if (!((checkOSVersion("10.4")) || (checkOSVersion("10.5")) || (checkOSVersion("10.6"))) )
221{
222// Notify modules that the kernel is about to be started
223execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgs, NULL, NULL);
224
225// Masking out so that Lion doesn't doublefault
226outb(0x21, 0xff);/* Maskout all interrupts Pic1 */
227outb(0xa1, 0xff);/* Maskout all interrupts Pic2 */
228
229 if (gVerboseMode) pause("");
230startprog( kernelEntry, bootArgs );
231} else {
232// Notify modules that the kernel is about to be started
233execute_hook("Kernel Start", (void *)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);
234
235 if (gVerboseMode) pause("");
236startprog( kernelEntry, bootArgsPreLion );
237}
238
239// Not reached
240return 0;
241}
242
243
244//==========================================================================
245// LoadKernelCache - Try to load Kernel Cache.
246// return the length of the loaded cache file or -1 on error
247long LoadKernelCache(const char* cacheFile, void **binary)
248{
249charkernelCacheFile[512];
250charkernelCachePath[512];
251longflags, ret=-1;
252unsigned long adler32;
253u_int32_t time, cachetime, kerneltime, exttime;
254
255if ((gBootMode & kBootModeSafe) != 0) {
256verbose("Kernel Cache ignored.\n");
257return -1;
258}
259
260// Use specify kernel cache file if not empty
261if (cacheFile[0] != 0)
262{
263strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));
264verbose("Specified kernel cache file path: %s\n", cacheFile);
265}
266else
267{
268// Lion, Mountain Lion Mavericks and Yosemite prelink kernel cache file
269if (MacOSVerCurrent >= MacOSVer2Int("10.7")) // OSX is Lion (10.7) or newer
270{
271snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);
272verbose("Kernel Cache file path (Mac OS X 10.7 and newer): %s\n", kernelCacheFile);
273}
274// Snow Leopard prelink kernel cache file
275else
276 {
277 if (MacOSVerCurrent >= MacOSVer2Int("10.6")) // OSX is Snow (10.6)
278 {
279 snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",
280 (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");
281
282 int lnam = strlen(kernelCacheFile) + 9; //with adler32
283 char* name;
284u_int32_t prev_time = 0;
285
286 struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
287
288 /* TODO: handle error? */
289 if (cacheDir)
290 {
291 while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
292 {
293 if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
294 && strstr(name, kernelCacheFile) && (name[lnam] != '.'))
295 {
296 snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);
297 prev_time = time;
298 }
299 }
300 verbose("Kernel Cache file path (Mac OS X 10.6.X): %s\n", kernelCacheFile);
301 }
302 closedir(cacheDir);
303 }
304 else
305 {
306 // Reset cache name.
307 bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
308 snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64, "%s,%s", gRootDevice, bootInfo->bootFile);
309 adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
310 snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);
311 verbose("Reseted kernel cache file path: %s\n", kernelCacheFile);
312 }
313}
314}
315
316// Check if the kernel cache file exists
317ret = -1;
318
319// If boot from a boot helper partition check the kernel cache file on it
320if (gBootVolume->flags & kBVFlagBooter)
321{
322snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);
323ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
324
325if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
326{
327snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);
328ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
329
330if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
331{
332snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);
333ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
334
335if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
336{
337snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.recovery.boot/kernelcache", kernelCacheFile);
338ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
339
340if ((flags & kFileTypeMask) != kFileTypeFlat)
341{
342ret = -1;
343}
344}
345}
346}
347}
348
349// If not found, use the original kernel cache path.
350if (ret == -1)
351{
352strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));
353ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
354
355if ((flags & kFileTypeMask) != kFileTypeFlat)
356{
357ret = -1;
358}
359}
360
361// Exit if kernel cache file wasn't found
362if (ret == -1) {
363verbose("No Kernel Cache File '%s' found\n", kernelCacheFile);
364return -1;
365}
366
367// Check if the kernel cache file is more recent (mtime)
368// than the kernel file or the S/L/E directory
369ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
370
371// Check if the kernel file is more recent than the cache file
372if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat)
373&& (kerneltime > cachetime)) {
374verbose("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n",
375bootInfo->bootFile, kernelCacheFile);
376return -1;
377}
378
379ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
380
381// Check if the S/L/E directory time is more recent than the cache file
382if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
383&& (exttime > cachetime)) {
384verbose("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n",
385kernelCacheFile);
386return -1;
387}
388
389// Since the kernel cache file exists and is the most recent try to load it
390verbose("Loading Kernel Cache from: '%s%s' (%s)\n", gBootVolume->label, gBootVolume->altlabel, gBootVolume->type_name);
391
392ret = LoadThinFatFile(kernelCachePath, binary);
393 if (gVerboseMode) pause("");
394return ret; // ret contain the length of the binary
395}
396
397//==========================================================================
398// This is the entrypoint from real-mode which functions exactly as it did
399// before. Multiboot does its own runtime initialization, does some of its
400// own things, and then calls common_boot.
401void boot(int biosdev)
402{
403initialize_runtime();
404// Enable A20 gate before accessing memory above 1Mb.
405enableA20();
406common_boot(biosdev);
407}
408
409//==========================================================================
410// The 'main' function for the booter. Called by boot0 when booting
411// from a block device, or by the network booter.
412//
413// arguments:
414// biosdev - Value passed from boot1/NBP to specify the device
415// that the booter was loaded from.
416//
417// If biosdev is kBIOSDevNetwork, then this function will return if
418// booting was unsuccessful. This allows the PXE firmware to try the
419// next boot device on its list.
420void common_boot(int biosdev)
421{
422bool quiet;
423bool firstRun = true;
424bool instantMenu;
425bool rescanPrompt;
426intstatus;
427unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
428unsigned intdenyBVFlags = kBVFlagEFISystem;
429
430// Set reminder to unload the PXE base code. Neglect to unload
431// the base code will result in a hang or kernel panic.
432gUnloadPXEOnExit = true;
433
434// Record the device that the booter was loaded from.
435gBIOSDev = biosdev & kBIOSDevMask;
436
437// Initialize boot-log
438initBooterLog();
439
440// Initialize boot info structure.
441initKernBootStruct();
442
443// Setup VGA text mode.
444// Not sure if it is safe to call setVideoMode() before the
445// config table has been loaded. Call video_mode() instead.
446#if DEBUG
447printf("before video_mode\n");
448#endif
449video_mode( 2 ); // 80x25 mono text mode.
450#if DEBUG
451printf("after video_mode\n");
452#endif
453
454// Scan and record the system's hardware information.
455scan_platform();
456
457// First get info for boot volume.
458scanBootVolumes(gBIOSDev, 0);
459bvChain = getBVChainForBIOSDev(gBIOSDev);
460setBootGlobals(bvChain);
461
462// Load boot.plist config file
463status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);
464
465if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)
466{
467gBootMode |= kBootModeQuiet;
468}
469
470// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
471if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)
472{
473firstRun = false;
474}
475
476// Loading preboot ramdisk if exists.
477loadPrebootRAMDisk();
478
479// Disable rescan option by default
480gEnableCDROMRescan = false;
481
482// Enable it with Rescan=y in system config
483if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)&& gEnableCDROMRescan)
484{
485gEnableCDROMRescan = true;
486}
487
488 // Disable rescanPrompt option by default
489 rescanPrompt = false;
490
491// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
492if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig)
493&& rescanPrompt && biosDevIsCDROM(gBIOSDev))
494{
495gEnableCDROMRescan = promptForRescanOption();
496}
497
498 // Disable ScanSingleDrive option by default
499 gScanSingleDrive = false;
500
501// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
502if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)
503{
504gScanSingleDrive = true;
505}
506
507// Create a list of partitions on device(s).
508if (gScanSingleDrive)
509{
510scanBootVolumes(gBIOSDev, &bvCount);
511}
512else
513{
514scanDisks(gBIOSDev, &bvCount);
515}
516
517// Create a separated bvr chain using the specified filters.
518bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
519
520gBootVolume = selectBootVolume(bvChain);
521
522// Intialize module system
523init_module_system();
524
525#if DEBUG
526 if (gBootVolume) {
527 verbose("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
528 gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
529 }
530 else {
531 verbose("Boot Volume not selected yet.\n");
532 }
533verbose("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
534 gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
535pause("");
536#endif
537
538useGUI = true;
539// Override useGUI default
540getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
541if (useGUI && initGUI())
542{
543// initGUI() returned with an error, disabling GUI.
544useGUI = false;
545}
546
547setBootGlobals(bvChain);
548
549// Parse args, load and start kernel.
550while (1)
551{
552booltryresume, tryresumedefault, forceresume;
553booluseKernelCache = true; // by default try to use the prelinked kernel
554const char*val;
555intlen, ret = -1;
556longflags;
557u_int32_tsleeptime, time;
558void*binary = (void *)kLoadAddr;
559
560char bootFile[sizeof(bootInfo->bootFile)];
561charbootFilePath[512];
562charkernelCacheFile[512];
563
564// Initialize globals.
565sysConfigValid = false;
566gErrors = false;
567
568status = getBootOptions(firstRun);
569firstRun = false;
570if (status == -1) continue;
571
572status = processBootOptions();
573// Status == 1 means to chainboot
574if ( status ==1 ) break;
575// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
576if ( status == -1 )
577{
578// gBootVolume == NULL usually means the user hit escape.
579if (gBootVolume == NULL)
580{
581freeFilteredBVChain(bvChain);
582
583if (gEnableCDROMRescan)
584rescanBIOSDevice(gBIOSDev);
585
586bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
587setBootGlobals(bvChain);
588setupDeviceList(&bootInfo->themeConfig);
589}
590continue;
591}
592
593// Other status (e.g. 0) means that we should proceed with boot.
594
595// Turn off any GUI elements
596if ( bootArgs->Video.v_display == GRAPHICS_MODE )
597{
598gui.devicelist.draw = false;
599gui.bootprompt.draw = false;
600gui.menu.draw = false;
601gui.infobox.draw = false;
602gui.logo.draw = false;
603drawBackground();
604updateVRAM();
605}
606
607if (platformCPUFeature(CPU_FEATURE_EM64T))
608{
609archCpuType = CPU_TYPE_X86_64;
610}
611else
612{
613archCpuType = CPU_TYPE_I386;
614}
615
616if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig))
617{
618if (strncmp(val, "i386", 4) == 0)
619{
620archCpuType = CPU_TYPE_I386;
621}
622}
623
624if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig)) {
625if (strncmp(val, "i386", 4) == 0)
626{
627archCpuType = CPU_TYPE_I386;
628}
629}
630
631// Notify modules that we are attempting to boot
632execute_hook("PreBoot", NULL, NULL, NULL, NULL);
633
634if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig))
635{
636tryresume = true;
637tryresumedefault = true;
638}
639else
640{
641tryresumedefault = false;
642}
643
644if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig)) {
645forceresume = false;
646}
647
648if (forceresume)
649{
650tryresume = true;
651tryresumedefault = false;
652}
653
654while (tryresume)
655{
656const char *tmp;
657BVRef bvr;
658if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
659val = "/private/var/vm/sleepimage";
660
661// Do this first to be sure that root volume is mounted
662ret = GetFileInfo(0, val, &flags, &sleeptime);
663
664if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
665break;
666
667// Can't check if it was hibernation Wake=y is required
668if (bvr->modTime == 0 && tryresumedefault)
669break;
670
671if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
672break;
673
674if (!forceresume && ((sleeptime+3)<bvr->modTime))
675{
676#if DEBUG
677printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
678bvr->modTime-sleeptime);
679#endif
680break;
681}
682
683HibernateBoot((char *)val);
684break;
685}
686
687getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
688if (useKernelCache) {
689 do {
690 // Determine the name of the Kernel Cache
691 if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {
692 if (val[0] == '\\')
693 {
694 len--;
695 val++;
696 }
697 /* FIXME: check len vs sizeof(kernelCacheFile) */
698 strlcpy(kernelCacheFile, val, len + 1);
699}
700else
701{
702 kernelCacheFile[0] = 0; // Use default kernel cache file
703 }
704
705 if (gOverrideKernel && kernelCacheFile[0] == 0) {
706 verbose("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);
707 useKernelCache = false;
708 break;
709 }
710 if (gMKextName[0] != 0) {
711 verbose("Using a specific MKext Cache (%s), KernelCache will not be used\n",
712 gMKextName);
713 useKernelCache = false;
714 break;
715 }
716 if (gBootFileType != kBlockDeviceType)
717 useKernelCache = false;
718
719 } while(0);
720 } else {
721 verbose("Kernel Cache using disabled by user.");
722 }
723
724do {
725if (useKernelCache)
726 {
727ret = LoadKernelCache(kernelCacheFile, &binary);
728if (ret >= 0)
729{
730 break;
731}
732}
733
734bool bootFileWithDevice = false;
735// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
736if (strncmp(bootInfo->bootFile,"bt(",3) == 0 ||
737strncmp(bootInfo->bootFile,"hd(",3) == 0 ||
738strncmp(bootInfo->bootFile,"rd(",3) == 0)
739bootFileWithDevice = true;
740
741// bootFile must start with a / if it not start with a device name
742if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')
743{
744if (MacOSVerCurrent >= MacOSVer2Int("10.10")) // OSX is 10.10 or newer
745snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathForYos "%s", bootInfo->bootFile); // Yosemite: '/System/Library/Kernels/'
746 else
747snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathPreYos "%s", bootInfo->bootFile); // append a leading '/'
748}
749 else
750 {
751strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));
752}
753
754// Try to load kernel image from alternate locations on boot helper partitions.
755ret = -1;
756if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice)
757 {
758snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);
759ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
760if (ret == -1)
761{
762snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);
763ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
764if (ret == -1)
765{
766snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);
767ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
768}
769}
770}
771if (ret == -1)
772 {
773// No alternate location found, using the original kernel image path.
774strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));
775}
776
777verbose("Loading kernel from: '%s' (%s)\n", gBootVolume->label, gBootVolume->type_name);
778ret = LoadThinFatFile(bootFilePath, &binary);
779if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
780{
781archCpuType = CPU_TYPE_I386;
782ret = LoadThinFatFile(bootFilePath, &binary);
783}
784} while (0);
785
786clearActivityIndicator();
787
788#if DEBUG
789printf("Pausing...");
790sleep(8);
791#endif
792
793if (ret <= 0) {
794error("Can't find boot file: '%s'\n", bootFile);
795sleep(1);
796
797if (gBootFileType == kNetworkDeviceType)
798{
799// Return control back to PXE. Don't unload PXE base code.
800gUnloadPXEOnExit = false;
801break;
802}
803pause("");
804
805}
806else
807{
808/* Won't return if successful. */
809ret = ExecKernel(binary);
810}
811}
812
813// chainboot
814if (status == 1)
815{
816// if we are already in graphics-mode,
817if (getVideoMode() == GRAPHICS_MODE)
818{
819setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode.
820}
821}
822
823if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
824{
825nbpUnloadBaseCode();
826}
827}
828
829/*!
830Selects a new BIOS device, taking care to update the global state appropriately.
831 */
832/*
833static void selectBiosDevice(void)
834{
835struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
836CacheReset();
837diskFreeMap(oldMap);
838oldMap = NULL;
839
840int dev = selectAlternateBootDevice(gBIOSDev);
841
842BVRef bvchain = scanBootVolumes(dev, 0);
843BVRef bootVol = selectBootVolume(bvchain);
844gBootVolume = bootVol;
845setRootVolume(bootVol);
846gBIOSDev = dev;
847}
848*/
849
850bool checkOSVersion(const char * version)
851{
852if ( (sizeof(version) > 4) && ('.' != version[4]) && ('\0' != version[4]) )
853{
854return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
855&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3])
856&& (gMacOSVersion[4] == version[4]));
857}
858else
859{
860return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
861&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));
862}
863}
864
865uint32_t getMacOSVerCurrent()
866{
867 MacOSVerCurrent = MacOSVer2Int(gBootVolume->OSVersion);
868 return MacOSVerCurrent;
869}
870
871#define BASE 65521L /* largest prime smaller than 65536 */
872#define NMAX 5000
873// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
874
875#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
876#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
877#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
878#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
879#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
880
881unsigned long Adler32(unsigned char *buf, long len)
882{
883unsigned long s1 = 1; // adler & 0xffff;
884unsigned long s2 = 0; // (adler >> 16) & 0xffff;
885unsigned long result;
886int k;
887
888while (len > 0) {
889k = len < NMAX ? len : NMAX;
890len -= k;
891while (k >= 16) {
892DO16(buf);
893buf += 16;
894k -= 16;
895}
896if (k != 0) do {
897s1 += *buf++;
898s2 += s1;
899} while (--k);
900s1 %= BASE;
901s2 %= BASE;
902}
903result = (s2 << 16) | s1;
904return OSSwapHostToBigInt32(result);
905}
906

Archive Download this file

Revision: 2537