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

Archive Download this file

Revision: 2479