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

Archive Download this file

Revision: 1595