Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1820