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

Archive Download this file

Revision: 1159