Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/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
53#include "boot.h"
54#include "bootstruct.h"
55#include "fake_efi.h"
56#include "sl.h"
57#include "libsa.h"
58#include "platform.h"
59#include "graphics.h"
60#ifndef OPTION_ROM
61#include "appleboot.h"
62#endif
63#include "modules.h"
64#include "xml.h"
65
66#ifndef DEBUG_BOOT
67#define DEBUG_BOOT 0
68#endif
69
70#if DEBUG_BOOT
71#define DBG(x...)printf(x)
72#else
73#define DBG(x...)
74#endif
75
76
77typedef struct platform_info {
78char platformName[PLATFORM_NAME_LEN];
79char rootPath[ROOT_PATH_LEN];
80} PlatformInfo;
81
82
83char *gboardproduct = NULL;
84char *gPlatformName = NULL;
85//char gRootPath[256];
86long gBootMode; /* defaults to 0 == kBootModeNormal */
87bool gOverrideKernel;
88char gBootKernelCacheFile[512];
89char gMKextName[512];
90char gMacOSVersion[8];
91char *gRootDevice = NULL;
92
93#ifndef OPTION_ROM
94bool gEnableCDROMRescan;
95bool gScanSingleDrive;
96#endif
97
98int bvCount = 0;
99//intmenucount = 0;
100int gDeviceCount = 0;
101
102BVRef bvr;
103BVRef menuBVR;
104BVRef bvChain;
105
106static void zeroBSS(void);
107#ifdef SAFE_MALLOC
108static inline void malloc_error(char *addr, size_t size, const char *file, int line);
109#else
110static inline void malloc_error(char *addr, size_t size);
111#endif
112static int ExecKernel(void *binary);
113static bool getOSVersion(char *str);
114static void getRootDevice();
115#ifdef NBP_SUPPORT
116static bool gUnloadPXEOnExit = false;
117#endif
118static bool find_file_with_ext(const char* dir, const char *ext, const char * name_compare, size_t ext_size);
119static bool found_extra_kext(void);
120static void determineCpuArch(void);
121
122void getKernelCachePath();
123
124
125/*
126 * How long to wait (in seconds) to load the
127 * kernel after displaying the "boot:" prompt.
128 */
129#define kBootErrorTimeout 5
130
131/*
132 * Default path to kernel cache file
133 */
134#define kDefaultCachePath "/System/Library/Caches/com.apple.kext.caches/Startup/kernelcache"
135
136//==========================================================================
137// Zero the BSS.
138
139static void zeroBSS(void)
140{
141extern char _DATA__bss__begin, _DATA__bss__end;
142extern char _DATA__common__begin, _DATA__common__end;
143
144bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));
145bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));
146}
147
148//==========================================================================
149// Malloc error function
150
151#ifdef SAFE_MALLOC
152static inline void malloc_error(char *addr, size_t size, const char *file, int line)
153{
154 stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);
155}
156#else
157static inline void malloc_error(char *addr, size_t size)
158{
159 printf("\nMemory allocation error (0x%x, 0x%x)\n", (unsigned)addr, (unsigned)size);
160 asm volatile ("hlt");
161}
162#endif
163
164//==========================================================================
165//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
166//
167void initialize_runtime(void)
168{
169zeroBSS();
170malloc_init(0, 0, 0, malloc_error);
171}
172
173//==========================================================================
174// execKernel - Load the kernel image (mach-o) and jump to its entry point.
175
176static int ExecKernel(void *binary)
177{
178 entry_t kernelEntry;
179 int ret;
180
181 bootArgs->kaddr = bootArgs->ksize = 0;
182
183if(gMacOSVersion[3] <= '6')
184{
185bootArgs->Version = kBootArgsVersion1;
186bootArgs->Revision = gMacOSVersion[3];
187}
188else
189{
190#if kBootArgsVersion > 1
191
192bootArgs->Version = kBootArgsVersion;
193bootArgs->Revision = kBootArgsRevision;
194#else
195bootArgs->Version = 2;
196 bootArgs->Revision = 0;
197#endif
198}
199
200execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL, NULL, NULL);
201
202 ret = DecodeKernel(binary,
203 &kernelEntry,
204 (char **) &bootArgs->kaddr,
205 (int *)&bootArgs->ksize );
206
207 if ( ret != 0 )
208 return ret;
209
210 // Reserve space for boot args for 10.7 only (for 10.6 and earlier, we will convert (to legacy) the structure and reserve kernel memory for it later.)
211if(gMacOSVersion[3] == '7')
212 reserveKernBootStruct();
213
214 // Load boot drivers from the specifed root path.
215
216 if (!gHaveKernelCache)
217{
218LoadDrivers("/");
219 }
220
221#if TEXT_SPINNER
222clearActivityIndicator();
223#endif
224
225 if (gErrors)
226{
227 printf("Errors encountered while starting up the computer.\n");
228#if DEBUG_BOOT
229 printf("Pausing %d seconds...\n", kBootErrorTimeout);
230 sleep(kBootErrorTimeout);
231#endif
232 }
233
234execute_hook("md0Ramdisk", NULL, NULL, NULL, NULL, NULL, NULL);
235
236 setupFakeEfi();
237
238 verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");
239#ifdef NBP_SUPPORT
240 // Cleanup the PXE base code.
241
242 if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit )
243{
244if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
245 {
246 printf("nbpUnloadBaseCode error %d\n", (int) ret);
247 sleep(2);
248 }
249 }
250#endif
251{
252bool wait = false;
253const char *strval = 0;
254int dummysize /*= 0*/;
255
256getBoolForKey(kWaitForKeypressKey, &wait, &bootInfo->bootConfig);
257
258if (getValueForBootKey(bootArgs->CommandLine, "-wait", &strval, &dummysize))
259{
260wait = true;
261
262if (strval && ((strcmp(strval, "no") == 0) || (strcmp(strval, "No") == 0)))
263{
264wait = false;
265}
266}
267
268if (wait == true)
269{
270pause();
271}
272}
273
274if ((execute_hook("GUI_ExecKernel", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)) // (bootArgs->Video.v_display == VGA_TEXT_MODE)
275{
276#if UNUSED
277setVideoMode( GRAPHICS_MODE, 0 );
278#else
279setVideoMode( GRAPHICS_MODE );
280#endif
281
282#ifndef OPTION_ROM
283
284if(!gVerboseMode)
285{
286drawColorRectangle(0, 0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 0x01);
287
288uint8_t *appleBootPict;
289uint16_t bootImageWidth = kAppleBootWidth;
290uint16_t bootImageHeight = kAppleBootHeight;
291uint8_t *bootImageData = NULL;
292uint16_t x, y;
293
294unsigned long screen_params[4] = {DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 32, 0};// here we store the used screen resolution
295// Prepare the data for the default Apple boot image.
296appleBootPict = (uint8_t *) decodeRLE(gAppleBootPictRLE, kAppleBootRLEBlocks, bootImageWidth * bootImageHeight);
297if (appleBootPict)
298{
299convertImage(bootImageWidth, bootImageHeight, appleBootPict, &bootImageData);
300if (bootImageData)
301{
302x = (screen_params[0] - MIN(kAppleBootWidth, screen_params[0])) / 2;
303y = (screen_params[1] - MIN(kAppleBootHeight, screen_params[1])) / 2;
304drawDataRectangle(x, y, kAppleBootWidth, kAppleBootHeight, bootImageData);
305free(bootImageData);
306}
307free(appleBootPict);
308}
309
310}
311#endif
312}
313
314 finalizeEFIConfigTable();
315
316setupBooterLog();
317
318 finalizeBootStruct();
319
320
321if (gMacOSVersion[3] <= '6')
322reserveKernLegacyBootStruct();
323
324execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL, NULL, NULL);// Notify modules that the kernel is about to be started
325#if UNUSED
326turnOffFloppy();
327#endif
328#if BETA
329#include "smp-imps.h"
330#include "apic.h"
331IMPS_LAPIC_WRITE(LAPIC_LVT1, LAPIC_ICR_DM_NMI);
332#endif
333
334if (gMacOSVersion[3] <= '6')
335{
336// Jump to kernel's entry point. There's no going back now. XXX LEGACY OS XXX
337startprog( kernelEntry, bootArgsLegacy );
338}
339
340outb(0x21, 0xff); /* Maskout all interrupts Pic1 */
341outb(0xa1, 0xff); /* Maskout all interrupts Pic2 */
342
343// Jump to kernel's entry point. There's no going back now. XXX LION XXX
344 startprog( kernelEntry, bootArgs );
345
346 // Not reached
347
348 return 0;
349}
350
351//==========================================================================
352// This is the entrypoint from real-mode which functions exactly as it did
353// before. Multiboot does its own runtime initialization, does some of its
354// own things, and then calls common_boot.
355void boot(int biosdev)
356{
357initialize_runtime();
358// Enable A20 gate before accessing memory above 1Mb.
359enableA20();
360common_boot(biosdev);
361}
362
363//==========================================================================
364// The 'main' function for the booter. Called by boot0 when booting
365// from a block device, or by the network booter.
366//
367// arguments:
368// biosdev - Value passed from boot1/NBP to specify the device
369// that the booter was loaded from.
370//
371// If biosdev is kBIOSDevNetwork, then this function will return if
372// booting was unsuccessful. This allows the PXE firmware to try the
373// next boot device on its list.
374void common_boot(int biosdev)
375{
376 int status;
377 bool firstRun = true;
378
379 unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;
380 unsigned int denyBVFlags = kBVFlagEFISystem;
381
382#ifdef NBP_SUPPORT
383 // Set reminder to unload the PXE base code. Neglect to unload
384 // the base code will result in a hang or kernel panic.
385 gUnloadPXEOnExit = true;
386#endif
387 // Record the device that the booter was loaded from.
388 gBIOSDev = biosdev & kBIOSDevMask;
389
390
391
392 // Setup VGA text mode.
393 // Not sure if it is safe to call setVideoMode() before the
394 // config table has been loaded. Call video_mode() instead.
395#if DEBUG
396 printf("before video_mode\n");
397#endif
398 video_mode( 2 ); // 80x25 mono text mode.
399#if DEBUG
400 printf("after video_mode\n");
401#endif
402#if !TEXT_SPINNER
403printf("Starting Chameleon ...\n");
404#endif
405
406initBooterLog();
407
408// Initialize boot info structure.
409 initKernBootStruct();
410
411
412 // Scan and record the system's hardware information.
413 scan_platform();
414
415 // First get info for boot volume.
416 scanBootVolumes(gBIOSDev, 0);
417 bvChain = getBVChainForBIOSDev(gBIOSDev);
418 setBootGlobals(bvChain);
419
420 // Load boot.plist config file
421 status = loadSystemConfig(&bootInfo->bootConfig);
422
423Platform->CPU.isServer = false;
424 getBoolForKey(kIsServer, &Platform->CPU.isServer, &bootInfo->bootConfig); // set this as soon as possible
425
426{
427bool quiet = false;
428if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet)
429{
430gBootMode |= kBootModeQuiet;
431}
432}
433
434{
435bool instantMenu = false;
436// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
437if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu)
438{
439firstRun = false;
440}
441}
442
443#ifndef OPTION_ROM
444// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
445 if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive)
446{
447 gScanSingleDrive = true;
448 }
449
450// Create a list of partitions on device(s).
451 if (gScanSingleDrive)
452{
453scanBootVolumes(gBIOSDev, &bvCount);
454 }
455else
456#endif
457{
458#if UNUSED
459 scanDisks(gBIOSDev, &bvCount);
460#else
461 scanDisks();
462#endif
463 }
464
465// Create a separated bvr chain using the specified filters.
466 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
467gBootVolume = selectBootVolume(bvChain);
468
469{
470// Intialize module system
471EFI_STATUS sysinit = init_module_system();
472if((sysinit == EFI_SUCCESS) || (sysinit == EFI_ALREADY_STARTED) /*should never happen*/ )
473{
474load_all_modules();
475}
476}
477
478 // Loading preboot ramdisk if exists.
479execute_hook("loadPrebootRAMDisk", NULL, NULL, NULL, NULL, NULL, NULL);
480
481#ifndef OPTION_ROM
482
483 // Disable rescan option by default
484 gEnableCDROMRescan = false;
485
486 // Enable it with Rescan=y in system config
487 if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan)
488{
489 gEnableCDROMRescan = true;
490 }
491
492{
493bool rescanPrompt = false;
494// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
495if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))
496{
497gEnableCDROMRescan = promptForRescanOption();
498}
499}
500
501#endif
502
503#if DEBUG
504 printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
505 printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
506 getc();
507#endif
508
509 setBootGlobals(bvChain);
510
511// Display the GUI
512execute_hook("GUI_Display", NULL, NULL, NULL, NULL, NULL, NULL);
513
514 // Parse args, load and start kernel.
515 while (1) {
516 const char *val;
517 int len;
518char *bootFile;
519bool trycache = true; // Always try to catch the kernelcache first
520
521 long flags;
522#ifdef BOOT_HELPER_SUPPORT
523long time;
524#endif
525 int ret = -1;
526 void *binary = (void *)kLoadAddr;
527
528 // additional variable for testing alternate kernel image locations on boot helper partitions.
529 char bootFileSpec[512];
530
531 // Initialize globals.
532
533 sysConfigValid = false;
534 gErrors = false;
535
536 status = getBootOptions(firstRun);
537 firstRun = false;
538 if (status == -1) continue;
539
540 status = processBootOptions();
541 // Status==1 means to chainboot
542 if ( status == 1 ) break;
543 // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL
544 if ( status == -1 )
545 {
546// gBootVolume == NULL usually means the user hit escape.
547if(gBootVolume == NULL)
548{
549freeFilteredBVChain(bvChain);
550#ifndef OPTION_ROM
551if (gEnableCDROMRescan)
552rescanBIOSDevice(gBIOSDev);
553#endif
554
555bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
556setBootGlobals(bvChain);
557}
558continue;
559 }
560
561 // Other status (e.g. 0) means that we should proceed with boot.
562execute_hook("GUI_PreBoot", NULL, NULL, NULL, NULL, NULL, NULL);
563
564// Find out which version mac os we're booting.
565getOSVersion(gMacOSVersion);
566
567if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig) && val)
568{
569if (strncmp(val, "x86_64", 4) == 0)
570{
571archCpuType = CPU_TYPE_X86_64;
572}
573else if (strncmp(val, "i386", 4) == 0)
574{
575archCpuType = CPU_TYPE_I386;
576}
577else
578{
579DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n")
580determineCpuArch();
581}
582
583}
584else determineCpuArch();
585
586
587getRootDevice();
588
589// Notify to all modules that we are attempting to boot
590execute_hook("PreBoot", NULL, NULL, NULL, NULL, NULL, NULL);
591
592if (execute_hook("getProductNamePatched", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)
593readSMBIOS(thePlatformName); // read smbios Platform Name
594
595
596if (((gBootMode & kBootModeSafe) == 0) &&
597!gOverrideKernel &&
598(gBootFileType == kBlockDeviceType) &&
599(gMKextName[0] == '\0') &&
600!getValueForBootKey(bootArgs->CommandLine, kIgnorePrelinkKern, &val, &len))
601{
602getBoolForKey(kUseKernelCache, &trycache, &bootInfo->bootConfig);
603if (trycache == true)
604{
605// try to find the cache and fill the gBootKernelCacheFile string
606getKernelCachePath();
607
608// Check for cache file
609trycache = (gBootKernelCacheFile[0] != '\0') ? true : false; // if gBootKernelCacheFile is filled et bla bla bla.... :-)
610}
611
612}
613else
614{
615trycache = false;
616}
617
618verbose("Loading Darwin %s\n", gMacOSVersion);
619{
620long cachetime, kerneltime, exttime;
621if (trycache ) do {
622
623// if we haven't found the kernel yet, don't use the cache
624ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
625if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
626{
627trycache = 0;
628bootInfo->adler32 = 0;
629DBG("No kernel found, kernelcache disabled !!!\n");
630break;
631}
632ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
633if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
634|| (cachetime < kerneltime))
635{
636trycache = 0;
637bootInfo->adler32 = 0;
638DBG("Warning: kernelcache too old, timestamp of the kernel > timestamp of the cache, kernelcache disabled !!!\n");
639
640break;
641}
642ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
643if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
644&& (cachetime < exttime))
645{
646trycache = 0;
647bootInfo->adler32 = 0;
648DBG("Warning: kernelcache too old, timestamp of S/L/E > timestamp of the cache, kernelcache disabled !!! \n");
649
650break;
651}
652if (kerneltime > exttime)
653{
654exttime = kerneltime;
655}
656if (cachetime != (exttime + 1))
657{
658trycache = 0;
659bootInfo->adler32 = 0;
660DBG("Warning: invalid timestamp, kernelcache disabled !!!\n");
661
662break;
663}
664} while (0);
665}
666
667 do {
668 if (trycache)
669{
670 bootFile = gBootKernelCacheFile;
671 verbose("Loading kernel cache %s\n", bootFile);
672if (gMacOSVersion[3] == '7')
673{
674ret = LoadThinFatFile(bootFile, &binary);
675if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
676{
677archCpuType = CPU_TYPE_I386;
678ret = LoadThinFatFile(bootFile, &binary);
679}
680}
681else
682{
683ret = LoadFile(bootFile);
684binary = (void *)kLoadAddr;
685}
686
687 if (ret >= 0)
688{
689 break;
690 }
691
692 }
693bootInfo->adler32 = 0;
694 bootFile = bootInfo->bootFile;
695#ifdef BOOT_HELPER_SUPPORT
696
697 // Try to load kernel image from alternate locations on boot helper partitions.
698 sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);
699 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
700 if (ret == -1)
701 {
702sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);
703ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
704if (ret == -1)
705{
706sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);
707ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
708if (ret == -1)
709{
710// Not found any alternate locations, using the original kernel image path.
711strlcpy(bootFileSpec, bootFile,sizeof(bootFileSpec)+1);
712}
713}
714 }
715#else
716strlcpy(bootFileSpec, bootFile,sizeof(bootFileSpec)+1);
717#endif
718
719 verbose("Loading kernel %s\n", bootFileSpec);
720 ret = LoadThinFatFile(bootFileSpec, &binary);
721 if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
722 {
723archCpuType = CPU_TYPE_I386;
724ret = LoadThinFatFile(bootFileSpec, &binary);
725 }
726
727 } while (0);
728#if TEXT_SPINNER
729 clearActivityIndicator();
730#endif
731
732#if DEBUG
733 printf("Pausing...");
734 sleep(8);
735#endif
736
737 if (ret <= 0)
738{
739printf("Can't find %s\n", bootFile);
740
741sleep(1);
742#ifdef NBP_SUPPORT
743 if (gBootFileType == kNetworkDeviceType)
744{
745 // Return control back to PXE. Don't unload PXE base code.
746 gUnloadPXEOnExit = false;
747 break;
748 }
749#endif
750 }
751else
752{
753 /* Won't return if successful. */
754 ret = ExecKernel(binary);
755 }
756 }
757
758 // chainboot
759 if (status==1)
760{
761if (getVideoMode() == GRAPHICS_MODE)
762{// if we are already in graphics-mode,
763#if UNUSED
764setVideoMode(VGA_TEXT_MODE, 0);// switch back to text mode
765#else
766setVideoMode(VGA_TEXT_MODE);// switch back to text mode
767#endif
768}
769 }
770#ifdef NBP_SUPPORT
771 if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
772{
773nbpUnloadBaseCode();
774 }
775#endif
776}
777
778static void determineCpuArch(void)
779{
780if (cpu_mode_is64bit())
781{
782archCpuType = CPU_TYPE_X86_64;
783}
784else
785{
786archCpuType = CPU_TYPE_I386;
787}
788}
789
790void getKernelCachePath()
791{
792{
793// If there is an extra kext/mkext, we return immediatly and we skip the kernelCache
794// since kexts/mkexts are not loaded properly when the kernelCache is used.
795// Another method would be to re-build the kernelCache one the fly
796if (found_extra_kext() == true) return;
797}
798
799{
800const char *val;
801int len;
802
803if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))
804{
805 char * buffer = newString(val);
806
807if (val[0] == '\\')
808{
809// Flip the back slash's to slash's .
810 len = 0;
811 while (buffer[len] != '\0') {
812 if (buffer[len] == '\\')
813 {
814 buffer[len] = '/';
815 }
816 len++;
817 }
818}
819strlcpy(gBootKernelCacheFile, buffer, sizeof(gBootKernelCacheFile)+1);
820}
821else
822{
823if(gMacOSVersion[3] == '7')
824{
825sprintf(gBootKernelCacheFile, "%s", kDefaultCachePath);
826}
827else if(gMacOSVersion[3] <= '6')
828{
829
830PlatformInfo *platformInfo = malloc(sizeof(PlatformInfo));
831if (platformInfo)
832{
833
834bzero(platformInfo, sizeof(PlatformInfo));
835
836if (gPlatformName)
837strlcpy(platformInfo->platformName,gPlatformName, sizeof(platformInfo->platformName)+1);
838
839if (gRootDevice)
840{
841char *rootPath_p = platformInfo->rootPath;
842len = strlen(gRootDevice) + 1;
843if ((unsigned)len > sizeof(platformInfo->rootPath))
844{
845len = sizeof(platformInfo->rootPath);
846}
847memcpy(rootPath_p, gRootDevice,len);
848
849rootPath_p += len;
850
851len = strlen(bootInfo->bootFile);
852
853if ((unsigned)(rootPath_p - platformInfo->rootPath + len) >=
854sizeof(platformInfo->rootPath))
855{
856
857len = sizeof(platformInfo->rootPath) -
858(rootPath_p - platformInfo->rootPath);
859}
860memcpy(rootPath_p, bootInfo->bootFile, len);
861
862}
863
864if (!platformInfo->platformName[0] || !platformInfo->rootPath[0])
865{
866platformInfo->platformName[0] = platformInfo->rootPath[0] = 0;
867}
868//memcpy(gRootPath,platformInfo->rootPath, sizeof(gRootPath));
869
870
871bootInfo->adler32 = OSSwapHostToBigInt32(adler32((unsigned char *)platformInfo, sizeof(*platformInfo)));
872
873free(platformInfo);
874}
875
876DBG("Adler32: %08lX\n",bootInfo->adler32);
877
878if (gMacOSVersion[3] < '6')
879{
880long flags, cachetime;
881int ret = -1;
882sprintf(gBootKernelCacheFile, "%s.%08lX", "/System/Library/Caches/com.apple.kernelcaches/kernelcache",bootInfo->adler32);
883ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
884if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
885{
886bootInfo->adler32 = 0;
887sprintf(gBootKernelCacheFile, "%s", "/System/Library/Caches/com.apple.kernelcaches/kernelcache");
888}
889
890} else
891sprintf(gBootKernelCacheFile, "%s_%s.%08lX", kDefaultCachePath, (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64", bootInfo->adler32); //Snow Leopard
892
893}
894}
895}
896}
897
898
899static void getRootDevice()
900{
901// Maximum config table value size
902#define VALUE_SIZE 2048
903bool uuidSet = false;
904const char *val = 0;
905 int cnt = 0;
906
907if (getValueForKey(kBootUUIDKey, &val, &cnt, &bootInfo->bootConfig))
908{
909uuidSet = true;
910}
911else
912{
913if (getValueForBootKey(bootArgs->CommandLine, kRootDeviceKey, &val, &cnt))
914{
915if (*val == '*' && *(val + 1) != '/' && *(val + 1) != 'u')
916{
917val += 1; //skip the *
918uuidSet = true;
919
920}
921else if (*val == '*' && *(val + 1) == 'u')
922{
923
924if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig))
925uuidSet = true;
926
927}
928}
929else
930{
931#ifdef BOOT_HELPER_SUPPORT
932//
933// Try an alternate method for getting the root UUID on boot helper partitions.
934//
935if (gBootVolume->flags & kBVFlagBooter)
936{
937if((loadHelperConfig(&bootInfo->helperConfig) == 0)
938 && getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig) )
939{
940getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig);
941uuidSet = true;
942goto out;
943}
944}
945#endif
946if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig))
947{
948extern int ArgCntRemaining;
949uuidSet = false;
950char * valueBuffer;
951valueBuffer = malloc(VALUE_SIZE);
952char * argP = bootArgs->CommandLine;
953valueBuffer[0] = '*';
954if (cnt > VALUE_SIZE)
955{
956cnt = VALUE_SIZE;
957}
958cnt++;
959strlcpy(valueBuffer + 1, val, cnt);
960if (!copyArgument( kRootDeviceKey, valueBuffer, cnt, &argP, &ArgCntRemaining))
961{
962free(valueBuffer);
963printf("Error: boot arguments too long, unable to set root device !!");
964getc();
965return;
966}
967free(valueBuffer);
968goto out;
969}
970
971if (gBootVolume->fs_getuuid && gBootVolume->fs_getuuid (gBootVolume, bootInfo->uuidStr) == 0)
972{
973verbose("Setting boot-uuid to: %s\n", bootInfo->uuidStr);
974uuidSet = true;
975gRootDevice = bootInfo->uuidStr;
976return;
977}
978
979}
980}
981
982out:
983verbose("Setting %s to: %s\n", uuidSet ? kBootUUIDKey : "root device", (char* )val);
984gRootDevice = (char* )val;
985}
986
987static bool getOSVersion(char *str)
988{
989bool valid = false;
990config_file_t systemVersion;
991
992if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))
993{
994valid = true;
995}
996else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))
997{
998valid = true;
999}
1000
1001if (valid)
1002{
1003const char *val;
1004int len;
1005
1006if (getValueForKey(kProductVersion, &val, &len, &systemVersion))
1007{
1008// getValueForKey uses const char for val
1009// so copy it and trim
1010*str = '\0';
1011strncat(str, val, MIN(len, 4));
1012}
1013else
1014valid = false;
1015}
1016
1017return valid;
1018}
1019
1020static bool find_file_with_ext(const char* dir, const char *ext, const char * name_compare, size_t ext_size)
1021{
1022char* name;
1023long flags;
1024long time;
1025struct dirstuff* moduleDir = opendir(dir);
1026while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)
1027{
1028int len = strlen(name);
1029
1030if (len >= ext_size)
1031{
1032if(strcmp(&name[len - ext_size], ext) == 0)
1033{
1034if (name_compare)
1035{
1036if (strcmp(name, name_compare) == 0)
1037{
1038DBG("found : %s\n", name);
1039return true;
1040}
1041}
1042else
1043{
1044DBG("found : %s\n", name);
1045return true;
1046}
1047}
1048#if DEBUG_BOOT
1049else
1050{
1051DBG("Ignoring %s\n", name);
1052}
1053#endif
1054}
1055#if DEBUG_BOOT
1056else
1057{
1058DBG("Ignoring %s\n", name);
1059}
1060#endif
1061}
1062
1063return false;
1064}
1065
1066// If a kext is found in /Extra/Extentions return true
1067// If a mkext is found in /Extra return true
1068// Otherwise return false
1069// Tips (if you still use extra kext(s)/mkext(s) ): With Lion it's recommended to create a system mkext (see the kextcache commandline) to decrease boot time,
1070// otherwise it will act like a -f each time a extra kext/kext is detected
1071static bool found_extra_kext(void)
1072{
1073#define EXTENSIONS "Extensions"
1074#define MKEXT_EXT ".mkext"
1075#define MKEXT_EXT_SIZE sizeof("mkext")
1076#define KEXT_EXT ".kext"
1077#define KEXT_EXT_SIZE sizeof("kext")
1078
1079long flags;
1080long exttime;
1081int ret = -1;
1082
1083ret = GetFileInfo("rd(0,0)/Extra/", EXTENSIONS, &flags, &exttime);
1084if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))
1085{
1086if (((flags & kFileTypeMask) == kFileTypeFlat))
1087{
1088if (find_file_with_ext("rd(0,0)/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))
1089{
1090return true;
1091}
1092}
1093else if (((flags & kFileTypeMask) == kFileTypeDirectory))
1094{
1095if (find_file_with_ext("rd(0,0)/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))
1096{
1097return true;
1098}
1099}
1100}
1101ret = GetFileInfo("/Extra/", EXTENSIONS, &flags, &exttime);
1102if (ret == 0)
1103{
1104if (((flags & kFileTypeMask) == kFileTypeFlat))
1105{
1106if (find_file_with_ext("/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))
1107{
1108return true;
1109}
1110}
1111else if (((flags & kFileTypeMask) == kFileTypeDirectory))
1112{
1113if (find_file_with_ext("/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))
1114{
1115return true;
1116}
1117}
1118}
1119ret = GetFileInfo("bt(0,0)/Extra/", EXTENSIONS, &flags, &exttime);
1120if (ret == 0)
1121{
1122if (((flags & kFileTypeMask) == kFileTypeFlat))
1123{
1124if (find_file_with_ext("bt(0,0)/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))
1125{
1126return true;
1127}
1128}
1129else if (((flags & kFileTypeMask) == kFileTypeDirectory))
1130{
1131if (find_file_with_ext("bt(0,0)/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))
1132{
1133return true;
1134}
1135}
1136}
1137DBG("NO Extra Mkext/Kext found\n");
1138
1139// nothing found
1140return false;
1141}
1142
1143#if 0
1144static char *FIXED_BOOTFILE_PATH(char * str)
1145{
1146char bootfile[128];
1147
1148bool bootFileWithDevice = false;
1149// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
1150if (strncmp(str,"bt(",3) == 0 ||
1151strncmp(str,"hd(",3) == 0 ||
1152strncmp(str,"rd(",3) == 0)
1153{
1154bootFileWithDevice = true;
1155}
1156
1157// bootFile must start with a / if it not start with a device name
1158if (!bootFileWithDevice && (str)[0] != '/')
1159sprintf(bootFile, "/%s", str); // append a leading /
1160else
1161strlcpy(bootFile, bootInfo->bootFile, sizeof(bootInfo->bootFile)+1);
1162
1163return bootfile;
1164}
1165#endif
1166

Archive Download this file

Revision: 1525