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;
78#ifndef OPTION_ROM
79bool gEnableCDROMRescan;
80bool gScanSingleDrive;
81#endif
82config_file_t systemVersion;// system.plist of booting partition
83
84
85int bvCount = 0;
86//intmenucount = 0;
87int gDeviceCount = 0;
88
89BVRef bvr;
90BVRef menuBVR;
91BVRef bvChain;
92
93//static void selectBiosDevice(void);
94static unsigned long Adler32(unsigned char *buffer, long length);
95static bool getOSVersion(char *str);
96
97#ifndef OPTION_ROM
98static bool gUnloadPXEOnExit = false;
99#endif
100
101/*
102 * How long to wait (in seconds) to load the
103 * kernel after displaying the "boot:" prompt.
104 */
105#define kBootErrorTimeout 5
106
107/*
108 * Default path to kernel cache file
109 */
110 //Slice - first one for Leopard
111#define kDefaultCachePath "/System/Library/Caches/com.apple.kernelcaches/kernelcache"
112#define kDefaultCachePathSnow "/System/Library/Caches/com.apple.kext.caches/Startup/kernelcache"
113#define kDefaultCachePathTiger "/System/Library/Extensions.kextcache"
114//file://localhost/System/Library/Extensions.kextcache
115
116
117
118//==========================================================================
119// Zero the BSS.
120
121static void zeroBSS(void)
122{
123extern char _DATA__bss__begin, _DATA__bss__end;
124extern char _DATA__common__begin, _DATA__common__end;
125
126bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));
127bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));
128}
129
130//==========================================================================
131// Malloc error function
132
133static void malloc_error(char *addr, size_t size, const char *file, int line)
134{
135stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);
136}
137
138//==========================================================================
139//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
140//
141void initialize_runtime(void)
142{
143zeroBSS();
144malloc_init(0, 0, 0, malloc_error);
145}
146
147//==========================================================================
148// execKernel - Load the kernel image (mach-o) and jump to its entry point.
149
150int ExecKernel(void *binary)
151{
152 entry_t kernelEntry;
153 int ret;
154
155 bootArgs->kaddr = bootArgs->ksize = 0;
156execute_hook("ExecKernel", (void*)binary, NULL, NULL, NULL);
157
158 ret = DecodeKernel(binary,
159 &kernelEntry,
160 (char **) &bootArgs->kaddr,
161 (int *)&bootArgs->ksize );
162
163 if ( ret != 0 )
164 return ret;
165 // Reserve space for boot args
166 reserveKernBootStruct();
167
168 // Load boot drivers from the specifed root path.
169execute_hook("DecodedKernel", (void*)binary, NULL, NULL, NULL);
170
171
172setupFakeEfi();
173
174 if (!gHaveKernelCache) {
175LoadDrivers("/");
176 }
177
178 clearActivityIndicator();
179
180 if (gErrors) {
181 msglog("Errors encountered while starting up the computer.\n");
182 // printf("Pausing %d seconds...\n", kBootErrorTimeout);
183 // sleep(kBootErrorTimeout);
184 }
185
186md0Ramdisk();
187
188 verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "i386" : "x86_64");
189#ifndef OPTION_ROM
190 // Cleanup the PXE base code.
191
192 if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
193if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
194 {
195 printf("nbpUnloadBaseCode error %d\n", (int) ret);
196 sleep(2);
197 }
198 }
199#endif
200 bool dummyVal;
201if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal)
202{
203printf("Press any key to continue...");
204getc();
205}
206
207if (bootArgs->Video.v_display == VGA_TEXT_MODE)
208{
209setVideoMode( GRAPHICS_MODE, 0 );
210// Draw gray screen. NOTE: no boot image, that's in the gui module
211#ifndef OPTION_ROM
212if(!gVerboseMode) drawColorRectangle(0, 0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 0x01);
213#endif
214}
215
216
217setupBooterLog();
218
219 finalizeBootStruct();
220
221execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);// Notify modules that the kernel is about to be started
222
223// Jump to kernel's entry point. There's no going back now.
224 startprog( kernelEntry, bootArgs );
225
226 // Not reached
227
228 return 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;
257inttrycache;
258 char *bootFile;
259 unsigned long adler32;
260 bool quiet;
261 bool firstRun = true;
262 bool instantMenu;
263#ifndef OPTION_ROM
264 bool rescanPrompt;
265#endif
266 unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;
267 unsigned int denyBVFlags = kBVFlagEFISystem;
268
269#ifndef OPTION_ROM
270 // Set reminder to unload the PXE base code. Neglect to unload
271 // the base code will result in a hang or kernel panic.
272 gUnloadPXEOnExit = true;
273#endif
274
275 // Record the device that the booter was loaded from.
276 gBIOSDev = biosdev & kBIOSDevMask; // 0xff
277
278 // Initialize boot info structure.
279 initKernBootStruct();
280
281initBooterLog();
282
283 // Setup VGA text mode.
284 // Not sure if it is safe to call setVideoMode() before the
285 // config table has been loaded. Call video_mode() instead.
286#if DEBUG
287 printf("before video_mode\n");
288#endif
289 video_mode( 2 ); // 80x25 mono text mode.
290#if DEBUG
291 printf("after video_mode\n");
292#endif
293
294 // Scan and record the system's hardware information.
295 scan_platform();
296
297 // First get info for boot volume.
298 scanBootVolumes(gBIOSDev, 0);
299 bvChain = getBVChainForBIOSDev(gBIOSDev);
300
301#if 0
302 BVRef next; /* list linkage pointer */
303 int biosdev; /* BIOS device number */
304 int type; /* device type (floppy, hd, network) */
305 unsigned int flags; /* attribute flags */
306 BVGetDescription description; /* BVGetDescription function */
307 int part_no; /* partition number (1 based) */
308#endif
309#if DEBUG
310 printf("get bvChain dev=%02x type=%02x part_no=%d\n", bvChain->biosdev, bvChain->type, bvChain->part_no); //dev=0x80 - flash-stick
311#endif
312
313 setBootGlobals(bvChain);
314
315 // Load boot.plist config file
316 status = loadSystemConfig(&bootInfo->bootConfig);
317
318 if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {
319 gBootMode |= kBootModeQuiet;
320 }
321
322 // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
323 if (getBoolForKey(kInsantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {
324 firstRun = false;
325 }
326
327#ifndef OPTION_ROM
328// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
329 if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive) {
330 gScanSingleDrive = true;
331 }
332
333// Create a list of partitions on device(s).
334 if (gScanSingleDrive)
335{
336scanBootVolumes(gBIOSDev, &bvCount);
337 }
338else
339#endif
340{
341scanDisks(gBIOSDev, &bvCount);
342 }
343
344// Create a separated bvr chain using the specified filters.
345 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
346gBootVolume = selectBootVolume(bvChain);
347
348#if DEBUG
349 printf("separated bvChain dev=%02x type=%02x part_no=%d\n", bvChain->biosdev, bvChain->type, bvChain->part_no); //dev=0x81 - HDD
350pause();
351#endif
352
353// Intialize module system
354if(init_module_system())
355{
356#if DEBUG
357printf("begin load_all_modules\n");
358pause();
359#endif
360
361load_all_modules();
362}
363
364execute_hook("ModulesLoaded", NULL, NULL, NULL, NULL);
365#if DEBUG
366 printf("ModulesLoaded\n");
367pause();
368#endif
369
370
371#ifndef OPTION_ROM
372 // Loading preboot ramdisk if exists.
373 loadPrebootRAMDisk();
374
375 // Disable rescan option by default
376 gEnableCDROMRescan = false;
377
378 // Enable it with Rescan=y in system config
379 if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan) {
380 gEnableCDROMRescan = true;
381 }
382
383 // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
384 rescanPrompt = false;
385 if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {
386 gEnableCDROMRescan = promptForRescanOption();
387 }
388#endif
389
390#if DEBUG
391 printf("After rescan\n");
392pause();
393#endif
394
395
396
397#if DEBUG
398 verbose(" Default: %x, ->biosdev: %x, ->part_no: %d ->flags: %x\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
399 verbose(" bt(0,0): %x, ->biosdev: %x, ->part_no: %d ->flags: %x\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
400 // getc();
401/* Results
402 Rescan found 1 HDD and bvCount=4
403 bvr: 836bc10, dev: 80, part: 4, flags: 4a, vis: 1
404 bvr: 836bce0, dev: 80, part: 3, flags: 4b, vis: 1
405 bvr: 836be90, dev: 80, part: 2, flags: 4, vis: 1
406 bvr: 836bf60, dev: 80, part: 1, flags: a, vis: 0
407 count: 3
408
409 foundPrimary and flags bvr1=a bvr2=4b
410 Default: 836bce0, ->biosdev: 80, ->part_no: 3 ->flags: 4b //booted partition
411 bt(0,0): 8262030, ->biosdev: 80, ->part_no: 3 ->flags: 4b //unknown pointer
412
413 */
414#endif
415
416
417 setBootGlobals(bvChain);
418
419 // Parse args, load and start kernel.
420 while (1) {
421 const char *val;
422 int len;
423 // int trycache;
424 long flags, cachetime, kerneltime, exttime, sleeptime, time;
425 int ret = -1;
426 void *binary = (void *)kLoadAddr;
427 bool tryresume;
428 bool tryresumedefault;
429 bool forceresume;
430
431 // additional variable for testing alternate kernel image locations on boot helper partitions.
432 char bootFileSpec[512];
433
434 // Initialize globals.
435
436 sysConfigValid = false;
437 gErrors = false;
438 trycache=1;
439
440 status = getBootOptions(firstRun);
441 firstRun = false;
442 if (status == -1) continue;
443
444 status = processBootOptions();
445 // Status==1 means to chainboot
446 if ( status == 1 ) break;
447 // Status==-1 means that the config file couldn't be loaded or that gBootVolume is NULL
448 if ( status == -1 )
449 {
450// gBootVolume == NULL usually means the user hit escape.
451if(gBootVolume == NULL)
452{
453freeFilteredBVChain(bvChain);
454#ifndef OPTION_ROM
455if (gEnableCDROMRescan)
456rescanBIOSDevice(gBIOSDev);
457#endif
458
459bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
460setBootGlobals(bvChain);
461}
462#if DEBUG
463verbose("After rescan\n");
464verbose(" System: %x, ->biosdev: %x, ->part_no: %d ->flags: %x\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
465verbose(" Booter: %x, ->biosdev: %x, ->part_no: %d ->flags: %x\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
466// getc();
467#endif
468
469continue;
470 }
471
472
473
474 // Other status (e.g. 0) means that we should proceed with boot.
475
476
477// Find out which version mac os we're booting.
478getOSVersion(gMacOSVersion);
479
480if (platformCPUFeature(CPU_FEATURE_EM64T)) {
481archCpuType = CPU_TYPE_X86_64;
482} else {
483archCpuType = CPU_TYPE_I386;
484}
485if (getValueForKey(karch, &val, &len, &bootInfo->bootConfig)) {
486if (strncmp(val, "i386", 4) == 0) {
487archCpuType = CPU_TYPE_I386;
488}
489}
490
491// Notify modules that we are attempting to boot
492execute_hook("PreBoot", NULL, NULL, NULL, NULL);
493
494if (!getBoolForKey (kWake, &tryresume, &bootInfo->bootConfig)) {
495tryresume = true;
496tryresumedefault = true;
497} else {
498tryresumedefault = false;
499}
500
501if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->bootConfig)) {
502forceresume = false;
503}
504
505if (forceresume) {
506tryresume = true;
507tryresumedefault = false;
508}
509
510while (tryresume) {
511const char *tmp;
512BVRef bvr;
513if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->bootConfig))
514val="/private/var/vm/sleepimage";
515
516// Do this first to be sure that root volume is mounted
517ret = GetFileInfo(0, val, &flags, &sleeptime);
518
519if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
520break;
521
522// Can't check if it was hibernation Wake=y is required
523if (bvr->modTime == 0 && tryresumedefault)
524break;
525
526if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
527break;
528
529if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
530printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",bvr->modTime-sleeptime);
531break;
532}
533
534HibernateBoot((char *)val);
535break;
536}
537
538 // Reset cache name.
539 bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
540
541 sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
542
543 adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
544
545 if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig) == true) {
546 strlcpy(gBootKernelCacheFile, val, len+1);
547 } else {
548if(gMacOSVersion[3] == '6')
549sprintf(gBootKernelCacheFile, "%s_%s.%08lX", kDefaultCachePathSnow, (archCpuType == CPU_TYPE_I386) ? "i386" : "x86_64", adler32);
550else if(gMacOSVersion[3] == '5')
551sprintf(gBootKernelCacheFile, "%s", kDefaultCachePath);
552else
553sprintf(gBootKernelCacheFile, "%s", kDefaultCachePathTiger);
554 }
555verbose("Try cache %s\n", gBootKernelCacheFile);
556 // Check for cache file.
557 trycache = (((gBootMode & kBootModeSafe) == 0) &&
558 !gOverrideKernel &&
559 (gBootFileType == kBlockDeviceType) &&
560 (gMKextName[0] == '\0') &&
561 (gBootKernelCacheFile[0] != '\0') &&
562(gMacOSVersion[3] != '4'));
563
564verbose("Loading Darwin %s\n", gMacOSVersion);
565
566 if (trycache) do {
567
568 // if we haven't found the kernel yet, don't use the cache
569 ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
570 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
571 trycache = 0;
572 break;
573 }
574 ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
575 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
576 || (cachetime < kerneltime)) {
577 trycache = 0;
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: 676