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

Archive Download this file

Revision: 2045