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

Archive Download this file

Revision: 2436