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

Archive Download this file

Revision: 2879