Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1398