Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/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 * INTEL CORPORATION PROPRIETARY INFORMATION
33 *
34 * This software is supplied under the terms of a license agreement or
35 * nondisclosure agreement with Intel Corporation and may not be copied
36 * nor disclosed except in accordance with the terms of that agreement.
37 *
38 *Copyright 1988, 1989 by Intel Corporation
39 *
40 *
41 * Copyright 1993 NeXT Computer, Inc.
42 * All rights reserved.
43 *
44 * Completely reworked by Sam Streeper (sam_s@NeXT.com)
45 * Reworked again by Curtis Galloway (galloway@NeXT.com)
46 */
47
48#include "boot.h"
49#include "bootstruct.h"
50#include "fake_efi.h"
51#include "sl.h"
52#include "libsa.h"
53#include "ramdisk.h"
54#include "gui.h"
55#include "platform.h"
56#include "modules.h"
57
58/*
59 * How long to wait (in seconds) to load the
60 * kernel after displaying the "boot:" prompt.
61 */
62#define kBootErrorTimeout 5
63
64boolgOverrideKernel, gEnableCDROMRescan, gScanSingleDrive, useGUI;
65static boolgUnloadPXEOnExit = false;
66
67static chargCacheNameAdler[64 + 256];
68char*gPlatformName = gCacheNameAdler;
69
70chargRootDevice[ROOT_DEVICE_SIZE];
71chargMKextName[512];
72chargMacOSVersion[8];
73intbvCount = 0, gDeviceCount = 0;
74//intmenucount = 0;
75longgBootMode; /* defaults to 0 == kBootModeNormal */
76BVRefbvr, menuBVR, bvChain;
77
78static boolcheckOSVersion(const char * version);
79static voidgetOSVersion();
80static unsigned longAdler32(unsigned char *buffer, long length);
81//static voidselectBiosDevice(void);
82
83/** options.c **/
84extern char* msgbuf;
85void showTextBuffer(char *buf, int size);
86
87
88//==========================================================================
89// Zero the BSS.
90
91static void zeroBSS(void)
92{
93extern char bss_start __asm("section$start$__DATA$__bss");
94extern char bss_end __asm("section$end$__DATA$__bss");
95extern char common_start __asm("section$start$__DATA$__common");
96extern char common_end __asm("section$end$__DATA$__common");
97
98bzero(&bss_start, (&bss_end - &bss_start));
99bzero(&common_start, (&common_end - &common_start));
100}
101
102//==========================================================================
103// Malloc error function
104
105static void malloc_error(char *addr, size_t size, const char *file, int line)
106{
107stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",
108 (unsigned)addr, (unsigned)size, file, line);
109}
110
111//==========================================================================
112//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
113//
114void initialize_runtime(void)
115{
116zeroBSS();
117malloc_init(0, 0, 0, malloc_error);
118}
119
120//==========================================================================
121// execKernel - Load the kernel image (mach-o) and jump to its entry point.
122
123static int ExecKernel(void *binary)
124{
125intret;
126entry_tkernelEntry;
127
128bootArgs->kaddr = bootArgs->ksize = 0;
129execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL);
130
131ret = DecodeKernel(binary,
132 &kernelEntry,
133 (char **) &bootArgs->kaddr,
134 (int *)&bootArgs->ksize );
135
136if ( ret != 0 )
137return ret;
138
139// Reserve space for boot args
140reserveKernBootStruct();
141
142// Notify modules that the kernel has been decoded
143execute_hook("DecodedKernel", (void*)binary, (void*)bootArgs->kaddr, (void*)bootArgs->ksize, NULL);
144
145setupFakeEfi();
146
147// Load boot drivers from the specifed root path.
148//if (!gHaveKernelCache)
149LoadDrivers("/");
150
151execute_hook("DriversLoaded", (void*)binary, NULL, NULL, NULL);
152
153clearActivityIndicator();
154
155if (gErrors) {
156printf("Errors encountered while starting up the computer.\n");
157printf("Pausing %d seconds...\n", kBootErrorTimeout);
158sleep(kBootErrorTimeout);
159}
160
161md0Ramdisk();
162
163verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");
164verbose("Boot Args: %s\n", bootArgs->CommandLine);
165
166// Cleanup the PXE base code.
167
168if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
169if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess ) {
170printf("nbpUnloadBaseCode error %d\n", (int) ret);
171sleep(2);
172}
173}
174
175bool dummyVal;
176if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal) {
177showTextBuffer(msgbuf, strlen(msgbuf));
178}
179
180usb_loop();
181
182// If we were in text mode, switch to graphics mode.
183// This will draw the boot graphics unless we are in
184// verbose mode.
185if (gVerboseMode) {
186setVideoMode( GRAPHICS_MODE, 0 );
187} else {
188drawBootGraphics();
189}
190setupBooterLog();
191
192finalizeBootStruct();
193
194// Jump to kernel's entry point. There's no going back now.
195if ((checkOSVersion("10.7")) || (checkOSVersion("10.8")) || (checkOSVersion("10.9")))
196{
197
198// Notify modules that the kernel is about to be started
199execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);
200
201// Masking out so that Lion doesn't doublefault
202
203startprog( kernelEntry, bootArgs );
204} else {
205// Notify modules that the kernel is about to be started
206execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);
207
208startprog( kernelEntry, bootArgsPreLion );
209}
210
211// Not reached
212return 0;
213}
214
215
216//==========================================================================
217// LoadKernelCache - Try to load Kernel Cache.
218// return the length of the loaded cache file or -1 on error
219long LoadKernelCache(const char* cacheFile, void **binary)
220{
221charkernelCacheFile[512];
222charkernelCachePath[512];
223longflags, time, cachetime, kerneltime, exttime, ret=-1;
224unsigned long adler32;
225
226if((gBootMode & kBootModeSafe) != 0) {
227verbose("Kernel Cache ignored.\n");
228return -1;
229}
230
231// Use specify kernel cache file if not empty
232if (cacheFile[0] != 0)
233{
234strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));
235}
236else
237{
238// Lion, Mountain Lion and Mavericks prelink kernel cache file
239if ((checkOSVersion("10.7")) || (checkOSVersion("10.8")) || (checkOSVersion("10.9")))
240{
241snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);
242}
243// Snow Leopard prelink kernel cache file
244else if (checkOSVersion("10.6")) {
245snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",
246(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");
247int lnam = strlen(kernelCacheFile) + 9; //with adler32
248
249char* name;
250long prev_time = 0;
251
252struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
253/* TODO: handle error? */
254if (cacheDir) {
255while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0) {
256if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
257&& strstr(name, kernelCacheFile) && (name[lnam] != '.')) {
258snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);
259prev_time = time;
260}
261}
262}
263closedir(cacheDir);
264} else {
265// Reset cache name.
266bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
267snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64,
268"%s,%s",
269gRootDevice, bootInfo->bootFile);
270adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
271snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);
272}
273}
274/* Issue: http://forge.voodooprojects.org/p/chameleon/issues/270/
275// kernelCacheFile must start with a /
276if (kernelCacheFile[0] != '/')
277{
278char *str = strdup(kernelCacheFile);
279if (str == NULL)
280{
281return -1;
282}
283sprintf(kernelCacheFile, "/%s", str);
284free(str);
285}
286*/
287// Check if the kernel cache file exists
288ret = -1;
289
290// If boot from a boot helper partition check the kernel cache file on it
291if (gBootVolume->flags & kBVFlagBooter) {
292snprintf(kernelCachePath, sizeof(kernelCachePath), "com.apple.boot.P%s", kernelCacheFile);
293ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
294if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
295snprintf(kernelCachePath, sizeof(kernelCachePath), "com.apple.boot.R%s", kernelCacheFile);
296ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
297if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
298snprintf(kernelCachePath, sizeof(kernelCachePath), "com.apple.boot.S%s", kernelCacheFile);
299ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
300if ((flags & kFileTypeMask) != kFileTypeFlat) {
301ret = -1;
302}
303}
304}
305}
306// If not found, use the original kernel cache path.
307if (ret == -1) {
308strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));
309ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
310if ((flags & kFileTypeMask) != kFileTypeFlat) {
311ret = -1;
312}
313}
314
315// Exit if kernel cache file wasn't found
316if (ret == -1) {
317verbose("No Kernel Cache File '%s' found\n", kernelCacheFile);
318return -1;
319}
320
321// Check if the kernel cache file is more recent (mtime)
322// than the kernel file or the S/L/E directory
323ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
324// Check if the kernel file is more recent than the cache file
325if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat)
326&& (kerneltime > cachetime)) {
327verbose("Kernel file (%s) is more recent than KernelCache (%s), ignoring KernelCache\n",
328bootInfo->bootFile, kernelCacheFile);
329return -1;
330}
331
332ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
333// Check if the S/L/E directory time is more recent than the cache file
334if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
335&& (exttime > cachetime)) {
336verbose("/System/Library/Extensions is more recent than KernelCache (%s), ignoring KernelCache\n",
337kernelCacheFile);
338return -1;
339}
340
341// Since the kernel cache file exists and is the most recent try to load it
342verbose("Loading kernel cache %s\n", kernelCachePath);
343
344ret = LoadThinFatFile(kernelCachePath, binary);
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 = true; // by default try to use the prelinked kernel
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
614getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
615if (useKernelCache) do {
616
617// Determine the name of the Kernel Cache
618if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {
619if (val[0] == '\\')
620{
621len--;
622val++;
623}
624/* FIXME: check len vs sizeof(kernelCacheFile) */
625strlcpy(kernelCacheFile, val, len + 1);
626} else {
627kernelCacheFile[0] = 0; // Use default kernel cache file
628}
629
630if (gOverrideKernel && kernelCacheFile[0] == 0) {
631verbose("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n",
632bootInfo->bootFile);
633useKernelCache = false;
634break;
635}
636if (gMKextName[0] != 0) {
637verbose("Using a specific MKext Cache (%s), KernelCache will not be used\n",
638gMKextName);
639useKernelCache = false;
640break;
641}
642if (gBootFileType != kBlockDeviceType)
643useKernelCache = false;
644
645} while(0);
646
647do {
648if (useKernelCache) {
649ret = LoadKernelCache(kernelCacheFile, &binary);
650if (ret >= 0)
651break;
652}
653
654bool bootFileWithDevice = false;
655// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
656if (strncmp(bootInfo->bootFile,"bt(",3) == 0 ||
657strncmp(bootInfo->bootFile,"hd(",3) == 0 ||
658strncmp(bootInfo->bootFile,"rd(",3) == 0)
659bootFileWithDevice = true;
660
661// bootFile must start with a / if it not start with a device name
662if (!bootFileWithDevice && (bootInfo->bootFile)[0] != '/')
663snprintf(bootFile, sizeof(bootFile), "/%s", bootInfo->bootFile); // append a leading /
664else
665strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));
666
667// Try to load kernel image from alternate locations on boot helper partitions.
668ret = -1;
669if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice) {
670snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);
671ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
672if (ret == -1)
673{
674snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);
675ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
676if (ret == -1)
677{
678snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);
679ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
680}
681}
682}
683if (ret == -1) {
684// No alternate location found, using the original kernel image path.
685strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));
686}
687
688verbose("Loading kernel %s\n", bootFilePath);
689ret = LoadThinFatFile(bootFilePath, &binary);
690if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
691{
692archCpuType = CPU_TYPE_I386;
693ret = LoadThinFatFile(bootFilePath, &binary);
694}
695} while (0);
696
697clearActivityIndicator();
698
699#if DEBUG
700printf("Pausing...");
701sleep(8);
702#endif
703
704if (ret <= 0) {
705printf("Can't find %s\n", bootFile);
706sleep(1);
707
708if (gBootFileType == kNetworkDeviceType) {
709// Return control back to PXE. Don't unload PXE base code.
710gUnloadPXEOnExit = false;
711break;
712}
713pause();
714
715} else {
716/* Won't return if successful. */
717ret = ExecKernel(binary);
718}
719}
720
721// chainboot
722if (status == 1) {
723// if we are already in graphics-mode,
724if (getVideoMode() == GRAPHICS_MODE) {
725setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode.
726}
727}
728
729if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {
730nbpUnloadBaseCode();
731}
732}
733
734/*!
735Selects a new BIOS device, taking care to update the global state appropriately.
736 */
737/*
738static void selectBiosDevice(void)
739{
740struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
741CacheReset();
742diskFreeMap(oldMap);
743oldMap = NULL;
744
745int dev = selectAlternateBootDevice(gBIOSDev);
746
747BVRef bvchain = scanBootVolumes(dev, 0);
748BVRef bootVol = selectBootVolume(bvchain);
749gBootVolume = bootVol;
750setRootVolume(bootVol);
751gBIOSDev = dev;
752}
753*/
754
755bool checkOSVersion(const char * version)
756{
757return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
758&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));
759}
760
761static void getOSVersion()
762{
763strncpy(gMacOSVersion, gBootVolume->OSVersion, sizeof(gMacOSVersion));
764}
765
766#define BASE 65521L /* largest prime smaller than 65536 */
767#define NMAX 5000
768// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
769
770#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
771#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
772#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
773#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
774#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
775
776unsigned long Adler32(unsigned char *buf, long len)
777{
778unsigned long s1 = 1; // adler & 0xffff;
779unsigned long s2 = 0; // (adler >> 16) & 0xffff;
780unsigned long result;
781int k;
782
783while (len > 0) {
784k = len < NMAX ? len : NMAX;
785len -= k;
786while (k >= 16) {
787DO16(buf);
788buf += 16;
789k -= 16;
790}
791if (k != 0) do {
792s1 += *buf++;
793s2 += s1;
794} while (--k);
795s1 %= BASE;
796s2 %= BASE;
797}
798result = (s2 << 16) | s1;
799return OSSwapHostToBigInt32(result);
800}
801

Archive Download this file

Revision: 2323