Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1896