Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/Chazi/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 * Mach Operating System
26 * Copyright (c) 1990 Carnegie-Mellon University
27 * Copyright (c) 1989 Carnegie-Mellon University
28 * All rights reserved. The CMU software License Agreement specifies
29 * the terms and conditions for use and redistribution.
30 */
31
32/*
33 * INTEL CORPORATION PROPRIETARY INFORMATION
34 *
35 * This software is supplied under the terms of a license agreement or
36 * nondisclosure agreement with Intel Corporation and may not be copied
37 * nor disclosed except in accordance with the terms of that agreement.
38 *
39 * Copyright 1988, 1989 by Intel Corporation
40 */
41
42/*
43 * Copyright 1993 NeXT Computer, Inc.
44 * All rights reserved.
45 */
46
47/*
48 * Completely reworked by Sam Streeper (sam_s@NeXT.com)
49 * Reworked again by Curtis Galloway (galloway@NeXT.com)
50 */
51
52//#define DEBUG 1
53
54//#include "bootstruct.h"
55//#include "libsa.h"
56#include "boot.h"
57//#include "io_inline.h" // Lion
58#include "ramdisk.h" // bootstruct.h: memory.h instead of saio_internal.h
59#include "gui.h"
60#include "modules.h"
61#include "fake_efi.h"
62#include "platform.h" // bootstruct.h - device_tree.h
63#include "sl.h" // bootstruct.h: memory.h instead of saio_types.h
64
65long gBootMode; /* defaults to 0 == kBootModeNormal */
66bool gOverrideKernel;
67//--- testing
68#define PLATFORM_NAME_LEN 64
69#define ROOT_PATH_LEN 256
70static char gCacheNameAdler[PLATFORM_NAME_LEN + ROOT_PATH_LEN];
71#define BOOT_DEVICE_PATH "\\System\\Library\\CoreServices\\boot.efi"
72static char gBootKernelCacheFile[512];
73//---
74char *gPlatformName = gCacheNameAdler; // disabled ??
75char gRootDevice[512];
76char gMKextName[512];
77bool gEnableCDROMRescan;
78bool gScanSingleDrive;
79
80int bvCount = 0; // global ?? - Slice
81//intmenucount = 0;
82//int gDeviceCount = 0;
83
84BVRef bvr;
85//BVRef menuBVR; - doesn't seem used here
86BVRef bvChain;
87bool useGUI;
88
89//static void selectBiosDevice(void);
90//Azi: this doesn't match the function; matches the "Alder32" on drivers.c
91static unsigned long Adler32(unsigned char *buffer, long length);
92
93
94static bool gUnloadPXEOnExit = false;
95
96/*
97 * How long to wait (in seconds) to load the
98 * kernel after displaying the "boot:" prompt.
99 */
100#define kBootErrorTimeout 5
101
102/*
103 * Default path to kernel cache file
104 */
105// OS X 10.4 & 10.5
106#define kCachePathTigerLeopard "/System/Library/Caches/com.apple.kernelcaches/kernelcache"
107// OS X 10.6
108#define kCachePathSnowLion "/System/Library/Caches/com.apple.kext.caches/Startup/kernelcache"
109
110//==========================================================================
111// Zero the BSS.
112
113static void zeroBSS(void)
114{
115extern char _DATA__bss__begin, _DATA__bss__end;
116extern char _DATA__common__begin, _DATA__common__end;
117
118bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));
119bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));
120}
121
122//==========================================================================
123// Malloc error function
124
125static void malloc_error(char *addr, size_t size, const char *file, int line)
126{
127stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);
128}
129
130//==========================================================================
131//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
132//
133void initialize_runtime(void)
134{
135zeroBSS();
136malloc_init(0, 0, 0, malloc_error);
137}
138
139//==========================================================================
140// execKernel - Load the kernel image (mach-o) and jump to its entry point.
141
142//Azi:autoresolution
143extern void initAutoRes();
144extern void finishAutoRes();
145
146static int ExecKernel(void *binary)
147{
148 entry_t kernelEntry;
149 int ret;
150
151 bootArgs->kaddr = bootArgs->ksize = 0;
152execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL);
153
154 //Azi: gHaveKernelCache is set here
155 ret = DecodeKernel(binary,
156 &kernelEntry,
157 (char **) &bootArgs->kaddr,
158 (int *)&bootArgs->ksize );
159
160 if ( ret != 0 )
161 return ret;
162
163 // Reserve space for boot args
164 reserveKernBootStruct();
165
166 //Azi: ...
167 // Load boot drivers from the specifed root path,
168 // if we don't have a prelinked kernel - check load.c 43 & 264
169 if (!gHaveKernelCache) {
170 LoadDrivers("/");
171 }
172
173 clearActivityIndicator();
174
175 if (gErrors) {
176 printf("Errors encountered while starting up the computer.\n");
177 printf("Pausing %d seconds...\n", kBootErrorTimeout);
178 sleep(kBootErrorTimeout);
179 }
180
181 setupFakeEfi(); //Azi: check position on Mek (plkernel)
182
183 md0Ramdisk();
184
185 verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");
186
187 // Cleanup the PXE base code.
188
189 if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
190if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
191 {
192 printf("nbpUnloadBaseCode error %d\n", (int) ret);
193 sleep(2);
194 }
195 }
196
197 bool dummyVal;
198
199//Azi: Wait=y is breaking other keys when typed "after them" at boot prompt.
200// Works properly if typed in first place or used on Boot.plist.
201if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal) {
202printf("(Wait) "); // Add to trunk?? don't forget that puse uses printf, not verbose!!
203pause();
204}
205
206usb_loop();
207
208//autoresolution - Check if user disabled AutoResolution at the boot prompt.
209// we can't check the plist here if we have AutoResolution=n there and we anabled it
210// at boot prompt...?????
211//getBoolForKey(kAutoResolutionKey, &gAutoResolution, &bootInfo->bootConfig);
212finishAutoRes();
213
214//Azi: closing Vbios after "if (gVerboseMode)" stuff eliminates the need for setting
215// AutoResolution = true above; but creates another bug when booting in TextMode with -v arg.
216// Simptoms include: staring some seconds at a nicely drawn white screen, after boot prompt.
217// Think i'm just going to end up removing setting gAutoResolution = false
218// on closeVbios().. the more i think, the less sense it makes doing it there!!
219//autoresolution - end
220
221// Notify modules that the kernel is about to be started
222// testing...
223
224if (gMacOSVersion[3] <= '6')
225execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);
226else
227execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);
228
229 // If we were in text mode, switch to graphics mode.
230 // This will draw the boot graphics unless we are in
231 // verbose mode.
232
233 if(gVerboseMode)
234 setVideoMode( GRAPHICS_MODE, 0 );
235 else
236 drawBootGraphics();
237
238setupBooterLog();
239
240 finalizeBootStruct();
241
242//Azi: see asm.s LABEL(_disableIRQs)
243//outb(0x21, 0xff);
244//outb(0xa1, 0xff);
245
246 // Jump to kernel's entry point. There's no going back now.
247if (gMacOSVersion[3] <= '6')
248startprog( kernelEntry, bootArgsPreLion );
249else
250startprog( kernelEntry, bootArgs );
251
252 // Not reached
253 return 0;
254}
255
256//==========================================================================
257// This is the entrypoint from real-mode which functions exactly as it did
258// before. Multiboot does its own runtime initialization, does some of its
259// own things, and then calls common_boot.
260void boot(int biosdev)
261{
262initialize_runtime();
263// Enable A20 gate before accessing memory above 1Mb.
264enableA20();
265common_boot(biosdev);
266}
267
268//==========================================================================
269// The 'main' function for the booter. Called by boot0 when booting
270// from a block device, or by the network booter.
271//
272// arguments:
273// biosdev - Value passed from boot1/NBP to specify the device
274// that the booter was loaded from.
275//
276// If biosdev is kBIOSDevNetwork, then this function will return if
277// booting was unsuccessful. This allows the PXE firmware to try the
278// next boot device on its list.
279void common_boot(int biosdev)
280{
281 int status;
282 char *bootFile;
283 unsigned long adler32;
284 bool quiet;
285 bool firstRun = true;
286 bool instantMenu;
287 bool rescanPrompt = false;
288 unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;
289 unsigned int denyBVFlags = kBVFlagEFISystem;
290
291 // Set reminder to unload the PXE base code. Neglect to unload
292 // the base code will result in a hang or kernel panic.
293 gUnloadPXEOnExit = true;
294
295 // Record the device that the booter was loaded from.
296 gBIOSDev = biosdev & kBIOSDevMask;
297
298 // Initialize boot info structure.
299 initKernBootStruct();
300
301initBooterLog();
302
303 // Setup VGA text mode.
304 // Not sure if it is safe to call setVideoMode() before the
305 // config table has been loaded. Call video_mode() instead.
306#if DEBUG
307 printf("before video_mode\n"); //Azi: this one is not printing... i remember it did.. check trunk.
308#endif
309 video_mode( 2 ); // 80x25 mono text mode.
310#if DEBUG
311 printf("after video_mode\n");
312#endif
313
314 // Scan and record the system's hardware information.
315 scan_platform();
316
317 // First get info for boot volume.
318 scanBootVolumes(gBIOSDev, 0);
319 bvChain = getBVChainForBIOSDev(gBIOSDev);
320//Azi: initialising gBIOSBootVolume & gBootVolume (Startup volume) for the first time.. i think!?
321// also, kDefaultPartitionKey is checked here, on selectBootVolume.
322 setBootGlobals(bvChain);
323msglog("setBootGlobals:\n Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
324 msglog(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
325
326// Boot Volume is set as Root at this point so, pointing to Extra, /Extra or bt(0,0)/Extra
327// is exactly the same.Review bt(0,0)/bla bla paths......(Reviewing...)
328
329//Azi: works as expected but... trying this because Kernel=mach_kernel doesn't work on a
330// override Boot.plist; this makes it impossible to override e.g. Kernel=bt(0,0)mach_kernel
331// on the main Boot.plist, when loading kernel from ramdisk btAliased.
332loadPrebootRAMDisk();
333
334 // Load boot.plist config file
335 //Azi: on this first check, boot.plist acts as both "booter config file"
336 // and bootargs/options "carrier".*****
337 status = loadSystemConfig(&bootInfo->bootConfig);
338
339 if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {
340 gBootMode |= kBootModeQuiet;
341 }
342
343 // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
344 if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {
345 firstRun = false;
346 }
347
348// Loading preboot ramdisk if exists.
349//loadPrebootRAMDisk(); //Azi: this needs to be done before load_all_modules()
350// because of btAlias...(Reviewing...)
351
352// Intialize module system
353if (init_module_system())
354{
355load_all_modules();
356}
357
358 // Disable rescan option by default
359 gEnableCDROMRescan = false;
360
361 // If we're loading the booter from optical media...(Reviewing...)
362if (biosDevIsCDROM(gBIOSDev))
363{
364// ... ask the user for Rescan option by setting "Rescan Prompt"=y in system config...
365if (getBoolForKey(kRescanPromptKey, &rescanPrompt, &bootInfo->bootConfig) && rescanPrompt)
366{
367 gEnableCDROMRescan = promptForRescanOption();
368 }
369else // ... or enable it with Rescan=y in system config.
370 if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan)
371{
372 gEnableCDROMRescan = true;
373 }
374}
375
376//Azi: Is this a cdrom only thing?(Reviewing...)
377 // Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
378 if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive)
379{
380scanBootVolumes(gBIOSDev, &bvCount);
381 }
382else
383{
384//Azi: scanDisks uses scanBootVolumes.
385scanDisks(gBIOSDev, &bvCount);
386}
387
388 // Create a separated bvr chain using the specified filters.
389 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
390
391 gBootVolume = selectBootVolume(bvChain);
392
393//#if DEBUG
394//printf
395 msglog(":something...???\n Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
396 msglog(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
397// getchar();
398//#endif
399
400 useGUI = true;
401 // Override useGUI default
402 getBoolForKey(kGUIKey, &useGUI, &bootInfo->bootConfig);
403
404// AutoResolution - Azi: default to false
405// http://forum.voodooprojects.org/index.php/topic,1227.0.html
406gAutoResolution = false;
407
408// Check if user enabled AutoResolution on Boot.plist...
409getBoolForKey(kAutoResolutionKey, &gAutoResolution, &bootInfo->bootConfig);
410
411// Patch the Video Bios with the extracted resolution, before initGui.
412if (gAutoResolution == true)
413{
414initAutoRes();
415}
416
417 if (useGUI && initGUI())
418{
419// initGUI() returned with an error, disabling GUI.
420useGUI = false;
421}
422
423 setBootGlobals(bvChain);
424msglog("setBootGlobals:\n Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
425 msglog(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
426
427 // Parse args, load and start kernel.
428 while (1) {
429 const char *val;
430 int len;
431 int trycache;
432long flags, cachetime, kerneltime, exttime, sleeptime, time;
433 int ret = -1;
434 void *binary = (void *)kLoadAddr;
435 bool tryresume;
436 bool tryresumedefault;
437 bool forceresume;
438bool ignoreKC = false;
439
440 // additional variable for testing alternate kernel image locations on boot helper partitions.
441 char bootFileSpec[512];
442
443 // Initialize globals.
444
445 sysConfigValid = false;
446 gErrors = false;
447
448 status = getBootOptions(firstRun);
449 firstRun = false;
450 if (status == -1) continue;
451
452//Azi: test (gBootVolume == NULL) - so far Ok!
453// test with optical media again...?
454// Turn off any GUI elements, draw background and update VRAM.
455if ( bootArgs->Video.v_display == GRAPHICS_MODE )
456{
457gui.devicelist.draw = false;
458gui.bootprompt.draw = false;
459gui.menu.draw = false;
460gui.infobox.draw = false;
461gui.logo.draw = false;
462drawBackground();
463updateVRAM();
464}
465
466status = processBootOptions();
467
468//Azi: AutoResolution - closing Vbios here without restoring, causes an allocation error,
469// if the user tries to boot, after a e.g."Can't find bla_kernel" msg.
470// Doing it on execKernel() instead.
471
472// Status == 1 means to chainboot
473if ( status ==1 ) break;
474
475// Status == -1 means that gBootVolume is NULL. Config file is not mandatory anymore!
476if ( status == -1 )
477{
478// gBootVolume == NULL usually means the user hit escape.(Reviewing...)
479if (gBootVolume == NULL)
480{
481freeFilteredBVChain(bvChain);
482
483if (gEnableCDROMRescan)
484rescanBIOSDevice(gBIOSDev);
485
486bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
487setBootGlobals(bvChain);
488setupDeviceList(&bootInfo->themeConfig);
489}
490continue;
491}
492
493 // Other status (e.g. 0) means that we should proceed with boot.
494
495// If cpu handles 64 bit instructions...
496if (platformCPUFeature(CPU_FEATURE_EM64T))
497{
498// use x86_64 kernel arch,...
499archCpuType = CPU_TYPE_X86_64;
500}
501else
502{
503// else use i386 kernel arch.
504archCpuType = CPU_TYPE_I386;
505}
506// If user override...
507if (getValueForKey(kArchKey, &val, &len, &bootInfo->bootConfig))
508{
509// matches i386...
510if (strncmp(val, "i386", 4) == 0)
511{
512// use i386 kernel arch.
513archCpuType = CPU_TYPE_I386;
514}
515}
516
517if (!getBoolForKey (kWakeKey, &tryresume, &bootInfo->bootConfig)) {
518tryresume = true;
519tryresumedefault = true;
520} else {
521tryresumedefault = false;
522}
523
524if (!getBoolForKey (kForceWakeKey, &forceresume, &bootInfo->bootConfig)) {
525forceresume = false;
526}
527
528if (forceresume) {
529tryresume = true;
530tryresumedefault = false;
531}
532
533while (tryresume) {
534const char *tmp;
535BVRef bvr;
536if (!getValueForKey(kWakeKeyImageKey, &val, &len, &bootInfo->bootConfig))
537val="/private/var/vm/sleepimage";
538
539// Do this first to be sure that root volume is mounted
540ret = GetFileInfo(0, val, &flags, &sleeptime);
541
542if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
543break;
544
545// Can't check if it was hibernation Wake=y is required
546if (bvr->modTime == 0 && tryresumedefault)
547break;
548
549if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
550break;
551
552if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
553//Azi: no need for printf at this point - reminder
554printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override.\n",bvr->modTime-sleeptime);
555break;
556}
557
558HibernateBoot((char *)val);
559break;
560}
561
562//Azi:kernelcache stuff
563bool patchKernel = false;
564getBoolForKey(kKPatcherKey, &patchKernel, &bootInfo->bootConfig);
565//Azi: avoiding having to use -f to ignore kernel cache
566//Azi: ignore kernel cache but still use kext cache (E/E.mkext & S/L/E.mkext). - explain...
567getBoolForKey(kUseKCKey, &ignoreKC, &bootInfo->bootConfig); // equivalent to UseKernelCache
568if (ignoreKC)
569{
570verbose("KC: cache ignored by user.\n");
571// make sure the damn thing get's zeroed, just in case... :)*
572bzero(gBootKernelCacheFile, sizeof(gBootKernelCacheFile));
573}
574else if (patchKernel) // to be moved..?
575{
576verbose("KC: kernel patcher enabled, ignore cache.\n");
577bzero(gBootKernelCacheFile, sizeof(gBootKernelCacheFile));
578}
579else if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))
580{
581 strlcpy(gBootKernelCacheFile, val, len + 1);
582verbose("KC: path set by user = %s\n", gBootKernelCacheFile);
583//Azi: bypass time check when user sets path ???
584// cache is still ignored if time doesn't match... (e.g. booter on usb stick)
585 }
586else
587{
588// Reset cache name.
589bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
590
591// kextcache_main.c: Construct entry from UUID of boot volume...(reminder)
592// assemble ?string? to generate adler from...
593//sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
594const char *ProductName = getStringForKey("SMproductname", &bootInfo->smbiosConfig);
595sprintf(gCacheNameAdler, ProductName); // well, at least the smbios.plist can be loaded this early...
596// to set/get "ProductName" this early, booter needs complete rewrite!!
597// see DHP's Revolution booter rework example!
598verbose("KC: gCacheNameAdler 1 = %s\n", gCacheNameAdler);
599//Azi: check the validity of this, e.g. on Helper Partitions
600sprintf(gCacheNameAdler + 64, "%s", "\\System\\Library\\CoreServices\\boot.efi");
601verbose("KC: gCacheNameAdler 2 = %s\n", gCacheNameAdler + 64);
602sprintf(gCacheNameAdler + (64 + 38), "%s", bootInfo->bootFile);
603verbose("KC: gCacheNameAdler 3 = %s\n", gCacheNameAdler + (64 + 38));
604
605// generate adler
606adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
607verbose("KC: Adler32 = %08X\n", adler32);
608//Azi: no check for OS version here ?? - yes there is :)
609// append arch and/or adler (checksum) to kc path...
610if (gMacOSVersion[3] <= '5')
611{
612sprintf(gBootKernelCacheFile, "%s.%08lX", kCachePathTigerLeopard, adler32);
613verbose("KC: adler added to path = %s\n", gBootKernelCacheFile);
614}
615else
616{
617sprintf(gBootKernelCacheFile, "%s_%s.%08X", kCachePathSnowLion,
618(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64", adler32);
619verbose("KC: arch & adler added to path = %s\n", gBootKernelCacheFile);
620}
621 }
622
623 // Check for cache file.
624//Azi: trycache is done if...
625 trycache = ( ( (gBootMode & kBootModeSafe) == 0) //... we're not booting in safe mode (-x arg),
626&& !gOverrideKernel // we're not overriding default kernel "name",
627&& (gBootFileType == kBlockDeviceType) // we're booting from local storage device,
628&& (gMKextName[0] == '\0') // "MKext Cache" key IS NOT in use, and
629&& (gBootKernelCacheFile[0] != '\0') ); // gBootKernelCacheFile is populated.
630// we could add the use of "kernelpatcher" to this bunch..??
631
632//verbose("Loading Darwin %s\n", gMacOSVersion); //Azi: move?? to getOSVersion? :)
633
634 if (trycache) do
635{
636verbose("KC: checking kernel cache (system prelinked kernel)...\n");
637 // if we haven't found the kernel yet, don't use the cache
638 ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
639 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
640 {
641verbose("KC: no kernel found (shouldn't happen?!?)\n");
642 trycache = 0; // ignore kernel cache...
643 break;
644 }
645verbose("KC: kerneltime = %d\n", kerneltime);
646
647 ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
648 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
649 || (cachetime < kerneltime))
650 {
651if (cachetime <= 100) // confirm: 100 = inexistent path, -xxxxxxxxx = wrong name
652// not confirming... i also get -xxxxxxxxx with inexisting prelinked kernel
653verbose("KC: cachetime = %d, kernel cache path/adler is incorrect, ignoring it. ??? \n",
654cachetime);
655else
656verbose("KC: cachetime = %d, kernel cache is older than the kernel, ignoring it.\n",
657cachetime);
658 trycache = 0;
659 break;
660 }
661verbose("KC: cachetime = %d\n", cachetime);
662
663 ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
664 if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
665 && (cachetime < exttime))
666 {
667verbose("KC: exttime = %d, kernel cache is older than S/L/E, ignoring it.\n", exttime);
668 trycache = 0;
669 break;
670 }
671verbose("KC: exttime = %d\n", exttime);
672
673 if (kerneltime > exttime) // if S/L/E is older than the kernel...
674 {
675verbose("KC: S/L/E is older than the kernel, matching exttime with kerneltime...\n");
676 exttime = kerneltime;
677 }
678verbose("KC: exttime +1 = %d\n", exttime + 1);
679
680 if (cachetime != (exttime + 1))
681 {
682verbose("KC: kernel cache time is diff from S/L/E time, ignoring it.\n");
683 trycache = 0;
684 break;
685 }
686verbose("KC: kernel cache found and up to date, will be used.\n");
687
688 } while (0);
689
690 do
691 {
692// Load kernel cache if not ignored.
693 if (trycache)
694 {
695 bootFile = gBootKernelCacheFile;
696 verbose("Loading kernel cache %s\n", bootFile);
697
698 ret = LoadFile(bootFile);
699 binary = (void *)kLoadAddr;
700
701 if (ret >= 0)
702 {
703 break;
704 }
705 }
706
707 bootFile = bootInfo->bootFile;
708
709 // Try to load kernel image from alternate locations on boot helper partitions.
710 sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);
711 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
712 if (ret == -1)
713 {
714sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);
715ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
716if (ret == -1)
717{
718sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);
719ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
720if (ret == -1)
721{
722// No alternate location found, using the original kernel image path.
723strcpy(bootFileSpec, bootFile);
724}
725}
726 }
727
728 verbose("Loading kernel %s\n", bootFileSpec);
729 ret = LoadThinFatFile(bootFileSpec, &binary);
730 if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
731 {
732archCpuType = CPU_TYPE_I386;
733ret = LoadThinFatFile(bootFileSpec, &binary);
734 }
735
736 } while (0);
737
738 clearActivityIndicator();
739/*#if DEBUG
740 printf("Pausing...");
741 sleep(8);
742#endif
743Azi: annoying stuff :P */
744 if (ret <= 0)
745{
746printf("Can't find %s\n", bootFile);
747
748sleep(1);
749
750 if (gBootFileType == kNetworkDeviceType) {
751 // Return control back to PXE. Don't unload PXE base code.
752 gUnloadPXEOnExit = false;
753 break;
754 }
755 }
756else
757{
758 // Won't return if successful.
759 ret = ExecKernel(binary);
760 }
761 } // while (1)
762
763 // chainboot
764 if (status==1) {
765if (getVideoMode() == GRAPHICS_MODE) {// if we are already in graphics-mode,
766setVideoMode(VGA_TEXT_MODE, 0);// switch back to text mode
767}
768 }
769
770 if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
771{
772nbpUnloadBaseCode();
773 }
774}
775
776/*!
777 Selects a new BIOS device, taking care to update the global state appropriately.
778 */
779/*
780static void selectBiosDevice(void)
781{
782 struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
783 CacheReset();
784 diskFreeMap(oldMap);
785 oldMap = NULL;
786
787 int dev = selectAlternateBootDevice(gBIOSDev);
788
789 BVRef bvchain = scanBootVolumes(dev, 0);
790 BVRef bootVol = selectBootVolume(bvchain);
791 gBootVolume = bootVol;
792 setRootVolume(bootVol);
793 gBIOSDev = dev;
794}
795*/
796
797#define BASE 65521L /* largest prime smaller than 65536 */
798#define NMAX 5000
799// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
800
801#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
802#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
803#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
804#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
805#define DO16(buf) DO8(buf,0); DO8(buf,8);
806
807unsigned long Adler32(unsigned char *buf, long len)
808{
809 unsigned long s1 = 1; // adler & 0xffff;
810 unsigned long s2 = 0; // (adler >> 16) & 0xffff;
811 unsigned long result;
812 int k;
813
814 while (len > 0) {
815 k = len < NMAX ? len : NMAX;
816 len -= k;
817 while (k >= 16) {
818 DO16(buf);
819 buf += 16;
820 k -= 16;
821 }
822 if (k != 0) do {
823 s1 += *buf++;
824 s2 += s1;
825 } while (--k);
826 s1 %= BASE;
827 s2 %= BASE;
828 }
829 result = (s2 << 16) | s1;
830 return OSSwapHostToBigInt32(result);
831}
832

Archive Download this file

Revision: 1050