Chameleon

Chameleon Svn Source Tree

Root/branches/valv/i386/boot2/boot.c

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

Archive Download this file

Revision: 709