Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2807