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 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 ( LION || MOUNTAIN_LION || MAVERICKS || YOSEMITE )
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
229
230startprog( kernelEntry, bootArgs );
231}
232else
233{
234// Notify modules that the kernel is about to be started
235execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);
236
237startprog( kernelEntry, bootArgsPreLion );
238}
239
240// Not reached
241return 0;
242}
243
244
245//==========================================================================
246// LoadKernelCache - Try to load Kernel Cache.
247// return the length of the loaded cache file or -1 on error
248long LoadKernelCache(const char* cacheFile, void **binary)
249{
250charkernelCacheFile[512];
251charkernelCachePath[512];
252longflags, ret=-1;
253unsigned long adler32;
254u_int32_t time, cachetime, kerneltime, exttime;
255
256if((gBootMode & kBootModeSafe) != 0)
257{
258DBG("Kernel Cache ignored.\n");
259return -1;
260}
261
262// Use specify kernel cache file if not empty
263if (cacheFile[0] != 0)
264{
265strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));
266verbose("Specified kernel cache file path: %s\n", cacheFile);
267}
268else
269{
270if ( LION || MOUNTAIN_LION || MAVERICKS || YOSEMITE)
271{
272// Lion, Mountain Lion, Mavericks, and Yosemite prelink kernel cache file
273// for 10.7 10.8 10.9 10.10
274snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);
275verbose("Kernel Cache file path (Mac OS X 10.7 and newer): %s\n", kernelCacheFile);;
276}
277// Snow Leopard prelink kernel cache file
278else if ( SNOW_LEOPARD )
279{
280snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",
281(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");
282
283intlnam = strlen(kernelCacheFile) + 9; //with adler32
284char*name;
285u_int32_t prev_time = 0;
286
287struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
288
289/* TODO: handle error? */
290if (cacheDir)
291{
292while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
293{
294if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
295&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))
296{
297snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);
298prev_time = time;
299}
300}
301verbose("Kernel Cache file path (Mac OS X 10.6.X): %s\n", kernelCacheFile);
302}
303closedir(cacheDir);
304}
305else
306{
307// Reset cache name.
308bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
309snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64, "%s,%s", gRootDevice, bootInfo->bootFile);
310adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
311snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);
312verbose("Reseted kernel cache file path: %s\n", kernelCacheFile);;
313}
314}
315
316// Check if the kernel cache file exists
317ret = -1;
318
319// If boot from a boot helper partition check the kernel cache file on it
320if (gBootVolume->flags & kBVFlagBooter)
321{
322snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.P/%s", kernelCacheFile);
323ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
324
325if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
326{
327snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.R/%s", kernelCacheFile);
328ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
329
330if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
331{
332snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.boot.S/%s", kernelCacheFile);
333ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
334
335if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
336{
337snprintf(kernelCachePath, sizeof(kernelCachePath), "/com.apple.recovery.boot/kernelcache", kernelCacheFile);
338ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
339
340if ((flags & kFileTypeMask) != kFileTypeFlat)
341{
342ret = -1;
343}
344}
345}
346}
347}
348
349// If not found, use the original kernel cache path.
350if (ret == -1)
351{
352strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));
353ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
354
355if ((flags & kFileTypeMask) != kFileTypeFlat)
356{
357ret = -1;
358}
359}
360
361// Exit if kernel cache file wasn't found
362if (ret == -1)
363{
364DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);
365return -1;
366}
367
368// Check if the kernel cache file is more recent (mtime)
369// than the kernel file or the S/L/E directory
370ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
371
372// Check if the kernel file is more recent than the cache file
373if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat) && (kerneltime > cachetime))
374{
375DBG("Kernel file '%s' is more recent than Kernel Cache '%s'! Ignoring Kernel Cache.\n", bootInfo->bootFile, kernelCacheFile);
376return -1;
377}
378
379ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
380
381// Check if the S/L/E directory time is more recent than the cache file
382if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory) && (exttime > cachetime))
383{
384DBG("Folder '/System/Library/Extensions' is more recent than Kernel Cache file '%s'! Ignoring Kernel Cache.\n", kernelCacheFile);
385return -1;
386}
387
388// Since the kernel cache file exists and is the most recent try to load it
389DBG("Loading Kernel Cache from: '%s'\n", kernelCachePath);
390
391ret = LoadThinFatFile(kernelCachePath, binary);
392return ret; // ret contain the length of the binary
393}
394
395//==========================================================================
396// This is the entrypoint from real-mode which functions exactly as it did
397// before. Multiboot does its own runtime initialization, does some of its
398// own things, and then calls common_boot.
399void boot(int biosdev)
400{
401initialize_runtime();
402// Enable A20 gate before accessing memory above 1Mb.
403enableA20();
404common_boot(biosdev);
405}
406
407//==========================================================================
408// The 'main' function for the booter. Called by boot0 when booting
409// from a block device, or by the network booter.
410//
411// arguments:
412// biosdev - Value passed from boot1/NBP to specify the device
413// that the booter was loaded from.
414//
415// If biosdev is kBIOSDevNetwork, then this function will return if
416// booting was unsuccessful. This allows the PXE firmware to try the
417// next boot device on its list.
418void common_boot(int biosdev)
419{
420bool quiet;
421bool firstRun = true;
422bool instantMenu;
423bool rescanPrompt;
424intstatus;
425unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
426unsigned intdenyBVFlags = kBVFlagEFISystem;
427
428// Set reminder to unload the PXE base code. Neglect to unload
429// the base code will result in a hang or kernel panic.
430gUnloadPXEOnExit = true;
431
432// Record the device that the booter was loaded from.
433gBIOSDev = biosdev & kBIOSDevMask;
434
435// Initialize boot-log
436initBooterLog();
437
438// Initialize boot info structure.
439initKernBootStruct();
440
441// Setup VGA text mode.
442// Not sure if it is safe to call setVideoMode() before the
443// config table has been loaded. Call video_mode() instead.
444#if DEBUG
445printf("before video_mode\n");
446#endif
447video_mode( 2 ); // 80x25 mono text mode.
448#if DEBUG
449printf("after video_mode\n");
450#endif
451
452// Scan and record the system's hardware information.
453scan_platform();
454
455// First get info for boot volume.
456scanBootVolumes(gBIOSDev, 0);
457bvChain = getBVChainForBIOSDev(gBIOSDev);
458setBootGlobals(bvChain);
459
460// Load boot.plist config file
461status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);
462
463if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet)
464{
465gBootMode |= kBootModeQuiet;
466}
467
468// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
469if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu)
470{
471firstRun = false;
472}
473
474// Loading preboot ramdisk if exists.
475loadPrebootRAMDisk();
476
477// Disable rescan option by default
478gEnableCDROMRescan = false;
479
480// Enable it with Rescan=y in system config
481if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)&& gEnableCDROMRescan)
482{
483gEnableCDROMRescan = true;
484}
485
486// Disable rescanPrompt option by default
487rescanPrompt = false;
488
489// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
490if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))
491{
492gEnableCDROMRescan = promptForRescanOption();
493}
494
495// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
496if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive)
497{
498gScanSingleDrive = true;
499}
500
501// Create a list of partitions on device(s).
502if (gScanSingleDrive)
503{
504scanBootVolumes(gBIOSDev, &bvCount);
505}
506else
507{
508scanDisks(gBIOSDev, &bvCount);
509}
510
511// Create a separated bvr chain using the specified filters.
512bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
513
514gBootVolume = selectBootVolume(bvChain);
515
516// Intialize module system
517init_module_system();
518
519#if DEBUG
520printf("Default: %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
521gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
522printf("bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: 0x%08X\n",
523gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
524getchar();
525#endif
526
527useGUI = true;
528// Override useGUI default
529getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
530if (useGUI && initGUI())
531{
532// initGUI() returned with an error, disabling GUI.
533useGUI = false;
534}
535
536setBootGlobals(bvChain);
537
538// Parse args, load and start kernel.
539while (1)
540{
541booltryresume, tryresumedefault, forceresume;
542booluseKernelCache = true; // by default try to use the prelinked kernel
543const char*val;
544intlen, ret = -1;
545longflags;
546u_int32_tsleeptime, time;
547void*binary = (void *)kLoadAddr;
548
549charbootFile[sizeof(bootInfo->bootFile)];
550charbootFilePath[512];
551charkernelCacheFile[512];
552
553// Initialize globals.
554sysConfigValid = false;
555gErrors = false;
556
557status = getBootOptions(firstRun);
558firstRun = false;
559if (status == -1) continue;
560
561status = processBootOptions();
562// Status == 1 means to chainboot
563if ( status ==1 ) break;
564// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
565if ( status == -1 )
566{
567// gBootVolume == NULL usually means the user hit escape.
568if (gBootVolume == NULL)
569{
570freeFilteredBVChain(bvChain);
571
572if (gEnableCDROMRescan)
573rescanBIOSDevice(gBIOSDev);
574
575bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
576setBootGlobals(bvChain);
577setupDeviceList(&bootInfo->themeConfig);
578}
579continue;
580}
581
582// Other status (e.g. 0) means that we should proceed with boot.
583
584// Turn off any GUI elements
585if ( bootArgs->Video.v_display == GRAPHICS_MODE )
586{
587gui.devicelist.draw = false;
588gui.bootprompt.draw = false;
589gui.menu.draw = false;
590gui.infobox.draw = false;
591gui.logo.draw = false;
592drawBackground();
593updateVRAM();
594}
595
596if (platformCPUFeature(CPU_FEATURE_EM64T))
597{
598archCpuType = CPU_TYPE_X86_64;
599}
600else
601{
602archCpuType = CPU_TYPE_I386;
603}
604
605if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig))
606{
607if (strncmp(val, "i386", 4) == 0)
608{
609archCpuType = CPU_TYPE_I386;
610}
611else
612{
613DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");
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{
724DBG("Kernel Cache using disabled by user.\n");
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: 2550