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

Archive Download this file

Revision: 2585