Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2469