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

Archive Download this file

Revision: 2382