Chameleon

Chameleon Svn Source Tree

Root/branches/Jief/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 bss_start __asm("section$start$__DATA$__bss");
99 extern char bss_end __asm("section$end$__DATA$__bss");
100 extern char common_start __asm("section$start$__DATA$__common");
101 extern char common_end __asm("section$end$__DATA$__common");
102
103bzero(&bss_start, (&bss_end - &bss_start));
104bzero(&common_start, (&common_end - &common_start));
105}
106
107//==========================================================================
108// Malloc error function
109
110static void malloc_error(char *addr, size_t size, const char *file, int line)
111{
112stop("\nMemory allocation error! Addr: 0x%x, Size: 0x%x, File: %s, Line: %d\n",
113 (unsigned)addr, (unsigned)size, file, line);
114}
115
116//==========================================================================
117//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
118//
119void initialize_runtime(void)
120{
121zeroBSS();
122malloc_init(0, 0, 0, malloc_error);
123}
124
125//==========================================================================
126// execKernel - Load the kernel image (mach-o) and jump to its entry point.
127
128static int ExecKernel(void *binary)
129{
130intret;
131entry_tkernelEntry;
132
133bootArgs->kaddr = bootArgs->ksize = 0;
134execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL);
135
136ret = DecodeKernel(binary,
137 &kernelEntry,
138 (char **) &bootArgs->kaddr,
139 (int *)&bootArgs->ksize );
140
141if ( ret != 0 )
142return ret;
143
144// Reserve space for boot args
145reserveKernBootStruct();
146
147// Notify modules that the kernel has been decoded
148execute_hook("DecodedKernel", (void*)binary, (void*)bootArgs->kaddr, (void*)bootArgs->ksize, NULL);
149
150setupFakeEfi();
151
152// Load boot drivers from the specifed root path.
153//if (!gHaveKernelCache)
154LoadDrivers("/");
155
156execute_hook("DriversLoaded", (void*)binary, NULL, NULL, NULL);
157
158clearActivityIndicator();
159
160if (gErrors) {
161printf("Errors encountered while starting up the computer.\n");
162printf("Pausing %d seconds...\n", kBootErrorTimeout);
163sleep(kBootErrorTimeout);
164}
165
166md0Ramdisk();
167
168verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");
169verbose("Boot Args: %s\n", bootArgs->CommandLine);
170
171// Cleanup the PXE base code.
172
173if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
174if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
175{
176printf("nbpUnloadBaseCode error %d\n", (int) ret);
177sleep(2);
178}
179}
180
181bool dummyVal;
182if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal) {
183showTextBuffer(msgbuf, strlen(msgbuf));
184}
185
186usb_loop();
187
188// If we were in text mode, switch to graphics mode.
189// This will draw the boot graphics unless we are in
190// verbose mode.
191if (gVerboseMode)
192setVideoMode( GRAPHICS_MODE, 0 );
193else
194drawBootGraphics();
195
196setupBooterLog();
197
198finalizeBootStruct();
199
200// Jump to kernel's entry point. There's no going back now.
201if ((checkOSVersion("10.7")) || (checkOSVersion("10.8")) || (checkOSVersion("10.9")))
202{
203
204// Notify modules that the kernel is about to be started
205execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);
206
207// Masking out so that Lion doesn't doublefault
208outb(0x21, 0xff);/* Maskout all interrupts Pic1 */
209outb(0xa1, 0xff);/* Maskout all interrupts Pic2 */
210
211startprog( kernelEntry, bootArgs );
212}
213else {
214// Notify modules that the kernel is about to be started
215execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);
216
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 if((gBootMode & kBootModeSafe) != 0)
235 {
236verbose("Kernel Cache ignored.\n");
237return -1;
238 }
239
240// Use specify kernel cache file if not empty
241if (cacheFile[0] != 0)
242{
243strlcpy(kernelCacheFile, cacheFile, sizeof(kernelCacheFile));
244verbose("Specified kernel cache file path=%s\n", cacheFile);
245}
246else
247{
248// Lion, Mountain Lion and Mavericks prelink kernel cache file
249if ((checkOSVersion("10.7")) || (checkOSVersion("10.8")) || (checkOSVersion("10.9")))
250{
251sprintf(kernelCacheFile, "%skernelcache", kDefaultCachePathSnow);
252verbose("L & ML kernel cache file path=%s\n", kernelCacheFile);
253}
254// Snow Leopard prelink kernel cache file
255else if (checkOSVersion("10.6")) {
256sprintf(kernelCacheFile, "kernelcache_%s", (archCpuType == CPU_TYPE_I386)
257? "i386" : "x86_64");
258int lnam = strlen(kernelCacheFile) + 9; //with adler32
259
260char* name;
261long prev_time = 0;
262
263struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
264
265while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
266{
267if (((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time
268&& strstr(name, kernelCacheFile) && (name[lnam] != '.'))
269{
270sprintf(kernelCacheFile, "%s%s", kDefaultCachePathSnow, name);
271prev_time = time;
272}
273}
274 verbose("SnowLeo kernel cache file path=%s\n", kernelCacheFile);
275}
276else {
277// Reset cache name.
278bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
279sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
280adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
281sprintf(kernelCacheFile, "%s.%08lX", kDefaultCachePathLeo, adler32);
282 verbose("Reseted kernel cache file path=%s\n", kernelCacheFile);
283}
284}
285
286// Check if the kernel cache file exists
287ret = -1;
288
289// If boot from a boot helper partition check the kernel cache file on it
290if (gBootVolume->flags & kBVFlagBooter) {
291sprintf(kernelCachePath, "com.apple.boot.P%s", kernelCacheFile);
292ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
293if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
294{
295sprintf(kernelCachePath, "com.apple.boot.R%s", kernelCacheFile);
296ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
297if ((ret == -1) || ((flags & kFileTypeMask) != kFileTypeFlat))
298{
299sprintf(kernelCachePath, "com.apple.boot.S%s", kernelCacheFile);
300ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
301if ((flags & kFileTypeMask) != kFileTypeFlat)
302ret = -1;
303}
304}
305}
306// If not found, use the original kernel cache path.
307if (ret == -1) {
308strcpy(kernelCachePath, kernelCacheFile);
309ret = GetFileInfo(NULL, kernelCachePath, &flags, &cachetime);
310if ((flags & kFileTypeMask) != kFileTypeFlat)
311ret = -1;
312}
313
314// Exit if kernel cache file wasn't found
315if (ret == -1) {
316verbose("No Kernel Cache File '%s' found\n", kernelCacheFile);
317return -1;
318}
319
320// Check if the kernel cache file is more recent (mtime)
321// than the kernel file or the S/L/E directory
322ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
323// Check if the kernel file is more recent than the cache file
324if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat)
325&& (kerneltime > cachetime)) {
326verbose("Kernel file (%s) is more recent than KernelCache (%s), ignoring KernelCache\n",
327bootInfo->bootFile, kernelCacheFile);
328return -1;
329}
330
331ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
332// Check if the S/L/E directory time is more recent than the cache file
333if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
334&& (exttime > cachetime)) {
335verbose("/System/Library/Extensions is more recent than KernelCache (%s), ignoring KernelCache\n",
336kernelCacheFile);
337return -1;
338}
339
340// Since the kernel cache file exists and is the most recent try to load it
341verbose("Loading kernel cache %s\n", kernelCachePath);
342
343ret = LoadThinFatFile(kernelCachePath, binary);
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 biosDevAndPart)
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 = biosDevAndPart & kBIOSDevMask;
386gBootPartition = ((biosDevAndPart & 0xFF00) >> 8);
387
388// Initialize boot info structure.
389initKernBootStruct();
390
391initBooterLog();
392
393// Setup VGA text mode.
394// Not sure if it is safe to call setVideoMode() before the
395// config table has been loaded. Call video_mode() instead.
396video_mode( 2 ); // 80x25 mono text mode.
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("gBIOSDev=%d\n", gBIOSDev);
461printf("gBootPartition=%d\n", gBootPartition);
462
463printf(" gBootVolume: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",
464 gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
465printf(" gBIOSBootVolume==bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",
466 gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
467
468for (BVRef bvr = bvChain; bvr; bvr = bvr->next)
469{
470printf(" chain: %d, dev: %d, part: %d, flags: %d, ver:%s vis: %d\n", bvr, bvr->biosdev, bvr->part_no, bvr->flags, bvr->OSVersion, bvr->visible);
471}
472
473getchar();
474#endif
475
476
477useGUI = true;
478// Override useGUI default
479getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
480if (useGUI && initGUI())
481{
482// initGUI() returned with an error, disabling GUI.
483useGUI = false;
484}
485
486setBootGlobals(bvChain);
487
488// Parse args, load and start kernel.
489while (1)
490{
491booltryresume, tryresumedefault, forceresume;
492booluseKernelCache = true; // by default try to use the prelinked kernel
493const char*val;
494intlen, ret = -1;
495longflags, sleeptime, time;
496void*binary = (void *)kLoadAddr;
497
498char bootFile[sizeof(bootInfo->bootFile)];
499charbootFilePath[512];
500charkernelCacheFile[512];
501
502// Initialize globals.
503sysConfigValid = false;
504gErrors = false;
505
506status = getBootOptions(firstRun);
507firstRun = false;
508if (status == -1) continue;
509
510status = processBootOptions();
511// Status == 1 means to chainboot
512if ( status ==1 ) break;
513// Status == -1 means that the config file couldn't be loaded or that gBootVolume is NULL
514if ( status == -1 )
515{
516// gBootVolume == NULL usually means the user hit escape.
517if (gBootVolume == NULL)
518{
519freeFilteredBVChain(bvChain);
520
521if (gEnableCDROMRescan)
522rescanBIOSDevice(gBIOSDev);
523
524bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
525setBootGlobals(bvChain);
526setupDeviceList(&bootInfo->themeConfig);
527}
528continue;
529}
530
531// Other status (e.g. 0) means that we should proceed with boot.
532
533// Turn off any GUI elements
534if ( bootArgs->Video.v_display == GRAPHICS_MODE )
535{
536gui.devicelist.draw = false;
537gui.bootprompt.draw = false;
538gui.menu.draw = false;
539gui.infobox.draw = false;
540gui.logo.draw = false;
541drawBackground();
542updateVRAM();
543}
544
545// Find out which version mac os we're booting.
546getOSVersion();
547
548if (platformCPUFeature(CPU_FEATURE_EM64T)) {
549archCpuType = CPU_TYPE_X86_64;
550} else {
551archCpuType = CPU_TYPE_I386;
552}
553
554if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig)) {
555if (strncmp(val, "i386", 4) == 0) {
556archCpuType = CPU_TYPE_I386;
557}
558}
559
560if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig)) {
561if (strncmp(val, "i386", 4) == 0) {
562archCpuType = CPU_TYPE_I386;
563}
564}
565
566// Notify modules that we are attempting to boot
567execute_hook("PreBoot", NULL, NULL, NULL, NULL);
568
569if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig)) {
570tryresume = true;
571tryresumedefault = true;
572} else {
573tryresumedefault = false;
574}
575
576if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig)) {
577forceresume = false;
578}
579
580if (forceresume) {
581tryresume = true;
582tryresumedefault = false;
583}
584
585while (tryresume) {
586const char *tmp;
587BVRef bvr;
588if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
589val = "/private/var/vm/sleepimage";
590
591// Do this first to be sure that root volume is mounted
592ret = GetFileInfo(0, val, &flags, &sleeptime);
593
594if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
595break;
596
597// Can't check if it was hibernation Wake=y is required
598if (bvr->modTime == 0 && tryresumedefault)
599break;
600
601if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
602break;
603
604if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
605#if DEBUG
606printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
607bvr->modTime-sleeptime);
608#endif
609break;
610}
611
612HibernateBoot((char *)val);
613break;
614}
615
616verbose("Loading Darwin %s\n", gMacOSVersion);
617
618getBoolForKey(kUseKernelCache, &useKernelCache, &bootInfo->chameleonConfig);
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
764static void getOSVersion()
765{
766strlcpy(gMacOSVersion, gBootVolume->OSVersion, sizeof(gMacOSVersion));
767}
768
769#define BASE 65521L /* largest prime smaller than 65536 */
770#define NMAX 5000
771// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
772
773#define DO1(buf, i){s1 += buf[i]; s2 += s1;}
774#define DO2(buf, i)DO1(buf, i); DO1(buf, i + 1);
775#define DO4(buf, i)DO2(buf, i); DO2(buf, i + 2);
776#define DO8(buf, i)DO4(buf, i); DO4(buf, i + 4);
777#define DO16(buf)DO8(buf, 0); DO8(buf, 8);
778
779unsigned long Adler32(unsigned char *buf, long len)
780{
781unsigned long s1 = 1; // adler & 0xffff;
782unsigned long s2 = 0; // (adler >> 16) & 0xffff;
783unsigned long result;
784int k;
785
786while (len > 0) {
787k = len < NMAX ? len : NMAX;
788len -= k;
789while (k >= 16) {
790DO16(buf);
791buf += 16;
792k -= 16;
793}
794if (k != 0) do {
795s1 += *buf++;
796s2 += s1;
797} while (--k);
798s1 %= BASE;
799s2 %= BASE;
800}
801result = (s2 << 16) | s1;
802return OSSwapHostToBigInt32(result);
803}
804

Archive Download this file

Revision: 2271