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

Archive Download this file

Revision: 815