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

Archive Download this file

Revision: HEAD