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[ROOT_DEVICE_SIZE];
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 voidgetOSVersion();
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");
167verbose("Boot Args: %s\n", bootArgs->CommandLine);
168
169// Cleanup the PXE base code.
170
171if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
172if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
173{
174printf("nbpUnloadBaseCode error %d\n", (int) ret);
175sleep(2);
176}
177}
178
179bool dummyVal;
180if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal) {
181showTextBuffer(msgbuf, strlen(msgbuf));
182}
183
184usb_loop();
185
186// If we were in text mode, switch to graphics mode.
187// This will draw the boot graphics unless we are in
188// verbose mode.
189if (gVerboseMode)
190setVideoMode( GRAPHICS_MODE, 0 );
191else
192drawBootGraphics();
193
194setupBooterLog();
195
196finalizeBootStruct();
197
198// Jump to kernel's entry point. There's no going back now.
199if ((checkOSVersion("10.7")) || (checkOSVersion("10.8"))) {
200
201// Notify modules that the kernel is about to be started
202execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);
203
204// Masking out so that Lion doesn't doublefault
205outb(0x21, 0xff);/* Maskout all interrupts Pic1 */
206outb(0xa1, 0xff);/* Maskout all interrupts Pic2 */
207
208startprog( kernelEntry, bootArgs );
209}
210else {
211// Notify modules that the kernel is about to be started
212execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);
213
214startprog( kernelEntry, bootArgsPreLion );
215}
216
217// Not reached
218return 0;
219}
220
221
222//==========================================================================
223// LoadKernelCache - Try to load Kernel Cache.
224// return the length of the loaded cache file or -1 on error
225long LoadKernelCache(const char* cacheFile, void **binary) {
226charkernelCacheFile[512];
227charkernelCachePath[512];
228longflags, time, cachetime, kerneltime, exttime, ret=-1;
229 unsigned long adler32;
230
231 if((gBootMode & kBootModeSafe) != 0)
232 {
233verbose("Kernel Cache ignored.\n");
234return -1;
235 }
236
237// Use specify kernel cache file if not empty
238if (cacheFile[0] != 0)
239strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));
240else {
241 // Lion and Mountain Lion prelink kernel cache file
242if ((checkOSVersion("10.7")) || (checkOSVersion("10.8"))) {
243sprintf(kernelCacheFile, "%skernelcache", kDefaultCachePathSnow);
244}
245// Snow Leopard prelink kernel cache file
246else if (checkOSVersion("10.6")) {
247sprintf(kernelCacheFile, "kernelcache_%s", (archCpuType == CPU_TYPE_I386)
248? "i386" : "x86_64");
249int lnam = strlen(kernelCacheFile) + 9; //with adler32
250
251char* name;
252long prev_time = 0;
253
254struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
255
256while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
257{
258if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
259&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))
260{
261sprintf(kernelCacheFile, "%s%s", kDefaultCachePathSnow, name);
262prev_time = time;
263}
264}
265}
266else {
267// Reset cache name.
268bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
269sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
270adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
271sprintf(kernelCacheFile, "%s.%08lX", kDefaultCachePathLeo, adler32);
272}
273}
274
275// kernelCacheFile must start with a /
276if (kernelCacheFile[0] != '/') {
277char *str = strdup(kernelCacheFile);
278if (str == NULL)
279return -1;
280sprintf(kernelCacheFile, "/%s", str);
281free(str);
282}
283
284// Check if the kernel cache file exists
285ret = -1;
286
287// If boot from a boot helper partition check the kernel cache file on it
288if (gBootVolume->flags & kBVFlagBooter) {
289sprintf(kernelCachePath, "com.apple.boot.P%s", kernelCacheFile);
290ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
291if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
292{
293sprintf(kernelCachePath, "com.apple.boot.R%s", kernelCacheFile);
294ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
295if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
296{
297sprintf(kernelCachePath, "com.apple.boot.S%s", kernelCacheFile);
298ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
299if ((flags & kFileTypeMask) != kFileTypeFlat)
300ret = -1;
301}
302}
303}
304// If not found, use the original kernel cache path.
305if (ret == -1) {
306strcpy(kernelCachePath, kernelCacheFile);
307ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
308if ((flags & kFileTypeMask) != kFileTypeFlat)
309ret = -1;
310}
311
312// Exit if kernel cache file wasn't found
313if (ret == -1) {
314verbose("No Kernel Cache File '%s' found\n", kernelCacheFile);
315return -1;
316}
317
318// Check if the kernel cache file is more recent (mtime)
319// than the kernel file or the S/L/E directory
320ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
321// Check if the kernel file is more recent than the cache file
322if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat)
323&& (kerneltime > cachetime)) {
324verbose("Kernel file (%s) is more recent than KernelCache (%s), ignoring KernelCache\n",
325bootInfo->bootFile, kernelCacheFile);
326return -1;
327}
328
329ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
330// Check if the S/L/E directory time is more recent than the cache file
331if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
332&& (exttime > cachetime)) {
333verbose("/System/Library/Extensions is more recent than KernelCache (%s), ignoring KernelCache\n",
334kernelCacheFile);
335return -1;
336}
337
338// Since the kernel cache file exists and is the most recent try to load it
339verbose("Loading kernel cache %s\n", kernelCachePath);
340
341if (checkOSVersion("10.7") || checkOSVersion("10.8")) {
342ret = LoadThinFatFile(kernelCachePath, binary);
343} else {
344ret = LoadFile(kernelCachePath);
345*binary = (void *)kLoadAddr;
346}
347return ret; // ret contain the length of the binary
348}
349
350//==========================================================================
351// This is the entrypoint from real-mode which functions exactly as it did
352// before. Multiboot does its own runtime initialization, does some of its
353// own things, and then calls common_boot.
354void boot(int biosdev)
355{
356initialize_runtime();
357// Enable A20 gate before accessing memory above 1Mb.
358enableA20();
359common_boot(biosdev);
360}
361
362//==========================================================================
363// The 'main' function for the booter. Called by boot0 when booting
364// from a block device, or by the network booter.
365//
366// arguments:
367// biosdev - Value passed from boot1/NBP to specify the device
368// that the booter was loaded from.
369//
370// If biosdev is kBIOSDevNetwork, then this function will return if
371// booting was unsuccessful. This allows the PXE firmware to try the
372// next boot device on its list.
373void common_boot(int biosdev)
374{
375bool quiet;
376bool firstRun = true;
377bool instantMenu;
378bool rescanPrompt;
379intstatus;
380unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
381unsigned intdenyBVFlags = kBVFlagEFISystem;
382
383// Set reminder to unload the PXE base code. Neglect to unload
384// the base code will result in a hang or kernel panic.
385gUnloadPXEOnExit = true;
386
387// Record the device that the booter was loaded from.
388gBIOSDev = biosdev & kBIOSDevMask;
389
390// Initialize boot info structure.
391initKernBootStruct();
392
393initBooterLog();
394
395// Setup VGA text mode.
396// Not sure if it is safe to call setVideoMode() before the
397// config table has been loaded. Call video_mode() instead.
398#if DEBUG
399printf("before video_mode\n");
400#endif
401video_mode( 2 ); // 80x25 mono text mode.
402#if DEBUG
403printf("after video_mode\n");
404#endif
405
406// Scan and record the system's hardware information.
407scan_platform();
408
409// First get info for boot volume.
410scanBootVolumes(gBIOSDev, 0);
411bvChain = getBVChainForBIOSDev(gBIOSDev);
412setBootGlobals(bvChain);
413
414// Load boot.plist config file
415status = loadChameleonConfig(&bootInfo->chameleonConfig);
416
417if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet) {
418gBootMode |= kBootModeQuiet;
419}
420
421// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
422if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu) {
423firstRun = false;
424}
425
426// Loading preboot ramdisk if exists.
427loadPrebootRAMDisk();
428
429// Disable rescan option by default
430gEnableCDROMRescan = false;
431
432// Enable it with Rescan=y in system config
433if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)
434&& gEnableCDROMRescan) {
435gEnableCDROMRescan = true;
436}
437
438// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
439rescanPrompt = false;
440if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig)
441&& rescanPrompt && biosDevIsCDROM(gBIOSDev))
442{
443gEnableCDROMRescan = promptForRescanOption();
444}
445
446// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
447if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig)
448&& gScanSingleDrive) {
449gScanSingleDrive = true;
450}
451
452// Create a list of partitions on device(s).
453if (gScanSingleDrive) {
454scanBootVolumes(gBIOSDev, &bvCount);
455} else {
456scanDisks(gBIOSDev, &bvCount);
457}
458
459// Create a separated bvr chain using the specified filters.
460bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
461
462gBootVolume = selectBootVolume(bvChain);
463
464// Intialize module system
465init_module_system();
466
467#if DEBUG
468printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",
469 gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
470printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",
471 gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
472getchar();
473#endif
474
475useGUI = true;
476// Override useGUI default
477getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
478if (useGUI && initGUI())
479{
480// initGUI() returned with an error, disabling GUI.
481useGUI = false;
482}
483
484setBootGlobals(bvChain);
485
486// Parse args, load and start kernel.
487while (1)
488{
489booltryresume, tryresumedefault, forceresume;
490booluseKernelCache = false; // by default don't use prelink kernel cache
491const char*val;
492intlen, ret = -1;
493longflags, sleeptime, time;
494void*binary = (void *)kLoadAddr;
495
496char bootFile[sizeof(bootInfo->bootFile)];
497charbootFilePath[512];
498charkernelCacheFile[512];
499
500// Initialize globals.
501sysConfigValid = false;
502gErrors = false;
503
504status = getBootOptions(firstRun);
505firstRun = false;
506if (status == -1) continue;
507
508status = processBootOptions();
509// Status == 1 means to chainboot
510if ( status ==1 ) break;
511// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
512if ( status == -1 )
513{
514// gBootVolume == NULL usually means the user hit escape.
515if (gBootVolume == NULL)
516{
517freeFilteredBVChain(bvChain);
518
519if (gEnableCDROMRescan)
520rescanBIOSDevice(gBIOSDev);
521
522bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
523setBootGlobals(bvChain);
524setupDeviceList(&bootInfo->themeConfig);
525}
526continue;
527}
528
529// Other status (e.g. 0) means that we should proceed with boot.
530
531// Turn off any GUI elements
532if ( bootArgs->Video.v_display == GRAPHICS_MODE )
533{
534gui.devicelist.draw = false;
535gui.bootprompt.draw = false;
536gui.menu.draw = false;
537gui.infobox.draw = false;
538gui.logo.draw = false;
539drawBackground();
540updateVRAM();
541}
542
543// Find out which version mac os we're booting.
544getOSVersion();
545
546if (platformCPUFeature(CPU_FEATURE_EM64T)) {
547archCpuType = CPU_TYPE_X86_64;
548} else {
549archCpuType = CPU_TYPE_I386;
550}
551
552if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig)) {
553if (strncmp(val, "i386", 4) == 0) {
554archCpuType = CPU_TYPE_I386;
555}
556}
557
558if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig)) {
559if (strncmp(val, "i386", 4) == 0) {
560archCpuType = CPU_TYPE_I386;
561}
562}
563
564// Notify modules that we are attempting to boot
565execute_hook("PreBoot", NULL, NULL, NULL, NULL);
566
567if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig)) {
568tryresume = true;
569tryresumedefault = true;
570} else {
571tryresumedefault = false;
572}
573
574if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig)) {
575forceresume = false;
576}
577
578if (forceresume) {
579tryresume = true;
580tryresumedefault = false;
581}
582
583while (tryresume) {
584const char *tmp;
585BVRef bvr;
586if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
587val = "/private/var/vm/sleepimage";
588
589// Do this first to be sure that root volume is mounted
590ret = GetFileInfo(0, val, &flags, &sleeptime);
591
592if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
593break;
594
595// Can't check if it was hibernation Wake=y is required
596if (bvr->modTime == 0 && tryresumedefault)
597break;
598
599if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
600break;
601
602if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
603#if DEBUG
604printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
605bvr->modTime-sleeptime);
606#endif
607break;
608}
609
610HibernateBoot((char *)val);
611break;
612}
613
614verbose("Loading Darwin %s\n", gMacOSVersion);
615
616// If boot from boot helper partitions and OS is Lion use prelink kernel.
617// We need to find a solution to load extra mkext with a prelink kernel.
618if (gBootVolume->flags & kBVFlagBooter && (checkOSVersion("10.7") || checkOSVersion("10.8")))
619useKernelCache = true;
620else
621useKernelCache = false; // by default don't use prelink kernel cache
622
623getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
624
625if (useKernelCache) do {
626
627// Determine the name of the Kernel Cache
628if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {
629if (val[0] == '\\')
630{
631len--;
632val++;
633}
634strlcpy(kernelCacheFile, val, len + 1);
635} else {
636kernelCacheFile[0] = 0; // Use default kernel cache file
637}
638
639if (gOverrideKernel && kernelCacheFile[0] == 0) {
640verbose("Using a non default kernel (%s) without specifying 'Kernel Cache' path, 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(kernelCacheFile, &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
674strlcpy(bootFile, bootInfo->bootFile, sizeof(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.
694strlcpy(bootFilePath, bootFile,sizeof(bootFilePath));
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
770static void getOSVersion()
771{
772strlcpy(gMacOSVersion, gBootVolume->OSVersion, sizeof(gMacOSVersion));
773}
774
775#define BASE 65521L /* largest prime smaller than 65536 */
776#define NMAX 5000
777// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
778
779#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
780#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
781#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
782#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
783#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
784
785unsigned long Adler32(unsigned char *buf, long len)
786{
787unsigned long s1 = 1; // adler & 0xffff;
788unsigned long s2 = 0; // (adler >> 16) & 0xffff;
789unsigned long result;
790int k;
791
792while (len > 0) {
793k = len < NMAX ? len : NMAX;
794len -= k;
795while (k >= 16) {
796DO16(buf);
797buf += 16;
798k -= 16;
799}
800if (k != 0) do {
801s1 += *buf++;
802s2 += s1;
803} while (--k);
804s1 %= BASE;
805s2 %= BASE;
806}
807result = (s2 << 16) | s1;
808return OSSwapHostToBigInt32(result);
809}
810

Archive Download this file

Revision: 1939