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

Archive Download this file

Revision: 2560