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

Archive Download this file

Revision: 2458