Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/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];
78void*gRootPCIDev;
79
80intgDualLink;
81intbvCount = 0, gDeviceCount = 0;
82//intmenucount = 0;
83longgBootMode; /* defaults to 0 == kBootModeNormal */
84BVRefbvr, menuBVR, bvChain;
85
86static boolcheckOSVersion(const char * version);
87static voidgetOSVersion();
88static unsigned longAdler32(unsigned char *buffer, long length);
89//static voidselectBiosDevice(void);
90
91/** options.c **/
92extern char* msgbuf;
93void showTextBuffer(char *buf, int size);
94
95
96//==========================================================================
97// Zero the BSS.
98
99static void zeroBSS(void)
100{
101extern char _DATA__bss__begin, _DATA__bss__end;
102extern char _DATA__common__begin, _DATA__common__end;
103
104bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));
105bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));
106}
107
108//==========================================================================
109// Malloc error function
110
111static void malloc_error(char *addr, size_t size, const char *file, int line)
112{
113stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",
114 (unsigned)addr, (unsigned)size, file, line);
115}
116
117//==========================================================================
118//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
119//
120void initialize_runtime(void)
121{
122zeroBSS();
123malloc_init(0, 0, 0, malloc_error);
124}
125
126//==========================================================================
127// execKernel - Load the kernel image (mach-o) and jump to its entry point.
128
129static int ExecKernel(void *binary)
130{
131intret;
132entry_tkernelEntry;
133
134bootArgs->kaddr = bootArgs->ksize = 0;
135execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL);
136
137ret = DecodeKernel(binary,
138 &kernelEntry,
139 (char **) &bootArgs->kaddr,
140 (int *)&bootArgs->ksize );
141
142if ( ret != 0 )
143return ret;
144
145// Reserve space for boot args
146reserveKernBootStruct();
147
148// Notify modules that the kernel has been decoded
149execute_hook("DecodedKernel", (void*)binary, NULL, NULL, NULL);
150
151setupFakeEfi();
152
153// Load boot drivers from the specifed root path.
154//if (!gHaveKernelCache)
155LoadDrivers("/");
156
157execute_hook("DriversLoaded", (void*)binary, NULL, NULL, NULL);
158
159clearActivityIndicator();
160
161if (gErrors) {
162printf("Errors encountered while starting up the computer.\n");
163printf("Pausing %d seconds...\n", kBootErrorTimeout);
164sleep(kBootErrorTimeout);
165}
166
167md0Ramdisk();
168
169verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");
170verbose("Boot Args: %s\n", bootArgs->CommandLine);
171
172// Cleanup the PXE base code.
173
174if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
175if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
176{
177printf("nbpUnloadBaseCode error %d\n", (int) ret);
178sleep(2);
179}
180}
181
182bool dummyVal;
183if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal) {
184showTextBuffer(msgbuf, strlen(msgbuf));
185}
186
187usb_loop();
188
189// If we were in text mode, switch to graphics mode.
190// This will draw the boot graphics unless we are in
191// verbose mode.
192if (gVerboseMode)
193setVideoMode( GRAPHICS_MODE, 0 );
194else
195drawBootGraphics();
196
197setupBooterLog();
198
199finalizeBootStruct();
200
201// Jump to kernel's entry point. There's no going back now.
202if ((checkOSVersion("10.7")) || (checkOSVersion("10.8"))) {
203
204// Notify modules that the kernel is about to be started
205execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);
206
207// Masking out so that Lion doesn't doublefault
208
209startprog( kernelEntry, bootArgs );
210}
211else {
212// Notify modules that the kernel is about to be started
213execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);
214
215startprog( kernelEntry, bootArgsPreLion );
216}
217
218// Not reached
219return 0;
220}
221
222
223//==========================================================================
224// LoadKernelCache - Try to load Kernel Cache.
225// return the length of the loaded cache file or -1 on error
226long LoadKernelCache(const char* cacheFile, void **binary) {
227charkernelCacheFile[512];
228charkernelCachePath[512];
229longflags, time, cachetime, kerneltime, exttime, ret=-1;
230 unsigned long adler32;
231
232 if((gBootMode & kBootModeSafe) != 0)
233 {
234verbose("Kernel Cache ignored.\n");
235return -1;
236 }
237
238// Use specify kernel cache file if not empty
239if (cacheFile[0] != 0)
240strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));
241else {
242// Mountain Lion and Lion prelink kernel cache file
243if ((checkOSVersion("10.7")) || (checkOSVersion("10.8"))) {
244sprintf(kernelCacheFile, "%skernelcache", kDefaultCachePathSnow);
245 }
246// Snow Leopard prelink kernel cache file
247else if (checkOSVersion("10.6")) {
248sprintf(kernelCacheFile, "kernelcache_%s", (archCpuType == CPU_TYPE_I386)
249? "i386" : "x86_64");
250int lnam = strlen(kernelCacheFile) + 9; //with adler32
251
252char* name;
253long prev_time = 0;
254
255struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
256
257while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
258{
259if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
260&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))
261{
262sprintf(kernelCacheFile, "%s%s", kDefaultCachePathSnow, name);
263prev_time = time;
264}
265}
266}
267else {
268// Reset cache name.
269bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
270sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
271adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
272sprintf(kernelCacheFile, "%s.%08lX", kDefaultCachePathLeo, adler32);
273}
274}
275
276// kernelCacheFile must start with a /
277if (kernelCacheFile[0] != '/') {
278char *str = strdup(kernelCacheFile);
279if (str == NULL)
280return -1;
281sprintf(kernelCacheFile, "/%s", str);
282free(str);
283}
284
285// Check if the kernel cache file exists
286ret = -1;
287
288// If boot from a boot helper partition check the kernel cache file on it
289if (gBootVolume->flags & kBVFlagBooter) {
290sprintf(kernelCachePath, "com.apple.boot.P%s", kernelCacheFile);
291ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
292if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
293{
294sprintf(kernelCachePath, "com.apple.boot.R%s", kernelCacheFile);
295ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
296if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
297{
298sprintf(kernelCachePath, "com.apple.boot.S%s", kernelCacheFile);
299ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
300if ((flags & kFileTypeMask) != kFileTypeFlat)
301ret = -1;
302}
303}
304}
305// If not found, use the original kernel cache path.
306if (ret == -1) {
307strcpy(kernelCachePath, kernelCacheFile);
308ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
309if ((flags & kFileTypeMask) != kFileTypeFlat)
310ret = -1;
311}
312
313// Exit if kernel cache file wasn't found
314if (ret == -1) {
315verbose("No Kernel Cache File '%s' found\n", kernelCacheFile);
316return -1;
317}
318
319// Check if the kernel cache file is more recent (mtime)
320// than the kernel file or the S/L/E directory
321ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
322// Check if the kernel file is more recent than the cache file
323if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat)
324&& (kerneltime > cachetime)) {
325verbose("Kernel file (%s) is more recent than KernelCache (%s), ignoring KernelCache\n",
326bootInfo->bootFile, kernelCacheFile);
327return -1;
328}
329
330ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
331// Check if the S/L/E directory time is more recent than the cache file
332if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
333&& (exttime > cachetime)) {
334verbose("/System/Library/Extensions is more recent than KernelCache (%s), ignoring KernelCache\n",
335kernelCacheFile);
336return -1;
337}
338
339// Since the kernel cache file exists and is the most recent try to load it
340verbose("Loading kernel cache %s\n", kernelCachePath);
341
342if ((checkOSVersion("10.7")) || (checkOSVersion("10.8"))) {
343ret = LoadThinFatFile(kernelCachePath, binary);
344} else {
345ret = LoadFile(kernelCachePath);
346*binary = (void *)kLoadAddr;
347}
348return ret; // ret contain the length of the binary
349}
350
351//==========================================================================
352// This is the entrypoint from real-mode which functions exactly as it did
353// before. Multiboot does its own runtime initialization, does some of its
354// own things, and then calls common_boot.
355void boot(int biosdev)
356{
357initialize_runtime();
358// Enable A20 gate before accessing memory above 1Mb.
359enableA20();
360common_boot(biosdev);
361}
362
363//==========================================================================
364// The 'main' function for the booter. Called by boot0 when booting
365// from a block device, or by the network booter.
366//
367// arguments:
368// biosdev - Value passed from boot1/NBP to specify the device
369// that the booter was loaded from.
370//
371// If biosdev is kBIOSDevNetwork, then this function will return if
372// booting was unsuccessful. This allows the PXE firmware to try the
373// next boot device on its list.
374void common_boot(int biosdev)
375{
376bool quiet;
377bool firstRun = true;
378bool instantMenu;
379bool rescanPrompt;
380intstatus;
381unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
382unsigned intdenyBVFlags = kBVFlagEFISystem;
383
384// Set reminder to unload the PXE base code. Neglect to unload
385// the base code will result in a hang or kernel panic.
386gUnloadPXEOnExit = true;
387
388// Record the device that the booter was loaded from.
389gBIOSDev = biosdev & kBIOSDevMask;
390
391// Initialize boot info structure.
392initKernBootStruct();
393
394initBooterLog();
395
396// Setup VGA text mode.
397// Not sure if it is safe to call setVideoMode() before the
398// config table has been loaded. Call video_mode() instead.
399#if DEBUG
400printf("before video_mode\n");
401#endif
402video_mode( 2 ); // 80x25 mono text mode.
403#if DEBUG
404printf("after video_mode\n");
405#endif
406
407// Scan and record the system's hardware information.
408scan_platform();
409
410// First get info for boot volume.
411scanBootVolumes(gBIOSDev, 0);
412bvChain = getBVChainForBIOSDev(gBIOSDev);
413setBootGlobals(bvChain);
414
415// Load boot.plist config file
416status = loadChameleonConfig(&bootInfo->chameleonConfig);
417
418if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet) {
419gBootMode |= kBootModeQuiet;
420}
421
422// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
423if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu) {
424firstRun = false;
425}
426
427// Loading preboot ramdisk if exists.
428loadPrebootRAMDisk();
429
430// Disable rescan option by default
431gEnableCDROMRescan = false;
432
433// Enable it with Rescan=y in system config
434if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)
435&& gEnableCDROMRescan) {
436gEnableCDROMRescan = true;
437}
438
439// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
440rescanPrompt = false;
441if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig)
442&& rescanPrompt && biosDevIsCDROM(gBIOSDev))
443{
444gEnableCDROMRescan = promptForRescanOption();
445}
446
447// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
448if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig)
449&& gScanSingleDrive) {
450gScanSingleDrive = true;
451}
452
453// Create a list of partitions on device(s).
454if (gScanSingleDrive) {
455scanBootVolumes(gBIOSDev, &bvCount);
456} else {
457scanDisks(gBIOSDev, &bvCount);
458}
459
460// Create a separated bvr chain using the specified filters.
461bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
462
463gBootVolume = selectBootVolume(bvChain);
464
465// Intialize module system
466init_module_system();
467
468#if DEBUG
469printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",
470 gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
471printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",
472 gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
473getchar();
474#endif
475
476useGUI = true;
477// Override useGUI default
478getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
479if (useGUI && initGUI())
480{
481// initGUI() returned with an error, disabling GUI.
482useGUI = false;
483}
484
485setBootGlobals(bvChain);
486
487// Parse args, load and start kernel.
488while (1)
489{
490booltryresume, tryresumedefault, forceresume;
491booluseKernelCache = false; // by default don't use prelink kernel cache
492const char*val;
493intlen, ret = -1;
494longflags, sleeptime, time;
495void*binary = (void *)kLoadAddr;
496
497char bootFile[sizeof(bootInfo->bootFile)];
498charbootFilePath[512];
499charkernelCacheFile[512];
500
501// Initialize globals.
502sysConfigValid = false;
503gErrors = false;
504
505status = getBootOptions(firstRun);
506firstRun = false;
507if (status == -1) continue;
508
509status = processBootOptions();
510// Status == 1 means to chainboot
511if ( status ==1 ) break;
512// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
513if ( status == -1 )
514{
515// gBootVolume == NULL usually means the user hit escape.
516if (gBootVolume == NULL)
517{
518freeFilteredBVChain(bvChain);
519
520if (gEnableCDROMRescan)
521rescanBIOSDevice(gBIOSDev);
522
523bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
524setBootGlobals(bvChain);
525setupDeviceList(&bootInfo->themeConfig);
526}
527continue;
528}
529
530// Other status (e.g. 0) means that we should proceed with boot.
531
532// Turn off any GUI elements
533if ( bootArgs->Video.v_display == GRAPHICS_MODE )
534{
535gui.devicelist.draw = false;
536gui.bootprompt.draw = false;
537gui.menu.draw = false;
538gui.infobox.draw = false;
539gui.logo.draw = false;
540drawBackground();
541updateVRAM();
542}
543
544// Find out which version mac os we're booting.
545getOSVersion();
546
547if (platformCPUFeature(CPU_FEATURE_EM64T)) {
548archCpuType = CPU_TYPE_X86_64;
549} else {
550archCpuType = CPU_TYPE_I386;
551}
552
553if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig)) {
554if (strncmp(val, "i386", 4) == 0) {
555archCpuType = CPU_TYPE_I386;
556}
557}
558
559if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig)) {
560if (strncmp(val, "i386", 4) == 0) {
561archCpuType = CPU_TYPE_I386;
562}
563}
564
565// Notify modules that we are attempting to boot
566execute_hook("PreBoot", NULL, NULL, NULL, NULL);
567
568if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig)) {
569tryresume = true;
570tryresumedefault = true;
571} else {
572tryresumedefault = false;
573}
574
575if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig)) {
576forceresume = false;
577}
578
579if (forceresume) {
580tryresume = true;
581tryresumedefault = false;
582}
583
584while (tryresume) {
585const char *tmp;
586BVRef bvr;
587if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
588val = "/private/var/vm/sleepimage";
589
590// Do this first to be sure that root volume is mounted
591ret = GetFileInfo(0, val, &flags, &sleeptime);
592
593if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
594break;
595
596// Can't check if it was hibernation Wake=y is required
597if (bvr->modTime == 0 && tryresumedefault)
598break;
599
600if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
601break;
602
603if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
604#if DEBUG
605printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
606bvr->modTime-sleeptime);
607#endif
608break;
609}
610
611HibernateBoot((char *)val);
612break;
613}
614
615verbose("Loading Darwin %s\n", gMacOSVersion);
616
617// If boot from boot helper partitions and OS is Mountain Lion or Lion use prelink kernel.
618// We need to find a solution to load extra mkext with a prelink kernel.
619if (gBootVolume->flags & kBVFlagBooter && ((checkOSVersion("10.7")) || (checkOSVersion("10.8"))))
620useKernelCache = true;
621else
622useKernelCache = false; // by default don't use prelink kernel cache
623
624getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
625
626if (useKernelCache) do {
627
628// Determine the name of the Kernel Cache
629if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {
630if (val[0] == '\\')
631{
632len--;
633val++;
634}
635strlcpy(kernelCacheFile, val, len + 1);
636} else {
637kernelCacheFile[0] = 0; // Use default kernel cache file
638}
639
640if (gOverrideKernel && kernelCacheFile[0] == 0) {
641verbose("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n",
642bootInfo->bootFile);
643useKernelCache = false;
644break;
645}
646if (gMKextName[0] != 0) {
647verbose("Using a specific MKext Cache (%s), KernelCache will not be used\n",
648gMKextName);
649useKernelCache = false;
650break;
651}
652if (gBootFileType != kBlockDeviceType)
653useKernelCache = false;
654
655} while(0);
656
657do {
658if (useKernelCache) {
659ret = LoadKernelCache(kernelCacheFile, &binary);
660if (ret >= 0)
661break;
662}
663
664bool bootFileWithDevice = false;
665// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
666if (strncmp(bootInfo->bootFile,"bt(",3) == 0 ||
667strncmp(bootInfo->bootFile,"hd(",3) == 0 ||
668strncmp(bootInfo->bootFile,"rd(",3) == 0)
669bootFileWithDevice = true;
670
671// bootFile must start with a / if it not start with a device name
672if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')
673sprintf(bootFile, "/%s", bootInfo->bootFile); // append a leading /
674else
675strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));
676
677// Try to load kernel image from alternate locations on boot helper partitions.
678ret = -1;
679if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice) {
680sprintf(bootFilePath, "com.apple.boot.P%s", bootFile);
681ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
682if (ret == -1)
683{
684sprintf(bootFilePath, "com.apple.boot.R%s", bootFile);
685ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
686if (ret == -1)
687{
688sprintf(bootFilePath, "com.apple.boot.S%s", bootFile);
689ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
690}
691}
692}
693if (ret == -1) {
694// No alternate location found, using the original kernel image path.
695strlcpy(bootFilePath, bootFile,sizeof(bootFilePath));
696}
697
698verbose("Loading kernel %s\n", bootFilePath);
699ret = LoadThinFatFile(bootFilePath, &binary);
700if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
701{
702archCpuType = CPU_TYPE_I386;
703ret = LoadThinFatFile(bootFilePath, &binary);
704}
705} while (0);
706
707clearActivityIndicator();
708
709#if DEBUG
710printf("Pausing...");
711sleep(8);
712#endif
713
714if (ret <= 0) {
715printf("Can't find %s\n", bootFile);
716sleep(1);
717
718if (gBootFileType == kNetworkDeviceType) {
719// Return control back to PXE. Don't unload PXE base code.
720gUnloadPXEOnExit = false;
721break;
722}
723pause();
724
725} else {
726/* Won't return if successful. */
727ret = ExecKernel(binary);
728}
729}
730
731// chainboot
732if (status == 1) {
733// if we are already in graphics-mode,
734if (getVideoMode() == GRAPHICS_MODE) {
735setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode.
736}
737}
738
739if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {
740nbpUnloadBaseCode();
741}
742}
743
744/*!
745Selects a new BIOS device, taking care to update the global state appropriately.
746 */
747/*
748static void selectBiosDevice(void)
749{
750struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
751CacheReset();
752diskFreeMap(oldMap);
753oldMap = NULL;
754
755int dev = selectAlternateBootDevice(gBIOSDev);
756
757BVRef bvchain = scanBootVolumes(dev, 0);
758BVRef bootVol = selectBootVolume(bvchain);
759gBootVolume = bootVol;
760setRootVolume(bootVol);
761gBIOSDev = dev;
762}
763*/
764
765bool checkOSVersion(const char * version)
766{
767return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
768&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));
769}
770
771static void getOSVersion()
772{
773strlcpy(gMacOSVersion, gBootVolume->OSVersion, sizeof(gMacOSVersion));
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: 1987