Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2740