Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 353