Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2238