Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/boot2/boot.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Mach Operating System
26 * Copyright (c) 1990 Carnegie-Mellon University
27 * Copyright (c) 1989 Carnegie-Mellon University
28 * All rights reserved. The CMU software License Agreement specifies
29 * the terms and conditions for use and redistribution.
30 */
31
32/*
33 * INTEL CORPORATION PROPRIETARY INFORMATION
34 *
35 * This software is supplied under the terms of a license agreement or
36 * nondisclosure agreement with Intel Corporation and may not be copied
37 * nor disclosed except in accordance with the terms of that agreement.
38 *
39 * Copyright 1988, 1989 by Intel Corporation
40 */
41
42/*
43 * Copyright 1993 NeXT Computer, Inc.
44 * All rights reserved.
45 */
46
47/*
48 * Completely reworked by Sam Streeper (sam_s@NeXT.com)
49 * Reworked again by Curtis Galloway (galloway@NeXT.com)
50 */
51
52
53#include "boot.h"
54#include "bootstruct.h"
55#include "fake_efi.h"
56#include "sl.h"
57#include "libsa.h"
58#include "platform.h"
59#include "graphics.h"
60#ifndef OPTION_ROM
61#include "appleboot.h"
62#endif
63#include "modules.h"
64#include "xml.h"
65
66#ifndef DEBUG_BOOT
67#define DEBUG_BOOT 0
68#endif
69
70#if DEBUG_BOOT
71#define DBG(x...)printf(x)
72#else
73#define DBG(x...)
74#endif
75
76
77typedef struct platform_info {
78char platformName[PLATFORM_NAME_LEN];
79char rootPath[ROOT_PATH_LEN];
80} PlatformInfo;
81
82
83char *gboardproduct = NULL;
84char *gPlatformName = NULL;
85//char gRootPath[256];
86long gBootMode; /* defaults to 0 == kBootModeNormal */
87bool gOverrideKernel;
88char gBootKernelCacheFile[512];
89char gMKextName[512];
90char gMacOSVersion[8];
91char *gRootDevice = NULL;
92bool uuidSet = false;
93#ifndef OPTION_ROM
94bool gEnableCDROMRescan;
95bool gScanSingleDrive;
96#endif
97
98int bvCount = 0;
99//intmenucount = 0;
100int gDeviceCount = 0;
101
102BVRef bvr;
103BVRef menuBVR;
104BVRef bvChain;
105
106//static void selectBiosDevice(void);
107
108static bool getOSVersion(char *str);
109static void getRootDevice();
110
111#ifdef NBP_SUPPORT
112static bool gUnloadPXEOnExit = false;
113#endif
114
115/*
116 * How long to wait (in seconds) to load the
117 * kernel after displaying the "boot:" prompt.
118 */
119#define kBootErrorTimeout 5
120
121/*
122 * Default path to kernel cache file
123 */
124#define kDefaultCachePath "/System/Library/Caches/com.apple.kext.caches/Startup/kernelcache"
125
126//==========================================================================
127// Zero the BSS.
128
129static void zeroBSS(void)
130{
131extern char _DATA__bss__begin, _DATA__bss__end;
132extern char _DATA__common__begin, _DATA__common__end;
133
134bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));
135bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));
136}
137
138//==========================================================================
139// Malloc error function
140
141#ifdef SAFE_MALLOC
142static inline void malloc_error(char *addr, size_t size, const char *file, int line)
143{
144 stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);
145}
146#else
147static inline void malloc_error(char *addr, size_t size)
148{
149 printf("\nMemory allocation error (0x%x, 0x%x)\n", (unsigned)addr, (unsigned)size);
150 asm volatile ("hlt");
151}
152#endif
153
154//==========================================================================
155//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
156//
157void initialize_runtime(void)
158{
159zeroBSS();
160malloc_init(0, 0, 0, malloc_error);
161}
162
163//==========================================================================
164// execKernel - Load the kernel image (mach-o) and jump to its entry point.
165
166static int ExecKernel(void *binary)
167{
168 entry_t kernelEntry;
169 int ret;
170
171 bootArgs->kaddr = bootArgs->ksize = 0;
172
173if(gMacOSVersion[3] <= '6')
174{
175bootArgs->Version = kBootArgsVersion1;
176bootArgs->Revision = gMacOSVersion[3];
177}
178else
179{
180#if kBootArgsVersion > 1
181
182bootArgs->Version = kBootArgsVersion;
183bootArgs->Revision = kBootArgsRevision;
184#else
185bootArgs->Version = 2;
186 bootArgs->Revision = 0;
187#endif
188}
189
190execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL, NULL, NULL);
191
192 ret = DecodeKernel(binary,
193 &kernelEntry,
194 (char **) &bootArgs->kaddr,
195 (int *)&bootArgs->ksize );
196
197 if ( ret != 0 )
198 return ret;
199
200 // 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.)
201if(gMacOSVersion[3] == '7')
202 reserveKernBootStruct();
203
204 // Load boot drivers from the specifed root path.
205
206 if (!gHaveKernelCache) {
207LoadDrivers("/");
208 }
209
210#if TEXT_SPINNER
211clearActivityIndicator();
212#endif
213
214 if (gErrors) {
215 printf("Errors encountered while starting up the computer.\n");
216 printf("Pausing %d seconds...\n", kBootErrorTimeout);
217 sleep(kBootErrorTimeout);
218 }
219
220execute_hook("md0Ramdisk", NULL, NULL, NULL, NULL, NULL, NULL);
221
222 setupFakeEfi();
223
224 verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");
225#ifdef NBP_SUPPORT
226 // Cleanup the PXE base code.
227
228 if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
229if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
230 {
231 printf("nbpUnloadBaseCode error %d\n", (int) ret);
232 sleep(2);
233 }
234 }
235#endif
236 bool wait = false;
237const char *strval = 0;
238int dummysize /*= 0*/;
239
240getBoolForKey(kWaitForKeypressKey, &wait, &bootInfo->bootConfig);
241
242if (getValueForBootKey(bootArgs->CommandLine, "-wait", &strval, &dummysize))
243{
244wait = true;
245
246if (strval && ((strcmp(strval, "no") == 0) || (strcmp(strval, "No") == 0))) {
247wait = false;
248}
249}
250
251if (wait == true) {
252pause();
253}
254
255if ((execute_hook("GUI_ExecKernel", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)) // (bootArgs->Video.v_display == VGA_TEXT_MODE)
256{
257#if UNUSED
258setVideoMode( GRAPHICS_MODE, 0 );
259#else
260setVideoMode( GRAPHICS_MODE );
261#endif
262
263#ifndef OPTION_ROM
264
265if(!gVerboseMode)
266{
267drawColorRectangle(0, 0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 0x01);
268
269uint8_t *appleBootPict;
270uint16_t bootImageWidth = kAppleBootWidth;
271uint16_t bootImageHeight = kAppleBootHeight;
272uint8_t *bootImageData = NULL;
273uint16_t x, y;
274
275unsigned long screen_params[4] = {DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 32, 0};// here we store the used screen resolution
276// Prepare the data for the default Apple boot image.
277appleBootPict = (uint8_t *) decodeRLE(gAppleBootPictRLE, kAppleBootRLEBlocks, bootImageWidth * bootImageHeight);
278if (appleBootPict) {
279convertImage(bootImageWidth, bootImageHeight, appleBootPict, &bootImageData);
280if (bootImageData) {
281x = (screen_params[0] - MIN(kAppleBootWidth, screen_params[0])) / 2;
282y = (screen_params[1] - MIN(kAppleBootHeight, screen_params[1])) / 2;
283drawDataRectangle(x, y, kAppleBootWidth, kAppleBootHeight, bootImageData);
284free(bootImageData);
285}
286free(appleBootPict);
287}
288
289}
290#endif
291}
292
293 finalizeEFIConfigTable();
294
295setupBooterLog();
296
297 finalizeBootStruct();
298
299
300if (gMacOSVersion[3] <= '6')
301reserveKernLegacyBootStruct();
302
303
304//uint8_t Pic1,Pic2;
305
306//Pic1 = inb(0x21); /* Save all interrupts Pic1 */
307//Pic2 = inb(0xa1); /* Save all interrupts Pic2 */
308
309
310
311execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL, NULL, NULL);// Notify modules that the kernel is about to be started
312//#if UNUSED
313turnOffFloppy();
314//#endif
315//#if BETA
316#include "smp.h"
317#include "apic.h"
318IMPS_LAPIC_WRITE(LAPIC_LVT1, LAPIC_ICR_DM_NMI);
319//#endif
320
321if (gMacOSVersion[3] <= '6') {
322
323//outb(0x21, Pic1); /* Restore all interrupts Pic1 */
324//outb(0xa1, Pic2); /* Restore all interrupts Pic2 */
325
326// Jump to kernel's entry point. There's no going back now. XXX LEGACY OS XXX
327startprog( kernelEntry, bootArgsLegacy );
328}
329
330outb(0x21, 0xff); /* Maskout all interrupts Pic1 */
331outb(0xa1, 0xff); /* Maskout all interrupts Pic2 */
332
333// Jump to kernel's entry point. There's no going back now. XXX LION XXX
334 startprog( kernelEntry, bootArgs );
335
336 // Not reached
337
338 return 0;
339}
340
341//==========================================================================
342// This is the entrypoint from real-mode which functions exactly as it did
343// before. Multiboot does its own runtime initialization, does some of its
344// own things, and then calls common_boot.
345void boot(int biosdev)
346{
347initialize_runtime();
348// Enable A20 gate before accessing memory above 1Mb.
349enableA20();
350common_boot(biosdev);
351}
352
353//==========================================================================
354// The 'main' function for the booter. Called by boot0 when booting
355// from a block device, or by the network booter.
356//
357// arguments:
358// biosdev - Value passed from boot1/NBP to specify the device
359// that the booter was loaded from.
360//
361// If biosdev is kBIOSDevNetwork, then this function will return if
362// booting was unsuccessful. This allows the PXE firmware to try the
363// next boot device on its list.
364void common_boot(int biosdev)
365{
366 int status;
367 char *bootFile;
368 bool quiet;
369 bool firstRun = true;
370 bool instantMenu;
371#ifndef OPTION_ROM
372 bool rescanPrompt;
373#endif
374 unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;
375 unsigned int denyBVFlags = kBVFlagEFISystem;
376
377#ifdef NBP_SUPPORT
378 // Set reminder to unload the PXE base code. Neglect to unload
379 // the base code will result in a hang or kernel panic.
380 gUnloadPXEOnExit = true;
381#endif
382 // Record the device that the booter was loaded from.
383 gBIOSDev = biosdev & kBIOSDevMask;
384
385
386
387 // Setup VGA text mode.
388 // Not sure if it is safe to call setVideoMode() before the
389 // config table has been loaded. Call video_mode() instead.
390#if DEBUG
391 printf("before video_mode\n");
392#endif
393 video_mode( 2 ); // 80x25 mono text mode.
394#if DEBUG
395 printf("after video_mode\n");
396#endif
397#if !TEXT_SPINNER
398printf("Starting Chameleon ...\n");
399#endif
400
401initBooterLog();
402
403// Initialize boot info structure.
404 initKernBootStruct();
405
406
407 // Scan and record the system's hardware information.
408 scan_platform();
409
410 // First get info for boot volume.
411 scanBootVolumes(gBIOSDev, 0);
412 bvChain = getBVChainForBIOSDev(gBIOSDev);
413 setBootGlobals(bvChain);
414
415 // Load boot.plist config file
416 status = loadSystemConfig(&bootInfo->bootConfig);
417
418Platform->CPU.isServer = false;
419 getBoolForKey(kIsServer, &Platform->CPU.isServer, &bootInfo->bootConfig); // set this as soon as possible
420
421 if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {
422 gBootMode |= kBootModeQuiet;
423 }
424
425 // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
426 if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {
427 firstRun = false;
428 }
429
430#ifndef OPTION_ROM
431// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
432 if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive) {
433 gScanSingleDrive = true;
434 }
435
436// Create a list of partitions on device(s).
437 if (gScanSingleDrive)
438{
439scanBootVolumes(gBIOSDev, &bvCount);
440 }
441else
442#endif
443{
444#if UNUSED
445 scanDisks(gBIOSDev, &bvCount);
446#else
447 scanDisks();
448#endif
449 }
450
451// Create a separated bvr chain using the specified filters.
452 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
453gBootVolume = selectBootVolume(bvChain);
454
455// Intialize module system
456EFI_STATUS sysinit = init_module_system();
457if((sysinit == EFI_SUCCESS) || (sysinit == EFI_ALREADY_STARTED)/*should never happen*/ )
458{
459load_all_modules();
460}
461
462 // Loading preboot ramdisk if exists.
463execute_hook("loadPrebootRAMDisk", NULL, NULL, NULL, NULL, NULL, NULL);
464
465#ifndef OPTION_ROM
466
467 // Disable rescan option by default
468 gEnableCDROMRescan = false;
469
470 // Enable it with Rescan=y in system config
471 if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan) {
472 gEnableCDROMRescan = true;
473 }
474
475 // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
476 rescanPrompt = false;
477 if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {
478 gEnableCDROMRescan = promptForRescanOption();
479 }
480#endif
481
482#if DEBUG
483 printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
484 printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
485 getc();
486#endif
487
488 setBootGlobals(bvChain);
489
490// Display the GUI
491execute_hook("GUI_Display", NULL, NULL, NULL, NULL, NULL, NULL);
492
493 // Parse args, load and start kernel.
494 while (1) {
495 const char *val;
496 int len;
497 int trycache = 0;
498 long flags, cachetime, kerneltime, exttime;
499#ifdef BOOT_HELPER_SUPPORT
500long time;
501#endif
502 int ret = -1;
503 void *binary = (void *)kLoadAddr;
504
505 // additional variable for testing alternate kernel image locations on boot helper partitions.
506 char bootFileSpec[512];
507
508 // Initialize globals.
509
510 sysConfigValid = false;
511 gErrors = false;
512
513 status = getBootOptions(firstRun);
514 firstRun = false;
515 if (status == -1) continue;
516
517 status = processBootOptions();
518 // Status==1 means to chainboot
519 if ( status == 1 ) break;
520 // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL
521 if ( status == -1 )
522 {
523// gBootVolume == NULL usually means the user hit escape.
524if(gBootVolume == NULL)
525{
526freeFilteredBVChain(bvChain);
527#ifndef OPTION_ROM
528if (gEnableCDROMRescan)
529rescanBIOSDevice(gBIOSDev);
530#endif
531
532bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
533setBootGlobals(bvChain);
534}
535continue;
536 }
537
538 // Other status (e.g. 0) means that we should proceed with boot.
539execute_hook("GUI_PreBoot", NULL, NULL, NULL, NULL, NULL, NULL);
540
541// Find out which version mac os we're booting.
542getOSVersion(gMacOSVersion);
543
544//if (platformCPUFeature(CPU_FEATURE_EM64T)) {
545 if (cpu_mode_is64bit()) {
546archCpuType = CPU_TYPE_X86_64;
547} else {
548archCpuType = CPU_TYPE_I386;
549}
550if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig)) {
551if (strncmp(val, "i386", 4) == 0) {
552archCpuType = CPU_TYPE_I386;
553}
554}
555
556getRootDevice();
557
558// Notify to all modules that we are attempting to boot
559execute_hook("PreBoot", NULL, NULL, NULL, NULL, NULL, NULL);
560
561if (execute_hook("getProductNamePatched", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)
562readSMBIOS(thePlatformName); // read smbios Platform Name
563
564
565if (!getValueForBootKey(bootArgs->CommandLine, kIgnorePrelinkKern, &val, &len)) {
566
567if(gMacOSVersion[3] == '7'){
568sprintf(gBootKernelCacheFile, "%s", kDefaultCachePath);
569}
570else if(gMacOSVersion[3] <= '6')
571{
572
573PlatformInfo *platformInfo = malloc(sizeof(PlatformInfo));
574if (platformInfo) {
575
576bzero(platformInfo, sizeof(PlatformInfo));
577
578if (gPlatformName)
579strlcpy(platformInfo->platformName,gPlatformName, sizeof(platformInfo->platformName)+1);
580
581if (gRootDevice) {
582char *rootPath_p = platformInfo->rootPath;
583 int len = strlen(gRootDevice) + 1;
584 if ((unsigned)len > sizeof(platformInfo->rootPath)) {
585 len = sizeof(platformInfo->rootPath);
586 }
587memcpy(rootPath_p, gRootDevice,len);
588
589rootPath_p += len;
590
591 len = strlen(bootInfo->bootFile);
592
593 if ((unsigned)(rootPath_p - platformInfo->rootPath + len) >=
594 sizeof(platformInfo->rootPath)) {
595
596 len = sizeof(platformInfo->rootPath) -
597 (rootPath_p - platformInfo->rootPath);
598 }
599memcpy(rootPath_p, bootInfo->bootFile, len);
600
601}
602
603if (!platformInfo->platformName[0] || !platformInfo->rootPath[0]) {
604platformInfo->platformName[0] = platformInfo->rootPath[0] = 0;
605}
606//memcpy(gRootPath,platformInfo->rootPath, sizeof(platformInfo->rootPath));
607
608
609bootInfo->adler32 = OSSwapHostToBigInt32(local_adler32((unsigned char *)platformInfo, sizeof(*platformInfo)));
610
611free(platformInfo);
612}
613
614DBG("Adler32: %08lX\n",bootInfo->adler32);
615
616if (gMacOSVersion[3] < '6') {
617sprintf(gBootKernelCacheFile, "%s.%08lX", "/System/Library/Caches/com.apple.kernelcaches/kernelcache",bootInfo->adler32);
618ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
619if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
620bootInfo->adler32 = 0;
621sprintf(gBootKernelCacheFile, "%s", "/System/Library/Caches/com.apple.kernelcaches/kernelcache");
622}
623} else
624sprintf(gBootKernelCacheFile, "%s_%s.%08lX", kDefaultCachePath, (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64", bootInfo->adler32); //Snow Leopard
625
626
627}
628
629}
630
631// Check for cache file.
632trycache = (((gBootMode & kBootModeSafe) == 0) &&
633!gOverrideKernel &&
634(gBootFileType == kBlockDeviceType) &&
635(gMKextName[0] == '\0') &&
636(gBootKernelCacheFile[0] != '\0'));
637
638verbose("Loading Darwin %s\n", gMacOSVersion);
639
640 if (trycache ) do {
641
642 // if we haven't found the kernel yet, don't use the cache
643 ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
644 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
645 trycache = 0;
646bootInfo->adler32 = 0;
647 break;
648 }
649 ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
650 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
651 || (cachetime < kerneltime)) {
652trycache = 0;
653bootInfo->adler32 = 0;
654break;
655 }
656 ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
657 if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
658 && (cachetime < exttime)) {
659 trycache = 0;
660bootInfo->adler32 = 0;
661 break;
662 }
663 if (kerneltime > exttime) {
664 exttime = kerneltime;
665 }
666 if (cachetime != (exttime + 1)) {
667 trycache = 0;
668bootInfo->adler32 = 0;
669 break;
670 }
671 } while (0);
672
673 do {
674 if (trycache) {
675 bootFile = gBootKernelCacheFile;
676 verbose("Loading kernel cache %s\n", bootFile);
677if (gMacOSVersion[3] == '7') {
678ret = LoadThinFatFile(bootFile, &binary);
679if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
680{
681archCpuType = CPU_TYPE_I386;
682ret = LoadThinFatFile(bootFile, &binary);
683}
684} else {
685ret = LoadFile(bootFile);
686binary = (void *)kLoadAddr;
687}
688
689 if (ret >= 0) {
690 break;
691 }
692
693 }
694bootInfo->adler32 = 0;
695 bootFile = bootInfo->bootFile;
696#ifdef BOOT_HELPER_SUPPORT
697
698 // Try to load kernel image from alternate locations on boot helper partitions.
699 sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);
700 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
701 if (ret == -1)
702 {
703sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);
704ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
705if (ret == -1)
706{
707sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);
708ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
709if (ret == -1)
710{
711// Not found any alternate locations, using the original kernel image path.
712strcpy(bootFileSpec, bootFile);
713}
714}
715 }
716#else
717strcpy(bootFileSpec, bootFile);
718#endif
719
720 verbose("Loading kernel %s\n", bootFileSpec);
721 ret = LoadThinFatFile(bootFileSpec, &binary);
722 if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
723 {
724archCpuType = CPU_TYPE_I386;
725ret = LoadThinFatFile(bootFileSpec, &binary);
726 }
727
728 } while (0);
729#if TEXT_SPINNER
730 clearActivityIndicator();
731#endif
732
733#if DEBUG
734 printf("Pausing...");
735 sleep(8);
736#endif
737
738 if (ret <= 0) {
739printf("Can't find %s\n", bootFile);
740
741sleep(1);
742#ifdef NBP_SUPPORT
743 if (gBootFileType == kNetworkDeviceType) {
744 // Return control back to PXE. Don't unload PXE base code.
745 gUnloadPXEOnExit = false;
746 break;
747 }
748#endif
749 } else {
750 /* Won't return if successful. */
751 ret = ExecKernel(binary);
752 }
753 }
754
755 // chainboot
756 if (status==1) {
757if (getVideoMode() == GRAPHICS_MODE) {// if we are already in graphics-mode,
758#if UNUSED
759setVideoMode(VGA_TEXT_MODE, 0);// switch back to text mode
760#else
761setVideoMode(VGA_TEXT_MODE);// switch back to text mode
762#endif
763}
764 }
765#ifdef NBP_SUPPORT
766 if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {
767nbpUnloadBaseCode();
768 }
769#endif
770}
771
772// Maximum config table value size
773#define VALUE_SIZE 2048
774static void getRootDevice()
775{
776const char *val = 0;
777 int cnt = 0;
778
779if (getValueForKey(kBootUUIDKey, &val, &cnt, &bootInfo->bootConfig)){
780uuidSet = true;
781
782 } else {
783if (getValueForBootKey(bootArgs->CommandLine, kRootDeviceKey, &val, &cnt)) {
784if (*val == '*' && *(val + 1) != '/' && *(val + 1) != 'u') {
785val += 1; //skip the *
786uuidSet = true;
787
788} else if (*val == '*' && *(val + 1) == 'u') {
789
790if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig))
791uuidSet = true;
792
793}
794} else {
795#ifdef BOOT_HELPER_SUPPORT
796//
797// Try an alternate method for getting the root UUID on boot helper partitions.
798//
799if (gBootVolume->flags & kBVFlagBooter)
800{
801if((loadHelperConfig(&bootInfo->helperConfig) == 0)
802 && getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig) )
803{
804getValueForKey(kHelperRootUUIDKey, &val, &cnt, &bootInfo->helperConfig);
805uuidSet = true;
806goto out;
807}
808}
809#endif
810
811if ( getValueForKey( kBootDeviceKey, &val, &cnt, &bootInfo->bootConfig)) {
812extern int ArgCntRemaining;
813uuidSet = false;
814char * valueBuffer;
815valueBuffer = malloc(VALUE_SIZE);
816char * argP = bootArgs->CommandLine;
817valueBuffer[0] = '*';
818cnt++;
819strlcpy(valueBuffer + 1, val, cnt);
820if (!copyArgument( kRootDeviceKey, valueBuffer, cnt, &argP, &ArgCntRemaining))
821{
822free(valueBuffer);
823printf("Error: boot arguments too long, unable to set root device !!");
824getc();
825return;
826}
827free(valueBuffer);
828goto out;
829}
830
831if (gBootVolume->fs_getuuid && gBootVolume->fs_getuuid (gBootVolume, bootInfo->uuidStr) == 0) {
832verbose("Setting boot-uuid to: %s\n", bootInfo->uuidStr);
833uuidSet = true;
834gRootDevice = bootInfo->uuidStr;
835return;
836}
837
838}
839}
840
841out:
842verbose("Setting %s to: %s\n", uuidSet ? kBootUUIDKey : "root device", (char* )val);
843gRootDevice = (char* )val;
844}
845
846static bool getOSVersion(char *str)
847{
848bool valid = false;
849config_file_t systemVersion;
850const char *val;
851int len;
852
853if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))
854{
855valid = true;
856}
857else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))
858{
859valid = true;
860}
861
862if (valid)
863{
864if (getValueForKey(kProductVersion, &val, &len, &systemVersion))
865{
866// getValueForKey uses const char for val
867// so copy it and trim
868*str = '\0';
869strncat(str, val, MIN(len, 4));
870}
871else
872valid = false;
873}
874
875return valid;
876}
877
878unsigned long
879local_adler32( unsigned char * buffer, long length )
880{
881 long cnt;
882 unsigned long result, lowHalf, highHalf;
883
884 lowHalf = 1;
885 highHalf = 0;
886
887for ( cnt = 0; cnt < length; cnt++ )
888 {
889 if ((cnt % 5000) == 0)
890 {
891 lowHalf %= 65521L;
892 highHalf %= 65521L;
893 }
894
895 lowHalf += buffer[cnt];
896 highHalf += lowHalf;
897 }
898
899lowHalf %= 65521L;
900highHalf %= 65521L;
901
902result = (highHalf << 16) | lowHalf;
903
904return result;
905}
906

Archive Download this file

Revision: 1119