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

Archive Download this file

Revision: 602