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

Archive Download this file

Revision: 2385