Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1475