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

Archive Download this file

Revision: 2347