Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1698