Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2037