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

Archive Download this file

Revision: 715