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}
425else if ( MacOSVerCurrent > MacOSVer2Int("10.12") && MacOSVerCurrent < MacOSVer2Int("10.16") )
426{
427snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kHSierraInstallerDataFolder);
428}
429}
430else if (isRecoveryHD)
431{
432snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);
433}
434else
435{
436snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);
437}
438
439verbose("Kernel Cache file path (Mac OS X 10.7 and newer): %s\n", kernelCacheFile);
440
441}
442else if ( MacOSVerCurrent >= MacOSVer2Int("10.9") && MacOSVerCurrent < MacOSVer2Int("10.10") )
443{
444// Mavericks prelinked cache file
445// for 10.9
446if (isOSXUpgrade)
447{
448snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/OS X Install Data/");
449}
450else if (isInstaller)
451{
452snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheInstallerNew);
453}
454else if (isRecoveryHD)
455{
456snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);
457}
458else
459{
460snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);
461}
462
463verbose("Kernel Cache file path (OS X 10.9): %s\n", kernelCacheFile);
464
465}
466else if ( MacOSVerCurrent >= MacOSVer2Int("10.10") && MacOSVerCurrent < MacOSVer2Int("10.11") )
467{
468// Yosemite prelink kernel cache file
469// for 10.10 and 10.11
470if (isOSXUpgrade)
471{
472snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", "/OS X Install Data/");
473}
474else if (isInstaller)
475{
476snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheInstallerNew);
477}
478else if (isRecoveryHD)
479{
480snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCacheRecoveryHD);
481}
482else
483{
484snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);
485}
486
487verbose("Kernel Cache file path (OS X 10.10): %s\n", kernelCacheFile);
488}
489else if ( MacOSVerCurrent >= MacOSVer2Int("10.11") )
490{
491// El Capitan on prelinked kernel cache file
492// for 10.10 and 10.11
493if (isOSXUpgrade)
494{
495if ( MacOSVerCurrent >= MacOSVer2Int("10.13") )
496{
497snprintf(kernelCacheFile, sizeof(kernelCacheFile),
498 "%sprelinkedkernel",
499"/macOS Install Data/Locked Files/Boot Files/");
500}
501else
502{
503snprintf(kernelCacheFile,
504sizeof(kernelCacheFile),
505"%sprelinkedkernel",
506"/OS X Install Data/");
507}
508
509}
510else if (isInstaller)
511{
512snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCacheInstallerNew);
513}
514else if (isRecoveryHD)
515{
516snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCacheRecoveryHD);
517}
518else
519{
520snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);
521}
522
523verbose("Kernel Cache file path (OS X 10.11 and newer): %s\n", kernelCacheFile);
524}
525}
526
527// Check if the kernel cache file exists
528ret = -1;
529
530if (gBootVolume->flags & kBVFlagBooter)
531{
532if (isRecoveryHD)
533{
534strncpy(kernelCachePath, "/com.apple.recovery.boot/prelinkedkernel", sizeof(kernelCachePath) );
535ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
536
537if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
538{
539strncpy(kernelCachePath, "/com.apple.recovery.boot/kernelcache", sizeof(kernelCachePath) );
540ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
541
542if ((flags & kFileTypeMask) != kFileTypeFlat)
543{
544ret = -1;
545}
546}
547}
548else if (isInstaller)
549{
550strncpy(kernelCachePath, "/.IABootFiles/prelinkedkernel", sizeof(kernelCachePath) );
551ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
552
553if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
554{
555strncpy(kernelCachePath, "/.IABootFiles/kernelcache", sizeof(kernelCachePath) );
556ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
557
558if ((flags & kFileTypeMask) != kFileTypeFlat)
559{
560ret = -1;
561}
562}
563}
564else if (isMacOSXUpgrade)
565{
566strncpy(kernelCachePath, "/Mac OS X Install Data/kernelcache", sizeof(kernelCachePath) );
567ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
568
569if ((flags & kFileTypeMask) != kFileTypeFlat)
570{
571ret = -1;
572}
573}
574else if (isOSXUpgrade)
575{
576strncpy(kernelCachePath,
577"/macOS Install Data/Locked Files/Boot Files/prelinkedkernel",
578sizeof(kernelCachePath) );
579ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
580
581if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
582{
583strncpy(kernelCachePath, "/OS X Install Data/prelinkedkernel", sizeof(kernelCachePath) );
584ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
585
586if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
587{
588strncpy(kernelCachePath, "/OS X Install Data/kernelcache", sizeof(kernelCachePath) );
589ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
590
591if ((flags & kFileTypeMask) != kFileTypeFlat)
592{
593ret = -1;
594}
595}
596}
597}
598else
599{
600snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);
601ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
602if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
603{
604snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);
605ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
606
607if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
608{
609snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);
610ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
611
612if ((flags & kFileTypeMask) != kFileTypeFlat)
613{
614ret = -1;
615}
616}
617}
618}
619}
620
621// If not found, use the original kernel cache path.
622if (ret == -1)
623{
624strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));
625ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
626
627if ((flags & kFileTypeMask) != kFileTypeFlat)
628{
629ret = -1;
630}
631}
632
633// Exit if kernel cache file wasn't found
634if (ret == -1)
635{
636DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);
637return -1;
638}
639
640if ( !isInstaller && !isRecoveryHD && !isMacOSXUpgrade && !isOSXUpgrade )
641{
642// Check if the kernel cache file is more recent (mtime)
643// than the kernel file or the S/L/E directory
644ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
645
646// Check if the kernel file is more recent than the cache file
647if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat) && (kerneltime > cachetime))
648{
649DBG("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n", bootInfo->bootFile, kernelCacheFile);
650return -1;
651}
652
653ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
654
655// Check if the S/L/E directory time is more recent than the cache file
656if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory) && (exttime > cachetime))
657{
658DBG("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n", kernelCacheFile);
659return -1;
660}
661}
662
663// Since the kernel cache file exists and is the most recent try to load it
664DBG("Loading Kernel Cache from: '%s%s' (%s)\n", gBootVolume->label, gBootVolume->altlabel, gBootVolume->type_name);
665
666ret = LoadThinFatFile(kernelCachePath, binary);
667return ret; // ret contain the length of the binary
668}
669
670//==========================================================================
671// This is the entrypoint from real-mode which functions exactly as it did
672// before. Multiboot does its own runtime initialization, does some of its
673// own things, and then calls common_boot.
674void boot(int biosdev)
675{
676// Enable A20 gate before accessing memory above 1Mb.
677// Note: malloc_init(), called via initialize_runtime() writes
678// memory >= 1Mb, so A20 must be enabled before calling it. - zenith432
679zeroBSS();
680enableA20();
681malloc_init(0, 0, 0, malloc_error);
682common_boot(biosdev);
683}
684
685//==========================================================================
686// The 'main' function for the booter. Called by boot0 when booting
687// from a block device, or by the network booter.
688//
689// arguments:
690// biosdev - Value passed from boot1/NBP to specify the device
691// that the booter was loaded from.
692//
693// If biosdev is kBIOSDevNetwork, then this function will return if
694// booting was unsuccessful. This allows the PXE firmware to try the
695// next boot device on its list.
696void common_boot(int biosdev)
697{
698bool quiet;
699bool firstRun = true;
700bool instantMenu;
701bool rescanPrompt;
702intstatus;
703unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
704unsigned intdenyBVFlags = kBVFlagEFISystem;
705
706// Set reminder to unload the PXE base code. Neglect to unload
707// the base code will result in a hang or kernel panic.
708gUnloadPXEOnExit = true;
709
710// Record the device that the booter was loaded from.
711gBIOSDev = biosdev & kBIOSDevMask;
712
713// Initialize boot-log
714initBooterLog();
715
716#if DEBUG_INTERRUPTS
717// Enable interrupts
718interruptsAvailable = SetupInterrupts();
719if (interruptsAvailable)
720{
721EnableInterrupts();
722}
723#endif
724
725// Initialize boot info structure.
726initKernBootStruct();
727
728// Setup VGA text mode.
729// Not sure if it is safe to call setVideoMode() before the
730// config table has been loaded. Call video_mode() instead.
731#if DEBUG
732printf("before video_mode\n");
733#endif
734video_mode( 2 ); // 80x25 mono text mode.
735#if DEBUG
736printf("after video_mode\n");
737#endif
738
739// Scan and record the system's hardware information.
740scan_platform();
741
742// First get info for boot volume.
743scanBootVolumes(gBIOSDev, 0);
744bvChain = getBVChainForBIOSDev(gBIOSDev);
745setBootGlobals(bvChain);
746
747// Load boot.plist config file
748status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);
749
750if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)
751{
752gBootMode |= kBootModeQuiet;
753}
754
755// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
756if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)
757{
758firstRun = false;
759}
760
761// Loading preboot ramdisk if exists.
762loadPrebootRAMDisk();
763
764// Disable rescan option by default
765gEnableCDROMRescan = false;
766
767// Enable it with Rescan=y in system config
768if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)&& gEnableCDROMRescan)
769{
770gEnableCDROMRescan = true;
771}
772
773// Disable rescanPrompt option by default
774rescanPrompt = false;
775
776// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
777if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))
778{
779gEnableCDROMRescan = promptForRescanOption();
780}
781
782// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
783if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)
784{
785gScanSingleDrive = true;
786}
787
788// Create a list of partitions on device(s).
789if (gScanSingleDrive)
790{
791scanBootVolumes(gBIOSDev, &bvCount);
792}
793else
794{
795scanDisks(gBIOSDev, &bvCount);
796}
797
798// Create a separated bvr chain using the specified filters.
799bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
800
801gBootVolume = selectBootVolume(bvChain);
802
803// Intialize module system
804init_module_system();
805
806#if DEBUG
807printf("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
808gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
809printf("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
810gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
811getchar();
812#endif
813
814useGUI = true;
815// Override useGUI default
816getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
817if (useGUI && initGUI())
818{
819// initGUI() returned with an error, disabling GUI.
820useGUI = false;
821}
822
823setBootGlobals(bvChain);
824
825// Parse args, load and start kernel.
826while (1)
827{
828booltryresume, tryresumedefault, forceresume;
829booluseKernelCache = true; // by default try to use the prelinked kernel
830const char*val;
831intlen, ret = -1;
832longflags;
833u_int32_tsleeptime, time;
834void*binary = (void *)kLoadAddr;
835
836charbootFile[sizeof(bootInfo->bootFile)];
837charbootFilePath[512];
838charkernelCacheFile[512];
839
840// Initialize globals.
841sysConfigValid = false;
842gErrors = false;
843
844status = getBootOptions(firstRun);
845firstRun = false;
846if (status == -1) continue;
847
848status = processBootOptions();
849// Status == 1 means to chainboot
850if ( status ==1 ) break;
851// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
852if ( status == -1 )
853{
854// gBootVolume == NULL usually means the user hit escape.
855if (gBootVolume == NULL)
856{
857freeFilteredBVChain(bvChain);
858
859if (gEnableCDROMRescan)
860{
861rescanBIOSDevice(gBIOSDev);
862}
863
864bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
865setBootGlobals(bvChain);
866setupDeviceList(&bootInfo->themeConfig);
867}
868continue;
869}
870
871// Other status (e.g. 0) means that we should proceed with boot.
872
873// Turn off any GUI elements
874if ( bootArgs->Video.v_display == GRAPHICS_MODE )
875{
876gui.devicelist.draw = false;
877gui.bootprompt.draw = false;
878gui.menu.draw = false;
879gui.infobox.draw = false;
880gui.logo.draw = false;
881drawBackground();
882updateVRAM();
883}
884
885if (platformCPUFeature(CPU_FEATURE_EM64T))
886{
887archCpuType = CPU_TYPE_X86_64;
888}
889else
890{
891archCpuType = CPU_TYPE_I386;
892}
893
894if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig))
895{
896if (strncmp(val, "x86_64", sizeof("x86_64") ) == 0)
897{
898archCpuType = CPU_TYPE_X86_64;
899}
900else if (strncmp(val, "i386", sizeof("i386") ) == 0)
901{
902archCpuType = CPU_TYPE_I386;
903}
904else
905{
906DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");
907}
908}
909
910if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig))
911{
912if (strncmp(val, "i386", sizeof("i386") ) == 0)
913{
914archCpuType = CPU_TYPE_I386;
915}
916}
917
918// Notify modules that we are attempting to boot
919execute_hook("PreBoot", NULL, NULL, NULL, NULL);
920
921//--------------------------------------
922getBoolForKey(kSkipKernelPatcher, &skipKernelPatcher, &bootInfo->chameleonConfig);
923
924// kernel patcher
925if (!skipKernelPatcher)
926{
927const char*key = NULL;
928int len = 0;
929setupKernelConfigFile("kernel.plist");
930
931// looking at command line for kernel patcher setting and override where needed
932if (getValueForKey(kKernelBooter_kexts, &key, &len, &bootInfo->chameleonConfig) && len > 0)
933{
934getBoolForKey(kKernelBooter_kexts, &KernelBooter_kexts, &bootInfo->chameleonConfig);
935}
936key = NULL; len = 0;
937
938if (getValueForKey(kKernelPm, &key, &len, &bootInfo->chameleonConfig) && len > 0)
939{
940getBoolForKey(kKernelPm, &KernelPm, &bootInfo->chameleonConfig);
941}
942key = NULL; len = 0;
943
944if (getValueForKey(kKernelLapicError, &key, &len, &bootInfo->chameleonConfig) && len > 0)
945{
946getBoolForKey(kKernelLapicError, &KernelLapicError, &bootInfo->chameleonConfig);
947}
948key = NULL; len = 0;
949
950if (getValueForKey(kKernelLapicVersion, &key, &len, &bootInfo->chameleonConfig) && len > 0)
951{
952getBoolForKey(kKernelLapicVersion, &KernelLapicVersion, &bootInfo->chameleonConfig);
953}
954key = NULL; len = 0;
955
956if (getValueForKey(kKernelHaswell, &key, &len, &bootInfo->chameleonConfig) && len > 0)
957{
958getBoolForKey(kKernelHaswell, &KernelHaswell, &bootInfo->chameleonConfig);
959}
960key = NULL; len = 0;
961
962if (getValueForKey(kKernelcpuFamily, &key, &len, &bootInfo->chameleonConfig) && len > 0)
963{
964getBoolForKey(kKernelcpuFamily, &KernelcpuFamily, &bootInfo->chameleonConfig);
965}
966key = NULL; len = 0;
967
968if (getValueForKey(kKernelSSE3, &key, &len, &bootInfo->chameleonConfig) && len > 0)
969{
970getBoolForKey(kKernelSSE3, &KernelSSE3, &bootInfo->chameleonConfig);
971}
972}
973
974#if KEXTPATCH_SUPPORT
975//-------------------------------------- kext patcher
976getBoolForKey(kSkipKextsPatcher, &skipKextsPatcher, &bootInfo->chameleonConfig);
977
978// kext patcher
979if (!skipKextsPatcher)
980{
981const char*key = NULL;
982intlen = 0;
983setupKextConfigFile("kexts.plist");
984
985// looking at command line for kext patcher setting and override where needed
986if (getValueForKey(kAICPMPatch, &key, &len, &bootInfo->chameleonConfig) && len > 0)
987{
988getBoolForKey(kAICPMPatch, &AICPMPatch, &bootInfo->chameleonConfig);
989}
990
991key = NULL;
992len = 0;
993if (getValueForKey(kNVIDIAWebDrv, &key, &len, &bootInfo->chameleonConfig) && len > 0)
994{
995getBoolForKey(kNVIDIAWebDrv, &NVIDIAWebDrv, &bootInfo->chameleonConfig);
996}
997
998key = NULL;
999len = 0;
1000if (getValueForKey(kOrangeIconFixSata, &key, &len, &bootInfo->chameleonConfig) && len > 0)
1001{
1002getBoolForKey(kOrangeIconFixSata, &OrangeIconFixSata, &bootInfo->chameleonConfig);
1003}
1004
1005key = NULL;
1006len = 0;
1007if (getValueForKey(kTrimEnablerSata, &key, &len, &bootInfo->chameleonConfig) && len > 0)
1008{
1009getBoolForKey(kTrimEnablerSata, &TrimEnablerSata, &bootInfo->chameleonConfig);
1010}
1011
1012key = NULL;
1013len = 0;
1014if (getValueForKey(kAppleRTCPatch, &key, &len, &bootInfo->chameleonConfig) && len > 0)
1015{
1016getBoolForKey(kAppleRTCPatch, &AppleRTCPatch, &bootInfo->chameleonConfig);
1017}
1018}
1019
1020// -------------------------------------- kext patcher
1021#endif
1022
1023if (gBootVolume->OSisInstaller)
1024{
1025isInstaller = true;
1026}
1027
1028if (gBootVolume->OSisMacOSXUpgrade)
1029{
1030isMacOSXUpgrade = true;
1031}
1032
1033if (gBootVolume->OSisOSXUpgrade)
1034{
1035isOSXUpgrade = true;
1036}
1037
1038if (gBootVolume->OSisRecovery)
1039{
1040isRecoveryHD = true;
1041}
1042
1043if ( !isRecoveryHD && !isInstaller && !isMacOSXUpgrade && !isOSXUpgrade )
1044{
1045if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig))
1046{
1047tryresume = true;
1048tryresumedefault = true;
1049}
1050else
1051{
1052tryresumedefault = false;
1053}
1054
1055if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig))
1056{
1057forceresume = false;
1058}
1059
1060if (forceresume)
1061{
1062tryresume = true;
1063tryresumedefault = false;
1064}
1065
1066while (tryresume)
1067{
1068const char *tmp;
1069BVRef bvr;
1070if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
1071val = "/private/var/vm/sleepimage";
1072
1073// Do this first to be sure that root volume is mounted
1074ret = GetFileInfo(0, val, &flags, &sleeptime);
1075
1076if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
1077break;
1078
1079// Can't check if it was hibernation Wake=y is required
1080if (bvr->modTime == 0 && tryresumedefault)
1081break;
1082
1083if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
1084break;
1085
1086if (!forceresume && ((sleeptime+3)<bvr->modTime))
1087{
1088#if DEBUG
1089printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
1090bvr->modTime-sleeptime);
1091#endif
1092break;
1093}
1094
1095HibernateBoot((char *)val);
1096break;
1097}
1098}
1099
1100if (!isRecoveryHD && !isInstaller && !isMacOSXUpgrade && !isOSXUpgrade )
1101{
1102getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
1103}
1104
1105if (useKernelCache)
1106{
1107do
1108{
1109// Determine the name of the Kernel Cache
1110if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))
1111{
1112if (val[0] == '\\')
1113{
1114len--;
1115val++;
1116}
1117/* FIXME: check len vs sizeof(kernelCacheFile) */
1118strlcpy(kernelCacheFile, val, len + 1);
1119}
1120else
1121{
1122kernelCacheFile[0] = 0; // Use default kernel cache file
1123}
1124
1125if (gOverrideKernel && kernelCacheFile[0] == 0)
1126{
1127DBG("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);
1128useKernelCache = false;
1129break;
1130}
1131
1132if (gMKextName[0] != 0)
1133{
1134DBG("Using a specific MKext Cache (%s), KernelCache will not be used\n", gMKextName);
1135useKernelCache = false;
1136break;
1137}
1138
1139if (gBootFileType != kBlockDeviceType)
1140{
1141useKernelCache = false;
1142}
1143
1144} while(0);
1145}
1146else
1147{
1148DBG("Kernel Cache using disabled by user.\n");
1149}
1150
1151do
1152{
1153if (useKernelCache)
1154{
1155ret = LoadKernelCache(kernelCacheFile, &binary);
1156if (ret >= 0)
1157{
1158break;
1159}
1160}
1161
1162bool bootFileWithDevice = false;
1163// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
1164if (strncmp(bootInfo->bootFile, "bt(", sizeof("bt(") ) == 0 ||
1165strncmp(bootInfo->bootFile, "hd(", sizeof("hd(") ) == 0 ||
1166strncmp(bootInfo->bootFile, "rd(", sizeof("rd(") ) == 0)
1167{
1168bootFileWithDevice = true;
1169}
1170
1171// bootFile must start with a / if it not start with a device name
1172if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')
1173{
1174if ( MacOSVerCurrent < MacOSVer2Int("10.10") ) // Mavericks and older
1175{
1176snprintf(bootFile, sizeof(bootFile), "/%s", bootInfo->bootFile); // append a leading /
1177}
1178else
1179{
1180// Yosemite and newer
1181snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathForYos"%s", bootInfo->bootFile); // Yosemite or El Capitan
1182}
1183}
1184else
1185{
1186strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));
1187}
1188
1189// Try to load kernel image from alternate locations on boot helper partitions.
1190ret = -1;
1191if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice)
1192{
1193snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);
1194ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
1195if (ret == -1)
1196{
1197snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);
1198ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
1199if (ret == -1)
1200{
1201snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);
1202ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
1203}
1204}
1205}
1206if (ret == -1)
1207{
1208// No alternate location found, using the original kernel image path.
1209strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));
1210}
1211
1212DBG("Loading kernel from: '%s' (%s)\n", gBootVolume->label, gBootVolume->type_name);
1213ret = LoadThinFatFile(bootFilePath, &binary);
1214if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
1215{
1216archCpuType = CPU_TYPE_I386;
1217ret = LoadThinFatFile(bootFilePath, &binary);
1218}
1219} while (0);
1220
1221clearActivityIndicator();
1222
1223#if DEBUG
1224printf("Pausing...");
1225sleep(8);
1226#endif
1227
1228if (ret <= 0)
1229{
1230printf("Can't find boot file: '%s'\n", bootFile);
1231sleep(1);
1232
1233if (gBootFileType == kNetworkDeviceType)
1234{
1235// Return control back to PXE. Don't unload PXE base code.
1236gUnloadPXEOnExit = false;
1237break;
1238}
1239pause();
1240
1241}
1242else
1243{
1244/* Won't return if successful. */
1245ret = ExecKernel(binary);
1246}
1247}
1248
1249// chainboot
1250if (status == 1)
1251{
1252// if we are already in graphics-mode,
1253if (getVideoMode() == GRAPHICS_MODE)
1254{
1255setVideoMode( VGA_TEXT_MODE ); // switch back to text mode.
1256}
1257}
1258
1259if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
1260{
1261nbpUnloadBaseCode();
1262}
1263
1264#if DEBUG_INTERRUPTS
1265if (interruptsAvailable)
1266{
1267DisableInterrupts();
1268}
1269#endif
1270
1271}
1272
1273// =========================================================================
1274//
1275void setupBooterArgs()
1276{
1277bool KPRebootOption= false; // I don't want this by default ( It makes me angry because I do not see the reason for the panic)+
1278//bool HiDPIOption= false; // (Disabled by default) 10.8+
1279//bool FlagBlackOption= false; // (Disabled by default) 10.10+
1280
1281// OS X Lion 10.7
1282if ( MacOSVerCurrent >= MacOSVer2Int("10.7") ) // Lion and Up!
1283{
1284// Pike R. Alpha: Adding a 16 KB log space.
1285bootArgs->performanceDataSize= 0;
1286bootArgs->performanceDataStart= 0;
1287
1288// Pike R. Alpha: AppleKeyStore.kext
1289bootArgs->keyStoreDataSize= 0;
1290bootArgs->keyStoreDataStart= 0;
1291
1292bootArgs->bootMemSize= 0;
1293bootArgs->bootMemStart= 0;
1294}
1295
1296// OS X Mountain Lion 10.8
1297if ( MacOSVerCurrent >= MacOSVer2Int("10.8") ) // Mountain Lion and Up!
1298{
1299// cparm
1300getBoolForKey(kRebootOnPanic, &KPRebootOption, &bootInfo->chameleonConfig);
1301if ( KPRebootOption )
1302{
1303bootArgs->flags |= kBootArgsFlagRebootOnPanic;
1304}
1305
1306// cparm
1307getBoolForKey(kEnableHiDPI, &HiDPIOption, &bootInfo->chameleonConfig);
1308
1309if ( HiDPIOption )
1310{
1311bootArgs->flags |= kBootArgsFlagHiDPI;
1312}
1313}
1314
1315// OS X Yosemite 10.10
1316if ( MacOSVerCurrent >= MacOSVer2Int("10.10") ) // Yosemite and Up!
1317{
1318// Pike R. Alpha
1319getBoolForKey(kBlackMode, &FlagBlackOption, &bootInfo->chameleonConfig);
1320if ( FlagBlackOption )
1321{
1322// bootArgs->flags |= kBootArgsFlagBlack;
1323bootArgs->flags |= kBootArgsFlagBlackBg; // Micky1979
1324}
1325}
1326
1327// OS X El Capitan 10.11
1328if ( MacOSVerCurrent >= MacOSVer2Int("10.11") ) // El Capitan, Sierra and High Sierra!
1329{
1330// ErmaC
1331verbose("\n");
1332intcsrValue;
1333
1334/*
1335 * A special BootArgs flag "kBootArgsFlagCSRBoot"
1336 * is set in the Recovery or Installation environment.
1337 * This flag is kind of overkill by turning off all the protections
1338 */
1339
1340if (isRecoveryHD || isInstaller || isOSXUpgrade || isMacOSXUpgrade)
1341{
1342// SIP can be controlled with or without FileNVRAM.kext (Pike R. Alpha)
1343bootArgs->flags|=(kBootArgsFlagCSRActiveConfig + kBootArgsFlagCSRConfigMode + kBootArgsFlagCSRBoot + kBootArgsFlagInstallUI);
1344}
1345else
1346{
1347bootArgs->flags|= kBootArgsFlagCSRActiveConfig;
1348}
1349
1350// Set limit to 8bit
1351if ( getIntForKey(kCsrActiveConfig, &csrValue, &bootInfo->chameleonConfig) && (csrValue >= 0 && csrValue <= 255) )
1352{
1353bootArgs->csrActiveConfig= csrValue;
1354csrInfo(csrValue, 1);
1355}
1356else
1357{
1358// zenith432
1359bootArgs->csrActiveConfig= 0x67;
1360csrInfo(0x67, 0);
1361
1362}
1363
1364
1365// ===============================================================================
1366
1367bootArgs->csrCapabilities= CSR_VALID_CAPABILITIES; // CSR_VALID_FLAGS
1368bootArgs->boot_SMC_plimit= 0;
1369bootArgs->bootProgressMeterStart= 0;
1370bootArgs->bootProgressMeterEnd= 0;
1371}
1372}
1373
1374// =========================================================================
1375// ErmaC
1376void csrInfo(int csrValue, bool custom)
1377{
1378#define CSR_BITS 0x080
1379
1380int mask = CSR_BITS;
1381verbose("System Integrity Protection status: %s ", (csrValue == 0) ? "enabled":"disabled");
1382verbose("(%s Configuration).\nCsrActiveConfig = 0x%02X (", custom ? "Custom":"Default", csrValue);
1383
1384// Display integer number into binary using bitwise operator
1385((csrValue & 0x020) == 0) ? verbose("0"): verbose("1");
1386while (mask != 0)
1387{
1388( ((csrValue & mask) == 0) ? verbose("0"): verbose("1") );
1389mask = mask >> 1; // Right Shift
1390}
1391verbose(")\n");
1392if (csrValue != 0)
1393{
1394verbose("\nConfiguration:\n");
1395verbose("\tKext Signing: %s\n", ((csrValue & 0x001) == 0) ? "enabled" : "disabled"); /* (1 << 0) Allow untrusted kexts */
1396verbose("\tFilesystem Protections: %s\n", ((csrValue & 0x002) == 0) ? "enabled" : "disabled"); /* (1 << 1) Allow unrestricted file system. */
1397verbose("\tTask for PID: %s\n", ((csrValue & 0x004) == 0) ? "enabled" : "disabled"); /* (1 << 2) */
1398verbose("\tDebugging Restrictions: %s\n", ((csrValue & 0x008) == 0) ? "enabled" : "disabled"); /* (1 << 3) */
1399verbose("\tApple Internal: %s\n", ((csrValue & 0x010) == 0) ? "enabled" : "disabled"); /* (1 << 4) */
1400verbose("\tDTrace Restrictions: %s\n", ((csrValue & 0x020) == 0) ? "enabled" : "disabled"); /* (1 << 5) Allow unrestricted dtrace */
1401verbose("\tNVRAM Protections: %s\n", ((csrValue & 0x040) == 0) ? "enabled" : "disabled"); /* (1 << 6) Allow unrestricted NVRAM */
1402verbose("\tDevice configuration: %s\n", ((csrValue & 0x080) == 0) ? "enabled" : "disabled"); /* (1 << 7) Allow device configuration */
1403verbose("\tBaseSystem Verification: %s\n", ((csrValue & 0x100) == 0) ? "enabled" : "disabled"); /* (1 << 8) Allow any Recovery OS */
1404//verbose("\tApple Internal HS: %s\n", ((csrValue & 0x200) == 0) ? "enabled" : "disabled"); /* (1 << 9) Allow Apple Internal HS */
1405}
1406verbose("\n");
1407}
1408
1409// =========================================================================
1410
1411/*!
1412Selects a new BIOS device, taking care to update the global state appropriately.
1413 */
1414/*
1415static void selectBiosDevice(void)
1416{
1417struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
1418CacheReset();
1419diskFreeMap(oldMap);
1420oldMap = NULL;
1421
1422int dev = selectAlternateBootDevice(gBIOSDev);
1423
1424BVRef bvchain = scanBootVolumes(dev, 0);
1425BVRef bootVol = selectBootVolume(bvchain);
1426gBootVolume = bootVol;
1427setRootVolume(bootVol);
1428gBIOSDev = dev;
1429}
1430*/
1431
1432// =========================================================================
1433bool checkOSVersion(const char *version)
1434{
1435if ( '\0' != version[4] )
1436{
1437return !memcmp(&gMacOSVersion[0], &version[0], 5);
1438}
1439else
1440{
1441return !memcmp(&gMacOSVersion[0], &version[0], 4);
1442}
1443}
1444
1445uint32_t getMacOSVerCurrent()
1446{
1447MacOSVerCurrent = MacOSVer2Int(gBootVolume->OSVersion);
1448return MacOSVerCurrent;
1449}
1450
1451// =========================================================================
1452unsigned long Adler32(unsigned char *buf, long len)
1453{
1454#define BASE 65521L /* largest prime smaller than 65536 */
1455#define NMAX 5000
1456// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
1457
1458#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
1459#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
1460#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
1461#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
1462#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
1463
1464int k;
1465
1466unsigned long s1 = 1; // adler & 0xffff;
1467unsigned long s2 = 0; // (adler >> 16) & 0xffff;
1468unsigned long result;
1469
1470while (len > 0)
1471{
1472k = len < NMAX ? len : NMAX;
1473len -= k;
1474while (k >= 16)
1475{
1476DO16(buf);
1477buf += 16;
1478k -= 16;
1479}
1480
1481if (k != 0)
1482{
1483do
1484{
1485s1 += *buf++;
1486s2 += s1;
1487} while (--k);
1488}
1489
1490s1 %= BASE;
1491s2 %= BASE;
1492}
1493
1494result = (s2 << 16) | s1;
1495
1496return OSSwapHostToBigInt32(result);
1497}
1498

Archive Download this file

Revision: HEAD