Chameleon

Chameleon Svn Source Tree

Root/trunk/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 "ramdisk.h"
59#include "gui.h"
60#include "platform.h"
61#include "modules.h"
62
63long gBootMode; /* defaults to 0 == kBootModeNormal */
64bool gOverrideKernel;
65static char gBootKernelCacheFile[512];
66static char gCacheNameAdler[64 + 256];
67char *gPlatformName = gCacheNameAdler;
68char gRootDevice[512];
69char gMKextName[512];
70char gMacOSVersion[8];
71bool gEnableCDROMRescan;
72bool gScanSingleDrive;
73
74int bvCount = 0;
75//intmenucount = 0;
76int gDeviceCount = 0;
77
78BVRef bvr;
79BVRef menuBVR;
80BVRef bvChain;
81bool useGUI;
82
83//static void selectBiosDevice(void);
84static unsigned long Adler32(unsigned char *buffer, long length);
85static bool checkOSVersion(const char * version);
86static bool getOSVersion();
87
88static bool gUnloadPXEOnExit = false;
89
90/*
91 * How long to wait (in seconds) to load the
92 * kernel after displaying the "boot:" prompt.
93 */
94#define kBootErrorTimeout 5
95
96
97//==========================================================================
98// Zero the BSS.
99
100static void zeroBSS(void)
101{
102extern char _DATA__bss__begin, _DATA__bss__end;
103extern char _DATA__common__begin, _DATA__common__end;
104
105bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));
106bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));
107}
108
109//==========================================================================
110// Malloc error function
111
112static void malloc_error(char *addr, size_t size, const char *file, int line)
113{
114stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);
115}
116
117//==========================================================================
118//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
119//
120void initialize_runtime(void)
121{
122zeroBSS();
123malloc_init(0, 0, 0, malloc_error);
124}
125
126//==========================================================================
127// execKernel - Load the kernel image (mach-o) and jump to its entry point.
128
129static int ExecKernel(void *binary)
130{
131entry_t kernelEntry;
132int ret;
133
134bootArgs->kaddr = bootArgs->ksize = 0;
135execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL);
136
137ret = DecodeKernel(binary,
138 &kernelEntry,
139 (char **) &bootArgs->kaddr,
140 (int *)&bootArgs->ksize );
141
142if ( ret != 0 )
143return ret;
144
145// Reserve space for boot args
146reserveKernBootStruct();
147
148// Notify modules that the kernel has been decoded
149execute_hook("DecodedKernel", (void*)binary, NULL, NULL, NULL);
150
151 setupFakeEfi();
152
153// Load boot drivers from the specifed root path.
154 //if (!gHaveKernelCache)
155 LoadDrivers("/");
156
157 execute_hook("DriversLoaded", (void*)binary, NULL, NULL, NULL);
158
159clearActivityIndicator();
160
161if (gErrors) {
162printf("Errors encountered while starting up the computer.\n");
163printf("Pausing %d seconds...\n", kBootErrorTimeout);
164sleep(kBootErrorTimeout);
165}
166
167md0Ramdisk();
168
169verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");
170
171// Cleanup the PXE base code.
172
173if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
174if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
175{
176printf("nbpUnloadBaseCode error %d\n", (int) ret);
177sleep(2);
178}
179}
180
181bool dummyVal;
182if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->chameleonConfig) && dummyVal) {
183printf("(Wait) ");
184pause();
185}
186
187usb_loop();
188
189 if (checkOSVersion("10.7"))
190 {
191 execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);// Notify modules that the kernel is about to be started
192 }
193 else
194 {
195 execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgsPreLion, NULL, NULL);// Notify modules that the kernel is about to be started
196 }
197
198// If we were in text mode, switch to graphics mode.
199// This will draw the boot graphics unless we are in
200// verbose mode.
201
202if(gVerboseMode)
203 setVideoMode( GRAPHICS_MODE, 0 );
204else
205 drawBootGraphics();
206
207setupBooterLog();
208
209finalizeBootStruct();
210
211 if (checkOSVersion("10.7")) {
212
213// Masking out so that Lion doesn't doublefault
214outb(0x21, 0xff);/* Maskout all interrupts Pic1 */
215outb(0xa1, 0xff);/* Maskout all interrupts Pic2 */
216
217// Jump to kernel's entry point. There's no going back now.
218
219startprog( kernelEntry, bootArgs );
220}
221else {
222// Jump to kernel's entry point. There's no going back now.
223
224startprog( kernelEntry, bootArgsPreLion );
225}
226
227// Not reached
228return 0;
229}
230
231//==========================================================================
232// This is the entrypoint from real-mode which functions exactly as it did
233// before. Multiboot does its own runtime initialization, does some of its
234// own things, and then calls common_boot.
235void boot(int biosdev)
236{
237initialize_runtime();
238// Enable A20 gate before accessing memory above 1Mb.
239enableA20();
240common_boot(biosdev);
241}
242
243//==========================================================================
244// The 'main' function for the booter. Called by boot0 when booting
245// from a block device, or by the network booter.
246//
247// arguments:
248// biosdev - Value passed from boot1/NBP to specify the device
249// that the booter was loaded from.
250//
251// If biosdev is kBIOSDevNetwork, then this function will return if
252// booting was unsuccessful. This allows the PXE firmware to try the
253// next boot device on its list.
254void common_boot(int biosdev)
255{
256 int status;
257 char *bootFile;
258 unsigned long adler32;
259 bool quiet;
260 bool firstRun = true;
261 bool instantMenu;
262 bool rescanPrompt;
263 unsigned int allowBVFlags = kBVFlagSystemVolume | kBVFlagForeignBoot;
264 unsigned int denyBVFlags = kBVFlagEFISystem;
265
266 // Set reminder to unload the PXE base code. Neglect to unload
267 // the base code will result in a hang or kernel panic.
268 gUnloadPXEOnExit = true;
269
270 // Record the device that the booter was loaded from.
271 gBIOSDev = biosdev & kBIOSDevMask;
272
273 // Initialize boot info structure.
274 initKernBootStruct();
275
276initBooterLog();
277
278 // Setup VGA text mode.
279 // Not sure if it is safe to call setVideoMode() before the
280 // config table has been loaded. Call video_mode() instead.
281#if DEBUG
282 printf("before video_mode\n");
283#endif
284 video_mode( 2 ); // 80x25 mono text mode.
285#if DEBUG
286 printf("after video_mode\n");
287#endif
288
289 // Scan and record the system's hardware information.
290 scan_platform();
291
292 // First get info for boot volume.
293 scanBootVolumes(gBIOSDev, 0);
294 bvChain = getBVChainForBIOSDev(gBIOSDev);
295 setBootGlobals(bvChain);
296
297 // Load boot.plist config file
298 status = loadChameleonConfig(&bootInfo->chameleonConfig);
299
300 if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->chameleonConfig) && quiet) {
301 gBootMode |= kBootModeQuiet;
302 }
303
304 // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
305 if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->chameleonConfig) && instantMenu) {
306 firstRun = false;
307 }
308
309 // Loading preboot ramdisk if exists.
310 loadPrebootRAMDisk();
311
312 // Disable rescan option by default
313 gEnableCDROMRescan = false;
314
315 // Enable it with Rescan=y in system config
316 if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->chameleonConfig) && gEnableCDROMRescan) {
317 gEnableCDROMRescan = true;
318 }
319
320 // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
321 rescanPrompt = false;
322 if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->chameleonConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {
323 gEnableCDROMRescan = promptForRescanOption();
324 }
325
326 // Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
327 if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->chameleonConfig) && gScanSingleDrive) {
328 gScanSingleDrive = true;
329 }
330
331 // Create a list of partitions on device(s).
332 if (gScanSingleDrive) {
333 scanBootVolumes(gBIOSDev, &bvCount);
334 } else {
335 scanDisks(gBIOSDev, &bvCount);
336 }
337
338 // Create a separated bvr chain using the specified filters.
339 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
340
341 gBootVolume = selectBootVolume(bvChain);
342
343// Intialize module system
344init_module_system();
345
346#if DEBUG
347 printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
348 printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
349 getchar();
350#endif
351
352useGUI = true;
353// Override useGUI default
354getBoolForKey(kGUIKey, &useGUI, &bootInfo->chameleonConfig);
355if (useGUI && initGUI())
356{
357// initGUI() returned with an error, disabling GUI.
358useGUI = false;
359}
360
361 setBootGlobals(bvChain);
362
363 // Parse args, load and start kernel.
364 while (1) {
365 const char *val;
366 int len;
367 int trycache;
368 long flags, cachetime, kerneltime, exttime, sleeptime, time;
369 int ret = -1;
370 void *binary = (void *)kLoadAddr;
371 bool tryresume;
372 bool tryresumedefault;
373 bool forceresume;
374bool usecache = false;//true;
375
376 // additional variable for testing alternate kernel image locations on boot helper partitions.
377 char bootFileSpec[512];
378
379 // Initialize globals.
380
381 sysConfigValid = false;
382 gErrors = false;
383
384 status = getBootOptions(firstRun);
385 firstRun = false;
386 if (status == -1) continue;
387
388 status = processBootOptions();
389 // Status==1 means to chainboot
390 if ( status == 1 ) break;
391 // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL
392 if ( status == -1 )
393 {
394 // gBootVolume == NULL usually means the user hit escape.
395 if(gBootVolume == NULL)
396 {
397 freeFilteredBVChain(bvChain);
398
399 if (gEnableCDROMRescan)
400 rescanBIOSDevice(gBIOSDev);
401
402 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
403 setBootGlobals(bvChain);
404 setupDeviceList(&bootInfo->themeConfig);
405 }
406 continue;
407 }
408
409 // Other status (e.g. 0) means that we should proceed with boot.
410
411// Turn off any GUI elements
412if( bootArgs->Video.v_display == GRAPHICS_MODE )
413{
414gui.devicelist.draw = false;
415gui.bootprompt.draw = false;
416gui.menu.draw = false;
417gui.infobox.draw = false;
418gui.logo.draw = false;
419drawBackground();
420updateVRAM();
421}
422
423// Find out which version mac os we're booting.
424getOSVersion();
425
426if (platformCPUFeature(CPU_FEATURE_EM64T)) {
427archCpuType = CPU_TYPE_X86_64;
428} else {
429archCpuType = CPU_TYPE_I386;
430}
431if (getValueForKey(karch, &val, &len, &bootInfo->chameleonConfig)) {
432if (strncmp(val, "i386", 4) == 0) {
433archCpuType = CPU_TYPE_I386;
434}
435}
436
437 if (getValueForKey(kKernelArchKey, &val, &len, &bootInfo->chameleonConfig)) {
438if (strncmp(val, "i386", 4) == 0) {
439archCpuType = CPU_TYPE_I386;
440}
441}
442
443 //archCpuType = CPU_TYPE_I386;
444
445// Notify moduals that we are attempting to boot
446execute_hook("PreBoot", NULL, NULL, NULL, NULL);
447
448if (!getBoolForKey (kWake, &tryresume, &bootInfo->chameleonConfig)) {
449tryresume = true;
450tryresumedefault = true;
451} else {
452tryresumedefault = false;
453}
454
455if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->chameleonConfig)) {
456forceresume = false;
457}
458
459if (forceresume) {
460tryresume = true;
461tryresumedefault = false;
462}
463
464while (tryresume) {
465const char *tmp;
466BVRef bvr;
467if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->chameleonConfig))
468val="/private/var/vm/sleepimage";
469
470// Do this first to be sure that root volume is mounted
471ret = GetFileInfo(0, val, &flags, &sleeptime);
472
473if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
474break;
475
476// Can't check if it was hibernation Wake=y is required
477if (bvr->modTime == 0 && tryresumedefault)
478break;
479
480if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
481break;
482
483if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
484printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",bvr->modTime-sleeptime);
485break;
486}
487
488HibernateBoot((char *)val);
489break;
490}
491
492getBoolForKey(kUseKernelCache, &usecache, &bootInfo->chameleonConfig);
493if(usecache) {
494if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {
495 if(val[0] == '\\')
496 {
497 len--;
498 val++;
499 }
500 strlcpy(gBootKernelCacheFile, val, len+1);
501}
502else {
503//Lion
504if (checkOSVersion("10.7")) {
505sprintf(gBootKernelCacheFile, "%skernelcache", kDefaultCachePathSnow);
506}
507// Snow Leopard
508else if (checkOSVersion("10.6")) {
509sprintf(gBootKernelCacheFile, "kernelcache_%s", (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64");
510int lnam = sizeof(gBootKernelCacheFile) + 9; //with adler32
511//Slice - TODO ???
512// e.g. kernelcache_i386.E102928C.qSs0 = "unsaved" cache file.
513//
514// See kext_tools-180.2.1/kextcache_main.c:
515// "Source directory has changed since starting; "
516 // "not saving cache file %s."
517// or
518// "Source kernel has changed since starting; "
519 // "not saving cache file %s."
520
521char* name;
522long prev_time = 0;
523
524struct dirstuff* cacheDir = opendir(kDefaultCachePathSnow);
525
526while(readdir(cacheDir, (const char**)&name, &flags, &time) >= 0)
527{
528if(((flags & kFileTypeMask) != kFileTypeDirectory) && time > prev_time && strstr(name, gBootKernelCacheFile) && (name[lnam] != '.'))
529{
530sprintf(gBootKernelCacheFile, "%s%s", kDefaultCachePathSnow, name);
531prev_time = time;
532}
533}
534}
535else {
536// Reset cache name.
537bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
538
539sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->chameleonConfig);
540
541adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
542
543sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePathLeo, adler32);
544}
545}
546}
547
548 // Check for cache file.
549 trycache = (usecache &&
550((gBootMode & kBootModeSafe) == 0) &&
551 !gOverrideKernel &&
552 (gBootFileType == kBlockDeviceType) &&
553 (gMKextName[0] == '\0') &&
554 (gBootKernelCacheFile[0] != '\0'));
555
556verbose("Loading Darwin %s\n", gMacOSVersion);
557
558 if (trycache) do {
559 ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
560 if(ret != 0) kerneltime = 0;
561 else if ((flags & kFileTypeMask) != kFileTypeFlat) {
562 trycache = 0;
563 break;
564 }
565
566 ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
567 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
568 || (cachetime < kerneltime)) {
569 trycache = 0;
570 break;
571 }
572 ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
573 if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
574 && (cachetime < exttime)) {
575 trycache = 0;
576 break;
577 }
578 if (ret == 0 && kerneltime > exttime) {
579 exttime = kerneltime;
580 }
581 if (ret == 0 && cachetime != (exttime + 1)) {
582 trycache = 0;
583 break;
584 }
585 } while (0);
586
587 do {
588 if (trycache) {
589 bootFile = gBootKernelCacheFile;
590
591verbose("Loading kernel cache %s\n", bootFile);
592
593 if (checkOSVersion("10.7")) {
594 ret = LoadThinFatFile(bootFile, &binary);
595}
596else {
597 ret = LoadFile(bootFile);
598binary = (void *)kLoadAddr;
599}
600
601 if (ret >= 0)
602 break;
603
604verbose("Kernel cache did not load %s\n ", bootFile);
605 }
606
607 bootFile = bootInfo->bootFile;
608
609 // Try to load kernel image from alternate locations on boot helper partitions.
610 sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);
611 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
612 if (ret == -1)
613 {
614 sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);
615 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
616 if (ret == -1)
617 {
618 sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);
619 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
620 if (ret == -1)
621 {
622 // No alternate location found, using the original kernel image path.
623 strcpy(bootFileSpec, bootFile);
624 }
625 }
626 }
627
628 if (checkOSVersion("10.7"))
629 {
630 //Lion, dont load kernel if haz cache
631 if (!trycache) {
632 verbose("Loading kernel %s\n", bootFileSpec);
633 ret = LoadThinFatFile(bootFileSpec, &binary);
634 if (ret <= 0 && archCpuType == CPU_TYPE_X86_64) {
635 archCpuType = CPU_TYPE_I386;
636 ret = LoadThinFatFile(bootFileSpec, &binary);
637 }
638 }
639else ret = 1;
640 }
641else {
642 //Snow Leopard or older
643 verbose("Loading kernel %s\n", bootFileSpec);
644 ret = LoadThinFatFile(bootFileSpec, &binary);
645 if (ret <= 0 && archCpuType == CPU_TYPE_X86_64) {
646 archCpuType = CPU_TYPE_I386;
647 ret = LoadThinFatFile(bootFileSpec, &binary);
648 }
649 }
650 } while (0);
651
652 clearActivityIndicator();
653#if DEBUG
654 printf("Pausing...");
655 sleep(8);
656#endif
657
658 if (ret <= 0) {
659printf("Can't find %s\n", bootFile);
660
661sleep(1);
662
663 if (gBootFileType == kNetworkDeviceType) {
664 // Return control back to PXE. Don't unload PXE base code.
665 gUnloadPXEOnExit = false;
666 break;
667 }
668 } else {
669 /* Won't return if successful. */
670 ret = ExecKernel(binary);
671 }
672 }
673
674 // chainboot
675 if (status==1) {
676if (getVideoMode() == GRAPHICS_MODE) {// if we are already in graphics-mode,
677setVideoMode(VGA_TEXT_MODE, 0);// switch back to text mode
678}
679 }
680
681 if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {
682nbpUnloadBaseCode();
683 }
684}
685
686/*!
687 Selects a new BIOS device, taking care to update the global state appropriately.
688 */
689/*
690static void selectBiosDevice(void)
691{
692 struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
693 CacheReset();
694 diskFreeMap(oldMap);
695 oldMap = NULL;
696
697 int dev = selectAlternateBootDevice(gBIOSDev);
698
699 BVRef bvchain = scanBootVolumes(dev, 0);
700 BVRef bootVol = selectBootVolume(bvchain);
701 gBootVolume = bootVol;
702 setRootVolume(bootVol);
703 gBIOSDev = dev;
704}
705*/
706
707bool checkOSVersion(const char * version)
708{
709return ((gMacOSVersion[0] == version[0]) && (gMacOSVersion[1] == version[1]) && (gMacOSVersion[2] == version[2]) && (gMacOSVersion[3] == version[3]));
710}
711
712bool getOSVersion()
713{
714bool valid = false;
715config_file_t systemVersion;
716const char *val;
717int len;
718
719if (!loadConfigFile("System/Library/CoreServices/SystemVersion.plist", &systemVersion))
720{
721valid = true;
722}
723else if (!loadConfigFile("System/Library/CoreServices/ServerVersion.plist", &systemVersion))
724{
725valid = true;
726}
727
728if (valid)
729{
730if (getValueForKey(kProductVersion, &val, &len, &systemVersion))
731{
732// getValueForKey uses const char for val
733// so copy it and trim
734*gMacOSVersion = '\0';
735strncat(gMacOSVersion, val, MIN(len, 4));
736}
737else
738valid = false;
739}
740
741return valid;
742}
743
744#define BASE 65521L /* largest prime smaller than 65536 */
745#define NMAX 5000
746// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
747
748#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
749#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
750#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
751#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
752#define DO16(buf) DO8(buf,0); DO8(buf,8);
753
754unsigned long Adler32(unsigned char *buf, long len)
755{
756 unsigned long s1 = 1; // adler & 0xffff;
757 unsigned long s2 = 0; // (adler >> 16) & 0xffff;
758 unsigned long result;
759 int k;
760
761 while (len > 0) {
762 k = len < NMAX ? len : NMAX;
763 len -= k;
764 while (k >= 16) {
765 DO16(buf);
766 buf += 16;
767 k -= 16;
768 }
769 if (k != 0) do {
770 s1 += *buf++;
771 s2 += s1;
772 } while (--k);
773 s1 %= BASE;
774 s2 %= BASE;
775 }
776 result = (s2 << 16) | s1;
777 return OSSwapHostToBigInt32(result);
778}
779

Archive Download this file

Revision: 1152