Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/boot2/boot.c

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

Archive Download this file

Revision: 2456