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

Archive Download this file

Revision: 708