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

Archive Download this file

Revision: 2476