Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2403