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#if virtualM
443 firstRun = false;
444#endif
445}
446
447#ifndef OPTION_ROM
448// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
449 if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive)
450{
451 gScanSingleDrive = true;
452 }
453
454// Create a list of partitions on device(s).
455 if (gScanSingleDrive)
456{
457scanBootVolumes(gBIOSDev, &bvCount);
458 }
459else
460#endif
461{
462#if UNUSED
463 scanDisks(gBIOSDev, &bvCount);
464#else
465 scanDisks();
466#endif
467 }
468
469// Create a separated bvr chain using the specified filters.
470 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
471gBootVolume = selectBootVolume(bvChain);
472
473{
474// Intialize module system
475EFI_STATUS sysinit = init_module_system();
476if((sysinit == EFI_SUCCESS) || (sysinit == EFI_ALREADY_STARTED) /*should never happen*/ )
477{
478load_all_modules();
479}
480}
481
482 load_all_internal_modules();
483
484 // Loading preboot ramdisk if exists.
485execute_hook("loadPrebootRAMDisk", NULL, NULL, NULL, NULL, NULL, NULL);
486
487#ifndef OPTION_ROM
488
489 // Disable rescan option by default
490 gEnableCDROMRescan = false;
491
492 // Enable it with Rescan=y in system config
493 if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan)
494{
495 gEnableCDROMRescan = true;
496 }
497
498{
499bool rescanPrompt = false;
500// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
501if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev))
502{
503gEnableCDROMRescan = promptForRescanOption();
504}
505}
506
507#endif
508
509#if DEBUG
510 printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
511 printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
512 getc();
513#endif
514
515 setBootGlobals(bvChain);
516
517// Display the GUI
518execute_hook("GUI_Display", NULL, NULL, NULL, NULL, NULL, NULL);
519
520 // Parse args, load and start kernel.
521 while (1) {
522 const char *val;
523 int len;
524char *bootFile;
525bool trycache = true; // Always try to catch the kernelcache first
526
527 long flags;
528#ifdef BOOT_HELPER_SUPPORT
529long time;
530#endif
531 int ret = -1;
532 void *binary = (void *)kLoadAddr;
533
534 // additional variable for testing alternate kernel image locations on boot helper partitions.
535 char bootFileSpec[512];
536
537 // Initialize globals.
538
539 sysConfigValid = false;
540 gErrors = false;
541
542 status = getBootOptions(firstRun);
543 firstRun = false;
544 if (status == -1) continue;
545
546 status = processBootOptions();
547 // Status==1 means to chainboot
548 if ( status == 1 ) break;
549 // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL
550 if ( status == -1 )
551 {
552// gBootVolume == NULL usually means the user hit escape.
553if(gBootVolume == NULL)
554{
555freeFilteredBVChain(bvChain);
556#ifndef OPTION_ROM
557if (gEnableCDROMRescan)
558rescanBIOSDevice(gBIOSDev);
559#endif
560
561bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
562setBootGlobals(bvChain);
563}
564continue;
565 }
566
567 // Other status (e.g. 0) means that we should proceed with boot.
568execute_hook("GUI_PreBoot", NULL, NULL, NULL, NULL, NULL, NULL);
569
570// Find out which version mac os we're booting.
571getOSVersion(gMacOSVersion);
572
573if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig) && val)
574{
575if (strncmp(val, "x86_64", 4) == 0)
576{
577archCpuType = CPU_TYPE_X86_64;
578}
579else if (strncmp(val, "i386", 4) == 0)
580{
581archCpuType = CPU_TYPE_I386;
582}
583else
584{
585DBG("Incorrect parameter for option 'arch =' , please use x86_64 or i386\n");
586determineCpuArch();
587}
588
589}
590else determineCpuArch();
591
592
593getRootDevice();
594
595// Notify to all modules that we are attempting to boot
596execute_hook("PreBoot", NULL, NULL, NULL, NULL, NULL, NULL);
597
598if (execute_hook("getProductNamePatched", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)
599readSMBIOS(thePlatformName); // read smbios Platform Name
600
601
602if (((gBootMode & kBootModeSafe) == 0) &&
603!gOverrideKernel &&
604(gBootFileType == kBlockDeviceType) &&
605(gMKextName[0] == '\0') &&
606!getValueForBootKey(bootArgs->CommandLine, kIgnorePrelinkKern, &val, &len))
607{
608getBoolForKey(kUseKernelCache, &trycache, &bootInfo->bootConfig);
609if (trycache == true)
610{
611// try to find the cache and fill the gBootKernelCacheFile string
612getKernelCachePath();
613
614// Check for cache file
615trycache = (gBootKernelCacheFile[0] != '\0') ? true : false; // if gBootKernelCacheFile is filled et bla bla bla.... :-)
616}
617
618}
619else
620{
621trycache = false;
622}
623
624verbose("Loading Darwin %s\n", gMacOSVersion);
625{
626long cachetime, kerneltime, exttime;
627if (trycache && !forcecache) do {
628
629// if we haven't found the kernel yet, don't use the cache
630ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
631if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
632{
633trycache = 0;
634bootInfo->adler32 = 0;
635DBG("No kernel found, kernelcache disabled !!!\n");
636break;
637}
638ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
639if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
640|| (cachetime < kerneltime))
641{
642trycache = 0;
643bootInfo->adler32 = 0;
644DBG("Warning: No kernelcache found or kernelcache too old (timestamp of the kernel > timestamp of the cache), kernelcache disabled !!!\n");
645
646break;
647}
648ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
649if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
650&& (cachetime < exttime))
651{
652trycache = 0;
653bootInfo->adler32 = 0;
654DBG("Warning: kernelcache too old, timestamp of S/L/E > timestamp of the cache, kernelcache disabled !!! \n");
655
656break;
657}
658if (kerneltime > exttime)
659{
660exttime = kerneltime;
661}
662if (cachetime != (exttime + 1))
663{
664trycache = 0;
665bootInfo->adler32 = 0;
666DBG("Warning: invalid timestamp, kernelcache disabled !!!\n");
667
668break;
669}
670} while (0);
671}
672
673 do {
674 if (trycache)
675{
676 bootFile = gBootKernelCacheFile;
677 verbose("Loading kernel cache %s\n", bootFile);
678if (gMacOSVersion[3] == '7')
679{
680ret = LoadThinFatFile(bootFile, &binary);
681if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
682{
683archCpuType = CPU_TYPE_I386;
684ret = LoadThinFatFile(bootFile, &binary);
685}
686}
687else
688{
689ret = LoadFile(bootFile);
690binary = (void *)kLoadAddr;
691}
692
693 if (ret >= 0)
694{
695 break;
696 }
697
698 }
699bootInfo->adler32 = 0;
700 bootFile = bootInfo->bootFile;
701#ifdef BOOT_HELPER_SUPPORT
702
703 // Try to load kernel image from alternate locations on boot helper partitions.
704 sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);
705 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
706 if (ret == -1)
707 {
708sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);
709ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
710if (ret == -1)
711{
712sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);
713ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
714if (ret == -1)
715{
716// Not found any alternate locations, using the original kernel image path.
717strlcpy(bootFileSpec, bootFile,sizeof(bootFileSpec)+1);
718}
719}
720 }
721#else
722strlcpy(bootFileSpec, bootFile,sizeof(bootFileSpec)+1);
723#endif
724
725 verbose("Loading kernel %s\n", bootFileSpec);
726 ret = LoadThinFatFile(bootFileSpec, &binary);
727 if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
728 {
729archCpuType = CPU_TYPE_I386;
730ret = LoadThinFatFile(bootFileSpec, &binary);
731 }
732
733 } while (0);
734#if TEXT_SPINNER
735 clearActivityIndicator();
736#endif
737
738#if DEBUG
739 printf("Pausing...");
740 sleep(8);
741#endif
742
743 if (ret <= 0)
744{
745printf("Can't find %s\n", bootFile);
746
747sleep(1);
748#ifdef NBP_SUPPORT
749 if (gBootFileType == kNetworkDeviceType)
750{
751 // Return control back to PXE. Don't unload PXE base code.
752 gUnloadPXEOnExit = false;
753 break;
754 }
755#endif
756 }
757else
758{
759 /* Won't return if successful. */
760 ret = ExecKernel(binary);
761 }
762 }
763
764 // chainboot
765 if (status==1)
766{
767if (getVideoMode() == GRAPHICS_MODE)
768{// if we are already in graphics-mode,
769#if UNUSED
770setVideoMode(VGA_TEXT_MODE, 0);// switch back to text mode
771#else
772setVideoMode(VGA_TEXT_MODE);// switch back to text mode
773#endif
774}
775 }
776#ifdef NBP_SUPPORT
777 if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
778{
779nbpUnloadBaseCode();
780 }
781#endif
782}
783
784static void determineCpuArch(void)
785{
786if (cpu_mode_is64bit())
787{
788archCpuType = CPU_TYPE_X86_64;
789}
790else
791{
792archCpuType = CPU_TYPE_I386;
793}
794}
795
796void getKernelCachePath()
797{
798{
799// If there is an extra kext/mkext, we return immediatly and we skip the kernelCache
800// since kexts/mkexts are not loaded properly when the kernelCache is used.
801// Another method would be to re-build the kernelCache one the fly
802if (found_extra_kext() == true) return;
803}
804
805{
806const char *val;
807int len;
808
809if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))
810{
811 char * buffer = newString(val);
812
813if (val[0] == '\\')
814{
815// Flip the back slash's to slash's .
816 len = 0;
817 while (buffer[len] != '\0') {
818 if (buffer[len] == '\\')
819 {
820 buffer[len] = '/';
821 }
822 len++;
823 }
824}
825strlcpy(gBootKernelCacheFile, buffer, sizeof(gBootKernelCacheFile)+1);
826 forcecache = true;
827}
828else
829{
830if(gMacOSVersion[3] == '7')
831{
832sprintf(gBootKernelCacheFile, "%s", kDefaultCachePath);
833}
834else if(gMacOSVersion[3] <= '6')
835{
836
837PlatformInfo *platformInfo = malloc(sizeof(PlatformInfo));
838if (platformInfo)
839{
840
841bzero(platformInfo, sizeof(PlatformInfo));
842
843if (gPlatformName)
844strlcpy(platformInfo->platformName,gPlatformName, sizeof(platformInfo->platformName)+1);
845
846if (gRootDevice)
847{
848char *rootPath_p = platformInfo->rootPath;
849len = strlen(gRootDevice) + 1;
850if ((unsigned)len > sizeof(platformInfo->rootPath))
851{
852len = sizeof(platformInfo->rootPath);
853}
854memcpy(rootPath_p, gRootDevice,len);
855
856rootPath_p += len;
857
858len = strlen(bootInfo->bootFile);
859
860if ((unsigned)(rootPath_p - platformInfo->rootPath + len) >=
861sizeof(platformInfo->rootPath))
862{
863
864len = sizeof(platformInfo->rootPath) -
865(rootPath_p - platformInfo->rootPath);
866}
867memcpy(rootPath_p, bootInfo->bootFile, len);
868
869}
870
871if (!platformInfo->platformName[0] || !platformInfo->rootPath[0])
872{
873platformInfo->platformName[0] = platformInfo->rootPath[0] = 0;
874}
875//memcpy(gRootPath,platformInfo->rootPath, sizeof(gRootPath));
876
877
878bootInfo->adler32 = OSSwapHostToBigInt32(adler32((unsigned char *)platformInfo, sizeof(*platformInfo)));
879
880free(platformInfo);
881}
882
883DBG("Adler32: %08lX\n",bootInfo->adler32);
884
885if (gMacOSVersion[3] < '6')
886{
887long flags, cachetime;
888int ret = -1;
889sprintf(gBootKernelCacheFile, "%s.%08lX", "/System/Library/Caches/com.apple.kernelcaches/kernelcache",bootInfo->adler32);
890ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
891if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
892{
893bootInfo->adler32 = 0;
894sprintf(gBootKernelCacheFile, "%s", "/System/Library/Caches/com.apple.kernelcaches/kernelcache");
895}
896
897} else
898sprintf(gBootKernelCacheFile, "%s_%s.%08lX", kDefaultCachePath, (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64", bootInfo->adler32); //Snow Leopard
899
900}
901}
902}
903}
904
905
906static void getRootDevice()
907{
908// Maximum config table value size
909#define VALUE_SIZE 2048
910bool uuidSet = false;
911const char *val = 0;
912 int cnt = 0;
913
914if (getValueForKey(kBootUUIDKey, &val, &cnt, &bootInfo->bootConfig))
915{
916uuidSet = true;
917}
918else
919{
920if (getValueForBootKey(bootArgs->CommandLine, kRootDeviceKey, &val, &cnt))
921{
922if (*val == '*' && *(val + 1) != '/' && *(val + 1) != 'u')
923{
924val += 1; //skip the *
925uuidSet = true;
926
927}
928else if (*val == '*' && *(val + 1) == 'u')
929{
930
931if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig))
932uuidSet = true;
933
934}
935}
936else
937{
938#ifdef BOOT_HELPER_SUPPORT
939//
940// Try an alternate method for getting the root UUID on boot helper partitions.
941//
942if (gBootVolume->flags & kBVFlagBooter)
943{
944if((loadHelperConfig(&bootInfo->helperConfig) == 0)
945 && getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig) )
946{
947getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig);
948uuidSet = true;
949goto out;
950}
951}
952#endif
953if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig))
954{
955extern int ArgCntRemaining;
956uuidSet = false;
957char * valueBuffer;
958valueBuffer = malloc(VALUE_SIZE);
959char * argP = bootArgs->CommandLine;
960valueBuffer[0] = '*';
961if (cnt > VALUE_SIZE)
962{
963cnt = VALUE_SIZE;
964}
965//cnt++;
966strlcpy(valueBuffer + 1, val, cnt+1);
967if (!copyArgument( kRootDeviceKey, valueBuffer, cnt, &argP, &ArgCntRemaining))
968{
969free(valueBuffer);
970printf("Error: boot arguments too long, unable to set root device !!");
971getc();
972return;
973}
974free(valueBuffer);
975goto out;
976}
977
978if (gBootVolume->fs_getuuid && gBootVolume->fs_getuuid (gBootVolume, bootInfo->uuidStr) == 0)
979{
980verbose("Setting boot-uuid to: %s\n", bootInfo->uuidStr);
981uuidSet = true;
982gRootDevice = bootInfo->uuidStr;
983return;
984}
985
986}
987}
988
989out:
990verbose("Setting %s to: %s\n", uuidSet ? kBootUUIDKey : "root device", (char* )val);
991gRootDevice = (char* )val;
992}
993
994static bool getOSVersion(char *str)
995{
996bool valid = false;
997config_file_t systemVersion;
998
999if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))
1000{
1001valid = true;
1002}
1003else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))
1004{
1005valid = true;
1006}
1007
1008if (valid)
1009{
1010const char *val;
1011int len;
1012
1013if (getValueForKey(kProductVersion, &val, &len, &systemVersion))
1014{
1015// getValueForKey uses const char for val
1016// so copy it and trim
1017*str = '\0';
1018strncat(str, val, MIN(len, 4));
1019}
1020else
1021valid = false;
1022}
1023
1024return valid;
1025}
1026
1027static bool find_file_with_ext(const char* dir, const char *ext, const char * name_compare, size_t ext_size)
1028{
1029char* name;
1030long flags;
1031long time;
1032struct dirstuff* moduleDir = opendir(dir);
1033while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)
1034{
1035int len = strlen(name);
1036
1037if (len >= ext_size)
1038{
1039if(strcmp(&name[len - ext_size], ext) == 0)
1040{
1041if (name_compare)
1042{
1043if (strcmp(name, name_compare) == 0)
1044{
1045DBG("found : %s\n", name);
1046return true;
1047}
1048}
1049else
1050{
1051DBG("found : %s\n", name);
1052return true;
1053}
1054}
1055#if DEBUG_BOOT
1056else
1057{
1058DBG("Ignoring %s\n", name);
1059}
1060#endif
1061}
1062#if DEBUG_BOOT
1063else
1064{
1065DBG("Ignoring %s\n", name);
1066}
1067#endif
1068}
1069
1070return false;
1071}
1072
1073// If a kext is found in /Extra/Extentions return true
1074// If a mkext is found in /Extra return true
1075// Otherwise return false
1076// 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,
1077// otherwise it will act like a -f each time a extra kext/kext is detected
1078static bool found_extra_kext(void)
1079{
1080#define EXTENSIONS "Extensions"
1081#define MKEXT_EXT ".mkext"
1082#define MKEXT_EXT_SIZE sizeof("mkext")
1083#define KEXT_EXT ".kext"
1084#define KEXT_EXT_SIZE sizeof("kext")
1085
1086long flags;
1087long exttime;
1088int ret = -1;
1089
1090ret = GetFileInfo("rd(0,0)/Extra/", EXTENSIONS, &flags, &exttime);
1091if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeFlat))
1092{
1093if (((flags & kFileTypeMask) == kFileTypeFlat))
1094{
1095if (find_file_with_ext("rd(0,0)/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))
1096{
1097return true;
1098}
1099}
1100else if (((flags & kFileTypeMask) == kFileTypeDirectory))
1101{
1102if (find_file_with_ext("rd(0,0)/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))
1103{
1104return true;
1105}
1106}
1107}
1108ret = GetFileInfo("/Extra/", EXTENSIONS, &flags, &exttime);
1109if (ret == 0)
1110{
1111if (((flags & kFileTypeMask) == kFileTypeFlat))
1112{
1113if (find_file_with_ext("/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))
1114{
1115return true;
1116}
1117}
1118else if (((flags & kFileTypeMask) == kFileTypeDirectory))
1119{
1120if (find_file_with_ext("/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))
1121{
1122return true;
1123}
1124}
1125}
1126ret = GetFileInfo("bt(0,0)/Extra/", EXTENSIONS, &flags, &exttime);
1127if (ret == 0)
1128{
1129if (((flags & kFileTypeMask) == kFileTypeFlat))
1130{
1131if (find_file_with_ext("bt(0,0)/Extra/", MKEXT_EXT, EXTENSIONS, MKEXT_EXT_SIZE))
1132{
1133return true;
1134}
1135}
1136else if (((flags & kFileTypeMask) == kFileTypeDirectory))
1137{
1138if (find_file_with_ext("bt(0,0)/Extra/Extensions/", KEXT_EXT, NULL, KEXT_EXT_SIZE))
1139{
1140return true;
1141}
1142}
1143}
1144DBG("NO Extra Mkext/Kext found\n");
1145
1146// nothing found
1147return false;
1148}
1149
1150#if 0
1151static char *FIXED_BOOTFILE_PATH(char * str)
1152{
1153char bootfile[128];
1154
1155bool bootFileWithDevice = false;
1156// Check if bootFile start with a device ex: bt(0,0)/Extra/mach_kernel
1157if (strncmp(str,"bt(",3) == 0 ||
1158strncmp(str,"hd(",3) == 0 ||
1159strncmp(str,"rd(",3) == 0)
1160{
1161bootFileWithDevice = true;
1162}
1163
1164// bootFile must start with a / if it not start with a device name
1165if (!bootFileWithDevice && (str)[0] != '/')
1166sprintf(bootFile, "/%s", str); // append a leading /
1167else
1168strlcpy(bootFile, bootInfo->bootFile, sizeof(bootInfo->bootFile)+1);
1169
1170return bootfile;
1171}
1172#endif
1173

Archive Download this file

Revision: 1654