Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2538