Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/boot2/boot.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the "License").You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25/*
26 * Mach Operating System
27 * Copyright (c) 1990 Carnegie-Mellon University
28 * Copyright (c) 1989 Carnegie-Mellon University
29 * All rights reserved. The CMU software License Agreement specifies
30 * the terms and conditions for use and redistribution.
31 */
32
33/*
34 *INTEL CORPORATION PROPRIETARY INFORMATION
35 *
36 *This software is supplied under the terms of a licenseagreement or
37 *nondisclosure agreement with Intel Corporation and may not be copied
38 *nor disclosed except in accordance with the terms of that agreement.
39 *
40 *Copyright 1988, 1989 by Intel Corporation
41 */
42
43/*
44 * Copyright 1993 NeXT Computer, Inc.
45 * All rights reserved.
46 */
47
48/*
49 * Completely reworked by Sam Streeper (sam_s@NeXT.com)
50 * Reworked again by Curtis Galloway (galloway@NeXT.com)
51 */
52
53#include "boot.h"
54#include "bootstruct.h"
55#include "fake_efi.h"
56#include "sl.h"
57#include "libsa.h"
58#include "ramdisk.h"
59#include "gui.h"
60#include "platform.h"
61#include "modules.h"
62#include "device_tree.h"
63
64#ifndef DEBUG_BOOT2
65#define DEBUG_BOOT2 0
66#endif
67
68#if DEBUG_BOOT2
69#define DBG(x...)printf(x)
70#else
71#define DBG(x...)msglog(x)
72#endif
73
74/*
75 * How long to wait (in seconds) to load the
76 * kernel after displaying the "boot:" prompt.
77 */
78#define kBootErrorTimeout 5
79
80boolgOverrideKernel, gEnableCDROMRescan, gScanSingleDrive, useGUI;
81static boolgUnloadPXEOnExit = false;
82
83static chargCacheNameAdler[64 + 256];
84char*gPlatformName = gCacheNameAdler;
85
86chargRootDevice[ROOT_DEVICE_SIZE];
87chargMKextName[512];
88chargMacOSVersion[8];
89intbvCount = 0, gDeviceCount = 0;
90//intmenucount = 0;
91longgBootMode; /* defaults to 0 == kBootModeNormal */
92BVRefbvr, menuBVR, bvChain;
93
94static boolcheckOSVersion(const char * version);
95static voidgetOSVersion();
96static unsigned longAdler32(unsigned char *buffer, long length);
97//static voidselectBiosDevice(void);
98
99/** options.c **/
100extern char* msgbuf;
101void showTextBuffer(char *buf, int size);
102
103
104//==========================================================================
105// Zero the BSS.
106
107static void zeroBSS(void)
108{
109extern char bss_start __asm("section$start$__DATA$__bss");
110extern char bss_end __asm("section$end$__DATA$__bss");
111extern char common_start __asm("section$start$__DATA$__common");
112extern char common_end __asm("section$end$__DATA$__common");
113
114bzero(&bss_start, (&bss_end - &bss_start));
115bzero(&common_start, (&common_end - &common_start));
116}
117
118//==========================================================================
119// Malloc error function
120
121static void malloc_error(char *addr, size_t size, const char *file, int line)
122{
123stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",
124 (unsigned)addr, (unsigned)size, file, line);
125}
126
127//==========================================================================
128//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
129//
130void initialize_runtime(void)
131{
132zeroBSS();
133malloc_init(0, 0, 0, malloc_error);
134}
135
136//==========================================================================
137// execKernel - Load the kernel image (mach-o) and jump to its entry point.
138
139static int ExecKernel(void *binary)
140{
141intret;
142entry_tkernelEntry;
143
144bootArgs->kaddr = bootArgs->ksize = 0;
145execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL);
146
147ret = DecodeKernel(binary,
148 &kernelEntry,
149 (char **) &bootArgs->kaddr,
150 (int *)&bootArgs->ksize );
151
152if ( ret != 0 )
153return ret;
154
155// Reserve space for boot args
156reserveKernBootStruct();
157
158// Notify modules that the kernel has been decoded
159execute_hook("DecodedKernel", (void*)binary, (void*)bootArgs->kaddr, (void*)bootArgs->ksize, NULL);
160
161setupFakeEfi();
162
163// Load boot drivers from the specifed root path.
164//if (!gHaveKernelCache)
165LoadDrivers("/");
166
167execute_hook("DriversLoaded", (void*)binary, NULL, NULL, NULL);
168
169clearActivityIndicator();
170
171if (gErrors) {
172printf("Errors encountered while starting up the computer.\n");
173printf("Pausing %d seconds...\n", kBootErrorTimeout);
174sleep(kBootErrorTimeout);
175}
176
177md0Ramdisk();
178
179// Cleanup the PXE base code.
180
181if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
182if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess ) {
183printf("nbpUnloadBaseCode error %d\n", (int) ret);
184sleep(2);
185}
186}
187
188bool dummyVal;
189if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal) {
190showTextBuffer(msgbuf, strlen(msgbuf));
191}
192
193usb_loop();
194
195// If we were in text mode, switch to graphics mode.
196// This will draw the boot graphics unless we are in
197// verbose mode.
198if (gVerboseMode) {
199setVideoMode( GRAPHICS_MODE, 0 );
200} else {
201drawBootGraphics();
202}
203
204DBG("Starting Darwin/%s [%s]\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64", gDarwinBuildVerStr);
205DBG("Boot Args: %s\n", bootArgs->CommandLine);
206
207setupBooterLog();
208
209finalizeBootStruct();
210
211// Jump to kernel's entry point. There's no going back now.
212if ((checkOSVersion("10.7")) || (checkOSVersion("10.8")) || (checkOSVersion("10.9")))
213{
214
215// Notify modules that the kernel is about to be started
216execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);
217
218// Masking out so that Lion doesn't doublefault
219outb(0x21, 0xff);/* Maskout all interrupts Pic1 */
220outb(0xa1, 0xff);/* Maskout all interrupts Pic2 */
221
222startprog( kernelEntry, bootArgs );
223} else {
224// Notify modules that the kernel is about to be started
225execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);
226
227startprog( kernelEntry, bootArgsPreLion );
228}
229
230// Not reached
231return 0;
232}
233
234
235//==========================================================================
236// LoadKernelCache - Try to load Kernel Cache.
237// return the length of the loaded cache file or -1 on error
238long LoadKernelCache(const char* cacheFile, void **binary)
239{
240charkernelCacheFile[512];
241charkernelCachePath[512];
242longflags, time, cachetime, kerneltime, exttime, ret=-1;
243unsigned long adler32;
244
245if((gBootMode & kBootModeSafe) != 0) {
246DBG("Kernel Cache ignored.\n");
247return -1;
248}
249
250// Use specify kernel cache file if not empty
251if (cacheFile[0] != 0)
252{
253strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));
254verbose("Specified kernel cache file path = %s\n", cacheFile);
255} else {
256// Lion, Mountain Lion and Mavericks prelink kernel cache file
257if ((checkOSVersion("10.7")) || (checkOSVersion("10.8")) || (checkOSVersion("10.9")))
258{
259snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%skernelcache", kDefaultCachePathSnow);
260verbose("10.7, 10.8 & 10.9 kernel cache file path = %s\n", kernelCacheFile);}
261// Snow Leopard prelink kernel cache file
262else if (checkOSVersion("10.6")) {
263snprintf(kernelCacheFile, sizeof(kernelCacheFile), "kernelcache_%s",
264(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");
265
266intlnam = strlen(kernelCacheFile) + 9; //with adler32
267char*name;
268longprev_time = 0;
269
270structdirstuff* cacheDir = opendir(kDefaultCachePathSnow);
271
272/* TODO: handle error? */
273if (cacheDir) {
274while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0) {
275if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
276&& strstr(name, kernelCacheFile) && (name[lnam] != '.')) {
277snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s%s", kDefaultCachePathSnow, name);
278prev_time = time;
279}
280}
281verbose("Snow Leopard kernel cache file path = %s\n", kernelCacheFile);
282}
283closedir(cacheDir);
284} else {
285// Reset cache name.
286bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
287snprintf(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64,
288"%s,%s",
289gRootDevice, bootInfo->bootFile);
290adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
291snprintf(kernelCacheFile, sizeof(kernelCacheFile), "%s.%08lX", kDefaultCachePathLeo, adler32);
292verbose("Reseted kernel cache file path = %s\n", kernelCacheFile);
293}
294}
295
296// Check if the kernel cache file exists
297ret = -1;
298
299// If boot from a boot helper partition check the kernel cache file on it
300if (gBootVolume->flags & kBVFlagBooter) {
301snprintf(kernelCachePath, sizeof(kernelCachePath), "com.apple.boot.P%s", kernelCacheFile);
302ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
303if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
304snprintf(kernelCachePath, sizeof(kernelCachePath), "com.apple.boot.R%s", kernelCacheFile);
305ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
306if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
307snprintf(kernelCachePath, sizeof(kernelCachePath), "com.apple.boot.S%s", kernelCacheFile);
308ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
309if ((flags & kFileTypeMask) != kFileTypeFlat) {
310ret = -1;
311}
312}
313}
314}
315// If not found, use the original kernel cache path.
316if (ret == -1) {
317strlcpy(kernelCachePath, kernelCacheFile, sizeof(kernelCachePath));
318ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
319if ((flags & kFileTypeMask) != kFileTypeFlat) {
320ret = -1;
321}
322}
323
324// Exit if kernel cache file wasn't found
325if (ret == -1) {
326DBG("No Kernel Cache File '%s' found\n", kernelCacheFile);
327return -1;
328}
329
330// Check if the kernel cache file is more recent (mtime)
331// than the kernel file or the S/L/E directory
332ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
333
334// Check if the kernel file is more recent than the cache file
335if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat)
336&& (kerneltime > cachetime))
337{
338DBG("Kernel file (%s) is more recent than Kernel Cache (%s)! Ignoring Kernel Cache.\n",
339bootInfo->bootFile, kernelCacheFile);
340return -1;
341}
342
343ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
344// Check if the S/L/E directory time is more recent than the cache file
345if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
346&& (exttime > cachetime))
347{
348DBG("Folder: '/System/Library/Extensions' is more recent than Kernel Cache file (%s)! Ignoring Kernel Cache.\n",
349kernelCacheFile);
350return -1;
351}
352
353// Since the kernel cache file exists and is the most recent try to load it
354DBG("Loading kernel cache: '%s'\n", kernelCachePath);
355
356ret = LoadThinFatFile(kernelCachePath, binary);
357return ret; // ret contain the length of the binary
358}
359
360//==========================================================================
361// This is the entrypoint from real-mode which functions exactly as it did
362// before. Multiboot does its own runtime initialization, does some of its
363// own things, and then calls common_boot.
364void boot(int biosdev)
365{
366initialize_runtime();
367// Enable A20 gate before accessing memory above 1Mb.
368enableA20();
369common_boot(biosdev);
370}
371
372//==========================================================================
373// The 'main' function for the booter. Called by boot0 when booting
374// from a block device, or by the network booter.
375//
376// arguments:
377// biosdev - Value passed from boot1/NBP to specify the device
378// that the booter was loaded from.
379//
380// If biosdev is kBIOSDevNetwork, then this function will return if
381// booting was unsuccessful. This allows the PXE firmware to try the
382// next boot device on its list.
383void common_boot(int biosdev)
384{
385bool quiet;
386bool firstRun = true;
387bool instantMenu;
388bool rescanPrompt;
389intstatus;
390unsigned intallowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
391unsigned intdenyBVFlags = kBVFlagEFISystem;
392
393// Set reminder to unload the PXE base code. Neglect to unload
394// the base code will result in a hang or kernel panic.
395gUnloadPXEOnExit = true;
396
397// Record the device that the booter was loaded from.
398gBIOSDev = biosdev & kBIOSDevMask;
399
400// Initialize boot-log
401initBooterLog();
402
403// Initialize boot info structure.
404initKernBootStruct();
405
406// Setup VGA text mode.
407// Not sure if it is safe to call setVideoMode() before the
408// config table has been loaded. Call video_mode() instead.
409#if DEBUG
410printf("before video_mode\n");
411#endif
412video_mode( 2 ); // 80x25 mono text mode.
413#if DEBUG
414printf("after video_mode\n");
415#endif
416
417// Scan and record the system's hardware information.
418scan_platform();
419
420// First get info for boot volume.
421scanBootVolumes(gBIOSDev, 0);
422bvChain = getBVChainForBIOSDev(gBIOSDev);
423setBootGlobals(bvChain);
424
425// Load boot.plist config file
426status = loadChameleonConfig(&bootInfo->chameleonConfig, bvChain);
427
428if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet) {
429gBootMode |= kBootModeQuiet;
430}
431
432// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
433if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu) {
434firstRun = false;
435}
436
437// Loading preboot ramdisk if exists.
438loadPrebootRAMDisk();
439
440// Disable rescan option by default
441gEnableCDROMRescan = false;
442
443// Enable it with Rescan=y in system config
444if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig)
445&& gEnableCDROMRescan) {
446gEnableCDROMRescan = true;
447}
448
449// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
450rescanPrompt = false;
451if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig)
452&& rescanPrompt && biosDevIsCDROM(gBIOSDev))
453{
454gEnableCDROMRescan = promptForRescanOption();
455}
456
457// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
458if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig)
459&& gScanSingleDrive) {
460gScanSingleDrive = true;
461}
462
463// Create a list of partitions on device(s).
464if (gScanSingleDrive) {
465scanBootVolumes(gBIOSDev, &bvCount);
466} else {
467scanDisks(gBIOSDev, &bvCount);
468}
469
470// Create a separated bvr chain using the specified filters.
471bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
472
473gBootVolume = selectBootVolume(bvChain);
474
475// Intialize module system
476init_module_system();
477
478#if DEBUG
479printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",
480 gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
481printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",
482 gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
483getchar();
484#endif
485
486useGUI = true;
487// Override useGUI default
488getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
489if (useGUI && initGUI())
490{
491// initGUI() returned with an error, disabling GUI.
492useGUI = false;
493}
494
495setBootGlobals(bvChain);
496
497// Parse args, load and start kernel.
498while (1)
499{
500booltryresume, tryresumedefault, forceresume;
501booluseKernelCache = true; // by default try to use the prelinked kernel
502const char*val;
503intlen, ret = -1;
504longflags, sleeptime, time;
505void*binary = (void *)kLoadAddr;
506
507char bootFile[sizeof(bootInfo->bootFile)];
508charbootFilePath[512];
509charkernelCacheFile[512];
510
511// Initialize globals.
512sysConfigValid = false;
513gErrors = false;
514
515status = getBootOptions(firstRun);
516firstRun = false;
517if (status == -1) continue;
518
519status = processBootOptions();
520// Status == 1 means to chainboot
521if ( status ==1 ) break;
522// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
523if ( status == -1 )
524{
525// gBootVolume == NULL usually means the user hit escape.
526if (gBootVolume == NULL)
527{
528freeFilteredBVChain(bvChain);
529
530if (gEnableCDROMRescan)
531rescanBIOSDevice(gBIOSDev);
532
533bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
534setBootGlobals(bvChain);
535setupDeviceList(&bootInfo->themeConfig);
536}
537continue;
538}
539
540// Other status (e.g. 0) means that we should proceed with boot.
541
542// Turn off any GUI elements
543if ( bootArgs->Video.v_display == GRAPHICS_MODE )
544{
545gui.devicelist.draw = false;
546gui.bootprompt.draw = false;
547gui.menu.draw = false;
548gui.infobox.draw = false;
549gui.logo.draw = false;
550drawBackground();
551updateVRAM();
552}
553
554// Find out which version mac os we're booting.
555getOSVersion();
556
557if (platformCPUFeature(CPU_FEATURE_EM64T)) {
558archCpuType = CPU_TYPE_X86_64;
559} else {
560archCpuType = CPU_TYPE_I386;
561}
562
563if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig)) {
564if (strncmp(val, "i386", 4) == 0) {
565archCpuType = CPU_TYPE_I386;
566}
567}
568
569if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig)) {
570if (strncmp(val, "i386", 4) == 0) {
571archCpuType = CPU_TYPE_I386;
572}
573}
574
575// Notify modules that we are attempting to boot
576execute_hook("PreBoot", NULL, NULL, NULL, NULL);
577
578if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig)) {
579tryresume = true;
580tryresumedefault = true;
581} else {
582tryresumedefault = false;
583}
584
585if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig)) {
586forceresume = false;
587}
588
589if (forceresume) {
590tryresume = true;
591tryresumedefault = false;
592}
593
594while (tryresume) {
595const char *tmp;
596BVRef bvr;
597if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
598val = "/private/var/vm/sleepimage";
599
600// Do this first to be sure that root volume is mounted
601ret = GetFileInfo(0, val, &flags, &sleeptime);
602
603if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
604break;
605
606// Can't check if it was hibernation Wake=y is required
607if (bvr->modTime == 0 && tryresumedefault)
608break;
609
610if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
611break;
612
613if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
614#if DEBUG
615printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
616bvr->modTime-sleeptime);
617#endif
618break;
619}
620
621HibernateBoot((char *)val);
622break;
623}
624
625getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
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}
635/* FIXME: check len vs sizeof(kernelCacheFile) */
636strlcpy(kernelCacheFile, val, len + 1);
637} else {
638kernelCacheFile[0] = 0; // Use default kernel cache file
639}
640
641if (gOverrideKernel && kernelCacheFile[0] == 0) {
642DBG("Using a non default kernel (%s) without specifying 'Kernel Cache' path, KernelCache will not be used\n", bootInfo->bootFile);
643useKernelCache = false;
644break;
645}
646if (gMKextName[0] != 0) {
647DBG("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] != '/')
673{
674snprintf(bootFile, sizeof(bootFile), "/%s", bootInfo->bootFile); // append a leading /
675} else {
676strlcpy(bootFile, bootInfo->bootFile, sizeof(bootFile));
677}
678
679// Try to load kernel image from alternate locations on boot helper partitions.
680ret = -1;
681if ((gBootVolume->flags & kBVFlagBooter) && !bootFileWithDevice) {
682snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.P%s", bootFile);
683ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
684if (ret == -1)
685{
686snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.R%s", bootFile);
687ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
688if (ret == -1)
689{
690snprintf(bootFilePath, sizeof(bootFilePath), "com.apple.boot.S%s", bootFile);
691ret = GetFileInfo(NULL, bootFilePath, &flags, &time);
692}
693}
694}
695if (ret == -1) {
696// No alternate location found, using the original kernel image path.
697strlcpy(bootFilePath, bootFile, sizeof(bootFilePath));
698}
699
700DBG("Loading kernel: '%s'\n", bootFilePath);
701ret = LoadThinFatFile(bootFilePath, &binary);
702if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
703{
704archCpuType = CPU_TYPE_I386;
705ret = LoadThinFatFile(bootFilePath, &binary);
706}
707} while (0);
708
709clearActivityIndicator();
710
711#if DEBUG
712printf("Pausing...");
713sleep(8);
714#endif
715
716if (ret <= 0)
717{
718printf("Can't find %s\n", bootFile);
719sleep(1);
720
721if (gBootFileType == kNetworkDeviceType)
722{
723// Return control back to PXE. Don't unload PXE base code.
724gUnloadPXEOnExit = false;
725break;
726}
727pause();
728
729} else {
730/* Won't return if successful. */
731ret = ExecKernel(binary);
732}
733}
734
735// chainboot
736if (status == 1) {
737// if we are already in graphics-mode,
738if (getVideoMode() == GRAPHICS_MODE) {
739setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode.
740}
741}
742
743if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {
744nbpUnloadBaseCode();
745}
746}
747
748/*!
749Selects a new BIOS device, taking care to update the global state appropriately.
750 */
751/*
752static void selectBiosDevice(void)
753{
754struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
755CacheReset();
756diskFreeMap(oldMap);
757oldMap = NULL;
758
759int dev = selectAlternateBootDevice(gBIOSDev);
760
761BVRef bvchain = scanBootVolumes(dev, 0);
762BVRef bootVol = selectBootVolume(bvchain);
763gBootVolume = bootVol;
764setRootVolume(bootVol);
765gBIOSDev = dev;
766}
767*/
768
769bool checkOSVersion(const char * version)
770{
771return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1])
772&& (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));
773}
774
775static void getOSVersion()
776{
777strncpy(gMacOSVersion, gBootVolume->OSVersion, sizeof(gMacOSVersion));
778}
779
780#define BASE 65521L /* largest prime smaller than 65536 */
781#define NMAX 5000
782// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
783
784#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
785#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
786#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
787#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
788#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
789
790unsigned long Adler32(unsigned char *buf, long len)
791{
792unsigned long s1 = 1; // adler & 0xffff;
793unsigned long s2 = 0; // (adler >> 16) & 0xffff;
794unsigned long result;
795int k;
796
797while (len > 0) {
798k = len < NMAX ? len : NMAX;
799len -= k;
800while (k >= 16) {
801DO16(buf);
802buf += 16;
803k -= 16;
804}
805if (k != 0) do {
806s1 += *buf++;
807s2 += s1;
808} while (--k);
809s1 %= BASE;
810s2 %= BASE;
811}
812result = (s2 << 16) | s1;
813return OSSwapHostToBigInt32(result);
814}
815

Archive Download this file

Revision: 2423