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

Archive Download this file

Revision: 2546