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

Archive Download this file

Revision: 1735