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

Archive Download this file

Revision: 1931