Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2453