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

Archive Download this file

Revision: 2433