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

Archive Download this file

Revision: 693