Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 711