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 ( MacOSVerCurrent < MacOSVer2Int("10.7") )
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 ( MacOSVerCurrent >= MacOSVer2Int("10.4") && MacOSVerCurrent <= MacOSVer2Int("10.5") ) // 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 ( MacOSVerCurrent >= MacOSVer2Int("10.6") && MacOSVerCurrent < MacOSVer2Int("10.7") )
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", kernelCacheFile);
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", kernelCacheFile);
325
326}
327}
328
329// Check if the kernel cache file exists
330ret = -1;
331
332if (gBootVolume->flags & kBVFlagBooter)
333{
334snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);
335ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
336
337if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
338{
339snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);
340ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
341
342if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
343{
344snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);
345ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
346
347if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
348{
349strncpy(kernelCachePath, "/com.apple.recovery.boot/kernelcache", sizeof(kernelCachePath) );
350ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
351
352if ((flags & kFileTypeMask) != kFileTypeFlat)
353{
354ret = -1;
355}
356}
357}
358}
359}
360
361// If not found, use the original kernel cache path.
362if (ret == -1)
363{
364strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));
365ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
366
367if ((flags & kFileTypeMask) != kFileTypeFlat)
368{
369ret = -1;
370}
371}
372
373// Exit if kernel cache file wasn't found
374if (ret == -1)
375{
376DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);
377return -1;
378}
379
380// Check if the kernel cache file is more recent (mtime)
381// than the kernel file or the S/L/E directory
382ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
383
384// Check if the kernel file is more recent than the cache file
385if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat) && (kerneltime > cachetime))
386{
387DBG("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n", bootInfo->bootFile, kernelCacheFile);
388return -1;
389}
390
391ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
392
393// Check if the S/L/E directory time is more recent than the cache file
394if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory) && (exttime > cachetime))
395{
396DBG("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n", kernelCacheFile);
397return -1;
398}
399
400// Since the kernel cache file exists and is the most recent try to load it
401DBG("Loading Kernel Cache from: '%s%s' (%s)\n", gBootVolume->label, gBootVolume->altlabel, gBootVolume->type_name);
402
403ret = LoadThinFatFile(kernelCachePath, binary);
404return ret; // ret contain the length of the binary
405}
406
407//==========================================================================
408// This is the entrypoint from real-mode which functions exactly as it did
409// before. Multiboot does its own runtime initialization, does some of its
410// own things, and then calls common_boot.
411void boot(int biosdev)
412{
413initialize_runtime();
414// Enable A20 gate before accessing memory above 1Mb.
415enableA20();
416common_boot(biosdev);
417}
418
419//==========================================================================
420// The 'main' function for the booter. Called by boot0 when booting
421// from a block device, or by the network booter.
422//
423// arguments:
424// biosdev - Value passed from boot1/NBP to specify the device
425// that the booter was loaded from.
426//
427// If biosdev is kBIOSDevNetwork, then this function will return if
428// booting was unsuccessful. This allows the PXE firmware to try the
429// next boot device on its list.
430void common_boot(int biosdev)
431{
432bool quiet;
433bool firstRun = true;
434bool instantMenu;
435bool rescanPrompt;
436intstatus;
437unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
438unsigned intdenyBVFlags = kBVFlagEFISystem;
439
440// Set reminder to unload the PXE base code. Neglect to unload
441// the base code will result in a hang or kernel panic.
442gUnloadPXEOnExit = true;
443
444// Record the device that the booter was loaded from.
445gBIOSDev = biosdev & kBIOSDevMask;
446
447// Initialize boot-log
448initBooterLog();
449
450#if DEBUG_INTERRUPTS
451// Enable interrupts
452interruptsAvailable = SetupInterrupts();
453if (interruptsAvailable)
454{
455EnableInterrupts();
456}
457#endif
458
459// Initialize boot info structure.
460initKernBootStruct();
461
462// Setup VGA text mode.
463// Not sure if it is safe to call setVideoMode() before the
464// config table has been loaded. Call video_mode() instead.
465#if DEBUG
466printf("before video_mode\n");
467#endif
468video_mode( 2 ); // 80x25 mono text mode.
469#if DEBUG
470printf("after video_mode\n");
471#endif
472
473// Scan and record the system's hardware information.
474scan_platform();
475
476// First get info for boot volume.
477scanBootVolumes(gBIOSDev, 0);
478bvChain = getBVChainForBIOSDev(gBIOSDev);
479setBootGlobals(bvChain);
480
481// Load boot.plist config file
482status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);
483
484if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)
485{
486gBootMode |= kBootModeQuiet;
487}
488
489// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
490if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)
491{
492firstRun = false;
493}
494
495// Loading preboot ramdisk if exists.
496loadPrebootRAMDisk();
497
498// Disable rescan option by default
499gEnableCDROMRescan = false;
500
501// Enable it with Rescan=y in system config
502if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)&& gEnableCDROMRescan)
503{
504gEnableCDROMRescan = true;
505}
506
507// Disable rescanPrompt option by default
508rescanPrompt = false;
509
510// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
511if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))
512{
513gEnableCDROMRescan = promptForRescanOption();
514}
515
516// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
517if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)
518{
519gScanSingleDrive = true;
520}
521
522// Create a list of partitions on device(s).
523if (gScanSingleDrive)
524{
525scanBootVolumes(gBIOSDev, &bvCount);
526}
527else
528{
529scanDisks(gBIOSDev, &bvCount);
530}
531
532// Create a separated bvr chain using the specified filters.
533bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
534
535gBootVolume = selectBootVolume(bvChain);
536
537// Intialize module system
538init_module_system();
539
540#if DEBUG
541printf("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
542gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
543printf("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
544gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
545getchar();
546#endif
547
548useGUI = true;
549// Override useGUI default
550getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
551if (useGUI && initGUI())
552{
553// initGUI() returned with an error, disabling GUI.
554useGUI = false;
555}
556
557setBootGlobals(bvChain);
558
559// Parse args, load and start kernel.
560while (1)
561{
562booltryresume, tryresumedefault, forceresume;
563booluseKernelCache = true; // by default try to use the prelinked kernel
564const char*val;
565intlen, ret = -1;
566longflags;
567u_int32_tsleeptime, time;
568void*binary = (void *)kLoadAddr;
569
570charbootFile[sizeof(bootInfo->bootFile)];
571charbootFilePath[512];
572charkernelCacheFile[512];
573
574// Initialize globals.
575sysConfigValid = false;
576gErrors = false;
577
578status = getBootOptions(firstRun);
579firstRun = false;
580if (status == -1) continue;
581
582status = processBootOptions();
583// Status == 1 means to chainboot
584if ( status ==1 ) break;
585// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
586if ( status == -1 )
587{
588// gBootVolume == NULL usually means the user hit escape.
589if (gBootVolume == NULL)
590{
591freeFilteredBVChain(bvChain);
592
593if (gEnableCDROMRescan)
594rescanBIOSDevice(gBIOSDev);
595
596bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
597setBootGlobals(bvChain);
598setupDeviceList(&bootInfo->themeConfig);
599}
600continue;
601}
602
603// Other status (e.g. 0) means that we should proceed with boot.
604
605// Turn off any GUI elements
606if ( bootArgs->Video.v_display == GRAPHICS_MODE )
607{
608gui.devicelist.draw = false;
609gui.bootprompt.draw = false;
610gui.menu.draw = false;
611gui.infobox.draw = false;
612gui.logo.draw = false;
613drawBackground();
614updateVRAM();
615}
616
617if (platformCPUFeature(CPU_FEATURE_EM64T))
618{
619archCpuType = CPU_TYPE_X86_64;
620}
621else
622{
623archCpuType = CPU_TYPE_I386;
624}
625
626if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig))
627{
628if (strncmp(val, "x86_64", sizeof("x86_64") ) == 0)
629{
630archCpuType = CPU_TYPE_X86_64;
631}
632else if (strncmp(val, "i386", sizeof("i386") ) == 0)
633{
634archCpuType = CPU_TYPE_I386;
635}
636else
637{
638DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");
639}
640}
641
642if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig))
643{
644if (strncmp(val, "i386", sizeof("i386") ) == 0)
645{
646archCpuType = CPU_TYPE_I386;
647}
648}
649
650// Notify modules that we are attempting to boot
651execute_hook("PreBoot", NULL, NULL, NULL, NULL);
652
653if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig))
654{
655tryresume = true;
656tryresumedefault = true;
657}
658else
659{
660tryresumedefault = false;
661}
662
663if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig))
664{
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 ); // 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
876// =========================================================================
877
878/*!
879Selects a new BIOS device, taking care to update the global state appropriately.
880 */
881/*
882static void selectBiosDevice(void)
883{
884struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
885CacheReset();
886diskFreeMap(oldMap);
887oldMap = NULL;
888
889int dev = selectAlternateBootDevice(gBIOSDev);
890
891BVRef bvchain = scanBootVolumes(dev, 0);
892BVRef bootVol = selectBootVolume(bvchain);
893gBootVolume = bootVol;
894setRootVolume(bootVol);
895gBIOSDev = dev;
896}
897*/
898
899// =========================================================================
900bool checkOSVersion(const char *version)
901{
902if ( (sizeof(version) > 4) && ('.' != version[4]) && ('\0' != version[4]) )
903{
904return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
905&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3])
906&& (gMacOSVersion[4] == version[4]));
907}
908else
909{
910return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
911&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));
912}
913}
914
915uint32_t getMacOSVerCurrent()
916{
917MacOSVerCurrent = MacOSVer2Int(gBootVolume->OSVersion);
918return MacOSVerCurrent;
919}
920
921// =========================================================================
922unsigned long Adler32(unsigned char *buf, long len)
923{
924#define BASE 65521L /* largest prime smaller than 65536 */
925#define NMAX 5000
926// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
927
928#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
929#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
930#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
931#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
932#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
933
934int k;
935
936unsigned long s1 = 1; // adler & 0xffff;
937unsigned long s2 = 0; // (adler >> 16) & 0xffff;
938unsigned long result;
939
940while (len > 0) {
941k = len < NMAX ? len : NMAX;
942len -= k;
943while (k >= 16)
944{
945DO16(buf);
946buf += 16;
947k -= 16;
948}
949
950if (k != 0)
951{
952do
953{
954s1 += *buf++;
955s2 += s1;
956} while (--k);
957}
958
959s1 %= BASE;
960s2 %= BASE;
961}
962
963result = (s2 << 16) | s1;
964
965return OSSwapHostToBigInt32(result);
966}
967

Archive Download this file

Revision: 2769