Chameleon

Chameleon Svn Source Tree

Root/branches/Bungo/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#if DEBUG_BOOT2
64#define DBG(x...)printf(x)
65#else
66#define DBG(x...)msglog(x)
67#endif
68
69/*
70 * How long to wait (in seconds) to load the
71 * kernel after displaying the "boot:" prompt.
72 */
73#define kBootErrorTimeout 5
74
75boolgOverrideKernel, gEnableCDROMRescan, gScanSingleDrive, useGUI;
76static boolgUnloadPXEOnExit = false;
77
78static chargCacheNameAdler[64 + 256];
79char*gPlatformName = gCacheNameAdler;
80
81chargRootDevice[ROOT_DEVICE_SIZE];
82chargMKextName[512];
83int bvCount = 0, gDeviceCount = 0;
84//intmenucount = 0;
85longgBootMode; /* defaults to 0 == kBootModeNormal */
86BVRefbvr, menuBVR, bvChain;
87
88// Bungo:
89void *new_dsdt = NULL,
90 *new_ecdt = NULL;
91char gDarwinBuildVerStr[256];
92
93static unsigned longAdler32(unsigned char *buffer, long length);
94//static voidselectBiosDevice(void);
95
96/** options.c **/
97extern char* msgbuf;
98void showTextBuffer(char *buf, int size);
99
100//==========================================================================
101// Zero the BSS.
102
103static void zeroBSS(void)
104{
105extern char bss_start __asm("section$start$__DATA$__bss");
106extern char bss_end __asm("section$end$__DATA$__bss");
107extern char common_start __asm("section$start$__DATA$__common");
108extern char common_end __asm("section$end$__DATA$__common");
109
110bzero(&bss_start, (&bss_end - &bss_start));
111bzero(&common_start, (&common_end - &common_start));
112}
113
114//==========================================================================
115// Malloc error function
116
117static void malloc_error(char *addr, size_t size, const char *file, int line)
118{
119stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",
120 (unsigned)addr, (unsigned)size, file, line);
121}
122
123//==========================================================================
124//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
125//
126void initialize_runtime(void)
127{
128zeroBSS();
129malloc_init(0, 0, 0, malloc_error);
130}
131
132//==========================================================================
133// ExecKernel - Load the kernel image (mach-o) and jump to its entry point.
134
135static int ExecKernel(void *binary)
136{
137intret;
138entry_tkernelEntry;
139
140bootArgs->kaddr = bootArgs->ksize = 0;
141execute_hook("ExecKernel", (void *)binary, NULL, NULL, NULL);
142
143ret = DecodeKernel(binary,
144 &kernelEntry,
145 (char **) &bootArgs->kaddr,
146 (int *)&bootArgs->ksize);
147
148if ( ret != 0 )
149return ret;
150
151// Reserve space for boot args
152reserveKernBootStruct();
153
154// Notify modules that the kernel has been decoded
155execute_hook("DecodedKernel", (void *)binary, (void *)bootArgs->kaddr, (void *)bootArgs->ksize, NULL);
156
157setupFakeEfi();
158
159// Load boot drivers from the specifed root path.
160//if (!gHaveKernelCache)
161LoadDrivers("/");
162
163execute_hook("DriversLoaded", (void *)binary, NULL, NULL, NULL);
164
165clearActivityIndicator();
166
167if (gErrors) {
168printf("Errors encountered while starting up the computer.\n");
169printf("Pausing %d seconds...\n", kBootErrorTimeout);
170sleep(kBootErrorTimeout);
171}
172
173md0Ramdisk();
174
175// Cleanup the PXE base code.
176
177if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
178if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess ) {
179printf("nbpUnloadBaseCode error %d\n", (int)ret);
180sleep(2);
181}
182}
183
184bool dummyVal;
185if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal) {
186showTextBuffer(msgbuf, strlen(msgbuf));
187}
188
189usb_loop();
190
191// If we were in text mode, switch to graphics mode.
192// This will draw the boot graphics unless we are in
193// verbose mode.
194if (gVerboseMode) {
195setVideoMode(GRAPHICS_MODE, 0);
196} else {
197drawBootGraphics();
198}
199
200 getCurrentEDID();
201
202 verbose("GUI Graphics: %dx%dx%d.\n", gui.screen.width, gui.screen.height, bootArgs->Video.v_depth);
203 verbose("Boot Graphics: %dx%dx%d, mode=0x%04X.\n", bootArgs->Video.v_width, bootArgs->Video.v_height, bootArgs->Video.v_depth, getCurrentGraphicsMode());
204
205verbose("Starting Mac OS X %s [0x%08X], %s\n", gBootVolume->OSVersion, MacOSVerCurrent, gDarwinBuildVerStr);
206verbose("Boot Args: %s\n", bootArgs->CommandLine);
207
208setupBooterLog();
209
210finalizeBootStruct();
211
212// Jump to kernel's entry point. There's no going back now.
213 if (MacOSVerCurrent >= MacOSVer2Int("10.7"))
214//if (!((checkOSVersion("10.4")) || (checkOSVersion("10.5")) || (checkOSVersion("10.6"))) )
215{
216// Notify modules that the kernel is about to be started
217execute_hook("Kernel Start", (void *)kernelEntry, (void *)bootArgs, NULL, NULL);
218
219// Masking out so that Lion doesn't doublefault
220outb(0x21, 0xff);/* Maskout all interrupts Pic1 */
221outb(0xa1, 0xff);/* Maskout all interrupts Pic2 */
222
223 if (gVerboseMode) pause("");
224startprog( kernelEntry, bootArgs );
225} else {
226// Notify modules that the kernel is about to be started
227execute_hook("Kernel Start", (void *)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);
228
229 if (gVerboseMode) pause("");
230startprog( kernelEntry, bootArgsPreLion );
231}
232
233// Not reached
234return 0;
235}
236
237
238//==========================================================================
239// LoadKernelCache - Try to load Kernel Cache.
240// return the length of the loaded cache file or -1 on error
241long LoadKernelCache(const char* cacheFile, void **binary)
242{
243charkernelCacheFile[512];
244charkernelCachePath[512];
245longflags, ret=-1;
246unsigned long adler32;
247u_int32_t time, cachetime, kerneltime, exttime;
248
249if ((gBootMode & kBootModeSafe) != 0) {
250verbose("Kernel Cache ignored.\n");
251return -1;
252}
253
254// Use specify kernel cache file if not empty
255if (cacheFile[0] != 0)
256{
257strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));
258verbose("Specified kernel cache file path: %s\n", cacheFile);
259}
260else
261{
262// Lion, Mountain Lion Mavericks and Yosemite prelink kernel cache file
263if (MacOSVerCurrent >= MacOSVer2Int("10.7")) // OSX is Lion (10.7) or newer
264{
265snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);
266verbose("Kernel Cache file path for Mac OS X 10.7 and newer: %s\n", kernelCacheFile);
267}
268// Snow Leopard prelink kernel cache file
269else
270 {
271 if (MacOSVerCurrent >= MacOSVer2Int("10.6")) // OSX is Snow (10.6)
272 {
273 snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",
274 (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");
275
276 int lnam = strlen(kernelCacheFile) + 9; //with adler32
277 char* name;
278 long prev_time = 0;
279
280 struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
281
282 /* TODO: handle error? */
283 if (cacheDir)
284 {
285 while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
286 {
287 if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
288 && strstr(name, kernelCacheFile) && (name[lnam] != '.'))
289 {
290 snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);
291 prev_time = time;
292 }
293 }
294 verbose("Kernel Cache file path for Mac OS X 10.6.X: %s\n", kernelCacheFile);
295 }
296 closedir(cacheDir);
297 }
298 else
299 {
300 // Reset cache name.
301 bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
302 snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64, "%s,%s", gRootDevice, bootInfo->bootFile);
303 adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
304 snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);
305 verbose("Reseted kernel cache file path: %s\n", kernelCacheFile);
306 }
307}
308}
309
310// Check if the kernel cache file exists
311ret = -1;
312
313// If boot from a boot helper partition check the kernel cache file on it
314if (gBootVolume->flags & kBVFlagBooter)
315{
316snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);
317ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
318
319if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
320{
321snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);
322ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
323
324if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
325{
326snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);
327ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
328
329if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
330{
331snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.recovery.boot/kernelcache", kernelCacheFile);
332ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
333
334if ((flags & kFileTypeMask) != kFileTypeFlat)
335{
336ret = -1;
337}
338}
339}
340}
341}
342
343// If not found, use the original kernel cache path.
344if (ret == -1) {
345strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));
346ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
347if ((flags & kFileTypeMask) != kFileTypeFlat) {
348ret = -1;
349}
350}
351
352// Exit if kernel cache file wasn't found
353if (ret == -1) {
354verbose("No Kernel Cache File '%s' found\n", kernelCacheFile);
355return -1;
356}
357
358// Check if the kernel cache file is more recent (mtime)
359// than the kernel file or the S/L/E directory
360ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
361// Check if the kernel file is more recent than the cache file
362if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat)
363&& (kerneltime > cachetime)) {
364verbose("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n",
365bootInfo->bootFile, kernelCacheFile);
366return -1;
367}
368
369ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
370
371// Check if the S/L/E directory time is more recent than the cache file
372if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
373&& (exttime > cachetime)) {
374verbose("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n",
375kernelCacheFile);
376return -1;
377}
378
379// Since the kernel cache file exists and is the most recent try to load it
380verbose("Loading Kernel Cache from: '%s%s' (%s)\n", gBootVolume->label, gBootVolume->altlabel, gBootVolume->type_name);
381
382ret = LoadThinFatFile(kernelCachePath, binary);
383 if (gVerboseMode) pause("");
384return ret; // ret contain the length of the binary
385}
386
387//==========================================================================
388// This is the entrypoint from real-mode which functions exactly as it did
389// before. Multiboot does its own runtime initialization, does some of its
390// own things, and then calls common_boot.
391void boot(int biosdev)
392{
393initialize_runtime();
394// Enable A20 gate before accessing memory above 1Mb.
395enableA20();
396common_boot(biosdev);
397}
398
399//==========================================================================
400// The 'main' function for the booter. Called by boot0 when booting
401// from a block device, or by the network booter.
402//
403// arguments:
404// biosdev - Value passed from boot1/NBP to specify the device
405// that the booter was loaded from.
406//
407// If biosdev is kBIOSDevNetwork, then this function will return if
408// booting was unsuccessful. This allows the PXE firmware to try the
409// next boot device on its list.
410void common_boot(int biosdev)
411{
412bool quiet;
413bool firstRun = true;
414bool instantMenu;
415bool rescanPrompt;
416intstatus;
417unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
418unsigned intdenyBVFlags = kBVFlagEFISystem;
419
420// Set reminder to unload the PXE base code. Neglect to unload
421// the base code will result in a hang or kernel panic.
422gUnloadPXEOnExit = true;
423
424// Record the device that the booter was loaded from.
425gBIOSDev = biosdev & kBIOSDevMask;
426
427// Initialize boot-log
428initBooterLog();
429
430// Initialize boot info structure.
431initKernBootStruct();
432
433// Setup VGA text mode.
434// Not sure if it is safe to call setVideoMode() before the
435// config table has been loaded. Call video_mode() instead.
436#if DEBUG
437printf("before video_mode\n");
438#endif
439video_mode( 2 ); // 80x25 mono text mode.
440#if DEBUG
441printf("after video_mode\n");
442#endif
443
444// Scan and record the system's hardware information.
445scan_platform();
446
447// First get info for boot volume.
448scanBootVolumes(gBIOSDev, 0);
449bvChain = getBVChainForBIOSDev(gBIOSDev);
450setBootGlobals(bvChain);
451
452// Load boot.plist config file
453status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);
454
455if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet) {
456gBootMode |= kBootModeQuiet;
457}
458
459// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
460if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu) {
461firstRun = false;
462}
463
464// Loading preboot ramdisk if exists.
465loadPrebootRAMDisk();
466
467// Disable rescan option by default
468gEnableCDROMRescan = false;
469
470// Enable it with Rescan=y in system config
471if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)
472&& gEnableCDROMRescan) {
473gEnableCDROMRescan = true;
474}
475
476 // Disable rescanPrompt option by default
477 rescanPrompt = false;
478
479// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
480if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig)
481&& rescanPrompt && biosDevIsCDROM(gBIOSDev))
482{
483gEnableCDROMRescan = promptForRescanOption();
484}
485
486 // Disable ScanSingleDrive option by default
487 gScanSingleDrive = false;
488
489// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
490if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig)
491&& gScanSingleDrive) {
492gScanSingleDrive = true;
493}
494
495// Create a list of partitions on device(s).
496if (gScanSingleDrive) {
497scanBootVolumes(gBIOSDev, &bvCount);
498} else {
499scanDisks(gBIOSDev, &bvCount);
500}
501
502// Create a separated bvr chain using the specified filters.
503bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
504
505gBootVolume = selectBootVolume(bvChain);
506
507// Intialize module system
508init_module_system();
509
510#if DEBUG
511 if (gBootVolume) {
512 verbose("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
513 gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
514 }
515 else {
516 verbose("Boot Volume not selected yet.\n");
517 }
518verbose("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
519 gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
520pause("");
521#endif
522
523useGUI = true;
524// Override useGUI default
525getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
526if (useGUI && initGUI())
527{
528// initGUI() returned with an error, disabling GUI.
529useGUI = false;
530}
531
532setBootGlobals(bvChain);
533
534// Parse args, load and start kernel.
535while (1)
536{
537booltryresume, tryresumedefault, forceresume;
538booluseKernelCache = true; // by default try to use the prelinked kernel
539const char*val;
540intlen, ret = -1;
541longflags;
542u_int32_tsleeptime, time;
543void*binary = (void *)kLoadAddr;
544
545char bootFile[sizeof(bootInfo->bootFile)];
546charbootFilePath[512];
547charkernelCacheFile[512];
548
549// Initialize globals.
550sysConfigValid = false;
551gErrors = false;
552
553status = getBootOptions(firstRun);
554firstRun = false;
555if (status == -1) continue;
556
557status = processBootOptions();
558// Status == 1 means to chainboot
559if ( status ==1 ) break;
560// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
561if ( status == -1 )
562{
563// gBootVolume == NULL usually means the user hit escape.
564if (gBootVolume == NULL)
565{
566freeFilteredBVChain(bvChain);
567
568if (gEnableCDROMRescan)
569rescanBIOSDevice(gBIOSDev);
570
571bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
572setBootGlobals(bvChain);
573setupDeviceList(&bootInfo->themeConfig);
574}
575continue;
576}
577
578// Other status (e.g. 0) means that we should proceed with boot.
579
580// Turn off any GUI elements
581if ( bootArgs->Video.v_display == GRAPHICS_MODE )
582{
583gui.devicelist.draw = false;
584gui.bootprompt.draw = false;
585gui.menu.draw = false;
586gui.infobox.draw = false;
587gui.logo.draw = false;
588drawBackground();
589updateVRAM();
590}
591
592if (platformCPUFeature(CPU_FEATURE_EM64T))
593{
594archCpuType = CPU_TYPE_X86_64;
595} else {
596archCpuType = CPU_TYPE_I386;
597}
598
599if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig)) {
600if (strncmp(val, "i386", 4) == 0) {
601archCpuType = CPU_TYPE_I386;
602}
603}
604
605if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig)) {
606if (strncmp(val, "i386", 4) == 0) {
607archCpuType = CPU_TYPE_I386;
608}
609}
610
611// Notify modules that we are attempting to boot
612execute_hook("PreBoot", NULL, NULL, NULL, NULL);
613
614if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig)) {
615tryresume = true;
616tryresumedefault = true;
617} else {
618tryresumedefault = false;
619}
620
621if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig)) {
622forceresume = false;
623}
624
625if (forceresume) {
626tryresume = true;
627tryresumedefault = false;
628}
629
630while (tryresume) {
631const char *tmp;
632BVRef bvr;
633if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
634val = "/private/var/vm/sleepimage";
635
636// Do this first to be sure that root volume is mounted
637ret = GetFileInfo(0, val, &flags, &sleeptime);
638
639if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
640break;
641
642// Can't check if it was hibernation Wake=y is required
643if (bvr->modTime == 0 && tryresumedefault)
644break;
645
646if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
647break;
648
649if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
650#if DEBUG
651printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
652bvr->modTime-sleeptime);
653#endif
654break;
655}
656
657HibernateBoot((char *)val);
658break;
659}
660
661getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
662if (useKernelCache) {
663 do {
664 // Determine the name of the Kernel Cache
665 if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {
666 if (val[0] == '\\')
667 {
668 len--;
669 val++;
670 }
671 /* FIXME: check len vs sizeof(kernelCacheFile) */
672 strlcpy(kernelCacheFile, val, len + 1);
673 } else {
674 kernelCacheFile[0] = 0; // Use default kernel cache file
675 }
676
677 if (gOverrideKernel && kernelCacheFile[0] == 0) {
678 verbose("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);
679 useKernelCache = false;
680 break;
681 }
682 if (gMKextName[0] != 0) {
683 verbose("Using a specific MKext Cache (%s), KernelCache will not be used\n",
684 gMKextName);
685 useKernelCache = false;
686 break;
687 }
688 if (gBootFileType != kBlockDeviceType)
689 useKernelCache = false;
690
691 } while(0);
692 } else {
693 verbose("Kernel Cache using disabled by user.");
694 }
695
696do
697 {
698if (useKernelCache) {
699ret = LoadKernelCache(kernelCacheFile, &binary);
700if (ret >= 0)
701 break;
702}
703
704bool bootFileWithDevice = false;
705// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
706if (strncmp(bootInfo->bootFile,"bt(",3) == 0 ||
707strncmp(bootInfo->bootFile,"hd(",3) == 0 ||
708strncmp(bootInfo->bootFile,"rd(",3) == 0)
709bootFileWithDevice = true;
710
711// bootFile must start with a / if it not start with a device name
712if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')
713{
714if (MacOSVerCurrent >= MacOSVer2Int("10.10")) // OSX is 10.10 or newer
715snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathForYos "%s", bootInfo->bootFile); // Yosemite: '/System/Library/Kernels/'
716 else
717snprintf(bootFile, sizeof(bootFile), kDefaultKernelPathPreYos "%s", bootInfo->bootFile); // append a leading '/'
718}
719 else
720 {
721strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));
722}
723
724// Try to load kernel image from alternate locations on boot helper partitions.
725ret = -1;
726if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice)
727 {
728snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);
729ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
730if (ret == -1)
731{
732snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);
733ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
734if (ret == -1)
735{
736snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);
737ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
738}
739}
740}
741if (ret == -1)
742 {
743// No alternate location found, using the original kernel image path.
744strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));
745}
746
747verbose("Loading kernel from: '%s' (%s)\n", gBootVolume->label, gBootVolume->type_name);
748ret = LoadThinFatFile(bootFilePath, &binary);
749if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
750{
751archCpuType = CPU_TYPE_I386;
752ret = LoadThinFatFile(bootFilePath, &binary);
753}
754} while (0);
755
756clearActivityIndicator();
757
758#if DEBUG
759printf("Pausing...");
760sleep(8);
761#endif
762
763if (ret <= 0) {
764error("Can't find boot file: '%s'\n", bootFile);
765sleep(1);
766
767if (gBootFileType == kNetworkDeviceType) {
768// Return control back to PXE. Don't unload PXE base code.
769gUnloadPXEOnExit = false;
770break;
771}
772pause("");
773
774} else {
775/* Won't return if successful. */
776ret = ExecKernel(binary);
777}
778}
779
780// chainboot
781if (status == 1) {
782// if we are already in graphics-mode,
783if (getVideoMode() == GRAPHICS_MODE) {
784setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode.
785}
786}
787
788if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {
789nbpUnloadBaseCode();
790}
791}
792
793/*!
794Selects a new BIOS device, taking care to update the global state appropriately.
795 */
796/*
797static void selectBiosDevice(void)
798{
799struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
800CacheReset();
801diskFreeMap(oldMap);
802oldMap = NULL;
803
804int dev = selectAlternateBootDevice(gBIOSDev);
805
806BVRef bvchain = scanBootVolumes(dev, 0);
807BVRef bootVol = selectBootVolume(bvchain);
808gBootVolume = bootVol;
809setRootVolume(bootVol);
810gBIOSDev = dev;
811}
812*/
813
814bool checkOSVersion(const char * version)
815{
816if ( (sizeof(version) > 4) && ('.' != version[4]) && ('\0' != version[4]) )
817{
818return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
819&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3])
820&& (gMacOSVersion[4] == version[4]));
821}
822else
823{
824return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
825&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));
826}
827}
828
829uint32_t getMacOSVerCurrent()
830{
831 MacOSVerCurrent = MacOSVer2Int(gBootVolume->OSVersion);
832 return MacOSVerCurrent;
833}
834
835#define BASE 65521L /* largest prime smaller than 65536 */
836#define NMAX 5000
837// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
838
839#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
840#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
841#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
842#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
843#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
844
845unsigned long Adler32(unsigned char *buf, long len)
846{
847unsigned long s1 = 1; // adler & 0xffff;
848unsigned long s2 = 0; // (adler >> 16) & 0xffff;
849unsigned long result;
850int k;
851
852while (len > 0) {
853k = len < NMAX ? len : NMAX;
854len -= k;
855while (k >= 16) {
856DO16(buf);
857buf += 16;
858k -= 16;
859}
860if (k != 0) do {
861s1 += *buf++;
862s2 += s1;
863} while (--k);
864s1 %= BASE;
865s2 %= BASE;
866}
867result = (s2 << 16) | s1;
868return OSSwapHostToBigInt32(result);
869}
870

Archive Download this file

Revision: 2531