Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1447