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 );
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 longadler32 = 0;
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): %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))
665{
666forceresume = false;
667}
668
669if (forceresume)
670{
671tryresume = true;
672tryresumedefault = false;
673}
674
675while (tryresume)
676{
677const char *tmp;
678BVRef bvr;
679if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
680val = "/private/var/vm/sleepimage";
681
682// Do this first to be sure that root volume is mounted
683ret = GetFileInfo(0, val, &flags, &sleeptime);
684
685if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
686break;
687
688// Can't check if it was hibernation Wake=y is required
689if (bvr->modTime == 0 && tryresumedefault)
690break;
691
692if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
693break;
694
695if (!forceresume && ((sleeptime+3)<bvr->modTime))
696{
697#if DEBUG
698printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
699bvr->modTime-sleeptime);
700#endif
701break;
702}
703
704HibernateBoot((char *)val);
705break;
706}
707
708getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
709if (useKernelCache)
710{
711do
712{
713// Determine the name of the Kernel Cache
714if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))
715{
716if (val[0] == '\\')
717{
718len--;
719val++;
720}
721/* FIXME: check len vs sizeof(kernelCacheFile) */
722strlcpy(kernelCacheFile, val, len + 1);
723}
724else
725{
726kernelCacheFile[0] = 0; // Use default kernel cache file
727}
728
729if (gOverrideKernel && kernelCacheFile[0] == 0)
730{
731DBG("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);
732useKernelCache = false;
733break;
734}
735
736if (gMKextName[0] != 0)
737{
738DBG("Using a specific MKext Cache (%s), KernelCache will not be used\n", gMKextName);
739useKernelCache = false;
740break;
741}
742
743if (gBootFileType != kBlockDeviceType)
744{
745useKernelCache = false;
746}
747
748} while(0);
749}
750else
751{
752DBG("Kernel Cache using disabled by user.\n");
753}
754
755do
756{
757if (useKernelCache)
758{
759ret = LoadKernelCache(kernelCacheFile, &binary);
760if (ret >= 0)
761{
762break;
763}
764}
765
766bool bootFileWithDevice = false;
767// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
768if (strncmp(bootInfo->bootFile, "bt(", sizeof("bt(") ) == 0 ||
769strncmp(bootInfo->bootFile, "hd(", sizeof("hd(") ) == 0 ||
770strncmp(bootInfo->bootFile, "rd(", sizeof("rd(") ) == 0)
771{
772bootFileWithDevice = true;
773}
774
775// bootFile must start with a / if it not start with a device name
776if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')
777{
778if ( !YOSEMITE || !ELCAPITAN ) //Is not Yosemite 10.10 or El Capitan 10.11
779{
780snprintf(bootFile, sizeof(bootFile), "/%s", bootInfo->bootFile); // append a leading /
781}
782else
783{
784snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathForYos"%s", bootInfo->bootFile); // Yosemite or El Capitan
785}
786}
787else
788{
789strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));
790}
791
792// Try to load kernel image from alternate locations on boot helper partitions.
793ret = -1;
794if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice)
795{
796snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);
797ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
798if (ret == -1)
799{
800snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);
801ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
802if (ret == -1)
803{
804snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);
805ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
806}
807}
808}
809if (ret == -1)
810{
811// No alternate location found, using the original kernel image path.
812strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));
813}
814
815DBG("Loading kernel from: '%s' (%s)\n", gBootVolume->label, gBootVolume->type_name);
816ret = LoadThinFatFile(bootFilePath, &binary);
817if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
818{
819archCpuType = CPU_TYPE_I386;
820ret = LoadThinFatFile(bootFilePath, &binary);
821}
822} while (0);
823
824clearActivityIndicator();
825
826#if DEBUG
827printf("Pausing...");
828sleep(8);
829#endif
830
831if (ret <= 0)
832{
833printf("Can't find boot file: '%s'\n", bootFile);
834sleep(1);
835
836if (gBootFileType == kNetworkDeviceType)
837{
838// Return control back to PXE. Don't unload PXE base code.
839gUnloadPXEOnExit = false;
840break;
841}
842pause();
843
844}
845else
846{
847/* Won't return if successful. */
848ret = ExecKernel(binary);
849}
850}
851
852// chainboot
853if (status == 1)
854{
855// if we are already in graphics-mode,
856if (getVideoMode() == GRAPHICS_MODE)
857{
858setVideoMode( VGA_TEXT_MODE ); // switch back to text mode.
859}
860}
861
862if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
863{
864nbpUnloadBaseCode();
865}
866
867#if DEBUG_INTERRUPTS
868if (interruptsAvailable)
869{
870DisableInterrupts();
871}
872#endif
873
874}
875
876/*!
877Selects a new BIOS device, taking care to update the global state appropriately.
878 */
879/*
880static void selectBiosDevice(void)
881{
882struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
883CacheReset();
884diskFreeMap(oldMap);
885oldMap = NULL;
886
887int dev = selectAlternateBootDevice(gBIOSDev);
888
889BVRef bvchain = scanBootVolumes(dev, 0);
890BVRef bootVol = selectBootVolume(bvchain);
891gBootVolume = bootVol;
892setRootVolume(bootVol);
893gBIOSDev = dev;
894}
895*/
896
897bool checkOSVersion(const char * version)
898{
899if ( (sizeof(version) > 4) && ('.' != version[4]) && ('\0' != version[4]) )
900{
901return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
902&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3])
903&& (gMacOSVersion[4] == version[4]));
904}
905else
906{
907return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
908&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));
909}
910
911}
912
913uint32_t getMacOSVerCurrent()
914{
915MacOSVerCurrent = MacOSVer2Int(gBootVolume->OSVersion);
916return MacOSVerCurrent;
917}
918
919#define BASE 65521L /* largest prime smaller than 65536 */
920#define NMAX 5000
921// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
922
923#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
924#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
925#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
926#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
927#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
928
929unsigned long Adler32(unsigned char *buf, long len)
930{
931unsigned long s1 = 1; // adler & 0xffff;
932unsigned long s2 = 0; // (adler >> 16) & 0xffff;
933unsigned long result;
934int k;
935
936while (len > 0) {
937k = len < NMAX ? len : NMAX;
938len -= k;
939while (k >= 16) {
940DO16(buf);
941buf += 16;
942k -= 16;
943}
944if (k != 0) do {
945s1 += *buf++;
946s2 += s1;
947} while (--k);
948s1 %= BASE;
949s2 %= BASE;
950}
951result = (s2 << 16) | s1;
952return OSSwapHostToBigInt32(result);
953}
954

Archive Download this file

Revision: 2758