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

Archive Download this file

Revision: 2428