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

Archive Download this file

Revision: 2117