Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1464