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

Archive Download this file

Revision: 1448