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 "config.h"
49#include "boot.h"
50#include "bootstruct.h"
51#include "fake_efi.h"
52#include "sl.h"
53#include "libsa.h"
54#include "ramdisk.h"
55#include "gui.h"
56#include "platform.h"
57#include "modules.h"
58#include "device_tree.h"
59#include "xml.h"
60
61#if DEBUG_BOOT2
62#define DBG(x...)printf(x)
63#else
64#define DBG(x...)msglog(x)
65#endif
66
67/*
68 * How long to wait (in seconds) to load the
69 * kernel after displaying the "boot:" prompt.
70 */
71#define kBootErrorTimeout 5
72
73boolgOverrideKernel;
74boolgEnableCDROMRescan;
75boolgScanSingleDrive;
76booluseGUI;
77
78/* recovery or installer ? */
79bool isInstaller;
80bool isRecoveryHD;
81bool isMacOSXUpgrade;
82bool isOSXUpgrade;
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// Load the Kernel.plist override config file if any
145static void setupKernelConfigFile(const char *filename)
146{
147chardirSpec[128];
148const char*override_pathname = NULL;
149intlen = 0, err = 0;
150
151// Take in account user overriding
152if (getValueForKey(kKERNELKey, &override_pathname, &len, &bootInfo->chameleonConfig) && len > 0)
153{
154// Specify a path to a file, e.g. KERNELPlist=/Extra/Kernel2.plist
155strcpy(dirSpec, override_pathname);
156err = loadConfigFile(dirSpec, &bootInfo->kernelConfig);
157}
158else
159{
160// Check selected volume's Extra.
161sprintf(dirSpec, "/Extra/%s", filename);
162err = loadConfigFile(dirSpec, &bootInfo->kernelConfig);
163}
164
165if (!err)
166{
167getBoolForKey(kKernelBooter_kexts, &KernelBooter_kexts, KERNELPlist);
168getBoolForKey(kKernelPm, &KernelPm, KERNELPlist);
169getBoolForKey(kKernelLapicError, &KernelLapicError, KERNELPlist);
170getBoolForKey(kKernelLapicVersion, &KernelLapicVersion, KERNELPlist);
171getBoolForKey(kKernelHaswell, &KernelHaswell, KERNELPlist);
172getBoolForKey(kKernelcpuFamily, &KernelcpuFamily, KERNELPlist);
173getBoolForKey(kKernelSSE3, &KernelSSE3, KERNELPlist);
174}
175else
176{
177verbose("No %s replacement found.\n", filename);
178}
179}
180
181#if KEXTPATCH_SUPPORT
182// =========================================================================
183// Load the Kexts.plist override config file if any
184static void setupKextConfigFile(const char *filename)
185{
186chardirSpec[128];
187const char*override_pathname = NULL;
188intlen = 0, err = 0;
189
190AppleRTCPatch = false;
191AICPMPatch = false;
192OrangeIconFixSata = false;
193NVIDIAWebDrv = false;
194TrimEnablerSata = false;
195
196// Take in account user overriding
197if (getValueForKey(kKEXTKey, &override_pathname, &len, &bootInfo->chameleonConfig) && len > 0)
198{
199// Specify a path to a file, e.g. KEXTPlist=/Extra/Kext2.plist
200strcpy(dirSpec, override_pathname);
201err = loadConfigFile(dirSpec, &bootInfo->kextConfig);
202} else {
203// Check selected volume's Extra.
204sprintf(dirSpec, "/Extra/%s", filename);
205err = loadConfigFile(dirSpec, &bootInfo->kextConfig);
206}
207
208if (!err)
209{
210getBoolForKey(kAppleRTCPatch, &AppleRTCPatch, KEXTPlist);
211getBoolForKey(kAICPMPatch, &AICPMPatch, KEXTPlist);
212getBoolForKey(kNVIDIAWebDrv, &NVIDIAWebDrv, KEXTPlist);
213getBoolForKey(kOrangeIconFixSata, &OrangeIconFixSata, KEXTPlist);
214getBoolForKey(kTrimEnablerSata, &TrimEnablerSata, KEXTPlist);
215} else {
216verbose("No %s replacement found.\n", filename);
217}
218}
219#endif
220
221//==========================================================================
222// ExecKernel - Load the kernel image (mach-o) and jump to its entry point.
223
224static int ExecKernel(void *binary)
225{
226intret;
227entry_tkernelEntry;
228
229bootArgs->kaddr = bootArgs->ksize = 0;
230
231// ===============================================================================
232
233gMacOSVersion[0] = 0;
234verbose("Booting on %s %s (%s)\n", (MacOSVerCurrent < MacOSVer2Int("10.8")) ? "Mac OS X" : (MacOSVerCurrent < MacOSVer2Int("10.12")) ? "OS X" : "macOS", gBootVolume->OSFullVer, gBootVolume->OSBuildVer );
235
236setupBooterArgs();
237
238// ===============================================================================
239
240execute_hook("ExecKernel", (void *)binary, NULL, NULL, NULL);
241
242ret = DecodeKernel(binary,
243 &kernelEntry,
244 (char **) &bootArgs->kaddr,
245 (int *)&bootArgs->ksize);
246
247if ( ret != 0 )
248{
249printf("Decoding kernel failed.\n");
250pause();
251return ret;
252}
253
254// Reserve space for boot args
255reserveKernBootStruct();
256
257// Notify modules that the kernel has been decoded
258execute_hook("DecodedKernel", (void *)binary, (void *)bootArgs->kaddr, (void *)bootArgs->ksize, NULL);
259
260setupFakeEfi();
261
262// Load boot drivers from the specifed root path.
263//if (!gHaveKernelCache)
264{
265LoadDrivers("/");
266}
267
268execute_hook("DriversLoaded", (void *)binary, NULL, NULL, NULL);
269
270clearActivityIndicator();
271
272if (gErrors)
273{
274printf("Errors encountered while starting up the computer.\n");
275printf("Pausing %d seconds...\n", kBootErrorTimeout);
276sleep(kBootErrorTimeout);
277}
278
279md0Ramdisk();
280
281// Cleanup the PXE base code.
282
283if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit )
284{
285if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
286{
287printf("nbpUnloadBaseCode error %d\n", (int)ret);
288sleep(2);
289}
290}
291
292bool dummyVal;
293if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal)
294{
295showTextBuffer(msgbuf, strlen(msgbuf));
296}
297
298usb_loop();
299
300#if DEBUG
301if (interruptsAvailable) ShowInterruptCounters();
302#endif
303
304// If we were in text mode, switch to graphics mode.
305// This will draw the boot graphics unless we are in
306// verbose mode.
307if (gVerboseMode)
308{
309setVideoMode( GRAPHICS_MODE );
310}
311else
312{
313drawBootGraphics();
314}
315
316DBG("Starting Darwin/%s [%s]\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64", gDarwinBuildVerStr);
317DBG("Boot Args: %s\n", bootArgs->CommandLine);
318
319setupBooterLog();
320
321finalizeBootStruct();
322
323// Jump to kernel's entry point. There's no going back now.
324if ( MacOSVerCurrent < MacOSVer2Int("10.7") )
325{
326// Notify modules that the kernel is about to be started
327execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgsLegacy, NULL, NULL);
328
329startprog( kernelEntry, bootArgsLegacy );
330}
331else
332{
333// Notify modules that the kernel is about to be started
334execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgs, NULL, NULL);
335
336startprog( kernelEntry, bootArgs );
337}
338
339// Not reached
340__builtin_unreachable();
341}
342
343//==========================================================================
344// LoadKernelCache - Try to load Kernel Cache.
345// return the length of the loaded cache file or -1 on error
346long LoadKernelCache(const char *cacheFile, void **binary)
347{
348charkernelCacheFile[512];
349charkernelCachePath[512];
350longflags, ret=-1;
351unsigned longadler32 = 0;
352u_int32_t time, cachetime, kerneltime, exttime;
353
354if((gBootMode & kBootModeSafe) != 0)
355{
356DBG("Kernel Cache ignored.\n");
357return -1;
358}
359
360// Use specify kernel cache file if not empty
361if (cacheFile[0] != 0)
362{
363strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));
364verbose("Specified kernel cache file path: %s\n", cacheFile);
365}
366else
367{
368// Leopard prelink kernel cache file
369if ( MacOSVerCurrent >= MacOSVer2Int("10.4") && MacOSVerCurrent <= MacOSVer2Int("10.5") ) // OSX is 10.4 or 10.5
370{
371// Reset cache name.
372bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
373snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64, "%s,%s", gRootDevice, bootInfo->bootFile);
374adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
375snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);
376verbose("Reseted kernel cache file path: %s\n", kernelCacheFile);
377
378}
379// Snow Leopard prelink kernel cache file
380else if ( MacOSVerCurrent >= MacOSVer2Int("10.6") && MacOSVerCurrent < MacOSVer2Int("10.7") )
381{
382snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",
383(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");
384
385intlnam = strlen(kernelCacheFile) + 9; //with adler32
386char*name;
387u_int32_tprev_time = 0;
388
389struct dirstuff *cacheDir = opendir(kDefaultCachePathSnow);
390
391/* TODO: handle error? */
392if (cacheDir)
393{
394while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
395{
396if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
397&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))
398{
399snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);
400prev_time = time;
401}
402}
403verbose("Kernel Cache file path (Mac OS X 10.6): %s\n", kernelCacheFile);
404}
405closedir(cacheDir);
406}
407else if ( MacOSVerCurrent >= MacOSVer2Int("10.7") && MacOSVerCurrent < MacOSVer2Int("10.9") )
408{
409// Lion, Mountain Lion
410// for 10.7 10.8
411if (isMacOSXUpgrade)
412{
413snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/Mac OS X Install Data/");
414}
415else if (isInstaller)
416{
417if (MacOSVerCurrent >= MacOSVer2Int("10.7") && MacOSVerCurrent < MacOSVer2Int("10.8") )
418{
419snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kLionInstallerDataFolder);
420}
421else if ( MacOSVerCurrent >= MacOSVer2Int("10.8") && MacOSVerCurrent < MacOSVer2Int("10.9") )
422{
423snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kMLionInstallerDataFolder);
424}
425
426}
427else if (isRecoveryHD)
428{
429snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);
430}
431else
432{
433snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);
434}
435
436verbose("Kernel Cache file path (Mac OS X 10.7 and newer): %s\n", kernelCacheFile);
437
438}
439else if ( MacOSVerCurrent >= MacOSVer2Int("10.9") && MacOSVerCurrent < MacOSVer2Int("10.10") )
440{
441// Mavericks prelinked cache file
442// for 10.9
443if (isOSXUpgrade)
444{
445snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/OS X Install Data/");
446}
447else if (isInstaller)
448{
449snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheInstallerNew);
450}
451else if (isRecoveryHD)
452{
453snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);
454}
455else
456{
457snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);
458}
459
460verbose("Kernel Cache file path (OS X 10.9): %s\n", kernelCacheFile);
461
462}
463else if ( MacOSVerCurrent >= MacOSVer2Int("10.10") && MacOSVerCurrent < MacOSVer2Int("10.11") )
464{
465// Yosemite prelink kernel cache file
466// for 10.10 and 10.11
467if (isOSXUpgrade)
468{
469snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/OS X Install Data/");
470}
471else if (isInstaller)
472{
473snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheInstallerNew);
474}
475else if (isRecoveryHD)
476{
477snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);
478}
479else
480{
481snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);
482}
483
484verbose("Kernel Cache file path (OS X 10.10): %s\n", kernelCacheFile);
485}
486else if ( MacOSVerCurrent >= MacOSVer2Int("10.11") )
487{
488// El Capitan on prelinked kernel cache file
489// for 10.10 and 10.11
490if (isOSXUpgrade)
491{
492if ( MacOSVerCurrent >= MacOSVer2Int("10.13") )
493{
494snprintf(kernelCacheFile, sizeof(kernelCacheFile),
495 "%sprelinkedkernel",
496"/macOS Install Data/Locked Files/Boot Files/");
497}
498else
499{
500snprintf(kernelCacheFile,
501sizeof(kernelCacheFile),
502"%sprelinkedkernel",
503"/OS X Install Data/");
504}
505
506}
507else if (isInstaller)
508{
509snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCacheInstallerNew);
510}
511else if (isRecoveryHD)
512{
513snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCacheRecoveryHD);
514}
515else
516{
517snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);
518}
519
520verbose("Kernel Cache file path (OS X 10.11 and newer): %s\n", kernelCacheFile);
521}
522}
523
524// Check if the kernel cache file exists
525ret = -1;
526
527if (gBootVolume->flags & kBVFlagBooter)
528{
529if (isRecoveryHD)
530{
531strncpy(kernelCachePath, "/com.apple.recovery.boot/prelinkedkernel", sizeof(kernelCachePath) );
532ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
533
534if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
535{
536strncpy(kernelCachePath, "/com.apple.recovery.boot/kernelcache", sizeof(kernelCachePath) );
537ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
538
539if ((flags & kFileTypeMask) != kFileTypeFlat)
540{
541ret = -1;
542}
543}
544}
545else if (isInstaller)
546{
547strncpy(kernelCachePath, "/.IABootFiles/prelinkedkernel", sizeof(kernelCachePath) );
548ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
549
550if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
551{
552strncpy(kernelCachePath, "/.IABootFiles/kernelcache", sizeof(kernelCachePath) );
553ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
554
555if ((flags & kFileTypeMask) != kFileTypeFlat)
556{
557ret = -1;
558}
559}
560}
561else if (isMacOSXUpgrade)
562{
563strncpy(kernelCachePath, "/Mac OS X Install Data/kernelcache", sizeof(kernelCachePath) );
564ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
565
566if ((flags & kFileTypeMask) != kFileTypeFlat)
567{
568ret = -1;
569}
570}
571else if (isOSXUpgrade)
572{
573strncpy(kernelCachePath,
574"/macOS Install Data/Locked Files/Boot Files/prelinkedkernel",
575sizeof(kernelCachePath) );
576ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
577
578if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
579{
580strncpy(kernelCachePath, "/OS X Install Data/prelinkedkernel", sizeof(kernelCachePath) );
581ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
582
583if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
584{
585strncpy(kernelCachePath, "/OS X Install Data/kernelcache", sizeof(kernelCachePath) );
586ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
587
588if ((flags & kFileTypeMask) != kFileTypeFlat)
589{
590ret = -1;
591}
592}
593}
594}
595else
596{
597snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);
598ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
599if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
600{
601snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);
602ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
603
604if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
605{
606snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);
607ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
608
609if ((flags & kFileTypeMask) != kFileTypeFlat)
610{
611ret = -1;
612}
613}
614}
615}
616}
617
618// If not found, use the original kernel cache path.
619if (ret == -1)
620{
621strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));
622ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
623
624if ((flags & kFileTypeMask) != kFileTypeFlat)
625{
626ret = -1;
627}
628}
629
630// Exit if kernel cache file wasn't found
631if (ret == -1)
632{
633DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);
634return -1;
635}
636
637if ( !isInstaller && !isRecoveryHD && !isMacOSXUpgrade && !isOSXUpgrade )
638{
639// Check if the kernel cache file is more recent (mtime)
640// than the kernel file or the S/L/E directory
641ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
642
643// Check if the kernel file is more recent than the cache file
644if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat) && (kerneltime > cachetime))
645{
646DBG("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n", bootInfo->bootFile, kernelCacheFile);
647return -1;
648}
649
650ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
651
652// Check if the S/L/E directory time is more recent than the cache file
653if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory) && (exttime > cachetime))
654{
655DBG("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n", kernelCacheFile);
656return -1;
657}
658}
659
660// Since the kernel cache file exists and is the most recent try to load it
661DBG("Loading Kernel Cache from: '%s%s' (%s)\n", gBootVolume->label, gBootVolume->altlabel, gBootVolume->type_name);
662
663ret = LoadThinFatFile(kernelCachePath, binary);
664return ret; // ret contain the length of the binary
665}
666
667//==========================================================================
668// This is the entrypoint from real-mode which functions exactly as it did
669// before. Multiboot does its own runtime initialization, does some of its
670// own things, and then calls common_boot.
671void boot(int biosdev)
672{
673// Enable A20 gate before accessing memory above 1Mb.
674// Note: malloc_init(), called via initialize_runtime() writes
675// memory >= 1Mb, so A20 must be enabled before calling it. - zenith432
676zeroBSS();
677enableA20();
678malloc_init(0, 0, 0, malloc_error);
679common_boot(biosdev);
680}
681
682//==========================================================================
683// The 'main' function for the booter. Called by boot0 when booting
684// from a block device, or by the network booter.
685//
686// arguments:
687// biosdev - Value passed from boot1/NBP to specify the device
688// that the booter was loaded from.
689//
690// If biosdev is kBIOSDevNetwork, then this function will return if
691// booting was unsuccessful. This allows the PXE firmware to try the
692// next boot device on its list.
693void common_boot(int biosdev)
694{
695bool quiet;
696bool firstRun = true;
697bool instantMenu;
698bool rescanPrompt;
699intstatus;
700unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
701unsigned intdenyBVFlags = kBVFlagEFISystem;
702
703// Set reminder to unload the PXE base code. Neglect to unload
704// the base code will result in a hang or kernel panic.
705gUnloadPXEOnExit = true;
706
707// Record the device that the booter was loaded from.
708gBIOSDev = biosdev & kBIOSDevMask;
709
710// Initialize boot-log
711initBooterLog();
712
713#if DEBUG_INTERRUPTS
714// Enable interrupts
715interruptsAvailable = SetupInterrupts();
716if (interruptsAvailable)
717{
718EnableInterrupts();
719}
720#endif
721
722// Initialize boot info structure.
723initKernBootStruct();
724
725// Setup VGA text mode.
726// Not sure if it is safe to call setVideoMode() before the
727// config table has been loaded. Call video_mode() instead.
728#if DEBUG
729printf("before video_mode\n");
730#endif
731video_mode( 2 ); // 80x25 mono text mode.
732#if DEBUG
733printf("after video_mode\n");
734#endif
735
736// Scan and record the system's hardware information.
737scan_platform();
738
739// First get info for boot volume.
740scanBootVolumes(gBIOSDev, 0);
741bvChain = getBVChainForBIOSDev(gBIOSDev);
742setBootGlobals(bvChain);
743
744// Load boot.plist config file
745status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);
746
747if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)
748{
749gBootMode |= kBootModeQuiet;
750}
751
752// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
753if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)
754{
755firstRun = false;
756}
757
758// Loading preboot ramdisk if exists.
759loadPrebootRAMDisk();
760
761// Disable rescan option by default
762gEnableCDROMRescan = false;
763
764// Enable it with Rescan=y in system config
765if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)&& gEnableCDROMRescan)
766{
767gEnableCDROMRescan = true;
768}
769
770// Disable rescanPrompt option by default
771rescanPrompt = false;
772
773// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
774if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))
775{
776gEnableCDROMRescan = promptForRescanOption();
777}
778
779// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
780if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)
781{
782gScanSingleDrive = true;
783}
784
785// Create a list of partitions on device(s).
786if (gScanSingleDrive)
787{
788scanBootVolumes(gBIOSDev, &bvCount);
789}
790else
791{
792scanDisks(gBIOSDev, &bvCount);
793}
794
795// Create a separated bvr chain using the specified filters.
796bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
797
798gBootVolume = selectBootVolume(bvChain);
799
800// Intialize module system
801init_module_system();
802
803#if DEBUG
804printf("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
805gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
806printf("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
807gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
808getchar();
809#endif
810
811useGUI = true;
812// Override useGUI default
813getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
814if (useGUI && initGUI())
815{
816// initGUI() returned with an error, disabling GUI.
817useGUI = false;
818}
819
820setBootGlobals(bvChain);
821
822// Parse args, load and start kernel.
823while (1)
824{
825booltryresume, tryresumedefault, forceresume;
826booluseKernelCache = true; // by default try to use the prelinked kernel
827const char*val;
828intlen, ret = -1;
829longflags;
830u_int32_tsleeptime, time;
831void*binary = (void *)kLoadAddr;
832
833charbootFile[sizeof(bootInfo->bootFile)];
834charbootFilePath[512];
835charkernelCacheFile[512];
836
837// Initialize globals.
838sysConfigValid = false;
839gErrors = false;
840
841status = getBootOptions(firstRun);
842firstRun = false;
843if (status == -1) continue;
844
845status = processBootOptions();
846// Status == 1 means to chainboot
847if ( status ==1 ) break;
848// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
849if ( status == -1 )
850{
851// gBootVolume == NULL usually means the user hit escape.
852if (gBootVolume == NULL)
853{
854freeFilteredBVChain(bvChain);
855
856if (gEnableCDROMRescan)
857{
858rescanBIOSDevice(gBIOSDev);
859}
860
861bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
862setBootGlobals(bvChain);
863setupDeviceList(&bootInfo->themeConfig);
864}
865continue;
866}
867
868// Other status (e.g. 0) means that we should proceed with boot.
869
870// Turn off any GUI elements
871if ( bootArgs->Video.v_display == GRAPHICS_MODE )
872{
873gui.devicelist.draw = false;
874gui.bootprompt.draw = false;
875gui.menu.draw = false;
876gui.infobox.draw = false;
877gui.logo.draw = false;
878drawBackground();
879updateVRAM();
880}
881
882if (platformCPUFeature(CPU_FEATURE_EM64T))
883{
884archCpuType = CPU_TYPE_X86_64;
885}
886else
887{
888archCpuType = CPU_TYPE_I386;
889}
890
891if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig))
892{
893if (strncmp(val, "x86_64", sizeof("x86_64") ) == 0)
894{
895archCpuType = CPU_TYPE_X86_64;
896}
897else if (strncmp(val, "i386", sizeof("i386") ) == 0)
898{
899archCpuType = CPU_TYPE_I386;
900}
901else
902{
903DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");
904}
905}
906
907if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig))
908{
909if (strncmp(val, "i386", sizeof("i386") ) == 0)
910{
911archCpuType = CPU_TYPE_I386;
912}
913}
914
915// Notify modules that we are attempting to boot
916execute_hook("PreBoot", NULL, NULL, NULL, NULL);
917
918//--------------------------------------
919getBoolForKey(kSkipKernelPatcher, &skipKernelPatcher, &bootInfo->chameleonConfig);
920
921// kernel patcher
922if (!skipKernelPatcher)
923{
924const char*key = NULL;
925int len = 0;
926setupKernelConfigFile("kernel.plist");
927
928// looking at command line for kernel patcher setting and override where needed
929if (getValueForKey(kKernelBooter_kexts, &key, &len, &bootInfo->chameleonConfig) && len > 0)
930{
931getBoolForKey(kKernelBooter_kexts, &KernelBooter_kexts, &bootInfo->chameleonConfig);
932}
933key = NULL; len = 0;
934
935if (getValueForKey(kKernelPm, &key, &len, &bootInfo->chameleonConfig) && len > 0)
936{
937getBoolForKey(kKernelPm, &KernelPm, &bootInfo->chameleonConfig);
938}
939key = NULL; len = 0;
940
941if (getValueForKey(kKernelLapicError, &key, &len, &bootInfo->chameleonConfig) && len > 0)
942{
943getBoolForKey(kKernelLapicError, &KernelLapicError, &bootInfo->chameleonConfig);
944}
945key = NULL; len = 0;
946
947if (getValueForKey(kKernelLapicVersion, &key, &len, &bootInfo->chameleonConfig) && len > 0)
948{
949getBoolForKey(kKernelLapicVersion, &KernelLapicVersion, &bootInfo->chameleonConfig);
950}
951key = NULL; len = 0;
952
953if (getValueForKey(kKernelHaswell, &key, &len, &bootInfo->chameleonConfig) && len > 0)
954{
955getBoolForKey(kKernelHaswell, &KernelHaswell, &bootInfo->chameleonConfig);
956}
957key = NULL; len = 0;
958
959if (getValueForKey(kKernelcpuFamily, &key, &len, &bootInfo->chameleonConfig) && len > 0)
960{
961getBoolForKey(kKernelcpuFamily, &KernelcpuFamily, &bootInfo->chameleonConfig);
962}
963key = NULL; len = 0;
964
965if (getValueForKey(kKernelSSE3, &key, &len, &bootInfo->chameleonConfig) && len > 0)
966{
967getBoolForKey(kKernelSSE3, &KernelSSE3, &bootInfo->chameleonConfig);
968}
969}
970
971#if KEXTPATCH_SUPPORT
972//-------------------------------------- kext patcher
973getBoolForKey(kSkipKextsPatcher, &skipKextsPatcher, &bootInfo->chameleonConfig);
974
975// kext patcher
976if (!skipKextsPatcher)
977{
978const char*key = NULL;
979intlen = 0;
980setupKextConfigFile("kexts.plist");
981
982// looking at command line for kext patcher setting and override where needed
983if (getValueForKey(kAICPMPatch, &key, &len, &bootInfo->chameleonConfig) && len > 0)
984{
985getBoolForKey(kAICPMPatch, &AICPMPatch, &bootInfo->chameleonConfig);
986}
987
988key = NULL;
989len = 0;
990if (getValueForKey(kNVIDIAWebDrv, &key, &len, &bootInfo->chameleonConfig) && len > 0)
991{
992getBoolForKey(kNVIDIAWebDrv, &NVIDIAWebDrv, &bootInfo->chameleonConfig);
993}
994
995key = NULL;
996len = 0;
997if (getValueForKey(kOrangeIconFixSata, &key, &len, &bootInfo->chameleonConfig) && len > 0)
998{
999getBoolForKey(kOrangeIconFixSata, &OrangeIconFixSata, &bootInfo->chameleonConfig);
1000}
1001
1002key = NULL;
1003len = 0;
1004if (getValueForKey(kTrimEnablerSata, &key, &len, &bootInfo->chameleonConfig) && len > 0)
1005{
1006getBoolForKey(kTrimEnablerSata, &TrimEnablerSata, &bootInfo->chameleonConfig);
1007}
1008
1009key = NULL;
1010len = 0;
1011if (getValueForKey(kAppleRTCPatch, &key, &len, &bootInfo->chameleonConfig) && len > 0)
1012{
1013getBoolForKey(kAppleRTCPatch, &AppleRTCPatch, &bootInfo->chameleonConfig);
1014}
1015}
1016
1017// -------------------------------------- kext patcher
1018#endif
1019
1020if (gBootVolume->OSisInstaller)
1021{
1022isInstaller = true;
1023}
1024
1025if (gBootVolume->OSisMacOSXUpgrade)
1026{
1027isMacOSXUpgrade = true;
1028}
1029
1030if (gBootVolume->OSisOSXUpgrade)
1031{
1032isOSXUpgrade = true;
1033}
1034
1035if (gBootVolume->OSisRecovery)
1036{
1037isRecoveryHD = true;
1038}
1039
1040if ( !isRecoveryHD && !isInstaller && !isMacOSXUpgrade && !isOSXUpgrade )
1041{
1042if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig))
1043{
1044tryresume = true;
1045tryresumedefault = true;
1046}
1047else
1048{
1049tryresumedefault = false;
1050}
1051
1052if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig))
1053{
1054forceresume = false;
1055}
1056
1057if (forceresume)
1058{
1059tryresume = true;
1060tryresumedefault = false;
1061}
1062
1063while (tryresume)
1064{
1065const char *tmp;
1066BVRef bvr;
1067if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
1068val = "/private/var/vm/sleepimage";
1069
1070// Do this first to be sure that root volume is mounted
1071ret = GetFileInfo(0, val, &flags, &sleeptime);
1072
1073if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
1074break;
1075
1076// Can't check if it was hibernation Wake=y is required
1077if (bvr->modTime == 0 && tryresumedefault)
1078break;
1079
1080if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
1081break;
1082
1083if (!forceresume && ((sleeptime+3)<bvr->modTime))
1084{
1085#if DEBUG
1086printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
1087bvr->modTime-sleeptime);
1088#endif
1089break;
1090}
1091
1092HibernateBoot((char *)val);
1093break;
1094}
1095}
1096
1097if (!isRecoveryHD && !isInstaller && !isMacOSXUpgrade && !isOSXUpgrade )
1098{
1099getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
1100}
1101
1102if (useKernelCache)
1103{
1104do
1105{
1106// Determine the name of the Kernel Cache
1107if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))
1108{
1109if (val[0] == '\\')
1110{
1111len--;
1112val++;
1113}
1114/* FIXME: check len vs sizeof(kernelCacheFile) */
1115strlcpy(kernelCacheFile, val, len + 1);
1116}
1117else
1118{
1119kernelCacheFile[0] = 0; // Use default kernel cache file
1120}
1121
1122if (gOverrideKernel && kernelCacheFile[0] == 0)
1123{
1124DBG("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);
1125useKernelCache = false;
1126break;
1127}
1128
1129if (gMKextName[0] != 0)
1130{
1131DBG("Using a specific MKext Cache (%s), KernelCache will not be used\n", gMKextName);
1132useKernelCache = false;
1133break;
1134}
1135
1136if (gBootFileType != kBlockDeviceType)
1137{
1138useKernelCache = false;
1139}
1140
1141} while(0);
1142}
1143else
1144{
1145DBG("Kernel Cache using disabled by user.\n");
1146}
1147
1148do
1149{
1150if (useKernelCache)
1151{
1152ret = LoadKernelCache(kernelCacheFile, &binary);
1153if (ret >= 0)
1154{
1155break;
1156}
1157}
1158
1159bool bootFileWithDevice = false;
1160// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
1161if (strncmp(bootInfo->bootFile, "bt(", sizeof("bt(") ) == 0 ||
1162strncmp(bootInfo->bootFile, "hd(", sizeof("hd(") ) == 0 ||
1163strncmp(bootInfo->bootFile, "rd(", sizeof("rd(") ) == 0)
1164{
1165bootFileWithDevice = true;
1166}
1167
1168// bootFile must start with a / if it not start with a device name
1169if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')
1170{
1171if ( MacOSVerCurrent < MacOSVer2Int("10.10") ) // Mavericks and older
1172{
1173snprintf(bootFile, sizeof(bootFile), "/%s", bootInfo->bootFile); // append a leading /
1174}
1175else
1176{
1177// Yosemite and newer
1178snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathForYos"%s", bootInfo->bootFile); // Yosemite or El Capitan
1179}
1180}
1181else
1182{
1183strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));
1184}
1185
1186// Try to load kernel image from alternate locations on boot helper partitions.
1187ret = -1;
1188if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice)
1189{
1190snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);
1191ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
1192if (ret == -1)
1193{
1194snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);
1195ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
1196if (ret == -1)
1197{
1198snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);
1199ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
1200}
1201}
1202}
1203if (ret == -1)
1204{
1205// No alternate location found, using the original kernel image path.
1206strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));
1207}
1208
1209DBG("Loading kernel from: '%s' (%s)\n", gBootVolume->label, gBootVolume->type_name);
1210ret = LoadThinFatFile(bootFilePath, &binary);
1211if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
1212{
1213archCpuType = CPU_TYPE_I386;
1214ret = LoadThinFatFile(bootFilePath, &binary);
1215}
1216} while (0);
1217
1218clearActivityIndicator();
1219
1220#if DEBUG
1221printf("Pausing...");
1222sleep(8);
1223#endif
1224
1225if (ret <= 0)
1226{
1227printf("Can't find boot file: '%s'\n", bootFile);
1228sleep(1);
1229
1230if (gBootFileType == kNetworkDeviceType)
1231{
1232// Return control back to PXE. Don't unload PXE base code.
1233gUnloadPXEOnExit = false;
1234break;
1235}
1236pause();
1237
1238}
1239else
1240{
1241/* Won't return if successful. */
1242ret = ExecKernel(binary);
1243}
1244}
1245
1246// chainboot
1247if (status == 1)
1248{
1249// if we are already in graphics-mode,
1250if (getVideoMode() == GRAPHICS_MODE)
1251{
1252setVideoMode( VGA_TEXT_MODE ); // switch back to text mode.
1253}
1254}
1255
1256if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
1257{
1258nbpUnloadBaseCode();
1259}
1260
1261#if DEBUG_INTERRUPTS
1262if (interruptsAvailable)
1263{
1264DisableInterrupts();
1265}
1266#endif
1267
1268}
1269
1270// =========================================================================
1271//
1272void setupBooterArgs()
1273{
1274bool KPRebootOption= false; // I don't want this by default ( It makes me angry because I do not see the reason for the panic)+
1275//bool HiDPIOption= false; // (Disabled by default) 10.8+
1276//bool FlagBlackOption= false; // (Disabled by default) 10.10+
1277
1278// OS X Lion 10.7
1279if ( MacOSVerCurrent >= MacOSVer2Int("10.7") ) // Lion and Up!
1280{
1281// Pike R. Alpha: Adding a 16 KB log space.
1282bootArgs->performanceDataSize= 0;
1283bootArgs->performanceDataStart= 0;
1284
1285// Pike R. Alpha: AppleKeyStore.kext
1286bootArgs->keyStoreDataSize= 0;
1287bootArgs->keyStoreDataStart= 0;
1288
1289bootArgs->bootMemSize= 0;
1290bootArgs->bootMemStart= 0;
1291}
1292
1293// OS X Mountain Lion 10.8
1294if ( MacOSVerCurrent >= MacOSVer2Int("10.8") ) // Mountain Lion and Up!
1295{
1296// cparm
1297getBoolForKey(kRebootOnPanic, &KPRebootOption, &bootInfo->chameleonConfig);
1298if ( KPRebootOption )
1299{
1300bootArgs->flags |= kBootArgsFlagRebootOnPanic;
1301}
1302
1303// cparm
1304getBoolForKey(kEnableHiDPI, &HiDPIOption, &bootInfo->chameleonConfig);
1305
1306if ( HiDPIOption )
1307{
1308bootArgs->flags |= kBootArgsFlagHiDPI;
1309}
1310}
1311
1312// OS X Yosemite 10.10
1313if ( MacOSVerCurrent >= MacOSVer2Int("10.10") ) // Yosemite and Up!
1314{
1315// Pike R. Alpha
1316getBoolForKey(kBlackMode, &FlagBlackOption, &bootInfo->chameleonConfig);
1317if ( FlagBlackOption )
1318{
1319// bootArgs->flags |= kBootArgsFlagBlack;
1320bootArgs->flags |= kBootArgsFlagBlackBg; // Micky1979
1321}
1322}
1323
1324// OS X El Capitan 10.11
1325if ( MacOSVerCurrent >= MacOSVer2Int("10.11") ) // El Capitan, Sierra and High Sierra!
1326{
1327// ErmaC
1328verbose("\n");
1329intcsrValue;
1330
1331/*
1332 * A special BootArgs flag "kBootArgsFlagCSRBoot"
1333 * is set in the Recovery or Installation environment.
1334 * This flag is kind of overkill by turning off all the protections
1335 */
1336
1337if (isRecoveryHD || isInstaller || isOSXUpgrade || isMacOSXUpgrade)
1338{
1339// SIP can be controlled with or without FileNVRAM.kext (Pike R. Alpha)
1340bootArgs->flags|=(kBootArgsFlagCSRActiveConfig + kBootArgsFlagCSRConfigMode + kBootArgsFlagCSRBoot + kBootArgsFlagInstallUI);
1341}
1342else
1343{
1344bootArgs->flags|= kBootArgsFlagCSRActiveConfig;
1345}
1346
1347// Set limit to 8bit
1348if ( getIntForKey(kCsrActiveConfig, &csrValue, &bootInfo->chameleonConfig) && (csrValue >= 0 && csrValue <= 255) )
1349{
1350bootArgs->csrActiveConfig= csrValue;
1351csrInfo(csrValue, 1);
1352}
1353else
1354{
1355// zenith432
1356bootArgs->csrActiveConfig= 0x67;
1357csrInfo(0x67, 0);
1358
1359}
1360
1361
1362// ===============================================================================
1363
1364bootArgs->csrCapabilities= CSR_VALID_FLAGS;
1365bootArgs->boot_SMC_plimit= 0;
1366}
1367}
1368
1369// =========================================================================
1370// ErmaC
1371void csrInfo(int csrValue, bool custom)
1372{
1373#define CSR_BITS 0x080
1374
1375int mask = CSR_BITS;
1376verbose("System Integrity Protection status: %s ", (csrValue == 0) ? "enabled":"disabled");
1377verbose("(%s Configuration).\nCsrActiveConfig = 0x%02X (", custom ? "Custom":"Default", csrValue);
1378
1379// Display integer number into binary using bitwise operator
1380((csrValue & 0x020) == 0) ? verbose("0"): verbose("1");
1381while (mask != 0)
1382{
1383( ((csrValue & mask) == 0) ? verbose("0"): verbose("1") );
1384mask = mask >> 1; // Right Shift
1385}
1386verbose(")\n");
1387if (csrValue != 0)
1388{
1389verbose("\nConfiguration:\n");
1390verbose("Kext Signing: %s\n", ((csrValue & 0x001) == 0) ? "enabled":"disabled"); /* (1 << 0) Allow untrusted kexts */
1391verbose("Filesystem Protections: %s\n", ((csrValue & 0x002) == 0) ? "enabled":"disabled"); /* (1 << 1) Allow unrestricted file system. */
1392verbose("Task for PID: %s\n", ((csrValue & 0x004) == 0) ? "enabled":"disabled"); /* (1 << 2) */
1393verbose("Debugging Restrictions: %s\n", ((csrValue & 0x008) == 0) ? "enabled":"disabled"); /* (1 << 3) */
1394verbose("Apple Internal: %s\n", ((csrValue & 0x010) == 0) ? "enabled":"disabled"); /* (1 << 4) */
1395verbose("DTrace Restrictions: %s\n", ((csrValue & 0x020) == 0) ? "enabled":"disabled"); /* (1 << 5) Allow unrestricted dtrace */
1396verbose("NVRAM Protections: %s\n", ((csrValue & 0x040) == 0) ? "enabled":"disabled"); /* (1 << 6) Allow unrestricted NVRAM */
1397verbose("Device configuration: %s\n", ((csrValue & 0x080) == 0) ? "enabled":"disabled"); /* (1 << 7) Allow device configuration */
1398verbose("BaseSystem Verification: %s\n", ((csrValue & 0x100) == 0) ? "enabled":"disabled"); /* (1 << 8) Allow any Recovery OS */
1399}
1400verbose("\n");
1401}
1402
1403// =========================================================================
1404
1405/*!
1406Selects a new BIOS device, taking care to update the global state appropriately.
1407 */
1408/*
1409static void selectBiosDevice(void)
1410{
1411struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
1412CacheReset();
1413diskFreeMap(oldMap);
1414oldMap = NULL;
1415
1416int dev = selectAlternateBootDevice(gBIOSDev);
1417
1418BVRef bvchain = scanBootVolumes(dev, 0);
1419BVRef bootVol = selectBootVolume(bvchain);
1420gBootVolume = bootVol;
1421setRootVolume(bootVol);
1422gBIOSDev = dev;
1423}
1424*/
1425
1426// =========================================================================
1427bool checkOSVersion(const char *version)
1428{
1429if ( '\0' != version[4] )
1430{
1431return !memcmp(&gMacOSVersion[0], &version[0], 5);
1432}
1433else
1434{
1435return !memcmp(&gMacOSVersion[0], &version[0], 4);
1436}
1437}
1438
1439uint32_t getMacOSVerCurrent()
1440{
1441MacOSVerCurrent = MacOSVer2Int(gBootVolume->OSVersion);
1442return MacOSVerCurrent;
1443}
1444
1445// =========================================================================
1446unsigned long Adler32(unsigned char *buf, long len)
1447{
1448#define BASE 65521L /* largest prime smaller than 65536 */
1449#define NMAX 5000
1450// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
1451
1452#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
1453#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
1454#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
1455#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
1456#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
1457
1458int k;
1459
1460unsigned long s1 = 1; // adler & 0xffff;
1461unsigned long s2 = 0; // (adler >> 16) & 0xffff;
1462unsigned long result;
1463
1464while (len > 0)
1465{
1466k = len < NMAX ? len : NMAX;
1467len -= k;
1468while (k >= 16)
1469{
1470DO16(buf);
1471buf += 16;
1472k -= 16;
1473}
1474
1475if (k != 0)
1476{
1477do
1478{
1479s1 += *buf++;
1480s2 += s1;
1481} while (--k);
1482}
1483
1484s1 %= BASE;
1485s2 %= BASE;
1486}
1487
1488result = (s2 << 16) | s1;
1489
1490return OSSwapHostToBigInt32(result);
1491}
1492

Archive Download this file

Revision: 2897