Chameleon

Chameleon Svn Source Tree

Root/branches/zenith432/i386/boot2/boot.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the "License").You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25/*
26 * Mach Operating System
27 * Copyright (c) 1990 Carnegie-Mellon University
28 * Copyright (c) 1989 Carnegie-Mellon University
29 * All rights reserved. The CMU software License Agreement specifies
30 * the terms and conditions for use and redistribution.
31 *
32 * INTEL CORPORATION PROPRIETARY INFORMATION
33 *
34 * This software is supplied under the terms of a license agreement or
35 * nondisclosure agreement with Intel Corporation and may not be copied
36 * nor disclosed except in accordance with the terms of that agreement.
37 *
38 *Copyright 1988, 1989 by Intel Corporation
39 *
40 *
41 * Copyright 1993 NeXT Computer, Inc.
42 * All rights reserved.
43 *
44 * Completely reworked by Sam Streeper (sam_s@NeXT.com)
45 * Reworked again by Curtis Galloway (galloway@NeXT.com)
46 */
47
48#include "boot.h"
49#include "bootstruct.h"
50#include "fake_efi.h"
51#include "sl.h"
52#include "libsa.h"
53#include "ramdisk.h"
54#include "gui.h"
55#include "platform.h"
56#include "modules.h"
57#include "device_tree.h"
58
59#ifndef DEBUG_BOOT2
60#define DEBUG_BOOT2 0
61#endif
62
63#ifndef DEBUG_INTERRUPTS
64#define DEBUG_INTERRUPTS 0
65#endif
66
67#if DEBUG_BOOT2
68#define DBG(x...)printf(x)
69#else
70#define DBG(x...)msglog(x)
71#endif
72
73/*
74 * How long to wait (in seconds) to load the
75 * kernel after displaying the "boot:" prompt.
76 */
77#define kBootErrorTimeout 5
78
79boolgOverrideKernel;
80boolgEnableCDROMRescan;
81boolgScanSingleDrive;
82booluseGUI;
83
84#if DEBUG_INTERRUPTS
85static intinterruptsAvailable = 0;
86#endif
87
88static boolgUnloadPXEOnExit = false;
89
90static chargCacheNameAdler[64 + 256];
91char*gPlatformName = gCacheNameAdler;
92
93chargRootDevice[ROOT_DEVICE_SIZE];
94chargMKextName[512];
95intbvCount = 0;
96intgDeviceCount = 0;
97//intmenucount = 0;
98longgBootMode; /* defaults to 0 == kBootModeNormal */
99
100BVRefbvr;
101BVRefmenuBVR;
102BVRefbvChain;
103
104static unsigned longAdler32(unsigned char *buffer, long length);
105//static voidselectBiosDevice(void);
106
107/** options.c **/
108extern char *msgbuf;
109void showTextBuffer(char *buf, int size);
110
111//==========================================================================
112// Zero the BSS.
113
114static void zeroBSS(void)
115{
116extern char bss_start __asm("section$start$__DATA$__bss");
117extern char bss_end __asm("section$end$__DATA$__bss");
118extern char common_start __asm("section$start$__DATA$__common");
119extern char common_end __asm("section$end$__DATA$__common");
120
121bzero(&bss_start, (&bss_end - &bss_start));
122bzero(&common_start, (&common_end - &common_start));
123}
124
125//==========================================================================
126// Malloc error function
127
128static void malloc_error(char *addr, size_t size, const char *file, int line)
129{
130stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",
131 (unsigned)addr, (unsigned)size, file, line);
132}
133
134//==========================================================================
135//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
136//
137void initialize_runtime(void)
138{
139zeroBSS();
140malloc_init(0, 0, 0, malloc_error);
141}
142
143//==========================================================================
144// ExecKernel - Load the kernel image (mach-o) and jump to its entry point.
145
146static int ExecKernel(void *binary)
147{
148intret;
149entry_tkernelEntry;
150
151bootArgs->kaddr = bootArgs->ksize = 0;
152// ===============================================================================
153
154 // OS X Mountain Lion 10.8
155if ( MacOSVerCurrent >= MacOSVer2Int("10.8") ) // Mountain Lion and Up!
156{
157// cparm
158bool KPRebootOption= false;
159bool HiDPIOption= false;
160getBoolForKey(kRebootOnPanic, &KPRebootOption, &bootInfo->chameleonConfig);
161if ( KPRebootOption )
162{
163bootArgs->flags |= kBootArgsFlagRebootOnPanic;
164}
165
166// cparm
167getBoolForKey(kEnableHiDPI, &HiDPIOption, &bootInfo->chameleonConfig);
168
169if ( HiDPIOption )
170{
171bootArgs->flags |= kBootArgsFlagHiDPI;
172}
173}
174
175// OS X Yosemite 10.10
176if ( MacOSVerCurrent >= MacOSVer2Int("10.10") ) // Yosemite and Up!
177{
178// Pike R. Alpha
179bool FlagBlackOption= false;
180getBoolForKey(kBlackMode, &FlagBlackOption, &bootInfo->chameleonConfig);
181if ( FlagBlackOption )
182{
183//bootArgs->flags |= kBootArgsFlagBlack;
184bootArgs->flags |= kBootArgsFlagBlackBg; // Micky1979
185}
186}
187
188// OS X El Capitan 10.11
189if ( MacOSVerCurrent >= MacOSVer2Int("10.11") ) // El Capitan and Up!
190{
191// ErmaC
192intcrsValue;
193
194#if 0
195/*
196 * A special BootArgs flag "kBootArgsFlagCSRBoot"
197 * is set in the Recovery or Installation environment.
198 * This flag is kind of overkill by turning off all the protections
199 */
200
201if (isRecoveryHD)
202{
203// SIP can be controlled with or without FileNVRAM.kext (Pike R. Alpha)
204bootArgs->flags|=(kBootArgsFlagCSRActiveConfig + kBootArgsFlagCSRConfigMode + kBootArgsFlagCSRBoot);
205}
206#endif
207
208bootArgs->flags|= kBootArgsFlagCSRActiveConfig;
209
210// Set limit to 7bit
211if ( getIntForKey(KCsrActiveConfig, &crsValue, &bootInfo->chameleonConfig) && (crsValue >= 0 && crsValue <= 127) )
212{
213bootArgs->csrActiveConfig= crsValue;
214}
215else
216{
217// zenith432
218bootArgs->csrActiveConfig= 0x67;
219}
220bootArgs->csrCapabilities= CSR_VALID_FLAGS;
221bootArgs->boot_SMC_plimit= 0;
222 }
223
224// ===============================================================================
225
226execute_hook("ExecKernel", (void *)binary, NULL, NULL, NULL);
227
228ret = DecodeKernel(binary,
229 &kernelEntry,
230 (char **) &bootArgs->kaddr,
231 (int *)&bootArgs->ksize);
232
233if ( ret != 0 )
234{
235printf("Decoding kernel failed.\n");
236pause();
237return ret;
238}
239
240// Reserve space for boot args
241reserveKernBootStruct();
242
243// Notify modules that the kernel has been decoded
244execute_hook("DecodedKernel", (void *)binary, (void *)bootArgs->kaddr, (void *)bootArgs->ksize, NULL);
245
246setupFakeEfi();
247
248// Load boot drivers from the specifed root path.
249//if (!gHaveKernelCache)
250{
251LoadDrivers("/");
252}
253
254execute_hook("DriversLoaded", (void *)binary, NULL, NULL, NULL);
255
256clearActivityIndicator();
257
258if (gErrors)
259{
260printf("Errors encountered while starting up the computer.\n");
261printf("Pausing %d seconds...\n", kBootErrorTimeout);
262sleep(kBootErrorTimeout);
263}
264
265md0Ramdisk();
266
267// Cleanup the PXE base code.
268
269if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit )
270{
271if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
272{
273printf("nbpUnloadBaseCode error %d\n", (int)ret);
274sleep(2);
275}
276}
277
278bool dummyVal;
279if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal)
280{
281showTextBuffer(msgbuf, strlen(msgbuf));
282}
283
284usb_loop();
285
286#if DEBUG
287if (interruptsAvailable) ShowInterruptCounters();
288#endif
289
290// If we were in text mode, switch to graphics mode.
291// This will draw the boot graphics unless we are in
292// verbose mode.
293if (gVerboseMode)
294{
295setVideoMode(GRAPHICS_MODE, 0);
296}
297else
298{
299drawBootGraphics();
300}
301
302DBG("Starting Darwin/%s [%s]\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64", gDarwinBuildVerStr);
303DBG("Boot Args: %s\n", bootArgs->CommandLine);
304
305setupBooterLog();
306
307finalizeBootStruct();
308
309// Jump to kernel's entry point. There's no going back now.
310if ( MacOSVerCurrent < MacOSVer2Int("10.7") )
311{
312// Notify modules that the kernel is about to be started
313execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgsLegacy, NULL, NULL);
314
315startprog( kernelEntry, bootArgsLegacy );
316}
317else
318{
319// Notify modules that the kernel is about to be started
320execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgs, NULL, NULL);
321
322// Masking out so that Lion doesn't doublefault
323outb(0x21, 0xff);/* Maskout all interrupts Pic1 */
324outb(0xa1, 0xff);/* Maskout all interrupts Pic2 */
325
326startprog( kernelEntry, bootArgs );
327}
328
329// Not reached
330__builtin_unreachable();
331}
332
333//==========================================================================
334// LoadKernelCache - Try to load Kernel Cache.
335// return the length of the loaded cache file or -1 on error
336long LoadKernelCache(const char *cacheFile, void **binary)
337{
338charkernelCacheFile[512];
339charkernelCachePath[512];
340longflags, ret=-1;
341unsigned longadler32 = 0;
342u_int32_t time, cachetime, kerneltime, exttime;
343
344if((gBootMode & kBootModeSafe) != 0)
345{
346DBG("Kernel Cache ignored.\n");
347return -1;
348}
349
350// Use specify kernel cache file if not empty
351if (cacheFile[0] != 0)
352{
353strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));
354verbose("Specified kernel cache file path: %s\n", cacheFile);
355}
356else
357{
358// Leopard prelink kernel cache file
359if ( MacOSVerCurrent >= MacOSVer2Int("10.4") && MacOSVerCurrent <= MacOSVer2Int("10.5") ) // OSX is 10.4 or 10.5
360{
361// Reset cache name.
362bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
363snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64, "%s,%s", gRootDevice, bootInfo->bootFile);
364adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
365snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);
366verbose("Reseted kernel cache file path: %s\n", kernelCacheFile);
367
368}
369// Snow Leopard prelink kernel cache file
370else if ( MacOSVerCurrent >= MacOSVer2Int("10.6") && MacOSVerCurrent < MacOSVer2Int("10.7") )
371{
372snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",
373(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");
374
375intlnam = strlen(kernelCacheFile) + 9; //with adler32
376char*name;
377u_int32_tprev_time = 0;
378
379struct dirstuff *cacheDir = opendir(kDefaultCachePathSnow);
380
381/* TODO: handle error? */
382if (cacheDir)
383{
384while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
385{
386if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
387&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))
388{
389snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);
390prev_time = time;
391}
392}
393verbose("Kernel Cache file path (Mac OS X 10.6): %s\n", kernelCacheFile);
394}
395closedir(cacheDir);
396}
397else if ( MacOSVerCurrent >= MacOSVer2Int("10.7") && MacOSVerCurrent < MacOSVer2Int("10.10") )
398{
399// Lion, Mountain Lion and Mavericks prelink kernel cache file
400// for 10.7 10.8 10.9
401snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);
402verbose("Kernel Cache file path (Mac OS X 10.7 and newer): %s\n", kernelCacheFile);
403
404}
405else
406{
407// Yosemite and El Capitan prelink kernel cache file
408// for 10.10 10.11
409snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%sprelinkedkernel", kDefaultCachePathYosemite);
410verbose("Kernel Cache file path (Mac OS X 10.10 and newer): %s\n", kernelCacheFile);
411}
412}
413
414// Check if the kernel cache file exists
415ret = -1;
416
417if (gBootVolume->flags & kBVFlagBooter)
418{
419snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);
420ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
421
422if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
423{
424snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);
425ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
426
427if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
428{
429snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);
430ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
431
432if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
433{
434strncpy(kernelCachePath, "/com.apple.recovery.boot/kernelcache", sizeof kernelCachePath);
435ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
436
437if ((flags & kFileTypeMask) != kFileTypeFlat)
438{
439ret = -1;
440}
441}
442}
443}
444}
445
446// If not found, use the original kernel cache path.
447if (ret == -1)
448{
449strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));
450ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
451
452if ((flags & kFileTypeMask) != kFileTypeFlat)
453{
454ret = -1;
455}
456}
457
458// Exit if kernel cache file wasn't found
459if (ret == -1)
460{
461DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);
462return -1;
463}
464
465// Check if the kernel cache file is more recent (mtime)
466// than the kernel file or the S/L/E directory
467ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
468
469// Check if the kernel file is more recent than the cache file
470if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat) && (kerneltime > cachetime))
471{
472DBG("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n", bootInfo->bootFile, kernelCacheFile);
473return -1;
474}
475
476ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
477
478// Check if the S/L/E directory time is more recent than the cache file
479if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory) && (exttime > cachetime))
480{
481DBG("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n", kernelCacheFile);
482return -1;
483}
484
485// Since the kernel cache file exists and is the most recent try to load it
486DBG("Loading Kernel Cache from: '%s%s' (%s)\n", gBootVolume->label, gBootVolume->altlabel, gBootVolume->type_name);
487
488ret = LoadThinFatFile(kernelCachePath, binary);
489return ret; // ret contain the length of the binary
490}
491
492//==========================================================================
493// This is the entrypoint from real-mode which functions exactly as it did
494// before. Multiboot does its own runtime initialization, does some of its
495// own things, and then calls common_boot.
496void boot(int biosdev)
497{
498initialize_runtime();
499// Enable A20 gate before accessing memory above 1Mb.
500enableA20();
501common_boot(biosdev);
502}
503
504//==========================================================================
505// The 'main' function for the booter. Called by boot0 when booting
506// from a block device, or by the network booter.
507//
508// arguments:
509// biosdev - Value passed from boot1/NBP to specify the device
510// that the booter was loaded from.
511//
512// If biosdev is kBIOSDevNetwork, then this function will return if
513// booting was unsuccessful. This allows the PXE firmware to try the
514// next boot device on its list.
515void common_boot(int biosdev)
516{
517bool quiet;
518bool firstRun = true;
519bool instantMenu;
520bool rescanPrompt;
521intstatus;
522unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
523unsigned intdenyBVFlags = kBVFlagEFISystem;
524
525// Set reminder to unload the PXE base code. Neglect to unload
526// the base code will result in a hang or kernel panic.
527gUnloadPXEOnExit = true;
528
529// Record the device that the booter was loaded from.
530gBIOSDev = biosdev & kBIOSDevMask;
531
532// Initialize boot-log
533initBooterLog();
534
535#if DEBUG_INTERRUPTS
536// Enable interrupts
537interruptsAvailable = SetupInterrupts();
538if (interruptsAvailable)
539{
540EnableInterrupts();
541}
542#endif
543
544// Initialize boot info structure.
545initKernBootStruct();
546
547// Setup VGA text mode.
548// Not sure if it is safe to call setVideoMode() before the
549// config table has been loaded. Call video_mode() instead.
550#if DEBUG
551printf("before video_mode\n");
552#endif
553video_mode( 2 ); // 80x25 mono text mode.
554#if DEBUG
555printf("after video_mode\n");
556#endif
557
558// Scan and record the system's hardware information.
559scan_platform();
560
561// First get info for boot volume.
562scanBootVolumes(gBIOSDev, 0);
563bvChain = getBVChainForBIOSDev(gBIOSDev);
564setBootGlobals(bvChain);
565
566// Load boot.plist config file
567status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);
568
569if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)
570{
571gBootMode |= kBootModeQuiet;
572}
573
574// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
575if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)
576{
577firstRun = false;
578}
579
580// Loading preboot ramdisk if exists.
581loadPrebootRAMDisk();
582
583// Disable rescan option by default
584gEnableCDROMRescan = false;
585
586// Enable it with Rescan=y in system config
587if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)&& gEnableCDROMRescan)
588{
589gEnableCDROMRescan = true;
590}
591
592// Disable rescanPrompt option by default
593rescanPrompt = false;
594
595// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
596if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))
597{
598gEnableCDROMRescan = promptForRescanOption();
599}
600
601// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
602if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)
603{
604gScanSingleDrive = true;
605}
606
607// Create a list of partitions on device(s).
608if (gScanSingleDrive)
609{
610scanBootVolumes(gBIOSDev, &bvCount);
611}
612else
613{
614scanDisks(gBIOSDev, &bvCount);
615}
616
617// Create a separated bvr chain using the specified filters.
618bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
619
620gBootVolume = selectBootVolume(bvChain);
621
622// Intialize module system
623init_module_system();
624
625#if DEBUG
626printf("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
627gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
628printf("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
629gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
630getchar();
631#endif
632
633useGUI = true;
634// Override useGUI default
635getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
636if (useGUI && initGUI())
637{
638// initGUI() returned with an error, disabling GUI.
639useGUI = false;
640}
641
642setBootGlobals(bvChain);
643
644// Parse args, load and start kernel.
645while (1)
646{
647booltryresume, tryresumedefault, forceresume;
648booluseKernelCache = true; // by default try to use the prelinked kernel
649const char*val;
650intlen, ret = -1;
651longflags;
652u_int32_tsleeptime, time;
653void*binary = (void *)kLoadAddr;
654
655charbootFile[sizeof(bootInfo->bootFile)];
656charbootFilePath[512];
657charkernelCacheFile[512];
658
659// Initialize globals.
660sysConfigValid = false;
661gErrors = false;
662
663status = getBootOptions(firstRun);
664firstRun = false;
665if (status == -1) continue;
666
667status = processBootOptions();
668// Status == 1 means to chainboot
669if ( status ==1 ) break;
670// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
671if ( status == -1 )
672{
673// gBootVolume == NULL usually means the user hit escape.
674if (gBootVolume == NULL)
675{
676freeFilteredBVChain(bvChain);
677
678if (gEnableCDROMRescan)
679rescanBIOSDevice(gBIOSDev);
680
681bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
682setBootGlobals(bvChain);
683setupDeviceList(&bootInfo->themeConfig);
684}
685continue;
686}
687
688// Other status (e.g. 0) means that we should proceed with boot.
689
690// Turn off any GUI elements
691if ( bootArgs->Video.v_display == GRAPHICS_MODE )
692{
693gui.devicelist.draw = false;
694gui.bootprompt.draw = false;
695gui.menu.draw = false;
696gui.infobox.draw = false;
697gui.logo.draw = false;
698drawBackground();
699updateVRAM();
700}
701
702if (platformCPUFeature(CPU_FEATURE_EM64T))
703{
704archCpuType = CPU_TYPE_X86_64;
705}
706else
707{
708archCpuType = CPU_TYPE_I386;
709}
710
711if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig))
712{
713if (strncmp(val, "x86_64", sizeof("x86_64") ) == 0)
714{
715archCpuType = CPU_TYPE_X86_64;
716}
717else if (strncmp(val, "i386", sizeof("i386") ) == 0)
718{
719archCpuType = CPU_TYPE_I386;
720}
721else
722{
723DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");
724}
725}
726
727if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig))
728{
729if (strncmp(val, "i386", sizeof("i386") ) == 0)
730{
731archCpuType = CPU_TYPE_I386;
732}
733}
734
735// Notify modules that we are attempting to boot
736execute_hook("PreBoot", NULL, NULL, NULL, NULL);
737
738if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig))
739{
740tryresume = true;
741tryresumedefault = true;
742}
743else
744{
745tryresumedefault = false;
746}
747
748if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig))
749{
750forceresume = false;
751}
752
753if (forceresume)
754{
755tryresume = true;
756tryresumedefault = false;
757}
758
759while (tryresume)
760{
761const char *tmp;
762BVRef bvr;
763if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
764val = "/private/var/vm/sleepimage";
765
766// Do this first to be sure that root volume is mounted
767ret = GetFileInfo(0, val, &flags, &sleeptime);
768
769if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
770break;
771
772// Can't check if it was hibernation Wake=y is required
773if (bvr->modTime == 0 && tryresumedefault)
774break;
775
776if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
777break;
778
779if (!forceresume && ((sleeptime+3)<bvr->modTime))
780{
781#if DEBUG
782printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
783bvr->modTime-sleeptime);
784#endif
785break;
786}
787
788HibernateBoot((char *)val);
789break;
790}
791
792getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
793if (useKernelCache)
794{
795do
796{
797// Determine the name of the Kernel Cache
798if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))
799{
800if (val[0] == '\\')
801{
802len--;
803val++;
804}
805/* FIXME: check len vs sizeof(kernelCacheFile) */
806strlcpy(kernelCacheFile, val, len + 1);
807}
808else
809{
810kernelCacheFile[0] = 0; // Use default kernel cache file
811}
812
813if (gOverrideKernel && kernelCacheFile[0] == 0)
814{
815DBG("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);
816useKernelCache = false;
817break;
818}
819
820if (gMKextName[0] != 0)
821{
822DBG("Using a specific MKext Cache (%s), KernelCache will not be used\n", gMKextName);
823useKernelCache = false;
824break;
825}
826
827if (gBootFileType != kBlockDeviceType)
828{
829useKernelCache = false;
830}
831
832} while(0);
833}
834else
835{
836DBG("Kernel Cache using disabled by user.\n");
837}
838
839do
840{
841if (useKernelCache)
842{
843ret = LoadKernelCache(kernelCacheFile, &binary);
844if (ret >= 0)
845{
846break;
847}
848}
849
850bool bootFileWithDevice = false;
851// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
852if (strncmp(bootInfo->bootFile, "bt(", sizeof("bt(") ) == 0 ||
853strncmp(bootInfo->bootFile, "hd(", sizeof("hd(") ) == 0 ||
854strncmp(bootInfo->bootFile, "rd(", sizeof("rd(") ) == 0)
855{
856bootFileWithDevice = true;
857}
858
859// bootFile must start with a / if it not start with a device name
860if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')
861{
862if ( !YOSEMITE || !ELCAPITAN ) //Is not Yosemite 10.10 or El Capitan 10.11
863{
864snprintf(bootFile, sizeof(bootFile), "/%s", bootInfo->bootFile); // append a leading /
865}
866else
867{
868snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathForYos"%s", bootInfo->bootFile); // Yosemite or El Capitan
869}
870}
871else
872{
873strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));
874}
875
876// Try to load kernel image from alternate locations on boot helper partitions.
877ret = -1;
878if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice)
879{
880snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);
881ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
882if (ret == -1)
883{
884snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);
885ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
886if (ret == -1)
887{
888snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);
889ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
890}
891}
892}
893if (ret == -1)
894{
895// No alternate location found, using the original kernel image path.
896strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));
897}
898
899DBG("Loading kernel from: '%s' (%s)\n", gBootVolume->label, gBootVolume->type_name);
900ret = LoadThinFatFile(bootFilePath, &binary);
901if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
902{
903archCpuType = CPU_TYPE_I386;
904ret = LoadThinFatFile(bootFilePath, &binary);
905}
906} while (0);
907
908clearActivityIndicator();
909
910#if DEBUG
911printf("Pausing...");
912sleep(8);
913#endif
914
915if (ret <= 0)
916{
917printf("Can't find boot file: '%s'\n", bootFile);
918sleep(1);
919
920if (gBootFileType == kNetworkDeviceType)
921{
922// Return control back to PXE. Don't unload PXE base code.
923gUnloadPXEOnExit = false;
924break;
925}
926pause();
927
928}
929else
930{
931/* Won't return if successful. */
932ret = ExecKernel(binary);
933}
934}
935
936// chainboot
937if (status == 1)
938{
939// if we are already in graphics-mode,
940if (getVideoMode() == GRAPHICS_MODE)
941{
942setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode.
943}
944}
945
946if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
947{
948nbpUnloadBaseCode();
949}
950
951#if DEBUG_INTERRUPTS
952if (interruptsAvailable)
953{
954DisableInterrupts();
955}
956#endif
957
958}
959
960
961// =========================================================================
962
963/*!
964Selects a new BIOS device, taking care to update the global state appropriately.
965 */
966/*
967static void selectBiosDevice(void)
968{
969struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
970CacheReset();
971diskFreeMap(oldMap);
972oldMap = NULL;
973
974int dev = selectAlternateBootDevice(gBIOSDev);
975
976BVRef bvchain = scanBootVolumes(dev, 0);
977BVRef bootVol = selectBootVolume(bvchain);
978gBootVolume = bootVol;
979setRootVolume(bootVol);
980gBIOSDev = dev;
981}
982*/
983
984// =========================================================================
985bool checkOSVersion(const char *version)
986{
987if ( '\0' != version[4] )
988{
989return !memcmp(&gMacOSVersion[0], &version[0], 5);
990}
991else
992{
993return !memcmp(&gMacOSVersion[0], &version[0], 4);
994}
995}
996
997uint32_t getMacOSVerCurrent()
998{
999MacOSVerCurrent = MacOSVer2Int(gBootVolume->OSVersion);
1000return MacOSVerCurrent;
1001}
1002
1003// =========================================================================
1004unsigned long Adler32(unsigned char *buf, long len)
1005{
1006#define BASE 65521L /* largest prime smaller than 65536 */
1007#define NMAX 5000
1008// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
1009
1010#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
1011#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
1012#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
1013#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
1014#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
1015
1016int k;
1017
1018unsigned long s1 = 1; // adler & 0xffff;
1019unsigned long s2 = 0; // (adler >> 16) & 0xffff;
1020unsigned long result;
1021
1022while (len > 0) {
1023k = len < NMAX ? len : NMAX;
1024len -= k;
1025while (k >= 16)
1026{
1027DO16(buf);
1028buf += 16;
1029k -= 16;
1030}
1031
1032if (k != 0)
1033{
1034do
1035{
1036s1 += *buf++;
1037s2 += s1;
1038} while (--k);
1039}
1040
1041s1 %= BASE;
1042s2 %= BASE;
1043}
1044
1045result = (s2 << 16) | s1;
1046
1047return OSSwapHostToBigInt32(result);
1048}
1049

Archive Download this file

Revision: 2806