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

Archive Download this file

Revision: 1468