Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2457