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.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) {
202verbose("(Wait) ");
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//Azi: log booter version, revision & build date, for bdmesg.
304msglog(bootLogBanner);
305
306 // Setup VGA text mode.
307 // Not sure if it is safe to call setVideoMode() before the
308 // config table has been loaded. Call video_mode() instead.
309#if DEBUG
310 printf("before video_mode\n"); //Azi: this one is not printing... i remember it did.. check trunk.
311#endif
312 video_mode( 2 ); // 80x25 mono text mode.
313#if DEBUG
314 printf("after video_mode\n");
315#endif
316
317 // Scan and record the system's hardware information.
318 scan_platform();
319
320 // First get info for boot volume.
321 scanBootVolumes(gBIOSDev, 0);
322 bvChain = getBVChainForBIOSDev(gBIOSDev);
323//Azi: initialising gBIOSBootVolume & gBootVolume for the first time.. i think!?
324// also, kDefaultPartitionKey is checked here, on selectBootVolume.
325 setBootGlobals(bvChain);
326msglog("setBootGlobals:\n Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
327 msglog(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
328
329// Boot Volume is set as Root at this point so, pointing to Extra, /Extra or bt(0,0)/Extra
330// is exactly the same.Review bt(0,0)/bla bla paths......(Reviewing...)
331
332//Azi: works as expected but... trying this because Kernel=mach_kernel doesn't work on a
333// override Boot.plist; this makes it impossible to override e.g. Kernel=bt(0,0)mach_kernel
334// on the main Boot.plist, when loading kernel from ramdisk btAliased.
335loadPrebootRAMDisk();
336
337 // Load boot.plist config file
338 //Azi: on this first check, boot.plist acts as both "booter config file"
339 // and bootargs/options "carrier".*****
340 status = loadSystemConfig(&bootInfo->bootConfig);
341
342 if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {
343 gBootMode |= kBootModeQuiet;
344 }
345
346 // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
347 if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {
348 firstRun = false;
349 }
350
351// Loading preboot ramdisk if exists.
352//loadPrebootRAMDisk(); //Azi: this needs to be done before load_all_modules()
353// because of btAlias...(Reviewing...)
354
355// Intialize module system
356if (init_module_system())
357{
358load_all_modules();
359}
360
361 // Disable rescan option by default
362 gEnableCDROMRescan = false;
363
364 // If we're loading the booter from optical media...(Reviewing...)
365if (biosDevIsCDROM(gBIOSDev))
366{
367// ... ask the user for Rescan option by setting "Rescan Prompt"=y in system config...
368if (getBoolForKey(kRescanPromptKey, &rescanPrompt, &bootInfo->bootConfig) && rescanPrompt)
369{
370 gEnableCDROMRescan = promptForRescanOption();
371 }
372else // ... or enable it with Rescan=y in system config.
373 if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan)
374{
375 gEnableCDROMRescan = true;
376 }
377}
378
379//Azi: Is this a cdrom only thing?(Reviewing...)
380 // Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
381 if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive)
382{
383scanBootVolumes(gBIOSDev, &bvCount);
384 }
385else
386{
387//Azi: scanDisks uses scanBootVolumes.
388scanDisks(gBIOSDev, &bvCount);
389}
390
391 // Create a separated bvr chain using the specified filters.
392 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
393
394 gBootVolume = selectBootVolume(bvChain);
395
396//#if DEBUG
397//printf
398 msglog(":something...???\n Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
399 msglog(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
400// getchar(); //getc(); Azi: getc stuff - DON'T FORGET pause() - take modules.c as reference.
401//#endif
402
403 useGUI = true;
404 // Override useGUI default
405 getBoolForKey(kGUIKey, &useGUI, &bootInfo->bootConfig);
406
407// AutoResolution - Azi: default to false
408// http://forum.voodooprojects.org/index.php/topic,1227.0.html
409gAutoResolution = false;
410
411// Check if user enabled AutoResolution on Boot.plist...
412getBoolForKey(kAutoResolutionKey, &gAutoResolution, &bootInfo->bootConfig);
413
414// Patch the Video Bios with the extracted resolution, before initGui.
415if (gAutoResolution == true)
416{
417initAutoRes();
418}
419
420 if (useGUI && initGUI())
421{
422// initGUI() returned with an error, disabling GUI.
423useGUI = false;
424}
425
426 setBootGlobals(bvChain);
427msglog("setBootGlobals:\n Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
428 msglog(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
429
430 // Parse args, load and start kernel.
431 while (1) {
432 const char *val;
433 int len;
434 int trycache;
435long flags, cachetime, kerneltime, exttime, sleeptime, time;
436 int ret = -1;
437 void *binary = (void *)kLoadAddr;
438 bool tryresume;
439 bool tryresumedefault;
440 bool forceresume;
441bool ignoreKC = false;
442
443 // additional variable for testing alternate kernel image locations on boot helper partitions.
444 char bootFileSpec[512];
445
446 // Initialize globals.
447
448 sysConfigValid = false;
449 gErrors = false;
450
451 status = getBootOptions(firstRun);
452 firstRun = false;
453 if (status == -1) continue;
454
455//Azi: test (gBootVolume == NULL) - so far Ok!
456// test with optical media again...?
457// Turn off any GUI elements, draw background and update VRAM.
458if ( bootArgs->Video.v_display == GRAPHICS_MODE )
459{
460gui.devicelist.draw = false;
461gui.bootprompt.draw = false;
462gui.menu.draw = false;
463gui.infobox.draw = false;
464gui.logo.draw = false;
465drawBackground();
466updateVRAM();
467}
468
469status = processBootOptions();
470
471//Azi: AutoResolution - closing Vbios here without restoring, causes an allocation error,
472// if the user tries to boot, after a e.g."Can't find bla_kernel" msg.
473// Doing it on execKernel() instead.
474
475// Status == 1 means to chainboot
476if ( status ==1 ) break;
477
478// Status == -1 means that gBootVolume is NULL. Config file is not mandatory anymore!
479if ( status == -1 )
480{
481// gBootVolume == NULL usually means the user hit escape.(Reviewing...)
482if (gBootVolume == NULL)
483{
484freeFilteredBVChain(bvChain);
485
486if (gEnableCDROMRescan)
487rescanBIOSDevice(gBIOSDev);
488
489bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
490setBootGlobals(bvChain);
491setupDeviceList(&bootInfo->themeConfig);
492}
493continue;
494}
495
496 // Other status (e.g. 0) means that we should proceed with boot.
497
498// If cpu handles 64 bit instructions...
499if (platformCPUFeature(CPU_FEATURE_EM64T))
500{
501// use x86_64 kernel arch,...
502archCpuType = CPU_TYPE_X86_64;
503}
504else
505{
506// else use i386 kernel arch.
507archCpuType = CPU_TYPE_I386;
508}
509// If user override...
510if (getValueForKey(kArchKey, &val, &len, &bootInfo->bootConfig))
511{
512// matches i386...
513if (strncmp(val, "i386", 4) == 0)
514{
515// use i386 kernel arch.
516archCpuType = CPU_TYPE_I386;
517}
518}
519
520if (!getBoolForKey (kWakeKey, &tryresume, &bootInfo->bootConfig)) {
521tryresume = true;
522tryresumedefault = true;
523} else {
524tryresumedefault = false;
525}
526
527if (!getBoolForKey (kForceWakeKey, &forceresume, &bootInfo->bootConfig)) {
528forceresume = false;
529}
530
531if (forceresume) {
532tryresume = true;
533tryresumedefault = false;
534}
535
536while (tryresume) {
537const char *tmp;
538BVRef bvr;
539if (!getValueForKey(kWakeKeyImageKey, &val, &len, &bootInfo->bootConfig))
540val="/private/var/vm/sleepimage";
541
542// Do this first to be sure that root volume is mounted
543ret = GetFileInfo(0, val, &flags, &sleeptime);
544
545if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
546break;
547
548// Can't check if it was hibernation Wake=y is required
549if (bvr->modTime == 0 && tryresumedefault)
550break;
551
552if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
553break;
554
555if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
556//Azi: no need for printf at this point - reminder
557printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override.\n",bvr->modTime-sleeptime);
558break;
559}
560
561HibernateBoot((char *)val);
562break;
563}
564
565//Azi:kernelcache stuff
566bool patchKernel = false;
567getBoolForKey(kKPatcherKey, &patchKernel, &bootInfo->bootConfig);
568//Azi: avoiding having to use -f to ignore kernel cache
569//Azi: ignore kernel cache but still use kext cache (E/E.mkext & S/L/E.mkext). - explain...
570getBoolForKey(kIgnoreKCKey, &ignoreKC, &bootInfo->bootConfig); // equivalent to UseKernelCache
571if (ignoreKC)
572{
573verbose("KC: cache ignored by user.\n");
574// make sure the damn thing get's zeroed, just in case... :)*
575bzero(gBootKernelCacheFile, sizeof(gBootKernelCacheFile));
576}
577else if (patchKernel) // to be moved..?
578{
579verbose("KC: kernel patcher enabled, ignore cache.\n");
580bzero(gBootKernelCacheFile, sizeof(gBootKernelCacheFile));
581}
582else if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))
583{
584 strlcpy(gBootKernelCacheFile, val, len + 1);
585verbose("KC: path set by user = %s\n", gBootKernelCacheFile);
586//Azi: bypass time check when user sets path ???
587// cache is still ignored if time doesn't match... (e.g. booter on usb stick)
588 }
589else
590{
591// Reset cache name.
592bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
593
594// kextcache_main.c: Construct entry from UUID of boot volume...(reminder)
595// assemble ?string? to generate adler from...
596//sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
597const char *ProductName = getStringForKey("SMproductname", &bootInfo->smbiosConfig);
598sprintf(gCacheNameAdler, ProductName); // well, at least the smbios.plist can be loaded this early...
599// to set/get "ProductName" this early, booter needs complete rewrite!!
600// see DHP's Revolution booter rework example!
601verbose("KC: gCacheNameAdler 1 = %s\n", gCacheNameAdler);
602//Azi: check the validity of this, e.g. on Helper Partitions
603sprintf(gCacheNameAdler + 64, "%s", "\\System\\Library\\CoreServices\\boot.efi");
604verbose("KC: gCacheNameAdler 2 = %s\n", gCacheNameAdler + 64);
605sprintf(gCacheNameAdler + (64 + 38), "%s", bootInfo->bootFile);
606verbose("KC: gCacheNameAdler 3 = %s\n", gCacheNameAdler + (64 + 38));
607
608// generate adler
609adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
610verbose("KC: Adler32 = %08X\n", adler32);
611//Azi: no check for OS version here ?? - yes there is :)
612// append arch and/or adler (checksum) to kc path...
613if (gMacOSVersion[3] <= '5')
614{
615sprintf(gBootKernelCacheFile, "%s.%08lX", kCachePathTigerLeopard, adler32);
616verbose("KC: adler added to path = %s\n", gBootKernelCacheFile);
617}
618else
619{
620sprintf(gBootKernelCacheFile, "%s_%s.%08X", kCachePathSnowLion,
621(archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64", adler32);
622verbose("KC: arch & adler added to path = %s\n", gBootKernelCacheFile);
623}
624 }
625
626 // Check for cache file.
627//Azi: trycache is done if...
628 trycache = ( ( (gBootMode & kBootModeSafe) == 0) //... we're not booting in safe mode (-x arg),
629&& !gOverrideKernel // we're not overriding default kernel "name",
630&& (gBootFileType == kBlockDeviceType) // we're booting from local storage device,
631&& (gMKextName[0] == '\0') // "MKext Cache" key IS NOT in use, and
632&& (gBootKernelCacheFile[0] != '\0') ); // gBootKernelCacheFile is populated.
633// we could add the use of "kernelpatcher" to this bunch..??
634
635//verbose("Loading Darwin %s\n", gMacOSVersion); //Azi: move?? to getOSVersion? :)
636
637 if (trycache) do
638{
639verbose("KC: checking kernel cache (system prelinked kernel)...\n");
640 // if we haven't found the kernel yet, don't use the cache
641 ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
642 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
643 {
644verbose("KC: no kernel found (shouldn't happen?!?)\n");
645 trycache = 0; // ignore kernel cache...
646 break;
647 }
648verbose("KC: kerneltime = %d\n", kerneltime);
649
650 ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
651 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
652 || (cachetime < kerneltime))
653 {
654if (cachetime <= 100) // confirm: 100 = inexistent path, -xxxxxxxxx = wrong name
655// not confirming... i also get -xxxxxxxxx with inexisting prelinked kernel
656verbose("KC: cachetime = %d, kernel cache path/adler is incorrect, ignoring it. ??? \n",
657cachetime);
658else
659verbose("KC: cachetime = %d, kernel cache is older than the kernel, ignoring it.\n",
660cachetime);
661 trycache = 0;
662 break;
663 }
664verbose("KC: cachetime = %d\n", cachetime);
665
666 ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
667 if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
668 && (cachetime < exttime))
669 {
670verbose("KC: exttime = %d, kernel cache is older than S/L/E, ignoring it.\n", exttime);
671 trycache = 0;
672 break;
673 }
674verbose("KC: exttime = %d\n", exttime);
675
676 if (kerneltime > exttime) // if S/L/E is older than the kernel...
677 {
678verbose("KC: S/L/E is older than the kernel, matching exttime with kerneltime...\n");
679 exttime = kerneltime;
680 }
681verbose("KC: exttime +1 = %d\n", exttime + 1);
682
683 if (cachetime != (exttime + 1))
684 {
685verbose("KC: kernel cache time is diff from S/L/E time, ignoring it.\n");
686 trycache = 0;
687 break;
688 }
689verbose("KC: kernel cache found and up to date, will be used.\n");
690
691 } while (0);
692
693 do
694 {
695// Load kernel cache if not ignored.
696 if (trycache)
697 {
698 bootFile = gBootKernelCacheFile;
699 verbose("Loading kernel cache %s\n", bootFile);
700
701 ret = LoadFile(bootFile);
702 binary = (void *)kLoadAddr;
703
704 if (ret >= 0)
705 {
706 break;
707 }
708 }
709
710 bootFile = bootInfo->bootFile;
711
712 // Try to load kernel image from alternate locations on boot helper partitions.
713 sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);
714 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
715 if (ret == -1)
716 {
717sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);
718ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
719if (ret == -1)
720{
721sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);
722ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
723if (ret == -1)
724{
725// No alternate location found, using the original kernel image path.
726strcpy(bootFileSpec, bootFile);
727}
728}
729 }
730
731 verbose("Loading kernel %s\n", bootFileSpec);
732 ret = LoadThinFatFile(bootFileSpec, &binary);
733 if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
734 {
735archCpuType = CPU_TYPE_I386;
736ret = LoadThinFatFile(bootFileSpec, &binary);
737 }
738
739 } while (0);
740
741 clearActivityIndicator();
742/*#if DEBUG
743 printf("Pausing...");
744 sleep(8);
745#endif
746Azi: annoying stuff :P */
747 if (ret <= 0)
748{
749printf("Can't find %s\n", bootFile);
750
751sleep(1);
752
753 if (gBootFileType == kNetworkDeviceType) {
754 // Return control back to PXE. Don't unload PXE base code.
755 gUnloadPXEOnExit = false;
756 break;
757 }
758 }
759else
760{
761 // Won't return if successful.
762 ret = ExecKernel(binary);
763 }
764 } // while (1)
765
766 // chainboot
767 if (status==1) {
768if (getVideoMode() == GRAPHICS_MODE) {// if we are already in graphics-mode,
769setVideoMode(VGA_TEXT_MODE, 0);// switch back to text mode
770}
771 }
772
773 if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
774{
775nbpUnloadBaseCode();
776 }
777}
778
779/*!
780 Selects a new BIOS device, taking care to update the global state appropriately.
781 */
782/*
783static void selectBiosDevice(void)
784{
785 struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
786 CacheReset();
787 diskFreeMap(oldMap);
788 oldMap = NULL;
789
790 int dev = selectAlternateBootDevice(gBIOSDev);
791
792 BVRef bvchain = scanBootVolumes(dev, 0);
793 BVRef bootVol = selectBootVolume(bvchain);
794 gBootVolume = bootVol;
795 setRootVolume(bootVol);
796 gBIOSDev = dev;
797}
798*/
799
800#define BASE 65521L /* largest prime smaller than 65536 */
801#define NMAX 5000
802// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
803
804#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
805#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
806#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
807#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
808#define DO16(buf) DO8(buf,0); DO8(buf,8);
809
810unsigned long Adler32(unsigned char *buf, long len)
811{
812 unsigned long s1 = 1; // adler & 0xffff;
813 unsigned long s2 = 0; // (adler >> 16) & 0xffff;
814 unsigned long result;
815 int k;
816
817 while (len > 0) {
818 k = len < NMAX ? len : NMAX;
819 len -= k;
820 while (k >= 16) {
821 DO16(buf);
822 buf += 16;
823 k -= 16;
824 }
825 if (k != 0) do {
826 s1 += *buf++;
827 s2 += s1;
828 } while (--k);
829 s1 %= BASE;
830 s2 %= BASE;
831 }
832 result = (s2 << 16) | s1;
833 return OSSwapHostToBigInt32(result);
834}
835

Archive Download this file

Revision: 983