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

Archive Download this file

Revision: 2527