Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1725