Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 567