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

Archive Download this file

Revision: 1452